Android build system 分析之 envsetup.sh

来源:互联网 发布:java 文件上传类型 编辑:程序博客网 时间:2024/05/16 06:22

http://keyewan.blog.163.com/blog/static/1898272332011820115647247/

每次进入到android source目录后的第一件事恐怕就是先执行. build/envsetup.sh,完成一些命令的初始化,今天主要分析envsetup.sh。

1. 命令-也就是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 ()                  # 跳到指定目录

# add the default one here
add_lunch_combo generic-eng

# Execute the contents of any vendorsetup.sh files we can find.
for f in `/bin/ls vendor/*/vendorsetup.sh vendor/*/build/vendorsetup.sh 2> /dev/null`
do
echo “including $f”
. $f
done

2. lunch命令

已经有人分析过了,直接搬过来:

http://www.lupaworld.com/home.php?mod=space&uid=131820&do=blog&id=149462

这里说一下check_product的流程:

待续

3. choosecombo

待续

http://www.cnblogs.com/cnhome/archive/2010/07/24/1784382.html



build/envsetup.sh 脚本分析(lunch函数)

lunch函数提供了一个菜单,让开发人员选择需要编译的目标产品(target product)和变体(variant),并做一些检查,设置环境变量,并打印出主要的环境变量。

直接运行lunch(必须先运行 build/envsetup.sh,让lunch函数驻留到环境变量中)

ning@ning-desktop:~/donut-compare/mydroid$ lunch

You're building on Linux

generic-eng simulator aosp_emulator_us-eng aosp_emulator_eu-eng aosp_dream_us-userdebug aosp_dream_eu-userdebug aosp_dream_us-eng aosp_dream_eu-eng aosp_sapphire_us-userdebug aosp_sapphire_eu-userdebug aosp_sapphire_us-eng aosp_sapphire_eu-eng
Lunch menu... pick a combo:
     1. generic-eng
     2. simulator
     3. aosp_emulator_us-eng
     4. aosp_emulator_eu-eng
     5. aosp_dream_us-userdebug
     6. aosp_dream_eu-userdebug
     7. aosp_dream_us-eng
     8. aosp_dream_eu-eng
     9. aosp_sapphire_us-userdebug
     10. aosp_sapphire_eu-userdebug
     11. aosp_sapphire_us-eng
     12. aosp_sapphire_eu-eng

Which would you like? [generic-eng] 7

============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=1.6
TARGET_PRODUCT=aosp_dream_us
TARGET_BUILD_VARIANT=eng
TARGET_SIMULATOR=false
TARGET_BUILD_TYPE=release
TARGET_ARCH=arm
HOST_ARCH=x86
HOST_OS=linux
HOST_BUILD_TYPE=release
BUILD_ID=Donut
============================================

用户也可以直接输入参数,不使用菜单

ning@ning-desktop:~/donut-compare/mydroid$ lunch aosp_dream_us-eng

============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=1.6
TARGET_PRODUCT=aosp_dream_us
TARGET_BUILD_VARIANT=eng
TARGET_SIMULATOR=false
TARGET_BUILD_TYPE=release
TARGET_ARCH=arm
HOST_ARCH=x86
HOST_OS=linux
HOST_BUILD_TYPE=release
BUILD_ID=Donut
============================================

下面是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
}


原创粉丝点击