2020-11-04 23:37:41 +01:00
|
|
|
#!/bin/bash
|
2020-11-04 18:30:59 +01:00
|
|
|
|
|
|
|
#
|
2022-07-26 01:36:53 +02:00
|
|
|
# Copyright (C) 2020-2022 Signal Messenger, LLC.
|
2020-11-04 18:30:59 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
#
|
|
|
|
|
2020-12-08 23:40:30 +01:00
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
SCRIPT_DIR=$(dirname "$0")
|
|
|
|
cd "${SCRIPT_DIR}"/..
|
|
|
|
. bin/build_helpers.sh
|
|
|
|
|
2020-11-04 18:30:59 +01:00
|
|
|
# These paths are relative to the root directory
|
|
|
|
ANDROID_LIB_DIR=java/android/src/main/jniLibs
|
2024-07-31 19:07:54 +02:00
|
|
|
DESKTOP_LIB_DIR=java/client/src/main/resources
|
|
|
|
SERVER_LIB_DIR=java/server/src/main/resources
|
2020-11-04 18:30:59 +01:00
|
|
|
|
2020-12-04 20:35:03 +01:00
|
|
|
export CARGO_PROFILE_RELEASE_DEBUG=1 # enable line tables
|
2024-07-30 22:37:55 +02:00
|
|
|
export RUSTFLAGS="--cfg aes_armv8 --cfg polyval_armv8 ${RUSTFLAGS:-}" # Enable ARMv8 cryptography acceleration when available
|
2020-11-04 18:30:59 +01:00
|
|
|
|
2024-06-12 21:43:24 +02:00
|
|
|
DEBUG_LEVEL_LOGS=
|
|
|
|
while [ "${1:-}" != "" ]; do
|
|
|
|
case "${1:-}" in
|
|
|
|
--debug-level-logs )
|
|
|
|
DEBUG_LEVEL_LOGS=1
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-* )
|
2024-08-24 01:12:00 +02:00
|
|
|
echo "Unrecognized flag $1; expected --debug-level-logs" >&2
|
2024-06-12 21:43:24 +02:00
|
|
|
exit 2
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
break
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ -z "${DEBUG_LEVEL_LOGS:-}" ]]; then
|
|
|
|
FEATURES+=("log/release_max_level_info")
|
|
|
|
fi
|
2024-02-15 22:53:05 +01:00
|
|
|
|
2024-07-31 22:25:12 +02:00
|
|
|
# usage: build_desktop_for_arch target_triple host_triple output_dir
|
2024-07-30 22:37:55 +02:00
|
|
|
build_desktop_for_arch () {
|
|
|
|
local CC
|
|
|
|
local CXX
|
|
|
|
local CPATH
|
|
|
|
|
2024-07-31 19:07:54 +02:00
|
|
|
local lib_dir="${3}/"
|
2024-07-30 22:37:55 +02:00
|
|
|
local cpuarch="${1%%-*}"
|
|
|
|
case "$cpuarch" in
|
|
|
|
x86_64)
|
|
|
|
suffix=amd64
|
|
|
|
;;
|
|
|
|
aarch64)
|
|
|
|
suffix=aarch64
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "building for unknown CPU architecture ${cpuarch}; update build_jni.sh"
|
|
|
|
exit 2
|
|
|
|
esac
|
|
|
|
if [[ "$1" != "$2" ]]; then
|
|
|
|
# Set up cross-compiling flags
|
2024-07-31 22:25:12 +02:00
|
|
|
if [[ "$1" == *-linux-* && "$2" == *-linux-* && -z "${CC:-}" ]]; then
|
|
|
|
# When cross-compiling *from* Linux *to* Linux,
|
|
|
|
# set up standard cross-compiling environment if not already set
|
|
|
|
echo 'setting Linux cross-compilation options...'
|
|
|
|
export "CARGO_TARGET_$(echo "$cpuarch" | tr "[:lower:]" "[:upper:]")_UNKNOWN_LINUX_GNU_LINKER"="${cpuarch}-linux-gnu-gcc"
|
|
|
|
export CC="${cpuarch}-linux-gnu-gcc"
|
|
|
|
export CXX="${cpuarch}-linux-gnu-g++"
|
|
|
|
export CPATH="/usr/${cpuarch}-linux-gnu/include"
|
2024-07-30 22:37:55 +02:00
|
|
|
fi
|
|
|
|
fi
|
2024-08-24 01:12:00 +02:00
|
|
|
|
2024-07-30 22:37:55 +02:00
|
|
|
echo_then_run cargo build -p libsignal-jni -p libsignal-jni-testing --release ${FEATURES:+--features "${FEATURES[*]}"} --target "$1"
|
2024-07-31 22:25:12 +02:00
|
|
|
copy_built_library "target/${1}/release" signal_jni "$lib_dir" "signal_jni_${suffix}"
|
|
|
|
copy_built_library "target/${1}/release" signal_jni_testing "$lib_dir" "signal_jni_testing_${suffix}"
|
2024-07-30 22:37:55 +02:00
|
|
|
}
|
|
|
|
|
2024-08-01 22:42:36 +02:00
|
|
|
android_abis=()
|
|
|
|
|
2024-03-06 21:06:44 +01:00
|
|
|
while [ "${1:-}" != "" ]; do
|
|
|
|
case "${1:-}" in
|
2024-07-30 22:37:55 +02:00
|
|
|
desktop | server | server-all )
|
2024-07-31 20:22:35 +02:00
|
|
|
if [[ "$1" == desktop ]]; then
|
|
|
|
lib_dir=$DESKTOP_LIB_DIR
|
|
|
|
else
|
|
|
|
lib_dir=$SERVER_LIB_DIR
|
|
|
|
fi
|
2024-03-06 21:06:44 +01:00
|
|
|
# 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
|
2024-07-30 22:37:55 +02:00
|
|
|
host_triple=$(rustc -vV | sed -n 's|host: ||p')
|
|
|
|
if [[ "$1" == "server-all" ]]; then
|
2024-07-31 19:07:54 +02:00
|
|
|
build_desktop_for_arch x86_64-unknown-linux-gnu "$host_triple" $lib_dir
|
2024-07-31 22:25:12 +02:00
|
|
|
# Enable ARMv8.2 extensions for a production aarch64 server build
|
|
|
|
RUSTFLAGS="-C target-feature=+v8.2a ${RUSTFLAGS:-}" \
|
|
|
|
build_desktop_for_arch aarch64-unknown-linux-gnu "$host_triple" $lib_dir
|
2024-07-30 22:37:55 +02:00
|
|
|
else
|
2024-07-31 22:25:12 +02:00
|
|
|
build_desktop_for_arch "${CARGO_BUILD_TARGET:-$host_triple}" "$host_triple" $lib_dir
|
2024-03-06 21:06:44 +01:00
|
|
|
fi
|
|
|
|
exit
|
|
|
|
;;
|
2024-07-30 22:37:55 +02:00
|
|
|
|
2024-03-06 21:06:44 +01:00
|
|
|
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
|
|
|
|
|
2024-08-01 22:42:36 +02:00
|
|
|
if (( ${#android_abis[@]} == 0 )); then
|
|
|
|
echo "Missing target (use 'desktop', 'android', or 'android-\$ARCH')" >&2
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
2023-10-20 03:14:19 +02:00
|
|
|
# Everything from here down is Android-only.
|
2024-07-30 22:37:55 +02:00
|
|
|
export CARGO_PROFILE_RELEASE_OPT_LEVEL=s # optimize for size over speed
|
2023-10-20 03:14:19 +02:00
|
|
|
|
|
|
|
# Use full LTO and small BoringSSL curve tables to reduce binary size.
|
|
|
|
export CFLAGS="-DOPENSSL_SMALL -flto=full ${CFLAGS:-}"
|
|
|
|
export CARGO_PROFILE_RELEASE_LTO=fat
|
|
|
|
export CARGO_PROFILE_RELEASE_CODEGEN_UNITS=1
|
2023-10-04 22:49:19 +02:00
|
|
|
|
|
|
|
# Use the Android NDK's prebuilt Clang+lld as pqcrypto's compiler and Rust's linker.
|
|
|
|
ANDROID_TOOLCHAIN_DIR=$(echo "${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt"/*/bin/)
|
|
|
|
export CC_aarch64_linux_android="${ANDROID_TOOLCHAIN_DIR}/aarch64-linux-android21-clang"
|
|
|
|
export CC_armv7_linux_androideabi="${ANDROID_TOOLCHAIN_DIR}/armv7a-linux-androideabi21-clang"
|
|
|
|
export CC_x86_64_linux_android="${ANDROID_TOOLCHAIN_DIR}/x86_64-linux-android21-clang"
|
|
|
|
export CC_i686_linux_android="${ANDROID_TOOLCHAIN_DIR}/i686-linux-android21-clang"
|
|
|
|
|
|
|
|
export CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER="${CC_aarch64_linux_android}"
|
|
|
|
export CARGO_TARGET_ARMV7_LINUX_ANDROIDEABI_LINKER="${CC_armv7_linux_androideabi}"
|
|
|
|
export CARGO_TARGET_X86_64_LINUX_ANDROID_LINKER="${CC_x86_64_linux_android}"
|
|
|
|
export CARGO_TARGET_I686_LINUX_ANDROID_LINKER="${CC_i686_linux_android}"
|
|
|
|
|
|
|
|
export TARGET_AR="${ANDROID_TOOLCHAIN_DIR}/llvm-ar"
|
|
|
|
|
2023-10-31 20:23:56 +01:00
|
|
|
# The 64-bit curve25519-dalek backend is faster than the 32-bit one on at least some armv7a phones.
|
|
|
|
# Comment out the following to allow the 32-bit backend on 32-bit targets.
|
|
|
|
export RUSTFLAGS="--cfg curve25519_dalek_bits=\"64\" ${RUSTFLAGS:-}"
|
2023-10-04 22:49:19 +02:00
|
|
|
|
|
|
|
target_for_abi() {
|
|
|
|
case "$1" in
|
|
|
|
arm64-v8a)
|
|
|
|
echo aarch64-linux-android
|
|
|
|
;;
|
|
|
|
armeabi-v7a)
|
|
|
|
echo armv7-linux-androideabi
|
|
|
|
;;
|
|
|
|
x86_64)
|
|
|
|
echo x86_64-linux-android
|
|
|
|
;;
|
|
|
|
x86)
|
|
|
|
echo i686-linux-android
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Unknown Android ABI $1; please update build_jni.sh" >&2
|
|
|
|
exit 2
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
for abi in "${android_abis[@]}"; do
|
|
|
|
rust_target=$(target_for_abi "$abi")
|
2024-07-09 19:07:36 +02:00
|
|
|
echo_then_run cargo build -p libsignal-jni -p libsignal-jni-testing --release ${FEATURES:+--features "${FEATURES[*]}"} -Z unstable-options --target "$rust_target" --out-dir "${ANDROID_LIB_DIR}/$abi"
|
2023-10-04 22:49:19 +02:00
|
|
|
done
|