1# SPDX-License-Identifier: Apache-2.0 2# 3# Copyright (c) 2021, Nordic Semiconductor ASA 4 5# Zephyr Kernel CMake module. 6# 7# This is the main Zephyr Kernel CMake module which is responsible for creation 8# of Zephyr libraries and the Zephyr executable. 9# 10# This CMake module creates 'project(Zephyr-Kernel)' 11# 12# It defines properties to use while configuring libraries to be built as well 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 22 23include_guard(GLOBAL) 24 25find_package(TargetTools) 26find_package(ScaTools) 27 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. 30 31define_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. 34zephyr_library() appends libs to this list.") 35set_property(GLOBAL PROPERTY ZEPHYR_LIBS "") 36 37define_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. 40zephyr_interface_library_named() appends libs to this list.") 41set_property(GLOBAL PROPERTY ZEPHYR_INTERFACE_LIBS "") 42 43define_property(GLOBAL PROPERTY GENERATED_APP_SOURCE_FILES 44 BRIEF_DOCS "Source files that are generated after Zephyr has been linked once." 45 FULL_DOCS "\ 46Source files that are generated after Zephyr has been linked once.\ 47May include dev_handles.c etc." 48 ) 49set_property(GLOBAL PROPERTY GENERATED_APP_SOURCE_FILES "") 50 51define_property(GLOBAL PROPERTY GENERATED_KERNEL_OBJECT_FILES 52 BRIEF_DOCS "Object files that are generated after symbol addresses are fixed." 53 FULL_DOCS "\ 54Object files that are generated after symbol addresses are fixed.\ 55May include mmu tables, etc." 56 ) 57set_property(GLOBAL PROPERTY GENERATED_KERNEL_OBJECT_FILES "") 58 59define_property(GLOBAL PROPERTY GENERATED_KERNEL_SOURCE_FILES 60 BRIEF_DOCS "Source files that are generated after symbol addresses are fixed." 61 FULL_DOCS "\ 62Source files that are generated after symbol addresses are fixed.\ 63May include isr_tables.c etc." 64 ) 65set_property(GLOBAL PROPERTY GENERATED_KERNEL_SOURCE_FILES "") 66 67add_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. 88add_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 99file(TO_CMAKE_PATH "${ZEPHYR_BASE}" PROJECT_SOURCE_DIR) 100 101set(ZEPHYR_BINARY_DIR ${PROJECT_BINARY_DIR}) 102 103if(${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.") 107endif() 108 109add_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. 118zephyr_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 123set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE CACHE BOOL 124 "Export CMake compile commands. Used by gen_app_partitions.py script" 125 FORCE 126) 127 128project(Zephyr-Kernel VERSION ${PROJECT_VERSION}) 129 130# Add .S file extension suffix into CMAKE_ASM_SOURCE_FILE_EXTENSIONS, 131# because clang from OneApi can't recognize them as asm files on 132# windows now. 133list(APPEND CMAKE_ASM_SOURCE_FILE_EXTENSIONS "S") 134enable_language(ASM) 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. 144zephyr_check_compiler_flag(C "" toolchain_is_ok) 145assert(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") 147 148include(${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 153set(PROJECT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/zephyr) 154set(PROJECT_SOURCE_DIR ${ZEPHYR_BASE}) 155 156set(KERNEL_NAME ${CONFIG_KERNEL_BIN_NAME}) 157 158set(KERNEL_ELF_NAME ${KERNEL_NAME}.elf) 159set(KERNEL_BIN_NAME ${KERNEL_NAME}.bin) 160set(KERNEL_HEX_NAME ${KERNEL_NAME}.hex) 161set(KERNEL_UF2_NAME ${KERNEL_NAME}.uf2) 162set(KERNEL_MAP_NAME ${KERNEL_NAME}.map) 163set(KERNEL_LST_NAME ${KERNEL_NAME}.lst) 164set(KERNEL_S19_NAME ${KERNEL_NAME}.s19) 165set(KERNEL_EXE_NAME ${KERNEL_NAME}.exe) 166set(KERNEL_STAT_NAME ${KERNEL_NAME}.stat) 167set(KERNEL_STRIP_NAME ${KERNEL_NAME}.strip) 168set(KERNEL_META_NAME ${KERNEL_NAME}.meta) 169set(KERNEL_SYMBOLS_NAME ${KERNEL_NAME}.symbols) 170 171# Enable dynamic library support when required by LLEXT. 172if(CONFIG_LLEXT AND CONFIG_LLEXT_TYPE_ELF_SHAREDLIB) 173 set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE) 174endif() 175 176foreach(dir ${BOARD_DIRECTORIES}) 177 include(${dir}/board.cmake OPTIONAL) 178endforeach() 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. 184if(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() 206endif() 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 213add_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. 218zephyr_library_named(app) 219set_property(TARGET app PROPERTY ARCHIVE_OUTPUT_DIRECTORY app) 220 221add_subdirectory(${ZEPHYR_BASE} ${__build_dir}) 222 223# Link 'app' with the Zephyr interface libraries. 224# 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 227# done after 'add_subdirectory(${ZEPHYR_BASE} ${__build_dir})' 228# because interface libraries are defined while processing that 229# subdirectory. 230get_property(ZEPHYR_INTERFACE_LIBS_PROPERTY GLOBAL PROPERTY ZEPHYR_INTERFACE_LIBS) 231foreach(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 ) 244endforeach() 245 246if("${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) 252endif() 253