Android底层开发 - 编译过程及脚本分析

文章目录[x]
  1. 1:AOSP主要目录介绍
  2. 2:编译过程及脚本分析
  3. 2.1:安卓编译过程
  4. 2.2:. build/envsetup.sh
  5. 2.3:lunch

本文将简要介绍AOSP编译的几个命令和相关的脚本。

AOSP主要目录介绍

  • bionic : C库
  • build : 编译系统规则基础开发包配置
  • cts : 兼容性测试
  • art、dalvik : java虚拟机
  • external :Android引用的第三方模块
  • frameworks :Android核心框架
  • hardware : 硬件适配层
  • system : 底层文件系统库,应用和组件
  • device : 产品目标目录
  • out :编译生成的目标文件目录
  • packages : 系统的app和预装app
  • vendor : 厂商自定义的目录

编译过程及脚本分析

安卓编译过程

  1. 初始化参数设置
  2. 检测环境变量和目标环境
  3. 选择lunch并读取目标配置和平台信息
  4. 清空输出目录
  5. 编译
  6. 生成升级包

. build/envsetup.sh

Run "m help" for help with the build system itself.

Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
- lunch:      lunch <product_name>-<build_variant>
Selects <product_name> as the product to build, and <build_variant> as the variant to
build, and stores those selections in the environment to be read by subsequent
invocations of 'm' etc.
- tapas:      tapas [<App1> <App2> ...] [arm|x86|mips|arm64|x86_64|mips64] [eng|userdebug|user]
- croot:      Changes directory to the top of the tree, or a subdirectory thereof.
- m:          Makes from the top of the tree.
- mm:         Builds all of the modules in the current directory, but not their dependencies.
- mmm:        Builds all of the modules in the supplied directories, but not their dependencies.
To limit the modules being built use the syntax: mmm dir/:target1,target2.
- mma:        Builds all of the modules in the current directory, and their dependencies.
- mmma:       Builds all of the modules in the supplied directories, and their dependencies.
- provision:  Flash device with all required partitions. Options will be passed on to fastboot.
- cgrep:      Greps on all local C/C++ files.
- ggrep:      Greps on all local Gradle files.
- jgrep:      Greps on all local Java files.
- resgrep:    Greps on all local res/*.xml files.
- mangrep:    Greps on all local AndroidManifest.xml files.
- mgrep:      Greps on all local Makefiles files.
- sepgrep:    Greps on all local sepolicy files.
- sgrep:      Greps on all local source files.
- godir:      Go to the directory containing a file.
- allmod:     List all modules.
- gomod:      Go to the directory containing a module.
- pathmod:    Get the directory containing a module.
- refreshmod: Refresh list of modules for allmod/gomod.

Environment options:
- SANITIZE_HOST: Set to 'true' to use ASAN for all host modules. Note that
ASAN_OPTIONS=detect_leaks=0 will be set by default until the
build is leak-check clean.
- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
...
# 判断当前的shell是否是bash和zsh
validate_current_shell
#添加厂商的vendorsetup.sh文件
source_vendorsetup
#会检测某些文件是否在环境变量中
addcompletions

build/envsetup.sh的顶部,可以看出,该脚本会把诸如lunchmmmcgrep等函数加载到环境变量里面去。然后会判断当前的shell,然后会查找所有的vendorsetup.sh文件并执行,在其中的一个vendorsetup.sh中可以看到,会执行add_lunch_combo函数将一些产品选项添加到lunch的列表中:

add_lunch_combo full_k57pv1_dm_64-eng
add_lunch_combo full_k57pv1_dm_64-user
add_lunch_combo full_k57pv1_dm_64-userdebug

lunch

function lunch()
{
local answer

 

if [ "$1" ] ; then
answer=$1
else
print_lunch_menu
echo -n "Which would you like? [aosp_arm-eng] "
read answer
fi

 

local selection=

 

if [ -z "$answer" ]
then
selection=aosp_arm-eng
elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
then
local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
if [ $answer -le ${#choices[@]} ]
then
# array in zsh starts from 1 instead of 0.
if [ -n "$ZSH_VERSION" ]
then
selection=${choices[$(($answer))]}
else
selection=${choices[$(($answer-1))]}
fi
fi
else
selection=$answer
fi

 

export TARGET_BUILD_APPS=

 

local product variant_and_version variant version

 

product=${selection%%-*} # Trim everything after first dash
variant_and_version=${selection#*-} # Trim everything up to first dash
if [ "$variant_and_version" != "$selection" ]; then
variant=${variant_and_version%%-*}
if [ "$variant" != "$variant_and_version" ]; then
version=${variant_and_version#*-}
fi
fi

 

if [ -z "$product" ]
then
echo
echo "Invalid lunch combo: $selection"
return 1
fi

 

TARGET_PRODUCT=$product \
TARGET_BUILD_VARIANT=$variant \
TARGET_PLATFORM_VERSION=$version \
build_build_var_cache
if [ $? -ne 0 ]
then
return 1
fi

 

export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
if [ -n "$version" ]; then
export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
else
unset TARGET_PLATFORM_VERSION
fi
export TARGET_BUILD_TYPE=release

 

echo

//设置构建工具链
set_stuff_for_environment
printconfig
destroy_build_var_cache
}

可以看到该函数,它会判断是否提供了选项,如果提供了直接把值赋值给answer(如 lunch 6),否则就打印列表并让其进行选择。选择完成之后就会记录并做一些初始化工作。

点赞

发表评论

昵称和uid可以选填一个,填邮箱必填(留言回复后将会发邮件给你)
tips:输入uid可以快速获得你的昵称和头像

Title - Artist
0:00