產生 toolchain 請參考先前的文章。
到 Node.js 官網。如果要跨編譯最新的 preview 版,可以到 Github 網址上下載。
建議到官網下載 stable 版的 source 進行 cross compile. 到官網點選 source code.
參考官方提供的 android-configure 進行修改並 autoconf. 下面是官方預設的參數,若有參考 ./configure –help 產生出說明的文件,發現有一些參數可以加上去,像是 –fully-static 和
–enable-static 等。
不過,當加上 –fully-static ,產生執行檔沒有包含外部的動態l ibs ,使用這個參數的時候,意思是,我在 make 編譯期間會出現錯誤,會出現:/usr/bin/ld: cannot find -llog 目前在 Github 也有類似的問題。
當加上 –enable-static 時,是需要 build as static library 將l ibs 函式庫做靜態連結。不過這一步也沒有成功,一開始的 autoconf 就會有問題。
目前這兩個問題沒有解決的方式。
修改如下:(toolchain 依照自己路徑進行修改)
需要:Python 2.7+ (Linux/Ubuntu 內建)pshot
若編譯進去,則在 make 期間會出現類似下列這樣的錯誤:
pi1p/out/Release/mksnapshot: 1: /home/iojs/build/workspace/node-cross-compile/nodes/cross-compiler-pi1p/out/Release/mksnapshot: Syntax error: word unexpected (expecting ")")
#!/bin/bash export TOOLCHAIN=/home/lab501/android-toolchain-16 export PATH=$TOOLCHAIN/bin:$PATH export AR=$TOOLCHAIN/bin/arm-linux-androideabi-ar export CC=$TOOLCHAIN/bin/arm-linux-androideabi-gcc export CXX=$TOOLCHAIN/bin/arm-linux-androideabi-g++ export LINK=$TOOLCHAIN/bin/arm-linux-androideabi-g++ ./configure \ --dest-cpu=arm \ --dest-os=android \ --without-snapshot \ --prefix=/home/lab501/node-v4.3.0/_install
編譯 Node.js 版本:v4.3.0
需要:Python 2.7+ (Linux/Ubuntu 內建)
遇到的問題:
也可以設定一些相關的東西讓 snapshot 編進去,不過省麻煩,這裡就不編進去了。這裡編譯用全靜態連結。(static mode)
再來就是 make 和 make install 了,完成之後。會在指定目錄下產生出 node 執行檔。這樣 Node.js 編譯就完成了。
[錯誤]
使用官方參數所遇到的錯誤:在編譯時,呼叫 python 檔做執行時,會 import bz2 沒有找到相關的 module 。
這是一個普通的錯誤 (general error),所以只要:sudo apt-get install libbz2-dev 把套件安裝起來就可以。之後還需要重新編譯 Python 否則會一直出現相同錯誤導致編譯中斷。
雖然可以輸入指令:make 再繼續編譯,一樣會成功,不過這不是一個好的方式。
再來就是要注意,若是自己的電腦上在編譯時有其他 Python 需要的 module 時,也是需要查詢並做安裝。這篇文章沒有出線原因是因為可能之前我做其他事已經把相關的套件做安裝了。
所以這個錯誤是因人而異。
[參考資料]
Android編譯提示ImportError: No module named bz2的解決辦法
[後記]
編譯 Node.js 版本:v0.12.10
下載自:Github
需要:Python 2.7+ (Linux/Ubuntu 內建)
./android-configure bash scrit for auto conf.
#!/bin/bash export TOOLCHAIN=/home/lab501/android-toolchain-16 export PATH=$TOOLCHAIN/bin:$PATH export AR=$TOOLCHAIN/bin/arm-linux-androideabi-ar export CC=$TOOLCHAIN/bin/arm-linux-androideabi-gcc export CXX=$TOOLCHAIN/bin/arm-linux-androideabi-g++ export LINK=$TOOLCHAIN/bin/arm-linux-androideabi-g++ ./configure \ --dest-cpu=arm \ --dest-os=android \ --without-snapshot \ --prefix=/home/lab501/node/_install
結果:編譯成功,無錯誤,使用 –fully-static 參數仍會出現錯誤,懷疑是 Node.js 較高版本才出現的問題。需要再詳細測試才會知道。
[參考文章]
Building and running Node.js for Android
[後記]
PIE 問題,參考:https://github.com/nodejs/node/issues/3581
修改:/path/to/node-v4.3.0/common.gypi 下的檔案。
找到下列的片段:
'target_defaults': {
'default_configuration': 'Release',
'configurations': {
'Debug': {
'variables': {
'v8_enable_handle_zapping': 1,
},
'defines': [ 'DEBUG', '_DEBUG' ],
'cflags': [ '-g', '-O0' ],
'conditions': [
['target_arch=="x64"', {
'msvs_configuration_platform': 'x64',
}],
['OS=="aix"', {
'cflags': [ '-gxcoff' ],
'ldflags': [ '-Wl,-bbigtoc' ],
}],
['OS == "android"', {
'cflags': [ '-fPIE' ],
'ldflags': [ '-fPIE', '-pie' ]
}]
],
'msvs_settings': {
'VCCLCompilerTool': {
'RuntimeLibrary': 1, # static debug
'Optimization': 0, # /Od, no optimization
'MinimalRebuild': 'false',
'OmitFramePointers': 'false',
'BasicRuntimeChecks': 3, # /RTC1
},
'VCLinkerTool': {
'LinkIncremental': 2, # enable incremental linking
},
},
'xcode_settings': {
'GCC_OPTIMIZATION_LEVEL': '0', # stop gyp from defaulting to -Os
},
},
'Release': {
'variables': {
'v8_enable_handle_zapping': 0,
},
'cflags': [ '-O3', '-ffunction-sections', '-fdata-sections' ],
'conditions': [
['target_arch=="x64"', {
'msvs_configuration_platform': 'x64',
}],
['OS=="solaris"', {
`# pull in V8's postmortem metadata`
'ldflags': [ '-Wl,-z,allextract' ]
}],
['OS!="mac" and OS!="win"', {
'cflags': [ '-fno-omit-frame-pointer' ],
}],
['OS == "android"', {
'cflags': [ '-fPIE' ],
'ldflags': [ '-fPIE', '-pie' ]
}]
],
新增這段文字:
['OS == "android"', {
'cflags': [ '-fPIE', '-pie' ],
'ldflags': [ '-fPIE', '-pie' ]
}]