Skip to content

Commit 8ab590c

Browse files
committed
Swift Build: pass some additional settings needed for cross-compilation
This patch ensures that the Swift Build SDK and platform name are set correctly for embedded Apple platforms (iOS, tvOS, watchOS) and Android, that the SDK variant is set correctly for Mac Catalyst, and that the deployment target version is correctly propagated from the `--triple` argument specified on the command line.
1 parent dcab689 commit 8ab590c

File tree

1 file changed

+82
-6
lines changed

1 file changed

+82
-6
lines changed

Sources/SwiftBuildSupport/SwiftBuildSystem.swift

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -449,16 +449,42 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
449449
}
450450
}
451451

452-
func makeBuildParameters() throws -> SwiftBuild.SWBBuildParameters {
453-
// Generate the run destination parameters.
454-
let runDestination = SwiftBuild.SWBRunDestinationInfo(
455-
platform: self.buildParameters.triple.osNameUnversioned,
456-
sdk: self.buildParameters.triple.osNameUnversioned,
457-
sdkVariant: nil,
452+
func makeRunDestination() -> SwiftBuild.SWBRunDestinationInfo {
453+
let platformName: String
454+
let sdkName: String
455+
if self.buildParameters.triple.isAndroid() {
456+
// Android triples are identified by the environment part of the triple
457+
platformName = "android"
458+
sdkName = platformName
459+
} else if self.buildParameters.triple.isWasm {
460+
// Swift Build uses webassembly instead of wasi as the platform name
461+
platformName = "webassembly"
462+
sdkName = platformName
463+
} else {
464+
platformName = self.buildParameters.triple.darwinPlatform?.platformName ?? self.buildParameters.triple.osNameUnversioned
465+
sdkName = platformName
466+
}
467+
468+
let sdkVariant: String?
469+
if self.buildParameters.triple.environment == .macabi {
470+
sdkVariant = "iosmac"
471+
} else {
472+
sdkVariant = nil
473+
}
474+
475+
return SwiftBuild.SWBRunDestinationInfo(
476+
platform: platformName,
477+
sdk: sdkName,
478+
sdkVariant: sdkVariant,
458479
targetArchitecture: buildParameters.triple.archName,
459480
supportedArchitectures: [],
460481
disableOnlyActiveArch: false
461482
)
483+
}
484+
485+
func makeBuildParameters() throws -> SwiftBuild.SWBBuildParameters {
486+
// Generate the run destination parameters.
487+
let runDestination = makeRunDestination()
462488

463489
var verboseFlag: [String] = []
464490
if self.logLevel == .debug {
@@ -475,6 +501,18 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
475501
// FIXME: workaround for old Xcode installations such as what is in CI
476502
settings["LM_SKIP_METADATA_EXTRACTION"] = "YES"
477503

504+
let normalizedTriple = Triple(buildParameters.triple.triple, normalizing: true)
505+
if let deploymentTargetSettingName = normalizedTriple.deploymentTargetSettingName {
506+
let value = normalizedTriple.deploymentTargetVersion
507+
508+
// Only override the deployment target if a version is explicitly specified;
509+
// for Apple platforms this normally comes from the package manifest and may
510+
// not be set to the same value for all packages in the package graph.
511+
if value != .zero {
512+
settings[deploymentTargetSettingName] = value.description
513+
}
514+
}
515+
478516
settings["LIBRARY_SEARCH_PATHS"] = try "$(inherited) \(buildParameters.toolchain.toolchainLibDir.pathString)"
479517
settings["OTHER_CFLAGS"] = (
480518
["$(inherited)"]
@@ -593,3 +631,41 @@ fileprivate extension SwiftBuild.SwiftBuildMessage.DiagnosticInfo.Location {
593631
}
594632
}
595633
}
634+
635+
fileprivate extension Triple {
636+
var deploymentTargetSettingName: String? {
637+
switch (self.os, self.environment) {
638+
case (.macosx, _):
639+
return "MACOSX_DEPLOYMENT_TARGET"
640+
case (.ios, _):
641+
return "IPHONEOS_DEPLOYMENT_TARGET"
642+
case (.tvos, _):
643+
return "TVOS_DEPLOYMENT_TARGET"
644+
case (.watchos, _):
645+
return "WATCHOS_DEPLOYMENT_TARGET"
646+
case (_, .android):
647+
return "ANDROID_DEPLOYMENT_TARGET"
648+
default:
649+
return nil
650+
}
651+
}
652+
653+
var deploymentTargetVersion: Version {
654+
if isAndroid() {
655+
// Android triples store the version in the environment
656+
var environmentName = self.environmentName[...]
657+
if environment != nil {
658+
let prefixes = ["androideabi", "android"]
659+
for prefix in prefixes {
660+
if environmentName.hasPrefix(prefix) {
661+
environmentName = environmentName.dropFirst(prefix.count)
662+
break
663+
}
664+
}
665+
}
666+
667+
return Version(parse: environmentName)
668+
}
669+
return osVersion
670+
}
671+
}

0 commit comments

Comments
 (0)