Skip to content

Commit

Permalink
[Explicit Modules] Have -clang-target on Darwin platforms set to th…
Browse files Browse the repository at this point in the history
…e SDK version by-default
  • Loading branch information
artemcm committed Mar 28, 2023
1 parent a7104aa commit dc9c95f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
13 changes: 1 addition & 12 deletions Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,6 @@ extension Driver {
break
}

// Pass down -clang-target.
// If not specified otherwise, we should use the same triple as -target
// TODO: enable -clang-target for implicit module build as well.
if !parsedOptions.hasArgument(.disableClangTarget) &&
isFrontendArgSupported(.clangTarget) &&
parsedOptions.contains(.driverExplicitModuleBuild) {
let clangTriple = parsedOptions.getLastArgument(.clangTarget)?.asSingle ?? targetTriple.triple
commandLine.appendFlag(.clangTarget)
commandLine.appendFlag(clangTriple)
}

// If in ExplicitModuleBuild mode and the dependency graph has been computed, add module
// dependencies.
// May also be used for generation of the dependency graph itself in ExplicitModuleBuild mode.
Expand Down Expand Up @@ -387,7 +376,7 @@ extension Driver {
try toolchain.addPlatformSpecificCommonFrontendOptions(commandLine: &commandLine,
inputs: &inputs,
frontendTargetInfo: frontendTargetInfo,
driver: self)
driver: &self)
}

mutating func addFrontendSupplementaryOutputArguments(commandLine: inout [Job.ArgTemplate],
Expand Down
22 changes: 21 additions & 1 deletion Sources/SwiftDriver/Toolchains/DarwinToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public final class DarwinToolchain: Toolchain {
commandLine: inout [Job.ArgTemplate],
inputs: inout [TypedVirtualPath],
frontendTargetInfo: FrontendTargetInfo,
driver: Driver
driver: inout Driver
) throws {
guard let sdkPath = frontendTargetInfo.sdkPath?.path,
let sdkInfo = getTargetSDKInfo(sdkPath: sdkPath) else { return }
Expand Down Expand Up @@ -401,6 +401,26 @@ public final class DarwinToolchain: Toolchain {
.appending(component: "macosx").appending(component: "prebuilt-modules")
.appending(component: sdkInfo.versionString))
}

// Pass down -clang-target.
// If not specified otherwise, we should use the same triple as -target
if !driver.parsedOptions.hasArgument(.disableClangTarget) &&
driver.isFrontendArgSupported(.clangTarget) &&
driver.parsedOptions.contains(.driverExplicitModuleBuild) {
// The common target triple for all Clang dependencies of this compilation,
// both direct and transitive is computed as:
// 1. An explicitly-specified `-clang-target` argument to this driver invocation
// 2. (On Darwin) The target triple of the selected SDK
let clangTargetTriple: String
if let explicitClangTripleArg = driver.parsedOptions.getLastArgument(.clangTarget)?.asSingle {
clangTargetTriple = explicitClangTripleArg
} else {
clangTargetTriple = frontendTargetInfo.target.unversionedTriple.triple + sdkInfo.versionString
}

commandLine.appendFlag(.clangTarget)
commandLine.appendFlag(clangTargetTriple)
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftDriver/Toolchains/Toolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public protocol Toolchain {
commandLine: inout [Job.ArgTemplate],
inputs: inout [TypedVirtualPath],
frontendTargetInfo: FrontendTargetInfo,
driver: Driver
driver: inout Driver
) throws

var dummyForTestingObjectFormat: Triple.ObjectFormat {get}
Expand Down Expand Up @@ -306,7 +306,7 @@ extension Toolchain {
commandLine: inout [Job.ArgTemplate],
inputs: inout [TypedVirtualPath],
frontendTargetInfo: FrontendTargetInfo,
driver: Driver
driver: inout Driver
) throws {}

/// Resolves the path to the given tool and whether or not it supports response files so that it
Expand Down
24 changes: 17 additions & 7 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3626,20 +3626,24 @@ final class SwiftDriverTests: XCTestCase {

func testClangTargetForExplicitModule() throws {
#if os(macOS)
let sdkRoot = testInputsPath.appending(component: "SDKChecks").appending(component: "iPhoneOS.sdk")

// Check -clang-target is on by default when explicit module is on.
try withTemporaryDirectory { path in
let main = path.appending(component: "Foo.swift")
try localFileSystem.writeFileContents(main) {
$0 <<< "import Swift"
}
var driver = try Driver(args: ["swiftc", "-explicit-module-build", "-target",
"x86_64-apple-macosx10.14", main.pathString])
var driver = try Driver(args: ["swiftc", "-explicit-module-build",
"-target", "arm64-apple-ios14.0",
"-sdk", sdkRoot.pathString,
main.pathString])
guard driver.isFrontendArgSupported(.clangTarget) else {
throw XCTSkip("Skipping: compiler does not support '-clang-target'")
}
let plannedJobs = try driver.planBuild()
XCTAssertTrue(plannedJobs.contains { job in
job.commandLine.contains(.flag("-clang-target"))
job.commandLine.contains(subsequence: [.flag("-clang-target"), .flag("arm64-apple-ios13.0")])
})
}
// Check -disable-clang-target works
Expand All @@ -3649,8 +3653,10 @@ final class SwiftDriverTests: XCTestCase {
$0 <<< "import Swift"
}
var driver = try Driver(args: ["swiftc", "-disable-clang-target",
"-explicit-module-build", "-target",
"x86_64-apple-macosx10.14", main.pathString])
"-explicit-module-build",
"-target", "arm64-apple-ios14.0",
"-sdk", sdkRoot.pathString,
main.pathString])
guard driver.isFrontendArgSupported(.clangTarget) else {
throw XCTSkip("Skipping: compiler does not support '-clang-target'")
}
Expand All @@ -3663,16 +3669,20 @@ final class SwiftDriverTests: XCTestCase {
}

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

let sdkRoot = testInputsPath.appending(component: "SDKChecks").appending(component: "iPhoneOS.sdk")
var driver = try Driver(args: ["swiftc", "-target",
"x86_64-apple-macosx10.14", "foo.swift"],
"arm64-apple-ios12.0", "foo.swift",
"-sdk", sdkRoot.pathString],
env: envVars)
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.count, 2)
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-target")))
XCTAssertTrue(plannedJobs[0].commandLine.contains(.flag("-target")))
XCTAssertFalse(plannedJobs[0].commandLine.contains(.flag("-clang-target")))
#endif
}

func testPCHasCompileInput() throws {
Expand Down

0 comments on commit dc9c95f

Please sign in to comment.