来自http://blog.csdn.net/zjg555543/article/details/7804701
一、概述
1. Multiple Targets
2. Non-Recursive Make
3. Rapid Compile-Test Cycles
4. Both Environment and Config File Based Settings
二、 Android编译系统的架构
分析Android编译系统,你会发现,Android编译系统完成的并不仅仅是对目标(主机)系统二进制文件、java应用程序的编译、链接、打包等,而且还有包括生成各种依赖关系、确保某个模块的修改引起相依赖的文件的重新编译链接,甚至还包括目标文件系统的生成,配置文件的生成等,因此Android编译系统具有支持多架构(linux-x86、windows、arm等)、多语言(汇编、C、C++、Java等)、多目标、多编译方式。这些目标和结构决定其架构也很重要。
Android编译系统集中于build/core下,几个很重要的*.mk文件如下:
main.mk(主控Makefile)
base_rules.mk(对一些Makefile的变量规则化)
config.mk(关于编译参数、编译命令的一些配置)
definations.mk(定义了很多编译系统中用到的宏,相当于函数库)
Makefile(这个Makefile特指build/core下的Makefile,此文件主要控制生成system.img,ramdisk.img,userdata.img,以及recorvery p_w_picpath,sdk等)
Binary.mk(控制如何生成目标文件)
Clear_vars.mk(清除编译系统中用到的临时变量)
Combo/linux-arm.mk(控制如何生成linux-arm二进制文件,包括ARM相关的编译器,编译参数等的设置)
Copy_headers.mk(将头文件拷贝到指定目录)
分散于各个目录下的Android.mk(控制生成局部模块的源码,名称所需头文件路径,依赖库等特殊选项)
Build/envsetup.mk(编译环境初始化,定义一些实用的shell函数,方便编译使用)
以上几个主要的文件,可以按照社会分工打一个比方:
Main.mk是总统,是老大,承担了很多工作。
Makefile是副总统,辅佐老大Main.mk
Base_rules.mk是交警,让不规则的东西,变得规则。
Config.mk是省长,规定了各个人民群众该如何行事
Definations.mk是图书馆管理员
Binary.mk应该属于村长了,规定每个人该如何行事
Clear_vars.mk应该属于×××公司的工人吧
1. SHELL设置
2. 编译环境配置
3. 编译环境检查
4. 包含必要的宏
5. 根据make参数设置编译时的变量
6. 包含需要编译的Android.mk
7. 设置编译系统Target:prerequisites 控制整个编译流程
下面对上面几点进行必要解释:
有了前面小节对Android编译系统架构的分析,如果需要修改Android编译系统,就可以不至于盲目找问题,修改代码了。今天分析下main.mk,看它能为我们Android编译提供什么有价值的信息,以及如何自己定制我们的Android编译系统。
.SUFFIXES:
# If a rule fails, delete $@.
.DELETE_ON_ERROR:
# Figure out where we are.
#TOP := $(dir $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)))
#TOP := $(patsubst %/,%,$(TOP))
# TOPDIR is the normal variable you should use, because
# if we are executing relative to the current directory
# it can be "", whereas TOP must be "." which causes
# pattern matching probles when make strips off the
# trailing "./" from paths in various places.
#ifeq ($(TOP),.)
#TOPDIR :=
#else
#TOPDIR := $(TOP)/
#endif
2 对MAKE_VERSION的检查。对这个项目的编译,需要make的版本大于或等于3.81。
# check for broken versions of make
ifeq (0,$(shell expr $$(echo $(MAKE_VERSION) | sed "s/[^0-9/.].*//") />= 3.81))
$(warning ********************************************************************************)
$(warning * You are using version $(MAKE_VERSION) of make.)
$(warning * You must upgrade to version 3.81 or greater.)
$(warning * see file://$(shell pwd)/docs/development-environment/machine-setup.html)
$(warning ********************************************************************************)
$(error stopping)
endif
TOP := .
TOPDIR :=
droid。虽然后面的include $(BUILD_SYSTEM)/config.mk写在默认目标droid依赖之后,但该目标之后的语句
# 都是要执行的,这由Makefile的语法决定的。
# This is the default target. It must be the first declared target.
DEFAULT_GOAL := droid
$(DEFAULT_GOAL):
# Set up various standard variables based on configuration
# and host information.
4 包含config.mk:include $(BUILD_SYSTEM)/config.mk,config.mk是一个总括性的文件,它里面定义了
# 各种module编译所需要使用的HOST工具以及如何来编译各种模块。
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
5 包含cleanbuild.mk,cleanbuild.mk完成三件事,一是载入所有的CleanSpec.mk,二是检查更新clean_steps.mk
# 和previous_build_config.mk,避免不同编译间的互相干扰。最后是,定义installclean和dataclean。
include $(BUILD_SYSTEM)/cleanbuild.mk
VERSION_CHECK_SEQUENCE_NUMBER := 1
-include $(OUT_DIR)/versions_checked.mk
ifneq ($(VERSION_CHECK_SEQUENCE_NUMBER),$(VERSIONS_CHECKED))
$(info Checking build tools versions...)
6 编译环境若不是windows的话,会检查Case-insensitive filesystems。(这里好像无关紧要)
ifneq ($(HOST_OS),windows)
ifneq ($(HOST_OS)-$(HOST_ARCH),darwin-ppc)
# check for a case sensitive file system
ifneq (a,$(shell mkdir -p $(OUT_DIR) ; /
echo a > $(OUT_DIR)/casecheck.txt; /
echo B > $(OUT_DIR)/CaseCheck.txt; /
cat $(OUT_DIR)/casecheck.txt))
$(warning ************************************************************)
$(warning You are building on a case-insensitive filesystem.)
$(warning Please move your source tree to a case-sensitive filesystem.)
$(warning ************************************************************)
$(error Case-insensitive filesystems not supported)
endif
endif
endif
# Make sure that there are no spaces in the absolute path; the
# build system can't deal with them.
7 检查当前编译的绝对路径是否纯在空格:ifneq ($(words $(shell pwd)),1)若是存在空格,会抛出警告并退出。
ifneq ($(words $(shell pwd)),1)
$(warning ************************************************************)
$(warning You are building in a directory whose absolute path contains)
$(warning a space character:)
$(warning $(space))
$(warning "$(shell pwd)")
$(warning $(space))
$(warning Please move your source tree to a path that does not contain)
$(warning any spaces.)
$(warning ************************************************************)
$(error Directory names containing spaces not supported)
endif
# The windows build server currently uses 1.6. This will be fixed.
ifneq ($(HOST_OS),windows)
# Check for the correct version of java
java_version := $(shell java -version 2>&1 | head -n 1 | grep '[ "]1/.5[/. "$$]')
ifeq ($(strip $(java_version)),)
$(info ************************************************************)
$(info You are attempting to build with the incorrect version)
$(info of java.)
$(info $(space))
$(info Your version is: $(shell java -version 2>&1 | head -n 1).)
$(info The correct version is: 1.5.)
$(info $(space))
$(info Please follow the machine setup instructions at)
$(info $(space)$(space)$(space)$(space)http://source.android.com/download)
$(info ************************************************************)
$(error stop)
endif
# Check for the correct version of javac
8 对编译的java版本的检查,在windows编译是使用1.6的,在别的平台上要使用1.5,如下。
javac_version := $(shell javac -version 2>&1 | head -n 1 | grep '[ "]1/.5[/. "$$]')
ifeq ($(strip $(javac_version)),)
$(info ************************************************************)
$(info You are attempting to build with the incorrect version)
$(info of javac.)
$(info $(space))
$(info Your version is: $(shell javac -version 2>&1 | head -n 1).)
$(info The correct version is: 1.5.)
$(info $(space))
$(info Please follow the machine setup instructions at)
$(info $(space)$(space)$(space)$(space)http://source.android.com/download)
$(info ************************************************************)
$(error stop)
endif
endif # windows
$(shell echo 'VERSIONS_CHECKED := $(VERSION_CHECK_SEQUENCE_NUMBER)' /
> $(OUT_DIR)/versions_checked.mk)
endif
# These are the modifier targets that don't do anything themselves, but
# change the behavior of the build.
# (must be defined before including definitions.make)
INTERNAL_MODIFIER_TARGETS := showcommands checkbuild
# Bring in standard build system definitions.
9 包含definition.mk: definations.mk中定义了很多编译系统中用到的宏,这些宏在编译时需要经常调用,
# 因此在编译的很靠前的阶段,就将之包含了进来。
include $(BUILD_SYSTEM)/definitions.mk
ifneq ($(filter eng user userdebug tests,$(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
10 TARGET_BUILD_VARIANT在buildspec.mk设定,这个参数决定了要安装的模块。(重要,在后面会用到)
ifneq ($(filter-out $(INTERNAL_VALID_VARIANTS),$(TARGET_BUILD_VARIANT)),)
$(info ***************************************************************)
$(info ***************************************************************)
$(info Invalid variant: $(TARGET_BUILD_VARIANT)
$(info Valid values are: $(INTERNAL_VALID_VARIANTS)
$(info ***************************************************************)
$(info ***************************************************************)
$(error stopping)
endif
###
### In this section we set up the things that are different
### between the build variants
###
11 对is_sdk_build的设置,根据在主目录里执行make时,make后面的参数决定。
is_sdk_build :=
ifneq ($(filter sdk,$(MAKECMDGOALS)),)
is_sdk_build := true
endif
ifneq ($(filter sdk_addon,$(MAKECMDGOALS)),)
is_sdk_build := true
endif
## user/userdebug ##
12 查看buildspec.mk文件中定义的TARGET_BUILD_VARIANT是否已经定义有userdebug或user,将结果保存到user_variant
# user_variant这个参数在后面会用很多,关系到后面ADDITIONAL_DEFAULT_PROPERTIES,tags_to_install,enable_target_debugging
# 这些重要参数的设置。ADDITIONAL_BUILD_PROPERTIES用于指定被编译项的文件特性。
user_variant := $(filter userdebug user,$(TARGET_BUILD_VARIANT))
enable_target_debugging := true
ifneq (,$(user_variant))
# Target is secure in user builds.
ADDITIONAL_DEFAULT_PROPERTIES += ro.secure=1
tags_to_install := user
ifeq ($(user_variant),userdebug)
# Pick up some extra useful tools
tags_to_install += debug
else
# Disable debugging in plain user builds.
enable_target_debugging :=
endif
# TODO: Always set WITH_DEXPREOPT (for user builds) once it works on OSX.
# Also, remove the corresponding block in config/product_config.make.
ifeq ($(HOST_OS)-$(WITH_DEXPREOPT_buildbot),linux-true)
WITH_DEXPREOPT := true
endif
# Disallow mock locations by default for user builds
ADDITIONAL_DEFAULT_PROPERTIES += ro.allow.mock.location=0
else # !user_variant
# Turn on checkjni for non-user builds.
ADDITIONAL_BUILD_PROPERTIES += ro.kernel.android.checkjni=1
# Set device insecure for non-user builds.
ADDITIONAL_DEFAULT_PROPERTIES += ro.secure=0
# Allow mock locations by default for non user builds
ADDITIONAL_DEFAULT_PROPERTIES += ro.allow.mock.location=1
endif # !user_variant
13 根据enable_target_debugging的设置(在前面设定),设置DDITIONAL_DEFAULT_PROPERTIES的值。
ifeq (true,$(strip $(enable_target_debugging)))
# Target is more debuggable and adbd is on by default
ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=1 persist.service.adb.enable=1
# Include the debugging/testing OTA keys in this build.
INCLUDE_TEST_OTA_KEYS := true
else # !enable_target_debugging
# Target is less debuggable and adbd is off by default
ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=0 persist.service.adb.enable=0
endif # !enable_target_debugging
## eng ##
14 若是buildspec.mk文件中定义的TARGET_BUILD_VARIANT为eng,再对tags_to_install和ADDITIONAL_BUILD_PROPERTIES
# 进行设置。
ifeq ($(TARGET_BUILD_VARIANT),eng)
tags_to_install := user debug eng
# Don't require the setup wizard on eng builds
ADDITIONAL_BUILD_PROPERTIES := $(filter-out ro.setupwizard.mode=%,/
$(call collapse-pairs, $(ADDITIONAL_BUILD_PROPERTIES))) /
ro.setupwizard.mode=OPTIONAL
endif
## tests ##
15 若是buildspec.mk文件中定义的TARGET_BUILD_VARIANT为test,再对tags_to_install进行配置
ifeq ($(TARGET_BUILD_VARIANT),tests)
tags_to_install := user debug eng tests
endif
## sdk ##
16 若是对sdk的编译,会对MAKECMDGOALS的值进行检查,在编译sdk的时候,是不允许再另多其他中间目标的。
# 若是是sdk的边缘,还会tags_to_install,ADDITIONAL_BUILD_PROPERTIES添加一些值。
ifdef is_sdk_build
ifneq ($(words $(filter-out $(INTERNAL_MODIFIER_TARGETS),$(MAKECMDGOALS))),1)
$(error The 'sdk' target may not be specified with any other targets)
endif
# TODO: this should be eng I think. Since the sdk is built from the eng
# variant.
tags_to_install := user debug eng
ADDITIONAL_BUILD_PROPERTIES += xmpp.auto-presence=true
ADDITIONAL_BUILD_PROPERTIES += ro.config.nocheckin=yes
else # !sdk
endif
## precise GC ##
17 查看PRODUCT_TAGS是否有定义dalvik.gc.type-precise,PRODUCT_TAGS这个变量在product_config.mk定义。
# 若是有定义的话还要在ADDITIONAL_BUILD_PROPERTIES添加dalvik.vm.dexopt-flags这个配置信息。
ifneq ($(filter dalvik.gc.type-precise,$(PRODUCT_TAGS)),)
# Enabling type-precise GC results in larger optimized DEX files. The
# additional storage requirements for ".odex" files can cause /system
# to overflow on some devices, so this is configured separately for
# each product.
ADDITIONAL_BUILD_PROPERTIES += dalvik.vm.dexopt-flags=m=y
endif
#########################################################################################################
#(18)若是PRODUCT_COPY_FILES没有记录apns-conf.xml,要确保添加上去。
# 注意:这几个文件的作用如下:
# apns-conf_sdk.xml如:$ANDROID_SRC_HOME/development/data/etc/apns-conf_sdk.xml
# --主要用于生成system/etc/apns-conf.xml文件,生成过程本质上就是原文拷贝
# apns.xml如:$ANDROID_SRC_HOME/frameworks/base/core/res/res/xml/apns.xml
# --该文件实际上在apn的设置方面没有实际意义,然而在android apn设置的逻辑方面却很重要,尤其是里面的version的值。
# apns-conf.xml如:$ANDROID_SRC_HOME/out/target/product/generic/system/etc/apns-conf.xml
# --该文件里记录的内容最终打包到system.img中:
##########################################################################################################
#
# Install an apns-conf.xml file if one's not already being installed.
ifeq (,$(filter %:system/etc/apns-conf.xml, $(PRODUCT_COPY_FILES)))
PRODUCT_COPY_FILES += /
development/data/etc/apns-conf_sdk.xml:system/etc/apns-conf.xml
ifeq ($(filter eng tests,$(TARGET_BUILD_VARIANT)),)
$(warning implicitly installing apns-conf_sdk.xml)
endif
endif