TooChain包括的内容
gcc - C编译器
glibc - C标准库
binutils - 二进制工具集,如ld, ar, as等
kernle header files - Linux内核头文件
这4部分不同的版本的正确组合才能成功构建一个交叉编译器,可参考http://www.kegel.com/crosstool/
几个重要的名词
Target: 嵌入式软件真正运行的机器,不是开发机器,而是目标机器,一般用[arch]-[vendor]-[OS]-[libc]进行标识,
- Common archs: arm, sparc, x86_64, powerpc
- Common vendors: pc, softfloat, unknown
- Common OS: liniux, elf, uclinux
- Common libc: gnu, eabi, uclibc
比如:arm-unknown-linux-gnu。
Sysroot: 目标系统的根目录
ToolChain详细的制作过程
1. 从kernel source package提取出头文件
2. 使用本地gcc编译binutils
3. 使用本地gcc编译出一个简单的不用libc的simple ARM-GCC
4. 使用simple ARM-GCC编译出glibc
5. 使用本地GCC编译出完整版的ARM-GCC
6. 使用完整版的ARM-GCC编译binutils
7.Build gcc,g++
可见,这里涉及到三个GCC,一个是本地GCC,然后是不带libc的简单的ARM-GCC,然后是完整的ARM-GCC。
详细过程如下:
1.
Environment setup
§Define your target
–Export the ARCH part of the target
export ARCH=”arm”
export ARCH=”arm”
–Export the tuple as TARGET
export TARGET=”${ARCH}-unknown-linux-gnu”
export TARGET=”${ARCH}-unknown-linux-gnu”
§Define your build target (ie the target for you PC)
–Export the tuplce as BUILD
export BUILD=”i686-unknown-linux-gnu”
export BUILD=”i686-unknown-linux-gnu”
§Create a directory which will contain our work
–Export this value as PREFIX
export PREFIX=”/usr/${TARGET}”
export PREFIX=”/usr/${TARGET}”
§Create the sysroot
–Export this value as SYSROOT
export SYSROOT=”${PREFIX}/sysroot”
export SYSROOT=”${PREFIX}/sysroot”
2.
Build
binutils with local gcc
1.Download the binutils tarball from ftp.gnu.org
2.
Extract
binutils
to PREFIX/
src
tar -xf binutils-*.tar.gz -C “${PREFIX}/src”
tar -xf binutils-*.tar.gz -C “${PREFIX}/src”
3.
Create the build directory
mkdir -p “${PREFIX}/BUILD/binutils”
mkdir -p “${PREFIX}/BUILD/binutils”
4.
Enter the build directory
cd “${PREFIX}/BUILD/binutils”
cd “${PREFIX}/BUILD/binutils”
5.
Configure
binutils
../../binutils-*/configure \
--prefix=${PREFIX} --target=${TARGET} \
--with-sysroot=${SYSROOT}
../../binutils-*/configure \
--prefix=${PREFIX} --target=${TARGET} \
--with-sysroot=${SYSROOT}
6.
Make
binutils
and install
make && make install
make && make install
3.
Generate
linux
headers
1.Download the Linux kernel source tarball from www.kernel.org
2.
Extract the kernel to PREFIX/
src
tar -xf linux-*.tar.gz -C “${PREFIX}/src”
tar -xf linux-*.tar.gz -C “${PREFIX}/src”
3.
Create the
linux
symlink
cd “${PREFIX}/src”
ln -s “${PREFIX}/src/linux-”* “${PREFIX}/src/linux”
cd “${PREFIX}/src”
ln -s “${PREFIX}/src/linux-”* “${PREFIX}/src/linux”
4.
Enter the
linux
directory
cd “${PREFIX}/src/linux”
cd “${PREFIX}/src/linux”
5.
Generate the headers with the
defconfig
for your platform (or
menuconfig
)
make mts6000_defconfig
make include/linux/version.h
mkdir -p “${SYSROOT}/usr/include”
cp -a include/linux “${SYSROOT}/usr/include”
cp -a include/asm-arm “${SYSROOT}/usr/include”
cp -a include/asm-generic “${SYSROOT}/usr/include”
make mts6000_defconfig
make include/linux/version.h
mkdir -p “${SYSROOT}/usr/include”
cp -a include/linux “${SYSROOT}/usr/include”
cp -a include/asm-arm “${SYSROOT}/usr/include”
cp -a include/asm-generic “${SYSROOT}/usr/include”
4.
Generate
glibc
headers
1.Download the glibc tarball from ftp.gnu.org
2.
Extract
glibc
to PREFIX/
src
tar -xf glibc-*.tar.gz -C “${PREFIX}/src”
tar -xf glibc-*.tar.gz -C “${PREFIX}/src”
3.
Create the build directory
mkdir -p “${PREFIX}/BUILD/glibc-headers”
mkdir -p “${PREFIX}/BUILD/glibc-headers”
4.
Enter the build directory
cd “${PREFIX}/BUILD/glibc-headers”
cd “${PREFIX}/BUILD/glibc-headers”
5.
Configure
glibc
../../glibc-*/configure \
--prefix=/usr –host=${TARGET}\
--with-headers=${SYSROOT}/usr/include
../../glibc-*/configure \
--prefix=/usr –host=${TARGET}\
--with-headers=${SYSROOT}/usr/include
6.
Make the headers
make cross-compiling=yes install_root=${SYSROOT} install-headers
make cross-compiling=yes install_root=${SYSROOT} install-headers
7.
Create empty files needed later (these will be replaced w/ the real build)
touch ${SYSROOT}/usr/include/gnu/stubs.h
touch ${SYSROOT}/usr/include/bits/stdio_lim.h
touch ${SYSROOT}/usr/include/gnu/stubs.h
touch ${SYSROOT}/usr/include/bits/stdio_lim.h
5.
Build
gcc
(needed for
glibc
) with local gcc
1.Download the gcc tarball from ftp.gnu.org
2.
Extract
gcc
to PREFIX/
src
tar -xf gcc-*.tar.gz -C “${PREFIX}/src”
tar -xf gcc-*.tar.gz -C “${PREFIX}/src”
3.
Create the build directory
mkdir -p “${PREFIX}/BUILD/gcc”
mkdir -p “${PREFIX}/BUILD/gcc”
4.
Enter the build directory
cd “${PREFIX}/BUILD/gcc”
cd “${PREFIX}/BUILD/gcc”
5.
You may need to patch
gcc
to generate
ctri.o
and
ctrn.o
needed by
glibc
patch: http://frank.harvard.edu/~coldwell/toolchain/t-linux.diff
patch: http://frank.harvard.edu/~coldwell/toolchain/t-linux.diff
6.
Configure
gcc
to just generate
gcc
(no g++,
gcj
,
etc
)
../../gcc-*/configure \
–prefix=${PREFIX} –target=${TARGET}\
--enable-languages=c --with-sysroot=${SYSROOT}
../../gcc-*/configure \
–prefix=${PREFIX} –target=${TARGET}\
--enable-languages=c --with-sysroot=${SYSROOT}
7.
Make
gcc
and install
make && make install
make && make install
6.
Build
glibc
(needed for g++) with gcc built in the last step
1.Create the build directory
mkdir -p “${PREFIX}/BUILD/glibc”
mkdir -p “${PREFIX}/BUILD/glibc”
2.
Enter the build directory
cd “${PREFIX}/BUILD/glibc”
cd “${PREFIX}/BUILD/glibc”
3.
Reconfigure
glibc
to really build now
export CROSS_COMPILE=”${TARGET}-”
BUILD_CC=gcc CC=${CROSS_COMPILE}gcc \
AR=${CROSS_COMPILE}ar RANLIB=${CROSS_COMPILE}ranlib \
AS=${CROSS_COMPILE}as LD=${CROSS_COMPILE}ld \
../../glibc-*/configure --prefix=/usr –build=${BUILD}\
--host=${TARGET} –target=${TARGET}\
--with-headers=${SYSROOT}/usr/include
export CROSS_COMPILE=”${TARGET}-”
BUILD_CC=gcc CC=${CROSS_COMPILE}gcc \
AR=${CROSS_COMPILE}ar RANLIB=${CROSS_COMPILE}ranlib \
AS=${CROSS_COMPILE}as LD=${CROSS_COMPILE}ld \
../../glibc-*/configure --prefix=/usr –build=${BUILD}\
--host=${TARGET} –target=${TARGET}\
--with-headers=${SYSROOT}/usr/include
4.
Make and install
glibc
make && make install_root=”${SYSROOT}” install
make && make install_root=”${SYSROOT}” install
1.Enter the build directory
cd “${PREFIX}/BUILD/gcc”2.Configure gcc to just generate gcc and g++ (now that we have glibc)
../../gcc-*/configure \
--prefix=${PREFIX} --target=${TARGET} \
--enable-languages=c,c++ --with-sysroot=${SYSROOT}3. Make gcc and install
make && make install
8.Build any other tools: kernel, gdb, libtool, etc