Skip to content

Commit 4e6f250

Browse files
MeowSheLampese
andcommitted
build: rewritten the Android build system
Completely rewritten the Android build system using Python Co-Authored-By: 东灯 <[email protected]>
1 parent 6280034 commit 4e6f250

File tree

2 files changed

+112
-98
lines changed

2 files changed

+112
-98
lines changed

android-configure

+35-98
Original file line numberDiff line numberDiff line change
@@ -1,98 +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 "android-configure: 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-
echo "###########################"
50-
echo "- Patch List"
51-
echo "[1] [deps/v8/src/trap-handler/trap-handler.h] related to https://github.com/nodejs/node/issues/36287"
52-
echo "###########################"
53-
54-
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
55-
56-
case $INPUTS in
57-
[yY][eE][sS]|[yY])
58-
patch -f ./deps/v8/src/trap-handler/trap-handler.h < ./android-patches/trap-handler.h.patch
59-
echo "Patches have been applied."
60-
;;
61-
*)
62-
;;
63-
esac
64-
65-
HOST_OS="linux"
66-
HOST_ARCH="x86_64"
67-
export CC_host=$(command -v gcc)
68-
export CXX_host=$(command -v g++)
69-
70-
host_gcc_version=$($CC_host --version | grep gcc | awk '{print $NF}')
71-
major=$(echo $host_gcc_version | awk -F . '{print $1}')
72-
minor=$(echo $host_gcc_version | awk -F . '{print $2}')
73-
if [ -z $major ] || [ -z $minor ] || [ $major -lt 6 ] || ( [ $major -eq 6 ] && [ $minor -lt 3 ] ); then
74-
echo "host gcc $host_gcc_version is too old, need gcc 6.3.0"
75-
return 1
76-
fi
77-
78-
SUFFIX="$TOOLCHAIN_NAME$ANDROID_SDK_VERSION"
79-
TOOLCHAIN=$NDK_PATH/toolchains/llvm/prebuilt/$HOST_OS-$HOST_ARCH
80-
81-
export PATH=$TOOLCHAIN/bin:$PATH
82-
export CC=$TOOLCHAIN/bin/$SUFFIX-clang
83-
export CXX=$TOOLCHAIN/bin/$SUFFIX-clang++
84-
85-
86-
GYP_DEFINES="target_arch=$ARCH"
87-
GYP_DEFINES+=" v8_target_arch=$ARCH"
88-
GYP_DEFINES+=" android_target_arch=$ARCH"
89-
GYP_DEFINES+=" host_os=$HOST_OS OS=android"
90-
export GYP_DEFINES
91-
92-
if [ -f "configure" ]; then
93-
./configure \
94-
--dest-cpu=$DEST_CPU \
95-
--dest-os=android \
96-
--openssl-no-asm \
97-
--cross-compiling
98-
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_configure.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from platform 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/3628")
9+
if platform().startswith('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().startswith('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+
36+
if sys.argv[3] == "arm":
37+
DEST_CPU = "arm"
38+
TOOLCHAIN_PREFIX = "armv7a-linux-androideabi"
39+
elif sys.argv[3] == "arm64" or sys.argv[3] == "aarch64":
40+
DEST_CPU = "arm64"
41+
TOOLCHAIN_PREFIX = "aarch64-linux-android"
42+
arch = "arm64"
43+
elif sys.argv[3] == "x86":
44+
DEST_CPU = "ia32"
45+
TOOLCHAIN_PREFIX = "i686-linux-android"
46+
elif sys.argv[3] == "x86_64":
47+
DEST_CPU = "x64"
48+
TOOLCHAIN_PREFIX = "x86_64-linux-android"
49+
arch = "x64"
50+
else:
51+
print("\033[91mError: \033[0m" + "Invalid target architecture, must be one of: arm, arm64, x86, x86_64")
52+
sys.exit(1)
53+
54+
arch = sys.argv[3]
55+
56+
print("\033[92mInfo: \033[0m" + "Configuring for " + DEST_CPU + "...")
57+
58+
if platform().startswith('Darwin'):
59+
host_os = "darwin"
60+
toolchain_path = android_ndk_path + "/toolchains/llvm/prebuilt/darwin-x86_64"
61+
62+
elif platform().startswith('Linux'):
63+
host_os = "linux"
64+
toolchain_path = android_ndk_path + "/toolchains/llvm/prebuilt/linux-x86_64"
65+
66+
os.environ['PATH'] += os.pathsep + toolchain_path + "/bin"
67+
os.environ['CC'] = toolchain_path + "/bin/" + TOOLCHAIN_PREFIX + android_sdk_version + "-" + "clang"
68+
os.environ['CXX'] = toolchain_path + "/bin/" + TOOLCHAIN_PREFIX + android_sdk_version + "-" + "clang++"
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+
os.environ['GYP_DEFINES'] = GYP_DEFINES
75+
76+
if os.path.exists("./configure"):
77+
os.system("./configure --dest-cpu=" + DEST_CPU + " --dest-os=android --openssl-no-asm --cross-compiling")

0 commit comments

Comments
 (0)