OpenWrt编译系统(1)之make之前

OpenWrt的编译系统负责编译出可以最终烧录的img文件。由于全志的Tina采用的是OpenWrt编译系统,所以这个系列以Tina编译为例进行分析。

在make之前,需要设置环境变量,用到的命令是:

source build/envsetup.sh
lunch

这两条命令干了什么?怎么做的?

打开build/envsetup.sh脚本,发现这个脚本几乎全部是一些命令,如mm、mmm、cgrep、jgrep等(做过Android系统开发的对这些肯定相当眼熟了)。不错,OpenWrt用的和Android类似的编译系统。
关于这些命令的作用和实现方式,这里不作解释,只看下envsetup.sh脚本中最后几行:

if [ "x$SHELL" != "x/bin/bash" ]; then
    case `ps -o command -p $$` in
        *bash*)
            ;;
        *)
            echo "WARNING: Only bash is supported, use of other shell would lead to erroneous results"
            ;;
    esac
fi

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

SHELL是Linux的一个环境变量,指定打开终端时启用的默认shell,以Ubuntu 14.04为例:

$ echo $SHELL
/bin/bash

所以第一行就是用来判定当前系统shell是不是bash,如果不是通过"ps -o command -p $$"查看当前shell的名称。

"ps -o command -p $$"解释:

--$$:shell本身的pid,示例:

$ ps -p $$
   PID TTY          TIME CMD
 62208 pts/0    00:00:00 bash

---o command:ps命令的-o参数允许用户指定显示信息的格式,command指示只显示CMD对应的信息,这里就是"bash"。

我们完整执行下"ps -o command -p $$"这个命令串:

扫描二维码关注公众号,回复: 4480608 查看本文章
$ ps -o command -p $$
COMMAND
bash

至此,shell检测完成。

接下来的for循环加载target目录下的所有vendorsetup.sh脚本,实现步骤:

"test -d target":当前目录下存在target且是一个目录
"find -L target -maxdepth 4 -name 'vendorsetup.sh'":在target目录下查找"vendorsetup.sh"文件

". $f":加载并执行对应的"vendorsetup.sh"文件,我们看下/target/allwinner/astar_parrot/目录下一个"vendorsetup.sh"文件内容:

add_lunch_combo astar_parrot-tina

add_lunch_combo是envsetup.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)
}

shell数组的知识:

1、数组用括号来表示,元素用"空格"符号分割开,语法格式如下:array_name=(value1 ... valuen)

2、读取数组元素值的一般格式是:${array_name[index]}

3、@ 或 * 可以获取数组中的所有元素,例如:${my_array[*]}或${my_array[@]}

4、获取数组元素个数:${#my_array[*]}或${#my_array[@]}

至此,上述add_lunch_combo函数就明了了哈。

最后,lunch函数用来遍历add_lunch_combo添加的所有方案,并提供用户选择的交互口。

接下来分析下lunch函数实现细节。

猜你喜欢

转载自www.cnblogs.com/rockyching2009/p/10111258.html