// 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/clang_runtimes.h" #include #include #include #include #include #include #include #include #include #include #include "common/check.h" #include "common/error.h" #include "common/filesystem.h" #include "common/latch.h" #include "common/vlog.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/STLFunctionalExtras.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/IR/LLVMContext.h" #include "llvm/Object/Archive.h" #include "llvm/Object/ArchiveWriter.h" #include "llvm/Support/Error.h" #include "llvm/Support/FormatAdapters.h" #include "llvm/Support/FormatVariadic.h" #include "llvm/Support/Path.h" #include "llvm/Support/ThreadPool.h" #include "llvm/Support/raw_ostream.h" #include "llvm/TargetParser/Host.h" #include "llvm/TargetParser/Triple.h" #include "toolchain/base/kind_switch.h" #include "toolchain/base/runtimes_build_info.h" #include "toolchain/driver/clang_runner.h" #include "toolchain/driver/runtimes_cache.h" namespace Carbon { auto ClangRuntimesBuilderBase::ArchiveBuilder::Setup(Latch::Handle latch_handle) -> void { // `NewArchiveMember` isn't default constructable unfortunately, so we have to // manually populate the vector with errors that we'll replace with the actual // result in each thread. objs_.reserve(src_files_.size()); for (const auto& _ : src_files_) { objs_.push_back(Error("Never constructed archive member!")); } // Finish building the archive when the last compile finishes. Latch::Handle comp_latch_handle = compilation_latch_.Init([this, latch_handle] { result_ = Finish(); }); // Add all the compiles to the thread pool to run concurrently. The latch // handle ensures the last one triggers the finishing closure above. for (auto [src_file, obj] : llvm::zip_equal(src_files_, objs_)) { builder_->tasks_.async([this, comp_latch_handle, src_file, &obj]() mutable { obj = CompileMember(src_file); }); } } auto ClangRuntimesBuilderBase::ArchiveBuilder::Finish() -> ErrorOr { // We build this directly into the desired location as this is expected to be // a staging directory and cleaned up on errors. We do need to create any // intermediate directories. Filesystem::DirRef runtimes_dir = builder_->runtimes_builder_->dir(); if (archive_path_.has_parent_path()) { CARBON_RETURN_IF_ERROR( runtimes_dir.CreateDirectories(archive_path_.parent_path())); } // Check if any compilations ended up producing an error. If so, return the // first error for the entire function. Otherwise, move the archive member // into a direct vector to match the required archive building API. llvm::SmallVector unwrapped_objs; unwrapped_objs.reserve(objs_.size()); for (auto& obj : objs_) { if (!obj.ok()) { return std::move(obj).error(); } unwrapped_objs.push_back(*std::move(obj)); } objs_.clear(); // Remove any directories created for the object files, the files should // already be removed. We walk the sorted list of these in reverse so we // remove child directories before parent directories. for (const auto& obj_dir : llvm::reverse(obj_dirs_)) { auto rmdir_result = runtimes_dir.Rmdir(obj_dir); // Don't return an error on failure here as this has no problematic // effect, just log that we couldn't clean up a directory. if (!rmdir_result.ok()) { CARBON_VLOG("Unable to remove object directory `{0}` in the runtime: {1}", obj_dir.native(), rmdir_result.error()); } } // Write the actual archive. CARBON_ASSIGN_OR_RETURN( Filesystem::WriteFile archive_file, runtimes_dir.OpenWriteOnly(archive_path_, Filesystem::CreateAlways)); { llvm::raw_fd_ostream archive_os = archive_file.WriteStream(); llvm::Error archive_err = llvm::writeArchiveToStream( archive_os, unwrapped_objs, llvm::SymtabWritingMode::NormalSymtab, builder_->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) { (void)std::move(archive_file).Close(); return Error(llvm::toString(std::move(archive_err))); } } // Close and return any errors, potentially from the writes above. CARBON_RETURN_IF_ERROR(std::move(archive_file).Close()); return Success(); } auto ClangRuntimesBuilderBase::ArchiveBuilder::CreateObjDir( const std::filesystem::path& src_path) -> ErrorOr { auto obj_dir_path = src_path.parent_path(); if (obj_dir_path.empty()) { return Success(); } std::scoped_lock lock(obj_dirs_mu_); auto* it = std::lower_bound(obj_dirs_.begin(), obj_dirs_.end(), obj_dir_path); if (it != obj_dirs_.end() && *it == obj_dir_path) { return Success(); } auto create_result = builder_->runtimes_builder_->dir().CreateDirectories(obj_dir_path); if (!create_result.ok()) { return Error(llvm::formatv( "Unable to create object directory mirroring source file `{0}`: {1}", src_path, create_result.error())); } it = obj_dirs_.insert(it, obj_dir_path); // Also insert any parent paths. These should always sort earlier. CARBON_DCHECK(!obj_dir_path.has_parent_path() || obj_dir_path.parent_path() < obj_dir_path); obj_dir_path = obj_dir_path.parent_path(); while (!obj_dir_path.empty()) { it = std::lower_bound(obj_dirs_.begin(), it, obj_dir_path); if (*it != obj_dir_path) { it = obj_dirs_.insert(it, obj_dir_path); } obj_dir_path = obj_dir_path.parent_path(); } return Success(); } auto ClangRuntimesBuilderBase::ArchiveBuilder::CompileMember( llvm::StringRef src_file) -> ErrorOr { // Create any obj subdirectories needed for this file. CARBON_RETURN_IF_ERROR(CreateObjDir(src_file.str())); std::filesystem::path src_path = srcs_root_ / std::string_view(src_file); std::filesystem::path obj_path = builder_->runtimes_builder_->path() / std::string_view(src_file); obj_path += ".o"; CARBON_VLOG("Building `{0}' from `{1}`...\n", obj_path, src_file); llvm::SmallVector args(cflags_); // Add language-specific flags based on file extension. // // Currently, we hard code a sufficiently "recent" C++ standard, but this is // arbitrary and brittle. We'll have to update these any time one of the // libraries uses a too-new feature. // // TODO: We should eventually switch to something more like `/std:c++latest` // in MSVC-style command lines, but would need that implemented in Clang. if (src_file.ends_with(".c")) { args.push_back("-std=c11"); } else if (src_file.ends_with(".cpp")) { args.push_back("-std=c++26"); } // Collect the additional required flags and dynamic flags for this builder. args.append({ "-c", builder_->target_flag_, "-o", obj_path.native(), src_path.native(), }); CARBON_ASSIGN_OR_RETURN(bool success, builder_->clang_->RunWithNoRuntimes(args)); if (!success) { return Error( llvm::formatv("Failed to compile runtime source file '{0}'", src_file)); } auto obj_result = llvm::NewArchiveMember::getFile(obj_path.native(), /*Deterministic=*/true); if (!obj_result) { return Error(llvm::formatv("Unable to read `{0}` object file: {1}", src_file, llvm::fmt_consume(obj_result.takeError()))); } // Only use the basename as the member name to match the behavior of `ar`. We // also specifically use the LLVM path function rather than the standard // library as it allows us to get the filename within the member-owned // filename storage. obj_result->MemberName = llvm::sys::path::filename(obj_result->MemberName); // Unlink the object file once we've read it -- we only want to retain the // copy inside the archive member and there's no advantage to using // thin-archives or something else that leaves the object file in place. // However, we log and ignore any errors here as they aren't fatal. auto unlink_result = builder_->runtimes_builder_->dir().Unlink(obj_path); if (!unlink_result.ok()) { CARBON_VLOG("Unable to unlink object file `{0}`: {1}\n", obj_path, unlink_result.error()); } return std::move(*obj_result); } template requires IsClangArchiveRuntimes ClangArchiveRuntimesBuilder::ClangArchiveRuntimesBuilder( ClangRunner* clang, llvm::ThreadPoolInterface* threads, llvm::Triple target_triple, Runtimes* runtimes) : ClangRuntimesBuilderBase(clang, threads, std::move(target_triple)) { // Ensure we're on a platform where we _can_ build a working runtime. if (target_triple_.isOSWindows()) { result_ = Error("TODO: Windows runtimes are untested and not yet supported."); return; } auto build_dir_or_error = runtimes->Build(Component); if (!build_dir_or_error.ok()) { result_ = std::move(build_dir_or_error).error(); return; } auto build_dir = *(std::move(build_dir_or_error)); CARBON_KIND_SWITCH(std::move(build_dir)) { case CARBON_KIND(std::filesystem::path build_dir_path): { // Found cached build. result_ = std::move(build_dir_path); return; } case CARBON_KIND(Runtimes::Builder builder): { runtimes_builder_ = std::move(builder); // Building the runtimes is handled below. break; } } if constexpr (Component == Runtimes::LibUnwind) { archive_path_ = std::filesystem::path("lib") / "libunwind.a"; include_paths_ = {installation().libunwind_path() / "include"}; } else if constexpr (Component == Runtimes::Libcxx) { archive_path_ = std::filesystem::path("lib") / "libc++.a"; include_paths_ = { installation().libcxx_path() / "include", // Some private headers of libc++ are nested in the source directory. installation().libcxx_path() / "src", installation().libcxxabi_path() / "include", // Libc++ also uses llvm-libc header-only libraries for parts of its // implementation. All the `#include`s are relative to the root of the // internal libc source tree rather than an `include` directory. installation().libc_path() / "internal", }; } else { static_assert(false, "Invalid runtimes component for an archive runtime builder."); } archive_.emplace(this, archive_path_, installation().root(), CollectSrcFiles(), CollectCflags()); tasks_.async([this]() mutable { Setup(); }); } template requires IsClangArchiveRuntimes auto ClangArchiveRuntimesBuilder::CollectSrcFiles() -> llvm::SmallVector { if constexpr (Component == Runtimes::LibUnwind) { return llvm::to_vector_of(llvm::make_filter_range( RuntimesBuildInfo::LibunwindSrcs, [](llvm::StringRef src) { return src.ends_with(".c") || src.ends_with(".cpp") || src.ends_with(".S"); })); } else if constexpr (Component == Runtimes::Libcxx) { auto libcxx_target_srcs = target_triple_.isOSWindows() ? llvm::ArrayRef(RuntimesBuildInfo::LibcxxWin32Srcs) : target_triple_.isMacOSX() ? llvm::ArrayRef(RuntimesBuildInfo::LibcxxMacosSrcs) : llvm::ArrayRef(RuntimesBuildInfo::LibcxxLinuxSrcs); auto libcxx_srcs = llvm::make_filter_range( libcxx_target_srcs, [](llvm::StringRef src) { return src.ends_with(".cpp"); }); auto libcxxabi_srcs = llvm::make_filter_range( RuntimesBuildInfo::LibcxxabiSrcs, [](llvm::StringRef src) { return src.ends_with(".cpp"); }); return llvm::to_vector( llvm::concat(libcxx_srcs, libcxxabi_srcs)); } else { static_assert(false, "Invalid runtimes component for an archive runtime builder."); } } template requires IsClangArchiveRuntimes auto ClangArchiveRuntimesBuilder::CollectCflags() -> llvm::SmallVector { // Start with some hard-coded flags used across any runtime. // // TODO: It would be nice to plumb through an option to enable (some) warnings // when building runtimes, especially for folks working directly on the Carbon // toolchain to validate our builds of runtimes. llvm::SmallVector cflags = { "-no-canonical-prefixes", "-w", }; if constexpr (Component == Runtimes::LibUnwind) { llvm::append_range(cflags, RuntimesBuildInfo::LibunwindCopts); } else if constexpr (Component == Runtimes::Libcxx) { llvm::append_range(cflags, RuntimesBuildInfo::LibcxxCopts); } else { static_assert(false, "Invalid runtimes component for an archive runtime builder."); } for (const auto& include_path : include_paths_) { cflags.append({"-I", include_path.native()}); } return cflags; } template requires IsClangArchiveRuntimes auto ClangArchiveRuntimesBuilder::Setup() -> void { // Finish building the runtime once the archive is built. Latch::Handle latch_handle = step_counter_.Init( [this]() mutable { tasks_.async([this]() mutable { Finish(); }); }); // Start building the archive itself with a handle to detect when complete. archive_->Setup(std::move(latch_handle)); } template requires IsClangArchiveRuntimes auto ClangArchiveRuntimesBuilder::Finish() -> void { CARBON_VLOG("Finished building {0}...\n", archive_path_); if (!archive_->result().ok()) { result_ = std::move(archive_->result()).error(); return; } result_ = (*std::move(runtimes_builder_)).Commit(); } template class ClangArchiveRuntimesBuilder; template class ClangArchiveRuntimesBuilder; auto ClangResourceDirBuilder::GetDarwinOsSuffix(llvm::Triple target_triple) -> llvm::StringRef { switch (target_triple.getOS()) { case llvm::Triple::IOS: return target_triple.isSimulatorEnvironment() ? "iossim" : "ios"; case llvm::Triple::WatchOS: return target_triple.isSimulatorEnvironment() ? "watchossim" : "watchos"; case llvm::Triple::TvOS: return target_triple.isSimulatorEnvironment() ? "tvossim" : "tvos"; case llvm::Triple::XROS: return target_triple.isSimulatorEnvironment() ? "xrossim" : "xros"; default: return "osx"; } } ClangResourceDirBuilder::ClangResourceDirBuilder( ClangRunner* clang, llvm::ThreadPoolInterface* threads, llvm::Triple target_triple, Runtimes* runtimes) : ClangRuntimesBuilderBase(clang, threads, std::move(target_triple)), crt_begin_result_(Error("Never built CRT begin file!")), crt_end_result_(Error("Never built CRT end file!")) { // Ensure we're on a platform where we _can_ build a working runtime. if (target_triple_.isOSWindows()) { result_ = Error("TODO: Windows runtimes are untested and not yet supported."); return; } auto build_dir_or_error = runtimes->Build(Runtimes::ClangResourceDir); if (!build_dir_or_error.ok()) { result_ = std::move(build_dir_or_error).error(); return; } auto build_dir = *std::move(build_dir_or_error); if (std::holds_alternative(build_dir)) { // Found cached build. result_ = std::get(std::move(build_dir)); return; } runtimes_builder_ = std::get(std::move(build_dir)); lib_path_ = std::filesystem::path("lib"); std::filesystem::path builtins_name = "libclang_rt.builtins.a"; if (target_triple.isOSDarwin()) { // Darwin targets don't use the full triple, and don't include the // architecture in the resource directory naming structure. // // TODO: We should add support for embedded Darwin as well which uses a // different layout. lib_path_ /= "darwin"; // Darwin targets also use a custom naming convention for the builtins // archive. builtins_name = llvm::formatv("libclang_rt.{0}.a", GetDarwinOsSuffix(target_triple_)) .str(); } else { lib_path_ /= target_triple_.str(); } // TODO: Currently, we only need a single include path to see headers inside // the `builtins` directory. However, we're anticipating needing more, for // example to support SipHash. If that need doesn't materialize, we should // simplify this to a single path instead of a vector. include_paths_.push_back(installation().runtimes_root() / "builtins"); llvm::SmallVector copts = { "-no-canonical-prefixes", "-w", }; llvm::append_range(copts, RuntimesBuildInfo::BuiltinsCopts); for (const auto& include_path : include_paths_) { copts.append({"-I", include_path.native()}); } archive_.emplace(this, lib_path_ / builtins_name, installation().root(), CollectBuiltinsSrcFiles(), copts); tasks_.async([this]() { Setup(); }); } auto ClangResourceDirBuilder::CollectBuiltinsSrcFiles() -> llvm::SmallVector { llvm::SmallVector src_files; if (target_triple_.isAArch64()) { llvm::append_range(src_files, RuntimesBuildInfo::BuiltinsAarch64Srcs); } else if (target_triple_.isX86()) { if (target_triple_.isArch64Bit()) { llvm::append_range(src_files, RuntimesBuildInfo::BuiltinsX86_64Srcs); } 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."); llvm::append_range(src_files, RuntimesBuildInfo::BuiltinsI386Srcs); } } 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()); } // Only compile source files, not headers. llvm::erase_if(src_files, [](llvm::StringRef file) { return file.ends_with(".h"); }); return src_files; } auto ClangResourceDirBuilder::Setup() -> void { // Symlink the installation's `include` and `share` directories. std::filesystem::path install_resource_path = installation().clang_resource_path(); if (auto result = runtimes_builder_->dir().Symlink( "include", install_resource_path / "include"); !result.ok()) { result_ = std::move(result).error(); return; } // Create the target's `lib` directory. auto lib_dir_result = runtimes_builder_->dir().CreateDirectories(lib_path_); if (!lib_dir_result.ok()) { result_ = std::move(lib_dir_result).error(); return; } lib_dir_ = *std::move(lib_dir_result); Latch::Handle latch_handle = step_counter_.Init([this] { tasks_.async([this] { Finish(); }); }); // 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()) { tasks_.async([this, latch_handle] { crt_begin_result_ = BuildCrtFile(RuntimesBuildInfo::CrtBegin); }); tasks_.async([this, latch_handle] { crt_end_result_ = BuildCrtFile(RuntimesBuildInfo::CrtEnd); }); } archive_->Setup(std::move(latch_handle)); } auto ClangResourceDirBuilder::Finish() -> void { CARBON_VLOG("Finished building resource dir...\n"); if (!archive_->result().ok()) { result_ = std::move(archive_->result()).error(); return; } if (target_triple_.isOSLinux()) { for (ErrorOr* result : {&crt_begin_result_, &crt_end_result_}) { if (!result->ok()) { result_ = std::move(*result).error(); return; } } } result_ = (*std::move(runtimes_builder_)).Commit(); } auto ClangResourceDirBuilder::BuildCrtFile(llvm::StringRef src_file) -> ErrorOr { CARBON_CHECK(src_file == RuntimesBuildInfo::CrtBegin || src_file == RuntimesBuildInfo::CrtEnd); std::filesystem::path out_path = runtimes_builder_->path() / lib_path_ / (src_file == RuntimesBuildInfo::CrtBegin ? "clang_rt.crtbegin.o" : "clang_rt.crtend.o"); std::filesystem::path src_path = installation().root() / std::string_view(src_file); CARBON_VLOG("Building `{0}' from `{1}`...\n", out_path, src_path); llvm::SmallVector copts = { "-no-canonical-prefixes", "-w", target_flag_, }; llvm::append_range(copts, RuntimesBuildInfo::CrtCopts); copts.append({ "-c", "-o", out_path.native(), src_path.native(), }); CARBON_ASSIGN_OR_RETURN(bool success, clang_->RunWithNoRuntimes(copts)); if (success) { return Success(); } return Error(llvm::formatv("Failed to compile CRT file: {0}", src_file)); } } // namespace Carbon