From 09b1f461a61dc20353a8f9180e7f2b733a7183c2 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/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 ++++++--------- 7 files changed, 69 insertions(+), 68 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 8c9076b7352d81..87c9f435695c4d 100644 --- a/common.gypi +++ b/common.gypi @@ -439,6 +439,10 @@ 'defines': [ '_GLIBCXX_USE_C99_MATH' ], 'libraries': [ '-llog' ], }], + ['_toolset=="host"', { + 'cflags': [ '-pthread' ], + 'ldflags': [ '-pthread' ], + }], ], }], ['OS=="mac"', { 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 c4564c04086bc0..462a9625c65e37 100644 --- a/deps/uv/uv.gyp +++ b/deps/uv/uv.gyp @@ -262,6 +262,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 91a119c5a57694..eb39e0d2f9ed0e 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 d154dcd005dd84..26b8e56e834a05 100644 --- a/tools/v8_gypfiles/v8.gyp +++ b/tools/v8_gypfiles/v8.gyp @@ -956,41 +956,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', ], }], ],