Skip to content

Commit 5569606

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 ff790c6 commit 5569606

File tree

7 files changed

+106
-100
lines changed

7 files changed

+106
-100
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: 14 additions & 23 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

@@ -361,7 +361,7 @@ class SearchPathOptions {
361361
/// Computed when the SDK path is set and cached so we can reference the
362362
/// Darwin implicit framework search paths as \c StringRef from
363363
/// \c ModuleSearchPath.
364-
std::vector<std::string> DarwinImplicitFrameworkSearchPaths;
364+
std::vector<std::string> ImplicitFrameworkSearchPaths;
365365

366366
/// Compiler plugin library search paths.
367367
std::vector<std::string> CompilerPluginLibraryPaths;
@@ -401,21 +401,6 @@ class SearchPathOptions {
401401

402402
void setSDKPath(std::string NewSDKPath) {
403403
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();
419404
}
420405

421406
/// Retrieves the corresponding parent platform path for the SDK, or
@@ -470,8 +455,14 @@ class SearchPathOptions {
470455

471456
/// The extra implicit framework search paths on Apple platforms:
472457
/// $SDKROOT/System/Library/Frameworks/ and $SDKROOT/Library/Frameworks/.
473-
ArrayRef<std::string> getDarwinImplicitFrameworkSearchPaths() const {
474-
return DarwinImplicitFrameworkSearchPaths;
458+
ArrayRef<std::string> getImplicitFrameworkSearchPaths() const {
459+
return ImplicitFrameworkSearchPaths;
460+
}
461+
462+
void setImplicitFrameworkSearchPaths(
463+
std::vector<std::string> NewImplicitFrameworkSearchPaths) {
464+
ImplicitFrameworkSearchPaths = NewImplicitFrameworkSearchPaths;
465+
Lookup.searchPathsDidChange();
475466
}
476467

477468
ArrayRef<std::string> getRuntimeLibraryImportPaths() const {
@@ -505,11 +496,11 @@ class SearchPathOptions {
505496
/// Path to in-process plugin server shared library.
506497
std::string InProcessPluginServerPath;
507498

508-
/// Don't look in for compiler-provided modules.
509-
bool SkipRuntimeLibraryImportPaths = false;
499+
/// Don't automatically add any import paths.
500+
bool SkipAllImportPaths = false;
510501

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

514505
/// Scanner Prefix Mapper.
515506
std::vector<std::string> ScannerPrefixMapper;

lib/AST/ASTContext.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2208,12 +2208,6 @@ namespace {
22082208
}
22092209
}
22102210

2211-
std::vector<std::string> ASTContext::getDarwinImplicitFrameworkSearchPaths()
2212-
const {
2213-
assert(LangOpts.Target.isOSDarwin());
2214-
return SearchPathOpts.getDarwinImplicitFrameworkSearchPaths();
2215-
}
2216-
22172211
void ASTContext::loadExtensions(NominalTypeDecl *nominal,
22182212
unsigned previousGeneration) {
22192213
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: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@
4343
using namespace swift;
4444
using namespace llvm::opt;
4545

46-
/// The path for Swift libraries in the OS on Darwin.
47-
#define DARWIN_OS_LIBRARY_PATH "/usr/lib/swift"
48-
4946
static constexpr const char *const localeCodes[] = {
5047
#define SUPPORTED_LOCALE(Code, Language) #Code,
5148
#include "swift/AST/LocalizationLanguages.def"
@@ -211,6 +208,12 @@ void CompilerInvocation::setDefaultInProcessPluginServerPathIfNecessary() {
211208
static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
212209
const FrontendOptions &FrontendOpts,
213210
const LangOptions &LangOpts) {
211+
// If this is set, we don't want any runtime import paths.
212+
if (SearchPathOpts.SkipAllImportPaths) {
213+
SearchPathOpts.setRuntimeLibraryImportPaths({});
214+
return;
215+
}
216+
214217
const llvm::Triple &Triple = LangOpts.Target;
215218
llvm::SmallString<128> LibPath(SearchPathOpts.RuntimeResourcePath);
216219

@@ -220,44 +223,7 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
220223
if (LangOpts.hasFeature(Feature::Embedded))
221224
LibSubDir = "embedded";
222225

223-
SearchPathOpts.RuntimeLibraryPaths.clear();
224-
225-
#if defined(_WIN32)
226-
// Resource path looks like this:
227-
//
228-
// C:\...\Swift\Toolchains\6.0.0+Asserts\usr\lib\swift
229-
//
230-
// The runtimes are in
231-
//
232-
// C:\...\Swift\Runtimes\6.0.0\usr\bin
233-
//
234-
llvm::SmallString<128> RuntimePath(LibPath);
235-
236-
llvm::sys::path::remove_filename(RuntimePath);
237-
llvm::sys::path::remove_filename(RuntimePath);
238-
llvm::sys::path::remove_filename(RuntimePath);
239-
240-
llvm::SmallString<128> VersionWithAttrs(llvm::sys::path::filename(RuntimePath));
241-
size_t MaybePlus = VersionWithAttrs.find_first_of('+');
242-
StringRef Version = VersionWithAttrs.substr(0, MaybePlus);
243-
244-
llvm::sys::path::remove_filename(RuntimePath);
245-
llvm::sys::path::remove_filename(RuntimePath);
246-
llvm::sys::path::append(RuntimePath, "Runtimes", Version, "usr", "bin");
247-
248-
SearchPathOpts.RuntimeLibraryPaths.push_back(std::string(RuntimePath.str()));
249-
#endif
250-
251226
llvm::sys::path::append(LibPath, LibSubDir);
252-
SearchPathOpts.RuntimeLibraryPaths.push_back(std::string(LibPath.str()));
253-
if (Triple.isOSDarwin())
254-
SearchPathOpts.RuntimeLibraryPaths.push_back(DARWIN_OS_LIBRARY_PATH);
255-
256-
// If this is set, we don't want any runtime import paths.
257-
if (SearchPathOpts.SkipRuntimeLibraryImportPaths) {
258-
SearchPathOpts.setRuntimeLibraryImportPaths({});
259-
return;
260-
}
261227

262228
// Set up the import paths containing the swiftmodules for the libraries in
263229
// RuntimeLibraryPath.
@@ -270,7 +236,7 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
270236
RuntimeLibraryImportPaths.push_back(std::string(LibPath.str()));
271237
}
272238

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

@@ -300,6 +266,31 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
300266
SearchPathOpts.setRuntimeLibraryImportPaths(RuntimeLibraryImportPaths);
301267
}
302268

269+
static void
270+
updateImplicitFrameworkSearchPaths(SearchPathOptions &SearchPathOpts,
271+
const LangOptions &LangOpts) {
272+
std::vector<std::string> ImplicitFrameworkSearchPaths;
273+
if (!SearchPathOpts.SkipAllImportPaths &&
274+
!SearchPathOpts.SkipSDKImportPaths &&
275+
!SearchPathOpts.getSDKPath().empty()) {
276+
if (LangOpts.Target.isOSDarwin()) {
277+
StringRef SDKPath = SearchPathOpts.getSDKPath();
278+
SmallString<128> systemFrameworksScratch(SDKPath);
279+
llvm::sys::path::append(systemFrameworksScratch, "System", "Library",
280+
"Frameworks");
281+
SmallString<128> systemSubFrameworksScratch(SDKPath);
282+
llvm::sys::path::append(systemSubFrameworksScratch, "System", "Library",
283+
"SubFrameworks");
284+
SmallString<128> frameworksScratch(SDKPath);
285+
llvm::sys::path::append(frameworksScratch, "Library", "Frameworks");
286+
ImplicitFrameworkSearchPaths = {systemFrameworksScratch.str().str(),
287+
systemSubFrameworksScratch.str().str(),
288+
frameworksScratch.str().str()};
289+
}
290+
}
291+
SearchPathOpts.setImplicitFrameworkSearchPaths(ImplicitFrameworkSearchPaths);
292+
}
293+
303294
static void
304295
setIRGenOutputOptsFromFrontendOptions(IRGenOptions &IRGenOpts,
305296
const FrontendOptions &FrontendOpts) {
@@ -411,11 +402,13 @@ void CompilerInvocation::setTargetTriple(StringRef Triple) {
411402
void CompilerInvocation::setTargetTriple(const llvm::Triple &Triple) {
412403
LangOpts.setTarget(Triple);
413404
updateRuntimeLibraryPaths(SearchPathOpts, FrontendOpts, LangOpts);
405+
updateImplicitFrameworkSearchPaths(SearchPathOpts, LangOpts);
414406
}
415407

416408
void CompilerInvocation::setSDKPath(const std::string &Path) {
417409
SearchPathOpts.setSDKPath(Path);
418410
updateRuntimeLibraryPaths(SearchPathOpts, FrontendOpts, LangOpts);
411+
updateImplicitFrameworkSearchPaths(SearchPathOpts, LangOpts);
419412
}
420413

421414
bool CompilerInvocation::setModuleAliasMap(std::vector<std::string> args,
@@ -2387,8 +2380,8 @@ static bool ParseSearchPathArgs(SearchPathOptions &Opts, ArgList &Args,
23872380
if (const Arg *A = Args.getLastArg(OPT_resource_dir))
23882381
Opts.RuntimeResourcePath = A->getValue();
23892382

2390-
Opts.SkipRuntimeLibraryImportPaths |= Args.hasArg(OPT_nostdimport);
2391-
Opts.ExcludeSDKPathsFromRuntimeLibraryImportPaths |= Args.hasArg(OPT_nostdlibimport);
2383+
Opts.SkipAllImportPaths |= Args.hasArg(OPT_nostdimport);
2384+
Opts.SkipSDKImportPaths |= Args.hasArg(OPT_nostdlibimport);
23922385

23932386
Opts.DisableModulesValidateSystemDependencies |=
23942387
Args.hasArg(OPT_disable_modules_validate_system_headers);
@@ -4043,6 +4036,7 @@ bool CompilerInvocation::parseArgs(
40434036
}
40444037

40454038
updateRuntimeLibraryPaths(SearchPathOpts, FrontendOpts, LangOpts);
4039+
updateImplicitFrameworkSearchPaths(SearchPathOpts, LangOpts);
40464040
setDefaultPrebuiltCacheIfNecessary();
40474041
setDefaultBlocklistsIfNecessary();
40484042
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-darwin24.4 -typecheck %s -Rmodule-loading 2>&1 | %FileCheck -check-prefix=APPLE %s
3+
// APPLE: Implicit framework search paths:
4+
// APPLE-NEXT: [0] %clang-importer-sdk-path/System/Library/Frameworks
5+
// APPLE-NEXT: [1] %clang-importer-sdk-path/System/Library/SubFrameworks
6+
// APPLE-NEXT: [2] %clang-importer-sdk-path/Library/Frameworks
7+
// APPLE-NEXT: Runtime library import search paths:
8+
// APPLE-NEXT: [0] %platform-module-dir
9+
// APPLE-NEXT: [1] %clang-importer-sdk-path/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 -typecheck %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] %platform-module-dir
17+
// ANDROID-NEXT: [1] %platform-module-dir/x86_64
18+
// ANDROID-NEXT: [2] %clang-importer-sdk-path/usr/lib/swift/android
19+
// ANDROID-NEXT: [3] %clang-importer-sdk-path/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-darwin24.4 -nostdimport -typecheck %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 -typecheck %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-darwin24.4 -nostdlibimport -typecheck %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] %platform-module-dir
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 -typecheck %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] %platform-module-dir
40+
// ANDROID-NOSTDLIBIMPORT-NEXT: [1] %platform-module-dir/x86_64
41+
// ANDROID-NOSTDLIBIMPORT-NEXT: (End of search path lists.)

0 commit comments

Comments
 (0)