build/envsetup.sh中hmm、get_abs_build_var、get_build_var解析

时间:2023-03-09 03:33:04
build/envsetup.sh中hmm、get_abs_build_var、get_build_var解析
 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|arm64|x86_64|mips64] [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.
To limit the modules being built use the syntax: mmm dir/:target1,target2.
- 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.
- ggrep: Greps on all local Gradle files.
- jgrep: Greps on all local Java files.
- resgrep: Greps on all local res/*.xml files.
- sgrep: Greps on all local source files.
- godir: Go to the directory containing a file. Look at the source to view more functions. The complete list is:
EOF
# gettop是脚本内函数,功能:返回当前android代码树的顶层路径。前提是当前路径位于android代码树中
T=$(gettop)
# A变量 局部变量化
local A
A=""
# 查看envsetup.sh内容,且行首开始空格、制表符任意次,替换function为匹配的子串(函数名),按字符排升序、去掉重复
for i in `cat $T/build/envsetup.sh | sed -n "/^[ \t]*function /s/function \([a-z_]*\).*/\/p" | sort | uniq`; do
# 替换出来的函数名 追加在A后 并打印
A="$A $i"
done
echo $A
}

//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 #得到build变量(脚本内)的绝对路径
function get_abs_build_var()
{
# 得到源码树顶层目录
T=$(gettop)
# 源码树顶层目录有效性容错
if [ ! "$T" ]; then
echo "Couldn't locate the top of the tree. Try setting TOP." >&
return
fi
# 进入顶层目录,设置build变量,
# 通过make指定build/core/config.mk文件
# --no-print-directory:不要再屏幕上打印"Entering directory..
# CALLED_FROM_SETUP=true:打印出dumpvar-abs-$1所表示的变量的值必须先设置CALLED_FROM_SETUP
# BUILD_SYSTEM:设置所有的编译脚本路径
(\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
command make --no-print-directory -f build/core/config.mk dumpvar-abs-$)
}
 function get_build_var()
{
T=$(gettop)
if [ ! "$T" ]; then
echo "Couldn't locate the top of the tree. Try setting TOP." >&
return
fi
# 注释同上 唯一不同是目标dumpvar-$1不同
(\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
command make --no-print-directory -f build/core/config.mk dumpvar-$)
}

题外话:

BUILD_SYSTEM设置的Makefile目录——build/core目录下所有Makefile一览

build/envsetup.sh中hmm、get_abs_build_var、get_build_var解析

config.mk详见一篇,此处一笔带过,只为得到CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core command make --no-print-directory -f build/core/config.mk dumpvar-$1”最终执行结果与过程。

..... include $(BUILD_SYSTEM)/dumpvar.mk

继续引入dumpvar.mk

 # MAKECMDGOALS 变量是传进来的目标dumpvar-abs-$1或 dumpvar-$1的abs-$1或$
dumpvar_goals := \
$(strip $(patsubst dumpvar-%,%,$(filter dumpvar-%,$(MAKECMDGOALS))))
ifdef dumpvar_goals ifneq ($(words $(dumpvar_goals)),)
$(error Only one "dumpvar-" goal allowed. Saw "$(MAKECMDGOALS)")
endif # If the goal is of the form "dumpvar-abs-VARNAME", then
# treat VARNAME as a path and return the absolute path to it.
absolute_dumpvar := $(strip $(filter abs-%,$(dumpvar_goals)))
# 是否有abs
ifdef absolute_dumpvar
dumpvar_goals := $(patsubst abs-%,%,$(dumpvar_goals))
ifneq ($(filter /%,$($(dumpvar_goals))),)
DUMPVAR_VALUE := $($(dumpvar_goals))
else
DUMPVAR_VALUE := $(PWD)/$($(dumpvar_goals))
endif
# get_abs_build_var()中dumpvar-abs-$1对应的
dumpvar_target := dumpvar-abs-$(dumpvar_goals)
else
DUMPVAR_VALUE := $($(dumpvar_goals))
# get_build_var()中dumpvar-$1对应的
dumpvar_target := dumpvar-$(dumpvar_goals)
endif .PHONY: $(dumpvar_target)
# 执行目标与执行语句
$(dumpvar_target):
@echo $(DUMPVAR_VALUE)

即打印

其中如有abs则ifdef absolute_dumpvar->DUMPVAR_VALUE := $(PWD)/$($(dumpvar_goals)),打印信息增加pwd绝对路径

如没有  则else->DUMPVAR_VALUE := $($(dumpvar_goals)),打印信息没有绝对路径

//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

envsetup.sh使用方法:

book@book-virtual-machine:/work/android-5.0.$ source build/envsetup.sh
including device/asus/deb/vendorsetup.sh
including device/asus/fugu/vendorsetup.sh
including device/asus/tilapia/vendorsetup.sh
including device/asus/flo/vendorsetup.sh
including device/asus/grouper/vendorsetup.sh
including device/samsung/manta/vendorsetup.sh
including device/friendly-arm/tiny4412/vendorsetup.sh
including device/lge/mako/vendorsetup.sh
including device/lge/hammerhead/vendorsetup.sh
including device/moto/shamu/vendorsetup.sh
including device/generic/mini-emulator-mips/vendorsetup.sh
including device/generic/mini-emulator-armv7-a-neon/vendorsetup.sh
including device/generic/mini-emulator-arm64/vendorsetup.sh
including device/generic/mini-emulator-x86/vendorsetup.sh
including device/generic/mini-emulator-x86_64/vendorsetup.sh
including sdk/bash_completion/adb.bash
book@book-virtual-machine:/work/android-5.0.$ get_abs_build_var TARGET_PRODUCT
/work/android-5.0./full