Skip to content

Commit 78956df

Browse files
committed
[Explicit Module Builds] Add support for '-clang-target-variant' flag
Adds equivalent functionality to that specified by '-clang-target' but for the zippered library variant on Darwin platforms.
1 parent 5da348d commit 78956df

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

Sources/SwiftDriver/Toolchains/DarwinToolchain.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,23 @@ public final class DarwinToolchain: Toolchain {
453453

454454
commandLine.appendFlag(.clangTarget)
455455
commandLine.appendFlag(clangTargetTriple)
456+
457+
// Repeat the above for the '-target-variant' flag
458+
if driver.parsedOptions.contains(.targetVariant),
459+
driver.isFrontendArgSupported(.clangTargetVariant),
460+
let targetVariantTripleStr = frontendTargetInfo.targetVariant?.triple {
461+
let clangTargetVariantTriple: String
462+
if let explicitClangTargetVariantArg = driver.parsedOptions.getLastArgument(.clangTargetVariant)?.asSingle {
463+
clangTargetVariantTriple = explicitClangTargetVariantArg
464+
} else {
465+
let currentVariantTriple = targetVariantTripleStr
466+
let sdkVersionedOSSString = currentVariantTriple.osNameUnversioned + sdkInfo.sdkVersion(for: currentVariantTriple).sdkVersionString
467+
clangTargetVariantTriple = currentVariantTriple.triple.replacingOccurrences(of: currentVariantTriple.osName, with: sdkVersionedOSSString)
468+
}
469+
470+
commandLine.appendFlag(.clangTargetVariant)
471+
commandLine.appendFlag(clangTargetVariantTriple)
472+
}
456473
}
457474

458475
if driver.isFrontendArgSupported(.externalPluginPath) {

Sources/SwiftOptions/Options.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ extension Option {
8686
public static let clangIncludeTreeFilelist: Option = Option("-clang-include-tree-filelist", .separate, attributes: [.helpHidden, .frontend, .noDriver], metaVar: "<cas-id>", helpText: "Clang Include Tree FileList CASID")
8787
public static let clangIncludeTreeRoot: Option = Option("-clang-include-tree-root", .separate, attributes: [.helpHidden, .frontend, .noDriver], metaVar: "<cas-id>", helpText: "Clang Include Tree CASID")
8888
public static let clangScannerModuleCachePath: Option = Option("-clang-scanner-module-cache-path", .separate, attributes: [.frontend, .doesNotAffectIncrementalBuild, .argumentIsPath], helpText: "Specifies the Clang dependency scanner module cache path")
89+
public static let clangTargetVariant: Option = Option("-clang-target-variant", .separate, attributes: [.frontend, .synthesizeInterface], helpText: "Separately set the target we should use for internal Clang instance for the 'zippered' code for macCatalyst")
8990
public static let clangTarget: Option = Option("-clang-target", .separate, attributes: [.frontend, .synthesizeInterface], helpText: "Separately set the target we should use for internal Clang instance")
9091
public static let codeCompleteCallPatternHeuristics: Option = Option("-code-complete-call-pattern-heuristics", .flag, attributes: [.helpHidden, .frontend, .noDriver], helpText: "Use heuristics to guess whether we want call pattern completions")
9192
public static let codeCompleteInitsInPostfixExpr: Option = Option("-code-complete-inits-in-postfix-expr", .flag, attributes: [.helpHidden, .frontend, .noDriver], helpText: "Include initializers when completing a postfix expression")
@@ -1029,6 +1030,7 @@ extension Option {
10291030
Option.clangIncludeTreeFilelist,
10301031
Option.clangIncludeTreeRoot,
10311032
Option.clangScannerModuleCachePath,
1033+
Option.clangTargetVariant,
10321034
Option.clangTarget,
10331035
Option.codeCompleteCallPatternHeuristics,
10341036
Option.codeCompleteInitsInPostfixExpr,

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4346,6 +4346,32 @@ final class SwiftDriverTests: XCTestCase {
43464346
job.commandLine.contains(.flag("-clang-target"))
43474347
})
43484348
}
4349+
4350+
// Check -clang-target-variant is handled correctly with the MacCatalyst remap.
4351+
try withTemporaryDirectory { path in
4352+
let main = path.appending(component: "Foo.swift")
4353+
try localFileSystem.writeFileContents(main, bytes:
4354+
"""
4355+
import Swift
4356+
"""
4357+
)
4358+
var driver = try Driver(args: ["swiftc", "-explicit-module-build",
4359+
"-target", "arm64e-apple-ios13.0-macabi",
4360+
"-target-variant", "arm64e-apple-macos10.0",
4361+
"-sdk", sdkRoot.pathString,
4362+
main.pathString])
4363+
guard driver.isFrontendArgSupported(.clangTarget) else {
4364+
throw XCTSkip("Skipping: compiler does not support '-clang-target'")
4365+
}
4366+
guard driver.isFrontendArgSupported(.clangTargetVariant) else {
4367+
throw XCTSkip("Skipping: compiler does not support '-clang-target-variant'")
4368+
}
4369+
let plannedJobs = try driver.planBuild()
4370+
XCTAssertTrue(plannedJobs.contains { job in
4371+
job.commandLine.contains(subsequence: [.flag("-clang-target"), .flag("arm64e-apple-ios13.3-macabi")]) &&
4372+
job.commandLine.contains(subsequence: [.flag("-clang-target-variant"), .flag("arm64e-apple-macos10.15")])
4373+
})
4374+
}
43494375
#endif
43504376
}
43514377

0 commit comments

Comments
 (0)