Skip to content

Commit a9c3185

Browse files
[Driver][Frontend] -nostdimport and -nostdlibimport should remove the default framework search paths
-nostdimport and -nostdlibimport only remove the toolchain and usr/lib/swift search paths, and they leave the framework search paths intact. That makes it impossible to get a fully custom SDK environment. Make their behavior match clang's -nostdinc/-nostdlibinc behavior: treat framework and non-framework paths the same. In other words, -nostdinc removes *all* compiler provided search paths, and -nostdlibinc removes *all* SDK search paths. Rename SkipRuntimeLibraryImportPaths to SkipAllImportPaths, and ExcludeSDKPathsFromRuntimeLibraryImportPaths to SkipSDKImportPaths to reflect their updated behavior. Move the DarwinImplicitFrameworkSearchPaths handling from SearchPathOptions to CompilerInvocation, where RuntimeLibraryImportPaths is managed. Rename it to just ImplicitFrameworkSearchPaths, and filter for Darwin when it's set up so that all of the clients don't have to do Darwin filtering themselves later. rdar://150557632
1 parent 8ab726c commit a9c3185

File tree

7 files changed

+108
-66
lines changed

7 files changed

+108
-66
lines changed

include/swift/AST/ASTContext.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,10 +1093,6 @@ class ASTContext final {
10931093
/// Retrieve the module interface checker associated with this AST context.
10941094
ModuleInterfaceChecker *getModuleInterfaceChecker() const;
10951095

1096-
/// Compute the extra implicit framework search paths on Apple platforms:
1097-
/// $SDKROOT/System/Library/Frameworks/ and $SDKROOT/Library/Frameworks/.
1098-
std::vector<std::string> getDarwinImplicitFrameworkSearchPaths() const;
1099-
11001096
/// Load extensions to the given nominal type from the external
11011097
/// module loaders.
11021098
///

include/swift/AST/SearchPathOptions.h

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace swift {
3535
enum class ModuleSearchPathKind {
3636
Import,
3737
Framework,
38-
DarwinImplicitFramework,
38+
ImplicitFramework,
3939
RuntimeLibrary,
4040
};
4141

@@ -356,12 +356,8 @@ class SearchPathOptions {
356356
/// When on Darwin the framework paths that are implicitly imported.
357357
/// $SDKROOT/System/Library/Frameworks/ and $SDKROOT/Library/Frameworks/.
358358
///
359-
/// On non-Darwin platforms these are populated, but ignored.
360-
///
361-
/// Computed when the SDK path is set and cached so we can reference the
362-
/// Darwin implicit framework search paths as \c StringRef from
363-
/// \c ModuleSearchPath.
364-
std::vector<std::string> DarwinImplicitFrameworkSearchPaths;
359+
/// Must be modified through setter to keep \c Lookup in sync.
360+
std::vector<std::string> ImplicitFrameworkSearchPaths;
365361

366362
/// Compiler plugin library search paths.
367363
std::vector<std::string> CompilerPluginLibraryPaths;
@@ -401,21 +397,6 @@ class SearchPathOptions {
401397

402398
void setSDKPath(std::string NewSDKPath) {
403399
SDKPath = NewSDKPath;
404-
405-
// Compute Darwin implicit framework search paths.
406-
SmallString<128> systemFrameworksScratch(NewSDKPath);
407-
llvm::sys::path::append(systemFrameworksScratch, "System", "Library",
408-
"Frameworks");
409-
SmallString<128> systemSubFrameworksScratch(NewSDKPath);
410-
llvm::sys::path::append(systemSubFrameworksScratch, "System", "Library",
411-
"SubFrameworks");
412-
SmallString<128> frameworksScratch(NewSDKPath);
413-
llvm::sys::path::append(frameworksScratch, "Library", "Frameworks");
414-
DarwinImplicitFrameworkSearchPaths = {systemFrameworksScratch.str().str(),
415-
systemSubFrameworksScratch.str().str(),
416-
frameworksScratch.str().str()};
417-
418-
Lookup.searchPathsDidChange();
419400
}
420401

421402
/// Retrieves the corresponding parent platform path for the SDK, or
@@ -470,8 +451,14 @@ class SearchPathOptions {
470451

471452
/// The extra implicit framework search paths on Apple platforms:
472453
/// $SDKROOT/System/Library/Frameworks/ and $SDKROOT/Library/Frameworks/.
473-
ArrayRef<std::string> getDarwinImplicitFrameworkSearchPaths() const {
474-
return DarwinImplicitFrameworkSearchPaths;
454+
ArrayRef<std::string> getImplicitFrameworkSearchPaths() const {
455+
return ImplicitFrameworkSearchPaths;
456+
}
457+
458+
void setImplicitFrameworkSearchPaths(
459+
std::vector<std::string> NewImplicitFrameworkSearchPaths) {
460+
ImplicitFrameworkSearchPaths = NewImplicitFrameworkSearchPaths;
461+
Lookup.searchPathsDidChange();
475462
}
476463

477464
ArrayRef<std::string> getRuntimeLibraryImportPaths() const {
@@ -505,11 +492,11 @@ class SearchPathOptions {
505492
/// Path to in-process plugin server shared library.
506493
std::string InProcessPluginServerPath;
507494

508-
/// Don't look in for compiler-provided modules.
509-
bool SkipRuntimeLibraryImportPaths = false;
495+
/// Don't automatically add any import paths.
496+
bool SkipAllImplicitImportPaths = false;
510497

511-
/// Don't include SDK paths in the RuntimeLibraryImportPaths
512-
bool ExcludeSDKPathsFromRuntimeLibraryImportPaths = false;
498+
/// Don't automatically add any import paths from the SDK.
499+
bool SkipSDKImportPaths = false;
513500

514501
/// Scanner Prefix Mapper.
515502
std::vector<std::string> ScannerPrefixMapper;
@@ -619,6 +606,8 @@ class SearchPathOptions {
619606
RuntimeResourcePath,
620607
hash_combine_range(RuntimeLibraryImportPaths.begin(),
621608
RuntimeLibraryImportPaths.end()),
609+
hash_combine_range(ImplicitFrameworkSearchPaths.begin(),
610+
ImplicitFrameworkSearchPaths.end()),
622611
DisableModulesValidateSystemDependencies,
623612
ScannerModuleValidation,
624613
ModuleLoadMode);

lib/AST/ASTContext.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,12 +2200,6 @@ Identifier ASTContext::getRealModuleName(Identifier key, ModuleAliasLookupOption
22002200
return value.first;
22012201
}
22022202

2203-
std::vector<std::string> ASTContext::getDarwinImplicitFrameworkSearchPaths()
2204-
const {
2205-
assert(LangOpts.Target.isOSDarwin());
2206-
return SearchPathOpts.getDarwinImplicitFrameworkSearchPaths();
2207-
}
2208-
22092203
void ASTContext::loadExtensions(NominalTypeDecl *nominal,
22102204
unsigned previousGeneration) {
22112205
PrettyStackTraceDecl stackTrace("loading extensions for", nominal);

lib/AST/SearchPathOptions.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,10 @@ void ModuleSearchPathLookup::rebuildLookupTable(const SearchPathOptions *Opts,
5858
Entry.value().IsSystem, Entry.index());
5959
}
6060

61-
// Apple platforms have extra implicit framework search paths:
62-
// $SDKROOT/System/Library/Frameworks/ and $SDKROOT/Library/Frameworks/.
63-
if (IsOSDarwin) {
64-
for (auto Entry : llvm::enumerate(Opts->getDarwinImplicitFrameworkSearchPaths())) {
65-
addFilesInPathToLookupTable(FS, Entry.value(),
66-
ModuleSearchPathKind::DarwinImplicitFramework,
67-
/*isSystem=*/true, Entry.index());
68-
}
61+
for (auto Entry : llvm::enumerate(Opts->getImplicitFrameworkSearchPaths())) {
62+
addFilesInPathToLookupTable(FS, Entry.value(),
63+
ModuleSearchPathKind::ImplicitFramework,
64+
/*isSystem=*/true, Entry.index());
6965
}
7066

7167
for (auto Entry : llvm::enumerate(Opts->getRuntimeLibraryImportPaths())) {
@@ -123,12 +119,9 @@ void SearchPathOptions::dump(bool isDarwin) const {
123119
<< Entry.value().Path << "\n";
124120
}
125121

126-
if (isDarwin) {
127-
llvm::errs() << "Darwin implicit framework search paths:\n";
128-
for (auto Entry :
129-
llvm::enumerate(getDarwinImplicitFrameworkSearchPaths())) {
130-
llvm::errs() << " [" << Entry.index() << "] " << Entry.value() << "\n";
131-
}
122+
llvm::errs() << "Implicit framework search paths:\n";
123+
for (auto Entry : llvm::enumerate(getImplicitFrameworkSearchPaths())) {
124+
llvm::errs() << " [" << Entry.index() << "] " << Entry.value() << "\n";
132125
}
133126

134127
llvm::errs() << "Runtime library import search paths:\n";

lib/Frontend/CompilerInvocation.cpp

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
254254
SearchPathOpts.RuntimeLibraryPaths.push_back(DARWIN_OS_LIBRARY_PATH);
255255

256256
// If this is set, we don't want any runtime import paths.
257-
if (SearchPathOpts.SkipRuntimeLibraryImportPaths) {
257+
if (SearchPathOpts.SkipAllImplicitImportPaths) {
258258
SearchPathOpts.setRuntimeLibraryImportPaths({});
259259
return;
260260
}
@@ -270,7 +270,7 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
270270
RuntimeLibraryImportPaths.push_back(std::string(LibPath.str()));
271271
}
272272

273-
if (!SearchPathOpts.ExcludeSDKPathsFromRuntimeLibraryImportPaths && !SearchPathOpts.getSDKPath().empty()) {
273+
if (!SearchPathOpts.SkipSDKImportPaths && !SearchPathOpts.getSDKPath().empty()) {
274274
const char *swiftDir = FrontendOpts.UseSharedResourceFolder
275275
? "swift" : "swift_static";
276276

@@ -300,6 +300,35 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
300300
SearchPathOpts.setRuntimeLibraryImportPaths(RuntimeLibraryImportPaths);
301301
}
302302

303+
static void
304+
updateImplicitFrameworkSearchPaths(SearchPathOptions &SearchPathOpts,
305+
const LangOptions &LangOpts) {
306+
if (SearchPathOpts.SkipAllImplicitImportPaths) {
307+
SearchPathOpts.setImplicitFrameworkSearchPaths({});
308+
return;
309+
}
310+
311+
std::vector<std::string> ImplicitFrameworkSearchPaths;
312+
if (LangOpts.Target.isOSDarwin()) {
313+
if (!SearchPathOpts.SkipSDKImportPaths &&
314+
!SearchPathOpts.getSDKPath().empty()) {
315+
StringRef SDKPath = SearchPathOpts.getSDKPath();
316+
SmallString<128> systemFrameworksScratch(SDKPath);
317+
llvm::sys::path::append(systemFrameworksScratch, "System", "Library",
318+
"Frameworks");
319+
SmallString<128> systemSubFrameworksScratch(SDKPath);
320+
llvm::sys::path::append(systemSubFrameworksScratch, "System", "Library",
321+
"SubFrameworks");
322+
SmallString<128> frameworksScratch(SDKPath);
323+
llvm::sys::path::append(frameworksScratch, "Library", "Frameworks");
324+
ImplicitFrameworkSearchPaths = {systemFrameworksScratch.str().str(),
325+
systemSubFrameworksScratch.str().str(),
326+
frameworksScratch.str().str()};
327+
}
328+
}
329+
SearchPathOpts.setImplicitFrameworkSearchPaths(ImplicitFrameworkSearchPaths);
330+
}
331+
303332
static void
304333
setIRGenOutputOptsFromFrontendOptions(IRGenOptions &IRGenOpts,
305334
const FrontendOptions &FrontendOpts) {
@@ -411,11 +440,13 @@ void CompilerInvocation::setTargetTriple(StringRef Triple) {
411440
void CompilerInvocation::setTargetTriple(const llvm::Triple &Triple) {
412441
LangOpts.setTarget(Triple);
413442
updateRuntimeLibraryPaths(SearchPathOpts, FrontendOpts, LangOpts);
443+
updateImplicitFrameworkSearchPaths(SearchPathOpts, LangOpts);
414444
}
415445

416446
void CompilerInvocation::setSDKPath(const std::string &Path) {
417447
SearchPathOpts.setSDKPath(Path);
418448
updateRuntimeLibraryPaths(SearchPathOpts, FrontendOpts, LangOpts);
449+
updateImplicitFrameworkSearchPaths(SearchPathOpts, LangOpts);
419450
}
420451

421452
bool CompilerInvocation::setModuleAliasMap(std::vector<std::string> args,
@@ -2387,8 +2418,8 @@ static bool ParseSearchPathArgs(SearchPathOptions &Opts, ArgList &Args,
23872418
if (const Arg *A = Args.getLastArg(OPT_resource_dir))
23882419
Opts.RuntimeResourcePath = A->getValue();
23892420

2390-
Opts.SkipRuntimeLibraryImportPaths |= Args.hasArg(OPT_nostdimport);
2391-
Opts.ExcludeSDKPathsFromRuntimeLibraryImportPaths |= Args.hasArg(OPT_nostdlibimport);
2421+
Opts.SkipAllImplicitImportPaths |= Args.hasArg(OPT_nostdimport);
2422+
Opts.SkipSDKImportPaths |= Args.hasArg(OPT_nostdlibimport);
23922423

23932424
Opts.DisableModulesValidateSystemDependencies |=
23942425
Args.hasArg(OPT_disable_modules_validate_system_headers);
@@ -4056,6 +4087,7 @@ bool CompilerInvocation::parseArgs(
40564087
}
40574088

40584089
updateRuntimeLibraryPaths(SearchPathOpts, FrontendOpts, LangOpts);
4090+
updateImplicitFrameworkSearchPaths(SearchPathOpts, LangOpts);
40594091
setDefaultPrebuiltCacheIfNecessary();
40604092
setDefaultBlocklistsIfNecessary();
40614093
setDefaultInProcessPluginServerPathIfNecessary();

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,11 @@ std::optional<bool> forEachModuleSearchPath(
108108
callback(path.Path, ModuleSearchPathKind::Framework, path.IsSystem))
109109
return result;
110110

111-
// Apple platforms have extra implicit framework search paths:
112-
// $SDKROOT/System/Library/Frameworks/ and $SDKROOT/Library/Frameworks/.
113-
if (Ctx.LangOpts.Target.isOSDarwin()) {
114-
for (const auto &path : Ctx.getDarwinImplicitFrameworkSearchPaths())
115-
if (auto result =
116-
callback(path, ModuleSearchPathKind::DarwinImplicitFramework,
117-
/*isSystem=*/true))
118-
return result;
111+
for (const auto &path :
112+
Ctx.SearchPathOpts.getImplicitFrameworkSearchPaths()) {
113+
if (auto result = callback(path, ModuleSearchPathKind::ImplicitFramework,
114+
/*isSystem=*/true))
115+
return result;
119116
}
120117

121118
for (const auto &importPath :
@@ -240,7 +237,7 @@ void SerializedModuleLoaderBase::collectVisibleTopLevelModuleNamesImpl(
240237
return std::nullopt;
241238
}
242239
case ModuleSearchPathKind::Framework:
243-
case ModuleSearchPathKind::DarwinImplicitFramework: {
240+
case ModuleSearchPathKind::ImplicitFramework: {
244241
// Look for:
245242
// $PATH/{name}.framework/Modules/{name}.swiftmodule/{arch}.{extension}
246243
forEachDirectoryEntryPath(searchPath, [&](StringRef path) {
@@ -964,7 +961,7 @@ bool SerializedModuleLoaderBase::findModule(
964961
continue;
965962
}
966963
case ModuleSearchPathKind::Framework:
967-
case ModuleSearchPathKind::DarwinImplicitFramework: {
964+
case ModuleSearchPathKind::ImplicitFramework: {
968965
isFramework = true;
969966
llvm::sys::path::append(currPath, moduleName + ".framework");
970967

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Standard Apple paths.
2+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource) -target arm64-apple-macos15.4 -parse %s -Rmodule-loading 2>&1 | %FileCheck -check-prefix=APPLE %s
3+
// APPLE: Implicit framework search paths:
4+
// APPLE-NEXT: [0] SOURCE_DIR/test/Inputs/clang-importer-sdk/System/Library/Frameworks
5+
// APPLE-NEXT: [1] SOURCE_DIR/test/Inputs/clang-importer-sdk/System/Library/SubFrameworks
6+
// APPLE-NEXT: [2] SOURCE_DIR/test/Inputs/clang-importer-sdk/Library/Frameworks
7+
// APPLE-NEXT: Runtime library import search paths:
8+
// APPLE-NEXT: [0] BUILD_DIR/lib/swift/macosx
9+
// APPLE-NEXT: [1] SOURCE_DIR/test/Inputs/clang-importer-sdk/usr/lib/swift
10+
// APPLE-NEXT: (End of search path lists.)
11+
12+
// Non-Apple platforms don't have any implicit framework search paths.
13+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource) -target x86_64-unknown-linux-android -parse %s -Rmodule-loading 2>&1 | %FileCheck -check-prefix=ANDROID %s
14+
// ANDROID: Implicit framework search paths:
15+
// ANDROID-NEXT: Runtime library import search paths:
16+
// ANDROID-NEXT: [0] BUILD_DIR/lib/swift/android
17+
// ANDROID-NEXT: [1] BUILD_DIR/lib/swift/android/x86_64
18+
// ANDROID-NEXT: [2] SOURCE_DIR/test/Inputs/clang-importer-sdk/usr/lib/swift/android
19+
// ANDROID-NEXT: [3] SOURCE_DIR/test/Inputs/clang-importer-sdk/usr/lib/swift/android/x86_64
20+
// ANDROID-NEXT: (End of search path lists.)
21+
22+
// -nostdimport doesn't set up any standard import paths at all.
23+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource) -target arm64-apple-macos15.4 -nostdimport -parse %s -Rmodule-loading 2>&1 | %FileCheck -check-prefix=NOSTDIMPORT %s
24+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource) -target x86_64-unknown-linux-android -nostdimport -parse %s -Rmodule-loading 2>&1 | %FileCheck -check-prefix=NOSTDIMPORT %s
25+
// NOSTDIMPORT: Implicit framework search paths:
26+
// NOSTDIMPORT-NEXT: Runtime library import search paths:
27+
// NOSTDIMPORT-NEXT: (End of search path lists.)
28+
29+
// -nostdlibimport removes all of the standard imports from the SDK but leaves the toolchain ones.
30+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource) -target arm64-apple-macos15.4 -nostdlibimport -parse %s -Rmodule-loading 2>&1 | %FileCheck -check-prefix=APPLE-NOSTDLIBIMPORT %s
31+
// APPLE-NOSTDLIBIMPORT: Implicit framework search paths:
32+
// APPLE-NOSTDLIBIMPORT-NEXT: Runtime library import search paths:
33+
// APPLE-NOSTDLIBIMPORT-NEXT: [0] BUILD_DIR/lib/swift/macosx
34+
// APPLE-NOSTDLIBIMPORT-NEXT: (End of search path lists.)
35+
36+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource) -target x86_64-unknown-linux-android -nostdlibimport -parse %s -Rmodule-loading 2>&1 | %FileCheck -check-prefix=ANDROID-NOSTDLIBIMPORT %s
37+
// ANDROID-NOSTDLIBIMPORT: Implicit framework search paths:
38+
// ANDROID-NOSTDLIBIMPORT-NEXT: Runtime library import search paths:
39+
// ANDROID-NOSTDLIBIMPORT-NEXT: [0] BUILD_DIR/lib/swift/android
40+
// ANDROID-NOSTDLIBIMPORT-NEXT: [1] BUILD_DIR/lib/swift/android/x86_64
41+
// ANDROID-NOSTDLIBIMPORT-NEXT: (End of search path lists.)

0 commit comments

Comments
 (0)