Build system usage

See also: Build system setup, Compiler optimization tweaks

  • Do everything as a normal user, do not log in as root and do not use sudo.
  • Do not build in a directory that has spaces in its full path.

Typical build steps:

# Download and update the sources
git clone https://git.openwrt.org/openwrt/openwrt.git openwrt
cd openwrt
git pull
 
# Update the feeds
scripts/feeds update -a
scripts/feeds install -a
 
# Select a specific code revision
git checkout v19.07.7
 
# Configure the firmware image and the kernel
make menuconfig
make kernel_menuconfig
 
# Build the firmware image
make -j $(nproc) defconfig download clean world

The build steps are explained in detail below.

Clone the Git repository using the following command.

git clone https://git.openwrt.org/openwrt/openwrt.git [<buildroot>]

:!: Sources in development branch change frequently. It is recommended that you work with the latest sources.

git pull

Pull the latest updates for the feeds in case it became outdated.

scripts/feeds update -a

Make the downloaded package/packages available in make menuconfig.

scripts/feeds install <package_name>
scripts/feeds install -a
  1. Add a line to feeds.conf.default. src-link my_packages <buildroot>/my_packages. Replace the <buildroot> with cloned openwrt sources directory e.g. ~/openwrt
  2. Create the dir: mkdir -p <buildroot>/my_packages/<section>/<category>/<package_name>. Replace the <package_name> with the name of your package e.g. mkdir -p my_packages/net/network/rpcbind. The section and category can be found in the Makefile.
  3. Download the Makefile from github, download the source package from sources.openwrt.org and place them in the <package_name> directory.
  4. Now run: scripts/feeds update -a; scripts/feeds install <package_name>
  5. If you are doing this to resolve a dependency you can run install one more time and you should notice the dependency has been resolved.

This step can be skipped you want to build bleeding edge images.

Each branch contains the baseline code for the release version (e.g. 17.01, 18.06, 19.07) and the individual releases (e.g. 17.01.1, 18.06.2, 19.07.3). Each branch is intended to contain stable code with carefully selected fixes and updates backported from the development branch.

To use a branch, you should first clone the Git repository using the git clone command shown above. Then move to the branch by using the git checkout command.

# Use OpenWrt 19.07 branch
git checkout openwrt-19.07

:!: When changing branches, it is recommended to perform a thorough scrub of your source tree by using the make distclean command. This ensures that your source tree does not contain any build artifacts or configuration files from previous build runs.

Checkout a tagged release’s source code. See above for an explanation. The same conditions and recommendations apply here. This may be useful when you want to be able to install packages from the repositories for a long time.

# Fetch and list tags
git fetch --tags
git tag -l
 
# Use OpenWrt 19.07.7 release
git checkout v19.07.7

Compile OpenWrt in a way that it gets the same packages as the default official image:

# OpenWrt 19.07 and later
wget https://downloads.openwrt.org/releases/19.07.0/targets/ramips/mt7621/config.buildinfo -O .config
 
# OpenWrt 18.06 and before
wget https://downloads.openwrt.org/releases/18.06.0/targets/ramips/mt7621/config.seed -O .config

Sync all feeds to a specific OpenWrt code revision.

REV_HASH="4c73c34ec4215deb690bf03faea2a0fe725476f0"
git checkout ${REV_HASH}
REV_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
sed -i -e "/^src-git\S*/s//src-git-full/" feeds.conf.default
scripts/feeds update -a
sed -n -e "/^src-git\S*\s/{s///;s/\s.*$//p}" feeds.conf.default \
| while read -r FEED_ID
do
REV_DATE="$(git log -1 --format=%cd --date=iso8601-strict)"
REV_HASH="$(git -C feeds/${FEED_ID} rev-list -n 1 --before=${REV_DATE} ${REV_BRANCH})"
sed -i -e "/\s${FEED_ID}\s.*\.git$/s/$/^${REV_HASH}/" feeds.conf.default
done
scripts/feeds update -a
scripts/feeds install -a

The build system configuration interface handles the selection of the target platform, packages to be compiled, packages to be included in the firmware file, some kernel options, etc.

Start the build system configuration interface by writing the following command:

make menuconfig

This will update the dependencies of your existing configuration automatically, and you can now proceed to build your updated images.

You will see a list of options. This list is really the top of a tree. You can select a list item, and descend into its tree.

To search for the package or feature in the tree, you can type the “/” key, and search for a string. This will give you its locations within the tree.

For most packages and features, you have three options: y, m, n which are represented as follows:

  • pressing y sets the <*> built-in label
    This package will be compiled and included in the firmware image file.
  • pressing m sets the <M> package label
    This package will be compiled, but not included in the firmware image file, e.g. to be installed with opkg after flashing the firmware image file to the device.
  • pressing n sets the < > excluded label
    The source code will not be processed.

When you save your configuration, the file <buildroot>/.config will be created according to your configuration.

When you open menuconfig you will need to set the build settings in this order (also shown in this order in menuconfig's interface):

  1. Target system (general category of similar devices)
  2. Subtarget (subcategory of Target system, grouping similar devices)
  3. Target profile (each specific device)
  4. Package selection
  5. Build system settings
  6. Kernel modules

Select your device's Target system first, then select the right Subtarget, then you can find your device in the Target profile's list of supported platforms.

Beside make menuconfig another way to configure is using a configuration diff file. This file includes only the changes compared to the default configuration. A benefit is that this file can be version-controlled in your downstream project. It's also less affected by upstream updates, because it only contains the changes.

Creating diff file

Save the build config changes.

# Write the changes to diffconfig
scripts/diffconfig.sh > diffconfig

Note: Since r2752 LEDE firmware make process automatically creates the configuration diff file config.seed file to the target image directory.

Using diff file

These changes can form the basis of a config file <buildroot>/.config. By running make defconfig these changes will be expanded into a full config.

# Write changes to .config
cp diffconfig .config
 
# Expand to full config
make defconfig

These changes can also be added to the bottom of the config file (<buildroot>/.config), by running make defconfig these changes will override the existing configuration.

# Append changes to bottom of .config
cat diffconfig >> .config
 
# Apply changes
make defconfig

The build system integrates quilt for easy patch management:

In case you want to include some custom configuration files, the correct place to put them is:

  • <buildroot>/files/

For example, let's say that you want an image with a custom /etc/config/firewall or a custom etc/sysctl.conf, then create this files as:

  • <buildroot>/files/etc/config/firewall
  • <buildroot>/files/etc/sysctl.conf

E.g. if your <buildroot> is ~/source and you want some files to be copied into firmware image's /etc/config directory, the correct place to put them is ~/source/files/etc/config.

make defconfig

will produce a default configuration of the target device and build system, including a check of dependencies and prerequisites for the build environment.

Defconfig will also remove outdated items from .config, e.g. references to non-existing packages or config options.

It also checks the dependencies and will add possibly missing necessary dependencies. This can be used to “expand” a short .config recipe (like diffconfig output, possible even pruned further) to a full .config that the make process accepts.

Note that make kernel_menuconfig modifies the Kernel configuration templates of the build tree and clearing the build_dir will not revert them. :!: Also you won't be able to install kernel packages from the official repositories when you make changes here.

While you won't typically need to do this, you can do it:

make kernel_menuconfig CONFIG_TARGET=subtarget

CONFIG_TARGET allows you to select which config you want to edit, possible options: target, subtarget, env.

The changes can be reviewed and reverted with:

git diff target/linux/
git checkout target/linux/

Source mirrors

The 'Build system settings' include some efficient options for changing package locations which makes it easy to handle a local package set:

  1. Local mirror for source packages
  2. Download folder

In the case of the first option, you simply enter a full URL to the HTTP 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 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.

Before running final make it is best to issue make download command first, this step will pre-fetch all source code for all dependencies, this enables you compile with more CPU cores, e.g. make -j10, for 4 core, 8 thread CPU works great.

If you try compiling OpenWrt on multiple cores and don't download all source files for all dependency packages it is very likely that your build will fail.

make download

Everything is now ready for building the image(s), which is done with one single command:

make

This should compile toolchain, cross-compile sources, package packages, and generate an image ready to be flashed.

make download will pre-download all source code for all dependencies, this will enable multi core compilation to succeed, without it is is very likely to fail. make -jN will speed up compilation by using up to N cores or hardware threads to speed up compilation, make -j9 fully uses 8 cores or hardware threads.

Example of pre-downloading and building the images on a 4 core CPU:

make -j5 download world

You can use ''nproc'' command to get available CPU count:

make -j $(nproc) download world

or a better macro with nproc+1:

make -j $(($(nproc)+1))

Building in the background

If you intend to use your system while building, you can have the build process use only idle I/O and CPU capacity like this (4 core, 8 thread CPU):

make download
ionice -c 3 nice -n19 make -j9

Building single packages

When developing or packaging software, it is convenient to be able to build only the package in question, e.g. with package jsonpath:

make package/utils/jsonpath/compile V=s

For a rebuild:

make package/utils/jsonpath/{clean,compile} V=s

It doesn't matter what feed the package is located in, this same syntax works for any installed package.

Spotting build errors

If for some reason the build fails, the easiest way to spot the error is to do:

make V=s 2>&1 | tee build.log | grep -i -E "^make.*(error|[12345]...Entering dir)"
 
make V=s 2>&1 | tee build.log | grep -i '[^_-"a-z]error[^_-.a-z]' 
(may not work)

:!: If grep throws an error, use fgrep instead.

The above saves a full verbose copy of the build output (with stdout piped to stderr) in ~/source/build.log and shows errors on the screen (along with a few spurious instances of 'error').

Another example:

ionice -c 3 nice -n 20 make -j 2 V=s CONFIG_DEBUG_SECTION_MISMATCH=y 2>&1 | tee build.log

The above saves a full verbose copy of the build output (with stdout piped to stderr) in build.log while building using only background resources on a dual core CPU.

Yet another way to focus on the problem without having to wade through tons of output from Make as described above is to check the corresponding log in logs folder. i.e. if the build fails at make[3] -C package/kernel/mac80211 compile, then you can go to <buildroot>/logs/package/kernel/mac80211 and view the compile.txt found there.

Getting beep notification

Depending on your CPU, the process will take a while, or while longer. If you want an acoustic notification, you could use this way:

make ...; echo -e '\a'

Ignore build errors

If you are building everything (not just the packages to make a flashable image), you will probably want to keep building all packages even if some have compile errors and won't be built.

# Ignore compilation errors
IGNORE_ERRORS=1 make ...
 
# Ignore all errors including firmware assembly stage
make -i ...

After a successful build, the freshly built image(s) can be found below the newly created <buildroot>/bin directory. The compiled files are additionally classified by the target platform and subtarget, so e.g. a generic firmware built for an ar71xx device will be located in <buildroot>/bin/targets/ar71xx/generic directory (and the package files are below <buildroot>/bin/packages/mips_24kc).

E.g. if your <buildroot> is ~/source, the binaries are in ~/source/bin/targets/ar71xx/generic and ~/source/bin/packages/mips_24kc.

See also: Directory structure

You might need to clean your build environment every now and then. The following make-targets are useful for that job.

make clean

Delete contents of the directories /bin and /build_dir. This doesn't remove the toolchain, and it also avoids cleaning architectures/targets other than the one you have selected in your .config. It is a good practice to do make clean before a build to ensure that no outdated artefacts have been left from the previous builds. That may not be necessary always, but as a general rule it helps to ensure quality builds.

make dirclean

This is your basic “full clean” operation. Delete contents of the directories /bin and /build_dir and additionally /staging_dir and /toolchain (=the cross-compile tools), /tmp (e.g data about packages) and /logs.

make distclean

Nuke everything you have compiled or configured and also deletes all downloaded feeds contents and package sources. :!: In addition to all else, this will erase your build configuration <buildroot>/.config. Use only if you need a “factory reset” of the build system!

In more time, you may not want to clean so many objects, then you can use some of the commands below to do it.

# Clean linux objects
make target/linux/clean
 
# Clean package base-files objects
make package/base-files/clean
 
# Clean luci objects
make package/luci/clean

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.

Try to update the main source and all the feeds, however beware of other potential problems. Check for related issues in the bugtracker, otherwise report the problem there mentioning the package, target (CPU, image, etc.) and code revisions (main & package).

Some packages may not be updated properly and built after they got stuck with old dependencies, resulting in warnings at the beginning of the compilation looking similar to:

WARNING: Makefile 'package/feeds/packages/openssh/Makefile' has a dependency on 'libfido2', which does not exist

The build environment can be recovered by uninstalling and reinstalling the failing package

$ scripts/feeds uninstall openssh
Uninstalling package 'openssh'
$ scripts/feeds install openssh
Installing package 'openssh' from packages
Installing package 'libfido2' from packages
Installing package 'libcbor' from packages

Run make menuconfig and enable compilation for your package. It should be labeled with <*> or <M> to work correctly.

When you execute make to build a flashable image for your device, both a sysupgrade and a factory image should be generated for every board that is linked to the device profile that you have selected via make config or make menuconfig.

If running make does not yield images for one (or even all) of the boards linked to the device profile that you have selected, than you probably have selected/enabled too many options or packages, and the image was too big to be flashed onto your device.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
  • Last modified: 2021/04/23 13:52
  • by vgaetera