blob: a2b7df2f8b57b940714a1369cbaafa3c78dce4ea [file] [log] [blame]
Tim Windelschmidt98000a52025-03-06 14:22:15 +01001load("@rules_foreign_cc//toolchains/native_tools:native_tools_toolchain.bzl", "native_tool_toolchain")
2
3# Copied from bazel-contrib/rules_foreign_cc licensed under Apache-2.0
4def _current_toolchain_impl(ctx):
5 toolchain = ctx.toolchains[ctx.attr._toolchain]
6
7 if toolchain.data.target:
8 return [
9 toolchain,
10 platform_common.TemplateVariableInfo(toolchain.data.env),
11 DefaultInfo(
12 files = toolchain.data.target.files,
13 runfiles = toolchain.data.target.default_runfiles,
14 ),
15 ]
16 return [
17 toolchain,
18 platform_common.TemplateVariableInfo(toolchain.data.env),
19 DefaultInfo(),
20 ]
21
22def current_toolchain(name):
23 return rule(
24 implementation = _current_toolchain_impl,
25 attrs = {
26 "_toolchain": attr.string(default = "//build/toolchain/toolchain-bundle:%s_toolchain" % name),
27 },
28 toolchains = [
29 "//build/toolchain/toolchain-bundle:%s_toolchain" % name,
30 ],
31 )
32
33def toolchain_for(name, config):
34 native.toolchain_type(
35 name = "%s_toolchain" % name,
36 )
37
38 config.current_toolchain_func(
39 name = name,
40 )
41
42 native.toolchain(
43 name = "%s_linux_x86_64_toolchain" % name,
44 exec_compatible_with = [
45 "@platforms//os:linux",
46 "@platforms//cpu:x86_64",
47 ],
48 toolchain = ":%s_linux_x86_64" % name,
49 toolchain_type = ":%s_toolchain" % name,
50 )
51
52 native.toolchain(
53 name = "%s_linux_aarch64_toolchain" % name,
54 exec_compatible_with = [
55 "@platforms//os:linux",
56 "@platforms//cpu:aarch64",
57 ],
58 toolchain = ":%s_linux_aarch64" % name,
59 toolchain_type = ":%s_toolchain" % name,
60 )
61
62 native_tool_toolchain(
63 name = "%s_linux_aarch64" % name,
64 env = {
65 name.upper(): "$(execpath @toolchain-bundle-aarch64-unknown-linux-musl//:%s)" % config.target,
66 },
67 target = "@toolchain-bundle-aarch64-unknown-linux-musl//:%s" % config.target,
68 )
69
70 native_tool_toolchain(
71 name = "%s_linux_x86_64" % name,
72 env = {
73 name.upper(): "$(execpath @toolchain-bundle-x86_64-unknown-linux-musl//:%s)" % config.target,
74 },
75 target = "@toolchain-bundle-x86_64-unknown-linux-musl//:%s" % config.target,
76 )
77
78current_qemu_img_toolchain = current_toolchain("qemu-img")
79current_qemu_kvm_toolchain = current_toolchain("qemu-kvm")
80current_make_toolchain = current_toolchain("make")
81current_strace_toolchain = current_toolchain("strace")
82current_nasm_toolchain = current_toolchain("nasm")
83current_bison_toolchain = current_toolchain("bison")
84current_flex_toolchain = current_toolchain("flex")
85current_m4_toolchain = current_toolchain("m4")
86current_bc_toolchain = current_toolchain("bc")
87current_busybox_toolchain = current_toolchain("busybox")
88current_diff_toolchain = current_toolchain("diff")
89current_perl_toolchain = current_toolchain("perl")
90current_iasl_toolchain = current_toolchain("iasl")
91current_lz4_toolchain = current_toolchain("lz4")
92
93TOOLCHAINS = {
94 "qemu-img": struct(
95 target = "bin/qemu-img",
96 current_toolchain_func = current_qemu_img_toolchain,
97 ),
98 "qemu-kvm": struct(
99 target = "qemu-kvm",
100 current_toolchain_func = current_qemu_kvm_toolchain,
101 ),
102 "make": struct(
103 target = "bin/make",
104 current_toolchain_func = current_make_toolchain,
105 ),
106 "strace": struct(
107 target = "bin/strace",
108 current_toolchain_func = current_strace_toolchain,
109 ),
110 "nasm": struct(
111 target = "bin/nasm",
112 current_toolchain_func = current_nasm_toolchain,
113 ),
114 "bison": struct(
115 target = "bison",
116 current_toolchain_func = current_bison_toolchain,
117 ),
118 "flex": struct(
119 target = "bin/flex",
120 current_toolchain_func = current_flex_toolchain,
121 ),
122 "m4": struct(
123 target = "bin/m4",
124 current_toolchain_func = current_m4_toolchain,
125 ),
126 "bc": struct(
127 target = "bin/bc",
128 current_toolchain_func = current_bc_toolchain,
129 ),
130 "diff": struct(
131 target = "bin/diff",
132 current_toolchain_func = current_diff_toolchain,
133 ),
134 "iasl": struct(
135 target = "bin/iasl",
136 current_toolchain_func = current_iasl_toolchain,
137 ),
138 "busybox": struct(
139 target = "busybox",
140 current_toolchain_func = current_busybox_toolchain,
141 ),
142 "perl": struct(
143 target = "perl",
144 current_toolchain_func = current_perl_toolchain,
145 ),
146 "lz4": struct(
147 target = "bin/lz4",
148 current_toolchain_func = current_lz4_toolchain,
149 ),
150}
151
152def build_toolchain_env(ctx, toolchains):
153 toolchain_info = [ctx.toolchains[t] for t in toolchains]
154 env = dict([(k, v) for t in toolchain_info for k, v in t.data.env.items()])
155 env = env | {"TOOL_PATH": ":".join([t.data.target.files.to_list()[0].path.rsplit("/", 1)[0] for t in toolchain_info])}
156
157 inputs = depset(transitive = [
158 depset(transitive = [t.data.target.files, t.data.target.default_runfiles.files])
159 for t in toolchain_info
160 ])
161
162 return env, inputs
163
164TOOLCHAIN_ENV_SETUP = """
165set -e
166
167# Iterate over all environment variables and expand paths that are
168# either external or bazel-out.
169for name in $(env | cut -d= -f1); do
170 val="${!name}"
171 [[ "$val" != *external/* && "$val" != *bazel-out/* ]] && continue # Quick skip
172
173 sep=' '; [[ $name == "TOOL_PATH" ]] && sep=':' # Set separator: : for PATH, space otherwise
174 IFS=$sep read -r -a items <<< "$val" # Split value into array using correct separator
175
176 for i in "${!items[@]}"; do
177 key="${items[i]%%=*}"; v="${items[i]#*=}" # Handle 'key=val' and standalone paths
178 if [[ ( $v == external/* || $v == bazel-out/* ) && -e "$v" ]]; then
179 [ "$key" = "$v" ] && items[i]=$(realpath -s "$v") || items[i]="$key=$(realpath -s "$v")"
180 fi
181 done
182 export "$name=$(IFS=$sep; echo "${items[*]}")" # Re-export with correct separator
183done
184
185# Add our now expanded TOOL_PATH to PATH
186PATH="$PATH:$TOOL_PATH"
187
188"""