BUILD 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. load(
  5. "@llvm-project//:vars.bzl",
  6. "LLVM_VERSION_MAJOR",
  7. )
  8. load("@rules_python//python:defs.bzl", "py_binary", "py_test")
  9. load("//bazel/cc_rules:defs.bzl", "cc_binary", "cc_library", "cc_test")
  10. load("//bazel/manifest:defs.bzl", "manifest")
  11. load("//toolchain/base:llvm_tools.bzl", "LLVM_MAIN_TOOLS", "LLVM_TOOL_ALIASES")
  12. load(
  13. "//toolchain/base:runtimes_build_info.bzl",
  14. "BUILTINS_SRCS_FILEGROUPS",
  15. "BUILTINS_TEXTUAL_SRCS_FILEGROUPS",
  16. "CRT_FILES",
  17. )
  18. load("configure_cmake_file.bzl", "configure_cmake_file")
  19. load("install_filegroups.bzl", "install_filegroup", "install_symlink", "install_target", "make_install_filegroups")
  20. load("pkg_helpers.bzl", "pkg_naming_variables", "pkg_tar_and_test")
  21. package(default_visibility = ["//visibility:public"])
  22. # Build rules supporting the install data tree for the Carbon toolchain.
  23. #
  24. # This populates a synthetic Carbon toolchain installation under the
  25. # `prefix` directory. For details on its layout, see `install_dirs` below.
  26. cc_library(
  27. name = "busybox_info",
  28. srcs = ["busybox_info.cpp"],
  29. hdrs = ["busybox_info.h"],
  30. deps = [
  31. "//common:error",
  32. "//common:exe_path",
  33. "//common:filesystem",
  34. "@llvm-project//llvm:Support",
  35. ],
  36. )
  37. cc_test(
  38. name = "busybox_info_test",
  39. size = "small",
  40. srcs = ["busybox_info_test.cpp"],
  41. deps = [
  42. ":busybox_info",
  43. "//common:check",
  44. "//common:filesystem",
  45. "//testing/base:gtest_main",
  46. "@googletest//:gtest",
  47. "@llvm-project//llvm:Support",
  48. ],
  49. )
  50. # This target doesn't include prelude libraries. To get a target that has the
  51. # prelude available, use //toolchain.
  52. cc_binary(
  53. name = "carbon-busybox",
  54. srcs = ["busybox_main.cpp"],
  55. deps = [
  56. ":busybox_info",
  57. "//common:all_llvm_targets",
  58. "//common:bazel_working_dir",
  59. "//common:error",
  60. "//common:exe_path",
  61. "//common:init_llvm",
  62. "//common:version_stamp",
  63. "//toolchain/base:install_paths",
  64. "//toolchain/base:llvm_tools_def",
  65. "//toolchain/driver",
  66. "@llvm-project//clang:driver",
  67. "@llvm-project//llvm:Support",
  68. ],
  69. )
  70. clang_aliases = [
  71. "clang",
  72. "clang++",
  73. "clang-cl",
  74. "clang-cpp",
  75. ]
  76. # TODO: Add remaining aliases of LLD for Windows and WASM when we have support
  77. # for them wired up through the busybox.
  78. lld_aliases = [
  79. "ld.lld",
  80. "ld64.lld",
  81. ]
  82. llvm_binaries = clang_aliases + lld_aliases + [
  83. tool.bin_name
  84. for tool in LLVM_MAIN_TOOLS.values()
  85. ] + [
  86. "llvm-" + alias
  87. for (_, aliases) in LLVM_TOOL_ALIASES.items()
  88. for alias in aliases
  89. ]
  90. filegroup(
  91. name = "clang_headers",
  92. srcs = ["@llvm-project//clang:builtin_headers_gen"],
  93. )
  94. # Collect the runtime sources that are collectively installed into the
  95. # `builtins` directory.
  96. filegroup(
  97. name = "clang_builtins_runtimes",
  98. srcs = (
  99. CRT_FILES.values() +
  100. BUILTINS_SRCS_FILEGROUPS +
  101. BUILTINS_TEXTUAL_SRCS_FILEGROUPS
  102. ),
  103. )
  104. py_binary(
  105. name = "configure_cmake_file_impl",
  106. srcs = ["configure_cmake_file_impl.py"],
  107. )
  108. configure_cmake_file(
  109. name = "libcxx_site_config_gen",
  110. src = "@llvm-project//libcxx:include/__config_site.in",
  111. out = "staging_libcxx/include/__config_site",
  112. defines = {
  113. # We can inject custom logic at the end of the site configuration with
  114. # the ABI defines. This can custom set (or re-set) any of the relevant
  115. # configuration defines. Note that while this is sorted here in the
  116. # BUILD file, it is expanded at the _end_ of the configuration header
  117. # and so overrides the other configuration settings.
  118. #
  119. # TODO: This is a lot of C++ code to embed into a BUILD file. Even
  120. # though it moves it farther from the interacting CMake defines, we
  121. # should look at factoring this into a header that is included.
  122. "_LIBCPP_ABI_DEFINES": "\n".join([
  123. # We want to install a single header that works in all build modes,
  124. # so we define the ABI namespace based on how the header is used
  125. # rather than a fixed one. However, we only support use with Clang
  126. # and so we assume `__has_feature` is available and works.
  127. #
  128. # Note that generally, we don't rely on different ABI namespaces for
  129. # functionality -- the distinction is more to make errors when
  130. # linking with the wrong build of the standard library obvious and
  131. # immediate. We only can achieve this for sanitizers that have a
  132. # preprocessor detectable model.
  133. "#if __has_feature(address_sanitizer)",
  134. "# undef _LIBCPP_ABI_NAMESPACE",
  135. "# define _LIBCPP_ABI_NAMESPACE __asan",
  136. # Also mark that libc++ will be instrumented.
  137. "# undef _LIBCPP_INSTRUMENTED_WITH_ASAN",
  138. "# define _LIBCPP_INSTRUMENTED_WITH_ASAN 1",
  139. "#elif __has_feature(memory_sanitizer)",
  140. # TODO: If a track-origins macro becomes available, we should
  141. # distinguish that case, too.
  142. "# undef _LIBCPP_ABI_NAMESPACE",
  143. "# define _LIBCPP_ABI_NAMESPACE __msan",
  144. "#elif __has_feature(thread_sanitizer)",
  145. "# undef _LIBCPP_ABI_NAMESPACE",
  146. "# define _LIBCPP_ABI_NAMESPACE __tsan",
  147. "#elif __has_feature(cfi_sanitizer)",
  148. "# undef _LIBCPP_ABI_NAMESPACE",
  149. "# define _LIBCPP_ABI_NAMESPACE __cfi",
  150. "#endif",
  151. "",
  152. # Establish a default hardening mode where possible.
  153. "#ifndef _LIBCPP_HARDENING_MODE",
  154. "# ifndef NDEBUG",
  155. # !NDEBUG has significant overhead anyway and is explicitly a
  156. # debugging build rather than a production build.
  157. "# define _LIBCPP_HARDENING_MODE _LIBCPP_HARDENING_MODE_DEBUG",
  158. "# else",
  159. # Default to the fast hardening checks.
  160. "# define _LIBCPP_HARDENING_MODE _LIBCPP_HARDENING_MODE_FAST",
  161. "# endif",
  162. "#endif",
  163. "",
  164. # CUDA can't call any existing abort implementations, so disable
  165. # hardening there.
  166. "#ifdef __CUDA__",
  167. "# undef _LIBCPP_HARDENING_MODE",
  168. "# define _LIBCPP_HARDENING_MODE _LIBCPP_HARDENING_MODE_NONE",
  169. "#endif",
  170. "",
  171. # Setup platform-dependent features using preprocessor logic.
  172. "#ifdef __linux__",
  173. "# undef _LIBCPP_HAS_TIME_ZONE_DATABASE",
  174. "# define _LIBCPP_HAS_TIME_ZONE_DATABASE 1",
  175. "#endif",
  176. "",
  177. # Mixing translation units compiled with different versions of
  178. # libc++ is unsupported. Disable ABI tags to decrease symbol
  179. # lengths.
  180. "#define _LIBCPP_NO_ABI_TAG",
  181. ]),
  182. # No forced ABI.
  183. "_LIBCPP_ABI_FORCE_ITANIUM": "OFF",
  184. "_LIBCPP_ABI_FORCE_MICROSOFT": "OFF",
  185. # We use the unstable ABI and define a custom, Carbon-specific ABI
  186. # namespace. This also matches the mangling prefix used for Carbon
  187. # symbols.
  188. "_LIBCPP_ABI_NAMESPACE": "_C",
  189. # TODO: Fix the need to define _LIBCPP_ABI_VERSION when the unstable
  190. # ABI is selected.
  191. "_LIBCPP_ABI_VERSION": "999",
  192. # Follow hardening mode for the assertion semantics.
  193. "_LIBCPP_ASSERTION_SEMANTIC_DEFAULT": "_LIBCPP_ASSERTION_SEMANTIC_HARDENING_DEPENDENT",
  194. # Enable various features in libc++ available across platforms. We
  195. # describe these in a block to allow the BUILD file to sort them.
  196. #
  197. # - We enable threads, and use auto-detection rather than forcing an
  198. # API.
  199. # - Availability annotations do not apply to Carbon's libc++, so those
  200. # are disabled.
  201. #
  202. # Where there are platform differences in the features, we disable them
  203. # here and re-enable them in the `_LIBCPP_ABI_DEFINES` section using
  204. # custom logic to detect the relevant platform.
  205. "_LIBCPP_HAS_FILESYSTEM": "ON",
  206. "_LIBCPP_HAS_LOCALIZATION": "ON",
  207. "_LIBCPP_HAS_MONOTONIC_CLOCK": "ON",
  208. "_LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS": "ON",
  209. "_LIBCPP_HAS_RANDOM_DEVICE": "ON",
  210. "_LIBCPP_HAS_TERMINAL": "ON",
  211. "_LIBCPP_HAS_THREADS": "ON",
  212. "_LIBCPP_HAS_THREAD_API_EXTERNAL": "OFF",
  213. "_LIBCPP_HAS_THREAD_API_PTHREAD": "OFF",
  214. "_LIBCPP_HAS_THREAD_API_WIN32": "OFF",
  215. "_LIBCPP_HAS_TIME_ZONE_DATABASE": "OFF",
  216. "_LIBCPP_HAS_UNICODE": "ON",
  217. "_LIBCPP_HAS_VENDOR_AVAILABILITY_ANNOTATIONS": "OFF",
  218. "_LIBCPP_HAS_WIDE_CHARACTERS": "ON",
  219. # When ASan is enabled, we ensure that libc++ is built with it as well.
  220. # However, we can't set this more carefully here so we set it to off and
  221. # override it below when using ASan.
  222. "_LIBCPP_INSTRUMENTED_WITH_ASAN": "OFF",
  223. # Set the parallel backend to serial.
  224. # TODO: We should revisit this.
  225. "_LIBCPP_PSTL_BACKEND_SERIAL": "1",
  226. },
  227. )
  228. configure_cmake_file(
  229. name = "libcxx_assertion_handler_gen",
  230. src = "@llvm-project//libcxx:vendor/llvm/default_assertion_handler.in",
  231. out = "staging_libcxx/include/__assertion_handler",
  232. defines = {
  233. # Currently the default handler needs no substitutions.
  234. },
  235. )
  236. filegroup(
  237. name = "libcxx",
  238. srcs = [
  239. "@llvm-project//libcxx:libcxx_all_srcs",
  240. "@llvm-project//libcxx:libcxx_hdrs",
  241. ],
  242. )
  243. filegroup(
  244. name = "libcxx_gen_files",
  245. srcs = [
  246. "staging_libcxx/include/__assertion_handler",
  247. "staging_libcxx/include/__config_site",
  248. ],
  249. )
  250. filegroup(
  251. name = "libcxxabi",
  252. srcs = [
  253. "@llvm-project//libcxxabi:libcxxabi_hdrs",
  254. "@llvm-project//libcxxabi:libcxxabi_srcs",
  255. "@llvm-project//libcxxabi:libcxxabi_textual_srcs",
  256. ],
  257. )
  258. filegroup(
  259. name = "libunwind",
  260. srcs = [
  261. "@llvm-project//libunwind:libunwind_hdrs",
  262. "@llvm-project//libunwind:libunwind_srcs",
  263. ],
  264. )
  265. # Currently, we're only installing the subset of LLVM's libc internals needed to
  266. # build libc++. At some point, we should ship LLVM's libc itself, and that will
  267. # likely expand this to cover more of the source. However, we'll still want to
  268. # distinguish between the _internal_ installation and the generated set of
  269. # headers that we inject into the include search for user compiles. The
  270. # `include` subdirectory in this file group is _not_ intended to be exposed to
  271. # user compiles, only to compilation of runtimes.
  272. filegroup(
  273. name = "libc_internal",
  274. srcs = [
  275. "@llvm-project//libc:libcxx_shared_headers_hdrs",
  276. ],
  277. )
  278. # Given a CMake-style install prefix[1], the hierarchy looks like:
  279. #
  280. # - prefix/bin: Binaries intended for direct use.
  281. # - prefix/lib/carbon: Private data and files.
  282. # - prefix/lib/carbon/core: The `Core` package files.
  283. # - prefix/lib/carbon/llvm/bin: LLVM binaries.
  284. #
  285. # This will be how installs are provided on Unix-y platforms, and is loosely
  286. # based on the FHS (Filesystem Hierarchy Standard). See the CMake install prefix
  287. # documentation[1] for more details.
  288. #
  289. # [1]: https://cmake.org/cmake/help/latest/variable/CMAKE_INSTALL_PREFIX.html
  290. install_dirs = {
  291. "bin": [
  292. install_symlink(
  293. "carbon",
  294. "../lib/carbon/carbon-busybox",
  295. is_driver = True,
  296. ),
  297. ],
  298. "lib/carbon": [
  299. install_target("MODULE.bazel", "bazel/install.MODULE.bazel"),
  300. install_target("BUILD", "bazel/install.BUILD"),
  301. install_target("carbon_install.txt", "carbon_install.txt"),
  302. install_target(
  303. "install_digest.txt",
  304. ":install_digest.txt",
  305. executable = False,
  306. is_digest = True,
  307. is_driver = True,
  308. ),
  309. install_target(
  310. "carbon-busybox",
  311. ":carbon-busybox",
  312. executable = True,
  313. is_driver = True,
  314. ),
  315. install_filegroup("bazel", "//bazel/cc_toolchains:installed_cc_toolchain_starlark"),
  316. # TODO: Consider if we want to keep `core` here or group it with
  317. # runtimes. It is a bit of both -- standard library, and runtimes.
  318. install_filegroup("core", "//core:prelude"),
  319. ],
  320. "lib/carbon/bazel": [
  321. install_target("carbon_cc_toolchain_config.bzl", "bazel/carbon_cc_toolchain_config.bzl"),
  322. install_target("carbon_detected_variables.tpl.bzl", "bazel/carbon_detected_variables.tpl.bzl"),
  323. install_target("carbon_toolchain.bzl", "bazel/carbon_toolchain.bzl"),
  324. install_target("BUILD", "bazel/empty.BUILD"),
  325. ],
  326. "lib/carbon/llvm/bin": [install_symlink(
  327. name,
  328. "../../carbon-busybox",
  329. is_driver = True,
  330. ) for name in llvm_binaries],
  331. "lib/carbon/runtimes": [
  332. install_filegroup(
  333. "builtins",
  334. ":clang_builtins_runtimes",
  335. remove_prefix = "lib/builtins/",
  336. ),
  337. install_filegroup("libcxx", ":libcxx"),
  338. install_filegroup("libcxxabi", ":libcxxabi"),
  339. install_filegroup("libunwind", ":libunwind"),
  340. ],
  341. "lib/carbon/runtimes/libc": [
  342. install_filegroup("internal", ":libc_internal"),
  343. ],
  344. "lib/carbon/runtimes/libcxx": [
  345. install_filegroup(
  346. "include",
  347. ":libcxx_gen_files",
  348. remove_prefix = "staging_libcxx/include/",
  349. ),
  350. ],
  351. "lib/carbon/llvm/lib/clang/" + LLVM_VERSION_MAJOR: [
  352. install_filegroup(
  353. "include",
  354. ":clang_headers",
  355. label = "installed_clang_headers",
  356. remove_prefix = "staging/include/",
  357. ),
  358. ],
  359. "lib/carbon/llvm/lib/clang/" + LLVM_VERSION_MAJOR + "/include": [
  360. install_filegroup(
  361. "fuzzer",
  362. "@llvm-project//compiler-rt:fuzzer_installed_hdrs",
  363. remove_prefix = "include/fuzzer/",
  364. ),
  365. install_filegroup(
  366. "profile",
  367. "@llvm-project//compiler-rt:profile_installed_hdrs",
  368. remove_prefix = "include/profile/",
  369. ),
  370. install_filegroup(
  371. "sanitizer",
  372. "@llvm-project//compiler-rt:sanitizer_installed_hdrs",
  373. remove_prefix = "include/sanitizer/",
  374. ),
  375. ],
  376. }
  377. make_install_filegroups(
  378. name = "install_data",
  379. install_dirs = install_dirs,
  380. no_digest_name = "install_data.no_digest",
  381. no_driver_name = "install_data.no_driver",
  382. pkg_name = "pkg_data",
  383. prefix = "prefix",
  384. )
  385. py_test(
  386. name = "llvm_symlinks_test",
  387. size = "small",
  388. srcs = ["llvm_symlinks_test.py"],
  389. data = [
  390. ":install_data",
  391. "//toolchain/driver:prebuilt_runtimes",
  392. ],
  393. deps = ["@bazel_tools//tools/python/runfiles"],
  394. )
  395. manifest(
  396. name = "install_data_manifest.txt",
  397. srcs = [":install_data"],
  398. )
  399. cc_binary(
  400. name = "make-installation-digest",
  401. srcs = ["make_installation_digest.cpp"],
  402. deps = [
  403. "//common:bazel_working_dir",
  404. "//common:error",
  405. "//common:exe_path",
  406. "//common:filesystem",
  407. "//common:init_llvm",
  408. "//common:map",
  409. "//common:vlog",
  410. "@llvm-project//llvm:Support",
  411. ],
  412. )
  413. manifest(
  414. name = "install_digest_manifest.txt",
  415. srcs = [":install_data.no_digest"],
  416. )
  417. genrule(
  418. name = "gen_digest",
  419. srcs = [
  420. ":install_data.no_digest",
  421. "install_digest_manifest.txt",
  422. ],
  423. outs = [":install_digest.txt"],
  424. cmd = "$(location :make-installation-digest) " +
  425. "$(location install_digest_manifest.txt) $@",
  426. tools = [":make-installation-digest"],
  427. )
  428. # A list of clang's installed builtin header files.
  429. # This is consumed by //toolchain/testing:file_test.
  430. manifest(
  431. name = "clang_headers_manifest.txt",
  432. srcs = [":installed_clang_headers"],
  433. strip_package_dir = True,
  434. )
  435. pkg_naming_variables(
  436. name = "packaging_variables",
  437. )
  438. # We build both a compressed and uncompressed tar file with the same code here.
  439. # This lets us use the tar file in testing as it is fast to create, but ship the
  440. # compressed version as a release.
  441. #
  442. # For manual tests, the tar rules are `carbon_toolchain_tar_rule` and
  443. # `carbon_toolchain_tar_gz_rule`.
  444. pkg_tar_and_test(
  445. srcs = [":pkg_data"],
  446. install_data_manifest = ":install_data_manifest.txt",
  447. name_base = "carbon_toolchain",
  448. package_dir = "carbon_toolchain-$(version)",
  449. package_file_name_base = "carbon_toolchain-$(version)",
  450. package_variables = ":packaging_variables",
  451. stamp = -1, # Allow `--stamp` builds to produce file timestamps.
  452. )