zephyr/cmake/zephyr_module.cmake
Torsten Rasmussen 7e9d1bdda4 CMake/Kconfig: CMake and Kconfig build integration
This commit allows for Zephyr modules to be natively integrated into
the build system with CMakeLists.txt and Kconfig files.

The sourcing of module files are done in following order:
- If <module>/zephyr/module.yml exists, use cmake and kconfig settings
  for sourcing of additional file
- Else if <module>/zephyr/CMakeLists.txt exists, source this file into
  CMake build tree and add <module>/zephyr/Kconfig as osource

If none of the above files are present, the project is considered to
not be a Zephyr module

Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
2019-02-08 22:47:02 -05:00

85 lines
3.0 KiB
CMake

# This cmake file provides functionality to import additional out-of-tree, OoT
# CMakeLists.txt and Kconfig files into Zephyr build system.
# It uses -DZEPHYR_MODULES=<oot-path-to-module>[;<additional-oot-module(s)>]
# given to CMake for a list of folders to search.
# It looks for: <oot-module>/zephyr/module.yml or
# <oot-module>/zephyr/CMakeLists.txt
# to load the oot-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
if(ZEPHYR_MODULES)
set(west_project_list ${ZEPHYR_MODULES})
elseif(WEST)
## Use `west list` to fetch all west handled projects.
execute_process(
COMMAND
${WEST} list --format={posixpath}
OUTPUT_VARIABLE
west_project_output
)
string(REGEX REPLACE "[\r\n]+" ";" west_project_list "${west_project_output}")
endif()
if(ZEPHYR_EXTRA_MODULES)
list(APPEND west_project_list ${ZEPHYR_EXTRA_MODULES})
endif()
# Clear the Kconfig.modules generated file in case modules has been removed.
# The Kconfig.modules contains a list of additional Kconfig files to be sourced
# based upon <module>/zephyr/module.yml files.
file(WRITE ${PROJECT_BINARY_DIR}/Kconfig.modules
"# NOTE: THIS FILE IS AUTOGENERATED BY CMAKE\n"
)
# For each west managed project, determine if the project is a zephyr module.
foreach(module ${west_project_list})
set(cmake_subdir "zephyr")
if(${ZEPHYR_BASE} STREQUAL ${module})
# Ignore Zephyr project to avoid potential invalid looping
elseif(EXISTS "${module}/zephyr/module.yml")
set(kconfig_osource "osource \"${module}/zephyr/Kconfig\"\n")
get_filename_component(module_name ${module} NAME)
execute_process(
COMMAND
${PYTHON_EXECUTABLE}
${ZEPHYR_BASE}/scripts/yaml_to_cmake.py
-i "${module}/zephyr/module.yml"
-o "${CMAKE_CURRENT_BINARY_DIR}/zephyr_module_${module_name}.txt"
-s "build"
)
file(
STRINGS
"${CMAKE_CURRENT_BINARY_DIR}/zephyr_module_${module_name}.txt"
zephyr_module
)
foreach(key_value ${zephyr_module})
if(${key_value} MATCHES "^cmake=")
string(REGEX REPLACE "^cmake=" "" cmake_subdir ${key_value})
elseif(${key_value} MATCHES "^kconfig=")
string(
REGEX REPLACE
"^kconfig="
"osource \"${module}/"
kconfig_osource
${key_value} "\"\n"
)
endif()
endforeach()
list(APPEND ZEPHYR_MODULES_NAME ${module_name})
list(APPEND ZEPHYR_MODULES_DIR ${module}/${cmake_subdir})
file(APPEND ${PROJECT_BINARY_DIR}/Kconfig.modules ${kconfig_osource})
elseif(EXISTS "${module}/${cmake_subdir}/CMakeLists.txt")
set(kconfig_osource "osource \"${module}/zephyr/Kconfig\"\n")
get_filename_component(module_name ${module} NAME)
list(APPEND ZEPHYR_MODULES_NAME ${module_name})
list(APPEND ZEPHYR_MODULES_DIR ${module}/${cmake_subdir})
file(APPEND ${PROJECT_BINARY_DIR}/Kconfig.modules ${kconfig_osource})
else()
# Not a Zephyr module, ignore.
endif()
endforeach()