OpenWrt的编译系统负责编译出可以最终烧录的img文件。由于全志的Tina采用的是OpenWrt编译系统,所以这个系列以Tina编译为例进行分析。
在make之前,需要设置环境变量,用到的命令是:
1 2 | source build/envsetup.sh lunch |
这两条命令干了什么?怎么做的?
打开build/envsetup.sh脚本,发现这个脚本几乎全部是一些命令,如mm、mmm、cgrep、jgrep等(做过Android系统开发的对这些肯定相当眼熟了)。不错,OpenWrt用的和Android类似的编译系统。
关于这些命令的作用和实现方式,这里不作解释,只看下envsetup.sh脚本中最后几行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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为例:
1 2 | $ echo $SHELL /bin/bash |
所以第一行就是用来判定当前系统shell是不是bash,如果不是通过"ps -o command -p $$"查看当前shell的名称。
"ps -o command -p $$"解释:
--$$:shell本身的pid,示例:
1 2 3 | $ ps -p $$ PID TTY TIME CMD 62208 pts/0 00:00:00 bash |
---o command:ps命令的-o参数允许用户指定显示信息的格式,command指示只显示CMD对应的信息,这里就是"bash"。
我们完整执行下"ps -o command -p $$"这个命令串:
1 2 3 | $ 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"文件内容:
1 | add_lunch_combo astar_parrot-tina |
add_lunch_combo是envsetup.sh中的一个命令函数,用来添加一个方案,函数实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | # 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函数实现细节。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!