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

Fix build script for secp256k1 on iOS #287

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added ._bitcoin_issue_fix.md
Binary file not shown.
23 changes: 12 additions & 11 deletions BitcoinKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@ Pod::Spec.new do |spec|
spec.version = '1.1.0'
spec.summary = 'Bitcoin(BCH/BTC) protocol toolkit for Swift'
spec.description = <<-DESC
The BitcoinKit library is a Swift implementation of the Bitcoin(BCH/BTC) protocol. This library was originally made by Katsumi Kishikawa, and now is maintained by Yenom Inc. It allows maintaining a wallet and sending/receiving transactions without needing a full blockchain node. It comes with a simple wallet app showing how to use it.
```
DESC
spec.homepage = 'https://github.com/yenom/BitcoinKit'
The BitcoinKit library is a Swift implementation of the Bitcoin(BCH/BTC) protocol. This library was originally made by Katsumi Kishikawa, and now is maintained by Yenom Inc. It allows maintaining a wallet and sending/receiving transactions without needing a full blockchain node. It comes with a simple wallet app showing how to use it.
DESC
spec.homepage = 'https://github.com/Whitehare2023/BitcoinKit'
spec.license = { :type => 'MIT', :file => 'LICENSE' }
spec.author = { 'BitcoinKit developers' => '[email protected]' }

spec.requires_arc = true
spec.source = { git: 'https://github.com/yenom/BitcoinKit.git', tag: "v#{spec.version}" }
spec.source = { git: 'https://github.com/Whitehare2023/BitcoinKit.git', branch: 'fix-secp256k1-build' }
spec.source_files = 'BitcoinKit/**/*.{h,m,swift}', 'Sources/BitcoinKit/**/*.{h,m,swift}'
spec.private_header_files = 'BitcoinKit/**/BitcoinKitPrivate.h'
spec.exclude_files = 'Sources/**/LinuxSupport.swift'
spec.module_map = 'BitcoinKit/BitcoinKit.modulemap'
spec.ios.deployment_target = '8.0'
spec.swift_version = '5.0'

spec.pod_target_xcconfig = { 'SWIFT_WHOLE_MODULE_OPTIMIZATION' => 'YES',
'APPLICATION_EXTENSION_API_ONLY' => 'YES',
'SWIFT_INCLUDE_PATHS' => '${PODS_ROOT}/BitcoinKit/Libraries',
'HEADER_SEARCH_PATHS' => '"${PODS_ROOT}/BitcoinKit/Libraries/openssl/include" "${PODS_ROOT}/BitcoinKit/Libraries/secp256k1/include"',
'LIBRARY_SEARCH_PATHS' => '"${PODS_ROOT}/BitcoinKit/Libraries/openssl/lib" "${PODS_ROOT}/BitcoinKit/Libraries/secp256k1/lib"',
'OTHER_SWIFT_FLAGS' => '-D BitcoinKitXcode' }
spec.pod_target_xcconfig = {
'SWIFT_WHOLE_MODULE_OPTIMIZATION' => 'YES',
'APPLICATION_EXTENSION_API_ONLY' => 'YES',
'SWIFT_INCLUDE_PATHS' => '${PODS_ROOT}/BitcoinKit/Libraries',
'HEADER_SEARCH_PATHS' => '"${PODS_ROOT}/BitcoinKit/Libraries/openssl/include" "${PODS_ROOT}/BitcoinKit/Libraries/secp256k1/include"',
'LIBRARY_SEARCH_PATHS' => '"${PODS_ROOT}/BitcoinKit/Libraries/openssl/lib" "${PODS_ROOT}/BitcoinKit/Libraries/secp256k1/lib"',
'OTHER_SWIFT_FLAGS' => '-D BitcoinKitXcode'
}
spec.preserve_paths = ['setup', 'Libraries']
spec.prepare_command = 'sh setup/build_libraries.sh'
end
37 changes: 37 additions & 0 deletions Sources/BitcoinKit/Core/Mnemonic/BitArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,43 @@ public struct BitArray: Hashable, RangeReplaceableCollection {
/// Constructs an empty bit array.
public init() {}

public mutating func replaceSubrange<C>(_ subrange: Range<Int>, with newElements: C) where C : Collection, Bool == C.Element {
let removeCount = subrange.count
let insertCount = newElements.count
let shift = insertCount - removeCount

if shift > 0 {
bits.reserveCapacity(bits.count + (shift / Constants.IntSize) + 1)
}

// Shift existing elements to make space or close the gap
for i in stride(from: count - 1, through: subrange.lowerBound, by: -1) {
let targetIndex = i + shift
if targetIndex < count {
let value = valueAtIndex(i)
setValue(value, atIndex: targetIndex)
}
}

// Insert new elements
var currentIndex = subrange.lowerBound
for element in newElements {
setValue(element, atIndex: currentIndex)
currentIndex += 1
}

// Update count
count += shift

// Recalculate cardinality
cardinality = 0
for i in 0..<count {
if valueAtIndex(i) {
cardinality += 1
}
}
}

/// Constructs a bit array from a `Bool` sequence, such as an array.
public init<S: Sequence>(_ elements: S) where S.Iterator.Element == Bool {
for value in elements {
Expand Down
54 changes: 54 additions & 0 deletions bitcoin_issue_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

# BitcoinKit Issue Fix

## 问题 / Issue
在运行 `pod install` 时,构建 `secp256k1` 库时出现了以下错误:
While running `pod install`, the following error occurred during the `secp256k1` library build:

```
fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: /var/folders/jp/86j3w3fx08j54wjdp81ln0n80000gn/T/tmp.SKt6tdknEr/.build/iphoneos/lib/libsecp256k1.a and /var/folders/jp/86j3w3fx08j54wjdp81ln0n80000gn/T/tmp.SKt6tdknEr/.build/iphonesimulator/lib/libsecp256k1.a have the same architectures (x86_64) and can't be in the same fat output file
```

## 原因 / Cause
这是因为构建的库在 `iphoneos` 和 `iphonesimulator` 中包含相同的架构(如 x86_64),导致 `lipo` 命令无法合并这些库。
This is because the built libraries for `iphoneos` and `iphonesimulator` contain the same architecture (e.g., x86_64), causing the `lipo` command to fail to merge these libraries.

## 解决方法 / Solution
修改 `build_secp256k1.sh` 脚本,确保正确处理架构。具体更改如下:
Modify the `build_secp256k1.sh` script to correctly handle the architectures. The specific changes are as follows:

```sh
#!/bin/sh
set -ex

SCRIPT_DIR=`dirname "$0"`

TDIR=`mktemp -d`
trap "{ cd - ; rm -rf $TDIR; exit 255; }" SIGINT

cd $TDIR

git clone https://github.com/bitcoin-core/secp256k1.git src

CURRENTPATH=`pwd`

TARGETDIR_IPHONEOS="$CURRENTPATH/.build/iphoneos"
mkdir -p "$TARGETDIR_IPHONEOS"

TARGETDIR_SIMULATOR="$CURRENTPATH/.build/iphonesimulator"
mkdir -p "$TARGETDIR_SIMULATOR"

(cd src && ./autogen.sh)
(cd src && ./configure --host=arm-apple-darwin CC=`xcrun -find clang` CFLAGS="-O3 -arch armv7 -arch armv7s -arch arm64 -isysroot `xcrun -sdk iphoneos --show-sdk-path` -fembed-bitcode -mios-version-min=8.0" CXX=`xcrun -find clang++` CXXFLAGS="-O3 -arch armv7 -arch armv7s -arch arm64 -isysroot `xcrun -sdk iphoneos --show-sdk-path` -fembed-bitcode -mios-version-min=8.0" --prefix="$TARGETDIR_IPHONEOS" && make install)
(cd src && ./configure --host=x86_64-apple-darwin CC=`xcrun -find clang` CFLAGS="-O3 -arch x86_64 -isysroot `xcrun -sdk iphonesimulator --show-sdk-path` -fembed-bitcode-marker -mios-simulator-version-min=8.0" CXX=`xcrun -find clang++` CXXFLAGS="-O3 -arch x86_64 -isysroot `xcrun -sdk iphonesimulator --show-sdk-path` -fembed-bitcode-marker -mios-simulator-version-min=8.0" --prefix="$TARGETDIR_SIMULATOR" && make install)

cd -

mkdir -p "$SCRIPT_DIR/../Libraries/secp256k1/lib"
xcrun lipo -create "$TARGETDIR_IPHONEOS/lib/libsecp256k1.a" "$TARGETDIR_SIMULATOR/lib/libsecp256k1.a" -o "$SCRIPT_DIR/../Libraries/secp256k1/lib/libsecp256k1.a"
cp -rf $TDIR/src/include "$SCRIPT_DIR/../Libraries/secp256k1"

rm -rf $TDIR

exit 0
```
5 changes: 3 additions & 2 deletions setup/build_secp256k1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ TARGETDIR_SIMULATOR="$CURRENTPATH/.build/iphonesimulator"
mkdir -p "$TARGETDIR_SIMULATOR"

(cd src && ./autogen.sh)
(cd src && ./configure --host=x86_64-apple-darwin CC=`xcrun -find clang` CFLAGS="-O3 -arch i386 -arch x86_64 -isysroot `xcrun -sdk iphonesimulator --show-sdk-path` -fembed-bitcode-marker -mios-simulator-version-min=8.0" CXX=`xcrun -find clang++` CXXFLAGS="-O3 -arch i386 -arch x86_64 -isysroot `xcrun -sdk iphonesimulator --show-sdk-path` -fembed-bitcode-marker -mios-simulator-version-min=8.0" --prefix="$TARGETDIR_IPHONEOS" && make install)
(cd src && ./configure --host=arm-apple-darwin CC=`xcrun -find clang` CFLAGS="-O3 -arch armv7 -arch armv7s -arch arm64 -isysroot `xcrun -sdk iphoneos --show-sdk-path` -fembed-bitcode -mios-version-min=8.0" CXX=`xcrun -find clang++` CXXFLAGS="-O3 -arch armv7 -arch armv7s -arch arm64 -isysroot `xcrun -sdk iphoneos --show-sdk-path` -fembed-bitcode -mios-version-min=8.0" --prefix="$TARGETDIR_SIMULATOR" && make install)
(cd src && ./configure --host=arm-apple-darwin CC=`xcrun -find clang` CFLAGS="-O3 -arch armv7 -arch armv7s -arch arm64 -isysroot `xcrun -sdk iphoneos --show-sdk-path` -fembed-bitcode -mios-version-min=8.0" CXX=`xcrun -find clang++` CXXFLAGS="-O3 -arch armv7 -arch armv7s -arch arm64 -isysroot `xcrun -sdk iphoneos --show-sdk-path` -fembed-bitcode -mios-version-min=8.0" --prefix="$TARGETDIR_IPHONEOS" && make install)
(cd src && make clean)
(cd src && ./configure --host=x86_64-apple-darwin CC=`xcrun -find clang` CFLAGS="-O3 -arch x86_64 -isysroot `xcrun -sdk iphonesimulator --show-sdk-path` -fembed-bitcode-marker -mios-simulator-version-min=8.0" CXX=`xcrun -find clang++` CXXFLAGS="-O3 -arch x86_64 -isysroot `xcrun -sdk iphonesimulator --show-sdk-path` -fembed-bitcode-marker -mios-simulator-version-min=8.0" --prefix="$TARGETDIR_SIMULATOR" && make install)

cd -

Expand Down