How patching works in yocto
Asked Answered
C

4

17

I am using BBB to understand the yocto-project. I am not sure how patching works. This is my project directory

├── meta-testlayer
├── poky

meta-test layer contains a "helloworld" example

├── conf
│   └── layer.conf
├── COPYING.MIT
├── README
└── recipes-hello
    └── helloworld
        ├── helloworld-0.1
        │   ├── helloworld.c
        │   ├── helloworld.patch
        │   └── newhelloworld.c
        └── helloworld_0.1.bb

"helloworld.c" and "newhelloworld.c" differ by only one printf() statement. Here is the content of "helloworld.c":

#include <stdio.h>

int main(int argc, char **argv)
{

    printf("Hi, this is my first custom recipe. Have a good day\n");
    return 0;
}

The content of "newhelloworld.c":

#include <stdio.h>

int main(int argc, char **argv)
{

    printf("Let see if patch works\n");
    printf("Hi, this patch is from the test-layer\n");
    return 0;
}

Here is the patch I created using diff helloworld.c newhelloworld.c > helloworld.patch command.

6c6,7
<     printf("Hi, this is my first custom recipe. Have a good day\n");
---
>     printf("Let see if patch works\n");
>     printf("Hi, this patch is from the test-layer\n");

This is the content of "helloworld_0.1.bb" file

SUMMARY = "Simple helloworld application"
SECTION = "examples"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"

#here we specify the source we want to build
SRC_URI = "file://helloworld.c"
SRC_URI += "file://helloworld.patch"

#here we specify the source directory, where we can do all the building and expect sources to be placed
S = "${WORKDIR}"

#bitbake task
do_compile() {
         ${CC} ${LDFLAGS} helloworld.c -o helloworld
}

#bitbake task
do_install() {
         install -d ${D}${bindir}
         install -m 0755 helloworld ${D}${bindir}
}

This is the error message when I run bitbake -c patch helloworld:

NOTE: Executing SetScene Tasks
NOTE: Executing RunQueue Tasks
ERROR: helloworld-0.1-r0 do_patch: Command Error: 'quilt --quiltrc /home/guest/yocto_practice/poky/build-beaglebone/tmp/work/cortexa8hf-neon-poky-linux-gnueabi/helloworld/0.1-r0/recipe-sysroot-native/etc/quiltrc push' exited with 0  Output:
Applying patch helloworld.patch
patch: **** Only garbage was found in the patch input.
Patch helloworld.patch does not apply (enforce with -f)
ERROR: helloworld-0.1-r0 do_patch: Function failed: patch_do_patch
ERROR: Logfile of failure stored in: /home/guest/yocto_practice/poky/build-beaglebone/tmp/work/cortexa8hf-neon-poky-linux-gnueabi/helloworld/0.1-r0/temp/log.do_patch.22267
ERROR: Task (/home/guest/yocto_practice/meta-testlayer/recipes-hello/helloworld/helloworld_0.1.bb:do_patch) failed with exit code '1'
NOTE: Tasks Summary: Attempted 11 tasks of which 8 didn't need to be rerun and 1 failed.

Summary: 1 task failed:
  /home/guest/yocto_practice/meta-testlayer/recipes-hello/helloworld/helloworld_0.1.bb:do_patch
Summary: There were 2 ERROR messages shown, returning a non-zero exit code.
Carlynne answered 9/8, 2017 at 7:23 Comment(2)
There's not enough info here to tell why you were unable to apply the patch -- but maybe it conflicts with the other patch in meta-test? If you put your patch in meta-testlayer then you don't need a bbappend at all: you can just modify helloworld_0.1.bb.Complex
shall I paste the log screenshot here ? Also how can to apply patch from same layer ?Carlynne
I
26

First, create the patch:

diff -u helloworld.c newhelloworld.c > helloworld.patch

or using Git (replace x by the number of commits you want to extract a patch):

git format-patch -x

Two ways to apply the patch:

  • Put it into your test-layer, add a line on your .bb file: SRC_URI += " file://example.patch "

  • Put it in another layer, but it's only needed if it isn't your layer (meta-oe, meta-fsl, meta-qt...)

For this case, use in your .bbappend file:

FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"
SRC_URI += " file://helloworld.patch "
Immediacy answered 9/8, 2017 at 8:7 Comment(10)
I followed both the approaches, but may be I am doing something wrong within the patch file. As in the debug messages I can see the only garbage was found inside the patch inputCarlynne
Your patch is broken, how did you make it?Immediacy
I have added my example.patch file content in the question. If I do bitabke helloworld or bitbake -c patch helloworld I get errors. One more thing I have to apply the patch first and then bake the recipe ? Or can I directly bake the helloworld recipe.Carlynne
bitbake helloworld runs the default "build" task which depends on "patch" task: in other words patching will happen automatically when neededComplex
@jku but still I am unable to patch the recipe. I have example.patch and helloworld.c in same directory. And in helloworld_0.1.bb I added this line SRC_URI = "file://example.patch"Carlynne
First, that's not the exact SRC_URI change @Immediacy suggested, second we can't help if you don't show your work: you need to post at least your bb file and your patch file and the exact error message you get.Complex
Based on the screenshot of the error: your patch file is somehow broken (even if fixed it also wouldn't correctly patch the source code you pasted as far as I can tell -- it's just not a correct patch). Maybe you need to read up on how patches work first?Complex
Have edited the question, included all the files an patch with error message. Kindly have a look at the error now.Carlynne
The diff command has to be invoked from one of the parent directory and this time I used diff -u. It worked. ThanksCarlynne
If we have the changes in git already... why not just point to a new commit ID? Instead of managing individual patch files outside of git? Ironically, the patch files themselves will likely be in a git repo...Matadi
H
4
  • bitbake -c devshell recipe
  • git init
  • git add *
  • git commit (sourcetree recorded by git)
  • Edit the file in any editor you like and then save it eg.(vi /path/to/file)
  • git status (shows that the file is modified)
  • git add /path/to/file
  • git commit -m "a sutaible comment according to the changes you made"
  • git log (shows that changes have be made and commit history)
  • git format-patch HEAD~1 (outputs the patch file created by the last commit)
  • ls (check if patch file is there)
  • Copy the patch file into recipe/files folder
  • execute exit to exit devshell
  • Edit recipe.bb OR create recipe.bbappend file and add patch file in SRC_URI variable
  • Example:
   FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
   SRC_URI += " file://patchfile.patch "
  • Build the image again with bitbake image_name
Henn answered 5/4, 2023 at 12:7 Comment(0)
C
3

Create a .bbappend file for that recipe.

FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
SRC_URI += " file://your.patch "
Cylix answered 19/9, 2022 at 16:26 Comment(0)
G
0

If you want to conveniently add a patch to a recipe, use devtool's devtool modify <recipename>. The Yocto wiki also has a guide on it.

Basic process

One time setup:

devtool modify <recipename>

Then iterate this process:

  • Perform changes in workspace/sources/<recipename>
vi workspace/sources/<recipename>/path/to/src
  • Build recipe including the changes1
devtool build <recipename>
  • Once you're happy, generate the patches2
devtool update-recipe [-a <layerpath>] <recipename>

One time clean up:

devtool reset <recipename>

  1. devtool build <recipe> will use the changes in workdir, while bitbake <recipe> will ignore them
  2. Using -a <layerpath> is optional. Omit it if you'll upstream your patches anyway. See the linked wiki entry for details.
Guyer answered 12/8 at 8:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.