Skip to content

Propagate sysroot to module loader subinvocation #80007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/AST/ModuleLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ struct InterfaceSubContextDelegate {
virtual std::error_code runInSubContext(StringRef moduleName,
StringRef interfacePath,
StringRef sdkPath,
std::optional<StringRef> sysroot,
StringRef outputPath,
SourceLoc diagLoc,
llvm::function_ref<std::error_code(ASTContext&, ModuleDecl*,
Expand All @@ -217,6 +218,7 @@ struct InterfaceSubContextDelegate {
virtual std::error_code runInSubCompilerInstance(StringRef moduleName,
StringRef interfacePath,
StringRef sdkPath,
std::optional<StringRef> sysroot,
StringRef outputPath,
SourceLoc diagLoc,
bool silenceErrors,
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Frontend/Frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ class CompilerInvocation {
SearchPathOpts.VFSOverlayFiles = Overlays;
}

void setSysRoot(StringRef SysRoot) {
SearchPathOpts.setSysRoot(SysRoot);
}

void setExtraClangArgs(const std::vector<std::string> &Args) {
ClangImporterOpts.ExtraArgs = Args;
}
Expand Down
2 changes: 2 additions & 0 deletions include/swift/Frontend/ModuleInterfaceLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ struct InterfaceSubContextDelegateImpl : InterfaceSubContextDelegate {
std::error_code runInSubContext(StringRef moduleName,
StringRef interfacePath,
StringRef sdkPath,
std::optional<StringRef> sysroot,
StringRef outputPath,
SourceLoc diagLoc,
llvm::function_ref<std::error_code(ASTContext&, ModuleDecl*,
Expand All @@ -710,6 +711,7 @@ struct InterfaceSubContextDelegateImpl : InterfaceSubContextDelegate {
std::error_code runInSubCompilerInstance(StringRef moduleName,
StringRef interfacePath,
StringRef sdkPath,
std::optional<StringRef> sysroot,
StringRef outputPath,
SourceLoc diagLoc,
bool silenceErrors,
Expand Down
2 changes: 1 addition & 1 deletion lib/Frontend/ModuleInterfaceBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
13 changes: 8 additions & 5 deletions lib/Frontend/ModuleInterfaceBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class ImplicitModuleInterfaceBuilder {
InterfaceSubContextDelegate &subASTDelegate;
const StringRef interfacePath;
const StringRef sdkPath;
const std::optional<StringRef> sysroot;
const StringRef moduleName;
const StringRef moduleCachePath;
const StringRef prebuiltCachePath;
Expand Down Expand Up @@ -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<StringRef> 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),
Expand Down
13 changes: 11 additions & 2 deletions lib/Frontend/ModuleInterfaceLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -2049,12 +2053,13 @@ std::error_code
InterfaceSubContextDelegateImpl::runInSubContext(StringRef moduleName,
StringRef interfacePath,
StringRef sdkPath,
std::optional<StringRef> sysroot,
StringRef outputPath,
SourceLoc diagLoc,
llvm::function_ref<std::error_code(ASTContext&, ModuleDecl*, ArrayRef<StringRef>,
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();
Expand All @@ -2070,6 +2075,7 @@ std::error_code
InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName,
StringRef interfacePath,
StringRef sdkPath,
std::optional<StringRef> sysroot,
StringRef outputPath,
SourceLoc diagLoc,
bool silenceErrors,
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/Serialization/ScanningLoaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ SwiftModuleScanner::scanInterfaceFile(Twine moduleInterfacePath,
std::optional<ModuleDependencyInfo> Result;
std::error_code code = astDelegate.runInSubContext(
realModuleName.str(), moduleInterfacePath.str(), sdkPath,
StringRef(), SourceLoc(),
Ctx.SearchPathOpts.getSysRoot(), StringRef(), SourceLoc(),
[&](ASTContext &Ctx, ModuleDecl *mainMod, ArrayRef<StringRef> BaseArgs,
StringRef Hash, StringRef UserModVer) {
assert(mainMod);
Expand Down