BUILD 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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_root` 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 root `prefix_root`, the hierarchy looks like:
  268. #
  269. # - prefix_root/bin: Binaries intended for direct use.
  270. # - prefix_root/lib/carbon: Private data and files.
  271. # - prefix_root/lib/carbon/core: The `Core` package files.
  272. # - prefix_root/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).
  276. install_dirs = {
  277. "bin": [
  278. install_symlink(
  279. "carbon",
  280. "../lib/carbon/carbon-busybox",
  281. is_driver = True,
  282. ),
  283. ],
  284. "lib/carbon": [
  285. install_target("carbon_install.txt", "carbon_install.txt"),
  286. install_target(
  287. "install_digest.txt",
  288. ":install_digest.txt",
  289. executable = False,
  290. is_digest = True,
  291. is_driver = True,
  292. ),
  293. install_target(
  294. "carbon-busybox",
  295. ":carbon-busybox",
  296. executable = True,
  297. is_driver = True,
  298. ),
  299. # TODO: Consider if we want to keep `core` here or group it with
  300. # runtimes. It is a bit of both -- standard library, and runtimes.
  301. install_filegroup("core", "//core:prelude"),
  302. ],
  303. "lib/carbon/llvm/bin": [install_symlink(
  304. name,
  305. "../../carbon-busybox",
  306. is_driver = True,
  307. ) for name in llvm_binaries],
  308. "lib/carbon/runtimes": [
  309. install_filegroup(
  310. "builtins",
  311. ":clang_builtins_runtimes",
  312. remove_prefix = "lib/builtins/",
  313. ),
  314. install_filegroup("libcxx", ":libcxx"),
  315. install_filegroup("libcxxabi", ":libcxxabi"),
  316. install_filegroup("libunwind", ":libunwind"),
  317. ],
  318. "lib/carbon/runtimes/libc": [
  319. install_filegroup("internal", ":libc_internal"),
  320. ],
  321. "lib/carbon/runtimes/libcxx": [
  322. install_filegroup(
  323. "include",
  324. ":libcxx_gen_files",
  325. remove_prefix = "staging_libcxx/include/",
  326. ),
  327. ],
  328. "lib/carbon/llvm/lib/clang/" + LLVM_VERSION_MAJOR: [
  329. install_filegroup(
  330. "include",
  331. ":clang_headers",
  332. label = "installed_clang_headers",
  333. remove_prefix = "staging/include/",
  334. ),
  335. ],
  336. }
  337. make_install_filegroups(
  338. name = "install_data",
  339. install_dirs = install_dirs,
  340. no_digest_name = "install_data.no_digest",
  341. no_driver_name = "install_data.no_driver",
  342. pkg_name = "pkg_data",
  343. prefix = "prefix_root",
  344. )
  345. py_test(
  346. name = "llvm_symlinks_test",
  347. size = "small",
  348. srcs = ["llvm_symlinks_test.py"],
  349. data = [":install_data"],
  350. )
  351. manifest(
  352. name = "install_data_manifest.txt",
  353. srcs = [":install_data"],
  354. )
  355. cc_binary(
  356. name = "make-installation-digest",
  357. srcs = ["make_installation_digest.cpp"],
  358. deps = [
  359. "//common:bazel_working_dir",
  360. "//common:error",
  361. "//common:exe_path",
  362. "//common:filesystem",
  363. "//common:init_llvm",
  364. "//common:map",
  365. "//common:vlog",
  366. "//toolchain/base:install_paths",
  367. "@llvm-project//llvm:Support",
  368. ],
  369. )
  370. manifest(
  371. name = "install_digest_manifest.txt",
  372. srcs = [":install_data.no_digest"],
  373. )
  374. genrule(
  375. name = "gen_digest",
  376. srcs = [
  377. ":install_data.no_digest",
  378. "install_digest_manifest.txt",
  379. ],
  380. outs = [":install_digest.txt"],
  381. cmd = "$(location :make-installation-digest) " +
  382. "$(location install_digest_manifest.txt) $@",
  383. tools = [":make-installation-digest"],
  384. )
  385. # A list of clang's installed builtin header files.
  386. # This is consumed by //toolchain/testing:file_test.
  387. manifest(
  388. name = "clang_headers_manifest.txt",
  389. srcs = [":installed_clang_headers"],
  390. strip_package_dir = True,
  391. )
  392. pkg_naming_variables(
  393. name = "packaging_variables",
  394. )
  395. # We build both a compressed and uncompressed tar file with the same code here.
  396. # This lets us use the tar file in testing as it is fast to create, but ship the
  397. # compressed version as a release.
  398. #
  399. # For manual tests, the tar rules are `carbon_toolchain_tar_rule` and
  400. # `carbon_toolchain_tar_gz_rule`.
  401. pkg_tar_and_test(
  402. srcs = [":pkg_data"],
  403. install_data_manifest = ":install_data_manifest.txt",
  404. name_base = "carbon_toolchain",
  405. package_dir = "carbon_toolchain-$(version)",
  406. package_file_name_base = "carbon_toolchain-$(version)",
  407. package_variables = ":packaging_variables",
  408. stamp = -1, # Allow `--stamp` builds to produce file timestamps.
  409. )