Define CMake Rules

View source on GitHub

The CMake build system relies on CMakeLists.txt files which define build targets. The CMakeLists.txt at the root of your repository is the top of the build tree and is a good place to start defining the various targets, options, and macros used in the build process.

The very first values to define are the project name and the supported platforms.

In this example, we set the project name to ot-efr32 with a version of 0.0.1. We also define a variable EFR32_PLATFORM_VALUES which is a list of efr32 platforms supported by ot-efr32. For the sake of this example, we've defined multiple platforms, but having a single platform for the _PLATFORM_VALUES variable is fine as well.

cmake_minimum_required(VERSION 3.10.2)
project(ot-efr32 VERSION 0.0.1)


The CMakeLists.txt file includes a check that aborts builds launched for unsupported platforms.

    message(FATAL_ERROR "Please select a supported platform: ${EFR32_PLATFORM_VALUES}")

The next variable which needs to be defined is OT_PLATFORM_LIB. This variable is used by the OpenThread example applications to link against your platform.

set(OT_PLATFORM_LIB "openthread-${EFR32_PLATFORM}")

OpenThread CMake options

Various features in OpenThread may be enabled/disabled/configured by defining CMake variables.

On the ot-efr32 platform, an external mbedTLS library silabs-mbedtls is used.

set(OT_BUILTIN_MBEDTLS_MANAGEMENT OFF CACHE BOOL "disable builtin mbedtls management" FORCE)
set(OT_EXTERNAL_MBEDTLS "silabs-mbedtls" CACHE STRING "use silabs mbedtls" FORCE)

Define output directories

The output directory for different target files can be configured using the variables below.


Add OpenThread to the build tree

To include the openthread submodule in the build tree:


Pass build properties to OpenThread

The final section of this file allows you to define build properties (such as definitions, options, and include directories) which you may want to add to the openthread build tree and to your platform libraries.

A convenient way to add these definitions is by using the ot-config target. This target is a phony target which is used solely for the purpose of defining configuration and is linked against by almost all CMake targets in openthread.

# Define config filename macros
target_compile_definitions(ot-config INTERFACE

# Disable -Wshadow and -Wpedantic
target_compile_options(ot-config INTERFACE

# Add platform dirs to "include" dirs
target_include_directories(ot-config INTERFACE

Add subdirectories to the build tree

Now that the top-level configuration is defined, it's time to add other subdirectories to the build tree.

The src folder is where the platform-abstraction layer source code is located. The third_party folder contains any third-party code.

If you would like to include sample applications in the repo, create an examples folder and add it to the build tree as well.


# Optional

The src directory

This is the heart of the repository and is where the platform abstraction layer is implemented. It also contains a few other required files.


This is a toolchain file which defines some variables that CMake uses in the build process. A good starting point would be to copy the ot-efr32 version of this file and modify it to fit your platform.


This is where you define the source files, include paths, and compiler flags for your platform library. A good example of this file for a repository which supports only a single platform is from ot-cc2538.

Support for multiple platforms in a single repository is possible and several platform repositories can be used as reference. See ot-efr32 and ot-nrf528xx.