1、get_abs_build_var() 和 get_build_var()的实现都在build/envsetup.sh中。
2、在buld目录下grep这两个函数可知:这两个函数只在build/envsetup.sh脚本中使用。
言归正传,贴代码
# Get the value of a build variable as an absolute path.
function get_abs_build_var()
{
T=$(gettop)
if [ ! "$T" ]; then
echo "Couldn't locate the top of the tree. Try setting TOP." >&2
return
fi
(\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
command make --no-print-directory -f build/core/config.mk dumpvar-abs-$1)
}
# 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
(\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
command make --no-print-directory -f build/core/config.mk dumpvar-$1)
}
这两个shell函数的实现很相似,都是通过make指定build/core/config.mk文件,唯一不同的地方是target目标不同。
接下来我们分析config.mk文件。
config.mk文件开头的注释:
# This is included by the top-level Makefile.
# It sets up standard variables based on the
# current configuration and platform, which
# are not specific to what is being built.
根据注释,我们可知config.mk文件主要是配置变量。
而且,config.mk 文件最后一行包含的$(BUILD_SYSTEM)/dumpvar.mk用于获取一些变量。
接下来我们分析dumpvar.mk文件。
# ---------------------------------------------------------------
# the setpath shell function in envsetup.sh uses this to figure out
# what to add to the path given the config we have chosen.
ifeq ($(CALLED_FROM_SETUP),true) # 在get_abs_build_var() 和 get_build_var()函数中,CALLED_FROM_SETUP=true,走if分支。
ifneq ($(filter /%,$(HOST_OUT_EXECUTABLES)),)
# HOST_OUT_EXECUTABLES变量是在envsetup.mk文件中设置的。
ABP:=$(HOST_OUT_EXECUTABLES)
else
ABP:=$(PWD)/$(HOST_OUT_EXECUTABLES)
endif
ANDROID_BUILD_PATHS := $(ABP) # 应该是 out/host/linux-x86/bin
ANDROID_PREBUILTS := prebuilt/$(HOST_PREBUILT_TAG)
# HOST_PREBUILT_TAG变量是在envsetup.mk文件中设置的。应该是 linux-x86
ANDROID_GCC_PREBUILTS := prebuilts/gcc/$(HOST_PREBUILT_TAG)
# The "dumpvar" stuff lets you say something like
#
# CALLED_FROM_SETUP=true \
# make -f config/envsetup.make dumpvar-TARGET_OUT
# or
# CALLED_FROM_SETUP=true \
# make -f config/envsetup.make dumpvar-abs-HOST_OUT_EXECUTABLES
#
# The plain (non-abs) version just dumps the value of the named variable.
# The "abs" version will treat the variable as a path, and dumps an
# absolute path to it.
#
dumpvar_goals := \
$(strip $(patsubst dumpvar-%,%,$(filter dumpvar-%,$(MAKECMDGOALS))))
# MAKECMDGOALS 变量就是传进来的目标dumpvar-abs-$1或 dumpvar-$1。
ifdef dumpvar_goals
ifneq ($(words $(dumpvar_goals)),1)
$(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)))
ifdef absolute_dumpvar
dumpvar_goals := $(patsubst abs-%,%,$(dumpvar_goals))
ifneq ($(filter /%,$($(dumpvar_goals))),)
DUMPVAR_VALUE := $($(dumpvar_goals))
else
DUMPVAR_VALUE := $(PWD)/$($(dumpvar_goals))
endif
dumpvar_target := dumpvar-abs-$(dumpvar_goals)
else
DUMPVAR_VALUE := $($(dumpvar_goals))
dumpvar_target := dumpvar-$(dumpvar_goals)
endif
.PHONY: $(dumpvar_target)
$(dumpvar_target):
@echo $(DUMPVAR_VALUE)
# DUMPVAR_VALE其实就是dumpvar-$1中的$1变量的值。
endif # dumpvar_goals
ifneq ($(dumpvar_goals),report_config)
# PRINT_BUILD_CONFIG 在envsetup.mk中被定义为true
PRI