clang_runner.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. #include "toolchain/driver/clang_runner.h"
  5. #include <unistd.h>
  6. #include <algorithm>
  7. #include <filesystem>
  8. #include <memory>
  9. #include <numeric>
  10. #include <optional>
  11. #include <string>
  12. #include <system_error>
  13. #include <utility>
  14. #include <variant>
  15. #include "clang/Basic/Diagnostic.h"
  16. #include "clang/Basic/DiagnosticOptions.h"
  17. #include "clang/Driver/Compilation.h"
  18. #include "clang/Driver/Driver.h"
  19. #include "clang/Frontend/CompilerInvocation.h"
  20. #include "clang/Frontend/TextDiagnosticPrinter.h"
  21. #include "common/filesystem.h"
  22. #include "common/vlog.h"
  23. #include "llvm/ADT/ArrayRef.h"
  24. #include "llvm/ADT/ScopeExit.h"
  25. #include "llvm/ADT/StringExtras.h"
  26. #include "llvm/ADT/StringRef.h"
  27. #include "llvm/IR/LLVMContext.h"
  28. #include "llvm/Object/ArchiveWriter.h"
  29. #include "llvm/Support/Error.h"
  30. #include "llvm/Support/FileSystem.h"
  31. #include "llvm/Support/FormatAdapters.h"
  32. #include "llvm/Support/LLVMDriver.h"
  33. #include "llvm/Support/Path.h"
  34. #include "llvm/Support/Program.h"
  35. #include "llvm/TargetParser/Host.h"
  36. #include "toolchain/base/runtime_sources.h"
  37. // Defined in:
  38. // https://github.com/llvm/llvm-project/blob/main/clang/tools/driver/driver.cpp
  39. //
  40. // While not in a header, this is the API used by llvm-driver.cpp for
  41. // busyboxing.
  42. //
  43. // NOLINTNEXTLINE(readability-identifier-naming)
  44. auto clang_main(int Argc, char** Argv, const llvm::ToolContext& ToolContext)
  45. -> int;
  46. namespace Carbon {
  47. ClangRunner::ClangRunner(const InstallPaths* install_paths,
  48. Runtimes::Cache* runtimes_cache,
  49. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
  50. llvm::raw_ostream* vlog_stream,
  51. bool build_runtimes_on_demand)
  52. : ToolRunnerBase(install_paths, vlog_stream),
  53. runtimes_cache_(runtimes_cache),
  54. fs_(std::move(fs)),
  55. diagnostic_ids_(new clang::DiagnosticIDs()),
  56. build_runtimes_on_demand_(build_runtimes_on_demand) {}
  57. // Searches an argument list to a Clang execution to determine the expected
  58. // target string, suitable for use with `llvm::Triple`.
  59. //
  60. // If no explicit target flags are present, this defaults to the default
  61. // LLVM target.
  62. //
  63. // Works to handle the most common flags that modify the expected target as
  64. // well as direct target flags.
  65. //
  66. // Note: this has known fidelity issues if the args include separate-value flags
  67. // (`--flag value` style as opposed to `--flag=value`) where the value might
  68. // match the spelling of one of the target flags. For example, args that include
  69. // an output file spelled `-m32` (so `-o` followed by `-m32`) will be
  70. // misinterpreted by considering the value to itself be a flag. Addressing this
  71. // would add substantial complexity, including likely parsing the entire args
  72. // twice with the Clang driver. Instead, our current plan is to document this
  73. // limitation and encourage the use of flags with joined values
  74. // (`--flag=value`).
  75. static auto ComputeClangTarget(llvm::ArrayRef<llvm::StringRef> args)
  76. -> std::string {
  77. std::string target = llvm::sys::getDefaultTargetTriple();
  78. bool explicit_target = false;
  79. for (auto [i, arg] : llvm::enumerate(args)) {
  80. if (llvm::StringRef arg_copy = arg; arg_copy.consume_front("--target=")) {
  81. target = arg_copy.str();
  82. explicit_target = true;
  83. } else if ((arg == "--target" || arg == "-target") &&
  84. (i + 1) < args.size()) {
  85. target = args[i + 1].str();
  86. explicit_target = true;
  87. } else if (!explicit_target &&
  88. (arg == "--driver-mode=cl" ||
  89. ((arg == "--driver-mode" || arg == "-driver-mode") &&
  90. (i + 1) < args.size() && args[i + 1] == "cl"))) {
  91. // The `cl.exe` compatible driver mode should switch the default target to
  92. // a `...-pc-windows-msvc` target. However, a subsequent explicit target
  93. // should override this.
  94. llvm::Triple triple(target);
  95. triple.setVendor(llvm::Triple::PC);
  96. triple.setOS(llvm::Triple::Win32);
  97. triple.setEnvironment(llvm::Triple::MSVC);
  98. target = triple.str();
  99. } else if (arg == "-m32") {
  100. llvm::Triple triple(target);
  101. if (!triple.isArch32Bit()) {
  102. target = triple.get32BitArchVariant().str();
  103. }
  104. } else if (arg == "-m64") {
  105. llvm::Triple triple(target);
  106. if (!triple.isArch64Bit()) {
  107. target = triple.get64BitArchVariant().str();
  108. }
  109. }
  110. }
  111. return target;
  112. }
  113. // Tries to detect a a non-linking list of Clang arguments to avoid setting up
  114. // the more complete resource directory needed for linking. False negatives are
  115. // fine here, and we use that to keep things simple.
  116. static auto IsNonLinkCommand(llvm::ArrayRef<llvm::StringRef> args) -> bool {
  117. return llvm::any_of(args, [](llvm::StringRef arg) {
  118. // Only check the most common cases as we have to do this for each argument.
  119. // Everything else is rare and likely not worth the cost of searching for
  120. // since it's fine to have false negatives.
  121. return arg == "-c" || arg == "-E" || arg == "-S" ||
  122. arg == "-fsyntax-only" || arg == "--version" || arg == "--help" ||
  123. arg == "/?" || arg == "--driver-mode=cpp";
  124. });
  125. }
  126. auto ClangRunner::Run(llvm::ArrayRef<llvm::StringRef> args,
  127. Runtimes* prebuilt_runtimes) -> ErrorOr<bool> {
  128. // Check the args to see if we have a known target-independent command. If so,
  129. // directly dispatch it to avoid the cost of building the target resource
  130. // directory.
  131. // TODO: Maybe handle response file expansion similar to the Clang CLI?
  132. if (args.empty() || args[0].starts_with("-cc1") || IsNonLinkCommand(args) ||
  133. (!build_runtimes_on_demand_ && !prebuilt_runtimes)) {
  134. return RunTargetIndependentCommand(args);
  135. }
  136. std::string target = ComputeClangTarget(args);
  137. // If we have pre-built runtimes, use them rather than building on demand.
  138. if (prebuilt_runtimes) {
  139. CARBON_ASSIGN_OR_RETURN(std::filesystem::path prebuilt_resource_dir_path,
  140. prebuilt_runtimes->Get(Runtimes::ClangResourceDir));
  141. return RunInternal(args, target, prebuilt_resource_dir_path.native());
  142. }
  143. CARBON_CHECK(build_runtimes_on_demand_);
  144. // Otherwise, we need to build a target resource directory.
  145. CARBON_VLOG("Building target resource dir...\n");
  146. Runtimes::Cache::Features features = {.target = target};
  147. CARBON_ASSIGN_OR_RETURN(Runtimes runtimes, runtimes_cache_->Lookup(features));
  148. // We need to build the Clang resource directory for these runtimes. This
  149. // requires a temporary directory as well as the destination directory for
  150. // the build. The temporary directory should only be used during the build,
  151. // not once we are running Clang with the built runtime.
  152. std::filesystem::path resource_dir_path;
  153. {
  154. CARBON_ASSIGN_OR_RETURN(Filesystem::RemovingDir tmp_dir,
  155. Filesystem::MakeTmpDir());
  156. CARBON_ASSIGN_OR_RETURN(
  157. resource_dir_path,
  158. BuildTargetResourceDir(features, runtimes, tmp_dir.abs_path()));
  159. }
  160. // Note that this function always successfully runs `clang` and returns a bool
  161. // to indicate whether `clang` itself succeeded, not whether the runner was
  162. // able to run it. As a consequence, even a `false` here is a non-`Error`
  163. // return.
  164. return RunInternal(args, target, resource_dir_path.native());
  165. }
  166. auto ClangRunner::RunTargetIndependentCommand(
  167. llvm::ArrayRef<llvm::StringRef> args) -> bool {
  168. std::string target = ComputeClangTarget(args);
  169. return RunInternal(args, target, std::nullopt);
  170. }
  171. auto ClangRunner::BuildTargetResourceDir(
  172. const Runtimes::Cache::Features& features, Runtimes& runtimes,
  173. const std::filesystem::path& tmp_path) -> ErrorOr<std::filesystem::path> {
  174. // Disable any leaking of memory while building the target resource dir, and
  175. // restore the previous setting at the end.
  176. auto restore_leak_flag = llvm::make_scope_exit(
  177. [&, orig_flag = enable_leaking_] { enable_leaking_ = orig_flag; });
  178. enable_leaking_ = false;
  179. CARBON_ASSIGN_OR_RETURN(auto build_dir,
  180. runtimes.Build(Runtimes::ClangResourceDir));
  181. if (std::holds_alternative<std::filesystem::path>(build_dir)) {
  182. // Found cached build.
  183. return std::get<std::filesystem::path>(std::move(build_dir));
  184. }
  185. auto builder = std::get<Runtimes::Builder>(std::move(build_dir));
  186. std::string target = features.target;
  187. // Symlink the installation's `include` and `share` directories.
  188. std::filesystem::path install_resource_path =
  189. installation_->clang_resource_path();
  190. CARBON_RETURN_IF_ERROR(
  191. builder.dir().Symlink("include", install_resource_path / "include"));
  192. CARBON_RETURN_IF_ERROR(
  193. builder.dir().Symlink("share", install_resource_path / "share"));
  194. // Create the target's `lib` directory.
  195. std::filesystem::path lib_path =
  196. std::filesystem::path("lib") / std::string_view(target);
  197. CARBON_ASSIGN_OR_RETURN(Filesystem::Dir lib_dir,
  198. builder.dir().CreateDirectories(lib_path));
  199. llvm::Triple target_triple(target);
  200. if (target_triple.isOSWindows()) {
  201. return Error("TODO: Windows runtimes are untested and not yet supported.");
  202. }
  203. // For Linux targets, the system libc (typically glibc) doesn't necessarily
  204. // provide the CRT begin/end files, and so we need to build them.
  205. if (target_triple.isOSLinux()) {
  206. BuildCrtFile(target, RuntimeSources::CrtBegin,
  207. builder.path() / lib_path / "clang_rt.crtbegin.o");
  208. BuildCrtFile(target, RuntimeSources::CrtEnd,
  209. builder.path() / lib_path / "clang_rt.crtend.o");
  210. }
  211. CARBON_RETURN_IF_ERROR(
  212. BuildBuiltinsLib(target, target_triple, tmp_path, lib_dir));
  213. return std::move(builder).Commit();
  214. }
  215. auto ClangRunner::RunInternal(
  216. llvm::ArrayRef<llvm::StringRef> args, llvm::StringRef target,
  217. std::optional<llvm::StringRef> target_resource_dir_path) -> bool {
  218. std::string clang_path = installation_->clang_path();
  219. // Rebuild the args as C-string args.
  220. llvm::OwningArrayRef<char> cstr_arg_storage;
  221. llvm::SmallVector<const char*, 64> cstr_args =
  222. BuildCStrArgs("Clang", clang_path, "-v", args, cstr_arg_storage);
  223. // Handle special dispatch for CC1 commands as they don't use the driver.
  224. if (!args.empty() && args[0].starts_with("-cc1")) {
  225. CARBON_VLOG("Calling clang_main for cc1...");
  226. // cstr_args[0] will be the `clang_path` so we don't need the prepend arg.
  227. llvm::ToolContext tool_context = {
  228. .Path = cstr_args[0], .PrependArg = "clang", .NeedsPrependArg = false};
  229. int exit_code = clang_main(
  230. cstr_args.size(), const_cast<char**>(cstr_args.data()), tool_context);
  231. // TODO: Should this be forwarding the full exit code?
  232. return exit_code == 0;
  233. }
  234. CARBON_VLOG("Preparing Clang driver...\n");
  235. // Create the diagnostic options and parse arguments controlling them out of
  236. // our arguments.
  237. std::unique_ptr<clang::DiagnosticOptions> diagnostic_options =
  238. clang::CreateAndPopulateDiagOpts(cstr_args);
  239. // TODO: We don't yet support serializing diagnostics the way the actual
  240. // `clang` command line does. Unclear if we need to or not, but it would need
  241. // a bit more logic here to set up chained consumers.
  242. clang::TextDiagnosticPrinter diagnostic_client(llvm::errs(),
  243. *diagnostic_options);
  244. clang::DiagnosticsEngine diagnostics(diagnostic_ids_, *diagnostic_options,
  245. &diagnostic_client,
  246. /*ShouldOwnClient=*/false);
  247. clang::ProcessWarningOptions(diagnostics, *diagnostic_options, *fs_);
  248. // Note that we configure the driver's *default* target here, not the expected
  249. // target as that will be parsed out of the command line below.
  250. clang::driver::Driver driver(clang_path, llvm::sys::getDefaultTargetTriple(),
  251. diagnostics, "clang LLVM compiler", fs_);
  252. llvm::Triple target_triple(target);
  253. // We need to set an SDK system root on macOS by default. Setting it here
  254. // allows a custom sysroot to still be specified on the command line.
  255. //
  256. // TODO: A different system root should be used for iOS, watchOS, tvOS.
  257. // Currently, we're only targeting macOS support though.
  258. if (target_triple.isMacOSX()) {
  259. // This is the default CLT system root, shown by `xcrun --show-sdk-path`.
  260. // We hard code it here to avoid the overhead of subprocessing to `xcrun` on
  261. // each Clang invocation, but this may need to be updated to search or
  262. // reflect macOS versions if this changes in the future.
  263. driver.SysRoot = "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk";
  264. }
  265. // If we have a target-specific resource directory, set it as the default
  266. // here.
  267. if (target_resource_dir_path) {
  268. driver.ResourceDir = target_resource_dir_path->str();
  269. }
  270. // Configure the install directory to find other tools and data files.
  271. //
  272. // We directly override the detected directory as we use a synthetic path
  273. // above. This makes it appear that our binary was in the installed binaries
  274. // directory, and allows finding tools relative to it.
  275. driver.Dir = installation_->llvm_install_bin();
  276. CARBON_VLOG("Setting bin directory to: {0}\n", driver.Dir);
  277. // When there's only one command being run, this will run it in-process.
  278. // However, a `clang` invocation may cause multiple `cc1` invocations, which
  279. // still subprocess. See `InProcess` comment at:
  280. // https://github.com/llvm/llvm-project/blob/86ce8e4504c06ecc3cc42f002ad4eb05cac10925/clang/lib/Driver/Job.cpp#L411-L413
  281. //
  282. // Note the subprocessing will effectively call `clang -cc1`, which turns into
  283. // `carbon-busybox clang -cc1`, which results in an equivalent `clang_main`
  284. // call.
  285. //
  286. // Also note that we only do `-disable-free` filtering in the in-process
  287. // execution here, as subprocesses leaking memory won't impact this process.
  288. auto cc1_main = [enable_leaking = enable_leaking_](
  289. llvm::SmallVectorImpl<const char*>& cc1_args) -> int {
  290. if (!enable_leaking) {
  291. // Last-flag wins, so this forcibly re-enables freeing memory.
  292. cc1_args.push_back("-no-disable-free");
  293. }
  294. // cc1_args[0] will be the `clang_path` so we don't need the prepend arg.
  295. llvm::ToolContext tool_context = {
  296. .Path = cc1_args[0], .PrependArg = "clang", .NeedsPrependArg = false};
  297. return clang_main(cc1_args.size(), const_cast<char**>(cc1_args.data()),
  298. tool_context);
  299. };
  300. driver.CC1Main = cc1_main;
  301. std::unique_ptr<clang::driver::Compilation> compilation(
  302. driver.BuildCompilation(cstr_args));
  303. CARBON_CHECK(compilation, "Should always successfully allocate!");
  304. if (compilation->containsError()) {
  305. // These should have been diagnosed by the driver.
  306. return false;
  307. }
  308. // Make sure our target detection matches Clang's. Sadly, we can't just reuse
  309. // Clang's as it is available too late.
  310. // TODO: Use nice diagnostics here rather than a check failure.
  311. CARBON_CHECK(llvm::Triple(target) == llvm::Triple(driver.getTargetTriple()),
  312. "Mismatch between the expected target '{0}' and the one "
  313. "computed by Clang '{1}'",
  314. target, driver.getTargetTriple());
  315. CARBON_VLOG("Running Clang driver...\n");
  316. llvm::SmallVector<std::pair<int, const clang::driver::Command*>>
  317. failing_commands;
  318. int result = driver.ExecuteCompilation(*compilation, failing_commands);
  319. // Finish diagnosing any failures before we verbosely log the source of those
  320. // failures.
  321. diagnostic_client.finish();
  322. CARBON_VLOG("Execution result code: {0}\n", result);
  323. for (const auto& [command_result, failing_command] : failing_commands) {
  324. CARBON_VLOG("Failing command '{0}' with code '{1}' was:\n",
  325. failing_command->getExecutable(), command_result);
  326. if (vlog_stream_) {
  327. failing_command->Print(*vlog_stream_, "\n\n", /*Quote=*/true);
  328. }
  329. }
  330. // Return whether the command was executed successfully.
  331. return result == 0 && failing_commands.empty();
  332. }
  333. auto ClangRunner::BuildCrtFile(llvm::StringRef target, llvm::StringRef src_file,
  334. const std::filesystem::path& out_path) -> void {
  335. std::filesystem::path src_path =
  336. installation_->llvm_runtime_srcs() / std::string_view(src_file);
  337. CARBON_VLOG("Building `{0}' from `{1}`...\n", out_path, src_path);
  338. std::string target_arg = llvm::formatv("--target={0}", target).str();
  339. CARBON_CHECK(RunTargetIndependentCommand({
  340. "-no-canonical-prefixes",
  341. target_arg,
  342. "-DCRT_HAS_INITFINI_ARRAY",
  343. "-DEH_USE_FRAME_REGISTRY",
  344. "-O3",
  345. "-fPIC",
  346. "-ffreestanding",
  347. "-std=c11",
  348. "-w",
  349. "-c",
  350. "-o",
  351. out_path.native(),
  352. src_path.native(),
  353. }));
  354. }
  355. auto ClangRunner::CollectBuiltinsSrcFiles(const llvm::Triple& target_triple)
  356. -> llvm::SmallVector<llvm::StringRef> {
  357. llvm::SmallVector<llvm::StringRef> src_files;
  358. auto append_src_files =
  359. [&](auto input_srcs,
  360. llvm::function_ref<bool(llvm::StringRef)> filter_out = {}) {
  361. for (llvm::StringRef input_src : input_srcs) {
  362. if (!input_src.ends_with(".c") && !input_src.ends_with(".S")) {
  363. // Not a compiled file.
  364. continue;
  365. }
  366. if (filter_out && filter_out(input_src)) {
  367. // Filtered out.
  368. continue;
  369. }
  370. src_files.push_back(input_src);
  371. }
  372. };
  373. append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsGenericSrcs));
  374. append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsBf16Srcs));
  375. if (target_triple.isArch64Bit()) {
  376. append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsTfSrcs));
  377. }
  378. auto filter_out_chkstk = [&](llvm::StringRef src) {
  379. return !target_triple.isOSWindows() || !src.ends_with("chkstk.S");
  380. };
  381. if (target_triple.isAArch64()) {
  382. append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsAarch64Srcs),
  383. filter_out_chkstk);
  384. } else if (target_triple.isX86()) {
  385. append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsX86ArchSrcs));
  386. if (target_triple.isArch64Bit()) {
  387. append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsX86_64Srcs),
  388. filter_out_chkstk);
  389. } else {
  390. // TODO: This should be turned into a nice user-facing diagnostic about an
  391. // unsupported target.
  392. CARBON_CHECK(
  393. target_triple.isArch32Bit(),
  394. "The Carbon toolchain doesn't currently support 16-bit x86.");
  395. append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsI386Srcs),
  396. filter_out_chkstk);
  397. }
  398. } else {
  399. // TODO: This should be turned into a nice user-facing diagnostic about an
  400. // unsupported target.
  401. CARBON_FATAL("Target architecture is not supported: {0}",
  402. target_triple.str());
  403. }
  404. return src_files;
  405. }
  406. auto ClangRunner::BuildBuiltinsFile(llvm::StringRef target,
  407. llvm::StringRef src_file,
  408. const std::filesystem::path& out_path)
  409. -> void {
  410. std::filesystem::path src_path =
  411. installation_->llvm_runtime_srcs() / std::string_view(src_file);
  412. CARBON_VLOG("Building `{0}' from `{1}`...\n", out_path, src_path);
  413. std::string target_arg = llvm::formatv("--target={0}", target).str();
  414. CARBON_CHECK(RunTargetIndependentCommand({
  415. "-no-canonical-prefixes",
  416. target_arg,
  417. "-O3",
  418. "-fPIC",
  419. "-ffreestanding",
  420. "-fno-builtin",
  421. "-fomit-frame-pointer",
  422. "-fvisibility=hidden",
  423. "-std=c11",
  424. "-w",
  425. "-c",
  426. "-o",
  427. out_path.native(),
  428. src_path.native(),
  429. }));
  430. }
  431. auto ClangRunner::BuildBuiltinsLib(llvm::StringRef target,
  432. const llvm::Triple& target_triple,
  433. const std::filesystem::path& tmp_path,
  434. Filesystem::DirRef lib_dir)
  435. -> ErrorOr<Success> {
  436. llvm::SmallVector<llvm::StringRef> src_files =
  437. CollectBuiltinsSrcFiles(target_triple);
  438. CARBON_ASSIGN_OR_RETURN(Filesystem::Dir tmp_dir,
  439. Filesystem::Cwd().OpenDir(tmp_path));
  440. llvm::SmallVector<llvm::NewArchiveMember> objs;
  441. objs.reserve(src_files.size());
  442. for (llvm::StringRef src_file : src_files) {
  443. // Create any subdirectories needed for this file.
  444. std::filesystem::path src_path = src_file.str();
  445. if (src_path.has_parent_path()) {
  446. CARBON_RETURN_IF_ERROR(tmp_dir.CreateDirectories(src_path.parent_path()));
  447. }
  448. std::filesystem::path obj_path = tmp_path / std::string_view(src_file);
  449. obj_path += ".o";
  450. BuildBuiltinsFile(target, src_file, obj_path);
  451. llvm::Expected<llvm::NewArchiveMember> obj =
  452. llvm::NewArchiveMember::getFile(obj_path.native(),
  453. /*Deterministic=*/true);
  454. CARBON_CHECK(obj, "TODO: Diagnose this: {0}",
  455. llvm::fmt_consume(obj.takeError()));
  456. objs.push_back(std::move(*obj));
  457. }
  458. // Now build an archive out of the `.o` files for the builtins. Note that we
  459. // build this directly into the `lib_dir` as this is expected to be a staging
  460. // directory and cleaned up on errors.
  461. std::filesystem::path builtins_a_path = "libclang_rt.builtins.a";
  462. CARBON_ASSIGN_OR_RETURN(
  463. Filesystem::WriteFile builtins_a_file,
  464. lib_dir.OpenWriteOnly(builtins_a_path, Filesystem::CreateAlways));
  465. {
  466. llvm::raw_fd_ostream builtins_a_os = builtins_a_file.WriteStream();
  467. llvm::Error archive_err = llvm::writeArchiveToStream(
  468. builtins_a_os, objs, llvm::SymtabWritingMode::NormalSymtab,
  469. target_triple.isOSDarwin() ? llvm::object::Archive::K_DARWIN
  470. : llvm::object::Archive::K_GNU,
  471. /*Deterministic=*/true, /*Thin=*/false);
  472. // The presence of an error is `true`.
  473. if (archive_err) {
  474. return Error(llvm::toString(std::move(archive_err)));
  475. }
  476. }
  477. CARBON_RETURN_IF_ERROR(std::move(builtins_a_file).Close());
  478. return Success();
  479. }
  480. } // namespace Carbon