Skip to content

Commit fe70b3a

Browse files
committed
First commit
1 parent 7f8505d commit fe70b3a

File tree

7 files changed

+89
-10
lines changed

7 files changed

+89
-10
lines changed

Sources/SWBCore/SpecImplementations/Tools/LinkerTools.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ public final class LdLinkerSpec : GenericLinkerSpec, SpecIdentifierType, @unchec
368368
&& cbc.producer.platform?.minimumOSForSwiftInTheOS != nil {
369369
// NOTE: For swift.org toolchains, this is fine as `DYLD_LIBRARY_PATH` is used to override these settings.
370370
let swiftABIVersion = await (cbc.producer.swiftCompilerSpec.discoveredCommandLineToolSpecInfo(cbc.producer, cbc.scope, delegate) as? DiscoveredSwiftCompilerToolSpecInfo)?.swiftABIVersion
371-
runpathSearchPaths.insert( swiftABIVersion.flatMap { "/usr/lib/swift-\($0)" } ?? "/usr/lib/swift", at: 0)
371+
runpathSearchPaths.insert( swiftABIVersion.flatMap { "/usr/lib/swift-\($0)" } ?? "/usr/lib/swift_static", at: 0)
372372
// Ensure the linker driver does not insert a duplicate rpath (if linking using swiftc)
373373
suppressDriverStdlibPaths = true
374374
}

Sources/SWBCore/SpecImplementations/Tools/SwiftCompiler.swift

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2961,8 +2961,10 @@ public final class SwiftCompilerSpec : CompilerSpec, SpecIdentifierType, SwiftDi
29612961
// NOTE: With Swift in the OS, static libs aren't being supplied by the toolchains
29622962
// so users of this flag will need to provide their own.
29632963
if scope.evaluate(BuiltinMacros.SWIFT_FORCE_STATIC_LINK_STDLIB, lookup: lookup) {
2964+
print("staticallyLinkSwiftStdlib: true")
29642965
return true
29652966
}
2967+
print("staticallyLinkSwiftStdlib: false")
29662968
return false
29672969
}
29682970

@@ -3001,19 +3003,24 @@ public final class SwiftCompilerSpec : CompilerSpec, SpecIdentifierType, SwiftDi
30013003
if swiftLibraryPath.isEmpty {
30023004
// Look next to the compiler and in the toolchains for one.
30033005
if shouldStaticLinkStdlib {
3006+
print("Finding static lib search paths")
30043007
swiftLibraryPath = findSearchPathForLibrary(executablePath: swiftc, possibleNames: [
30053008
"swift_static/\(platformName)/lib\(swiftStdlibName).a",
30063009
"swift_static/lib\(swiftStdlibName).a",
30073010
"lib\(swiftStdlibName).a",
30083011
], toolchains: producer.toolchains) ?? Path("")
30093012
} else {
3013+
print("Finding dynamiclib search paths")
30103014
swiftLibraryPath = findSearchPathForLibrary(executablePath: swiftc, possibleNames: [
30113015
"swift/\(platformName)/lib\(swiftStdlibName).\(dynamicLibraryExtension)",
30123016
"swift/lib\(swiftStdlibName).\(dynamicLibraryExtension)",
30133017
"lib\(swiftStdlibName).\(dynamicLibraryExtension)",
30143018
], toolchains: producer.toolchains) ?? Path("")
30153019
}
3020+
} else {
3021+
print("swiftLibraryPath is not empty.")
30163022
}
3023+
print("********** swiftLibraryPath are: \(swiftLibraryPath)")
30173024

30183025
let isMacCatalystUnzippered = producer.sdkVariant?.isMacCatalyst == true && !scope.evaluate(BuiltinMacros.IS_ZIPPERED, lookup: lookup)
30193026

@@ -3023,12 +3030,12 @@ public final class SwiftCompilerSpec : CompilerSpec, SpecIdentifierType, SwiftDi
30233030
// TAPI requires absolute paths.
30243031
let sdkroot = scope.evaluate(BuiltinMacros.SDKROOT, lookup: lookup)
30253032
if !sdkroot.isEmpty {
3026-
sdkPathArgument = ["-L" + sdkroot.join("usr/lib/swift").str]
3033+
sdkPathArgument = ["-L" + sdkroot.join("usr/lib/swift_static").str]
30273034
unzipperedSDKPathArgument = ["-L" + sdkroot.join("System/iOSSupport/usr/lib/swift").str]
30283035
}
30293036
} else {
30303037
// ld prefers SDK relative paths.
3031-
sdkPathArgument = ["-L/usr/lib/swift"]
3038+
sdkPathArgument = ["-L/usr/lib/swift_static"]
30323039
unzipperedSDKPathArgument = ["-L/System/iOSSupport/usr/lib/swift"]
30333040
}
30343041

@@ -3039,6 +3046,7 @@ public final class SwiftCompilerSpec : CompilerSpec, SpecIdentifierType, SwiftDi
30393046
}
30403047

30413048
args += [sdkPathArgument]
3049+
print("**** SDK Parth arguments: \(sdkPathArgument)")
30423050
}
30433051

30443052
// Add the -L to the standard library path. This is used primarily for the OSS toolchain now as each of the libs have been moved into the Swift SDK.
@@ -3061,9 +3069,20 @@ public final class SwiftCompilerSpec : CompilerSpec, SpecIdentifierType, SwiftDi
30613069

30623070
if !forTAPI {
30633071
if shouldStaticLinkStdlib {
3064-
args += [["-Xlinker", "-force_load_swift_libs"]]
3065-
// The Swift runtime requires libc++ & Foundation.
3066-
args += [["-lc++", "-framework", "Foundation"]]
3072+
// Platform-specific static linking flags
3073+
// args += [["-Xlinker", "$(LD_FORCE_STATIC_LINK_STDLIB)"]]
3074+
if producer.isApplePlatform {
3075+
// Darwin/Apple platforms use force_load_swift_libs and Framework linking
3076+
args += [["-Xlinker", "-force_load_swift_libs"]]
3077+
args += [["-lc++", "-framework", "Foundation"]]
3078+
} else {
3079+
// Non-Apple platforms (Linux, etc.) use static library linking
3080+
// args += [["-lc++", "-framework", "Foundation"]]
3081+
// args += [["-lc++"]]
3082+
args += [["-static-stdlib"]]
3083+
// args += [["-Xlinker", "-no-pie"]]
3084+
// Note: Foundation is not available as a framework on non-Apple platforms
3085+
}
30673086
}
30683087

30693088
// Add the AST, if debugging.

Sources/SWBTaskExecution/TaskActions/EmbedSwiftStdLibTaskAction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fileprivate extension MachO {
2727
.flatMap { try $0.linkedLibraryPaths() }
2828
.compactMap { (lib: String) -> String? in
2929
let pcs = Path(lib)
30-
if (pcs.dirname.str == "@rpath" || pcs.dirname == Path("/usr/lib/swift")) && executableFileNameMatchesSwiftRuntimeLibPattern(pcs.basename) {
30+
if (pcs.dirname.str == "@rpath" || pcs.dirname == Path("/usr/lib/swift_static")) && executableFileNameMatchesSwiftRuntimeLibPattern(pcs.basename) {
3131
return pcs.basename
3232
}
3333
else {

Sources/SWBUniversalPlatform/Specs/Clang.xcspec

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,22 @@
323323
);
324324
Category = LanguageObjC;
325325
},
326+
{
327+
Name = "CLANG_FORCE_STATIC_LINK_STDLIB";
328+
Type = Boolean;
329+
DefaultValue = "$(SWIFT_FORCE_STATIC_LINK_STDLIB)";
330+
CommandLineArgs = {
331+
YES = ();
332+
NO = ();
333+
};
334+
// This option is passed for linking to inform the compiler
335+
// that the ObjC runtime must be linked in (with possible
336+
// backwards compatibility libraries linked in).
337+
AdditionalLinkerArgs = {
338+
YES = ( "$(LD_FORCE_STATIC_LINK_STDLIB)" );
339+
NO = ();
340+
};
341+
},
326342
{
327343
Name = "CLANG_USE_RESPONSE_FILE";
328344
Type = Boolean;

Sources/SWBUniversalPlatform/Specs/Ld.xcspec

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,18 @@
157157
Name = LD_OBJC_RUNTIME_ARGS_swiftc;
158158
DefaultValue = "-link-objc-runtime";
159159
},
160+
{
161+
Name = LD_FORCE_STATIC_LINK_STDLIB;
162+
DefaultValue = "$(LD_STATIC_LINK_ARGS$(LINKER_DRIVER))";
163+
},
164+
{
165+
Name = LD_FORCE_STATIC_LINK_STDLIB_clang;
166+
DefaultValue = "-static";
167+
},
168+
{
169+
Name = LD_FORCE_STATIC_LINK_STDLIB_swiftc;
170+
DefaultValue = "-force_load_swift_libs";
171+
},
160172
{
161173
Name = CLANG_SDKROOT_LINKER_INPUT;
162174
Type = Path;

Sources/SWBUniversalPlatform/Specs/Swift.xcspec

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,22 @@
11191119
NO = ();
11201120
};
11211121
},
1122-
1122+
{
1123+
Name = "SWIFT_FORCE_STATIC_LINK_STDLIB";
1124+
Type = Boolean;
1125+
DefaultValue = "$(SWIFT_FORCE_STATIC_LINK_STDLIB)";
1126+
CommandLineArgs = {
1127+
YES = ();
1128+
NO = ();
1129+
};
1130+
// This option is passed for linking to inform the compiler
1131+
// that the ObjC runtime must be linked in (with possible
1132+
// backwards compatibility libraries linked in).
1133+
AdditionalLinkerArgs = {
1134+
YES = ( "$(LD_FORCE_STATIC_LINK_STDLIB)" );
1135+
NO = ();
1136+
};
1137+
},
11231138
{
11241139
Name = "CLANG_COVERAGE_MAPPING";
11251140
Type = Boolean;

Tests/SWBCoreTests/SwiftCompilerTests.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ fileprivate final class TestSwiftParserDelegate: TaskOutputParserDelegate, Senda
117117
}
118118

119119
/// Check the standard library linking options.
120-
@Test(.requireHostOS(.macOS))
120+
@Test
121121
func standardLibraryLinking() async throws {
122122
let core = try await getCore()
123123

@@ -145,7 +145,7 @@ fileprivate final class TestSwiftParserDelegate: TaskOutputParserDelegate, Senda
145145
try await #expect(spec.computeAdditionalLinkerArgs(producer, scope: scope, lookup: { _ in nil }, inputFileTypes: [], optionContext: optionContext, delegate: CapturingTaskGenerationDelegate(producer: producer, userPreferences: .defaultForTesting)).args == additionalSwiftLinkerArgs(spec, producer, scope, stdlibPath))
146146
}
147147

148-
// Check force static stdlib.
148+
// Check force static stdlib on Apple platforms (macOS).
149149
do {
150150
let producer = try MockCommandProducer(core: core, productTypeIdentifier: "com.apple.product-type.framework", platform: "macosx")
151151
let stdlibPath = swiftcPath.dirname.dirname.join("lib/swift_static/fakeos")
@@ -157,9 +157,26 @@ fileprivate final class TestSwiftParserDelegate: TaskOutputParserDelegate, Senda
157157
let scope = MacroEvaluationScope(table: table)
158158
let delegate = TestTaskPlanningDelegate(clientDelegate: MockTestTaskPlanningClientDelegate(), fs: localFS)
159159
let optionContext = await spec.discoveredCommandLineToolSpecInfo(producer, scope, delegate)
160+
// On Apple platforms, should use Apple-specific static linking flags
160161
try await #expect(spec.computeAdditionalLinkerArgs(producer, scope: scope, lookup: { _ in nil }, inputFileTypes: [], optionContext: optionContext, delegate: CapturingTaskGenerationDelegate(producer: producer, userPreferences: .defaultForTesting)).args == (additionalSwiftLinkerArgs(spec, producer, scope, stdlibPath)) + [["-Xlinker", "-force_load_swift_libs"], ["-lc++", "-framework", "Foundation"]])
161162
}
162163

164+
// Check force static stdlib on non-Apple platforms (Linux).
165+
do {
166+
let producer = try MockCommandProducer(core: core, productTypeIdentifier: "com.apple.product-type.framework", platform: "linux")
167+
let stdlibPath = swiftcPath.dirname.dirname.join("lib/swift_static/linux")
168+
var table = MacroValueAssignmentTable(namespace: core.specRegistry.internalMacroNamespace)
169+
table.push(BuiltinMacros.SWIFT_EXEC, literal: swiftcPath.str)
170+
table.push(BuiltinMacros.SWIFT_STDLIB, literal: "swiftCore")
171+
table.push(BuiltinMacros.PLATFORM_NAME, literal: "linux")
172+
table.push(BuiltinMacros.SWIFT_FORCE_STATIC_LINK_STDLIB, literal: true)
173+
let scope = MacroEvaluationScope(table: table)
174+
let delegate = TestTaskPlanningDelegate(clientDelegate: MockTestTaskPlanningClientDelegate(), fs: localFS)
175+
let optionContext = await spec.discoveredCommandLineToolSpecInfo(producer, scope, delegate)
176+
// On non-Apple platforms, should use standard static linking flags (no Darwin-specific flags)
177+
try await #expect(spec.computeAdditionalLinkerArgs(producer, scope: scope, lookup: { _ in nil }, inputFileTypes: [], optionContext: optionContext, delegate: CapturingTaskGenerationDelegate(producer: producer, userPreferences: .defaultForTesting)).args == (additionalSwiftLinkerArgs(spec, producer, scope, stdlibPath)) + [["-lc++"]])
178+
}
179+
163180
// Check tool product type.
164181
do {
165182
let producer = try MockCommandProducer(core: core, productTypeIdentifier: "com.apple.product-type.tool", platform: "macosx", toolchain: core.toolchainRegistry.defaultToolchain)

0 commit comments

Comments
 (0)