Skip to content

Commit 70898b4

Browse files
MeowSheLampeseF3n67u
authored andcommitted
build: rewritten the Android build system
Completely rewritten the Android build system using Python Co-Authored-By: 东灯 <43312495+Lampese@users.noreply.github.com> Co-Authored-By: Feng Yu <F3n67u@outlook.com> PR-URL: #44207 Refs: #36287 Reviewed-By: Feng Yu <F3n67u@outlook.com> Reviewed-By: Christian Clauss <cclauss@me.com>
1 parent caad474 commit 70898b4

File tree

3 files changed

+137
-83
lines changed

3 files changed

+137
-83
lines changed

android-configure

+35-83
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,35 @@
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-
11-
if [ $# -ne 3 ]; then
12-
echo "$0 should have 3 parameters: ndk_path, target_arch and sdk_version"
13-
return 1
14-
fi
15-
16-
NDK_PATH=$1
17-
ARCH="$2"
18-
ANDROID_SDK_VERSION=$3
19-
20-
if [ $ANDROID_SDK_VERSION -lt 24 ]; then
21-
echo "$ANDROID_SDK_VERSION should equal or later than 24 (Android 7.0)"
22-
fi
23-
24-
case $ARCH in
25-
arm)
26-
DEST_CPU="arm"
27-
TOOLCHAIN_NAME="armv7a-linux-androideabi"
28-
;;
29-
x86)
30-
DEST_CPU="ia32"
31-
TOOLCHAIN_NAME="i686-linux-android"
32-
;;
33-
x86_64)
34-
DEST_CPU="x64"
35-
TOOLCHAIN_NAME="x86_64-linux-android"
36-
ARCH="x64"
37-
;;
38-
arm64|aarch64)
39-
DEST_CPU="arm64"
40-
TOOLCHAIN_NAME="aarch64-linux-android"
41-
ARCH="arm64"
42-
;;
43-
*)
44-
echo "Unsupported architecture provided: $ARCH"
45-
return 1
46-
;;
47-
esac
48-
49-
HOST_OS="linux"
50-
HOST_ARCH="x86_64"
51-
export CC_host=$(command -v gcc)
52-
export CXX_host=$(command -v g++)
53-
54-
host_gcc_version=$($CC_host --version | grep gcc | awk '{print $NF}')
55-
major=$(echo $host_gcc_version | awk -F . '{print $1}')
56-
minor=$(echo $host_gcc_version | awk -F . '{print $2}')
57-
if [ -z $major ] || [ -z $minor ] || [ $major -lt 6 ] || ( [ $major -eq 6 ] && [ $minor -lt 3 ] ); then
58-
echo "host gcc $host_gcc_version is too old, need gcc 6.3.0"
59-
return 1
60-
fi
61-
62-
SUFFIX="$TOOLCHAIN_NAME$ANDROID_SDK_VERSION"
63-
TOOLCHAIN=$NDK_PATH/toolchains/llvm/prebuilt/$HOST_OS-$HOST_ARCH
64-
65-
export PATH=$TOOLCHAIN/bin:$PATH
66-
export CC=$TOOLCHAIN/bin/$SUFFIX-clang
67-
export CXX=$TOOLCHAIN/bin/$SUFFIX-clang++
68-
69-
70-
GYP_DEFINES="target_arch=$ARCH"
71-
GYP_DEFINES+=" v8_target_arch=$ARCH"
72-
GYP_DEFINES+=" android_target_arch=$ARCH"
73-
GYP_DEFINES+=" host_os=$HOST_OS OS=android"
74-
export GYP_DEFINES
75-
76-
if [ -f "configure" ]; then
77-
./configure \
78-
--dest-cpu=$DEST_CPU \
79-
--dest-os=android \
80-
--without-snapshot \
81-
--openssl-no-asm \
82-
--cross-compiling
83-
fi
1+
#!/bin/sh
2+
3+
# Locate an acceptable Python interpreter and then re-execute the script.
4+
# Note that the mix of single and double quotes is intentional,
5+
# as is the fact that the ] goes on a new line.
6+
_=[ 'exec' '/bin/sh' '-c' '''
7+
command -v python3.10 >/dev/null && exec python3.10 "$0" "$@"
8+
command -v python3.9 >/dev/null && exec python3.9 "$0" "$@"
9+
command -v python3.8 >/dev/null && exec python3.8 "$0" "$@"
10+
command -v python3.7 >/dev/null && exec python3.7 "$0" "$@"
11+
command -v python3.6 >/dev/null && exec python3.6 "$0" "$@"
12+
command -v python3 >/dev/null && exec python3 "$0" "$@"
13+
exec python "$0" "$@"
14+
''' "$0" "$@"
15+
]
16+
del _
17+
18+
import sys
19+
try:
20+
from shutil import which
21+
except ImportError:
22+
from distutils.spawn import find_executable as which
23+
24+
print('Node.js android configure: Found Python {}.{}.{}...'.format(*sys.version_info))
25+
acceptable_pythons = ((3, 10), (3, 9), (3, 8), (3, 7), (3, 6))
26+
if sys.version_info[:2] in acceptable_pythons:
27+
import android_configure
28+
else:
29+
python_cmds = ['python{}.{}'.format(*vers) for vers in acceptable_pythons]
30+
sys.stderr.write('Please use {}.\n'.format(' or '.join(python_cmds)))
31+
for python_cmd in python_cmds:
32+
python_cmd_path = which(python_cmd)
33+
if python_cmd_path and 'pyenv/shims' not in python_cmd_path:
34+
sys.stderr.write('\t{} {}\n'.format(python_cmd_path, ' '.join(sys.argv[:1])))
35+
sys.exit(1)

android-patches/trap-handler.h.patch

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--- trap-handler.h 2022-08-11 09:01:23.384000000 +0800
2+
+++ fixed-trap-handler.h 2022-08-11 09:09:15.352000000 +0800
3+
@@ -17,23 +17,7 @@
4+
namespace internal {
5+
namespace trap_handler {
6+
7+
-// X64 on Linux, Windows, MacOS, FreeBSD.
8+
-#if V8_HOST_ARCH_X64 && V8_TARGET_ARCH_X64 && \
9+
- ((V8_OS_LINUX && !V8_OS_ANDROID) || V8_OS_WIN || V8_OS_DARWIN || \
10+
- V8_OS_FREEBSD)
11+
-#define V8_TRAP_HANDLER_SUPPORTED true
12+
-// Arm64 (non-simulator) on Mac.
13+
-#elif V8_TARGET_ARCH_ARM64 && V8_HOST_ARCH_ARM64 && V8_OS_DARWIN
14+
-#define V8_TRAP_HANDLER_SUPPORTED true
15+
-// Arm64 simulator on x64 on Linux, Mac, or Windows.
16+
-#elif V8_TARGET_ARCH_ARM64 && V8_HOST_ARCH_X64 && \
17+
- (V8_OS_LINUX || V8_OS_DARWIN)
18+
-#define V8_TRAP_HANDLER_VIA_SIMULATOR
19+
-#define V8_TRAP_HANDLER_SUPPORTED true
20+
-// Everything else is unsupported.
21+
-#else
22+
#define V8_TRAP_HANDLER_SUPPORTED false
23+
-#endif
24+
25+
// Setup for shared library export.
26+
#if defined(BUILDING_V8_SHARED) && defined(V8_OS_WIN)

android_configure.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import platform
2+
import sys
3+
import os
4+
5+
# TODO: In next version, it will be a JSON file listing all the patches, and then it will iterate through to apply them.
6+
def patch_android():
7+
print("- Patches List -")
8+
print("[1] [deps/v8/src/trap-handler/trap-handler.h] related to https://github.com/nodejs/node/issues/36287")
9+
if platform.system() == "Linux":
10+
os.system('patch -f ./deps/v8/src/trap-handler/trap-handler.h < ./android-patches/trap-handler.h.patch')
11+
print("\033[92mInfo: \033[0m" + "Tried to patch.")
12+
13+
if platform.system() == "Windows":
14+
print("android-configure is not supported on Windows yet.")
15+
sys.exit(1)
16+
17+
if len(sys.argv) == 2 and sys.argv[1] == "patch":
18+
patch_android()
19+
sys.exit(0)
20+
21+
if len(sys.argv) != 4:
22+
print("Usage: ./android-configure [patch] <path to the Android NDK> <Android SDK version> <target architecture>")
23+
sys.exit(1)
24+
25+
if not os.path.exists(sys.argv[1]) or not os.listdir(sys.argv[1]):
26+
print("\033[91mError: \033[0m" + "Invalid path to the Android NDK")
27+
sys.exit(1)
28+
29+
if int(sys.argv[2]) < 24:
30+
print("\033[91mError: \033[0m" + "Android SDK version must be at least 24 (Android 7.0)")
31+
sys.exit(1)
32+
33+
android_ndk_path = sys.argv[1]
34+
android_sdk_version = sys.argv[2]
35+
arch = sys.argv[3]
36+
37+
if arch == "arm":
38+
DEST_CPU = "arm"
39+
TOOLCHAIN_PREFIX = "armv7a-linux-androideabi"
40+
elif arch in ("aarch64", "arm64"):
41+
DEST_CPU = "arm64"
42+
TOOLCHAIN_PREFIX = "aarch64-linux-android"
43+
arch = "arm64"
44+
elif arch == "x86":
45+
DEST_CPU = "ia32"
46+
TOOLCHAIN_PREFIX = "i686-linux-android"
47+
elif arch == "x86_64":
48+
DEST_CPU = "x64"
49+
TOOLCHAIN_PREFIX = "x86_64-linux-android"
50+
arch = "x64"
51+
else:
52+
print("\033[91mError: \033[0m" + "Invalid target architecture, must be one of: arm, arm64, aarch64, x86, x86_64")
53+
sys.exit(1)
54+
55+
print("\033[92mInfo: \033[0m" + "Configuring for " + DEST_CPU + "...")
56+
57+
if platform.system() == "Darwin":
58+
host_os = "darwin"
59+
toolchain_path = android_ndk_path + "/toolchains/llvm/prebuilt/darwin-x86_64"
60+
61+
elif platform.system() == "Linux":
62+
host_os = "linux"
63+
toolchain_path = android_ndk_path + "/toolchains/llvm/prebuilt/linux-x86_64"
64+
65+
os.environ['PATH'] += os.pathsep + toolchain_path + "/bin"
66+
os.environ['CC'] = toolchain_path + "/bin/" + TOOLCHAIN_PREFIX + android_sdk_version + "-" + "clang"
67+
os.environ['CXX'] = toolchain_path + "/bin/" + TOOLCHAIN_PREFIX + android_sdk_version + "-" + "clang++"
68+
69+
GYP_DEFINES = "target_arch=" + arch
70+
GYP_DEFINES += " v8_target_arch=" + arch
71+
GYP_DEFINES += " android_target_arch=" + arch
72+
GYP_DEFINES += " host_os=" + host_os + " OS=android"
73+
os.environ['GYP_DEFINES'] = GYP_DEFINES
74+
75+
if os.path.exists("./configure"):
76+
os.system("./configure --dest-cpu=" + DEST_CPU + " --dest-os=android --openssl-no-asm --cross-compiling")

0 commit comments

Comments
 (0)