clang_runner.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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/CodeGen/ObjectFilePCHContainerWriter.h"
  18. #include "clang/Driver/Compilation.h"
  19. #include "clang/Driver/Driver.h"
  20. #include "clang/Frontend/CompilerInstance.h"
  21. #include "clang/Frontend/CompilerInvocation.h"
  22. #include "clang/Frontend/TextDiagnosticBuffer.h"
  23. #include "clang/Frontend/TextDiagnosticPrinter.h"
  24. #include "clang/Frontend/Utils.h"
  25. #include "clang/FrontendTool/Utils.h"
  26. #include "clang/Serialization/ObjectFilePCHContainerReader.h"
  27. #include "common/filesystem.h"
  28. #include "common/vlog.h"
  29. #include "llvm/ADT/ArrayRef.h"
  30. #include "llvm/ADT/ScopeExit.h"
  31. #include "llvm/ADT/Statistic.h"
  32. #include "llvm/ADT/StringExtras.h"
  33. #include "llvm/ADT/StringRef.h"
  34. #include "llvm/IR/LLVMContext.h"
  35. #include "llvm/Object/ArchiveWriter.h"
  36. #include "llvm/Support/Error.h"
  37. #include "llvm/Support/FileSystem.h"
  38. #include "llvm/Support/FormatAdapters.h"
  39. #include "llvm/Support/LLVMDriver.h"
  40. #include "llvm/Support/Path.h"
  41. #include "llvm/Support/Program.h"
  42. #include "llvm/Support/TimeProfiler.h"
  43. #include "llvm/Support/Timer.h"
  44. #include "llvm/TargetParser/Host.h"
  45. #include "toolchain/base/runtime_sources.h"
  46. namespace Carbon {
  47. ClangRunner::ClangRunner(const InstallPaths* install_paths,
  48. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
  49. llvm::raw_ostream* vlog_stream)
  50. : ToolRunnerBase(install_paths, vlog_stream), fs_(std::move(fs)) {}
  51. // Searches an argument list to a Clang execution to determine the expected
  52. // target string, suitable for use with `llvm::Triple`.
  53. //
  54. // If no explicit target flags are present, this defaults to the default
  55. // LLVM target.
  56. //
  57. // Works to handle the most common flags that modify the expected target as
  58. // well as direct target flags.
  59. //
  60. // Note: this has known fidelity issues if the args include separate-value flags
  61. // (`--flag value` style as opposed to `--flag=value`) where the value might
  62. // match the spelling of one of the target flags. For example, args that include
  63. // an output file spelled `-m32` (so `-o` followed by `-m32`) will be
  64. // misinterpreted by considering the value to itself be a flag. Addressing this
  65. // would add substantial complexity, including likely parsing the entire args
  66. // twice with the Clang driver. Instead, our current plan is to document this
  67. // limitation and encourage the use of flags with joined values
  68. // (`--flag=value`).
  69. static auto ComputeClangTarget(llvm::ArrayRef<llvm::StringRef> args)
  70. -> std::string {
  71. std::string target = llvm::sys::getDefaultTargetTriple();
  72. bool explicit_target = false;
  73. for (auto [i, arg] : llvm::enumerate(args)) {
  74. if (llvm::StringRef arg_copy = arg; arg_copy.consume_front("--target=")) {
  75. target = arg_copy.str();
  76. explicit_target = true;
  77. } else if ((arg == "--target" || arg == "-target") &&
  78. (i + 1) < args.size()) {
  79. target = args[i + 1].str();
  80. explicit_target = true;
  81. } else if (!explicit_target &&
  82. (arg == "--driver-mode=cl" ||
  83. ((arg == "--driver-mode" || arg == "-driver-mode") &&
  84. (i + 1) < args.size() && args[i + 1] == "cl"))) {
  85. // The `cl.exe` compatible driver mode should switch the default target to
  86. // a `...-pc-windows-msvc` target. However, a subsequent explicit target
  87. // should override this.
  88. llvm::Triple triple(target);
  89. triple.setVendor(llvm::Triple::PC);
  90. triple.setOS(llvm::Triple::Win32);
  91. triple.setEnvironment(llvm::Triple::MSVC);
  92. target = triple.str();
  93. } else if (arg == "-m32") {
  94. llvm::Triple triple(target);
  95. if (!triple.isArch32Bit()) {
  96. target = triple.get32BitArchVariant().str();
  97. }
  98. } else if (arg == "-m64") {
  99. llvm::Triple triple(target);
  100. if (!triple.isArch64Bit()) {
  101. target = triple.get64BitArchVariant().str();
  102. }
  103. }
  104. }
  105. return target;
  106. }
  107. // Tries to detect a a non-linking list of Clang arguments to avoid setting up
  108. // the more complete resource directory needed for linking. False negatives are
  109. // fine here, and we use that to keep things simple.
  110. static auto IsNonLinkCommand(llvm::ArrayRef<llvm::StringRef> args) -> bool {
  111. return llvm::any_of(args, [](llvm::StringRef arg) {
  112. // Only check the most common cases as we have to do this for each argument.
  113. // Everything else is rare and likely not worth the cost of searching for
  114. // since it's fine to have false negatives.
  115. return arg == "-c" || arg == "-E" || arg == "-S" ||
  116. arg == "-fsyntax-only" || arg == "--version" || arg == "--help" ||
  117. arg == "/?" || arg == "--driver-mode=cpp";
  118. });
  119. }
  120. auto ClangRunner::RunWithPrebuiltRuntimes(llvm::ArrayRef<llvm::StringRef> args,
  121. Runtimes& prebuilt_runtimes)
  122. -> ErrorOr<bool> {
  123. // Check the args to see if we have a known target-independent command. If so,
  124. // directly dispatch it to avoid the cost of building the target resource
  125. // directory.
  126. // TODO: Maybe handle response file expansion similar to the Clang CLI?
  127. if (args.empty() || args[0].starts_with("-cc1") || IsNonLinkCommand(args)) {
  128. return RunWithNoRuntimes(args);
  129. }
  130. std::string target = ComputeClangTarget(args);
  131. CARBON_ASSIGN_OR_RETURN(std::filesystem::path prebuilt_resource_dir_path,
  132. prebuilt_runtimes.Get(Runtimes::ClangResourceDir));
  133. return RunInternal(args, target, prebuilt_resource_dir_path.native());
  134. }
  135. auto ClangRunner::Run(llvm::ArrayRef<llvm::StringRef> args,
  136. Runtimes::Cache& runtimes_cache,
  137. llvm::ThreadPoolInterface& runtimes_build_thread_pool)
  138. -> ErrorOr<bool> {
  139. // Check the args to see if we have a known target-independent command. If so,
  140. // directly dispatch it to avoid the cost of building the target resource
  141. // directory.
  142. // TODO: Maybe handle response file expansion similar to the Clang CLI?
  143. if (args.empty() || args[0].starts_with("-cc1") || IsNonLinkCommand(args)) {
  144. return RunWithNoRuntimes(args);
  145. }
  146. std::string target = ComputeClangTarget(args);
  147. CARBON_VLOG("Building target resource dir...\n");
  148. Runtimes::Cache::Features features = {.target = target};
  149. CARBON_ASSIGN_OR_RETURN(Runtimes runtimes, runtimes_cache.Lookup(features));
  150. // We need to build the Clang resource directory for these runtimes. This
  151. // requires a temporary directory as well as the destination directory for
  152. // the build. The temporary directory should only be used during the build,
  153. // not once we are running Clang with the built runtime.
  154. std::filesystem::path resource_dir_path;
  155. {
  156. // Build the temporary directory and threadpool needed.
  157. CARBON_ASSIGN_OR_RETURN(Filesystem::RemovingDir tmp_dir,
  158. Filesystem::MakeTmpDir());
  159. CARBON_ASSIGN_OR_RETURN(
  160. resource_dir_path,
  161. BuildTargetResourceDir(features, runtimes, tmp_dir.abs_path(),
  162. runtimes_build_thread_pool));
  163. }
  164. // Note that this function always successfully runs `clang` and returns a bool
  165. // to indicate whether `clang` itself succeeded, not whether the runner was
  166. // able to run it. As a consequence, even a `false` here is a non-`Error`
  167. // return.
  168. return RunInternal(args, target, resource_dir_path.native());
  169. }
  170. auto ClangRunner::RunWithNoRuntimes(llvm::ArrayRef<llvm::StringRef> args)
  171. -> bool {
  172. std::string target = ComputeClangTarget(args);
  173. return RunInternal(args, target, std::nullopt);
  174. }
  175. auto ClangRunner::BuildTargetResourceDir(
  176. const Runtimes::Cache::Features& features, Runtimes& runtimes,
  177. const std::filesystem::path& tmp_path, llvm::ThreadPoolInterface& threads)
  178. -> ErrorOr<std::filesystem::path> {
  179. // Disable any leaking of memory while building the target resource dir, and
  180. // restore the previous setting at the end.
  181. auto restore_leak_flag = llvm::make_scope_exit(
  182. [&, orig_flag = enable_leaking_] { enable_leaking_ = orig_flag; });
  183. enable_leaking_ = false;
  184. CARBON_ASSIGN_OR_RETURN(auto build_dir,
  185. runtimes.Build(Runtimes::ClangResourceDir));
  186. if (std::holds_alternative<std::filesystem::path>(build_dir)) {
  187. // Found cached build.
  188. return std::get<std::filesystem::path>(std::move(build_dir));
  189. }
  190. auto builder = std::get<Runtimes::Builder>(std::move(build_dir));
  191. std::string target = features.target;
  192. // Symlink the installation's `include` and `share` directories.
  193. std::filesystem::path install_resource_path =
  194. installation_->clang_resource_path();
  195. CARBON_RETURN_IF_ERROR(
  196. builder.dir().Symlink("include", install_resource_path / "include"));
  197. CARBON_RETURN_IF_ERROR(
  198. builder.dir().Symlink("share", install_resource_path / "share"));
  199. // Create the target's `lib` directory.
  200. std::filesystem::path lib_path =
  201. std::filesystem::path("lib") / std::string_view(target);
  202. CARBON_ASSIGN_OR_RETURN(Filesystem::Dir lib_dir,
  203. builder.dir().CreateDirectories(lib_path));
  204. llvm::Triple target_triple(target);
  205. if (target_triple.isOSWindows()) {
  206. return Error("TODO: Windows runtimes are untested and not yet supported.");
  207. }
  208. llvm::ThreadPoolTaskGroup task_group(threads);
  209. // For Linux targets, the system libc (typically glibc) doesn't necessarily
  210. // provide the CRT begin/end files, and so we need to build them.
  211. if (target_triple.isOSLinux()) {
  212. task_group.async(
  213. [this, target,
  214. path = builder.path() / lib_path / "clang_rt.crtbegin.o"] {
  215. BuildCrtFile(target, RuntimeSources::CrtBegin, path);
  216. });
  217. task_group.async(
  218. [this, target, path = builder.path() / lib_path / "clang_rt.crtend.o"] {
  219. BuildCrtFile(target, RuntimeSources::CrtEnd, path);
  220. });
  221. }
  222. CARBON_RETURN_IF_ERROR(
  223. BuildBuiltinsLib(target, target_triple, tmp_path, lib_dir, threads));
  224. // Now wait for all the queued builds to complete before we commit the
  225. // runtimes into the cache.
  226. task_group.wait();
  227. return std::move(builder).Commit();
  228. }
  229. auto ClangRunner::RunCC1(llvm::SmallVectorImpl<const char*>& cc1_args) -> int {
  230. llvm::BumpPtrAllocator allocator;
  231. llvm::cl::ExpansionContext expansion_context(
  232. allocator, llvm::cl::TokenizeGNUCommandLine);
  233. if (llvm::Error error = expansion_context.expandResponseFiles(cc1_args)) {
  234. llvm::errs() << toString(std::move(error)) << '\n';
  235. return 1;
  236. }
  237. CARBON_CHECK(cc1_args[1] == llvm::StringRef("-cc1"));
  238. llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diag_ids =
  239. clang::DiagnosticIDs::create();
  240. // Register the support for object-file-wrapped Clang modules.
  241. auto pch_ops = std::make_shared<clang::PCHContainerOperations>();
  242. pch_ops->registerWriter(
  243. std::make_unique<clang::ObjectFilePCHContainerWriter>());
  244. pch_ops->registerReader(
  245. std::make_unique<clang::ObjectFilePCHContainerReader>());
  246. // Buffer diagnostics from argument parsing so that we can output them using a
  247. // well formed diagnostic object.
  248. clang::DiagnosticOptions diag_opts;
  249. clang::TextDiagnosticBuffer diag_buffer;
  250. clang::DiagnosticsEngine diags(diag_ids, diag_opts, &diag_buffer,
  251. /*ShouldOwnClient=*/false);
  252. // Setup round-trip remarks for the DiagnosticsEngine used in CreateFromArgs.
  253. if (llvm::find(cc1_args, llvm::StringRef("-Rround-trip-cc1-args")) !=
  254. cc1_args.end()) {
  255. diags.setSeverity(clang::diag::remark_cc1_round_trip_generated,
  256. clang::diag::Severity::Remark, {});
  257. }
  258. auto invocation = std::make_shared<clang::CompilerInvocation>();
  259. bool success = clang::CompilerInvocation::CreateFromArgs(
  260. *invocation, llvm::ArrayRef(cc1_args).slice(1), diags, cc1_args[0]);
  261. // Heap allocate the compiler instance so that if we disable freeing we can
  262. // discard the pointer without destroying or deallocating it.
  263. auto clang_instance = std::make_unique<clang::CompilerInstance>(
  264. std::move(invocation), std::move(pch_ops));
  265. // Override the disabling of free when we don't want to leak memory.
  266. if (!enable_leaking_) {
  267. clang_instance->getFrontendOpts().DisableFree = false;
  268. clang_instance->getCodeGenOpts().DisableFree = false;
  269. }
  270. if (!clang_instance->getFrontendOpts().TimeTracePath.empty()) {
  271. llvm::timeTraceProfilerInitialize(
  272. clang_instance->getFrontendOpts().TimeTraceGranularity, cc1_args[0],
  273. clang_instance->getFrontendOpts().TimeTraceVerbose);
  274. }
  275. // TODO: These options should take priority over the actual compilation.
  276. // However, their implementation is currently not accessible from a library.
  277. // We should factor the implementation into a reusable location and then use
  278. // that here.
  279. CARBON_CHECK(!clang_instance->getFrontendOpts().PrintSupportedCPUs &&
  280. !clang_instance->getFrontendOpts().PrintSupportedExtensions &&
  281. !clang_instance->getFrontendOpts().PrintEnabledExtensions);
  282. // Infer the builtin include path if unspecified.
  283. if (clang_instance->getHeaderSearchOpts().UseBuiltinIncludes &&
  284. clang_instance->getHeaderSearchOpts().ResourceDir.empty()) {
  285. clang_instance->getHeaderSearchOpts().ResourceDir =
  286. installation_->clang_resource_path();
  287. }
  288. // Create the actual diagnostics engine.
  289. clang_instance->createDiagnostics(*fs_);
  290. if (!clang_instance->hasDiagnostics()) {
  291. return EXIT_FAILURE;
  292. }
  293. // Now flush the buffered diagnostics into the Clang instance's diagnostic
  294. // engine. If we've already hit an error, we can exit early once that's done.
  295. diag_buffer.FlushDiagnostics(clang_instance->getDiagnostics());
  296. if (!success) {
  297. clang_instance->getDiagnosticClient().finish();
  298. return EXIT_FAILURE;
  299. }
  300. // Execute the frontend actions.
  301. {
  302. llvm::TimeTraceScope time_scope("ExecuteCompiler");
  303. bool time_passes = clang_instance->getCodeGenOpts().TimePasses;
  304. if (time_passes) {
  305. clang_instance->createFrontendTimer();
  306. }
  307. llvm::TimeRegion timer(time_passes ? &clang_instance->getFrontendTimer()
  308. : nullptr);
  309. success = clang::ExecuteCompilerInvocation(clang_instance.get());
  310. }
  311. // If any timers were active but haven't been destroyed yet, print their
  312. // results now. This happens in -disable-free mode.
  313. std::unique_ptr<llvm::raw_ostream> io_file = llvm::CreateInfoOutputFile();
  314. if (clang_instance->getCodeGenOpts().TimePassesJson) {
  315. *io_file << "{\n";
  316. llvm::TimerGroup::printAllJSONValues(*io_file, "");
  317. *io_file << "\n}\n";
  318. } else if (!clang_instance->getCodeGenOpts().TimePassesStatsFile) {
  319. llvm::TimerGroup::printAll(*io_file);
  320. }
  321. llvm::TimerGroup::clearAll();
  322. if (llvm::timeTraceProfilerEnabled()) {
  323. // It is possible that the compiler instance doesn't own a file manager here
  324. // if we're compiling a module unit, since the file manager is owned by the
  325. // AST when we're compiling a module unit. So the file manager may be
  326. // invalid here.
  327. //
  328. // It should be fine to create file manager here since the file system
  329. // options are stored in the compiler invocation and we can recreate the VFS
  330. // from the compiler invocation.
  331. if (!clang_instance->hasFileManager()) {
  332. clang_instance->createFileManager(fs_);
  333. }
  334. if (auto profiler_output = clang_instance->createOutputFile(
  335. clang_instance->getFrontendOpts().TimeTracePath, /*Binary=*/false,
  336. /*RemoveFileOnSignal=*/false,
  337. /*useTemporary=*/false)) {
  338. llvm::timeTraceProfilerWrite(*profiler_output);
  339. profiler_output.reset();
  340. llvm::timeTraceProfilerCleanup();
  341. clang_instance->clearOutputFiles(false);
  342. }
  343. }
  344. // When running with -disable-free, don't do any destruction or shutdown.
  345. if (clang_instance->getFrontendOpts().DisableFree) {
  346. llvm::BuryPointer(std::move(clang_instance));
  347. }
  348. return success ? EXIT_SUCCESS : EXIT_FAILURE;
  349. }
  350. auto ClangRunner::RunInternal(
  351. llvm::ArrayRef<llvm::StringRef> args, llvm::StringRef target,
  352. std::optional<llvm::StringRef> target_resource_dir_path) -> bool {
  353. std::string clang_path = installation_->clang_path();
  354. // Rebuild the args as C-string args.
  355. llvm::OwningArrayRef<char> cstr_arg_storage;
  356. llvm::SmallVector<const char*, 64> cstr_args =
  357. BuildCStrArgs("Clang", clang_path, "-v", args, cstr_arg_storage);
  358. // Handle special dispatch for CC1 commands as they don't use the driver.
  359. if (!args.empty() && args[0].starts_with("-cc1")) {
  360. CARBON_VLOG("Calling Clang's CC1...");
  361. int exit_code = RunCC1(cstr_args);
  362. // TODO: Should this be forwarding the full exit code?
  363. return exit_code == 0;
  364. }
  365. CARBON_VLOG("Preparing Clang driver...\n");
  366. // Create the diagnostic options and parse arguments controlling them out of
  367. // our arguments.
  368. std::unique_ptr<clang::DiagnosticOptions> diagnostic_options =
  369. clang::CreateAndPopulateDiagOpts(cstr_args);
  370. // TODO: We don't yet support serializing diagnostics the way the actual
  371. // `clang` command line does. Unclear if we need to or not, but it would need
  372. // a bit more logic here to set up chained consumers.
  373. clang::TextDiagnosticPrinter diagnostic_client(llvm::errs(),
  374. *diagnostic_options);
  375. // Note that the `DiagnosticsEngine` takes ownership (via a ref count) of the
  376. // DiagnosticIDs, unlike the other parameters.
  377. clang::DiagnosticsEngine diagnostics(clang::DiagnosticIDs::create(),
  378. *diagnostic_options, &diagnostic_client,
  379. /*ShouldOwnClient=*/false);
  380. clang::ProcessWarningOptions(diagnostics, *diagnostic_options, *fs_);
  381. // Note that we configure the driver's *default* target here, not the expected
  382. // target as that will be parsed out of the command line below.
  383. clang::driver::Driver driver(clang_path, llvm::sys::getDefaultTargetTriple(),
  384. diagnostics, "clang LLVM compiler", fs_);
  385. llvm::Triple target_triple(target);
  386. // We need to set an SDK system root on macOS by default. Setting it here
  387. // allows a custom sysroot to still be specified on the command line.
  388. //
  389. // TODO: A different system root should be used for iOS, watchOS, tvOS.
  390. // Currently, we're only targeting macOS support though.
  391. if (target_triple.isMacOSX()) {
  392. // This is the default CLT system root, shown by `xcrun --show-sdk-path`.
  393. // We hard code it here to avoid the overhead of subprocessing to `xcrun` on
  394. // each Clang invocation, but this may need to be updated to search or
  395. // reflect macOS versions if this changes in the future.
  396. driver.SysRoot = "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk";
  397. }
  398. // If we have a target-specific resource directory, set it as the default
  399. // here.
  400. if (target_resource_dir_path) {
  401. driver.ResourceDir = target_resource_dir_path->str();
  402. }
  403. // Configure the install directory to find other tools and data files.
  404. //
  405. // We directly override the detected directory as we use a synthetic path
  406. // above. This makes it appear that our binary was in the installed binaries
  407. // directory, and allows finding tools relative to it.
  408. driver.Dir = installation_->llvm_install_bin();
  409. CARBON_VLOG("Setting bin directory to: {0}\n", driver.Dir);
  410. // When there's only one command being run, this will run it in-process.
  411. // However, a `clang` invocation may cause multiple `cc1` invocations, which
  412. // still subprocess. See `InProcess` comment at:
  413. // https://github.com/llvm/llvm-project/blob/86ce8e4504c06ecc3cc42f002ad4eb05cac10925/clang/lib/Driver/Job.cpp#L411-L413
  414. //
  415. // Note the subprocessing will effectively call `clang -cc1`, which turns into
  416. // `carbon-busybox clang -cc1`, which results in an equivalent `clang_main`
  417. // call.
  418. //
  419. // Also note that we only do `-disable-free` filtering in the in-process
  420. // execution here, as subprocesses leaking memory won't impact this process.
  421. auto cc1_main = [this](llvm::SmallVectorImpl<const char*>& cc1_args) -> int {
  422. return RunCC1(cc1_args);
  423. };
  424. driver.CC1Main = cc1_main;
  425. std::unique_ptr<clang::driver::Compilation> compilation(
  426. driver.BuildCompilation(cstr_args));
  427. CARBON_CHECK(compilation, "Should always successfully allocate!");
  428. if (compilation->containsError()) {
  429. // These should have been diagnosed by the driver.
  430. return false;
  431. }
  432. // Make sure our target detection matches Clang's. Sadly, we can't just reuse
  433. // Clang's as it is available too late.
  434. // TODO: Use nice diagnostics here rather than a check failure.
  435. CARBON_CHECK(llvm::Triple(target) == llvm::Triple(driver.getTargetTriple()),
  436. "Mismatch between the expected target '{0}' and the one "
  437. "computed by Clang '{1}'",
  438. target, driver.getTargetTriple());
  439. CARBON_VLOG("Running Clang driver...\n");
  440. llvm::SmallVector<std::pair<int, const clang::driver::Command*>>
  441. failing_commands;
  442. int result = driver.ExecuteCompilation(*compilation, failing_commands);
  443. // Finish diagnosing any failures before we verbosely log the source of those
  444. // failures.
  445. diagnostic_client.finish();
  446. CARBON_VLOG("Execution result code: {0}\n", result);
  447. for (const auto& [command_result, failing_command] : failing_commands) {
  448. CARBON_VLOG("Failing command '{0}' with code '{1}' was:\n",
  449. failing_command->getExecutable(), command_result);
  450. if (vlog_stream_) {
  451. failing_command->Print(*vlog_stream_, "\n\n", /*Quote=*/true);
  452. }
  453. }
  454. // Return whether the command was executed successfully.
  455. return result == 0 && failing_commands.empty();
  456. }
  457. auto ClangRunner::BuildCrtFile(llvm::StringRef target, llvm::StringRef src_file,
  458. const std::filesystem::path& out_path) -> void {
  459. std::filesystem::path src_path =
  460. installation_->llvm_runtime_srcs() / std::string_view(src_file);
  461. CARBON_VLOG("Building `{0}' from `{1}`...\n", out_path, src_path);
  462. std::string target_arg = llvm::formatv("--target={0}", target).str();
  463. CARBON_CHECK(RunWithNoRuntimes({
  464. "-no-canonical-prefixes",
  465. target_arg,
  466. "-DCRT_HAS_INITFINI_ARRAY",
  467. "-DEH_USE_FRAME_REGISTRY",
  468. "-O3",
  469. "-fPIC",
  470. "-ffreestanding",
  471. "-std=c11",
  472. "-w",
  473. "-c",
  474. "-o",
  475. out_path.native(),
  476. src_path.native(),
  477. }));
  478. }
  479. auto ClangRunner::CollectBuiltinsSrcFiles(const llvm::Triple& target_triple)
  480. -> llvm::SmallVector<llvm::StringRef> {
  481. llvm::SmallVector<llvm::StringRef> src_files;
  482. auto append_src_files =
  483. [&](auto input_srcs,
  484. llvm::function_ref<bool(llvm::StringRef)> filter_out = {}) {
  485. for (llvm::StringRef input_src : input_srcs) {
  486. if (!input_src.ends_with(".c") && !input_src.ends_with(".S")) {
  487. // Not a compiled file.
  488. continue;
  489. }
  490. if (filter_out && filter_out(input_src)) {
  491. // Filtered out.
  492. continue;
  493. }
  494. src_files.push_back(input_src);
  495. }
  496. };
  497. append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsGenericSrcs));
  498. append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsBf16Srcs));
  499. if (target_triple.isArch64Bit()) {
  500. append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsTfSrcs));
  501. }
  502. auto filter_out_chkstk = [&](llvm::StringRef src) {
  503. return !target_triple.isOSWindows() || !src.ends_with("chkstk.S");
  504. };
  505. if (target_triple.isAArch64()) {
  506. append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsAarch64Srcs),
  507. filter_out_chkstk);
  508. } else if (target_triple.isX86()) {
  509. append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsX86ArchSrcs));
  510. if (target_triple.isArch64Bit()) {
  511. append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsX86_64Srcs),
  512. filter_out_chkstk);
  513. } else {
  514. // TODO: This should be turned into a nice user-facing diagnostic about an
  515. // unsupported target.
  516. CARBON_CHECK(
  517. target_triple.isArch32Bit(),
  518. "The Carbon toolchain doesn't currently support 16-bit x86.");
  519. append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsI386Srcs),
  520. filter_out_chkstk);
  521. }
  522. } else {
  523. // TODO: This should be turned into a nice user-facing diagnostic about an
  524. // unsupported target.
  525. CARBON_FATAL("Target architecture is not supported: {0}",
  526. target_triple.str());
  527. }
  528. return src_files;
  529. }
  530. auto ClangRunner::BuildBuiltinsFile(llvm::StringRef target,
  531. llvm::StringRef src_file,
  532. const std::filesystem::path& out_path)
  533. -> void {
  534. std::filesystem::path src_path =
  535. installation_->llvm_runtime_srcs() / std::string_view(src_file);
  536. CARBON_VLOG("Building `{0}' from `{1}`...\n", out_path, src_path);
  537. std::string target_arg = llvm::formatv("--target={0}", target).str();
  538. CARBON_CHECK(RunWithNoRuntimes({
  539. "-no-canonical-prefixes",
  540. target_arg,
  541. "-O3",
  542. "-fPIC",
  543. "-ffreestanding",
  544. "-fno-builtin",
  545. "-fomit-frame-pointer",
  546. "-fvisibility=hidden",
  547. "-std=c11",
  548. "-w",
  549. "-c",
  550. "-o",
  551. out_path.native(),
  552. src_path.native(),
  553. }));
  554. }
  555. auto ClangRunner::BuildBuiltinsLib(llvm::StringRef target,
  556. const llvm::Triple& target_triple,
  557. const std::filesystem::path& tmp_path,
  558. Filesystem::DirRef lib_dir,
  559. llvm::ThreadPoolInterface& threads)
  560. -> ErrorOr<Success> {
  561. llvm::SmallVector<llvm::StringRef> src_files =
  562. CollectBuiltinsSrcFiles(target_triple);
  563. CARBON_ASSIGN_OR_RETURN(Filesystem::Dir tmp_dir,
  564. Filesystem::Cwd().OpenDir(tmp_path));
  565. // `NewArchiveMember` isn't default constructable unfortunately, so we first
  566. // build the objects using an optional wrapper.
  567. llvm::SmallVector<std::optional<llvm::NewArchiveMember>> objs;
  568. objs.resize(src_files.size());
  569. llvm::ThreadPoolTaskGroup member_group(threads);
  570. for (auto [src_file, obj] : llvm::zip_equal(src_files, objs)) {
  571. // Create any subdirectories needed for this file.
  572. std::filesystem::path src_path = src_file.str();
  573. if (src_path.has_parent_path()) {
  574. CARBON_RETURN_IF_ERROR(tmp_dir.CreateDirectories(src_path.parent_path()));
  575. }
  576. member_group.async([this, target, src_file, &obj, &tmp_path] {
  577. std::filesystem::path obj_path = tmp_path / std::string_view(src_file);
  578. obj_path += ".o";
  579. BuildBuiltinsFile(target, src_file, obj_path);
  580. auto obj_result = llvm::NewArchiveMember::getFile(obj_path.native(),
  581. /*Deterministic=*/true);
  582. CARBON_CHECK(obj_result, "TODO: Diagnose this: {0}",
  583. llvm::fmt_consume(obj_result.takeError()));
  584. obj = std::move(*obj_result);
  585. });
  586. }
  587. // Now build an archive out of the `.o` files for the builtins. Note that we
  588. // build this directly into the `lib_dir` as this is expected to be a staging
  589. // directory and cleaned up on errors.
  590. std::filesystem::path builtins_a_path = "libclang_rt.builtins.a";
  591. CARBON_ASSIGN_OR_RETURN(
  592. Filesystem::WriteFile builtins_a_file,
  593. lib_dir.OpenWriteOnly(builtins_a_path, Filesystem::CreateAlways));
  594. // Wait for all the object compiles to complete, and then move the objects out
  595. // of their optional wrappers to match the API required by the archive writer.
  596. member_group.wait();
  597. llvm::SmallVector<llvm::NewArchiveMember> unwrapped_objs;
  598. unwrapped_objs.reserve(objs.size());
  599. for (auto& obj : objs) {
  600. unwrapped_objs.push_back(*std::move(obj));
  601. }
  602. objs.clear();
  603. // Write the actual archive.
  604. {
  605. llvm::raw_fd_ostream builtins_a_os = builtins_a_file.WriteStream();
  606. llvm::Error archive_err = llvm::writeArchiveToStream(
  607. builtins_a_os, unwrapped_objs, llvm::SymtabWritingMode::NormalSymtab,
  608. target_triple.isOSDarwin() ? llvm::object::Archive::K_DARWIN
  609. : llvm::object::Archive::K_GNU,
  610. /*Deterministic=*/true, /*Thin=*/false);
  611. // The presence of an error is `true`.
  612. if (archive_err) {
  613. return Error(llvm::toString(std::move(archive_err)));
  614. }
  615. }
  616. CARBON_RETURN_IF_ERROR(std::move(builtins_a_file).Close());
  617. return Success();
  618. }
  619. } // namespace Carbon