cc_toolchain_cpp_features.bzl 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. """Definitions of general C++ `cc_toolchain_config` features."""
  5. load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES")
  6. load(
  7. "@rules_cc//cc:cc_toolchain_config_lib.bzl",
  8. "feature",
  9. "flag_group",
  10. "flag_set",
  11. "with_feature_set",
  12. )
  13. load(
  14. ":cc_toolchain_actions.bzl",
  15. "all_compile_actions",
  16. "all_cpp_compile_actions",
  17. "all_link_actions",
  18. "codegen_compile_actions",
  19. "preprocessor_compile_actions",
  20. )
  21. clang_feature = feature(
  22. name = "clang",
  23. enabled = True,
  24. flag_sets = [
  25. flag_set(
  26. actions = all_compile_actions + all_link_actions,
  27. flag_groups = [
  28. flag_group(flags = [
  29. "-no-canonical-prefixes",
  30. "-fcolor-diagnostics",
  31. ]),
  32. flag_group(
  33. expand_if_available = "sysroot",
  34. flags = ["--sysroot=%{sysroot}"],
  35. ),
  36. ],
  37. ),
  38. flag_set(
  39. actions = all_compile_actions,
  40. flag_groups = [
  41. flag_group(flags = [
  42. # Compile actions shouldn't link anything.
  43. "-c",
  44. ]),
  45. # Flags controlling the production of specific outputs from
  46. # compile actions.
  47. flag_group(
  48. expand_if_available = "output_assembly_file",
  49. flags = ["-S"],
  50. ),
  51. flag_group(
  52. expand_if_available = "output_preprocess_file",
  53. flags = ["-E"],
  54. ),
  55. flag_group(
  56. expand_if_available = "dependency_file",
  57. flags = ["-MD", "-MF", "%{dependency_file}"],
  58. ),
  59. flag_group(
  60. expand_if_available = "output_file",
  61. flags = ["-frandom-seed=%{output_file}"],
  62. ),
  63. ],
  64. ),
  65. flag_set(
  66. # Flags specific to compiling C++ sources.
  67. actions = all_cpp_compile_actions,
  68. flag_groups = [flag_group(flags = [
  69. "-std=c++20",
  70. ])],
  71. ),
  72. flag_set(
  73. actions = codegen_compile_actions,
  74. flag_groups = [flag_group(flags = [
  75. "-ffunction-sections",
  76. "-fdata-sections",
  77. ])],
  78. ),
  79. flag_set(
  80. actions = codegen_compile_actions,
  81. flag_groups = [flag_group(
  82. expand_if_available = "pic",
  83. flags = ["-fPIC"],
  84. )],
  85. ),
  86. flag_set(
  87. actions = preprocessor_compile_actions,
  88. flag_groups = [
  89. flag_group(flags = [
  90. # Disable a warning and override builtin macros to
  91. # ensure a hermetic build.
  92. "-Wno-builtin-macro-redefined",
  93. "-D__DATE__=\"redacted\"",
  94. "-D__TIMESTAMP__=\"redacted\"",
  95. "-D__TIME__=\"redacted\"",
  96. ]),
  97. flag_group(
  98. flags = ["-D%{preprocessor_defines}"],
  99. iterate_over = "preprocessor_defines",
  100. ),
  101. flag_group(
  102. expand_if_available = "includes",
  103. flags = ["-include", "%{includes}"],
  104. iterate_over = "includes",
  105. ),
  106. flag_group(
  107. flags = ["-iquote", "%{quote_include_paths}"],
  108. iterate_over = "quote_include_paths",
  109. ),
  110. flag_group(
  111. flags = ["-I%{include_paths}"],
  112. iterate_over = "include_paths",
  113. ),
  114. flag_group(
  115. flags = ["-isystem", "%{system_include_paths}"],
  116. iterate_over = "system_include_paths",
  117. ),
  118. ],
  119. ),
  120. flag_set(
  121. actions = [
  122. ACTION_NAMES.cpp_link_dynamic_library,
  123. ACTION_NAMES.cpp_link_nodeps_dynamic_library,
  124. ],
  125. flag_groups = [flag_group(flags = ["-shared"])],
  126. ),
  127. flag_set(
  128. actions = all_link_actions,
  129. flag_groups = [
  130. flag_group(
  131. expand_if_available = "strip_debug_symbols",
  132. flags = ["-Wl,-S"],
  133. ),
  134. flag_group(
  135. expand_if_available = "library_search_directories",
  136. flags = ["-L%{library_search_directories}"],
  137. iterate_over = "library_search_directories",
  138. ),
  139. flag_group(
  140. expand_if_available =
  141. "runtime_library_search_directories",
  142. iterate_over = "runtime_library_search_directories",
  143. flags = [
  144. "-Wl,-rpath,$ORIGIN/%{runtime_library_search_directories}",
  145. ],
  146. ),
  147. ],
  148. ),
  149. ],
  150. )
  151. clang_warnings_feature = feature(
  152. name = "clang_warnings",
  153. enabled = True,
  154. flag_sets = [flag_set(
  155. actions = all_compile_actions,
  156. flag_groups = [flag_group(flags = [
  157. "-Werror",
  158. "-Wall",
  159. "-Wextra",
  160. "-Wthread-safety",
  161. "-Wself-assign",
  162. "-Wimplicit-fallthrough",
  163. "-Wctad-maybe-unsupported",
  164. "-Wextra-semi",
  165. "-Wmissing-prototypes",
  166. "-Wzero-as-null-pointer-constant",
  167. "-Wdelete-non-virtual-dtor",
  168. # TODO: Regression that warns on anonymous unions; remove depending
  169. # on fix.
  170. "-Wno-missing-designated-field-initializers",
  171. ])],
  172. )],
  173. )
  174. libcxx_feature = feature(
  175. name = "libcxx",
  176. enabled = True,
  177. flag_sets = [
  178. flag_set(
  179. actions = all_cpp_compile_actions + all_link_actions,
  180. flag_groups = [flag_group(flags = [
  181. "-stdlib=libc++",
  182. ])],
  183. with_features = [
  184. # libc++ is only used on non-Windows platforms.
  185. with_feature_set(not_features = ["windows_target"]),
  186. ],
  187. ),
  188. flag_set(
  189. actions = all_link_actions,
  190. flag_groups = [flag_group(flags = [
  191. "-unwindlib=libunwind",
  192. ])],
  193. with_features = [
  194. # libc++ is only used on non-Windows platforms.
  195. with_feature_set(not_features = ["windows_target"]),
  196. ],
  197. ),
  198. ],
  199. )