cmake: create a root.cmake CMake module

The root.cmake CMake module remove boilerplate code and place
it inside a dedicated root.cmake CMake module.

This is part of a general CMake overhaul to allow better modularization
and reuse of the Zephyr build system.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
This commit is contained in:
Torsten Rasmussen 2021-12-16 15:52:28 +01:00 committed by Marti Bolivar
parent bdb99a330f
commit 96b3e4d0c4
3 changed files with 45 additions and 28 deletions

View file

@ -71,13 +71,7 @@ include(${ZEPHYR_BASE}/cmake/python.cmake)
include(${ZEPHYR_BASE}/cmake/west.cmake)
include(${ZEPHYR_BASE}/cmake/ccache.cmake)
# 'MODULE_EXT_ROOT' is a prioritized list of directories where module glue code
# may be found. It always includes ${ZEPHYR_BASE} at the lowest priority.
# For module roots, later entries may overrule module settings already defined
# by processed module roots, hence first in list means lowest priority.
zephyr_file(APPLICATION_ROOT MODULE_EXT_ROOT)
list(INSERT MODULE_EXT_ROOT 0 ${ZEPHYR_BASE})
include(${ZEPHYR_BASE}/cmake/root.cmake)
#
# Find Zephyr modules.
# Those may contain additional DTS, BOARD, SOC, ARCH ROOTs.
@ -85,12 +79,6 @@ list(INSERT MODULE_EXT_ROOT 0 ${ZEPHYR_BASE})
#
include(${ZEPHYR_BASE}/cmake/zephyr_module.cmake)
zephyr_file(APPLICATION_ROOT BOARD_ROOT)
zephyr_file(APPLICATION_ROOT SOC_ROOT)
zephyr_file(APPLICATION_ROOT ARCH_ROOT)
include(${ZEPHYR_BASE}/cmake/boards.cmake)
include(${ZEPHYR_BASE}/cmake/shields.cmake)
include(${ZEPHYR_BASE}/cmake/arch.cmake)

30
cmake/root.cmake Normal file
View file

@ -0,0 +1,30 @@
# SPDX-License-Identifier: Apache-2.0
#
# Copyright (c) 2021, Nordic Semiconductor ASA
# Convert Zephyr roots to absolute paths.
#
# This CMake module will convert all relative paths in existing ROOT lists to
# absolute path relative from APPLICATION_SOURCE_DIR.
#
# Optional variables:
# - ARCH_ROOT: CMake list of arch roots containing arch implementations
# - SOC_ROOT: CMake list of SoC roots containing SoC implementations
# - BOARD_ROOT: CMake list of board roots containing board and shield implementations
# - MODULE_EXT_ROOT: CMake list of module external roots containing module glue code
#
# If a root is defined it will check the list of paths in the root and convert
# any relative path to absolute path and update the root list.
# If a root is undefined it will still be undefined when this module has loaded.
# Convert paths to absolute, relative from APPLICATION_SOURCE_DIR
zephyr_file(APPLICATION_ROOT MODULE_EXT_ROOT)
# Convert paths to absolute, relative from APPLICATION_SOURCE_DIR
zephyr_file(APPLICATION_ROOT BOARD_ROOT)
# Convert paths to absolute, relative from APPLICATION_SOURCE_DIR
zephyr_file(APPLICATION_ROOT SOC_ROOT)
# Convert paths to absolute, relative from APPLICATION_SOURCE_DIR
zephyr_file(APPLICATION_ROOT ARCH_ROOT)

View file

@ -3,17 +3,17 @@
# This cmake file provides functionality to import CMakeLists.txt and Kconfig
# files for Zephyr modules into Zephyr build system.
#
# CMakeLists.txt and Kconfig files can reside directly in the module or in a
# MODULE_EXT_ROOT.
# CMakeLists.txt and Kconfig files can reside directly in the Zephyr module or
# in a MODULE_EXT_ROOT.
# The `<module>/zephyr/module.yml` file specifies whether the build files are
# located in the module or in a MODULE_EXT_ROOT.
# located in the Zephyr module or in a MODULE_EXT_ROOT.
#
# A list of Zephyr modules can be provided to the build system using:
# -DZEPHYR_MODULES=<module-path>[;<additional-module(s)-path>]
#
# It looks for: <module>/zephyr/module.yml or
# <module>/zephyr/CMakeLists.txt
# to load the module into Zephyr build system.
# to load the Zephyr module into Zephyr build system.
# If west is available, it uses `west list` to obtain a list of projects to
# search for zephyr/module.yml
#
@ -81,17 +81,13 @@ if(WEST OR ZEPHYR_MODULES)
# lazy regexes (it supports greedy only).
string(REGEX REPLACE "\"(.*)\":\".*\"" "\\1" key ${setting})
string(REGEX REPLACE "\".*\":\"(.*)\"" "\\1" value ${setting})
# MODULE_EXT_ROOT is process order which means module roots processed
# later wins. To ensure ZEPHYR_BASE stays first, and command line settings
# are processed last, we insert at position 1.
if ("${key}" STREQUAL "MODULE_EXT_ROOT")
list(INSERT ${key} 1 ${value})
else()
list(APPEND ${key} ${value})
endif()
list(APPEND ${key} ${value})
endforeach()
endif()
# Append ZEPHYR_BASE as a default ext root at lowest priority
list(APPEND MODULE_EXT_ROOT ${ZEPHYR_BASE})
if(EXISTS ${CMAKE_BINARY_DIR}/zephyr_modules.txt)
file(STRINGS ${CMAKE_BINARY_DIR}/zephyr_modules.txt ZEPHYR_MODULES_TXT
ENCODING UTF-8)
@ -106,6 +102,9 @@ if(WEST OR ZEPHYR_MODULES)
endforeach()
endif()
# MODULE_EXT_ROOT is process order which means Zephyr module roots processed
# later wins. therefore we reverse the list before processing.
list(REVERSE MODULE_EXT_ROOT)
foreach(root ${MODULE_EXT_ROOT})
if(NOT EXISTS ${root})
message(FATAL_ERROR "No `modules.cmake` found in module root `${root}`.")
@ -117,8 +116,8 @@ if(WEST OR ZEPHYR_MODULES)
if(DEFINED ZEPHYR_MODULES_TXT)
foreach(module ${ZEPHYR_MODULES_TXT})
# Match "<name>":"<path>" for each line of file, each corresponding to
# one module. The use of quotes is required due to CMake not supporting
# lazy regexes (it supports greedy only).
# one Zephyr module. The use of quotes is required due to CMake not
# supporting lazy regexes (it supports greedy only).
string(CONFIGURE ${module} module)
string(REGEX REPLACE "\"(.*)\":\".*\":\".*\"" "\\1" module_name ${module})
string(REGEX REPLACE "\".*\":\"(.*)\":\".*\"" "\\1" module_path ${module})
@ -138,7 +137,7 @@ ${MODULE_NAME_UPPER} is a restricted name for Zephyr modules as it is used for \
else()
file(WRITE ${KCONFIG_MODULES_FILE}
"# No west and no modules\n"
"# No west and no Zephyr modules\n"
)
endif()