# [BSP] Add an app package with patches in Openwrt
###### tags: `openwrt`, `package`, `patch`, `quilt`
[toc]
## Preface
This is an example to show
1. how to add a new application in openwrt
2. how to add a patch for an application in openwrt
## Add a new application package
### Directory structure
The folder "patches" is optional.
```
package/mytest
├── Config.in
├── Makefile
├── [patches]
│ └── 999-patch-test.patch
└── src
├── main.c
└── Makefile
```
### Add source code and Makefile and (patch if needed)
1. Prepare your main program main.c and Makefile in advance and follow the aforementioned file tree.
2. Write your own menuconfig option Config.in and Makefile in openwrt
{%gist 007b3a263a8e3591b50461f5becac66f%}
In openwrt Makefile, it's worth to notice that we have to add below line in Build/Prepare function as shown in the following line 4.
:::info
*$(call Build/Prepare/Default)*
:::
```=
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
$(call Build/Prepare/Default)
endef
```
### Add a patch
1. Utilize quilt
- Setup **quilt**
```shell
Tomas# sudo apt-get install quilt
Tomas# quilt --version
0.63
Tomas# 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
```
```shell
Tomas::openwrt# make menuconfig
Tomas::openwrt# make package/mytest/{clean,prepare} V=s QUILT=1
Tomas::openwrt# cd build_dir/target-arm_cortex-a7_musl-1.1.16_eabi/mytest-0.0.1
Tomas::mytest-0.0.1# quilt push -a
Tomas::mytest-0.0.1# quilt new 999-patch-test.patch
Tomas::mytest-0.0.1# quilt edit main.c
Tomas::mytest-0.0.1# quilt diff
Tomas::mytest-0.0.1# quilt refresh
Tomas::mytest-0.0.1# cd ../../../
Failed! cannot work (Tomas::openwrt# make package/mytest/update V=s)
Tomas::openwrt# cp -rf build_dir/target-arm_cortex-a7_musl-1.1.16_eabi/mytest-0.0.1/patches package/mytest/
```
2. Utilize diff
```shell
Tomas::openwrt# cd package/mytest
Tomas::mytest# mkdir patches
Tomas::mytest# cp src/main.c patches
Tomas::mytest# cp src/main.new patches
Tomas::mytest# cd patches
Tomas::patches# vi main.n (modify the new file)
Tomas::patches# diff -ruN main.c main.n > 999-patch-test.patch
Tomas::patches# vi 999-patch-test.patch (modify the first two line)
before
--- main.c>-2022-08-01 16:37:40.009611327 +0800
+++ main.n>-2022-08-01 16:37:55.206101827 +0800
after
--- a/main.c
+++ b/main.c
Tomas::patches# rm main.*; cd ../../..
```
### Build and compile
```shell
Tomas::openwrt# make package/mytest/{clean,compile} V=s
```
### Demo
When you "make menuconfig", you will get



## Reference
https://openwrt.org/docs/guide-developer/toolchain/use-patches-with-buildsystem
https://openwrt.org/docs/guide-developer/helloworld/chapter7
https://blog.51cto.com/linuxcgi/2059410
https://openwrt.org/zh-tw/doc/devel/packages
https://openwrt-devel.openwrt.narkive.com/9m6HmCTB/working-with-patches
https://blog.csdn.net/wind0419/article/details/82996738
https://www.796t.com/content/1542777546.html
https://github.com/mwarning/openwrt-examples