Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you build iOS specific packages using SPM?

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 ^

like image 901
Captain Charmi Avatar asked Jun 16 '20 20:06

Captain Charmi


People also ask

What is SPM in iOS?

Swift Package Manager (SPM) a tool that you use to manage dependencies in your iOS app projects. A dependency is a 1st- or 3rd-party library, package or framework. For example, you can add Alamofire to your iOS project with SPM.

What are SPM modules?

The goal of SPM is to allow developers to define modules that can be reused across different applications and platforms. A package could be defined to work with both iOS and macOS, for example, and therefore its features can be shared across tools and apps on those platforms.


Video Answer


2 Answers

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.

like image 173
AlexMath Avatar answered Oct 22 '22 14:10

AlexMath


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'
like image 23
bscothern Avatar answered Oct 22 '22 12:10

bscothern