nunchuk-io / nunchuk-android-nativesdk

4 stars 1 forks source link

Not found errors when compiling with ndk25 due to incorrect path/name for ar & ranlib tools #9

Open emanuelb opened 1 year ago

emanuelb commented 1 year ago

Compiling with ndk25 fail with:

/bin/sh: 2: /home/appuser/app/sdk/ndk/25.1.8937393//toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android-ar: not found
/bin/sh: 3: /home/appuser/app/sdk/ndk/25.1.8937393//toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android-ranlib: not found

due to (multiple occurrences in script): https://github.com/nunchuk-io/nunchuk-android-nativesdk/blob/36af375f6f60728d871f5c0df63391395b1289cf/src/main/native/.install_linux_deps.sh#L78 https://github.com/nunchuk-io/nunchuk-android-nativesdk/blob/36af375f6f60728d871f5c0df63391395b1289cf/src/main/native/.install_linux_deps.sh#L73

as from ndk25 https://dl.google.com/android/repository/android-ndk-r25b-linux.zip the files aarch64-linux-android-ar and aarch64-linux-android-ranlib not exists and instead there are files: llvm-ranlib and llvm-ar

fix:

  1. rename script to use llvm-ranlib and llvm-ar instead in all RANLIB and AR variables
  2. also rename to llvm-as in AS variable
  3. also rename to llvm-strip in STRIP variable
  4. remove LD variable (no file ending with -ld exists in ndk25)
  5. apply minimum ndk check in script (so error when compiling with older ndk will be obvious), or use the old variables currently used only if ndk is old version, for newer ndk use the lllvm- strings instead.

applying above fixes manually with:

sed -i '/$TARGET-ld$/d' .install_linux_deps.sh; \
sed -i 's/$TARGET-strip/llvm-strip/g' .install_linux_deps.sh; \
sed -i 's/$TARGET-ranlib/llvm-ranlib/g' .install_linux_deps.sh; \
sed -i 's/$TARGET-as/llvm-as/g' .install_linux_deps.sh; \
sed -i 's/$TARGET-ar/llvm-ar/g' .install_linux_deps.sh;

Resulted in failure from openssl with message:

$ANDROID_NDK_HOME=/home/appuser/app/sdk/ndk/25.1.8937393/ is invalid at (eval 10) line 37

because the openssl version specified is old and not support ndk22+, see issue I opened at: https://github.com/nunchuk-io/libnunchuk/issues/21

JayceLuong commented 1 year ago
#!/bin/bash
set -e

if [ -z "$ANDROID_SDK" ]; then
  echo "export the ANDROID_SDK environment variable"
  exit 1
fi

if [ -z "$ANDROID_NDK_HOME" ]; then
  echo "export the ANDROID_NDK_HOME environment variable"
  exit 1
fi

if [ "$(uname)" = "Darwin" ]; then
    export TOOLCHAIN=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/darwin-x86_64
else
    export TOOLCHAIN=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64
fi

export API=21

pwd=$(pwd)
echo "pwd::$pwd"

export ANDROID_ABI_ARMEABI_V7A="armeabi-v7a"
export ANDROID_ABI_ARM64_V8A="arm64-v8a"
export ANDROID_ABI_X86_64="x86_64"
export ANDROID_ABI_X86="x86"

export ANDROID_TARGET_ARMEABI_V7A="armv7a-linux-androideabi"
export ANDROID_TARGET_ARM64_V8A="aarch64-linux-android"
export ANDROID_TARGET_X86_64="x86_64-linux-android"
export ANDROID_TARGET_X86="i686-linux-android"

num_jobs=4
if [ -f /proc/cpuinfo ]; then
   num_jobs=$(grep ^processor /proc/cpuinfo | wc -l)
fi

export ANDROID_ABI="${1:-arm64-v8a}"
ANDROID_TARGET=""

parseArgs() {
  if [ "$ANDROID_ABI" = $ANDROID_ABI_ARMEABI_V7A ]; then
    ANDROID_TARGET=$ANDROID_TARGET_ARMEABI_V7A
  elif [ "$ANDROID_ABI" = $ANDROID_ABI_ARM64_V8A ]; then
    ANDROID_TARGET=$ANDROID_TARGET_ARM64_V8A
  elif [ "$ANDROID_ABI" = $ANDROID_ABI_X86_64 ]; then
    ANDROID_TARGET=$ANDROID_TARGET_X86_64
  elif [ "$ANDROID_ABI" = $ANDROID_ABI_X86 ]; then
    ANDROID_TARGET=$ANDROID_TARGET_X86
  else
    echo "Invalid ABI argument $ANDROID_ABI"
    exit 1
  fi
}
parseArgs
#########################################################################################
####                                Bitcoin Deps                                     ####
#########################################################################################
installBitcoinDeps() {
  abi=$ANDROID_ABI
  target=$ANDROID_TARGET

  echo "-------------------------------------------------------------------------------"
  echo "                     Installing deps for $abi $target                          "
  echo "-------------------------------------------------------------------------------"

  export TARGET=$target

  export AR=$TOOLCHAIN/bin/llvm-ar
  export CC=$TOOLCHAIN/bin/$TARGET$API-clang
  export AS=$CC
  export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++
  export LD=$TOOLCHAIN/bin/ld
  export RANLIB=$TOOLCHAIN/bin/llvm-ranlib
  export STRIP=$TOOLCHAIN/bin/llvm-strip

  ANDROID_SDK=$ANDROID_SDK ANDROID_NDK=$ANDROID_NDK_HOME make HOST=$TARGET ANDROID_TOOLCHAIN_BIN=$TOOLCHAIN ANDROID_API_LEVEL=$API NO_QT=1 NO_ZMQ=1 NO_QR=1 NO_UPNP=1 -j $num_jobs
}
#
pushd libnunchuk/contrib/bitcoin/depends || exit
installBitcoinDeps
popd || exit

#########################################################################################
####                                 Bitcoin Core                                    ####
#########################################################################################
installBitcoinCore() {
  abi=$ANDROID_ABI
  target=$ANDROID_TARGET
  echo "-------------------------------------------------------------------------------"
  echo "                        Installing core for $abi $target                       "
  echo "-------------------------------------------------------------------------------"

  export TARGET=$target

  export AR=$TOOLCHAIN/bin/llvm-ar
  export CC=$TOOLCHAIN/bin/$TARGET$API-clang
  export AS=$CC
  export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++
  export LD=$TOOLCHAIN/bin/ld
  export RANLIB=$TOOLCHAIN/bin/llvm-ranlib
  export STRIP=$TOOLCHAIN/bin/llvm-strip
  sh ./autogen.sh
  CONFIG_SITE="$PWD/depends/$ANDROID_TARGET/share/config.site" CFLAGS="-fPIC" CXXFLAGS="-fPIC" ./configure --prefix=$PWD/depends/$TARGET --without-gui --disable-zmq --with-miniupnpc=no --with-incompatible-bdb --disable-bench --disable-tests --enable-module-ecdh
  make -j $num_jobs
}

pushd libnunchuk/contrib/bitcoin || exit
installBitcoinCore
popd || exit

#########################################################################################
####                                 OpenSSL Lib                                     ####
#########################################################################################

installOpenSSL() {
  if [ "$ANDROID_ABI" = $ANDROID_ABI_ARM64_V8A ]; then
    abi="arm64"
  elif [ "$ANDROID_ABI" = $ANDROID_ABI_ARMEABI_V7A ]; then
    abi="arm"
  else
    abi="$ANDROID_ABI"
  fi

  target=$ANDROID_TARGET
  echo "-------------------------------------------------------------------------------"
  echo "                    Installing OpenSSL for $abi $target                        "
  echo "-------------------------------------------------------------------------------"

  export TARGET=$target

  export AR=$TOOLCHAIN/bin/llvm-ar
  export CC=$TOOLCHAIN/bin/$TARGET$API-clang
  export AS=$CC
  export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++
  export LD=$TOOLCHAIN/bin/ld
  export RANLIB=$TOOLCHAIN/bin/llvm-ranlib
  export STRIP=$TOOLCHAIN/bin/llvm-strip
  PATH=$TOOLCHAIN/bin:$PATH
  ./Configure android-$abi -D__ANDROID_API__=$API
  make -j $num_jobs
}

pushd libnunchuk/contrib/openssl || exit
installOpenSSL
popd || exit

echo "done"
JayceLuong commented 1 year ago

Hi @emanuelb I update the build script for ndk25, it works with arm64-v8a but it does not work with other abi? Have you build other abi successfully?