Microprocessors
Programs

Hacking Raspberry Pi 4 with Yocto: OpenEmbedded Core, Meta-OpenEmbedded, Python, and More

10.0 Using the openembedded-core and meta-openembedded to Install Common Packages

When we built our core image using the tools provided by Yocto and poky, we were using the foundation of OpenEmbedded-Core (OE-Core). According to this Yocto Project page, OE-Core contains a base layer of recipes, classes, and associated files that are meant to be common among different OpenEmbedded-derived systems, including the Yocto Project. The recipes in OE-Core are the core recipes that one needs to build an small image on an embedded device.

But what about applications and tools that aren't considered to be part of the core? Well, there is another layer called "meta-openembedded" that contains a lot of non-essential, but very commonly used software that was originally included in the OpenEmbedded project. From what I've gathered, the software was pulled out into a separate layer to make things more manageable (at least that's how I understand it...I can't claim to be an expert here).

In the following subsections, we'll install a common application like Python3 from OE-Core, and then follow that up by installing a perhaps less commonly used Python package from meta-openembedded.

10.1 Installing Python3 from OpenEmbedded-Core

One of the most commonly used programming applications in use today is Python. If you followed the steps outlined in the previous nine sections and played around with the image we built on the RPi, you may have noticed that Python is not installed. Don't worry, it's very easy to include it in our build. We already have it in the OE-Core distribution, which is buried within our Poky directory.

How do we know if the recipe for Python3 is already present in the layers we have? We can use bitbake -s to list all the packages currently available in our recipes like so:

$ cd ~/Yocto/poky
$ source oe-init-build-env
$ bitbake -s rpilinux-image | grep python3

When you run the latter command, you should see a fair amount of python3 recipes, including the base "python3" recipe we need. It was already included in the layers of poky that we cloned earlier. In general, if you're looking for a package with the name [package-name] in the image with [image-name], you use bitbake -s [image-name] | grep [package-name].

Since we already have the recipe in our layers, installing python3 on our root filesystem is as simple as adding it to the IMAGE_INSTALL directive in our rpilinux-image.bb recipe (see Section 6 to find where that is).

After adding it, our rpilinux-image.bb should look something like this (you don't need the separate lines; they're just for organizational purposes):

require recipes-core/images/core-image-minimal.bb 
    
IMAGE_INSTALL += "libstdc++ mtd-utils" 
IMAGE_INSTALL += "openssh openssl openssh-sftp-server" 
IMAGE_INSTALL += "python3"

After adding this, we just need to do another bitbake rpilinux-image and we should now have python3 in our root filesystem, ready to go! If you decompress the new rootfs onto the SD card and bootup, the python3 command should now bring up a Python terminal.

10.2 Installing numpy from OpenEmbedded-Core

OE-Core comes with a few other goodies. If you use python for scientific, mathematical, etc. purposes, you'll almost certainly want numpy on the board. To include numpy on the build, just add it to the IMAGE_INSTALL directive in our rpilinux-image.bb. It's already in OE-Core provided by Poky.

require recipes-core/images/core-image-minimal.bb 
    
IMAGE_INSTALL += "libstdc++ mtd-utils" 
IMAGE_INSTALL += "openssh openssl openssh-sftp-server" 
IMAGE_INSTALL += "python3 python3-numpy"

Another bitbake rpilinux-image, and it should be in our compressed rootfs.

10.3 Installing Less Common Pacakges from meta-openembedded

Some very commonly used Python packages are not included with poky. Take paho-mqtt, for instance. You won't find it in OE-Core. That's reasonable given that it's not a core package. But is there a way to install it easily?

Luckily, meta-openembedded comes to the rescue. The layers within the meta-openembedded package contain a whole slew of good recipes that extend the functionality of your board.

Let's clone meta-openembedded in our Yocto directory.

$ cd ~/Yocto 
$ git clone -b zeus https://github.com/openembedded/meta-openembedded

After we clone this repo, our project should look like this:


Now let's say we want to install the paho-mqtt package for python3 so that it's available in our root filesystem right out of the box. We can search for the package by cd'ing into the meta-openembedded directory and using find ./ -name "*paho-mqtt*" -print. If we do that, we'll see five instances that match, including this one: ./meta-python/recipes-devtools/python/python3-paho-mqtt_1.4.0.bb. This indicates we have a recipe for paho-mqtt!

To take advantage of this, we first have to add the layer that contains the recipe. If you dig around in the meta-openembedded directory, you'll see that the layers are broken up into subdirectories. We need to add the meta-python layer to our bblayers.conf file in ~/Yocto/poky/build. After we do so, it should look like this:

# POKY_BBLAYERS_CONF_VERSION is increased each time build/conf/bblayers.conf 
# changes incompatibly 
POKY_BBLAYERS_CONF_VERSION = "2" 

BBPATH = "${TOPDIR}" 
BBFILES ?= "" 

BBLAYERS ?= " \ 
  /home/lance/Yocto/poky/meta \ 
  /home/lance/Yocto/poky/meta-poky \ 
  /home/lance/Yocto/poky/meta-yocto-bsp \ 
  /home/lance/Yocto/meta-raspberrypi \ 
  /home/lance/Yocto/meta-rpilinux \
  /home/lance/Yocto/meta-openembedded/meta-oe \
  /home/lance/Yocto/meta-openembedded/meta-python \
  " 

You may notice that we also added the "meta-oe" layer in meta-openembedded. We have to add this layer or bitbake will complain because it's at the core of meta-openembedded.

If we change the last line of our IMAGE_INSTALL directive in our rpilinux-image.bb to IMAGE_INSTALL += "python3 python3-numpy python3-paho-mqtt" and do another bitbake of rpilinux-image, we should now have paho-mqtt available for import in python3.


In a few easy steps, we were able to install python3, numpy, and paho-mqtt in our build. That sure beats having to run "pip3 install numpy paho-mqtt" on every board that we're loading the image on to, doesn't it?



← Previous    ...    Next →