clang_runner.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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 filesystem.
  289. clang_instance->createVirtualFileSystem(fs_, &diag_buffer);
  290. // Create the actual diagnostics engine.
  291. clang_instance->createDiagnostics();
  292. if (!clang_instance->hasDiagnostics()) {
  293. return EXIT_FAILURE;
  294. }
  295. // Now flush the buffered diagnostics into the Clang instance's diagnostic
  296. // engine. If we've already hit an error, we can exit early once that's done.
  297. diag_buffer.FlushDiagnostics(clang_instance->getDiagnostics());
  298. if (!success) {
  299. clang_instance->getDiagnosticClient().finish();
  300. return EXIT_FAILURE;
  301. }
  302. // Execute the frontend actions.
  303. {
  304. llvm::TimeTraceScope time_scope("ExecuteCompiler");
  305. bool time_passes = clang_instance->getCodeGenOpts().TimePasses;
  306. if (time_passes) {
  307. clang_instance->createFrontendTimer();
  308. }
  309. llvm::TimeRegion timer(time_passes ? &clang_instance->getFrontendTimer()
  310. : nullptr);
  311. success = clang::ExecuteCompilerInvocation(clang_instance.get());
  312. }
  313. // If any timers were active but haven't been destroyed yet, print their
  314. // results now. This happens in -disable-free mode.
  315. std::unique_ptr<llvm::raw_ostream> io_file = llvm::CreateInfoOutputFile();
  316. if (clang_instance->getCodeGenOpts().TimePassesJson) {
  317. *io_file << "{\n";
  318. llvm::TimerGroup::printAllJSONValues(*io_file, "");
  319. *io_file << "\n}\n";
  320. } else if (!clang_instance->getCodeGenOpts().TimePassesStatsFile) {
  321. llvm::TimerGroup::printAll(*io_file);
  322. }
  323. llvm::TimerGroup::clearAll();
  324. if (llvm::timeTraceProfilerEnabled()) {
  325. // It is possible that the compiler instance doesn't own a file manager here
  326. // if we're compiling a module unit, since the file manager is owned by the
  327. // AST when we're compiling a module unit. So the file manager may be
  328. // invalid here.
  329. //
  330. // It should be fine to create file manager here since the file system
  331. // options are stored in the compiler invocation and we can recreate the VFS
  332. // from the compiler invocation.
  333. if (!clang_instance->hasFileManager()) {
  334. clang_instance->createFileManager();
  335. }
  336. if (auto profiler_output = clang_instance->createOutputFile(
  337. clang_instance->getFrontendOpts().TimeTracePath, /*Binary=*/false,
  338. /*RemoveFileOnSignal=*/false,
  339. /*useTemporary=*/false)) {
  340. llvm::timeTraceProfilerWrite(*profiler_output);
  341. profiler_output.reset();
  342. llvm::timeTraceProfilerCleanup();
  343. clang_instance->clearOutputFiles(false);
  344. }
  345. }
  346. // When running with -disable-free, don't do any destruction or shutdown.
  347. if (clang_instance->getFrontendOpts().DisableFree) {
  348. llvm::BuryPointer(std::move(clang_instance));
  349. }
  350. return success ? EXIT_SUCCESS : EXIT_FAILURE;
  351. }
  352. auto ClangRunner::RunInternal(
  353. llvm::ArrayRef<llvm::StringRef> args, llvm::StringRef target,
  354. std::optional<llvm::StringRef> target_resource_dir_path) -> bool {
  355. std::string clang_path = installation_->clang_path();
  356. // Rebuild the args as C-string args.
  357. llvm::OwningArrayRef<char> cstr_arg_storage;
  358. llvm::SmallVector<const char*, 64> cstr_args =
  359. BuildCStrArgs("Clang", clang_path, "-v", args, cstr_arg_storage);
  360. // Handle special dispatch for CC1 commands as they don't use the driver.
  361. if (!args.empty() && args[0].starts_with("-cc1")) {
  362. CARBON_VLOG("Calling Clang's CC1...");
  363. int exit_code = RunCC1(cstr_args);
  364. // TODO: Should this be forwarding the full exit code?
  365. return exit_code == 0;
  366. }
  367. CARBON_VLOG("Preparing Clang driver...\n");
  368. // Create the diagnostic options and parse arguments controlling them out of
  369. // our arguments.
  370. std::unique_ptr<clang::DiagnosticOptions> diagnostic_options =
  371. clang::CreateAndPopulateDiagOpts(cstr_args);
  372. // TODO: We don't yet support serializing diagnostics the way the actual
  373. // `clang` command line does. Unclear if we need to or not, but it would need
  374. // a bit more logic here to set up chained consumers.
  375. clang::TextDiagnosticPrinter diagnostic_client(llvm::errs(),
  376. *diagnostic_options);
  377. // Note that the `DiagnosticsEngine` takes ownership (via a ref count) of the
  378. // DiagnosticIDs, unlike the other parameters.
  379. clang::DiagnosticsEngine diagnostics(clang::DiagnosticIDs::create(),
  380. *diagnostic_options, &diagnostic_client,
  381. /*ShouldOwnClient=*/false);
  382. clang::ProcessWarningOptions(diagnostics, *diagnostic_options, *fs_);
  383. // Note that we configure the driver's *default* target here, not the expected
  384. // target as that will be parsed out of the command line below.
  385. clang::driver::Driver driver(clang_path, llvm::sys::getDefaultTargetTriple(),
  386. diagnostics, "clang LLVM compiler", fs_);
  387. llvm::Triple target_triple(target);
  388. // We need to set an SDK system root on macOS by default. Setting it here
  389. // allows a custom sysroot to still be specified on the command line.
  390. //
  391. // TODO: A different system root should be used for iOS, watchOS, tvOS.
  392. // Currently, we're only targeting macOS support though.
  393. if (target_triple.isMacOSX()) {
  394. // This is the default CLT system root, shown by `xcrun --show-sdk-path`.
  395. // We hard code it here to avoid the overhead of subprocessing to `xcrun` on
  396. // each Clang invocation, but this may need to be updated to search or
  397. // reflect macOS versions if this changes in the future.
  398. driver.SysRoot = "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk";
  399. }
  400. // If we have a target-specific resource directory, set it as the default
  401. // here.
  402. if (target_resource_dir_path) {
  403. driver.ResourceDir = target_resource_dir_path->str();
  404. }
  405. // Configure the install directory to find other tools and data files.
  406. //
  407. // We directly override the detected directory as we use a synthetic path
  408. // above. This makes it appear that our binary was in the installed binaries
  409. // directory, and allows finding tools relative to it.
  410. driver.Dir = installation_->llvm_install_bin();
  411. CARBON_VLOG("Setting bin directory to: {0}\n", driver.Dir);
  412. // When there's only one command being run, this will run it in-process.
  413. // However, a `clang` invocation may cause multiple `cc1` invocations, which
  414. // still subprocess. See `InProcess` comment at:
  415. // https://github.com/llvm/llvm-project/blob/86ce8e4504c06ecc3cc42f002ad4eb05cac10925/clang/lib/Driver/Job.cpp#L411-L413
  416. //
  417. // Note the subprocessing will effectively call `clang -cc1`, which turns into
  418. // `carbon-busybox clang -cc1`, which results in an equivalent `clang_main`
  419. // call.
  420. //
  421. // Also note that we only do `-disable-free` filtering in the in-process
  422. // execution here, as subprocesses leaking memory won't impact this process.
  423. auto cc1_main = [this](llvm::SmallVectorImpl<const char*>& cc1_args) -> int {
  424. return RunCC1(cc1_args);
  425. };
  426. driver.CC1Main = cc1_main;
  427. std::unique_ptr<clang::driver::Compilation> compilation(
  428. driver.BuildCompilation(cstr_args));
  429. CARBON_CHECK(compilation, "Should always successfully allocate!");
  430. if (compilation->containsError()) {
  431. // These should have been diagnosed by the driver.
  432. return false;
  433. }
  434. // Make sure our target detection matches Clang's. Sadly, we can't just reuse
  435. // Clang's as it is available too late.
  436. // TODO: Use nice diagnostics here rather than a check failure.
  437. CARBON_CHECK(llvm::Triple(target) == llvm::Triple(driver.getTargetTriple()),
  438. "Mismatch between the expected target '{0}' and the one "
  439. "computed by Clang '{1}'",
  440. target, driver.getTargetTriple());
  441. CARBON_VLOG("Running Clang driver...\n");
  442. llvm::SmallVector<std::pair<int, const clang::driver::Command*>>
  443. failing_commands;
  444. int result = driver.ExecuteCompilation(*compilation, failing_commands);
  445. // Finish diagnosing any failures before we verbosely log the source of those
  446. // failures.
  447. diagnostic_client.finish();
  448. CARBON_VLOG("Execution result code: {0}\n", result);
  449. for (const auto& [command_result, failing_command] : failing_commands) {
  450. CARBON_VLOG("Failing command '{0}' with code '{1}' was:\n",
  451. failing_command->getExecutable(), command_result);
  452. if (vlog_stream_) {
  453. failing_command->Print(*vlog_stream_, "\n\n", /*Quote=*/true);
  454. }
  455. }
  456. // Return whether the command was executed successfully.
  457. return result == 0 && failing_commands.empty();
  458. }
  459. auto ClangRunner::BuildCrtFile(llvm::StringRef target, llvm::StringRef src_file,
  460. const std::filesystem::path& out_path) -> void {
  461. std::filesystem::path src_path =
  462. installation_->llvm_runtime_srcs() / std::string_view(src_file);
  463. CARBON_VLOG("Building `{0}' from `{1}`...\n", out_path, src_path);
  464. std::string target_arg = llvm::formatv("--target={0}", target).str();
  465. CARBON_CHECK(RunWithNoRuntimes({
  466. "-no-canonical-prefixes",
  467. target_arg,
  468. "-DCRT_HAS_INITFINI_ARRAY",
  469. "-DEH_USE_FRAME_REGISTRY",
  470. "-O3",
  471. "-fPIC",
  472. "-ffreestanding",
  473. "-std=c11",
  474. "-w",
  475. "-c",
  476. "-o",
  477. out_path.native(),
  478. src_path.native(),
  479. }));
  480. }
  481. auto ClangRunner::CollectBuiltinsSrcFiles(const llvm::Triple& target_triple)
  482. -> llvm::SmallVector<llvm::StringRef> {
  483. llvm::SmallVector<llvm::StringRef> src_files;
  484. auto append_src_files =
  485. [&](auto input_srcs,
  486. llvm::function_ref<bool(llvm::StringRef)> filter_out = {}) {
  487. for (llvm::StringRef input_src : input_srcs) {
  488. if (!input_src.ends_with(".c") && !input_src.ends_with(".S")) {
  489. // Not a compiled file.
  490. continue;
  491. }
  492. if (filter_out && filter_out(input_src)) {
  493. // Filtered out.
  494. continue;
  495. }
  496. src_files.push_back(input_src);
  497. }
  498. };
  499. append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsGenericSrcs));
  500. append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsBf16Srcs));
  501. if (target_triple.isArch64Bit()) {
  502. append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsTfSrcs));
  503. }
  504. auto filter_out_chkstk = [&](llvm::StringRef src) {
  505. return !target_triple.isOSWindows() || !src.ends_with("chkstk.S");
  506. };
  507. if (target_triple.isAArch64()) {
  508. append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsAarch64Srcs),
  509. filter_out_chkstk);
  510. } else if (target_triple.isX86()) {
  511. append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsX86ArchSrcs));
  512. if (target_triple.isArch64Bit()) {
  513. append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsX86_64Srcs),
  514. filter_out_chkstk);
  515. } else {
  516. // TODO: This should be turned into a nice user-facing diagnostic about an
  517. // unsupported target.
  518. CARBON_CHECK(
  519. target_triple.isArch32Bit(),
  520. "The Carbon toolchain doesn't currently support 16-bit x86.");
  521. append_src_files(llvm::ArrayRef(RuntimeSources::BuiltinsI386Srcs),
  522. filter_out_chkstk);
  523. }
  524. } else {
  525. // TODO: This should be turned into a nice user-facing diagnostic about an
  526. // unsupported target.
  527. CARBON_FATAL("Target architecture is not supported: {0}",
  528. target_triple.str());
  529. }
  530. return src_files;
  531. }
  532. auto ClangRunner::BuildBuiltinsFile(llvm::StringRef target,
  533. llvm::StringRef src_file,
  534. const std::filesystem::path& out_path)
  535. -> void {
  536. std::filesystem::path src_path =
  537. installation_->llvm_runtime_srcs() / std::string_view(src_file);
  538. CARBON_VLOG("Building `{0}' from `{1}`...\n", out_path, src_path);
  539. std::string target_arg = llvm::formatv("--target={0}", target).str();
  540. CARBON_CHECK(RunWithNoRuntimes({
  541. "-no-canonical-prefixes",
  542. target_arg,
  543. "-O3",
  544. "-fPIC",
  545. "-ffreestanding",
  546. "-fno-builtin",
  547. "-fomit-frame-pointer",
  548. "-fvisibility=hidden",
  549. "-std=c11",
  550. "-w",
  551. "-c",
  552. "-o",
  553. out_path.native(),
  554. src_path.native(),
  555. }));
  556. }
  557. auto ClangRunner::BuildBuiltinsLib(llvm::StringRef target,
  558. const llvm::Triple& target_triple,
  559. const std::filesystem::path& tmp_path,
  560. Filesystem::DirRef lib_dir,
  561. llvm::ThreadPoolInterface& threads)
  562. -> ErrorOr<Success> {
  563. llvm::SmallVector<llvm::StringRef> src_files =
  564. CollectBuiltinsSrcFiles(target_triple);
  565. CARBON_ASSIGN_OR_RETURN(Filesystem::Dir tmp_dir,
  566. Filesystem::Cwd().OpenDir(tmp_path));
  567. // `NewArchiveMember` isn't default constructable unfortunately, so we first
  568. // build the objects using an optional wrapper.
  569. llvm::SmallVector<std::optional<llvm::NewArchiveMember>> objs;
  570. objs.resize(src_files.size());
  571. llvm::ThreadPoolTaskGroup member_group(threads);
  572. for (auto [src_file, obj] : llvm::zip_equal(src_files, objs)) {
  573. // Create any subdirectories needed for this file.
  574. std::filesystem::path src_path = src_file.str();
  575. if (src_path.has_parent_path()) {
  576. CARBON_RETURN_IF_ERROR(tmp_dir.CreateDirectories(src_path.parent_path()));
  577. }
  578. member_group.async([this, target, src_file, &obj, &tmp_path] {
  579. std::filesystem::path obj_path = tmp_path / std::string_view(src_file);
  580. obj_path += ".o";
  581. BuildBuiltinsFile(target, src_file, obj_path);
  582. auto obj_result = llvm::NewArchiveMember::getFile(obj_path.native(),
  583. /*Deterministic=*/true);
  584. CARBON_CHECK(obj_result, "TODO: Diagnose this: {0}",
  585. llvm::fmt_consume(obj_result.takeError()));
  586. obj = std::move(*obj_result);
  587. });
  588. }
  589. // Now build an archive out of the `.o` files for the builtins. Note that we
  590. // build this directly into the `lib_dir` as this is expected to be a staging
  591. // directory and cleaned up on errors.
  592. std::filesystem::path builtins_a_path = "libclang_rt.builtins.a";
  593. CARBON_ASSIGN_OR_RETURN(
  594. Filesystem::WriteFile builtins_a_file,
  595. lib_dir.OpenWriteOnly(builtins_a_path, Filesystem::CreateAlways));
  596. // Wait for all the object compiles to complete, and then move the objects out
  597. // of their optional wrappers to match the API required by the archive writer.
  598. member_group.wait();
  599. llvm::SmallVector<llvm::NewArchiveMember> unwrapped_objs;
  600. unwrapped_objs.reserve(objs.size());
  601. for (auto& obj : objs) {
  602. unwrapped_objs.push_back(*std::move(obj));
  603. }
  604. objs.clear();
  605. // Write the actual archive.
  606. {
  607. llvm::raw_fd_ostream builtins_a_os = builtins_a_file.WriteStream();
  608. llvm::Error archive_err = llvm::writeArchiveToStream(
  609. builtins_a_os, unwrapped_objs, llvm::SymtabWritingMode::NormalSymtab,
  610. target_triple.isOSDarwin() ? llvm::object::Archive::K_DARWIN
  611. : llvm::object::Archive::K_GNU,
  612. /*Deterministic=*/true, /*Thin=*/false);
  613. // The presence of an error is `true`.
  614. if (archive_err) {
  615. return Error(llvm::toString(std::move(archive_err)));
  616. }
  617. }
  618. CARBON_RETURN_IF_ERROR(std::move(builtins_a_file).Close());
  619. return Success();
  620. }
  621. } // namespace Carbon