|
|
@@ -4,19 +4,18 @@
|
|
|
|
|
|
#include "toolchain/driver/clang_runner.h"
|
|
|
|
|
|
+#include <stdlib.h>
|
|
|
#include <unistd.h>
|
|
|
|
|
|
-#include <algorithm>
|
|
|
#include <filesystem>
|
|
|
#include <memory>
|
|
|
-#include <numeric>
|
|
|
#include <optional>
|
|
|
#include <string>
|
|
|
-#include <system_error>
|
|
|
#include <utility>
|
|
|
-#include <variant>
|
|
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
+#include "clang/Basic/DiagnosticDriver.h"
|
|
|
+#include "clang/Basic/DiagnosticIDs.h"
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
|
|
#include "clang/CodeGen/ObjectFilePCHContainerWriter.h"
|
|
|
#include "clang/Driver/Compilation.h"
|
|
|
@@ -28,25 +27,42 @@
|
|
|
#include "clang/Frontend/Utils.h"
|
|
|
#include "clang/FrontendTool/Utils.h"
|
|
|
#include "clang/Serialization/ObjectFilePCHContainerReader.h"
|
|
|
-#include "common/filesystem.h"
|
|
|
+#include "clang/Serialization/PCHContainerOperations.h"
|
|
|
+#include "common/check.h"
|
|
|
+#include "common/error.h"
|
|
|
#include "common/vlog.h"
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
-#include "llvm/ADT/ScopeExit.h"
|
|
|
+#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
|
|
+#include "llvm/ADT/STLExtras.h"
|
|
|
+#include "llvm/ADT/SmallVector.h"
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
-#include "llvm/Object/ArchiveWriter.h"
|
|
|
+#include "llvm/Support/Allocator.h"
|
|
|
+#include "llvm/Support/BuryPointer.h"
|
|
|
+#include "llvm/Support/CommandLine.h"
|
|
|
#include "llvm/Support/Error.h"
|
|
|
-#include "llvm/Support/FileSystem.h"
|
|
|
-#include "llvm/Support/FormatAdapters.h"
|
|
|
#include "llvm/Support/LLVMDriver.h"
|
|
|
-#include "llvm/Support/Path.h"
|
|
|
-#include "llvm/Support/Program.h"
|
|
|
+#include "llvm/Support/ThreadPool.h"
|
|
|
#include "llvm/Support/TimeProfiler.h"
|
|
|
#include "llvm/Support/Timer.h"
|
|
|
+#include "llvm/Support/raw_ostream.h"
|
|
|
#include "llvm/TargetParser/Host.h"
|
|
|
-#include "toolchain/base/runtime_sources.h"
|
|
|
+#include "toolchain/driver/clang_runtimes.h"
|
|
|
+#include "toolchain/driver/runtimes_cache.h"
|
|
|
+#include "toolchain/driver/tool_runner_base.h"
|
|
|
+#include "toolchain/install/install_paths.h"
|
|
|
+
|
|
|
+// Defined in:
|
|
|
+// https://github.com/llvm/llvm-project/blob/main/clang/tools/driver/driver.cpp
|
|
|
+//
|
|
|
+// While not in a header, this is the API used by llvm-driver.cpp for
|
|
|
+// busyboxing.
|
|
|
+//
|
|
|
+// NOLINTNEXTLINE(readability-identifier-naming)
|
|
|
+auto clang_main(int Argc, char** Argv, const llvm::ToolContext& ToolContext)
|
|
|
+ -> int;
|
|
|
|
|
|
namespace Carbon {
|
|
|
|
|
|
@@ -127,33 +143,35 @@ static auto IsNonLinkCommand(llvm::ArrayRef<llvm::StringRef> args) -> bool {
|
|
|
}
|
|
|
|
|
|
auto ClangRunner::RunWithPrebuiltRuntimes(llvm::ArrayRef<llvm::StringRef> args,
|
|
|
- Runtimes& prebuilt_runtimes)
|
|
|
+ Runtimes& prebuilt_runtimes,
|
|
|
+ bool enable_leaking)
|
|
|
-> ErrorOr<bool> {
|
|
|
// Check the args to see if we have a known target-independent command. If so,
|
|
|
// directly dispatch it to avoid the cost of building the target resource
|
|
|
// directory.
|
|
|
// TODO: Maybe handle response file expansion similar to the Clang CLI?
|
|
|
if (args.empty() || args[0].starts_with("-cc1") || IsNonLinkCommand(args)) {
|
|
|
- return RunWithNoRuntimes(args);
|
|
|
+ return RunWithNoRuntimes(args, enable_leaking);
|
|
|
}
|
|
|
|
|
|
std::string target = ComputeClangTarget(args);
|
|
|
|
|
|
CARBON_ASSIGN_OR_RETURN(std::filesystem::path prebuilt_resource_dir_path,
|
|
|
prebuilt_runtimes.Get(Runtimes::ClangResourceDir));
|
|
|
- return RunInternal(args, target, prebuilt_resource_dir_path.native());
|
|
|
+ return RunInternal(args, target, prebuilt_resource_dir_path.native(),
|
|
|
+ enable_leaking);
|
|
|
}
|
|
|
|
|
|
auto ClangRunner::Run(llvm::ArrayRef<llvm::StringRef> args,
|
|
|
Runtimes::Cache& runtimes_cache,
|
|
|
- llvm::ThreadPoolInterface& runtimes_build_thread_pool)
|
|
|
- -> ErrorOr<bool> {
|
|
|
+ llvm::ThreadPoolInterface& runtimes_build_thread_pool,
|
|
|
+ bool enable_leaking) -> ErrorOr<bool> {
|
|
|
// Check the args to see if we have a known target-independent command. If so,
|
|
|
// directly dispatch it to avoid the cost of building the target resource
|
|
|
// directory.
|
|
|
// TODO: Maybe handle response file expansion similar to the Clang CLI?
|
|
|
if (args.empty() || args[0].starts_with("-cc1") || IsNonLinkCommand(args)) {
|
|
|
- return RunWithNoRuntimes(args);
|
|
|
+ return RunWithNoRuntimes(args, enable_leaking);
|
|
|
}
|
|
|
|
|
|
std::string target = ComputeClangTarget(args);
|
|
|
@@ -168,95 +186,26 @@ auto ClangRunner::Run(llvm::ArrayRef<llvm::StringRef> args,
|
|
|
// not once we are running Clang with the built runtime.
|
|
|
std::filesystem::path resource_dir_path;
|
|
|
{
|
|
|
- // Build the temporary directory and threadpool needed.
|
|
|
- CARBON_ASSIGN_OR_RETURN(Filesystem::RemovingDir tmp_dir,
|
|
|
- Filesystem::MakeTmpDir());
|
|
|
-
|
|
|
- CARBON_ASSIGN_OR_RETURN(
|
|
|
- resource_dir_path,
|
|
|
- BuildTargetResourceDir(features, runtimes, tmp_dir.abs_path(),
|
|
|
- runtimes_build_thread_pool));
|
|
|
+ ClangResourceDirBuilder builder(this, &runtimes_build_thread_pool,
|
|
|
+ llvm::Triple(features.target), &runtimes);
|
|
|
+ CARBON_ASSIGN_OR_RETURN(resource_dir_path, std::move(builder).Wait());
|
|
|
}
|
|
|
|
|
|
// Note that this function always successfully runs `clang` and returns a bool
|
|
|
// to indicate whether `clang` itself succeeded, not whether the runner was
|
|
|
// able to run it. As a consequence, even a `false` here is a non-`Error`
|
|
|
// return.
|
|
|
- return RunInternal(args, target, resource_dir_path.native());
|
|
|
+ return RunInternal(args, target, resource_dir_path.native(), enable_leaking);
|
|
|
}
|
|
|
|
|
|
-auto ClangRunner::RunWithNoRuntimes(llvm::ArrayRef<llvm::StringRef> args)
|
|
|
- -> bool {
|
|
|
+auto ClangRunner::RunWithNoRuntimes(llvm::ArrayRef<llvm::StringRef> args,
|
|
|
+ bool enable_leaking) -> bool {
|
|
|
std::string target = ComputeClangTarget(args);
|
|
|
- return RunInternal(args, target, std::nullopt);
|
|
|
+ return RunInternal(args, target, std::nullopt, enable_leaking);
|
|
|
}
|
|
|
|
|
|
-auto ClangRunner::BuildTargetResourceDir(
|
|
|
- const Runtimes::Cache::Features& features, Runtimes& runtimes,
|
|
|
- const std::filesystem::path& tmp_path, llvm::ThreadPoolInterface& threads)
|
|
|
- -> ErrorOr<std::filesystem::path> {
|
|
|
- // Disable any leaking of memory while building the target resource dir, and
|
|
|
- // restore the previous setting at the end.
|
|
|
- auto restore_leak_flag = llvm::make_scope_exit(
|
|
|
- [&, orig_flag = enable_leaking_] { enable_leaking_ = orig_flag; });
|
|
|
- enable_leaking_ = false;
|
|
|
-
|
|
|
- CARBON_ASSIGN_OR_RETURN(auto build_dir,
|
|
|
- runtimes.Build(Runtimes::ClangResourceDir));
|
|
|
- if (std::holds_alternative<std::filesystem::path>(build_dir)) {
|
|
|
- // Found cached build.
|
|
|
- return std::get<std::filesystem::path>(std::move(build_dir));
|
|
|
- }
|
|
|
-
|
|
|
- auto builder = std::get<Runtimes::Builder>(std::move(build_dir));
|
|
|
- std::string target = features.target;
|
|
|
-
|
|
|
- // Symlink the installation's `include` and `share` directories.
|
|
|
- std::filesystem::path install_resource_path =
|
|
|
- installation_->clang_resource_path();
|
|
|
- CARBON_RETURN_IF_ERROR(
|
|
|
- builder.dir().Symlink("include", install_resource_path / "include"));
|
|
|
- CARBON_RETURN_IF_ERROR(
|
|
|
- builder.dir().Symlink("share", install_resource_path / "share"));
|
|
|
-
|
|
|
- // Create the target's `lib` directory.
|
|
|
- std::filesystem::path lib_path =
|
|
|
- std::filesystem::path("lib") / std::string_view(target);
|
|
|
- CARBON_ASSIGN_OR_RETURN(Filesystem::Dir lib_dir,
|
|
|
- builder.dir().CreateDirectories(lib_path));
|
|
|
-
|
|
|
- llvm::Triple target_triple(target);
|
|
|
- if (target_triple.isOSWindows()) {
|
|
|
- return Error("TODO: Windows runtimes are untested and not yet supported.");
|
|
|
- }
|
|
|
-
|
|
|
- llvm::ThreadPoolTaskGroup task_group(threads);
|
|
|
-
|
|
|
- // For Linux targets, the system libc (typically glibc) doesn't necessarily
|
|
|
- // provide the CRT begin/end files, and so we need to build them.
|
|
|
- if (target_triple.isOSLinux()) {
|
|
|
- task_group.async(
|
|
|
- [this, target,
|
|
|
- path = builder.path() / lib_path / "clang_rt.crtbegin.o"] {
|
|
|
- BuildCrtFile(target, RuntimeSources::CrtBegin, path);
|
|
|
- });
|
|
|
- task_group.async(
|
|
|
- [this, target, path = builder.path() / lib_path / "clang_rt.crtend.o"] {
|
|
|
- BuildCrtFile(target, RuntimeSources::CrtEnd, path);
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- CARBON_RETURN_IF_ERROR(
|
|
|
- BuildBuiltinsLib(target, target_triple, tmp_path, lib_dir, threads));
|
|
|
-
|
|
|
- // Now wait for all the queued builds to complete before we commit the
|
|
|
- // runtimes into the cache.
|
|
|
- task_group.wait();
|
|
|
-
|
|
|
- return std::move(builder).Commit();
|
|
|
-}
|
|
|
-
|
|
|
-auto ClangRunner::RunCC1(llvm::SmallVectorImpl<const char*>& cc1_args) -> int {
|
|
|
+auto ClangRunner::RunCC1(llvm::SmallVectorImpl<const char*>& cc1_args,
|
|
|
+ bool enable_leaking) -> int {
|
|
|
llvm::BumpPtrAllocator allocator;
|
|
|
llvm::cl::ExpansionContext expansion_context(
|
|
|
allocator, llvm::cl::TokenizeGNUCommandLine);
|
|
|
@@ -300,7 +249,7 @@ auto ClangRunner::RunCC1(llvm::SmallVectorImpl<const char*>& cc1_args) -> int {
|
|
|
std::move(invocation), std::move(pch_ops));
|
|
|
|
|
|
// Override the disabling of free when we don't want to leak memory.
|
|
|
- if (!enable_leaking_) {
|
|
|
+ if (!enable_leaking) {
|
|
|
clang_instance->getFrontendOpts().DisableFree = false;
|
|
|
clang_instance->getCodeGenOpts().DisableFree = false;
|
|
|
}
|
|
|
@@ -400,7 +349,8 @@ auto ClangRunner::RunCC1(llvm::SmallVectorImpl<const char*>& cc1_args) -> int {
|
|
|
|
|
|
auto ClangRunner::RunInternal(
|
|
|
llvm::ArrayRef<llvm::StringRef> args, llvm::StringRef target,
|
|
|
- std::optional<llvm::StringRef> target_resource_dir_path) -> bool {
|
|
|
+ std::optional<llvm::StringRef> target_resource_dir_path,
|
|
|
+ bool enable_leaking) -> bool {
|
|
|
std::string clang_path = installation_->clang_path();
|
|
|
|
|
|
// Rebuild the args as C-string args.
|
|
|
@@ -410,8 +360,27 @@ auto ClangRunner::RunInternal(
|
|
|
|
|
|
// Handle special dispatch for CC1 commands as they don't use the driver.
|
|
|
if (!args.empty() && args[0].starts_with("-cc1")) {
|
|
|
- CARBON_VLOG("Calling Clang's CC1...");
|
|
|
- int exit_code = RunCC1(cstr_args);
|
|
|
+ if (args[0] == "-cc1") {
|
|
|
+ CARBON_VLOG("Dispatching `-cc1` command line");
|
|
|
+ int exit_code = RunCC1(cstr_args, enable_leaking);
|
|
|
+ // TODO: Should this be forwarding the full exit code?
|
|
|
+ return exit_code == 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Other CC1-based invocations need to dispatch into the `clang_main`
|
|
|
+ // routine to work correctly. This means they're not reliable in a library
|
|
|
+ // context but currently there is too much logic to reasonably extract here.
|
|
|
+ // This at least allows simple cases (often when directly used on the
|
|
|
+ // command line) to work correctly.
|
|
|
+ //
|
|
|
+ // TODO: Factor the relevant code paths into a library API or move this into
|
|
|
+ // the busybox dispatch logic.
|
|
|
+ CARBON_VLOG("Calling clang_main for a cc1-based invocation...");
|
|
|
+ // cstr_args[0] will be the `clang_path` so we don't need the prepend arg.
|
|
|
+ llvm::ToolContext tool_context = {
|
|
|
+ .Path = cstr_args[0], .PrependArg = "clang", .NeedsPrependArg = false};
|
|
|
+ int exit_code = clang_main(
|
|
|
+ cstr_args.size(), const_cast<char**>(cstr_args.data()), tool_context);
|
|
|
// TODO: Should this be forwarding the full exit code?
|
|
|
return exit_code == 0;
|
|
|
}
|
|
|
@@ -481,8 +450,9 @@ auto ClangRunner::RunInternal(
|
|
|
//
|
|
|
// Also note that we only do `-disable-free` filtering in the in-process
|
|
|
// execution here, as subprocesses leaking memory won't impact this process.
|
|
|
- auto cc1_main = [this](llvm::SmallVectorImpl<const char*>& cc1_args) -> int {
|
|
|
- return RunCC1(cc1_args);
|
|
|
+ auto cc1_main = [this, enable_leaking](
|
|
|
+ llvm::SmallVectorImpl<const char*>& cc1_args) -> int {
|
|
|
+ return RunCC1(cc1_args, enable_leaking);
|
|
|
};
|
|
|
driver.CC1Main = cc1_main;
|
|
|
|
|
|
@@ -525,180 +495,4 @@ auto ClangRunner::RunInternal(
|
|
|
return result == 0 && failing_commands.empty();
|
|
|
}
|
|
|
|
|
|
-auto ClangRunner::BuildCrtFile(llvm::StringRef target, llvm::StringRef src_file,
|
|
|
- const std::filesystem::path& out_path) -> void {
|
|
|
- std::filesystem::path src_path =
|
|
|
- installation_->llvm_runtime_srcs() / std::string_view(src_file);
|
|
|
- CARBON_VLOG("Building `{0}' from `{1}`...\n", out_path, src_path);
|
|
|
-
|
|
|
- std::string target_arg = llvm::formatv("--target={0}", target).str();
|
|
|
- CARBON_CHECK(RunWithNoRuntimes({
|
|
|
- "-no-canonical-prefixes",
|
|
|
- target_arg,
|
|
|
- "-DCRT_HAS_INITFINI_ARRAY",
|
|
|
- "-DEH_USE_FRAME_REGISTRY",
|
|
|
- "-O3",
|
|
|
- "-fPIC",
|
|
|
- "-ffreestanding",
|
|
|
- "-std=c11",
|
|
|
- "-w",
|
|
|
- "-c",
|
|
|
- "-o",
|
|
|
- out_path.native(),
|
|
|
- src_path.native(),
|
|
|
- }));
|
|
|
-}
|
|
|
-
|
|
|
-auto ClangRunner::CollectBuiltinsSrcFiles(const llvm::Triple& target_triple)
|
|
|
- -> llvm::SmallVector<llvm::StringRef> {
|
|
|
- llvm::SmallVector<llvm::StringRef> src_files;
|
|
|
- auto append_src_files =
|
|
|
- [&](auto input_srcs,
|
|
|
- llvm::function_ref<bool(llvm::StringRef)> filter_out = {}) {
|
|
|
- for (llvm::StringRef input_src : input_srcs) {
|
|
|
- if (!input_src.ends_with(".c") && !input_src.ends_with(".S")) {
|
|
|
- // Not a compiled file.
|
|
|
- continue;
|
|
|
- }
|
|
|
- if (filter_out && filter_out(input_src)) {
|
|
|
- // Filtered out.
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- src_files.push_back(input_src);
|
|
|
- }
|
|
|
- };
|
|
|
- append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsGenericSrcs));
|
|
|
- append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsBf16Srcs));
|
|
|
- if (target_triple.isArch64Bit()) {
|
|
|
- append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsTfSrcs));
|
|
|
- }
|
|
|
- auto filter_out_chkstk = [&](llvm::StringRef src) {
|
|
|
- return !target_triple.isOSWindows() || !src.ends_with("chkstk.S");
|
|
|
- };
|
|
|
- if (target_triple.isAArch64()) {
|
|
|
- append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsAarch64Srcs),
|
|
|
- filter_out_chkstk);
|
|
|
- } else if (target_triple.isX86()) {
|
|
|
- append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsX86ArchSrcs));
|
|
|
- if (target_triple.isArch64Bit()) {
|
|
|
- append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsX86_64Srcs),
|
|
|
- filter_out_chkstk);
|
|
|
- } else {
|
|
|
- // TODO: This should be turned into a nice user-facing diagnostic about an
|
|
|
- // unsupported target.
|
|
|
- CARBON_CHECK(
|
|
|
- target_triple.isArch32Bit(),
|
|
|
- "The Carbon toolchain doesn't currently support 16-bit x86.");
|
|
|
- append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsI386Srcs),
|
|
|
- filter_out_chkstk);
|
|
|
- }
|
|
|
- } else {
|
|
|
- // TODO: This should be turned into a nice user-facing diagnostic about an
|
|
|
- // unsupported target.
|
|
|
- CARBON_FATAL("Target architecture is not supported: {0}",
|
|
|
- target_triple.str());
|
|
|
- }
|
|
|
- return src_files;
|
|
|
-}
|
|
|
-
|
|
|
-auto ClangRunner::BuildBuiltinsFile(llvm::StringRef target,
|
|
|
- llvm::StringRef src_file,
|
|
|
- const std::filesystem::path& out_path)
|
|
|
- -> void {
|
|
|
- std::filesystem::path src_path =
|
|
|
- installation_->llvm_runtime_srcs() / std::string_view(src_file);
|
|
|
- CARBON_VLOG("Building `{0}' from `{1}`...\n", out_path, src_path);
|
|
|
-
|
|
|
- std::string target_arg = llvm::formatv("--target={0}", target).str();
|
|
|
- CARBON_CHECK(RunWithNoRuntimes({
|
|
|
- "-no-canonical-prefixes",
|
|
|
- target_arg,
|
|
|
- "-O3",
|
|
|
- "-fPIC",
|
|
|
- "-ffreestanding",
|
|
|
- "-fno-builtin",
|
|
|
- "-fomit-frame-pointer",
|
|
|
- "-fvisibility=hidden",
|
|
|
- "-std=c11",
|
|
|
- "-w",
|
|
|
- "-c",
|
|
|
- "-o",
|
|
|
- out_path.native(),
|
|
|
- src_path.native(),
|
|
|
- }));
|
|
|
-}
|
|
|
-
|
|
|
-auto ClangRunner::BuildBuiltinsLib(llvm::StringRef target,
|
|
|
- const llvm::Triple& target_triple,
|
|
|
- const std::filesystem::path& tmp_path,
|
|
|
- Filesystem::DirRef lib_dir,
|
|
|
- llvm::ThreadPoolInterface& threads)
|
|
|
- -> ErrorOr<Success> {
|
|
|
- llvm::SmallVector<llvm::StringRef> src_files =
|
|
|
- CollectBuiltinsSrcFiles(target_triple);
|
|
|
-
|
|
|
- CARBON_ASSIGN_OR_RETURN(Filesystem::Dir tmp_dir,
|
|
|
- Filesystem::Cwd().OpenDir(tmp_path));
|
|
|
-
|
|
|
- // `NewArchiveMember` isn't default constructable unfortunately, so we first
|
|
|
- // build the objects using an optional wrapper.
|
|
|
- llvm::SmallVector<std::optional<llvm::NewArchiveMember>> objs;
|
|
|
- objs.resize(src_files.size());
|
|
|
- llvm::ThreadPoolTaskGroup member_group(threads);
|
|
|
- for (auto [src_file, obj] : llvm::zip_equal(src_files, objs)) {
|
|
|
- // Create any subdirectories needed for this file.
|
|
|
- std::filesystem::path src_path = src_file.str();
|
|
|
- if (src_path.has_parent_path()) {
|
|
|
- CARBON_RETURN_IF_ERROR(tmp_dir.CreateDirectories(src_path.parent_path()));
|
|
|
- }
|
|
|
-
|
|
|
- member_group.async([this, target, src_file, &obj, &tmp_path] {
|
|
|
- std::filesystem::path obj_path = tmp_path / std::string_view(src_file);
|
|
|
- obj_path += ".o";
|
|
|
- BuildBuiltinsFile(target, src_file, obj_path);
|
|
|
-
|
|
|
- auto obj_result = llvm::NewArchiveMember::getFile(obj_path.native(),
|
|
|
- /*Deterministic=*/true);
|
|
|
- CARBON_CHECK(obj_result, "TODO: Diagnose this: {0}",
|
|
|
- llvm::fmt_consume(obj_result.takeError()));
|
|
|
- obj = std::move(*obj_result);
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- // Now build an archive out of the `.o` files for the builtins. Note that we
|
|
|
- // build this directly into the `lib_dir` as this is expected to be a staging
|
|
|
- // directory and cleaned up on errors.
|
|
|
- std::filesystem::path builtins_a_path = "libclang_rt.builtins.a";
|
|
|
- CARBON_ASSIGN_OR_RETURN(
|
|
|
- Filesystem::WriteFile builtins_a_file,
|
|
|
- lib_dir.OpenWriteOnly(builtins_a_path, Filesystem::CreateAlways));
|
|
|
-
|
|
|
- // Wait for all the object compiles to complete, and then move the objects out
|
|
|
- // of their optional wrappers to match the API required by the archive writer.
|
|
|
- member_group.wait();
|
|
|
- llvm::SmallVector<llvm::NewArchiveMember> unwrapped_objs;
|
|
|
- unwrapped_objs.reserve(objs.size());
|
|
|
- for (auto& obj : objs) {
|
|
|
- unwrapped_objs.push_back(*std::move(obj));
|
|
|
- }
|
|
|
- objs.clear();
|
|
|
-
|
|
|
- // Write the actual archive.
|
|
|
- {
|
|
|
- llvm::raw_fd_ostream builtins_a_os = builtins_a_file.WriteStream();
|
|
|
- llvm::Error archive_err = llvm::writeArchiveToStream(
|
|
|
- builtins_a_os, unwrapped_objs, llvm::SymtabWritingMode::NormalSymtab,
|
|
|
- target_triple.isOSDarwin() ? llvm::object::Archive::K_DARWIN
|
|
|
- : llvm::object::Archive::K_GNU,
|
|
|
- /*Deterministic=*/true, /*Thin=*/false);
|
|
|
- // The presence of an error is `true`.
|
|
|
- if (archive_err) {
|
|
|
- return Error(llvm::toString(std::move(archive_err)));
|
|
|
- }
|
|
|
- }
|
|
|
- CARBON_RETURN_IF_ERROR(std::move(builtins_a_file).Close());
|
|
|
- return Success();
|
|
|
-}
|
|
|
-
|
|
|
} // namespace Carbon
|