build_runtimes_subcommand.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. namespace Carbon {
  8. auto BuildRuntimesOptions::Build(CommandLine::CommandBuilder& b) -> void {
  9. b.AddStringOption(
  10. {
  11. .name = "output-directory",
  12. .value_name = "DIR",
  13. .help = R"""(
  14. The directory to populate with runtime libraries suitable for the selected code
  15. generation options.
  16. )""",
  17. },
  18. [&](auto& arg_b) { arg_b.Set(&directory); });
  19. codegen_options.Build(b);
  20. }
  21. static constexpr CommandLine::CommandInfo SubcommandInfo = {
  22. .name = "build-runtimes",
  23. .help = R"""(
  24. Build Carbon's runtime libraries.
  25. This subcommand builds Carbon's runtime libraries for a particular code
  26. generation target, either in their default location or a specified one.
  27. Running this command directly is not necessary as Carbon will build and cache
  28. runtimes as needed when linking, but building them directly can aid in
  29. debugging issues or allow them to be prebuilt, possibly with customized code
  30. generation flags, and used explicitly when linking.
  31. )""",
  32. };
  33. BuildRuntimesSubcommand::BuildRuntimesSubcommand()
  34. : DriverSubcommand(SubcommandInfo) {}
  35. auto BuildRuntimesSubcommand::Run(DriverEnv& driver_env) -> DriverResult {
  36. ClangRunner runner(driver_env.installation, driver_env.fs,
  37. driver_env.vlog_stream);
  38. // Don't run Clang when fuzzing, it is known to not be reliable under fuzzing
  39. // due to many unfixed issues.
  40. if (!TestAndDiagnoseIfFuzzingExternalLibraries(driver_env, "clang")) {
  41. return {.success = false};
  42. }
  43. // For diagnosing filesystem or other errors when building runtimes.
  44. CARBON_DIAGNOSTIC(FailureBuildingRuntimes, Error,
  45. "failure building runtimes: {0}", std::string);
  46. auto tmp_result = Filesystem::MakeTmpDir();
  47. if (!tmp_result.ok()) {
  48. driver_env.emitter.Emit(FailureBuildingRuntimes,
  49. tmp_result.error().message());
  50. return {.success = false};
  51. }
  52. Filesystem::RemovingDir tmp_dir = *std::move(tmp_result);
  53. // TODO: Currently, the default location is just a subdirectory of the
  54. // temporary directory used for the build. This allows the subcommand to be
  55. // used to test and debug runtime building, but not for the results to be
  56. // reused. Eventually, this should be connected to the same runtimes cache
  57. // used by link commands.
  58. std::filesystem::path output_path =
  59. options_.directory.empty()
  60. ? tmp_dir.abs_path() / "runtimes"
  61. : std::filesystem::path(options_.directory.str());
  62. // Hard code a subdirectory of the runtimes output for the Clang resource
  63. // directory runtimes.
  64. //
  65. // TODO: This should be replaced with an abstraction that manages the layout
  66. // of the generated runtimes rather than hardcoding it.
  67. std::filesystem::path resource_dir_path = output_path / "clang_resource_dir";
  68. auto build_result = runner.BuildTargetResourceDir(
  69. options_.codegen_options.target, resource_dir_path, tmp_dir.abs_path());
  70. if (!build_result.ok()) {
  71. driver_env.emitter.Emit(FailureBuildingRuntimes,
  72. build_result.error().message());
  73. }
  74. return {.success = build_result.ok()};
  75. }
  76. } // namespace Carbon