Android源码编译有很多全局的参数,例如TARGET_OUT_DATA,TARGET_OUT_ROOT ,这些参数是从何而来的呢?
编译步骤
我们通过走一下编译步骤,看一下,这些参数的来源吧!
souce build/envsetup.sh
上面这句命令,是编译前准备环境的。
/build/envsetup.sh
可以看到到引进了一堆的函数,有mm,mmm等常用指令
看看跑了啥逻辑,
1.首先判断了下shell的环境,最好使用/bin/bash。
参考:https://www.douban.com/note/404726254/
2.包含厂商vendor目录的vendorsetup.sh
#只支持shell为/bin/bash,否则会报错,ps -o command -p $$是显示shell的进程名。
if [ "x$SHELL" != "x/bin/bash" ]; then
case `ps -o command -p $$` in
*bash*)
;;
*)
echo "WARNING: Only bash is supported, use of other shell would lead to erroneous results"
;;
esac
fi
#引进一些厂商的verdorsetup.sh
# Execute the contents of any vendorsetup.sh files we can find.
for f in `/bin/ls vendor/*/vendorsetup.sh vendor/*/*/vendorsetup.sh device/*/*/vendorsetup.sh 2> /dev/null`
do
echo "including $f"
. $f
done
unset f
addcompletions
包含后,echo打印出来,像下面这样子~
lunch
lunch就是选择一些编译参数,估计就是在这里准备好这些全局的参数的。
lunch函数在envsetup.sh里面定义
function lunch()
{
local answer #定义一个本地变量answer
#获取第一个输入参数,例如如果执行lunch mini_emulator_arm64-userdebug,第一个参数就是mini_emulator_arm64-userdebug
if [ "$1" ] ; then
answer=$1
else
print_lunch_menu #如果没有输入第一个参数,例如直接执行lunch,就会打印出选项让你选择
echo -n "Which would you like? [full-eng] "
read answer
fi
local selection= #定义一个本地变量selection
if [ -z "$answer" ] #如果answer为空,selection就是full-eng
then
selection=full-eng
elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$") #grep -q表示安静输出,-e为正则表达式,匹配0-99
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" ] #无效输入,例如你输入1000就无效啦
then
echo
echo "Invalid lunch combo: $answer"
return 1
fi
export TARGET_BUILD_APPS=
local product=$(echo -n $selection | sed -e "s/-.*$//")
check_product $product ##检查product
if [ $? -ne 0 ]
then
echo
echo "** Don't have a product spec for: '$product'" #无效的product
echo "** Do you have the right repo manifest?"
product=
fi
local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//") ##编译类型,例如user,debug
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
}
set_stuff_for_environment
会进入set_stuff_for_environment,主要是设置title,java环境,paths还有序列号。貌似没多大用处~
function set_stuff_for_environment()
{
settitle
set_java_home
setpaths #一些交叉编译链路径
set_sequence_number
export ANDROID_BUILD_TOP=$(gettop)
}
printconfig
最后会进入printconfig,就是打印配置信息给我们看啦!
function printconfig()
{
T=$(gettop)
if [ ! "$T" ]; then
echo "Couldn't locate the top of the tree. Try setting TOP." >&2
return
fi
get_build_var report_config
}
打印一些配置,可以看到TARGET_PRODUCT,TARGET_BUILD_VARIANT和TARGET_BUILD_TYPE。
printconfig包含get_build_var函数,函数执行了一句make指令,make --no-print-directory -C "$T" -f build/core/config.mk dumpvar-$1
(1)make -C "$T" 就是跳转到top目录。
(2)make -f 来指定makefile文件build/core/config.mk,我去config.mk还是个makefile文件。
(3)dumpvary用来打印用的,就是打印第一个参数$1啦,呵呵呵
所以,主要get_build_var主要是执行makefile config.mk。
function get_build_var()
{
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 \
make --no-print-directory -C "$T" -f build/core/config.mk dumpvar-$1
}
看看/build/core/config.mk,可以看到他进一步包含了envsetup.mk
include $(BUILD_SYSTEM)/envsetup.mk
envsetup.mk就是最后设置一些常用编译环境参数的文件了!!
/build/core/envsetup.mk
看下面的图,可以看到他定义了很多编译常量了!!
总结
1.通过source envsetup.sh可以引进很多函数,包含vendorsetup.sh
2.通过lunch,选择类型后,会编译build/core/config.mk文件,进而再编译envsetup.mk文件,而这个文件就是定义编译常量的地方!