一、Android编译步骤:
二、初始化编译环境--source build/envsetup.sh
在build/envsetup.sh中主要完成了三件事。
添加编译目标的代码如下:
for f in `/bin/ls vendor/*/vendorsetup.sh vendor/*/*/vendorsetup.sh device/*/*/vendorsetup.sh 2> /dev/null`
do
echo "including $f"
. $f
done
u
unset f
三、选择编译目标--lunch
通过lunch选择编译目标,函数定义如下:
function lunch(){
local answer
//如果调用lunch时带参数就表示手动指定编译目标,否则打印选择编译目标的菜单
if [ "$1" ] ; then
answer=$1
else
print_lunch_menu
echo -n "Which would you like? [full-eng] "
read answer
fi
local selection=
//如果没有编译目标默认为full-eng,否则如果answer是数字就将其转换为对应的字符串表示
if [ -z "$answer" ]
then
selection=full-eng
elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
then
if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
then
selection=${LUNCH_MENU_CHOICES[$(($answer-1))]}
fi
elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
then
selection=$answer
fi
//如果表示编译目标的字符串是空的表示非法选择,错误返回
if [ -z "$selection" ]
then
echo
echo "Invalid lunch combo: $answer"
return 1
fi
export TARGET_BUILD_APPS=
//检测选择的product是否合法(只要在相应目录新建vendorsetup.sh并add_lunch_combo就可在lunch menu中显示相应项,但如果不配置product相关信息就会Invalid)
local product=$(echo -n $selection | sed -e "s/-.*$//")
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
//从selection解析出product,variant
local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
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
//导出TARGET_PRODECT,TARGET_BUILD_VARIANT与TARGET_BUILD_TYPE,比如若选择的是full-eng,那么TARGET_PRODUCT=full,TARGET_BUILD_VARIANT=eng
export TARGET_PRODUCT=$product
export TARGET_BUILD_VARIANT=$variant
export TARGET_BUILD_TYPE=release
echo
set_stuff_for_environment
printconfig
}
四、编译--make -j8
要make肯定有Makefile,Android根目录的Makefile只是简单地include进来了一个文件:
1 ### DO NOT EDIT THIS FILE ###
2 include build/core/main.mk
3 ### DO NOT EDIT THIS FILE ###
而build/core/main.mk就比较复杂了,以下是main.mk的结构图:
在main.mk中首先定义了默认编译目标并做版本检查,然后include config.mk。
在config.mk中定义了在Android.mk中编译目标时会用到的常量
CLEAR_VARS:= $(BUILD_SYSTEM)/clear_vars.mk
BUILD_HOST_STATIC_LIBRARY:= $(BUILD_SYSTEM)/host_static_library.mk
BUILD_HOST_SHARED_LIBRARY:= $(BUILD_SYSTEM)/host_shared_library.mk
BUILD_STATIC_LIBRARY:= $(BUILD_SYSTEM)/static_library.mk
BUILD_RAW_STATIC_LIBRARY := $(BUILD_SYSTEM)/raw_static_library.mk
BUILD_SHARED_LIBRARY:= $(BUILD_SYSTEM)/shared_library.mk
BUILD_EXECUTABLE:= $(BUILD_SYSTEM)/executable.mk
BUILD_RAW_EXECUTABLE:= $(BUILD_SYSTEM)/raw_executable.mk
BUILD_HOST_EXECUTABLE:= $(BUILD_SYSTEM)/host_executable.mk
BUILD_PACKAGE:= $(BUILD_SYSTEM)/package.mk
BUILD_PHONY_PACKAGE:= $(BUILD_SYSTEM)/phony_package.mk
BUILD_HOST_PREBUILT:= $(BUILD_SYSTEM)/host_prebuilt.mk
BUILD_PREBUILT:= $(BUILD_SYSTEM)/prebuilt.mk
BUILD_MULTI_PREBUILT:= $(BUILD_SYSTEM)/multi_prebuilt.mk
BUILD_JAVA_LIBRARY:= $(BUILD_SYSTEM)/java_library.mk
BUILD_STATIC_JAVA_LIBRARY:= $(BUILD_SYSTEM)/static_java_library.mk
BUILD_HOST_JAVA_LIBRARY:= $(BUILD_SYSTEM)/host_java_library.mk
BUILD_DROIDDOC:= $(BUILD_SYSTEM)/droiddoc.mk
BUILD_COPY_HEADERS := $(BUILD_SYSTEM)/copy_headers.mk
BUILD_NATIVE_TEST := $(BUILD_SYSTEM)/native_test.mk
BUILD_HOST_NATIVE_TEST := $(BUILD_SYSTEM)/host_native_test.mk
然后include进来了/build/core/envsetup.sh,在envsetup.sh中定义了HOST_OS,OUT_DIR等环境变量,并通过include product_config.mk检索所有Product,并做有效性检查,然后根据lunch时选择的Product设置TARGET_DEVICE。
然后在envsetup.sh中根据TARGET_DEVICE查找BoardConfig.mk文件进行Board相关配置,查找KERNEL_HEADER,配置编译工具链。
在definitions.mk中定义了all_makefiles_under等用来检索文件的函数。
根据TARGET_BUILD_VARIANT设置ro.secure等系统属性,设定tags_to_install等变量。
接下来通过
subdir_makefiles := \
$(shell build/tools/findleaves.py --prune=out --prune=.repo --prune=.git $(subdirs) Android.mk)
include $(subdir_makefiles)
找到所有子目录下的Android.mk文件并include进来,这样在Android.mk定义的LOCAL_PACKAGE_NAME与include $(BUILD_PACKAGE)就可以将相应的Android.mk中定义的目标包含进编译模块中,在config.mk中有如下定义:
BUILD_PACKAGE:= $(BUILD_SYSTEM)/package.mk
而package.mk又包含base_rules.mk将LOCAL_MODULE添加到ALL_MODULES中。
post_clean.mk是跟Resource Overlay相关,使们们不需要修改framework中原来的资源文件就可以定义framework-res.apk中的资源。
接下来include build/core/Makefile文件,定义bootimage,systemimage等依赖目标,然后通过make编译droid(默认)目标就可以编译系统了。
Android采用检索所有子目录中的Android.mk将所有要编译的模块收集起来的方式编译系统,而不是进入到每个子目录执行make,这样就方便了我们添加自己的模块。