luis.espinal
luis.espinal

Reputation: 10539

shell function expansion in bitbake recipe

So, I have a shell function in a bitbake recipe, that, just for the sake of an example, will look at the recipe name and see if it has a "native" substring in it.

# foo.bb

# function is within foo.bb but outside other tasks.
is_recipe_native() {
    local is_native="false"

    case "${PN}" in
        *native*)
            is_native="true"
            ;;
    esac

    echo "${is_native}"
}

# a shell task within foo.bb
do_some_task() {

    # here, shell expansion won't work, resulting in an empty string.
    local is_native="$(is_recipe_native)"
    bbnote "${is_native}" # logs an empty string
}

In this case, the value of is_native is an empty string (""). Shell expansion does not occur, or bitbake doesn't know what ${is_native} is.

However, if I do this, shell expansion occurs:

## foo.bb

## a shell task within foo.bb
do_some_task() {

    ## a shell function defined within the task.

    is_recipe_native() {
        local is_native="false"

        case "${PN}" in
            *native*)
                is_native="true"
                ;;
        esac

        echo "${is_native}"
    }

    # here, shell expansion works.
    local is_native="$(is_recipe_native)"
    bbnote "${is_native}" # logs "true" or "false" as needed.
}

I understand the discrepancy happens because of the way the run._do_some_task script is finally generated.

But there's some boilerplate occurs here and there in some recipes I'm working on that I want to consolidate into functions I can call (and interpolate within variables.)

But the amount of boilerplate doesn't warrant the trouble of using python, either.

So, the question is (and I can't seem to find an answer in the Yocto manual): can I define a shell function inside a recipe, but outside of tasks, that I can invoke, in a shell task, within a string via shell expansion?

Thanks.

Upvotes: 2

Views: 1479

Answers (1)

antznin
antznin

Reputation: 95

You need to remove quotes after is_native:

is_native=$(is_recipe_native)

The parser is limited and does not support a syntax with quotes.


Note: shell functions are executed with /bin/sh, so you should try avoiding bashisms such as local, etc.

See the documentation:

The scripts are executed by /bin/sh, which may not be a bash shell but might be something such as dash. You should not use Bash-specific script (bashisms).

Upvotes: 0

Related Questions