Various fixes/improvements to Android building scripts

Use [cargo-ndk](https://github.com/bbqsrc/cargo-ndk) to configure cargo for building with the Android NDK. The output of `cargo ndk` is written to a subfolder for the target triplet, which cannot be changed by parameter. Therefore, the `CARGO_BUILD_DIR` is changed to this subfolder for the Android build, so the build process can find the output.

Use `$HOME` instead of `~` because cargo-ndk cannot correctly access the folder otherwise (will fail with error message indicating it could not determine the NDK version).

Add `DDNET_TEST_NO_LINK=ON` CMake argument, to fix Rust linking errors with tests.

Add additional CMake arguments to specify system name, API version and NDK location. Upgrade to Android API 34.

Use `nproc` instead of `32` threads for building.
This commit is contained in:
Robert Müller 2024-05-07 16:34:49 +02:00
parent b2c2a74db7
commit cdbe1f775a
4 changed files with 47 additions and 24 deletions

View file

@ -866,9 +866,15 @@ set_glob(RUST_MASTERSRV GLOB "rs;toml" src/mastersrv/src
add_library(rust-bridge-shared EXCLUDE_FROM_ALL OBJECT ${RUST_BRIDGE_SHARED}) add_library(rust-bridge-shared EXCLUDE_FROM_ALL OBJECT ${RUST_BRIDGE_SHARED})
list(APPEND TARGETS_OWN rust-bridge-shared) list(APPEND TARGETS_OWN rust-bridge-shared)
if(TARGET_OS STREQUAL "android")
set(CARGO_BUILD_DIR "${CARGO_NDK_TARGET}/")
set(CARGO_BUILD ${CMAKE_COMMAND} -E env CARGO_TARGET_DIR=${PROJECT_BINARY_DIR} DDNET_TEST_NO_LINK=1 ${RUST_CARGO} ndk -t ${CARGO_NDK_TARGET} -p ${CARGO_NDK_API} build)
set(CARGO_TEST ${CMAKE_COMMAND} -E env CARGO_TARGET_DIR=${PROJECT_BINARY_DIR} ${RUST_CARGO} ndk -t ${CARGO_NDK_TARGET} -p ${CARGO_NDK_API} test)
else()
set(CARGO_BUILD_DIR "") set(CARGO_BUILD_DIR "")
set(CARGO_BUILD ${CMAKE_COMMAND} -E env CARGO_TARGET_DIR=${PROJECT_BINARY_DIR} DDNET_TEST_NO_LINK=1 ${RUST_CARGO} build) set(CARGO_BUILD ${CMAKE_COMMAND} -E env CARGO_TARGET_DIR=${PROJECT_BINARY_DIR} DDNET_TEST_NO_LINK=1 ${RUST_CARGO} build)
set(CARGO_TEST ${CMAKE_COMMAND} -E env CARGO_TARGET_DIR=${PROJECT_BINARY_DIR} ${RUST_CARGO} test) set(CARGO_TEST ${CMAKE_COMMAND} -E env CARGO_TARGET_DIR=${PROJECT_BINARY_DIR} ${RUST_CARGO} test)
endif()
if(MSVC) if(MSVC)
list(INSERT CARGO_BUILD 0 ${CMAKE_COMMAND} -E env $<$<CONFIG:Debug>:CFLAGS=/MTd> $<$<CONFIG:Debug>:CXXFLAGS=/MTd>) list(INSERT CARGO_BUILD 0 ${CMAKE_COMMAND} -E env $<$<CONFIG:Debug>:CFLAGS=/MTd> $<$<CONFIG:Debug>:CXXFLAGS=/MTd>)
list(INSERT CARGO_TEST 0 ${CMAKE_COMMAND} -E env RUSTFLAGS=-Ctarget-feature=+crt-static) list(INSERT CARGO_TEST 0 ${CMAKE_COMMAND} -E env RUSTFLAGS=-Ctarget-feature=+crt-static)

View file

@ -1,18 +1,22 @@
#!/bin/bash #!/bin/bash
export ANDROID_HOME=~/Android/Sdk # $HOME must be used instead of ~ else cargo-ndk cannot find the folder
export MAKEFLAGS=-j32 export ANDROID_HOME=$HOME/Android/Sdk
MAKEFLAGS=-j$(nproc)
export MAKEFLAGS
ANDROID_NDK_VERSION="$(cd "$ANDROID_HOME/ndk" && find . -maxdepth 1 | sort -n | tail -1)" ANDROID_NDK_VERSION="$(cd "$ANDROID_HOME/ndk" && find . -maxdepth 1 | sort -n | tail -1)"
ANDROID_NDK_VERSION="${ANDROID_NDK_VERSION:2}" ANDROID_NDK_VERSION="${ANDROID_NDK_VERSION:2}"
# ANDROID_NDK_VERSION must be exported for build.sh step
export ANDROID_NDK_VERSION export ANDROID_NDK_VERSION
ANDROID_NDK="$ANDROID_HOME/ndk/$ANDROID_NDK_VERSION" # ANDROID_NDK_HOME must be exported for cargo-ndk
export ANDROID_NDK_HOME="$ANDROID_HOME/ndk/$ANDROID_NDK_VERSION"
_DEFAULT_ANDROID_BUILD=x86 _DEFAULT_ANDROID_BUILD=x86
_DEFAULT_GAME_NAME=DDNet _DEFAULT_GAME_NAME=DDNet
_DEFAULT_BUILD_TYPE=Debug _DEFAULT_BUILD_TYPE=Debug
_DEFAULT_BUILD_FOLDER=build-android _DEFAULT_BUILD_FOLDER=build-android
_ANDROID_API_LEVEL=android-24 _ANDROID_API_LEVEL=34
_ANDROID_SUB_BUILD_DIR=build_arch _ANDROID_SUB_BUILD_DIR=build_arch
@ -102,12 +106,19 @@ function build_for_type() {
-G "Ninja" \ -G "Ninja" \
-DPREFER_BUNDLED_LIBS=ON \ -DPREFER_BUNDLED_LIBS=ON \
-DCMAKE_BUILD_TYPE="${_DEFAULT_BUILD_TYPE}" \ -DCMAKE_BUILD_TYPE="${_DEFAULT_BUILD_TYPE}" \
-DANDROID_NATIVE_API_LEVEL="$_ANDROID_API_LEVEL" \ -DANDROID_PLATFORM="android-${_ANDROID_API_LEVEL}" \
-DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake" \ -DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake" \
-DANDROID_NDK="$ANDROID_NDK" \ -DANDROID_NDK="$ANDROID_NDK_HOME" \
-DANDROID_ABI="${2}" \ -DANDROID_ABI="${2}" \
-DANDROID_ARM_NEON=TRUE \ -DANDROID_ARM_NEON=TRUE \
-B${_DEFAULT_BUILD_FOLDER}/"$_ANDROID_SUB_BUILD_DIR/$1" \ -DCMAKE_ANDROID_NDK="$ANDROID_NDK_HOME" \
-DCMAKE_SYSTEM_NAME=Android \
-DCMAKE_SYSTEM_VERSION="$_ANDROID_API_LEVEL" \
-DCMAKE_ANDROID_ARCH_ABI="${2}" \
-DCARGO_NDK_TARGET="${3}" \
-DCARGO_NDK_API="$_ANDROID_API_LEVEL" \
-DDDNET_TEST_NO_LINK=ON \
-B"${_DEFAULT_BUILD_FOLDER}/$_ANDROID_SUB_BUILD_DIR/$1" \
-DSERVER=OFF \ -DSERVER=OFF \
-DTOOLS=OFF \ -DTOOLS=OFF \
-DDEV=TRUE \ -DDEV=TRUE \
@ -120,29 +131,29 @@ function build_for_type() {
) )
} }
mkdir ${_DEFAULT_BUILD_FOLDER} mkdir "${_DEFAULT_BUILD_FOLDER}"
if [[ "${_DEFAULT_ANDROID_BUILD}" == "arm" || "${_DEFAULT_ANDROID_BUILD}" == "all" ]]; then if [[ "${_DEFAULT_ANDROID_BUILD}" == "arm" || "${_DEFAULT_ANDROID_BUILD}" == "all" ]]; then
build_for_type arm armeabi-v7a & build_for_type arm armeabi-v7a armv7-linux-androideabi &
fi fi
if [[ "${_DEFAULT_ANDROID_BUILD}" == "arm64" || "${_DEFAULT_ANDROID_BUILD}" == "all" ]]; then if [[ "${_DEFAULT_ANDROID_BUILD}" == "arm64" || "${_DEFAULT_ANDROID_BUILD}" == "all" ]]; then
build_for_type arm64 arm64-v8a & build_for_type arm64 arm64-v8a aarch64-linux-android &
fi fi
if [[ "${_DEFAULT_ANDROID_BUILD}" == "x86" || "${_DEFAULT_ANDROID_BUILD}" == "all" ]]; then if [[ "${_DEFAULT_ANDROID_BUILD}" == "x86" || "${_DEFAULT_ANDROID_BUILD}" == "all" ]]; then
build_for_type x86 x86 & build_for_type x86 x86 i686-linux-android &
fi fi
if [[ "${_DEFAULT_ANDROID_BUILD}" == "x86_64" || "${_DEFAULT_ANDROID_BUILD}" == "x64" || "${_DEFAULT_ANDROID_BUILD}" == "all" ]]; then if [[ "${_DEFAULT_ANDROID_BUILD}" == "x86_64" || "${_DEFAULT_ANDROID_BUILD}" == "x64" || "${_DEFAULT_ANDROID_BUILD}" == "all" ]]; then
build_for_type x86_64 x86_64 & build_for_type x86_64 x86_64 x86_64-linux-android &
fi fi
wait wait
printf "\e[36mPreparing gradle build\n" printf "\e[36mPreparing gradle build\n"
cd ${_DEFAULT_BUILD_FOLDER} || exit 1 cd "${_DEFAULT_BUILD_FOLDER}" || exit 1
mkdir -p src/main mkdir -p src/main
mkdir -p src/main/res/mipmap mkdir -p src/main/res/mipmap

View file

@ -1,10 +1,11 @@
#!/bin/bash #!/bin/bash
ANDROID_HOME=~/Android/Sdk ANDROID_HOME=~/Android/Sdk
ANDROID_NDK="$(find "$ANDROID_HOME/ndk" -maxdepth 1 | sort -n | tail -1)" ANDROID_NDK_HOME="$(find "$ANDROID_HOME/ndk" -maxdepth 1 | sort -n | tail -1)"
echo "$ANDROID_NDK" export ANDROID_NDK_HOME
export MAKEFLAGS=-j32 MAKEFLAGS=-j$(nproc)
export MAKEFLAGS
if [[ "${2}" == "webasm" ]]; then if [[ "${2}" == "webasm" ]]; then
COMPILEFLAGS="-pthread -O3 -g -s USE_PTHREADS=1" COMPILEFLAGS="-pthread -O3 -g -s USE_PTHREADS=1"
@ -20,10 +21,15 @@ function compile_source() {
-H. \ -H. \
-G "Unix Makefiles" \ -G "Unix Makefiles" \
-DCMAKE_BUILD_TYPE=Release \ -DCMAKE_BUILD_TYPE=Release \
-DANDROID_NATIVE_API_LEVEL="android-$1" \ -DANDROID_PLATFORM="android-$1" \
-DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake" \ -DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake" \
-DANDROID_NDK="$ANDROID_NDK_HOME" \
-DANDROID_ABI="${3}" \ -DANDROID_ABI="${3}" \
-DANDROID_ARM_NEON=TRUE \ -DANDROID_ARM_NEON=TRUE \
-DCMAKE_ANDROID_NDK="$ANDROID_NDK_HOME" \
-DCMAKE_SYSTEM_NAME=Android \
-DCMAKE_SYSTEM_VERSION="$1" \
-DCMAKE_ANDROID_ARCH_ABI="${3}" \
-B"$2" \ -B"$2" \
-DBUILD_SHARED_LIBS=OFF \ -DBUILD_SHARED_LIBS=OFF \
-DHIDAPI_SKIP_LIBUSB=TRUE \ -DHIDAPI_SKIP_LIBUSB=TRUE \

View file

@ -53,6 +53,8 @@ fi
mkdir -p "$1" mkdir -p "$1"
cd "$1" || exit 1 cd "$1" || exit 1
_ANDROID_ABI_LEVEL=34
function build_cmake_lib() { function build_cmake_lib() {
if [ ! -d "${1}" ]; then if [ ! -d "${1}" ]; then
if [ -z ${3+x} ]; then if [ -z ${3+x} ]; then
@ -68,8 +70,6 @@ function build_cmake_lib() {
) )
} }
_ANDROID_ABI_LEVEL=24
mkdir -p compile_libs mkdir -p compile_libs
cd compile_libs || exit 1 cd compile_libs || exit 1