BUILD 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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("@bazel_skylib//rules:write_file.bzl", "write_file")
  5. load("@llvm-project//llvm:binary_alias.bzl", "binary_alias")
  6. load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
  7. load("@rules_pkg//pkg:mappings.bzl", "pkg_attributes", "pkg_filegroup", "pkg_files", "pkg_mklink", "strip_prefix")
  8. load("pkg_helpers.bzl", "pkg_naming_variables", "pkg_tar_and_test")
  9. load("symlink_filegroup.bzl", "symlink_filegroup")
  10. package(default_visibility = ["//visibility:public"])
  11. # Build rules supporting the install data tree for the Carbon toolchain.
  12. #
  13. # This populates a synthetic Carbon toolchain installation under the
  14. # `prefix_root` directory. For details on its layout, see `install_paths.h`.
  15. # A marker of a valid Carbon install. All filegroups in the install should
  16. # include this one.
  17. write_file(
  18. name = "install_marker",
  19. out = "prefix_root/lib/carbon/carbon_install.txt",
  20. content = [
  21. "// Part of the Carbon Language project, under the Apache License v2.0 with LLVM",
  22. "// Exceptions. See /LICENSE for license information.",
  23. "// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception",
  24. "",
  25. "This marks a valid Carbon install tree.",
  26. ],
  27. )
  28. # Create symlinks for the core library. Note that this should *not* be depended
  29. # on directly, use `:core_data` instead.
  30. symlink_filegroup(
  31. name = "symlink_core",
  32. srcs = ["//core:prelude"],
  33. out_prefix = "prefix_root/lib/carbon/",
  34. visibility = ["//visibility:private"],
  35. )
  36. # The filegroup to get the core library.
  37. filegroup(
  38. name = "core_data",
  39. srcs = [
  40. ":install_marker",
  41. ":symlink_core",
  42. ],
  43. )
  44. # Copy Clang and LLVM toolchain files into a synthetic LLVM installation under
  45. # `prefix_root/lib/carbon/llvm` so that parts of Clang that expect to find an LLVM
  46. # installation at relative paths work correctly without exposing these in an
  47. # installed 'bin' directory where it might get added to a user's PATH.
  48. binary_alias(
  49. name = "prefix_root/lib/carbon/llvm/bin/lld",
  50. binary = "@llvm-project//lld:lld",
  51. )
  52. lld_bin_names = [
  53. "ld.lld",
  54. "ld64.lld",
  55. "lld-link",
  56. "wasm-ld",
  57. ]
  58. [
  59. binary_alias(
  60. name = "prefix_root/lib/carbon/llvm/bin/" + bin_name,
  61. binary = "@llvm-project//lld:lld",
  62. )
  63. for bin_name in lld_bin_names
  64. ]
  65. filegroup(
  66. name = "llvm_link_data",
  67. srcs = [
  68. "prefix_root/lib/carbon/llvm/bin/lld",
  69. ":install_marker",
  70. ] + [
  71. "prefix_root/lib/carbon/llvm/bin/" + bin_name
  72. for bin_name in lld_bin_names
  73. ],
  74. )
  75. # All of the install data except for the user-facing binaries. This is typically
  76. # a reasonable data dependency for libraries and the user-facing binaries
  77. # without creating a cycle.
  78. filegroup(
  79. name = "install_lib_data",
  80. srcs = [
  81. ":core_data",
  82. ":install_marker",
  83. ":llvm_link_data",
  84. ],
  85. )
  86. binary_alias(
  87. name = "prefix_root/bin/carbon",
  88. binary = "//toolchain/driver:carbon",
  89. )
  90. filegroup(
  91. name = "install_data",
  92. srcs = [
  93. "prefix_root/bin/carbon",
  94. ":install_lib_data",
  95. ":install_marker",
  96. ],
  97. )
  98. # A library for computing install paths for the toolchain. Note that this
  99. # library does *not* include the data itself, as that would form a dependency
  100. # cycle. Each part of the toolchain should add the narrow data file groups to
  101. # their data dependencies, and then use this library to locate them.
  102. cc_library(
  103. name = "install_paths",
  104. srcs = ["install_paths.cpp"],
  105. hdrs = ["install_paths.h"],
  106. deps = [
  107. "//common:check",
  108. "//common:error",
  109. "@bazel_tools//tools/cpp/runfiles",
  110. "@llvm-project//llvm:Support",
  111. ],
  112. )
  113. cc_binary(
  114. name = "test_binary",
  115. testonly = 1,
  116. srcs = ["test_binary.cpp"],
  117. data = [":install_data"],
  118. )
  119. cc_test(
  120. name = "install_paths_test",
  121. size = "small",
  122. srcs = ["install_paths_test.cpp"],
  123. data = [
  124. ":install_data",
  125. ":test_binary",
  126. ],
  127. deps = [
  128. ":install_paths",
  129. "//common:check",
  130. "//common:ostream",
  131. "//testing/base:gtest_main",
  132. "@bazel_tools//tools/cpp/runfiles",
  133. "@googletest//:gtest",
  134. "@llvm-project//llvm:Support",
  135. ],
  136. )
  137. # Build rules to construct packaged versions of the toolchain's install.
  138. pkg_files(
  139. name = "packaging_exe_files",
  140. # We break out the driver and LLD here because we can't easily use file
  141. # groups that contain symlinks, so those are manually handled below. Other
  142. # file groups should be directly included.
  143. srcs = [
  144. "prefix_root/bin/carbon",
  145. "prefix_root/lib/carbon/llvm/bin/lld",
  146. ],
  147. attributes = pkg_attributes(mode = "0755"),
  148. strip_prefix = strip_prefix.from_pkg("prefix_root"),
  149. )
  150. # Currently, `rules_pkg` can't replicate symlinks from the main tree. To an
  151. # extent, this is reasonable because we want to be much more explicit about the
  152. # symlink structure in the package where as for the filegroups we're comfortable
  153. # with whatever "just works" for development and testing.
  154. [
  155. pkg_mklink(
  156. name = "packaging_link_lld_alias_" + bin_name,
  157. link_name = "lib/carbon/llvm/bin/" + bin_name,
  158. target = "lld",
  159. )
  160. for bin_name in lld_bin_names
  161. ]
  162. pkg_files(
  163. name = "packaging_data_files",
  164. srcs = [
  165. ":core_data",
  166. ],
  167. strip_prefix = strip_prefix.from_pkg("prefix_root"),
  168. )
  169. pkg_filegroup(
  170. name = "packaging_files",
  171. srcs = [
  172. ":packaging_data_files",
  173. ":packaging_exe_files",
  174. ] + [
  175. ":packaging_link_lld_alias_" + bin_name
  176. for bin_name in lld_bin_names
  177. ],
  178. )
  179. pkg_naming_variables(
  180. name = "packaging_variables",
  181. )
  182. # We build both a compressed and uncompressed tar file with the same code here.
  183. # This lets us use the tar file in testing as it is fast to create, but ship the
  184. # compressed version as a release.
  185. pkg_tar_and_test(
  186. srcs = [":packaging_files"],
  187. name_base = "carbon_toolchain",
  188. package_dir = "carbon_toolchain-$(version)",
  189. package_file_name_base = "carbon_toolchain-$(version)",
  190. package_variables = ":packaging_variables",
  191. stamp = -1, # Allow `--stamp` builds to produce file timestamps.
  192. test_data = [
  193. ":install_data",
  194. ],
  195. test_install_marker = ":install_marker",
  196. )