免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1005 | 回复: 0
打印 上一主题 下一主题

用uClibc库的.sh脚本 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-09-21 10:58 |只看该作者 |倒序浏览
#!/bin/sh
#
# This script builds the m68k-elf- or arm-elf- toolchain for use
# with uClinux.  It can be used to build for almost any architecture
# without too much change.
#
# Before running you will need to obtain (if you don't have them in this
# directory):
#
#    binutils-2.11.2.tar.bz2       in current directory (or a gzipped version)
#    binutils-2.11.2-full.patch    in current directory
#    ${THEGCC}.tar.bz2            in current directory (or a gzipped version)
#    ${THEGCC}-full.patch         in current directory
#    ${THEGCC}-arm-pic.patch      in current directory
#    ${THEGCC}-arm-pic.patch2     in current directory
#    ${THEGCC}-arm-mlib.patch     in current directory
#    ${THEGCC}-sigset.patch       in current directory
#    ${THEELF2FLT}.tar.gz       in current directory
#    ${THEGENROMFS}.tar.gz         in current directory
#
# You will also need
#
#    a current uClibc tree from opensource.lineo.com
#    a current uClinux kernel (2.0/2.4) from cvs.uclinux.org
#    to change the EDIT section below appropriately
#    to be root in order to run this script
#
# You can link the uClibc and uClinux-2.0.x or uClinux-2.4.x dirs into the
# current directory or change the values below.
#
# This script:
#
# DOES build all the gcc tools/libraries
#
# DOES NOT build target specific versions of libc but provides a mechanism
#          to do this with the "uclibc" argument if that is what you want.
#
# You must be root to run this script correctly.
#
# To build everything run "./build-uclinux-tools.sh build 2>&1 | tee errs"
#
# WARNING: it removes all current tools from ${PREFIXDIR},  so back them up
#          first :-)
#
# Copyright (C) 2001 David McCullough
#
#############################################################
#
# our build starts here
#
BASEDIR="`pwd`"
#############################################################
#
# EDIT these to suit your system and source locations
#
MAKE=make
UCLIBC="$BASEDIR/../snapgear-p34b/uClibc"
KERNEL="$BASEDIR/../snapgear-p34b/linux-2.0.x"
#my#################################################
PREFIXDIR=/home/friky/toolchain
THEBINUTILS=binutils-2.16.1
THEGCC=gcc-4.0.0
THEELF2FLT=elf2flt-20041205
THEGENROMFS=genromfs-0.5.1
PATH=${PREFIXDIR}/bin:$PATH
export PATH
#my#############################################
#TARGET=m68k-elf
#TARGET=arm-elf
#TARGET=sh-linux
TARGET=sparc-linux
#############################################################
#
# mark stage done
#
mark()
{
    echo "STAGE $1 - complete"
    touch "$BASEDIR/STAGE$1"
}
#
# check if stage should be built
#
schk()
{
    echo "--------------------------------------------------------"
    [ -f "$BASEDIR/STAGE$1" ] && echo "STAGE $1 - already built" && return 1
    echo "STAGE $1 - needs building"
    echo "ARCH=${_CPU}${NOMMU}"
    echo "PATH=${PATH}"
    return 0
}
#
# extract most XYZ format files
#
extract()
{
    for i in "$@"; do
        case "$i" in
        *.tar.gz|*.tgz)   tar xzf "$i" ;;
        *.tar.bz2|*.tbz2) bunzip2 &2
            return 1
            ;;
        esac
    done
    return 0
}
#############################################################
#
# clean any previous runs, extract some stuff
#
stage1()
{
    schk 1 || return 0
    rm -rf ${THEBINUTILS}
    rm -rf ${THEGCC}
    rm -rf ${PREFIXDIR}/${TARGET}
    rm -rf ${PREFIXDIR}/lib/gcc-lib/${TARGET}
    rm -rf ${PREFIXDIR}/bin/${TARGET}*
#
#    extract binutils, gcc and anything else we know about
#
    extract ${THEBINUTILS}.*
    extract ${THEGCC}.*
#
#    apply any patches
#
#    patch -p0  Config
    rm -f ${KERNEL}/include/asm
    ln -s ${KERNEL}/include/asm-${_CPU}${NOMMU} ${KERNEL}/include/asm
    #${MAKE} uClibc_config.h CROSS="${TARGET}-"
    #chmod 644 uClibc_config.h
    rm -rf ${PREFIXDIR}/${TARGET}/sys-include
    rm -f ${UCLIBC}/include/asm
    cp -r ${UCLIBC}/include ${PREFIXDIR}/${TARGET}/sys-include
    rm -rf ${PREFIXDIR}/${TARGET}/sys-include/asm
    rm -rf ${PREFIXDIR}/${TARGET}/sys-include/bits
    cp -r ${UCLIBC}/libc/sysdeps/linux/${_CPU}/bits \
                    ${PREFIXDIR}/${TARGET}/sys-include/.
    #cp uClibc_config.h ${PREFIXDIR}/${TARGET}/sys-include/bits/.
    cp ${KERNEL}/../uClibc/include/bits/uClibc_config.h ${PREFIXDIR}/${TARGET}/sys-include/bits/.
    rm -rf ${PREFIXDIR}/${TARGET}/sys-include/linux
    cp -r ${KERNEL}/include/linux ${PREFIXDIR}/${TARGET}/sys-include/linux
    touch ${PREFIXDIR}/${TARGET}/sys-include/linux/autoconf.h
    cp -r ${KERNEL}/include/asm-${_CPU}${NOMMU} \
                    ${PREFIXDIR}/${TARGET}/sys-include/.
    # 2.4 headers also need this (may not be there for some archs)
    cp -r ${KERNEL}/include/asm-${_CPU} ${PREFIXDIR}/${TARGET}/sys-include/.  ||\
            true
    ln -s ${PREFIXDIR}/${TARGET}/sys-include/asm-${_CPU}${NOMMU} \
                    ${PREFIXDIR}/${TARGET}/sys-include/asm
   
    case ${TARGET} in
    arm*)
        ln -s ${PREFIXDIR}/${TARGET}/sys-include/asm-${_CPU}${NOMMU}/proc-armv \
                        ${PREFIXDIR}/${TARGET}/sys-include/asm/proc
        ;;
    esac
    #
    # clean out any CVS files,  don't fail on this one
    #
    set +e
    find ${PREFIXDIR}/${TARGET}/sys-include -name CVS | xargs rm -rf
    set -e
    mkdir -p ${PREFIXDIR}/lib/gcc-lib || true
    chmod 755 ${PREFIXDIR}/lib/gcc-lib
    cd $BASEDIR
    mark 3
}
#############################################################
#
# first pass,  just the C compiler so we can build uClibc
#
stage4()
{
    schk 4 || return 0
    rm -rf ${TARGET}-gcc
    mkdir ${TARGET}-gcc
    cd ${TARGET}-gcc
    ../${THEGCC}/configure --enable-languages=c --target=${TARGET} --prefix=${PREFIXDIR} --disable-threads \
        --disable-shared --without-headers
    ${MAKE}
    ${MAKE} install
    cd $BASEDIR
    mark 4
}
#############################################################
#
# build uCLibc with first pass compiler
#
stage5()
{
    schk 5 || return 0
    cd ${UCLIBC}/.
    #fix_uclibc_config  Config
    rm -f ${KERNEL}/include/asm
    ln -s ${KERNEL}/include/asm-${_CPU}${NOMMU} ${KERNEL}/include/asm
    ${MAKE} clean CROSS="${TARGET}-"
    ${MAKE} CROSS="${TARGET}-"
    cd $BASEDIR
    mark 5
}
#############################################################
#
# second pass,  build everything,  all compilers of use :-)
#
stage6()
{
    schk 6 || return 0
    #
    # We need these files for the configure parts of this stage
    #
    cp ${UCLIBC}/lib/libc.a ${PREFIXDIR}/${TARGET}/lib/.
    cp ${UCLIBC}/lib/crt0.o ${PREFIXDIR}/${TARGET}/lib/.
    rm -rf ${TARGET}-gcc
    mkdir ${TARGET}-gcc
    cd ${TARGET}-gcc
    case "${TARGET}" in
    arm-*) ar rv ${PREFIXDIR}/${TARGET}/lib/libg.a ;; # create if not there
    esac
    case "${TARGET}" in
    sh-*)
        ../${THEGCC}/configure --enable-languages=c --target=${TARGET} \
                --enable-multilib --prefix=${PREFIXDIR}
        ${MAKE} LIBS=-lc
        ${MAKE} install
        ;;
    *)
        ../${THEGCC}/configure --enable-languages=c,c++ --target=${TARGET} \
                --enable-multilib --prefix=${PREFIXDIR}
        ${MAKE} LIBS=-lc
        #
        # Make sure the multilib directories exist, the ARM install misses
        # these for some reason
        #
        for lib in libio libiostream libstdc++
        do
            find ${TARGET} -name $lib.a -print | while read file
            do
                MLIB=`expr $file : "${TARGET}\(.*\)"`
                MLIB=`expr $MLIB : "\(.*\)/[^/]*/$lib.a"`
                if [ ! -d "${PREFIXDIR}/lib/gcc-lib/${TARGET}/2.95.3/$MLIB" ]
                then
                    echo "Fixing ${PREFIXDIR}/lib/gcc-lib/${TARGET}/2.95.3/$MLIB"
                    mkdir -p "${PREFIXDIR}/lib/gcc-lib/${TARGET}/2.95.3/$MLIB"
                    chmod 755 "${PREFIXDIR}/lib/gcc-lib/${TARGET}/2.95.3/$MLIB"
                fi
            done || exit 1
        done || exit 1
        ${MAKE} install
        #
        # for some reason "make install" doesn't install all the multilib
        # libraries in the right place.  We have to do this ourselves.
        # the directories should exist, so failing if they don't is ok
        #
        for lib in libio libiostream
        do
            echo "Manual install of $lib.a ..."
            find ${TARGET} -name $lib.a -print | while read file
            do
                MLIB=`expr $file : "${TARGET}\(.*\)"`
                MLIB=`expr $MLIB : "\(.*\)/[^/]*/$lib.a"`
                echo "  $MLIB/$lib.a"
                cp $file ${PREFIXDIR}/${TARGET}/lib/$MLIB/$lib.a || exit 1
            done || exit 1
        done || exit 1
        ;;
    esac
    #
    # Don't leave these around as they will not work for all targets
    # If you want uClibc-multilib,  build that later
    #
    rm -f ${PREFIXDIR}/${TARGET}/lib/libc.a
    rm -f ${PREFIXDIR}/${TARGET}/lib/crt0.o
    cd $BASEDIR
    mark 6
}
#############################################################
#
# build genromfs
#
stage7()
{
    schk 7 || return 0
    rm -rf ${THEGENROMFS}
    extract ${THEGENROMFS}*
    cd ${THEGENROMFS}
    ${MAKE}
    cp genromfs ${PREFIXDIR}/bin/.
    chmod 755 ${PREFIXDIR}/bin/genromfs
    cd $BASEDIR
    mark 7
}
#############################################################
#
# build elf2flt
#
stage8()
{
    schk 8 || return 0
    rm -rf ${THEELF2FLT}
    extract ${THEELF2FLT}*
    cd ${THEELF2FLT}
    mv Makefile Makefile.tmp
    echo "EXTRA_CFLAGS=-DTARGET_${_CPU}" > Makefile
    sed "s?^[     ]*PREFIX.*=.*\$?PREFIX=../${TARGET}-binutils?" \
                        > Makefile
    cp $KERNEL/include/linux/flat.h .
    ${MAKE}
    cp flthdr ${PREFIXDIR}/bin/${TARGET}-flthdr
    chmod 755 ${PREFIXDIR}/bin/${TARGET}-flthdr
    ln -f ${PREFIXDIR}/bin/${TARGET}-flthdr ${PREFIXDIR}/bin/flthdr
    cp elf2flt ${PREFIXDIR}/bin/${TARGET}-elf2flt
    ln -f ${PREFIXDIR}/bin/${TARGET}-elf2flt ${PREFIXDIR}/bin/elf2flt
    ln -f ${PREFIXDIR}/bin/${TARGET}-elf2flt ${PREFIXDIR}/${TARGET}/bin/elf2flt
    chmod 755 ${PREFIXDIR}/bin/elf2flt
    [ -f ${PREFIXDIR}/bin/${TARGET}-ld.real ] || \
        cp ${PREFIXDIR}/bin/${TARGET}-ld ${PREFIXDIR}/bin/${TARGET}-ld.real
    [ -f ${PREFIXDIR}/${TARGET}/bin/ld.real ] || \
        cp ${PREFIXDIR}/${TARGET}/bin/ld ${PREFIXDIR}/${TARGET}/bin/ld.real
    cp ld-elf2flt ${PREFIXDIR}/bin/${TARGET}-ld
    cp ld-elf2flt ${PREFIXDIR}/${TARGET}/bin/ld
    cp elf2flt.ld ${PREFIXDIR}/${TARGET}/lib/.
    cd $BASEDIR
    mark 8
}
#############################################################
#
# build multilib versions of uCLibc for a fuller install
#
build_uclibc_mlib()
{
    # set -x
    cd ${UCLIBC}/.
    case "${_CPU}" in
    m68k*) ALL_BUILDS="-Wa,--bitwise-or -D__linux__=1" ;;
    *)     ALL_BUILDS="-D__linux__=1" ;;
    esac
    (
        echo ". $ALL_BUILDS"
        case "${_CPU}" in
        m68k*)
            echo "msoft-float       false $ALL_BUILDS -msoft-float"
            echo "m5200             false $ALL_BUILDS -m5200 -Wa,-m5200"
            echo "m5200/msep-data   true  $ALL_BUILDS -m5200 -Wa,-m5200 -msep-data"
            echo "m5307             false $ALL_BUILDS -m5307 -Wa,-m5307"
            echo "m5307/msep-data   true  $ALL_BUILDS -m5307 -Wa,-m5307 -msep-data"
            echo "m68000            false $ALL_BUILDS -m68000"
            echo "m68000/msep-data  true  $ALL_BUILDS -m68000 -msep-data"
            echo "mcpu32            false $ALL_BUILDS -mcpu32"
            echo "mcpu32/msep-data  true  $ALL_BUILDS -mcpu32 -msep-data"
            ;;
        arm*)
            echo "fpic                           true $ALL_BUILDS -fpic"
            echo "mapcs-26                       false $ALL_BUILDS -mapcs-26"
            echo "fpic/mapcs-26                  true $ALL_BUILDS -fpic -mapcs-26"
            echo "fpic/mapcs-26/msingle-pic-base true $ALL_BUILDS -fpic -mapcs-26 -msingle-pic-base"
            echo "mbig-endian/fpic                           true $ALL_BUILDS -fpic -mbig-endian"
            echo "mbig-endian/mapcs-26                       false $ALL_BUILDS -mapcs-26 -mbig-endian"
            echo "mbig-endian/fpic/mapcs-26                  true $ALL_BUILDS -fpic -mapcs-26 -mbig-endian"
            echo "mbig-endian/fpic/mapcs-26/msingle-pic-base true $ALL_BUILDS -fpic -mapcs-26 -msingle-pic-base -mbig-endian"
            ;;
        sh*)
            # don't know what is needed here yet
            ;;
        esac
    ) | while read mlibdir pic cflags
    do
        fix_uclibc_config $pic  Config
        ${MAKE} clean CROSS="${TARGET}-"
        ${MAKE} CROSS="${TARGET}-"
        cp lib/crt0.o ${PREFIXDIR}/${TARGET}/lib/$mlibdir/crt0.o || exit 1
        cp lib/libc.a ${PREFIXDIR}/${TARGET}/lib/$mlibdir/libc.a || exit 1
        cp lib/libcrypt.a ${PREFIXDIR}/${TARGET}/lib/$mlibdir/libcrypt.a || exit 1
        cp lib/libm.a ${PREFIXDIR}/${TARGET}/lib/$mlibdir/libm.a || exit 1
        cp lib/libresolv.a ${PREFIXDIR}/${TARGET}/lib/$mlibdir/libresolv.a || \
                exit 1
        cp lib/libutil.a ${PREFIXDIR}/${TARGET}/lib/$mlibdir/libutil.a || exit 1
        chmod 644 ${PREFIXDIR}/${TARGET}/lib/$mlibdir/libc.a || exit 1
        chmod 644 ${PREFIXDIR}/${TARGET}/lib/$mlibdir/crt0.o || exit 1
    done
    cd $BASEDIR
}
#############################################################
#
# tar up everthing we have built
#
build_tar_file()
{
    # set -x
    cd /
    #
    # strip the binaries,  make sure we don't strip the libraries (some
    # platforms allow this :-(
    #
    strip ${PREFIXDIR}/bin/genromfs > /dev/null 2>&1 || true
    strip ${PREFIXDIR}/bin/${TARGET}-* > /dev/null 2>&1 || true
    strip ${PREFIXDIR}/${TARGET}/bin/* > /dev/null 2>&1 || true
    strip ${PREFIXDIR}/lib/gcc-lib/${TARGET}/2.95.3/*[!a] > /dev/null 2>&1 || true
    #
    # tar it all up
    #
    tar cvzf $BASEDIR/${TARGET}-tools-`date +%Y%m%d`.tar.gz \
        .${PREFIXDIR}/${TARGET} \
        .${PREFIXDIR}/lib/gcc-lib/${TARGET} \
        .${PREFIXDIR}/include/g++-3 \
        .${PREFIXDIR}/bin/${TARGET}-* \
        .${PREFIXDIR}/bin/genromfs \
        .${PREFIXDIR}/bin/elf2flt \
        .${PREFIXDIR}/bin/flthdr
    cd $BASEDIR
}
#############################################################
#
# cleanup
#
clean_all()
{
    echo "Cleaning everything up..."
    rm -f $BASEDIR/STAGE*
    rm -rf ${THEBINUTILS}
    rm -rf ${THEGCC}
    rm -rf ${THEGENROMFS}
    rm -rf ${THEELF2FLT}
    rm -rf ${TARGET}-gcc
    rm -rf ${TARGET}-binutils
}
#############################################################
#
# main - put everything together in order.
#
# Some setup
#
case ${TARGET} in
m68k*) _CPU=m68k; NOMMU=nommu ;;
arm*)  _CPU=arm;  NOMMU=nommu ;;
sh*)   _CPU=sh;   NOMMU=      ;;
sparc*)    _CPU=sparc;    NOMMU=nommu ;;
esac
#
# first check some args
#
case "$1" in
build)
    rm -f $BASEDIR/STAGE*
    ;;
continue)
    # do nothing here
    ;;
uclibc)
    build_uclibc_mlib
    exit 0
    ;;
tar)
    build_tar_file
    exit 0
    ;;
clean)
    clean_all
    exit 0
    ;;
*)
    echo "usage: $0 (build|continue|clean)" >&2
    echo ""
    echo "       build    = build everything from scratch."
    echo "       continue = continue building from last error."
    echo "       uclibc   = build multilib versions of uClibc (if wanted)."
    echo "       tar      = build for distribution of binaries."
    echo "       clean    = clean all temporary files etc."
    exit 1
    ;;
esac
#
# You have to root for this one
#
if id | grep root > /dev/null
then
    echo "Good, you are root :-)"
else
    echo "Bad,  you are not root."
    #exit 1
fi
if [ ! -f ${KERNEL}/include/linux/version.h -o \
        ! -f ${KERNEL}/include/linux/autoconf.h ]; then
    echo "Your kernel is not configured, cannot continue." >&2
    echo "The following files do not exist:"
    echo
    echo "    ${KERNEL}/include/linux/version.h"
    echo "    ${KERNEL}/include/linux/autoconf.h"
    echo
    echo "These are need by the build.  You should do the following:"
    echo
    echo "    cd ${KERNEL}"
    echo "    make ARCH=${_CPU}${NOMMU} oldconfig"
    echo "    make dep"
    echo
    echo "You should then be able to continue."
    exit 1
fi
# set -x    # debug script
set -e        # if anything fails, stop
stage1
stage2
stage3
stage4
stage5
stage6
stage7
stage8
echo "--------------------------------------------------------"
echo "Build successful !"
echo "--------------------------------------------------------"
#############################################################


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/42111/showart_386846.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP