Skip to content

Commit

Permalink
Enable -clang-target on Darwin platforms set to the SDK version by-de…
Browse files Browse the repository at this point in the history
…fault
  • Loading branch information
artemcm committed Feb 2, 2023
1 parent 65e0b28 commit 31941bd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 24 deletions.
13 changes: 1 addition & 12 deletions Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,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 @@ -323,7 +312,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
21 changes: 20 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,25 @@ 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) {
// 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
28 changes: 19 additions & 9 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3586,20 +3586,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 @@ -3609,8 +3613,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 @@ -3622,17 +3628,21 @@ final class SwiftDriverTests: XCTestCase {
#endif
}

func testDisableClangTargetForImplicitModule() throws {
func testEnableClangTargetForImplicitModule() 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")))
XCTAssertFalse(plannedJobs[0].commandLine.contains(.flag("-clang-target")))
XCTAssertTrue(plannedJobs[0].commandLine.contains(.flag("-target")))
XCTAssertTrue(plannedJobs[0].commandLine.contains(subsequence: [.flag("-clang-target"), .flag("arm64-apple-ios13.0")]))
#endif
}

func testPCHasCompileInput() throws {
Expand Down

0 comments on commit 31941bd

Please sign in to comment.