Cmake

From Wiki at Neela Nurseries
Revision as of 20:21, 18 March 2024 by Ted (talk | contribs) (Reorganize some section levels.)
Jump to: navigation, search

Zephyr RTOS building blocks   ::   Device Tree Source   ::   Kconfig   ::   cmake   ::   `west` manifest files

Keywords and noteworthy: Zephyr app common code


- Overview -

This page a starting point for cmake notes and links to references about cmake.


^ Configuration Stage and Build Stage of Cmake


^ Practical Things to Know and Do

stub section intro

^ To Debug Cmake Scripts

A gitlab dot io cmake resource:

Note that the following post has a cool answer which involves a cmake macro to print all variables known to a given cmake invocation or use, and also a more simple answer to print one specific variable:


A modified generic.cmake file, changes address one or more unset variables and solved build error on newest Zephyr work station:

# SPDX-License-Identifier: Apache-2.0

set_ifndef(CC gcc)

## 2022-03-03 - Ted moving this cmake function call beyond locally
##  added variable assignements:
##
## find_program(CMAKE_C_COMPILER ${CROSS_COMPILE}${CC}   PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH)

#
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
## 2022-02-08 Reference https://cmake.org/cmake/help/latest/command/message.html
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
message(NOTICE "*** zztop 1 *** building with user configuration var ZEPHYR_TOOLCHAIN_VARIANT set to '${ZEPHYR_TOOLCHAIN_VARIANT}'")
message(NOTICE "*** zztop 2 *** internal variable CROSS_COMPILE set to '${CROSS_COMPILE}'")
message(NOTICE "*** zztop 3 *** internal variable TOOLCHAIN_HOME set to '${TOOLCHAIN_HOME}'")

## 2022-03-03 THU - Not ***ing sure why we need to set these today:
set(ZEPHYR_TOOLCHAIN_VARIANT gnuarmemb)
set(GNUARMEMB_TOOLCHAIN_PATH /opt/gcc-arm-none-eabi-10.3-2021.10)
set(TOOLCHAIN_HOME /opt/gcc-arm-none-eabi-10.3-2021.10)
## ^^^ These three cmake variable settings not sufficient, trying another based on working but different
##  toolchain on host ind:
set(CROSS_COMPILE /opt/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-)
##
## $ ls /opt/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-*
##

message(NOTICE "*** zztop 4 *** building with user configuration var ZEPHYR_TOOLCHAIN_VARIANT set to '${ZEPHYR_TOOLCHAIN_VARIANT}'")
message(NOTICE "*** zztop 5 *** internal variable CROSS_COMPILE set to '${CROSS_COMPILE}'")
message(NOTICE "*** zztop 6 *** internal variable TOOLCHAIN_HOME set to '${TOOLCHAIN_HOME}'")


find_program(CMAKE_C_COMPILER ${CROSS_COMPILE}${CC}   PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH)

if(CMAKE_C_COMPILER STREQUAL CMAKE_C_COMPILER-NOTFOUND)
  message(FATAL_ERROR "Zephyr was unable to find the toolchain. Is the environment misconfigured?
User-configuration:
ZEPHYR_TOOLCHAIN_VARIANT: ${ZEPHYR_TOOLCHAIN_VARIANT}
Internal variables:
CROSS_COMPILE: ${CROSS_COMPILE}
TOOLCHAIN_HOME: ${TOOLCHAIN_HOME}
")
endif()

execute_process(
  COMMAND ${CMAKE_C_COMPILER} --version
  RESULT_VARIABLE ret
  OUTPUT_QUIET
  ERROR_QUIET
  )
if(ret)
  message(FATAL_ERROR "Executing the below command failed. Are permissions set correctly?
'${CMAKE_C_COMPILER} --version'
"
    )
endif()

^ Source and Library Path Setting

Cmake has a notion of a binary or binaries directory, and has a built-in variable CMAKE_BINARY_DIR as described here:


^ cmake Lists

Look up `append_if_path_exists()` . . .


^ cmake Functions

An interesting pattern search in Zephyr 3.2.0 source tree and associated third party libraries is `$ grep -nr ExternalProject_Add ./*`, which reveals which projects and locations in Zephyr's source tree this cmake function is called. This function is needed when building multicore Zephyr apps.

There is a detailed document on `ExternalProject_Add()` at the Kitware public github repository:

    ^ ExternalProject_Add

    Cmake function `ExternalProject_Add()` is used in multi-core Zephyr sample projects. Some refs to this function include:

    ^ find_package


    ^ add_subdirectory_ifdef()

    Really good blog post at blog.golioth.io regarding how to configure a "super project" containing multiple Zephyr based apps with shared, select-able at build time common code:

    ^ function()

    - stub -

    ^ execute_process()

    - stub -

    ^ add_custom_command()

    - stub -

    ^ Install latest version of cmake


    ^ cmake Package Configuration Files - What Are They?

    QUESTION (2022-12-29): what are cmake package configuration files?

    When attempting to switch Zephyr sdk of original latest 2021 kionix-driver-demo, from Nordic ncs SDK to Zephyr RTOS Project itself, and a more recent release of Zephyr RTOS, get following error early early in build process:

    :~/projects-sandbox/workspace-out-of-tree/kionix-driver-demo$ west build -b lpcxpresso55s69_cpu0 -p
    -- west build: making build dir /home/ted/projects-sandbox/workspace-out-of-tree/kionix-driver-demo/build pristine
    -- west build: generating a build system
    -- Build type: debug
    Loading Zephyr default modules (Zephyr base).
    -- Application: /home/ted/projects-sandbox/workspace-out-of-tree/kionix-driver-demo
    -- Found Python3: /usr/bin/python3.8 (found suitable exact version "3.8.10") found components: Interpreter 
    -- Cache files will be written to: /home/ted/.cache/zephyr
    -- Zephyr version: 3.1.0 (/home/ted/projects-sandbox/workspace-out-of-tree/zephyr)
    -- Found west (found suitable version "0.12.0", minimum required is "0.7.1")
    -- Board: lpcxpresso55s69_cpu0
    CMake Error at /home/ted/projects-sandbox/workspace-out-of-tree/zephyr/cmake/modules/verify-toolchain.cmake:79 (find_package):
      Could not find a package configuration file provided by "Zephyr-sdk"
      (requested version 0.13.1) with any of the following names:
    
        Zephyr-sdkConfig.cmake
        zephyr-sdk-config.cmake
    
      Add the installation prefix of "Zephyr-sdk" to CMAKE_PREFIX_PATH or set
      "Zephyr-sdk_DIR" to a directory containing one of the above files.  If
      "Zephyr-sdk" provides a separate development package or SDK, be sure it has
      been installed.
    Call Stack (most recent call first):
      /home/ted/projects-sandbox/workspace-out-of-tree/zephyr/cmake/modules/zephyr_default.cmake:121 (include)
      /home/ted/projects-sandbox/workspace-out-of-tree/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:51 (include)
      /home/ted/projects-sandbox/workspace-out-of-tree/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:76 (include_boilerplate)
      CMakeLists.txt:51 (find_package)
    
    
    -- Configuring incomplete, errors occurred!
    FATAL ERROR: command exited with status 1: /usr/bin/cmake -DWEST_PYTHON=/usr/bin/python3 -B/home/ted/projects-sandbox/workspace-out-of-tree/kionix-driver-demo/build -S/home/ted/projects-sandbox/workspace-out-of-tree/kionix-driver-demo -GNinja -DBOARD=lpcxpresso55s69_cpu0
    

    So at time of above error we didn't know how to trace that problem, though there is a call stack reported by cmake which tells us some cmake files we may review for better understanding. There are notes on cmake's documentation pages as of 2022-12-27 which mention the naming conventions of files which cmake's find_package() will look for when it is invoked.

    NEED to add link to cmake's find_package() documentation.

    2022-12-28 - some new cmake build errors:

    (1)

    ted@localhost:~/projects/zephyr-based/out-of-tree/kionix-driver-demo$ west build -b lpcxpresso55s69_cpu0 -p
    -- west build: making build dir /home/ted/projects/zephyr-based/out-of-tree/kionix-driver-demo/build pristine
    -- west build: generating a build system
    Loading Zephyr default modules (Zephyr base).
    -- Application: /home/ted/projects/zephyr-based/out-of-tree/kionix-driver-demo
    -- Found Python3: /usr/bin/python3.8 (found suitable exact version "3.8.10") found components: Interpreter 
    -- Cache files will be written to: /home/ted/.cache/zephyr
    -- Zephyr version: 3.2.0 (/home/ted/projects/zephyr-based/out-of-tree/zephyr)
    -- Found west (found suitable version "0.11.0", minimum required is "0.7.1")
    -- Board: lpcxpresso55s69_cpu0
    -- ZEPHYR_TOOLCHAIN_VARIANT not set, trying to locate Zephyr SDK
    CMake Error at /home/ted/projects/zephyr-based/out-of-tree/zephyr/cmake/modules/FindZephyr-sdk.cmake:63 (find_package):
      Could not find a configuration file for package "Zephyr-sdk" that is
      compatible with requested version "0.15".
    
      The following configuration files were considered but not accepted:
    
        /opt/zephyr-sdk-0.12.4/cmake/Zephyr-sdkConfig.cmake, version: 0.12.4
        /opt/zephyr-sdk-0.14.2/cmake/Zephyr-sdkConfig.cmake, version: 0.14.2
        /opt/zephyr-sdk-0.13.0/cmake/Zephyr-sdkConfig.cmake, version: 0.13.0
    
    Call Stack (most recent call first):
      /home/ted/projects/zephyr-based/out-of-tree/zephyr/cmake/modules/FindHostTools.cmake:53 (find_package)
      /home/ted/projects/zephyr-based/out-of-tree/zephyr/cmake/modules/dts.cmake:8 (find_package)
      /home/ted/projects/zephyr-based/out-of-tree/zephyr/cmake/modules/zephyr_default.cmake:108 (include)
      /home/ted/projects/zephyr-based/out-of-tree/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:66 (include)
      /home/ted/projects/zephyr-based/out-of-tree/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:92 (include_boilerplate)
      CMakeLists.txt:15 (find_package)
    
    
    -- Configuring incomplete, errors occurred!
    FATAL ERROR: command exited with status 1: /usr/bin/cmake -DWEST_PYTHON=/usr/bin/python3 -B/home/ted/projects/zephyr-based/out-of-tree/kionix-driver-demo/build -GNinja -DBOARD=lpcxpresso55s69_cpu0 -S/home/ted/projects/zephyr-based/out-of-tree/kionix-driver-demo
    


    After setting `ZEPHYR_TOOLCHAIN_VARIANT` in the current shell, get these cmake errors:

    (2)

    ted@localhost:~/projects/zephyr-based/out-of-tree/kionix-driver-demo$ export ZEPHYR_TOOLCHAIN_VARIANT=gnuarmemb
    ted@localhost:~/projects/zephyr-based/out-of-tree/kionix-driver-demo$ west build -b lpcxpresso55s69_cpu0 -p
    -- west build: making build dir /home/ted/projects/zephyr-based/out-of-tree/kionix-driver-demo/build pristine
    -- west build: generating a build system
    Loading Zephyr default modules (Zephyr base).
    -- Application: /home/ted/projects/zephyr-based/out-of-tree/kionix-driver-demo
    -- Found Python3: /usr/bin/python3.8 (found suitable exact version "3.8.10") found components: Interpreter 
    -- Cache files will be written to: /home/ted/.cache/zephyr
    -- Zephyr version: 3.2.0 (/home/ted/projects/zephyr-based/out-of-tree/zephyr)
    -- Found west (found suitable version "0.11.0", minimum required is "0.7.1")
    -- Board: lpcxpresso55s69_cpu0
    CMake Error at /home/ted/projects/zephyr-based/out-of-tree/zephyr/cmake/modules/extensions.cmake:2071 (message):
      Assertion failed: GNUARMEMB_TOOLCHAIN_PATH is not set
    Call Stack (most recent call first):
      /home/ted/projects/zephyr-based/out-of-tree/zephyr/cmake/toolchain/gnuarmemb/generic.cmake:4 (assert)
      /home/ted/projects/zephyr-based/out-of-tree/zephyr/cmake/modules/FindHostTools.cmake:103 (include)
      /home/ted/projects/zephyr-based/out-of-tree/zephyr/cmake/modules/dts.cmake:8 (find_package)
      /home/ted/projects/zephyr-based/out-of-tree/zephyr/cmake/modules/zephyr_default.cmake:108 (include)
      /home/ted/projects/zephyr-based/out-of-tree/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:66 (include)
      /home/ted/projects/zephyr-based/out-of-tree/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:92 (include_boilerplate)
      CMakeLists.txt:15 (find_package)
    
    
    -- Configuring incomplete, errors occurred!
    FATAL ERROR: command exited with status 1: /usr/bin/cmake -DWEST_PYTHON=/usr/bin/python3 -B/home/ted/projects/zephyr-based/out-of-tree/kionix-driver-demo/build -GNinja -DBOARD=lpcxpresso55s69_cpu0 -S/home/ted/projects/zephyr-based/out-of-tree/kionix-driver-demo
    


    ^ Zephyr Threads Tutorial by Maksim Drachov

    Developer Maksim Drachov some helpful Zephyr build system and cmake tutorials, or explanatory documents on github. While not everything is explained, one of his example projects which builds a TCP/IP networking stack for a posix_native targeted board offers a number of more advanced cmake functions and features as part of this example project.

    Earliest Maksim github link noted here is:


    ^ cmake Debugging

    -- Application: /home/ted/projects/zephyr-based/kionix-driver-demo
    -- Zephyr version: 2.6.0-rc1 (/home/ted/projects/zephyr-based/zephyr), build: v2.6.0-rc1-ncs1
    -- Found Python3: /usr/bin/python3.8 (found suitable exact version "3.8.10") found components: Interpreter 
    -- Found west (found suitable version "0.12.0", minimum required is "0.7.1")
    -- Board: sparkfun_thing_plus_nrf9160
    -- Cache files will be written to: /home/ted/.cache/zephyr
    -- Found dtc: /usr/bin/dtc (found suitable version "1.5.0", minimum required is "1.4.6")
    CMake Error at /home/ted/projects/zephyr-based/zephyr/cmake/toolchain/zephyr/generic.cmake:3 (if):
      if given arguments:
    
        "VERSION_LESS_EQUAL" "0.11.2"
    
      Unknown arguments specified
    Call Stack (most recent call first):
      /home/ted/projects/zephyr-based/zephyr/cmake/generic_toolchain.cmake:36 (include)
      /home/ted/projects/zephyr-based/zephyr/cmake/app/boilerplate.cmake:553 (include)
      /home/ted/projects/zephyr-based/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:24 (include)
      /home/ted/projects/zephyr-based/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:35 (include_boilerplate)
      CMakeLists.txt:40 (find_package)
    
    
    -- Configuring incomplete, errors occurred!
    FATAL ERROR: command exited with status 1: /usr/bin/cmake -DWEST_PYTHON=/usr/bin/python3 -B/home/ted/projects/zephyr-based/kionix-driver-demo/build -S/home/ted/projects/zephyr-based/kionix-driver-demo -GNinja -DBOARD=sparkfun_thing_plus_nrf9160
    


    ^ Cmake links to sort

    2021-08-06

    Starting a page for cmake notes here. Links to cmake site:

    Longer guides:


    A Nordic Semi document on `cmake` and its role in the build process of Zephyr based applications:


    2021-08-17 - search for cmake / Zephyr means to add sources to a Zephyr based project:

    ^ Topics To Follow Up

    Who accepts the `--extra-args=CONFIG_*` option plus value? From `zephyr/scripts/pylib/twister/twisterlib/testinstance.py`:

    225     def create_overlay(self, platform, enable_asan=False, enable_ubsan=False, enable_coverage=False, coverage_platform=[]):
    226         # Create this in a "twister/" subdirectory otherwise this
    227         # will pass this overlay to kconfig.py *twice* and kconfig.cmake
    228         # will silently give that second time precedence over any
    229         # --extra-args=CONFIG_*
    230         subdir = os.path.join(self.build_dir, "twister")
    231 
    232         content = ""
    


    In Zephyr 3.2.0, `zephyr/cmake/modules/configuration_files.cmake` looks promising in terms of revealing where and how Kconfig processing is called during build of a Zephyr based app. An excerpt from this file:

      1 # SPDX-License-Identifier: Apache-2.0
      2 #
      3 # Copyright (c) 2021, Nordic Semiconductor ASA
      4 
      5 # Zephyr build system configuration files.
      6 #
      7 # Locate the Kconfig and DT config files that are to be used.
      8 # Also, locate the appropriate application config directory.
      9 #
     10 # Outcome:
     11 # The following variables will be defined when this CMake module completes:
     12 #
     13 # - CONF_FILE:              List of Kconfig fragments
     14 # - DTC_OVERLAY_FILE:       List of devicetree overlay files
     15 # - APPLICATION_CONFIG_DIR: Root folder for application configuration
     16 #
     17 # If any of the above variables are already set when this CMake module is
     18 # loaded, then no changes to the variable will happen.
     19 #
     20 # Variables set by this module and not mentioned above are considered internal
     21 # use only and may be removed, renamed, or re-purposed without prior notice.
    


    - - - top of page - - -