OpenWrt编译系统(2)之lunch函数的细节

build/envsetup.sh

function lunch()
{
    local answer

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

    local selection=

    if [ -z "$answer" ]
    then
        selection=astar_parrot-tina
        # "^[0-9][0-9]*$":检查answer变量是否是数字
    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-1))]}
        fi
        # "^[^-][^-]*-[^-][^-]*$":检查answer变量格式是否形如"XXXX-XXXXX"
    elif (echo -n $answer | grep -q -e "^[^-][^-]*-[^-][^-]*$")
    then
        # 当answer的值为字符串的时候
        selection=$answer
    fi

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

    # 截掉_及其后面的内容,结果赋给platform。astar_parrot-tina--->astar
    local platform=$(echo -n $selection | sed -e "s/_.*$//")
    # 检测platform是否在PLATFORM_CHOICES的支持列表中
    check_platform $platform

    if [ $? -ne 0 ]
    then
        echo
        echo "** Don't have a platform spec for: '$platform'"
        echo "** Must be one of ${PLATFORM_CHOICES[@]}"
        echo "** Do you have the right repo manifest?"
        platform=
    fi

    # 截掉-及其后面的内容,结果赋给product。astar_parrot-tina--->astar_parrot
    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

    # variant: astar_parrot-tina--->tina
    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

    # productvariantplatform任意一个为空,说明有错误
    if [ -z "$product" -o -z "$variant" -o -z "$platform" ]
    then
        echo
        return 1
    fi

    export TARGET_PRODUCT=$product
    export TARGET_PLATFORM=$platform
    export TARGET_BOARD=$(get_build_var TARGET_DEVICE)
    export TARGET_BUILD_VARIANT=$variant
    export TARGET_BUILD_TYPE=release

    rm -rf tmp
    echo

    set_stuff_for_environment
    # 显示当前配置信息
    printconfig
}

在lunch函数执行过程中,很多次通过直接/间接的方式调用到get_build_var()函数:

function get_build_var()
{
    # 编译项目的顶层目录
    T=$(gettop)
    if [ ! "$T" ]; then
        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
        return
    fi
    (cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build 
      command make --no-print-directory -f build/config.mk dumpvar-$1)
}

以printconfig为例,最终执行的是:
get_build_var report_config
然后调用build/dumpvar.mk这个Makefile,显示当前配置的环境变量。

原文地址:https://www.cnblogs.com/rockyching2009/p/10246297.html