clang_runner.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // Run Clang with the provided arguments and a runtime cache for on-demand
  64. // runtime building.
  65. //
  66. // This works to support all of the Clang commandline, including commands that
  67. // use target-dependent resources like linking. When it detects such commands,
  68. // it will use runtimes from the provided cache. If not available in the
  69. // cache, it will build the necessary runtimes using the provided thread pool
  70. // both to use and incorporate into the cache.
  71. //
  72. // Returns an error only if unable to successfully run Clang with the
  73. // arguments. If able to run Clang, no error is returned a bool indicating
  74. // whether than Clang invocation succeeded is returned.
  75. auto Run(llvm::ArrayRef<llvm::StringRef> args,
  76. Runtimes::Cache& runtimes_cache,
  77. llvm::ThreadPoolInterface& runtimes_build_thread_pool,
  78. bool enable_leaking = false) -> ErrorOr<bool>;
  79. // Run Clang with the provided arguments and prebuilt runtimes.
  80. //
  81. // Similar to `Run`, but requires and uses pre-built runtimes rather than a
  82. // cache or building them on demand.
  83. auto RunWithPrebuiltRuntimes(llvm::ArrayRef<llvm::StringRef> args,
  84. Runtimes& prebuilt_runtimes,
  85. bool enable_leaking = false) -> ErrorOr<bool>;
  86. // Run Clang with the provided arguments and without any target runtimes.
  87. //
  88. // This method can be used to avoid building target-dependent resources when
  89. // unnecessary, but not all Clang command lines will work correctly.
  90. // Specifically, compile-only commands will typically work, while linking will
  91. // not.
  92. //
  93. // This function simply returns true or false depending on whether Clang runs
  94. // successfully, as it should display any needed error messages.
  95. auto RunWithNoRuntimes(llvm::ArrayRef<llvm::StringRef> args,
  96. bool enable_leaking = false) -> 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. bool enable_leaking) -> bool;
  103. // Returns the target-specific source files for the builtins runtime library.
  104. auto CollectBuiltinsSrcFiles(const llvm::Triple& target_triple)
  105. -> llvm::SmallVector<llvm::StringRef>;
  106. // Builds the builtins runtime library into the provided archive file path,
  107. // using the provided objects path for intermediate object files.
  108. auto BuildBuiltinsLib(llvm::StringRef target,
  109. const llvm::Triple& target_triple,
  110. const std::filesystem::path& runtimes_path,
  111. llvm::ThreadPoolTaskGroup& threads) -> ErrorOr<Success>;
  112. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs_;
  113. };
  114. } // namespace Carbon
  115. #endif // CARBON_TOOLCHAIN_DRIVER_CLANG_RUNNER_H_