Refactor FindPreludeFiles into InstallPaths (#4268)

From the driver's perspective, `FindPreludeFiles` is closely tied to
`compile`. This makes it difficult to refactor commands without
affecting the test dependencies on `FindPreludeFiles`. `InstallPaths`
seems like a decent home since it is responsible for the install
structure.

I'm switching to an `Error` return to allow callers to choose how to
handle it (e.g., in file tests, we typically don't want the direct error
stream).
This commit is contained in:
Jon Ross-Perkins
2024-09-04 22:22:48 +00:00
committed by GitHub
parent 1412ecd3f4
commit e382e6fd97
12 changed files with 124 additions and 83 deletions
+8 -45
View File
@@ -34,44 +34,6 @@
namespace Carbon {
auto Driver::FindPreludeFiles(llvm::StringRef core_package_dir,
llvm::raw_ostream& error_stream)
-> llvm::SmallVector<std::string> {
llvm::SmallVector<std::string> result;
// Include <data>/core/prelude.carbon, which is the entry point into the
// prelude.
{
llvm::SmallString<256> prelude_file(core_package_dir);
llvm::sys::path::append(prelude_file, llvm::sys::path::Style::posix,
"prelude.carbon");
result.push_back(prelude_file.str().str());
}
// Glob for <data>/core/prelude/**/*.carbon and add all the files we find.
llvm::SmallString<256> prelude_dir(core_package_dir);
llvm::sys::path::append(prelude_dir, llvm::sys::path::Style::posix,
"prelude");
std::error_code ec;
for (llvm::sys::fs::recursive_directory_iterator prelude_files_it(
prelude_dir, ec, /*follow_symlinks=*/false);
prelude_files_it != llvm::sys::fs::recursive_directory_iterator();
prelude_files_it.increment(ec)) {
if (ec) {
error_stream << "ERROR: Could not find prelude: " << ec.message() << "\n";
result.clear();
return result;
}
auto prelude_file = prelude_files_it->path();
if (llvm::sys::path::extension(prelude_file) == ".carbon") {
result.push_back(prelude_file);
}
}
return result;
}
struct Driver::CodegenOptions {
void Build(CommandLine::CommandBuilder& b) {
b.AddStringOption(
@@ -865,13 +827,14 @@ auto Driver::Compile(const CompileOptions& options,
// Find the files comprising the prelude if we are importing it.
// TODO: Replace this with a search for library api files in a
// package-specific search path based on the library name.
bool want_prelude =
options.prelude_import && options.phase >= CompileOptions::Phase::Check;
auto prelude = want_prelude ? FindPreludeFiles(installation_->core_package(),
error_stream_)
: llvm::SmallVector<std::string>{};
if (want_prelude && prelude.empty()) {
return {.success = false};
llvm::SmallVector<std::string> prelude;
if (options.prelude_import && options.phase >= CompileOptions::Phase::Check) {
if (auto find = installation_->FindPreludeFiles(); find.ok()) {
prelude = std::move(*find);
} else {
error_stream_ << "ERROR: " << find.error() << "\n";
return {.success = false};
}
}
// Prepare CompilationUnits before building scope exit handlers.