clang_runner.h 6.1 KB

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