Anas Nashif | 3ae5262 | 2019-04-06 09:08:09 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | |
Torsten Rasmussen | 61453e4 | 2021-12-16 17:13:54 +0100 | [diff] [blame] | 3 | include_guard(GLOBAL) |
| 4 | |
| 5 | include(extensions) |
| 6 | include(python) |
| 7 | |
Torsten Rasmussen | 3673e28 | 2020-12-17 11:27:42 +0100 | [diff] [blame] | 8 | # This cmake file provides functionality to import CMakeLists.txt and Kconfig |
| 9 | # files for Zephyr modules into Zephyr build system. |
| 10 | # |
Torsten Rasmussen | 96b3e4d | 2021-12-16 15:52:28 +0100 | [diff] [blame] | 11 | # CMakeLists.txt and Kconfig files can reside directly in the Zephyr module or |
| 12 | # in a MODULE_EXT_ROOT. |
Torsten Rasmussen | 3673e28 | 2020-12-17 11:27:42 +0100 | [diff] [blame] | 13 | # The `<module>/zephyr/module.yml` file specifies whether the build files are |
Torsten Rasmussen | 96b3e4d | 2021-12-16 15:52:28 +0100 | [diff] [blame] | 14 | # located in the Zephyr module or in a MODULE_EXT_ROOT. |
Torsten Rasmussen | 3673e28 | 2020-12-17 11:27:42 +0100 | [diff] [blame] | 15 | # |
| 16 | # A list of Zephyr modules can be provided to the build system using: |
| 17 | # -DZEPHYR_MODULES=<module-path>[;<additional-module(s)-path>] |
| 18 | # |
| 19 | # It looks for: <module>/zephyr/module.yml or |
| 20 | # <module>/zephyr/CMakeLists.txt |
Torsten Rasmussen | 96b3e4d | 2021-12-16 15:52:28 +0100 | [diff] [blame] | 21 | # to load the Zephyr module into Zephyr build system. |
Marti Bolivar | 9516647 | 2023-01-07 14:47:42 -0800 | [diff] [blame] | 22 | # If west is installed, it uses west's APIs to obtain a list of projects to |
| 23 | # search for zephyr/module.yml from the current workspace's manifest. |
Torsten Rasmussen | 3673e28 | 2020-12-17 11:27:42 +0100 | [diff] [blame] | 24 | # |
| 25 | # If the module.yml file specifies that build files are located in a |
| 26 | # MODULE_EXT_ROOT then the variables: |
| 27 | # - `ZEPHYR_<MODULE_NAME>_CMAKE_DIR` is used for inclusion of the CMakeLists.txt |
| 28 | # - `ZEPHYR_<MODULE_NAME>_KCONFIG` is used for inclusion of the Kconfig |
| 29 | # files into the build system. |
Torsten Rasmussen | 7e9d1bd | 2019-02-05 10:36:22 +0100 | [diff] [blame] | 30 | |
Torsten Rasmussen | 0a9a3c0 | 2021-12-15 15:22:39 +0100 | [diff] [blame] | 31 | # Settings used by Zephyr module but where systems may define an alternative value. |
| 32 | set_ifndef(KCONFIG_BINARY_DIR ${CMAKE_BINARY_DIR}/Kconfig) |
| 33 | |
Torsten Rasmussen | 44a05e4 | 2022-08-19 16:27:39 +0200 | [diff] [blame] | 34 | zephyr_get(ZEPHYR_MODULES) |
Torsten Rasmussen | 7e9d1bd | 2019-02-05 10:36:22 +0100 | [diff] [blame] | 35 | if(ZEPHYR_MODULES) |
Torsten Rasmussen | bd7569f | 2019-03-19 10:38:18 +0100 | [diff] [blame] | 36 | set(ZEPHYR_MODULES_ARG "--modules" ${ZEPHYR_MODULES}) |
Torsten Rasmussen | 7e9d1bd | 2019-02-05 10:36:22 +0100 | [diff] [blame] | 37 | endif() |
| 38 | |
Torsten Rasmussen | a885027 | 2023-05-17 13:59:28 +0200 | [diff] [blame] | 39 | zephyr_get(EXTRA_ZEPHYR_MODULES VAR EXTRA_ZEPHYR_MODULES ZEPHYR_EXTRA_MODULES) |
| 40 | if(EXTRA_ZEPHYR_MODULES) |
| 41 | set(EXTRA_ZEPHYR_MODULES_ARG "--extra-modules" ${EXTRA_ZEPHYR_MODULES}) |
Torsten Rasmussen | 7e9d1bd | 2019-02-05 10:36:22 +0100 | [diff] [blame] | 42 | endif() |
| 43 | |
Torsten Rasmussen | 0a9a3c0 | 2021-12-15 15:22:39 +0100 | [diff] [blame] | 44 | file(MAKE_DIRECTORY ${KCONFIG_BINARY_DIR}) |
Jamie McCrae | 8cc7167 | 2023-02-20 09:36:50 +0000 | [diff] [blame] | 45 | set(kconfig_modules_file ${KCONFIG_BINARY_DIR}/Kconfig.modules) |
Jamie McCrae | df9027a | 2023-02-20 10:00:38 +0000 | [diff] [blame] | 46 | set(kconfig_sysbuild_file ${KCONFIG_BINARY_DIR}/Kconfig.sysbuild.modules) |
| 47 | set(cmake_modules_file ${CMAKE_BINARY_DIR}/zephyr_modules.txt) |
| 48 | set(cmake_sysbuild_file ${CMAKE_BINARY_DIR}/sysbuild_modules.txt) |
Jamie McCrae | 8cc7167 | 2023-02-20 09:36:50 +0000 | [diff] [blame] | 49 | set(zephyr_settings_file ${CMAKE_BINARY_DIR}/zephyr_settings.txt) |
Marc Herbert | c240b69 | 2019-03-29 23:35:12 -0700 | [diff] [blame] | 50 | |
Torsten Rasmussen | bd7569f | 2019-03-19 10:38:18 +0100 | [diff] [blame] | 51 | if(WEST OR ZEPHYR_MODULES) |
| 52 | # Zephyr module uses west, so only call it if west is installed or |
| 53 | # ZEPHYR_MODULES was provided as argument to CMake. |
| 54 | execute_process( |
| 55 | COMMAND |
| 56 | ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/zephyr_module.py |
Thomas Gagneret | c62dbb1 | 2024-01-17 15:56:59 +0100 | [diff] [blame] | 57 | --zephyr-base=${ZEPHYR_BASE} |
Torsten Rasmussen | bd7569f | 2019-03-19 10:38:18 +0100 | [diff] [blame] | 58 | ${ZEPHYR_MODULES_ARG} |
Torsten Rasmussen | a885027 | 2023-05-17 13:59:28 +0200 | [diff] [blame] | 59 | ${EXTRA_ZEPHYR_MODULES_ARG} |
Jamie McCrae | 8cc7167 | 2023-02-20 09:36:50 +0000 | [diff] [blame] | 60 | --kconfig-out ${kconfig_modules_file} |
Jamie McCrae | df9027a | 2023-02-20 10:00:38 +0000 | [diff] [blame] | 61 | --cmake-out ${cmake_modules_file} |
| 62 | --sysbuild-kconfig-out ${kconfig_sysbuild_file} |
| 63 | --sysbuild-cmake-out ${cmake_sysbuild_file} |
Jamie McCrae | 8cc7167 | 2023-02-20 09:36:50 +0000 | [diff] [blame] | 64 | --settings-out ${zephyr_settings_file} |
Torsten Rasmussen | 6c294bc | 2020-08-26 12:09:43 +0200 | [diff] [blame] | 65 | WORKING_DIRECTORY ${ZEPHYR_BASE} |
Torsten Rasmussen | bd7569f | 2019-03-19 10:38:18 +0100 | [diff] [blame] | 66 | ERROR_VARIABLE |
| 67 | zephyr_module_error_text |
| 68 | RESULT_VARIABLE |
| 69 | zephyr_module_return |
Torsten Rasmussen | 7e9d1bd | 2019-02-05 10:36:22 +0100 | [diff] [blame] | 70 | ) |
Torsten Rasmussen | 7e9d1bd | 2019-02-05 10:36:22 +0100 | [diff] [blame] | 71 | |
Torsten Rasmussen | 25d57ba | 2020-07-07 17:29:56 +0200 | [diff] [blame] | 72 | if(${zephyr_module_return}) |
Torsten Rasmussen | bd7569f | 2019-03-19 10:38:18 +0100 | [diff] [blame] | 73 | message(FATAL_ERROR "${zephyr_module_error_text}") |
Torsten Rasmussen | 7e9d1bd | 2019-02-05 10:36:22 +0100 | [diff] [blame] | 74 | endif() |
Marc Herbert | c240b69 | 2019-03-29 23:35:12 -0700 | [diff] [blame] | 75 | |
Jamie McCrae | 8cc7167 | 2023-02-20 09:36:50 +0000 | [diff] [blame] | 76 | if(EXISTS ${zephyr_settings_file}) |
| 77 | file(STRINGS ${zephyr_settings_file} zephyr_settings_txt ENCODING UTF-8 REGEX "^[^#]") |
| 78 | foreach(setting ${zephyr_settings_txt}) |
Torsten Rasmussen | 25d57ba | 2020-07-07 17:29:56 +0200 | [diff] [blame] | 79 | # Match <key>:<value> for each line of file, each corresponding to |
| 80 | # a setting. The use of quotes is required due to CMake not supporting |
| 81 | # lazy regexes (it supports greedy only). |
| 82 | string(REGEX REPLACE "\"(.*)\":\".*\"" "\\1" key ${setting}) |
| 83 | string(REGEX REPLACE "\".*\":\"(.*)\"" "\\1" value ${setting}) |
Torsten Rasmussen | 96b3e4d | 2021-12-16 15:52:28 +0100 | [diff] [blame] | 84 | list(APPEND ${key} ${value}) |
Torsten Rasmussen | 25d57ba | 2020-07-07 17:29:56 +0200 | [diff] [blame] | 85 | endforeach() |
| 86 | endif() |
| 87 | |
Torsten Rasmussen | 96b3e4d | 2021-12-16 15:52:28 +0100 | [diff] [blame] | 88 | # Append ZEPHYR_BASE as a default ext root at lowest priority |
| 89 | list(APPEND MODULE_EXT_ROOT ${ZEPHYR_BASE}) |
| 90 | |
Jamie McCrae | df9027a | 2023-02-20 10:00:38 +0000 | [diff] [blame] | 91 | if(EXISTS ${cmake_modules_file}) |
| 92 | file(STRINGS ${cmake_modules_file} zephyr_modules_txt ENCODING UTF-8) |
Torsten Rasmussen | a1c49be | 2021-11-26 10:11:00 +0100 | [diff] [blame] | 93 | endif() |
| 94 | |
Jamie McCrae | df9027a | 2023-02-20 10:00:38 +0000 | [diff] [blame] | 95 | set(ZEPHYR_MODULE_NAMES) |
| 96 | foreach(module ${zephyr_modules_txt}) |
| 97 | # Match "<name>":"<path>" for each line of file, each corresponding to |
| 98 | # one module. The use of quotes is required due to CMake not supporting |
| 99 | # lazy regexes (it supports greedy only). |
| 100 | string(REGEX REPLACE "\"(.*)\":\".*\":\".*\"" "\\1" module_name ${module}) |
| 101 | list(APPEND ZEPHYR_MODULE_NAMES ${module_name}) |
| 102 | endforeach() |
| 103 | |
| 104 | if(EXISTS ${cmake_sysbuild_file}) |
| 105 | file(STRINGS ${cmake_sysbuild_file} sysbuild_modules_txt ENCODING UTF-8) |
| 106 | endif() |
| 107 | |
| 108 | set(SYSBUILD_MODULE_NAMES) |
| 109 | foreach(module ${sysbuild_modules_txt}) |
| 110 | # Match "<name>":"<path>" for each line of file, each corresponding to |
| 111 | # one module. The use of quotes is required due to CMake not supporting |
| 112 | # lazy regexes (it supports greedy only). |
| 113 | string(REGEX REPLACE "\"(.*)\":\".*\":\".*\"" "\\1" module_name ${module}) |
| 114 | list(APPEND SYSBUILD_MODULE_NAMES ${module_name}) |
| 115 | endforeach() |
| 116 | |
Torsten Rasmussen | 96b3e4d | 2021-12-16 15:52:28 +0100 | [diff] [blame] | 117 | # MODULE_EXT_ROOT is process order which means Zephyr module roots processed |
| 118 | # later wins. therefore we reverse the list before processing. |
| 119 | list(REVERSE MODULE_EXT_ROOT) |
Torsten Rasmussen | 3673e28 | 2020-12-17 11:27:42 +0100 | [diff] [blame] | 120 | foreach(root ${MODULE_EXT_ROOT}) |
Pete Dietl | fd94ca7 | 2023-03-27 13:15:30 +0200 | [diff] [blame] | 121 | set(module_cmake_file_path modules/modules.cmake) |
| 122 | if(NOT EXISTS ${root}/${module_cmake_file_path}) |
| 123 | message(FATAL_ERROR "No `${module_cmake_file_path}` found in module root `${root}`.") |
Torsten Rasmussen | 3673e28 | 2020-12-17 11:27:42 +0100 | [diff] [blame] | 124 | endif() |
| 125 | |
Pete Dietl | fd94ca7 | 2023-03-27 13:15:30 +0200 | [diff] [blame] | 126 | include(${root}/${module_cmake_file_path}) |
Torsten Rasmussen | 3673e28 | 2020-12-17 11:27:42 +0100 | [diff] [blame] | 127 | endforeach() |
| 128 | |
Jamie McCrae | df9027a | 2023-02-20 10:00:38 +0000 | [diff] [blame] | 129 | foreach(module ${zephyr_modules_txt}) |
| 130 | # Match "<name>":"<path>" for each line of file, each corresponding to |
| 131 | # one Zephyr module. The use of quotes is required due to CMake not |
| 132 | # supporting lazy regexes (it supports greedy only). |
| 133 | string(CONFIGURE ${module} module) |
| 134 | string(REGEX REPLACE "\"(.*)\":\".*\":\".*\"" "\\1" module_name ${module}) |
| 135 | string(REGEX REPLACE "\".*\":\"(.*)\":\".*\"" "\\1" module_path ${module}) |
| 136 | string(REGEX REPLACE "\".*\":\".*\":\"(.*)\"" "\\1" cmake_path ${module}) |
Torsten Rasmussen | 2fc062b | 2020-08-24 11:01:04 +0200 | [diff] [blame] | 137 | |
Jamie McCrae | df9027a | 2023-02-20 10:00:38 +0000 | [diff] [blame] | 138 | zephyr_string(SANITIZE TOUPPER MODULE_NAME_UPPER ${module_name}) |
| 139 | if(NOT ${MODULE_NAME_UPPER} STREQUAL CURRENT) |
Pieter De Gendt | fcaed38 | 2024-01-16 19:16:23 +0100 | [diff] [blame] | 140 | set(ZEPHYR_${MODULE_NAME_UPPER}_MODULE_NAME ${module_name}) |
Jamie McCrae | df9027a | 2023-02-20 10:00:38 +0000 | [diff] [blame] | 141 | set(ZEPHYR_${MODULE_NAME_UPPER}_MODULE_DIR ${module_path}) |
| 142 | set(ZEPHYR_${MODULE_NAME_UPPER}_CMAKE_DIR ${cmake_path}) |
| 143 | else() |
| 144 | message(FATAL_ERROR "Found Zephyr module named: ${module_name}\n\ |
Torsten Rasmussen | 4e8a991 | 2020-08-26 09:45:08 +0200 | [diff] [blame] | 145 | ${MODULE_NAME_UPPER} is a restricted name for Zephyr modules as it is used for \ |
| 146 | \${ZEPHYR_${MODULE_NAME_UPPER}_MODULE_DIR} CMake variable.") |
Jamie McCrae | df9027a | 2023-02-20 10:00:38 +0000 | [diff] [blame] | 147 | endif() |
| 148 | endforeach() |
| 149 | |
| 150 | foreach(module ${sysbuild_modules_txt}) |
| 151 | # Match "<name>":"<path>" for each line of file, each corresponding to |
| 152 | # one Zephyr module. The use of quotes is required due to CMake not |
| 153 | # supporting lazy regexes (it supports greedy only). |
| 154 | string(CONFIGURE ${module} module) |
| 155 | string(REGEX REPLACE "\"(.*)\":\".*\":\".*\"" "\\1" module_name ${module}) |
| 156 | string(REGEX REPLACE "\".*\":\"(.*)\":\".*\"" "\\1" module_path ${module}) |
| 157 | string(REGEX REPLACE "\".*\":\".*\":\"(.*)\"" "\\1" cmake_path ${module}) |
| 158 | |
| 159 | zephyr_string(SANITIZE TOUPPER MODULE_NAME_UPPER ${module_name}) |
| 160 | if(NOT ${MODULE_NAME_UPPER} STREQUAL CURRENT) |
| 161 | set(SYSBUILD_${MODULE_NAME_UPPER}_MODULE_DIR ${module_path}) |
| 162 | set(SYSBUILD_${MODULE_NAME_UPPER}_CMAKE_DIR ${cmake_path}) |
| 163 | else() |
| 164 | message(FATAL_ERROR "Found Zephyr module named: ${module_name}\n\ |
| 165 | ${MODULE_NAME_UPPER} is a restricted name for Zephyr modules as it is used for \ |
| 166 | \${SYSBUILD_${MODULE_NAME_UPPER}_MODULE_DIR} CMake variable.") |
| 167 | endif() |
| 168 | endforeach() |
Marc Herbert | c240b69 | 2019-03-29 23:35:12 -0700 | [diff] [blame] | 169 | else() |
| 170 | |
Jamie McCrae | 8cc7167 | 2023-02-20 09:36:50 +0000 | [diff] [blame] | 171 | file(WRITE ${kconfig_modules_file} |
Torsten Rasmussen | 96b3e4d | 2021-12-16 15:52:28 +0100 | [diff] [blame] | 172 | "# No west and no Zephyr modules\n" |
Marc Herbert | c240b69 | 2019-03-29 23:35:12 -0700 | [diff] [blame] | 173 | ) |
| 174 | |
Jamie McCrae | df9027a | 2023-02-20 10:00:38 +0000 | [diff] [blame] | 175 | file(WRITE ${kconfig_sysbuild_file} |
| 176 | "# No west and no Zephyr modules\n" |
| 177 | ) |
| 178 | |
Torsten Rasmussen | bd7569f | 2019-03-19 10:38:18 +0100 | [diff] [blame] | 179 | endif() |