diff --git a/include/swift/AST/ModuleLoader.h b/include/swift/AST/ModuleLoader.h index 74f0979791a90..7d53e169898c8 100644 --- a/include/swift/AST/ModuleLoader.h +++ b/include/swift/AST/ModuleLoader.h @@ -209,6 +209,7 @@ struct InterfaceSubContextDelegate { virtual std::error_code runInSubContext(StringRef moduleName, StringRef interfacePath, StringRef sdkPath, + std::optional sysroot, StringRef outputPath, SourceLoc diagLoc, llvm::function_ref sysroot, StringRef outputPath, SourceLoc diagLoc, bool silenceErrors, diff --git a/include/swift/Frontend/Frontend.h b/include/swift/Frontend/Frontend.h index 85ba492501f4b..55d984a53db8a 100644 --- a/include/swift/Frontend/Frontend.h +++ b/include/swift/Frontend/Frontend.h @@ -222,6 +222,10 @@ class CompilerInvocation { SearchPathOpts.VFSOverlayFiles = Overlays; } + void setSysRoot(StringRef SysRoot) { + SearchPathOpts.setSysRoot(SysRoot); + } + void setExtraClangArgs(const std::vector &Args) { ClangImporterOpts.ExtraArgs = Args; } diff --git a/include/swift/Frontend/ModuleInterfaceLoader.h b/include/swift/Frontend/ModuleInterfaceLoader.h index f05c65756445f..6e17a11738db7 100644 --- a/include/swift/Frontend/ModuleInterfaceLoader.h +++ b/include/swift/Frontend/ModuleInterfaceLoader.h @@ -702,6 +702,7 @@ struct InterfaceSubContextDelegateImpl : InterfaceSubContextDelegate { std::error_code runInSubContext(StringRef moduleName, StringRef interfacePath, StringRef sdkPath, + std::optional sysroot, StringRef outputPath, SourceLoc diagLoc, llvm::function_ref sysroot, StringRef outputPath, SourceLoc diagLoc, bool silenceErrors, diff --git a/lib/Frontend/ModuleInterfaceBuilder.cpp b/lib/Frontend/ModuleInterfaceBuilder.cpp index 0b55818a51bb3..63eba165e60d3 100644 --- a/lib/Frontend/ModuleInterfaceBuilder.cpp +++ b/lib/Frontend/ModuleInterfaceBuilder.cpp @@ -356,7 +356,7 @@ bool ImplicitModuleInterfaceBuilder::buildSwiftModuleInternal( } SubError = (bool)subASTDelegate.runInSubCompilerInstance( - moduleName, interfacePath, sdkPath, OutPath, diagnosticLoc, + moduleName, interfacePath, sdkPath, sysroot, OutPath, diagnosticLoc, silenceInterfaceDiagnostics, [&](SubCompilerInstanceInfo &info) { auto EBuilder = ExplicitModuleInterfaceBuilder( diff --git a/lib/Frontend/ModuleInterfaceBuilder.h b/lib/Frontend/ModuleInterfaceBuilder.h index beac821642fb0..975b74f12f93a 100644 --- a/lib/Frontend/ModuleInterfaceBuilder.h +++ b/lib/Frontend/ModuleInterfaceBuilder.h @@ -42,6 +42,7 @@ class ImplicitModuleInterfaceBuilder { InterfaceSubContextDelegate &subASTDelegate; const StringRef interfacePath; const StringRef sdkPath; + const std::optional sysroot; const StringRef moduleName; const StringRef moduleCachePath; const StringRef prebuiltCachePath; @@ -87,15 +88,17 @@ class ImplicitModuleInterfaceBuilder { SourceManager &sourceMgr, DiagnosticEngine *diags, InterfaceSubContextDelegate &subASTDelegate, StringRef interfacePath, StringRef sdkPath, - StringRef moduleName, StringRef moduleCachePath, - StringRef backupInterfaceDir, StringRef prebuiltCachePath, - StringRef ABIDescriptorPath, bool disableInterfaceFileLock = false, + std::optional sysroot, StringRef moduleName, + StringRef moduleCachePath, StringRef backupInterfaceDir, + StringRef prebuiltCachePath, StringRef ABIDescriptorPath, + bool disableInterfaceFileLock = false, bool silenceInterfaceDiagnostics = false, SourceLoc diagnosticLoc = SourceLoc(), DependencyTracker *tracker = nullptr) : sourceMgr(sourceMgr), diags(diags), subASTDelegate(subASTDelegate), - interfacePath(interfacePath), sdkPath(sdkPath), moduleName(moduleName), - moduleCachePath(moduleCachePath), prebuiltCachePath(prebuiltCachePath), + interfacePath(interfacePath), sdkPath(sdkPath), sysroot(sysroot), + moduleName(moduleName), moduleCachePath(moduleCachePath), + prebuiltCachePath(prebuiltCachePath), backupInterfaceDir(backupInterfaceDir), ABIDescriptorPath(ABIDescriptorPath), disableInterfaceFileLock(disableInterfaceFileLock), diff --git a/lib/Frontend/ModuleInterfaceLoader.cpp b/lib/Frontend/ModuleInterfaceLoader.cpp index 096f3adc14ec6..57c2f01d40336 100644 --- a/lib/Frontend/ModuleInterfaceLoader.cpp +++ b/lib/Frontend/ModuleInterfaceLoader.cpp @@ -1215,6 +1215,7 @@ class ModuleInterfaceLoaderImpl { ctx.SourceMgr, diagsToUse, astDelegate, interfacePath, ctx.SearchPathOpts.getSDKPath(), + ctx.SearchPathOpts.getSysRoot(), realName.str(), cacheDir, prebuiltCacheDir, backupInterfaceDir, StringRef(), Opts.disableInterfaceLock, @@ -1250,6 +1251,7 @@ class ModuleInterfaceLoaderImpl { ImplicitModuleInterfaceBuilder fallbackBuilder( ctx.SourceMgr, &ctx.Diags, astDelegate, backupPath, ctx.SearchPathOpts.getSDKPath(), + ctx.SearchPathOpts.getSysRoot(), moduleName, cacheDir, prebuiltCacheDir, backupInterfaceDir, StringRef(), Opts.disableInterfaceLock, @@ -1475,6 +1477,7 @@ bool ModuleInterfaceLoader::buildSwiftModuleFromSwiftInterface( RequireOSSAModules); ImplicitModuleInterfaceBuilder builder(SourceMgr, &Diags, astDelegate, InPath, SearchPathOpts.getSDKPath(), + SearchPathOpts.getSysRoot(), ModuleName, CacheDir, PrebuiltCacheDir, BackupInterfaceDir, ABIOutputPath, LoaderOpts.disableInterfaceLock, @@ -1495,6 +1498,7 @@ bool ModuleInterfaceLoader::buildSwiftModuleFromSwiftInterface( assert(!backInPath.empty()); ImplicitModuleInterfaceBuilder backupBuilder(SourceMgr, &Diags, astDelegate, backInPath, SearchPathOpts.getSDKPath(), + SearchPathOpts.getSysRoot(), ModuleName, CacheDir, PrebuiltCacheDir, BackupInterfaceDir, ABIOutputPath, LoaderOpts.disableInterfaceLock, @@ -2049,12 +2053,13 @@ std::error_code InterfaceSubContextDelegateImpl::runInSubContext(StringRef moduleName, StringRef interfacePath, StringRef sdkPath, + std::optional sysroot, StringRef outputPath, SourceLoc diagLoc, llvm::function_ref, StringRef, StringRef)> action) { - return runInSubCompilerInstance(moduleName, interfacePath, sdkPath, outputPath, - diagLoc, /*silenceErrors=*/false, + return runInSubCompilerInstance(moduleName, interfacePath, sdkPath, sysroot, + outputPath, diagLoc, /*silenceErrors=*/false, [&](SubCompilerInstanceInfo &info){ std::string UserModuleVer = info.Instance->getInvocation().getFrontendOptions() .UserModuleVersion.getAsString(); @@ -2070,6 +2075,7 @@ std::error_code InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName, StringRef interfacePath, StringRef sdkPath, + std::optional sysroot, StringRef outputPath, SourceLoc diagLoc, bool silenceErrors, @@ -2101,6 +2107,9 @@ InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName, subInvocation.setModuleName(moduleName); BuildArgs.push_back("-module-name"); BuildArgs.push_back(moduleName); + if (sysroot) { + subInvocation.setSysRoot(sysroot.value()); + } // FIXME: Hack for Darwin.swiftmodule, which cannot be rebuilt with C++ // interop enabled by the Swift CI because it uses an old host SDK. diff --git a/lib/Serialization/ScanningLoaders.cpp b/lib/Serialization/ScanningLoaders.cpp index 4b27683652e1d..414043651ef67 100644 --- a/lib/Serialization/ScanningLoaders.cpp +++ b/lib/Serialization/ScanningLoaders.cpp @@ -155,7 +155,7 @@ SwiftModuleScanner::scanInterfaceFile(Twine moduleInterfacePath, std::optional Result; std::error_code code = astDelegate.runInSubContext( realModuleName.str(), moduleInterfacePath.str(), sdkPath, - StringRef(), SourceLoc(), + Ctx.SearchPathOpts.getSysRoot(), StringRef(), SourceLoc(), [&](ASTContext &Ctx, ModuleDecl *mainMod, ArrayRef BaseArgs, StringRef Hash, StringRef UserModVer) { assert(mainMod);