From dc5aad2342d0139e7d3e088bc5d57c926f1c97a3 Mon Sep 17 00:00:00 2001 From: MeowShe Date: Wed, 17 Aug 2022 08:05:14 +0800 Subject: [PATCH] build: rewritten the Android build system MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Completely rewritten the Android build system using Python Co-Authored-By: 东灯 <43312495+Lampese@users.noreply.github.com> --- android-configure | 133 ++++++++++++------------------------------- android_configure.py | 76 +++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 98 deletions(-) create mode 100644 android_configure.py diff --git a/android-configure b/android-configure index 7281a4604b4d61..f06dfc50f32111 100755 --- a/android-configure +++ b/android-configure @@ -1,98 +1,35 @@ -#!/bin/bash - -# In order to cross-compile node for Android using NDK, run: -# source android-configure [arch] -# -# By running android-configure with source, will allow environment variables to -# be persistent in current session. This is useful for installing native node -# modules with npm. Also, don't forget to set the arch in npm config using -# 'npm config set arch=' - -if [ $# -ne 3 ]; then - echo "android-configure: should have 3 parameters: ndk_path, target_arch and sdk_version" - return 1 -fi - -NDK_PATH=$1 -ARCH="$2" -ANDROID_SDK_VERSION=$3 - -if [ $ANDROID_SDK_VERSION -lt 24 ]; then - echo "$ANDROID_SDK_VERSION should equal or later than 24 (Android 7.0)" -fi - -case $ARCH in - arm) - DEST_CPU="arm" - TOOLCHAIN_NAME="armv7a-linux-androideabi" - ;; - x86) - DEST_CPU="ia32" - TOOLCHAIN_NAME="i686-linux-android" - ;; - x86_64) - 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" - return 1 - ;; -esac - -echo "###########################" -echo "- Patch List" -echo "[1] [deps/v8/src/trap-handler/trap-handler.h] related to https://github.com/nodejs/node/issues/36287" -echo "###########################" - -read -r -p "For building node for the Android platform, the above patch will be applied automatically to fix some of the potential issues during compilation, would you like to do that? [Y/n] " INPUTS - -case $INPUTS in - [yY][eE][sS]|[yY]) - patch -f ./deps/v8/src/trap-handler/trap-handler.h < ./android-patches/trap-handler.h.patch - echo "Patches have been applied." - ;; - *) - ;; -esac - -HOST_OS="linux" -HOST_ARCH="x86_64" -export CC_host=$(command -v gcc) -export CXX_host=$(command -v g++) - -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 ] && [ $minor -lt 3 ] ); then - echo "host gcc $host_gcc_version is too old, need gcc 6.3.0" - return 1 -fi - -SUFFIX="$TOOLCHAIN_NAME$ANDROID_SDK_VERSION" -TOOLCHAIN=$NDK_PATH/toolchains/llvm/prebuilt/$HOST_OS-$HOST_ARCH - -export PATH=$TOOLCHAIN/bin:$PATH -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=$HOST_OS OS=android" -export GYP_DEFINES - -if [ -f "configure" ]; then - ./configure \ - --dest-cpu=$DEST_CPU \ - --dest-os=android \ - --openssl-no-asm \ - --cross-compiling -fi +#!/bin/sh + +# Locate an acceptable Python interpreter and then re-execute the script. +# Note that the mix of single and double quotes is intentional, +# as is the fact that the ] goes on a new line. +_=[ 'exec' '/bin/sh' '-c' ''' +command -v python3.10 >/dev/null && exec python3.10 "$0" "$@" +command -v python3.9 >/dev/null && exec python3.9 "$0" "$@" +command -v python3.8 >/dev/null && exec python3.8 "$0" "$@" +command -v python3.7 >/dev/null && exec python3.7 "$0" "$@" +command -v python3.6 >/dev/null && exec python3.6 "$0" "$@" +command -v python3 >/dev/null && exec python3 "$0" "$@" +exec python "$0" "$@" +''' "$0" "$@" +] +del _ + +import sys +try: + from shutil import which +except ImportError: + from distutils.spawn import find_executable as which + +print('Node.js android configure: Found Python {}.{}.{}...'.format(*sys.version_info)) +acceptable_pythons = ((3, 10), (3, 9), (3, 8), (3, 7), (3, 6)) +if sys.version_info[:2] in acceptable_pythons: + import android_configure +else: + python_cmds = ['python{}.{}'.format(*vers) for vers in acceptable_pythons] + sys.stderr.write('Please use {}.\n'.format(' or '.join(python_cmds))) + for python_cmd in python_cmds: + python_cmd_path = which(python_cmd) + if python_cmd_path and 'pyenv/shims' not in python_cmd_path: + sys.stderr.write('\t{} {}\n'.format(python_cmd_path, ' '.join(sys.argv[:1]))) + sys.exit(1) \ No newline at end of file diff --git a/android_configure.py b/android_configure.py new file mode 100644 index 00000000000000..1ba0abc6e21aeb --- /dev/null +++ b/android_configure.py @@ -0,0 +1,76 @@ +from platform import platform +import sys +import os + +# TODO: In next version, it will be a JSON file listing all the patches, and then it will iterate through to apply them. +def patch_android(): + print("- Patches List -") + print("[1] [deps/v8/src/trap-handler/trap-handler.h] related to https://github.com/nodejs/node/issues/3628") + if platform().startswith('Linux'): + os.system('patch -f ./deps/v8/src/trap-handler/trap-handler.h < ./android-patches/trap-handler.h.patch') + print("\033[92mInfo: \033[0m" + "Tried to patch.") + +if platform().startswith('Windows'): + print('android-configure is not supported on Windows yet.') + sys.exit(1) + +if len(sys.argv) == 2 and sys.argv[1] == "patch": + patch_android() + sys.exit(0) + +if len(sys.argv) != 4: + print("Usage: ./android-configure [patch] ") + sys.exit(1) + +if not os.path.exists(sys.argv[1]) or not os.listdir(sys.argv[1]): + print("\033[91mError: \033[0m" + "Invalid path to the Android NDK") + sys.exit(1) + +if int(sys.argv[2]) < 24: + print("\033[91mError: \033[0m" + "Android SDK version must be at least 24 (Android 7.0)") + sys.exit(1) + +android_ndk_path = sys.argv[1] +android_sdk_version = sys.argv[2] +arch = sys.argv[3] + +if sys.argv[3] == "arm": + DEST_CPU = "arm" + TOOLCHAIN_PREFIX = "armv7a-linux-androideabi" +elif sys.argv[3] == "arm64" or sys.argv[3] == "aarch64": + DEST_CPU = "arm64" + TOOLCHAIN_PREFIX = "aarch64-linux-android" + arch = "arm64" +elif sys.argv[3] == "x86": + DEST_CPU = "ia32" + TOOLCHAIN_PREFIX = "i686-linux-android" +elif sys.argv[3] == "x86_64": + DEST_CPU = "x64" + TOOLCHAIN_PREFIX = "x86_64-linux-android" + arch = "x64" +else: + print("\033[91mError: \033[0m" + "Invalid target architecture, must be one of: arm, arm64, x86, x86_64") + sys.exit(1) + +print("\033[92mInfo: \033[0m" + "Configuring for " + DEST_CPU + "...") + +if platform().startswith('Darwin'): + host_os = "darwin" + toolchain_path = android_ndk_path + "/toolchains/llvm/prebuilt/darwin-x86_64" + +elif platform().startswith('Linux'): + host_os = "linux" + toolchain_path = android_ndk_path + "/toolchains/llvm/prebuilt/linux-x86_64" + +os.environ['PATH'] += os.pathsep + toolchain_path + "/bin" +os.environ['CC'] = toolchain_path + "/bin/" + TOOLCHAIN_PREFIX + android_sdk_version + "-" + "clang" +os.environ['CXX'] = toolchain_path + "/bin/" + TOOLCHAIN_PREFIX + android_sdk_version + "-" + "clang++" + +GYP_DEFINES = "target_arch=" + arch +GYP_DEFINES += " v8_target_arch=" + arch +GYP_DEFINES += " android_target_arch=" + arch +GYP_DEFINES += " host_os=" + host_os + " OS=android" +os.environ['GYP_DEFINES'] = GYP_DEFINES + +if os.path.exists("./configure"): + os.system("./configure --dest-cpu=" + DEST_CPU + " --dest-os=android --openssl-no-asm --cross-compiling")