Skip to content

Commit

Permalink
Java: Allow limiting which archs are built for Android
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
jrose-signal committed Mar 7, 2024
1 parent 1c8fd06 commit c80ceda
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 17 additions & 11 deletions java/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -137,4 +143,4 @@ afterEvaluate {
required { isReleaseBuild() && gradle.taskGraph.hasTask(":android:publish") }
sign publishing.publications.mavenJava
}
}
}
85 changes: 45 additions & 40 deletions java/build_jni.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit c80ceda

Please sign in to comment.