歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android編譯之解析main.mk

Android編譯之解析main.mk

日期:2017/3/1 10:03:21   编辑:Linux編程

上篇文章講了Android的基本的make腳本文件格式,雖然android的mk腳本很復雜,但我們可以把它拆解成基本格式來看。下面我我們來一步一步的解析android的make文件流程。

相關閱讀:Android編譯之make腳本 http://www.linuxidc.com/Linux/2013-01/77863.htm

首先是根目錄的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

Copyright © Linux教程網 All Rights Reserved