mk脚本分析
源码及注释如下:
#!/bin/sh
#
# Description
: Build Android Script.
# Authors
: jianjun jiang -
[email protected]# Version
: 0.01
# Notes
: None
#
export ANDROID_JAVA_HOME=/usr/lib/jvm/java-6-sun/
#java编译环境声明,如果按照前面的步骤手动安装了jdk1.6并声明了环境变量,则该句可省略
SOURCE_DIR=$(cd `dirname $0` ; pwd)
#通过脚本指令查找当前目录,事实上使用./mk执行脚本,会有警告提示,直接用SOURCE_DIR=$(pwd)即可
TOOLS_DIR=${SOURCE_DIR}/tools/
#tools目录
RELEASE_DIR=${SOURCE_DIR}/out/release/
#最终生成的映像文件的释放目录
TARGET_DIR=${SOURCE_DIR}/out/target/product/smdkv210/
#映像文件的目标目录
BOOTLOADER_UBOOT_CONFIG=smdkv210single_config
#uboot配置脚本
BOOTLOADER_XBOOT_CONFIG=arm-mpad
#xboot配置脚本
ANDROID_KERNEL_CONFIG=s5pv210_android_defconfig
#android内核配置脚本
INITRD_KERNEL_CONFIG=s5pv210_initrd_defconfig
#linux内核配置脚本
FILESYSTEM_CONFIG=PRODUCT-full_smdkv210-eng
#指定目标工程名称
setup_environment()
#设置环境变量函数,在android源码目录创建映像释放目录和映像的目标目录
{
cd ${SOURCE_DIR};
mkdir -p ${RELEASE_DIR} || return 1;
mkdir -p ${TARGET_DIR} || return 1;
}
build_bootloader_uboot()
#编译uboot,我们使用xboot,不使用uboot
{
cd ${SOURCE_DIR}/uboot || return 1
make distclean || return 1;
make ${BOOTLOADER_UBOOT_CONFIG} || return 1;
make
./mkmovi
./mkheader bootstrap.bin
./mkheader u-boot.bin
cp -v ${SOURCE_DIR}/uboot/bootstrap.bin ${RELEASE_DIR}
cp -v ${SOURCE_DIR}/uboot/u-boot.bin ${RELEASE_DIR}
echo "" >&2
echo "^_^ uboot path: ${RELEASE_DIR}/bootstrap.bin" >&2
echo "^_^ uboot path: ${RELEASE_DIR}/u-boot.bin" >&2
return 0
}
build_bootloader_xboot()
#编译xboot
{
if [ ! -f ${RELEASE_DIR}/zImage-initrd ]; then
#xboot将linux内核打包了,故必须存在linux内核文件
echo "not found kernel zImage-initrd, please build kernel first" >&2
return 1
fi
if [ ! -f ${RELEASE_DIR}/zImage-android ]; then
#xboot将android内核打包了,故必须存在android内核文件
echo "not found kernel zImage-android, please build kernel first" >&2
return 1
fi
# copy zImage-initrd and zImage-android to xboot's romdisk directory
cp -v ${RELEASE_DIR}/zImage-initrd ${SOURCE_DIR}/xboot/src/arch/arm/mach-mpad/romdisk/boot || return 1;
cp -v ${RELEASE_DIR}/zImage-android ${SOURCE_DIR}/xboot/src/arch/arm/mach-mpad/romdisk/boot || return 1;
# compiler xboot
cd ${SOURCE_DIR}/xboot || return 1
make TARGET=${BOOTLOADER_XBOOT_CONFIG} CROSS=/usr/local/arm/arm-none-eabi-2010-09-51/bin/arm-none-eabi- clean || return 1;
make TARGET=${BOOTLOADER_XBOOT_CONFIG} CROSS=/usr/local/arm/arm-none-eabi-2010-09-51/bin/arm-none-eabi- || return 1;
# rm zImage-initrd and zImage-android
rm -fr ${SOURCE_DIR}/xboot/src/arch/arm/mach-mpad/romdisk/boot/zImage-initrd
rm -fr ${SOURCE_DIR}/xboot/src/arch/arm/mach-mpad/romdisk/boot/zImage-android
# copy xboot.bin to release directory
cp -v ${SOURCE_DIR}/xboot/output/xboot.bin ${RELEASE_DIR}
echo "" >&2
echo "^_^ xboot path: ${RELEASE_DIR}/xboot.bin" >&2
return 0
}
build_kernel()
#编译内核
{
cd ${SOURCE_DIR}/kernel || return 1
make ${INITRD_KERNEL_CONFIG} || return 1
#编译linux内核
make -j${threads} || return 1
#编译完后将映像zImage重命名为zImage-initrd
dd if=${SOURCE_DIR}/kernel/arch/arm/boot/zImage of=${RELEASE_DIR}/zImage-initrd bs=2048 count=8192 conv=sync;
make ${ANDROID_KERNEL_CONFIG} || return 1
#编译android内核
make -j${threads} || return 1
#编译完后将映像zImage重命名为zImage-android
dd if=${SOURCE_DIR}/kernel/arch/arm/boot/zImage of=${RELEASE_DIR}/zImage-android bs=2048 count=8192 conv=sync;
echo "" >&2
echo "^_^ initrd kernel path: ${RELEASE_DIR}/zImage-initrd" >&2
echo "^_^ android kernel path: ${RELEASE_DIR}/zImage-android" >&2
return 0
}
build_system()
#编译android文件系统
{
cd ${SOURCE_DIR} || return 1
make -j${threads} ${FILESYSTEM_CONFIG} || return 1
# create android.img.cpio
rm -fr ${TARGET_DIR}/cpio_list ${TARGET_DIR}/android.img.cpio || { return 1; }
$TOOLS_DIR/gen_initramfs_list.sh ${TARGET_DIR}/root > ${TARGET_DIR}/cpio_list || { return 1; }
$TOOLS_DIR/gen_init_cpio ${TARGET_DIR}/cpio_list > ${TARGET_DIR}/android.img.cpio || { return 1; }
# create data.tar
cd ${TARGET_DIR}/data || { echo "Error: Could not enter the ${TARGET_DIR}/data directory."; return 1; }
rm -fr ${TARGET_DIR}/data.tar || { return 1; }
tar cvf ${TARGET_DIR}/data.tar ./* || { return 1; }
#将data目录下生成的所有文件打包 ***/
# create system.tar
cd ${TARGET_DIR}/system || { echo "Error: Could not enter the ${TARGET_DIR}/system directory."; return 1; }
rm -fr ${TARGET_DIR}/system.tar || { return 1; }
tar cvf ${TARGET_DIR}/system.tar ./* || { return 1; }
#将system目录下生成的所有文件打包 ***/
cp -av ${TARGET_DIR}/installed-files.txt ${RELEASE_DIR}/ || return 1;
cp -av ${TARGET_DIR}/installed-files.txt ${RELEASE_DIR}/ || return 1;
cp -av ${TARGET_DIR}/android.img.cpio ${RELEASE_DIR}/ || return 1;
cp -av ${TARGET_DIR}/system.img ${RELEASE_DIR}/ || return 1;
cp -av ${TARGET_DIR}/system.tar ${RELEASE_DIR}/ || return 1;
cp -av ${TARGET_DIR}/userdata.img ${RELEASE_DIR}/ || return 1;
cp -av ${TARGET_DIR}/data.tar ${RELEASE_DIR}/ || return 1;
echo "" >&2
echo "^_^ system path: ${RELEASE_DIR}/system.tar" >&2
return 0
}
gen_update_bin()
#使用genromfs工具将生成的映像文件打包成update.bin
{
# check image files
if [ ! -f ${RELEASE_DIR}/xboot.bin ]; then
#update.bin包含xboot.bin
echo "not found bootloader xboot.bin, please build bootloader" >&2
return 1
fi
if [ ! -f ${RELEASE_DIR}/zImage-initrd ]; then
#update.bin包含zImage-initrd
echo "not found kernel zImage-initrd, please build kernel first" >&2
return 1
fi
if [ ! -f ${RELEASE_DIR}/zImage-android ]; then
#update.bin包含zImage-android
echo "not found kernel zImage-android, please build kernel first" >&2
return 1
fi
if [ ! -f ${RELEASE_DIR}/system.tar ]; then
#update.bin包含system.tar
echo "not found system.tar, please build system" >&2
return 1
fi
if [ ! -f ${RELEASE_DIR}/data.tar ]; then
#update.bin包含data.tar
echo "not found data.tar, please build system" >&2
return 1
fi
rm -fr ${RELEASE_DIR}/tmp || return 1;
rm -fr ${RELEASE_DIR}/update.bin || return 1;
mkdir -p ${RELEASE_DIR}/tmp || return 1;
# copy image files
cp ${RELEASE_DIR}/xboot.bin ${RELEASE_DIR}/tmp/;
cp ${RELEASE_DIR}/zImage-initrd ${RELEASE_DIR}/tmp/;
cp ${RELEASE_DIR}/zImage-android ${RELEASE_DIR}/tmp/;
cp ${RELEASE_DIR}/system.tar ${RELEASE_DIR}/tmp/;
cp ${RELEASE_DIR}/data.tar ${RELEASE_DIR}/tmp/;
# create md5sum.txt
cd ${RELEASE_DIR}/tmp/;
find . -type f -print | while read line; do
if [ $line != 0 ]; then
md5sum ${line} >> md5sum.txt
fi
done
# genromfs
#使用genromfs工具打包
genromfs -v -d ${RELEASE_DIR}/tmp/ -f ${RELEASE_DIR}/update.bin || return 1;
cd ${SOURCE_DIR} || return 1
rm -fr ${RELEASE_DIR}/tmp || return 1;
return 0;
}
threads=4;
#默认使用4核编译,如果需要使用单核编译,执行脚本时添加-j1
uboot=no;
#默认都不编译
xboot=no;
kernel=no;
system=no;
update=no;
if [ -z $1 ]; then
#如果执行脚本时不带参数,则全部编译
uboot=yes
xboot=yes
kernel=yes
system=yes
update=yes
fi
while [ "$1" ]; do
#通过while循环检测要编译的映像文件
case "$1" in
-j=*)
#使用几核编译
x=$1
threads=${x#-j=}
;;
-u|--uboot)
#编译uboot
uboot=yes
;;
-x|--xboot)
#编译xboot
xboot=yes
;;
-k|--kernel)
#编译kernel
kernel=yes
;;
-s|--system)
#编译android system
system=yes
;;
-U|--update)
#打包所有映像
update=yes
;;
-a|--all)
#编译所有文件
uboot=yes
xboot=yes
kernel=yes
system=yes
update=yes
;;
-h|--help)
#帮助信息
cat >&2 <<EOF
Usage: ./mk [OPTION]
Build script for compile the source of telechips project.
-j=n using n threads when building source project (example: -j=16)
-u, --uboot build bootloader uboot from source file
-x, --xboot build bootloader xboot from source file
-k, --kernel build kernel from source file and using default config file
-s, --system build file system from source file
-U, --update gen update package update.bin
-a, --all build all, include anything
-h, --help display this help and exit
EOF
exit 0
;;
*)
echo "build.sh: Unrecognised option $1" >&2
exit 1
;;
esac
#case语句结束标志,注意正好是case反过来
shift
#通过shift来平移执行脚本时输入的参数
done
setup_environment || exit 1
#脚本的第一条执行指令,设置编译环境变量
if [ "${kernel}" = yes ]; then
#编译内核
build_kernel || exit 1
fi
if [ "${system}" = yes ]; then
#编译android系统
build_system || exit 1
fi
if [ "${uboot}" = yes ]; then
#编译uboot
build_bootloader_uboot || exit 1
fi
if [ "${xboot}" = yes ]; then
#编译xboot
build_bootloader_xboot || exit 1
fi
if [ "${update}" = yes ]; then
#打包映像文件
gen_update_bin || exit 1
fi
exit 0
#退出脚本文件