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