Section 12.15

West

Definition

West is the command-line tool of the Zephyr §6.33 operating system project for managing a workspace: a directory holding several Git repositories, each checked out at a chosen revision. Git is a version-control system; a revision is one recorded state. A file called the manifest §13.4 lists the repositories and their revisions. Built-in commands fetch and inspect the repositories; extension commands, which the repositories supply as Python §12.23 code, add actions such as building a program and writing it into a device. The name is a plain word, not an abbreviation.

The idea

A Zephyr application is built from many repositories at once: the Zephyr operating system itself, hardware support code from chip makers, cryptography and file-system libraries, a bootloader §6.30 (the first program that runs when the device starts), and the application’s own repository. Their versions must fit together. A note saying “use Zephyr 3.7” leaves each developer to fetch dozens of repositories by hand and to guess the version of each one. West replaces the note with a file kept under version control, the manifest, which names every repository and the revision to use. One command, west update, makes every repository in the workspace match the manifest, and a change of versions becomes an ordinary recorded change to the manifest.

The second job is to put one command line in front of many tools. Building needs a long command for CMake §12.12 (cross-platform make), the program that generates a project’s build instructions, and writing a program into a board needs a different programming tool for each kind of board. West does not contain these commands. A repository in the workspace supplies each of them as Python code; the Zephyr repository supplies build, flash and debug.

A parts list with exact part numbers is a fair picture of the manifest, and west is the clerk who fetches every listed part. The picture stops at one point: west also runs tools that the fetched parts supply, so the same typed command can do different things in two workspaces.

How it works

West uses these names.

The commands work in this order.

  1. west init creates the workspace. It makes .west and clones the manifest repository, or, with the option -l, uses a manifest repository that is already on disk.

  2. west update reads the manifest, follows its imports, fetches each project and checks out the commit that the project’s revision names. In each project it points a branch named manifest-rev at that commit and checks the commit out as a detached HEAD: a checkout that is on no branch. HEAD is Git’s name for whatever is currently checked out.

  3. When the user types west and a command name, west first looks for a built-in command of that name, such as init, update, list, manifest, status, diff or config. West ignores an extension command whose name equals a built-in command’s name.

  4. Otherwise west reads the extension command files that the manifest names, finds the Python file and class given for the name, imports that file from the project’s directory as it is on disk at that moment, and runs the class. West imports an extension’s code only when that command is run.

  5. The Zephyr repository’s file scripts/west-commands.yml declares Zephyr’s commands, and their code lies in scripts/west_commands. west build runs CMake to generate a build system in a build directory, by default build, and passes the board’s name in the CMake variable BOARD. It then runs the build tool, Ninja §12.13 by default. Arguments after -- go to CMake. west flash writes the program into the device’s flash memory §6.6. It first rebuilds, unless the setting flash.rebuild is false, and then hands the built program to a runner: a Python class in the Zephyr repository that drives one programming tool, such as the J-Link §6.27 software or OpenOCD §6.29 (Open On-Chip Debugger). The board’s build files name a default runner, and the option --runner picks another.

West reads settings from three files: a system-wide file, the file .westconfig in the user’s home directory, and the workspace’s .west/config. A setting in a later file overrides the same setting in an earlier one. The command west config reads and changes them.

Worked example

A developer keeps an application in a Git repository named app. Its manifest file app/west.yml reads:

manifest:
remotes:
- name: upstream
url-base: https://github.com/zephyrproject-rtos
projects:
- name: zephyr
remote: upstream
revision: v3.7.0
import: true
self:
path: app

The developer clones app into an empty directory ws and, inside ws, runs west init -l app and then west update. West builds the address of the project zephyr from the remote’s url-base, a slash and the project’s name: https://github.com/zephyrproject-rtos/zephyr. It fetches that repository into ws/zephyr, because a project’s path defaults to its name, and checks out the commit that the tag v3.7.0 names. Because of import: true, west then reads zephyr/west.yml at that commit and adds every project it lists, such as hal_nordic (hardware support code for one chip maker’s devices), mbedtls (a cryptography library) and mcuboot (a bootloader), each at the revision given there.

Next, inside ws, the developer runs:

west build -b nrf52840dk/nrf52840 app

where nrf52840dk/nrf52840 names a development board §6.5. West decides what this means in five steps.

  1. It finds ws/.west, so the workspace is ws.

  2. The file ws/.west/config names the manifest app/west.yml.

  3. build is not a built-in command.

  4. The manifest imported from zephyr/west.yml gives the project zephyr the extension command file scripts/west-commands.yml, and that file maps build to the class Build in scripts/west_commands/build.py.

  5. West imports that file from ws/zephyr and runs Build, which runs CMake for the board nrf52840dk/nrf52840 and then Ninja, in the new directory ws/build.

The command west -v build prints the CMake and compiler commands as they run.

Now suppose that after the update a colleague ran git checkout main inside ws/zephyr to try a fix. The manifest still says v3.7.0. The next west build nonetheless runs the Build class and the Zephyr build files of the commit that the branch main named, because west loads the files that are on disk. west status, which runs git status in every project, shows that zephyr is on the branch main, and git -C zephyr rev-parse HEAD prints the commit actually checked out. The typed command is identical; the action is not.

Stated precisely

West’s contract has two parts.

  1. Update. Let pp be a project of the resolved manifest, meaning the manifest after all imports, and let rpr_p be its revision. west update checks out, as a detached HEAD, the commit cpc_p that rpr_p names when the fetch happens, and points manifest-rev at cpc_p. If rpr_p is a commit hash, cpc_p is that commit whenever the update runs. If rpr_p is a branch, cpc_p is whatever commit the branch names at that moment.

  2. Dispatch. For a typed command name nn: if nn is a built-in command of the installed west, that command runs. Otherwise west runs the class that the resolved manifest’s extension command files give for nn, loaded from the file now on disk in the directory of the project that supplies it.

The action of a west command is therefore fixed by six inputs:

West makes no promise about the outcome of those programs. It reports their exit status §13.24, the number a program returns when it ends, where 0 means success.

Limits and common mistakes

West is not a build system: west build delegates to CMake, Ninja and the Zephyr build files.

A manifest that names branches does not identify a build: two runs of west update a week apart can check out different commits. A tag can be moved by whoever controls the repository. Only a commit hash names exact content. west manifest --resolve prints the manifest with all imports included, and west manifest --freeze prints it with every revision written as a commit hash. A reproducible build §13.8, one that anyone can repeat bit for bit from the same inputs, needs commit hashes.

West does not stop anyone from changing a checkout after west update, as the worked example shows. west status and west diff reveal changed checkouts and edited files.

Running an extension command runs Python code from a repository with the user’s own permissions; the west documentation describes importing a Python module as equivalent to running shell commands. West imports an extension’s code only when that command is run. A west update with a manifest from an untrusted source therefore runs none of that code, but the next west build can.

The environment variable ZEPHYR_BASE names a Zephyr source directory. By default, with the west setting zephyr.base-prefer equal to env, a value already set in the environment overrides the workspace’s own zephyr.base setting. A value left over from another workspace can therefore point a command at another copy of Zephyr.

Success from west flash means that the programming tool reported success. Whether the device now runs the intended program is a separate question, answered by a separate check, such as reading the device’s memory back and comparing it with the built file.

Questions answered

Which manifest and extension command define the action actually performed?

The action is defined by the resolved manifest of the workspace that west finds from the current directory and, for a command that is not built in, by the Python class to which that manifest’s extension command files map the command’s name, as the class’s file is checked out on disk at that moment. A workspace is the directory that holds the repositories; the manifest is its file, by default west.yml, that lists each repository and its revision; an extension command is a command that a repository in the workspace supplies as Python code. To identify both:

  1. west topdir prints the workspace, and west manifest --resolve prints its manifest with all imports included.

  2. west --help lists every extension command with the project that supplies it; that project’s extension command file names the Python file and class. A built-in command always wins over an extension of the same name.

  3. The code that runs is the project’s current checkout, not necessarily the revision the manifest names; west status and west diff show whether a project has left that revision or has edited files.

  4. The command then starts other programs: west -v build prints the CMake and compiler commands, and west flash uses the board’s default runner unless --runner names another.

  5. Settings and environment variables, such as ZEPHYR_BASE, can redirect the command.

Record all of these with any result; the typed words west build alone do not identify the action.

Related entries: Cross-compilation (§12.1), Toolchain (§12.2), Zephyr SDK (§12.16), CMake (§12.12), Ninja (§12.13), Manifest (§13.4), Git commit and working tree (§13.19), Zephyr RTOS (§6.33). Further reading: [39].