BitcoinKit icon indicating copy to clipboard operation
BitcoinKit copied to clipboard

Swift Package Manager support for iOS

Open usatie opened this issue 6 years ago • 10 comments

What problem does this feature solve?

iOS Developers cannot use SwiftPM for installing BitcoinKit.

Describe what you've considered?

For now, the difference between apple platforms and Linux are

Apple platforms

  • Swift flag BitcoinKitXcode
#if BitcoinKitXcode
// code for apple platforms
#else
// code for Linux
#endif
  • Xcode
  • Carthage/CocoaPods
  • dependencies via Xcode() (Libraries/openssl) (Libraries/secp256k1)

Build Phase

if [ ! -d "$SRCROOT/Libraries/secp256k1/lib" ] || [ ! -d "$SRCROOT/Libraries/openssl/lib" ]; then
  env -i PATH=$PATH sh "$SRCROOT/setup/build_libraries.sh"
fi

modulemap BitcoinKit ├── BitcoinKit.h ├── BitcoinKit.modulemap ├── BitcoinKitPrivate.h ├── BitcoinKitPrivate.m └── Info.plist

Linux

  • SwiftPM
  • Sources/BitcoinKitPrivate/BitcoinKit.Private.swift
  • dependencies via SwiftPM
        .package(url: "https://github.com/vapor-community/copenssl.git", .exact("1.0.0-rc.1")),
        .package(url: "https://github.com/Boilertalk/secp256k1.swift", .upToNextMinor(from: "0.1.0")),
        .package(url: "https://github.com/vapor-community/random.git", .upToNextMinor(from: "1.2.0"))

usatie avatar Sep 13 '19 17:09 usatie

If I install BitcoinKit to an app via SwiftPM on Xcode11 and run the app now, this error is thrown.

dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib
  Referenced from: /Users/shunusami/Library/Developer/CoreSimulator/Devices/F835204C-1ACF-4026-BDB3-80A21C0C1F1E/data/Containers/Bundle/Application/706D4A0F-56F9-4618-B7AD-16504D2D262C/Xcode11SandBox.app/Xcode11SandBox
  Reason: no suitable image found.  Did find:
	/usr/local/opt/openssl/lib/libssl.1.0.0.dylib: mach-o, but not built for iOS simulator

usatie avatar Sep 13 '19 17:09 usatie

I've never created an SPM package before, so I do not know.... but just adding some comments... (that might be confusing or may be helpful)

So the error says that the reference to openssl is not working, as in OpenSSL binary is not embedded (SO answer to similar issue).

So currently Package.swift file contains:

    dependencies: [
        .package(url: "https://github.com/vapor-community/copenssl.git", .exact("1.0.0-rc.1")),
        .package(url: "https://github.com/Boilertalk/secp256k1.swift", .upToNextMinor(from: "0.1.0")),
        .package(url: "https://github.com/vapor-community/random.git", .upToNextMinor(from: "1.2.0"))
    ]

I thought that we, in general, were moving more towards just using C library libsecp256k1, and moving away from OpenSSL all together? If not, maybe we should? And if so, then we can remove the dependency vapor-community/copenssl.git and possibly also vapor-community/random.git?

Sajjon avatar Sep 17 '19 14:09 Sajjon

~Also, hmm... do we need to, or should we do a PR into Boilertalk/secp256k1.swift github repo, adding SPM support? As I said I'm not entirely up to speed on how SPM resolves dependencies of swift projects which does not themselves contain any Package.swift file....~

Sorry nevermind, I was blind, there is alreay a Package.swift in boilertalk/secp256k1 repo, so we should be good,

Sajjon avatar Sep 17 '19 14:09 Sajjon

So far (before Xcode11), SPM was almost only for Linux platforms. In other words, xcodebuild build is for apple platforms, and swift build command is for Linux.

Xcode build phase is dependent on shell files in setup directory. These files are written for apple platforms. So we need to re-write them.

Package.swift dependencies are for Linux. For example, vapor-community/copenssl is a wrapper library of Linux's builtin COpenSSL system library.

usatie avatar Sep 18 '19 06:09 usatie

@usatie what is the status of SPM? :) does it work on macOS for iOS dev? :)

Sajjon avatar Oct 09 '19 13:10 Sajjon

I tried SPM a couple of days and got the same error as you wrote about above @usatie

dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib

I wonder if packaging BitcoinKit using brand new XCFramework (XFWK) could resolve the issue? Apple documents it here, WWDC2019 video here

Sajjon avatar Oct 22 '19 07:10 Sajjon

That doesn't help it. I think currently BitcoinKit's SPM only works for Linux.

usatie avatar Oct 22 '19 08:10 usatie

A little bit late, but possibly useful for everyone stumbling here for the same problem: BitcoinKit integrated with Swift Package Manager (and the problem with OpenSSL).

The problem Due to a refactor I decided to import BitcoinKit with SPM and not as pod anymore. Build phase fail due to

Building for iOS-simulator ... but linking in dylib libssl built for macOS

In particular, the error log refers to /usr/local/Cellar/openssl@3/.... This is due to the fact that OpenSSL is built for macOS but I'm using it in an iOS project. (Here some reference to similar problems).

The possible solutions

Out of the box solution: forked BitcoinKit, removed copenssl SPM dependency and changed with OpenSSL. There'll be some deprecation warnings in BitcoinKit.Private but everything is building and working.

Alternatives:

  • Integrate inside BitcoinKit project OpenSSL built for iOS (Here a good tutorial and here a good scripts sets to compile everything auto-magically)
  • Try to think about the fact that maybe it should be better to remove OpenSSL, as suggested by @Sajjon in one of his replies, in favor of something else.

Hope this helps!

matteogazzato avatar Apr 16 '24 10:04 matteogazzato

A little bit late, but possibly useful for everyone stumbling here for the same problem: BitcoinKit integrated with Swift Package Manager (and the problem with OpenSSL).

The problem

Due to a refactor I decided to import BitcoinKit with SPM and not as pod anymore.

Build phase fail due to


Building for iOS-simulator ... but linking in dylib libssl built for macOS

In particular, the error log refers to /usr/local/Cellar/openssl@3/....

This is due to the fact that OpenSSL is built for macOS but I'm using it in an iOS project.

(Here some reference to similar problems).

The possible solutions

Out of the box solution: forked BitcoinKit, removed copenssl SPM dependency and changed with OpenSSL. There'll be some deprecation warnings in BitcoinKit.Private but everything is building and working.

Alternatives:

  • Integrate inside BitcoinKit project OpenSSL built for iOS (Here a good tutorial and here a good scripts sets to compile everything auto-magically)

  • Try to think about the fact that maybe it should be better to remove OpenSSL, as suggested by @Sajjon in one of his replies, in favor of something else.

Hope this helps!

What do you use BitcoinKit for? This project is obsolete and should have been archived long time ago.

Have a look at my package K1: https://github.com/Sajjon/K1

Sajjon avatar Apr 16 '24 11:04 Sajjon

Still using in one of our project but the idea is to pass to something else. Thanks for your suggestion

matteogazzato avatar Apr 16 '24 11:04 matteogazzato