Custom CMake Toolchains

To use a custom toolchain defined in an external CMake file, set these environment variables:

  • Set ZEPHYR_TOOLCHAIN_VARIANT to your toolchain’s name

  • Set TOOLCHAIN_ROOT to the path to the directory containing your toolchain’s CMake configuration files.

Zephyr will then include the toolchain cmake files located in the TOOLCHAIN_ROOT directory:

  • cmake/toolchain/generic.cmake: configures the toolchain for “generic” use, which mostly means running the C preprocessor on devicetree input files.

  • cmake/toolchain/target.cmake: configures the toolchain for “target” use, i.e. building Zephyr and your application’s source code.

See the zephyr files cmake/generic_toolchain.cmake and cmake/target_toolchain.cmake for more details on what your generic.cmake and target.cmake files should contain.

You can also set ZEPHYR_TOOLCHAIN_VARIANT and TOOLCHAIN_ROOT as CMake variables when generating a build system for a Zephyr application, like so:

west build ... -- -DZEPHYR_TOOLCHAIN_VARIANT=... -DTOOLCHAIN_ROOT=...
cmake -DZEPHYR_TOOLCHAIN_VARIANT=... -DTOOLCHAIN_ROOT=...

If you do this, -C <initial-cache> cmake option may useful. If you save your ZEPHYR_TOOLCHAIN_VARIANT, TOOLCHAIN_ROOT, and other settings in a file named my-toolchain.cmake, you can then invoke cmake as cmake -C my-toolchain.cmake ... to save typing.