4141#include " llvm/ADT/IntrusiveRefCntPtr.h"
4242#include " llvm/ADT/SmallVector.h"
4343#include " llvm/ADT/Triple.h"
44+ #include " llvm/CAS/ActionCache.h"
45+ #include " llvm/CAS/BuiltinUnifiedCASDatabases.h"
46+ #include " llvm/CAS/CASFileSystem.h"
4447#include " llvm/Support/CommandLine.h"
4548#include " llvm/Support/Error.h"
4649#include " llvm/Support/MemoryBuffer.h"
@@ -395,6 +398,40 @@ void CompilerInstance::setupDependencyTrackerIfNeeded() {
395398 DepTracker->addDependency (path, /* isSystem=*/ false );
396399}
397400
401+ bool CompilerInstance::setupCASIfNeeded () {
402+ const auto &Opts = getInvocation ().getFrontendOptions ();
403+ if (!Opts.EnableCAS )
404+ return false ;
405+
406+ auto MaybeCache = llvm::cas::createOnDiskUnifiedCASDatabases (Opts.CASPath );
407+ if (!MaybeCache) {
408+ Diagnostics.diagnose (SourceLoc (), diag::error_create_cas, Opts.CASPath ,
409+ toString (MaybeCache.takeError ()));
410+ return true ;
411+ }
412+ CAS = std::move (MaybeCache->first );
413+ ResultCache = std::move (MaybeCache->second );
414+
415+ // create baseline key.
416+ llvm::Optional<llvm::cas::ObjectRef> FSRef;
417+ if (!Opts.CASFSRootID .empty ()) {
418+ auto CASFSID = CAS->parseID (Opts.CASFSRootID );
419+ if (!CASFSID) {
420+ Diagnostics.diagnose (SourceLoc (), diag::error_cas,
421+ toString (CASFSID.takeError ()));
422+ return true ;
423+ }
424+ FSRef = CAS->getReference (*CASFSID);
425+ if (!FSRef) {
426+ Diagnostics.diagnose (SourceLoc (), diag::error_cas,
427+ " -cas-fs value does not exist in CAS" );
428+ return true ;
429+ }
430+ }
431+
432+ return false ;
433+ }
434+
398435void CompilerInstance::setupOutputBackend () {
399436 // Skip if output backend is not setup, default to OnDiskOutputBackend.
400437 if (OutputBackend)
@@ -418,6 +455,11 @@ bool CompilerInstance::setup(const CompilerInvocation &Invoke,
418455 std::string &Error) {
419456 Invocation = Invoke;
420457
458+ if (setupCASIfNeeded ()) {
459+ Error = " Setting up CAS failed" ;
460+ return true ;
461+ }
462+
421463 setupDependencyTrackerIfNeeded ();
422464 setupOutputBackend ();
423465
@@ -463,6 +505,26 @@ bool CompilerInstance::setup(const CompilerInvocation &Invoke,
463505}
464506
465507bool CompilerInstance::setUpVirtualFileSystemOverlays () {
508+ if (Invocation.getFrontendOptions ().EnableCAS &&
509+ !Invocation.getFrontendOptions ().CASFSRootID .empty ()) {
510+ // Set up CASFS as BaseFS.
511+ auto RootID = CAS->parseID (Invocation.getFrontendOptions ().CASFSRootID );
512+ if (!RootID) {
513+ Diagnostics.diagnose (SourceLoc (), diag::error_invalid_cas_id,
514+ Invocation.getFrontendOptions ().CASFSRootID ,
515+ toString (RootID.takeError ()));
516+ return true ;
517+ }
518+ auto FS = llvm::cas::createCASFileSystem (*CAS, *RootID);
519+ if (!FS) {
520+ Diagnostics.diagnose (SourceLoc (), diag::error_invalid_cas_id,
521+ Invocation.getFrontendOptions ().CASFSRootID ,
522+ toString (FS.takeError ()));
523+ return true ;
524+ }
525+ SourceMgr.setFileSystem (std::move (*FS));
526+ }
527+
466528 auto ExpectedOverlay =
467529 Invocation.getSearchPathOptions ().makeOverlayFileSystem (
468530 SourceMgr.getFileSystem ());
0 commit comments