Files
carbon-lang/toolchain/driver/build_runtimes_subcommand.cpp
T
Chandler Carruth 217c7ba0b2 Begin building libc++ and libc++abi runtimes on demand (#6424)
This builds on the previous work to flesh out more on-demand runtimes
building. It adds building of the `libc++.a` archive runtime.

A number of changes are required for this to work:

- The runtimes build infrastructure needs to support building sources
  from multiple parts of LLVM rather than a single part. We do this by
  lifting the root of the runtimes source paths up a level to a common
  runtimes tree, and installing the runtimes sources below this
  directory.

- Both libc++ and libc++abi runtimes sources need to be installed, and
  we even need to install some interesting parts of llvm-libc that are
  used in the build of libc++.

- We need to generate the site configuration header file for libc++ from
  the CMake template. This includes both setting up a set of
  platform-independent defines and introducing some basic Bazel support
  for processing the CMake template itself.

Doing all of this also exposed some missing features and limitations of
the runtimes building infrastructure that are addressed here.

One note is that all of this just adds libc++ to the explicit
`build-runtimes` command for testing. It doesn't yet trigger
automatically building these prior to linking, or configuring any of the
other subcommands to automatically use these runtimes. All of that will
come in follow-up PRs.

Also, this makes the `clang_runtimes_test` ... _very_ slow in our
default build configuration. Compiling libc++, even with many threads on
a large Linux server requires up to 50 seconds. I'm open to any
suggestions on how to handle this, including disabling the test in
non-optimized builds. I have some ideas to speed this up, but
fundamentally building libc++ is... not cheap.

I did look at some of the existing Bazel tools to process the CMake
template, but they all seemed significantly more complex than what we
need and didn't have broad adoption. Given that, it seemed slightly
better to just roll our own given the simple format.

Two of the new LLVM patch are currently under review upstream and so
hopefully temporary:

- https://github.com/llvm/llvm-project/pull/169155
- https://github.com/llvm/llvm-project/pull/169292
2025-11-27 02:16:51 +00:00

112 lines
3.9 KiB
C++

// 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
#include "toolchain/driver/build_runtimes_subcommand.h"
#include "llvm/TargetParser/Triple.h"
#include "toolchain/driver/clang_runner.h"
#include "toolchain/driver/clang_runtimes.h"
namespace Carbon {
auto BuildRuntimesOptions::Build(CommandLine::CommandBuilder& b) -> void {
b.AddStringOption(
{
.name = "output-directory",
.value_name = "DIR",
.help = R"""(
The directory to populate with runtime libraries suitable for the selected code
generation options.
)""",
},
[&](auto& arg_b) { arg_b.Set(&directory); });
codegen_options.Build(b);
}
static constexpr CommandLine::CommandInfo SubcommandInfo = {
.name = "build-runtimes",
.help = R"""(
Build Carbon's runtime libraries.
This subcommand builds Carbon's runtime libraries for a particular code
generation target, either in their default location or a specified one.
Running this command directly is not necessary as Carbon will build and cache
runtimes as needed when linking, but building them directly can aid in
debugging issues or allow them to be prebuilt, possibly with customized code
generation flags, and used explicitly when linking.
)""",
};
BuildRuntimesSubcommand::BuildRuntimesSubcommand()
: DriverSubcommand(SubcommandInfo) {}
auto BuildRuntimesSubcommand::Run(DriverEnv& driver_env) -> DriverResult {
// Don't run Clang when fuzzing, it is known to not be reliable under fuzzing
// due to many unfixed issues.
if (TestAndDiagnoseIfFuzzingExternalLibraries(driver_env, "clang")) {
return {.success = false};
}
// For diagnosing filesystem or other errors when building runtimes.
CARBON_DIAGNOSTIC(FailureBuildingRuntimes, Error,
"failure building runtimes: {0}", std::string);
auto run_result = RunInternal(driver_env);
if (!run_result.ok()) {
driver_env.emitter.Emit(FailureBuildingRuntimes,
run_result.error().message());
return {.success = false};
}
llvm::outs() << "Built runtimes: " << *run_result << "\n";
return {.success = true};
}
auto BuildRuntimesSubcommand::RunInternal(DriverEnv& driver_env)
-> ErrorOr<std::filesystem::path> {
ClangRunner runner(driver_env.installation, driver_env.fs,
driver_env.vlog_stream);
Runtimes::Cache::Features features = {
.target = options_.codegen_options.target.str()};
bool is_cache = options_.directory.empty();
std::filesystem::path explicit_output_path = options_.directory.str();
if (!is_cache) {
auto access_result = Filesystem::Cwd().Access(explicit_output_path);
if (access_result.ok()) {
return Error("output directory already exists");
}
if (!access_result.error().no_entity()) {
return std::move(access_result).error();
}
}
CARBON_ASSIGN_OR_RETURN(
auto runtimes,
is_cache ? driver_env.runtimes_cache.Lookup(features)
: Runtimes::Make(explicit_output_path, driver_env.vlog_stream));
CARBON_ASSIGN_OR_RETURN(auto tmp_dir, Filesystem::MakeTmpDir());
ClangResourceDirBuilder resource_dir_builder(&runner, driver_env.thread_pool,
llvm::Triple(features.target),
&runtimes);
ClangArchiveRuntimesBuilder<Runtimes::LibUnwind> lib_unwind_builder(
&runner, driver_env.thread_pool, llvm::Triple(features.target),
&runtimes);
ClangArchiveRuntimesBuilder<Runtimes::Libcxx> libcxx_builder(
&runner, driver_env.thread_pool, llvm::Triple(features.target),
&runtimes);
CARBON_RETURN_IF_ERROR(std::move(resource_dir_builder).Wait());
CARBON_RETURN_IF_ERROR(std::move(lib_unwind_builder).Wait());
CARBON_RETURN_IF_ERROR(std::move(libcxx_builder).Wait());
return runtimes.base_path();
}
} // namespace Carbon