19
19
#include " llvm/CAS/CASOutputBackend.h"
20
20
#include " llvm/RemoteCachingService/Client.h"
21
21
#include " llvm/Support/FileOutputBuffer.h"
22
+ #include " llvm/Support/Path.h"
22
23
#include " llvm/Support/PrefixMapper.h"
23
24
#include " llvm/Support/Process.h"
24
25
#include " llvm/Support/ScopedDurationTimer.h"
@@ -34,7 +35,8 @@ class CompileJobCache::CachingOutputs {
34
35
public:
35
36
using OutputKind = clang::cas::CompileJobCacheResult::OutputKind;
36
37
37
- CachingOutputs (CompilerInstance &Clang, llvm::PrefixMapper Mapper);
38
+ CachingOutputs (CompilerInstance &Clang, StringRef Workingdir,
39
+ llvm::PrefixMapper Mapper);
38
40
virtual ~CachingOutputs () = default ;
39
41
40
42
// / \returns true if result was found and replayed, false otherwise.
@@ -73,10 +75,11 @@ namespace {
73
75
// / \p llvm::cas::ActionCache.
74
76
class ObjectStoreCachingOutputs : public CompileJobCache ::CachingOutputs {
75
77
public:
76
- ObjectStoreCachingOutputs (CompilerInstance &Clang, llvm::PrefixMapper Mapper,
78
+ ObjectStoreCachingOutputs (CompilerInstance &Clang, StringRef WorkingDir,
79
+ llvm::PrefixMapper Mapper,
77
80
std::shared_ptr<llvm::cas::ObjectStore> DB,
78
81
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)),
80
83
Cache (std::move(Cache)) {
81
84
if (CAS)
82
85
CASOutputs = llvm::makeIntrusiveRefCnt<llvm::cas::CASOutputBackend>(*CAS);
@@ -167,9 +170,10 @@ class CollectingOutputBackend : public llvm::vfs::ProxyOutputBackend {
167
170
// / and \p llvm::cas::KeyValueDBClient.
168
171
class RemoteCachingOutputs : public CompileJobCache ::CachingOutputs {
169
172
public:
170
- RemoteCachingOutputs (CompilerInstance &Clang, llvm::PrefixMapper Mapper,
173
+ RemoteCachingOutputs (CompilerInstance &Clang, StringRef WorkingDir,
174
+ llvm::PrefixMapper Mapper,
171
175
llvm::cas::remote::ClientServices Clients)
172
- : CachingOutputs(Clang, std::move(Mapper)) {
176
+ : CachingOutputs(Clang, WorkingDir, std::move(Mapper)) {
173
177
RemoteKVClient = std::move (Clients.KVDB );
174
178
RemoteCASClient = std::move (Clients.CASDB );
175
179
CollectingOutputs = llvm::makeIntrusiveRefCnt<CollectingOutputBackend>();
@@ -219,13 +223,23 @@ CompileJobCache::CachingOutputs::getPathForOutputKind(OutputKind Kind) {
219
223
}
220
224
}
221
225
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.
223
232
// FIXME: this needs to stay in sync with createOutputFileImpl. Ideally, clang
224
233
// 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);
229
243
}
230
244
return Path;
231
245
}
@@ -290,26 +304,27 @@ std::optional<int> CompileJobCache::initialize(CompilerInstance &Clang) {
290
304
return reportCachingBackendError (Clang.getDiagnostics (),
291
305
Clients.takeError ());
292
306
CacheBackend = std::make_unique<RemoteCachingOutputs>(
293
- Clang, std::move (PrefixMapper), std::move (*Clients));
307
+ Clang, /* WorkingDir= */ " " , std::move (PrefixMapper), std::move (*Clients));
294
308
} else {
295
309
CacheBackend = std::make_unique<ObjectStoreCachingOutputs>(
296
- Clang, std::move (PrefixMapper), CAS, Cache);
310
+ Clang, /* WorkingDir= */ " " , std::move (PrefixMapper), CAS, Cache);
297
311
}
298
312
299
313
return std::nullopt;
300
314
}
301
315
302
316
CompileJobCache::CachingOutputs::CachingOutputs (CompilerInstance &Clang,
317
+ StringRef WorkingDir,
303
318
llvm::PrefixMapper Mapper)
304
319
: Clang(Clang), PrefixMapper(std::move(Mapper)) {
305
320
CompilerInvocation &Invocation = Clang.getInvocation ();
306
321
FrontendOptions &FrontendOpts = Invocation.getFrontendOpts ();
307
322
if (!Clang.hasFileManager ())
308
323
Clang.createFileManager ();
309
324
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 );
313
328
DiagProcessor = std::make_unique<clang::cas::CachingDiagnosticsProcessor>(
314
329
PrefixMapper, FM);
315
330
}
@@ -491,8 +506,9 @@ bool CompileJobCache::finishComputedResult(CompilerInstance &Clang,
491
506
}
492
507
493
508
Expected<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) {
496
512
CompilerInstance Clang;
497
513
Clang.setInvocation (std::move (Invok));
498
514
llvm::raw_svector_ostream DiagOS (DiagText);
@@ -520,8 +536,9 @@ Expected<std::optional<int>> CompileJobCache::replayCachedResult(
520
536
521
537
assert (!Clang.getDiagnostics ().hasErrorOccurred ());
522
538
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 );
525
542
526
543
std::optional<int > Ret;
527
544
if (Error E = CachingOutputs
0 commit comments