Skip to content

Commit 7828cdb

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

File tree

4 files changed

+41
-24
lines changed

4 files changed

+41
-24
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

+20-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,25 @@ 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 {
417+
clangTargetTriple = frontendTargetInfo.target.unversionedTriple.triple + sdkInfo.versionString
418+
}
419+
420+
commandLine.appendFlag(.clangTarget)
421+
commandLine.appendFlag(clangTargetTriple)
422+
}
404423
}
405424
}
406425

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

+18-9
Original file line numberDiff line numberDiff line change
@@ -3586,20 +3586,24 @@ final class SwiftDriverTests: XCTestCase {
35863586

35873587
func testClangTargetForExplicitModule() throws {
35883588
#if os(macOS)
3589+
let sdkRoot = testInputsPath.appending(component: "SDKChecks").appending(component: "iPhoneOS.sdk")
3590+
35893591
// Check -clang-target is on by default when explicit module is on.
35903592
try withTemporaryDirectory { path in
35913593
let main = path.appending(component: "Foo.swift")
35923594
try localFileSystem.writeFileContents(main) {
35933595
$0 <<< "import Swift"
35943596
}
3595-
var driver = try Driver(args: ["swiftc", "-explicit-module-build", "-target",
3596-
"x86_64-apple-macosx10.14", main.pathString])
3597+
var driver = try Driver(args: ["swiftc", "-explicit-module-build",
3598+
"-target", "arm64-apple-ios14.0",
3599+
"-sdk", sdkRoot.pathString,
3600+
main.pathString])
35973601
guard driver.isFrontendArgSupported(.clangTarget) else {
35983602
throw XCTSkip("Skipping: compiler does not support '-clang-target'")
35993603
}
36003604
let plannedJobs = try driver.planBuild()
36013605
XCTAssertTrue(plannedJobs.contains { job in
3602-
job.commandLine.contains(.flag("-clang-target"))
3606+
job.commandLine.contains(subsequence: [.flag("-clang-target"), .flag("arm64-apple-ios13.0")])
36033607
})
36043608
}
36053609
// Check -disable-clang-target works
@@ -3609,8 +3613,10 @@ final class SwiftDriverTests: XCTestCase {
36093613
$0 <<< "import Swift"
36103614
}
36113615
var driver = try Driver(args: ["swiftc", "-disable-clang-target",
3612-
"-explicit-module-build", "-target",
3613-
"x86_64-apple-macosx10.14", main.pathString])
3616+
"-explicit-module-build",
3617+
"-target", "arm64-apple-ios14.0",
3618+
"-sdk", sdkRoot.pathString,
3619+
main.pathString])
36143620
guard driver.isFrontendArgSupported(.clangTarget) else {
36153621
throw XCTSkip("Skipping: compiler does not support '-clang-target'")
36163622
}
@@ -3622,17 +3628,20 @@ final class SwiftDriverTests: XCTestCase {
36223628
#endif
36233629
}
36243630

3625-
func testDisableClangTargetForImplicitModule() throws {
3631+
func testEnableClangTargetForImplicitModule() throws {
3632+
#if os(macOS)
36263633
var envVars = ProcessEnv.vars
36273634
envVars["SWIFT_DRIVER_LD_EXEC"] = ld.nativePathString(escaped: false)
36283635

3636+
let sdkRoot = testInputsPath.appending(component: "SDKChecks").appending(component: "iPhoneOS.sdk")
36293637
var driver = try Driver(args: ["swiftc", "-target",
3630-
"x86_64-apple-macosx10.14", "foo.swift"],
3638+
"arm64-apple-ios12.0", "foo.swift"],
36313639
env: envVars)
36323640
let plannedJobs = try driver.planBuild()
36333641
XCTAssertEqual(plannedJobs.count, 2)
3634-
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-target")))
3635-
XCTAssertFalse(plannedJobs[0].commandLine.contains(.flag("-clang-target")))
3642+
XCTAssertTrue(plannedJobs[0].commandLine.contains(.flag("-target")))
3643+
XCTAssertTrue(plannedJobs[0].commandLine.contains(subsequence: [.flag("-clang-target"), .flag("arm64-apple-ios13.0")]))
3644+
#endif
36363645
}
36373646

36383647
func testPCHasCompileInput() throws {

0 commit comments

Comments
 (0)