星期三, 3月 07, 2007

Cross compiler

最近弄cross compiler真是苦的要命。
簡單說,要build一個cross compiler,必需對gcc、binutils有相當程度的了解。
還有,要編譯前可不是打一個make下去就好了。make前的configure也是很麻煩。不管是binutils還是gcc,組態的參數超多,只能抱著網上的文件,慢慢k了。
在來就是make的問題了,在make前許多事前的準備工作都要作好,尤其是configure時設的選項一定要懂,不然make到一半才知道問題,那幾乎就要重來。
不過呢?重來倒也還好,重點是「時間」。
編譯binutils大概十來分鐘,但是,編譯gcc就不是十來分鐘就解決的事情。我只要c,c++的compiler編譯時間大概就要3個半小時,約200分鐘。漫長的等待是無所謂,怕就怕編譯到一半,突然停了,make error在去debug或是找patch那就真的麻煩了。
我的心得是,要善用google跟mailing list,通常比較容易找的到答案。
舉例來說,在編譯gcc時,我參考別人configure的參數--with-newlib,其實我一開始也不知道是什麼,而且gcc的文件說明我看了等於沒看,因為上面給的訊息太少了。經過幾次make error之後(我的數十個小時啊…),我開始懷疑是newlib的問題,這時去找才發現,原來newlib是一個給embedded system用的standard c library。但是問題並沒有完全解決。因為在編譯gcc 4.1.2+newlib 1.15.0時,居然make不下去。還好用google找到newlib的mailing list,也有人有同樣的問題,需要patch gcc的Makefile.in才行。
Anyway,問題已經解決了。
在我還沒有找到適合的網路空間時,我就先把解決方法貼上來好了。
====README================================================
This directory contains source code of cross compiler tool-chain.
The following verisions we used:

GCC 4.1.2
Binutils 2.17
Newlib 1.15.0

Directory Structure
===================
README: This document
build.script: Toolchain building script, use --help to get help information.
src/: Source code directory
binutils/: Symbolic link to binutils' source directory
gcc/: Symbolic link to gcc's source directory


Toolchain building steps:(gcc 4.1.2, binutils 2.17, newlib 1.15.0)
Step 1: Fecth source from the internet
You can use following commands to get the source code.
wget ftp://ftp.gnu.org/pub/gnu/binutils/binutils-2.17.tar.bz2
wget ftp://ftp.gnu.org/pub/gnu/gcc/gcc-4.1.2/gcc-4.1.2.tar.bz2
wget ftp://sourceware.org/pub/newlib/newlib-1.15.0.tar.gz

Step 2: Uncompress tarball
You can use following commands to uncompress source code tarball.
cd src
tar zxvf filename.gz or tar jxvf filename.bz2

Step 3: Establish symbolic link for each feather under directory src.
ln -s gcc-4.1.2/ gcc
ln -s binutils-2.17 binutils

Step 4: Deal with newlib's related things...
When we compile gcc we will enable option '--with-newlib', we shall add two
symbolic link under gcc directory.
cd gcc
ln -s ../newlib-1.15.0/newlib newlib
ln -s ../newlib-1.15.0/libgloss libgloss

Step 5: Patch gcc's Makefile.in
If we don't modify the Makefile.in, error occurs when we compile newlib.
-CFLAGS_FOR_TARGET = -O2 $(CFLAGS) $(SYSROOT_CFLAGS_FOR_TARGET)
+CFLAGS_FOR_TARGET = $(strip -O2 $(CFLAGS) $(SYSROOT_CFLAGS_FOR_TARGET))

-CXXFLAGS_FOR_TARGET = $(CXXFLAGS) $(SYSROOT_CFLAGS_FOR_TARGET)
+CXXFLAGS_FOR_TARGET = $(strip $(CXXFLAGS) $(SYSROOT_CFLAGS_FOR_TARGET))

Step 6: Build the toolchain
It will cost 12 minutes for binutils and 3 and a half hours for gcc and newlib.
Of cource, it depends on your machine's speed.
./build.script

==build.script======================================================
# Parameter settings
LANG=en
export LANG
TARGET=mips-elf
PREFIX=/opt/mycrosstools/mips
PROJDIR=$PWD
CONFIGBINLOG=configbin.log
MAKEBINLOG=makebin.log
CONFIGGCCLOG=configgcc.log
MAKEGCCLOG=makegcc.log

mkdir -p tmp/build/binutils
mkdir -p tmp/build/gcc

echo "Building Binutils Starts..."
cd tmp/build/binutils
date>$MAKEBINLOG

$PROJDIR/src/binutils/configure --target=$TARGET --prefix=$PREFIX 2>&1|tee $CONFIGBINLOG
make all install 2>&1| tee $MAKEBINLOG
date>>$MAKEBINLOG

cd $PROJDIR/tmp/build/gcc

PATH=$PREFIX/bin:$PATH
export PATH

date>$MAKEGCCLOG

$PROJDIR/src/gcc/configure --target=$TARGET --prefix=$PREFIX --disable-shared --enable-languages=c,c++ --with-gnu-as --with-gnu-ld --with-newlib --with-gxx-include-dir=$PREFIX/mips-elf/include 2>&1|tee $CONFIGGCCLOG
make all install 2>&1| tee $MAKEGCCLOG
date>>$MAKEGCCLOG
echo "DONE!!"

1 則留言:

匿名 提到...

真的是很辛苦啊,你要加油哦