Files
carbon-lang/toolchain/install/BUILD
T
d49cb3ecfb Start building Clang runtimes on-demand (#5338)
This is the first step to having Clang's runtime libraries fully
available for the Carbon toolchain. This PR focuses on the lowest level
runtimes, the CRT files and the builtins library.

The goal is to intercept Clang runs where it needs these
target-dependent pieces to be available, and build them on demand using
our Clang-running infrastructure. This avoids most of the subprocess
overhead, but there is still some due to missing features in Clang.

This requires exporting the sources for these runtimes from the Bazel
build, and installing them in our target-independent resource directory.
We then build a simplified "build" of these sources within the
`ClangRunner` itself to produce the specific artifacts and layout
expected by Clang.

It also required fixing our use of Clang on macOS to have a default
system root in order to successfully compile or link.

It also required cleaning up how the `ClangRunner` used target
information more generally -- instead of taking the target as
a constructor parameter, it manages its target internally and relies on
the Clang target-specifying command line flags.

I looked at whether we could split this into another layer separate from
the `ClangRunner`, but that proved frustratingly difficult to manage.
While we support building these on-demand as part of a detected link,
that doesn't seem feasible as we don't have the necessary separation
between compilation runs of Clang and link runs of Clang. However,
I have tried to factor the internals to provide as clear of separation
as I could across these.

I have also created a stand-alone subcommand to directly build the
runtimes which allows for easy testing. It also supports building them
into a specific directory, and that directory can in turn be passed to
a Clang invocation. This is designed to work both at the API level with
`ClangRunner` and at the subcommand level.

Currently, the only part of the commandline that is detected and
forwarded to the runtimes build is the target. Eventually, the plan is
to expand this so that we can build a maximally tailored set of runtimes
for a given compilation.

The other big TODO here is to actually implement caching storage of
these runtimes so they aren't built on every execution. Right now, this
uses a somewhat hack-y build of a temporary directory, but this isn't
expected to be suitable long-term. Building these runtimes on *every*
link makes those commands take approximately 15 seconds with an ASan
build like our default development build, and just over 2 seconds in an
optimized build. Because of this, I've kept all of this disabled by
default for now. The goal is that once caching and some other
improvements land, we can enable this by default.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2025-08-22 03:05:21 +00:00

257 lines
7.2 KiB
Python

# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
# Exceptions. See /LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
load(
"@llvm-project//:vars.bzl",
"LLVM_VERSION_MAJOR",
)
load("@rules_python//python:defs.bzl", "py_test")
load("//bazel/cc_rules:defs.bzl", "cc_binary", "cc_library", "cc_test")
load("//bazel/manifest:defs.bzl", "manifest")
load("//toolchain/base:llvm_tools.bzl", "LLVM_MAIN_TOOLS", "LLVM_TOOL_ALIASES")
load("//toolchain/base:runtime_sources.bzl", "BUILTINS_FILEGROUPS", "CRT_FILES")
load("install_filegroups.bzl", "install_filegroup", "install_symlink", "install_target", "make_install_filegroups")
load("pkg_helpers.bzl", "pkg_naming_variables", "pkg_tar_and_test")
package(default_visibility = ["//visibility:public"])
# Build rules supporting the install data tree for the Carbon toolchain.
#
# This populates a synthetic Carbon toolchain installation under the
# `prefix_root` directory. For details on its layout, see `install_dirs` below.
# A library for computing install paths for the toolchain. Note that this
# library does *not* include the data itself, as that would form a dependency
# cycle. Each part of the toolchain should add the narrow data file groups to
# their data dependencies, and then use this library to locate them.
cc_library(
name = "install_paths",
srcs = ["install_paths.cpp"],
hdrs = ["install_paths.h"],
deps = [
"//common:check",
"//common:error",
"//common:filesystem",
"//toolchain/base:llvm_tools",
"@bazel_tools//tools/cpp/runfiles",
"@llvm-project//clang:basic",
"@llvm-project//llvm:Support",
],
)
cc_binary(
name = "test_binary",
testonly = 1,
srcs = ["test_binary.cpp"],
data = [":install_data"],
)
cc_test(
name = "install_paths_test",
size = "small",
srcs = ["install_paths_test.cpp"],
data = [
":install_data",
":test_binary",
],
deps = [
":install_paths",
"//common:check",
"//common:error_test_helpers",
"//common:filesystem",
"//common:ostream",
"//testing/base:global_exe_path",
"//testing/base:gtest_main",
"@bazel_tools//tools/cpp/runfiles",
"@googletest//:gtest",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "install_paths_test_helpers",
testonly = 1,
srcs = ["install_paths_test_helpers.cpp"],
hdrs = ["install_paths_test_helpers.h"],
deps = [
":install_paths",
"//testing/base:global_exe_path",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "busybox_info",
srcs = ["busybox_info.cpp"],
hdrs = ["busybox_info.h"],
deps = [
"//common:error",
"//common:exe_path",
"//common:filesystem",
"@llvm-project//llvm:Support",
],
)
cc_test(
name = "busybox_info_test",
size = "small",
srcs = ["busybox_info_test.cpp"],
deps = [
":busybox_info",
"//common:check",
"//common:filesystem",
"//testing/base:gtest_main",
"@googletest//:gtest",
"@llvm-project//llvm:Support",
],
)
# This target doesn't include prelude libraries. To get a target that has the
# prelude available, use //toolchain.
cc_binary(
name = "carbon-busybox",
srcs = ["busybox_main.cpp"],
deps = [
":busybox_info",
":install_paths",
"//common:all_llvm_targets",
"//common:bazel_working_dir",
"//common:error",
"//common:exe_path",
"//common:init_llvm",
"//common:version_stamp",
"//toolchain/base:llvm_tools_def",
"//toolchain/driver",
"@llvm-project//llvm:Support",
],
)
clang_aliases = [
"clang",
"clang++",
"clang-cl",
"clang-cpp",
]
# TODO: Add remaining aliases of LLD for Windows and WASM when we have support
# for them wired up through the busybox.
lld_aliases = [
"ld.lld",
"ld64.lld",
]
llvm_binaries = clang_aliases + lld_aliases + [
tool.bin_name
for tool in LLVM_MAIN_TOOLS.values()
] + [
"llvm-" + alias
for (_, aliases) in LLVM_TOOL_ALIASES.items()
for alias in aliases
]
filegroup(
name = "clang_headers",
srcs = ["@llvm-project//clang:builtin_headers_gen"],
)
# Collect the runtime sources that are collectively installed into the
# `builtins` directory.
filegroup(
name = "clang_builtins_runtimes",
srcs = CRT_FILES.values() + BUILTINS_FILEGROUPS.values(),
)
# Given a root `prefix_root`, the hierarchy looks like:
#
# - prefix_root/bin: Binaries intended for direct use.
# - prefix_root/lib/carbon: Private data and files.
# - prefix_root/lib/carbon/core: The `Core` package files.
# - prefix_root/lib/carbon/llvm/bin: LLVM binaries.
#
# This will be how installs are provided on Unix-y platforms, and is loosely
# based on the FHS (Filesystem Hierarchy Standard).
install_dirs = {
"bin": [
install_symlink(
"carbon",
"../lib/carbon/carbon-busybox",
is_driver = True,
),
],
"lib/carbon": [
install_target("carbon_install.txt", "carbon_install.txt"),
install_target(
"carbon-busybox",
":carbon-busybox",
executable = True,
is_driver = True,
),
install_filegroup("core", "//core:prelude"),
],
"lib/carbon/llvm/bin": [install_symlink(
name,
"../../carbon-busybox",
is_driver = True,
) for name in llvm_binaries],
"lib/carbon/llvm/lib/clang/" + LLVM_VERSION_MAJOR: [
install_filegroup(
"include",
":clang_headers",
label = "installed_clang_headers",
remove_prefix = "staging/include/",
),
],
"lib/carbon/llvm/lib/clang/" + LLVM_VERSION_MAJOR + "/src": [
install_filegroup("builtins", ":clang_builtins_runtimes", "lib/builtins/"),
],
}
make_install_filegroups(
name = "install_data",
install_dirs = install_dirs,
no_driver_name = "install_data.no_driver",
pkg_name = "pkg_data",
prefix = "prefix_root",
)
py_test(
name = "llvm_symlinks_test",
size = "small",
srcs = ["llvm_symlinks_test.py"],
data = [":install_data"],
)
manifest(
name = "install_data_manifest.txt",
srcs = [":install_data"],
)
# A list of clang's installed builtin header files.
# This is consumed by //toolchain/testing:file_test.
manifest(
name = "clang_headers_manifest.txt",
srcs = [":installed_clang_headers"],
strip_package_dir = True,
)
pkg_naming_variables(
name = "packaging_variables",
)
# We build both a compressed and uncompressed tar file with the same code here.
# This lets us use the tar file in testing as it is fast to create, but ship the
# compressed version as a release.
#
# For manual tests, the tar rules are `carbon_toolchain_tar_rule` and
# `carbon_toolchain_tar_gz_rule`.
pkg_tar_and_test(
srcs = [":pkg_data"],
install_data_manifest = ":install_data_manifest.txt",
name_base = "carbon_toolchain",
package_dir = "carbon_toolchain-$(version)",
package_file_name_base = "carbon_toolchain-$(version)",
package_variables = ":packaging_variables",
stamp = -1, # Allow `--stamp` builds to produce file timestamps.
)