1. 程式人生 > >交叉編譯器製作流程

交叉編譯器製作流程

#!/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 "
[email protected]
/(^CROSS_SYSTEM_HEADER_DIR =/).*@/1 /opt/crossgcc/[email protected]" /
 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 "
[email protected]
/(^CROSS_SYSTEM_HEADER_DIR =/).*@/1 /opt/crossgcc/[email protected]" /
 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!+++++++++++++++++++++++++++"