clang_runner.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <optional>
  8. #include "common/error.h"
  9. #include "common/ostream.h"
  10. #include "llvm/ADT/ArrayRef.h"
  11. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  12. #include "llvm/ADT/SmallVector.h"
  13. #include "llvm/ADT/StringRef.h"
  14. #include "llvm/Support/Allocator.h"
  15. #include "llvm/Support/ThreadPool.h"
  16. #include "llvm/Support/VirtualFileSystem.h"
  17. #include "llvm/TargetParser/Triple.h"
  18. #include "toolchain/base/install_paths.h"
  19. #include "toolchain/driver/runtimes_cache.h"
  20. #include "toolchain/driver/tool_runner_base.h"
  21. namespace Carbon {
  22. // Runs Clang in a similar fashion to invoking it with the provided arguments on
  23. // the command line. We use a textual command line interface to allow easily
  24. // incorporating custom command line flags from user invocations that we don't
  25. // parse, but will pass transparently along to Clang itself.
  26. //
  27. // This class is thread safe, allowing multiple threads to share a single runner
  28. // and concurrently invoke Clang.
  29. //
  30. // This doesn't literally use a subprocess to invoke Clang; it instead tries to
  31. // directly use the Clang command line driver library. We also work to simplify
  32. // how that driver operates and invoke it in an opinionated way to get the best
  33. // behavior for our expected use cases in the Carbon driver:
  34. //
  35. // - Ensure thread-safe invocation of Clang to enable concurrent usage.
  36. // - Minimize canonicalization of file names to try to preserve the paths as
  37. // users type them.
  38. // - Minimize the use of subprocess invocations which are expensive on some
  39. // operating systems. To the extent possible, we try to directly invoke the
  40. // Clang logic within this process.
  41. // - Provide programmatic API to control defaults of Clang. For example, causing
  42. // verbose output.
  43. //
  44. // Note that this makes the current process behave like running Clang -- it uses
  45. // standard output and standard error, and otherwise can only read and write
  46. // files based on their names described in the arguments. It doesn't provide any
  47. // higher-level abstraction such as streams for inputs or outputs.
  48. //
  49. // TODO: Switch the diagnostic machinery to buffer and do locked output so that
  50. // concurrent invocations of Clang don't intermingle their diagnostic output.
  51. //
  52. // TODO: If support for thread-local overrides of `llvm::errs` and `llvm::outs`
  53. // becomes available upstream, also buffer and synchronize those streams to
  54. // further improve the behavior of concurrent invocations.
  55. class ClangRunner : ToolRunnerBase {
  56. public:
  57. // Build a Clang runner that uses the provided installation and filesystem.
  58. //
  59. // Optionally accepts a `vlog_stream` to enable verbose logging from Carbon to
  60. // that stream. The verbose output from Clang goes to stderr regardless.
  61. ClangRunner(const InstallPaths* install_paths,
  62. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
  63. llvm::raw_ostream* vlog_stream = nullptr,
  64. std::optional<std::filesystem::path> override_clang_path = {});
  65. // Run Clang with the provided arguments and a runtime cache for on-demand
  66. // runtime building.
  67. //
  68. // This works to support all of the Clang commandline, including commands that
  69. // use target-dependent resources like linking. When it detects such commands,
  70. // it will use runtimes from the provided cache. If not available in the
  71. // cache, it will build the necessary runtimes using the provided thread pool
  72. // both to use and incorporate into the cache.
  73. //
  74. // Returns an error only if unable to successfully run Clang with the
  75. // arguments. If able to run Clang, no error is returned, and a bool
  76. // indicating whether than Clang invocation succeeded is returned.
  77. auto Run(llvm::ArrayRef<llvm::StringRef> args,
  78. Runtimes::Cache& runtimes_cache,
  79. llvm::ThreadPoolInterface& runtimes_build_thread_pool,
  80. bool enable_leaking = false) -> ErrorOr<bool>;
  81. // Run Clang with the provided arguments and prebuilt runtimes.
  82. //
  83. // Similar to `Run`, but requires and uses pre-built runtimes rather than a
  84. // cache or building them on demand.
  85. auto RunWithPrebuiltRuntimes(llvm::ArrayRef<llvm::StringRef> args,
  86. Runtimes& prebuilt_runtimes,
  87. bool enable_leaking = false) -> ErrorOr<bool>;
  88. // Run Clang with the provided arguments and without any target runtimes.
  89. //
  90. // Similar to `Run`, but omits any target runtimes.
  91. //
  92. // This method can be used to avoid building target-dependent resources when
  93. // unnecessary, but not all Clang command lines will work correctly.
  94. // Specifically, compile-only commands will typically work, while linking will
  95. // not.
  96. auto RunWithNoRuntimes(llvm::ArrayRef<llvm::StringRef> args,
  97. bool enable_leaking = false) -> ErrorOr<bool>;
  98. private:
  99. friend class ClangRuntimesBuilderBase;
  100. auto RunClangCC1(llvm::SmallVectorImpl<const char*>& cstr_args,
  101. bool enable_leaking) -> int;
  102. // Handles building the Clang driver and passing the arguments down to it.
  103. auto RunInternal(llvm::ArrayRef<llvm::StringRef> args, llvm::StringRef target,
  104. std::optional<llvm::StringRef> target_resource_dir_path,
  105. std::optional<std::filesystem::path> libunwind_path,
  106. std::optional<std::filesystem::path> libcxx_path,
  107. bool link_runtime_libs, bool enable_leaking)
  108. -> ErrorOr<bool>;
  109. // Returns the target-specific source files for the builtins runtime library.
  110. auto CollectBuiltinsSrcFiles(const llvm::Triple& target_triple)
  111. -> llvm::SmallVector<llvm::StringRef>;
  112. // Builds the builtins runtime library into the provided archive file path,
  113. // using the provided objects path for intermediate object files.
  114. auto BuildBuiltinsLib(llvm::StringRef target,
  115. const llvm::Triple& target_triple,
  116. const std::filesystem::path& runtimes_path,
  117. llvm::ThreadPoolTaskGroup& threads) -> ErrorOr<Success>;
  118. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs_;
  119. std::filesystem::path clang_path_;
  120. };
  121. } // namespace Carbon
  122. #endif // CARBON_TOOLCHAIN_DRIVER_CLANG_RUNNER_H_