Migrating legacy Makefile applications to CMake

Note

Makefile applications are no longer maintained. please migrate ASAP.

Introduction

Migrating from Makefile to CMake brings many advantages:

  • CMake build allows using modern builders (Ninja etc)

  • It is modular: it is possible to build subparts of project with different flags, or to easily build multiple applications from one top application.

  • It also comes with Device Tree: code can be made simpler by relying on dts board config.

  • And KConfig is provided, making options easier to find, with builtin value checks.

Main differences

Migration from Makefile to CMake first require creating a CMakeLists.txt. This can be copied to have the base structure from helloworld example. From there, It is first needed to list source code, and add it to the target. In the case of helloworld, this is done as follows:

set(TARGET_NAME "helloworld")
set(TARGET_SRCS helloworld.c)
project(${TARGET_NAME} C ASM)
add_executable(${TARGET_NAME} ${TARGET_SRCS})

Then, options that were done using environment variables in Makefile are superseded by KConfig. See Kconfig to write options, and Kconfig options to check existing options.

A set of pre made macro are provided for FS, SFU, and other sub-projects. These can be found in CMake macros and commands.

Finally, a point to be careful about is the filesystem base: In makefile, the base of execution is the source dir, whereas in CMake it is the build directory. It is therefore recommended to use explicit path to reference files and other helpers. To do so, CMake provide variables: CMAKE_SOURCE_DIR and CMAKE_CURRENT_SOURCE_DIR, which respectively point to the main source directory, and the current source directory for a given sub CMakeLists.txt. The same variables are provided for binary directory: CMAKE_BINARY_DIR.

An example of their use can be found in examples/basic/gap9/first_app, in particular:

set(FILE0 ${CMAKE_CURRENT_SOURCE_DIR}/files/${FILE0_NAME})
--flash-property=${CMAKE_BINARY_DIR}/ssbl/ssbl@mram:ssbl:binary