Setting up a Zephyr toolchain
One problem for embedded developer is to manage different toolchains on the development machine. For instance, one might work with different hardware architectures in different projects or handling compilers in the same project, where different project releases use different compilers. One way to handle this is to use containerized development in Docker.
A container is a light-weight version of a virtual machine which makes it possible to isolate one computing environment from another. On the host machine a so-called image is created. Inside the image a complete file system of the development environment is located with all the tools needed to compile and link the software. When using Docker, a simple text file (Docker-file) is used to configure such image. Normally the base for the image is a minimal version of Linux and the developer then add other tools and environment variables to the Docker-file to get the complete environment. When the Docker-file is done you tell the Docker tool to build an image from the specifications in the Docker-file. Finally, you can “start” the image which is then called a container which is a running version of the image. Now you have the possibility to execute commands inside the container or connect a terminal to it to work with the container interactively. It should be noted that a container does not persist the file system. This mean that every time you “stop” and “restart” a container it starts from the state that was described in the Docker-file. If you want to have persistence storage you can either connect a directory from the host system or create a Docker volume. As a last feature It is possible to distribute images between computers and developers with a cloud service called registry. You can host your own registry, or you can use a commercial one from for instance Docker-Hub, GitLab, Artifactory or GitHub. By doing so you can make sure that all developers and CI machines uses the same environment when building your project.
As described in my previous post the Zephyr project provides a SDK that includes a complete toolchain to build Zephyr projects. The good thing is that this SDK is also distributed as a Docker image. To use this image, you first need to install Docker using the installation instruction that can be found here.
After installation you can download the Zephyr SDK image with the following command.
docker pull zephyrprojectrtos/zephyr-build
Next step is to create a directory on your host in which you will download Zephyr source code. For now, assume it is placed in /home/user1/zephyr
Now you want to start the Zephyr SDK and mount the directory above into the container. You also want to start a terminal connection to the container.
docker run -it -v /home/user1/zephyr:/workdir zephyrprojectrtos/zephyr-build:latest
You will get a terminal prompt and it is now possible to install Zephyr into /workdir by issuing the west commands:
west init .
west update
This will download all source code for Zephyr including all sub-modules. It will take some time so take a cup of coffee while everything gets into place.
When everything is downloaded one can test to build a simple hello_world program by issuing the command:
cd zephyr
west build -b native_posix samples/hello_world
Now you can start the image built with the command:
./build/zephyr/zephyr.elf
In the upcoming post we will continue setting up this environment for a custom project to show how to configure west to fit real production projects.