6
votes

I am using Swift Package Manager that doesn't having a xcodeproject file associated with it and I get an error when building through he terminal. When I call the swift build command I get an error that the MacOS build failed. The package I'm building doesn't support MacOS (It uses UIKit), but only iOS. I can't figure out a way to call the command to only specify that the build is targeted for iOS. I've Google searched around with no luck. Does anybody know the correct syntax if it exists to build an SPM package for iOS from the terminal?

The version of Swift I'm using is: "Apple Swift version 5.2.4 (swiftlang-1103.0.32.9 clang-1103.0.32.53)"

I have specified the platform in the Package.swift file

let package = Package(
    name: "Package",
    platforms: [.iOS(.v10), ],
    products: [
...

I have created a sample project on github https://github.com/mike011/Swift-Package-Manager-Example. When I run swift build it fails with

/git/Swift-Package-Manager-Example/Sources/Swift-Package-Manager-Example/iOSSpecificFile.swift:9:8: error: no such module 'UIKit' import UIKit ^ /git/Swift-Package-Manager-Example/Sources/Swift-Package-Manager-Example/iOSSpecificFile.swift:9:8: error: no such module 'UIKit' import UIKit ^

2

2 Answers

1
votes

I "solved" this by wrapping all my files in #if !os(macOS) #endif blocks. So the package builds on a Mac, but it doesn't have any content.

4
votes

SwiftPM doesn't currently have a way to disallow building for a specific platform but if you want you can take advantage of the minimum build version to cause compile time errors on platforms you don't support.

For example if you don't want to allow building on macOS you can use the platform version: .macOS("99.0") in your platforms section of your manifest and you will be given the compilation warnings and errors similar to this when building in Xcode:

The macOS deployment target 'MACOSX_DEPLOYMENT_TARGET' is set to 99.0, but the range of supported deployment target versions is 10.8 to 10.16.99
Invalid Darwin version number: macos99.0
Invalid version number in 'target x86_64-apple-macos99.0'