Skip to content

Commit 5f7cc65

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 99b0b06 commit 5f7cc65

File tree

1 file changed

+75
-6
lines changed

1 file changed

+75
-6
lines changed

Sources/SwiftBuildSupport/SwiftBuildSystem.swift

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -418,16 +418,35 @@ 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+
if self.buildParameters.triple.isAndroid() {
424+
// Android triples are identified by the environment part of the triple
425+
platformName = "android"
426+
} else {
427+
platformName = self.buildParameters.triple.darwinPlatform?.platformName ?? self.buildParameters.triple.osNameUnversioned
428+
}
429+
430+
let sdkVariant: String?
431+
if self.buildParameters.triple.environment == .macabi {
432+
sdkVariant = "iosmac"
433+
} else {
434+
sdkVariant = nil
435+
}
436+
437+
return SwiftBuild.SWBRunDestinationInfo(
438+
platform: platformName,
439+
sdk: platformName,
440+
sdkVariant: sdkVariant,
427441
targetArchitecture: buildParameters.triple.archName,
428442
supportedArchitectures: [],
429443
disableOnlyActiveArch: false
430444
)
445+
}
446+
447+
func makeBuildParameters() throws -> SwiftBuild.SWBBuildParameters {
448+
// Generate the run destination parameters.
449+
let runDestination = makeRunDestination()
431450

432451
var verboseFlag: [String] = []
433452
if self.logLevel == .debug {
@@ -444,6 +463,18 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
444463
// FIXME: workaround for old Xcode installations such as what is in CI
445464
settings["LM_SKIP_METADATA_EXTRACTION"] = "YES"
446465

466+
let normalizedTriple = Triple(buildParameters.triple.triple, normalizing: true)
467+
if let deploymentTargetSettingName = normalizedTriple.deploymentTargetSettingName {
468+
let value = normalizedTriple.deploymentTargetVersion
469+
470+
// Only override the deployment target if a version is explicitly specified;
471+
// for Apple platforms this normally comes from the package manifest and may
472+
// not be set to the same value for all packages in the package graph.
473+
if value != .zero {
474+
settings[deploymentTargetSettingName] = value.description
475+
}
476+
}
477+
447478
settings["LIBRARY_SEARCH_PATHS"] = try "$(inherited) \(buildParameters.toolchain.toolchainLibDir.pathString)"
448479
settings["OTHER_CFLAGS"] = (
449480
["$(inherited)"]
@@ -562,3 +593,41 @@ fileprivate extension SwiftBuild.SwiftBuildMessage.DiagnosticInfo.Location {
562593
}
563594
}
564595
}
596+
597+
fileprivate extension Triple {
598+
var deploymentTargetSettingName: String? {
599+
switch (self.os, self.environment) {
600+
case (.macosx, _):
601+
return "MACOSX_DEPLOYMENT_TARGET"
602+
case (.ios, _):
603+
return "IPHONEOS_DEPLOYMENT_TARGET"
604+
case (.tvos, _):
605+
return "TVOS_DEPLOYMENT_TARGET"
606+
case (.watchos, _):
607+
return "WATCHOS_DEPLOYMENT_TARGET"
608+
case (_, .android):
609+
return "ANDROID_DEPLOYMENT_TARGET"
610+
default:
611+
return nil
612+
}
613+
}
614+
615+
var deploymentTargetVersion: Version {
616+
if isAndroid() {
617+
// Android triples store the version in the environment
618+
var environmentName = self.environmentName[...]
619+
if environment != nil {
620+
let prefixes = ["androideabi", "android"]
621+
for prefix in prefixes {
622+
if environmentName.hasPrefix(prefix) {
623+
environmentName = environmentName.dropFirst(prefix.count)
624+
break
625+
}
626+
}
627+
}
628+
629+
return Version(parse: environmentName)
630+
}
631+
return osVersion
632+
}
633+
}

0 commit comments

Comments
 (0)