clang_cc_toolchain_config.bzl 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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(
  6. "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
  7. "action_config",
  8. "feature",
  9. "feature_set",
  10. "flag_group",
  11. "flag_set",
  12. "tool",
  13. "tool_path",
  14. "variable_with_value",
  15. "with_feature_set",
  16. )
  17. load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
  18. load(
  19. ":clang_detected_variables.bzl",
  20. "clang_include_dirs_list",
  21. "clang_resource_dir",
  22. "llvm_bindir",
  23. "sysroot_dir",
  24. )
  25. all_c_compile_actions = [
  26. ACTION_NAMES.c_compile,
  27. ACTION_NAMES.assemble,
  28. ACTION_NAMES.preprocess_assemble,
  29. ]
  30. all_cpp_compile_actions = [
  31. ACTION_NAMES.cpp_compile,
  32. ACTION_NAMES.linkstamp_compile,
  33. ACTION_NAMES.cpp_header_parsing,
  34. ACTION_NAMES.cpp_module_compile,
  35. ACTION_NAMES.cpp_module_codegen,
  36. ]
  37. all_compile_actions = all_c_compile_actions + all_cpp_compile_actions
  38. preprocessor_compile_actions = [
  39. ACTION_NAMES.c_compile,
  40. ACTION_NAMES.cpp_compile,
  41. ACTION_NAMES.linkstamp_compile,
  42. ACTION_NAMES.preprocess_assemble,
  43. ACTION_NAMES.cpp_header_parsing,
  44. ACTION_NAMES.cpp_module_compile,
  45. ]
  46. codegen_compile_actions = [
  47. ACTION_NAMES.c_compile,
  48. ACTION_NAMES.cpp_compile,
  49. ACTION_NAMES.linkstamp_compile,
  50. ACTION_NAMES.assemble,
  51. ACTION_NAMES.preprocess_assemble,
  52. ACTION_NAMES.cpp_module_codegen,
  53. ]
  54. all_link_actions = [
  55. ACTION_NAMES.cpp_link_executable,
  56. ACTION_NAMES.cpp_link_dynamic_library,
  57. ACTION_NAMES.cpp_link_nodeps_dynamic_library,
  58. ]
  59. def _impl(ctx):
  60. tool_paths = [
  61. tool_path(name = "ar", path = llvm_bindir + "/llvm-ar"),
  62. tool_path(name = "ld", path = llvm_bindir + "/ld.lld"),
  63. tool_path(name = "cpp", path = llvm_bindir + "/clang-cpp"),
  64. tool_path(name = "gcc", path = llvm_bindir + "/clang++"),
  65. tool_path(name = "dwp", path = llvm_bindir + "/llvm-dwp"),
  66. tool_path(name = "gcov", path = llvm_bindir + "/llvm-cov"),
  67. tool_path(name = "nm", path = llvm_bindir + "/llvm-nm"),
  68. tool_path(name = "objcopy", path = llvm_bindir + "/llvm-objcopy"),
  69. tool_path(name = "objdump", path = llvm_bindir + "/llvm-objdump"),
  70. tool_path(name = "strip", path = llvm_bindir + "/llvm-strip"),
  71. ]
  72. action_configs = [
  73. action_config(action_name = name, enabled = True, tools = [tool(path = llvm_bindir + "/clang")])
  74. for name in all_c_compile_actions
  75. ] + [
  76. action_config(action_name = name, enabled = True, tools = [tool(path = llvm_bindir + "/clang++")])
  77. for name in all_cpp_compile_actions
  78. ] + [
  79. action_config(action_name = name, enabled = True, tools = [tool(path = llvm_bindir + "/clang++")])
  80. for name in all_link_actions
  81. ] + [
  82. action_config(action_name = name, enabled = True, tools = [tool(path = llvm_bindir + "/llvm-ar")])
  83. for name in [ACTION_NAMES.cpp_link_static_library]
  84. ] + [
  85. action_config(action_name = name, enabled = True, tools = [tool(path = llvm_bindir + "/llvm-strip")])
  86. for name in [ACTION_NAMES.strip]
  87. ]
  88. default_flags_feature = feature(
  89. name = "default_flags",
  90. enabled = True,
  91. flag_sets = [
  92. flag_set(
  93. actions = all_compile_actions + all_link_actions,
  94. flag_groups = ([
  95. flag_group(
  96. flags = [
  97. "-no-canonical-prefixes",
  98. "-fcolor-diagnostics",
  99. ],
  100. ),
  101. ]),
  102. ),
  103. flag_set(
  104. actions = all_compile_actions,
  105. flag_groups = ([
  106. flag_group(
  107. flags = [
  108. "-Werror",
  109. "-Wall",
  110. "-Wextra",
  111. "-Wthread-safety",
  112. "-Wself-assign",
  113. "-Wimplicit-fallthrough",
  114. "-Wctad-maybe-unsupported",
  115. "-Wnon-virtual-dtor",
  116. # Unfortunately, LLVM isn't clean for this warning.
  117. "-Wno-unused-parameter",
  118. # Compile actions shouldn't link anything.
  119. "-c",
  120. ],
  121. ),
  122. flag_group(
  123. expand_if_available = "output_assembly_file",
  124. flags = ["-S"],
  125. ),
  126. flag_group(
  127. expand_if_available = "output_preprocess_file",
  128. flags = ["-E"],
  129. ),
  130. flag_group(
  131. flags = ["-MD", "-MF", "%{dependency_file}"],
  132. expand_if_available = "dependency_file",
  133. ),
  134. flag_group(
  135. flags = ["-frandom-seed=%{output_file}"],
  136. expand_if_available = "output_file",
  137. ),
  138. ]),
  139. ),
  140. flag_set(
  141. actions = all_cpp_compile_actions + all_link_actions,
  142. flag_groups = ([
  143. flag_group(
  144. flags = [
  145. "-std=c++17",
  146. "-stdlib=libc++",
  147. ],
  148. ),
  149. ]),
  150. ),
  151. flag_set(
  152. actions = codegen_compile_actions,
  153. flag_groups = ([
  154. flag_group(
  155. flags = [
  156. "-ffunction-sections",
  157. "-fdata-sections",
  158. ],
  159. ),
  160. ]),
  161. ),
  162. flag_set(
  163. actions = codegen_compile_actions,
  164. flag_groups = [
  165. flag_group(flags = ["-fPIC"], expand_if_available = "pic"),
  166. ],
  167. ),
  168. flag_set(
  169. actions = preprocessor_compile_actions,
  170. flag_groups = [
  171. flag_group(
  172. flags = [
  173. # Disable a warning and override builtin macros to
  174. # ensure a hermetic build.
  175. "-Wno-builtin-macro-redefined",
  176. "-D__DATE__=\"redacted\"",
  177. "-D__TIMESTAMP__=\"redacted\"",
  178. "-D__TIME__=\"redacted\"",
  179. ],
  180. ),
  181. flag_group(
  182. flags = ["-D%{preprocessor_defines}"],
  183. iterate_over = "preprocessor_defines",
  184. ),
  185. flag_group(
  186. flags = ["-include", "%{includes}"],
  187. iterate_over = "includes",
  188. expand_if_available = "includes",
  189. ),
  190. flag_group(
  191. flags = ["-iquote", "%{quote_include_paths}"],
  192. iterate_over = "quote_include_paths",
  193. ),
  194. flag_group(
  195. flags = ["-I%{include_paths}"],
  196. iterate_over = "include_paths",
  197. ),
  198. flag_group(
  199. flags = ["-isystem", "%{system_include_paths}"],
  200. iterate_over = "system_include_paths",
  201. ),
  202. ],
  203. ),
  204. flag_set(
  205. actions = [
  206. ACTION_NAMES.cpp_link_dynamic_library,
  207. ACTION_NAMES.cpp_link_nodeps_dynamic_library,
  208. ],
  209. flag_groups = [flag_group(flags = ["-shared"])],
  210. ),
  211. flag_set(
  212. actions = [
  213. ACTION_NAMES.cpp_link_executable,
  214. ],
  215. flag_groups = [
  216. flag_group(
  217. flags = ["-pie"],
  218. expand_if_available = "force_pic",
  219. ),
  220. ],
  221. ),
  222. flag_set(
  223. actions = all_link_actions,
  224. flag_groups = [
  225. flag_group(
  226. flags = ["-Wl,--gdb-index"],
  227. expand_if_available = "is_using_fission",
  228. ),
  229. flag_group(
  230. flags = ["-Wl,-S"],
  231. expand_if_available = "strip_debug_symbols",
  232. ),
  233. flag_group(
  234. flags = ["-L%{library_search_directories}"],
  235. iterate_over = "library_search_directories",
  236. expand_if_available = "library_search_directories",
  237. ),
  238. flag_group(
  239. iterate_over = "runtime_library_search_directories",
  240. flags = [
  241. "-Wl,-rpath,$ORIGIN/%{runtime_library_search_directories}",
  242. ],
  243. expand_if_available =
  244. "runtime_library_search_directories",
  245. ),
  246. ],
  247. ),
  248. ],
  249. )
  250. # Handle different levels of optimization with individual features so that
  251. # they can be ordered and the defaults can override the minimal settings if
  252. # both are enabled.
  253. minimal_optimization_flags = feature(
  254. name = "minimal_optimization_flags",
  255. flag_sets = [
  256. flag_set(
  257. actions = codegen_compile_actions,
  258. flag_groups = [flag_group(flags = [
  259. "-O1",
  260. ])],
  261. ),
  262. # Use a conditional flag set for enabling the fast instruction
  263. # selector to work around an LLVM bug:
  264. # https://github.com/llvm/llvm-project/issues/56133
  265. flag_set(
  266. actions = codegen_compile_actions,
  267. flag_groups = [flag_group(flags = [
  268. "-mllvm",
  269. "-fast-isel",
  270. ])],
  271. with_features = [
  272. with_feature_set(not_features = ["fuzzer"]),
  273. ],
  274. ),
  275. ],
  276. )
  277. default_optimization_flags = feature(
  278. name = "default_optimization_flags",
  279. enabled = True,
  280. requires = [feature_set(["opt"])],
  281. flag_sets = [
  282. flag_set(
  283. actions = all_compile_actions,
  284. flag_groups = [flag_group(flags = [
  285. "-DNDEBUG",
  286. ])],
  287. ),
  288. flag_set(
  289. actions = codegen_compile_actions,
  290. flag_groups = [flag_group(flags = [
  291. "-O3",
  292. ])],
  293. ),
  294. ],
  295. )
  296. # Handle different levels and forms of debug info emission with individual
  297. # features so that they can be ordered and the defaults can override the
  298. # minimal settings if both are enabled.
  299. minimal_debug_info_flags = feature(
  300. name = "minimal_debug_info_flags",
  301. flag_sets = [flag_set(
  302. actions = codegen_compile_actions,
  303. flag_groups = [flag_group(flags = [
  304. "-gmlt",
  305. ])],
  306. )],
  307. )
  308. default_debug_info_flags = feature(
  309. name = "default_debug_info_flags",
  310. enabled = True,
  311. flag_sets = [
  312. flag_set(
  313. actions = codegen_compile_actions,
  314. flag_groups = ([
  315. flag_group(
  316. flags = ["-g"],
  317. ),
  318. ]),
  319. with_features = [with_feature_set(features = ["dbg"])],
  320. ),
  321. flag_set(
  322. actions = codegen_compile_actions,
  323. flag_groups = [
  324. flag_group(
  325. flags = ["-gsplit-dwarf", "-g"],
  326. expand_if_available = "per_object_debug_info_file",
  327. ),
  328. ],
  329. ),
  330. ],
  331. )
  332. # This feature can be enabled in conjunction with any optimizations to
  333. # ensure accurate call stacks and backtraces for profilers or errors.
  334. preserve_call_stacks = feature(
  335. name = "preserve_call_stacks",
  336. flag_sets = [flag_set(
  337. actions = codegen_compile_actions,
  338. flag_groups = [flag_group(flags = [
  339. # Ensure good backtraces by preserving frame pointers and
  340. # disabling tail call elimination.
  341. "-fno-omit-frame-pointer",
  342. "-mno-omit-leaf-frame-pointer",
  343. "-fno-optimize-sibling-calls",
  344. ])],
  345. )],
  346. )
  347. sysroot_feature = feature(
  348. name = "sysroot",
  349. enabled = True,
  350. flag_sets = [
  351. flag_set(
  352. actions = all_compile_actions + all_link_actions,
  353. flag_groups = [
  354. flag_group(
  355. flags = ["--sysroot=%{sysroot}"],
  356. expand_if_available = "sysroot",
  357. ),
  358. ],
  359. ),
  360. ],
  361. )
  362. use_module_maps = feature(
  363. name = "use_module_maps",
  364. requires = [feature_set(features = ["module_maps"])],
  365. flag_sets = [
  366. flag_set(
  367. actions = [
  368. ACTION_NAMES.c_compile,
  369. ACTION_NAMES.cpp_compile,
  370. ACTION_NAMES.cpp_header_parsing,
  371. ACTION_NAMES.cpp_module_compile,
  372. ],
  373. flag_groups = [
  374. # These flag groups are separate so they do not expand to
  375. # the cross product of the variables.
  376. flag_group(flags = ["-fmodule-name=%{module_name}"]),
  377. flag_group(
  378. flags = ["-fmodule-map-file=%{module_map_file}"],
  379. ),
  380. ],
  381. ),
  382. ],
  383. )
  384. # Tell bazel we support module maps in general, so they will be generated
  385. # for all c/c++ rules.
  386. # Note: not all C++ rules support module maps; thus, do not imply this
  387. # feature from other features - instead, require it.
  388. module_maps = feature(
  389. name = "module_maps",
  390. enabled = True,
  391. implies = [
  392. # "module_map_home_cwd",
  393. # "module_map_without_extern_module",
  394. # "generate_submodules",
  395. ],
  396. )
  397. layering_check = feature(
  398. name = "layering_check",
  399. implies = ["use_module_maps"],
  400. flag_sets = [
  401. flag_set(
  402. actions = [
  403. ACTION_NAMES.c_compile,
  404. ACTION_NAMES.cpp_compile,
  405. ACTION_NAMES.cpp_header_parsing,
  406. ACTION_NAMES.cpp_module_compile,
  407. ],
  408. flag_groups = [
  409. flag_group(flags = [
  410. "-fmodules-strict-decluse",
  411. "-Wprivate-header",
  412. ]),
  413. flag_group(
  414. iterate_over = "dependent_module_map_files",
  415. flags = [
  416. "-fmodule-map-file=%{dependent_module_map_files}",
  417. ],
  418. ),
  419. ],
  420. ),
  421. ],
  422. )
  423. sanitizer_common_flags = feature(
  424. name = "sanitizer_common_flags",
  425. requires = [feature_set(["nonhost"])],
  426. implies = ["minimal_optimization_flags", "minimal_debug_info_flags", "preserve_call_stacks"],
  427. flag_sets = [flag_set(
  428. actions = all_link_actions,
  429. flag_groups = [flag_group(flags = [
  430. "-static-libsan",
  431. ])],
  432. )],
  433. )
  434. asan = feature(
  435. name = "asan",
  436. requires = [feature_set(["nonhost"])],
  437. implies = ["sanitizer_common_flags"],
  438. flag_sets = [flag_set(
  439. actions = all_compile_actions + all_link_actions,
  440. flag_groups = [flag_group(flags = [
  441. "-fsanitize=address,undefined,nullability",
  442. "-fsanitize-address-use-after-scope",
  443. # We don't need the recovery behavior of UBSan as we expect
  444. # builds to be clean. Not recoverying is a bit cheaper.
  445. "-fno-sanitize-recover=undefined",
  446. # Don't embed the full path name for files. This limits the size
  447. # and combined with line numbers is unlikely to result in many
  448. # ambiguities.
  449. "-fsanitize-undefined-strip-path-components=-1",
  450. # Needed due to clang AST issues, such as in
  451. # clang/AST/Redeclarable.h line 199.
  452. "-fno-sanitize=vptr",
  453. ])],
  454. )],
  455. )
  456. enable_asan_in_fastbuild = feature(
  457. name = "enable_asan_in_fastbuild",
  458. enabled = True,
  459. requires = [feature_set(["nonhost", "fastbuild"])],
  460. implies = ["asan"],
  461. )
  462. fuzzer = feature(
  463. name = "fuzzer",
  464. requires = [feature_set(["nonhost"])],
  465. implies = ["asan"],
  466. flag_sets = [flag_set(
  467. actions = all_compile_actions + all_link_actions,
  468. flag_groups = [flag_group(flags = [
  469. "-fsanitize=fuzzer-no-link",
  470. ])],
  471. )],
  472. )
  473. proto_fuzzer = feature(
  474. name = "proto-fuzzer",
  475. enabled = False,
  476. requires = [feature_set(["nonhost"])],
  477. implies = ["fuzzer"],
  478. )
  479. linux_flags_feature = feature(
  480. name = "linux_flags",
  481. enabled = True,
  482. flag_sets = [
  483. flag_set(
  484. actions = all_link_actions,
  485. flag_groups = ([
  486. flag_group(
  487. flags = [
  488. "-fuse-ld=lld",
  489. "-stdlib=libc++",
  490. "-unwindlib=libunwind",
  491. # Force the C++ standard library and runtime
  492. # libraries to be statically linked. This works even
  493. # with libc++ and libunwind despite the names,
  494. # provided libc++ is built with two CMake options:
  495. # - `-DCMAKE_POSITION_INDEPENDENT_CODE=ON`
  496. # - `-DLIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY`
  497. # These are both required because of PR43604
  498. # (impacting at least Debian packages of libc++) and
  499. # PR46321 (impacting most other packages).
  500. # We recommend using Homebrew's LLVM install on
  501. # Linux.
  502. "-static-libstdc++",
  503. "-static-libgcc",
  504. # Link with Clang's runtime library. This is always
  505. # linked statically.
  506. "-rtlib=compiler-rt",
  507. # Explicitly add LLVM libs to the search path to
  508. # preempt the detected GCC installation's library
  509. # paths. Those might have a system installed libc++
  510. # and we want to find the one next to our Clang.
  511. "-L" + llvm_bindir + "/../lib",
  512. # Link with pthread.
  513. "-lpthread",
  514. ],
  515. ),
  516. ]),
  517. ),
  518. flag_set(
  519. actions = all_compile_actions,
  520. flag_groups = [flag_group(flags = [
  521. # Enable libc++'s debug features.
  522. "-D_LIBCPP_DEBUG=1",
  523. ])],
  524. with_features = [
  525. with_feature_set(not_features = ["opt"]),
  526. ],
  527. ),
  528. ],
  529. )
  530. default_link_libraries_feature = feature(
  531. name = "default_link_libraries",
  532. enabled = True,
  533. flag_sets = [
  534. flag_set(
  535. actions = all_link_actions,
  536. flag_groups = [
  537. flag_group(
  538. flags = ["%{linkstamp_paths}"],
  539. iterate_over = "linkstamp_paths",
  540. expand_if_available = "linkstamp_paths",
  541. ),
  542. flag_group(
  543. iterate_over = "libraries_to_link",
  544. flag_groups = [
  545. flag_group(
  546. flags = ["-Wl,--start-lib"],
  547. expand_if_equal = variable_with_value(
  548. name = "libraries_to_link.type",
  549. value = "object_file_group",
  550. ),
  551. ),
  552. flag_group(
  553. flags = ["-Wl,-whole-archive"],
  554. expand_if_true =
  555. "libraries_to_link.is_whole_archive",
  556. ),
  557. flag_group(
  558. flags = ["%{libraries_to_link.object_files}"],
  559. iterate_over = "libraries_to_link.object_files",
  560. expand_if_equal = variable_with_value(
  561. name = "libraries_to_link.type",
  562. value = "object_file_group",
  563. ),
  564. ),
  565. flag_group(
  566. flags = ["%{libraries_to_link.name}"],
  567. expand_if_equal = variable_with_value(
  568. name = "libraries_to_link.type",
  569. value = "object_file",
  570. ),
  571. ),
  572. flag_group(
  573. flags = ["%{libraries_to_link.name}"],
  574. expand_if_equal = variable_with_value(
  575. name = "libraries_to_link.type",
  576. value = "interface_library",
  577. ),
  578. ),
  579. flag_group(
  580. flags = ["%{libraries_to_link.name}"],
  581. expand_if_equal = variable_with_value(
  582. name = "libraries_to_link.type",
  583. value = "static_library",
  584. ),
  585. ),
  586. flag_group(
  587. flags = ["-l%{libraries_to_link.name}"],
  588. expand_if_equal = variable_with_value(
  589. name = "libraries_to_link.type",
  590. value = "dynamic_library",
  591. ),
  592. ),
  593. flag_group(
  594. flags = ["-l:%{libraries_to_link.name}"],
  595. expand_if_equal = variable_with_value(
  596. name = "libraries_to_link.type",
  597. value = "versioned_dynamic_library",
  598. ),
  599. ),
  600. flag_group(
  601. flags = ["-Wl,-no-whole-archive"],
  602. expand_if_true = "libraries_to_link.is_whole_archive",
  603. ),
  604. flag_group(
  605. flags = ["-Wl,--end-lib"],
  606. expand_if_equal = variable_with_value(
  607. name = "libraries_to_link.type",
  608. value = "object_file_group",
  609. ),
  610. ),
  611. ],
  612. expand_if_available = "libraries_to_link",
  613. ),
  614. # Note that the params file comes at the end, after the
  615. # libraries to link above.
  616. flag_group(
  617. expand_if_available = "linker_param_file",
  618. flags = ["@%{linker_param_file}"],
  619. ),
  620. ],
  621. ),
  622. ],
  623. )
  624. # Place user provided compile flags after all the features so that these
  625. # flags can override or customize behavior. The only thing user flags
  626. # cannot override is the output file as Bazel depends on that.
  627. #
  628. # Finally, place the source file (if present) and output file last to make
  629. # reading the compile command lines easier for humans.
  630. final_flags_feature = feature(
  631. name = "final_flags",
  632. enabled = True,
  633. flag_sets = [
  634. flag_set(
  635. actions = all_compile_actions,
  636. flag_groups = [
  637. flag_group(
  638. flags = ["%{user_compile_flags}"],
  639. iterate_over = "user_compile_flags",
  640. expand_if_available = "user_compile_flags",
  641. ),
  642. flag_group(
  643. flags = ["%{source_file}"],
  644. expand_if_available = "source_file",
  645. ),
  646. flag_group(
  647. expand_if_available = "output_file",
  648. flags = ["-o", "%{output_file}"],
  649. ),
  650. ],
  651. ),
  652. flag_set(
  653. actions = all_link_actions,
  654. flag_groups = [
  655. flag_group(
  656. flags = ["%{user_link_flags}"],
  657. iterate_over = "user_link_flags",
  658. expand_if_available = "user_link_flags",
  659. ),
  660. flag_group(
  661. flags = ["-o", "%{output_execpath}"],
  662. expand_if_available = "output_execpath",
  663. ),
  664. ],
  665. ),
  666. ],
  667. )
  668. # Archive actions have an entirely independent set of flags and don't
  669. # interact with either compiler or link actions.
  670. default_archiver_flags_feature = feature(
  671. name = "default_archiver_flags",
  672. enabled = True,
  673. flag_sets = [
  674. flag_set(
  675. actions = [ACTION_NAMES.cpp_link_static_library],
  676. flag_groups = [
  677. flag_group(flags = ["rcsD"]),
  678. flag_group(
  679. flags = ["%{output_execpath}"],
  680. expand_if_available = "output_execpath",
  681. ),
  682. flag_group(
  683. iterate_over = "libraries_to_link",
  684. flag_groups = [
  685. flag_group(
  686. flags = ["%{libraries_to_link.name}"],
  687. expand_if_equal = variable_with_value(
  688. name = "libraries_to_link.type",
  689. value = "object_file",
  690. ),
  691. ),
  692. flag_group(
  693. flags = ["%{libraries_to_link.object_files}"],
  694. iterate_over = "libraries_to_link.object_files",
  695. expand_if_equal = variable_with_value(
  696. name = "libraries_to_link.type",
  697. value = "object_file_group",
  698. ),
  699. ),
  700. ],
  701. expand_if_available = "libraries_to_link",
  702. ),
  703. flag_group(
  704. expand_if_available = "linker_param_file",
  705. flags = ["@%{linker_param_file}"],
  706. ),
  707. ],
  708. ),
  709. ],
  710. )
  711. # Now that we have built up the constituent feature definitions, compose
  712. # them, including configuration based on the target platform. Currently,
  713. # the target platform is configured with the "cpu" attribute for legacy
  714. # reasons. Further, for legacy reasons the default is a Linux OS target and
  715. # the x88-64 CPU name is "k8".
  716. # First, define features that are simply used to configure others.
  717. features = [
  718. feature(name = "dbg"),
  719. feature(name = "fastbuild"),
  720. feature(name = "host"),
  721. feature(name = "no_legacy_features"),
  722. feature(name = "nonhost"),
  723. feature(name = "opt"),
  724. feature(name = "supports_dynamic_linker", enabled = ctx.attr.target_cpu == "k8"),
  725. feature(name = "supports_pic", enabled = True),
  726. feature(name = "supports_start_end_lib", enabled = ctx.attr.target_cpu == "k8"),
  727. ]
  728. # The order of the features determines the relative order of flags used.
  729. # Start off adding the baseline features.
  730. features += [
  731. default_flags_feature,
  732. minimal_optimization_flags,
  733. default_optimization_flags,
  734. minimal_debug_info_flags,
  735. default_debug_info_flags,
  736. preserve_call_stacks,
  737. sysroot_feature,
  738. sanitizer_common_flags,
  739. asan,
  740. enable_asan_in_fastbuild,
  741. fuzzer,
  742. proto_fuzzer,
  743. layering_check,
  744. module_maps,
  745. use_module_maps,
  746. default_archiver_flags_feature,
  747. ]
  748. # Next, add the features based on the target platform. Here too the
  749. # features are order sensitive. We also setup the sysroot here.
  750. if ctx.attr.target_cpu == "k8":
  751. features += [linux_flags_feature]
  752. sysroot = None
  753. elif ctx.attr.target_cpu in ["darwin", "darwin_arm64"]:
  754. sysroot = sysroot_dir
  755. else:
  756. fail("Unsupported target platform!")
  757. # Finally append the libraries to link and any final flags.
  758. features += [
  759. default_link_libraries_feature,
  760. final_flags_feature,
  761. ]
  762. return cc_common.create_cc_toolchain_config_info(
  763. ctx = ctx,
  764. features = features,
  765. action_configs = action_configs,
  766. cxx_builtin_include_directories = clang_include_dirs_list + [
  767. # Add Clang's resource directory to the end of the builtin include
  768. # directories to cover the use of sanitizer resource files by the driver.
  769. clang_resource_dir + "/share",
  770. ],
  771. builtin_sysroot = sysroot,
  772. # This configuration only supports local non-cross builds so derive
  773. # everything from the target CPU selected.
  774. toolchain_identifier = "local-" + ctx.attr.target_cpu,
  775. host_system_name = "local-" + ctx.attr.target_cpu,
  776. target_system_name = "local-" + ctx.attr.target_cpu,
  777. target_cpu = ctx.attr.target_cpu,
  778. # These attributes aren't meaningful at all so just use placeholder
  779. # values.
  780. target_libc = "local",
  781. compiler = "local",
  782. abi_version = "local",
  783. abi_libc_version = "local",
  784. # We do have to pass in our tool paths.
  785. tool_paths = tool_paths,
  786. )
  787. cc_toolchain_config = rule(
  788. implementation = _impl,
  789. attrs = {
  790. "target_cpu": attr.string(mandatory = True),
  791. },
  792. provides = [CcToolchainConfigInfo],
  793. )