yenom / BitcoinKit

Bitcoin protocol toolkit for Swift
MIT License
842 stars 262 forks source link

Swift Package Manager support for iOS #224

Open usatie opened 5 years ago

usatie commented 5 years ago

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

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

usatie commented 5 years ago

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
Sajjon commented 5 years ago

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 commented 5 years ago

~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,

usatie commented 5 years ago

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.

Sajjon commented 5 years ago

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

Sajjon commented 5 years ago

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

usatie commented 5 years ago

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

matteogazzato commented 6 months ago

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:

Hope this helps!

Sajjon commented 6 months ago

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

matteogazzato commented 6 months ago

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