Skip to content

Improve handling of non-macOS Darwin triples (reprise) #6732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Sources/Basics/Triple+Basics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ extension Triple {
}

switch os {
case .darwin, .macosx:
case _ where isDarwin():
return ".dylib"
case .linux, .openbsd:
return ".so"
Expand All @@ -155,7 +155,7 @@ extension Triple {
}

switch os {
case .darwin, .macosx:
case _ where isDarwin():
return ""
case .linux, .openbsd:
return ""
Expand All @@ -178,7 +178,7 @@ extension Triple {
/// The file extension for Foundation-style bundle.
public var nsbundleExtension: String {
switch os {
case .darwin, .macosx:
case _ where isDarwin():
return ".bundle"
default:
// See: https://github.com/apple/swift-corelibs-foundation/blob/master/Docs/FHS%20Bundles.md
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription

// When deploying to macOS prior to macOS 12, add an rpath to the
// back-deployed concurrency libraries.
if useStdlibRpath, self.buildParameters.targetTriple.isDarwin(),
if useStdlibRpath, self.buildParameters.targetTriple.isMacOSX,
let macOSSupportedPlatform = self.package.platforms.getDerived(for: .macOS),
macOSSupportedPlatform.version.major < 12
{
Expand Down
7 changes: 4 additions & 3 deletions Sources/Build/BuildPlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ extension BuildParameters {
var args = ["-target"]
// Compute the triple string for Darwin platform using the platform version.
if targetTriple.isDarwin() {
guard let macOSSupportedPlatform = target.platforms.getDerived(for: .macOS) else {
throw StringError("the target \(target) doesn't support building for macOS")
let platform = buildEnvironment.platform
guard let supportedPlatform = target.platforms.getDerived(for: platform) else {
throw StringError("the target \(target) doesn't support building for the \(platform.name) platform")
}
args += [targetTriple.tripleString(forPlatformVersion: macOSSupportedPlatform.version.versionString)]
args += [targetTriple.tripleString(forPlatformVersion: supportedPlatform.version.versionString)]
} else {
args += [targetTriple.tripleString]
}
Expand Down
23 changes: 21 additions & 2 deletions Sources/SPMBuildCore/BinaryTarget+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ extension BinaryTarget {
// At the moment we return at most a single library.
let metadata = try XCFrameworkMetadata.parse(fileSystem: fileSystem, rootPath: self.artifactPath)
// Filter the libraries that are relevant to the triple.
// FIXME: this filter needs to become more sophisticated
guard let library = metadata.libraries.first(where: {
$0.platform == triple.os?.asXCFrameworkPlatformString &&
$0.architectures.contains(triple.archName)
$0.variant == triple.environment?.asXCFrameworkPlatformVariantString &&
$0.architectures.contains(triple.archName)
}) else {
return []
}
Expand Down Expand Up @@ -100,8 +100,27 @@ extension Triple.OS {
return nil // XCFrameworks do not support any of these platforms today.
case .macosx:
return "macos"
case .ios:
return "ios"
case .tvos:
return "tvos"
case .watchos:
return "watchos"
default:
return nil // XCFrameworks do not support any of these platforms today.
}
}
}

extension Triple.Environment {
fileprivate var asXCFrameworkPlatformVariantString: String? {
switch self {
case .simulator:
return "simulator"
case .macabi:
return "maccatalyst"
default:
return nil // XCFrameworks do not support any of these platform variants today.
}
}
}
13 changes: 12 additions & 1 deletion Sources/SPMBuildCore/BuildParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,18 @@ public struct BuildParameters: Encodable {
/// The current platform we're building for.
var currentPlatform: PackageModel.Platform {
if self.targetTriple.isDarwin() {
return .macOS
switch self.targetTriple.darwinPlatform {
case .iOS(.catalyst):
return .macCatalyst
case .iOS(.device), .iOS(.simulator):
return .iOS
case .tvOS:
return .tvOS
case .watchOS:
return .watchOS
case .macOS, nil:
return .macOS
}
} else if self.targetTriple.isAndroid() {
return .android
} else if self.targetTriple.isWASI() {
Expand Down
6 changes: 5 additions & 1 deletion Sources/SPMBuildCore/XCFrameworkMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ public struct XCFrameworkMetadata: Equatable {
public let headersPath: String?
public let platform: String
public let architectures: [String]
public let variant: String?

public init(
libraryIdentifier: String,
libraryPath: String,
headersPath: String?,
platform: String,
architectures: [String]
architectures: [String],
variant: String?
) {
self.libraryIdentifier = libraryIdentifier
self.libraryPath = libraryPath
self.headersPath = headersPath
self.platform = platform
self.architectures = architectures
self.variant = variant
}
}

Expand Down Expand Up @@ -78,6 +81,7 @@ extension XCFrameworkMetadata.Library: Decodable {
case headersPath = "HeadersPath"
case platform = "SupportedPlatform"
case architectures = "SupportedArchitectures"
case variant = "SupportedPlatformVariant"
}
}

Expand Down
59 changes: 59 additions & 0 deletions Tests/BuildTests/BuildPlanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3270,6 +3270,65 @@ final class BuildPlanTests: XCTestCase {
#endif
}

func testPlatformsCustomTriple() throws {
let fileSystem = InMemoryFileSystem(emptyFiles:
"/A/Sources/ATarget/foo.swift",
"/B/Sources/BTarget/foo.swift"
)

let observability = ObservabilitySystem.makeForTesting()
let graph = try loadPackageGraph(
fileSystem: fileSystem,
manifests: [
Manifest.createRootManifest(
displayName: "A",
path: "/A",
platforms: [
PlatformDescription(name: "ios", version: "11.0"),
PlatformDescription(name: "macos", version: "10.13"),
],
toolsVersion: .v5,
dependencies: [
.localSourceControl(path: "/B", requirement: .upToNextMajor(from: "1.0.0")),
],
targets: [
TargetDescription(name: "ATarget", dependencies: ["BLibrary"]),
]),
Manifest.createFileSystemManifest(
displayName: "B",
path: "/B",
platforms: [
PlatformDescription(name: "ios", version: "10.0"),
PlatformDescription(name: "macos", version: "10.12"),
],
toolsVersion: .v5,
products: [
ProductDescription(name: "BLibrary", type: .library(.automatic), targets: ["BTarget"]),
],
targets: [
TargetDescription(name: "BTarget", dependencies: []),
]),
],
observabilityScope: observability.topScope
)
XCTAssertNoDiagnostics(observability.diagnostics)

let result = try BuildPlanResult(plan: BuildPlan(
buildParameters: mockBuildParameters(targetTriple: .init("arm64-apple-ios")),
graph: graph,
fileSystem: fileSystem,
observabilityScope: observability.topScope
))

let targetTriple = try Triple("arm64-apple-ios")

let aTarget = try result.target(for: "ATarget").swiftTarget().compileArguments()
XCTAssertMatch(aTarget, [.equal("-target"), .equal(targetTriple.tripleString(forPlatformVersion: "11.0")), .anySequence])

let bTarget = try result.target(for: "BTarget").swiftTarget().compileArguments()
XCTAssertMatch(bTarget, [.equal("-target"), .equal(targetTriple.tripleString(forPlatformVersion: "11.0")), .anySequence])
}

func testPlatformsValidation() throws {
let fileSystem = InMemoryFileSystem(emptyFiles:
"/A/Sources/ATarget/foo.swift",
Expand Down
29 changes: 27 additions & 2 deletions Tests/SPMBuildCoreTests/XCFrameworkMetadataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ final class XCFrameworkMetadataTests: XCTestCase {
<key>SupportedPlatform</key>
<string>macos</string>
</dict>
<dict>
<key>LibraryIdentifier</key>
<string>ios-arm64_x86_64-simulator</string>
<key>LibraryPath</key>
<string>MyFramework.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>ios</string>
<key>SupportedPlatformVariant</key>
<string>simulator</string>
</dict>
</array>
<key>CFBundlePackageType</key>
<string>XFWK</string>
Expand All @@ -55,7 +70,16 @@ final class XCFrameworkMetadataTests: XCTestCase {
libraryPath: "MyFramework.framework",
headersPath: nil,
platform: "macos",
architectures: ["x86_64"]
architectures: ["x86_64"],
variant: nil
),
XCFrameworkMetadata.Library(
libraryIdentifier: "ios-arm64_x86_64-simulator",
libraryPath: "MyFramework.framework",
headersPath: nil,
platform: "ios",
architectures: ["arm64", "x86_64"],
variant: "simulator"
),
]))
}
Expand Down Expand Up @@ -102,7 +126,8 @@ final class XCFrameworkMetadataTests: XCTestCase {
libraryPath: "MyLibrary.a",
headersPath: "Headers",
platform: "macos",
architectures: ["x86_64"]
architectures: ["x86_64"],
variant: nil
),
]))
}
Expand Down