clang_cc_toolchain_config.bzl 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. # Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. # Exceptions. See /LICENSE for license information.
  3. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. """A Starlark cc_toolchain configuration rule"""
  5. load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
  6. load(
  7. "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
  8. "action_config",
  9. "feature",
  10. "feature_set",
  11. "flag_group",
  12. "flag_set",
  13. "tool",
  14. "tool_path",
  15. "variable_with_value",
  16. "with_feature_set",
  17. )
  18. load("@rules_cc//cc:defs.bzl", "cc_toolchain")
  19. load(
  20. ":clang_detected_variables.bzl",
  21. "clang_bindir",
  22. "clang_include_dirs_list",
  23. "clang_resource_dir",
  24. "clang_version",
  25. "clang_version_for_cache",
  26. "llvm_bindir",
  27. "sysroot_dir",
  28. )
  29. all_c_compile_actions = [
  30. ACTION_NAMES.c_compile,
  31. ACTION_NAMES.assemble,
  32. ACTION_NAMES.preprocess_assemble,
  33. ]
  34. all_cpp_compile_actions = [
  35. ACTION_NAMES.cpp_compile,
  36. ACTION_NAMES.linkstamp_compile,
  37. ACTION_NAMES.cpp_header_parsing,
  38. ACTION_NAMES.cpp_module_compile,
  39. ACTION_NAMES.cpp_module_codegen,
  40. ]
  41. all_compile_actions = all_c_compile_actions + all_cpp_compile_actions
  42. preprocessor_compile_actions = [
  43. ACTION_NAMES.c_compile,
  44. ACTION_NAMES.cpp_compile,
  45. ACTION_NAMES.linkstamp_compile,
  46. ACTION_NAMES.preprocess_assemble,
  47. ACTION_NAMES.cpp_header_parsing,
  48. ACTION_NAMES.cpp_module_compile,
  49. ]
  50. codegen_compile_actions = [
  51. ACTION_NAMES.c_compile,
  52. ACTION_NAMES.cpp_compile,
  53. ACTION_NAMES.linkstamp_compile,
  54. ACTION_NAMES.assemble,
  55. ACTION_NAMES.preprocess_assemble,
  56. ACTION_NAMES.cpp_module_codegen,
  57. ]
  58. all_link_actions = [
  59. ACTION_NAMES.cpp_link_executable,
  60. ACTION_NAMES.cpp_link_dynamic_library,
  61. ACTION_NAMES.cpp_link_nodeps_dynamic_library,
  62. ]
  63. def _impl(ctx):
  64. tool_paths = [
  65. tool_path(name = "ar", path = llvm_bindir + "/llvm-ar"),
  66. tool_path(name = "ld", path = clang_bindir + "/ld.lld"),
  67. tool_path(name = "cpp", path = clang_bindir + "/clang-cpp"),
  68. tool_path(name = "gcc", path = clang_bindir + "/clang++"),
  69. tool_path(name = "dwp", path = llvm_bindir + "/llvm-dwp"),
  70. tool_path(name = "gcov", path = llvm_bindir + "/llvm-cov"),
  71. tool_path(name = "nm", path = llvm_bindir + "/llvm-nm"),
  72. tool_path(name = "objcopy", path = llvm_bindir + "/llvm-objcopy"),
  73. tool_path(name = "objdump", path = llvm_bindir + "/llvm-objdump"),
  74. tool_path(name = "strip", path = llvm_bindir + "/llvm-strip"),
  75. ]
  76. action_configs = [
  77. action_config(action_name = name, enabled = True, tools = [tool(path = clang_bindir + "/clang")])
  78. for name in all_c_compile_actions
  79. ] + [
  80. action_config(action_name = name, enabled = True, tools = [tool(path = clang_bindir + "/clang++")])
  81. for name in all_cpp_compile_actions
  82. ] + [
  83. action_config(action_name = name, enabled = True, tools = [tool(path = clang_bindir + "/clang++")])
  84. for name in all_link_actions
  85. ] + [
  86. action_config(action_name = name, enabled = True, tools = [tool(path = llvm_bindir + "/llvm-ar")])
  87. for name in [ACTION_NAMES.cpp_link_static_library]
  88. ] + [
  89. action_config(action_name = name, enabled = True, tools = [tool(path = llvm_bindir + "/llvm-strip")])
  90. for name in [ACTION_NAMES.strip]
  91. ]
  92. std_compile_flags = ["-std=c++20"]
  93. # libc++ is only used on non-Windows platforms.
  94. if ctx.attr.target_os != "windows":
  95. std_compile_flags.append("-stdlib=libc++")
  96. # TODO: Regression that warns on anonymous unions; remove depending on fix.
  97. # Sets the flag for unknown clang versions, which are assumed to be at head.
  98. # https://github.com/llvm/llvm-project/issues/70384
  99. if not clang_version or clang_version == 18:
  100. missing_field_init_flags = ["-Wno-missing-field-initializers"]
  101. elif clang_version > 18:
  102. missing_field_init_flags = ["-Wno-missing-designated-field-initializers"]
  103. else:
  104. missing_field_init_flags = []
  105. default_flags_feature = feature(
  106. name = "default_flags",
  107. enabled = True,
  108. flag_sets = [
  109. flag_set(
  110. actions = all_compile_actions + all_link_actions,
  111. flag_groups = ([
  112. flag_group(
  113. flags = [
  114. "-no-canonical-prefixes",
  115. "-fcolor-diagnostics",
  116. ],
  117. ),
  118. ]),
  119. ),
  120. flag_set(
  121. actions = all_compile_actions,
  122. flag_groups = ([
  123. flag_group(
  124. flags = [
  125. "-Werror",
  126. "-Wall",
  127. "-Wextra",
  128. "-Wthread-safety",
  129. "-Wself-assign",
  130. "-Wimplicit-fallthrough",
  131. "-Wctad-maybe-unsupported",
  132. "-Wextra-semi",
  133. "-Wmissing-prototypes",
  134. "-Wzero-as-null-pointer-constant",
  135. "-Wdelete-non-virtual-dtor",
  136. # Don't warn on external code as we can't
  137. # necessarily patch it easily. Note that these have
  138. # to be initial directories in the `#include` line.
  139. "--system-header-prefix=absl/",
  140. "--system-header-prefix=benchmark/",
  141. "--system-header-prefix=boost/",
  142. "--system-header-prefix=clang-tools-extra/",
  143. "--system-header-prefix=clang/",
  144. "--system-header-prefix=gmock/",
  145. "--system-header-prefix=google/protobuf/",
  146. "--system-header-prefix=gtest/",
  147. "--system-header-prefix=libfuzzer/",
  148. "--system-header-prefix=llvm/",
  149. "--system-header-prefix=re2/",
  150. "--system-header-prefix=tools/cpp/",
  151. "--system-header-prefix=tree_sitter/",
  152. # Compile actions shouldn't link anything.
  153. "-c",
  154. ] + missing_field_init_flags,
  155. ),
  156. flag_group(
  157. expand_if_available = "output_assembly_file",
  158. flags = ["-S"],
  159. ),
  160. flag_group(
  161. expand_if_available = "output_preprocess_file",
  162. flags = ["-E"],
  163. ),
  164. flag_group(
  165. flags = ["-MD", "-MF", "%{dependency_file}"],
  166. expand_if_available = "dependency_file",
  167. ),
  168. flag_group(
  169. flags = ["-frandom-seed=%{output_file}"],
  170. expand_if_available = "output_file",
  171. ),
  172. ]),
  173. ),
  174. flag_set(
  175. actions = all_cpp_compile_actions + all_link_actions,
  176. flag_groups = ([
  177. flag_group(
  178. flags = std_compile_flags,
  179. ),
  180. ]),
  181. ),
  182. flag_set(
  183. actions = codegen_compile_actions,
  184. flag_groups = ([
  185. flag_group(
  186. flags = [
  187. "-ffunction-sections",
  188. "-fdata-sections",
  189. ],
  190. ),
  191. ]),
  192. ),
  193. flag_set(
  194. actions = codegen_compile_actions,
  195. flag_groups = [
  196. flag_group(flags = ["-fPIC"], expand_if_available = "pic"),
  197. ],
  198. ),
  199. flag_set(
  200. actions = preprocessor_compile_actions,
  201. flag_groups = [
  202. flag_group(
  203. flags = [
  204. # Disable a warning and override builtin macros to
  205. # ensure a hermetic build.
  206. "-Wno-builtin-macro-redefined",
  207. "-D__DATE__=\"redacted\"",
  208. "-D__TIMESTAMP__=\"redacted\"",
  209. "-D__TIME__=\"redacted\"",
  210. # Pass the clang version as a define so that bazel
  211. # caching is more likely to notice version changes.
  212. "-DCLANG_VERSION_FOR_CACHE=\"%s\"" % clang_version_for_cache,
  213. ],
  214. ),
  215. flag_group(
  216. flags = ["-D%{preprocessor_defines}"],
  217. iterate_over = "preprocessor_defines",
  218. ),
  219. flag_group(
  220. flags = ["-include", "%{includes}"],
  221. iterate_over = "includes",
  222. expand_if_available = "includes",
  223. ),
  224. flag_group(
  225. flags = ["-iquote", "%{quote_include_paths}"],
  226. iterate_over = "quote_include_paths",
  227. ),
  228. flag_group(
  229. flags = ["-I%{include_paths}"],
  230. iterate_over = "include_paths",
  231. ),
  232. flag_group(
  233. flags = ["-isystem", "%{system_include_paths}"],
  234. iterate_over = "system_include_paths",
  235. ),
  236. ],
  237. ),
  238. flag_set(
  239. actions = [
  240. ACTION_NAMES.cpp_link_dynamic_library,
  241. ACTION_NAMES.cpp_link_nodeps_dynamic_library,
  242. ],
  243. flag_groups = [flag_group(flags = ["-shared"])],
  244. ),
  245. flag_set(
  246. actions = all_link_actions,
  247. flag_groups = [
  248. flag_group(
  249. flags = ["-Wl,--gdb-index"],
  250. expand_if_available = "is_using_fission",
  251. ),
  252. flag_group(
  253. flags = ["-Wl,-S"],
  254. expand_if_available = "strip_debug_symbols",
  255. ),
  256. flag_group(
  257. flags = ["-L%{library_search_directories}"],
  258. iterate_over = "library_search_directories",
  259. expand_if_available = "library_search_directories",
  260. ),
  261. flag_group(
  262. iterate_over = "runtime_library_search_directories",
  263. flags = [
  264. "-Wl,-rpath,$ORIGIN/%{runtime_library_search_directories}",
  265. ],
  266. expand_if_available =
  267. "runtime_library_search_directories",
  268. ),
  269. ],
  270. ),
  271. ],
  272. )
  273. # Handle different levels of optimization with individual features so that
  274. # they can be ordered and the defaults can override the minimal settings if
  275. # both are enabled.
  276. minimal_optimization_flags = feature(
  277. name = "minimal_optimization_flags",
  278. flag_sets = [
  279. flag_set(
  280. actions = codegen_compile_actions,
  281. flag_groups = [flag_group(flags = [
  282. "-O1",
  283. ])],
  284. ),
  285. ],
  286. )
  287. default_optimization_flags = feature(
  288. name = "default_optimization_flags",
  289. enabled = True,
  290. requires = [feature_set(["opt"])],
  291. flag_sets = [
  292. flag_set(
  293. actions = all_compile_actions,
  294. flag_groups = [flag_group(flags = [
  295. "-DNDEBUG",
  296. ])],
  297. ),
  298. flag_set(
  299. actions = codegen_compile_actions,
  300. flag_groups = [flag_group(flags = [
  301. "-O3",
  302. ])],
  303. ),
  304. ],
  305. )
  306. x86_64_cpu_flags = feature(
  307. name = "x86_64_cpu_flags",
  308. enabled = True,
  309. flag_sets = [
  310. flag_set(
  311. actions = all_compile_actions,
  312. flag_groups = [flag_group(flags = [
  313. "-march=x86-64-v2",
  314. ])],
  315. ),
  316. ],
  317. )
  318. aarch64_cpu_flags = feature(
  319. name = "aarch64_cpu_flags",
  320. enabled = True,
  321. flag_sets = [
  322. flag_set(
  323. actions = all_compile_actions,
  324. flag_groups = [flag_group(flags = [
  325. "-march=armv8.2-a",
  326. ])],
  327. ),
  328. ],
  329. )
  330. # Handle different levels and forms of debug info emission with individual
  331. # features so that they can be ordered and the defaults can override the
  332. # minimal settings if both are enabled.
  333. minimal_debug_info_flags = feature(
  334. name = "minimal_debug_info_flags",
  335. flag_sets = [
  336. flag_set(
  337. actions = codegen_compile_actions,
  338. flag_groups = [
  339. flag_group(
  340. flags = ["-gmlt"],
  341. ),
  342. ],
  343. ),
  344. ],
  345. )
  346. default_debug_info_flags = feature(
  347. name = "default_debug_info_flags",
  348. enabled = True,
  349. flag_sets = [
  350. flag_set(
  351. actions = codegen_compile_actions,
  352. flag_groups = ([
  353. flag_group(
  354. flags = ["-g"],
  355. ),
  356. ]),
  357. with_features = [with_feature_set(features = ["dbg"])],
  358. ),
  359. flag_set(
  360. actions = codegen_compile_actions,
  361. flag_groups = [
  362. flag_group(
  363. flags = ["-gsplit-dwarf", "-g"],
  364. expand_if_available = "per_object_debug_info_file",
  365. ),
  366. ],
  367. ),
  368. ],
  369. )
  370. # This feature can be enabled in conjunction with any optimizations to
  371. # ensure accurate call stacks and backtraces for profilers or errors.
  372. preserve_call_stacks = feature(
  373. name = "preserve_call_stacks",
  374. flag_sets = [flag_set(
  375. actions = codegen_compile_actions,
  376. flag_groups = [flag_group(flags = [
  377. # Ensure good backtraces by preserving frame pointers and
  378. # disabling tail call elimination.
  379. "-fno-omit-frame-pointer",
  380. "-mno-omit-leaf-frame-pointer",
  381. "-fno-optimize-sibling-calls",
  382. ])],
  383. )],
  384. )
  385. sysroot_feature = feature(
  386. name = "sysroot",
  387. enabled = True,
  388. flag_sets = [
  389. flag_set(
  390. actions = all_compile_actions + all_link_actions,
  391. flag_groups = [
  392. flag_group(
  393. flags = ["--sysroot=%{sysroot}"],
  394. expand_if_available = "sysroot",
  395. ),
  396. ],
  397. ),
  398. ],
  399. )
  400. use_module_maps = feature(
  401. name = "use_module_maps",
  402. requires = [feature_set(features = ["module_maps"])],
  403. flag_sets = [
  404. flag_set(
  405. actions = [
  406. ACTION_NAMES.c_compile,
  407. ACTION_NAMES.cpp_compile,
  408. ACTION_NAMES.cpp_header_parsing,
  409. ACTION_NAMES.cpp_module_compile,
  410. ],
  411. flag_groups = [
  412. # These flag groups are separate so they do not expand to
  413. # the cross product of the variables.
  414. flag_group(flags = ["-fmodule-name=%{module_name}"]),
  415. flag_group(
  416. flags = ["-fmodule-map-file=%{module_map_file}"],
  417. ),
  418. ],
  419. ),
  420. ],
  421. )
  422. # Tell bazel we support module maps in general, so they will be generated
  423. # for all c/c++ rules.
  424. # Note: not all C++ rules support module maps; thus, do not imply this
  425. # feature from other features - instead, require it.
  426. module_maps = feature(
  427. name = "module_maps",
  428. enabled = True,
  429. implies = [
  430. # "module_map_home_cwd",
  431. # "module_map_without_extern_module",
  432. # "generate_submodules",
  433. ],
  434. )
  435. layering_check = feature(
  436. name = "layering_check",
  437. implies = ["use_module_maps"],
  438. flag_sets = [
  439. flag_set(
  440. actions = [
  441. ACTION_NAMES.c_compile,
  442. ACTION_NAMES.cpp_compile,
  443. ACTION_NAMES.cpp_header_parsing,
  444. ACTION_NAMES.cpp_module_compile,
  445. ],
  446. flag_groups = [
  447. flag_group(flags = [
  448. "-fmodules-strict-decluse",
  449. "-Wprivate-header",
  450. ]),
  451. flag_group(
  452. iterate_over = "dependent_module_map_files",
  453. flags = [
  454. "-fmodule-map-file=%{dependent_module_map_files}",
  455. ],
  456. ),
  457. ],
  458. ),
  459. ],
  460. )
  461. sanitizer_common_flags = feature(
  462. name = "sanitizer_common_flags",
  463. implies = ["minimal_debug_info_flags", "preserve_call_stacks"],
  464. )
  465. # Separated from the feature above so it can only be included on platforms
  466. # where it is supported. There is no negative flag in Clang so we can't just
  467. # override it later.
  468. sanitizer_static_lib_flags = feature(
  469. name = "sanitizer_static_lib_flags",
  470. enabled = True,
  471. requires = [feature_set(["sanitizer_common_flags"])],
  472. flag_sets = [flag_set(
  473. actions = all_link_actions,
  474. flag_groups = [flag_group(flags = [
  475. "-static-libsan",
  476. ])],
  477. )],
  478. )
  479. asan = feature(
  480. name = "asan",
  481. implies = ["sanitizer_common_flags"],
  482. flag_sets = [flag_set(
  483. actions = all_compile_actions + all_link_actions,
  484. flag_groups = [flag_group(flags = [
  485. "-fsanitize=address,undefined,nullability",
  486. "-fsanitize-address-use-after-scope",
  487. # Outlining is almost always the right tradeoff for our
  488. # sanitizer usage where we're more pressured on generated code
  489. # size than runtime performance.
  490. "-fsanitize-address-outline-instrumentation",
  491. # We don't need the recovery behavior of UBSan as we expect
  492. # builds to be clean. Not recovering is a bit cheaper.
  493. "-fno-sanitize-recover=undefined,nullability",
  494. # Don't embed the full path name for files. This limits the size
  495. # and combined with line numbers is unlikely to result in many
  496. # ambiguities.
  497. "-fsanitize-undefined-strip-path-components=-1",
  498. # Needed due to clang AST issues, such as in
  499. # clang/AST/Redeclarable.h line 199.
  500. "-fno-sanitize=vptr",
  501. ])],
  502. )],
  503. )
  504. # A feature that further reduces the generated code size of our the ASan
  505. # feature, but at the cost of lower quality diagnostics. This is enabled
  506. # along with ASan in our fastbuild configuration, but can be disabled
  507. # explicitly to get better error messages.
  508. asan_min_size = feature(
  509. name = "asan_min_size",
  510. requires = [feature_set(["asan"])],
  511. flag_sets = [flag_set(
  512. actions = all_compile_actions + all_link_actions,
  513. flag_groups = [flag_group(flags = [
  514. # Force two UBSan checks that have especially large code size
  515. # cost to use the minimal branch to a trapping instruction model
  516. # instead of the full diagnostic.
  517. "-fsanitize-trap=alignment,null",
  518. ])],
  519. )],
  520. )
  521. # Likely due to being unable to use the static-linked and up-to-date
  522. # sanitizer runtimes, we have to disable a number of sanitizers on macOS.
  523. macos_asan_workarounds = feature(
  524. name = "macos_sanitizer_workarounds",
  525. enabled = True,
  526. requires = [feature_set(["asan"])],
  527. flag_sets = [flag_set(
  528. actions = all_compile_actions + all_link_actions,
  529. flag_groups = [flag_group(flags = [
  530. "-fno-sanitize=function",
  531. ])],
  532. )],
  533. )
  534. # An enabled feature that requires the `fastbuild` compilation. This is used
  535. # to toggle general features on by default, while allowing them to be
  536. # directly enabled and disabled more generally as desired.
  537. enable_in_fastbuild = feature(
  538. name = "enable_in_fastbuild",
  539. enabled = True,
  540. requires = [feature_set(["fastbuild"])],
  541. implies = [
  542. "asan",
  543. "asan_min_size",
  544. "minimal_optimization_flags",
  545. "minimal_debug_info_flags",
  546. ],
  547. )
  548. fuzzer = feature(
  549. name = "fuzzer",
  550. flag_sets = [flag_set(
  551. actions = all_compile_actions + all_link_actions,
  552. flag_groups = [flag_group(flags = [
  553. "-fsanitize=fuzzer-no-link",
  554. ])],
  555. )],
  556. )
  557. if clang_version and clang_version <= 16:
  558. libcpp_debug_flags = ["-D_LIBCPP_ENABLE_ASSERTIONS=1"]
  559. else:
  560. # Clang 17 deprecates LIBCPP_ENABLE_ASSERTIONS in favor of
  561. # HARDENED_MODE.
  562. libcpp_debug_flags = ["-D_LIBCPP_ENABLE_HARDENED_MODE=1"]
  563. linux_flags_feature = feature(
  564. name = "linux_flags",
  565. enabled = True,
  566. flag_sets = [
  567. flag_set(
  568. actions = all_link_actions,
  569. flag_groups = ([
  570. flag_group(
  571. flags = [
  572. "-fuse-ld=lld",
  573. "-stdlib=libc++",
  574. "-unwindlib=libunwind",
  575. # Force the C++ standard library and runtime
  576. # libraries to be statically linked. This works even
  577. # with libc++ and libunwind despite the names,
  578. # provided libc++ is built with the CMake option:
  579. # - `-DCMAKE_POSITION_INDEPENDENT_CODE=ON`
  580. "-static-libstdc++",
  581. "-static-libgcc",
  582. # Link with Clang's runtime library. This is always
  583. # linked statically.
  584. "-rtlib=compiler-rt",
  585. # Explicitly add LLVM libs to the search path to
  586. # preempt the detected GCC installation's library
  587. # paths. Those might have a system installed libc++
  588. # and we want to find the one next to our Clang.
  589. "-L" + llvm_bindir + "/../lib",
  590. # Link with pthread.
  591. "-lpthread",
  592. # Force linking the static libc++abi archive here.
  593. # This *should* be linked automatically, but not
  594. # every release of LLVM correctly sets the CMake
  595. # flags to do so.
  596. "-l:libc++abi.a",
  597. ],
  598. ),
  599. ]),
  600. ),
  601. flag_set(
  602. actions = all_compile_actions,
  603. flag_groups = [flag_group(flags = libcpp_debug_flags)],
  604. with_features = [
  605. with_feature_set(not_features = ["opt"]),
  606. ],
  607. ),
  608. flag_set(
  609. actions = [
  610. ACTION_NAMES.cpp_link_executable,
  611. ],
  612. flag_groups = [
  613. flag_group(
  614. flags = ["-pie"],
  615. expand_if_available = "force_pic",
  616. ),
  617. ],
  618. ),
  619. ],
  620. )
  621. macos_flags_feature = feature(
  622. name = "macos_flags",
  623. enabled = True,
  624. flag_sets = [
  625. flag_set(
  626. actions = [
  627. ACTION_NAMES.cpp_link_executable,
  628. ],
  629. flag_groups = [
  630. flag_group(
  631. flags = ["-fpie"],
  632. expand_if_available = "force_pic",
  633. ),
  634. ],
  635. ),
  636. ],
  637. )
  638. freebsd_flags_feature = feature(
  639. name = "freebsd_flags",
  640. enabled = True,
  641. flag_sets = [
  642. flag_set(
  643. actions = [
  644. ACTION_NAMES.c_compile,
  645. ACTION_NAMES.cpp_compile,
  646. ACTION_NAMES.cpp_header_parsing,
  647. ACTION_NAMES.cpp_module_compile,
  648. ],
  649. flag_groups = [
  650. flag_group(
  651. flags = [
  652. "-DHAVE_MALLCTL",
  653. ],
  654. ),
  655. ],
  656. ),
  657. flag_set(
  658. actions = [
  659. ACTION_NAMES.cpp_link_executable,
  660. ],
  661. flag_groups = [
  662. flag_group(
  663. flags = ["-fpie"],
  664. expand_if_available = "force_pic",
  665. ),
  666. ],
  667. ),
  668. ],
  669. )
  670. default_link_libraries_feature = feature(
  671. name = "default_link_libraries",
  672. enabled = True,
  673. flag_sets = [
  674. flag_set(
  675. actions = all_link_actions,
  676. flag_groups = [
  677. flag_group(
  678. flags = ["%{linkstamp_paths}"],
  679. iterate_over = "linkstamp_paths",
  680. expand_if_available = "linkstamp_paths",
  681. ),
  682. flag_group(
  683. iterate_over = "libraries_to_link",
  684. flag_groups = [
  685. flag_group(
  686. flags = ["-Wl,--start-lib"],
  687. expand_if_equal = variable_with_value(
  688. name = "libraries_to_link.type",
  689. value = "object_file_group",
  690. ),
  691. ),
  692. flag_group(
  693. flags = ["-Wl,-whole-archive"],
  694. expand_if_true = "libraries_to_link.is_whole_archive",
  695. ),
  696. flag_group(
  697. flags = ["%{libraries_to_link.object_files}"],
  698. iterate_over = "libraries_to_link.object_files",
  699. expand_if_equal = variable_with_value(
  700. name = "libraries_to_link.type",
  701. value = "object_file_group",
  702. ),
  703. ),
  704. flag_group(
  705. flags = ["%{libraries_to_link.name}"],
  706. expand_if_equal = variable_with_value(
  707. name = "libraries_to_link.type",
  708. value = "object_file",
  709. ),
  710. ),
  711. flag_group(
  712. flags = ["%{libraries_to_link.name}"],
  713. expand_if_equal = variable_with_value(
  714. name = "libraries_to_link.type",
  715. value = "interface_library",
  716. ),
  717. ),
  718. flag_group(
  719. flags = ["%{libraries_to_link.name}"],
  720. expand_if_equal = variable_with_value(
  721. name = "libraries_to_link.type",
  722. value = "static_library",
  723. ),
  724. ),
  725. flag_group(
  726. flags = ["-l%{libraries_to_link.name}"],
  727. expand_if_equal = variable_with_value(
  728. name = "libraries_to_link.type",
  729. value = "dynamic_library",
  730. ),
  731. ),
  732. flag_group(
  733. flags = ["-l:%{libraries_to_link.name}"],
  734. expand_if_equal = variable_with_value(
  735. name = "libraries_to_link.type",
  736. value = "versioned_dynamic_library",
  737. ),
  738. ),
  739. flag_group(
  740. flags = ["-Wl,-no-whole-archive"],
  741. expand_if_true = "libraries_to_link.is_whole_archive",
  742. ),
  743. flag_group(
  744. flags = ["-Wl,--end-lib"],
  745. expand_if_equal = variable_with_value(
  746. name = "libraries_to_link.type",
  747. value = "object_file_group",
  748. ),
  749. ),
  750. ],
  751. expand_if_available = "libraries_to_link",
  752. ),
  753. # Note that the params file comes at the end, after the
  754. # libraries to link above.
  755. flag_group(
  756. expand_if_available = "linker_param_file",
  757. flags = ["@%{linker_param_file}"],
  758. ),
  759. ],
  760. ),
  761. ],
  762. )
  763. macos_link_libraries_feature = feature(
  764. name = "macos_link_libraries",
  765. enabled = True,
  766. flag_sets = [
  767. flag_set(
  768. actions = all_link_actions,
  769. flag_groups = [
  770. flag_group(
  771. flags = ["%{linkstamp_paths}"],
  772. iterate_over = "linkstamp_paths",
  773. expand_if_available = "linkstamp_paths",
  774. ),
  775. flag_group(
  776. iterate_over = "libraries_to_link",
  777. flag_groups = [
  778. flag_group(
  779. flags = ["-Wl,--start-lib"],
  780. expand_if_equal = variable_with_value(
  781. name = "libraries_to_link.type",
  782. value = "object_file_group",
  783. ),
  784. ),
  785. flag_group(
  786. iterate_over = "libraries_to_link.object_files",
  787. expand_if_equal = variable_with_value(
  788. name = "libraries_to_link.type",
  789. value = "object_file_group",
  790. ),
  791. flag_groups = [
  792. flag_group(
  793. flags = ["%{libraries_to_link.object_files}"],
  794. expand_if_false = "libraries_to_link.is_whole_archive",
  795. ),
  796. flag_group(
  797. flags = ["-Wl,-force_load,%{libraries_to_link.object_files}"],
  798. expand_if_true = "libraries_to_link.is_whole_archive",
  799. ),
  800. ],
  801. ),
  802. flag_group(
  803. expand_if_equal = variable_with_value(
  804. name = "libraries_to_link.type",
  805. value = "object_file",
  806. ),
  807. flag_groups = [
  808. flag_group(
  809. flags = ["%{libraries_to_link.name}"],
  810. expand_if_false = "libraries_to_link.is_whole_archive",
  811. ),
  812. flag_group(
  813. flags = ["-Wl,-force_load,%{libraries_to_link.name}"],
  814. expand_if_true = "libraries_to_link.is_whole_archive",
  815. ),
  816. ],
  817. ),
  818. flag_group(
  819. expand_if_equal = variable_with_value(
  820. name = "libraries_to_link.type",
  821. value = "interface_library",
  822. ),
  823. flag_groups = [
  824. flag_group(
  825. flags = ["%{libraries_to_link.name}"],
  826. expand_if_false = "libraries_to_link.is_whole_archive",
  827. ),
  828. flag_group(
  829. flags = ["-Wl,-force_load,%{libraries_to_link.name}"],
  830. expand_if_true = "libraries_to_link.is_whole_archive",
  831. ),
  832. ],
  833. ),
  834. flag_group(
  835. expand_if_equal = variable_with_value(
  836. name = "libraries_to_link.type",
  837. value = "static_library",
  838. ),
  839. flag_groups = [
  840. flag_group(
  841. flags = ["%{libraries_to_link.name}"],
  842. expand_if_false = "libraries_to_link.is_whole_archive",
  843. ),
  844. flag_group(
  845. flags = ["-Wl,-force_load,%{libraries_to_link.name}"],
  846. expand_if_true = "libraries_to_link.is_whole_archive",
  847. ),
  848. ],
  849. ),
  850. flag_group(
  851. flags = ["-l%{libraries_to_link.name}"],
  852. expand_if_equal = variable_with_value(
  853. name = "libraries_to_link.type",
  854. value = "dynamic_library",
  855. ),
  856. ),
  857. flag_group(
  858. flags = ["-l:%{libraries_to_link.name}"],
  859. expand_if_equal = variable_with_value(
  860. name = "libraries_to_link.type",
  861. value = "versioned_dynamic_library",
  862. ),
  863. ),
  864. flag_group(
  865. expand_if_true = "libraries_to_link.is_whole_archive",
  866. flag_groups = [
  867. flag_group(
  868. expand_if_false = "macos_flags",
  869. flags = ["-Wl,-no-whole-archive"],
  870. ),
  871. ],
  872. ),
  873. flag_group(
  874. flags = ["-Wl,--end-lib"],
  875. expand_if_equal = variable_with_value(
  876. name = "libraries_to_link.type",
  877. value = "object_file_group",
  878. ),
  879. ),
  880. ],
  881. expand_if_available = "libraries_to_link",
  882. ),
  883. # Note that the params file comes at the end, after the
  884. # libraries to link above.
  885. flag_group(
  886. expand_if_available = "linker_param_file",
  887. flags = ["@%{linker_param_file}"],
  888. ),
  889. ],
  890. ),
  891. ],
  892. )
  893. # Place user provided compile flags after all the features so that these
  894. # flags can override or customize behavior. The only thing user flags
  895. # cannot override is the output file as Bazel depends on that.
  896. #
  897. # Finally, place the source file (if present) and output file last to make
  898. # reading the compile command lines easier for humans.
  899. final_flags_feature = feature(
  900. name = "final_flags",
  901. enabled = True,
  902. flag_sets = [
  903. flag_set(
  904. actions = all_compile_actions,
  905. flag_groups = [
  906. flag_group(
  907. flags = ["%{user_compile_flags}"],
  908. iterate_over = "user_compile_flags",
  909. expand_if_available = "user_compile_flags",
  910. ),
  911. flag_group(
  912. flags = ["%{source_file}"],
  913. expand_if_available = "source_file",
  914. ),
  915. flag_group(
  916. expand_if_available = "output_file",
  917. flags = ["-o", "%{output_file}"],
  918. ),
  919. ],
  920. ),
  921. flag_set(
  922. actions = all_link_actions,
  923. flag_groups = [
  924. flag_group(
  925. flags = ["%{user_link_flags}"],
  926. iterate_over = "user_link_flags",
  927. expand_if_available = "user_link_flags",
  928. ),
  929. flag_group(
  930. flags = ["-o", "%{output_execpath}"],
  931. expand_if_available = "output_execpath",
  932. ),
  933. ],
  934. ),
  935. ],
  936. )
  937. # Archive actions have an entirely independent set of flags and don't
  938. # interact with either compiler or link actions.
  939. default_archiver_flags_feature = feature(
  940. name = "default_archiver_flags",
  941. enabled = True,
  942. flag_sets = [
  943. flag_set(
  944. actions = [ACTION_NAMES.cpp_link_static_library],
  945. flag_groups = [
  946. flag_group(flags = ["rcsD"]),
  947. flag_group(
  948. flags = ["%{output_execpath}"],
  949. expand_if_available = "output_execpath",
  950. ),
  951. flag_group(
  952. iterate_over = "libraries_to_link",
  953. flag_groups = [
  954. flag_group(
  955. flags = ["%{libraries_to_link.name}"],
  956. expand_if_equal = variable_with_value(
  957. name = "libraries_to_link.type",
  958. value = "object_file",
  959. ),
  960. ),
  961. flag_group(
  962. flags = ["%{libraries_to_link.object_files}"],
  963. iterate_over = "libraries_to_link.object_files",
  964. expand_if_equal = variable_with_value(
  965. name = "libraries_to_link.type",
  966. value = "object_file_group",
  967. ),
  968. ),
  969. ],
  970. expand_if_available = "libraries_to_link",
  971. ),
  972. flag_group(
  973. expand_if_available = "linker_param_file",
  974. flags = ["@%{linker_param_file}"],
  975. ),
  976. ],
  977. ),
  978. ],
  979. )
  980. # Now that we have built up the constituent feature definitions, compose
  981. # them, including configuration based on the target platform.
  982. # First, define features that are simply used to configure others.
  983. features = [
  984. feature(name = "dbg"),
  985. feature(name = "fastbuild"),
  986. feature(name = "host"),
  987. feature(name = "no_legacy_features"),
  988. feature(name = "opt"),
  989. feature(name = "supports_dynamic_linker", enabled = ctx.attr.target_os == "linux"),
  990. feature(name = "supports_pic", enabled = True),
  991. feature(name = "supports_start_end_lib", enabled = ctx.attr.target_os == "linux"),
  992. ]
  993. # The order of the features determines the relative order of flags used.
  994. # Start off adding the baseline features.
  995. features += [
  996. default_flags_feature,
  997. minimal_optimization_flags,
  998. default_optimization_flags,
  999. minimal_debug_info_flags,
  1000. default_debug_info_flags,
  1001. preserve_call_stacks,
  1002. sysroot_feature,
  1003. sanitizer_common_flags,
  1004. asan,
  1005. asan_min_size,
  1006. enable_in_fastbuild,
  1007. fuzzer,
  1008. layering_check,
  1009. module_maps,
  1010. use_module_maps,
  1011. default_archiver_flags_feature,
  1012. ]
  1013. # Next, add the features based on the target platform. Here too the
  1014. # features are order sensitive. We also setup the sysroot here.
  1015. if ctx.attr.target_os == "linux":
  1016. features.append(sanitizer_static_lib_flags)
  1017. features.append(linux_flags_feature)
  1018. sysroot = None
  1019. elif ctx.attr.target_os == "windows":
  1020. # TODO: Need to figure out if we need to add windows specific features
  1021. # I think the .pdb debug files will need to be handled differently,
  1022. # so that might be an example where a feature must be added.
  1023. sysroot = None
  1024. elif ctx.attr.target_os == "macos":
  1025. features.append(macos_asan_workarounds)
  1026. features.append(macos_flags_feature)
  1027. sysroot = sysroot_dir
  1028. elif ctx.attr.target_os == "freebsd":
  1029. features.append(sanitizer_static_lib_flags)
  1030. features.append(freebsd_flags_feature)
  1031. sysroot = sysroot_dir
  1032. else:
  1033. fail("Unsupported target OS!")
  1034. if ctx.attr.target_cpu in ["aarch64", "arm64"]:
  1035. features.append(aarch64_cpu_flags)
  1036. else:
  1037. features.append(x86_64_cpu_flags)
  1038. # Finally append the libraries to link and any final flags.
  1039. if ctx.attr.target_os == "macos":
  1040. features.append(macos_link_libraries_feature)
  1041. else:
  1042. features.append(default_link_libraries_feature)
  1043. features.append(final_flags_feature)
  1044. identifier = "local-{0}-{1}".format(ctx.attr.target_cpu, ctx.attr.target_os)
  1045. return cc_common.create_cc_toolchain_config_info(
  1046. ctx = ctx,
  1047. features = features,
  1048. action_configs = action_configs,
  1049. cxx_builtin_include_directories = clang_include_dirs_list + [
  1050. # Add Clang's resource directory to the end of the builtin include
  1051. # directories to cover the use of sanitizer resource files by the driver.
  1052. clang_resource_dir + "/share",
  1053. ],
  1054. builtin_sysroot = sysroot,
  1055. # This configuration only supports local non-cross builds so derive
  1056. # everything from the target CPU selected.
  1057. toolchain_identifier = identifier,
  1058. host_system_name = identifier,
  1059. target_system_name = identifier,
  1060. target_cpu = ctx.attr.target_cpu,
  1061. # This is used to expose a "flag" that `config_setting` rules can use to
  1062. # determine if the compiler is Clang.
  1063. compiler = "clang",
  1064. # These attributes aren't meaningful at all so just use placeholder
  1065. # values.
  1066. target_libc = "local",
  1067. abi_version = "local",
  1068. abi_libc_version = "local",
  1069. # We do have to pass in our tool paths.
  1070. tool_paths = tool_paths,
  1071. )
  1072. cc_toolchain_config = rule(
  1073. implementation = _impl,
  1074. attrs = {
  1075. "target_cpu": attr.string(mandatory = True),
  1076. "target_os": attr.string(mandatory = True),
  1077. },
  1078. provides = [CcToolchainConfigInfo],
  1079. )
  1080. def cc_local_toolchain_suite(name, configs):
  1081. """Create a toolchain suite that uses the local Clang/LLVM install.
  1082. Args:
  1083. name: The name of the toolchain suite to produce.
  1084. configs: An array of (os, cpu) pairs to support in the toolchain.
  1085. """
  1086. # An empty filegroup to use when stubbing out the toolchains.
  1087. native.filegroup(
  1088. name = name + "_empty",
  1089. srcs = [],
  1090. )
  1091. # Create the individual local toolchains for each CPU.
  1092. for (os, cpu) in configs:
  1093. config_name = "{0}_{1}_{2}".format(name, os, cpu)
  1094. cc_toolchain_config(
  1095. name = config_name + "_config",
  1096. target_os = os,
  1097. target_cpu = cpu,
  1098. )
  1099. cc_toolchain(
  1100. name = config_name + "_tools",
  1101. all_files = ":" + name + "_empty",
  1102. ar_files = ":" + name + "_empty",
  1103. as_files = ":" + name + "_empty",
  1104. compiler_files = ":" + name + "_empty",
  1105. dwp_files = ":" + name + "_empty",
  1106. linker_files = ":" + name + "_empty",
  1107. objcopy_files = ":" + name + "_empty",
  1108. strip_files = ":" + name + "_empty",
  1109. supports_param_files = 1,
  1110. toolchain_config = ":" + config_name + "_config",
  1111. toolchain_identifier = config_name,
  1112. )
  1113. compatible_with = ["@platforms//cpu:" + cpu, "@platforms//os:" + os]
  1114. native.toolchain(
  1115. name = config_name,
  1116. exec_compatible_with = compatible_with,
  1117. target_compatible_with = compatible_with,
  1118. toolchain = config_name + "_tools",
  1119. toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
  1120. )