Skip to content

enable static linking of the swift runtime libraries by default on supported platforms #3905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
30 changes: 20 additions & 10 deletions Sources/Build/BuildPlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1127,8 +1127,7 @@ public final class SwiftTargetBuildDescription {
}

private var stdlibArguments: [String] {
if buildParameters.shouldLinkStaticSwiftStdlib &&
buildParameters.triple.isSupportingStaticStdlib {
if buildParameters.shouldStaticallyLinkSwiftRuntimeLibrary {
return ["-static-stdlib"]
} else {
return []
Expand Down Expand Up @@ -1304,12 +1303,8 @@ public final class ProductBuildDescription {
args += deadStripArguments
case .executable, .snippet:
// Link the Swift stdlib statically, if requested.
if buildParameters.shouldLinkStaticSwiftStdlib {
if buildParameters.triple.isDarwin() {
self.observabilityScope.emit(.swiftBackDeployError)
} else if buildParameters.triple.isSupportingStaticStdlib {
args += ["-static-stdlib"]
}
if buildParameters.shouldStaticallyLinkSwiftRuntimeLibrary {
args += ["-static-stdlib"]
}
args += ["-emit-executable"]
args += deadStripArguments
Expand Down Expand Up @@ -2284,8 +2279,23 @@ private func generateResourceInfoPlist(
return true
}

fileprivate extension TSCUtility.Triple {
var isSupportingStaticStdlib: Bool {
extension BuildParameters {
fileprivate var shouldStaticallyLinkSwiftRuntimeLibrary: Bool {
guard !self.disableAutomaticSwiftRuntimeStaticLinking else {
return false
}
guard self.triple.isSupportingStaticStdlib else {
return false
}
guard self.configuration == .release else {
return false
}
return true
}
}

extension TSCUtility.Triple {
fileprivate var isSupportingStaticStdlib: Bool {
isLinux() || arch == .wasm32
}
}
9 changes: 6 additions & 3 deletions Sources/Commands/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ struct ResolverOptions: ParsableArguments {
@Flag(name: .customLong("skip-update"), help: "Skip updating dependencies from their remote during a resolution")
var skipDependencyUpdate: Bool = false


@Flag(help: "Define automatic transformation of source control based dependencies to registry based ones")
var sourceControlToRegistryDependencyTransformation: SourceControlToRegistryDependencyTransformation = .disabled

Expand Down Expand Up @@ -387,9 +386,13 @@ struct LinkerOptions: ParsableArguments {
help: "Disable/enable dead code stripping by the linker")
var linkerDeadStrip: Bool = true

/// If should link the Swift runtime libraries (stdlib, foundation, dispatch, etc) statically.
@Flag(name: .customLong("disable-static-swift-runtime"), help: "Disable static linking of the Swift runtime libraries (which is done automatically on supported platforms like Linux)")
var disableAutomaticSwiftRuntimeStaticLinking: Bool = false

/// If should link the Swift stdlib statically.
@Flag(name: .customLong("static-swift-stdlib"), inversion: .prefixedNo, help: "Link Swift stdlib statically")
var shouldLinkStaticSwiftStdlib: Bool = false
@Flag(name: .customLong("static-swift-stdlib"), inversion: .prefixedNo, help: .hidden)
var _deprecated_shouldLinkStaticSwiftStdlib: Bool?
}


Expand Down
11 changes: 8 additions & 3 deletions Sources/Commands/SwiftTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,10 @@ public class SwiftTool {
if options.caching._deprecated_useRepositoriesCache != nil {
observabilityScope.emit(warning: "'--disable-repository-cache'/'--enable-repository-cache' flags are deprecated; use '--disable-dependency-cache'/'--enable-dependency-cache' instead")
}

if options.linker._deprecated_shouldLinkStaticSwiftStdlib != nil {
observabilityScope.emit(warning: "'--shouldLinkStaticSwiftStdlib' option is deprecated; Statically linking the Swift runtime is automatically done on relevant platforms (eg Linux). You may opt out of this behavior with --disable-static-swift-runtime")
}
}

/// Returns the currently active workspace.
Expand Down Expand Up @@ -678,7 +682,7 @@ public class SwiftTool {
throw error
}
}

func getPluginScriptRunner(customPluginsDir: AbsolutePath? = .none) throws -> PluginScriptRunner {
let pluginsDir = try customPluginsDir ?? self.getActiveWorkspace().location.pluginWorkingDirectory
let cacheDir = pluginsDir.appending(component: "cache")
Expand Down Expand Up @@ -819,7 +823,8 @@ public class SwiftTool {
// can be used to build for any Apple platform and it has it's own
// conventions for build subpaths based on platforms.
let dataPath = self.scratchDirectory.appending(
component: options.build.buildSystem == .xcode ? "apple" : triple.platformBuildPathComponent())
component: options.build.buildSystem == .xcode ? "apple" : triple.platformBuildPathComponent()
)
return BuildParameters(
dataPath: dataPath,
configuration: options.build.configuration,
Expand All @@ -829,7 +834,7 @@ public class SwiftTool {
flags: options.build.buildFlags,
xcbuildFlags: options.build.xcbuildFlags,
jobs: options.build.jobs ?? UInt32(ProcessInfo.processInfo.activeProcessorCount),
shouldLinkStaticSwiftStdlib: options.linker.shouldLinkStaticSwiftStdlib,
disableAutomaticSwiftRuntimeStaticLinking: options.linker.disableAutomaticSwiftRuntimeStaticLinking,
canRenameEntrypointFunctionName: SwiftTargetBuildDescription.checkSupportedFrontendFlags(
flags: ["entry-point-function-name"], fileSystem: self.fileSystem
),
Expand Down
8 changes: 4 additions & 4 deletions Sources/SPMBuildCore/BuildParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ public struct BuildParameters: Encodable {
/// How many jobs should llbuild and the Swift compiler spawn
public var jobs: UInt32

/// If should link the Swift stdlib statically.
public var shouldLinkStaticSwiftStdlib: Bool
/// Disable automatic statically link the Swift runtime on supported platforms.
public var disableAutomaticSwiftRuntimeStaticLinking: Bool

/// Which compiler sanitizers should be enabled
public var sanitizers: EnabledSanitizers
Expand Down Expand Up @@ -188,7 +188,7 @@ public struct BuildParameters: Encodable {
flags: BuildFlags,
xcbuildFlags: [String] = [],
jobs: UInt32 = UInt32(ProcessInfo.processInfo.activeProcessorCount),
shouldLinkStaticSwiftStdlib: Bool = false,
disableAutomaticSwiftRuntimeStaticLinking: Bool = false,
shouldEnableManifestCaching: Bool = false,
canRenameEntrypointFunctionName: Bool = false,
shouldCreateDylibForDynamicProducts: Bool = true,
Expand Down Expand Up @@ -218,7 +218,7 @@ public struct BuildParameters: Encodable {
self.flags = flags
self.xcbuildFlags = xcbuildFlags
self.jobs = jobs
self.shouldLinkStaticSwiftStdlib = shouldLinkStaticSwiftStdlib
self.disableAutomaticSwiftRuntimeStaticLinking = disableAutomaticSwiftRuntimeStaticLinking
self.shouldEnableManifestCaching = shouldEnableManifestCaching
self.shouldCreateDylibForDynamicProducts = shouldCreateDylibForDynamicProducts
self.canRenameEntrypointFunctionName = canRenameEntrypointFunctionName
Expand Down
Loading