Skip to content

Commit 9b9a4eb

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 ccbf01f commit 9b9a4eb

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
@@ -418,16 +418,42 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
418418
}
419419
}
420420

421-
func makeBuildParameters() throws -> SwiftBuild.SWBBuildParameters {
422-
// Generate the run destination parameters.
423-
let runDestination = SwiftBuild.SWBRunDestinationInfo(
424-
platform: self.buildParameters.triple.osNameUnversioned,
425-
sdk: self.buildParameters.triple.osNameUnversioned,
426-
sdkVariant: nil,
421+
func makeRunDestination() -> SwiftBuild.SWBRunDestinationInfo {
422+
let platformName: String
423+
let sdkName: String
424+
if self.buildParameters.triple.isAndroid() {
425+
// Android triples are identified by the environment part of the triple
426+
platformName = "android"
427+
sdkName = platformName
428+
} else if self.buildParameters.triple.isWasm {
429+
// Swift Build uses webassembly instead of wasi as the platform name
430+
platformName = "webassembly"
431+
sdkName = platformName
432+
} else {
433+
platformName = self.buildParameters.triple.darwinPlatform?.platformName ?? self.buildParameters.triple.osNameUnversioned
434+
sdkName = platformName
435+
}
436+
437+
let sdkVariant: String?
438+
if self.buildParameters.triple.environment == .macabi {
439+
sdkVariant = "iosmac"
440+
} else {
441+
sdkVariant = nil
442+
}
443+
444+
return SwiftBuild.SWBRunDestinationInfo(
445+
platform: platformName,
446+
sdk: sdkName,
447+
sdkVariant: sdkVariant,
427448
targetArchitecture: buildParameters.triple.archName,
428449
supportedArchitectures: [],
429450
disableOnlyActiveArch: false
430451
)
452+
}
453+
454+
func makeBuildParameters() throws -> SwiftBuild.SWBBuildParameters {
455+
// Generate the run destination parameters.
456+
let runDestination = makeRunDestination()
431457

432458
var verboseFlag: [String] = []
433459
if self.logLevel == .debug {
@@ -444,6 +470,18 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
444470
// FIXME: workaround for old Xcode installations such as what is in CI
445471
settings["LM_SKIP_METADATA_EXTRACTION"] = "YES"
446472

473+
let normalizedTriple = Triple(buildParameters.triple.triple, normalizing: true)
474+
if let deploymentTargetSettingName = normalizedTriple.deploymentTargetSettingName {
475+
let value = normalizedTriple.deploymentTargetVersion
476+
477+
// Only override the deployment target if a version is explicitly specified;
478+
// for Apple platforms this normally comes from the package manifest and may
479+
// not be set to the same value for all packages in the package graph.
480+
if value != .zero {
481+
settings[deploymentTargetSettingName] = value.description
482+
}
483+
}
484+
447485
settings["LIBRARY_SEARCH_PATHS"] = try "$(inherited) \(buildParameters.toolchain.toolchainLibDir.pathString)"
448486
settings["OTHER_CFLAGS"] = (
449487
["$(inherited)"]
@@ -562,3 +600,41 @@ fileprivate extension SwiftBuild.SwiftBuildMessage.DiagnosticInfo.Location {
562600
}
563601
}
564602
}
603+
604+
fileprivate extension Triple {
605+
var deploymentTargetSettingName: String? {
606+
switch (self.os, self.environment) {
607+
case (.macosx, _):
608+
return "MACOSX_DEPLOYMENT_TARGET"
609+
case (.ios, _):
610+
return "IPHONEOS_DEPLOYMENT_TARGET"
611+
case (.tvos, _):
612+
return "TVOS_DEPLOYMENT_TARGET"
613+
case (.watchos, _):
614+
return "WATCHOS_DEPLOYMENT_TARGET"
615+
case (_, .android):
616+
return "ANDROID_DEPLOYMENT_TARGET"
617+
default:
618+
return nil
619+
}
620+
}
621+
622+
var deploymentTargetVersion: Version {
623+
if isAndroid() {
624+
// Android triples store the version in the environment
625+
var environmentName = self.environmentName[...]
626+
if environment != nil {
627+
let prefixes = ["androideabi", "android"]
628+
for prefix in prefixes {
629+
if environmentName.hasPrefix(prefix) {
630+
environmentName = environmentName.dropFirst(prefix.count)
631+
break
632+
}
633+
}
634+
}
635+
636+
return Version(parse: environmentName)
637+
}
638+
return osVersion
639+
}
640+
}

0 commit comments

Comments
 (0)