Android(RK平台)编译流程优化-----选择性内核编译

------------------------------------------------------------------------------------------------------

       此文章仅作为学习交流所用

       转载或引用请务必注明原文地址:

       http://blog.csdn.net/luzhenrong45/article/

       或联系作者:luzhenrong45@gmail.com 

       谢谢!                       

------------------------------------------------------------------------------------------------------


首先看一份简单的Android(RK3066 android4.2)编译脚本:

#! /system/bin/sh

#================================================================
#
#   filename:       build.sh
#   author:         luzhenrong45@163.com
#   created:        2016/10/10 17:24
#   purpose:        Build RK3066 android code
#
#================================================================


KERNEL_CONFIG="rk3066_box_defconfig"
CHIPTYPE=rk3066

#包含Android环境变量
source build/envsetup.sh
cd kernel
make mrproper
make $KERNEL_CONFIG
make kernel.img -j8
cd ..

make -j8


工作过程中经常需要编译Android代码,代码以SVN的形式托管的。有时候只是修改了一些应用层的代码,比如 /system/app目录的,服务端编译打包的时候,也是将kernel代码全部clean后再重新编译。这种情况无疑是没必要的,内核代码都没修改,编译却要花费一部分时间,关键是,服务端编译Android源码的时候,要同时编译好几次(有好几个不同的配置产品)。这样一来,花费在kernel上的时间便变得有点多,而且属于不必要的浪费。

因此,改写了目前使用的Android编译脚本,如果检查到kernel代码没有发生变化的时候,则直接拷贝原来的kernel编译结果,不再重新编译内核。至于判断内核代码是否发生改变,则是通过比较kernel目录,打包编译前后的svn version来作为判断依据。如果kernel代码没有修改,那么,得到的svn version跟上一次肯定是一样的,这种情况就不再需要重新编译kernel了。

修改后的编译脚本如下:(RK3066 kernel目录位于源码根目录)

#! /system/bin/sh

#================================================================
#
#   filename:       build.sh
#   author:         luzhenrong45@163.com
#   created:        2016/10/10 17:24
#   company:        Copyright (C) 2016, Sangfor Ltd.
#   purpose:        Build android code
#
#================================================================


# PN:判断是否需要重新编译kernel
KERNEL_CONFIG="rk3066_box_defconfig"
KERNEL_IMAGE_BACKUP_DIR=kernel-image-bk
NEED_BUILD_KERNEL="TRUE"
CHIPTYPE=rk3066

#包含Android环境变量
source build/envsetup.sh

#创建kernel镜像保存目录
if [ ! -d $KERNEL_IMAGE_BACKUP_DIR/$CHIPTYPE/ ]; then
	mkdir -p $KERNEL_IMAGE_BACKUP_DIR/$CHIPTYPE/
fi

check_if_need_build_kernel ()
{

	if [ ! -f out/kernel_version_check.mk ]; then
		#如果不存在out/kernel_version_check.mk文件,可能没编译过代码,需要编译kernel
		echo -e "no kernel_version_check.mk file, need to compile kernel...\n"  
		NEED_BUILD_KERNEL="TRUE"
		#如果不存在则新建一个空文件
		echo "" > out/kernel_version_check.mk

	elif [  ! -f $KERNEL_IMAGE_BACKUP_DIR/$CHIPTYPE/kernel ] || [  ! -f $KERNEL_IMAGE_BACKUP_DIR/$CHIPTYPE/kernel.img ]; then
		#如果不存在kernel image备份镜像,可能没编译过代码,需要编译kernel
		echo -e "no kernel backup image, need to compile kernel...\n"
		NEED_BUILD_KERNEL="TRUE"
	else
		##获取对应盒子上次编译kerner的svn版本号
		KERNEL_SVN_VERSION_OLD=`sed "/^$CHIPTYPE=/!d;s/.*=//" out/kernel_version_check.mk`

		cd kernel
		#获取kernel目录最新svn版本号,作为kernel代码有否更新的判断依据
		KERNEL_SVN_VERSION_NEW=`LANG=C svn info | sed -n 's/^Last Changed Rev: //p'`
		cd ..


		if [ "$KERNEL_SVN_VERSION_OLD"x == ""x ] || [ "$KERNEL_SVN_VERSION_NEW"x == ""x ]; then
			# 如果获取的kernel version为空,需要编译kernel
			echo -e "The svn version is null, need to compile kernel...\n"  
			NEED_BUILD_KERNEL="TRUE"

		elif [ "$KERNEL_SVN_VERSION_OLD"x == "$KERNEL_SVN_VERSION_NEW"x ]; then
			#内核代码无改动,不需要重新编译内核
			echo -e "same kernel version, not need to compile kernel again ...\n"
			NEED_BUILD_KERNEL="FALSE"

		else
			#内核代码有改动,需要重新编译内核
			echo -e "svn version is not same, need to compile kernel...\n"
			NEED_BUILD_KERNEL="TRUE"
		fi

	fi

}

check_if_need_build_kernel || exit 1

echo -e "\nNEED_BUILD_KERNEL ====> $NEED_BUILD_KERNEL\n"

if [ $NEED_BUILD_KERNEL == "TRUE" ]; then

	#需要重新编译内核
	echo -e "\nBUILDING KERNEL IMAGE, CHIP TYPE = $CHIPTYPE ...\n"
	cd ./kernel
	KERNEL_SVN_VERSION_NEW=`LANG=C svn info | sed -n 's/^Last Changed Rev: //p'`
	make mrproper -j${jobs_no}
	#release版内核配置文件, 默认开启了app coredump
	make $KERNEL_CONFIG
	make kernel.img -j8 || exit 1
	cd ..

	#更新kernel备份镜像
	echo -e "\nUPDATE KERNEL BACKUP IMAGE FOR $CHIPTYPE ...\n"
	cp -fv ./kernel/arch/arm/boot/Image ./$KERNEL_IMAGE_BACKUP_DIR/$CHIPTYPE/kernel
	cp -fv ./kernel/kernel.img ./$KERNEL_IMAGE_BACKUP_DIR/$CHIPTYPE/kernel.img
	#更新kernel_version_check.mk文件kernel svn版本号
	echo -e "\nUPDATE KERNEL SVN VERSION FOR $CHIPTYPE ...\n"
	grep -wq "^$CHIPTYPE=[0-9]\{0,\}" out/kernel_version_check.mk &&\
	sed -i "s/^$CHIPTYPE=[0-9]\{0,\}$/$CHIPTYPE=$KERNEL_SVN_VERSION_NEW/" out/kernel_version_check.mk \
		|| echo "$CHIPTYPE=$KERNEL_SVN_VERSION_NEW" >> out/kernel_version_check.mk

else

	#不需要编译内核情况下,使用对应盒子已备份的kernel镜像替换对应的kernel编译镜像
	echo -e "\nNO NEED TO RE COMPILE KERNEL, JUST COPY KERNEL IMAGE FORM BACKUP FILE ...\n"
	cp -fv ./$KERNEL_IMAGE_BACKUP_DIR/$CHIPTYPE/kernel $OUT/kernel
	cp -fv ./$KERNEL_IMAGE_BACKUP_DIR/$CHIPTYPE/kernel ./kernel/arch/arm/boot/Image
	cp -fv ./$KERNEL_IMAGE_BACKUP_DIR/$CHIPTYPE/kernel.img ./kernel/kernel.img

fi
unset NEED_BUILD_KERNEL
# PN:判断是否需要重新编译kernel

make -j8


修改后的编译脚本,可直接到以下地址下载:

build.sh

http://download.csdn.net/detail/luzhenrong45/9638305

©️2020 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值