歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

s5pv210開發板上Android編譯腳本分析--[基於x210開發板]

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 #退出腳本文件
Copyright © Linux教程網 All Rights Reserved