in Cross compile

Cross compile libraries for Android

前置作業:產生 standalone toolchain

cross compile 需要用到的 libraries.

dependencies:mhash,bz2,zlib,libxml,and more.

下載點:提供修改過後的版本,官方下載點也可,不過有些地方需要自己自行修改,會寫在每個 libs 裡的 bash script file for auto conf. 做註解

bash script file 若是自己編輯的,需要把權限改為可執行檔。(executable) 即:chmod 700 /path/to/android-configure

也可以自行下載其他版本的 libraries 做編譯,如果有遇到錯誤,則可以去 MEGA 載點參考 libraries 中的檔案或是 android-toolchain 裡的檔標頭案。裡面的標頭檔都附已修正過的。

就試著 try and error 吧,改成正確的。

下載點記得把裡面的目錄:_install 刪除並做:make distclean (OpenSSL 是 make dist 與 make clean。 ) 才可以清掉之前編譯的內容與 Makefile。

(也有 Android toolchain 可以參考)

環境變數 TOOLCHAIN 需改成自己正確的路徑與安裝路徑 (–prefix)。

下面各個 source 的 MEGA 載點

souce 目錄裡面的 android-configure 檔案即為下面的各個 libs 的 bash script file

mhash


#!/bin/bash

export TOOLCHAIN=/home/lab501/android-toolchain
export PATH=$TOOLCHAIN/bin:$PATH
export CC=$TOOLCHAIN/bin/arm-linux-androideabi-gcc
export CXX=$TOOLCHAIN/bin/arm-linux-androideabi-g++
export LD=$TOOLCHAIN/bin/arm-linux-androideabi-ld
export AR=$TOOLCHAIN/bin/arm-linux-androideabi-ar
export RANLIB=$TOOLCHAIN/bin/arm-linux-androideabi-ranlib
export ac_cv_func_malloc_0_nonnull=yes

./configure \
 --host=arm-linux-androideabi \
 --disable-shared \
 --enable-static \
 --prefix=/home/lab501/mhash-0.9.9.9/_install

#make and make install

 

bz2

#source code 沒有 configure 直接給 Makefile
#!/bin/bash

TOOLCHAIN=/home/lab501/android-toolchain
PATH=$TOOLCHAIN/bin:$PATH

make install \
 PREFIX=/home/lab501/bzip2-1.0.6/_install \
 CC=$TOOLCHAIN/bin/arm-linux-androideabi-gcc \
 AR=$TOOLCHAIN/bin/arm-linux-androideabi-ar \
 RANLIB=$TOOLCHAIN/bin/arm-linux-androideabi-ranlib

zlib

#!/bin/bash

export TOOLCHAIN=/home/lab501/android-toolchain
export PATH=$TOOLCHAIN/bin:$PATH
export CC=$TOOLCHAIN/bin/arm-linux-androideabi-gcc
export RANLIB=$TOOLCHAIN/bin/arm-linux-androideabi-ranlib

./configure \
 --prefix="/home/lab501/zlib-1.1.3/_install" \
 --static
#make and make install

libxml


#!/bin/bash

export TOOLCHAIN=/home/lab501/android-toolchain
export PATH=$TOOLCHAIN/bin:$PATH
export CC=$TOOLCHAIN/bin/arm-linux-androideabi-gcc
export RANLIB=$TOOLCHAIN/bin/arm-linux-androideabi-ranlib
export AR=$TOOLCHAIN/bin/arm-linux-androideabi-ar
export LD=$TOOLCHAIN/bin/arm-linux-androideabi-ld
export ARCH=arm

~/android-toolchain/bin/arm-linux-androideabi-gcc -c -I/home/lab501/android-toolchain/sysroot/usr/include -I. glob.c \

chmod +x glob.o

export LIBS=$PWD/glob.o

./configure \
 --enable-static \
 --disable-shared \
 --host=arm-linux-androideabi \
 --prefix=/home/lab501/libxml2-2.7.6/_install \
 --without-python \
 --with-iconv=/home/lab501/libiconv-1.14/_install \
 --with-zlib=/home/lab501/zlib-1.2.8/_install \
 --with-readline=/home/lab501/readline-6.2/_install

#make and make install

cURL

 #!/bin/bash

export TOOLCHAIN=/home/lab501/android-toolchain
export PATH=$TOOLCHAIN/bin:$PATH
export CC=$TOOLCHAIN/bin/arm-linux-androideabi-gcc
export CXX=$TOOLCHAIN/bin/arm-linux-androideabi-g++
export RANLIB=$TOOLCHAIN/bin/arm-linux-androideabi-ranlib

./configure \
 --host=arm-linux-androideabi \
 --with-ssl=/home/lab501/openssl-1.0.1f/_install \
 --disable-shared \
 --enable-static \
 --prefix=/home/lab501/curl-7.35.0/_install \
 --with-zlib=/home/lab501/zlib-1.2.8/_install
#make and make install

OpenSSL


#!/bin/bash

export OPENSSL_TARGET=linux-armv4
export TOOLCHAIN=/home/lab501/android-toolchain-16
export PATH=$TOOLCHAIN/bin:$PATH
export CC=$TOOLCHAIN/bin/arm-linux-androideabi-gcc
export AR=$TOOLCHAIN/bin/arm-linux-androideabi-ar
export LD=$TOOLCHAIN/bin/arm-linux-androideabi-ld
export AS=$TOOLCHAIN/bin/arm-linux-androideabi-as
export RANLIB=$TOOLCHAIN/bin/arm-linux-androideabi-ranlib

./Configure $OPENSSL_TARGET no-shared \
 --prefix=/home/lab501/android-cross-compile-16/openssl-1.0.1f/_install \
 --with-zlib-lib=/home/lab501/android-cross-compile-16/zlib-1.2.8/_install/lib \
 --with-zlib-include=/home/lab501/android-cross-compile-16/zlib-1.2.8/_install/include

#make

#make install_sw

 jpeg


#!/bin/bash

export TOOLCHAIN=/home/lab501/android-toolchain
export PATH=$TOOLCHAIN/bin:$PATH
export CC=$TOOLCHAIN/bin/arm-linux-androideabi-gcc
export AR=$TOOLCHAIN/bin/arm-linux-androideabi-ar
export RANLIB=$TOOLCHAIN/bin/arm-linux-androideabi-ranlib

./configure \
 --enable-static \
 --disable-shared \
 --host=arm-linux-androideabi \
 --prefix=/home/lab501/jpeg-9/_install

#make and make install

 

libpng


#!/bin/bash

export TOOLCHAIN=/home/lab501/android-toolchain
export PATH=$TOOLCHAIN/bin:$PATH
export CC=$TOOLCHAIN/bin/arm-linux-androideabi-gcc
export CXX=$TOOLCHAIN/bin/arm-linux-androideabi-g++
export LD=$TOOLCHAIN/bin/arm-linux-androideabi-ld
export AR=$TOOLCHAIN/bin/arm-linux-androideabi-ar
export RANLIB=$TOOLCHAIN/bin/arm-linux-androideabi-ranlib

./configure \
 --enable-static \
 --disable-shared \
 --prefix=/home/lab501/libpng-1.6.9/_install \
 --host=arm-linux-androideabi

#make and make install

 

freetype


#!/bin/bash

export TOOLCHAIN=/home/lab501/android-toolchain
export PATH=$TOOLCHAIN/bin:$PATH
export CC=$TOOLCHAIN/bin/arm-linux-androideabi-gcc
export LD=$TOOLCHAIN/bin/arm-linux-androideabi-ld
export AS=$TOOLCHAIN/bin/arm-linux-androideabi-as
export AR=$TOOLCHAIN/bin/arm-linux-androideabi-ar
export CXX=$TOOLCHAIN/bin/arm-linux-androideabi-g++
export RANLIB=$TOOLCHAIN/bin/arm-linux-androideabi-ranlib

#manual copy libpng header file 手動複製 libpng 相關標頭檔 (header file)

./configure \
 --enable-static \
 --disable-shared \
 --host=arm-linux-androideabi \
 --prefix=/home/lab501/freetype-2.5.3/_install \
 --without-zlib \
 --without-png \
 --without-harfbuzz

#make and make install

 

libiconv


#!/bin/bash

export TOOLCHAIN=/home/lab501/android-toolchain
export PATH=$TOOLCHAIN/bin:$PATH
export CC=$TOOLCHAIN/bin/arm-linux-androideabi-gcc
export AR=$TOOLCHAIN/bin/arm-linux-androideabi-ar
export CXX=$TOOLCHAIN/bin/arm-linux-androideabi-g++
export RANLIB=$TOOLCHAIN/bin/arm-linux-androideabi-ranlib

./configure \
 --host=arm-linux-androideabi \
 --target=arm-linux-androideabi \
 --prefix=/home/lab501/libiconv-1.14/_install \
 --enable-static \
 --disable-shared

#make and make install

 

libmcrypt


#!/bin/bash

export TOOLCHAIN=/home/lab501/android-toolchain
export PATH=$TOOLCHAIN/bin:$PATH
export CC=$TOOLCHAIN/bin/arm-linux-androideabi-gcc
export CXX=$TOOLCHAIN/bin/arm-linux-androideabi-g++
export LD=$TOOLCHAIN/bin/arm-linux-androideabi-ld
export RANLIB=$TOOLCHAIN/bin/arm-linux-androideabi-ranlib
export STRIP=$TOOLCHAIN/bin/arm-linux-androideabi-strip
export AR=$TOOLCHAIN/bin/arm-linux-androideabi-ar
export ac_cv_type_unsigned_long_int=no
export ac_cv_sizeof_unsigned_int=no
export ac_cv_sizeof_unsigned_short_int=no
export ac_cv_sizeof_unsigned_char=no
export ac_cv_func_malloc_0_nonnull=yes
export ac_cv_func_realloc_0_nonnull=yes

./configure \
 --prefix=/home/lab501/libmcrypt-2.5.8/_install \
 --host=arm-linux-androideabi \
 --with-mhash \
 --enable-static \
 --disable-shared

#fix some erros

sed -i "s{SIZEOF_UNSIGNED_CHAR no{SIZEOF_UNSIGNED_CHAR 1{" config.h
sed -i "s{SIZEOF_UNSIGNED_INT no{SIZEOF_UNSIGNED_INT 4{" config.h
sed -i "s{SIZEOF_UNSIGNED_LONG_INT 0{SIZEOF_UNSIGNED_LONG_INT 4{" config.h
sed -i "s{SIZEOF_UNSIGNED_SHORT_INT no{SIZEOF_UNSIGNED_SHORT_INT 2{" config.h

#make and make install

 

pcre


#!/bin/bash

export TOOLCHAIN=/home/lab501/android-toolchain
export PATH=$TOOLCHAIN/bin:$PATH
export CC=$TOOLCHAIN/bin/arm-linux-androideabi-gcc
export AR=$TOOLCHAIN/bin/arm-linux-androideabi-ar
export RANLIB=$TOOLCHAIN/bin/arm-linux-androideabi-ranlib
./configure \
 --enable-static \
 --disable-shared \
 --prefix=/home/lab501/pcre-8.34/_install \
 --host=arm-linux-androideabi \
 --disable-cpp

#make and make install

ncurses


#!/bin/bash

export TOOLCHAIN=/home/lab501/android-toolchain
export PATH=$TOOLCHAIN/bin:$PATH
export CC=$TOOLCHAIN/bin/arm-linux-androideabi-gcc
export AS=$TOOLCHAIN/bin/arm-linux-androideabi-as
export AR=$TOOLCHAIN/bin/arm-linux-androideabi-ar
export LD=$TOOLCHAIN/bin/arm-linux-androideabi-ld
export RANLIB=$TOOLCHAIN/bin/arm-linux-androideabi-ranlib
export ac_cv_header_locale_h=no

./configure \
 --host=arm-linux-androideabi \
 --disable-widec \
 --disable-ext-funcs \
 --without-cxx-binding \
 --without-cxx \
 --without-shared \
 --without-ada \
 --without-tests \
 --without-debug \
 --enable-overwrite \
 --prefix=/home/lab501/ncurses-5.9/_install

#make and make install

 

readline


#!/bin/bash

export TOOLCHAIN=/home/lab501/android-toolchain
export PATH=$TOOLCHAIN/bin:$PATH
export CC=$TOOLCHAIN/bin/arm-linux-androideabi-gcc
export AR=$TOOLCHAIN/bin/arm-linux-androideabi-ar
export RANLIB=$TOOLCHAIN/bin/arm-linux-androideabi-ranlib

./configure \
 --host=arm-linux-androideabi \
 --prefix=/home/lab501/readline-6.2/_install \
 --with-curses=/home/lab501/ncurses-5.9/_install \
 --enable-static

#make and make install

 

[ 編譯 libs 常見的錯誤 ]

  1. #include <xxxx.h> no such file or directory

解法找到哪個標頭檔案出問題,接著去尋找檔案,把有問題檔案裡的 #include 到正確的位置。(通常是用到產生的 standalone Android toolchain 裡的檔案)

可以使用下面指令找到相關位置的路徑檔。只截取有 android-toolchain 相關的 ouput strings .

 locate your-file.h | grep android-toolchain 

    2. undefined reference ‘XXX’ :有標頭檔沒有對應到相對應的參數,需要自行增加。(因為有些預設 Android toolchain 本身就沒有這個參數)

    3. ….parameter ‘xxxx’ no such parameters:沒有相對應的參數,這個錯誤目前出現較少。

4. 在 autoconf 時,出現 cannot recognize stsyem name “arm-linux-androideabi” source 裡附的 config.guess 和 config.sub 版本太舊,解決方式是從附的 MEGA 載點下載一個 libs 拿裡面這兩個檔案做覆蓋。

[ OpenSSL 錯誤 ]


../libcrypto.a(eng_rsax.o): In function `e_rsax_bn_mod_exp':
eng_rsax.c:(.text+0x132f): undefined reference to `mod_exp_512'
../libcrypto.a(eng_rsax.o): In function `e_rsax_rsa_mod_exp':
eng_rsax.c:(.text+0x1c08): undefined reference to `mod_exp_512'
eng_rsax.c:(.text+0x22a8): undefined reference to `mod_exp_512'
../libcrypto.a(e_rc4_hmac_md5.o): In function `rc4_hmac_md5_cipher':
e_rc4_hmac_md5.c:(.text+0x44e): undefined reference to `rc4_md5_enc'
e_rc4_hmac_md5.c:(.text+0x4f1): undefined reference to `rc4_md5_enc'
collect2: error: ld returned 1 exit status
make[2]: *** [link_app.] Error 1
make[2]: Leaving directory `/opt/openssl-1.0.1e/apps'
make[1]: *** [openssl] Error 2
make[1]: Leaving directory `/opt/openssl-1.0.1e/apps'
make: *** [build_apps] Error 1

原因是未設定 target 作業系統,設定成:linux-armv4 即可,可以參考前面的 bash script file for OpenSSL .

[ 參考資料 ]

DroidPHP (與PHP 和 lighttpd 相關 dependencies 內容不齊全,可能是作者還正在撰寫中吧,沒有提到相關編譯的錯誤,只有列出需要編譯的 libs)

build-libxm.sh (bash script file 可以做為 auto conf libxml 的參考)

 

[後記]

網路上很多文章就只有提到參數該怎麼設定,很少提到錯誤怎麼解決,這篇文章算是整個整理過後的心得。

萬一自己編譯有問題或是遇到非上述的 general errors 時,可以從提供的 MEGA 載點下載不同版本的 Android toolchain 參考檔案要如何加註與修改 header 檔中的 preprocessor code。