Skip to content

Commit 3e902fc

Browse files
authored
Merge pull request #1322 from artemcm/59ClangTargetAtSDKTriple
[5.9][Explicit Modules] Have `-clang-target` on Darwin platforms set to the SDK version by-default
2 parents ceb48b5 + 5796a48 commit 3e902fc

File tree

4 files changed

+41
-22
lines changed

4 files changed

+41
-22
lines changed

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

+1-12
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,6 @@ extension Driver {
7474
break
7575
}
7676

77-
// Pass down -clang-target.
78-
// If not specified otherwise, we should use the same triple as -target
79-
// TODO: enable -clang-target for implicit module build as well.
80-
if !parsedOptions.hasArgument(.disableClangTarget) &&
81-
isFrontendArgSupported(.clangTarget) &&
82-
parsedOptions.contains(.driverExplicitModuleBuild) {
83-
let clangTriple = parsedOptions.getLastArgument(.clangTarget)?.asSingle ?? targetTriple.triple
84-
commandLine.appendFlag(.clangTarget)
85-
commandLine.appendFlag(clangTriple)
86-
}
87-
8877
// If in ExplicitModuleBuild mode and the dependency graph has been computed, add module
8978
// dependencies.
9079
// May also be used for generation of the dependency graph itself in ExplicitModuleBuild mode.
@@ -387,7 +376,7 @@ extension Driver {
387376
try toolchain.addPlatformSpecificCommonFrontendOptions(commandLine: &commandLine,
388377
inputs: &inputs,
389378
frontendTargetInfo: frontendTargetInfo,
390-
driver: self)
379+
driver: &self)
391380
}
392381

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

Sources/SwiftDriver/Toolchains/DarwinToolchain.swift

+21-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,26 @@ 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+
driver.parsedOptions.contains(.driverExplicitModuleBuild) {
410+
// The common target triple for all Clang dependencies of this compilation,
411+
// both direct and transitive is computed as:
412+
// 1. An explicitly-specified `-clang-target` argument to this driver invocation
413+
// 2. (On Darwin) The target triple of the selected SDK
414+
let clangTargetTriple: String
415+
if let explicitClangTripleArg = driver.parsedOptions.getLastArgument(.clangTarget)?.asSingle {
416+
clangTargetTriple = explicitClangTripleArg
417+
} else {
418+
clangTargetTriple = frontendTargetInfo.target.unversionedTriple.triple + sdkInfo.versionString
419+
}
420+
421+
commandLine.appendFlag(.clangTarget)
422+
commandLine.appendFlag(clangTargetTriple)
423+
}
404424
}
405425
}
406426

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

+17-7
Original file line numberDiff line numberDiff line change
@@ -3626,20 +3626,24 @@ final class SwiftDriverTests: XCTestCase {
36263626

36273627
func testClangTargetForExplicitModule() throws {
36283628
#if os(macOS)
3629+
let sdkRoot = testInputsPath.appending(component: "SDKChecks").appending(component: "iPhoneOS.sdk")
3630+
36293631
// Check -clang-target is on by default when explicit module is on.
36303632
try withTemporaryDirectory { path in
36313633
let main = path.appending(component: "Foo.swift")
36323634
try localFileSystem.writeFileContents(main) {
36333635
$0 <<< "import Swift"
36343636
}
3635-
var driver = try Driver(args: ["swiftc", "-explicit-module-build", "-target",
3636-
"x86_64-apple-macosx10.14", main.pathString])
3637+
var driver = try Driver(args: ["swiftc", "-explicit-module-build",
3638+
"-target", "arm64-apple-ios14.0",
3639+
"-sdk", sdkRoot.pathString,
3640+
main.pathString])
36373641
guard driver.isFrontendArgSupported(.clangTarget) else {
36383642
throw XCTSkip("Skipping: compiler does not support '-clang-target'")
36393643
}
36403644
let plannedJobs = try driver.planBuild()
36413645
XCTAssertTrue(plannedJobs.contains { job in
3642-
job.commandLine.contains(.flag("-clang-target"))
3646+
job.commandLine.contains(subsequence: [.flag("-clang-target"), .flag("arm64-apple-ios13.0")])
36433647
})
36443648
}
36453649
// Check -disable-clang-target works
@@ -3649,8 +3653,10 @@ final class SwiftDriverTests: XCTestCase {
36493653
$0 <<< "import Swift"
36503654
}
36513655
var driver = try Driver(args: ["swiftc", "-disable-clang-target",
3652-
"-explicit-module-build", "-target",
3653-
"x86_64-apple-macosx10.14", main.pathString])
3656+
"-explicit-module-build",
3657+
"-target", "arm64-apple-ios14.0",
3658+
"-sdk", sdkRoot.pathString,
3659+
main.pathString])
36543660
guard driver.isFrontendArgSupported(.clangTarget) else {
36553661
throw XCTSkip("Skipping: compiler does not support '-clang-target'")
36563662
}
@@ -3663,16 +3669,20 @@ final class SwiftDriverTests: XCTestCase {
36633669
}
36643670

36653671
func testDisableClangTargetForImplicitModule() throws {
3672+
#if os(macOS)
36663673
var envVars = ProcessEnv.vars
36673674
envVars["SWIFT_DRIVER_LD_EXEC"] = ld.nativePathString(escaped: false)
36683675

3676+
let sdkRoot = testInputsPath.appending(component: "SDKChecks").appending(component: "iPhoneOS.sdk")
36693677
var driver = try Driver(args: ["swiftc", "-target",
3670-
"x86_64-apple-macosx10.14", "foo.swift"],
3678+
"arm64-apple-ios12.0", "foo.swift",
3679+
"-sdk", sdkRoot.pathString],
36713680
env: envVars)
36723681
let plannedJobs = try driver.planBuild()
36733682
XCTAssertEqual(plannedJobs.count, 2)
3674-
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-target")))
3683+
XCTAssertTrue(plannedJobs[0].commandLine.contains(.flag("-target")))
36753684
XCTAssertFalse(plannedJobs[0].commandLine.contains(.flag("-clang-target")))
3685+
#endif
36763686
}
36773687

36783688
func testPCHasCompileInput() throws {

0 commit comments

Comments
 (0)