BUILD 16 KB

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