yoctobitbake

Yocto: Overriding function in existing recipe


I am new to Yocto. I am trying to modify an existing function in a recipe. I am a little confused on how to override the function using a bbappend file.

I have the following code in an existing recipe:

fn_functionA () {
    pushd ${WORKDIR}/folder1
    git submodule update --init --recursive submodule1
    popd
}
do_unpack[postfuncs] += "fn_functionA"

I need to edit this function to the following:

fn_functionA () {
        pushd ${WORKDIR}/folder1
        git submodule update --init --recursive submodule1
        popd
        pushd ${WORKDIR}/folder1/submodule1
        git fetch "ssh link to project" refs/changes/99/68099/1 && git checkout FETCH_HEAD
        popd
    }
    do_unpack[postfuncs] += "fn_functionA"

Do I need to copy/paste the whole body of the edited function in the bbappend file, or do an addtask? A detailed answer would be appreciated.

Also, n00b question: Why can't we edit the bb file and modify the function directly instead of overriding the function in a bbappend file?


Solution

  • You can append to functions the same way as you append to recipe's tasks.

    Here is an example:

    LICENSE = "CLOSED"
    
    func_example() {
        bbwarn "From func_example"
    }
    
    do_example() {
        bbwarn "From do_example"
    }
    
    do_example[postfuncs] += "func_example"
    addtask do_example
    

    Now when you run: bitbake example -c example you should see:

    ...
    WARNING: example-0.1-r0 do_example: From do_example
    WARNING: example-0.1-r0 do_example: From func_example
    ...
    

    Now, let's create a bbappend file:

    func_example:append() {
        bbwarn "From func_example_append"
    }
    

    Now, when you run bitbake example -c example again you should see:

    ...
    WARNING: example-0.1-r0 do_example: From do_example
    WARNING: example-0.1-r0 do_example: From func_example
    WARNING: example-0.1-r0 do_example: From func_example_append
    ...
    

    If you want to override the function, just redefine it without append or prepend, in my example it would be:

    func_example() {
        bbwarn "From func_example_append"
    }
    

    And the output would be:

    ...
    WARNING: example-0.1-r0 do_example: From do_example
    WARNING: example-0.1-r0 do_example: From func_example_append
    ...
    

    Why using bbappend ?

    It is not recommended to change/edit something directly in a third party Yocto layer (Example: meta-raspberrypi, meta-qt5, ...), and here is why:

    The most import point for me, is a respect for those who developed the layer and make it public for everyone. Keeping it clean as cloned is one of the best practices in Yocto development.