clang_runner.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/VirtualFileSystem.h"
  13. #include "llvm/TargetParser/Triple.h"
  14. #include "toolchain/driver/tool_runner_base.h"
  15. #include "toolchain/install/install_paths.h"
  16. namespace Carbon {
  17. // Runs Clang in a similar fashion to invoking it with the provided arguments on
  18. // the command line. We use a textual command line interface to allow easily
  19. // incorporating custom command line flags from user invocations that we don't
  20. // parse, but will pass transparently along to Clang itself.
  21. //
  22. // This doesn't literally use a subprocess to invoke Clang; it instead tries to
  23. // directly use the Clang command line driver library. We also work to simplify
  24. // how that driver operates and invoke it in an opinionated way to get the best
  25. // behavior for our expected use cases in the Carbon driver:
  26. //
  27. // - Minimize canonicalization of file names to try to preserve the paths as
  28. // users type them.
  29. // - Minimize the use of subprocess invocations which are expensive on some
  30. // operating systems. To the extent possible, we try to directly invoke the
  31. // Clang logic within this process.
  32. // - Provide programmatic API to control defaults of Clang. For example, causing
  33. // verbose output.
  34. //
  35. // Note that this makes the current process behave like running Clang -- it uses
  36. // standard output and standard error, and otherwise can only read and write
  37. // files based on their names described in the arguments. It doesn't provide any
  38. // higher-level abstraction such as streams for inputs or outputs.
  39. class ClangRunner : ToolRunnerBase {
  40. public:
  41. // Build a Clang runner that uses the provided `exe_name` and `err_stream`.
  42. //
  43. // If `verbose` is passed as true, will enable verbose logging to the
  44. // `err_stream` both from the runner and Clang itself.
  45. ClangRunner(const InstallPaths* install_paths,
  46. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
  47. llvm::raw_ostream* vlog_stream = nullptr,
  48. bool build_runtimes_on_demand = false);
  49. // Run Clang with the provided arguments.
  50. //
  51. // This works to support all of the Clang commandline, including commands that
  52. // use target-dependent resources like linking. When it detects such commands,
  53. // it will either use the provided target resource-dir path, or if building
  54. // runtimes on demand is enabled it will build the needed resource-dir.
  55. //
  56. // Returns an error only if unable to successfully run Clang with the
  57. // arguments. If able to run Clang, no error is returned a bool indicating
  58. // whether than Clang invocation succeeded is returned.
  59. //
  60. // TODO: Eventually, this will need to accept an abstraction that can
  61. // represent multiple different pre-built runtimes.
  62. auto Run(llvm::ArrayRef<llvm::StringRef> args,
  63. std::optional<std::filesystem::path> prebuilt_resource_dir_path = {})
  64. -> ErrorOr<bool>;
  65. // Run Clang with the provided arguments and without any target-dependent
  66. // resources.
  67. //
  68. // This method can be used to avoid building target-dependent resources when
  69. // unnecessary, but not all Clang command lines will work correctly.
  70. // Specifically, compile-only commands will typically work, while linking will
  71. // not.
  72. auto RunTargetIndependentCommand(llvm::ArrayRef<llvm::StringRef> args)
  73. -> bool;
  74. // Builds the target-specific resource directory for Clang.
  75. //
  76. // There is a resource directory installed along side the Clang binary that
  77. // contains all the target independent files such as headers. However, for
  78. // target-specific files like runtimes, we build those on demand here and
  79. // return the path.
  80. auto BuildTargetResourceDir(llvm::StringRef target,
  81. const std::filesystem::path& resource_dir_path,
  82. const std::filesystem::path& tmp_path)
  83. -> ErrorOr<Success>;
  84. // Enable leaking memory.
  85. //
  86. // Clang can avoid deallocating some of its memory to improve compile time.
  87. // However, this isn't compatible with library-based invocations. When using
  88. // the runner in a context where memory leaks are acceptable, such as from a
  89. // command line driver, you can use this to enable that leaking behavior. Note
  90. // that this will not override _explicit_ `args` in a run invocation that
  91. // cause leaking, it will merely disable Clang's libraries injecting that
  92. // behavior.
  93. auto EnableLeakingMemory() -> void { enable_leaking_ = true; }
  94. private:
  95. // Handles building the Clang driver and passing the arguments down to it.
  96. auto RunInternal(llvm::ArrayRef<llvm::StringRef> args, llvm::StringRef target,
  97. std::optional<llvm::StringRef> target_resource_dir_path)
  98. -> bool;
  99. // Helper to compile a single file of the CRT runtimes.
  100. auto BuildCrtFile(llvm::StringRef target, llvm::StringRef src_file,
  101. const std::filesystem::path& out_path) -> void;
  102. // Returns the target-specific source files for the builtins runtime library.
  103. auto CollectBuiltinsSrcFiles(const llvm::Triple& target_triple)
  104. -> llvm::SmallVector<llvm::StringRef>;
  105. // Helper to compile a single file of the compiler builtins runtimes.
  106. auto BuildBuiltinsFile(llvm::StringRef target, llvm::StringRef src_file,
  107. const std::filesystem::path& out_path) -> void;
  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& tmp_path,
  113. Filesystem::DirRef lib_dir) -> ErrorOr<Success>;
  114. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs_;
  115. llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagnostic_ids_;
  116. std::optional<std::filesystem::path> prebuilt_runtimes_path_;
  117. bool build_runtimes_on_demand_;
  118. bool enable_leaking_ = false;
  119. };
  120. } // namespace Carbon
  121. #endif // CARBON_TOOLCHAIN_DRIVER_CLANG_RUNNER_H_