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.
Workspace: the top directory. It contains a hidden directory
.west, whose fileconfigrecords where the manifest is. West finds the workspace by looking for.westin the current directory and then in each parent directory in turn.Manifest file: by default
west.yml, written in YAML (YAML Ain’t Markup Language), a text format for nested lists and named values. Its listprojectsgives each repository aname, a place to fetch it from, arevisionand apathinside the workspace. A revision is one of three things: a commit hash, the identifier that Git computes for one commit (one recorded state of the repository); a tag, a name that a maintainer attaches to one commit; or a branch, a name that moves forward with each new commit. A project markedimportalso contributes the projects listed in its own manifest file.Manifest repository: the repository that holds the manifest file. The manifest’s section
selfdescribes it.Projects: the repositories that the manifest lists.
Extension command file: a YAML file, by convention
west-commands.yml, that a project names under the keywest-commands. For each command it gives the command’s name and the Python file and Python class that implement it.
The commands work in this order.
west initcreates the workspace. It makes.westand clones the manifest repository, or, with the option-l, uses a manifest repository that is already on disk.west updatereads 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 namedmanifest-revat 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.When the user types
westand a command name, west first looks for a built-in command of that name, such asinit,update,list,manifest,status,difforconfig. West ignores an extension command whose name equals a built-in command’s name.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.
The Zephyr repository’s file
scripts/west-commands.ymldeclares Zephyr’s commands, and their code lies inscripts/west_commands.west buildruns CMake to generate a build system in a build directory, by defaultbuild, and passes the board’s name in the CMake variableBOARD. It then runs the build tool, Ninja §12.13 by default. Arguments after--go to CMake.west flashwrites the program into the device’s flash memory §6.6. It first rebuilds, unless the settingflash.rebuildis 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--runnerpicks 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.
It finds
ws/.west, so the workspace isws.The file
ws/.west/confignames the manifestapp/west.yml.buildis not a built-in command.The manifest imported from
zephyr/west.ymlgives the projectzephyrthe extension command filescripts/west-commands.yml, and that file mapsbuildto the classBuildinscripts/west_commands/build.py.West imports that file from
ws/zephyrand runsBuild, which runs CMake for the boardnrf52840dk/nrf52840and then Ninja, in the new directoryws/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.
Update. Let be a project of the resolved manifest, meaning the manifest after all imports, and let be its revision.
west updatechecks out, as a detached HEAD, the commit that names when the fetch happens, and pointsmanifest-revat . If is a commit hash, is that commit whenever the update runs. If is a branch, is whatever commit the branch names at that moment.Dispatch. For a typed command name : if 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 , 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:
the installed version of west, which supplies the built-in commands;
the workspace that the search from the current directory finds;
the resolved manifest;
the files currently checked out in the project that supplies the command;
the arguments, the settings in the three configuration files, and environment variables;
the programs that the command’s code starts, such as CMake, Ninja, the compiler and the programming tool, each found through the search path for programs.
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:
west topdirprints the workspace, andwest manifest --resolveprints its manifest with all imports included.west --helplists 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.The code that runs is the project’s current checkout, not necessarily the revision the manifest names;
west statusandwest diffshow whether a project has left that revision or has edited files.The command then starts other programs:
west -v buildprints the CMake and compiler commands, andwest flashuses the board’s default runner unless--runnernames another.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].