iOS Development

Introduction to SPM artifact bundles

Introduction to SPM artifact bundles
Written by admin


XCFrameworks and SPM

Earlier than the introduction of the brand new format we needed to fiddle with FAT binaries to help a number of platforms. I’ve a deep dive article about frameworks and instruments that you should utilize to assemble a FAT binary, however I now not suggest it since XCFrameworks are right here to remain. 🔨


As a way to construct an XCFramework, it’s important to use Xcode and a course of could be very easy. You simply have to pick the Framework kind beneath the iOS tab once you create a brand new venture. Be happy to call it, add your Swift supply code and that is it.


You may construct this venture utilizing the command line for a number of platforms through the next script.



xcodebuild archive 
  -scheme MySDK 
  -sdk iphoneos 
  -archivePath "construct/ios_devices.xcarchive" 
  BUILD_LIBRARY_FOR_DISTRIBUTION=YES 
  SKIP_INSTALL=NO
  

xcodebuild archive 
  -scheme MySDK 
  -sdk iphonesimulator 
  -archivePath "construct/ios_simulators.xcarchive" 
  BUILD_LIBRARY_FOR_DISTRIBUTION=YES 
  SKIP_INSTALL=NO


xcodebuild archive 
  -sdk macosx MACOSX_DEPLOYMENT_TARGET=11.0 
  -arch x86_64 -arch arm64 
  BUILD_LIBRARY_FOR_DISTRIBUTION=YES 
  -scheme "MySDK" 
  -archivePath "construct/macos_devices.xcarchive" SKIP_INSTALL=NO


xcodebuild -create-xcframework 
  -framework construct/ios_devices.xcarchive/Merchandise/Library/Frameworks/MySDK.framework 
  -framework construct/ios_simulators.xcarchive/Merchandise/Library/Frameworks/MySDK.framework 
  -framework construct/macos_devices.xcarchive/Merchandise/Library/Frameworks/MySDK.framework 
  -output MySDK.xcframework


You may even construct variations for Catalyst and different working methods, if you perform a little search you possibly can simply work out the required parameters and configuration. Lengthy story quick, it’s totally straightforward to create an xcframework output together with all sort of platform slices for particular units. 😊


Now if you wish to use this XCFramework, you possibly can merely drag and drop it to your Xcode venture and it ought to work with out additional points (if it comprises the mandatory slices). Alternatively you should utilize Swift bundle supervisor and create a binary goal an hook up your exterior framework bundle through SPM. That is how a quite simple configuration file seems like.



import PackageDescription

let bundle = Bundle(
    title: "MySDK",
    merchandise: [
        .library(name: "MySDK", targets: ["MySDK"]),
    ],
    dependencies: [
        
    ],
    targets: [
        .binaryTarget(name: "MySDK", path: "./MySDK.xcframework")
    ]
)


In your venture you should utilize the library product as a typical dependency, and the underlying binary goal will handle importing the mandatory header recordsdata and linking the precise library. The one downside with this strategy is that it’s macOS (or to be much more exact Apple OS solely).

Say good day to artifact bundles for Swift PM


All proper, so XCFrameworks cannot be used beneath Linux, however individuals like to write down command line scripts in Swift and use them for server facet initiatives. In some circumstances these scripts (or plugins), wish to name exterior scripts that aren’t put in on the system by default. That is the place artifact bundles may help, as a result of it makes doable to ship a number of variations of the identical executable binary file. 🤔


Artifact bundles are usually not a substitute for xcframeworks, however extra like an addition, or enchancment because the proposal title signifies this, for the Swift bundle supervisor plugin structure. They permit us to ship precompiled binary recordsdata for a number of platforms, this manner plugin authors do not must compile these instruments from supply and the plugin execution time might be closely lowered.


There’s a nice weblog put up about wrapping the SwiftLint executable in an artifact bundle, so I do not actually wish to get into the small print this time, as a result of it is fairly simple. The proposal itself helps loads to grasp the fundamental setup, additionally the older binary dependencies proposal comprises some associated data good job Swift group. 👍


I might like to offer an honorable point out to Karim Alweheshy, who’s actively working with the brand new Swift bundle supervisor plugin infrastructure, he has an superb repository on GitHub that demos artifact bundles so please have a look when you’ve got time. 🙏


Anyway, I’ll present you how one can wrap an executable into an artifact bundle. Presently there is no technique to wrap libraries into artifact bundles, that is going to be added in a while.



mkdir MyApp
cd $_
swift bundle init --type=executable


swift construct -c launch


cp $(swift construct --show-bin-path -c launch)/MyApp ./myapp



mkdir MyPluginExample
cd $_
swift bundle init 

mkdir myapp.artifactbundle
cd $_
mkdir myapp-1.0.0-macos
cd $_
mkdir bin


Now the file construction is prepared, we should always create a brand new data.json file beneath the artifactbundle listing with the next contents. It will describe your bundle with the out there binary variants, you possibly can check out the proposals for the out there triplets variations.


{
    "schemaVersion": "1.0",
    "artifacts": {
        "myapp": {
            "model": "1.0.0",
            "kind": "executable",
            "variants": [
                {
                    "path": "myapp-1.0.0-macos/bin/myapp",
                    "supportedTriples": ["x86_64-apple-macosx", "arm64-apple-macosx"]
                }
            ]
        }
    }
}


Copy the myapp binary beneath the myapp-1.0.0-macos/bin/myapp location, and eventually we will make a quite simple command plugin to take advangate of this newly added software.


import PackagePlugin
import Basis

@predominant
struct MyDistCommandPlugin: CommandPlugin {
    
    func performCommand(context: PluginContext, arguments: [String]) throws {
        let myAppTool = strive context.software(named: "myapp")
        let myAppToolURL = URL(fileURLWithPath: myAppTool.path.string)

        let course of = strive Course of.run(myAppToolURL, arguments: [])
        course of.waitUntilExit()
    }
}


Watch out with the paths and file names, I used lowercase letters for every little thing on this instance, I like to recommend to observe this sample once you create your artifact bundle binaries.


swift bundle plugin --list
# ‘good day’ (plugin ‘HelloCommandin bundle ‘MyPluginExample’)
swift bundle good day
# Whats up, world!


That is it, now we have got a working artifact bundle with a customized made executable out there for macOS. We are able to use this artifact bundle as a dependency for a plugin and run the software through the use of the plugin APIs. I might actually love to have the ability to cross compile Swift libraries and executable recordsdata in a while, this might make the event / deployment workflow a bit easier. Anyway, artifact bundles are a pleasant little addition, I actually like the best way you possibly can ship binaries for a number of platforms and I hope that we’re going to have the ability to share libraries as effectively similarly. 😊




About the author

admin

Leave a Comment