1.. _custom_cmake_toolchains: 2 3Custom CMake Toolchains 4####################### 5 6To use a custom toolchain defined in an external CMake file, :ref:`set these 7environment variables <env_vars>`: 8 9- Set :envvar:`ZEPHYR_TOOLCHAIN_VARIANT` to your toolchain's name 10- Set ``TOOLCHAIN_ROOT`` to the path to the directory containing your 11 toolchain's CMake configuration files. 12 13Zephyr will then include the toolchain cmake files located in the 14:file:`TOOLCHAIN_ROOT` directory: 15 16- :file:`cmake/toolchain/<toolchain name>/generic.cmake`: configures the 17 toolchain for "generic" use, which mostly means running the C preprocessor 18 on the generated 19 :ref:`devicetree` file. 20- :file:`cmake/toolchain/<toolchain name>/target.cmake`: configures the 21 toolchain for "target" use, i.e. building Zephyr and your application's 22 source code. 23 24Here <toolchain name> is the same as the name provided in 25:envvar:`ZEPHYR_TOOLCHAIN_VARIANT` 26See the zephyr files :zephyr_file:`cmake/modules/FindHostTools.cmake` and 27:zephyr_file:`cmake/modules/FindTargetTools.cmake` for more details on what your 28:file:`generic.cmake` and :file:`target.cmake` files should contain. 29 30You can also set ``ZEPHYR_TOOLCHAIN_VARIANT`` and ``TOOLCHAIN_ROOT`` as CMake 31variables when generating a build system for a Zephyr application, like so: 32 33.. code-block:: console 34 35 west build ... -- -DZEPHYR_TOOLCHAIN_VARIANT=... -DTOOLCHAIN_ROOT=... 36 37.. code-block:: console 38 39 cmake -DZEPHYR_TOOLCHAIN_VARIANT=... -DTOOLCHAIN_ROOT=... 40 41If you do this, ``-C <initial-cache>`` `cmake option`_ may useful. If you save 42your :makevar:`ZEPHYR_TOOLCHAIN_VARIANT`, :makevar:`TOOLCHAIN_ROOT`, and other 43settings in a file named :file:`my-toolchain.cmake`, you can then invoke cmake 44as ``cmake -C my-toolchain.cmake ...`` to save typing. 45 46Zephyr includes :file:`include/toolchain.h` which again includes a toolchain 47specific header based on the compiler identifier, such as ``__llvm__`` or 48``__GNUC__``. 49Some custom compilers identify themselves as the compiler on which they are 50based, for example ``llvm`` which then gets the :file:`toolchain/llvm.h` included. 51This included file may though not be right for the custom toolchain. In order 52to solve this, and thus to get the :file:`include/other.h` included instead, 53add the set(TOOLCHAIN_USE_CUSTOM 1) cmake line to the generic.cmake and/or 54target.cmake files located under 55:file:`<TOOLCHAIN_ROOT>/cmake/toolchain/<toolchain name>/`. 56 57When :makevar:`TOOLCHAIN_USE_CUSTOM` is set, the :file:`other.h` must be 58available out-of-tree and it must include the correct header for the custom 59toolchain. 60A good location for the :file:`other.h` header file, would be a 61directory under the directory specified in ``TOOLCHAIN_ROOT`` as 62:file:`include/toolchain`. 63To get the toolchain header included in zephyr's build, the 64:makevar:`USERINCLUDE` can be set to point to the include directory, as shown 65here: 66 67.. code-block:: console 68 69 west build -- -DZEPHYR_TOOLCHAIN_VARIANT=... -DTOOLCHAIN_ROOT=... -DUSERINCLUDE=... 70 71.. _cmake option: 72 https://cmake.org/cmake/help/latest/manual/cmake.1.html#options 73