How to Create Yocto Projects

From RidgeRun Developer Wiki






Create a Basic Project

On this section, we will unify all the information provided about Yocto so far to create a simple project. In this project we will go through all the necessary steps to build out first Yocto Project starting with installing dependencies, learning where to get the Poky repository and setting up the environment. The second task will be to create a new meta-layer, with a simple recipe project that will compile a C program printing a message. Finally, we will use Qemu to test the system created.

If you have any questions feel free to contact us:



Setting Environment

  1. Install Dependencies
    sudo apt-get install gawk wget git-core diffstat unzip texinfo gcc-multilib build-essential chrpath socat cpio python python3 python3-pip python3-pexpect xz-utils debianutils iputils-ping
  2. Create the Yocto project directory
    mkdir yocto-first-project
    cd yocto-first-project
  3. Set a new variable named YOCTO_DIR pointing to the direction of your yocto repository.
    YOCTO_DIR=$(pwd)

    This will simplify the commands that will be run on the next steps.

  4. Get Poky source code
    git clone -b dunfell git://git.yoctoproject.org/poky.git poky
    cd poky
  5. Run Yocto enviroment script.
    source oe-init-build-env

    this will setup the yocto environment for development, generating a new directory called build/ at the root of your Poky repository cloned. This is where the build configurations and output files are stored.

Create a custom meta-layer

  1. Change to the $YOCTO_DIR directory
    cd $YOCTO_DIR
  2. Create your layer, in this case meta-rr. Yocto provides a command to create your own meta-layers
    bitbake-layers create-layer meta-rr

    This command will create a template named meta-rr with the structure containing the recipes, metadata and files necessary to include a new meta-layer in the system environment.

  3. Run the following command to include meta-rr into the project meta-layers.
    bitbake-layers add-layer meta-rr

    Now, this command will include into build/conf/bblayers.conf the route to the new meta-layer.

  4. Check meta-rr is active
    bitbake-layers show-layers

    The output generated must show the meta-layers just included as you could see below.

    $ bitbake-layers show-layers
    NOTE: Starting bitbake server...
    layer                 path                                                                    priority
    ========================================================================================================
    core                  /home/ofallas/work/RnD/Q32024/EES/poky/meta                             5
    yocto                 /home/ofallas/work/RnD/Q32024/EES/poky/meta-poky                        5
    yoctobsp              /home/ofallas/work/RnD/Q32024/EES/poky/meta-yocto-bsp                   5
    meta-rr               /home/ofallas/work/RnD/Q32024/EES/poky/meta-rr                          6

Create the Source Code

  1. Create the directory for the new recipe into the meta-layer template just created at YOCTO_DIR.
    cd $YOCTO_DIR
    mkdir -p meta-rr/recipes-example/rrrecipe/files
  2. With your favorite text editor create rrrecipe.c into the directory previously created and include the following content.
    #include <stdio.h>
    int main()
    {
    	printf("RidgeRun LLC. \n");
    	printf("Embedded Solutions. \n");
    	return 0;
    }

Create the recipe

  1. Run the following command to create the recipe file.
    touch $YOCTO_DIR/meta-rr/recipes-example/rrrecipe/rrrecipe_0.1.bb
  2. With your favorite text editor include the following content.
    DESCRIPTION = "Simple Print Application"
    LICENSE = "MIT"
    LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
    
    SRC_URI = "file://rrrecipe.c"
    S = "${WORKDIR}"
    
    do_compile() {
    	${CC} rrrecipe.c ${LDFLAGS} -o rrrecipe
    }	
    
    do_install() {
    	install -d ${D}${bindir}
    	install -m 0755 rrrecipe ${D}${bindir}
    }

    This recipe does the following:

    • Use metadata to describe the application.
    • Include the License for the project. In this case we use MIT License.
    • Specifies the source of the application’s code. The file://rrrecipe.c indicates that the source file rrrecipe.c is included in the layer and is located in the same directory as the recipe file.
    • Compile the C application with GCC using LDFLAGS to generate the rrrecipe output file.
    • Include the binary created into /usr/bin to be execute when rrrecipe is called.
  3. Compile the recipe
    cd $YOCTO_DIR
    bitbake rrrecipe

    This process could take a while. This will compile the recipe with just the set of instruction described on it.

  4. Once the recipe is compiled it is necessary to include it into the system's image.
    cd $YOCTO_DIR/build/conf

    With your favorite text editor open the file local.conf, and include the next line at the end of the file.

    IMAGE_INSTALL:append = " rrrecipe"

    The IMAGE_INSTALL variable specifies which packages should be included in the final image. If you don’t add your new recipe to IMAGE_INSTALL, your new application or package won’t be part of the generated image.

  5. Generate the image
    cd $YOCTO_DIR
    bitbake core-image-minimal

    This process could take a while. When we generate the image we are creating a complete system with all the configuration, including kernel, bootloader and packages.

The image generated will be stored at $YOCTO_DIR/build/tmp/deploy/images.

Test the image with Qemu

QEMU is an open source machine emulator and virtualizer, it is used to run virtual machines for testing images, including Yocto images. From the previous steps we created an image called core-image-minimal, using QEMU we can test and preview how this image would run in the target machine

Follow the next steps to test your environment.

  1. Run Qemu
    runqemu core-image-minimal nographic
  2. Once Qemu inits, you must visualize the following output
    Poky (Yocto Project Reference Distro) 5.0.2 qemux86-64 /dev/ttyS0
    
    qemux86-64 login:

    Use root as a login.

    root@qemux86-64:~#
  3. Finally, we test the executable created with the recipe
    root@qemux86-64:~# rrrecipe
    RidgeRun LLC. 
    Embedded Solutions.

Congratulations you just built and tested your first Yocto Project.