From cdbe1f775a7b2a19ff33fb5c0882ae29a684f604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Tue, 7 May 2024 16:34:49 +0200 Subject: [PATCH] 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. --- CMakeLists.txt | 12 +++++-- scripts/android/cmake_android.sh | 39 +++++++++++++++-------- scripts/compile_libs/cmake_lib_compile.sh | 16 +++++++--- scripts/compile_libs/gen_libs.sh | 4 +-- 4 files changed, 47 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ad42e0ee7..73756f23d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) list(APPEND TARGETS_OWN rust-bridge-shared) -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_TEST ${CMAKE_COMMAND} -E env CARGO_TARGET_DIR=${PROJECT_BINARY_DIR} ${RUST_CARGO} test) +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 ${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) +endif() if(MSVC) list(INSERT CARGO_BUILD 0 ${CMAKE_COMMAND} -E env $<$:CFLAGS=/MTd> $<$:CXXFLAGS=/MTd>) list(INSERT CARGO_TEST 0 ${CMAKE_COMMAND} -E env RUSTFLAGS=-Ctarget-feature=+crt-static) diff --git a/scripts/android/cmake_android.sh b/scripts/android/cmake_android.sh index 8035f82bc..cadfd88a6 100755 --- a/scripts/android/cmake_android.sh +++ b/scripts/android/cmake_android.sh @@ -1,18 +1,22 @@ #!/bin/bash -export ANDROID_HOME=~/Android/Sdk -export MAKEFLAGS=-j32 +# $HOME must be used instead of ~ else cargo-ndk cannot find the folder +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="${ANDROID_NDK_VERSION:2}" +# ANDROID_NDK_VERSION must be exported for build.sh step 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_GAME_NAME=DDNet _DEFAULT_BUILD_TYPE=Debug _DEFAULT_BUILD_FOLDER=build-android -_ANDROID_API_LEVEL=android-24 +_ANDROID_API_LEVEL=34 _ANDROID_SUB_BUILD_DIR=build_arch @@ -102,12 +106,19 @@ function build_for_type() { -G "Ninja" \ -DPREFER_BUNDLED_LIBS=ON \ -DCMAKE_BUILD_TYPE="${_DEFAULT_BUILD_TYPE}" \ - -DANDROID_NATIVE_API_LEVEL="$_ANDROID_API_LEVEL" \ - -DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake" \ - -DANDROID_NDK="$ANDROID_NDK" \ + -DANDROID_PLATFORM="android-${_ANDROID_API_LEVEL}" \ + -DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake" \ + -DANDROID_NDK="$ANDROID_NDK_HOME" \ -DANDROID_ABI="${2}" \ -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 \ -DTOOLS=OFF \ -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 - build_for_type arm armeabi-v7a & + build_for_type arm armeabi-v7a armv7-linux-androideabi & fi 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 if [[ "${_DEFAULT_ANDROID_BUILD}" == "x86" || "${_DEFAULT_ANDROID_BUILD}" == "all" ]]; then - build_for_type x86 x86 & + build_for_type x86 x86 i686-linux-android & fi 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 wait 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/res/mipmap diff --git a/scripts/compile_libs/cmake_lib_compile.sh b/scripts/compile_libs/cmake_lib_compile.sh index ba11a4a19..4109e115b 100755 --- a/scripts/compile_libs/cmake_lib_compile.sh +++ b/scripts/compile_libs/cmake_lib_compile.sh @@ -1,10 +1,11 @@ #!/bin/bash ANDROID_HOME=~/Android/Sdk -ANDROID_NDK="$(find "$ANDROID_HOME/ndk" -maxdepth 1 | sort -n | tail -1)" -echo "$ANDROID_NDK" +ANDROID_NDK_HOME="$(find "$ANDROID_HOME/ndk" -maxdepth 1 | sort -n | tail -1)" +export ANDROID_NDK_HOME -export MAKEFLAGS=-j32 +MAKEFLAGS=-j$(nproc) +export MAKEFLAGS if [[ "${2}" == "webasm" ]]; then COMPILEFLAGS="-pthread -O3 -g -s USE_PTHREADS=1" @@ -20,10 +21,15 @@ function compile_source() { -H. \ -G "Unix Makefiles" \ -DCMAKE_BUILD_TYPE=Release \ - -DANDROID_NATIVE_API_LEVEL="android-$1" \ - -DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake" \ + -DANDROID_PLATFORM="android-$1" \ + -DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake" \ + -DANDROID_NDK="$ANDROID_NDK_HOME" \ -DANDROID_ABI="${3}" \ -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" \ -DBUILD_SHARED_LIBS=OFF \ -DHIDAPI_SKIP_LIBUSB=TRUE \ diff --git a/scripts/compile_libs/gen_libs.sh b/scripts/compile_libs/gen_libs.sh index 7d144585a..00e0521c8 100755 --- a/scripts/compile_libs/gen_libs.sh +++ b/scripts/compile_libs/gen_libs.sh @@ -53,6 +53,8 @@ fi mkdir -p "$1" cd "$1" || exit 1 +_ANDROID_ABI_LEVEL=34 + function build_cmake_lib() { if [ ! -d "${1}" ]; then if [ -z ${3+x} ]; then @@ -68,8 +70,6 @@ function build_cmake_lib() { ) } -_ANDROID_ABI_LEVEL=24 - mkdir -p compile_libs cd compile_libs || exit 1