-
-
Notifications
You must be signed in to change notification settings - Fork 219
/
cc_toolchain_config.bzl
385 lines (357 loc) · 14.4 KB
/
cc_toolchain_config.bzl
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# Copyright 2021 The Bazel Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
load(
"@bazel_tools//tools/cpp:unix_cc_toolchain_config.bzl",
unix_cc_toolchain_config = "cc_toolchain_config",
)
load(
"//toolchain/internal:common.bzl",
_check_os_arch_keys = "check_os_arch_keys",
_os_arch_pair = "os_arch_pair",
)
# Bazel 4.* doesn't support nested starlark functions, so we cannot simplify
# _fmt_flags() by defining it as a nested function.
def _fmt_flags(flags, toolchain_path_prefix):
return [f.format(toolchain_path_prefix = toolchain_path_prefix) for f in flags]
# Macro for calling cc_toolchain_config from @bazel_tools with setting the
# right paths and flags for the tools.
def cc_toolchain_config(
name,
exec_arch,
exec_os,
target_arch,
target_os,
target_system_name,
toolchain_path_prefix,
tools_path_prefix,
wrapper_bin_prefix,
compiler_configuration,
cxx_builtin_include_directories,
major_llvm_version):
exec_os_arch_key = _os_arch_pair(exec_os, exec_arch)
target_os_arch_key = _os_arch_pair(target_os, target_arch)
_check_os_arch_keys([exec_os_arch_key, target_os_arch_key])
# A bunch of variables that get passed straight through to
# `create_cc_toolchain_config_info`.
# TODO: What do these values mean, and are they actually all correct?
(
toolchain_identifier,
target_cpu,
target_libc,
compiler,
abi_version,
abi_libc_version,
) = {
"darwin-x86_64": (
"clang-x86_64-darwin",
"darwin",
"macosx",
"clang",
"darwin_x86_64",
"darwin_x86_64",
),
"darwin-aarch64": (
"clang-aarch64-darwin",
"darwin",
"macosx",
"clang",
"darwin_aarch64",
"darwin_aarch64",
),
"linux-aarch64": (
"clang-aarch64-linux",
"aarch64",
"glibc_unknown",
"clang",
"clang",
"glibc_unknown",
),
"linux-x86_64": (
"clang-x86_64-linux",
"k8",
"glibc_unknown",
"clang",
"clang",
"glibc_unknown",
),
"wasm32": (
"clang-wasm32",
"wasm32",
"unknown",
"clang",
"unknown",
"unknown",
),
"wasm64": (
"clang-wasm64",
"wasm64",
"unknown",
"clang",
"unknown",
"unknown",
),
}[target_os_arch_key]
# Unfiltered compiler flags; these are placed at the end of the command
# line, so take precendence over any user supplied flags through --copts or
# such.
unfiltered_compile_flags = [
# Do not resolve our symlinked resource prefixes to real paths.
"-no-canonical-prefixes",
# Reproducibility
"-Wno-builtin-macro-redefined",
"-D__DATE__=\"redacted\"",
"-D__TIMESTAMP__=\"redacted\"",
"-D__TIME__=\"redacted\"",
"-fdebug-prefix-map={}=__bazel_toolchain_llvm_repo__/".format(toolchain_path_prefix),
]
is_xcompile = not (exec_os == target_os and exec_arch == target_arch)
# Default compiler flags:
compile_flags = [
"--target=" + target_system_name,
# Security
"-U_FORTIFY_SOURCE", # https://github.com/google/sanitizers/issues/247
"-fstack-protector",
"-fno-omit-frame-pointer",
# Diagnostics
"-fcolor-diagnostics",
"-Wall",
"-Wthread-safety",
"-Wself-assign",
]
dbg_compile_flags = ["-g", "-fstandalone-debug"]
opt_compile_flags = [
"-g0",
"-O2",
"-D_FORTIFY_SOURCE=1",
"-DNDEBUG",
"-ffunction-sections",
"-fdata-sections",
]
link_flags = [
"--target=" + target_system_name,
"-no-canonical-prefixes",
]
stdlib = compiler_configuration["stdlib"]
if stdlib != "none":
link_flags.extend([
"-lm",
])
# Similar to link_flags, but placed later in the command line such that
# unused symbols are not stripped.
link_libs = []
libunwind_link_flags = []
compiler_rt_link_flags = []
# Flags for ar.
archive_flags = []
# Linker flags:
if exec_os == "darwin" and not is_xcompile:
# lld is experimental for Mach-O, so we use the native ld64 linker.
# TODO: How do we cross-compile from Linux to Darwin?
use_lld = False
link_flags.extend([
"-headerpad_max_install_names",
"-fobjc-link-runtime",
])
# Use the bundled libtool (llvm-libtool-darwin).
use_libtool = True
# Pre-installed libtool on macOS has -static as default, but llvm-libtool-darwin needs it
# explicitly. cc_common.create_link_variables does not automatically add this either if
# output_file arg to it is None.
archive_flags.extend([
"-static",
])
elif target_arch in ["wasm32", "wasm64"]:
# lld is invoked as wasm-ld for WebAssembly targets.
use_lld = True
use_libtool = False
else:
# Note that for xcompiling from darwin to linux, the native ld64 is
# not an option because it is not a cross-linker, so lld is the
# only option.
use_lld = True
link_flags.extend([
"-fuse-ld=lld",
"-Wl,--build-id=md5",
"-Wl,--hash-style=gnu",
"-Wl,-z,relro,-z,now",
])
use_libtool = False
# Flags related to C++ standard.
# The linker has no way of knowing if there are C++ objects; so we
# always link C++ libraries.
cxx_standard = compiler_configuration["cxx_standard"]
sysroot_path = compiler_configuration["sysroot_path"]
if stdlib == "builtin-libc++" and is_xcompile:
stdlib = "stdc++"
if stdlib == "builtin-libc++":
cxx_flags = [
"-std=" + cxx_standard,
"-stdlib=libc++",
]
if major_llvm_version >= 14:
# With C++20, Clang defaults to using C++ rather than Clang modules,
# which breaks Bazel's `use_module_maps` feature, which is used by
# `layering_check`. Since Bazel doesn't support C++ modules yet, it
# is safe to disable them globally until the toolchain shipped by
# Bazel sets this flag on `use_module_maps`.
# https://github.com/llvm/llvm-project/commit/0556138624edf48621dd49a463dbe12e7101f17d
cxx_flags.append("-Xclang")
cxx_flags.append("-fno-cxx-modules")
if use_lld:
# For single-platform builds, we can statically link the bundled
# libraries.
link_flags.extend([
"-l:libc++.a",
"-l:libc++abi.a",
])
compiler_rt_link_flags = ["-rtlib=compiler-rt"]
libunwind_link_flags = [
"-l:libunwind.a",
# To support libunwind.
"-lpthread",
"-ldl",
]
else:
# Several system libraries on macOS dynamically link libc++ and
# libc++abi, so static linking them becomes a problem. We need to
# ensure that they are dynamic linked from the system sysroot and
# not static linked from the toolchain, so explicitly have the
# sysroot directory on the search path and then add the toolchain
# directory back after we are done.
link_flags.extend([
"-L{}/usr/lib".format(sysroot_path),
"-lc++",
"-lc++abi",
"-Bdynamic",
"-L{}lib".format(toolchain_path_prefix),
])
libunwind_link_flags = [
"-Bstatic",
"-lunwind",
]
elif stdlib == "libc++":
cxx_flags = [
"-std=" + cxx_standard,
"-stdlib=libc++",
]
link_flags.extend([
"-l:c++.a",
"-l:c++abi.a",
])
elif stdlib == "stdc++":
cxx_flags = [
"-std=" + cxx_standard,
"-stdlib=libstdc++",
]
link_flags.extend([
"-l:libstdc++.a",
])
elif stdlib == "libc":
cxx_flags = [
"-std=" + cxx_standard,
]
elif stdlib == "none":
cxx_flags = [
"-nostdlib",
]
link_flags.extend([
"-nostdlib",
])
else:
fail("Unknown value passed for stdlib: {stdlib}".format(stdlib = stdlib))
opt_link_flags = ["-Wl,--gc-sections"] if target_os == "linux" else []
# Coverage flags:
coverage_compile_flags = ["-fprofile-instr-generate", "-fcoverage-mapping"]
coverage_link_flags = ["-fprofile-instr-generate"]
## NOTE: framework paths is missing here; unix_cc_toolchain_config
## doesn't seem to have a feature for this.
## NOTE: make variables are missing here; unix_cc_toolchain_config doesn't
## pass these to `create_cc_toolchain_config_info`.
# The requirements here come from
# https://cs.opensource.google/bazel/bazel/+/master:src/main/starlark/builtins_bzl/common/cc/cc_toolchain_provider_helper.bzl;l=75;drc=f0150efd1cca473640269caaf92b5a23c288089d
# https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java;l=1257;drc=6743d76f9ecde726d592e88d8914b9db007b1c43
# https://cs.opensource.google/bazel/bazel/+/refs/tags/7.0.0:tools/cpp/unix_cc_toolchain_config.bzl;l=192,201;drc=044a14cca2747aeff258fc71eaeb153c08cb34d5
# NOTE: Ensure these are listed in toolchain_tools in toolchain/internal/common.bzl.
tool_paths = {
"ar": tools_path_prefix + ("llvm-ar" if not use_libtool else "libtool"),
"cpp": tools_path_prefix + "clang-cpp",
"dwp": tools_path_prefix + "llvm-dwp",
"gcc": wrapper_bin_prefix + "cc_wrapper.sh",
"gcov": tools_path_prefix + "llvm-profdata",
"ld": tools_path_prefix + "ld.lld" if use_lld else "/usr/bin/ld",
"llvm-cov": tools_path_prefix + "llvm-cov",
"llvm-profdata": tools_path_prefix + "llvm-profdata",
"nm": tools_path_prefix + "llvm-nm",
"objcopy": tools_path_prefix + "llvm-objcopy",
"objdump": tools_path_prefix + "llvm-objdump",
"strip": tools_path_prefix + "llvm-strip",
}
# Start-end group linker support:
# This was added to `lld` in this patch: http://reviews.llvm.org/D18814
#
# The oldest version of LLVM that we support is 6.0.0 which was released
# after the above patch was merged, so we just set this to `True` when
# `lld` is being used as the linker.
supports_start_end_lib = use_lld
# Replace flags with any user-provided overrides.
if compiler_configuration["compile_flags"] != None:
compile_flags = _fmt_flags(compiler_configuration["compile_flags"], toolchain_path_prefix)
if compiler_configuration["cxx_flags"] != None:
cxx_flags = _fmt_flags(compiler_configuration["cxx_flags"], toolchain_path_prefix)
if compiler_configuration["link_flags"] != None:
link_flags = _fmt_flags(compiler_configuration["link_flags"], toolchain_path_prefix)
if compiler_configuration["archive_flags"] != None:
archive_flags = _fmt_flags(compiler_configuration["archive_flags"], toolchain_path_prefix)
if compiler_configuration["link_libs"] != None:
link_libs = _fmt_flags(compiler_configuration["link_libs"], toolchain_path_prefix)
if compiler_configuration["opt_compile_flags"] != None:
opt_compile_flags = _fmt_flags(compiler_configuration["opt_compile_flags"], toolchain_path_prefix)
if compiler_configuration["opt_link_flags"] != None:
opt_link_flags = _fmt_flags(compiler_configuration["opt_link_flags"], toolchain_path_prefix)
if compiler_configuration["dbg_compile_flags"] != None:
dbg_compile_flags = _fmt_flags(compiler_configuration["dbg_compile_flags"], toolchain_path_prefix)
if compiler_configuration["coverage_compile_flags"] != None:
coverage_compile_flags = _fmt_flags(compiler_configuration["coverage_compile_flags"], toolchain_path_prefix)
if compiler_configuration["coverage_link_flags"] != None:
coverage_link_flags = _fmt_flags(compiler_configuration["coverage_link_flags"], toolchain_path_prefix)
if compiler_configuration["unfiltered_compile_flags"] != None:
unfiltered_compile_flags = _fmt_flags(compiler_configuration["unfiltered_compile_flags"], toolchain_path_prefix)
# Source: https://cs.opensource.google/bazel/bazel/+/master:tools/cpp/unix_cc_toolchain_config.bzl
unix_cc_toolchain_config(
name = name,
cpu = target_cpu,
compiler = compiler,
toolchain_identifier = toolchain_identifier,
host_system_name = exec_arch,
target_system_name = target_system_name,
target_libc = target_libc,
abi_version = abi_version,
abi_libc_version = abi_libc_version,
cxx_builtin_include_directories = cxx_builtin_include_directories,
tool_paths = tool_paths,
compile_flags = compile_flags,
dbg_compile_flags = dbg_compile_flags,
opt_compile_flags = opt_compile_flags,
cxx_flags = cxx_flags,
link_flags = link_flags + select({str(Label("@toolchains_llvm//toolchain/config:use_libunwind")): libunwind_link_flags, "//conditions:default": []}) +
select({str(Label("@toolchains_llvm//toolchain/config:use_compiler_rt")): compiler_rt_link_flags, "//conditions:default": []}),
archive_flags = archive_flags,
link_libs = link_libs,
opt_link_flags = opt_link_flags,
unfiltered_compile_flags = unfiltered_compile_flags,
coverage_compile_flags = coverage_compile_flags,
coverage_link_flags = coverage_link_flags,
supports_start_end_lib = supports_start_end_lib,
builtin_sysroot = sysroot_path,
)