android 编译 node js 14

本文基于wsl ubuntu 22.04.1 LTS 系统 上成功编译 安卓版 node js 14.15.4的一些记录.

编译环境:

nodejs 用到两套编译器分别用来编译本机的一些工具链和目标平台的node二进制

1.本机相关环境配置:

 下载ndk android-ndk-r23b 解压到~/android-ndk-r23b

 python 3.10 

  安装 gcc 9.40

# by default, build for arm64
HOST_OS="linux"
HOST_ARCH="x86_64"
HOST_GCC_DIR=/usr

export CC_host=$HOST_GCC_DIR/bin/gcc
export CXX_host=$HOST_GCC_DIR/bin/g++
export AR_host=$HOST_GCC_DIR/bin/gcc-ar
export RANLIB_host=$HOST_GCC_DIR/bin/gcc-ranlib
export LD_LIBRARY_PATH=$HOST_GCC_DIR/lib64:$LD_LIBRARY_PATH

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

 

2.ndk编译配置

export NDK=~/android-ndk-r23b
export ENVSRCTARBALL=""
export HOST_GCC_DIR=""
export ENVDISTBIN="dist"
export ENVHOST=linux-x86_64
export ENVTARGET=aarch64-linux-android
export ENVANDROIDVER=23
export CROSS_COMPILE="--build=x86_64-linux --host=arm-eabi --target=arm-eabi"

export COMPILERDIR="$NDK/toolchains/llvm/prebuilt/$ENVHOST/bin"
export CC="$COMPILERDIR/${ENVTARGET}${ENVANDROIDVER}-clang"
export CXX="$COMPILERDIR/${ENVTARGET}${ENVANDROIDVER}-clang++"
export LD="$COMPILERDIR/$ENVTARGET-ld"
export AS="$COMPILERDIR/$ENVTARGET-as"
export AR="$COMPILERDIR/llvm-ar"
export STRIP="$COMPILERDIR/llvm-strip"
export OBJCOPY="$COMPILERDIR/llvm-objcopy"
export OBJDUMP="$COMPILERDIR/llvm-objdump"
export RANLIB="$COMPILERDIR/llvm-ranlib"
export NM="$COMPILERDIR/llvm-nm"
export STRINGS="$COMPILERDIR/llvm-strings"
export READELF="$COMPILERDIR/llvm-readelf"
export ANDROID="$COMPILERDIR/../sysroot"

  

3.开始编译

./configure \
    --prefix=~/node-v14.15.4 \
    --dest-cpu=arm64 \
    --dest-os=android \
    --without-snapshot \
    --openssl-no-asm \
    --cross-compiling \
    --shared

 

4.编译过程中的一些问题

全文搜索python脚本 

grep -r 'from collections import Mapping'  ../node-v14.15.4/deps

grep -r 'from collections import Mapping'  ../node-v14.15.4/tools

把from collections import Mappin替换为from collections.abc import Mappin

原因是因为pyhton3.10版本高了

 

5.addon的编译

在同一台机器上安装原生版本的node-v14.14.4 和npm.

安装node-gyp,下面以官方例子为例:

#下载官方例子
git clone https://github.com/nodejs/node-addon-examples.git
#以helloword的为例
cd node-addon-examples/1_hello_world/napi
#修改binding.gyp ,在sources后面添加
# "libraries": [ "-lnode","-L.." ],
#并把libnode.so复制到这个目录下

#生成makefile
node-gyp configure --arch=$DEST_CPU --nodedir=~/node-v14.15.4
#编译
node-gyp build --silly

 

posted on 2023-02-06 14:35  Jamy325  阅读(320)  评论(0)    收藏  举报

导航