Skip to content

Driver: allow static executables when using Musl #1383

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

Merged
merged 3 commits into from
Jul 5, 2023
Merged
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
7 changes: 5 additions & 2 deletions Sources/SwiftDriver/Driver/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,9 @@ public struct Driver {

self.lto = Self.ltoKind(&parsedOptions, diagnosticsEngine: diagnosticsEngine)
// Figure out the primary outputs from the driver.
(self.compilerOutputType, self.linkerOutputType) = Self.determinePrimaryOutputs(&parsedOptions, driverKind: driverKind, diagnosticsEngine: diagnosticEngine)
(self.compilerOutputType, self.linkerOutputType) =
Self.determinePrimaryOutputs(&parsedOptions, targetTriple: self.frontendTargetInfo.target.triple,
driverKind: driverKind, diagnosticsEngine: diagnosticEngine)

// Multithreading.
self.numThreads = Self.determineNumThreads(&parsedOptions, compilerMode: compilerMode, diagnosticsEngine: diagnosticEngine)
Expand Down Expand Up @@ -1994,6 +1996,7 @@ extension Driver {
/// Determine the primary compiler and linker output kinds.
private static func determinePrimaryOutputs(
_ parsedOptions: inout ParsedOptions,
targetTriple: Triple,
driverKind: DriverKind,
diagnosticsEngine: DiagnosticsEngine
) -> (FileType?, LinkOutputType?) {
Expand All @@ -2005,7 +2008,7 @@ extension Driver {
if let outputOption = parsedOptions.getLast(in: .modes) {
switch outputOption.option {
case .emitExecutable:
if parsedOptions.contains(.static) {
if parsedOptions.contains(.static) && !targetTriple.supportsStaticExecutables {
diagnosticsEngine.emit(.error_static_emit_executable_disallowed)
}
linkerOutputType = .executable
Expand Down
11 changes: 11 additions & 0 deletions Sources/SwiftDriver/Utilities/Triple.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1705,3 +1705,14 @@ fileprivate extension Array {
}
}
}

// MARK: - Linker support

extension Triple {
/// Returns `true` if a given triple supports producing fully statically linked executables by providing `-static`
/// flag to the linker. This implies statically linking platform's libc, and of those that Swift supports currently
/// only Musl allows that reliably.
var supportsStaticExecutables: Bool {
self.environment == .musl
}
}
36 changes: 34 additions & 2 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2100,10 +2100,42 @@ final class SwiftDriverTests: XCTestCase {
XCTAssertFalse(cmd.contains(.flag("-dylib")))
XCTAssertFalse(cmd.contains(.flag("-shared")))
}

do {
// executable linking linux static stdlib with musl
var driver = try Driver(args: commonArgs + [
"-emit-executable", "-Osize", "-static-stdlib", "-static-executable", "-target", "x86_64-unknown-linux-musl"
], env: env)
let plannedJobs = try driver.planBuild()

XCTAssertEqual(plannedJobs.count, 4)

let autolinkExtractJob = plannedJobs[2]
XCTAssertEqual(autolinkExtractJob.kind, .autolinkExtract)

let autolinkCmd = autolinkExtractJob.commandLine
XCTAssertTrue(commandContainsTemporaryPath(autolinkCmd, "foo.o"))
XCTAssertTrue(commandContainsTemporaryPath(autolinkCmd, "bar.o"))
XCTAssertTrue(commandContainsTemporaryPath(autolinkCmd, "Test.autolink"))

let linkJob = plannedJobs[3]
let cmd = linkJob.commandLine
XCTAssertTrue(cmd.contains(.flag("-o")))
XCTAssertTrue(commandContainsTemporaryPath(cmd, "foo.o"))
XCTAssertTrue(commandContainsTemporaryPath(cmd, "bar.o"))
XCTAssertTrue(cmd.contains(.flag("--start-group")))
XCTAssertTrue(cmd.contains(.flag("--end-group")))
XCTAssertTrue(cmd.contains(.flag("-Os")))
XCTAssertTrue(cmd.contains(.flag("-static")))
XCTAssertEqual(linkJob.outputs[0].file, try VirtualPath(path: "Test"))

XCTAssertFalse(cmd.contains(.flag("-dylib")))
XCTAssertFalse(cmd.contains(.flag("-shared")))
}
#endif

do {
// static WASM linking
// static Wasm linking
var driver = try Driver(args: commonArgs + ["-emit-library", "-static", "-target", "wasm32-unknown-wasi"], env: env)
let plannedJobs = try driver.planBuild()

Expand Down Expand Up @@ -2135,7 +2167,7 @@ final class SwiftDriverTests: XCTestCase {
try withTemporaryDirectory { path in
try localFileSystem.writeFileContents(
path.appending(components: "wasi", "static-executable-args.lnk")) { $0 <<< "garbage" }
// WASM executable linking
// Wasm executable linking
var driver = try Driver(args: commonArgs + ["-emit-executable", "-Ounchecked",
"-target", "wasm32-unknown-wasi",
"-resource-dir", path.pathString,
Expand Down