Source:
执行envsetup.sh脚本,加载环境变量到当前终端,然后执行envsetup.sh中定义的函数指令,最后调用source_vendorsetup加载其他目录下的vendorsetup.sh脚本
function hmm() {
cat <<EOF
Run "m help" for help with the build system itself.
Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
- lunch: lunch <product_name>-<build_variant>
Selects <product_name> as the product to build, and <build_variant> as the variant to
build, and stores those selections in the environment to be read by subsequent
invocations of 'm' etc.
- tapas: tapas [<App1> <App2> ...] [arm|x86|mips|arm64|x86_64|mips64] [eng|userdebug|user]
- croot: Changes directory to the top of the tree, or a subdirectory thereof.
- 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.
- provision: Flash device with all required partitions. Options will be passed on to fastboot.
- 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.
- mangrep: Greps on all local AndroidManifest.xml files.
- mgrep: Greps on all local Makefiles files.
- sepgrep: Greps on all local sepolicy files.
- sgrep: Greps on all local source files.
- godir: Go to the directory containing a file.
- allmod: List all modules.
- gomod: Go to the directory containing a module.
- pathmod: Get the directory containing a module.
- refreshmod: Refresh list of modules for allmod/gomod.
Environment options:
- SANITIZE_HOST: Set to 'true' to use ASAN for all host modules. Note that
ASAN_OPTIONS=detect_leaks=0 will be set by default until the
build is leak-check clean.
- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Look at the source to view more functions. The complete list is:
EOF
local T=$(gettop)
local A=""
local i
for i in `cat $T/build/envsetup.sh | sed -n "/^[[:blank:]]*function /s/function \([a-z_]*\).*/\1/p" | sort | uniq`; do
A="$A $i"
done
echo $A
}
hmm():
调用另一个函数gettop获得Android源码的根目录
通过sed命令解析envsetup.sh文件,获得在里面定义的所有函数的名称
source_vendorsetup():
function source_vendorsetup() {
allowed=
for f in $(find -L device vendor product -maxdepth 4 -name 'allowed-vendorsetup_sh-files' 2>/dev/null | sort); do
if [ -n "$allowed" ]; then
echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
echo " $allowed"
echo " $f"
return
fi
allowed="$f"
done
allowed_files=
[ -n "$allowed" ] && allowed_files=$(cat "$allowed")
for dir in device vendor product; do
for f in $(test -d $dir && \
find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
echo "including $f"; . "$f"//@1
else
echo "ignoring $f, not in $allowed"
fi
done
done
}
循环遍历device/vendor/product目录下的vendorsetup.sh文件,通过@1命令将他们加载到当前终端。
Lunch:
选择当前编译平台,当前平台定义的product获得途径:通过envsetup.sh中定义的print_lunch_menu函数,在这个函数中调用get_build_var,并传递参数COMMON_LUNCH_CHOICES来获取当前所有平台,COMMOM_LUNCH_CHOICES定义在AndroidProducts.mk中
Android/build/envsetup
function print_lunch_menu()
{
local uname=$(uname)
echo
echo "You're building on" $uname
echo
echo "Lunch menu... pick a combo:"
local i=1
local choice
for choice in $(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
do
echo " $i. $choice"
i=$(($i+1))
done
echo
}
android/build/soong/soong_ui.bash
android/build/soong/cmd/soong_ui/main.go
android/build/soong/ui/build/finder.go
COMMON_LUNCH_CHOICES := \
trinket-userdebug \
trinket-user
当用户执行lunch命令时,加载print_lunch_menu函数会调用get_build_var函数,并传入COMMON_LUNCH_CHOICES参数,在get_build_var函数中执行完命令又会跳转到soong_ui.bash文件中,然后在soong_ui.bash再次跳转到main.go文件,最后在main_go中调用方finder.go中的findsource()函数,通过传入的COMMON_LUNCH_CHOICES参数去device,vendor,product目录下查找AndroidProducts.mk中的平台
函数最终返回所有AndroidProducts.mk中定义的PRODUCT_MAKEFILES的值,然后更新COMMON_LUNCH_CHOICES参数,将所有AndroidProducts.mk中定义的COMMON_LUNCH_CHOICES的值都写入
make:
以上两步执行完之后,可以使用 make 或者 mm 等命令进行编译,但是他们的流程有差异。按照 linux 的 Makefile 机制,make 命令是直接执行当前目录下的 makefile 脚本,在 Android 系统中首先会判断有没有 soong_ui.bash 脚本,如果有则用 soong 编译系统,否则使用 Makefile 机制。m 、mm 或者 mmm 这些命令是 Android 在 envsetup.sh 中定义的。
在 Android 7.0 之后 google 采用了 soong 编译系统,直接执行当前目录下的 android.mk 或者 android.bp 脚本。
make命令:
function make()
{
_wrap_build $(get_make_command "$@") "$@"
}
function get_make_command()
{
# If we're in the top of an Android tree, use soong_ui.bash instead of make
if [ -f build/soong/soong_ui.bash ]; then
# Always use the real make if -C is passed in
for arg in "$@"; do
if [[ $arg == -C* ]]; then
echo command make
return
fi
done
echo build/soong/soong_ui.bash --make-mode
else
echo command make
fi
}
m
:m
命令用于编译整个Android源代码树。在源代码根目录下执行m
命令时,会遍历所有模块,并根据其依赖关系进行编译。这个命令会调用Makefile来执行编译操作。
mm
: mm
命令用于编译指定模块。在某个模块的目录下执行mm
命令时,会编译该模块及其依赖的其他模块。这个命令同样也是调用Makefile来执行编译操作。
mmm
: mmm
命令用于编译指定路径下的模块。可以在任意路径下执行mmm
命令,并指定要编译的模块路径。这个命令同样也是调用Makefile来执行编译操作。
mma:当前目录有修改时,可以用这个命令重新编译
mmma:指定目录有修改时,可以用这个命令重新编译
// 编译整个安卓系统
function m()
(
_trigger_build "all-modules" "$@"
)
// 编译当前目录下的模块,当前目录下需要有 Android.mk, 否则就往上找最近的 Android.mk 文件
function mm()
(
_trigger_build "modules-in-a-dir-no-deps" "$@"
)
// 编译指定目录下的模块
function mmm()
(
_trigger_build "modules-in-dirs-no-deps" "$@"
)
// 当前目录有修改时,可以用这个命令重新编译
function mma()
(
_trigger_build "modules-in-a-dir" "$@"
)
// 指定目录有修改时,可以用这个命令重新编译
function mmma()
(
_trigger_build "modules-in-dirs" "$@"
)
function _trigger_build()
(
local -r bc="$1"; shift
if T="$(gettop)"; then
_wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
else
>&2 echo "Couldn't locate the top of the tree. Try setting TOP."
return 1
fi
)
Android.mk:
定义当前模块位置:
LOCAL_PATH := $(call my-dir)
my-dir是系统提供的宏函数,返回当前文件所在路径
清除LOCAL_XXX变量
include $(CLEAR_VARS)
需要编译的文件
LOCAL_SRC_FILES := $(call all-subdir-java-files)
$(call all-java-files-under, src) :获取指定目录下的所有 Java 文件。 $(call all-c-files-under, src) :获取指定目录下的所有 C 语言文件。 $(call all-Iaidl-files-under, src) :获取指定目录下的所有 AIDL 文件。 $(call all-makefiles-under, folder):获取指定目录下的所有 Make 文件。
直接跟上需要编译的文件路径
LOCAL_SRC_FILES :=src/com/example/test/MainActivity.java \ src/com/example/test/Demo1.java \ src/com/example/test/Demo2.java // 需要在文件最后面加上以下语句,指明 LOCAL_PATH 目录。 include $(call all-java-file-under,$(LOCAL_PATH))
或者
LOCAL_SRC_FILES := $(LOCAL_PATH)/src/com/example/test/MainActivity.java
定义编译生成的模块名称
LOCAL_PACKAGE_NAME :=
编译的标签
LOCAL_MODULE_TAGS := optional(默认)
签名属性
LOCAL_CERTIFICATE :=
platform:该 APK 完成一些系统的核心功能。经过对系统中存在的文件夹的访问测试。
shared:该APK需要和 home/contacts 进程共享数据。
media:该APK是 media/download 系统中的一环。
testkey:普通APK,默认情况下使用。
引用jar包
引用静态jar包
LOCAL_STATIC_JAVA_LIBRARIES := jar1
引用动态jar包
LOCAL_JAVA_LIBRARIES := jar2
编译类型
编译 APK
include $(BUILD_PACKAGE)
编译成静态库
include $(BUILD_STATIC_LIBRARY)
编译成动态库
include $(BUILD_SHARED_LIBRARY)
编译成可执行程序
include $(BUILD_EXECUTABLE)
编译成Java静态库
include $(BUILD_STATIC_JAVA_LIBRARY)
拷贝到本地编译
include $(BUILD_MULTI_PREBUILT)
指定生成目录
LOCAL_MODULE_PATH := $(TARGET_OUT)/
$(TARGET_OUT) 表示 /system,后续路径自行添加
如$(TARGET_OUT_DATA_APPS) 表示 data/app 目录。
Android.bp:
cc_binary:编译二进制可执行文件
android_app:编译APK
cc_library_shared:编译动态库
name:对应Android.mk的LOCAL_PACKAGE_NAME
srcs:源码路径,对应Android.mk的LOCAL_SRC_FILES
resource_dirs:资源文件路径,对应Android.mk的LOCAL_RESOURCE_DIR
static_libs:依赖的java库,对应Android.mk的LOCAL_STATIC_JAVA_LIBRARIES和LOCAL_STATIC_LIBRARIES
certificate:签名属性,对应Android.mk的LOCAL_CERTIFICATE