Skip to content
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

Support for Swift Package Manager #19

Open
andreasley opened this issue Sep 25, 2019 · 26 comments
Open

Support for Swift Package Manager #19

andreasley opened this issue Sep 25, 2019 · 26 comments
Labels
enhancement New feature or request help wanted Extra attention is needed
Milestone

Comments

@andreasley
Copy link

It would be great if the repository contained a Package.swift manifest to allow using objectbox-swift as a dependency with Swift Package Manager.

With Xcode 11, Swift Packages can be added and managed very easily directly in the GUI and without any external requirements (e.g. CocoaPods).

@uli-objectbox
Copy link
Contributor

We're keeping an eye on SPM, but there are a few features missing in it currently to work for ObjectBox.

@uli-objectbox
Copy link
Contributor

uli-objectbox commented Sep 26, 2019

To be clearer: SwiftPM currently does not support including an already-built library, nor a helper executable like our code generator.

@uli-objectbox uli-objectbox self-assigned this Sep 26, 2019
@greenrobot
Copy link
Member

greenrobot commented Sep 26, 2019

There is a proposal here:
https://forums.swift.org/t/pitch-support-for-binary-dependencies/27620/90

If you find a better way of "tracking" Apple's progress for this, please post here.

Can we work around those limitations some how? E.g. have the Swift code packaged with SPM, and have a hook somewhere to provide the non-Swift parts?

@andreasley
Copy link
Author

There is now an official proposal for binary dependencies:
https://github.com/apple/swift-evolution/blob/master/proposals/0272-swiftpm-binary-dependencies.md

Review (currently running):
https://forums.swift.org/t/se-0272-package-manager-binary-dependencies/30753

@fishcharlie
Copy link

This will be implemented into Swift 5.3. Hopefully objectbox will be able to implement it shortly after.

PS @greenrobot Check out https://apps.apple.com/us/app/evolution-app/id1210898168.

@greenrobot
Copy link
Member

This small WWDC 2020 video "Distribute binary frameworks as Swift packages" might be helpful:
https://developer.apple.com/videos/play/wwdc2020/10147

@greenrobot
Copy link
Member

I guess setting up an XCFramework and enabling .swiftinterface would be a prerequisite. Would be also useful to Carthage; see Carthage/Carthage#2887 (comment).

@greenrobot greenrobot added the help wanted Extra attention is needed label Jun 30, 2020
@ratranqu
Copy link

ratranqu commented Jul 1, 2020

Hi all,

@paulofaria and I have been looking at this, and we are able to build and use the ObjectBox framework using spm on MacOS Catalina using the 5.2 toolchain. To do so, we've reorganised the repo to better match the standard spm project structure, and would like to discuss how we can merge the main repo and our fork going forward. Below, you can find the result of our work:
https://github.com/katalysis-io/objectbox-swift (spm version ported to 1.3.1)
https://github.com/katalysis-io/objectbox-spm-test (simple cli binary linking to objectbox using spm to manage dependencies)

We still have issues compiling on Linux due to the presence of Foundation in the ObjectiveC/C++ wrappers around the ObjectBox C lib to catch exceptions.

@greenrobot
Copy link
Member

@ratranqu Cool. Thanks for pushing this!

Seems like we have 3 topics here:

  1. Reorganizing the repo is not trivial for us, as there are some interdependent things at our end internally. But let's see what we can do over time...

  2. I'd like to understand better the swift build approach vs. XCFrameworks. Iirc, XCFrameworks only works on Apple platforms, but not on Linux? XCFrameworks still seems to be the way to go for Apple ecosystem.

  3. Not sure if those ObjectiveC/C++ wrappers are still required... Could you open a new issue with some details?

@greenrobot
Copy link
Member

We've been experimenting with XCFramework a little bit.

You can download it here if you want to play around with it:
https://github.com/objectbox/objectbox-swift/releases/download/v1.3.1/ObjectBox.xcframework.zip

Related commit with the script building the xcframework: 25222a5

While the framework is one of the pieces, we also have to get the ObjectBox Swift code generator working. Not sure if Swift Package Manager can help with that...

@greenrobot
Copy link
Member

@ratranqu (off-topic) old error handling code was removed in ced59de - for any ObjC/NS leftovers, please raise an issues.

@rogerluan
Copy link

Any updates regarding adding support to SPM? As of Swift 5.3, SPM supports embeded binaries if needed 🙏

@caoyanglee
Copy link

Waiting For SPM ~

@greenrobot
Copy link
Member

Some updates: our latest version 1.5 now uses XCFramework; this allows us to bundle the runtime library using SPM. Expect some updates on that very soon. This will still require some manual setup for the code generator, that needs to be integrated into the build.

Next, we are looking forward to the availability of Package Manager Extensible Build Tools. If things work out, this SwiftPM plugin mechanism will eliminate the manual setup.

@greenrobot
Copy link
Member

OK, we have a preliminary SPM installation in place. Check the SPM tab in our Swift docs. Not sure if setting up the generator works for everyone yet, please let us know. If it doesn't, check the manual setup for the generator.

@funcJeff
Copy link

funcJeff commented Apr 6, 2022

Howdy. I added ObjectBox to a tiny app project.

In Xcode, I went to File > Add Packages..., pasted the ObjectBox GitHub URL into the search field, saw objectbox-swift and objectbox-swift-generator, chose objectbox-swift and pressed Add Package.

After I ran:

~/Library/Developer/Xcode/DerivedData/ndgrf2-cqpqfeggakslaudpbqkvztxcabps/SourcePackages/checkouts/objectbox-swift/Source/ios-framework/cocoapod/setup.rb ~/Code/ndgrf2/ndgrf2.xcodeproj/

And output looked good: a few blue diamonds and a couple green checkmarks.

I added the 12 lines of sample code from here under "Initializing a Store". The Xcode live issues feature had no problem with the code.

I pressed command+B for Simulator and got this build error:

Script: /Users/jeff/Code/ndgrf2/../../Library/Developer/Xcode/DerivedData/ndgrf2-cqpqfeggakslaudpbqkvztxcabps/SourcePackages/checkouts/objectbox-swift/Source/ios-framework/cocoapod/generate_sources.sh
Script realpath: /Users/jeff/Library/Developer/Xcode/DerivedData/ndgrf2-cqpqfeggakslaudpbqkvztxcabps/SourcePackages/checkouts/objectbox-swift/Source/ios-framework/cocoapod/generate_sources.sh
error: Cannot find Sourcery in the expected location at '/Users/jeff/Library/Developer/Xcode/DerivedData/ndgrf2-cqpqfeggakslaudpbqkvztxcabps/SourcePackages/checkouts/objectbox-swift/Source/ios-framework/cocoapod/Sourcery.app/Contents/MacOS/Sourcery'

So I guess I need to add that objectbox-swift-generator package also?

@greenrobot-team
Copy link
Member

greenrobot-team commented Oct 9, 2023

@coderliu007 Is this related to #82? This does not seem to be related to this issue.

Edit: note that ObjectBox for Swift does not support Swift Package Manager, yet. We only did an experimental release a while back.

@greenrobot-team
Copy link
Member

We just published a preview release of a new Swift Package that also includes the generator as a Swift command plugin. We welcome you to try it out and give your feedback!

See the Install ObjectBox Swift page for details.

Note that for the time being the ObjectBox Swift APIs (the code in this repo) are included as a binary target.

@maxibello
Copy link

Hi, I tried tot use SPM package objectbox-swift-spm:

  1. intalled package
  2. added Person class from tutorial to my target
  3. run ObjectBoxGeneratorCommand

But code wasn't generated, in logs I found "Target has no files"

What I have done wrong?

@greenrobot-team greenrobot-team added this to the 4.1.0 milestone Nov 25, 2024
@greenrobot-team
Copy link
Member

@maxibello Thanks for reporting!

Based on that message: did you maybe select the wrong target when running the generator command?

Anyhow, can you share more details so I can reproduce this? Like what type of project, how is it set up? Maybe share a ZIP or GitHub repo with the project? What version of Xcode are you using?

@maxibello
Copy link

Hi @greenrobot-team and thx for reply.
Unfortunately I can not provide repo or zip cause it's private.
I tried to reproduce the problem on an empty project but code generation works there.

I am using xcode 16.0 and here is log of ObjectBoxGeneratorCommand:

working directory:
/Users/maxibello/dev/deardiary
tool mapping:
objectbox-generator: /Users/maxibello/Library/Developer/Xcode/DerivedData/DearDiary-cefreptbjyvwnfhjczmjuklitprt/SourcePackages/artifacts/objectbox-swift-spm/ObjectBoxGenerator/Contents/MacOS/Sourcery [arm64-apple-macosx]
tool paths:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/Applications/Xcode.app/Contents/Developer/usr/bin
/bin
/sbin
/usr/bin
/usr/sbin

Target DearDiary has no files

@greenrobot-team
Copy link
Member

greenrobot-team commented Nov 26, 2024

@maxibello Thanks for the details!

For whatever reason, the DearDiary target of your Xcode project has no input files (XcodeTarget.inputFiles is nil). In an empty project I tried to remove all files, frameworks and assets from a target. But XcodeTarget.inputFiles would never be nil.

In the settings for the DearDiary target, maybe check under Build Phases that the Compile Sources phase has Swift files that contain your ObjectBox model classes.

Otherwise, can you at least share how your files and targets are organized? It might be an unusual setup that we did not think of.

@maxibello
Copy link

Hi @greenrobot-team
I've checked Build Phases -> Compile Sources list of files
Screenshot 2024-11-26 at 14 58 14

I have only one target and quote simple files structure
Screenshot 2024-11-26 at 14 54 36
Screenshot 2024-11-26 at 14 55 25

Hope these screenshots will help

@greenrobot-team
Copy link
Member

@maxibello Thanks. I'm still not sure why the Swift Plugin API would return no input files then.

A new preview release (4.1.0-beta.2) is available that only warns about this, but otherwise proceeds with running the generator. Maybe that will solve your issue.

@maxibello
Copy link

maxibello commented Nov 27, 2024

Hi @greenrobot-team thanks for your help, it seems to work now!

I can see generated files for my Person entity
I've attached full command log
command_log.txt

@greenrobot
Copy link
Member

@maxibello Glad to hear! Did the full round-trip work; including storing, loading and maybe querying for your Person entity?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

10 participants