Repository: carbon-language/carbon-lang
File: toolchain/driver/clang_runtimes.cpp (C++)

Code before:

  runtimes_builder_ = std::get<Runtimes::Builder>(std::move(build_dir));
  archive_.emplace(this, archive_path_, srcs_path_, CollectSrcFiles(),
                   CollectCflags());
  tasks_.async([this]() mutable { Setup(); });
}

template <Runtimes::Component Component>
auto ClangArchiveRuntimesBuilder<Component>::CollectSrcFiles()
    -> llvm::SmallVector<llvm::StringRef> {
  if constexpr (Component == Runtimes::LibUnwind) {
    return llvm::SmallVector<llvm::StringRef>(llvm::make_filter_range(
        RuntimeSources::LibunwindSrcs, [](llvm::StringRef src) {
          return src.ends_with(".c") || src.ends_with(".cpp") ||
                 src.ends_with(".S");
        }));
  } else {
    static_assert(false,
                  "Invalid runtimes component for an archive runtime builder.");
  }
}

template <Runtimes::Component Component>
auto ClangArchiveRuntimesBuilder<Component>::CollectCflags()
    -> llvm::SmallVector<llvm::StringRef> {
  if constexpr (Component == Runtimes::LibUnwind) {
    return {
        "-no-canonical-prefixes",
        "-O3",
        "-fPIC",
        "-funwind-tables",
        "-fno-exceptions",
        "-fno-rtti",
        "-nostdinc++",
        "-I",
        include_path_.native(),
        "-D_LIBUNWIND_IS_NATIVE_ONLY",
        "-w",
    };
  } else {
    static_assert(false,
                  "Invalid runtimes component for an archive runtime builder.");
  }
}

template <Runtimes::Component Component>
auto ClangArchiveRuntimesBuilder<Component>::Setup() -> void {
  // Symlink the installation's `include` into the runtime.
  CARBON_CHECK(include_path_.is_absolute(),
               "Unexpected relative include path: {0}", include_path_);
  if (auto result = runtimes_builder_->dir().Symlink("include", include_path_);

Review comment (suggestion):
It's not like this is bad, but did you consider making a static table for each component that has a src files lambda and an array of cflags? Wondering if that would be nicer when there's a bunch of these instead of branches. Maybe that would get more unruly, just a thought.

Code after:

  if constexpr (Component == Runtimes::LibUnwind) {
    srcs_path_ = installation().libunwind_path();
    include_path_ = installation().libunwind_path() / "include";
    archive_path_ = std::filesystem::path("lib") / "libunwind.a";
  } else {
    static_assert(false,
                  "Invalid runtimes component for an archive runtime builder.");
  }

  archive_.emplace(this, archive_path_, srcs_path_, CollectSrcFiles(),
                   CollectCflags());
  tasks_.async([this]() mutable { Setup(); });
}

template <Runtimes::Component Component>
  requires(Component == Runtimes::LibUnwind)
auto ClangArchiveRuntimesBuilder<Component>::CollectSrcFiles()
    -> llvm::SmallVector<llvm::StringRef> {
  if constexpr (Component == Runtimes::LibUnwind) {
    return llvm::SmallVector<llvm::StringRef>(llvm::make_filter_range(
        RuntimeSources::LibunwindSrcs, [](llvm::StringRef src) {
          return src.ends_with(".c") || src.ends_with(".cpp") ||
                 src.ends_with(".S");
        }));
  } else {
    static_assert(false,
                  "Invalid runtimes component for an archive runtime builder.");
  }
}

template <Runtimes::Component Component>
  requires(Component == Runtimes::LibUnwind)
auto ClangArchiveRuntimesBuilder<Component>::CollectCflags()
    -> llvm::SmallVector<llvm::StringRef> {
  if constexpr (Component == Runtimes::LibUnwind) {
    return {
        "-no-canonical-prefixes",
        "-O3",
        "-fPIC",
        "-funwind-tables",
        "-fno-exceptions",
        "-fno-rtti",
        "-nostdinc++",
        "-I",
        include_path_.native(),
        "-D_LIBUNWIND_IS_NATIVE_ONLY",
        "-w",
    };
  } else {
    static_assert(false,