carbon_cc_toolchain_config.bzl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. """Starlark cc_toolchain configuration rules for using the Carbon toolchain"""
  5. load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES", "ACTION_NAME_GROUPS")
  6. load(
  7. "@rules_cc//cc:cc_toolchain_config_lib.bzl",
  8. "action_config",
  9. "flag_group",
  10. "flag_set",
  11. "tool",
  12. )
  13. load(
  14. "@rules_cc//cc:defs.bzl",
  15. "CcToolchainConfigInfo",
  16. "cc_toolchain",
  17. )
  18. load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
  19. load(
  20. "carbon_clang_variables.bzl",
  21. "clang_include_dirs",
  22. "clang_resource_dir",
  23. "clang_sysroot",
  24. )
  25. load(
  26. "cc_toolchain_actions.bzl",
  27. "all_c_compile_actions",
  28. )
  29. load("cc_toolchain_carbon_project_features.bzl", "carbon_project_features")
  30. load("cc_toolchain_features.bzl", "clang_cc_toolchain_features")
  31. load(
  32. ":cc_toolchain_tools.bzl",
  33. "llvm_tool_paths",
  34. )
  35. def _make_action_configs(tools, runtimes_path = None):
  36. runtimes_flag = "--no-build-runtimes"
  37. if runtimes_path:
  38. runtimes_flag = "--prebuilt-runtimes={0}".format(runtimes_path)
  39. return [
  40. action_config(
  41. action_name = name,
  42. enabled = True,
  43. tools = [tools.clang],
  44. )
  45. for name in all_c_compile_actions
  46. ] + [
  47. action_config(
  48. action_name = name,
  49. enabled = True,
  50. tools = [tools.clangpp],
  51. )
  52. for name in ACTION_NAME_GROUPS.all_cpp_compile_actions
  53. ] + [
  54. action_config(
  55. action_name = name,
  56. enabled = True,
  57. tools = [tools.carbon_busybox],
  58. flag_sets = [flag_set(flag_groups = [flag_group(flags = [
  59. runtimes_flag,
  60. "link",
  61. # We want to allow Bazel to intermingle linked object files and
  62. # Clang-spelled link flags. The first `--` starts the list of
  63. # initial object files by ending flags to the `link` subcommand,
  64. # and the second `--` switches to Clang-spelled flags.
  65. "--",
  66. "--",
  67. ])])],
  68. )
  69. for name in ACTION_NAME_GROUPS.all_cc_link_actions
  70. ] + [
  71. action_config(
  72. action_name = name,
  73. enabled = True,
  74. tools = [tools.llvm_ar],
  75. )
  76. for name in [ACTION_NAMES.cpp_link_static_library]
  77. ] + [
  78. action_config(
  79. action_name = name,
  80. enabled = True,
  81. tools = [tools.llvm_strip],
  82. )
  83. for name in [ACTION_NAMES.strip]
  84. ]
  85. def _compute_clang_system_include_dirs():
  86. system_include_dirs_start_index = None
  87. for index, dir in enumerate(clang_include_dirs):
  88. # Skip over the include search directories until we find the resource
  89. # directory. The system include directories are everything after that.
  90. if dir.startswith(clang_resource_dir):
  91. system_include_dirs_start_index = index + 1
  92. break
  93. if not system_include_dirs_start_index:
  94. fail("Could not find the resource directory in the clang include " +
  95. "directories: {}".format(clang_include_dirs))
  96. return clang_include_dirs[system_include_dirs_start_index:]
  97. def _carbon_cc_toolchain_config_impl(ctx):
  98. llvm_bindir = "llvm/bin"
  99. clang_bindir = llvm_bindir
  100. tools = struct(
  101. carbon_busybox = tool(path = "carbon-busybox"),
  102. clang = tool(path = clang_bindir + "/clang"),
  103. clangpp = tool(path = clang_bindir + "/clang++"),
  104. llvm_ar = tool(path = llvm_bindir + "/llvm-ar"),
  105. llvm_strip = tool(path = llvm_bindir + "/llvm-strip"),
  106. )
  107. if ctx.attr.bins:
  108. carbon_busybox = None
  109. clang = None
  110. clangpp = None
  111. llvm_ar = None
  112. llvm_strip = None
  113. for f in ctx.files.bins:
  114. if f.basename == "carbon-busybox":
  115. carbon_busybox = f
  116. elif f.basename == "clang":
  117. clang = f
  118. elif f.basename == "clang++":
  119. clangpp = f
  120. elif f.basename == "llvm-ar":
  121. llvm_ar = f
  122. elif f.basename == "llvm-strip":
  123. llvm_strip = f
  124. if not all([carbon_busybox, clang, clangpp, llvm_ar, llvm_strip]):
  125. fail("Missing required tool in bins: {0}".format(ctx.attr.bins))
  126. llvm_bindir = llvm_ar.dirname
  127. clang_bindir = clang.dirname
  128. tools = struct(
  129. carbon_busybox = tool(tool = carbon_busybox),
  130. clang = tool(tool = clang),
  131. clangpp = tool(tool = clangpp),
  132. llvm_ar = tool(tool = llvm_ar),
  133. llvm_strip = tool(tool = llvm_strip),
  134. )
  135. # Only use a sysroot if a non-trivial one is set in Carbon's config.
  136. builtin_sysroot = None
  137. sysroot_include_search = []
  138. if clang_sysroot != "None" and clang_sysroot != "/":
  139. builtin_sysroot = clang_sysroot
  140. sysroot_include_search = ["%sysroot%/usr/include"]
  141. runtimes_path = None
  142. if ctx.attr.runtimes:
  143. for f in ctx.files.runtimes:
  144. if f.basename == "runtimes_root":
  145. runtimes_path = f.dirname
  146. break
  147. if not runtimes_path:
  148. fail("Unable to compute the runtimes path for: {0}".format(
  149. ctx.attr.runtimes,
  150. ))
  151. identifier = "{0}_toolchain_{1}_{2}".format(
  152. ctx.attr.identifier_prefix,
  153. ctx.attr.target_cpu,
  154. ctx.attr.target_os,
  155. )
  156. return cc_common.create_cc_toolchain_config_info(
  157. ctx = ctx,
  158. features = clang_cc_toolchain_features(
  159. target_os = ctx.attr.target_os,
  160. target_cpu = ctx.attr.target_cpu,
  161. # TODO: This should be configured externally rather than here so
  162. # that the install Carbon toolchain doesn't automatically include
  163. # Carbon-project-specific flags. However, that is especially awkward
  164. # to do until we fully migrate to a rules-based toolchain, and the
  165. # project-specific flags are largely harmless at the moment. We also
  166. # omit a meaningful cache key as when using the Carbon toolchain we
  167. # don't need it as it is a hermetic part of Bazel.
  168. project_features = carbon_project_features(cache_key = ""),
  169. ),
  170. action_configs = _make_action_configs(tools, runtimes_path),
  171. cxx_builtin_include_directories = [
  172. "runtimes/libunwind/include",
  173. "runtimes/libcxx/include",
  174. "runtimes/libcxxabi/include",
  175. "{}/include".format(clang_resource_dir),
  176. "runtimes/clang_resource_dir/include",
  177. ] + _compute_clang_system_include_dirs() + sysroot_include_search,
  178. builtin_sysroot = builtin_sysroot,
  179. # This configuration only supports local non-cross builds so derive
  180. # everything from the target CPU selected.
  181. toolchain_identifier = identifier,
  182. # This is used to expose a "flag" that `config_setting` rules can use to
  183. # determine if the compiler is Clang.
  184. compiler = "clang",
  185. # Pass in our tool paths to expose Make variables like $(NM) and
  186. # $(OBJCOPY).
  187. tool_paths = llvm_tool_paths(llvm_bindir, clang_bindir),
  188. )
  189. carbon_cc_toolchain_config = rule(
  190. implementation = _carbon_cc_toolchain_config_impl,
  191. attrs = {
  192. "bins": attr.label(mandatory = False),
  193. "identifier_prefix": attr.string(mandatory = True),
  194. "runtimes": attr.label(mandatory = False),
  195. "target_cpu": attr.string(mandatory = True),
  196. "target_os": attr.string(mandatory = True),
  197. },
  198. provides = [CcToolchainConfigInfo],
  199. )
  200. def _runtimes_transition_impl(_, attr):
  201. return {
  202. "//:runtimes_build": True,
  203. }
  204. _runtimes_transition = transition(
  205. inputs = [],
  206. outputs = [
  207. "//:runtimes_build",
  208. ],
  209. implementation = _runtimes_transition_impl,
  210. )
  211. def _filegroup_with_runtimes_build_impl(ctx):
  212. return [DefaultInfo(files = depset(ctx.files.srcs))]
  213. filegroup_with_runtimes_build = rule(
  214. implementation = _filegroup_with_runtimes_build_impl,
  215. attrs = {
  216. "srcs": attr.label_list(mandatory = True, cfg = _runtimes_transition),
  217. "_allowlist_function_transition": attr.label(
  218. default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
  219. ),
  220. },
  221. doc = "A filegroup whose sources are built with or without runtimes building enabled.",
  222. )
  223. def carbon_cc_toolchain(
  224. name,
  225. platforms,
  226. base_files_target,
  227. runtimes_compile_files_target,
  228. compile_files_target,
  229. runtimes_target,
  230. extra_toolchain_settings = [],
  231. tags = []):
  232. """Create a Carbon `cc_toolchain` for the current target.
  233. This macro constructs the configuration and toolchain rules for a baseline
  234. Carbon toolchain, including building its own runtimes on demand.
  235. Args:
  236. name: The base name for the toolchain targets.
  237. platforms: Supported platforms.
  238. base_files_target: Target for base files.
  239. runtimes_compile_files_target: Target for runtimes compile files.
  240. compile_files_target: Target for compile files.
  241. runtimes_target: Target for runtimes.
  242. extra_toolchain_settings: Extra toolchain settings.
  243. tags: Tags to apply to the toolchain.
  244. """
  245. impl_tags = tags if "manual" in tags else tags + ["manual"]
  246. carbon_cc_toolchain_config(
  247. name = "{}_runtimes_toolchain_config".format(name),
  248. identifier_prefix = "{}_runtimes".format(name),
  249. target_cpu = select({
  250. ":is_{}_{}".format(os, cpu): cpu
  251. for os, cpus in platforms.items()
  252. for cpu in cpus
  253. }),
  254. target_os = select({
  255. "@platforms//os:{}".format(os): os
  256. for os in platforms.keys()
  257. }),
  258. bins = base_files_target,
  259. tags = impl_tags,
  260. )
  261. cc_toolchain(
  262. name = "{}_runtimes_cc_toolchain".format(name),
  263. all_files = runtimes_compile_files_target,
  264. ar_files = base_files_target,
  265. as_files = runtimes_compile_files_target,
  266. compiler_files = runtimes_compile_files_target,
  267. dwp_files = base_files_target,
  268. linker_files = base_files_target,
  269. objcopy_files = base_files_target,
  270. strip_files = base_files_target,
  271. toolchain_config = ":{}_runtimes_toolchain_config".format(name),
  272. toolchain_identifier = select({
  273. ":is_{}_{}".format(os, cpu): "{}_{}_{}_runtimes_toolchain".format(name, os, cpu)
  274. for os, cpus in platforms.items()
  275. for cpu in cpus
  276. }),
  277. tags = impl_tags,
  278. )
  279. native.toolchain(
  280. name = "{}_runtimes_toolchain".format(name),
  281. target_settings = [":is_runtimes_build"] + extra_toolchain_settings,
  282. use_target_platform_constraints = True,
  283. toolchain = ":{}_runtimes_cc_toolchain".format(name),
  284. toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
  285. tags = tags,
  286. )
  287. carbon_cc_toolchain_config(
  288. name = "{}_toolchain_config".format(name),
  289. identifier_prefix = name,
  290. target_cpu = select({
  291. ":is_{}_{}".format(os, cpu): cpu
  292. for os, cpus in platforms.items()
  293. for cpu in cpus
  294. }),
  295. target_os = select({
  296. "@platforms//os:{}".format(os): os
  297. for os in platforms.keys()
  298. }),
  299. runtimes = runtimes_target,
  300. bins = base_files_target,
  301. tags = impl_tags,
  302. )
  303. native.filegroup(
  304. name = "{}_linker_files".format(name),
  305. srcs = [
  306. base_files_target,
  307. runtimes_target,
  308. ],
  309. tags = impl_tags,
  310. )
  311. native.filegroup(
  312. name = "{}_all_files".format(name),
  313. srcs = [
  314. compile_files_target,
  315. ":{}_linker_files".format(name),
  316. ],
  317. tags = impl_tags,
  318. )
  319. cc_toolchain(
  320. name = "{}_cc_toolchain".format(name),
  321. all_files = ":{}_all_files".format(name),
  322. ar_files = base_files_target,
  323. as_files = compile_files_target,
  324. compiler_files = compile_files_target,
  325. dwp_files = ":{}_linker_files".format(name),
  326. linker_files = ":{}_linker_files".format(name),
  327. objcopy_files = base_files_target,
  328. strip_files = base_files_target,
  329. toolchain_config = ":" + name + "_toolchain_config",
  330. toolchain_identifier = select({
  331. ":is_{}_{}".format(os, cpu): "{}_{}_{}_toolchain".format(name, os, cpu)
  332. for os, cpus in platforms.items()
  333. for cpu in cpus
  334. }),
  335. tags = impl_tags,
  336. )
  337. native.toolchain(
  338. name = name + "_toolchain",
  339. target_settings = [":not_runtimes_build"] + extra_toolchain_settings,
  340. use_target_platform_constraints = True,
  341. toolchain = ":" + name + "_cc_toolchain",
  342. toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
  343. tags = tags,
  344. )