Building System之 get_abs_build_var() && get_build_var()

本文介绍了Building System中的两个关键函数get_abs_build_var()和get_build_var(),它们都位于build/envsetup.sh。这两个shell函数主要在config.mk和dumpvar.mk文件中操作,用于获取和打印构建系统的变量值。例如:ANDROID_GCC_PREBUILTS, PRODUCT_OUT, HOST_OUT等。通过对config.mk和dumpvar.mk的分析,揭示了这些变量如何定义和使用。" 81231778,7354457,决策树算法详解:理论+图解+Python实现,"['机器学习', '数据挖掘', '决策树算法']
摘要由CSDN通过智能技术生成


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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值