clang_cc_toolchain_config.bzl 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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_compile_actions = [
  26. ACTION_NAMES.c_compile,
  27. ACTION_NAMES.cpp_compile,
  28. ACTION_NAMES.linkstamp_compile,
  29. ACTION_NAMES.assemble,
  30. ACTION_NAMES.preprocess_assemble,
  31. ACTION_NAMES.cpp_header_parsing,
  32. ACTION_NAMES.cpp_module_compile,
  33. ACTION_NAMES.cpp_module_codegen,
  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. 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 = llvm_bindir + "/ld.lld"),
  67. tool_path(name = "cpp", path = llvm_bindir + "/clang-cpp"),
  68. tool_path(name = "gcc", path = llvm_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 = llvm_bindir + "/clang")])
  78. for name in [ACTION_NAMES.c_compile]
  79. ] + [
  80. action_config(action_name = name, enabled = True, tools = [tool(path = llvm_bindir + "/clang++")])
  81. for name in all_cpp_compile_actions
  82. ] + [
  83. action_config(action_name = name, enabled = True, tools = [tool(path = llvm_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. default_compile_flags_feature = feature(
  93. name = "default_compile_flags",
  94. enabled = True,
  95. flag_sets = [
  96. flag_set(
  97. actions = all_compile_actions,
  98. flag_groups = ([
  99. flag_group(
  100. flags = [
  101. "-Wall",
  102. "-Wextra",
  103. "-Wthread-safety",
  104. "-Wself-assign",
  105. # Unfortunately, LLVM isn't clean for this warning.
  106. "-Wno-unused-parameter",
  107. # We use partial sets of designated initializers in
  108. # test code.
  109. "-Wno-missing-field-initializers",
  110. "-fcolor-diagnostics",
  111. ],
  112. ),
  113. ]),
  114. ),
  115. flag_set(
  116. actions = [
  117. ACTION_NAMES.assemble,
  118. ACTION_NAMES.preprocess_assemble,
  119. ACTION_NAMES.c_compile,
  120. ACTION_NAMES.cpp_compile,
  121. ACTION_NAMES.cpp_module_compile,
  122. ACTION_NAMES.cpp_header_parsing,
  123. ],
  124. flag_groups = [
  125. flag_group(
  126. flags = ["-MD", "-MF", "%{dependency_file}"],
  127. expand_if_available = "dependency_file",
  128. ),
  129. ],
  130. ),
  131. flag_set(
  132. actions = [
  133. ACTION_NAMES.c_compile,
  134. ACTION_NAMES.cpp_compile,
  135. ACTION_NAMES.cpp_module_codegen,
  136. ACTION_NAMES.cpp_module_compile,
  137. ],
  138. flag_groups = [
  139. flag_group(
  140. flags = ["-frandom-seed=%{output_file}"],
  141. expand_if_available = "output_file",
  142. ),
  143. ],
  144. ),
  145. flag_set(
  146. actions = [
  147. ACTION_NAMES.assemble,
  148. ACTION_NAMES.preprocess_assemble,
  149. ACTION_NAMES.linkstamp_compile,
  150. ACTION_NAMES.c_compile,
  151. ACTION_NAMES.cpp_compile,
  152. ACTION_NAMES.cpp_module_codegen,
  153. ACTION_NAMES.cpp_module_compile,
  154. ],
  155. flag_groups = [
  156. flag_group(flags = ["-fPIC"], expand_if_available = "pic"),
  157. ],
  158. ),
  159. flag_set(
  160. actions = [
  161. ACTION_NAMES.assemble,
  162. ACTION_NAMES.preprocess_assemble,
  163. ACTION_NAMES.c_compile,
  164. ACTION_NAMES.cpp_compile,
  165. ACTION_NAMES.cpp_module_codegen,
  166. ],
  167. flag_groups = [
  168. flag_group(
  169. flags = ["-gsplit-dwarf", "-g"],
  170. expand_if_available = "per_object_debug_info_file",
  171. ),
  172. ],
  173. ),
  174. flag_set(
  175. actions = [
  176. ACTION_NAMES.preprocess_assemble,
  177. ACTION_NAMES.linkstamp_compile,
  178. ACTION_NAMES.c_compile,
  179. ACTION_NAMES.cpp_compile,
  180. ACTION_NAMES.cpp_header_parsing,
  181. ACTION_NAMES.cpp_module_compile,
  182. ],
  183. flag_groups = [
  184. flag_group(
  185. flags = ["-D%{preprocessor_defines}"],
  186. iterate_over = "preprocessor_defines",
  187. ),
  188. ],
  189. ),
  190. flag_set(
  191. actions = [
  192. ACTION_NAMES.preprocess_assemble,
  193. ACTION_NAMES.linkstamp_compile,
  194. ACTION_NAMES.c_compile,
  195. ACTION_NAMES.cpp_compile,
  196. ACTION_NAMES.cpp_header_parsing,
  197. ACTION_NAMES.cpp_module_compile,
  198. ],
  199. flag_groups = [
  200. flag_group(
  201. flags = ["-include", "%{includes}"],
  202. iterate_over = "includes",
  203. expand_if_available = "includes",
  204. ),
  205. ],
  206. ),
  207. flag_set(
  208. actions = [
  209. ACTION_NAMES.preprocess_assemble,
  210. ACTION_NAMES.linkstamp_compile,
  211. ACTION_NAMES.c_compile,
  212. ACTION_NAMES.cpp_compile,
  213. ACTION_NAMES.cpp_header_parsing,
  214. ACTION_NAMES.cpp_module_compile,
  215. ],
  216. flag_groups = [
  217. flag_group(
  218. flags = ["-iquote", "%{quote_include_paths}"],
  219. iterate_over = "quote_include_paths",
  220. ),
  221. flag_group(
  222. flags = ["-I%{include_paths}"],
  223. iterate_over = "include_paths",
  224. ),
  225. flag_group(
  226. flags = ["-isystem", "%{system_include_paths}"],
  227. iterate_over = "system_include_paths",
  228. ),
  229. ],
  230. ),
  231. flag_set(
  232. actions = all_compile_actions,
  233. flag_groups = ([
  234. flag_group(
  235. flags = ["-g"],
  236. ),
  237. ]),
  238. with_features = [with_feature_set(features = ["dbg"])],
  239. ),
  240. flag_set(
  241. actions = all_compile_actions,
  242. flag_groups = ([
  243. flag_group(
  244. flags = [
  245. "-g0",
  246. "-O3",
  247. "-DNDEBUG",
  248. "-ffunction-sections",
  249. "-fdata-sections",
  250. # Even when optimizing, preserve frame pointers for profiling.
  251. "-fno-omit-frame-pointer",
  252. "-mno-omit-leaf-frame-pointer",
  253. ],
  254. ),
  255. ]),
  256. with_features = [with_feature_set(features = ["opt"])],
  257. ),
  258. flag_set(
  259. actions = all_cpp_compile_actions,
  260. flag_groups = ([
  261. flag_group(
  262. flags = [
  263. "-std=c++17",
  264. "-stdlib=libc++",
  265. ],
  266. ),
  267. ]),
  268. ),
  269. flag_set(
  270. actions = all_compile_actions,
  271. flag_groups = [
  272. flag_group(
  273. flags = ["%{user_compile_flags}"],
  274. iterate_over = "user_compile_flags",
  275. expand_if_available = "user_compile_flags",
  276. ),
  277. ],
  278. ),
  279. flag_set(
  280. actions = all_compile_actions,
  281. flag_groups = ([
  282. flag_group(
  283. flags = [
  284. "-no-canonical-prefixes",
  285. "-Wno-builtin-macro-redefined",
  286. "-D__DATE__=\"redacted\"",
  287. "-D__TIMESTAMP__=\"redacted\"",
  288. "-D__TIME__=\"redacted\"",
  289. ],
  290. ),
  291. ]),
  292. ),
  293. flag_set(
  294. actions = all_compile_actions,
  295. flag_groups = [
  296. flag_group(
  297. expand_if_available = "source_file",
  298. flags = ["-c", "%{source_file}"],
  299. ),
  300. flag_group(
  301. expand_if_available = "output_assembly_file",
  302. flags = ["-S"],
  303. ),
  304. flag_group(
  305. expand_if_available = "output_preprocess_file",
  306. flags = ["-E"],
  307. ),
  308. flag_group(
  309. expand_if_available = "output_file",
  310. flags = ["-o", "%{output_file}"],
  311. ),
  312. ],
  313. ),
  314. ],
  315. )
  316. linux_link_flags_feature = feature(
  317. name = "linux_link_flags",
  318. enabled = True,
  319. flag_sets = [
  320. flag_set(
  321. actions = all_link_actions,
  322. flag_groups = ([
  323. flag_group(
  324. flags = [
  325. "-fuse-ld=lld",
  326. "-Wl,-no-as-needed",
  327. # Force the C++ standard library to be statically
  328. # linked. This works even with libc++ despite the
  329. # name, however we have to manually link the ABI
  330. # library and libunwind.
  331. "-static-libstdc++",
  332. # Link with libc++.
  333. "-stdlib=libc++",
  334. # Force static linking with libc++abi as well.
  335. "-l:libc++abi.a",
  336. # Link with Clang's runtime library. This is always
  337. # linked statically.
  338. #"-rtlib=compiler-rt",
  339. # Explicitly add LLVM libs to the search path to
  340. # preempt the detected GCC installation's library
  341. # paths. Those might have a system installed libc++
  342. # and we want to find the one next to our Clang.
  343. "-L" + llvm_bindir + "/../lib",
  344. ],
  345. ),
  346. ]),
  347. ),
  348. ],
  349. )
  350. default_link_flags_feature = feature(
  351. name = "default_link_flags",
  352. enabled = True,
  353. flag_sets = [
  354. flag_set(
  355. actions = [
  356. ACTION_NAMES.cpp_link_dynamic_library,
  357. ACTION_NAMES.cpp_link_nodeps_dynamic_library,
  358. ],
  359. flag_groups = [flag_group(flags = ["-shared"])],
  360. ),
  361. flag_set(
  362. actions = all_link_actions,
  363. flag_groups = [
  364. flag_group(
  365. flags = ["%{linkstamp_paths}"],
  366. iterate_over = "linkstamp_paths",
  367. expand_if_available = "linkstamp_paths",
  368. ),
  369. ],
  370. ),
  371. flag_set(
  372. actions = all_link_actions,
  373. flag_groups = [
  374. flag_group(
  375. flags = ["-o", "%{output_execpath}"],
  376. expand_if_available = "output_execpath",
  377. ),
  378. ],
  379. ),
  380. flag_set(
  381. actions = [
  382. ACTION_NAMES.cpp_link_executable,
  383. ],
  384. flag_groups = [
  385. flag_group(
  386. flags = ["-pie"],
  387. expand_if_available = "force_pic",
  388. ),
  389. ],
  390. ),
  391. flag_set(
  392. actions = all_link_actions,
  393. flag_groups = [
  394. flag_group(
  395. flags = ["-L%{library_search_directories}"],
  396. iterate_over = "library_search_directories",
  397. expand_if_available = "library_search_directories",
  398. ),
  399. ],
  400. ),
  401. flag_set(
  402. flag_groups = [
  403. flag_group(
  404. iterate_over = "runtime_library_search_directories",
  405. flags = [
  406. "-Wl,-rpath,$ORIGIN/%{runtime_library_search_directories}",
  407. ],
  408. expand_if_available =
  409. "runtime_library_search_directories",
  410. ),
  411. ],
  412. actions = all_link_actions,
  413. ),
  414. flag_set(
  415. actions = all_link_actions,
  416. flag_groups = [
  417. flag_group(
  418. flags = ["-Wl,--gdb-index"],
  419. expand_if_available = "is_using_fission",
  420. ),
  421. ],
  422. ),
  423. flag_set(
  424. actions = all_link_actions,
  425. flag_groups = [
  426. flag_group(
  427. flags = ["-Wl,-S"],
  428. expand_if_available = "strip_debug_symbols",
  429. ),
  430. ],
  431. ),
  432. flag_set(
  433. actions = all_link_actions,
  434. flag_groups = [
  435. flag_group(
  436. iterate_over = "libraries_to_link",
  437. flag_groups = [
  438. flag_group(
  439. flags = ["-Wl,--start-lib"],
  440. expand_if_equal = variable_with_value(
  441. name = "libraries_to_link.type",
  442. value = "object_file_group",
  443. ),
  444. ),
  445. flag_group(
  446. flags = ["-Wl,-whole-archive"],
  447. expand_if_true =
  448. "libraries_to_link.is_whole_archive",
  449. ),
  450. flag_group(
  451. flags = ["%{libraries_to_link.object_files}"],
  452. iterate_over = "libraries_to_link.object_files",
  453. expand_if_equal = variable_with_value(
  454. name = "libraries_to_link.type",
  455. value = "object_file_group",
  456. ),
  457. ),
  458. flag_group(
  459. flags = ["%{libraries_to_link.name}"],
  460. expand_if_equal = variable_with_value(
  461. name = "libraries_to_link.type",
  462. value = "object_file",
  463. ),
  464. ),
  465. flag_group(
  466. flags = ["%{libraries_to_link.name}"],
  467. expand_if_equal = variable_with_value(
  468. name = "libraries_to_link.type",
  469. value = "interface_library",
  470. ),
  471. ),
  472. flag_group(
  473. flags = ["%{libraries_to_link.name}"],
  474. expand_if_equal = variable_with_value(
  475. name = "libraries_to_link.type",
  476. value = "static_library",
  477. ),
  478. ),
  479. flag_group(
  480. flags = ["-l%{libraries_to_link.name}"],
  481. expand_if_equal = variable_with_value(
  482. name = "libraries_to_link.type",
  483. value = "dynamic_library",
  484. ),
  485. ),
  486. flag_group(
  487. flags = ["-l:%{libraries_to_link.name}"],
  488. expand_if_equal = variable_with_value(
  489. name = "libraries_to_link.type",
  490. value = "versioned_dynamic_library",
  491. ),
  492. ),
  493. flag_group(
  494. flags = ["-Wl,-no-whole-archive"],
  495. expand_if_true = "libraries_to_link.is_whole_archive",
  496. ),
  497. flag_group(
  498. flags = ["-Wl,--end-lib"],
  499. expand_if_equal = variable_with_value(
  500. name = "libraries_to_link.type",
  501. value = "object_file_group",
  502. ),
  503. ),
  504. ],
  505. expand_if_available = "libraries_to_link",
  506. ),
  507. ],
  508. ),
  509. flag_set(
  510. actions = all_link_actions,
  511. flag_groups = [
  512. flag_group(
  513. flags = ["%{user_link_flags}"],
  514. iterate_over = "user_link_flags",
  515. expand_if_available = "user_link_flags",
  516. ),
  517. ],
  518. ),
  519. flag_set(
  520. actions = [
  521. ACTION_NAMES.cpp_link_static_library,
  522. ] + all_link_actions,
  523. flag_groups = [
  524. flag_group(
  525. expand_if_available = "linker_param_file",
  526. flags = ["@%{linker_param_file}"],
  527. ),
  528. ],
  529. ),
  530. ],
  531. )
  532. sysroot_feature = feature(
  533. name = "sysroot",
  534. enabled = True,
  535. flag_sets = [
  536. flag_set(
  537. actions = all_compile_actions + all_link_actions,
  538. flag_groups = [
  539. flag_group(
  540. flags = ["--sysroot=%{sysroot}"],
  541. expand_if_available = "sysroot",
  542. ),
  543. ],
  544. ),
  545. ],
  546. )
  547. default_archiver_flags_feature = feature(
  548. name = "default_archiver_flags",
  549. enabled = True,
  550. flag_sets = [
  551. flag_set(
  552. actions = [ACTION_NAMES.cpp_link_static_library],
  553. flag_groups = [
  554. flag_group(flags = ["rcsD"]),
  555. flag_group(
  556. flags = ["%{output_execpath}"],
  557. expand_if_available = "output_execpath",
  558. ),
  559. ],
  560. ),
  561. flag_set(
  562. actions = [ACTION_NAMES.cpp_link_static_library],
  563. flag_groups = [
  564. flag_group(
  565. iterate_over = "libraries_to_link",
  566. flag_groups = [
  567. flag_group(
  568. flags = ["%{libraries_to_link.name}"],
  569. expand_if_equal = variable_with_value(
  570. name = "libraries_to_link.type",
  571. value = "object_file",
  572. ),
  573. ),
  574. flag_group(
  575. flags = ["%{libraries_to_link.object_files}"],
  576. iterate_over = "libraries_to_link.object_files",
  577. expand_if_equal = variable_with_value(
  578. name = "libraries_to_link.type",
  579. value = "object_file_group",
  580. ),
  581. ),
  582. ],
  583. expand_if_available = "libraries_to_link",
  584. ),
  585. ],
  586. ),
  587. ],
  588. )
  589. use_module_maps = feature(
  590. name = "use_module_maps",
  591. requires = [feature_set(features = ["module_maps"])],
  592. flag_sets = [
  593. flag_set(
  594. actions = [
  595. ACTION_NAMES.c_compile,
  596. ACTION_NAMES.cpp_compile,
  597. ACTION_NAMES.cpp_header_parsing,
  598. ACTION_NAMES.cpp_module_compile,
  599. ],
  600. flag_groups = [
  601. # These flag groups are separate so they do not expand to
  602. # the cross product of the variables.
  603. flag_group(flags = ["-fmodule-name=%{module_name}"]),
  604. flag_group(
  605. flags = ["-fmodule-map-file=%{module_map_file}"],
  606. ),
  607. ],
  608. ),
  609. ],
  610. )
  611. # Tell bazel we support module maps in general, so they will be generated
  612. # for all c/c++ rules.
  613. # Note: not all C++ rules support module maps; thus, do not imply this
  614. # feature from other features - instead, require it.
  615. module_maps = feature(
  616. name = "module_maps",
  617. enabled = True,
  618. implies = [
  619. # "module_map_home_cwd",
  620. # "module_map_without_extern_module",
  621. # "generate_submodules",
  622. ],
  623. )
  624. layering_check = feature(
  625. name = "layering_check",
  626. implies = ["use_module_maps"],
  627. flag_sets = [
  628. flag_set(
  629. actions = [
  630. ACTION_NAMES.c_compile,
  631. ACTION_NAMES.cpp_compile,
  632. ACTION_NAMES.cpp_header_parsing,
  633. ACTION_NAMES.cpp_module_compile,
  634. ],
  635. flag_groups = [
  636. flag_group(flags = [
  637. "-fmodules-strict-decluse",
  638. "-Wprivate-header",
  639. ]),
  640. flag_group(
  641. iterate_over = "dependent_module_map_files",
  642. flags = [
  643. "-fmodule-map-file=%{dependent_module_map_files}",
  644. ],
  645. ),
  646. ],
  647. ),
  648. ],
  649. )
  650. fuzzer = feature(
  651. name = "fuzzer",
  652. flag_sets = [
  653. flag_set(
  654. actions = all_compile_actions + all_link_actions,
  655. flag_groups = [
  656. flag_group(
  657. flags = ["-fsanitize=fuzzer,address"],
  658. ),
  659. ],
  660. ),
  661. flag_set(
  662. actions = all_link_actions,
  663. flag_groups = ([
  664. flag_group(
  665. flags = [
  666. "-static-libsan",
  667. ],
  668. ),
  669. ]),
  670. ),
  671. ],
  672. )
  673. common_features = [
  674. feature(name = "no_legacy_features"),
  675. feature(name = "supports_pic", enabled = True),
  676. default_compile_flags_feature,
  677. default_archiver_flags_feature,
  678. default_link_flags_feature,
  679. feature(name = "dbg"),
  680. feature(name = "opt"),
  681. sysroot_feature,
  682. fuzzer,
  683. module_maps,
  684. layering_check,
  685. use_module_maps,
  686. ]
  687. # Select the features and builtin include directories based on the target
  688. # platform. Currently, this is configured with the "cpu" attribute for
  689. # legacy reasons. Further, for legacy reasons the default is a Linux OS
  690. # target and the x88-64 CPU name is "k8".
  691. if (ctx.attr.target_cpu == "k8"):
  692. features = common_features + [
  693. linux_link_flags_feature,
  694. feature(name = "supports_start_end_lib", enabled = True),
  695. feature(name = "supports_dynamic_linker", enabled = True),
  696. ]
  697. sysroot = None
  698. elif (ctx.attr.target_cpu == "darwin"):
  699. features = common_features
  700. sysroot = sysroot_dir
  701. else:
  702. fail("Unsupported target platform!")
  703. return cc_common.create_cc_toolchain_config_info(
  704. ctx = ctx,
  705. features = features,
  706. action_configs = action_configs,
  707. cxx_builtin_include_directories = clang_include_dirs_list + [
  708. # Add Clang's resource directory to the end of the builtin include
  709. # directories to cover the use of sanitizer resource files by the driver.
  710. clang_resource_dir + "/share",
  711. ],
  712. builtin_sysroot = sysroot,
  713. # This configuration only supports local non-cross builds so derive
  714. # everything from the target CPU selected.
  715. toolchain_identifier = "local-" + ctx.attr.target_cpu,
  716. host_system_name = "local-" + ctx.attr.target_cpu,
  717. target_system_name = "local-" + ctx.attr.target_cpu,
  718. target_cpu = ctx.attr.target_cpu,
  719. # These attributes aren't meaningful at all so just use placeholder
  720. # values.
  721. target_libc = "local",
  722. compiler = "local",
  723. abi_version = "local",
  724. abi_libc_version = "local",
  725. # We do have to pass in our tool paths.
  726. tool_paths = tool_paths,
  727. )
  728. cc_toolchain_config = rule(
  729. implementation = _impl,
  730. attrs = {
  731. "target_cpu": attr.string(mandatory = True),
  732. },
  733. provides = [CcToolchainConfigInfo],
  734. )