Skip to content

Commit ed40829

Browse files
committed
Enable -clang-target on Darwin platforms set to the SDK version by-default
1 parent fc4753a commit ed40829

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

+1-12
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,6 @@ extension Driver {
7272
break
7373
}
7474

75-
// Pass down -clang-target.
76-
// If not specified otherwise, we should use the same triple as -target
77-
// TODO: enable -clang-target for implicit module build as well.
78-
if !parsedOptions.hasArgument(.disableClangTarget) &&
79-
isFrontendArgSupported(.clangTarget) &&
80-
parsedOptions.contains(.driverExplicitModuleBuild) {
81-
let clangTriple = parsedOptions.getLastArgument(.clangTarget)?.asSingle ?? targetTriple.triple
82-
commandLine.appendFlag(.clangTarget)
83-
commandLine.appendFlag(clangTriple)
84-
}
85-
8675
// If in ExplicitModuleBuild mode and the dependency graph has been computed, add module
8776
// dependencies.
8877
// May also be used for generation of the dependency graph itself in ExplicitModuleBuild mode.
@@ -324,7 +313,7 @@ extension Driver {
324313
try toolchain.addPlatformSpecificCommonFrontendOptions(commandLine: &commandLine,
325314
inputs: &inputs,
326315
frontendTargetInfo: frontendTargetInfo,
327-
driver: self)
316+
driver: &self)
328317
}
329318

330319
mutating func addFrontendSupplementaryOutputArguments(commandLine: inout [Job.ArgTemplate],

Sources/SwiftDriver/Toolchains/DarwinToolchain.swift

+24-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ public final class DarwinToolchain: Toolchain {
371371
commandLine: inout [Job.ArgTemplate],
372372
inputs: inout [TypedVirtualPath],
373373
frontendTargetInfo: FrontendTargetInfo,
374-
driver: Driver
374+
driver: inout Driver
375375
) throws {
376376
guard let sdkPath = frontendTargetInfo.sdkPath?.path,
377377
let sdkInfo = getTargetSDKInfo(sdkPath: sdkPath) else { return }
@@ -401,6 +401,29 @@ public final class DarwinToolchain: Toolchain {
401401
.appending(component: "macosx").appending(component: "prebuilt-modules")
402402
.appending(component: sdkInfo.versionString))
403403
}
404+
405+
// Pass down -clang-target.
406+
// If not specified otherwise, we should use the same triple as -target
407+
if !driver.parsedOptions.hasArgument(.disableClangTarget) &&
408+
driver.isFrontendArgSupported(.clangTarget) {
409+
// The common target triple for all Clang dependencies of this compilation,
410+
// both direct and transitive is computed as:
411+
// 1. An explicitly-specified `-clang-target` argument to this driver invocation
412+
// 2. (On Darwin) The target triple of the selected SDK
413+
let clangTargetTriple: String
414+
if let explicitClangTripleArg = driver.parsedOptions.getLastArgument(.clangTarget)?.asSingle {
415+
clangTargetTriple = explicitClangTripleArg
416+
} else if let sdkPathHandle = frontendTargetInfo.sdkPath?.path,
417+
let sdkPath = VirtualPath.lookup(sdkPathHandle).absolutePath,
418+
let sdkInfo = Self.readSDKInfo(fileSystem, VirtualPath.absolute(sdkPath).intern()) {
419+
clangTargetTriple = frontendTargetInfo.target.unversionedTriple.triple + sdkInfo.versionString
420+
} else {
421+
clangTargetTriple = frontendTargetInfo.target.triple.triple
422+
}
423+
424+
commandLine.appendFlag(.clangTarget)
425+
commandLine.appendFlag(clangTargetTriple)
426+
}
404427
}
405428
}
406429

Sources/SwiftDriver/Toolchains/Toolchain.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public protocol Toolchain {
151151
commandLine: inout [Job.ArgTemplate],
152152
inputs: inout [TypedVirtualPath],
153153
frontendTargetInfo: FrontendTargetInfo,
154-
driver: Driver
154+
driver: inout Driver
155155
) throws
156156

157157
var dummyForTestingObjectFormat: Triple.ObjectFormat {get}
@@ -306,7 +306,7 @@ extension Toolchain {
306306
commandLine: inout [Job.ArgTemplate],
307307
inputs: inout [TypedVirtualPath],
308308
frontendTargetInfo: FrontendTargetInfo,
309-
driver: Driver
309+
driver: inout Driver
310310
) throws {}
311311

312312
/// Resolves the path to the given tool and whether or not it supports response files so that it

Tests/SwiftDriverTests/SwiftDriverTests.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -3622,7 +3622,7 @@ final class SwiftDriverTests: XCTestCase {
36223622
#endif
36233623
}
36243624

3625-
func testDisableClangTargetForImplicitModule() throws {
3625+
func testEnableClangTargetForImplicitModule() throws {
36263626
var envVars = ProcessEnv.vars
36273627
envVars["SWIFT_DRIVER_LD_EXEC"] = ld.nativePathString(escaped: false)
36283628

@@ -3631,8 +3631,8 @@ final class SwiftDriverTests: XCTestCase {
36313631
env: envVars)
36323632
let plannedJobs = try driver.planBuild()
36333633
XCTAssertEqual(plannedJobs.count, 2)
3634-
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-target")))
3635-
XCTAssertFalse(plannedJobs[0].commandLine.contains(.flag("-clang-target")))
3634+
XCTAssertTrue(plannedJobs[0].commandLine.contains(.flag("-target")))
3635+
XCTAssertTrue(plannedJobs[0].commandLine.contains(.flag("-clang-target")))
36363636
}
36373637

36383638
func testPCHasCompileInput() throws {

0 commit comments

Comments
 (0)