clang_runner.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 <stdlib.h>
  6. #include <unistd.h>
  7. #include <filesystem>
  8. #include <memory>
  9. #include <optional>
  10. #include <string>
  11. #include <utility>
  12. #include "clang/Basic/Diagnostic.h"
  13. #include "clang/Basic/DiagnosticDriver.h"
  14. #include "clang/Basic/DiagnosticIDs.h"
  15. #include "clang/Basic/DiagnosticOptions.h"
  16. #include "clang/CodeGen/ObjectFilePCHContainerWriter.h"
  17. #include "clang/Driver/Compilation.h"
  18. #include "clang/Driver/Driver.h"
  19. #include "clang/Frontend/CompilerInstance.h"
  20. #include "clang/Frontend/CompilerInvocation.h"
  21. #include "clang/Frontend/TextDiagnosticBuffer.h"
  22. #include "clang/Frontend/TextDiagnosticPrinter.h"
  23. #include "clang/Frontend/Utils.h"
  24. #include "clang/FrontendTool/Utils.h"
  25. #include "clang/Serialization/ObjectFilePCHContainerReader.h"
  26. #include "clang/Serialization/PCHContainerOperations.h"
  27. #include "common/check.h"
  28. #include "common/error.h"
  29. #include "common/string_helpers.h"
  30. #include "common/vlog.h"
  31. #include "llvm/ADT/ArrayRef.h"
  32. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  33. #include "llvm/ADT/STLExtras.h"
  34. #include "llvm/ADT/SmallVector.h"
  35. #include "llvm/ADT/Statistic.h"
  36. #include "llvm/ADT/StringExtras.h"
  37. #include "llvm/ADT/StringRef.h"
  38. #include "llvm/IR/LLVMContext.h"
  39. #include "llvm/Support/Allocator.h"
  40. #include "llvm/Support/BuryPointer.h"
  41. #include "llvm/Support/CommandLine.h"
  42. #include "llvm/Support/Error.h"
  43. #include "llvm/Support/FormatVariadic.h"
  44. #include "llvm/Support/LLVMDriver.h"
  45. #include "llvm/Support/ThreadPool.h"
  46. #include "llvm/Support/TimeProfiler.h"
  47. #include "llvm/Support/Timer.h"
  48. #include "llvm/Support/raw_ostream.h"
  49. #include "llvm/TargetParser/Host.h"
  50. #include "third_party/llvm/clang_cc1.h"
  51. #include "toolchain/base/clang_invocation.h"
  52. #include "toolchain/base/install_paths.h"
  53. #include "toolchain/driver/clang_runtimes.h"
  54. #include "toolchain/driver/runtimes_cache.h"
  55. #include "toolchain/driver/tool_runner_base.h"
  56. // Defined in:
  57. // https://github.com/llvm/llvm-project/blob/main/clang/tools/driver/driver.cpp
  58. //
  59. // While not in a header, this is the API used by llvm-driver.cpp for
  60. // busyboxing.
  61. //
  62. // NOLINTNEXTLINE(readability-identifier-naming)
  63. auto clang_main(int Argc, char** Argv, const llvm::ToolContext& ToolContext)
  64. -> int;
  65. namespace Carbon {
  66. ClangRunner::ClangRunner(
  67. const InstallPaths* install_paths,
  68. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
  69. llvm::raw_ostream* vlog_stream,
  70. std::optional<std::filesystem::path> override_clang_path)
  71. : ToolRunnerBase(install_paths, vlog_stream),
  72. fs_(std::move(fs)),
  73. clang_path_(override_clang_path ? *std::move(override_clang_path)
  74. : installation_->clang_path()) {}
  75. // Searches an argument list to a Clang execution to determine the expected
  76. // target string, suitable for use with `llvm::Triple`.
  77. //
  78. // If no explicit target flags are present, this defaults to the default
  79. // LLVM target.
  80. //
  81. // Works to handle the most common flags that modify the expected target as
  82. // well as direct target flags.
  83. //
  84. // Note: this has known fidelity issues if the args include separate-value flags
  85. // (`--flag value` style as opposed to `--flag=value`) where the value might
  86. // match the spelling of one of the target flags. For example, args that include
  87. // an output file spelled `-m32` (so `-o` followed by `-m32`) will be
  88. // misinterpreted by considering the value to itself be a flag. Addressing this
  89. // would add substantial complexity, including likely parsing the entire args
  90. // twice with the Clang driver. Instead, our current plan is to document this
  91. // limitation and encourage the use of flags with joined values
  92. // (`--flag=value`).
  93. static auto ComputeClangTarget(llvm::ArrayRef<llvm::StringRef> args)
  94. -> std::string {
  95. std::string target = llvm::sys::getDefaultTargetTriple();
  96. bool explicit_target = false;
  97. for (auto [i, arg] : llvm::enumerate(args)) {
  98. if (llvm::StringRef arg_copy = arg; arg_copy.consume_front("--target=")) {
  99. target = arg_copy.str();
  100. explicit_target = true;
  101. } else if ((arg == "--target" || arg == "-target") &&
  102. (i + 1) < args.size()) {
  103. target = args[i + 1].str();
  104. explicit_target = true;
  105. } else if (!explicit_target &&
  106. (arg == "--driver-mode=cl" ||
  107. ((arg == "--driver-mode" || arg == "-driver-mode") &&
  108. (i + 1) < args.size() && args[i + 1] == "cl"))) {
  109. // The `cl.exe` compatible driver mode should switch the default target to
  110. // a `...-pc-windows-msvc` target. However, a subsequent explicit target
  111. // should override this.
  112. llvm::Triple triple(target);
  113. triple.setVendor(llvm::Triple::PC);
  114. triple.setOS(llvm::Triple::Win32);
  115. triple.setEnvironment(llvm::Triple::MSVC);
  116. target = triple.str();
  117. } else if (arg == "-m32") {
  118. llvm::Triple triple(target);
  119. if (!triple.isArch32Bit()) {
  120. target = triple.get32BitArchVariant().str();
  121. }
  122. } else if (arg == "-m64") {
  123. llvm::Triple triple(target);
  124. if (!triple.isArch64Bit()) {
  125. target = triple.get64BitArchVariant().str();
  126. }
  127. }
  128. }
  129. return target;
  130. }
  131. // Tries to detect a a non-linking list of Clang arguments to avoid setting up
  132. // the more complete resource directory needed for linking. False negatives are
  133. // fine here, and we use that to keep things simple.
  134. static auto IsNonLinkCommand(llvm::ArrayRef<llvm::StringRef> args) -> bool {
  135. return llvm::any_of(args, [](llvm::StringRef arg) {
  136. // Only check the most common cases as we have to do this for each argument.
  137. // Everything else is rare and likely not worth the cost of searching for
  138. // since it's fine to have false negatives.
  139. return arg == "-c" || arg == "-E" || arg == "-S" ||
  140. arg == "-fsyntax-only" || arg == "--version" || arg == "--help" ||
  141. arg == "/?" || arg == "--driver-mode=cpp";
  142. });
  143. }
  144. auto ClangRunner::RunWithPrebuiltRuntimes(llvm::ArrayRef<llvm::StringRef> args,
  145. Runtimes& prebuilt_runtimes,
  146. bool enable_leaking)
  147. -> ErrorOr<bool> {
  148. // Check the args to see if we have a known target-independent command. If so,
  149. // directly dispatch it to avoid the cost of building the target resource
  150. // directory.
  151. // TODO: Maybe handle response file expansion similar to the Clang CLI?
  152. if (args.empty() || args[0].starts_with("-cc1") || IsNonLinkCommand(args)) {
  153. return RunWithNoRuntimes(args, enable_leaking);
  154. }
  155. std::string target = ComputeClangTarget(args);
  156. CARBON_ASSIGN_OR_RETURN(std::filesystem::path prebuilt_resource_dir_path,
  157. prebuilt_runtimes.Get(Runtimes::ClangResourceDir));
  158. CARBON_ASSIGN_OR_RETURN(std::filesystem::path libunwind_path,
  159. prebuilt_runtimes.Get(Runtimes::LibUnwind));
  160. CARBON_ASSIGN_OR_RETURN(std::filesystem::path libcxx_path,
  161. prebuilt_runtimes.Get(Runtimes::Libcxx));
  162. return RunInternal(args, target, prebuilt_resource_dir_path.native(),
  163. std::move(libunwind_path), std::move(libcxx_path),
  164. /*link_runtime_libs=*/true, enable_leaking);
  165. }
  166. auto ClangRunner::Run(llvm::ArrayRef<llvm::StringRef> args,
  167. Runtimes::Cache& runtimes_cache,
  168. llvm::ThreadPoolInterface& runtimes_build_thread_pool,
  169. bool enable_leaking) -> ErrorOr<bool> {
  170. std::string target = ComputeClangTarget(args);
  171. // Check the args to see if we have a known target-independent command. If so,
  172. // directly dispatch it to avoid the cost of building the target resource
  173. // directory.
  174. // TODO: Maybe handle response file expansion similar to the Clang CLI?
  175. if (args.empty() || args[0].starts_with("-cc1") || IsNonLinkCommand(args)) {
  176. // Note that we do allow linking default libraries here -- we want to learn
  177. // if a command ever goes through this path and Clang thinks it needs to
  178. // link a library as the goal here is to correctly detect that this will
  179. // _not_ happen. Suppressing the linking of default libraries would hide a
  180. // failure in that case.
  181. return RunInternal(args, target, /*target_resource_dir_path=*/std::nullopt,
  182. /*libunwind_path=*/std::nullopt,
  183. /*libcxx_path=*/std::nullopt, /*link_runtime_libs=*/true,
  184. enable_leaking);
  185. }
  186. Runtimes::Cache::Features features = {.target = target};
  187. CARBON_ASSIGN_OR_RETURN(Runtimes runtimes, runtimes_cache.Lookup(features));
  188. // We need to build the Clang resource directory for these runtimes. This
  189. // requires a temporary directory as well as the destination directory for
  190. // the build. The temporary directory should only be used during the build,
  191. // not once we are running Clang with the built runtime.
  192. CARBON_VLOG("Building target resource dir...\n");
  193. ClangResourceDirBuilder builder(this, &runtimes_build_thread_pool,
  194. llvm::Triple(features.target), &runtimes);
  195. ClangArchiveRuntimesBuilder<Runtimes::LibUnwind> lib_unwind_builder(
  196. this, &runtimes_build_thread_pool, llvm::Triple(features.target),
  197. &runtimes);
  198. ClangArchiveRuntimesBuilder<Runtimes::Libcxx> libcxx_builder(
  199. this, &runtimes_build_thread_pool, llvm::Triple(features.target),
  200. &runtimes);
  201. CARBON_ASSIGN_OR_RETURN(std::filesystem::path resource_dir_path,
  202. std::move(builder).Wait());
  203. CARBON_ASSIGN_OR_RETURN(std::filesystem::path libunwind_path,
  204. std::move(lib_unwind_builder).Wait());
  205. CARBON_ASSIGN_OR_RETURN(std::filesystem::path libcxx_path,
  206. std::move(libcxx_builder).Wait());
  207. // Note that this function always successfully runs `clang` and returns a bool
  208. // to indicate whether `clang` itself succeeded, not whether the runner was
  209. // able to run it. As a consequence, even a `false` here is a non-`Error`
  210. // return.
  211. return RunInternal(args, target, resource_dir_path.native(),
  212. std::move(libunwind_path), std::move(libcxx_path),
  213. /*link_runtime_libs=*/true, enable_leaking);
  214. }
  215. auto ClangRunner::RunWithNoRuntimes(llvm::ArrayRef<llvm::StringRef> args,
  216. bool enable_leaking) -> ErrorOr<bool> {
  217. std::string target = ComputeClangTarget(args);
  218. return RunInternal(args, target, /*target_resource_dir_path=*/std::nullopt,
  219. /*libunwind_path=*/std::nullopt,
  220. /*libcxx_path=*/std::nullopt, /*link_runtime_libs=*/false,
  221. enable_leaking);
  222. }
  223. auto ClangRunner::RunClangCC1(llvm::SmallVectorImpl<const char*>& cstr_args,
  224. bool enable_leaking) -> int {
  225. if (cstr_args[1] == llvm::StringRef("-cc1")) {
  226. CARBON_VLOG("Dispatching `-cc1` command line in-process...");
  227. int exit_code =
  228. RunClangCC1Main(*installation_, fs_, cstr_args, enable_leaking);
  229. return exit_code;
  230. }
  231. // Other CC1-based invocations need to dispatch into the `clang_main`
  232. // routine to work correctly. This means they're not reliable in a library
  233. // context but currently there is too much logic to reasonably extract here.
  234. // This at least allows simple cases (often when directly used on the
  235. // command line) to work correctly.
  236. //
  237. // TODO: Factor the relevant code paths into a library API or move this into
  238. // the busybox dispatch logic.
  239. CARBON_VLOG("Calling clang_main for a cc1-based invocation...");
  240. // cstr_args[0] will be the `clang_path` so we don't need the prepend arg.
  241. llvm::ToolContext tool_context = {
  242. .Path = cstr_args[0], .PrependArg = "clang", .NeedsPrependArg = false};
  243. int exit_code = clang_main(
  244. cstr_args.size(), const_cast<char**>(cstr_args.data()), tool_context);
  245. return exit_code;
  246. }
  247. auto ClangRunner::RunInternal(
  248. llvm::ArrayRef<llvm::StringRef> args, llvm::StringRef target,
  249. std::optional<llvm::StringRef> target_resource_dir_path,
  250. std::optional<std::filesystem::path> libunwind_path,
  251. std::optional<std::filesystem::path> libcxx_path, bool link_runtime_libs,
  252. bool enable_leaking) -> ErrorOr<bool> {
  253. llvm::BumpPtrAllocator alloc;
  254. // Handle special dispatch for CC1 commands as they don't use the driver and
  255. // we don't synthesize any default arguments there.
  256. if (!args.empty() && args[0].starts_with("-cc1")) {
  257. llvm::SmallVector<const char*, 64> cstr_args =
  258. BuildCStrArgs(clang_path_.native(), args, alloc);
  259. // TODO: Should this be forwarding the full exit code?
  260. return RunClangCC1(cstr_args, enable_leaking) == 0;
  261. }
  262. // We start with a custom prefix of arguments to establish Carbon's default
  263. // configuration for invoking Clang. These may not all be needed for all
  264. // invocations, so we also suppress warnings about any that are ignored.
  265. llvm::SmallVector<std::string> prefix_args;
  266. prefix_args.push_back("--start-no-unused-arguments");
  267. AppendDefaultClangArgs(*installation_, target, prefix_args);
  268. if (link_runtime_libs) {
  269. // We don't have a direct way to configure the linker search paths in the
  270. // Clang driver outside of command line flags, so we inject them here with
  271. // flags. Note that we only inject these as _search_ paths to allow the
  272. // normal linking rules to govern whether or not to link a given library. We
  273. // also build our runtimes exclusively as static archives so we don't need
  274. // to use command line flags to force static runtime linking to occur.
  275. if (libunwind_path) {
  276. prefix_args.push_back(
  277. llvm::formatv("-L{0}/lib", *std::move(libunwind_path)).str());
  278. }
  279. if (libcxx_path) {
  280. prefix_args.push_back(
  281. llvm::formatv("-L{0}/lib", std::move(libcxx_path)).str());
  282. }
  283. } else {
  284. // If we are suppressing the linking of default libs, ensure we didn't get a
  285. // path to add to the link for them, or an override of the resource
  286. // directory.
  287. CARBON_CHECK(!target_resource_dir_path);
  288. CARBON_CHECK(!libunwind_path);
  289. CARBON_CHECK(!libcxx_path);
  290. // Now suppress all the default library linking, as we don't expect to have
  291. // any target runtimes on this code path.
  292. //
  293. // TODO: What we actually want here is something more like `-nostdlib++`,
  294. // `-unwindlib=none`, `-rtlib=none`; however, the last of these doesn't
  295. // exist in Clang and looks tricky to introduce. This is almost certainly
  296. // wrong, as it likely suppresses the linking of the _C_ standard library,
  297. // which isn't one of the Clang runtime libraries we're trying to control
  298. // here. But the only user of this currently doesn't need to distinguish.
  299. prefix_args.push_back("-nostdlib");
  300. }
  301. prefix_args.push_back("--end-no-unused-arguments");
  302. // Rebuild the args as C-string args.
  303. llvm::SmallVector<const char*, 64> cstr_args =
  304. BuildCStrArgs(clang_path_.native(), prefix_args, args, alloc);
  305. // Expand any response files in the arguments.
  306. bool is_clang_cl_mode = clang::driver::IsClangCL(
  307. clang::driver::getDriverMode(clang_path_.native(), cstr_args));
  308. if (llvm::Error error = clang::driver::expandResponseFiles(
  309. cstr_args, is_clang_cl_mode, alloc, fs_.get())) {
  310. return Error(llvm::toString(std::move(error)));
  311. }
  312. CARBON_VLOG("Running Clang driver with the following arguments:\n");
  313. for (const char* cstr_arg : llvm::ArrayRef(cstr_args)) {
  314. CARBON_VLOG(" '{0}'\n", cstr_arg);
  315. }
  316. llvm::SmallVector<std::pair<int, const clang::driver::Command*>>
  317. failing_commands;
  318. int result = -1;
  319. // Create the diagnostic options and parse arguments controlling them out of
  320. // our arguments.
  321. std::unique_ptr<clang::DiagnosticOptions> diagnostic_options =
  322. clang::CreateAndPopulateDiagOpts(cstr_args);
  323. // TODO: We don't yet support serializing diagnostics the way the actual
  324. // `clang` command line does. Unclear if we need to or not, but it would
  325. // need a bit more logic here to set up chained consumers.
  326. clang::TextDiagnosticPrinter diagnostic_client(llvm::errs(),
  327. *diagnostic_options);
  328. // Note that the `DiagnosticsEngine` takes ownership (via a ref count) of
  329. // the DiagnosticIDs, unlike the other parameters.
  330. clang::DiagnosticsEngine diagnostics(clang::DiagnosticIDs::create(),
  331. *diagnostic_options, &diagnostic_client,
  332. /*ShouldOwnClient=*/false);
  333. clang::ProcessWarningOptions(diagnostics, *diagnostic_options, *fs_);
  334. // Note that we configure the driver's *default* target here, not the
  335. // expected target as that will be parsed out of the command line below.
  336. clang::driver::Driver driver(clang_path_.native(),
  337. llvm::sys::getDefaultTargetTriple(), diagnostics,
  338. "clang LLVM compiler", fs_);
  339. llvm::Triple target_triple(target);
  340. // We need to set an SDK system root on macOS by default. Setting it here
  341. // allows a custom sysroot to still be specified on the command line.
  342. //
  343. // TODO: A different system root should be used for iOS, watchOS, tvOS.
  344. // Currently, we're only targeting macOS support though.
  345. if (target_triple.isMacOSX()) {
  346. // This is the default CLT system root, shown by `xcrun --show-sdk-path`.
  347. // We hard code it here to avoid the overhead of subprocessing to `xcrun`
  348. // on each Clang invocation, but this may need to be updated to search or
  349. // reflect macOS versions if this changes in the future.
  350. driver.SysRoot = "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk";
  351. }
  352. // If we have a target-specific resource directory, set it as the default
  353. // here, otherwise use the installation's resource directory.
  354. driver.ResourceDir = target_resource_dir_path
  355. ? target_resource_dir_path->str()
  356. : installation_->clang_resource_path().native();
  357. // Configure the install directory to find other tools and data files.
  358. //
  359. // We directly override the detected directory as we use a synthetic path
  360. // above. This makes it appear that our binary was in the installed binaries
  361. // directory, and allows finding tools relative to it.
  362. driver.Dir = installation_->llvm_install_bin();
  363. CARBON_VLOG("Setting bin directory to: {0}\n", driver.Dir);
  364. // When there's only one command being run, this will run it in-process.
  365. // However, a `clang` invocation may cause multiple `cc1` invocations, which
  366. // still subprocess. See `InProcess` comment at:
  367. // https://github.com/llvm/llvm-project/blob/86ce8e4504c06ecc3cc42f002ad4eb05cac10925/clang/lib/Driver/Job.cpp#L411-L413
  368. //
  369. // Note the subprocessing will effectively call `clang -cc1`, which turns
  370. // into `carbon-busybox clang -cc1`, which results in an equivalent
  371. // `clang_main` call.
  372. //
  373. // Also note that we only do `-disable-free` filtering in the in-process
  374. // execution here, as subprocesses leaking memory won't impact this process.
  375. auto cc1_main = [this, enable_leaking](
  376. llvm::SmallVectorImpl<const char*>& cc1_args) -> int {
  377. return RunClangCC1(cc1_args, enable_leaking);
  378. };
  379. driver.CC1Main = cc1_main;
  380. std::unique_ptr<clang::driver::Compilation> compilation(
  381. driver.BuildCompilation(cstr_args));
  382. CARBON_CHECK(compilation, "Should always successfully allocate!");
  383. if (compilation->containsError()) {
  384. // These should have been diagnosed by the driver.
  385. return false;
  386. }
  387. // Make sure our target detection matches Clang's. Sadly, we can't just
  388. // reuse Clang's as it is available too late.
  389. // TODO: Use nice diagnostics here rather than a check failure.
  390. CARBON_CHECK(llvm::Triple(target) == llvm::Triple(driver.getTargetTriple()),
  391. "Mismatch between the expected target '{0}' and the one "
  392. "computed by Clang '{1}'",
  393. target, driver.getTargetTriple());
  394. CARBON_VLOG("Running Clang driver...\n");
  395. result = driver.ExecuteCompilation(*compilation, failing_commands);
  396. CARBON_VLOG("Execution result code: {0}\n", result);
  397. for (const auto& [command_result, failing_command] : failing_commands) {
  398. CARBON_VLOG("Failing command '{0}' with code '{1}' was:\n",
  399. failing_command->getExecutable(), command_result);
  400. if (vlog_stream_) {
  401. failing_command->Print(*vlog_stream_, "\n\n", /*Quote=*/true);
  402. }
  403. }
  404. // Return whether the command was executed successfully.
  405. return result == 0 && failing_commands.empty();
  406. }
  407. } // namespace Carbon