1919#include " llvm/CAS/CASOutputBackend.h"
2020#include " llvm/RemoteCachingService/Client.h"
2121#include " llvm/Support/FileOutputBuffer.h"
22+ #include " llvm/Support/Path.h"
2223#include " llvm/Support/PrefixMapper.h"
2324#include " llvm/Support/Process.h"
2425#include " llvm/Support/ScopedDurationTimer.h"
@@ -34,7 +35,8 @@ class CompileJobCache::CachingOutputs {
3435public:
3536 using OutputKind = clang::cas::CompileJobCacheResult::OutputKind;
3637
37- CachingOutputs (CompilerInstance &Clang, llvm::PrefixMapper Mapper);
38+ CachingOutputs (CompilerInstance &Clang, StringRef Workingdir,
39+ llvm::PrefixMapper Mapper);
3840 virtual ~CachingOutputs () = default ;
3941
4042 // / \returns true if result was found and replayed, false otherwise.
@@ -73,10 +75,11 @@ namespace {
7375// / \p llvm::cas::ActionCache.
7476class ObjectStoreCachingOutputs : public CompileJobCache ::CachingOutputs {
7577public:
76- ObjectStoreCachingOutputs (CompilerInstance &Clang, llvm::PrefixMapper Mapper,
78+ ObjectStoreCachingOutputs (CompilerInstance &Clang, StringRef WorkingDir,
79+ llvm::PrefixMapper Mapper,
7780 std::shared_ptr<llvm::cas::ObjectStore> DB,
7881 std::shared_ptr<llvm::cas::ActionCache> Cache)
79- : CachingOutputs(Clang, std::move(Mapper)), CAS(std::move(DB)),
82+ : CachingOutputs(Clang, WorkingDir, std::move(Mapper)), CAS(std::move(DB)),
8083 Cache (std::move(Cache)) {
8184 if (CAS)
8285 CASOutputs = llvm::makeIntrusiveRefCnt<llvm::cas::CASOutputBackend>(*CAS);
@@ -167,9 +170,10 @@ class CollectingOutputBackend : public llvm::vfs::ProxyOutputBackend {
167170// / and \p llvm::cas::KeyValueDBClient.
168171class RemoteCachingOutputs : public CompileJobCache ::CachingOutputs {
169172public:
170- RemoteCachingOutputs (CompilerInstance &Clang, llvm::PrefixMapper Mapper,
173+ RemoteCachingOutputs (CompilerInstance &Clang, StringRef WorkingDir,
174+ llvm::PrefixMapper Mapper,
171175 llvm::cas::remote::ClientServices Clients)
172- : CachingOutputs(Clang, std::move(Mapper)) {
176+ : CachingOutputs(Clang, WorkingDir, std::move(Mapper)) {
173177 RemoteKVClient = std::move (Clients.KVDB );
174178 RemoteCASClient = std::move (Clients.CASDB );
175179 CollectingOutputs = llvm::makeIntrusiveRefCnt<CollectingOutputBackend>();
@@ -219,13 +223,23 @@ CompileJobCache::CachingOutputs::getPathForOutputKind(OutputKind Kind) {
219223 }
220224}
221225
222- static std::string fixupRelativePath (const std::string &Path, FileManager &FM) {
226+ static std::string fixupRelativePath (const std::string &Path, FileManager &FM,
227+ StringRef WorkingDir) {
228+ if (llvm::sys::path::is_absolute (Path) || Path.empty () || Path == " -" )
229+ return Path;
230+
231+ // Apply -working-dir compiler option.
223232 // FIXME: this needs to stay in sync with createOutputFileImpl. Ideally, clang
224233 // would create output files by their "kind" rather than by path.
225- if (!Path.empty () && Path != " -" && !llvm::sys::path::is_absolute (Path)) {
226- SmallString<128 > PathStorage (Path);
227- if (FM.FixupRelativePath (PathStorage))
228- return std::string (PathStorage);
234+ SmallString<128 > PathStorage (Path);
235+ if (FM.FixupRelativePath (PathStorage))
236+ return std::string (PathStorage);
237+
238+ // Apply "normal" working directory.
239+ if (!WorkingDir.empty ()) {
240+ SmallString<128 > Tmp (Path);
241+ llvm::sys::fs::make_absolute (WorkingDir, Tmp);
242+ return std::string (Tmp);
229243 }
230244 return Path;
231245}
@@ -290,26 +304,27 @@ std::optional<int> CompileJobCache::initialize(CompilerInstance &Clang) {
290304 return reportCachingBackendError (Clang.getDiagnostics (),
291305 Clients.takeError ());
292306 CacheBackend = std::make_unique<RemoteCachingOutputs>(
293- Clang, std::move (PrefixMapper), std::move (*Clients));
307+ Clang, /* WorkingDir= */ " " , std::move (PrefixMapper), std::move (*Clients));
294308 } else {
295309 CacheBackend = std::make_unique<ObjectStoreCachingOutputs>(
296- Clang, std::move (PrefixMapper), CAS, Cache);
310+ Clang, /* WorkingDir= */ " " , std::move (PrefixMapper), CAS, Cache);
297311 }
298312
299313 return std::nullopt ;
300314}
301315
302316CompileJobCache::CachingOutputs::CachingOutputs (CompilerInstance &Clang,
317+ StringRef WorkingDir,
303318 llvm::PrefixMapper Mapper)
304319 : Clang(Clang), PrefixMapper(std::move(Mapper)) {
305320 CompilerInvocation &Invocation = Clang.getInvocation ();
306321 FrontendOptions &FrontendOpts = Invocation.getFrontendOpts ();
307322 if (!Clang.hasFileManager ())
308323 Clang.createFileManager ();
309324 FileManager &FM = Clang.getFileManager ();
310- OutputFile = fixupRelativePath (FrontendOpts.OutputFile , FM);
311- DependenciesFile =
312- fixupRelativePath ( Invocation.getDependencyOutputOpts ().OutputFile , FM);
325+ OutputFile = fixupRelativePath (FrontendOpts.OutputFile , FM, WorkingDir );
326+ DependenciesFile = fixupRelativePath (
327+ Invocation.getDependencyOutputOpts ().OutputFile , FM, WorkingDir );
313328 DiagProcessor = std::make_unique<clang::cas::CachingDiagnosticsProcessor>(
314329 PrefixMapper, FM);
315330}
@@ -491,8 +506,9 @@ bool CompileJobCache::finishComputedResult(CompilerInstance &Clang,
491506}
492507
493508Expected<std::optional<int >> CompileJobCache::replayCachedResult (
494- std::shared_ptr<CompilerInvocation> Invok, const llvm::cas::CASID &CacheKey,
495- cas::CompileJobCacheResult &CachedResult, SmallVectorImpl<char > &DiagText) {
509+ std::shared_ptr<CompilerInvocation> Invok, StringRef WorkingDir,
510+ const llvm::cas::CASID &CacheKey, cas::CompileJobCacheResult &CachedResult,
511+ SmallVectorImpl<char > &DiagText) {
496512 CompilerInstance Clang;
497513 Clang.setInvocation (std::move (Invok));
498514 llvm::raw_svector_ostream DiagOS (DiagText);
@@ -520,8 +536,9 @@ Expected<std::optional<int>> CompileJobCache::replayCachedResult(
520536
521537 assert (!Clang.getDiagnostics ().hasErrorOccurred ());
522538
523- ObjectStoreCachingOutputs CachingOutputs (Clang, std::move (PrefixMapper),
524- /* CAS*/ nullptr , /* Cache*/ nullptr );
539+ ObjectStoreCachingOutputs CachingOutputs (
540+ Clang, WorkingDir, std::move (PrefixMapper),
541+ /* CAS*/ nullptr , /* Cache*/ nullptr );
525542
526543 std::optional<int > Ret;
527544 if (Error E = CachingOutputs
0 commit comments