Skip to content

Commit 1224535

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 49aea31 commit 1224535

File tree

7 files changed

+113
-105
lines changed

7 files changed

+113
-105
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
@@ -2201,12 +2201,6 @@ Identifier ASTContext::getRealModuleName(Identifier key, ModuleAliasLookupOption
22012201
return value.first;
22022202
}
22032203

2204-
std::vector<std::string> ASTContext::getDarwinImplicitFrameworkSearchPaths()
2205-
const {
2206-
assert(LangOpts.Target.isOSDarwin());
2207-
return SearchPathOpts.getDarwinImplicitFrameworkSearchPaths();
2208-
}
2209-
22102204
void ASTContext::loadExtensions(NominalTypeDecl *nominal,
22112205
unsigned previousGeneration) {
22122206
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: 41 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.SkipAllImplicitImportPaths) {
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,35 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
300266
SearchPathOpts.setRuntimeLibraryImportPaths(RuntimeLibraryImportPaths);
301267
}
302268

269+
static void
270+
updateImplicitFrameworkSearchPaths(SearchPathOptions &SearchPathOpts,
271+
const LangOptions &LangOpts) {
272+
if (SearchPathOpts.SkipAllImplicitImportPaths) {
273+
SearchPathOpts.setImplicitFrameworkSearchPaths({});
274+
return;
275+
}
276+
277+
std::vector<std::string> ImplicitFrameworkSearchPaths;
278+
if (LangOpts.Target.isOSDarwin()) {
279+
if (!SearchPathOpts.SkipSDKImportPaths &&
280+
!SearchPathOpts.getSDKPath().empty()) {
281+
StringRef SDKPath = SearchPathOpts.getSDKPath();
282+
SmallString<128> systemFrameworksScratch(SDKPath);
283+
llvm::sys::path::append(systemFrameworksScratch, "System", "Library",
284+
"Frameworks");
285+
SmallString<128> systemSubFrameworksScratch(SDKPath);
286+
llvm::sys::path::append(systemSubFrameworksScratch, "System", "Library",
287+
"SubFrameworks");
288+
SmallString<128> frameworksScratch(SDKPath);
289+
llvm::sys::path::append(frameworksScratch, "Library", "Frameworks");
290+
ImplicitFrameworkSearchPaths = {systemFrameworksScratch.str().str(),
291+
systemSubFrameworksScratch.str().str(),
292+
frameworksScratch.str().str()};
293+
}
294+
}
295+
SearchPathOpts.setImplicitFrameworkSearchPaths(ImplicitFrameworkSearchPaths);
296+
}
297+
303298
static void
304299
setIRGenOutputOptsFromFrontendOptions(IRGenOptions &IRGenOpts,
305300
const FrontendOptions &FrontendOpts) {
@@ -411,11 +406,13 @@ void CompilerInvocation::setTargetTriple(StringRef Triple) {
411406
void CompilerInvocation::setTargetTriple(const llvm::Triple &Triple) {
412407
LangOpts.setTarget(Triple);
413408
updateRuntimeLibraryPaths(SearchPathOpts, FrontendOpts, LangOpts);
409+
updateImplicitFrameworkSearchPaths(SearchPathOpts, LangOpts);
414410
}
415411

416412
void CompilerInvocation::setSDKPath(const std::string &Path) {
417413
SearchPathOpts.setSDKPath(Path);
418414
updateRuntimeLibraryPaths(SearchPathOpts, FrontendOpts, LangOpts);
415+
updateImplicitFrameworkSearchPaths(SearchPathOpts, LangOpts);
419416
}
420417

421418
bool CompilerInvocation::setModuleAliasMap(std::vector<std::string> args,
@@ -2387,8 +2384,8 @@ static bool ParseSearchPathArgs(SearchPathOptions &Opts, ArgList &Args,
23872384
if (const Arg *A = Args.getLastArg(OPT_resource_dir))
23882385
Opts.RuntimeResourcePath = A->getValue();
23892386

2390-
Opts.SkipRuntimeLibraryImportPaths |= Args.hasArg(OPT_nostdimport);
2391-
Opts.ExcludeSDKPathsFromRuntimeLibraryImportPaths |= Args.hasArg(OPT_nostdlibimport);
2387+
Opts.SkipAllImplicitImportPaths |= Args.hasArg(OPT_nostdimport);
2388+
Opts.SkipSDKImportPaths |= Args.hasArg(OPT_nostdlibimport);
23922389

23932390
Opts.DisableModulesValidateSystemDependencies |=
23942391
Args.hasArg(OPT_disable_modules_validate_system_headers);
@@ -4056,6 +4053,7 @@ bool CompilerInvocation::parseArgs(
40564053
}
40574054

40584055
updateRuntimeLibraryPaths(SearchPathOpts, FrontendOpts, LangOpts);
4056+
updateImplicitFrameworkSearchPaths(SearchPathOpts, LangOpts);
40594057
setDefaultPrebuiltCacheIfNecessary();
40604058
setDefaultBlocklistsIfNecessary();
40614059
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)