• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3# In order to cross-compile node for Android using NDK, run:
4#   source android-configure <path_to_ndk> [arch]
5#
6# By running android-configure with source, will allow environment variables to
7# be persistent in current session. This is useful for installing native node
8# modules with npm. Also, don't forget to set the arch in npm config using
9# 'npm config set arch=<arch>'
10
11if [ $# -ne 3 ]; then
12  echo "$0 should have 3 parameters: ndk_path, target_arch and sdk_version"
13  return 1
14fi
15
16NDK_PATH=$1
17ARCH="$2"
18ANDROID_SDK_VERSION=$3
19
20if [ $ANDROID_SDK_VERSION -lt 23 ]; then
21  echo "$ANDROID_SDK_VERSION should equal or later than 23(Android 6.0)"
22fi
23
24CC_VER="4.9"
25
26case $ARCH in
27    arm)
28        DEST_CPU="arm"
29        TOOLCHAIN_NAME="armv7-linux-androideabi"
30        ;;
31    x86)
32        DEST_CPU="ia32"
33        TOOLCHAIN_NAME="i686-linux-android"
34        ;;
35    x86_64)
36        DEST_CPU="x64"
37        TOOLCHAIN_NAME="x86_64-linux-android"
38        ARCH="x64"
39        ;;
40    arm64|aarch64)
41        DEST_CPU="arm64"
42        TOOLCHAIN_NAME="aarch64-linux-android"
43        ARCH="arm64"
44        ;;
45    *)
46        echo "Unsupported architecture provided: $ARCH"
47        return 1
48        ;;
49esac
50
51HOST_OS="linux"
52HOST_ARCH="x86_64"
53export CC_host=$(command -v gcc)
54export CXX_host=$(command -v g++)
55
56host_gcc_version=$($CC_host --version | grep gcc | awk '{print $NF}')
57major=$(echo $host_gcc_version | awk -F . '{print $1}')
58minor=$(echo $host_gcc_version | awk -F . '{print $2}')
59if [ -z $major ] || [ -z $minor ] || [ $major -lt 6 ] || ( [ $major -eq 6 ] && [ $minor -lt 3 ] ); then
60  echo "host gcc $host_gcc_version is too old, need gcc 6.3.0"
61  return 1
62fi
63
64SUFFIX="$TOOLCHAIN_NAME$ANDROID_SDK_VERSION"
65TOOLCHAIN=$NDK_PATH/toolchains/llvm/prebuilt/$HOST_OS-$HOST_ARCH
66
67export PATH=$TOOLCHAIN/bin:$PATH
68export CC=$TOOLCHAIN/bin/$SUFFIX-clang
69export CXX=$TOOLCHAIN/bin/$SUFFIX-clang++
70
71
72GYP_DEFINES="target_arch=$ARCH"
73GYP_DEFINES+=" v8_target_arch=$ARCH"
74GYP_DEFINES+=" android_target_arch=$ARCH"
75GYP_DEFINES+=" host_os=$HOST_OS OS=android"
76export GYP_DEFINES
77
78if [ -f "configure" ]; then
79    ./configure \
80        --dest-cpu=$DEST_CPU \
81        --dest-os=android \
82        --without-snapshot \
83        --openssl-no-asm \
84        --cross-compiling
85fi
86