BUILD 16 KB

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