|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# When building from Xcode we want to ensure that `cargo` is in PATH. |
| 4 | +# as a convenience, add the default cargo install location |
| 5 | +export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin |
| 6 | +export PATH="$PATH:${HOME}/.cargo/bin" |
| 7 | + |
| 8 | +LDK_DIRECTORY=$1 # directory to compile the C bindings in |
| 9 | +C_BINDINGS_SOURCE_DIRECTORY="${LDK_DIRECTORY}/lightning-c-bindings" |
| 10 | + |
| 11 | +usage() { |
| 12 | + echo "USAGE: path/to/ldk-c-bindings" |
| 13 | + exit 1 |
| 14 | +} |
| 15 | + |
| 16 | +[ "${LDK_DIRECTORY}" = "" ] && echo "Usage: ./compile_dependency_binaries.sh /path/to/ldk-c-bindings" && exit 1; |
| 17 | +[ ! -d "${LDK_DIRECTORY}" ] && echo "Provided directory does not exist" && exit 1; |
| 18 | + |
| 19 | +if ! [[ -x "$(command -v cargo)" ]]; then |
| 20 | + echo 'error: Unable to find cargo command. Cargo is not installed visit rustup.rs.' >&2 |
| 21 | + exit 127 |
| 22 | +fi |
| 23 | + |
| 24 | +set -e # stop execution upon the first error |
| 25 | + |
| 26 | +C_BINDINGS_SOURCE_DIRECTORY="$(cd ${LDK_DIRECTORY}; pwd)/lightning-c-bindings" |
| 27 | +BUILT_PRODUCTS_DIR="`pwd`/binaries" # directory to copy the shared library and headers into |
| 28 | + |
| 29 | + |
| 30 | +TARGET_NAME="libldk" |
| 31 | + |
| 32 | +if [[ $PLATFORM_NAME = "" ]]; then |
| 33 | + # default for building with xcodebuild |
| 34 | + PLATFORM_NAME="macosx" |
| 35 | +fi |
| 36 | + |
| 37 | +if [[ $PLATFORM_NAME = "macosx" ]]; then |
| 38 | + if [[ $LLVM_TARGET_TRIPLE_SUFFIX = "-macabi" ]]; then |
| 39 | + RUST_TARGET_OS="ios-macabi" |
| 40 | + else |
| 41 | + RUST_TARGET_OS="darwin" |
| 42 | + fi |
| 43 | +else |
| 44 | + echo "PLATFORM_NAME ${PLATFORM_NAME}" |
| 45 | + RUST_TARGET_OS="ios" |
| 46 | +fi |
| 47 | + |
| 48 | +# Clean any pre-existing static libraries that doesn't align with what we're currently trying to build |
| 49 | + |
| 50 | +declare -a ARCHS=("arm64") |
| 51 | + |
| 52 | +for ARCH in $ARCHS |
| 53 | +do |
| 54 | + if [[ $(lipo -info "${BUILT_PRODUCTS_DIR}/${TARGET_NAME}" 2>&1) != *"${ARCH}"* ]]; then |
| 55 | + rm -f "${BUILT_PRODUCTS_DIR}/${TARGET_NAME}" |
| 56 | + fi |
| 57 | +done |
| 58 | + |
| 59 | +if [[ $CONFIGURATION = "Debug" ]]; then |
| 60 | + RUST_CONFIGURATION="debug" |
| 61 | + RUST_CONFIGURATION_FLAG="" |
| 62 | +else |
| 63 | + RUST_CONFIGURATION="release" |
| 64 | + RUST_CONFIGURATION_FLAG="--release" |
| 65 | +fi |
| 66 | + |
| 67 | +pushd $C_BINDINGS_SOURCE_DIRECTORY |
| 68 | +export RUSTFLAGS='--cfg=c_bindings' |
| 69 | + |
| 70 | +rustup override set nightly |
| 71 | +cargo clean |
| 72 | + |
| 73 | +EXECUTABLES=() |
| 74 | +for ARCH in $ARCHS |
| 75 | +do |
| 76 | + RUST_ARCH=$ARCH |
| 77 | + if [[ $RUST_ARCH = "arm64" ]]; then |
| 78 | + RUST_ARCH="aarch64" |
| 79 | + # This is because iOS Simulator builds for x86_64 is `x86_64-apple-ios`, while arm64 is `aarch64-apple-ios-sim` |
| 80 | + if [[ $PLATFORM_NAME = "iphonesimulator" ]]; then |
| 81 | + RUST_TARGET_OS="ios-sim" |
| 82 | + fi |
| 83 | + else |
| 84 | + if [[ $PLATFORM_NAME = "iphonesimulator" ]]; then |
| 85 | + RUST_TARGET_OS="ios" |
| 86 | + fi |
| 87 | + fi |
| 88 | + |
| 89 | + echo "BUILDING ${RUST_ARCH}-apple-${RUST_TARGET_OS}" |
| 90 | + |
| 91 | + cargo build -Z build-std=panic_abort,std --features "std" --target "${RUST_ARCH}-apple-${RUST_TARGET_OS}" $RUST_CONFIGURATION_FLAG |
| 92 | + EXECUTABLES+=("$C_BINDINGS_SOURCE_DIRECTORY/target/${RUST_ARCH}-apple-${RUST_TARGET_OS}/${RUST_CONFIGURATION}/${TARGET_NAME}.a") |
| 93 | + |
| 94 | + mkdir -p "${BUILT_PRODUCTS_DIR}/${ARCH}" |
| 95 | + cp "$C_BINDINGS_SOURCE_DIRECTORY/target/${RUST_ARCH}-apple-${RUST_TARGET_OS}/${RUST_CONFIGURATION}/${TARGET_NAME}.a" "${BUILT_PRODUCTS_DIR}/${ARCH}" |
| 96 | +done |
| 97 | + |
| 98 | +rustup override unset |
| 99 | + |
| 100 | +mkdir -p "${BUILT_PRODUCTS_DIR}" |
| 101 | +xcrun --sdk $PLATFORM_NAME lipo -create "${EXECUTABLES[@]}" -output "${PROJECT_DIR}/${TARGET_NAME}.a" |
0 commit comments