Android source build/envsetup.sh 及lunch 过程

来源:互联网 发布:ubuntu网络邻居 编辑:程序博客网 时间:2024/05/16 23:58
1,source build/envsetup.sh
        source 是用来运行 shell 脚本的命令  功能和 "."  和相同因此 也可以写作: . build/envsetup.sh
运行效果结果如下:
xxx@xxx:~/xxx/kitkat$ . build/envsetup.sh
including device/generic/armv7-a-neon/vendorsetup.sh
including device/generic/x86/vendorsetup.sh
including device/generic/mips/vendorsetup.sh
including sdk/bash_completion/adb.bash
buddy@RD3-199:~/C_BQ_T628VX_01/kitkat-mstar-master$
那么上面的结果是如何产生的呢?运行envsetup.sh的发生了什么呢?我们自己添加的客户分支是如何加入到 lunch 的选项中去的呢?
a,关于envsetup
envsetup 为我们提供了很多的shell脚本函数,例如 envsetup 开头的第一个函数 hmm,
function hmm() {
cat <<EOF
Invoke ". 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:
EOF
T=$(gettop)    #gettop也是envsetup.sh 中定义的一个 函数,有兴趣的童鞋可以看一下具体实现
local A
A=""
for i in `cat $T/build/envsetup.sh | sed -n "/^function /s/function \([a-z_]*\).*/\1/p" | sort`; do
A="$A $i"
done
echo $A
}
在运行envsetup .sh以后 就可以使用 hmm命令了,此时终端会输出命令帮助提示。
简单来说,envsetup.sh  给我们提供了一组自定义的 shell命令,而lunch 就是这些自定义命令中的一个。

2,lunch 命令
我们依然来看  envsetup.sh,在envsetup.sh 中可以找到 lunch 定义
function lunch()
{
local answer
 
if [ "$1" ] ; then #lunch可以接受一个输入参数,例如 lunch xxx-userdebug 此时直接选择了xxx。
answer=$1
else # 如果没有输入参数,则打印选择菜单,并等待选择输入
print_lunch_menu
echo -n "Which would you like? [aosp_arm-eng] "
read answer
fi
 
local selection=
 
if [ -z "$answer" ] #如果answer为空
then
selection=aosp_arm-eng #默认选择aosp_arm-eng
elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
then
if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
then
selection=${LUNCH_MENU_CHOICES[$(($answer-1))]}
fi
elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$") #不以 ‘-’ 开头和结束
then
selection=$answer
fi
 
if [ -z "$selection" ]
then
echo
echo "Invalid lunch combo: $answer"
return 1
fi
#导出编译使用的环境变量 
export TARGET_BUILD_APPS=
#提取"-"位置以前的字符串 这里是“xxx” 
local product=$(echo -n $selection | sed -e "s/-.*$//")
check_product $product
if [ $? -ne 0 ]
then
echo
echo "** 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 $variant
if [ $? -ne 0 ]
then
echo
echo "** Invalid variant: '$variant'"
echo "** Must be one of ${VARIANT_CHOICES[@]}"
variant=
fi
 
if [ -z "$product" -o -z "$variant" ] #如果为空则返回
then
echo
return 1
fi
 
#导出编译使用的环境变量
export TARGET_PRODUCT=$product
export TARGET_BUILD_VARIANT=$variant
export TARGET_BUILD_TYPE=release
 
echo
 
set_stuff_for_environment
printconfig
}
看一看其中调用的几个函数

print_lunch_menu
function print_lunch_menu()
{
local uname=$(uname)
echo
echo "You're building on" $uname
echo
echo "Lunch menu... pick a combo:"
 
local i=1
local choice
for choice in ${LUNCH_MENU_CHOICES[@]} #这里输出的就是我们看到的 lunch 命令的输出结果
do
echo " $i. $choice"
i=$(($i+1))
done
 
echo
}
那么 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=$1
local c
for c in ${LUNCH_MENU_CHOICES[@]} ; do
if [ "$new_combo" = "$c" ] ; then
return
fi
done
LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
}
 
# add the default one here
add_lunch_combo aosp_arm-eng
add_lunch_combo aosp_x86-eng
add_lunch_combo aosp_mips-eng
add_lunch_combo vbox_x86-eng
我们来看看 check_product 的作用
# check to see if the supplied product is one we can build
function check_product()
{
T=$(gettop)
if [ ! "$T" ]; then
echo "Couldn't locate the top of the tree. Try setting TOP." >&2
return
fi
#设置环境变量
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" ]; then
echo "Couldn't locate the top of the tree. Try setting TOP." >&2
return
fi
    #执行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 eng
VARIANT_CHOICES=(user userdebug eng)
# check to see if the supplied variant is valid
function check_variant()
{
for v in ${VARIANT_CHOICES[@]}
do
if [ "$v" = "$1" ]
then
return 0
fi
done
return 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()
{
settitle
set_java_home
setpaths
set_sequence_number
# MStar Android Patch Begin
set_ota
# MStar Android Patch End
 
export 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`
do
echo "including $f"
. $f # 找到vendorsetup.sh 并执行,
done
unset f
 
addcompletions
function addcompletions()
{
local T dir f
 
# Keep us from trying to run in something that isn't bash.
if [ -z "${BASH_VERSION}" ]; then
return
fi
 
# Keep us from trying to run in bash that's too old.
if [ ${BASH_VERSINFO[0]} -lt 3 ]; then
return
fi
 
dir="sdk/bash_completion"
if [ -d ${dir} ]; then
# 扫描 /sdk/bash_completion 目录并允许 .bash 文件
for f in `/bin/ls ${dir}/[a-z]*.bash 2> /dev/null`; do
echo "including $f"
. $f #运行bash文件
done
fi
}

source ./build/envsetup.sh
lunch
命令的基本过程大致如上所说。主要初始化编译所需要的系统变量









1 0