Skip to content

Commit

Permalink
build(android): added RUSTFLAGS workaround for runtime linker issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Sep 26, 2023
1 parent 5cb63a5 commit c3afce2
Showing 1 changed file with 34 additions and 27 deletions.
61 changes: 34 additions & 27 deletions compile-android.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
set -e
platform="$(uname -s | tr '[:upper:]' '[:lower:]')"

ORIG_PATH="$PATH"
ORIG_RUSTFLAGS="$RUSTFLAGS"

if [ -z "$ANDROID_NDK_HOME" ]; then
if [ -d `pwd`/"NDK" ]; then
echo "Found NDK folder in root, using."
Expand All @@ -22,8 +25,6 @@ else
RELEASE=false;
fi

ORIG_PATH="$PATH"

# Workaround for "error: unable to find library -lgcc"
# See: https://stackoverflow.com/questions/68873570/how-do-i-fix-ld-error-unable-to-find-library-lgcc-when-cross-compiling-rust
find "${ANDROID_NDK_HOME}" -name "libunwind.a" -execdir bash -c 'echo "INPUT(-lunwind)" > libgcc.a' \;
Expand All @@ -43,34 +44,40 @@ for archtargetstr in \
echo TARGET $target

NDK_ARCH_DIR="$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/$platform-x86_64/bin"
echo "Building for $arch..."
if [ ! -d "$NDK_ARCH_DIR" ]; then
echo "Couldn't find directory $NDK_ARCH_DIR"
exit 1
fi

if [ -d "$NDK_ARCH_DIR" ]; then
export PATH="$NDK_ARCH_DIR:$ORIG_PATH"
# Need to set AR for target since NDK 21+:
# https://github.com/rust-lang/cc-rs/issues/636#issuecomment-1075352495
declare -x "AR_${target_underscore}"="$NDK_ARCH_DIR/llvm-ar"
declare -x "CC_${target_underscore}"="$NDK_ARCH_DIR/${target}-clang"
declare -x "RANLIB_${target_underscore}"="$NDK_ARCH_DIR/llvm-ranlib"
echo "Building for $arch..."

# fix armv7 -> arm
if [ "$arch" = "arm" ]; then
declare -x "CC_${target_underscore}"="$NDK_ARCH_DIR/arm-linux-androideabi-clang"
fi
export PATH="$NDK_ARCH_DIR:$ORIG_PATH"
export RUSTFLAGS="$ORIG_RUSTFLAGS"
# Need to set AR for target since NDK 21+:
# https://github.com/rust-lang/cc-rs/issues/636#issuecomment-1075352495
declare -x "AR_${target_underscore}"="$NDK_ARCH_DIR/llvm-ar"
declare -x "CC_${target_underscore}"="$NDK_ARCH_DIR/${target}-clang"
declare -x "RANLIB_${target_underscore}"="$NDK_ARCH_DIR/llvm-ranlib"

# check that they exist
for var in AR_${target_underscore} CC_${target_underscore} RANLIB_${target_underscore}; do
if [ ! -f "${!var}" ]; then
echo "Couldn't find ${!var} set for variable $var"
exit 1
fi
done
# Needed for runtime error: https://github.com/termux/termux-packages/issues/8029
# java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "__extenddftf2"
export RUSTFLAGS+=" -C link-arg=$($NDK_ARCH_DIR/${target}-clang -print-libgcc-file-name)"
echo RUSTFLAGS=$RUSTFLAGS

# People suggest to use this, but ime it needs all the same workarounds anyway :shrug:
#cargo ndk build -p aw-server --target $target --lib $($RELEASE && echo '--release')
cargo build -p aw-server --target $target --lib $($RELEASE && echo '--release')
else
echo "Couldn't find directory $NDK_ARCH_DIR"
exit 1
# fix armv7 -> arm
if [ "$arch" = "arm" ]; then
declare -x "CC_${target_underscore}"="$NDK_ARCH_DIR/arm-linux-androideabi-clang"
fi

# check that they exist
for var in AR_${target_underscore} CC_${target_underscore} RANLIB_${target_underscore}; do
if [ ! -f "${!var}" ]; then
echo "Couldn't find ${!var} set for variable $var"
exit 1
fi
done

# People suggest to use this, but ime it needs all the same workarounds anyway :shrug:
#cargo ndk build -p aw-server --target $target --lib $($RELEASE && echo '--release')
cargo build -p aw-server --target $target --lib $($RELEASE && echo '--release')
done

0 comments on commit c3afce2

Please sign in to comment.