OpenWrt开发入门实战(四)添加补丁

1. 关于补丁

在OpenWrt中,补丁的功能使用一个名为quilt的工具完成的。这个工具由OpenWrt下载编译了,就在/home/buildbot/openwrt/staging_dir/host/bin/quilt
如果我们要使用quilt首先要配置quilt的配置文件,然后配置环境变量。注意,下面的EDITOR="nano"时指quilt默认的编辑环境,想用vi的可以替换成EDITOR="vi"

cat << EOF > ~/.quiltrc
QUILT_DIFF_ARGS="--no-timestamps --no-index -p ab --color=auto"
QUILT_REFRESH_ARGS="--no-timestamps --no-index -p ab"
QUILT_SERIES_ARGS="--color=auto"
QUILT_PATCH_OPTS="--unified"
QUILT_DIFF_OPTS="-p"
EDITOR="nano"
EOF

export PATH=/home/buildbot/openwrt/staging_dir/host/bin:$PATH

配置完成后,输入以下命令就可以看到quilt的版本。

quilt --version

2. 使用补丁添加新文件

2.1 准备补丁代码

在创建补丁之前,需要准备补丁源码环境

cd /home/buildbot/openwrt
make package/helloopenwrt/{clean,prepare} QUILT=1

请注意,当执行make指定参数QUILT=1时,build_dir下的软件包不用于构建最终包,并且该参数会在构建目录中创建额外的文件夹和文件。

最后到补丁源码目录,确保提交所有现有的补丁:

cd /home/buildbot/openwrt/build_dir/target-x86_64_musl/helloopenwrt-1.0
quilt push -a

现在执行quilt push -a时不会执行任何操作,因为这个示例包中还没有任何补丁。但时在处理由多人编写的软件包时,最好执行这条命令,因为不知道其他人是否提交了补丁。

2.2 创建一个补丁

现在开始创建第一个补丁

quilt new 100-add_functions_files.patch

补丁的命令最好遵循OpenWrt的构建约定,名称通常以数字开始,后跟其功能的简短描述。数字一般有特殊含义

0xx - 上游补丁
1xx - 代码等待上游合并
2xx - 内核构建、配置、头补丁
3xx - 特定架构的补丁
4xx - mtd相关的补丁(子系统和驱动)
5xx - 文件系统相关的补丁
6xx - 通用网络补丁
7xx - 网络层/物理层驱动补丁
8xx - 其他驱动
9xx - 未分类的其他补丁

我们要在helloopenwrt中增加两个文件,首先用quilt追踪这两个文件

quilt add functions.c
quilt add functions.h

创建C源码并用quilt编辑

touch functions.c
quilt edit functions.c
int mul(int a, int b)
{
        return a * b;
}

创建头文件

touch functions.h
quilt edit functions.h
int mul(int, int);

编辑完成之后我们可以使用以下命令查看更改的内容

quilt diff

最后别忘了执行以下命令提交更改

quilt refresh

2.3 将补丁提交到软件包中

在上述步骤中,我们已经在build_dir下的helloopenwrt代码中添加了补丁,但并没有将这些补丁添加到实际的软件包中,所以要执行以下命令,将补丁提交到软件包中。

cd /home/buildbot/openwrt
make package/helloopenwrt/update

这时候我们可以检测本地包源中的软件包的内容,和实际helloopenwrt的源码内容

ls -la /home/buildbot/mypackages/examples/helloopenwrt/
ls -la /home/buildbot/helloopenwrt/

我们可以发现本地包源中/home/buildbot/mypackages/examples/helloopenwrt/多了一个patches目录,而/home/buildbot/helloopenwrt/并无任何改变。

现在我们需要修改包清单文件/home/buildbot/mypackages/examples/helloopenwrt/Makefile中的Build/Prepare部分,因为在之前cp命令没有带参数-r无法把补丁文件复制到openwrt构建代码中。以下时修改部分的代码

define Build/Prepare
		mkdir -p $(PKG_BUILD_DIR)
		cp $(SOURCE_DIR)/* $(PKG_BUILD_DIR) -r
		$(Build/Patch)
endef

最后我们执行以下命令,已确保我们的补丁已生效

cd /home/buildbot/openwrt
make package/helloopenwrt/{clean,prepare}
ls -lls build_dir/target-x86_64_musl/helloopenwrt-1.0/

不出意外可以看到build_dir/target-x86_64_musl/helloopenwrt-1.0/下的文件都时重新生成的,而且functions.h、functions.c文件都存在,我们的补丁已生效。

3. 使用补丁修改原有文件

3.1 创建第二个补丁

在上一节中,我们创建了一个补丁,增加了两个文件,但文件中的mul方法并没有被调用,所以我们增加一个新的补丁,在helloopenwrt.c中调用mul方法。

cd /home/buildbot/openwrt
make package/helloopenwrt/{clean,prepare} QUILT=1
cd /home/buildbot/openwrt/build_dir/target-x86_64_musl/helloopenwrt-1.0
quilt push -a
quilt new 101-use_function.patch

3.2 编译原文件

现在使用quilt编辑helloopenwrt.c

quilt edit helloopenwrt.c

helloopenwrt.c

#include <stdio.h>
#include "functions.h"
 
int main(void)
{
        int result = mul(2, 4);
        printf("\nHello, OpenWrt!\nThe mul is '%d'\n", result);
        return 0;
}

保存更改之后,我们可以使用以下命令查看更改内容和生成最终的补丁

quilt diff
quilt refresh

最后返回OpenWrt源码目录,使用新补丁更新软件包

cd /home/buildbot/openwrt
make package/helloopenwrt/update

4. 测试加入补丁的新包

在上一节我们更新了软件包,所以我们先要重新编译软件包

cd /home/buildbot/openwrt
make package/helloopenwrt/{clean,compile}

然后根据第二章的内容,将新的软件包放入openwrt容器中,进行测试。
如果不出意外,安装完新的helloopenwrt包之后,执行helloopenwrt,会出现以下结果

Hello, OpenWrt!
The mul is '8'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值