clang_cc_toolchain_config.bzl 50 KB

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