Android编译之解析main.mk


上篇文章讲了Android的基本的make脚本文件格式,虽然android的mk脚本很复杂,但我们可以把它拆解成基本格式来看。下面我我们来一步一步的解析android的make文件流程。

相关阅读:Android编译之make脚本

首先是根目录的makefile文件

### DO NOT EDIT THIS FILE ###
include build/core/main.mk
### DO NOT EDIT THIS FILE ###

好简单额,意思很直观明了。就是包含了 build/core/main.mk文件。那么我们接下来将解析大头,就是main.mk文件。由于main.mk很大,我们一步一步的来解析它,当然我们只解析关键的步骤,因为android的脚本文件里含有丰富的注释(这是一个好习惯)。所以我只是解析下关键部分。

# This is the default target.  It must be the first declared target.
.PHONY: droid
DEFAULT_GOAL := droid
$(DEFAULT_GOAL):

这里设定了一个默认的target名称droid。当用户直接调用make命令的时候,生成的target就为droid。但是这里并没有定义编译条件。

# Set up various standard variables based on configuration
# and host information.
include $(BUILD_SYSTEM)/config.mk

# This allows us to force a clean build - included after the config.make
# environment setup is done, but before we generate any dependencies.  This
# file does the rm -rf inline so the deps which are all done below will
# be generated correctly
include $(BUILD_SYSTEM)/cleanbuild.mk

VERSION_CHECK_SEQUENCE_NUMBER := 2
-include $(OUT_DIR)/versions_checked.mk
ifneq ($(VERSION_CHECK_SEQUENCE_NUMBER),$(VERSIONS_CHECKED))

这里是包含了其它的mk文件。

后面有一大段是检测系统环境的,比如检测系统是否为windows(android只能在linux和苹果机上编译),java的版本。

ifneq ($(filter eng user userdebug,$(MAKECMDGOALS)),)
$(info ***************************************************************)
$(info ***************************************************************)
$(info Don't pass '$(filter eng user userdebug tests,$(MAKECMDGOALS))' on \
  the make command line.)
# XXX The single quote on this line fixes gvim's syntax highlighting.
# Without which, the rest of this file is impossible to read.
$(info Set TARGET_BUILD_VARIANT in buildspec.mk, or use lunch or)
$(info choosecombo.)
$(info ***************************************************************)
$(info ***************************************************************)
$(error stopping)
endif

检查product的类型,product只能为user,eng,userdebug,tests四种。

# Can't use first-makefiles-under here because
# --mindepth=2 makes the prunes not work.
subdir_makefiles := \
 $(shell build/tools/findleaves.py --prune=out --prune=.repo --prune=.git $(subdirs) Android.mk)

调用build/tools/findleaves.py脚本来遍历所有目录下的Android.mk文件,不包括out,repo和git目录。

include $(subdir_makefiles)

将所查找到的Android.mk包含进来。

接下来大部分就是定义了一下其它的target。

# All the droid stuff, in directories
.PHONY: files
files: prebuilt \
        $(modules_to_install) \
        $(modules_to_check) \
        $(INSTALLED_ANDROID_INFO_TXT_TARGET)

比如files就是编译过程中生成的一些临时文件等等。至此。main.mk的大致流程算上讲完了。后续将继续分析如何增加一个product

相关内容