BUILD 17 KB

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