Skip to content

Commit 36ae38d

Browse files
committed
Allow passing -nostdlib to clang linker driver
Embedded targets may need `-nostdlib` passed to `clang` linker driver in certain situations, see #1534 for more details. Currently, `swiftc` doesn't recognize `-nostdlib` at all, while `-Xlinker -nostdlib` passed to a `swiftc` link job from the build system will be passed verbatim to the linker and not recognized by the linker driver. We should support this flag in `swiftc` to fix this.
1 parent 4163263 commit 36ae38d

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

Sources/SwiftOptions/Options.swift

+2
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ extension Option {
608608
public static let driverScanDependenciesNonLib: Option = Option("-nonlib-dependency-scanner", .flag, attributes: [.helpHidden], helpText: "Use calls to `swift-frontend -scan-dependencies` instead of dedicated dependency scanning library")
609609
public static let nostartfiles: Option = Option("-nostartfiles", .flag, attributes: [.helpHidden, .frontend, .noInteractive, .doesNotAffectIncrementalBuild], helpText: "Do not link in the Swift language startup routines")
610610
public static let nostdimport: Option = Option("-nostdimport", .flag, attributes: [.frontend], helpText: "Don't search the standard library import path for modules")
611+
public static let nostdlib: Option = Option("-nostdlib", .flag, attributes: [.helpHidden], helpText: "Don't link the C and compiler runtime libraries", group: .linkerOption)
611612
public static let numThreads: Option = Option("-num-threads", .separate, attributes: [.frontend, .doesNotAffectIncrementalBuild, .cacheInvariant], metaVar: "<n>", helpText: "Enable multi-threading and specify number of threads")
612613
public static let omitExtensionBlockSymbols: Option = Option("-omit-extension-block-symbols", .flag, attributes: [.helpHidden, .frontend, .noInteractive, .supplementaryOutput], helpText: "Directly associate members and conformances with the extended nominal when generating symbol graphs instead of emitting 'swift.extension' symbols for extensions to external types")
613614
public static let Onone: Option = Option("-Onone", .flag, attributes: [.frontend, .moduleInterface], helpText: "Compile without any optimization", group: .O)
@@ -1437,6 +1438,7 @@ extension Option {
14371438
Option.driverScanDependenciesNonLib,
14381439
Option.nostartfiles,
14391440
Option.nostdimport,
1441+
Option.nostdlib,
14401442
Option.numThreads,
14411443
Option.omitExtensionBlockSymbols,
14421444
Option.Onone,

Tests/SwiftDriverTests/SwiftDriverTests.swift

+5-4
Original file line numberDiff line numberDiff line change
@@ -6635,11 +6635,12 @@ final class SwiftDriverTests: XCTestCase {
66356635

66366636
// Embedded Wasm link job
66376637
do {
6638-
var driver = try Driver(args: ["swiftc", "-target", "wasm32-none-none-wasm", "test.o", "-enable-experimental-feature", "Embedded", "-wmo", "-o", "a.wasm"], env: env)
6638+
var driver = try Driver(args: ["swiftc", "-target", "wasm32-none-none-wasm", "test.o", "-enable-experimental-feature", "Embedded", "-wmo", "-nostdlib", "-o", "a.wasm"], env: env)
66396639
let plannedJobs = try driver.planBuild()
66406640
XCTAssertEqual(plannedJobs.count, 2)
66416641
let _ /*autolinkJob*/ = plannedJobs[0]
66426642
let linkJob = plannedJobs[1]
6643+
XCTAssertTrue(linkJob.commandLine.contains(.flag("-nostdlib")))
66436644
XCTAssertFalse(linkJob.commandLine.contains(.flag("-force_load")))
66446645
XCTAssertFalse(linkJob.commandLine.contains(.flag("-rpath")))
66456646
XCTAssertFalse(linkJob.commandLine.contains(.flag("-lswiftCore")))
@@ -6648,19 +6649,19 @@ final class SwiftDriverTests: XCTestCase {
66486649

66496650
do {
66506651
let diags = DiagnosticsEngine()
6651-
var driver = try Driver(args: ["swiftc", "-target", "arm64-apple-macosx10.13", "test.swift", "-enable-experimental-feature", "Embedded", "-parse-as-library", "-wmo", "-o", "a.out", "-module-name", "main", "-enable-library-evolution"], diagnosticsEngine: diags)
6652+
var driver = try Driver(args: ["swiftc", "-target", "arm64-apple-macosx10.13", "test.swift", "-enable-experimental-feature", "Embedded", "-parse-as-library", "-wmo", "-o", "a.out", "-module-name", "main", "-enable-library-evolution"], diagnosticsEngine: diags)
66526653
_ = try driver.planBuild()
66536654
XCTAssertTrue(diags.diagnostics.first!.message.text == Diagnostic.Message.error_no_library_evolution_embedded.text)
66546655
} catch _ { }
66556656
do {
66566657
let diags = DiagnosticsEngine()
6657-
var driver = try Driver(args: ["swiftc", "-target", "arm64-apple-macosx10.13", "test.swift", "-enable-experimental-feature", "Embedded", "-parse-as-library", "-o", "a.out", "-module-name", "main"], diagnosticsEngine: diags)
6658+
var driver = try Driver(args: ["swiftc", "-target", "arm64-apple-macosx10.13", "test.swift", "-enable-experimental-feature", "Embedded", "-parse-as-library", "-o", "a.out", "-module-name", "main"], diagnosticsEngine: diags)
66586659
_ = try driver.planBuild()
66596660
XCTAssertTrue(diags.diagnostics.first!.message.text == Diagnostic.Message.error_need_wmo_embedded.text)
66606661
} catch _ { }
66616662
do {
66626663
let diags = DiagnosticsEngine()
6663-
var driver = try Driver(args: ["swiftc", "-target", "arm64-apple-macosx10.13", "test.swift", "-enable-experimental-feature", "Embedded", "-parse-as-library", "-wmo", "-o", "a.out", "-module-name", "main", "-enable-objc-interop"], diagnosticsEngine: diags)
6664+
var driver = try Driver(args: ["swiftc", "-target", "arm64-apple-macosx10.13", "test.swift", "-enable-experimental-feature", "Embedded", "-parse-as-library", "-wmo", "-o", "a.out", "-module-name", "main", "-enable-objc-interop"], diagnosticsEngine: diags)
66646665
_ = try driver.planBuild()
66656666
XCTAssertTrue(diags.diagnostics.first!.message.text == Diagnostic.Message.error_no_objc_interop_embedded.text)
66666667
} catch _ { }

0 commit comments

Comments
 (0)