From 50317c38a4511914e5eb69d6f05f290087887cf7 Mon Sep 17 00:00:00 2001 From: forfun414 Date: Sun, 26 Jan 2020 20:29:10 +0800 Subject: [PATCH] build: support android build on ndk version equal or above 23 change scripts and sources for android build, don't need standalone toolchain after ndk 19, and use clang as default android target compiler. PR-URL: https://github.com/nodejs/node/pull/31521 Reviewed-By: Christian Clauss --- android-configure | 78 +++++++++++++------------ common.gypi | 4 ++ deps/cares/config/android/ares_config.h | 2 +- deps/openssl/openssl-cl_no_asm.gypi | 4 +- deps/openssl/openssl_no_asm.gypi | 4 +- deps/uv/uv.gyp | 1 + tools/gyp/pylib/gyp/generator/make.py | 2 +- tools/v8_gypfiles/v8.gyp | 44 ++++++-------- 8 files changed, 70 insertions(+), 69 deletions(-) diff --git a/android-configure b/android-configure index 59f0a40c1ac214..a7cb2b9c8b4a78 100755 --- a/android-configure +++ b/android-configure @@ -8,29 +8,39 @@ # modules with npm. Also, don't forget to set the arch in npm config using # 'npm config set arch=' +if [ $# -ne 3 ]; then + echo "$0 should have 3 parameters: ndk_path, target_arch and sdk_version" + exit 1 +fi + +NDK_PATH=$1 +ARCH="$2" +ANDROID_SDK_VERSION=$3 -if [ -z "$2" ]; then - ARCH=arm -else - ARCH="$2" +if [ $ANDROID_SDK_VERSION -lt 23 ]; then + echo "$ANDROID_SDK_VERSION should equal or later than 23(Android 6.0)" fi CC_VER="4.9" + case $ARCH in arm) - DEST_CPU="$ARCH" - SUFFIX="$ARCH-linux-androideabi" - TOOLCHAIN_NAME="$SUFFIX" + DEST_CPU="arm" + TOOLCHAIN_NAME="armv7-linux-androideabi" ;; x86) DEST_CPU="ia32" - SUFFIX="i686-linux-android" - TOOLCHAIN_NAME="$ARCH" + TOOLCHAIN_NAME="i686-linux-android" ;; x86_64) - DEST_CPU="ia32" - SUFFIX="$ARCH-linux-android" - TOOLCHAIN_NAME="$ARCH" + DEST_CPU="x64" + TOOLCHAIN_NAME="x86_64-linux-android" + ARCH="x64" + ;; + arm64|aarch64) + DEST_CPU="arm64" + TOOLCHAIN_NAME="aarch64-linux-android" + ARCH="arm64" ;; *) echo "Unsupported architecture provided: $ARCH" @@ -38,36 +48,31 @@ case $ARCH in ;; esac -NDK_PATH=$1 -function make_toolchain { - $NDK_PATH/build/tools/make-standalone-toolchain.sh \ - --toolchain=$TOOLCHAIN_NAME-$CC_VER \ - --arch=$ARCH \ - --install-dir=$TOOLCHAIN \ - --platform=android-21 -} +HOST_OS="linux" +HOST_ARCH="x86_64" +export CC_host=$(which gcc) +export CXX_host=$(which g++) -export TOOLCHAIN=$PWD/android-toolchain -if [ -d "$TOOLCHAIN" ]; then - read -r -p "NDK toolchain already exists. Replace it? [y/N]" response - case "$response" in - [Yy]) - rm -rf "$TOOLCHAIN" - make_toolchain - esac -else - make_toolchain +host_gcc_version=$($CC_host --version | grep gcc | awk '{print $NF}') +major=$(echo $host_gcc_version | awk -F . '{print $1}') +minor=$(echo $host_gcc_version | awk -F . '{print $2}') +if [ -z $major ] || [ -z $minor ] || [ $major -lt 6 ] || [ $major -eq 6 -a $minor -lt 3 ]; then + echo "host gcc $host_gcc_version is too old, need gcc 6.3.0" + exit 1 fi + +SUFFIX="$TOOLCHAIN_NAME$ANDROID_SDK_VERSION" +TOOLCHAIN=$NDK_PATH/toolchains/llvm/prebuilt/$HOST_OS-$HOST_ARCH + export PATH=$TOOLCHAIN/bin:$PATH -export AR=$TOOLCHAIN/bin/$SUFFIX-ar -export CC=$TOOLCHAIN/bin/$SUFFIX-gcc -export CXX=$TOOLCHAIN/bin/$SUFFIX-g++ -export LINK=$TOOLCHAIN/bin/$SUFFIX-g++ +export CC=$TOOLCHAIN/bin/$SUFFIX-clang +export CXX=$TOOLCHAIN/bin/$SUFFIX-clang++ + GYP_DEFINES="target_arch=$ARCH" GYP_DEFINES+=" v8_target_arch=$ARCH" GYP_DEFINES+=" android_target_arch=$ARCH" -GYP_DEFINES+=" host_os=linux OS=android" +GYP_DEFINES+=" host_os=$HOST_OS OS=android" export GYP_DEFINES if [ -f "configure" ]; then @@ -75,5 +80,6 @@ if [ -f "configure" ]; then --dest-cpu=$DEST_CPU \ --dest-os=android \ --without-snapshot \ - --openssl-no-asm + --openssl-no-asm \ + --cross-compiling fi diff --git a/common.gypi b/common.gypi index 6488241b3c6601..574a4ddb2a3e32 100644 --- a/common.gypi +++ b/common.gypi @@ -417,6 +417,10 @@ 'defines': [ '_GLIBCXX_USE_C99_MATH' ], 'libraries': [ '-llog' ], }], + ['_toolset=="host"', { + 'cflags': [ '-pthread' ], + 'ldflags': [ '-pthread' ], + }], ], }], ['OS=="mac"', { diff --git a/deps/cares/config/android/ares_config.h b/deps/cares/config/android/ares_config.h index 1a5265a5098d3c..50c8114396b0fd 100644 --- a/deps/cares/config/android/ares_config.h +++ b/deps/cares/config/android/ares_config.h @@ -128,7 +128,7 @@ #define HAVE_GETNAMEINFO 1 /* Define to 1 if you have the getservbyport_r function. */ -#define HAVE_GETSERVBYPORT_R 1 +/* #undef HAVE_GETSERVBYPORT_R */ /* Define to 1 if you have the `gettimeofday' function. */ #define HAVE_GETTIMEOFDAY 1 diff --git a/deps/openssl/openssl-cl_no_asm.gypi b/deps/openssl/openssl-cl_no_asm.gypi index bb9e55de91e3f2..2e12816aa3ebfd 100644 --- a/deps/openssl/openssl-cl_no_asm.gypi +++ b/deps/openssl/openssl-cl_no_asm.gypi @@ -12,9 +12,9 @@ 'includes': ['config/archs/linux-ppc64/no-asm/openssl-cl.gypi'], }, 'target_arch=="s390x" and OS=="linux"', { 'includes': ['config/archs/linux64-s390x/no-asm/openssl-cl.gypi'], - }, 'target_arch=="arm" and OS=="linux"', { + }, 'target_arch=="arm" and OS in ("linux", "android")', { 'includes': ['config/archs/linux-armv4/no-asm/openssl-cl.gypi'], - }, 'target_arch=="arm64" and OS=="linux"', { + }, 'target_arch=="arm64" and OS in ("linux", "android")', { 'includes': ['config/archs/linux-aarch64/no-asm/openssl-cl.gypi'], }, 'target_arch=="arm64" and OS=="win"', { 'includes': ['config/archs/VC-WIN64-ARM/no-asm/openssl-cl.gypi'], diff --git a/deps/openssl/openssl_no_asm.gypi b/deps/openssl/openssl_no_asm.gypi index 8eb61d970380c3..858c7d895eaee8 100644 --- a/deps/openssl/openssl_no_asm.gypi +++ b/deps/openssl/openssl_no_asm.gypi @@ -13,9 +13,9 @@ 'includes': ['config/archs/linux-ppc64/no-asm/openssl.gypi'], }, 'target_arch=="s390x" and OS=="linux"', { 'includes': ['config/archs/linux64-s390x/no-asm/openssl.gypi'], - }, 'target_arch=="arm" and OS=="linux"', { + }, 'target_arch=="arm" and OS in ("linux", "android")', { 'includes': ['config/archs/linux-armv4/no-asm/openssl.gypi'], - }, 'target_arch=="arm64" and OS=="linux"', { + }, 'target_arch=="arm64" and OS in ("linux", "android")', { 'includes': ['config/archs/linux-aarch64/no-asm/openssl.gypi'], }, 'target_arch=="ia32" and OS=="freebsd"', { 'includes': ['config/archs/BSD-x86/no-asm/openssl.gypi'], diff --git a/deps/uv/uv.gyp b/deps/uv/uv.gyp index 116b7537195618..fa0a001527d790 100644 --- a/deps/uv/uv.gyp +++ b/deps/uv/uv.gyp @@ -261,6 +261,7 @@ 'src/unix/android-ifaddrs.c', 'src/unix/procfs-exepath.c', 'src/unix/random-getrandom.c', + 'src/unix/random-getentropy.c', 'src/unix/random-sysctl-linux.c', 'src/unix/sysinfo-loadavg.c', ], diff --git a/tools/gyp/pylib/gyp/generator/make.py b/tools/gyp/pylib/gyp/generator/make.py index 26cf88cccf275d..848418d3fb6f16 100644 --- a/tools/gyp/pylib/gyp/generator/make.py +++ b/tools/gyp/pylib/gyp/generator/make.py @@ -201,7 +201,7 @@ def CalculateGeneratorInputInfo(params): quiet_cmd_link = LINK($(TOOLSET)) $@ quiet_cmd_link_host = LINK($(TOOLSET)) $@ cmd_link = $(LINK.$(TOOLSET)) $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ -Wl,--start-group $(LD_INPUTS) -Wl,--end-group $(LIBS) -cmd_link_host = $(LINK.$(TOOLSET)) $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ $(LD_INPUTS) $(LIBS) +cmd_link_host = $(LINK.$(TOOLSET)) $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ -Wl,--start-group $(LD_INPUTS) -Wl,--end-group $(LIBS) # Other shared-object link notes: # - Set SONAME to the library filename so our binaries don't reference diff --git a/tools/v8_gypfiles/v8.gyp b/tools/v8_gypfiles/v8.gyp index 2e5477b0eb4e49..cc4ac3ddba6749 100644 --- a/tools/v8_gypfiles/v8.gyp +++ b/tools/v8_gypfiles/v8.gyp @@ -952,41 +952,31 @@ }], ['is_android', { 'sources': [ - '<(V8_ROOT)/src/base/debug/stack_trace_android.cc', '<(V8_ROOT)/src/base/platform/platform-posix.cc', '<(V8_ROOT)/src/base/platform/platform-posix.h', '<(V8_ROOT)/src/base/platform/platform-posix-time.cc', '<(V8_ROOT)/src/base/platform/platform-posix-time.h', ], - 'conditions': [ - ['_toolset=="host"', { - 'link_settings': { + 'link_settings': { + 'target_conditions': [ + ['_toolset=="host" and host_os=="linux"', { 'libraries': [ '-ldl', '-lrt' - ] - }, - 'conditions': [ - ['_toolset=="host"', { - 'conditions': [ - ['host_os == "mac"', { - 'sources': [ - '<(V8_ROOT)/src/base/debug/stack_trace_posix.cc' - '<(V8_ROOT)/src/base/platform/platform-macos.cc' - ] - }, { - 'sources': [ - '<(V8_ROOT)/src/base/debug/stack_trace_posix.cc' - '<(V8_ROOT)/src/base/platform/platform-linux.cc' - ] - }], - ], - }, { - 'sources': [ - '<(V8_ROOT)/src/base/debug/stack_trace_android.cc' - '<(V8_ROOT)/src/base/platform/platform-linux.cc' - ] - }], + ], + }], + ], + }, + 'target_conditions': [ + ['_toolset=="host"', { + 'sources': [ + '<(V8_ROOT)/src/base/debug/stack_trace_posix.cc', + '<(V8_ROOT)/src/base/platform/platform-linux.cc', + ], + }, { + 'sources': [ + '<(V8_ROOT)/src/base/debug/stack_trace_android.cc', + '<(V8_ROOT)/src/base/platform/platform-linux.cc', ], }], ],