clang_runner.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #ifndef CARBON_TOOLCHAIN_DRIVER_CLANG_RUNNER_H_
  5. #define CARBON_TOOLCHAIN_DRIVER_CLANG_RUNNER_H_
  6. #include <filesystem>
  7. #include "clang/Basic/DiagnosticIDs.h"
  8. #include "common/error.h"
  9. #include "common/ostream.h"
  10. #include "llvm/ADT/ArrayRef.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/Support/ThreadPool.h"
  13. #include "llvm/Support/VirtualFileSystem.h"
  14. #include "llvm/TargetParser/Triple.h"
  15. #include "toolchain/driver/runtimes_cache.h"
  16. #include "toolchain/driver/tool_runner_base.h"
  17. #include "toolchain/install/install_paths.h"
  18. namespace Carbon {
  19. // Runs Clang in a similar fashion to invoking it with the provided arguments on
  20. // the command line. We use a textual command line interface to allow easily
  21. // incorporating custom command line flags from user invocations that we don't
  22. // parse, but will pass transparently along to Clang itself.
  23. //
  24. // This class is thread safe, allowing multiple threads to share a single runner
  25. // and concurrently invoke Clang.
  26. //
  27. // This doesn't literally use a subprocess to invoke Clang; it instead tries to
  28. // directly use the Clang command line driver library. We also work to simplify
  29. // how that driver operates and invoke it in an opinionated way to get the best
  30. // behavior for our expected use cases in the Carbon driver:
  31. //
  32. // - Ensure thread-safe invocation of Clang to enable concurrent usage.
  33. // - Minimize canonicalization of file names to try to preserve the paths as
  34. // users type them.
  35. // - Minimize the use of subprocess invocations which are expensive on some
  36. // operating systems. To the extent possible, we try to directly invoke the
  37. // Clang logic within this process.
  38. // - Provide programmatic API to control defaults of Clang. For example, causing
  39. // verbose output.
  40. //
  41. // Note that this makes the current process behave like running Clang -- it uses
  42. // standard output and standard error, and otherwise can only read and write
  43. // files based on their names described in the arguments. It doesn't provide any
  44. // higher-level abstraction such as streams for inputs or outputs.
  45. //
  46. // TODO: Switch the diagnostic machinery to buffer and do locked output so that
  47. // concurrent invocations of Clang don't intermingle their diagnostic output.
  48. //
  49. // TODO: If support for thread-local overrides of `llvm::errs` and `llvm::outs`
  50. // becomes available upstream, also buffer and synchronize those streams to
  51. // further improve the behavior of concurrent invocations.
  52. class ClangRunner : ToolRunnerBase {
  53. public:
  54. // Build a Clang runner that uses the provided installation and filesystem.
  55. //
  56. // Optionally accepts a `vlog_stream` to enable verbose logging from Carbon to
  57. // that stream. The verbose output from Clang goes to stderr regardless.
  58. ClangRunner(const InstallPaths* install_paths,
  59. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
  60. llvm::raw_ostream* vlog_stream = nullptr);
  61. // Run Clang with the provided arguments and a runtime cache for on-demand
  62. // runtime building.
  63. //
  64. // This works to support all of the Clang commandline, including commands that
  65. // use target-dependent resources like linking. When it detects such commands,
  66. // it will use runtimes from the provided cache. If not available in the
  67. // cache, it will build the necessary runtimes using the provided thread pool
  68. // both to use and incorporate into the cache.
  69. //
  70. // Returns an error only if unable to successfully run Clang with the
  71. // arguments. If able to run Clang, no error is returned a bool indicating
  72. // whether than Clang invocation succeeded is returned.
  73. auto Run(llvm::ArrayRef<llvm::StringRef> args,
  74. Runtimes::Cache& runtimes_cache,
  75. llvm::ThreadPoolInterface& runtimes_build_thread_pool)
  76. -> ErrorOr<bool>;
  77. // Run Clang with the provided arguments and prebuilt runtimes.
  78. //
  79. // Similar to `Run`, but requires and uses pre-built runtimes rather than a
  80. // cache or building them on demand.
  81. auto RunWithPrebuiltRuntimes(llvm::ArrayRef<llvm::StringRef> args,
  82. Runtimes& prebuilt_runtimes) -> ErrorOr<bool>;
  83. // Run Clang with the provided arguments and without any target runtimes.
  84. //
  85. // This method can be used to avoid building target-dependent resources when
  86. // unnecessary, but not all Clang command lines will work correctly.
  87. // Specifically, compile-only commands will typically work, while linking will
  88. // not.
  89. //
  90. // This function simply returns true or false depending on whether Clang runs
  91. // successfully, as it should display any needed error messages.
  92. auto RunWithNoRuntimes(llvm::ArrayRef<llvm::StringRef> args) -> bool;
  93. // Builds the target-specific resource directory for Clang.
  94. //
  95. // There is a resource directory installed along side the Clang binary that
  96. // contains all the target independent files such as headers. However, for
  97. // target-specific files like runtimes, we build those on demand here and
  98. // return the path.
  99. auto BuildTargetResourceDir(const Runtimes::Cache::Features& features,
  100. Runtimes& runtimes,
  101. const std::filesystem::path& tmp_path,
  102. llvm::ThreadPoolInterface& threads)
  103. -> ErrorOr<std::filesystem::path>;
  104. // Enable leaking memory.
  105. //
  106. // Clang can avoid deallocating some of its memory to improve compile time.
  107. // However, this isn't compatible with library-based invocations. When using
  108. // the runner in a context where memory leaks are acceptable, such as from a
  109. // command line driver, you can use this to enable that leaking behavior. Note
  110. // that this will not override _explicit_ `args` in a run invocation that
  111. // cause leaking, it will merely disable Clang's libraries injecting that
  112. // behavior.
  113. auto EnableLeakingMemory() -> void { enable_leaking_ = true; }
  114. private:
  115. // Emulates `cc1_main` but in a way that doesn't assume it is running in the
  116. // main thread and can more easily fit into library calls to do compiles.
  117. //
  118. // TODO: Much of the logic here should be factored out of the CC1
  119. // implementation in Clang's driver and into a reusable part of its libraries.
  120. // That should allow reducing the code here to a minimal amount.
  121. auto RunCC1(llvm::SmallVectorImpl<const char*>& cc1_args) -> int;
  122. // Handles building the Clang driver and passing the arguments down to it.
  123. auto RunInternal(llvm::ArrayRef<llvm::StringRef> args, llvm::StringRef target,
  124. std::optional<llvm::StringRef> target_resource_dir_path)
  125. -> bool;
  126. // Helper to compile a single file of the CRT runtimes.
  127. auto BuildCrtFile(llvm::StringRef target, llvm::StringRef src_file,
  128. const std::filesystem::path& out_path) -> void;
  129. // Returns the target-specific source files for the builtins runtime library.
  130. auto CollectBuiltinsSrcFiles(const llvm::Triple& target_triple)
  131. -> llvm::SmallVector<llvm::StringRef>;
  132. // Helper to compile a single file of the compiler builtins runtimes.
  133. auto BuildBuiltinsFile(llvm::StringRef target, llvm::StringRef src_file,
  134. const std::filesystem::path& out_path) -> void;
  135. // Builds the builtins runtime library into the provided archive file path,
  136. // using the provided objects path for intermediate object files.
  137. auto BuildBuiltinsLib(llvm::StringRef target,
  138. const llvm::Triple& target_triple,
  139. const std::filesystem::path& tmp_path,
  140. Filesystem::DirRef lib_dir,
  141. llvm::ThreadPoolInterface& threads) -> ErrorOr<Success>;
  142. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs_;
  143. bool enable_leaking_ = false;
  144. };
  145. } // namespace Carbon
  146. #endif // CARBON_TOOLCHAIN_DRIVER_CLANG_RUNNER_H_