From 9f5e00104dfdd59a9ff8727dc7ee03f7f0db49ef Mon Sep 17 00:00:00 2001 From: _ov4 Date: Thu, 22 Oct 2026 03:22:32 +0000 Subject: refactor(driver): share INCLUDE and INCLUDE_OBJ path resolution Co-Authored-By: Claude Opus 5.5 --- tools/driver/main.cc | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'tools') diff --git a/tools/driver/main.cc b/tools/driver/main.cc index df551a2..681bf1c 100644 --- a/tools/driver/main.cc +++ b/tools/driver/main.cc @@ -63,6 +63,17 @@ bool colorTerminal() { #endif } +// INCLUDE and INCLUDE_OBJ paths are absolute or relative to the including +// unit's directory. +llvm::SmallString<256> relativeToUnit(llvm::StringRef UnitPath, llvm::StringRef Path) { + llvm::SmallString<256> Result(Path); + if (!llvm::sys::path::is_absolute(Result)) { + Result = llvm::sys::path::parent_path(UnitPath); + llvm::sys::path::append(Result, Path); + } + return Result; +} + #ifdef CPSC_RUN_PATH bool checkLliVersion(llvm::StringRef LliPath) { llvm::SmallString<256> VersionPath; @@ -243,11 +254,7 @@ int main(int argc, char **argv) { for (const auto &Stmt : Units[I].Statements) { auto *Include = dynamic_cast(Stmt.get()); if (!Include) continue; - llvm::SmallString<256> Path(Include->getPath()); - if (!llvm::sys::path::is_absolute(Path)) { - Path = llvm::sys::path::parent_path(Units[I].Path); - llvm::sys::path::append(Path, Include->getPath()); - } + llvm::SmallString<256> Path = relativeToUnit(Units[I].Path, Include->getPath()); bool Found = false; for (size_t J = 0; J < Units.size(); ++J) { if (llvm::sys::fs::equivalent(Path, Units[J].Path)) { @@ -284,11 +291,7 @@ int main(int argc, char **argv) { LibcLoaded = true; continue; } - llvm::SmallString<256> Path(Object->getPath()); - if (!llvm::sys::path::is_absolute(Path)) { - Path = llvm::sys::path::parent_path(Units[I].Path); - llvm::sys::path::append(Path, Object->getPath()); - } + llvm::SmallString<256> Path = relativeToUnit(Units[I].Path, Object->getPath()); if (llvm::sys::path::extension(Path) != ".o") { cps::sourceError(Stmt->getLine(), "INCLUDE_OBJ '%s' must name a .o object file or \"__libc\"", -- cgit v1.3.1-4-g156e