谷动谷力

 找回密码
 立即注册
查看: 641|回复: 0
打印 上一主题 下一主题
收起左侧

【openwrt】OpenWrt编译 – 说明

[复制链接]
跳转到指定楼层
楼主
发表于 2023-7-11 11:43:53 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
【openwrt】OpenWrt编译 – 说明

编译准备
OpenWrt固件镜像文件约8MB大小,编译nstallable OpenWrt固件镜像文件,需要准备:
  • 在您的操作系统上openwrt开发环境(参考:【openwrt】基于 WSL的openwrt开发环境(构建系统)配置-谷动谷力 (sunsili.com))。
  • 大约3-4 GB的空余硬盘空间
  • 环境变量:
    • 不应设置SED。如已设置,在编译前执行`unset SED`。(参见Ticket 10612.)
    • GREP_OPTIONS不应使用--initial-tab或其它会影响其输出的选项
    • 在您~/.bashrc文件中的PATH变量中,将<buildroot dir>/staging_dir/host/bin and <buildroot dir>/staging_dir/toolchain-<platform>-<gcc_ver>-<libc_ver>/bin放在最前。构建开始后很快就会创建staging目录不久创建,而toolchain目录将在toolchain构建开始时创建。构建过程将创建多个子shell,其中会有一些需要toolchain可执行文件设置于PATH变量中.

步骤
1. 以非root用户登录进行相关操作
2. 在<buildsystem root>目录中完成所有命令,例如~/openwrt/trunk/


  • 更新OpenWrt源。
  • 更新feeds的package 包.
  • 配置编译。
  • 开始编译。这会自动编译工具链,交叉编译源代码,打包包文件,最后生成可以可以刷写的镜像文件。
  • 进入安装OpenWrt环节

GIT签出源码
如果您想签出主干代码,使用如下命令:
git clone https://www.github.com/openwrt/openwrt
如果您想签出某个发布版源码,使用如下命令:
git clone https://www.github.com/openwrt/openwrt -b chaos_calmer(此版本已失效)
(请于Github上验证发布版名称.)
Github有的版本如下:
Default branch
  • main     Updated 17 hours ago by Rafał Miłecki              Default

Active branches
  • openwrt-21.02    Updated 17 hours ago by Rafał Miłecki
  • openwrt-22.03    Updated 17 hours ago by Rafał Miłecki
  • openwrt-23.05    Updated 17 hours ago by Rafał Miłecki
  • master               Updated 17 hours ago by Rafał Miłecki




Stale branches

用Git更新源码
OpenWrt源码变动频繁。建议总是使用最新的源码进行工作。
git pull

更新源(Feeds)
更新源:
./scripts/feeds update -a
安装下载好的包: (可选)
./scripts/feeds install -a   (或者 'install <ACKAGENAME>' )

镜像配置
开始OpenWrt Buildroot基于ncurses文本配置界面:
make menuconfig
同配置Linux内核类似,几乎每一个设置都有三个选项:y / m / n,分别代表如下含义:

<*> (按`y`键)这个包会被包含进固件镜像

<M> (按下`m`键)这个包会在生成刷新OpenWrt的镜像文件以后被编译,但是不会被包含进镜像文件

< > (按`n`键)这个包不会被编译

<*> <M>< > (按‘空格’键) 三个选项依次轮选

------>  (按‘空格’或‘回车Enter’键)进入下级 菜单

当你保存你的配置后,~/openwrt/.config这个文件会按照你的配置被生成。

\\编译环境会提供一个这样的配置文件给OpenWrt Chaos Calmer 15.05.1 r49396。 例如,MT7628.

  • 这个菜单允许你选择目标平台,也就是你想要用来编译你要包含进固件镜像的软件包的工具链(toolchain)的版本。
  • 在更新源码和源以后运行这个配置界面,确保你的配置中包含的软件包是最新的。

默认配置(Defconfig)make defconfig
将会生成一个通用的编译系统配置,这个配置包含了一个对于编译环境的先决条件和依赖的检查,同时会安装缺失的组件并再次运行。

总体配置
Menuconfig拥有一个文本界面,它包括选择要处理的目标平台,要编译的软件包,要被包含进固件文件的软件包和一些内核设置等。
make menuconfig
这会自动更新你现在存在的配置的依赖,方便你你准备编译你更新过的镜像。
It has from the beginning on been the intention, with the development of 'menuconfig', to create a simple yet powerful environment for the configuration of individual OpenWrt images. Menuconfig is more or less self-explanatory, and even the most specialized configuration needs can be solved with its help. Depending on the the particular target platform, package requirements and kernel module needs, the standard process of configuration will include modifying:
  • 目标系统
  • 软件包设定
  • 系统构建设定
  • 内核模块
Target system is selected from the extensive list of supported platforms, with the numerous target profiles – ranging from specific devices to generic profiles, all depending on the particular device at hand. Package selection has the option of either 'selecting all package', which might be un-practical in certain situation, or relying on the default set of packages will be adequate or make an individual selection. It is here needed to mention that some package combinations might break the build process, so it can take some experimentation before the expected result is reached. Added to this, the OpenWrt developers are themselves only maintaining a smaller set of packages – which includes all default packages – but, the feeds-script makes it very simple to handle a locally maintained set of packages and integrate them in the build-process.
这儿有三种选项可选:
  • < > 该代码将不会被编译
  • <M> 该代码将被交叉编译,生成的ipk软件包将被放在 /buildsystem/bla/bla/bla, 但该软件包不会放入固件中
  • <*> 该代码将被放入固件中 (on the SqashFS partition)
The final step before the process of compiling the intended image(s) is to exit 'menuconfig' – this also includes the option to save a specific configuration or load an already existing, and pre-configured, version.
Exit the TUI, and choose to save your settings.

内核配置
While you won't typically need to do this, you can do it:
make kernel_menuconfig
Source Mirrors
The 'Build system settings' include some efficient options for changing package locations which makes it easy to handle a local package set:
  • Local mirror for source packages
  • Download folder
In the case of the first option, you simply enter a full URL to the web or ftp server on which the package sources are hosted. Download folder would in the same way be the path to a local folder on the build system (or network). If you have a web/ftp-server hosting the tarballs, the OpenWrt build system will try this one before trying to download from the location(s) mentioned in the Makefiles . Similar if a local 'download folder', residing on the build system, has been specified.
The 'Kernel modules' option is required if you need specific (non-standard) drivers and so forth – this would typically be things like modules for USB or particular network interface drivers etc.

源镜像

“生成系统设置”包括一些用于更改包位置的有效选项,这使处理本地包集变得容易:

  • 源程序包的本地镜像
  • 下载文件夹


在第一个选项的情况下,只需输入包源所在的web或ftp服务器的完整URL。下载文件夹也是构建系统(或网络)上本地文件夹的路径。如果你有一个web/ftp服务器托管tarball,OpenWrt构建系统会在尝试从Makefiles中提到的位置下载之前尝试这个。如果已指定驻留在生成系统上的本地“下载文件夹”,则类似。

如果您需要特定(非标准)驱动程序等,则需要“内核模块”选项——这通常是USB模块或特定网络接口驱动程序等

定制固件
在很多情况下,您可能需要使用自己准备好的配置去创建一个自己定制的固件,如果是这样,请把您的配置文件放在下面的路径:
<buildroot dir>/files/
举例:假如说您需要一个使用预配置文件/etc/config/firewall来构建固件,那么把您的firewall配置文件文件放到下面的路径:
<buildroot dir>/files/etc/config

构建镜像
为构建镜像,一切已就绪。现只需一条命令:
make
或者:
make world
该命令会触发一连串的活动。之前也说过,它将会:
  • 编译工具链
  • 然后用这个工具链交叉编译源文件
  • 创建opkg软件包
  • 创建可随时刷机的镜像。

构建选项
在多核CPU上构建
可通过-j选项启用多个进程以加速构建过程:
make -j8 (多进程编译需要电脑支持)
  • 使用标准公式 <你的CPU个数 + 1>
  • 构建过程中若产生随机错误,尝试去掉-j 加V=s选项重新构建

在后台构建
如果你想在构建时干别的,你可以让构建进程只使用闲置的CPU和I/O资源(双核CPU):
ionice -c 3 nice -n 20 make -j 2

构建单个包
为OpenWrt开发或打包软件时,可以方便地只构建关心的包(以cups包为例):
make package/cups/compile V=99
For the package mc (midnight commander), which is contained the feed packages it looks like this:
make package/feeds/packages/mc/compile v=99

高亮构建错误
如果构建不知怎么搞的出错了,亮出错误的最简单方式是:
make V=99 2>&1 | tee build.log | grep -i error
以上命令会保存一份详尽的编译输出(同时把stdout泵至stderr)到/openwrt/build.log,并只在屏幕上输出错误。
另一个例子:
ionice -c 3 nice -n 20 make -j 2 V=99 CONFIG_DEBUG_SECTION_MISMATCH=y 2>&1 | tee build.log | egrep -i '(warn|error)'
再来一个例子:
somthing something screen
以上命令会保存一份详尽的编译输出(同时把stdout泵至stderr)到build.log,并只在屏幕上输出警告和错误,且在双核CPU上只使用后台资源。

编译时声音提示
取决于你的CPU,构建过程可能要花上很久、或更久的时间。不清楚什么时候编译完,可加提示音,可使用echo -e '\a':
make V=99 ; echo -e '\a'

本地固件
编译成功后的固件bin文件在 /openwrt/bin 目录下.
cd bin
ls

清理编译目标
Cleaning Up
更新过编译环境,请先清理编译目标:
You might need to clean your build environment every now and then. The following make-targets are useful for that job:

Clean
make clean
deletes contents of bin and build_dir directories.
删除bin和build_dir目录下的内容

Dirclean
make dirclean
deletes contents of /bin and /build_dir directories and additionally /staging_dir and /toolchain (=the cross-compile tools). 'Dirclean' is your basic “Full clean” operation.
删除bin / build_dir /staging_dir / toolchain 目录的内容, 相当于 make Full clean 操作

Distclean
make distclean
nukes everything you have compiled or configured and also deletes all downloaded feeds contents and package sources.
CAUTION: In addition to all else, this will erase your build configuration (.config), your toolchain and all other sources. Use with care!
There are numerous other functionalities in the OpenWrt build system, but the above should have covered some of the fundamentals.
彻底清除编译产物及脚本(feeds package)下载的包(dl) (慎用)
注意: 此看操作还清除编译配置(.config), 工具链,以及openwrt外其他代码。

例程
Troubleshooting
First get more information on the problem using the make option “make V=99”.
  • Missing source code file, due to download problems.
First check if the URL path in the make file contains a trailing slash, then try with it removed (helped several times). Otherwise try to download the source code manually and put it into “dl” directory
  • Compilation errors.
Try to update the main source and all the feeds (Warning! May result in other problems). Check for a related bug in (TRAC), use the filters to find it. Otherwise report the problem there, by mentioning the package, the target data (CPU, image, etc.) and the code revisions (main & package). Compiling with make -j ... sometimes gives random errors. Try compiling without -j first before reporting the problem.

故障排除

首先使用make时加选项“make V=99”可在编译时获取更多信息。

由于下载问题,缺少源代码文件

首先检查make文件中的URL路径是否包含尾部斜杠,然后尝试将其删除(多次尝试)。实在下载不了,请尝试手动下载源代码并将其放入“dl”目录

编译错误

尝试更新主源和所有提要(警告!可能会导致其他问题)。检查(TRAC)中的相关错误,使用过滤器查找。否则,通过提及包、目标数据(CPU、图像等)和代码修订(main和package)来报告那里的问题。使用make -jn 多线程编译。。。有时会出现随机错误。在排查问题时,请使用-j1(或省略-j选项)进行编译。

参考
+10

本帖被以下淘专辑推荐:

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|深圳市光明谷科技有限公司|光明谷商城|Sunshine Silicon Corpporation ( 粤ICP备14060730号|Sitemap

GMT+8, 2024-4-29 22:33 , Processed in 0.139902 second(s), 44 queries .

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表