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

 1 function hmm() {
 2   # 打印帮助信息
 3   cat <<EOF
 4   Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
 5   - lunch:   lunch <product_name>-<build_variant>
 6   - tapas:   tapas [<App1> <App2> ...] [arm|x86|mips|armv5|arm64|x86_64|mips64] [eng|userdebug|user]
 7   - croot:   Changes directory to the top of the tree.
 8   - m:       Makes from the top of the tree.
 9   - mm:      Builds all of the modules in the current directory, but not their dependencies.
10  - mmm:     Builds all of the modules in the supplied directories, but not their dependencies.
11             To limit the modules being built use the syntax: mmm dir/:target1,target2.
12  - mma:     Builds all of the modules in the current directory, and their dependencies.
13  - mmma:    Builds all of the modules in the supplied directories, and their dependencies.
14  - cgrep:   Greps on all local C/C++ files.
15  - ggrep:   Greps on all local Gradle files.
16  - jgrep:   Greps on all local Java files.
17  - resgrep: Greps on all local res/*.xml files.
18  - sgrep:   Greps on all local source files.
19  - godir:   Go to the directory containing a file.
20  
21  Look at the source to view more functions. The complete list is:
22  EOF
23  # gettop是脚本内函数,功能:返回当前android代码树的顶层路径。前提是当前路径位于android代码树中
24      T=$(gettop)
25  # A变量 局部变量化
26      local A
27      A=""
28  # 查看envsetup.sh内容,且行首开始空格、制表符任意次,替换function为匹配的子串(函数名),按字符排升序、去掉重复
29      for i in `cat $T/build/envsetup.sh | sed -n "/^[ 	]*function /s/function ([a-z_]*).*/1/p" | sort | uniq`; do
30  # 替换出来的函数名 追加在A后 并打印
31        A="$A $i"
32      done
33      echo $A
34  }

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

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

 题外话:

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

 

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

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

继续引入dumpvar.mk

 1 # MAKECMDGOALS 变量是传进来的目标dumpvar-abs-$1或 dumpvar-$1的abs-$1或$1
 2 dumpvar_goals :=   
 3         $(strip $(patsubst dumpvar-%,%,$(filter dumpvar-%,$(MAKECMDGOALS))))   
 4 ifdef dumpvar_goals
 5 
 6   ifneq ($(words $(dumpvar_goals)),1)
 7     $(error Only one "dumpvar-" goal allowed. Saw "$(MAKECMDGOALS)")
 8   endif
 9 
10   # If the goal is of the form "dumpvar-abs-VARNAME", then
11   # treat VARNAME as a path and return the absolute path to it.
12   absolute_dumpvar := $(strip $(filter abs-%,$(dumpvar_goals)))
13 # 是否有abs
14   ifdef absolute_dumpvar
15     dumpvar_goals := $(patsubst abs-%,%,$(dumpvar_goals))
16     ifneq ($(filter /%,$($(dumpvar_goals))),)
17       DUMPVAR_VALUE := $($(dumpvar_goals))
18     else
19       DUMPVAR_VALUE := $(PWD)/$($(dumpvar_goals))
20     endif
21 # get_abs_build_var()中dumpvar-abs-$1对应的
22     dumpvar_target := dumpvar-abs-$(dumpvar_goals)
23   else
24     DUMPVAR_VALUE := $($(dumpvar_goals))
25 # get_build_var()中dumpvar-$1对应的
26     dumpvar_target := dumpvar-$(dumpvar_goals)
27   endif
28 
29 .PHONY: $(dumpvar_target)
30 # 执行目标与执行语句
31 $(dumpvar_target):
32         @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.2$ 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.2$ get_abs_build_var TARGET_PRODUCT
/work/android-5.0.2/full
原文地址:https://www.cnblogs.com/pokerface/p/5742088.html