Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Sources/SwiftBuildSupport/Diagnostics+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import Basics

extension Basics.Diagnostic {
package static var swiftBackDeployWarning: Self {
.warning(
"""
Swift compiler no longer supports statically linking the Swift libraries. They're included in the OS by \
default starting with macOS Mojave 10.14.4 beta 3. For macOS Mojave 10.14.3 and earlier, there's an \
optional "Swift 5 Runtime Support for Command Line Tools" package that can be downloaded from \"More Downloads\" \
for Apple Developers at https://developer.apple.com/download/more/
"""
)
}
}
21 changes: 18 additions & 3 deletions Sources/SwiftBuildSupport/SwiftBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,7 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
}
try settings.merge(Self.constructDebuggingSettingsOverrides(from: buildParameters.debuggingParameters), uniquingKeysWith: reportConflict)
try settings.merge(Self.constructDriverSettingsOverrides(from: buildParameters.driverParameters), uniquingKeysWith: reportConflict)
try settings.merge(Self.constructLinkerSettingsOverrides(from: buildParameters.linkingParameters), uniquingKeysWith: reportConflict)
try settings.merge(self.constructLinkerSettingsOverrides(from: buildParameters.linkingParameters, triple: buildParameters.triple), uniquingKeysWith: reportConflict)
try settings.merge(Self.constructTestingSettingsOverrides(from: buildParameters.testingParameters), uniquingKeysWith: reportConflict)
try settings.merge(Self.constructAPIDigesterSettingsOverrides(from: buildParameters.apiDigesterMode), uniquingKeysWith: reportConflict)

Expand Down Expand Up @@ -1142,7 +1142,10 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
return settings
}

private static func constructLinkerSettingsOverrides(from parameters: BuildParameters.Linking) -> [String: String] {
private func constructLinkerSettingsOverrides(
from parameters: BuildParameters.Linking,
triple: Triple,
) -> [String: String] {
var settings: [String: String] = [:]

if parameters.linkerDeadStrip {
Expand All @@ -1160,7 +1163,19 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
break
}

// TODO: shouldLinkStaticSwiftStdlib
if triple.isDarwin() && parameters.shouldLinkStaticSwiftStdlib {
self.observabilityScope.emit(.swiftBackDeployWarning)
} else {
if parameters.shouldLinkStaticSwiftStdlib {
settings["SWIFT_FORCE_STATIC_LINK_STDLIB"] = "YES"
} else {
settings["SWIFT_FORCE_STATIC_LINK_STDLIB"] = "NO"
}
}

if let resourcesPath = self.buildParameters.toolchain.swiftResourcesPath(isStatic: parameters.shouldLinkStaticSwiftStdlib) {
settings["SWIFT_RESOURCE_DIR"] = resourcesPath.pathString
}

return settings
}
Expand Down
1 change: 1 addition & 0 deletions Sources/_InternalTestSupport/SwiftTesting+Tags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extension Tag.FunctionalArea {
@Tag public static var PIF: Tag
@Tag public static var IndexMode: Tag
@Tag public static var Sanitizer: Tag
@Tag public static var LinkSwiftStaticStdlib: Tag
}

extension Tag.Feature {
Expand Down
81 changes: 81 additions & 0 deletions Tests/SwiftBuildSupportTests/SwiftBuildSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,87 @@ struct SwiftBuildSystemTests {
}
}

@Suite(
.tags(
.FunctionalArea.LinkSwiftStaticStdlib,
),
)
struct SwiftStaticStdlibSettingTests {
@Test
func makingBuildParametersRaisesAWarningWhenRunOnDarwin() async throws {
// GIVEN we have a Darwin triple
let triple = try Triple("x86_64-apple-macosx")
// AND we want to statically link Swift sdtlib
let shouldLinkStaticSwiftStdlib = true
try await withInstantiatedSwiftBuildSystem(
fromFixture: "PIFBuilder/Simple",
buildParameters: mockBuildParameters(
destination: .host,
shouldLinkStaticSwiftStdlib: shouldLinkStaticSwiftStdlib,
triple: triple,
),
) { swiftBuild, session, observabilityScope, buildParameters in
// WHEN we make the build parameter
let _: SWBBuildParameters = try await swiftBuild.makeBuildParameters(
session: session,
symbolGraphOptions: nil,
setToolchainSetting: false, // Set this to false as SwiftBuild checks the toolchain path
)

// THEN we expect a warning to be emitted
let warnings = observabilityScope.diagnostics.filter {
$0.severity == .warning
}
#expect(warnings.count == 1)

let diagnostic = try #require(warnings.first)
// AND we expect the diagnostic message, severity and description to be as expected
#expect(diagnostic.message == Basics.Diagnostic.swiftBackDeployWarning.message)
#expect(diagnostic.severity == Basics.Diagnostic.swiftBackDeployWarning.severity)
#expect(diagnostic.description == Basics.Diagnostic.swiftBackDeployWarning.description)
}
}

@Test(
arguments: [
(shouldLinkStaticSwiftStdlib: true, expectedValue: "YES"),
(shouldLinkStaticSwiftStdlib: false, expectedValue: "NO"),
]
)
func swiftStaticStdLibSettingIsSetCorrectly(
shouldLinkStaticSwiftStdlib: Bool,
expectedValue: String
) async throws {
// GIVEN we have a non-darwin triple AND we want statically link Swift sdtlib or not
let nonDarwinTriple = try Triple("i686-pc-windows-cygnus")
try await withInstantiatedSwiftBuildSystem(
fromFixture: "PIFBuilder/Simple",
buildParameters: mockBuildParameters(
destination: .host,
shouldLinkStaticSwiftStdlib: shouldLinkStaticSwiftStdlib,
triple: nonDarwinTriple,
),
) { swiftBuild, session, observabilityScope, buildParameters in
// WHEN we make the build parameter
let buildSettings = try await swiftBuild.makeBuildParameters(
session: session,
symbolGraphOptions: nil,
setToolchainSetting: false, // Set this to false as SwiftBuild checks the toolchain path
)

// THEN we don't expect any warnings to be emitted
let warnings = observabilityScope.diagnostics.filter {
$0.severity == .warning
}
#expect(warnings.isEmpty)

// AND we expect the build setting to be set correctly
let synthesizedArgs = try #require(buildSettings.overrides.synthesized)
#expect(synthesizedArgs.table["SWIFT_FORCE_STATIC_LINK_STDLIB"] == expectedValue)
}
}
}

@Test(
arguments: BuildParameters.IndexStoreMode.allCases,
// arguments: [BuildParameters.IndexStoreMode.on],
Expand Down
Loading