交叉编译器制作流程

由于项目的需要,我们需要一个能在x86平台上运行,生成mips应用程序的交叉编译器,最近几天一直在搞这个,看了很多的文章也试了不少的方法,最后终于成功了,还不错,有些新的和大家交流一下。
 什么是交叉编译器?
    为什么叫“交叉编译器”(cross compiler),就是因为它跨平台来编译程序!做交叉编译器要弄清楚3个概念:host, build, target:
build -- 你在什么平台上编译的这个编译器
host -- 这个编译器将来要在什么平台上运行
target -- 编译器最终会生成在哪个平台上执行的可执行代码
    这里我可以给个例子 build=i386 host=sparc64 target=mips32 表示我们在x86平台上编译了一个在sparc64平台上运行的编译器,它将源码编译生成了要在mips32平台上运行的可执行程序。
描述一个平台,通常使用三元组:arch-vendor-os, 比如i386-redhat-linux 表示x86平台,生产商是redhat,而使用的os是linux。
制作交叉编译器的基本流程
    如果你之前没有做过交叉编译器,那我来告诉你好了,这将是一个痛苦的过程,不要想得像在linux下简单编译个程序那样,你将遇到一个又一个的错误,而且一时之间你解决不了,而且有些根本就不是你的错误,所以要是有现成的,就不要自己去做。
    基本过程分为4步:
编译binutils:这个一般都没有问题的。
编译bootstrap的gcc:这个也比较容易,这一步是为编译glibc准备的
编译glibc:这个最容易出错,因为你需要为它准备kernel的头文件,而且有时候需要打patch
重新编译gcc:告诉配置文件刚才编译的glibc的位置,这样在最终编译一个能够自动找到库和头文件的交叉编译器。
我的实践
    写这篇文站的目的主要是想把这个写出来,以便于以后自己在需要的就不用找那些资料了,照着这个重做一遍就是了,可是转念一想,我做的那些交叉编译器,我已经备份了,而且我的做法就是按照  http://documents.jg555.com/cross-lfs/mips64/ 这上面来的,他们经常有更新,而且解释也比较详细,干脆给大家这个网址,自己按照上面的步骤一步步来就是了,如果上面网址不能访问就动用搜索引擎,搜索“cross-lfs”或者“clfs”,应该能找到。
    这里还是给出我写的一个脚本,首先假定你已经到 http://documents.jg555.com/cross-lfs/ 网站上把所需要的软件包和patch(binutils, gcc, glibc等)都下载下来了,接下来的工作便可以交给我写的这个脚本去完成了,当然你也可以按照你的要求改动一些变量值来满足你。
#!/bin/bash

CROSSTOOL_PATH=/opt/crossgcc
KEREL_PATH=/work/lirui/tarball/crossgcc/linux-2.6.16.8
TARBALL_PATH=/work/lirui/tarball/crossgcc
TOOL_SRC_PATH=/work/lirui/tarball/crossgcc/src
BUILD_PATH=/work/lirui/builds

BINUTILS_VER=binutils-2.16.92
GCC_VER=gcc-4.1.0
GLIBC_VER=glibc-2.4

unset CFLAGS
unset CXXFLAGS
export LFS_HOST="`echo ${MACHTYPE} | sed -e 's/unknown/cross/g' -e 's/-pc-/-cross-/g'`"

# For a MIPS Little Endian Machine:
export LFS_TARGET="mips64el-unknown-linux-gnu"

# For a MIPS Big Endian Machine:
# export LFS_TARGET="mips64-unknown-linux-gnu"

export LFS_TARGET32="`echo ${LFS_TARGET}| sed -e 's/64//g'`"

export BUILD32="-mabi=32"
export BUILDN32="-mabi=n32"
export BUILD64="-mabi=64"

# prepare kernel-headers
cd ${KEREL_PATH}/include
mkdir -pv ${CROSSTOOL_PATH}/include
cp -Rv asm-mips ${CROSSTOOL_PATH}/include/asm
cp -Rv asm-generic ${CROSSTOOL_PATH}/include
cp -Rv linux ${CROSSTOOL_PATH}/include

# step 1) build binutils
cd {TOOL_SRC_PATH}
tar jxf {TARBALL_PATH}/{BINUTILS_VER}.tar.bz2
cd ${TOOL_SRC_PATH}/${BINUTILS_VER}
patch -Np1 -i ${TARBALL_PATH}/patches/${BINUTILS_VER}-posix-1.patch
patch -Np1 -i ${TARBALL_PATH}/patches/${BINUTILS_VER}-genscripts_multilib-1.patch

mkdir -pv ${BUILD_PATH}/build-binutils

cd ${BUILD_PATH}/build-binutils
${TOOL_SRC_PATH}/${BINUTILS_VER}/configure --prefix=${CROSSTOOL_PATH} /
   --host=${LFS_HOST} --target=${LFS_TARGET} --with-lib-path=${CROSSTOOL_PATH}/lib /
   --disable-nls --enable-shared --enable-64-bit-bfd
   
make configure-host
make 
if [ $? == "" ]; then
        echo "build binutils successfully!"
        cp ${TOOL_SRC_PATH}/${BINUTILS_VER}/include/libiberty.h ${CROSSTOOL_PATH}/include
        make install
else
        echo "build binutils failed!"
        exit
fi
export PATH=${CROSSTOOL_PATH}/bin:${PATH}
# step 2) build bootstrap gcc
cd ${TOOL_SRC_PATH}
tar jxvf  ${TARBALL_PATH}/${GCC_VER}.tar.bz2
cd ${TOOL_SRC_PATH}/${GCC_VER}
patch -Np1 -i ${TARBALL_PATH}/patches/${GCC_VER}-specs-1.patch
patch -Np1 -i ${TARBALL_PATH}/patches/${GCC_VER}-posix-1.patch
patch -Np1 -i ${TARBALL_PATH}/patches/${GCC_VER}-cross_search_paths-1.patch

echo "
#undef STARTFILE_PREFIX_SPEC
#define STARTFILE_PREFIX_SPEC /"/opt/crossgcc/lib//"" >> gcc/config/linux.h

cp -v gcc/Makefile.in{,.orig}
sed -e "s@/(^CROSS_SYSTEM_HEADER_DIR =/).*@/1 /opt/crossgcc/include@g" /
 gcc/Makefile.in.orig > gcc/Makefile.in

mkdir -pv ${BUILD_PATH}/build-gcc
cd ${BUILD_PATH}/build-gcc
${TOOL_SRC_PATH}/${GCC_VER}/configure --prefix=${CROSSTOOL_PATH} /
   --host=${LFS_HOST} --target=${LFS_TARGET} --with-local-prefix=${CROSSTOOL_PATH} /
   --disable-nls --disable-shared --disable-threads /
   --enable-languages=c

make all-gcc

if [ $? == "" ]; then
        echo "build bootstrap gcc successfully!"
        make install-gcc
else
        echo "build bootstrap gcc failed!"
        exit
fi

# step 3) build glibc abi=32
cd ${TOOL_SRC_PATH}
tar ${TARBALL_PATH}/${GLIBC_VER}.tar.bz2
cd ${TOOL_SRC_PATH}/${GLIBC_VER}
tar ${TARBALL_PATH}/glibc-ports-2.4.tar.bz2
mv -v glibc-ports-2.4 ports

patch -Np1 -i ${TARBALL_PATH}/patches/${GLIBC_VER}-mips_fixes-1.patch
patch -Np1 -i ${TARBALL_PATH}/patches/${GLIBC_VER}-libgcc_eh-1.patch
patch -Np1 -i ${TARBALL_PATH}/patches/${GLIBC_VER}-localedef_segfault-1.patch

cp -v nscd/Makefile{,.orig}
sed -e "/nscd_stat.o: sysincludes = # nothing/d" nscd/Makefile.orig > /
    nscd/Makefile

mkdir -pv ${BUILD_PATH}/build-glibc
cd ${BUILD_PATH}/build-glibc
echo "libc_cv_forced_unwind=yes" > config.cache
echo "libc_cv_c_cleanup=yes" >> config.cache
BUILD_CC="gcc" CC="${LFS_TARGET}-gcc ${BUILD32}" /
    AR="${LFS_TARGET}-ar" RANLIB="${LFS_TARGET}-ranlib" /
    ${TOOL_SRC_PATH}/${GLIBC_VER}/configure --prefix=${CROSSTOOL_PATH} /
    --host=${LFS_TARGET32} --build=${LFS_HOST} /
    --disable-profile --enable-add-ons /
    --with-tls --enable-kernel=2.6.0 --with-__thread /
    --with-binutils=${CROSSTOOL_PATH}/bin --with-headers=${CROSSTOOL_PATH}/include /
    --cache-file=config.cache 

make 
if [ $? == "" ]; then
        echo "build glibc abi=32 successfully!"
        make install
else
        echo "build glibc abi=32 failed!"
        exit
fi

# step 4) build glibc abi=n32
cd ${BUILD_PATH}/build-glibc && rm -rf ./* 
echo "libc_cv_forced_unwind=yes" > config.cache
echo "libc_cv_c_cleanup=yes" >> config.cache
echo "slibdir=/opt/crossgcc/lib32" >> configparms
BUILD_CC="gcc" CC="${LFS_TARGET}-gcc ${BUILDN32}" /
    AR="${LFS_TARGET}-ar" RANLIB="${LFS_TARGET}-ranlib" /
    ${TOOL_SRC_PATH}/${GLIBC_VER}/configure --prefix=${CROSSTOOL_PATH} /
    --host=${LFS_TARGET32} --build=${LFS_HOST} /
    --disable-profile --enable-add-ons /
    --with-tls --enable-kernel=2.6.0 --with-__thread /
    --with-binutils=${CROSSTOOL_PATH}/bin --with-headers=${CROSSTOOL_PATH}/include /
    --cache-file=config.cache --libdir=${CROSSTOOL_PATH}/lib32

make 
if [ $? == "" ]; then
        echo "build glibc abi=n32 successfully!"
        make install
else
        echo "build glibc abi=n32 failed!"
        exit
fi

# step 5) build glibc abi=64
cd ${BUILD_PATH}/build-glibc && rm -rf ./* 
echo "libc_cv_forced_unwind=yes" > config.cache
echo "libc_cv_c_cleanup=yes" >> config.cache
echo "slibdir=/crossgcc/lib64" >> configparms
BUILD_CC="gcc" CC="${LFS_TARGET}-gcc ${BUILD64}" /
    AR="${LFS_TARGET}-ar" RANLIB="${LFS_TARGET}-ranlib" /
    ${TOOL_SRC_PATH}/${GLIBC_VER}/configure --prefix=${CROSSTOOL_PATH} /
    --host=${LFS_TARGET32} --build=${LFS_HOST} /
    --disable-profile --enable-add-ons /
    --with-tls --enable-kernel=2.6.0 --with-__thread /
    --with-binutils=${CROSSTOOL_PATH}/bin --with-headers=${CROSSTOOL_PATH}/include /
    --cache-file=config.cache --libdir=${CROSSTOOL_PATH}/lib64

make 
if [ $? == "" ]; then
        echo "build glibc abi=64 successfully!"
        make install
else
        echo "build glibc abi=64 failed!"
        exit
fi

# step 6) rebuild gcc finally
cd ${TOOL_SRC_PATH} && rm -rf ${GCC_VER}
tar jxvf  ${TARBALL_PATH}/${GCC_VER}.tar.bz2
cd ${TOOL_SRC_PATH}/${GCC_VER}
patch -Np1 -i ${TARBALL_PATH}/patches/${GCC_VER}-PR20425-1.patch
patch -Np1 -i ${TARBALL_PATH}/patches/${GCC_VER}-specs-1.patch
patch -Np1 -i ${TARBALL_PATH}/patches/${GCC_VER}-posix-1.patch
patch -Np1 -i ${TARBALL_PATH}/patches/${GCC_VER}-cross_search_paths-1.patch

echo "
#undef STARTFILE_PREFIX_SPEC
#define STARTFILE_PREFIX_SPEC /"/opt/crossgcc/lib32//"" >> gcc/config/linux.h

cp -v gcc/Makefile.in{,.orig}
sed -e "s@/(^CROSS_SYSTEM_HEADER_DIR =/).*@/1 /opt/crossgcc/include@g" /
 gcc/Makefile.in.orig > gcc/Makefile.in

mkdir -pv ${BUILD_PATH}/build-gcc
cd ${BUILD_PATH}/build-gcc
${TOOL_SRC_PATH}/${GCC_VER}/configure --prefix=${CROSSTOOL_PATH} /
   --host=${LFS_HOST} --target=${LFS_TARGET} --with-local-prefix=${CROSSTOOL_PATH} /
   --disable-nls --enable-shared --enable-threads=posix /
   --enable-__cxa_atexit --enable-c99 --enable-long-long /
   --enable-languages="c,c++,fortran"

make AS_FOR_TARGET="${LFS_TARGET}-as" LD_FOR_TARGET="${LFS_TARGET}-ld"

if [ $? == "" ]; then
        echo "build gcc successfully!"
        make install
else
        echo "build gcc failed!"
        exit
fi

echo "++++++++++++++++++++ALL IS DONE!+++++++++++++++++++++++++++"
网络资源
如果你用google或者百度进行搜索“交叉编译器”这个关键字的话,能够找到很多的文章,但是可以告诉你的是,大部分对于你来说都没有用,也包括我这篇(除非你的环境和我很相似)。但是还是要给大家一个网络资源的列表,多少还是能有些帮助的:
http://www.linux-mips.org/ 这个站点维护了linux对mips架构的支持,以及对于mips各类cpu的介绍。
http://trac.cross-lfs.org/ 这个站点会告诉你如何从源码编译一个linux系统出来,当然也包括了如何制作交叉编译器
http://documents.jg555.com/cross-lfs/mips64-64/index.html Cross-Compiled Linux From Scratch 这个站点比较好,我的制作流程就是按照这个来的
http://documents.jg555.com/cross-lfs/mips64/ 这个讲述了如何制作支持多个abi标注的库的交叉编译器
http://kegel.com/crosstool/ 这个网站提供了一个自动化的工具crosstool,他可以自动帮你,但是对于我好像是不好使的,如果对于你能用的话,那是再好不过的了。
http://vmlinux.org/crash/mirror/www.objsw.com/CrossGCC/ crossGcc
--------------------- 
作者:colin719 
来源:CSDN 
原文:https://blog.csdn.net/colin719/article/details/758000 
版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(MakeFile)