From c80ceda985807823f9c38f7311a42d5c81aa49ec Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Wed, 6 Mar 2024 12:06:44 -0800 Subject: [PATCH] Java: Allow limiting which archs are built for Android And use this to cut down CI testing time: only build armv7 and aarch64 slices of the real library, and x86_64 for the testing library (which we don't even run in the every-commit CI, but we want to make sure we haven't broken something in that configuration). --- .github/workflows/build_and_test.yml | 2 +- java/android/build.gradle | 28 +++++---- java/build_jni.sh | 85 +++++++++++++++------------- 3 files changed, 63 insertions(+), 52 deletions(-) diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index d848bc11b..e303e80dd 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -260,7 +260,7 @@ jobs: - name: Verify that the JNI bindings are up to date run: rust/bridge/jni/bin/gen_java_decl.py --verify - - run: ./gradlew build assembleDebugAndroidTest android:lintDebug | tee ./gradle-output.txt + - run: ./gradlew build assembleDebugAndroidTest android:lintDebug -PandroidArchs=arm,arm64 -PandroidTestingArchs=x86_64 | tee ./gradle-output.txt working-directory: java shell: bash # Explicitly setting the shell turns on pipefail in GitHub Actions diff --git a/java/android/build.gradle b/java/android/build.gradle index b7b9bd09c..5259ebce4 100644 --- a/java/android/build.gradle +++ b/java/android/build.gradle @@ -69,22 +69,28 @@ preBuild { dependsOn 'makeTestJniLibraries' } +String[] archsFromProperty(String prop) { + findProperty(prop)?.split(',')?.findAll { it != '' }?.collect { 'android-' + it } +} + task makeJniLibraries(type:Exec) { - group 'Rust' - description 'Build the JNI libraries for Android' + group 'Rust' + description 'Build the JNI libraries for Android' - // Explicitly specify 'bash' for Windows compatibility. - commandLine 'bash', '../build_jni.sh', 'android' - environment 'ANDROID_NDK_HOME', android.ndkDirectory + def archs = archsFromProperty('androidArchs') ?: ['android'] + // Explicitly specify 'bash' for Windows compatibility. + commandLine 'bash', '../build_jni.sh', *archs + environment 'ANDROID_NDK_HOME', android.ndkDirectory } task makeTestJniLibraries(type:Exec) { - group 'Rust' - description 'Build JNI libraries for Android for testing' + group 'Rust' + description 'Build JNI libraries for Android for testing' - // Explicitly specify 'bash' for Windows compatibility. - commandLine 'bash', '../build_jni.sh', 'android', '--testing' - environment 'ANDROID_NDK_HOME', android.ndkDirectory + def archs = archsFromProperty('androidTestingArchs') ?: archsFromProperty('androidArchs') ?: ['android'] + // Explicitly specify 'bash' for Windows compatibility. + commandLine 'bash', '../build_jni.sh', '--testing', *archs + environment 'ANDROID_NDK_HOME', android.ndkDirectory } task collectAssets(type:Copy) { @@ -137,4 +143,4 @@ afterEvaluate { required { isReleaseBuild() && gradle.taskGraph.hasTask(":android:publish") } sign publishing.publications.mavenJava } -} \ No newline at end of file +} diff --git a/java/build_jni.sh b/java/build_jni.sh index f40c91901..3cbf2cafe 100755 --- a/java/build_jni.sh +++ b/java/build_jni.sh @@ -18,54 +18,59 @@ DESKTOP_LIB_DIR=java/shared/resources export CARGO_PROFILE_RELEASE_DEBUG=1 # enable line tables export CARGO_PROFILE_RELEASE_OPT_LEVEL=s # optimize for size over speed -case "${1:-}" in - desktop ) - # On Linux, cdylibs don't include public symbols from their dependencies, - # even if those symbols have been re-exported in the Rust source. - # Using LTO works around this at the cost of a slightly slower build. - # https://github.com/rust-lang/rfcs/issues/2771 - export CARGO_PROFILE_RELEASE_LTO=thin - echo_then_run cargo build -p libsignal-jni --release --features testing-fns - if [[ -z "${CARGO_BUILD_TARGET:-}" ]]; then - copy_built_library target/release signal_jni "${DESKTOP_LIB_DIR}/" - fi - exit - ;; - android ) - android_abis=(arm64-v8a armeabi-v7a x86_64 x86) - ;; - android-arm64 | android-aarch64 ) - android_abis=(arm64-v8a) - ;; - android-arm | android-armv7 ) - android_abis=(armeabi-v7a) - ;; - android-x86_64 ) - android_abis=(x86_64) - ;; - android-x86 | android-i686 ) - android_abis=(x86) - ;; - *) - echo "Unknown target (use 'desktop', 'android', or 'android-\$ARCH')" >&2 - exit 2 - ;; -esac - BUILD_FOR_TEST= -case "${2:-}" in +case "${1:-}" in --testing ) BUILD_FOR_TEST=1 + shift ;; - '') - # If unset, do nothing. - ;; - *) - echo "Unrecognized flag $2; use --testing to compile with test functions" >&2 + -* ) + echo "Unrecognized flag $1; use --testing to compile with test functions" >&2 exit 2 ;; + *) + # Do nothing + ;; esac +android_abis=() +while [ "${1:-}" != "" ]; do + case "${1:-}" in + desktop ) + # On Linux, cdylibs don't include public symbols from their dependencies, + # even if those symbols have been re-exported in the Rust source. + # Using LTO works around this at the cost of a slightly slower build. + # https://github.com/rust-lang/rfcs/issues/2771 + export CARGO_PROFILE_RELEASE_LTO=thin + echo_then_run cargo build -p libsignal-jni --release --features testing-fns + if [[ -z "${CARGO_BUILD_TARGET:-}" ]]; then + copy_built_library target/release signal_jni "${DESKTOP_LIB_DIR}/" + fi + exit + ;; + android ) + android_abis+=(arm64-v8a armeabi-v7a x86_64 x86) + ;; + android-arm64 | android-aarch64 ) + android_abis+=(arm64-v8a) + ;; + android-arm | android-armv7 ) + android_abis+=(armeabi-v7a) + ;; + android-x86_64 ) + android_abis+=(x86_64) + ;; + android-x86 | android-i686 ) + android_abis+=(x86) + ;; + *) + echo "Unknown target '${1:-}' (use 'desktop', 'android', or 'android-\$ARCH')" >&2 + exit 2 + ;; + esac + shift +done + # Everything from here down is Android-only. # Use full LTO and small BoringSSL curve tables to reduce binary size.