How to use macOS build OpenWRT

准备容器

注意:这一步并非必要的,比如我就是整个系统都是识别大小写的,所以新建一个文件夹即可

更好的实现是,我在自己的服务器(PVE)创建了一个专门进行编译的ubuntu,这样编译不占用本地性能

1
2
3
4
# 创建一个识别大小写的盘
hdiutil create -size 20g -type SPARSE -fs "Case-sensitive HFS+" -volname OpenWRT OpenWRT.sparseimage
#挂载镜像
hdiutil attach OpenWRT.sparseimage
GUI * 未验证是否能够成功
1
磁盘工具 添加卷宗 选择大小20GB 保留20GB 选择 APFS(区分大小写)
1
cd /Volumes/OpenWRT

使用Brew安装需要的包

1
brew install coreutils findutils gawk grep gnu-getopt gnu-tar wget diffutils git-extras quilt make ncurses pkg-config --verbose

配置环境变量

For x86_64 Apple hardware:

1
2
3
4
5
6
7
8
9
10
PATH="/usr/local/opt/gnu-getopt/bin:$PATH"
PATH="/usr/local/opt/make/libexec/gnubin:$PATH"
PATH="/usr/local/opt/gettext/bin:$PATH"
PATH="/usr/local/opt/coreutils/bin:$PATH"
PATH="/usr/local/opt/findutils/libexec/gnubin:$PATH"
export PATH
# 注释掉的变量反正我是没用到过
# PATH="/usr/local/opt/grep/libexec/gnubin:$PATH"
# PATH="/usr/local/opt/gnu-tar/libexec/gnubin:$PATH"
# PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"

For arm64 (Apple Silicon(M1)) Apple hardware:

1
2
3
4
5
6
PATH="/opt/homebrew/opt/make/libexec/gnubin:$PATH"
PATH="/opt/homebrew/opt/gnu-getopt/bin:$PATH"
PATH="/opt/homebrew/opt/gettext/bin:$PATH"
PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:$PATH"
PATH="/opt/homebrew/opt/findutils/libexec/gnubin:$PATH"
export PATH
1
2
# Build前使用
source /Volumes/OpenWRT/env.sh

或者运行一下命令行

1
2
3
#/bin/bash -c "$(curl -fsSL https://blog.songkunda.cn/files/路由器/doENV.sh)"
/bin/bash -c "$(curl -fsSL https://blog.songkunda.cn/files/%E8%B7%AF%E7%94%B1%E5%99%A8/doENV.sh)"

准备物料

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cd /Volumes/OpenWRT
git clone https://github.com/openwrt/openwrt.git
cd openwrt
git pull

# 更新 所有feeds到最新

./scripts/feeds update -a


# 下载所有menuconfig 需要的feeds
./scripts/feeds install <package_name>
./scripts/feeds install -a

# 如需其他版本
#git branch -a
#git tag

编译

1
2
3
4
5
6
7
8
9
10
11
12
13
## 检查环境
make defconfig download clean world

# 配置软件镜像及内核
make menuconfig
make kernel_menuconfig
# 制作自己的配置文件(diffconfig)
./scripts/diffconfig.sh > K3.config

# 编译软件镜像
# make -j $(nproc) V=99
# verbose 在后面添加" V=99 "
make -j $(nproc)

注意事项

make 报错

/Volumes/OpenWrt/openwrt/include/toplevel.mk:29: *** Please use a newer version of GNU make. The version shipped by Apple is not supported. Stop.

1
2
3
sh /Volumes/OpenWRT/doENV.sh
# 如果你已经运行过上面命令 只需要再source就行了
source /Volumes/OpenWrt/env.sh

make: *** No rule to make target ‘menuconfig’. Stop.

1
2
3
4
5
# 你可能没有在源码目录上
cd /Volumes/OpenWRT/openwrt
make menuconfig


Failed to install stub

1
2
3
4
5
6
7
Checking 'ldconfig-stub'... failed.

Build dependency: Failed to install stub

Prerequisite check failed. Use FORCE=1 to override.
gmake: *** [/Volumes/OpenWrt/openwrt/include/toplevel.mk:180: staging_dir/host/.prereq-build] Error 1

  • 解决办法:
1
2
3
4
rm -rf ./feeds
rm -rf ./staging_dir
rm -rf ./tmp
./scripts/feeds clean -f

luci fatal: 过早的文件结束符(EOF)

1
2
3
4
5
6
7
8
9
10
11
Updating feed 'luci' from 'https://git.openwrt.org/project/luci.git;openwrt-21.02' ...
正克隆到 './feeds/luci'...
remote: Enumerating objects: 8917, done.
remote: Counting objects: 100% (8917/8917), done.
remote: Compressing objects: 100% (4380/4380), done.
error: RPC 失败。curl 18 transfer closed with outstanding read data remaining
fetch-pack: unexpected disconnect while reading sideband packet
fatal: 过早的文件结束符(EOF)
fatal: fetch-pack:无效的 index-pack 输出
failed.

  • 解决方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cd  ./feeds
git clone git://git.openwrt.org/project/luci.git -j 4 -b openwrt-21.02 -v --depth 1
#正克隆到 'luci'...
#查找 git.openwrt.org ...完成。
#连接到 git.openwrt.org(端口 9418)... 46.101.214.210 完成。
#remote: Enumerating objects: 8917, done.
#remote: Counting objects: 100% (8917/8917), done.
#remote: Compressing objects: 100% (4380/4380), done.
#remote: Total 8917 (delta 2897), reused 6031 (delta 712), pack-reused 0
#接收对象中: 100% (8917/8917), 7.50 MiB | 18.00 KiB/s, 完成.
#处理 delta 中: 100% (2897/2897), 完成.
#正在更新文件: 100% (4474/4474), 完成.


# 完成后 运行命令让他重建luci.index package info 和 target info
#Updating feed 'luci' from 'https://git.openwrt.org/project/luci.git;openwrt-21.02' ...
#已经是最新的。
#Create index file './feeds/luci.index'
#Collecting package info: done
#Collecting target info: done
./scripts/feeds update luci -a

## 注意 以下安装命令要运行2次 第一次 会有部分依赖luci的缺失 第二次正常安装
./scripts/feeds install -a

编译后固件位置

1
2
bin/bcm53xx/xxx-squashfs-sysupgrade.bin
bin/bcm53xx/xxx-squashfs-sysupgrade.bin