全志A64 lichee编译脚本build.sh分析

lichee目录下的./build.sh脚本:

    set -e  #用于指此脚本里执行的命令返回值非0时,终止当前脚本的执行.
    buildroot/scripts/mkcommon.sh $@   
    #接着执行mkcommon.sh脚本,并把执行"./build.sh"附带的所有参数($@)传递过去,这里$@是空的.

buildroot/scripts/mkcommon.sh:

    BR_SCRIPTS_DIR=`dirname $0` #$0表示第0个参数,即是"buildroot/scripts/mkcommon.sh"
                       #dirname命令用于获取路径, `dirname $0`获取得到"buildroot/scripts"

. ${BR_SCRIPTS_DIR}/shflags/shflags
[ -f .buildconfig ] && . .buildconfig #如有.buildconfig文件,则导出里面的变量. 此文件是由"./build.sh config"生成的.
. ${BR_SCRIPTS_DIR}/mkcmd.sh #导出脚本里的下面function命令,以便在当前脚本里可以调用访问.
        mkboot      build boot
        mkbr        build buildroot
        mkkernel    build kernel
        mkrootfs    build rootfs for linux, dragonboard
        mklichee    build total lichee
        mkclean     clean current board output
        mkdistclean clean entires output
        mkpack      pack firmware for lichee
        mkhelp      show this message


[ -f .buildconfig ] && . .buildconfig #如有.buildconfig文件,则导出下面的环境变量. 此文件用于指定编译的目标平台
    export LICHEE_CHIP=sun50iw1p1
    export LICHEE_PLATFORM=android
    export LICHEE_SECURE=
    export LICHEE_KERN_VER=linux-3.10
    export LICHEE_BOARD=p1


if [ "x$1" = "xconfig" ] ; then  #执行"./build.sh config"时触发
    . ${BR_SCRIPTS_DIR}/mksetup.sh  #出现选择界面
    exit $?   #返回
elif [ "x$1" = "xpack" ] ; then  #执行"./build.sh pack"时触发
    if [  "x$2" = "x-d" ] ; then
        init_defconf        #init_defconf命令是由buildroot/scripts/mkcmd.sh里提供的
        mkpack -d card0     #mkpack命令是由buildroot/scripts/mkcmd.sh里提供的
        exit $?
    else
        init_defconf
        mkpack
        exit $?
    fi
elif [ "x$1" = "xpack_debug" ] ; then  #执行"./build.sh pack_debug"时触发
    init_defconf
    mkpack -d card0
    exit $?
elif [ "x$1" = "xpack_dump" ] ; then   #执行"./build.sh pack_dump"时触发
    init_defconf
    mkpack -m dump
    exit $?
elif [ "x$1" = "xpack_secure" ] ; then  #执行"./build.sh pack_secure"时触发
    init_defconf
    mkpack -s secure
    exit $?
elif [ "x$1" = "xpack_debug_secure" ] ; then #执行"./build.sh pack_debug_secure"时触发
    init_defconf
    mkpack -s secure -d card0
    exit $?
elif [ "x$1" = "xpack_prev_refurbish" ] ; then #执行"./build.sh pack_prev_refurbish"时触发
        init_defconf
        mkpack -s secure -f prev_refurbish
        exit $?
elif [ "x$1" = "xpack_prvt" ] ; then  #执行"./build.sh pack_prvt"时触发
    init_defconf
    mkpack -f prvt
    exit $?
elif [ "x$1" = "xpack_nor" ] ;then #执行"./build.sh pack_nor"时触发
    init_defconf
    if [ "x$2" = "xdebug" ] ; then
        mkpack -f spinor -d card0
    else
        mkpack -f spinor
    fi
    exit $?
elif [ "x$1" = "xpack_erase" ] ; then  #执行"./build.sh pack_erase"时触发
    init_defconf
    mkpack -f erase
    exit $?
elif [ $# -eq 0 ] ; then  #执行"./build.sh"没任何参数时触发
    init_defconf
    mklichee   #编译内核及生成文件系统镜像
    exit $?
fi

“buildroot/scripts/mkcmd.sh” 556 行

function mklichee()
{

    mk_info "----------------------------------------"
    mk_info "build lichee ..."
    mk_info "chip: $LICHEE_CHIP"
    mk_info "platform: $LICHEE_PLATFORM"
    mk_info "secure: $LICHEE_SECURE"
    mk_info "kernel: $LICHEE_KERN_VER"
    mk_info "board: $LICHEE_BOARD"
    mk_info "output: out/${LICHEE_CHIP}/${LICHEE_PLATFORM}/${LICHEE_BOARD}" #这几个环境变量由.buildconfig提供.
    mk_info "----------------------------------------"

    check_env

    mkbr && mkkernel && mkrootfs  #分别执行mkbr, mkkernel, mkrootfs命令
    [ $? -ne 0 ] && return 1

    mk_info "----------------------------------------"
    mk_info "build lichee OK."
    mk_info "----------------------------------------"
}

脚本里所用的几个路径环境
LICHEE_TOP_DIR=`pwd`   #表示当前目录,即lichee目录下
LICHEE_BR_DIR=${LICHEE_TOP_DIR}/buildroot  #表示./buildroot目录
LICHEE_KERN_DIR=${LICHEE_TOP_DIR}/${LICHEE_KERN_VER}  #表示./linux-3.10目录
LICHEE_TOOLS_DIR=${LICHEE_TOP_DIR}/tools   #表示./tools目录
LICHEE_OUT_DIR=${LICHEE_TOP_DIR}/out       #表示./out目录


function mkbr()
{
    mk_info "build buildroot ..."

    local build_script="scripts/build.sh"
    (cd ${LICHEE_BR_DIR} && [ -x ${build_script} ] && ./${build_script})
    #cd ./buildroot  及执行./buildroot/scripts/build.sh脚本
        #因选择编译的目标平台为android,这个脚本里只作了配置交叉编译器。没有编译uboot

    [ $? -ne 0 ] && mk_error "build buildroot Failed" && return 1

    mk_info "build buildroot OK."
}

function mkkernel()
{
    mk_info "build kernel ..."

    local build_script="scripts/build.sh"

    prepare_toolchain

    # mark kernel .config belong to which platform
     #因可以选择编译android, linux等几个不同的目标平台, 内核里用.config.mark文件记录编译目标平台.
     #当选择的编译目标平台与上次编译的平台不同,则需要清除内核里已编译文件.
    local config_mark="${LICHEE_KERN_DIR}/.config.mark"
    if [ -f ${config_mark} ] ; then
        if ! grep -q "${LICHEE_PLATFORM}" ${config_mark} ; then
            mk_info "clean last time build for different platform"
            (cd ${LICHEE_KERN_DIR} && [ -x ${build_script} ] && ./${build_script} "clean")
            echo "${LICHEE_PLATFORM}" > ${config_mark}
        fi
    else
        echo "${LICHEE_PLATFORM}" > ${config_mark}
    fi

    (cd ${LICHEE_KERN_DIR} && [ -x ${build_script} ] && ./${build_script})
    #进入内核源码目录./linux-3.10下,执行里面的./script/build.sh脚本
    #默认会编译内核,动态驱动模块和ramfs等.
    [ $? -ne 0 ] && mk_error "build kernel Failed" && return 1

    mk_info "build kernel OK."
}

function mkrootfs()  #因编译的目标平台为android, 所以这里基本什么都不做.
{
    mk_info "build rootfs ..."

    if [ ${LICHEE_PLATFORM} = "linux" ] ; then
        make O=${LICHEE_BR_OUT} -C ${LICHEE_BR_DIR} target-generic-getty-busybox
        [ $? -ne 0 ] && mk_error "build rootfs Failed" && return 1
        make O=${LICHEE_BR_OUT} -C ${LICHEE_BR_DIR} target-finalize
        [ $? -ne 0 ] && mk_error "build rootfs Failed" && return 1
        make O=${LICHEE_BR_OUT} -C ${LICHEE_BR_DIR} LICHEE_GEN_ROOTFS=y rootfs-ext4
        [ $? -ne 0 ] && mk_error "build rootfs Failed" && return 1
        cp ${LICHEE_BR_OUT}/images/rootfs.ext4 ${LICHEE_PLAT_OUT}
          if [ -f "${LICHEE_BR_OUT}/images/rootfs.squashfs" ]; then
           cp ${LICHEE_BR_OUT}/images/rootfs.squashfs ${LICHEE_PLAT_OUT}
        fi
        if [ "x$PACK_TINY_ANDROID" = "xtrue" ];then
            packtinyandroid
        fi
    elif [ ${LICHEE_PLATFORM} = "dragonboard" ] ; then
        echo "Regenerating dragonboard Rootfs..."
           (
            cd ${LICHEE_BR_DIR}/target/dragonboard; \
            if [ ! -d "./rootfs" ]; then \
            echo "extract dragonboard rootfs.tar.gz"; \
            tar zxf rootfs.tar.gz; \
            fi
          )
        tooldir_32=${LICHEE_BR_OUT}/external-toolchain_32
        export PATH=${tooldir_32}/bin:$PATH
        export LICHEE_TOOLCHAIN_PATH=${tooldir_32}
        mkdir -p ${LICHEE_BR_DIR}/target/dragonboard/rootfs/lib/modules
        rm -rf ${LICHEE_BR_DIR}/target/dragonboard/rootfs/lib/modules/*
        cp -rf ${LICHEE_KERN_DIR}/output/lib/modules/* ${LICHEE_BR_DIR}/target/dragonboard/rootfs/lib/modules/
        (cd ${LICHEE_BR_DIR}/target/dragonboard; ./build.sh)
        cp ${LICHEE_BR_DIR}/target/dragonboard/rootfs.ext4 ${LICHEE_PLAT_OUT}
    else
        mk_info "skip make rootfs for ${LICHEE_PLATFORM}"
    fi

    mk_info "build rootfs OK."
}

内核源码编译脚本 linux-3.10/scripts/build.sh里的内核编译命令:

LICHEE_KDIR=`pwd`
LICHEE_MOD_DIR=${LICHEE_KDIR}/output/lib/modules/${KERNEL_VERSION}

build_kernel()
{
    echo "Building kernel"

    cd ${LICHEE_KDIR}

    rm -rf output/
    echo "${LICHEE_MOD_DIR}"
    mkdir -p ${LICHEE_MOD_DIR}

    # We need to copy rootfs files to compile kernel for linux image
    echo "lichee_chip = $LICHEE_CHIP"
    if [ "${LICHEE_CHIP}" = "sun8iw10p1" ] || [ "${LICHEE_CHIP}" = "sun8iw11p1" ]; then
        echo "cp rootfs_32bit.cpio.gz"
        cp -f rootfs_32bit.cpio.gz output/rootfs.cpio.gz
    else
        cp -f rootfs.cpio.gz output/   #复制rootfs.cpio.gz文件系统镜像到output目录
    fi
    #exchange sdc0 and sdc2 in sun50iw1p1 for dragonBoard card boot
    if [ "x${LICHEE_PLATFORM}" = "xdragonboard"  -a "x${LICHEE_CHIP}" = "xsun50iw1p1" ]; then  #不成立
        local SYS_CONFIG_FILE=../tools/pack/chips/${LICHEE_CHIP}/configs/${LICHEE_BOARD}/sys_config.fex
        local DTS_PATH=./arch/arm64/boot/dts/

        #sun50iw1p1_bak.dtsi means the orginal dtsi
        if [ -f ${DTS_PATH}/sun50iw1p1_bak.dtsi ];then
            #sun50iw1p1.dtsi that we use in fact
            rm -f ${DTS_PATH}/sun50iw1p1.dtsi
            mv ${DTS_PATH}/sun50iw1p1_bak.dtsi ${DTS_PATH}/sun50iw1p1.dtsi
        fi
        # if find dragonboard_test=1 in sys_config.fex ,then will exchange sdc0 and sdc2
        if [ -n "`grep "dragonboard_test" $SYS_CONFIG_FILE | grep "1" | grep -v ";"`" ]; then
            cp ${DTS_PATH}/sun50iw1p1.dtsi  ${DTS_PATH}/sun50iw1p1_bak.dtsi
            rm -f ${DTS_PATH}/sun50iw1p1.dtsi
            cp  ${DTS_PATH}/sun50iw1p1_for_dragonboard.dtsi   ${DTS_PATH}/sun50iw1p1.dtsi
        fi
    fi

    if [ ! -f .config ] ; then  #如果没有内核的配置文件.config, 则使用arch/arm64/configs/sun50iw1p1smp_android_defconfig配置文件
        printf "\n\033[0;31;1mUsing default config ${LICHEE_KERN_DEFCONF} ...\033[0m\n\n"
        cp arch/${ARCH}/configs/${LICHEE_KERN_DEFCONF} .config
    fi

    if [ "x$SUNXI_CHECK" = "x1" ];then
        SUNXI_SPARSE_CHECK=1
        SUNXI_SMATCH_CHECK=1
        SUNXI_STACK_CHECK=1
    fi
    if [ "x$SUNXI_SPARSE_CHECK" = "x" ] && [ "x$SUNXI_SMATCH_CHECK" = "x" ];then
        make ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} -j${LICHEE_JLEVEL} all modules
    else
        if [ "x$SUNXI_SPARSE_CHECK" = "x1" ] && [ -f ../tools/codecheck/sparse/sparse ];then
            echo "\n\033[0;31;1mBuilding Round for sparse check ${KERNEL_CFG}...\033[0m\n\n"
            make clean
            make CHECK="../tools/codecheck/sparse/sparse" ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} -j${LICHEE_JLEVEL} C=2 all modules 2>&1|tee output/build_sparse.txt
            cat output/build_sparse.txt|egrep -w '(warn|error|warning)' >output/warn_sparse.txt
        fi
        if [ "x$SUNXI_SMATCH_CHECK" = "x1" ]&& [ -f ../tools/codecheck/smatch/smatch ];then
            echo "\n\033[0;31;1mBuilding Round for smatch check ${KERNEL_CFG}...\033[0m\n\n"
            make clean
            make CHECK="../tools/codecheck/smatch/smatch --full-path --no-data -p=kkernel" ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} -j${LICHEE_JLEVEL} C=2 all modules 2>&1|tee output/build_smatch.txt
            cat output/build_smatch.txt|egrep -w '(warn|error|warning)' >output/warn_smatch.txt
        fi
    fi
    if [ "x$SUNXI_STACK_CHECK" = "x1" ];then
        make ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} -j${LICHEE_JLEVEL} checkstack 2>&1|tee output/warn_stack.txt
    fi
    update_kern_ver

    #The Image is origin binary from vmlinux.
    if [ -f arch/${ARCH}/boot/Image ]; then
        cp -vf arch/${ARCH}/boot/Image output/bImage
    fi

    if [ -f arch/${ARCH}/boot/zImage ] || [ -f arch/${ARCH}/boot/uImage ]; then
        cp -vf arch/${ARCH}/boot/[zu]Image output/
    fi

    if [ -f arch/${ARCH}/boot/Image.gz ]; then
        cp -vf arch/${ARCH}/boot/Image.gz output/
    fi

    cp .config output/

    tar -jcf output/vmlinux.tar.bz2 vmlinux
    for file in $(find drivers sound crypto block fs security net -name "*.ko"); do
        cp $file ${LICHEE_MOD_DIR}
    done
    cp -f Module.symvers ${LICHEE_MOD_DIR}

}

你可能感兴趣的:(全志A64 lichee编译脚本build.sh分析)