Skip to content

Update XCFramework logic #2828

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 2 commits into from
Jul 28, 2020
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
16 changes: 14 additions & 2 deletions Sources/Build/BuildPlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1711,9 +1711,9 @@ public class BuildPlan {
return nil
}

// Check that it supports macOS.
// Check that it supports the target platform and architecture.
guard let library = info.libraries.first(where: {
$0.platform == "macos" && $0.architectures.contains(Triple.Arch.x86_64.rawValue)
return $0.platform == buildParameters.triple.os.asXCFrameworkPlatformString && $0.architectures.contains(buildParameters.triple.arch.rawValue)
}) else {
diagnostics.emit(error: """
artifact '\(target.name)' does not support the target platform and architecture \
Expand Down Expand Up @@ -1833,3 +1833,15 @@ private func generateResourceInfoPlist(
try fileSystem.writeIfChanged(path: path, bytes: stream.bytes)
return true
}

fileprivate extension Triple.OS {
/// Returns a representation of the receiver that can be compared with platform strings declared in an XCFramework.
var asXCFrameworkPlatformString: String? {
switch self {
case .darwin, .linux, .wasi, .windows:
return nil // XCFrameworks do not support any of these platforms today.
case .macOS:
return "macos"
}
}
}
20 changes: 11 additions & 9 deletions Sources/Workspace/InitPackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ public final class InitPackage {

var param = ".\(platform.manifestName)("
if supportedPlatform.isManifestAPIAvailable {
if platform == .macOS {
param += ".v10_\(version.minor)"
if version.minor > 0 {
param += ".v\(version.major)_\(version.minor)"
} else {
param += ".v\(version.major)"
}
Expand Down Expand Up @@ -516,11 +516,11 @@ extension PackageModel.Platform {

extension SupportedPlatform {
var isManifestAPIAvailable: Bool {
if platform == .macOS {
guard self.version.major == 10, self.version.patch == 0 else {
if platform == .macOS && self.version.major == 10 {
guard self.version.patch == 0 else {
return false
}
} else if [Platform.iOS, .watchOS, .tvOS].contains(platform) {
} else if [Platform.macOS, .iOS, .watchOS, .tvOS].contains(platform) {
guard self.version.minor == 0, self.version.patch == 0 else {
return false
}
Expand All @@ -529,14 +529,16 @@ extension SupportedPlatform {
}

switch platform {
case .macOS:
case .macOS where version.major == 10:
return (10...15).contains(version.minor)
case .macOS:
return (11...11).contains(version.major)
case .iOS:
return (8...13).contains(version.major)
return (8...14).contains(version.major)
case .tvOS:
return (9...13).contains(version.major)
return (9...14).contains(version.major)
case .watchOS:
return (2...6).contains(version.major)
return (2...7).contains(version.major)

default:
return false
Expand Down
26 changes: 17 additions & 9 deletions Tests/BuildTests/BuildPlanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2207,7 +2207,7 @@ final class BuildPlanTests: XCTestCase {
])
}

func testBinaryTargets() throws {
func testBinaryTargets(platform: String, arch: String, destinationTriple: Triple) throws {
let fs = InMemoryFileSystem(emptyFiles:
"/Pkg/Sources/exe/main.swift",
"/Pkg/Sources/Library/Library.swift",
Expand All @@ -2227,15 +2227,15 @@ final class BuildPlanTests: XCTestCase {
<array>
<dict>
<key>LibraryIdentifier</key>
<string>macos-x86_64</string>
<string>\(platform)-\(arch)</string>
<key>LibraryPath</key>
<string>Framework.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>x86_64</string>
<string>\(arch)</string>
</array>
<key>SupportedPlatform</key>
<string>macos</string>
<string>\(platform)</string>
</dict>
</array>
<key>CFBundlePackageType</key>
Expand All @@ -2258,17 +2258,17 @@ final class BuildPlanTests: XCTestCase {
<array>
<dict>
<key>LibraryIdentifier</key>
<string>macos-x86_64</string>
<string>\(platform)-\(arch)</string>
<key>HeadersPath</key>
<string>Headers</string>
<key>LibraryPath</key>
<string>libStaticLibrary.a</string>
<key>SupportedArchitectures</key>
<array>
<string>x86_64</string>
<string>\(arch)</string>
</array>
<key>SupportedPlatform</key>
<string>macos</string>
<string>\(platform)</string>
</dict>
</array>
<key>CFBundlePackageType</key>
Expand Down Expand Up @@ -2307,7 +2307,7 @@ final class BuildPlanTests: XCTestCase {
XCTAssertNoDiagnostics(diagnostics)

let result = BuildPlanResult(plan: try BuildPlan(
buildParameters: mockBuildParameters(destinationTriple: .macOS),
buildParameters: mockBuildParameters(destinationTriple: destinationTriple),
graph: graph,
diagnostics: diagnostics,
fileSystem: fs
Expand Down Expand Up @@ -2335,14 +2335,22 @@ final class BuildPlanTests: XCTestCase {

let clibraryBasicArguments = try result.target(for: "CLibrary").clangTarget().basicArguments()
XCTAssertMatch(clibraryBasicArguments, [.anySequence, "-F", "/path/to/build/debug", .anySequence])
XCTAssertMatch(clibraryBasicArguments, [.anySequence, "-I", "/Pkg/StaticLibrary.xcframework/macos-x86_64/Headers", .anySequence])
XCTAssertMatch(clibraryBasicArguments, [.anySequence, "-I", "/Pkg/StaticLibrary.xcframework/\(platform)-\(arch)/Headers", .anySequence])

let clibraryLinkArguments = try result.buildProduct(for: "CLibrary").linkArguments()
XCTAssertMatch(clibraryLinkArguments, [.anySequence, "-F", "/path/to/build/debug", .anySequence])
XCTAssertMatch(clibraryLinkArguments, [.anySequence, "-L", "/path/to/build/debug", .anySequence])
XCTAssertMatch(clibraryLinkArguments, ["-lStaticLibrary"])
}

func testBinaryTargets() throws {
try testBinaryTargets(platform: "macos", arch: "x86_64", destinationTriple: .macOS)
let arm64Triple = try Triple("arm64-apple-macosx")
try testBinaryTargets(platform: "macos", arch: "arm64", destinationTriple: arm64Triple)
let arm64eTriple = try Triple("arm64e-apple-macosx")
try testBinaryTargets(platform: "macos", arch: "arm64e", destinationTriple: arm64eTriple)
}

func testAddressSanitizer() throws {
try sanitizerTest(.address, expectedName: "address")
}
Expand Down