Sitemap
Jarsop

Jarsop

Husband, father and engineer. Love simple and complex things. Polyglot developer but preferring Rust (obviously)

Dynamic Bitbake functions

2 min readFeb 28, 2020
Press enter or click to view image in full size

During my Yocto experience, I had to work by supporting several machines in the same distribution. The difficulty was that new machines were added over time and several recipes had to be maintained with the risk of forgetting.

In this sense, I had the idea of creating dynamic helpers for increasing maintainability. Hoping that the idea can be useful to you here is the basis of the principle. The following example uses the raspberry pi machine, which in view of its reputation will speak, I think, to everyone

rpi_helpers.bblass

MY_MACHINES = “raspberrypi raspberrypi2 raspberrypi3 raspberrypi4”
MY_MACHINES[aliases] = “RPI1 RPI2 RPI3 RPI4”
python () {
funcs = {}
machines = d.getVar(‘MY_MACHINES’).split()
aliases = d.getVarFlag(‘MY_MACHINES’, ‘aliases’).split()
for machine, aliase in zip(machines, aliases):
func = ‘’’
[[ “$%s” = “%s” ]] && return 0
return 1
‘’’ % (“{MACHINE}”, machine)
funcname = ‘is%s’ % aliase
funcs[funcname] = func
if_statement = ‘’.join([‘ is%s || ‘ % al for al in aliases])
if_statement = if_statement[:-4]
funcs[‘isRPI’] = ‘’’
[[ %s ]] && return 0
return 1
‘’’ % if_statement
funcs[‘isQEMU’] = ‘’’
[[ “$%s” = “qemux86–64” ]] && return 0
return 1
‘’’ % “{MACHINE}”
for funcname, func in funcs.items():
d.setVar(funcname, func)
d.setVarFlag(funcname, ‘func’, ‘1’)
}

This code produce these snippets in recipe environment:

isRPI1() {
[[ "${MACHINE}" = "raspberrypi" ]] && return 0
return 1
}
isRPI2() {
[[ "${MACHINE}" = "raspberrypi2" ]] && return 0
return 1
}
isRPI3() {
[[ "${MACHINE}" = "raspberrypi3" ]] && return 0
return 1
}
isRPI4() {
[[ "${MACHINE}" = "raspberrypi4" ]] && return 0
return 1
}
isRPI() {
[[ isRPI1 || isRPI2 || isRPI3 || isRPI4 ]] && return 0
return 1
}
isQEMU() {
[[ "${MACHINE}" = "qemux86–64" ]] && return 0
return 1
}

With these snippets now we can just inherit rpi_helpers class in our recipe and this allow to use statement like this:

Get Jarsop’s stories in your inbox

Join Medium for free to get updates from this writer.

base-files_%.bbappend

inherit rpi_helpersdo_install_append() {

isRPI4 && {
some stuff
}
isRPI && {
other stuff
}
isQEMU || {
more stuff
}
}

You may find some advantages in terms of maintainability and readability. For example imagine that we have to add a new RPI machine, you just have to add it in the variable MY_MACHINES as well as its alias in the associated varflag. In this way isRPI will be automatically updated.

Of course you can generate functions for any use, this example is just there to illustrate the general idea of ​​automatic generation of functions in Bitbake.

Hoping this sketch will help someone, have a good hack :)

--

--

Jarsop
Jarsop

Written by Jarsop

Husband, father and engineer. Love simple and complex things. Polyglot developer but preferring Rust (obviously)