BUILD 6.5 KB

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