Module 4 · Lesson 110 of 156

Patching recipes and appending tasks from a bbappend

Add patches and extra install steps to any Yocto recipe from your own layer using SRC_URI appends, FILESEXTRAPATHS and do_install:append, without editing upstream recipes.

  • Beginner to advanced
  • 7 min read
  • 30 min lab
What you will need

Ubuntu 24.04 in WSL2 or a VM with a working Poky build directory and the custom layer created in the layers lesson; git

Your place in the module
By the end you can 4 objectives
  • Add a patch to a recipe with SRC_URI += and FILESEXTRAPATHS from a correctly named bbappend
  • Append shell commands to do_install with do_install:append and install files into ${D}
  • Use bbappend variables such as THISDIR, PN and custom variables inside appended tasks
  • Verify an append with bitbake-layers show-appends and bitbake -e
Video coming soonPatching recipes and appending tasks from a bbappend
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from PACKAGECONFIG and package splitting
  • PACKAGECONFIG is a list of enabled feature names; each feature flag carries enable and disable configure arguments plus dependency additions, and disabled features still pass their disable argument to prevent floating dependencies.
  • do_package assigns each installed file to the first package in PACKAGES whose FILES patterns match, with ${PN} last as the catch-all runtime package.
  • Versioned libraries (lib*.so.*) ship in ${PN}; headers, unversioned .so symlinks and pkg-config files ship in ${PN}-dev, which no image installs by default.
01

Adding a patch with SRC_URI

3 min read

Splitting packages is one bbappend use; the other two are patches and task overrides. This lesson answers SRC_URI += file:// patches and do_install:append with variables inside a bbappend.

In the layers lesson you created a layer with bitbake-layers, and in the kernel lesson you appended to the kernel recipe with a config fragment. The mechanism generalises to every recipe. A must have the same base name as the recipe. If the recipe were busybox_1.36.1.bb (check the actual version in your Poky checkout), your file would be busybox_1.36.1.bbappend, or better busybox_%.bbappend: the % wildcard matches any version, so the append survives version bumps. Place it in your layer under a directory that mirrors the recipe's location, with a subdirectory for the files it ships:

bash
mkdir -p meta-mylayer/recipes-core/busybox/busybox

In the scarthgap checkout the recipe is meta/recipes-core/busybox/busybox_1.36.1.bb, and it already has a files directory of its own, which is why this lesson names the new directory after ${PN} in your layer.

The first job is making the patch file findable. A file:// URI is not a path relative to the bbappend: BitBake searches for it in FILESPATH, a colon-separated list of directories derived from the recipe name, version and overrides. Your layer's directories are not in that list by default, so you extend the search path with FILESEXTRAPATHS:

bitbake
FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
SRC_URI += "file://0001-fix-serial-timeout.patch"

points at the bbappend's own directory, and ${PN} is the recipe name (busybox), so ${THISDIR}/${PN} resolves to the busybox subdirectory you just created. The := assignment expands immediately at parse time, which captures THISDIR while the bbappend is being read, and the trailing colon matters because the value is one entry in a colon-separated list.

SRC_URI += adds one entry with a separating space, exactly the operator semantics from the variables lesson. Because the file name ends in .patch, nothing else is needed: do_patch, which runs after do_unpack and before do_configure, applies every .patch and .diff entry in SRC_URI automatically. The default patch tool is quilt, and the default strip level is 1, which matches patches made by git format-patch: their paths carry a/ and b/ prefixes, the same -p1 convention as patch(1). If a patch was made without prefixes, set the strip level per entry with file://fix.patch;striplevel=0. Adding ;apply=no keeps a file fetched but unapplied, which is useful for reference diffs you do not want in the build.

To produce the patch itself, use the git workflow from the patch sessions: in a checkout of the same source version the recipe builds, commit the fix and run git format-patch -1. Copy the resulting 0001-*.patch into the busybox directory next to the bbappend. BitBake checksums every SRC_URI file, so editing the patch later invalidates the affected task signatures and the next build re-applies it automatically. The devtool lesson later in the module automates producing patches against the exact recipe source.

The patch enters through SRC_URI and is applied by do_patchdo_fetchdownloadsthe sourcesdo_unpackextractsinto theworkdirdo_patchappliesSRC_URIpatchesdo_configuredo_compiledo_fetchdownloadsthe sourcesdo_unpackextractsinto theworkdirdo_patchappliesSRC_URIpatchesdo_configuredo_compile
Where do_patch sits in the task pipeline, and what feeds it
02

Appending to do_install

3 min read

The second common bbappend use is installing something the recipe does not ship: a default configuration file, a udev rule, an extra script. do_install is a shell task that runs under fakeroot and copies built artifacts into ${D}, a staging directory whose contents later become the package. You extend it with the :append override on the task name. Here is the complete bbappend, patch and extra file together:

bitbake
FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
SRC_URI += "file://0001-fix-serial-timeout.patch"
SRC_URI += "file://sensornode.conf"

do_install:append() {
    install -d ${D}${sysconfdir}
    install -m 0644 ${WORKDIR}/sensornode.conf ${D}${sysconfdir}/sensornode.conf
}

sensornode.conf is fetched like any file:// source and lands in the work directory. install -d creates the destination directory and install -m 0644 copies the file with explicit permissions, the standard install(1) options. ${sysconfdir} is /etc and ${D} is the staging root, so the file ends up at /etc/sensornode.conf inside the package, never on your build host. The appended body is concatenated after the original function body: upstream's install steps run first, yours run last, in the same shell environment and the same fakeroot session. do_install:prepend() also exists and runs before the original body; use it rarely, since most additions belong at the end.

WORKDIR versus UNPACKDIR

Where fetched files land depends on the release. For years file:// sources were copied into ${WORKDIR}. Yocto 5.1 introduced ${UNPACKDIR} (it is mentioned in the 5.1 migration guide, and it does not exist in the scarthgap 5.0 sources checked here) and moved unpacked sources there, so on recent releases write ${UNPACKDIR}/sensornode.conf instead. Check your release's migration guide; the examples here use ${WORKDIR}.

Do not confuse task appends with variable appends. For a variable, FOO:append = " extra" glues the string onto the value at expansion time, and the leading space is yours to supply: forget it and file://fix.patch welds itself onto the previous URI, producing one broken entry. For a task, do_install:append() { ... } appends function body code, and BitBake handles the separation for you.

Never replace a task by accident

Writing a plain do_install() { ... } in a bbappend replaces the recipe's entire install logic. Packages come out empty or broken, and a later recipe upgrade changes nothing visible because your replacement still wins. Use :append unless you truly intend to take over the task.

Redefining a task versus appending to itNewdo_install()do_install:append()Original bodydiscardedkept, runsfirstYour codereplacesupstreamruns afterupstreamRecipe upgradesilent driftupstream kept
Redefining do_install versus appending to it
03

Variables in a bbappend, and proving it worked

2 min read

Because the bbappend is parsed into the same datastore, PN, WORKDIR, D, sysconfdir, bindir and your own variables all behave exactly as they do in a recipe. You can parameterise the append instead of repeating literals:

bitbake
SENSORNODE_CONF = "sensornode.conf"
SRC_URI += "file://${SENSORNODE_CONF}"

do_install:append() {
    install -d ${D}${sysconfdir}
    install -m 0644 ${WORKDIR}/${SENSORNODE_CONF} ${D}${sysconfdir}/${SENSORNODE_CONF}
}

BitBake expands ${SENSORNODE_CONF} before the shell ever runs: the task script written into the work directory already contains the literal file name. Inside an appended shell body, ${...} is always BitBake syntax, while a plain $HOME-style reference without braces passes through to the shell. Mixing the two up is a classic source of empty paths in install commands.

Verification is a habit, not an afterthought. A bbappend whose name matches no recipe at all is reported: in a real run on Poky scarthgap, a file named nonexistent_1.0.bbappend made BitBake stop with ERROR: No recipes in default available for: and the path of that file (the variable BB_DANGLINGAPPENDS_WARNONLY turns this into a warning). The silent failures are different: an append that matches the right recipe but is not doing what you intend, such as a patch that is never listed in SRC_URI, or a files directory that is not in the search path.

Step by stepProve the append is active and correct
  1. bitbake-layers show-appends: confirm busybox lists your bbappend. A missing entry means a name mismatch or a layer missing from BBLAYERS.

  2. bitbake -e busybox: inspect the final datastore. Grep for ^SRC_URI= to see your entries, and ^FILESPATH= to confirm your directory is searched.

  3. bitbake busybox: rebuild. Changed file checksums rerun the affected tasks automatically.

  4. Inspect the work directory: under tmp/work/ the unpacked source shows the patched code (with quilt, patches/series lists your patch), and the image directory holds ${D}, where etc/sensornode.conf must exist. Exact paths depend on your machine and recipe version.

bash
bitbake-layers show-appends
bitbake -e busybox | grep '^SRC_URI='
bitbake busybox
The verification loop for a bbappendshow-appendsis theappendmatched?bitbake -einspectfinalSRC_URIbitbakebusyboxrebuildaffectedtasksinspectimage dirfilepresentunder ${D}show-appendsis theappendmatched?bitbake -einspectfinalSRC_URIbitbakebusyboxrebuildaffectedtasksinspectimage dirfilepresentunder ${D}
The verification loop for any bbappend change

You can now patch any recipe, extend its tasks and parameterise the changes from your own layer. The next lesson, BSP layers and machine configuration, answers where these appends live in a board support package and how machine configuration decides which of them apply to your board.

Key takeaways

  • A bbappend is parsed after the recipe it matches, so += and :append extend rather than replace upstream work
  • Files ending in .patch or .diff in SRC_URI are applied automatically by do_patch, by default with quilt at strip level 1
  • FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:" lets file:// URIs find files stored next to the bbappend
  • do_install:append runs after the original task body under fakeroot, with ${D} as the staging root
Reflect
  1. Explain to a colleague, in two sentences: add a patch to a recipe with SRC_URI += and FILESEXTRAPATHS from a correctly named bbappend
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?
On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

References and further reading

Find your next step

↑ ↓ to move · Enter to open · Esc to close