How to use CoreML Model in a Swift Package Manager macOS Command Line App? - swift

I'm trying to create a command line app that runs on macOS and uses CoreML. I am using Swift Package Manager. Here are some steps to reproduce the problem:
Run
swift package init --type executable
Move a CoreML model into the Sources folder, for example, CNNEmotions.mlmodel
In main.swift, write
print(CNNEmotions()) // CNNEmotions is the name of the generated model type
I know that CoreML only runs on macOS and iOS., so in Package.swift, I added a platform parameter:
let package = Package.init(
name: "MyProject",
platforms: [
.macOS(.v10_13), // this is the minimum macOS version that supports CoreML
],
...
Run
swift build
And there will be an error saying that CNNEmotions is an unresolved identifier.
If I do swift package generate-xcodeproj and open up the project in Xcode, I can see that the .mlmodel file is not added to the project at all!
This person on Apple Forums seems to also be in the same situation as me, but there are no answers to their question...
How can I make SPM work with mlmodels?
Just when I thought it's the end of the world, I found that if I add the .mlmodel file into the project in Xcode, the program can compile and run successfully. But the problem with building it in Xcode is that it's quite inconvenient. It doesn't produce an executable in a convenient location (I have to go all the way to the DerivedData folder to find it). To be honest, I would just like a convenient command to build & run it, like swift build. This is my ultimate goal.

Related

Is it possible to produce an XCFramework from a Swift Package with dependencies?

I want to be able to take an internal SPM package, which depends on several other internal (and one external) SPM packages, and compile it into an XCFramework, using a series of xcodebuild like you would for a framework project. For example, I have PackageB, which references PackageA, and I'm trying to build PackageB.xcframework. The first step would be:
xcodebuild -scheme PackageB -destination "generic/platform=iOS" -configuration Release ARCHS="arm64" BUILD_DIR="./Build
The tool's output indicates that the dependency packages are being resolved, but they are then not recognized by the compiler (i.e. code in PackageB which references PackageA will not compile because PackageA is unknown at that point).
Thanks for any pointers.
You can create xcframework from swift package by using swift-create-xcframework command line tool, there is also GitHub action available for this.
If you don't want to use this tool, you can generate Xcode project by using swift package generate-xcodeproj command:
OVERVIEW: Generates an Xcode project. This command will be deprecated soon.
USAGE: swift package generate-xcodeproj
OPTIONS:
--xcconfig-overrides
Path to xcconfig file
--output Path where the Xcode project should be generated
--legacy-scheme-generator
Use the legacy scheme generator
--watch Watch for changes to the Package manifest to
regenerate the Xcode project
--skip-extra-files Do not add file references for extra files to the
generated Xcode project
--version Show the version.
-h, -help, --help Show help information.
and then create xcframework with the generated Xcode project.

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
^
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.
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'

Tried archiving flutter project with no disc space left, can't fix errors

Question upfront: How can I thoroughly clean/repair my flutter project to allow it to run/build again?
I had been building and archiving (in Xcode) flutter projects without issue until just now.
My computer ran out of disc space mid archive and forced it to stop with "nonzero exit code".
I deleted a bunch of things on the computer to make space, and now it says there is >100gb left.
I was due for a flutter upgrade, so I ran flutter upgrade, which completed without error.
I closed android studio and Xcode and restarted my computer.
I ran flutter clean as well as clean within Xcode.
I can no longer run the app within flutter or run/archive within Xcode.
The exact errors seem variable, but here are some of them that I see:
Running from Xcode:
.../ios/Flutter/Flutter.framework/Headers/FlutterPlugin.h:292:13: Cannot find protocol declaration for 'FlutterTextureRegistry'; did you mean 'FlutterPluginRegistry'?
.../ios/Flutter/Flutter.framework/Headers/FlutterEngine.h:54:38: Cannot find protocol declaration for 'FlutterTextureRegistry'; did you mean 'FlutterPluginRegistry'?
.../.pub-cache/hosted/pub.dartlang.org/in_app_purchase-0.3.1+2/ios/Classes/FIAPReceiptManager.m:13:9: Could not build module 'Flutter'
Running from Flutter (I can't paste all of the errors... way too long):
^~~~~~~~~~~~~~~~~~~~~~
FlutterPluginRegistry
While building module 'Flutter' imported from .../.pub-cache/hosted/pub.dartlang.org/path_provider-1.6.5/ios/Classes/FLTPathProviderPlugin.h:5:
In file included from :1:
In file included from .../Documents/Flutter/patient_medical_record/ios/Pods/../Flutter/Flutter.framework/Headers/Flutter.h:54:
In file included from .../Documents/Flutter/patient_medical_record/ios/Pods/../Flutter/Flutter.framework/Headers/FlutterAppDelegate.h:11:
.../Documents/Flutter/patient_medical_record/ios/Pods/../Flutter/Flutter.framework/Headers/FlutterPlugin.h:395:11: note: 'FlutterPluginRegistry' declared here
#protocol FlutterPluginRegistry
^
3 errors generated.
In file included from .../.pub-cache/hosted/pub.dartlang.org/path_provider-1.6.5/ios/Classes/FLTPathProviderPlugin.m:5:
.../.pub-cache/hosted/pub.dartlang.org/path_provider-1.6.5/ios/Classes/FLTPathProviderPlugin.h:5:9: fatal error: could not build module 'Flutter'
#import
~~~~~~~^
4 errors generated.
note: Using new build system
note: Building targets in parallel
note: Planning build
note: Constructing build description
Well this is a bummer because I can't find a clean solution, but for now I did fix it with the following:
Create a new flutter project
Copied over the lib files
Copied over other setup in random places (i.e. google services, android changes)
Copied over pubspec.yaml and ran packages get
It now works again. Clearly something went wrong in the Flutter-managed files that I don't understand. If someone else can think of a better solution to effectively reset the project, please share.

Convert XCodeProj to Package.swift

I have recently inherited a swift project and would prefer to do my development on a Linux VM, rather then the under powered Mac I have. I have found lots of tutorials on how to got from a Package.swift to a xcodeproj. But I haven't found anything to go the other direction (xcodeproj to Package.swift). It seems that I need the Package.swift to be able to use the swift cli (swift build).
Is there a way to build without the Package.swift file?
Is there a way to do this that I haven't found?
Is there a technical reason that we can't go from a xcodeproj to a Package.swift?
1./2.
A Package.swift file is internally translated to a set of swiftc commands.
If you run swift build -v (verbose) on a fully working Swift package, you can see the commands that are being run. Using these commands, you may be able to figure out how to compile any collection of Swift sources without a Package.swift.
For a framework, one command could look something like this:
swiftc -module-name TestPackage -emit-dependencies -emit-module -c Path/to/Sources/*.swift -Onone -g -enable-testing -j8 -DSWIFT_PACKAGE -DDEBUG -Xfrontend -color-diagnostics
When compiling an executable, there may be additional commands to link the standard library, etc.
3.
The feature set of Xcode far exceeds the capabilities of Swift packages (e.g. .app bundles, Storyboards, Metal source files, ObjC, C, C++, etc.)
However, you can convert your command line project to a Swift package by running swift package init --type library|executable to create a Package.swift file. You can then specify a path parameter for the target in the Package.swift file:
.target(name: "YourTarget", path: "Path/To/Your/Swift/Files")

Cannot build any Swift project at all due to strange error about MacOSX10.11.sdk

I'm running macOS 10.12.3 with Xcode 8.3.
I cannot build any macOS Swift projects with it.
If I create a new Cocoa application Xcode project and set it to use Swift, when I try to compile it, I immediately get the following error in the "check dependencies" phase:
error: There is no SDK with the name or path '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk'
I have no idea what's going wrong!
The only clue I have is that earlier I created a symlink with the path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk that pointed to MacOSX.sdk in order to fix an issue where an older build of Qt that required an SDK with that path. However, deleting the symlink has no effect.
I can build Swift projects that targeting other platforms like iOS. Just not macOS.
Help!
try running this command then reopening the project
(Xcode uses xtools in the command line to build its projects)
sudo xcode-select -switch /path/to/file/
(by default the path is)/Applications/Xcode.app/
look at this pic
saw it here yesterday