Android Build-A ./build/envsetup.sh ——> lunch

我们编译Android源码的步骤是什么?

全编:

  1. source build/envsetup.sh
  2. lunch 特定的分支
  3. make -j8 2>&1|tee build.log
    单编:
  4. mm frameworks/base/
  5. mmm packages/apps/Settings/

基础知识:正则,bash shell

source build/envsetup.sh

source是Linux 内部命令,这里不多讲解,可以理解为执行;我们直接看build/envsetup.sh命令;

function hmm() {
cat <-
- tapas:   tapas [  ...] [arm|x86|mips|armv5|arm64|x86_64|mips64] [eng|userdebug|user]
- croot:   Changes directory to the top of the tree.
- 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.
- 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.
- sgrep:   Greps on all local source files.
- godir:   Go to the directory containing a file.

Look at the source to view more functions. The complete list is:
EOF
    T=$(gettop)
    local A
    A=""
    for i in `cat $T/build/envsetup.sh | sed -n "/^[ \t]*function /s/function \([a-z_]*\).*/\1/p" | sort | uniq`; do
      A="$A $i"
    done
    echo $A
}
......

文件以function开始,function就是一个方法,cat < 当我们在Shell 中执行source build/envsetup.sh后,执行hmm命令看看,截图如下:


Android Build-A ./build/envsetup.sh ——> lunch_第1张图片
hmm.png

function也对应于命令,这里暂时只举例了一个,有需要再列举;回到我们我们的主线,从source build/envsetup.sh开始;

1-1:source build/envsetup.sh

source build/envsetup.sh和". build/envsetup.sh"等价,就是执行这个sh脚本,这个脚本中全是方法,方法是被调用的,所以程序肯定从方法外的程序开始执行:

......
# Clear this variable.  It will be built up again when the vendorsetup.sh
# files are included at the end of this file.
unset LUNCH_MENU_CHOICES
function add_lunch_combo()
{
    local new_combo=$1
    local c
    for c in ${LUNCH_MENU_CHOICES[@]} ; do
        if [ "$new_combo" = "$c" ] ; then
            return
        fi
    done
    LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
}
# add the default one here
add_lunch_combo aosp_arm-eng
add_lunch_combo aosp_arm64-eng
add_lunch_combo aosp_mips-eng
add_lunch_combo aosp_mips64-eng
add_lunch_combo aosp_x86-eng
add_lunch_combo aosp_x86_64-eng
......

unset LUNCH_MENU_CHOICES 删除LUNCH_MENU_CHOICES,
add_lunch_combo()的功能是向LUNCH_MENU_CHOICES变量中添加元素,
相当于初始化LUNCH_MENU_CHOICES;
这里执行完成后LUNCH_MENU_CHOICES数组中就有了上述的元素值;

# Execute the contents of any vendorsetup.sh files we can find.
for f in `test -d device && find -L device -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null | sort` \
         `test -d vendor && find -L vendor -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null | sort`
do
    echo "including $f"
    . $f
done
unset f

shell 中反引号的作用:将反引号中的内容当做shell 命令来执行;
反引号中使用test -d 判断device目录是否存在,如果存在则利用find在device中查找vendorsetup.sh脚本,查找深度为4级目录;"2> /dev/null"是重定向等级为2的输出到/dev/null,2代表错误,即不输出错误,sort 是排序; echo "including $f"将找到的文件输出到标准输出,". $f"和source $f等价,即执行找到的vendorsetup.sh;

我在项目中执行source build/envsetup.sh,截图如下:

Android Build-A ./build/envsetup.sh ——> lunch_第2张图片
source.png

上述途中的including 项就是我们找到的vendorsetup.sh,这里我打开第一个device/samsung/manta/vendorsetup.sh脚本:

add_lunch_combo aosp_manta-userdebug

这里只有这一句话,调用add_lunch_combo 方法,即LUNCH_MENU_CHOICES数组中添加元素;上述图中的vendorsetup.sh 在不同的目录下,不同的vendorsetup.sh中内容还不同,比如asus,hts,samsung等,代表不同公司不同项目;add_lunch_combo的参数由两部分组成:product-variant,aosp_manta-userdebug代表product=aosp_manta,variant=userdebug;
所有source build/envsetup.sh脚本执行结束后,即初始化LUNCH_MENU_CHOICES数组完成;接下来我们一般执行lunch命令:

lunch

截图如下:


Android Build-A ./build/envsetup.sh ——> lunch_第3张图片
lunch.png

这里显示的内容就是前面source build/envsetup.sh调用add_lunch_combo初始化的LUNCH_MENU_CHOICES数组内容;我们看看具体代码是如何显示的呢?

function print_lunch_menu()
{
    local uname=$(uname)
    echo
    echo "You're building on" $uname
    echo
    echo "Lunch menu... pick a combo:"

    local i=1
    local choice
    for choice in ${LUNCH_MENU_CHOICES[@]}
    do
        echo "     $i. $choice"
        i=$(($i+1))
    done

    echo
}

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=
    #这里是判断answer变量长度是否为0
    if [ -z "$answer" ]
    then
    #如果长度为0,则 selection=aosp_arm-eng
        selection=aosp_arm-eng
    #这里是通过echo -n输出answer值,输出没输出到屏幕,而是通过管道传递给grep -q -e "^[0-9][0-9]*$",
    #即判断answer是不是以两个数字开头,是则返回true
    elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
    then
    #如果answer的值是否小于等于数组LUNCH_MENU_CHOICES个数
        if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
        then
    #如果上述条件都成立,将LUNCH_MENU_CHOICES数组的answer-1位数据赋值给selection
            selection=${LUNCH_MENU_CHOICES[$(($answer-1))]}
        fi
    #方括号中的^表示排除, 也就是不是这些字符的字符此例中的[^\-]表示一个不是'-'的字符,
    #因为'-'在方括号中有表示范围的意思,所以前面加了'\'来转义成一个普通字#符'-',
    #(但在此处转义符'\'多余:此例中'-'明显不是表示范围,作者低估了正则引擎的理解能力)
    #全表达式意思是:字符串开头是一个不为'-'的字符,后面跟0个到多个不为'-'的字符,
    #再后面是一个'-',再后面又是一个不为'-'的字符,后面跟0个到多个不为'-'的字符;
    #这里扯了这么多,其实这里就是期望得到product-varient的形式。也就是我们之前
    #使用add_lunch_combo添加的那些字符串的格式。比如:aosp_arm-eng,
    #product就是aosp_arm,varient就是eng.中间的-是必须的
    elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
    then
        selection=$answer
    fi
    #如果selecetion长度为0,则不合法,输出提示
    if [ -z "$selection" ]
    then
        echo
        echo "Invalid lunch combo: $answer"
        return 1
    fi

    export TARGET_BUILD_APPS=
    #利用sed替换selection中"-eng,-user,-userdebug"为空,即去掉"-"后面部分
    local product=$(echo -n $selection | sed -e "s/-.*$//")
    #j检查product是否合法
    check_product $product
    #根据上面product是否合法继续执行下面操作,如果返回值不等于0就出现问题了
    if [ $? -ne 0 ]
    then
        echo
        echo "** Don't have a product spec for: '$product'"
        echo "** Do you have the right repo manifest?"
        product=
    fi
    #利用sed替换selection中product部分,即去掉"-"前面部分
    local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
    #检查variant
    check_variant $variant
    #如果返回值不为0就出现问题了
    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_BUILD_TYPE=release

    echo

    set_stuff_for_environment
    printconfig
}

这个函数首先判断有没有传入参数,如果传入了就把值赋给answer这个变量,如果没有传参数,也就是说知识执行了lunch,那么就会调用fprint_lunch_menu函数显示一份菜单出来,显示出来后,接受用户的输入。
fprint_lunch_menu 就是利用for循环将LUNCH_MENU_CHOICES中的所有值都打印到屏幕上;接下来就是校验answer变量,看看answer是否合法;校验过程见上述代码注释,校验完成就是从answer变量中获取product值,后去后再调用check_product检验product的合法性;

# check to see if the supplied product is one we can build
function check_product()
{
    #获取源码跟目录
    T=$(gettop)
    if [ ! "$T" ]; then
        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
        return
    fi
        TARGET_PRODUCT=$1 \
        TARGET_BUILD_VARIANT= \
        TARGET_BUILD_TYPE= \
        TARGET_BUILD_APPS= \
        get_build_var TARGET_DEVICE > /dev/null
    # hide successful answers, but allow the errors to show
}

上述代码会调用get_build_var方法,这里我们可以在shell中执行这个命令看看结果:输出了generic,具体来看看代码;

get_build_var.png
# Get the exact value of a build variable.
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/core \
      command make --no-print-directory -f build/core/config.mk dumpvar-$1)
}

get_build_var 很简单,就是执行make --no-print-directory -f build/core/config.mk dumpvar-$1,$1为TARGET_DEVICE,这里传递的完整参数为dumpvar-TARGET_DEVICE,config.mk会include dumpvar.mk,这个文件中会提取我们传入的dumpvar-TARGET_DEVICE变量中的TARGET_DEVICE,然后打印$(TARGET_DEVICE)。

add_lunch_combo添加的字符串的格式为product-variant,检查完了product接下来就会检查variant;variant必须是(user userdebug eng)中的一种;

VARIANT_CHOICES=(user userdebug eng)
# check to see if the supplied variant is valid
function check_variant()
{
    for v in ${VARIANT_CHOICES[@]}
    do
        if [ "$v" = "$1" ]
        then
            return 0
        fi
    done
    return 1
}

各种校验结束后会调用

    #product和variant都不能为空,为空就是有问题
    if [ -z "$product" -o -z "$variant" ]
    then
        echo
        return 1
    fi
    export TARGET_PRODUCT=$product
    export TARGET_BUILD_VARIANT=$variant
    export TARGET_BUILD_TYPE=release

    echo
    set_stuff_for_environment
    printconfig

上述代码就是就前面的product和variant变量赋值给全局变量并且导出为环境变量。以后的需要用到这几个变量就是这里赋值的;set_stuff_for_environment 函数还会设置一些变量:

function set_stuff_for_environment()
{
    settitle
    set_java_home
    setpaths
    set_sequence_number

    export ANDROID_BUILD_TOP=$(gettop)
    # With this environment variable new GCC can apply colors to warnings/errors
    export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
}

其中set_java_home会检查导出JAVA_HOME这个环境变量,这个环境变量就是JDK所在路径,setpaths函数会给PATH环境变量增加编译Android需要的一些路径。最后lunch调用了 printconfig函数,这个函数打印出了配置信息。

总结:source build/envsetup.sh会调用add_lunch_combo函数添加编译信息,同时还会查找/device和/vendor下的vendorsetup.sh文件,查找深度为4级目录,找到后就执行它,它里面至少会有这么一行:add_lunch_combo xxxx,继续添加编译信息。lunch函数则会打印出所有的单板信息供你选择,你输入选择后,luch命令会对你的选择做一系列检测,并从中提取出product和varient,并最终导出这些信息,供正式编译的时候使用。

你可能感兴趣的:(Android Build-A ./build/envsetup.sh ——> lunch)