aboutsummaryrefslogtreecommitdiffstats
path: root/classes/bazel.bbclass
blob: 59b78e1a698bf9f17a18bdaa6c0ea84f32d027a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
DEPENDS += "bazel-native \
           openjdk-11-native \
          "
DEPENDS:append:class-target = " python3"

inherit bazel-base

BAZEL_DIR ?= "${WORKDIR}/bazel"
BAZEL_OUTPUTBASE_DIR ?= "${BAZEL_DIR}/output_base"
export USER="unused-bazel-user"
export BAZEL_STARTUP_OPTIONS="--output_user_root=${BAZEL_DIR}/user_root \
                   --output_base=${BAZEL_OUTPUTBASE_DIR} \
                   --bazelrc=${S}/bazelrc \
                   --batch  \
                  "

BAZEL ?= "${BAZEL_DIR}/bazel"

do_prepare_recipe_sysroot[postfuncs] += "do_install_bazel"
do_install_bazel() {
    mkdir -p ${BAZEL_DIR}
    install -m 0755 ${STAGING_BINDIR_NATIVE}/bazel ${BAZEL_DIR}
    create_cmdline_wrapper ${BAZEL} \$BAZEL_STARTUP_OPTIONS
    zip -A ${BAZEL}.real
}

def bazel_get_target_flags(d):
    flags = ""
    for i in d.getVar("CC").split()[1:]:
        flags += "# From CC\n"
        flags += "build --conlyopt=%s --cxxopt=%s --linkopt=%s\n" % (i, i, i)

    for i in d.getVar("CFLAGS").split():
        if i == "-g":
            continue
        flags += "# From CFLAGS\n"
        flags += "build --conlyopt=%s\n" % i

    for i in d.getVar("BUILD_CFLAGS").split():
        flags += "# From BUILD_CFLAGS\n"
        flags += "build --host_conlyopt=%s\n" % i

    for i in d.getVar("CXXFLAGS").split():
        if i == "-g":
            continue
        flags += "# From CXXFLAGS\n"
        flags += "build --cxxopt=%s\n" % i

    for i in d.getVar("BUILD_CXXFLAGS").split():
        flags += "# From BUILD_CXXFLAGS\n"
        flags += "build --host_cxxopt=%s\n" % i

    for i in d.getVar("CPPFLAGS").split():
        if i == "-g":
            continue
        flags += "# From CPPFLAGS\n"
        flags += "build --conlyopt=%s --cxxopt=%s\n" % (i, i)

    for i in d.getVar("BUILD_CPPFLAGS").split():
        flags += "# From BUILD_CPPFLAGS\n"
        flags += "build --host_conlyopt=%s --host_cxxopt=%s\n" % (i, i)

    for i in d.getVar("LDFLAGS").split():
        if i == "-Wl,--as-needed":
            continue
        flags += "# From LDFLAGS\n"
        flags += "build --linkopt=%s\n" % i

    for i in d.getVar("BUILD_LDFLAGS").split():
        if i == "-Wl,--as-needed":
            continue
        flags += "# From BUILD_LDFLAGS\n"
        flags += "build --host_linkopt=%s\n" % i

    for i in d.getVar("TOOLCHAIN_OPTIONS").split():
        if i == "-Wl,--as-needed":
            continue
        flags += "# From TOOLCHAIN_OPTIONS\n"
        flags += "build --linkopt=%s\n" % i

    return flags

def bazel_get_flags(d):
    flags = ""

    if d.getVar("BAZEL_JOBS"):
        flags += "# From BAZEL_JOBS\n"
        flags += "build --jobs=%s --local_cpu_resources=%s\n" % (d.getVar("BAZEL_JOBS"), d.getVar("BAZEL_JOBS"))

    if d.getVar("BAZEL_MEM"):
        flags += "# From BAZEL_MEM\n"
        flags += "build --local_ram_resources=%s\n" % (d.getVar("BAZEL_MEM"))

    return flags

bazel_do_configure () {
    cat > "${S}/bazelrc" <<-EOF
build --verbose_failures
build --spawn_strategy=standalone --genrule_strategy=standalone
test --verbose_failures --verbose_test_summary
test --spawn_strategy=standalone --genrule_strategy=standalone

build --linkopt=-Wl,--no-as-needed
build --host_linkopt=-Wl,--no-as-needed

build --host_conlyopt=-D_PYTHON_INCLUDE_NATIVE --host_cxxopt=-D_PYTHON_INCLUDE_NATIVE
build --conlyopt=-D_PYTHON_INCLUDE_TARGET --cxxopt=-D_PYTHON_INCLUDE_TARGET

build --strip=never

build --python_path=python3

fetch --distdir=${TS_DL_DIR}
build --distdir=${TS_DL_DIR}

${@bazel_get_flags(d)}

EOF

}

bazel_do_configure:append:class-target () {
    cat >> "${S}/bazelrc" <<-EOF
# FLAGS begin
${@bazel_get_target_flags(d)}
# FLAGS end

EOF

    sed -i "s:${WORKDIR}:${BAZEL_OUTPUTBASE_DIR}/external/yocto_compiler:g" ${S}/bazelrc

    # Unzip bazel packages
    ${BAZEL} ${BAZEL_STARTUP_OPTIONS} version

    for binary in build-runfiles daemonize linux-sandbox process-wrapper; do
        # Modify interpreter for bazel built-in binaries
        patchelf-uninative --set-interpreter "${UNINATIVE_LOADER}" ${BAZEL_DIR}/user_root/install/*/$binary

        # Set modification time somewhere in the future to avoid "corrupt installation: file PATH is missing or modified"
        # in this case modification is required for successful build
        touch -m -t 203712120101 ${BAZEL_DIR}/user_root/install/*/$binary
    done
}

EXPORT_FUNCTIONS do_configure

PSEUDO_IGNORE_PATHS .= ",${WORKDIR}/bazel"

inherit unsupportarch

export YOCTO_NATIVE_SYSROOT="${BAZEL_OUTPUTBASE_DIR}/external/yocto_compiler/recipe-sysroot-native"

do_rm_work[prefuncs] += "clean_bazel"
do_clean[prefuncs] += "clean_bazel"
clean_bazel() {
    if [ -d ${S} ]; then
        cd ${S}
        if [ -e ${BAZEL} ] && [ -e ${S}/bazelrc ]; then
            ${BAZEL} clean
        fi
    fi
    rm ${BAZEL_DIR} -rf
}

do_compile[network] = "1"