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

本文详细解析了OpenWrt编译系统的运作原理,包括环境变量设置、shell检测、vendorsetup.sh脚本加载及lunch函数实现。适用于对全志Tina平台OpenWrt系统感兴趣的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 $$"这个命令串:

$ 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函数实现细节。

转载于:https://www.cnblogs.com/rockyching2009/p/10111258.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值