build_runtimes_subcommand.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #include "toolchain/driver/build_runtimes_subcommand.h"
  5. #include "llvm/TargetParser/Triple.h"
  6. #include "toolchain/driver/clang_runner.h"
  7. #include "toolchain/driver/clang_runtimes.h"
  8. namespace Carbon {
  9. auto BuildRuntimesOptions::Build(CommandLine::CommandBuilder& b) -> void {
  10. b.AddStringOption(
  11. {
  12. .name = "output-directory",
  13. .value_name = "DIR",
  14. .help = R"""(
  15. The directory to populate with runtime libraries suitable for the selected code
  16. generation options.
  17. )""",
  18. },
  19. [&](auto& arg_b) { arg_b.Set(&directory); });
  20. b.AddFlag(
  21. {
  22. .name = "force",
  23. .help = R"""(
  24. Force re-creating the provided output path from scratch
  25. This will **remove** the provided output path and re-create it from scratch.
  26. )""",
  27. },
  28. [&](CommandLine::FlagBuilder& arg_b) { arg_b.Set(&force); });
  29. codegen_options.Build(b);
  30. }
  31. static constexpr CommandLine::CommandInfo SubcommandInfo = {
  32. .name = "build-runtimes",
  33. .help = R"""(
  34. Build Carbon's runtime libraries.
  35. This subcommand builds Carbon's runtime libraries for a particular code
  36. generation target, either in their default location or a specified one.
  37. Running this command directly is not necessary as Carbon will build and cache
  38. runtimes as needed when linking, but building them directly can aid in
  39. debugging issues or allow them to be prebuilt, possibly with customized code
  40. generation flags, and used explicitly when linking.
  41. )""",
  42. };
  43. BuildRuntimesSubcommand::BuildRuntimesSubcommand()
  44. : DriverSubcommand(SubcommandInfo) {}
  45. auto BuildRuntimesSubcommand::Run(DriverEnv& driver_env) -> DriverResult {
  46. // Don't run Clang when fuzzing, it is known to not be reliable under fuzzing
  47. // due to many unfixed issues.
  48. if (TestAndDiagnoseIfFuzzingExternalLibraries(driver_env, "clang")) {
  49. return {.success = false};
  50. }
  51. // For diagnosing filesystem or other errors when building runtimes.
  52. CARBON_DIAGNOSTIC(FailureBuildingRuntimes, Error,
  53. "failure building runtimes: {0}", std::string);
  54. auto run_result = RunInternal(driver_env);
  55. if (!run_result.ok()) {
  56. driver_env.emitter.Emit(FailureBuildingRuntimes,
  57. run_result.error().message());
  58. return {.success = false};
  59. }
  60. llvm::outs() << "Built runtimes: " << *run_result << "\n";
  61. return {.success = true};
  62. }
  63. auto BuildRuntimesSubcommand::RunInternal(DriverEnv& driver_env)
  64. -> ErrorOr<std::filesystem::path> {
  65. ClangRunner runner(driver_env.installation, driver_env.fs,
  66. driver_env.vlog_stream);
  67. Runtimes::Cache::Features features = {
  68. .target = options_.codegen_options.target.str()};
  69. bool is_cache = options_.directory.empty();
  70. std::filesystem::path output_path = options_.directory.str();
  71. CARBON_ASSIGN_OR_RETURN(
  72. auto runtimes, is_cache
  73. ? driver_env.runtimes_cache.Lookup(features)
  74. : Runtimes::Make(output_path, driver_env.vlog_stream));
  75. if (options_.force) {
  76. // Remove existing runtimes to force a rebuild.
  77. CARBON_RETURN_IF_ERROR(runtimes.Remove(Runtimes::ClangResourceDir));
  78. CARBON_RETURN_IF_ERROR(runtimes.Remove(Runtimes::LibUnwind));
  79. CARBON_RETURN_IF_ERROR(runtimes.Remove(Runtimes::Libcxx));
  80. }
  81. ClangResourceDirBuilder resource_dir_builder(&runner, driver_env.thread_pool,
  82. llvm::Triple(features.target),
  83. &runtimes);
  84. ClangArchiveRuntimesBuilder<Runtimes::LibUnwind> lib_unwind_builder(
  85. &runner, driver_env.thread_pool, llvm::Triple(features.target),
  86. &runtimes);
  87. ClangArchiveRuntimesBuilder<Runtimes::Libcxx> libcxx_builder(
  88. &runner, driver_env.thread_pool, llvm::Triple(features.target),
  89. &runtimes);
  90. CARBON_RETURN_IF_ERROR(std::move(resource_dir_builder).Wait());
  91. CARBON_RETURN_IF_ERROR(std::move(lib_unwind_builder).Wait());
  92. CARBON_RETURN_IF_ERROR(std::move(libcxx_builder).Wait());
  93. return runtimes.base_path();
  94. }
  95. } // namespace Carbon