From: Matt Corallo Date: Wed, 14 Jul 2021 00:54:10 +0000 (+0000) Subject: Add script to build deterministic android aar X-Git-Tag: v0.0.99.1 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=0969a2a6892c620ac50d5d8db1bb52fec631d5aa;p=ldk-java Add script to build deterministic android aar --- diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 96e06626..18dec673 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -71,7 +71,7 @@ jobs: - name: Checkout latest MacOS binaries run: | export LDK_GARBAGECOLLECTED_GIT_OVERRIDE="$(cat .git_ver)" - git clone https://git.bitcoin.ninja/ldk-java-bins + git clone --depth 1 https://git.bitcoin.ninja/ldk-java-bins mkdir -p src/main/resources/ cp "ldk-java-bins/${LDK_GARBAGECOLLECTED_GIT_OVERRIDE}/liblightningjni_MacOSX-"* src/main/resources/ - name: Build Java/TS Release Bindings @@ -82,7 +82,7 @@ jobs: run: ./build-release-jar.sh - name: Run Java Tests against built release jar run: | - mvn install:install-file -Dfile=target/ldk-java-1.0-SNAPSHOT.jar -DgroupId=org.ldk -DartifactId=ldk-java -Dversion=1.0-SNAPSHOT -Dpackaging=jar + mvn install:install-file -Dfile=ldk-java.jar -DgroupId=org.ldk -DartifactId=ldk-java -Dversion=1.0-SNAPSHOT -Dpackaging=jar cd javatester mvn package java -ea -jar target/ldk-java-tests-1.0-SNAPSHOT-jar-with-dependencies.jar @@ -93,9 +93,91 @@ jobs: - name: Check latest library and jars are in bins repo run: | export LDK_GARBAGECOLLECTED_GIT_OVERRIDE="$(cat .git_ver)" + rm "ldk-java-bins/${LDK_GARBAGECOLLECTED_GIT_OVERRIDE}/"*.jar + rm "ldk-java-bins/${LDK_GARBAGECOLLECTED_GIT_OVERRIDE}/"liblightningjni_Linux*.nativelib cp src/main/resources/liblightningjni_Linux-amd64.nativelib "ldk-java-bins/${LDK_GARBAGECOLLECTED_GIT_OVERRIDE}/" - cp target/ldk-java-1.0-SNAPSHOT.jar "ldk-java-bins/${LDK_GARBAGECOLLECTED_GIT_OVERRIDE}/" - cp target/ldk-java-1.0-SNAPSHOT-sources.jar "ldk-java-bins/${LDK_GARBAGECOLLECTED_GIT_OVERRIDE}/" + cp ldk-java-sources.jar "ldk-java-bins/${LDK_GARBAGECOLLECTED_GIT_OVERRIDE}/" + cp ldk-java.jar "ldk-java-bins/${LDK_GARBAGECOLLECTED_GIT_OVERRIDE}/" + cp ldk-java-classes.jar "ldk-java-bins/${LDK_GARBAGECOLLECTED_GIT_OVERRIDE}/" + cd ldk-java-bins + git diff --exit-code + + android: + runs-on: ubuntu-latest + # Frankly, I'm not really sure why debian and ubuntu differ in the results here, they really shouldn't + container: debian:bullseye + env: + TOOLCHAIN: stable + steps: + - name: Install rust targets + run: | + apt-get update + apt-get -y dist-upgrade + apt-get -y install git g++ clang faketime zip unzip curl openjdk-11-jdk + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > rustup.sh + chmod +x ./rustup.sh + ./rustup.sh -y + . $HOME/.cargo/env + rustup target add armv7-linux-androideabi + rustup target add aarch64-linux-android + rustup target add i686-linux-android + rustup target add x86_64-linux-android + - name: Checkout source code + uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: Install android NDK compilers + run: | + curl https://dl.google.com/android/repository/android-ndk-r22b-linux-x86_64.zip > android-ndk-r22b-linux-x86_64.zip + if [ "$(sha256sum android-ndk-r22b-linux-x86_64.zip | awk '{ print $1 }')" != "ac3a0421e76f71dd330d0cd55f9d99b9ac864c4c034fc67e0d671d022d4e806b" ]; then + echo "Bad hash" + exit 1 + fi + unzip android-ndk-r22b-linux-x86_64.zip + - name: Install cbindgen + run: | + . $HOME/.cargo/env + cargo install --force cbindgen + - name: Checkout Rust-Lightning and LDK-C-Bindings git + run: | + git config --global user.email "ldk-ci@example.com" + git config --global user.name "LDK CI" + # Note this is a different endpoint, as we need one non-upstream commit! + git clone https://git.bitcoin.ninja/rust-lightning + cd rust-lightning + git checkout origin/2021-03-java-bindings-base + cd .. + git clone https://github.com/lightningdevkit/ldk-c-bindings + - name: Detect current git version + run: | + # We assume the top commit is just a bindings update commit, so we + # check out the previous commit to use as the commit we git describe. + # If the top commit is a merge commit, we need to get the last merge + # head and assume the latest bindings are built against its parent. + COMMIT_PARENTS=$(git show -s --pretty=format:%P HEAD) + if [ "${#COMMIT_PARENTS}" = 40 ]; then + export LDK_GARBAGECOLLECTED_GIT_OVERRIDE="$(git describe --tag HEAD^1)" + else + MERGE_HEAD=$(git show --pretty=format:%P HEAD | ( for last in $(cat /dev/stdin); do true; done; echo $last )) + export LDK_GARBAGECOLLECTED_GIT_OVERRIDE="$(git describe --tag $MERGE_HEAD^1)" + fi + echo "Using $LDK_GARBAGECOLLECTED_GIT_OVERRIDE as git version" + echo "$LDK_GARBAGECOLLECTED_GIT_OVERRIDE" > .git_ver + - name: Checkout Android AAR binaries and artifacts + run: git clone --depth 1 https://git.bitcoin.ninja/ldk-java-bins + - name: Build Android aar + run: | + . $HOME/.cargo/env + export LDK_GARBAGECOLLECTED_GIT_OVERRIDE="$(cat .git_ver)" + cp "ldk-java-bins/${LDK_GARBAGECOLLECTED_GIT_OVERRIDE}/ldk-java-classes.jar" ./ + export ANDROID_TOOLCHAIN="$(pwd)/android-ndk-r22b/toolchains/llvm/prebuilt/linux-x86_64" + export PATH="$PATH:$ANDROID_TOOLCHAIN/bin" + ./android-build.sh ./rust-lightning ./ldk-c-bindings/ ./ldk-java-bins/android-artifacts + - name: Check latest library and jars are in bins repo + run: | + export LDK_GARBAGECOLLECTED_GIT_OVERRIDE="$(cat .git_ver)" + rm "ldk-java-bins/${LDK_GARBAGECOLLECTED_GIT_OVERRIDE}/LDK-release.aar" + cp LDK-release.aar "ldk-java-bins/${LDK_GARBAGECOLLECTED_GIT_OVERRIDE}/" cd ldk-java-bins git diff --exit-code @@ -172,7 +254,7 @@ jobs: - name: Checkout latest Linux binaries run: | export LDK_GARBAGECOLLECTED_GIT_OVERRIDE="$(cat .git_ver)" - git clone https://git.bitcoin.ninja/ldk-java-bins + git clone --depth 1 https://git.bitcoin.ninja/ldk-java-bins mkdir -p src/main/resources/ cp "ldk-java-bins/${LDK_GARBAGECOLLECTED_GIT_OVERRIDE}/liblightningjni_Linux-"* src/main/resources/ - name: Build Java/TS Release Bindings diff --git a/android-build.sh b/android-build.sh new file mode 100755 index 00000000..fe90de72 --- /dev/null +++ b/android-build.sh @@ -0,0 +1,74 @@ +#!/bin/bash +if [ ! -x "$ANDROID_TOOLCHAIN/bin/clang" ]; then + echo "Please set ANDROID_TOOLCHAIN to the path to NDK" > /dev/stderr + exit 1 +fi + +if [ "$1" = "" -o ! -f "$1/lightning/Cargo.toml" ]; then + echo "Please set first argument to the path to rust-lightning" > /dev/stderr + exit 1 +fi + +if [ "$2" = "" -o ! -d "$2/lightning-c-bindings" ]; then + echo "Please set second argument to the path to ldk-c-bindings" > /dev/stderr + exit 1 +fi + +if [ ! -d "$3" -o ! -f "$3/AndroidManifest.xml" ]; then + echo "Please set third argument to the path to ldk-java-bins/android-artifacts" > /dev/stderr + exit 1 +fi + +set -e +set -x + +LDK_C_BINDINGS="$(realpath $2)" +RUST_LIGHTNING="$(realpath $1)" +pushd "$2" +export LDK_C_BINDINGS_EXTRA_TARGETS="x86_64-linux-android i686-linux-android armv7-linux-androideabi aarch64-linux-android" +export LDK_C_BINDINGS_EXTRA_TARGET_CCS="x86_64-linux-android21-clang i686-linux-android21-clang armv7a-linux-androideabi21-clang aarch64-linux-android21-clang" +./genbindings.sh "$RUST_LIGHTNING" true +popd + +export PATH=$PATH:$ANDROID_TOOLCHAIN/bin +export SYSROOT=$ANDROID_TOOLCHAIN/sysroot/ + +# Remove any non-Android libraries installed locally +rm -fr src/main/resources + +EXTRA_TARGETS=( $LDK_C_BINDINGS_EXTRA_TARGETS ) +EXTRA_TARGET_CCS=( $LDK_C_BINDINGS_EXTRA_TARGET_CCS ) +TARGET_CPUS=( "sandybridge" "generic" "generic" "generic" ) +STRIPS=( "x86_64-linux-android-strip" "i686-linux-android-strip" "arm-linux-androideabi-strip" "aarch64-linux-android-strip" ) +for IDX in ${!EXTRA_TARGETS[@]}; do + export CC="${EXTRA_TARGET_CCS[$IDX]}" + export LDK_TARGET="${EXTRA_TARGETS[$IDX]}" + export LDK_TARGET_CPU="${TARGET_CPUS[$IDX]}" + # Note that we expect to often fail here if we don't have wasm32 bins in the C bindings dir + ./genbindings.sh "$LDK_C_BINDINGS" "-lm -llog -I$SYSROOT/usr/include/" false true || echo + ${STRIPS[$IDX]} liblightningjni_release_${LDK_TARGET}.so +done + +export LANG=C + +echo "Need local deterministic ldk-java-classes.jar" +ls ldk-java-classes.jar + +rm -rf aar +mkdir aar +cp -r "$3/"* ./aar/ +mkdir -p ./aar/jni/{arm64,armeabi,x86,x86_64} + +cp liblightningjni_release_aarch64-linux-android.so ./aar/jni/arm64/liblightningjni.so +cp liblightningjni_release_armv7-linux-androideabi.so ./aar/jni/armeabi/liblightningjni.so +cp liblightningjni_release_i686-linux-android.so ./aar/jni/x86/liblightningjni.so +cp liblightningjni_release_x86_64-linux-android.so ./aar/jni/x86_64/liblightningjni.so +cp ldk-java-classes.jar ./aar/classes.jar + +rm -f LDK-release.aar +cd ./aar +find . | sort > ../sources-zip-files.txt +touch -d "2021-01-01 00:00 UTC" $(cat ../sources-zip-files.txt) +cat ../sources-zip-files.txt | zip -X@ ../LDK-release.aar +cd .. +rm -r aar diff --git a/build-release-jar.sh b/build-release-jar.sh index 61d6fbcc..a1e405ad 100755 --- a/build-release-jar.sh +++ b/build-release-jar.sh @@ -19,10 +19,9 @@ cd ziptmp unzip ../target/ldk-java-1.0-SNAPSHOT-sources.jar | grep inflating | awk '{ print $2 }' | sort > ../sources-zip-files.txt sed -i 's/#Fri Jan 01 00:.* UTC 2021/#Fri Jan 01 00:00:00 UTC 2021/' META-INF/maven/org.ldk/ldk-java/pom.properties touch -d "2021-01-01 00:00 UTC" $(cat ../sources-zip-files.txt) -cat ../sources-zip-files.txt | zip -X@ ../ldk-java-1.0-SNAPSHOT-sources-deterministic.jar +cat ../sources-zip-files.txt | zip -X@ ../ldk-java-sources.jar cd .. rm -r ziptmp -mv ldk-java-1.0-SNAPSHOT-sources-deterministic.jar target/ldk-java-1.0-SNAPSHOT-sources.jar mkdir ziptmp cd ziptmp @@ -30,7 +29,7 @@ unzip ../target/ldk-java-1.0-SNAPSHOT.jar | grep inflating | awk '{ print $2 }' sed -i 's/#Fri Jan 01 00:.* UTC 2021/#Fri Jan 01 00:00:00 UTC 2021/' META-INF/maven/org.ldk/ldk-java/pom.properties sed -i 's/Built-By: .*/Built-By: ldk-deterministic-builds/' META-INF/MANIFEST.MF touch -d "2021-01-01 00:00 UTC" $(cat ../classes-zip-files.txt) -cat ../classes-zip-files.txt | zip -X@ ../ldk-java-1.0-SNAPSHOT-deterministic.jar +cat ../classes-zip-files.txt | zip -X@ ../ldk-java.jar +cat ../classes-zip-files.txt | grep -v nativelib | zip -X@ ../ldk-java-classes.jar cd .. rm -r ziptmp -mv ldk-java-1.0-SNAPSHOT-deterministic.jar target/ldk-java-1.0-SNAPSHOT.jar diff --git a/genbindings.sh b/genbindings.sh index 741f9de1..c7a06d5e 100755 --- a/genbindings.sh +++ b/genbindings.sh @@ -85,7 +85,7 @@ if [ "$3" = "true" ]; then $COMPILE -o liblightningjni_debug$LDK_TARGET_SUFFIX.so -g -fsanitize=address -shared-libasan -rdynamic -I"$1"/lightning-c-bindings/include/ $2 src/main/jni/bindings.c "$1"/lightning-c-bindings/target/$LDK_TARGET/debug/libldk.a -lm else LDK_LIB="$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a - if [ "$IS_MAC" = "false" ]; then + if [ "$IS_MAC" = "false" -a "$4" = "false" ]; then COMPILE="$COMPILE -Wl,--version-script=libcode.version -fuse-ld=lld" # __cxa_thread_atexit_impl is used to more effeciently cleanup per-thread local storage by rust libstd. # However, it is not available on glibc versions 2.17 or earlier, and rust libstd has a null-check and @@ -128,7 +128,7 @@ else LDK_LIB="tmp/libldk.bc tmp/libldk.a" fi $COMPILE -o liblightningjni_release$LDK_TARGET_SUFFIX.so -flto -O3 -I"$1"/lightning-c-bindings/include/ $2 src/main/jni/bindings.c $LDK_LIB - if [ "$IS_MAC" = "false" ]; then + if [ "$IS_MAC" = "false" -a "$4" = "false" ]; then GLIBC_SYMBS="$(objdump -T liblightningjni_release$LDK_TARGET_SUFFIX.so | grep GLIBC_ | grep -v "GLIBC_2\.2\." | grep -v "GLIBC_2\.3\(\.\| \)" | grep -v "GLIBC_2.\(14\|17\) " || echo)" if [ "$GLIBC_SYMBS" != "" ]; then echo "Unexpected glibc version dependency! Some users need glibc 2.17 support, symbols for newer glibcs cannot be included."