Getting 'bad substitution' error in Yocto shell code

Viewed 249

I have following shell code in my recipe

VARIABLE_dev = "dev value"
VARIABLE_prod = "prod value"

do_compile() {
    variant="dev"
    echo ${VARIABLE_${variant}}
}

As a result I alway get 'bad substitution' error

/home/dev/testproj/tmp/work/machine-poky-linux-gnueabi/test-app/1.0-raspberrypi-r0/temp/run.do_compile.253: 121: Bad substitution

If the 'variant' variable is defined as datastore variable (outside shell code scope) then it works fine. But in the code I pasted I always get error. Any idea what could be the solution here?

1 Answers

The conditional variable expansion syntax that you are using requires OVERRIDES variable, you can check details here.

But the example you provided is not correct and cannot be used.

The principle of OVERRIDES is just when you want to override a variable condionally based on some key.

But, I will suggest something more suitable for your case, which is Variable Flags.

Here is an example:

VARIABLE[dev] = "dev value"
VARIABLE[prod] = "prod value"

Now if you use a Shell based task you can use Python expansion:

do_compile(){
   bbwarn ${@d.getVarFlags("VARIABLE").get('dev')}
}

This should show a warning with the value "dev value"

You can then make it more conditional using another variable, for example:

PROJECT_TYPE = "dev"

do_compile(){
   bbwarn ${@d.getVarFlags("VARIABLE").get(d.getVar('PROJECT_TYPE'))}
}

You can use a python task directly for more flexible python syntax.

Advice: use logging.bbclass utilities to make the log:

Shell: bbinfo, bbwarn, bberror, ...

Python: bb.info, bb.warn, bb.error, ...

Update: Loop through the flags with shell for loop:

flags="${@''.join(' ' + flag for flag, _ in d.getVarFlags('PACKAGECONFIG').items())}"
for flag in $flags; do
   bbwarn "$flag"
done
Related