Skip to content

Commit

Permalink
Add script to build deterministic android aar
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBlueMatt committed Jul 14, 2021
1 parent aaaa51a commit da49351
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 11 deletions.
92 changes: 87 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 "[email protected]"
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
Expand Down Expand Up @@ -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
Expand Down
74 changes: 74 additions & 0 deletions android-build.sh
Original file line number Diff line number Diff line change
@@ -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
7 changes: 3 additions & 4 deletions build-release-jar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ 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
unzip ../target/ldk-java-1.0-SNAPSHOT.jar | grep inflating | awk '{ print $2 }' | sort > ../classes-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
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
4 changes: 2 additions & 2 deletions genbindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."
Expand Down

0 comments on commit da49351

Please sign in to comment.