Android Donut Makefile 分析之二 (build/envsetup.sh)

轉載:http://home.lupaworld.com/home-space-uid-131820-do-blog-id-149462.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
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章