@@ -37,11 +37,12 @@ def cc_toolchain_config(
37
37
target_os ,
38
38
target_system_name ,
39
39
toolchain_path_prefix ,
40
+ target_toolchain_path_prefix ,
40
41
tools_path_prefix ,
41
42
wrapper_bin_prefix ,
42
43
compiler_configuration ,
43
44
cxx_builtin_include_directories ,
44
- major_llvm_version ):
45
+ llvm_version ):
45
46
exec_os_arch_key = _os_arch_pair (exec_os , exec_arch )
46
47
target_os_arch_key = _os_arch_pair (target_os , target_arch )
47
48
_check_os_arch_keys ([exec_os_arch_key , target_os_arch_key ])
@@ -56,6 +57,7 @@ def cc_toolchain_config(
56
57
compiler ,
57
58
abi_version ,
58
59
abi_libc_version ,
60
+ multiarch ,
59
61
) = {
60
62
"darwin-x86_64" : (
61
63
"clang-x86_64-darwin" ,
@@ -64,6 +66,7 @@ def cc_toolchain_config(
64
66
"clang" ,
65
67
"darwin_x86_64" ,
66
68
"darwin_x86_64" ,
69
+ None ,
67
70
),
68
71
"darwin-aarch64" : (
69
72
"clang-aarch64-darwin" ,
@@ -72,6 +75,7 @@ def cc_toolchain_config(
72
75
"clang" ,
73
76
"darwin_aarch64" ,
74
77
"darwin_aarch64" ,
78
+ None ,
75
79
),
76
80
"linux-aarch64" : (
77
81
"clang-aarch64-linux" ,
@@ -80,6 +84,7 @@ def cc_toolchain_config(
80
84
"clang" ,
81
85
"clang" ,
82
86
"glibc_unknown" ,
87
+ "aarch64-linux-gnu" ,
83
88
),
84
89
"linux-x86_64" : (
85
90
"clang-x86_64-linux" ,
@@ -88,6 +93,7 @@ def cc_toolchain_config(
88
93
"clang" ,
89
94
"clang" ,
90
95
"glibc_unknown" ,
96
+ "x86_64-linux-gnu" ,
91
97
),
92
98
"wasm32" : (
93
99
"clang-wasm32" ,
@@ -96,6 +102,7 @@ def cc_toolchain_config(
96
102
"clang" ,
97
103
"unknown" ,
98
104
"unknown" ,
105
+ None ,
99
106
),
100
107
"wasm64" : (
101
108
"clang-wasm64" ,
@@ -104,6 +111,7 @@ def cc_toolchain_config(
104
111
"clang" ,
105
112
"unknown" ,
106
113
"unknown" ,
114
+ None ,
107
115
),
108
116
}[target_os_arch_key ]
109
117
@@ -120,12 +128,30 @@ def cc_toolchain_config(
120
128
"-D__TIME__=\" redacted\" " ,
121
129
"-fdebug-prefix-map={}=__bazel_toolchain_llvm_repo__/" .format (toolchain_path_prefix ),
122
130
]
131
+ if target_toolchain_path_prefix != toolchain_path_prefix :
132
+ unfiltered_compile_flags .extend ([
133
+ "-fdebug-prefix-map={}=__bazel_toolchain_llvm_repo__/" .format (target_toolchain_path_prefix ),
134
+ ])
123
135
124
136
is_xcompile = not (exec_os == target_os and exec_arch == target_arch )
125
137
126
- # Default compiler flags:
127
- compile_flags = [
138
+ major_llvm_version = int (llvm_version .split ("." )[0 ])
139
+
140
+ external_include_paths = None
141
+
142
+ resource_dir_version = llvm_version if major_llvm_version < 16 else major_llvm_version
143
+
144
+ resource_dir = [
145
+ "-resource-dir" ,
146
+ "{}lib/clang/{}" .format (target_toolchain_path_prefix , resource_dir_version ),
147
+ ]
148
+
149
+ target_flags = [
128
150
"--target=" + target_system_name ,
151
+ ]
152
+
153
+ # Default compiler flags:
154
+ compile_flags = target_flags + [
129
155
# Security
130
156
"-U_FORTIFY_SOURCE" , # https://github.com/google/sanitizers/issues/247
131
157
"-fstack-protector" ,
@@ -135,22 +161,36 @@ def cc_toolchain_config(
135
161
"-Wall" ,
136
162
"-Wthread-safety" ,
137
163
"-Wself-assign" ,
138
- ]
164
+ "-B{}bin/" .format (toolchain_path_prefix ),
165
+ ] + resource_dir
139
166
140
167
dbg_compile_flags = ["-g" , "-fstandalone-debug" ]
141
168
142
169
opt_compile_flags = [
143
- "-g0" ,
170
+ # Disable debug info
171
+ "-ggdb0" ,
144
172
"-O2" ,
145
173
"-D_FORTIFY_SOURCE=1" ,
146
174
"-DNDEBUG" ,
147
175
"-ffunction-sections" ,
148
176
"-fdata-sections" ,
149
177
]
150
178
151
- link_flags = [
152
- "--target=" + target_system_name ,
179
+ link_flags = target_flags + [
153
180
"-no-canonical-prefixes" ,
181
+ ] + resource_dir
182
+
183
+ # Use the standard libc++ location when not using msan
184
+ non_msan_compile_flags = [
185
+ "-isystem" ,
186
+ target_toolchain_path_prefix + "include/c++/v1/" ,
187
+ "-isystem" ,
188
+ target_toolchain_path_prefix + "include/" + target_system_name + "/c++/v1/" ,
189
+ ]
190
+
191
+ non_msan_link_flags = [
192
+ "-L{}lib" .format (target_toolchain_path_prefix ),
193
+ "-L{}lib/{}" .format (target_toolchain_path_prefix , target_system_name ),
154
194
]
155
195
156
196
stdlib = compiler_configuration ["stdlib" ]
@@ -210,13 +250,30 @@ def cc_toolchain_config(
210
250
cxx_standard = compiler_configuration ["cxx_standard" ]
211
251
conly_flags = compiler_configuration ["conly_flags" ]
212
252
sysroot_path = compiler_configuration ["sysroot_path" ]
253
+
254
+ # Flags related to C++ standard.
255
+ cxx_flags = [
256
+ "-std=" + cxx_standard ,
257
+ ]
258
+ compile_flags_post_language = []
259
+
260
+ # We only support getting libc++ from the toolchain for now. Is it worth
261
+ # supporting libc++ from the sysroot? Or maybe just part of a LLVM distribution
262
+ # that's built for the target?
263
+ if not stdlib and is_xcompile :
264
+ print ("WARNING: Using libc++ for host architecture while cross compiling, this is " +
265
+ "probably not what you want. Explicitly set standard_libraries to libc++ to silence." )
266
+
213
267
if stdlib == "builtin-libc++" and is_xcompile :
214
268
stdlib = "stdc++"
215
269
if stdlib == "builtin-libc++" :
216
- cxx_flags = [
217
- "-std=" + cxx_standard ,
270
+ cxx_flags .extend ([
218
271
"-stdlib=libc++" ,
219
- ]
272
+ ])
273
+ compile_flags_post_language .extend ([
274
+ "-isystem" ,
275
+ target_toolchain_path_prefix + "lib/clang/{}/include" .format (resource_dir_version ),
276
+ ])
220
277
if major_llvm_version >= 14 :
221
278
# With C++20, Clang defaults to using C++ rather than Clang modules,
222
279
# which breaks Bazel's `use_module_maps` feature, which is used by
@@ -260,37 +317,80 @@ def cc_toolchain_config(
260
317
]
261
318
262
319
elif stdlib == "libc++" :
263
- cxx_flags = [
264
- "-std=" + cxx_standard ,
320
+ cxx_flags .extend ([
265
321
"-stdlib=libc++" ,
266
- ]
322
+ ])
267
323
268
324
link_flags .extend ([
269
325
"-l:c++.a" ,
270
326
"-l:c++abi.a" ,
271
327
])
272
328
elif stdlib == "dynamic-stdc++" :
273
- cxx_flags = [
329
+ cxx_flags . extend ( [
274
330
"-std=" + cxx_standard ,
275
331
"-stdlib=libstdc++" ,
276
- ]
332
+ ])
277
333
278
334
link_flags .extend ([
279
335
"-lstdc++" ,
280
336
])
281
- elif stdlib == "stdc++" :
282
- cxx_flags = [
283
- "-std=" + cxx_standard ,
284
- "-stdlib=libstdc++" ,
285
- ]
337
+ elif stdlib .startswith ("stdc++" ):
338
+ if not use_lld :
339
+ fail ("libstdc++ only supported with lld" )
286
340
341
+ # We use libgcc when using libstdc++ from a sysroot. Most libstdc++
342
+ # builds link to libgcc, which means we need to use libgcc's exception
343
+ # handling implementation, not the separate one in compiler-rt.
344
+ # Unfortunately, clang sometimes emits code incompatible with libgcc,
345
+ # see <https://bugs.llvm.org/show_bug.cgi?id=27455> and
346
+ # <https://lists.llvm.org/pipermail/cfe-dev/2016-April/048466.html> for
347
+ # example. This seems to be a commonly-used configuration with clang
348
+ # though, so it's probably good enough for most people.
349
+ link_flags .extend ([
350
+ "-L{}lib" .format (target_toolchain_path_prefix ),
351
+ "-L{}lib/{}" .format (target_toolchain_path_prefix , target_system_name ),
352
+ ])
353
+
354
+ # We expect to pick up these libraries from the sysroot.
287
355
link_flags .extend ([
288
356
"-l:libstdc++.a" ,
289
357
])
358
+ if stdlib == "stdc++" :
359
+ cxx_flags .extend ([
360
+ "-stdlib=libstdc++" ,
361
+ ])
362
+ elif stdlib .startswith ("stdc++-" ):
363
+ if sysroot_path == None :
364
+ fail ("Need a sysroot to link against stdc++" )
365
+
366
+ # -stdlib does nothing when using -nostdinc besides produce a warning
367
+ # that it's unused, so don't use it here.
368
+ libstdcxx_version = stdlib [len ("stdc++-" ):]
369
+
370
+ cxx_flags .extend ([
371
+ "-nostdinc++" ,
372
+ "-isystem" ,
373
+ sysroot_path + "/usr/include/c++/" + libstdcxx_version ,
374
+ "-isystem" ,
375
+ sysroot_path + "/usr/include/" + multiarch + "/c++/" + libstdcxx_version ,
376
+ "-isystem" ,
377
+ sysroot_path + "/usr/include/c++/" + libstdcxx_version + "/backward" ,
378
+ ])
379
+
380
+ # Clang really wants C system header includes after C++ ones.
381
+ compile_flags_post_language .extend ([
382
+ "-nostdinc" ,
383
+ "-isystem" ,
384
+ target_toolchain_path_prefix + "lib/clang/{}/include" .format (resource_dir_version ),
385
+ ])
386
+
387
+ link_flags .extend ([
388
+ "-L" + sysroot_path + "/usr/lib/gcc/" + multiarch + "/" + libstdcxx_version ,
389
+ ])
390
+ else :
391
+ fail ("Invalid stdlib: " + stdlib )
290
392
elif stdlib == "libc" :
291
- cxx_flags = [
292
- "-std=" + cxx_standard ,
293
- ]
393
+ pass
294
394
elif stdlib == "none" :
295
395
cxx_flags = [
296
396
"-nostdlib" ,
@@ -343,27 +443,46 @@ def cc_toolchain_config(
343
443
344
444
# Replace flags with any user-provided overrides.
345
445
if compiler_configuration ["compile_flags" ] != None :
346
- compile_flags = _fmt_flags (compiler_configuration ["compile_flags" ], toolchain_path_prefix )
446
+ compile_flags . extend ( _fmt_flags (compiler_configuration ["compile_flags" ], toolchain_path_prefix ) )
347
447
if compiler_configuration ["cxx_flags" ] != None :
348
- cxx_flags = _fmt_flags (compiler_configuration ["cxx_flags" ], toolchain_path_prefix )
448
+ cxx_flags . extend ( _fmt_flags (compiler_configuration ["cxx_flags" ], toolchain_path_prefix ) )
349
449
if compiler_configuration ["link_flags" ] != None :
350
- link_flags = _fmt_flags (compiler_configuration ["link_flags" ], toolchain_path_prefix )
450
+ link_flags . extend ( _fmt_flags (compiler_configuration ["link_flags" ], toolchain_path_prefix ) )
351
451
if compiler_configuration ["archive_flags" ] != None :
352
- archive_flags = _fmt_flags (compiler_configuration ["archive_flags" ], toolchain_path_prefix )
452
+ archive_flags . extend ( _fmt_flags (compiler_configuration ["archive_flags" ], toolchain_path_prefix ) )
353
453
if compiler_configuration ["link_libs" ] != None :
354
- link_libs = _fmt_flags (compiler_configuration ["link_libs" ], toolchain_path_prefix )
454
+ link_libs . extend ( _fmt_flags (compiler_configuration ["link_libs" ], toolchain_path_prefix ) )
355
455
if compiler_configuration ["opt_compile_flags" ] != None :
356
- opt_compile_flags = _fmt_flags (compiler_configuration ["opt_compile_flags" ], toolchain_path_prefix )
456
+ opt_compile_flags . extend ( _fmt_flags (compiler_configuration ["opt_compile_flags" ], toolchain_path_prefix ) )
357
457
if compiler_configuration ["opt_link_flags" ] != None :
358
- opt_link_flags = _fmt_flags (compiler_configuration ["opt_link_flags" ], toolchain_path_prefix )
458
+ opt_link_flags . extend ( _fmt_flags (compiler_configuration ["opt_link_flags" ], toolchain_path_prefix ) )
359
459
if compiler_configuration ["dbg_compile_flags" ] != None :
360
- dbg_compile_flags = _fmt_flags (compiler_configuration ["dbg_compile_flags" ], toolchain_path_prefix )
460
+ dbg_compile_flags . extend ( _fmt_flags (compiler_configuration ["dbg_compile_flags" ], toolchain_path_prefix ) )
361
461
if compiler_configuration ["coverage_compile_flags" ] != None :
362
- coverage_compile_flags = _fmt_flags (compiler_configuration ["coverage_compile_flags" ], toolchain_path_prefix )
462
+ coverage_compile_flags . extend ( _fmt_flags (compiler_configuration ["coverage_compile_flags" ], toolchain_path_prefix ) )
363
463
if compiler_configuration ["coverage_link_flags" ] != None :
364
- coverage_link_flags = _fmt_flags (compiler_configuration ["coverage_link_flags" ], toolchain_path_prefix )
464
+ coverage_link_flags . extend ( _fmt_flags (compiler_configuration ["coverage_link_flags" ], toolchain_path_prefix ) )
365
465
if compiler_configuration ["unfiltered_compile_flags" ] != None :
366
- unfiltered_compile_flags = _fmt_flags (compiler_configuration ["unfiltered_compile_flags" ], toolchain_path_prefix )
466
+ unfiltered_compile_flags .extend (_fmt_flags (compiler_configuration ["unfiltered_compile_flags" ], toolchain_path_prefix ))
467
+
468
+ # TODO(AustinSchuh): Add msan support and make this conditional.
469
+ cxx_flags = non_msan_compile_flags + cxx_flags
470
+
471
+ # Add the sysroot flags here, as we want to check these last
472
+ if sysroot_path != None :
473
+ external_include_paths = [
474
+ sysroot_path + "usr/local/include" ,
475
+ ]
476
+ if multiarch != None :
477
+ external_include_paths .extend ([
478
+ sysroot_path + "usr/" + multiarch + "/include" ,
479
+ sysroot_path + "usr/include/" + multiarch ,
480
+ ])
481
+
482
+ external_include_paths .extend ([
483
+ sysroot_path + "usr/include" ,
484
+ sysroot_path + "include" ,
485
+ ])
367
486
368
487
# Source: https://cs.opensource.google/bazel/bazel/+/master:tools/cpp/unix_cc_toolchain_config.bzl
369
488
unix_cc_toolchain_config (
@@ -383,8 +502,10 @@ def cc_toolchain_config(
383
502
opt_compile_flags = opt_compile_flags ,
384
503
conly_flags = conly_flags ,
385
504
cxx_flags = cxx_flags ,
505
+ compile_flags_post_language = compile_flags_post_language ,
386
506
link_flags = link_flags + select ({str (Label ("@toolchains_llvm//toolchain/config:use_libunwind" )): libunwind_link_flags , "//conditions:default" : []}) +
387
- select ({str (Label ("@toolchains_llvm//toolchain/config:use_compiler_rt" )): compiler_rt_link_flags , "//conditions:default" : []}),
507
+ select ({str (Label ("@toolchains_llvm//toolchain/config:use_compiler_rt" )): compiler_rt_link_flags , "//conditions:default" : []}) +
508
+ non_msan_link_flags ,
388
509
archive_flags = archive_flags ,
389
510
link_libs = link_libs ,
390
511
opt_link_flags = opt_link_flags ,
0 commit comments