转自:http://blog.csdn.net/mr_raptor/article/details/7539978
++++++++++++++++++++++++++++++++++++++++++
Android编译过程详解(一):http://blog.csdn.net/mr_raptor/article/details/7539978
Android编译过程详解(二):http://blog.csdn.net/mr_raptor/article/details/7540066
Android编译过程详解(三):http://blog.csdn.net/mr_raptor/article/details/7540730
Android的优势就在于其开源,手机和平板生产商可以根据自己的硬件进行个性定制自己的手机产品,如小米,LePhone,M9等,因此,在我们在对Android的源码进行定制的时候,很有必要了解下,Android的编译过程。
如果你从来没有做过Android代码的编译,那么最官方的编译过程就是查看Android的官方网站:http://source.android.com/source/building.html
但是,这儿只是告诉你了如何去编译一个通用的系统,并没有详细告诉你细节,我们跟着编译过程来了解下。
+--------------------------------------------------------------------------------------------------------------------+
本文使用Android版本为2.1,采用开发板为华清远见研发的FS_S5PC100 A8开发板。
+--------------------------------------------------------------------------------------------------------------------+
按照google给出的编译步骤如下:
1> source build/envsetup.sh:加载命令
2> lunch:选择平台编译选项
3> make:执行编译
我们按照编译步骤来分析编译过程的细节,最终添加自己的平台产品的编译选项。
1. source build/envsetup.sh
这个命令是用来将envsetup.sh里的所有用到的命令加载到环境变量里去,我们来分析下它。
envsetup.sh里的主要命令如下:
function help() # 显示帮助信息
function get_abs_build_var() # 获取绝对变量
function get_build_var() # 获取绝对变量
function check_product() # 检查product
function check_variant() # 检查变量
function setpaths() # 设置文件路径
function printconfig() # 打印配置
function set_stuff_for_environment() # 设置环境变量
function set_sequence_number() # 设置序号
function settitle() # 设置标题
function choosetype() # 设置type
function chooseproduct() # 设置product
function choosevariant() # 设置variant
function tapas() # 功能同choosecombo
function choosecombo() # 设置编译参数
function add_lunch_combo() # 添加lunch项目
function print_lunch_menu() # 打印lunch列表
function lunch() # 配置lunch
function m() # make from top
function findmakefile() # 查找makefile
function mm() # make from current directory
function mmm() # make the supplied directories
function croot() # 回到根目录
function cproj()
function pid()
function systemstack()
function gdbclient()
function jgrep() # 查找java文件
function cgrep() # 查找c/cpp文件
function resgrep()
function tracedmdump()
function runhat()
function getbugreports()
function startviewserver()
function stopviewserver()
function isviewserverstarted()
function smoketest()
function runtest()
function godir () # 跳到指定目录 405
# add_lunch_combo函数被多次调用,就是它来添加Android编译选项
# Clear this variable. It will be built up again when the vendorsetup.sh
406 # files are included at the end of this file.
# 清空LUNCH_MENU_CHOICES变量,用来保存编译选项
407 unset LUNCH_MENU_CHOICES
408 function add_lunch_combo()
409 {
410 local new_combo=$1 # 获得add_lunch_combo被调用时的参数
411 local c
# 依次遍历LUNCH_MENU_CHOICES里的值,其实该函数第一次调用时,该值为空
412 for c in ${LUNCH_MENU_CHOICES[@]} ; do
413 if [ "$new_combo" = "$c" ] ; then # 如果参数里的值已经存在于LUNCH_MENU_CHOICES变量里,则返回
414 return
415 fi
416 done
# 如果参数的值不存在,则添加到LUNCH_MENU_CHOICES变量里
417 LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
418 }
# 这是系统自动增加了一个默认的编译项 generic-eng
420 # add the default one here
421 add_lunch_combo generic-eng # 调用上面的add_lunch_combo函数,将generic-eng作为参数传递过去
422
423 # if we're on linux, add the simulator. There is a special case
424 # in lunch to deal with the simulator
425 if [ "$(uname)" = "Linux" ] ; then
426 add_lunch_combo simulator
427 fi
# 下面的代码很重要,它要从vendor目录下查找vendorsetup.sh文件,如果查到了,就加载它
1037 # Execute the contents of any vendorsetup.sh files we can find.
1038 for f in `/bin/ls vendor/*/vendorsetup.sh vendor/*/build/vendorsetup.sh 2> /dev/null`
1039 do
1040 echo "including $f"
1041 . $f # 执行找到的脚本,其实里面就是厂商自己定义的编译选项
1042 done
1043 unset f
envsetup.sh其主要作用如下:
- 加载了编译时使用到的函数命令,如:help,lunch,m,mm,mmm等
- 添加了两个编译选项:generic-eng和simulator,这两个选项是系统默认选项
- 查找vendor/<-厂商目录>/和vendor/<厂商目录>/build/目录下的vendorsetup.sh,如果存在的话,加载执行它,添加厂商自己定义产品的编译选项
根据上面的内容,可以推测出,如果要想定义自己的平台产品编译项,简单的办法是直接在envsetup.sh最后添加上add_lunch_combo myProduct-eng,当然这么做,不太符合上面代码最后的本意,我们还是老实的在vendor目录下创建自己公司名字,然后在公司目录下创建一个新的vendorsetup.sh,在里面添加上自己的产品编译项
- #mkdir vendor/farsight/
- #touch vendor/farsight/vendorsetup.sh
- #echo "add_lunch_combo fs100-eng" > vendor/farsight/vendorsetup.sh
- including vendor/farsight/vendorsetup.sh
2. 按照android官网的步骤,开始执行lunch full-eng
当然如果你按上述命令执行,它编译的还是通用的eng版本系统,不是我们个性定制系统,我们可以执行lunch命令,它会打印出一个选择菜单,列出可用的编译选项
如果你按照第一步中添加了vendorsetup.sh那么,你的选项中会出现:
- You're building on Linux
- generic-eng simulator fs100-eng
- Lunch menu... pick a combo:
- 1. generic-eng
- 2. simulator
- 3. fs100-eng
lunch命令是envsetup.sh里定义的一个命令,用来让用户选择编译项,来定义Product和编译过程中用到的全局变量。
我们一直没有说明前面的fs100-eng是什么意思,现在来说明下,fs100是我定义的产品的名字,eng是产品的编译类型,除了eng外,还有user, userdebug,分别表示:
eng: 工程机,
user:最终用户机
userdebug:调试测试机
tests:测试机
由此可见,除了eng和user外,另外两个一般不能交给最终用户的,记得m8出来的时候,先放出了一部分eng工程机,然后出来了user机之后,可以用工程机换。
那么这四个类型是干什么用的呢?其实,在main.mk里有说明,在Android的源码里,每一个目标(也可以看成工程)目录都有一个Android.mk的makefile,每个目标的Android.mk中有一个类型声明:LOCAL_MODULE_TAGS,这个TAGS就是用来指定,当前的目标编译完了属于哪个分类里。
PS:Android.mk和Linux里的makefile不太一样,它是Android编译系统自己定义的一个makefile来方便编译成:c,c++的动态、静态库或可执行程序,或java库或android的程序,
好了,我们来分析下lunch命令干了什么?
function lunch()
{
local answer
if [ "$1" ] ; then
# lunch后面直接带参数
answer=$1
else
# lunch后面不带参数,则打印处所有的target product和variant菜单提供用户选择
print_lunch_menu
echo -n "Which would you like? [generic-eng] "
read answer
fi
local selection=
if [ -z "$answer" ]
then
# 如果用户在菜单中没有选择,直接回车,则为系统缺省的generic-eng
selection=generic-eng
elif [ "$answer" = "simulator" ]
then
# 如果是模拟器
selection=simulator
elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
then
# 如果answer是选择菜单的数字,则获取该数字对应的字符串
if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
then
selection=${LUNCH_MENU_CHOICES[$(($answer-$_arrayoffset))]}
fi
# 如果 answer字符串匹配 *-*模式(*的开头不能为-)
elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
then
selection=$answer
fi
if [ -z "$selection" ]
then
echo
echo "Invalid lunch combo: $answer"
return 1
fi
# special case the simulator
if [ "$selection" = "simulator" ]
then
# 模拟器模式
export TARGET_PRODUCT=sim
export TARGET_BUILD_VARIANT=eng
export TARGET_SIMULATOR=true
export TARGET_BUILD_TYPE=debug
else
# 将 product-variant模式中的product分离出来
local product=$(echo -n $selection | sed -e "s/-.*$//")
# 检查之,调用关系 check_product()->get_build_var()->build/core/config.mk比较罗嗦,不展开了
check_product $product
if [ $? -ne 0 ]
then
echo
echo "** Don't have a product spec for: '$product'"
echo "** Do you have the right repo manifest?"
product=
fi
# 将 product-variant模式中的variant分离出来
local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
# 检查之,看看是否在 (user userdebug eng) 范围内
check_variant $variant
if [ $? -ne 0 ]
then
echo
echo "** Invalid variant: '$variant'"
echo "** Must be one of ${VARIANT_CHOICES[@]}"
variant=
fi
if [ -z "$product" -o -z "$variant" ]
then
echo
return 1
fi
# 导出环境变量,这里很重要,因为后面的编译系统都是依赖于这里定义的几个变量的
export TARGET_PRODUCT=$product
export TARGET_BUILD_VARIANT=$variant
export TARGET_SIMULATOR=false
export TARGET_BUILD_TYPE=release
fi # !simulator
echo
# 设置到环境变量,比较多,不再一一列出,最简单的方法 set >env.txt 可获得
set_stuff_for_environment
# 打印一些主要的变量, 调用关系 printconfig()->get_build_var()->build/core/config.mk->build/core/envsetup.mk 比较罗嗦,不展开了
printconfig
}
由上面分析可知,lunch命令可以带参数和不带参数,最终导出一些重要的环境变量,从而影响编译系统的编译结果。导出的变量如下(以实际运行情况为例)
- TARGET_PRODUCT=fs100
- TARGET_BUILD_VARIANT=eng
- TARGET_SIMULATOR=false
- TARGET_BUILD_TYPE=release
执行完上述两个步骤,就该执行:make命令了,当然如果你按照上述两个步骤做完,执行make之后肯定会出问题,我们还要做一些其它的操作,下篇来分析。
1. make
执行make命令的结果就是去执行当前目录下的Makefile文件,我们来看下它的内容:
- 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文件里虽然脚本不多,但是却定义了整个Android的编译关系,它主要引入了下列几个重要的mk文件:
49 include $(BUILD_SYSTEM)/config.mk
55 include $(BUILD_SYSTEM)/cleanbuild.mk
142 include $(BUILD_SYSTEM)/definitions.mk
当然每个mk文件都有自己独特的意义,我们一并将主线流程相关mk文件都列出来,大概来介绍下,先有个整体的概念,然后再细化了解。
所有的Makefile都通过build/core/main.mk这个文件组织在一起,它定义了一个默认goals:droid,当我们在TOP目录下,敲Make实际上就等同于我们执行make droid。
当Make include所有的文件,完成对所有make我文件的解析以后就会寻找生成droid的规则,依次生成它的依赖,直到所有满足的模块被编译好,然后使用相应的工具打包成相应的img。其中,config.mk,envsetup.mk,product_config.mk文件是编译用户指定平台系统的关键文件。上图中红色部分是用户指定平台产品的编译主线,我们先来看下config.mk的主要作用。
2. build/core/config.mk
该文件被main.mk包含。
定义了以下环境变量:
- 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
- .....然后定义了下面几个重要的编译命令
- 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:用来指定编译动态库过程
- 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 :=
3. envsetup.mk
- 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
- 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
- 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
- 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
- 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)
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,找到与之对应的配置文件,然后读取产品配置文件,找到里面的PRODUCT_DEVICE的值,设置给TARGET_DEVICE变量,用于后续编译。
5. build/core/product.mk
- 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
_find-android-products-files:
用来获得vendor目录下,所有名字为AndroidProduct.mk的文件列表。
get-all-product-makefiles:
用来获得所有AndroidProduct.mk文件里定义的PRODUCT_MAKEFILES的值(其实是产品文件路径名)。
在vendor目录下,每个公司目录下都会存在一个AndroidProduct.mk文件,这个文件是用来定义这个公司的产品列表,每个产品用<product_name>.mk来表示
如Android给的示例:
- vendor/sample/products/AndroidProduct.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
- 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
由此可见,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里添加如下内容:
- PRODUCT_MAKEFILES := $(LOCAL_DIR)/fs100.mk
5. 在产品配置文件里添加最基本信息
- 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
一定要注意:
PRODUCT_NAME:是产品的名字,它要和最终出现的编译项产品名一致,也就是说fs100-eng
PRODUCT_DEVICE:表示设备的名字,它要和将来创建的设备目录名字一致。