OpenWrt build notes

This post contains Q&A-style notes on compiling software for OpenWrt or compiling OpenWrt itself.

SDK or Source code?

If you want to cross-compile software to run on an OpenWrt device or rebuild/patch existing OpenWrt packages, the SDK suffices.

The SDK can be found under the <target>/<device> folders on downloads.openwrt.org, note that the snapshots are rebuilt frequently and not a stable target to build for. The source is at https://github.com/openwrt/openwrt, make sure to check out a branch/tag if you don't want the development branch.

Ok, now how do I set it up?

Build tools (tested on Ubuntu 22.04):

sudo apt update
sudo apt install build-essential gawk gcc-multilib flex git gettext libncurses5-dev libssl-dev python3-distutils unzip zlib1g-dev

Feeds:

cd openwrt
./scripts/feeds update -a && ./scripts/feeds install -a

Don't forget to do all this as an unprivileged user (even if you have e.g. a container), the build tools don't like being run as a root.

[Source] Target and package selection

Run make menuconfig. The first three options select the device to target. Most of the rest are for packages, < > means a package will not be built, <*> will build it into the final firmware image (preinstalled), <M> will build it as an installable package file.

Pressing / will open up search.

Configuring is annoying so you can keep the .config file for next time. Copy it back and run make oldconfig or yes "" | make oldconfig if you don't like answering questions.

[Source] Building everything

Run nice make -j$(nproc).

The firmware and sysupgrade images will end up in bin/targets/, packages also under bin/packages/.

How do I build a single package?

make packages/NAMEHERE/compile, that's it.

[SDK] How do I build software that's not in OpenWrt?

Haven't had to do that yet, the answer is probably here.

[SDK] I don't care about packaging, where's the cross-compiler?
./staging_dir/toolchain-<whatever>/bin/<arch>-openwrt-linux-gcc
(This likely won't lead you to success unless all you need is a simple C program.)
How do I apply patches to packages?

There's probably multiple ways but the most convenient is overriding the source tree.

You should have a git checkout with your modifications (commit them!) somewhere. Then:

ln -s /somewhere/.git package/network/services/odhcpd/git-src
make package/odhcpd/{clean,compile} V=s CONFIG_SRC_TREE_OVERRIDE=y
[Source] Which kernel version will I get?

Check for KERNEL_PATCHVER in target/linux/<target>/Makefile.

If a KERNEL_TESTING_PATCHVER is defined too you can switch to the newer kernel by enabling "Global build settings > Use the testing kernel version".

Note that OpenWrt kernels are heavily patched so you can't really use a version other than the predefined ones even if you wanted to.

[Source] Ricing your build flags

If you've ever used Gentoo you will find this fun. [1]

Under "Advanced configuration options" you can configure global compiler flags (TARGET_OPTIMIZATION) and the ones used by the kernel (KERNEL_CFLAGS).

Tell me more!