1,source build/envsetup.sh
source 是用来运行 shell 脚本的命令 功能和 "." 和相同,因此
也可以写作: . build/envsetup.sh
运行效果结果如下:
xxx@xxx:~/xxx/kitkat$ . build/envsetup.shincluding device/generic/armv7-a-neon/vendorsetup.shincluding device/generic/x86/vendorsetup.shincluding device/generic/mips/vendorsetup.shincluding sdk/bash_completion/adb.bashbuddy@RD3-199:~/C_BQ_T628VX_01/kitkat-mstar-master$
那么上面的结果是如何产生的呢?运行envsetup.sh的发生了什么呢?我们自己添加的客户分支是如何加入到 lunch 的选项中去的呢?
a,关于envsetup
envsetup 为我们提供了很多的shell脚本函数,例如 envsetup 开头的第一个函数 hmm,
function hmm() {cat <<EOFInvoke ". build/envsetup.sh" from your shell to add the following functions to your environment:- lunch: lunch <product_name>-<build_variant>- tapas: tapas [<App1> <App2> ...] [arm|x86|mips|armv5] [eng|userdebug|user]- croot: Changes directory to the top of the tree.- m: Makes from the top of the tree.- mm: Builds all of the modules in the current directory, but not their dependencies.- mmm: Builds all of the modules in the supplied directories, but not their dependencies.- mma: Builds all of the modules in the current directory, and their dependencies.- mmma: Builds all of the modules in the supplied directories, and their dependencies.- cgrep: Greps on all local C/C++ files.- jgrep: Greps on all local Java files.- resgrep: Greps on all local res/*.xml files.- godir: Go to the directory containing a file.Look at the source to view more functions. The complete list is:EOFT=$(gettop) #gettop也是envsetup.sh 中定义的一个 函数,有兴趣的童鞋可以看一下具体实现local AA=""for i in `cat $T/build/envsetup.sh | sed -n "/^function /s/function \([a-z_]*\).*/\1/p" | sort`; doA="$A $i"doneecho $A}
在运行envsetup .sh以后 就可以使用 hmm命令了,此时终端会输出命令帮助提示。
简单来说,envsetup.sh 给我们提供了一组自定义的 shell命令,而lunch 就是这些自定义命令中的一个。
2,lunch 命令
我们依然来看 envsetup.sh,在envsetup.sh 中可以找到 lunch 定义
function lunch(){local answerif [ "$1" ] ; then #lunch可以接受一个输入参数,例如 lunch xxx-userdebug 此时直接选择了xxx。answer=$1else # 如果没有输入参数,则打印选择菜单,并等待选择输入print_lunch_menuecho -n "Which would you like? [aosp_arm-eng] "read answerfilocal selection=if [ -z "$answer" ] #如果answer为空thenselection=aosp_arm-eng #默认选择aosp_arm-engelif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")thenif [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]thenselection=${LUNCH_MENU_CHOICES[$(($answer-1))]}fielif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$") #不以 ‘-’ 开头和结束thenselection=$answerfiif [ -z "$selection" ]thenechoecho "Invalid lunch combo: $answer"return 1fi#导出编译使用的环境变量export TARGET_BUILD_APPS=#提取"-"位置以前的字符串 这里是“xxx”local product=$(echo -n $selection | sed -e "s/-.*$//")check_product $productif [ $? -ne 0 ]thenechoecho "** Don't have a product spec for: '$product'"echo "** Do you have the right repo manifest?"product=fi#提取"-"位置以后的字符串“ userdebug ”local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")check_variant $variantif [ $? -ne 0 ]thenechoecho "** Invalid variant: '$variant'"echo "** Must be one of ${VARIANT_CHOICES[@]}"variant=fiif [ -z "$product" -o -z "$variant" ] #如果为空则返回thenechoreturn 1fi#导出编译使用的环境变量export TARGET_PRODUCT=$productexport TARGET_BUILD_VARIANT=$variantexport TARGET_BUILD_TYPE=releaseechoset_stuff_for_environmentprintconfig}
看一看其中调用的几个函数
print_lunch_menu
function print_lunch_menu(){local uname=$(uname)echoecho "You're building on" $unameechoecho "Lunch menu... pick a combo:"local i=1local choicefor choice in ${LUNCH_MENU_CHOICES[@]} #这里输出的就是我们看到的 lunch 命令的输出结果doecho " $i. $choice"i=$(($i+1))doneecho}
那么 LUNCH_MENU_CHOICES 变量的数据又是从何而来呢?
# Clear this variable. It will be built up again when the vendorsetup.sh# files are included at the end of this file.unset LUNCH_MENU_CHOICES#这函数就是向lunch命令添加选择项 客户分支里 vendorsetup.sh 里的那一行代码的用途就是添加lunch选择项function add_lunch_combo(){local new_combo=$1local cfor c in ${LUNCH_MENU_CHOICES[@]} ; doif [ "$new_combo" = "$c" ] ; thenreturnfidoneLUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)}# add the default one hereadd_lunch_combo aosp_arm-engadd_lunch_combo aosp_x86-engadd_lunch_combo aosp_mips-engadd_lunch_combo vbox_x86-eng
我们来看看 check_product
的作用
# check to see if the supplied product is one we can buildfunction check_product(){T=$(gettop)if [ ! "$T" ]; thenecho "Couldn't locate the top of the tree. Try setting TOP." >&2returnfi#设置环境变量CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \TARGET_PRODUCT=$1 \TARGET_BUILD_VARIANT= \TARGET_BUILD_TYPE= \TARGET_BUILD_APPS= \get_build_var TARGET_DEVICE > /dev/null #get_build_var# hide successful answers, but allow the errors to show}
# Get the exact value of a build variable.function get_build_var(){T=$(gettop)if [ ! "$T" ]; thenecho "Couldn't locate the top of the tree. Try setting TOP." >&2returnfi#执行config.mk配置相关的内容,#进一步依次执行envsetup.mk,到product_config.mk,#最终获得TARGET_DEVICE变量的内容。CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \make --no-print-directory -C "$T" -f build/core/config.mk dumpvar-$1}
check_variant 函数
#三种模式 user userdebug engVARIANT_CHOICES=(user userdebug eng)# check to see if the supplied variant is validfunction check_variant(){for v in ${VARIANT_CHOICES[@]}doif [ "$v" = "$1" ]thenreturn 0fidonereturn 1}
以下内容待验证#1.eng,那么其编译进去的内容包括:· Intended for platform-level debugging· Installs modules tagged with: eng, debug, user, and/or development· Installs non-APK modules that have no tags specified· Installs APKs according to the product definition files, in addition to tagged APKs· Sets ro.secure=1· Sets ro.debuggable=0· Sets ro.kernel.android.checkjni=1· adbd is enabled by default#2.user,那么其编译进去的内容包括:· Intended to be the final release· Installs modules tagged as user· Installs non-APK modules that have no tags specified· Installs APKs according to the product definition files (tags are ignored for APK modules)· Sets ro.secure=1· Sets ro.debuggable=0· adbd is disabled by default#3.userdebug,那么其编译进去的内容包括:the same as user, except:· Intended for limited debugging· Installs modules tagged with debug· Sets ro.debuggable=1· adbd is enabled by default
set_stuff_for_environment函数,主要设备编译相关的环境
function set_stuff_for_environment(){settitleset_java_homesetpathsset_sequence_number# MStar Android Patch Beginset_ota# MStar Android Patch Endexport ANDROID_BUILD_TOP=$(gettop)}
关于lunch 命令的过程已经基本清晰,那么系统是如何找到客户分支下的 vendorsetup.sh 呢?
3,扫描系统中的vendorsetup.sh 文件
# Execute the contents of any vendorsetup.sh files we can find.# 检测 device 目录和vendor 目录是否存在 ,并且找到 目录下的vendorsetup.sh .最多目录层次四层for f in `test -d device && find device -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null` \`test -d vendor && find vendor -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null`doecho "including $f". $f # 找到vendorsetup.sh 并执行,doneunset faddcompletions
function addcompletions(){local T dir f# Keep us from trying to run in something that isn't bash.if [ -z "${BASH_VERSION}" ]; thenreturnfi# Keep us from trying to run in bash that's too old.if [ ${BASH_VERSINFO[0]} -lt 3 ]; thenreturnfidir="sdk/bash_completion"if [ -d ${dir} ]; then# 扫描 /sdk/bash_completion 目录并允许 .bash 文件for f in `/bin/ls ${dir}/[a-z]*.bash 2> /dev/null`; doecho "including $f". $f #运行bash文件donefi}
source ./build/envsetup.sh
lunch
命令的基本过程大致如上所说。主要初始化编译所需要的系统变量
本文详细介绍了Android源码构建过程中,`source build/envsetup.sh`和`lunch`命令的执行过程。envsetup.sh提供了一系列自定义shell命令,包括hmm用于显示帮助。lunch用于设置编译环境,通过调用一系列函数如print_lunch_menu、check_product等确定编译选项。同时,文章探讨了系统如何查找并执行vendorsetup.sh以支持客户分支。

3465

被折叠的 条评论
为什么被折叠?



