1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright 2018 The ChromiumOS Authors 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7"""Create llvm ebuild file. 8 9This script takes an existing host llvm compiler ebuild and 10creates another build that should be installable in a prefixed location. 11The script patches a few lines in the llvm ebuild to make that happen. 12 13Since the script is based on the current llvm ebuild patterns, 14it may need to be updated if those patterns change. 15 16This script should normally be invoked by the shell script 17create_llvm_extra.sh . 18 19Below is an example of the expected diff of the newly generated ebuild with 20some explanation of the diffs. 21 22diff -Nuar llvm-pre7.0_pre335547_p20180529.ebuild newly-created-file.ebuild 23--- llvm-7.0_pre331547_p20180529-r8.ebuild 24+++ newly-created-file.ebuild 25 26@@ -60,9 +60,9 @@ EGIT_REPO_URIS=( 27 fi 28 29 LICENSE="UoI-NCSA" 30-SLOT="0/${PV%%_*}" 31+SLOT="${PV%%_p[[:digit:]]*}" # Creates a unique slot so that multiple copies 32 of the new build can be installed. 33 34 KEYWORDS="-* amd64" 35 36 # Change USE flags to match llvm ebuild installtion. To see the set of flags 37 enabled in llvm compiler ebuild, run $ sudo emerge -pv llvm 38 39-IUSE="debug +default-compiler-rt +default-libcxx doc libedit +libffi" 40+IUSE="debug +default-compiler-rt +default-libcxx doc libedit +libffi 41 ncurses ocaml python llvm-next llvm-tot test xml video_cards_radeon" 42 43 COMMON_DEPEND=" 44@@ -145,6 +145,7 @@ pkg_pretend() { 45 } 46 47 pkg_setup() { 48 # This Change is to install the files in $PREFIX. 49+ export PREFIX="/usr/${PN}/${SLOT}" 50 pkg_pretend 51 } 52 53@@ -272,13 +273,13 @@ 54 sed -e "/RUN/s/-warn-error A//" -i test/Bindings/OCaml/*ml || die 55 56 # Allow custom cmake build types (like 'Gentoo') 57 # Convert use of PN to llvm in epatch commands. 58- epatch "${FILESDIR}"/cmake/${PN}-3.8-allow_custom_cmake_build.patch 59+ epatch "${FILESDIR}"/cmake/llvm-3.8-allow_custom_cmake_build.patch 60 61 # crbug/591436 62 epatch "${FILESDIR}"/clang-executable-detection.patch 63 64 # crbug/606391 65- epatch "${FILESDIR}"/${PN}-3.8-invocation.patch 66+ epatch "${FILESDIR}"/llvm-3.8-invocation.patch 67 68@@ -411,11 +412,14 @@ src_install() { 69 /usr/include/llvm/Config/llvm-config.h 70 ) 71 72+ MULTILIB_CHOST_TOOLS=() # No need to install any multilib tools/headers. 73+ MULTILIB_WRAPPED_HEADERS=() 74 multilib-minimal_src_install 75 } 76 77 multilib_src_install() { 78 cmake-utils_src_install 79+ return # No need to install any wrappers. 80 81 local wrapper_script=clang_host_wrapper 82 cat "${FILESDIR}/clang_host_wrapper.header" \ 83@@ -434,6 +438,7 @@ multilib_src_install() { 84 } 85 86 multilib_src_install_all() { 87+ return # No need to install common multilib files. 88 insinto /usr/share/vim/vimfiles 89 doins -r utils/vim/*/. 90 # some users may find it useful 91""" 92 93 94import os 95import sys 96 97 98def process_line(line, text): 99 # Process the line and append to the text we want to generate. 100 # Check if line has any patterns that we want to handle. 101 newline = line.strip() 102 if newline.startswith("#"): 103 # Do not process comment lines. 104 text.append(line) 105 elif line.startswith("SLOT="): 106 # Change SLOT to "${PV%%_p[[:digit:]]*}" 107 SLOT_STRING = 'SLOT="${PV%%_p[[:digit:]]*}"\n' 108 text.append(SLOT_STRING) 109 elif line.startswith("IUSE") and "multitarget" in line: 110 # Enable multitarget USE flag. 111 newline = line.replace("multitarget", "+multitarget") 112 text.append(newline) 113 elif line.startswith("pkg_setup()"): 114 # Setup PREFIX. 115 text.append(line) 116 text.append('\texport PREFIX="/usr/${PN}/${SLOT}"\n') 117 elif line.startswith("multilib_src_install_all()"): 118 text.append(line) 119 # Do not install any common files. 120 text.append("\treturn\n") 121 elif "epatch " in line: 122 # Convert any $PN or ${PN} in epatch files to llvm. 123 newline = line.replace("$PN", "llvm") 124 newline = newline.replace("${PN}", "llvm") 125 text.append(newline) 126 elif "multilib-minimal_src_install" in line: 127 # Disable MULTILIB_CHOST_TOOLS and MULTILIB_WRAPPED_HEADERS 128 text.append("\tMULTILIB_CHOST_TOOLS=()\n") 129 text.append("\tMULTILIB_WRAPPED_HEADERS=()\n") 130 text.append(line) 131 elif "cmake-utils_src_install" in line: 132 text.append(line) 133 # Do not install any wrappers. 134 text.append("\treturn\n") 135 else: 136 text.append(line) 137 138 139def main(): 140 if len(sys.argv) != 3: 141 filename = os.path.basename(__file__) 142 print("Usage: ", filename, " <input.ebuild> <output.ebuild>") 143 return 1 144 145 text = [] 146 with open(sys.argv[1], "r") as infile: 147 for line in infile: 148 process_line(line, text) 149 150 with open(sys.argv[2], "w") as outfile: 151 outfile.write("".join(text)) 152 153 return 0 154 155 156if __name__ == "__main__": 157 sys.exit(main()) 158