clang_runner.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. 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. // Check the args to see if we have a known target-independent command. If so,
  171. // directly dispatch it to avoid the cost of building the target resource
  172. // directory.
  173. // TODO: Maybe handle response file expansion similar to the Clang CLI?
  174. if (args.empty() || args[0].starts_with("-cc1") || IsNonLinkCommand(args)) {
  175. return RunWithNoRuntimes(args, enable_leaking);
  176. }
  177. std::string target = ComputeClangTarget(args);
  178. Runtimes::Cache::Features features = {.target = target};
  179. CARBON_ASSIGN_OR_RETURN(Runtimes runtimes, runtimes_cache.Lookup(features));
  180. // We need to build the Clang resource directory for these runtimes. This
  181. // requires a temporary directory as well as the destination directory for
  182. // the build. The temporary directory should only be used during the build,
  183. // not once we are running Clang with the built runtime.
  184. CARBON_VLOG("Building target resource dir...\n");
  185. ClangResourceDirBuilder builder(this, &runtimes_build_thread_pool,
  186. llvm::Triple(features.target), &runtimes);
  187. ClangArchiveRuntimesBuilder<Runtimes::LibUnwind> lib_unwind_builder(
  188. this, &runtimes_build_thread_pool, llvm::Triple(features.target),
  189. &runtimes);
  190. ClangArchiveRuntimesBuilder<Runtimes::Libcxx> libcxx_builder(
  191. this, &runtimes_build_thread_pool, llvm::Triple(features.target),
  192. &runtimes);
  193. CARBON_ASSIGN_OR_RETURN(std::filesystem::path resource_dir_path,
  194. std::move(builder).Wait());
  195. CARBON_ASSIGN_OR_RETURN(std::filesystem::path libunwind_path,
  196. std::move(lib_unwind_builder).Wait());
  197. CARBON_ASSIGN_OR_RETURN(std::filesystem::path libcxx_path,
  198. std::move(libcxx_builder).Wait());
  199. // Note that this function always successfully runs `clang` and returns a bool
  200. // to indicate whether `clang` itself succeeded, not whether the runner was
  201. // able to run it. As a consequence, even a `false` here is a non-`Error`
  202. // return.
  203. return RunInternal(args, target, resource_dir_path.native(),
  204. std::move(libunwind_path), std::move(libcxx_path),
  205. enable_leaking);
  206. }
  207. auto ClangRunner::RunWithNoRuntimes(llvm::ArrayRef<llvm::StringRef> args,
  208. bool enable_leaking) -> bool {
  209. std::string target = ComputeClangTarget(args);
  210. return RunInternal(args, target, /*target_resource_dir_path=*/std::nullopt,
  211. /*libunwind_path=*/std::nullopt,
  212. /*libcxx_path=*/std::nullopt, enable_leaking);
  213. }
  214. auto ClangRunner::RunInternal(
  215. llvm::ArrayRef<llvm::StringRef> args, llvm::StringRef target,
  216. std::optional<llvm::StringRef> target_resource_dir_path,
  217. std::optional<std::filesystem::path> libunwind_path,
  218. std::optional<std::filesystem::path> libcxx_path, bool enable_leaking)
  219. -> bool {
  220. llvm::BumpPtrAllocator alloc;
  221. // Handle special dispatch for CC1 commands as they don't use the driver and
  222. // we don't synthesize any default arguments there.
  223. if (!args.empty() && args[0].starts_with("-cc1")) {
  224. llvm::SmallVector<const char*, 64> cstr_args =
  225. BuildCStrArgs(clang_path_.native(), args, alloc);
  226. if (args[0] == "-cc1") {
  227. CARBON_VLOG("Dispatching `-cc1` command line...");
  228. int exit_code =
  229. RunClangCC1(*installation_, fs_, cstr_args, enable_leaking);
  230. // TODO: Should this be forwarding the full exit code?
  231. return exit_code == 0;
  232. }
  233. // Other CC1-based invocations need to dispatch into the `clang_main`
  234. // routine to work correctly. This means they're not reliable in a library
  235. // context but currently there is too much logic to reasonably extract here.
  236. // This at least allows simple cases (often when directly used on the
  237. // command line) to work correctly.
  238. //
  239. // TODO: Factor the relevant code paths into a library API or move this into
  240. // the busybox dispatch logic.
  241. CARBON_VLOG("Calling clang_main for a cc1-based invocation...");
  242. // cstr_args[0] will be the `clang_path` so we don't need the prepend arg.
  243. llvm::ToolContext tool_context = {
  244. .Path = cstr_args[0], .PrependArg = "clang", .NeedsPrependArg = false};
  245. int exit_code = clang_main(
  246. cstr_args.size(), const_cast<char**>(cstr_args.data()), tool_context);
  247. // TODO: Should this be forwarding the full exit code?
  248. return exit_code == 0;
  249. }
  250. // We start with a custom prefix of arguments to establish Carbon's default
  251. // configuration for invoking Clang. These may not all be needed for all
  252. // invocations, so we also suppress warnings about any that are ignored.
  253. llvm::SmallVector<std::string> prefix_args;
  254. prefix_args.push_back("--start-no-unused-arguments");
  255. AppendDefaultClangArgs(*installation_, target, prefix_args);
  256. // We don't have a direct way to configure the linker search paths in the
  257. // Clang driver outside of command line flags, so we inject them here with
  258. // flags. Note that we only inject these as _search_ paths to allow the normal
  259. // linking rules to govern whether or not to link a given library. We also
  260. // build our runtimes exclusively as static archives so we don't need to use
  261. // command line flags to force static runtime linking to occur.
  262. if (libunwind_path) {
  263. prefix_args.push_back(
  264. llvm::formatv("-L{0}/lib", *std::move(libunwind_path)).str());
  265. }
  266. if (libcxx_path) {
  267. prefix_args.push_back(
  268. llvm::formatv("-L{0}/lib", std::move(libcxx_path)).str());
  269. }
  270. prefix_args.push_back("--end-no-unused-arguments");
  271. // Rebuild the args as C-string args.
  272. llvm::SmallVector<const char*, 64> cstr_args =
  273. BuildCStrArgs(clang_path_.native(), prefix_args, args, alloc);
  274. CARBON_VLOG("Running Clang driver with the following arguments:\n");
  275. for (const char* cstr_arg : llvm::ArrayRef(cstr_args)) {
  276. CARBON_VLOG(" '{0}'\n", cstr_arg);
  277. }
  278. // Create the diagnostic options and parse arguments controlling them out of
  279. // our arguments.
  280. std::unique_ptr<clang::DiagnosticOptions> diagnostic_options =
  281. clang::CreateAndPopulateDiagOpts(cstr_args);
  282. // TODO: We don't yet support serializing diagnostics the way the actual
  283. // `clang` command line does. Unclear if we need to or not, but it would need
  284. // a bit more logic here to set up chained consumers.
  285. clang::TextDiagnosticPrinter diagnostic_client(llvm::errs(),
  286. *diagnostic_options);
  287. // Note that the `DiagnosticsEngine` takes ownership (via a ref count) of the
  288. // DiagnosticIDs, unlike the other parameters.
  289. clang::DiagnosticsEngine diagnostics(clang::DiagnosticIDs::create(),
  290. *diagnostic_options, &diagnostic_client,
  291. /*ShouldOwnClient=*/false);
  292. clang::ProcessWarningOptions(diagnostics, *diagnostic_options, *fs_);
  293. // Note that we configure the driver's *default* target here, not the expected
  294. // target as that will be parsed out of the command line below.
  295. clang::driver::Driver driver(clang_path_.native(),
  296. llvm::sys::getDefaultTargetTriple(), diagnostics,
  297. "clang LLVM compiler", fs_);
  298. llvm::Triple target_triple(target);
  299. // We need to set an SDK system root on macOS by default. Setting it here
  300. // allows a custom sysroot to still be specified on the command line.
  301. //
  302. // TODO: A different system root should be used for iOS, watchOS, tvOS.
  303. // Currently, we're only targeting macOS support though.
  304. if (target_triple.isMacOSX()) {
  305. // This is the default CLT system root, shown by `xcrun --show-sdk-path`.
  306. // We hard code it here to avoid the overhead of subprocessing to `xcrun` on
  307. // each Clang invocation, but this may need to be updated to search or
  308. // reflect macOS versions if this changes in the future.
  309. driver.SysRoot = "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk";
  310. }
  311. // If we have a target-specific resource directory, set it as the default
  312. // here, otherwise use the installation's resource directory.
  313. driver.ResourceDir = target_resource_dir_path
  314. ? target_resource_dir_path->str()
  315. : installation_->clang_resource_path().native();
  316. // Configure the install directory to find other tools and data files.
  317. //
  318. // We directly override the detected directory as we use a synthetic path
  319. // above. This makes it appear that our binary was in the installed binaries
  320. // directory, and allows finding tools relative to it.
  321. driver.Dir = installation_->llvm_install_bin();
  322. CARBON_VLOG("Setting bin directory to: {0}\n", driver.Dir);
  323. // When there's only one command being run, this will run it in-process.
  324. // However, a `clang` invocation may cause multiple `cc1` invocations, which
  325. // still subprocess. See `InProcess` comment at:
  326. // https://github.com/llvm/llvm-project/blob/86ce8e4504c06ecc3cc42f002ad4eb05cac10925/clang/lib/Driver/Job.cpp#L411-L413
  327. //
  328. // Note the subprocessing will effectively call `clang -cc1`, which turns into
  329. // `carbon-busybox clang -cc1`, which results in an equivalent `clang_main`
  330. // call.
  331. //
  332. // Also note that we only do `-disable-free` filtering in the in-process
  333. // execution here, as subprocesses leaking memory won't impact this process.
  334. auto cc1_main = [this, enable_leaking](
  335. llvm::SmallVectorImpl<const char*>& cc1_args) -> int {
  336. return RunClangCC1(*installation_, fs_, cc1_args, enable_leaking);
  337. };
  338. driver.CC1Main = cc1_main;
  339. std::unique_ptr<clang::driver::Compilation> compilation(
  340. driver.BuildCompilation(cstr_args));
  341. CARBON_CHECK(compilation, "Should always successfully allocate!");
  342. if (compilation->containsError()) {
  343. // These should have been diagnosed by the driver.
  344. return false;
  345. }
  346. // Make sure our target detection matches Clang's. Sadly, we can't just reuse
  347. // Clang's as it is available too late.
  348. // TODO: Use nice diagnostics here rather than a check failure.
  349. CARBON_CHECK(llvm::Triple(target) == llvm::Triple(driver.getTargetTriple()),
  350. "Mismatch between the expected target '{0}' and the one "
  351. "computed by Clang '{1}'",
  352. target, driver.getTargetTriple());
  353. CARBON_VLOG("Running Clang driver...\n");
  354. llvm::SmallVector<std::pair<int, const clang::driver::Command*>>
  355. failing_commands;
  356. int result = driver.ExecuteCompilation(*compilation, failing_commands);
  357. // Finish diagnosing any failures before we verbosely log the source of those
  358. // failures.
  359. diagnostic_client.finish();
  360. CARBON_VLOG("Execution result code: {0}\n", result);
  361. for (const auto& [command_result, failing_command] : failing_commands) {
  362. CARBON_VLOG("Failing command '{0}' with code '{1}' was:\n",
  363. failing_command->getExecutable(), command_result);
  364. if (vlog_stream_) {
  365. failing_command->Print(*vlog_stream_, "\n\n", /*Quote=*/true);
  366. }
  367. }
  368. // Return whether the command was executed successfully.
  369. return result == 0 && failing_commands.empty();
  370. }
  371. } // namespace Carbon