Skip to content

Commit f9228e2

Browse files
committed
Initial commit
0 parents  commit f9228e2

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM ubuntu
2+
3+
WORKDIR /github/build-nodejs
4+
5+
RUN apt update && apt install wget python3 g++ make python3-pip git unzip curl -y
6+
7+
# 下载Android NDK
8+
RUN wget -q https://dl.google.com/android/repository/android-ndk-r23b-linux.zip && unzip -q android-ndk-r23b-linux.zip
9+
10+
COPY node-build.sh node-build.sh

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 xiaowj
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.MD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# build-nodejs-for-android
2+
3+
fork from [dna2github/dna2oslab](https://github.com/dna2github/dna2oslab)
4+
5+
使用Android NDK交叉编译Nodejs,将Nodejs编译为libnode.so,便于在Android平台上使用。
6+
7+
也可以使用编译好的so[xiaowj/Android-nodejs](https://github.com/xiaowj/Android-nodejs)

node-build.sh

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#/bin/bash
2+
set -xe
3+
4+
MEDIR=$(cd `dirname $0`; pwd)
5+
ME=node-v16.13.1
6+
7+
cd $MEDIR
8+
9+
SELF=$(cd `dirname $0`; pwd)
10+
11+
export NDK="/github/build-nodejs/android-ndk-r23b"
12+
export ENVSRCTARBALL=""
13+
export HOST_GCC_DIR="/usr"
14+
export ENVDISTBIN="dist"
15+
export ENVHOST=linux-x86_64
16+
export ENVTARGET=aarch64-linux-android
17+
export ENVANDROIDVER=23
18+
export CROSS_COMPILE="--build=x86_64-linux --host=arm-eabi --target=arm-eabi"
19+
20+
if [ -f $SELF/local/env.sh ]; then
21+
source $SELF/local/env.sh
22+
fi
23+
24+
if [ "$NDK" == "" ]; then
25+
echo 'check: ENVHOST, ENVTARGET, ENVANDROIDVER'
26+
echo
27+
echo 'run cmd: mkdir local; touch local/env.sh'
28+
echo 'fill: NDK, ENVSRCTARBALL'
29+
echo 'optional: HOST_GCC_DIR'
30+
exit 0
31+
fi
32+
33+
export COMPILERDIR="$NDK/toolchains/llvm/prebuilt/$ENVHOST/bin"
34+
export CC="$COMPILERDIR/${ENVTARGET}${ENVANDROIDVER}-clang"
35+
export CXX="$COMPILERDIR/${ENVTARGET}${ENVANDROIDVER}-clang++"
36+
export LD="$COMPILERDIR/$ENVTARGET-ld"
37+
export AS="$COMPILERDIR/$ENVTARGET-as"
38+
export AR="$COMPILERDIR/llvm-ar"
39+
export STRIP="$COMPILERDIR/llvm-strip"
40+
export OBJCOPY="$COMPILERDIR/llvm-objcopy"
41+
export OBJDUMP="$COMPILERDIR/llvm-objdump"
42+
export RANLIB="$COMPILERDIR/llvm-ranlib"
43+
export NM="$COMPILERDIR/llvm-nm"
44+
export STRINGS="$COMPILERDIR/llvm-strings"
45+
export READELF="$COMPILERDIR/llvm-readelf"
46+
47+
export ANDROID="$COMPILERDIR/../sysroot"
48+
49+
function fetch_source() {
50+
# $1: package file name, e.g. vim-7.4.0001.tar.gz
51+
# $2: source url
52+
test -f "$ENVSRCTARBALL/$1" || curl -k -L -o "$ENVSRCTARBALL/$1" "$2"
53+
test -f "$ENVSRCTARBALL/$1" || exit 1
54+
}
55+
56+
cd ..
57+
rm -rf $ME
58+
fetch_source $ME.tar.gz https://nodejs.org/dist/v16.13.1/node-v16.13.1.tar.gz
59+
tar zxf $ENVSRCTARBALL/$ME.tar.gz
60+
cd $ME
61+
62+
# by default, build for arm64
63+
ARCH=arm64
64+
DEST_CPU=arm64
65+
HOST_OS="linux"
66+
HOST_ARCH="x86_64"
67+
68+
if [ "$HOST_GCC_DIR" == "" ]; then
69+
if [ ! -d $MEDIR/local/gcc-9.3.0 ]; then
70+
cat <<EOF
71+
it will start to compile gcc-9.3.0;
72+
if you have any (gcc >= 6.3), please stop the script and
73+
- set CC_host, CXX_host, AR_host, RANLIB_host
74+
- or set HOST_GCC_DIR to the specified GCC root directory
75+
EOF
76+
sleep 10
77+
# build gcc-9.3.0
78+
bash host_gcc_9.3.sh
79+
HOST_GCC_DIR=$MEDIR/local/gcc-9.3.0/dist
80+
fi
81+
fi
82+
83+
echo host gcc: $HOST_GCC_DIR
84+
85+
export CC_host=$HOST_GCC_DIR/bin/gcc
86+
export CXX_host=$HOST_GCC_DIR/bin/g++
87+
export AR_host=$HOST_GCC_DIR/bin/gcc-ar
88+
export RANLIB_host=$HOST_GCC_DIR/bin/gcc-ranlib
89+
export LD_LIBRARY_PATH=$HOST_GCC_DIR/lib64:$LD_LIBRARY_PATH
90+
91+
GYP_DEFINES="target_arch=$ARCH"
92+
GYP_DEFINES+=" v8_target_arch=$ARCH"
93+
GYP_DEFINES+=" android_target_arch=$ARCH"
94+
GYP_DEFINES+=" host_os=$HOST_OS OS=android"
95+
export GYP_DEFINES
96+
97+
./configure \
98+
--prefix=$MEDIR/../dist/$ME \
99+
--dest-cpu=$DEST_CPU \
100+
--dest-os=android \
101+
--without-snapshot \
102+
--openssl-no-asm \
103+
--cross-compiling \
104+
--shared
105+
106+
grep "LD_LIBRARY_PATH=" . -r | grep -v Binary | cut -d ':' -f 1 | sort -u | xargs sed -i "s|LD_LIBRARY_PATH=|LD_LIBRARY_PATH=$HOST_GCC_DIR/dist/lib64:|g"
107+
108+
# make sure some functions are available in link stage
109+
sed -i 's|/poll.o \\|/poll.o \\\n\t$(obj).target/$(TARGET)/deps/uv/src/unix/epoll.o \\|' out/deps/uv/libuv.target.mk
110+
111+
# disable TRAP_HANDLER
112+
sed -i "s|// Setup for shared library export.|#undef V8_TRAP_HANDLER_VIA_SIMULATOR\n#undef V8_TRAP_HANDLER_SUPPORTED\n#define V8_TRAP_HANDLER_SUPPORTED false\n\n// Setup for shared library export.|" deps/v8/src/trap-handler/trap-handler.h
113+
114+
make -j4

0 commit comments

Comments
 (0)