21
21
#include " llvm/MCCAS/MCCASObjectV1.h"
22
22
#include " llvm/RemoteCachingService/Client.h"
23
23
#include " llvm/Support/FileOutputBuffer.h"
24
+ #include " llvm/Support/Path.h"
24
25
#include " llvm/Support/PrefixMapper.h"
25
26
#include " llvm/Support/Process.h"
26
27
#include " llvm/Support/ScopedDurationTimer.h"
@@ -36,8 +37,9 @@ class CompileJobCache::CachingOutputs {
36
37
public:
37
38
using OutputKind = clang::cas::CompileJobCacheResult::OutputKind;
38
39
39
- CachingOutputs (CompilerInstance &Clang, llvm::PrefixMapper Mapper,
40
- bool WriteOutputAsCASID, bool UseCASBackend);
40
+ CachingOutputs (CompilerInstance &Clang, StringRef Workingdir,
41
+ llvm::PrefixMapper Mapper, bool WriteOutputAsCASID,
42
+ bool UseCASBackend);
41
43
virtual ~CachingOutputs () = default ;
42
44
43
45
// / \returns true if result was found and replayed, false otherwise.
@@ -78,12 +80,13 @@ namespace {
78
80
// / \p llvm::cas::ActionCache.
79
81
class ObjectStoreCachingOutputs : public CompileJobCache ::CachingOutputs {
80
82
public:
81
- ObjectStoreCachingOutputs (CompilerInstance &Clang, llvm::PrefixMapper Mapper,
82
- bool WriteOutputAsCASID, bool UseCASBackend,
83
+ ObjectStoreCachingOutputs (CompilerInstance &Clang, StringRef WorkingDir,
84
+ llvm::PrefixMapper Mapper, bool WriteOutputAsCASID,
85
+ bool UseCASBackend,
83
86
std::optional<llvm::cas::CASID> &MCOutputID,
84
87
std::shared_ptr<llvm::cas::ObjectStore> DB,
85
88
std::shared_ptr<llvm::cas::ActionCache> Cache)
86
- : CachingOutputs(Clang, std::move(Mapper), WriteOutputAsCASID,
89
+ : CachingOutputs(Clang, WorkingDir, std::move(Mapper), WriteOutputAsCASID,
87
90
UseCASBackend),
88
91
ComputedJobNeedsReplay (WriteOutputAsCASID || UseCASBackend),
89
92
MCOutputID(MCOutputID), CAS(std::move(DB)), Cache(std::move(Cache)) {
@@ -178,9 +181,10 @@ class CollectingOutputBackend : public llvm::vfs::ProxyOutputBackend {
178
181
// / and \p llvm::cas::KeyValueDBClient.
179
182
class RemoteCachingOutputs : public CompileJobCache ::CachingOutputs {
180
183
public:
181
- RemoteCachingOutputs (CompilerInstance &Clang, llvm::PrefixMapper Mapper,
184
+ RemoteCachingOutputs (CompilerInstance &Clang, StringRef WorkingDir,
185
+ llvm::PrefixMapper Mapper,
182
186
llvm::cas::remote::ClientServices Clients)
183
- : CachingOutputs(Clang, std::move(Mapper),
187
+ : CachingOutputs(Clang, WorkingDir, std::move(Mapper),
184
188
/* WriteOutputAsCASID*/ false ,
185
189
/* UseCASBackend*/ false ) {
186
190
RemoteKVClient = std::move (Clients.KVDB );
@@ -232,13 +236,23 @@ CompileJobCache::CachingOutputs::getPathForOutputKind(OutputKind Kind) {
232
236
}
233
237
}
234
238
235
- static std::string fixupRelativePath (const std::string &Path, FileManager &FM) {
239
+ static std::string fixupRelativePath (const std::string &Path, FileManager &FM,
240
+ StringRef WorkingDir) {
241
+ if (llvm::sys::path::is_absolute (Path) || Path.empty () || Path == " -" )
242
+ return Path;
243
+
244
+ // Apply -working-dir compiler option.
236
245
// FIXME: this needs to stay in sync with createOutputFileImpl. Ideally, clang
237
246
// would create output files by their "kind" rather than by path.
238
- if (!Path.empty () && Path != " -" && !llvm::sys::path::is_absolute (Path)) {
239
- SmallString<128 > PathStorage (Path);
240
- if (FM.FixupRelativePath (PathStorage))
241
- return std::string (PathStorage);
247
+ SmallString<128 > PathStorage (Path);
248
+ if (FM.FixupRelativePath (PathStorage))
249
+ return std::string (PathStorage);
250
+
251
+ // Apply "normal" working directory.
252
+ if (!WorkingDir.empty ()) {
253
+ SmallString<128 > Tmp (Path);
254
+ llvm::sys::fs::make_absolute (WorkingDir, Tmp);
255
+ return std::string (Tmp);
242
256
}
243
257
return Path;
244
258
}
@@ -311,17 +325,18 @@ std::optional<int> CompileJobCache::initialize(CompilerInstance &Clang) {
311
325
" combination of options not rejected earlier?" );
312
326
assert (!UseCASBackend && " combination of options not rejected earlier?" );
313
327
CacheBackend = std::make_unique<RemoteCachingOutputs>(
314
- Clang, std::move (PrefixMapper), std::move (*Clients));
328
+ Clang, /* WorkingDir= */ " " , std::move (PrefixMapper), std::move (*Clients));
315
329
} else {
316
330
CacheBackend = std::make_unique<ObjectStoreCachingOutputs>(
317
- Clang, std::move (PrefixMapper), CacheOpts. WriteOutputAsCASID ,
318
- UseCASBackend, MCOutputID, CAS, Cache);
331
+ Clang, /* WorkingDir= */ " " , std::move (PrefixMapper),
332
+ CacheOpts. WriteOutputAsCASID , UseCASBackend, MCOutputID, CAS, Cache);
319
333
}
320
334
321
335
return std::nullopt;
322
336
}
323
337
324
338
CompileJobCache::CachingOutputs::CachingOutputs (CompilerInstance &Clang,
339
+ StringRef WorkingDir,
325
340
llvm::PrefixMapper Mapper,
326
341
bool WriteOutputAsCASID,
327
342
bool UseCASBackend)
@@ -332,9 +347,9 @@ CompileJobCache::CachingOutputs::CachingOutputs(CompilerInstance &Clang,
332
347
if (!Clang.hasFileManager ())
333
348
Clang.createFileManager ();
334
349
FileManager &FM = Clang.getFileManager ();
335
- OutputFile = fixupRelativePath (FrontendOpts.OutputFile , FM);
336
- DependenciesFile =
337
- fixupRelativePath ( Invocation.getDependencyOutputOpts ().OutputFile , FM);
350
+ OutputFile = fixupRelativePath (FrontendOpts.OutputFile , FM, WorkingDir );
351
+ DependenciesFile = fixupRelativePath (
352
+ Invocation.getDependencyOutputOpts ().OutputFile , FM, WorkingDir );
338
353
DiagProcessor = std::make_unique<clang::cas::CachingDiagnosticsProcessor>(
339
354
PrefixMapper, FM);
340
355
}
@@ -524,10 +539,10 @@ bool CompileJobCache::finishComputedResult(CompilerInstance &Clang,
524
539
}
525
540
526
541
Expected<std::optional<int >> CompileJobCache::replayCachedResult (
527
- std::shared_ptr<CompilerInvocation> Invok, const llvm::cas::CASID &CacheKey ,
528
- cas::CompileJobCacheResult &CachedResult, SmallVectorImpl< char > &DiagText ,
529
- bool WriteOutputAsCASID , bool UseCASBackend ,
530
- std::optional<llvm::cas::CASID> *OutMCOutputID) {
542
+ std::shared_ptr<CompilerInvocation> Invok, StringRef WorkingDir ,
543
+ const llvm:: cas::CASID &CacheKey, cas::CompileJobCacheResult &CachedResult ,
544
+ SmallVectorImpl< char > &DiagText , bool WriteOutputAsCASID ,
545
+ bool UseCASBackend, std::optional<llvm::cas::CASID> *OutMCOutputID) {
531
546
CompilerInstance Clang;
532
547
Clang.setInvocation (std::move (Invok));
533
548
llvm::raw_svector_ostream DiagOS (DiagText);
@@ -556,10 +571,10 @@ Expected<std::optional<int>> CompileJobCache::replayCachedResult(
556
571
assert (!Clang.getDiagnostics ().hasErrorOccurred ());
557
572
558
573
std::optional<llvm::cas::CASID> MCOutputID;
559
- ObjectStoreCachingOutputs CachingOutputs (Clang, std::move (PrefixMapper),
560
- WriteOutputAsCASID, UseCASBackend ,
561
- MCOutputID,
562
- /* CAS*/ nullptr , /* Cache*/ nullptr );
574
+ ObjectStoreCachingOutputs CachingOutputs (
575
+ Clang, WorkingDir, std::move (PrefixMapper), WriteOutputAsCASID ,
576
+ UseCASBackend, MCOutputID,
577
+ /* CAS*/ nullptr , /* Cache*/ nullptr );
563
578
if (OutMCOutputID)
564
579
*OutMCOutputID = std::move (MCOutputID);
565
580
0 commit comments