android编译分析之10—config.mk

来源:互联网 发布:python 输出 编辑:程序博客网 时间:2024/06/07 13:33

config.mk可以说是android编译系统中关于配置环境的一个总的makefile,定义了编译环境的方方面面。执行完config.mk就完成了android编译系统的所有准备工作,即准备好了所有的编译需要的全局变量,下一步直接执行make,即可产生镜像文件。


首先,定义了一些变量和目录,

# Only use ANDROID_BUILD_SHELL to wrap around bash.# DO NOT use other shells such as zsh.ifdef ANDROID_BUILD_SHELLSHELL := $(ANDROID_BUILD_SHELL)else# Use bash, not whatever shell somebody has installed as /bin/sh# This is repeated from main.mk, since envsetup.sh runs this file# directly.SHELL := /bin/bashendif# Utility variables.empty :=space := $(empty) $(empty)comma := ,# Note that make will eat the newline just before endef.define newlineendef# Unfortunately you can't simply define backslash as \ or \\.backslash := \abackslash := $(patsubst %a,%,$(backslash))# Tell python not to spam the source tree with .pyc files.  This# only has an effect on python 2.6 and above.export PYTHONDONTWRITEBYTECODE := 1# Standard source directories.SRC_DOCS:= $(TOPDIR)docs# TODO: Enforce some kind of layering; only add include paths#       when a module links against a particular library.# TODO: See if we can remove most of these from the global list.SRC_HEADERS := \    $(TOPDIR)system/core/include \    $(TOPDIR)system/media/audio/include \    $(TOPDIR)hardware/libhardware/include \    $(TOPDIR)hardware/libhardware_legacy/include \    $(TOPDIR)hardware/ril/include \    $(TOPDIR)libnativehelper/include \    $(TOPDIR)frameworks/native/include \    $(TOPDIR)frameworks/native/opengl/include \    $(TOPDIR)frameworks/av/include \    $(TOPDIR)frameworks/base/includeSRC_HOST_HEADERS:=$(TOPDIR)tools/includeSRC_LIBRARIES:= $(TOPDIR)libsSRC_SERVERS:= $(TOPDIR)serversSRC_TARGET_DIR := $(TOPDIR)build/targetSRC_API_DIR := $(TOPDIR)prebuilts/sdk/apiSRC_SYSTEM_API_DIR := $(TOPDIR)prebuilts/sdk/system-api# Some specific paths to toolsSRC_DROIDDOC_DIR := $(TOPDIR)build/tools/droiddoc

然后调用pathmap.mk,包含一些hard code的路径,

# Various mappings to avoid hard-coding paths all over the placeinclude $(BUILD_SYSTEM)/pathmap.mk

下面的这些变量在我们写Android.mk时经常会用到,一般都是通过include调用,其实都是一些makefile文件,

# ################################################################ Build system internal files# ###############################################################BUILD_COMBOS:= $(BUILD_SYSTEM)/comboCLEAR_VARS:= $(BUILD_SYSTEM)/clear_vars.mkBUILD_HOST_STATIC_LIBRARY:= $(BUILD_SYSTEM)/host_static_library.mkBUILD_HOST_SHARED_LIBRARY:= $(BUILD_SYSTEM)/host_shared_library.mkBUILD_STATIC_LIBRARY:= $(BUILD_SYSTEM)/static_library.mkBUILD_SHARED_LIBRARY:= $(BUILD_SYSTEM)/shared_library.mkBUILD_EXECUTABLE:= $(BUILD_SYSTEM)/executable.mkBUILD_HOST_EXECUTABLE:= $(BUILD_SYSTEM)/host_executable.mkBUILD_PACKAGE:= $(BUILD_SYSTEM)/package.mkBUILD_PHONY_PACKAGE:= $(BUILD_SYSTEM)/phony_package.mkBUILD_HOST_PREBUILT:= $(BUILD_SYSTEM)/host_prebuilt.mkBUILD_PREBUILT:= $(BUILD_SYSTEM)/prebuilt.mkBUILD_MULTI_PREBUILT:= $(BUILD_SYSTEM)/multi_prebuilt.mkBUILD_JAVA_LIBRARY:= $(BUILD_SYSTEM)/java_library.mkBUILD_STATIC_JAVA_LIBRARY:= $(BUILD_SYSTEM)/static_java_library.mkBUILD_HOST_JAVA_LIBRARY:= $(BUILD_SYSTEM)/host_java_library.mkBUILD_DROIDDOC:= $(BUILD_SYSTEM)/droiddoc.mkBUILD_COPY_HEADERS := $(BUILD_SYSTEM)/copy_headers.mkBUILD_NATIVE_TEST := $(BUILD_SYSTEM)/native_test.mkBUILD_NATIVE_BENCHMARK := $(BUILD_SYSTEM)/native_benchmark.mkBUILD_HOST_NATIVE_TEST := $(BUILD_SYSTEM)/host_native_test.mkBUILD_SHARED_TEST_LIBRARY := $(BUILD_SYSTEM)/shared_test_lib.mkBUILD_HOST_SHARED_TEST_LIBRARY := $(BUILD_SYSTEM)/host_shared_test_lib.mkBUILD_STATIC_TEST_LIBRARY := $(BUILD_SYSTEM)/static_test_lib.mkBUILD_HOST_STATIC_TEST_LIBRARY := $(BUILD_SYSTEM)/host_static_test_lib.mkBUILD_NOTICE_FILE := $(BUILD_SYSTEM)/notice_files.mkBUILD_HOST_DALVIK_JAVA_LIBRARY := $(BUILD_SYSTEM)/host_dalvik_java_library.mkBUILD_HOST_DALVIK_STATIC_JAVA_LIBRARY := $(BUILD_SYSTEM)/host_dalvik_static_java_library.mk

如果make的目标中包含showcommands,则会打印完整的命令。

# The 'showcommands' goal says to show the full command# lines being executed, instead of a short message about# the kind of operation being done.SHOW_COMMANDS:= $(filter showcommands,$(MAKECMDGOALS))

定义了一些全局变量,cflags,常用文件后缀等,

# ################################################################ Set common values# ################################################################ These can be changed to modify both host and device modules.COMMON_GLOBAL_CFLAGS:= -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arithCOMMON_RELEASE_CFLAGS:= -DNDEBUG -UDEBUGCOMMON_GLOBAL_CPPFLAGS:= $(COMMON_GLOBAL_CFLAGS) -Wsign-promo -std=gnu++11COMMON_RELEASE_CPPFLAGS:= $(COMMON_RELEASE_CFLAGS)GLOBAL_CFLAGS_NO_OVERRIDE :=  \    -Werror=int-to-pointer-cast \    -Werror=pointer-to-int-cast \GLOBAL_CPPFLAGS_NO_OVERRIDE :=# Set the extensions used for various packagesCOMMON_PACKAGE_SUFFIX := .zipCOMMON_JAVA_PACKAGE_SUFFIX := .jarCOMMON_ANDROID_PACKAGE_SUFFIX := .apk# list of flags to turn specific warnings in to errorsTARGET_ERROR_FLAGS := -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-pointifdef TMPDIRJAVA_TMPDIR_ARG := -Djava.io.tmpdir=$(TMPDIR)elseJAVA_TMPDIR_ARG :=endif

buildspec.mk还没研究咋用,-include可以看出不是强制必须存在的,

# ---------------------------------------------------------------# Try to include buildspec.mk, which will try to set stuff up.# If this file doesn't exist, the environment variables will# be used, and if that doesn't work, then the default is an# arm buildifndef ANDROID_BUILDSPECANDROID_BUILDSPEC := $(TOPDIR)buildspec.mkendif-include $(ANDROID_BUILDSPEC)

include了envsetup.mk,前面已经介绍,设置了很多编译相关的变量。

# ---------------------------------------------------------------# Define most of the global variables.  These are the ones that# are specific to the user's build configuration.include $(BUILD_SYSTEM)/envsetup.mk

在调用findleaves.py时设置的选项,

# Pruned directory options used when using findleaves.py# See envsetup.mk for a description of SCAN_EXCLUDE_DIRSFIND_LEAVES_EXCLUDES := $(addprefix --prune=, $(OUT_DIR) $(SCAN_EXCLUDE_DIRS) .repo .git)

由于config.mk太长,后面不一一列出来了,找重点的分析,中间主要都是些host和target 编译链相关的变量,还有一些编译需要用的应用程序,例如aapt,aidl等等。
其中调用select.mk主要是为了设置host和target编译链相关的变量。

#下面调用select.mk设置host端,即编译机的编译链,gcc ar等combo_target := HOST_combo_2nd_arch_prefix :=include $(BUILD_SYSTEM)/combo/select.mk# Load the 2nd host arch if it's needed.ifdef HOST_2ND_ARCHcombo_target := HOST_# 2ND_combo_2nd_arch_prefix := $(HOST_2ND_ARCH_VAR_PREFIX)include $(BUILD_SYSTEM)/combo/select.mkendif# on windows, the tools have .exe at the end, and we depend on the# host config stuff being done first# 下面调用select.mk设置target端,即目标机的编译链,gcc ar等combo_target := TARGET_combo_2nd_arch_prefix :=include $(BUILD_SYSTEM)/combo/select.mk# Load the 2nd target arch if it's needed.ifdef TARGET_2ND_ARCHcombo_target := TARGET_combo_2nd_arch_prefix := $(TARGET_2ND_ARCH_VAR_PREFIX)include $(BUILD_SYSTEM)/combo/select.mkendif

然后include了ccache,为了加快再次编译速度,关于ccache后续再研究。

include $(BUILD_SYSTEM)/ccache.mk

接着include了javac.mk,用来设置java编译器,

include $(BUILD_SYSTEM)/combo/javac.mk

同时在android6.0以上默认使用jack进行编译,在javac.mk中,如果ANDROID_COMPILE_WITH_JACK默认没设置,就设置为true。

ifndef ANDROID_COMPILE_WITH_JACK# Defines if compilation with jack is enabled by default.ANDROID_COMPILE_WITH_JACK := trueendif

接着包含了clang相关的makefile,关于clang后续再研究。

include $(BUILD_SYSTEM)/clang/config.mk
注:Clang是LLVM的前端,可以用来编译C,C++,ObjectiveC等语言。传统的编译器通常分为三个部分,前端(frontEnd),优化器(Optimizer)和后端(backEnd)。在编译过程中,前端主要负责词法和语法分析,将源代码转化为抽象语法树;优化器则是在前端的基础上,对得到的中间代码进行优化,使代码更加高效;后端则是将已经优化的中间代码转化为针对各自平台的机器代码。Clang则是以LLVM为后端的一款高效易用,并且与IDE结合很好的编译前端。

最后include了dumpvar.mk,就是专为打印变量值的makefile,前面已经分析过。

include $(BUILD_SYSTEM)/dumpvar.mk

envsetup.sh中的一些函数,例如check_product,最终就是make config.mk,而make的目标为dumpvar-*。

0 0