Android編譯過程詳解(二)

通過上篇文章,我們分析瞭編譯android時source build/envsetup.sh和lunch命令,在執行完上述兩個命令後, 我們就可以進行編譯android瞭。

 

1. make

執行make命令的結果就是去執行當前目錄下的Makefile文件,我們來看下它的內容:

[html]  1 ### DO NOT EDIT THIS FILE ### 
2 include build/core/main.mk 
3 ### DO NOT EDIT THIS FILE ### 
  1 ### DO NOT EDIT THIS FILE ###
  2 include build/core/main.mk
  3 ### DO NOT EDIT THIS FILE ###

呵呵,看到上面 的內容,我們都會笑,這是我見過最簡單的Makefile瞭,我們再看下build/core/main.mk

在main.mk裡,定義瞭變量TOPDIR,TOP為當前目錄,BUILD_SYSTEM為build/core目錄。

在49行,包含瞭build/core/config.mk文件。

後面的代碼是check環境 變量,所有的Makefile都通過build/core/main.mk這個文件組織在一起,它定義瞭一個默認goals:droid,當我們在TOP目錄下,敲Make實際上就等同於我們執行make droid。當Make include所有的文件,完成對所有make我文件的解析以後就會尋找生成droid的規則,依次生成它的依賴,直到所有滿足的模塊被編譯好,然後使用相應的工具打包成相應的img。這兒不是我們的重點,不再多說。

 

2. build/core/config.mk

該文件被main.mk包含。

定義瞭以下環境變量:

[plain] 16 SRC_HEADERS := \ 
17     $(TOPDIR)system/core/include \ 
18     $(TOPDIR)hardware/libhardware/include \ 
19     $(TOPDIR)hardware/libhardware_legacy/include \ 
20     $(TOPDIR)hardware/ril/include \ 
21     $(TOPDIR)dalvik/libnativehelper/include \ 
22     $(TOPDIR)frameworks/base/include \ 
23     $(TOPDIR)frameworks/base/opengl/include \ 
24     $(TOPDIR)external/skia/include 
25 SRC_HOST_HEADERS:=$(TOPDIR)tools/include 
26 SRC_LIBRARIES:= $(TOPDIR)libs 
27 SRC_SERVERS:= $(TOPDIR)servers 
28 SRC_TARGET_DIR := $(TOPDIR)build/target 
29 SRC_API_DIR := $(TOPDIR)frameworks/base/api 
 16 SRC_HEADERS := \
 17     $(TOPDIR)system/core/include \
 18     $(TOPDIR)hardware/libhardware/include \
 19     $(TOPDIR)hardware/libhardware_legacy/include \
 20     $(TOPDIR)hardware/ril/include \
 21     $(TOPDIR)dalvik/libnativehelper/include \
 22     $(TOPDIR)frameworks/base/include \
 23     $(TOPDIR)frameworks/base/opengl/include \
 24     $(TOPDIR)external/skia/include
 25 SRC_HOST_HEADERS:=$(TOPDIR)tools/include
 26 SRC_LIBRARIES:= $(TOPDIR)libs
 27 SRC_SERVERS:= $(TOPDIR)servers
 28 SRC_TARGET_DIR := $(TOPDIR)build/target
 29 SRC_API_DIR := $(TOPDIR)frameworks/base/api
[plain] …..然後定義瞭下面幾個重要的編譯命令 
…..然後定義瞭下面幾個重要的編譯命令[plain] 43 CLEAR_VARS:= $(BUILD_SYSTEM)/clear_vars.mk 
 44 BUILD_HOST_STATIC_LIBRARY:= $(BUILD_SYSTEM)/host_static_library.mk 
 45 BUILD_HOST_SHARED_LIBRARY:= $(BUILD_SYSTEM)/host_shared_library.mk 
 46 BUILD_STATIC_LIBRARY:= $(BUILD_SYSTEM)/static_library.mk 
 47 BUILD_RAW_STATIC_LIBRARY := $(BUILD_SYSTEM)/raw_static_library.mk 
 48 BUILD_SHARED_LIBRARY:= $(BUILD_SYSTEM)/shared_library.mk 
 49 BUILD_EXECUTABLE:= $(BUILD_SYSTEM)/executable.mk 
 50 BUILD_RAW_EXECUTABLE:= $(BUILD_SYSTEM)/raw_executable.mk 
 51 BUILD_HOST_EXECUTABLE:= $(BUILD_SYSTEM)/host_executable.mk 
 52 BUILD_PACKAGE:= $(BUILD_SYSTEM)/package.mk 
 53 BUILD_HOST_PREBUILT:= $(BUILD_SYSTEM)/host_prebuilt.mk 
 54 BUILD_PREBUILT:= $(BUILD_SYSTEM)/prebuilt.mk 
 55 BUILD_MULTI_PREBUILT:= $(BUILD_SYSTEM)/multi_prebuilt.mk 
 56 BUILD_JAVA_LIBRARY:= $(BUILD_SYSTEM)/java_library.mk 
 57 BUILD_STATIC_JAVA_LIBRARY:= $(BUILD_SYSTEM)/static_java_library.mk 
 58 BUILD_HOST_JAVA_LIBRARY:= $(BUILD_SYSTEM)/host_java_library.mk 
 59 BUILD_DROIDDOC:= $(BUILD_SYSTEM)/droiddoc.mk 
 60 BUILD_COPY_HEADERS := $(BUILD_SYSTEM)/copy_headers.mk 
 61 BUILD_KEY_CHAR_MAP := $(BUILD_SYSTEM)/key_char_map.mk 
 43 CLEAR_VARS:= $(BUILD_SYSTEM)/clear_vars.mk
 44 BUILD_HOST_STATIC_LIBRARY:= $(BUILD_SYSTEM)/host_static_library.mk
 45 BUILD_HOST_SHARED_LIBRARY:= $(BUILD_SYSTEM)/host_shared_library.mk
 46 BUILD_STATIC_LIBRARY:= $(BUILD_SYSTEM)/static_library.mk
 47 BUILD_RAW_STATIC_LIBRARY := $(BUILD_SYSTEM)/raw_static_library.mk
 48 BUILD_SHARED_LIBRARY:= $(BUILD_SYSTEM)/shared_library.mk
 49 BUILD_EXECUTABLE:= $(BUILD_SYSTEM)/executable.mk
 50 BUILD_RAW_EXECUTABLE:= $(BUILD_SYSTEM)/raw_executable.mk
 51 BUILD_HOST_EXECUTABLE:= $(BUILD_SYSTEM)/host_executable.mk
 52 BUILD_PACKAGE:= $(BUILD_SYSTEM)/package.mk
 53 BUILD_HOST_PREBUILT:= $(BUILD_SYSTEM)/host_prebuilt.mk
 54 BUILD_PREBUILT:= $(BUILD_SYSTEM)/prebuilt.mk
 55 BUILD_MULTI_PREBUILT:= $(BUILD_SYSTEM)/multi_prebuilt.mk
 56 BUILD_JAVA_LIBRARY:= $(BUILD_SYSTEM)/java_library.mk
 57 BUILD_STATIC_JAVA_LIBRARY:= $(BUILD_SYSTEM)/static_java_library.mk
 58 BUILD_HOST_JAVA_LIBRARY:= $(BUILD_SYSTEM)/host_java_library.mk
 59 BUILD_DROIDDOC:= $(BUILD_SYSTEM)/droiddoc.mk
 60 BUILD_COPY_HEADERS := $(BUILD_SYSTEM)/copy_headers.mk
 61 BUILD_KEY_CHAR_MAP := $(BUILD_SYSTEM)/key_char_map.mk
上述命令變量其實是對應的mk文件名,所有的Android.mk文件裡基本上都包含上述命令變量,如:

CLEAR_VARS:用來清除之前定義的環境變量

BUILD_SHARED_LIBRARY:用來指定編譯動態庫過程

 

[plain] 109 # ————————————————————— 
110 # Define most of the global variables.  These are the ones that 
111 # are specific to the user's build configuration. 
112 include $(BUILD_SYSTEM)/envsetup.mk 
113  
114 # Boards may be defined under $(SRC_TARGET_DIR)/board/$(TARGET_DEVICE) 
115 # or under vendor/*/$(TARGET_DEVICE).  Search in both places, but 
116 # make sure only one exists. 
117 # Real boards should always be associated with an OEM vendor. 
118 board_config_mk := \ 
119     $(strip $(wildcard \ 
120         $(SRC_TARGET_DIR)/board/$(TARGET_DEVICE)/BoardConfig.mk \ 
121         vendor/*/$(TARGET_DEVICE)/BoardConfig.mk \ 
122     )) 
123 ifeq ($(board_config_mk),) 
124   $(error No config file found for TARGET_DEVICE $(TARGET_DEVICE)) 
125 endif 
126 ifneq ($(words $(board_config_mk)),1) 
127   $(error Multiple board config files for TARGET_DEVICE $(TARGET_DEVICE): $(board_config_mk)) 
128 endif 
129 include $(board_config_mk) 
130 TARGET_DEVICE_DIR := $(patsubst %/,%,$(dir $(board_config_mk))) 
131 board_config_mk := 
109 # —————————————————————
110 # Define most of the global variables.  These are the ones that
111 # are specific to the user's build configuration.
112 include $(BUILD_SYSTEM)/envsetup.mk
113
114 # Boards may be defined under $(SRC_TARGET_DIR)/board/$(TARGET_DEVICE)
115 # or under vendor/*/$(TARGET_DEVICE).  Search in both places, but
116 # make sure only one exists.
117 # Real boards should always be associated with an OEM vendor.
118 board_config_mk := \
119     $(strip $(wildcard \
120         $(SRC_TARGET_DIR)/board/$(TARGET_DEVICE)/BoardConfig.mk \
121         vendor/*/$(TARGET_DEVICE)/BoardConfig.mk \
122     ))
123 ifeq ($(board_config_mk),)
124   $(error No config file found for TARGET_DEVICE $(TARGET_DEVICE))
125 endif
126 ifneq ($(words $(board_config_mk)),1)
127   $(error Multiple board config files for TARGET_DEVICE $(TARGET_DEVICE): $(board_config_mk))
128 endif
129 include $(board_config_mk)
130 TARGET_DEVICE_DIR := $(patsubst %/,%,$(dir $(board_config_mk)))
131 board_config_mk :=
112行又包含瞭另外一個重要的mk文件envsetup.mk,我們來看一下。

 

3. envsetup.mk

[plain] 25 ifeq ($(TARGET_PRODUCT),)    #判斷TARGET_PRODUCT是否為空, 
26 ifeq ($(TARGET_SIMULATOR),true) 
27 TARGET_PRODUCT := sim 
28 else 
29 TARGET_PRODUCT := generic 
30 endif 
31 endif 
 25 ifeq ($(TARGET_PRODUCT),)    #判斷TARGET_PRODUCT是否為空,
 26 ifeq ($(TARGET_SIMULATOR),true)
 27 TARGET_PRODUCT := sim
 28 else
 29 TARGET_PRODUCT := generic
 30 endif
 31 endif
第25行,判斷TARGET_PRODUCT是否為空,根據上一節分析可知,TARGET_PRODUCT=fs100
[plain] 34 # the variant — the set of files that are included for a build 
35 ifeq ($(strip $(TARGET_BUILD_VARIANT)),) 
36 TARGET_BUILD_VARIANT := eng 
37 endif 
38  
39 # Read the product specs so we an get TARGET_DEVICE and other 
40 # variables that we need in order to locate the output files. 
41 include $(BUILD_SYSTEM)/product_config.mk 
 34 # the variant — the set of files that are included for a build
 35 ifeq ($(strip $(TARGET_BUILD_VARIANT)),)
 36 TARGET_BUILD_VARIANT := eng
 37 endif
 38
 39 # Read the product specs so we an get TARGET_DEVICE and other
 40 # variables that we need in order to locate the output files.
 41 include $(BUILD_SYSTEM)/product_config.mk在41行又包含瞭product_config.mk文件,等會我們再分析它,先看下面的

[plain] 148 # ————————————————————— 
149 # figure out the output directories 
150  
151 ifeq (,$(strip $(OUT_DIR))) 
152 OUT_DIR := $(TOPDIR)out 
153 endif 
154  
155 DEBUG_OUT_DIR := $(OUT_DIR)/debug 
156  
157 # Move the host or target under the debug/ directory 
158 # if necessary. 
159 TARGET_OUT_ROOT_release := $(OUT_DIR)/target 
160 TARGET_OUT_ROOT_debug := $(DEBUG_OUT_DIR)/target 
161 TARGET_OUT_ROOT := $(TARGET_OUT_ROOT_$(TARGET_BUILD_TYPE)) 
162  
… 
184 PRODUCT_OUT := $(TARGET_PRODUCT_OUT_ROOT)/$(TARGET_DEVICE) 
187  
188 HOST_OUT_EXECUTABLES:= $(HOST_OUT)/bin 
189 HOST_OUT_SHARED_LIBRARIES:= $(HOST_OUT)/lib 
190 HOST_OUT_JAVA_LIBRARIES:= $(HOST_OUT)/framework 
191 HOST_OUT_SDK_ADDON := $(HOST_OUT)/sdk_addon 
… 
200 TARGET_OUT_INTERMEDIATES := $(PRODUCT_OUT)/obj 
201 TARGET_OUT_HEADERS:= $(TARGET_OUT_INTERMEDIATES)/include 
202 TARGET_OUT_INTERMEDIATE_LIBRARIES := $(TARGET_OUT_INTERMEDIATES)/lib 
203 TARGET_OUT_COMMON_INTERMEDIATES := $(TARGET_COMMON_OUT_ROOT)/obj 
204  
205 TARGET_OUT := $(PRODUCT_OUT)/system 
206 TARGET_OUT_EXECUTABLES:= $(TARGET_OUT)/bin 
207 TARGET_OUT_OPTIONAL_EXECUTABLES:= $(TARGET_OUT)/xbin 
208 TARGET_OUT_SHARED_LIBRARIES:= $(TARGET_OUT)/lib 
209 TARGET_OUT_JAVA_LIBRARIES:= $(TARGET_OUT)/framework 
210 TARGET_OUT_APPS:= $(TARGET_OUT)/app 
211 TARGET_OUT_KEYLAYOUT := $(TARGET_OUT)/usr/keylayout 
212 TARGET_OUT_KEYCHARS := $(TARGET_OUT)/usr/keychars 
213 TARGET_OUT_ETC := $(TARGET_OUT)/etc 
214 TARGET_OUT_STATIC_LIBRARIES:= $(TARGET_OUT_INTERMEDIATES)/lib 
215 TARGET_OUT_NOTICE_FILES:=$(TARGET_OUT_INTERMEDIATES)/NOTICE_FILES 
216  
217 TARGET_OUT_DATA := $(PRODUCT_OUT)/data 
218 TARGET_OUT_DATA_EXECUTABLES:= $(TARGET_OUT_EXECUTABLES) 
219 TARGET_OUT_DATA_SHARED_LIBRARIES:= $(TARGET_OUT_SHARED_LIBRARIES) 
220 TARGET_OUT_DATA_JAVA_LIBRARIES:= $(TARGET_OUT_JAVA_LIBRARIES) 
221 TARGET_OUT_DATA_APPS:= $(TARGET_OUT_DATA)/app 
222 TARGET_OUT_DATA_KEYLAYOUT := $(TARGET_OUT_KEYLAYOUT) 
223 TARGET_OUT_DATA_KEYCHARS := $(TARGET_OUT_KEYCHARS) 
224 TARGET_OUT_DATA_ETC := $(TARGET_OUT_ETC) 
225 TARGET_OUT_DATA_STATIC_LIBRARIES:= $(TARGET_OUT_STATIC_LIBRARIES) 
226  
227 TARGET_OUT_UNSTRIPPED := $(PRODUCT_OUT)/symbols 
228 TARGET_OUT_EXECUTABLES_UNSTRIPPED := $(TARGET_OUT_UNSTRIPPED)/system/bin 
229 TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED := $(TARGET_OUT_UNSTRIPPED)/system/lib 
230 TARGET_ROOT_OUT_UNSTRIPPED := $(TARGET_OUT_UNSTRIPPED) 
231 TARGET_ROOT_OUT_SBIN_UNSTRIPPED := $(TARGET_OUT_UNSTRIPPED)/sbin 
232 TARGET_ROOT_OUT_BIN_UNSTRIPPED := $(TARGET_OUT_UNSTRIPPED)/bin 
233  
234 TARGET_ROOT_OUT := $(PRODUCT_OUT)/root 
235 TARGET_ROOT_OUT_BIN := $(TARGET_ROOT_OUT)/bin 
236 TARGET_ROOT_OUT_SBIN := $(TARGET_ROOT_OUT)/sbin 
237 TARGET_ROOT_OUT_ETC := $(TARGET_ROOT_OUT)/etc 
238 TARGET_ROOT_OUT_USR := $(TARGET_ROOT_OUT)/usr 
239  
240 TARGET_RECOVERY_OUT := $(PRODUCT_OUT)/recovery 
241 TARGET_RECOVERY_ROOT_OUT := $(TARGET_RECOVERY_OUT)/root 
242  
243 TARGET_SYSLOADER_OUT := $(PRODUCT_OUT)/sysloader 
244 TARGET_SYSLOADER_ROOT_OUT := $(TARGET_SYSLOADER_OUT)/root 
245 TARGET_SYSLOADER_SYSTEM_OUT := $(TARGET_SYSLOADER_OUT)/root/system 
246  
247 TARGET_INSTALLER_OUT := $(PRODUCT_OUT)/installer 
248 TARGET_INSTALLER_DATA_OUT := $(TARGET_INSTALLER_OUT)/data 
249 TARGET_INSTALLER_ROOT_OUT := $(TARGET_INSTALLER_OUT)/root 
250 TARGET_INSTALLER_SYSTEM_OUT := $(TARGET_INSTALLER_OUT)/root/system 
148 # —————————————————————
149 # figure out the output directories
150
151 ifeq (,$(strip $(OUT_DIR)))
152 OUT_DIR := $(TOPDIR)out
153 endif
154
155 DEBUG_OUT_DIR := $(OUT_DIR)/debug
156
157 # Move the host or target under the debug/ directory
158 # if necessary.
159 TARGET_OUT_ROOT_release := $(OUT_DIR)/target
160 TARGET_OUT_ROOT_debug := $(DEBUG_OUT_DIR)/target
161 TARGET_OUT_ROOT := $(TARGET_OUT_ROOT_$(TARGET_BUILD_TYPE))
162

184 PRODUCT_OUT := $(TARGET_PRODUCT_OUT_ROOT)/$(TARGET_DEVICE)
187
188 HOST_OUT_EXECUTABLES:= $(HOST_OUT)/bin
189 HOST_OUT_SHARED_LIBRARIES:= $(HOST_OUT)/lib
190 HOST_OUT_JAVA_LIBRARIES:= $(HOST_OUT)/framework
191 HOST_OUT_SDK_ADDON := $(HOST_OUT)/sdk_addon

200 TARGET_OUT_INTERMEDIATES := $(PRODUCT_OUT)/obj
201 TARGET_OUT_HEADERS:= $(TARGET_OUT_INTERMEDIATES)/include
202 TARGET_OUT_INTERMEDIATE_LIBRARIES := $(TARGET_OUT_INTERMEDIATES)/lib
203 TARGET_OUT_COMMON_INTERMEDIATES := $(TARGET_COMMON_OUT_ROOT)/obj
204
205 TARGET_OUT := $(PRODUCT_OUT)/system
206 TARGET_OUT_EXECUTABLES:= $(TARGET_OUT)/bin
207 TARGET_OUT_OPTIONAL_EXECUTABLES:= $(TARGET_OUT)/xbin
208 TARGET_OUT_SHARED_LIBRARIES:= $(TARGET_OUT)/lib
209 TARGET_OUT_JAVA_LIBRARIES:= $(TARGET_OUT)/framework
210 TARGET_OUT_APPS:= $(TARGET_OUT)/app
211 TARGET_OUT_KEYLAYOUT := $(TARGET_OUT)/usr/keylayout
212 TARGET_OUT_KEYCHARS := $(TARGET_OUT)/usr/keychars
213 TARGET_OUT_ETC := $(TARGET_OUT)/etc
214 TARGET_OUT_STATIC_LIBRARIES:= $(TARGET_OUT_INTERMEDIATES)/lib
215 TARGET_OUT_NOTICE_FILES:=$(TARGET_OUT_INTERMEDIATES)/NOTICE_FILES
216
217 TARGET_OUT_DATA := $(PRODUCT_OUT)/data
218 TARGET_OUT_DATA_EXECUTABLES:= $(TARGET_OUT_EXECUTABLES)
219 TARGET_OUT_DATA_SHARED_LIBRARIES:= $(TARGET_OUT_SHARED_LIBRARIES)
220 TARGET_OUT_DATA_JAVA_LIBRARIES:= $(TARGET_OUT_JAVA_LIBRARIES)
221 TARGET_OUT_DATA_APPS:= $(TARGET_OUT_DATA)/app
222 TARGET_OUT_DATA_KEYLAYOUT := $(TARGET_OUT_KEYLAYOUT)
223 TARGET_OUT_DATA_KEYCHARS := $(TARGET_OUT_KEYCHARS)
224 TARGET_OUT_DATA_ETC := $(TARGET_OUT_ETC)
225 TARGET_OUT_DATA_STATIC_LIBRARIES:= $(TARGET_OUT_STATIC_LIBRARIES)
226
227 TARGET_OUT_UNSTRIPPED := $(PRODUCT_OUT)/symbols
228 TARGET_OUT_EXECUTABLES_UNSTRIPPED := $(TARGET_OUT_UNSTRIPPED)/system/bin
229 TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED := $(TARGET_OUT_UNSTRIPPED)/system/lib
230 TARGET_ROOT_OUT_UNSTRIPPED := $(TARGET_OUT_UNSTRIPPED)
231 TARGET_ROOT_OUT_SBIN_UNSTRIPPED := $(TARGET_OUT_UNSTRIPPED)/sbin
232 TARGET_ROOT_OUT_BIN_UNSTRIPPED := $(TARGET_OUT_UNSTRIPPED)/bin
233
234 TARGET_ROOT_OUT := $(PRODUCT_OUT)/root
235 TARGET_ROOT_OUT_BIN := $(TARGET_ROOT_OUT)/bin
236 TARGET_ROOT_OUT_SBIN := $(TARGET_ROOT_OUT)/sbin
237 TARGET_ROOT_OUT_ETC := $(TARGET_ROOT_OUT)/etc
238 TARGET_ROOT_OUT_USR := $(TARGET_ROOT_OUT)/usr
239
240 TARGET_RECOVERY_OUT := $(PRODUCT_OUT)/recovery
241 TARGET_RECOVERY_ROOT_OUT := $(TARGET_RECOVERY_OUT)/root
242
243 TARGET_SYSLOADER_OUT := $(PRODUCT_OUT)/sysloader
244 TARGET_SYSLOADER_ROOT_OUT := $(TARGET_SYSLOADER_OUT)/root
245 TARGET_SYSLOADER_SYSTEM_OUT := $(TARGET_SYSLOADER_OUT)/root/system
246
247 TARGET_INSTALLER_OUT := $(PRODUCT_OUT)/installer
248 TARGET_INSTALLER_DATA_OUT := $(TARGET_INSTALLER_OUT)/data
249 TARGET_INSTALLER_ROOT_OUT := $(TARGET_INSTALLER_OUT)/root
250 TARGET_INSTALLER_SYSTEM_OUT := $(TARGET_INSTALLER_OUT)/root/system上面的代碼是指定瞭目標輸出代碼的位置和主機輸出代碼的位置,重要的幾個如下:

[plain] PRODUCT_OUT = 這個的結果要根據product_config.mk文件內容來決定,其實是out/target/product/fs100/ 
TARGET_OUT = $(PRODUCT_OUT)/system 
TARGET_OUT_EXECUTABLES =  $(PRODUCT_OUT)/system/bin 
TARGET_OUT_SHARED_LIBRARIES =  $(PRODUCT_OUT)/system/lib 
TARGET_OUT_JAVA_LIBRARIES = $(PRODUCT_OUT)/system/framework 
TARGET_OUT_APPS = $(PRODUCT_OUT)/system/app 
TARGET_OUT_ETC = $(PRODUCT_OUT)/system/etc 
TARGET_OUT_STATIC_LIBRARIES  = $(PRODUCT_OUT)/obj/lib 
TARGET_OUT_DATA = $(PRODUCT_OUT)/data 
TARGET_OUT_DATA_APPS = $(PRODUCT_OUT)/data/app 
TARGET_ROOT_OUT = $(PRODUCT_OUT)/root 
TARGET_ROOT_OUT_BIN = $(PRODUCT_OUT)/bin 
TARGET_ROOT_OUT_SBIN  = $(PRODUCT_OUT)/system/sbin 
TARGET_ROOT_OUT_ETC = $(PRODUCT_OUT)/system/etc 
TARGET_ROOT_OUT_USR = $(PRODUCT_OUT)/system/usr 
PRODUCT_OUT = 這個的結果要根據product_config.mk文件內容來決定,其實是out/target/product/fs100/
TARGET_OUT = $(PRODUCT_OUT)/system
TARGET_OUT_EXECUTABLES =  $(PRODUCT_OUT)/system/bin
TARGET_OUT_SHARED_LIBRARIES =  $(PRODUCT_OUT)/system/lib
TARGET_OUT_JAVA_LIBRARIES = $(PRODUCT_OUT)/system/framework
TARGET_OUT_APPS = $(PRODUCT_OUT)/system/app
TARGET_OUT_ETC = $(PRODUCT_OUT)/system/etc
TARGET_OUT_STATIC_LIBRARIES  = $(PRODUCT_OUT)/obj/lib
TARGET_OUT_DATA = $(PRODUCT_OUT)/data
TARGET_OUT_DATA_APPS = $(PRODUCT_OUT)/data/app
TARGET_ROOT_OUT = $(PRODUCT_OUT)/root
TARGET_ROOT_OUT_BIN = $(PRODUCT_OUT)/bin
TARGET_ROOT_OUT_SBIN  = $(PRODUCT_OUT)/system/sbin
TARGET_ROOT_OUT_ETC = $(PRODUCT_OUT)/system/etc
TARGET_ROOT_OUT_USR = $(PRODUCT_OUT)/system/usr

總結下:

envsetup.mk文件主要包含瞭product_config.mk文件,然後指定瞭編譯時要輸出的所有文件的OUT目錄。

 

4. build/core/product_config.mk

[plain] 157 include $(BUILD_SYSTEM)/product.mk 
… 
160 # Read in all of the product definitions specified by the AndroidProducts.mk 
161 # files in the tree. 
162 # 
163 #TODO: when we start allowing direct pointers to product files, 
164 #    guarantee that they're in this list. 
165 $(call import-products, $(get-all-product-makefiles)) 
166 $(check-all-products) 
… 
170 # Convert a short name like "sooner" into the path to the product 
171 # file defining that product. 
172 # 
173 INTERNAL_PRODUCT := $(call resolve-short-product-name, $(TARGET_PRODUCT)) 
… 
176 # Find the device that this product maps to. 
177 TARGET_DEVICE := $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_DEVICE) 
157 include $(BUILD_SYSTEM)/product.mk

160 # Read in all of the product definitions specified by the AndroidProducts.mk
161 # files in the tree.
162 #
163 #TODO: when we start allowing direct pointers to product files,
164 #    guarantee that they're in this list.
165 $(call import-products, $(get-all-product-makefiles))
166 $(check-all-products)

170 # Convert a short name like "sooner" into the path to the product
171 # file defining that product.
172 #
173 INTERNAL_PRODUCT := $(call resolve-short-product-name, $(TARGET_PRODUCT))

176 # Find the device that this product maps to.
177 TARGET_DEVICE := $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_DEVICE)157行,我靠,又包含瞭product.mk文件

165行,調用函數import-products, $(get-all-product-makefiles),這兒我們看上面的註釋:

    Read in all of the product definitions specified by the AndroidProducts.mk files in the tree.
    TODO: when we start allowing direct pointers to product files, guarantee that they're in this list.

    意思是說:讀取指定的目錄下所有的AndrodProducts.mk文件中定義的產品信息

    其實get-all-product-makefiles返回所有的產品文件xxx.mk

    import-products函數去驗證這些產品配置文件是否都包含有必須的配置信息,細節後面分析。

173行調用瞭resolve-short-product-name函數,它將返回TARGET_PRODUCT產品的配置文件目錄,並賦給INTERNAL_PRODUCT

    也就是說:

    INTERNAL_PRODUCT = vendor/farsight/products/fs100.mk

    TARGET_DEVICE = fs100

    如果調試看其結果,可以在167行,將#$(dump-product)取消註釋

    然後在175行添加: $(info $(INTERNAL_PRODUCT))

    在178行添加: $(info $(TARGET_DEVICE )),查看調試結果。

總結一下:

product_config.mk主要讀取vendor目錄下不同廠商自己定義的AndrodProducts.mk文件,從該文件裡取得所有產品的配置文件,然後再根據lunch選擇的編譯項TARGET_PRODUCT,找到與之對應的配置文件,然後設置TARGET_DEVICE變量,用於後續編譯。

 

 

5. build/core/product.mk

[plain] 17 # 
18 # Functions for including AndroidProducts.mk files 
19 # 
20  
21 # 
22 # Returns the list of all AndroidProducts.mk files. 
23 # $(call ) isn't necessary. 
24 # 
25 define <strong>_find-android-products-files</strong> 
26 $(shell test -d vendor && find vendor -maxdepth 6 -name AndroidProducts.mk) \ 
27   $(SRC_TARGET_DIR)/product/AndroidProducts.mk 
28 endef 
29  
30 # 
31 # Returns the sorted concatenation of all PRODUCT_MAKEFILES 
32 # variables set in all AndroidProducts.mk files. 
33 # $(call ) isn't necessary. 
34 # 
35 define <strong>get-all-product-makefiles</strong> 
36 $(sort \ 
37   $(foreach f,$(_find-android-products-files), \ 
38     $(eval PRODUCT_MAKEFILES :=) \ 
39     $(eval LOCAL_DIR := $(patsubst %/,%,$(dir $(f)))) \ 
40     $(eval include $(f)) \ 
41     $(PRODUCT_MAKEFILES) \ 
42    ) \ 
43   $(eval PRODUCT_MAKEFILES :=) \ 
44   $(eval LOCAL_DIR :=) \ 
45  ) 
46 endef 
 17 #
 18 # Functions for including AndroidProducts.mk files
 19 #
 20
 21 #
 22 # Returns the list of all AndroidProducts.mk files.
 23 # $(call ) isn't necessary.
 24 #
 25 define <strong>_find-android-products-files</strong>
 26 $(shell test -d vendor && find vendor -maxdepth 6 -name AndroidProducts.mk) \
 27   $(SRC_TARGET_DIR)/product/AndroidProducts.mk
 28 endef
 29
 30 #
 31 # Returns the sorted concatenation of all PRODUCT_MAKEFILES
 32 # variables set in all AndroidProducts.mk files.
 33 # $(call ) isn't necessary.
 34 #
 35 define <strong>get-all-product-makefiles</strong>
 36 $(sort \
 37   $(foreach f,$(_find-android-products-files), \
 38     $(eval PRODUCT_MAKEFILES :=) \
 39     $(eval LOCAL_DIR := $(patsubst %/,%,$(dir $(f)))) \
 40     $(eval include $(f)) \
 41     $(PRODUCT_MAKEFILES) \
 42    ) \
 43   $(eval PRODUCT_MAKEFILES :=) \
 44   $(eval LOCAL_DIR :=) \
 45  )
 46 endef通過註釋可知,本文件中主要是一些用來處理AndroidProduct.mk的函數
_find-android-products-files:

    用來獲得vendor目錄下,所有名字為AndroidProduct.mk的文件列表。
get-all-product-makefiles:

    用來獲得所有AndroidProduct.mk文件裡定義的PRODUCT_MAKEFILES的值(其實是產品文件路徑名)。

在vendor目錄下,每個公司目錄下都會存在一個AndroidProduct.mk文件,這個文件是用來定義這個公司的產品列表,每個產品用<product_name>.mk來表示
如Android給的示例:

[plain] vendor/sample/products/AndroidProduct.mk 
vendor/sample/products/AndroidProduct.mk其內容如下:

 

[plain] 1 # 
 2 # This file should set PRODUCT_MAKEFILES to a list of product makefiles 
 3 # to expose to the build system.  LOCAL_DIR will already be set to 
 4 # the directory containing this file.  
 5 # 
 6 # This file may not rely on the value of any variable other than 
 7 # LOCAL_DIR; do not use any conditionals, and do not look up the 
 8 # value of any variable that isn't set in this file or in a file that 
 9 # it includes. 
10 # 
11  
12 PRODUCT_MAKEFILES := \ 
13   $(LOCAL_DIR)/sample_addon.mk 
  1 #
  2 # This file should set PRODUCT_MAKEFILES to a list of product makefiles
  3 # to expose to the build system.  LOCAL_DIR will already be set to
  4 # the directory containing this file.
  5 #
  6 # This file may not rely on the value of any variable other than
  7 # LOCAL_DIR; do not use any conditionals, and do not look up the
  8 # value of any variable that isn't set in this file or in a file that
  9 # it includes.
 10 #
 11
 12 PRODUCT_MAKEFILES := \
 13   $(LOCAL_DIR)/sample_addon.mk裡面隻定義瞭一個產品配置文件,即當前目錄下的sample_addon.mk:
[plain] 1 # List of apps and optional libraries (Java and native) to put in the add-on system image. 
2 PRODUCT_PACKAGES := \ 
3     PlatformLibraryClient \ 
4     com.example.android.platform_library \ 
5     libplatform_library_jni 
  1 # List of apps and optional libraries (Java and native) to put in the add-on system image.
  2 PRODUCT_PACKAGES := \
  3     PlatformLibraryClient \
  4     com.example.android.platform_library \
  5     libplatform_library_jni上述文件裡定義瞭產品相關個性化信息,如,PRODUCT_PACKAGES表示要在當前產品裡添加一些安裝包。
由此可見,get-all-product-makefiles函數,其實就是返回瞭當前公司裡全部的產品對應的mk文件列表。

總結:

如果用戶想個性定制自己的產品,應該有以下流程,包含上一節內容:

1. 創建公司目錄

    #mkdir vendor/farsight

2. 創建一個vendorsetup.sh文件,將當前產品編譯項添加到lunch裡,讓lunch能找到用戶個性定制編譯項

    #echo "add_lunch_combo fs100-eng" > vendor/farsight/vendorsetup.sh

3. 仿著Android示例代碼,在公司目錄下創建products目錄

    #mkdir -p vendor/farsight/products

4. 仿著Android示例代碼,在products目錄下創建兩個mk文件

    #touch vendor/farsight/products/AndroidProduct.mk vendor/farsight/products/fs100.mk

在AndroidProduct.mk裡添加如下內容:

[plain] PRODUCT_MAKEFILES := $(LOCAL_DIR)/fs100.mk 
PRODUCT_MAKEFILES := $(LOCAL_DIR)/fs100.mk表示隻有一個產品fs100,它對應的配置文件在當前目錄下的fs100.mk。

5. 在產品配置文件裡添加最基本信息

[plain] 1  
 2 PRODUCT_PACKAGES := \ 
 3     IM \ 
 4     VoiceDialer 
 5  
 6 $(call inherit-product, build/target/product/generic.mk)  ##從某一默認配置開始派生餘下內容參考派生起點 
 7  
 8 # Overrides 
 9 PRODUCT_MANUFACTURER := farsight 
10 PRODUCT_NAME := fs100 
11 PRODUCT_DEVICE := fs100 

 

摘自 mr_raptor的專欄

發佈留言