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)
145set(log_file "CMakeConfigureLog.yaml")
146if(CMAKE_VERSION VERSION_LESS "3.26.0")
147  set(log_file "CMakeError.log")
148endif()
149assert(toolchain_is_ok "The toolchain is unable to build a dummy C file.\
150 Move ${USER_CACHE_DIR}, re-run and look at ${log_file}")
151
152include(${ZEPHYR_BASE}/cmake/target_toolchain_flags.cmake)
153
154# 'project' sets PROJECT_BINARY_DIR to ${CMAKE_CURRENT_BINARY_DIR},
155# but for legacy reasons we need it to be set to
156# ${CMAKE_CURRENT_BINARY_DIR}/zephyr
157set(PROJECT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/zephyr)
158set(PROJECT_SOURCE_DIR ${ZEPHYR_BASE})
159
160set(KERNEL_NAME ${CONFIG_KERNEL_BIN_NAME})
161
162set(KERNEL_ELF_NAME   ${KERNEL_NAME}.elf)
163set(KERNEL_BIN_NAME   ${KERNEL_NAME}.bin)
164set(KERNEL_HEX_NAME   ${KERNEL_NAME}.hex)
165set(KERNEL_UF2_NAME   ${KERNEL_NAME}.uf2)
166set(KERNEL_MAP_NAME   ${KERNEL_NAME}.map)
167set(KERNEL_LST_NAME   ${KERNEL_NAME}.lst)
168set(KERNEL_S19_NAME   ${KERNEL_NAME}.s19)
169set(KERNEL_EXE_NAME   ${KERNEL_NAME}.exe)
170set(KERNEL_STAT_NAME  ${KERNEL_NAME}.stat)
171set(KERNEL_STRIP_NAME ${KERNEL_NAME}.strip)
172set(KERNEL_META_NAME  ${KERNEL_NAME}.meta)
173set(KERNEL_SYMBOLS_NAME    ${KERNEL_NAME}.symbols)
174
175# Enable dynamic library support when required by LLEXT.
176if(CONFIG_LLEXT AND CONFIG_LLEXT_TYPE_ELF_SHAREDLIB)
177  set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)
178endif()
179
180foreach(dir ${BOARD_DIRECTORIES})
181  include(${dir}/board.cmake OPTIONAL)
182endforeach()
183
184# If we are using a suitable ethernet driver inside qemu, then these options
185# must be set, otherwise a zephyr instance cannot receive any network packets.
186# The Qemu supported ethernet driver should define CONFIG_ETH_NIC_MODEL
187# string that tells what nic model Qemu should use.
188if(CONFIG_QEMU_TARGET)
189  if ((CONFIG_NET_QEMU_ETHERNET OR CONFIG_NET_QEMU_USER) AND NOT CONFIG_ETH_NIC_MODEL)
190    message(FATAL_ERROR "
191      No Qemu ethernet driver configured!
192      Enable Qemu supported ethernet driver like e1000 at drivers/ethernet"
193    )
194  elseif(CONFIG_NET_QEMU_ETHERNET)
195    if(CONFIG_ETH_QEMU_EXTRA_ARGS)
196      set(NET_QEMU_ETH_EXTRA_ARGS ",${CONFIG_ETH_QEMU_EXTRA_ARGS}")
197    endif()
198    list(APPEND QEMU_FLAGS_${ARCH}
199      -nic tap,model=${CONFIG_ETH_NIC_MODEL},script=no,downscript=no,ifname=${CONFIG_ETH_QEMU_IFACE_NAME}${NET_QEMU_ETH_EXTRA_ARGS}
200    )
201  elseif(CONFIG_NET_QEMU_USER)
202    list(APPEND QEMU_FLAGS_${ARCH}
203      -nic user,model=${CONFIG_ETH_NIC_MODEL},${CONFIG_NET_QEMU_USER_EXTRA_ARGS}
204    )
205  else()
206    list(APPEND QEMU_FLAGS_${ARCH}
207      -net none
208    )
209  endif()
210endif()
211
212# General purpose Zephyr target.
213# This target can be used for custom zephyr settings that needs to be used elsewhere in the build system
214#
215# Currently used properties:
216# - COMPILES_OPTIONS: Used by application memory partition feature
217add_custom_target(zephyr_property_target)
218
219# "app" is a CMake library containing all the application code and is
220# modified by the entry point ${APPLICATION_SOURCE_DIR}/CMakeLists.txt
221# that was specified when cmake was called.
222zephyr_library_named(app)
223set_property(TARGET app PROPERTY ARCHIVE_OUTPUT_DIRECTORY app)
224
225add_subdirectory(${ZEPHYR_BASE} ${__build_dir})
226
227# Link 'app' with the Zephyr interface libraries.
228#
229# NB: This must be done here because 'app' can only be modified in the
230# CMakeLists.txt file that created it. And it must be
231# done after 'add_subdirectory(${ZEPHYR_BASE} ${__build_dir})'
232# because interface libraries are defined while processing that
233# subdirectory.
234get_property(ZEPHYR_INTERFACE_LIBS_PROPERTY GLOBAL PROPERTY ZEPHYR_INTERFACE_LIBS)
235foreach(boilerplate_lib ${ZEPHYR_INTERFACE_LIBS_PROPERTY})
236  # Linking 'app' with 'boilerplate_lib' causes 'app' to inherit the INTERFACE
237  # properties of 'boilerplate_lib'. The most common property is 'include
238  # directories', but it is also possible to have defines and compiler
239  # flags in the interface of a library.
240  #
241  string(TOUPPER ${boilerplate_lib} boilerplate_lib_upper_case) # Support lowercase lib names
242  target_link_libraries_ifdef(
243    CONFIG_APP_LINK_WITH_${boilerplate_lib_upper_case}
244    app
245    PUBLIC
246    ${boilerplate_lib}
247    )
248endforeach()
249
250if("${CMAKE_EXTRA_GENERATOR}" STREQUAL "Eclipse CDT4")
251  # Call the amendment function before .project and .cproject generation
252  # C and CXX includes, defines in .cproject without __cplusplus
253  # with project includes and defines
254  include(${ZEPHYR_BASE}/cmake/ide/eclipse_cdt4_generator_amendment.cmake)
255  eclipse_cdt4_generator_amendment(1)
256endif()
257