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 build 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(C CXX 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 171include(${BOARD_DIR}/board.cmake OPTIONAL) 172 173# If we are using a suitable ethernet driver inside qemu, then these options 174# must be set, otherwise a zephyr instance cannot receive any network packets. 175# The Qemu supported ethernet driver should define CONFIG_ETH_NIC_MODEL 176# string that tells what nic model Qemu should use. 177if(CONFIG_QEMU_TARGET) 178 if ((CONFIG_NET_QEMU_ETHERNET OR CONFIG_NET_QEMU_USER) AND NOT CONFIG_ETH_NIC_MODEL) 179 message(FATAL_ERROR " 180 No Qemu ethernet driver configured! 181 Enable Qemu supported ethernet driver like e1000 at drivers/ethernet" 182 ) 183 elseif(CONFIG_NET_QEMU_ETHERNET) 184 if(CONFIG_ETH_QEMU_EXTRA_ARGS) 185 set(NET_QEMU_ETH_EXTRA_ARGS ",${CONFIG_ETH_QEMU_EXTRA_ARGS}") 186 endif() 187 list(APPEND QEMU_FLAGS_${ARCH} 188 -nic tap,model=${CONFIG_ETH_NIC_MODEL},script=no,downscript=no,ifname=${CONFIG_ETH_QEMU_IFACE_NAME}${NET_QEMU_ETH_EXTRA_ARGS} 189 ) 190 elseif(CONFIG_NET_QEMU_USER) 191 list(APPEND QEMU_FLAGS_${ARCH} 192 -nic user,model=${CONFIG_ETH_NIC_MODEL},${CONFIG_NET_QEMU_USER_EXTRA_ARGS} 193 ) 194 else() 195 list(APPEND QEMU_FLAGS_${ARCH} 196 -net none 197 ) 198 endif() 199endif() 200 201# General purpose Zephyr target. 202# This target can be used for custom zephyr settings that needs to be used elsewhere in the build system 203# 204# Currently used properties: 205# - COMPILES_OPTIONS: Used by application memory partition feature 206add_custom_target(zephyr_property_target) 207 208# "app" is a CMake library containing all the application code and is 209# modified by the entry point ${APPLICATION_SOURCE_DIR}/CMakeLists.txt 210# that was specified when cmake was called. 211zephyr_library_named(app) 212set_property(TARGET app PROPERTY ARCHIVE_OUTPUT_DIRECTORY app) 213 214add_subdirectory(${ZEPHYR_BASE} ${__build_dir}) 215 216# Link 'app' with the Zephyr interface libraries. 217# 218# NB: This must be done here because 'app' can only be modified in the 219# CMakeLists.txt file that created it. And it must be 220# done after 'add_subdirectory(${ZEPHYR_BASE} ${__build_dir})' 221# because interface libraries are defined while processing that 222# subdirectory. 223get_property(ZEPHYR_INTERFACE_LIBS_PROPERTY GLOBAL PROPERTY ZEPHYR_INTERFACE_LIBS) 224foreach(boilerplate_lib ${ZEPHYR_INTERFACE_LIBS_PROPERTY}) 225 # Linking 'app' with 'boilerplate_lib' causes 'app' to inherit the INTERFACE 226 # properties of 'boilerplate_lib'. The most common property is 'include 227 # directories', but it is also possible to have defines and compiler 228 # flags in the interface of a library. 229 # 230 string(TOUPPER ${boilerplate_lib} boilerplate_lib_upper_case) # Support lowercase lib names 231 target_link_libraries_ifdef( 232 CONFIG_APP_LINK_WITH_${boilerplate_lib_upper_case} 233 app 234 PUBLIC 235 ${boilerplate_lib} 236 ) 237endforeach() 238 239if("${CMAKE_EXTRA_GENERATOR}" STREQUAL "Eclipse CDT4") 240 # Call the amendment function before .project and .cproject generation 241 # C and CXX includes, defines in .cproject without __cplusplus 242 # with project includes and defines 243 include(${ZEPHYR_BASE}/cmake/ide/eclipse_cdt4_generator_amendment.cmake) 244 eclipse_cdt4_generator_amendment(1) 245endif() 246