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 Jan 27, 2023
1 parent fc4753a commit ed40829
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
13 changes: 1 addition & 12 deletions Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,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 @@ -324,7 +313,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
25 changes: 24 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,29 @@ 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 if let sdkPathHandle = frontendTargetInfo.sdkPath?.path,
let sdkPath = VirtualPath.lookup(sdkPathHandle).absolutePath,
let sdkInfo = Self.readSDKInfo(fileSystem, VirtualPath.absolute(sdkPath).intern()) {
clangTargetTriple = frontendTargetInfo.target.unversionedTriple.triple + sdkInfo.versionString
} else {
clangTargetTriple = frontendTargetInfo.target.triple.triple
}

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
6 changes: 3 additions & 3 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3622,7 +3622,7 @@ final class SwiftDriverTests: XCTestCase {
#endif
}

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

Expand All @@ -3631,8 +3631,8 @@ final class SwiftDriverTests: XCTestCase {
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(.flag("-clang-target")))
}

func testPCHasCompileInput() throws {
Expand Down

0 comments on commit ed40829

Please sign in to comment.