Torsten Rasmussen | bdb99a3 | 2021-12-16 15:39:48 +0100 | [diff] [blame] | 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # |
| 3 | # Copyright (c) 2021, Nordic Semiconductor ASA |
| 4 | |
| 5 | # Zephyr Kernel CMake module. |
| 6 | # |
Nazar Kazakov | f483b1b | 2022-03-16 21:07:43 +0000 | [diff] [blame] | 7 | # This is the main Zephyr Kernel CMake module which is responsible for creation |
| 8 | # of Zephyr libraries and the Zephyr executable. |
Torsten Rasmussen | bdb99a3 | 2021-12-16 15:39:48 +0100 | [diff] [blame] | 9 | # |
| 10 | # This CMake module creates 'project(Zephyr-Kernel)' |
| 11 | # |
Anas Nashif | 82613fb | 2024-09-04 21:16:40 -0400 | [diff] [blame] | 12 | # It defines properties to use while configuring libraries to be built as well |
Torsten Rasmussen | bdb99a3 | 2021-12-16 15:39:48 +0100 | [diff] [blame] | 13 | # as using add_subdirectory() to add the main <ZEPHYR_BASE>/CMakeLists.txt file. |
| 14 | # |
| 15 | # Outcome: |
| 16 | # - Zephyr build system. |
| 17 | # - Zephyr project |
| 18 | # |
| 19 | # Important libraries: |
| 20 | # - app: This is the main application library where the application can add |
| 21 | # source files that must be included when building Zephyr |
Torsten Rasmussen | 61453e4 | 2021-12-16 17:13:54 +0100 | [diff] [blame] | 22 | |
| 23 | include_guard(GLOBAL) |
| 24 | |
Torsten Rasmussen | 7131d02 | 2022-08-30 11:11:36 +0200 | [diff] [blame] | 25 | find_package(TargetTools) |
Torsten Rasmussen | cb690ec | 2022-11-30 12:14:35 +0100 | [diff] [blame] | 26 | find_package(ScaTools) |
Torsten Rasmussen | 7131d02 | 2022-08-30 11:11:36 +0200 | [diff] [blame] | 27 | |
Torsten Rasmussen | 61453e4 | 2021-12-16 17:13:54 +0100 | [diff] [blame] | 28 | # As this module is not intended for direct loading, but should be loaded through |
| 29 | # find_package(Zephyr) then it won't be loading any Zephyr CMake modules by itself. |
Torsten Rasmussen | bdb99a3 | 2021-12-16 15:39:48 +0100 | [diff] [blame] | 30 | |
| 31 | define_property(GLOBAL PROPERTY ZEPHYR_LIBS |
| 32 | BRIEF_DOCS "Global list of all Zephyr CMake libs that should be linked in" |
| 33 | FULL_DOCS "Global list of all Zephyr CMake libs that should be linked in. |
| 34 | zephyr_library() appends libs to this list.") |
| 35 | set_property(GLOBAL PROPERTY ZEPHYR_LIBS "") |
| 36 | |
| 37 | define_property(GLOBAL PROPERTY ZEPHYR_INTERFACE_LIBS |
| 38 | BRIEF_DOCS "Global list of all Zephyr interface libs that should be linked in." |
| 39 | FULL_DOCS "Global list of all Zephyr interface libs that should be linked in. |
| 40 | zephyr_interface_library_named() appends libs to this list.") |
| 41 | set_property(GLOBAL PROPERTY ZEPHYR_INTERFACE_LIBS "") |
| 42 | |
| 43 | define_property(GLOBAL PROPERTY GENERATED_APP_SOURCE_FILES |
| 44 | BRIEF_DOCS "Source files that are generated after Zephyr has been linked once." |
| 45 | FULL_DOCS "\ |
| 46 | Source files that are generated after Zephyr has been linked once.\ |
| 47 | May include dev_handles.c etc." |
| 48 | ) |
| 49 | set_property(GLOBAL PROPERTY GENERATED_APP_SOURCE_FILES "") |
| 50 | |
| 51 | define_property(GLOBAL PROPERTY GENERATED_KERNEL_OBJECT_FILES |
| 52 | BRIEF_DOCS "Object files that are generated after symbol addresses are fixed." |
| 53 | FULL_DOCS "\ |
| 54 | Object files that are generated after symbol addresses are fixed.\ |
| 55 | May include mmu tables, etc." |
| 56 | ) |
| 57 | set_property(GLOBAL PROPERTY GENERATED_KERNEL_OBJECT_FILES "") |
| 58 | |
| 59 | define_property(GLOBAL PROPERTY GENERATED_KERNEL_SOURCE_FILES |
| 60 | BRIEF_DOCS "Source files that are generated after symbol addresses are fixed." |
| 61 | FULL_DOCS "\ |
| 62 | Source files that are generated after symbol addresses are fixed.\ |
| 63 | May include isr_tables.c etc." |
| 64 | ) |
| 65 | set_property(GLOBAL PROPERTY GENERATED_KERNEL_SOURCE_FILES "") |
| 66 | |
| 67 | add_custom_target(code_data_relocation_target) |
| 68 | |
| 69 | # The zephyr/runners.yaml file in the build directory is used to |
| 70 | # configure the scripts/west_commands/runners Python package used |
| 71 | # by 'west flash', 'west debug', etc. |
| 72 | # |
| 73 | # This is a helper target for setting property:value pairs related to |
| 74 | # this file: |
| 75 | # |
| 76 | # Property Description |
| 77 | # -------------- -------------------------------------------------- |
| 78 | # bin_file "zephyr.bin" file for flashing |
| 79 | # hex_file "zephyr.hex" file for flashing |
| 80 | # elf_file "zephyr.elf" file for flashing or debugging |
| 81 | # yaml_contents generated contents of runners.yaml |
| 82 | # |
| 83 | # Note: there are quotes around "zephyr.bin" etc. because the actual |
| 84 | # paths can be changed, e.g. to flash signed versions of these files |
| 85 | # for consumption by bootloaders such as MCUboot. |
| 86 | # |
| 87 | # See cmake/flash/CMakeLists.txt for more details. |
| 88 | add_custom_target(runners_yaml_props_target) |
| 89 | |
| 90 | # CMake's 'project' concept has proven to not be very useful for Zephyr |
| 91 | # due in part to how Zephyr is organized and in part to it not fitting well |
| 92 | # with cross compilation. |
| 93 | # Zephyr therefore tries to rely as little as possible on project() |
| 94 | # and its associated variables, e.g. PROJECT_SOURCE_DIR. |
| 95 | # It is recommended to always use ZEPHYR_BASE instead of PROJECT_SOURCE_DIR |
| 96 | # when trying to reference ENV${ZEPHYR_BASE}. |
| 97 | |
| 98 | # Note any later project() resets PROJECT_SOURCE_DIR |
| 99 | file(TO_CMAKE_PATH "${ZEPHYR_BASE}" PROJECT_SOURCE_DIR) |
| 100 | |
| 101 | set(ZEPHYR_BINARY_DIR ${PROJECT_BINARY_DIR}) |
| 102 | |
| 103 | if(${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_BINARY_DIR}) |
| 104 | message(FATAL_ERROR "Source directory equals build directory.\ |
| 105 | In-source builds are not supported.\ |
| 106 | Please specify a build directory, e.g. cmake -Bbuild -H.") |
| 107 | endif() |
| 108 | |
| 109 | add_custom_target( |
| 110 | pristine |
| 111 | COMMAND ${CMAKE_COMMAND} -DBINARY_DIR=${APPLICATION_BINARY_DIR} |
| 112 | -DSOURCE_DIR=${APPLICATION_SOURCE_DIR} |
| 113 | -P ${ZEPHYR_BASE}/cmake/pristine.cmake |
| 114 | # Equivalent to rm -rf build/* |
| 115 | ) |
| 116 | |
| 117 | # Dummy add to generate files. |
| 118 | zephyr_linker_sources(SECTIONS) |
| 119 | |
| 120 | # For the gen_app_partitions.py to work correctly, we must ensure that |
| 121 | # all targets exports their compile commands to fetch object files. |
| 122 | # We enable it unconditionally, as this is also useful for several IDEs |
| 123 | set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE CACHE BOOL |
| 124 | "Export CMake compile commands. Used by gen_app_partitions.py script" |
| 125 | FORCE |
| 126 | ) |
| 127 | |
| 128 | project(Zephyr-Kernel VERSION ${PROJECT_VERSION}) |
| 129 | |
| 130 | # Add .S file extension suffix into CMAKE_ASM_SOURCE_FILE_EXTENSIONS, |
Nazar Kazakov | f483b1b | 2022-03-16 21:07:43 +0000 | [diff] [blame] | 131 | # because clang from OneApi can't recognize them as asm files on |
Torsten Rasmussen | bdb99a3 | 2021-12-16 15:39:48 +0100 | [diff] [blame] | 132 | # windows now. |
| 133 | list(APPEND CMAKE_ASM_SOURCE_FILE_EXTENSIONS "S") |
Nicolas Lebedenco | 4df926b | 2024-03-28 23:37:01 -0400 | [diff] [blame] | 134 | enable_language(ASM) |
Torsten Rasmussen | bdb99a3 | 2021-12-16 15:39:48 +0100 | [diff] [blame] | 135 | |
| 136 | # The setup / configuration of the toolchain itself and the configuration of |
| 137 | # supported compilation flags are now split, as this allows to use the toolchain |
| 138 | # for generic purposes, for example DTS, and then test the toolchain for |
| 139 | # supported flags at stage two. |
| 140 | # Testing the toolchain flags requires the enable_language() to have been called in CMake. |
| 141 | |
| 142 | # Verify that the toolchain can compile a dummy file, if it is not we |
| 143 | # won't be able to test for compatibility with certain C flags. |
| 144 | zephyr_check_compiler_flag(C "" toolchain_is_ok) |
Marc Herbert | 4cba9e6 | 2023-09-14 22:30:33 +0000 | [diff] [blame] | 145 | assert(toolchain_is_ok "The toolchain is unable to build a dummy C file.\ |
| 146 | Move ${USER_CACHE_DIR}, re-run and look at CMakeError.log") |
Torsten Rasmussen | bdb99a3 | 2021-12-16 15:39:48 +0100 | [diff] [blame] | 147 | |
| 148 | include(${ZEPHYR_BASE}/cmake/target_toolchain_flags.cmake) |
| 149 | |
| 150 | # 'project' sets PROJECT_BINARY_DIR to ${CMAKE_CURRENT_BINARY_DIR}, |
| 151 | # but for legacy reasons we need it to be set to |
| 152 | # ${CMAKE_CURRENT_BINARY_DIR}/zephyr |
| 153 | set(PROJECT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/zephyr) |
| 154 | set(PROJECT_SOURCE_DIR ${ZEPHYR_BASE}) |
| 155 | |
| 156 | set(KERNEL_NAME ${CONFIG_KERNEL_BIN_NAME}) |
| 157 | |
| 158 | set(KERNEL_ELF_NAME ${KERNEL_NAME}.elf) |
| 159 | set(KERNEL_BIN_NAME ${KERNEL_NAME}.bin) |
| 160 | set(KERNEL_HEX_NAME ${KERNEL_NAME}.hex) |
| 161 | set(KERNEL_UF2_NAME ${KERNEL_NAME}.uf2) |
| 162 | set(KERNEL_MAP_NAME ${KERNEL_NAME}.map) |
| 163 | set(KERNEL_LST_NAME ${KERNEL_NAME}.lst) |
| 164 | set(KERNEL_S19_NAME ${KERNEL_NAME}.s19) |
| 165 | set(KERNEL_EXE_NAME ${KERNEL_NAME}.exe) |
| 166 | set(KERNEL_STAT_NAME ${KERNEL_NAME}.stat) |
| 167 | set(KERNEL_STRIP_NAME ${KERNEL_NAME}.strip) |
| 168 | set(KERNEL_META_NAME ${KERNEL_NAME}.meta) |
Anas Nashif | 47a673f | 2022-06-27 10:08:37 -0400 | [diff] [blame] | 169 | set(KERNEL_SYMBOLS_NAME ${KERNEL_NAME}.symbols) |
Torsten Rasmussen | bdb99a3 | 2021-12-16 15:39:48 +0100 | [diff] [blame] | 170 | |
Nicolas Lebedenco | 6837ca8 | 2024-04-25 08:47:10 -0400 | [diff] [blame] | 171 | # Enable dynamic library support when required by LLEXT. |
| 172 | if(CONFIG_LLEXT AND CONFIG_LLEXT_TYPE_ELF_SHAREDLIB) |
| 173 | set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE) |
| 174 | endif() |
| 175 | |
Torsten Rasmussen | 536d34f | 2024-05-08 13:10:48 +0200 | [diff] [blame] | 176 | foreach(dir ${BOARD_DIRECTORIES}) |
| 177 | include(${dir}/board.cmake OPTIONAL) |
| 178 | endforeach() |
Torsten Rasmussen | bdb99a3 | 2021-12-16 15:39:48 +0100 | [diff] [blame] | 179 | |
| 180 | # If we are using a suitable ethernet driver inside qemu, then these options |
| 181 | # must be set, otherwise a zephyr instance cannot receive any network packets. |
| 182 | # The Qemu supported ethernet driver should define CONFIG_ETH_NIC_MODEL |
| 183 | # string that tells what nic model Qemu should use. |
| 184 | if(CONFIG_QEMU_TARGET) |
| 185 | if ((CONFIG_NET_QEMU_ETHERNET OR CONFIG_NET_QEMU_USER) AND NOT CONFIG_ETH_NIC_MODEL) |
| 186 | message(FATAL_ERROR " |
| 187 | No Qemu ethernet driver configured! |
| 188 | Enable Qemu supported ethernet driver like e1000 at drivers/ethernet" |
| 189 | ) |
| 190 | elseif(CONFIG_NET_QEMU_ETHERNET) |
| 191 | if(CONFIG_ETH_QEMU_EXTRA_ARGS) |
| 192 | set(NET_QEMU_ETH_EXTRA_ARGS ",${CONFIG_ETH_QEMU_EXTRA_ARGS}") |
| 193 | endif() |
| 194 | list(APPEND QEMU_FLAGS_${ARCH} |
| 195 | -nic tap,model=${CONFIG_ETH_NIC_MODEL},script=no,downscript=no,ifname=${CONFIG_ETH_QEMU_IFACE_NAME}${NET_QEMU_ETH_EXTRA_ARGS} |
| 196 | ) |
| 197 | elseif(CONFIG_NET_QEMU_USER) |
| 198 | list(APPEND QEMU_FLAGS_${ARCH} |
| 199 | -nic user,model=${CONFIG_ETH_NIC_MODEL},${CONFIG_NET_QEMU_USER_EXTRA_ARGS} |
| 200 | ) |
| 201 | else() |
| 202 | list(APPEND QEMU_FLAGS_${ARCH} |
| 203 | -net none |
| 204 | ) |
| 205 | endif() |
| 206 | endif() |
| 207 | |
| 208 | # General purpose Zephyr target. |
| 209 | # This target can be used for custom zephyr settings that needs to be used elsewhere in the build system |
| 210 | # |
| 211 | # Currently used properties: |
| 212 | # - COMPILES_OPTIONS: Used by application memory partition feature |
| 213 | add_custom_target(zephyr_property_target) |
| 214 | |
| 215 | # "app" is a CMake library containing all the application code and is |
| 216 | # modified by the entry point ${APPLICATION_SOURCE_DIR}/CMakeLists.txt |
| 217 | # that was specified when cmake was called. |
| 218 | zephyr_library_named(app) |
| 219 | set_property(TARGET app PROPERTY ARCHIVE_OUTPUT_DIRECTORY app) |
| 220 | |
| 221 | add_subdirectory(${ZEPHYR_BASE} ${__build_dir}) |
| 222 | |
| 223 | # Link 'app' with the Zephyr interface libraries. |
| 224 | # |
Torsten Rasmussen | 61453e4 | 2021-12-16 17:13:54 +0100 | [diff] [blame] | 225 | # NB: This must be done here because 'app' can only be modified in the |
| 226 | # CMakeLists.txt file that created it. And it must be |
Torsten Rasmussen | bdb99a3 | 2021-12-16 15:39:48 +0100 | [diff] [blame] | 227 | # done after 'add_subdirectory(${ZEPHYR_BASE} ${__build_dir})' |
| 228 | # because interface libraries are defined while processing that |
| 229 | # subdirectory. |
| 230 | get_property(ZEPHYR_INTERFACE_LIBS_PROPERTY GLOBAL PROPERTY ZEPHYR_INTERFACE_LIBS) |
| 231 | foreach(boilerplate_lib ${ZEPHYR_INTERFACE_LIBS_PROPERTY}) |
| 232 | # Linking 'app' with 'boilerplate_lib' causes 'app' to inherit the INTERFACE |
| 233 | # properties of 'boilerplate_lib'. The most common property is 'include |
| 234 | # directories', but it is also possible to have defines and compiler |
| 235 | # flags in the interface of a library. |
| 236 | # |
| 237 | string(TOUPPER ${boilerplate_lib} boilerplate_lib_upper_case) # Support lowercase lib names |
| 238 | target_link_libraries_ifdef( |
| 239 | CONFIG_APP_LINK_WITH_${boilerplate_lib_upper_case} |
| 240 | app |
| 241 | PUBLIC |
| 242 | ${boilerplate_lib} |
| 243 | ) |
| 244 | endforeach() |
| 245 | |
| 246 | if("${CMAKE_EXTRA_GENERATOR}" STREQUAL "Eclipse CDT4") |
| 247 | # Call the amendment function before .project and .cproject generation |
| 248 | # C and CXX includes, defines in .cproject without __cplusplus |
| 249 | # with project includes and defines |
| 250 | include(${ZEPHYR_BASE}/cmake/ide/eclipse_cdt4_generator_amendment.cmake) |
| 251 | eclipse_cdt4_generator_amendment(1) |
| 252 | endif() |