Skip to content

Commit 46ddb2a

Browse files
committed
[NFC] Support many runtime library import paths
Replaces SearchPathOptions::RuntimeLibraryImportPath with an equivalent std::vector of paths. Also reimplements SearchPathOptions::SkipRuntimeLibraryImportPaths to cause the list of runtime library import paths to be empty, rather than exiting early from SerializedModuleLoader::findModule().
1 parent d6eb168 commit 46ddb2a

File tree

4 files changed

+52
-30
lines changed

4 files changed

+52
-30
lines changed

include/swift/AST/SearchPathOptions.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ class SearchPathOptions {
6767
/// Path to search for compiler-relative stdlib dylibs.
6868
std::string RuntimeLibraryPath;
6969

70-
/// Path to search for compiler-relative stdlib modules.
71-
std::string RuntimeLibraryImportPath;
70+
/// Paths to search for stdlib modules. One of these will be compiler-relative.
71+
std::vector<std::string> RuntimeLibraryImportPaths;
7272

7373
/// Don't look in for compiler-provided modules.
74-
bool SkipRuntimeLibraryImportPath = false;
74+
bool SkipRuntimeLibraryImportPaths = false;
7575

7676
/// Return a hash code of any components from these options that should
7777
/// contribute to a Swift Bridging PCH hash.
@@ -92,7 +92,9 @@ class SearchPathOptions {
9292
Code = hash_combine(Code, LibraryPath);
9393
}
9494
Code = hash_combine(Code, RuntimeResourcePath);
95-
Code = hash_combine(Code, RuntimeLibraryImportPath);
95+
for (auto RuntimeLibraryImportPath : RuntimeLibraryImportPaths) {
96+
Code = hash_combine(Code, RuntimeLibraryImportPath);
97+
}
9698
return Code;
9799
}
98100
};

include/swift/Frontend/Frontend.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,7 @@ class CompilerInvocation {
184184

185185
void setRuntimeResourcePath(StringRef Path);
186186

187-
void setSDKPath(const std::string &Path) {
188-
SearchPathOpts.SDKPath = Path;
189-
}
187+
void setSDKPath(const std::string &Path);
190188

191189
StringRef getSDKPath() const {
192190
return SearchPathOpts.SDKPath;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,27 @@ void CompilerInvocation::setMainExecutablePath(StringRef Path) {
4242
setRuntimeResourcePath(LibPath.str());
4343
}
4444

45-
static void updateRuntimeLibraryPath(SearchPathOptions &SearchPathOpts,
46-
llvm::Triple &Triple) {
45+
static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
46+
llvm::Triple &Triple) {
4747
llvm::SmallString<128> LibPath(SearchPathOpts.RuntimeResourcePath);
4848

4949
llvm::sys::path::append(LibPath, getPlatformNameForTriple(Triple));
5050
SearchPathOpts.RuntimeLibraryPath = LibPath.str();
5151

52+
SearchPathOpts.RuntimeLibraryImportPaths.clear();
53+
54+
// If this is set, we don't want any runtime import paths.
55+
if (SearchPathOpts.SkipRuntimeLibraryImportPaths)
56+
return;
57+
5258
if (!Triple.isOSDarwin())
5359
llvm::sys::path::append(LibPath, swift::getMajorArchitectureName(Triple));
54-
SearchPathOpts.RuntimeLibraryImportPath = LibPath.str();
60+
SearchPathOpts.RuntimeLibraryImportPaths.push_back(LibPath.str());
5561
}
5662

5763
void CompilerInvocation::setRuntimeResourcePath(StringRef Path) {
5864
SearchPathOpts.RuntimeResourcePath = Path;
59-
updateRuntimeLibraryPath(SearchPathOpts, LangOpts.Target);
65+
updateRuntimeLibraryPaths(SearchPathOpts, LangOpts.Target);
6066
}
6167

6268
void CompilerInvocation::setTargetTriple(StringRef Triple) {
@@ -65,7 +71,12 @@ void CompilerInvocation::setTargetTriple(StringRef Triple) {
6571

6672
void CompilerInvocation::setTargetTriple(const llvm::Triple &Triple) {
6773
LangOpts.setTarget(Triple);
68-
updateRuntimeLibraryPath(SearchPathOpts, LangOpts.Target);
74+
updateRuntimeLibraryPaths(SearchPathOpts, LangOpts.Target);
75+
}
76+
77+
void CompilerInvocation::setSDKPath(const std::string &Path) {
78+
SearchPathOpts.SDKPath = Path;
79+
updateRuntimeLibraryPaths(SearchPathOpts, LangOpts.Target);
6980
}
7081

7182
SourceFileKind CompilerInvocation::getSourceFileKind() const {
@@ -558,7 +569,7 @@ static bool ParseSearchPathArgs(SearchPathOptions &Opts,
558569
if (const Arg *A = Args.getLastArg(OPT_resource_dir))
559570
Opts.RuntimeResourcePath = A->getValue();
560571

561-
Opts.SkipRuntimeLibraryImportPath |= Args.hasArg(OPT_nostdimport);
572+
Opts.SkipRuntimeLibraryImportPaths |= Args.hasArg(OPT_nostdimport);
562573

563574
// Opts.RuntimeIncludePath is set by calls to
564575
// setRuntimeIncludePath() or setMainExecutablePath().
@@ -1270,7 +1281,7 @@ bool CompilerInvocation::parseArgs(
12701281
return true;
12711282
}
12721283

1273-
updateRuntimeLibraryPath(SearchPathOpts, LangOpts.Target);
1284+
updateRuntimeLibraryPaths(SearchPathOpts, LangOpts.Target);
12741285

12751286
return false;
12761287
}

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -278,24 +278,35 @@ SerializedModuleLoaderBase::findModule(AccessPathElem moduleID,
278278
}
279279
}
280280

281-
// If we're not allowed to look in the runtime library import path, stop.
282-
if (Ctx.SearchPathOpts.SkipRuntimeLibraryImportPath)
283-
return false;
284-
285-
// Search the runtime import path.
281+
// Search the runtime import paths.
286282
isFramework = false;
287-
currPath = Ctx.SearchPathOpts.RuntimeLibraryImportPath;
288-
if (Ctx.LangOpts.Target.isOSDarwin()) {
289-
// Apple platforms always use architecture-specific files within a
290-
// .swiftmodule directory for the stdlib.
291-
llvm::sys::path::append(currPath, fileNames.module.str());
292-
return findTargetSpecificModuleFiles().getValueOr(false);
283+
284+
// Apple platforms always use target-specific files within a
285+
// .swiftmodule directory for the stdlib; non-Apple platforms
286+
// always use single-architecture swiftmodules.
287+
// We could move the `if` outside of the `for` instead of inside,
288+
// but I/O will be so slow that we won't notice the difference,
289+
// so it's not worth the code duplication.
290+
bool hasTargetSpecificRuntimeModules = Ctx.LangOpts.Target.isOSDarwin();
291+
292+
for (auto RuntimeLibraryImportPath :
293+
Ctx.SearchPathOpts.RuntimeLibraryImportPaths) {
294+
currPath = RuntimeLibraryImportPath;
295+
if (hasTargetSpecificRuntimeModules) {
296+
llvm::sys::path::append(currPath, fileNames.module.str());
297+
if (auto outcome = findTargetSpecificModuleFiles())
298+
return *outcome;
299+
} else {
300+
auto result = findModuleFilesInDirectory(moduleID, currPath,
301+
fileNames.module.str(),
302+
fileNames.moduleDoc.str(),
303+
moduleBuffer, moduleDocBuffer);
304+
if (!result)
305+
return true;
306+
}
293307
}
294-
// Non-Apple platforms always use single-architecture swiftmodules.
295-
return !findModuleFilesInDirectory(moduleID, currPath,
296-
fileNames.module.str(),
297-
fileNames.moduleDoc.str(),
298-
moduleBuffer, moduleDocBuffer);
308+
309+
return false;
299310
}
300311

301312
static std::pair<StringRef, clang::VersionTuple>

0 commit comments

Comments
 (0)