blob: 88b2d77c3a4d5e638d1f69bb1f0275dd9aceb7c4 [file] [log] [blame]
Anas Nashif3ae52622019-04-06 09:08:09 -04001# SPDX-License-Identifier: Apache-2.0
2
Mark Ruvald Pedersen59036db2018-12-19 16:27:55 +01003# *DOCUMENTATION*
4#
5# Note that this is *NOT* the top-level CMakeLists.txt. That's in the
6# application. See the Application Development Primer documentation
7# for details.
8#
9# To see a list of typical targets execute "make usage"
10# More info can be located in ./README.rst
11# Comments in this file are targeted only to the developer, do not
12# expect to learn how to build the kernel reading this file.
13
Sebastian Bøeee9af862018-06-04 11:47:45 +020014if(NOT DEFINED ZEPHYR_BINARY_DIR)
Anas Nashiff2cb20c2019-06-18 14:45:40 -040015 message(FATAL_ERROR "A user error has occurred.
Sebastian Bøeee9af862018-06-04 11:47:45 +020016cmake was invoked with '${CMAKE_CURRENT_LIST_DIR}' specified as the source directory,
17but it must be invoked with an application source directory,
18such as '${CMAKE_CURRENT_LIST_DIR}/samples/hello_world'.
19Debug variables:
20CMAKE_CACHEFILE_DIR: ${CMAKE_CACHEFILE_DIR}
21")
22endif()
23
Marc Herbertd3d33942019-05-31 15:37:40 -070024
25# See https://gitlab.kitware.com/cmake/cmake/issues/16228
26# and https://cmake.org/pipermail/cmake/2019-May/thread.html#69496
27if(NOT ZEPHYR_BASE STREQUAL CMAKE_CURRENT_SOURCE_DIR)
28message(WARNING "ZEPHYR_BASE doesn't match CMAKE_CURRENT_SOURCE_DIR
29 ZEPHYR_BASE = ${ZEPHYR_BASE}
30 PWD = $ENV{PWD}
31 CMAKE_CURRENT_SOURCE_DIR = ${CMAKE_CURRENT_SOURCE_DIR}
32You may be using a mix of symbolic links and real paths which causes \
33subtle and hard to debug CMake issues.")
34endif()
35# For Zephyr more specifically this breaks (at least)
36# -fmacro-prefix-map=${ZEPHYR_BASE}=
37
Sebastian Bøe12f8f762017-10-27 15:43:34 +020038
Marc Herbert0370c9b2019-06-13 16:15:44 -070039# In some cases the "final" things are not used at all and "_prebuilt"
40# is the last station. See "logical_target_for_zephyr_elf" below for
41# details.
Sebastian Bøe12f8f762017-10-27 15:43:34 +020042set(CMAKE_EXECUTABLE_SUFFIX .elf)
Jordan Yates28b2e552021-10-20 20:19:28 +100043
44# Zephyr build system will use a dynamic number of linking stages based on build
45# configuration.
46#
47# Currently up to three linking stages may be executed:
48# zephyr_pre0: First linking stage
49# zephyr_pre1: Second linking stage
50# zephyr_final: Final linking stage
51#
52# There will at minimum be a single linking stage.
53# When only a single linking stage is required, the `zephyr_pre0` will be mapped
54# into the `zephyr_final` target.
55#
56# Multiple linking stages are required in the following cases:
Gerard Marull-Paretas48b201c2023-06-14 14:30:41 +020057# - device dependencies structs must be generated (CONFIG_DEVICE_DEPS=y)
Jordan Yates28b2e552021-10-20 20:19:28 +100058# - ISR tables must be generated (CONFIG_GEN_ISR_TABLES=y)
59# - Kernel objects hash tables (CONFIG_USERSPACE=y)
60# - Application memory partitions (CONFIG_USERSPACE=y)
61#
62# Some generators require that memory locations has been fixed, thus those are
63# placed at the second linking stage.
64#
65# When all three linking stages are active, then the following properties applies:
66# - zephyr_pre0: linker sections may resize / addresses may relocate
67# - zephyr_pre1: All linker section sizes are fixed, addresses cannot change
68# - zephyr_final: Final image.
69#
70set(ZEPHYR_CURRENT_LINKER_PASS 0)
71set(ZEPHYR_CURRENT_LINKER_CMD linker_zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS}.cmd)
72set(ZEPHYR_LINK_STAGE_EXECUTABLE zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS})
73
74# ZEPHYR_PREBUILT_EXECUTABLE is used outside of this file, therefore keep the
75# existing variable to allow slowly cleanup of linking stage handling.
76# Three stage linking active: pre0 -> pre1 -> final, this will correspond to `pre1`
77# Two stage linking active: pre0 -> final, this will correspond to `pre0`
Gerard Marull-Paretas48b201c2023-06-14 14:30:41 +020078if(CONFIG_USERSPACE OR CONFIG_DEVICE_DEPS)
Jordan Yates28b2e552021-10-20 20:19:28 +100079 set(ZEPHYR_PREBUILT_EXECUTABLE zephyr_pre1)
80else()
81 set(ZEPHYR_PREBUILT_EXECUTABLE zephyr_pre0)
82endif()
83set(ZEPHYR_FINAL_EXECUTABLE zephyr_final)
Sebastian Bøe12f8f762017-10-27 15:43:34 +020084
Mark Ruvald Pedersen11d6bae2019-04-29 16:57:37 +020085# Set some phony targets to collect dependencies
Sebastian Bøe1b86fb92019-01-14 16:39:33 +010086set(OFFSETS_H_TARGET offsets_h)
Sebastian Bøe1b86fb92019-01-14 16:39:33 +010087set(SYSCALL_LIST_H_TARGET syscall_list_h_target)
88set(DRIVER_VALIDATION_H_TARGET driver_validation_h_target)
89set(KOBJ_TYPES_H_TARGET kobj_types_h_target)
Andrew Boiec1c54b12020-03-16 12:48:00 -070090set(PARSE_SYSCALLS_TARGET parse_syscalls_target)
Sebastian Bøe1b86fb92019-01-14 16:39:33 +010091
Sebastian Bøe12f8f762017-10-27 15:43:34 +020092define_property(GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT BRIEF_DOCS " " FULL_DOCS " ")
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +010093set_property( GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT elf32-little${ARCH}) # BFD format
Sebastian Bøe12f8f762017-10-27 15:43:34 +020094
Daniel Leung80e78202023-05-25 11:41:48 -070095# Contains the list of files with syscall function prototypes.
96add_library(syscalls_interface INTERFACE)
97set(syscalls_file_list_output
98 ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_file_list.txt)
99
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +0100100# "zephyr_interface" is a source-less library that encapsulates all the global
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200101# compiler options needed by all source files. All zephyr libraries,
102# including the library named "zephyr" link with this library to
103# obtain these flags.
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +0100104# https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#interface-libraries
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200105add_library(zephyr_interface INTERFACE)
106
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +0100107# "zephyr" is a catch-all CMake library for source files that can be
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200108# built purely with the include paths, defines, and other compiler
109# flags that come with zephyr_interface.
110zephyr_library_named(zephyr)
111
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +0800112if(CONFIG_LEGACY_GENERATED_INCLUDE_PATH)
113 zephyr_include_directories(${PROJECT_BINARY_DIR}/include/generated/zephyr)
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +0800114endif()
115
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200116zephyr_include_directories(
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200117 include
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200118 ${PROJECT_BINARY_DIR}/include/generated
119 ${USERINCLUDE}
120 ${STDINCLUDE}
121)
122
Torsten Rasmussenc2842c32021-06-10 15:38:20 +0200123include(${ZEPHYR_BASE}/cmake/linker_script/${ARCH}/linker.cmake OPTIONAL)
124
Torsten Rasmussen8dc3f852022-09-14 22:23:15 +0200125zephyr_include_directories(${SOC_FULL_DIR})
Sebastian Bøe40363392019-01-25 10:14:13 +0100126
Torsten Rasmussen9a12f8b2022-08-22 16:45:23 +0200127# Don't inherit compiler flags from the environment
128foreach(var AFLAGS CFLAGS CXXFLAGS CPPFLAGS LDFLAGS)
129 if(DEFINED ENV{${var}})
130 message(WARNING "The environment variable '${var}' was set to $ENV{${var}}, "
131 "but Zephyr ignores flags from the environment. Use 'cmake "
132 "-DEXTRA_${var}=$ENV{${var}}' instead."
133 )
134 unset(ENV{${var}})
135 endif()
136endforeach()
137
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200138zephyr_compile_definitions(
139 KERNEL
140 __ZEPHYR__=1
Anas Nashif34aebad2018-01-03 12:26:19 -0500141)
142
Yuval Peress53ef68d2022-03-29 13:52:59 -0600143# Ensure that include/zephyr/toolchain.h includes toolchain/other.h for all off-tree toolchains
Danny Oerndrup94202f32021-07-16 11:50:40 +0200144if(TOOLCHAIN_USE_CUSTOM)
145 zephyr_compile_definitions(__TOOLCHAIN_CUSTOM__)
146endif()
147
Stephanos Ioannidis96728582022-08-11 03:06:46 +0900148# @Intent: Set compiler specific flag for disabling strict aliasing rule
149zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,no_strict_aliasing>>)
150zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,no_strict_aliasing>>)
151
Nikolay Agishev0dec4cf2022-12-22 15:46:04 +0400152# Extra warnings options for twister run
153if (CONFIG_COMPILER_WARNINGS_AS_ERRORS)
154 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warnings_as_errors>>)
Fabio Baltieri56dcafe2024-09-13 19:59:40 +0000155 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler,warnings_as_errors>>)
Nikolay Agishev0dec4cf2022-12-22 15:46:04 +0400156 zephyr_compile_options($<$<COMPILE_LANGUAGE:ASM>:$<TARGET_PROPERTY:asm,warnings_as_errors>>)
157 zephyr_link_libraries($<TARGET_PROPERTY:linker,warnings_as_errors>)
158endif()
159
Mark Ruvald Pedersen01592072019-01-10 12:07:51 +0100160# @Intent: Set compiler flags to enable buffer overflow checks in libc functions
Keith Packard62bc9bf2022-04-26 19:24:11 -0700161# @details:
162# Kconfig.zephyr "Detect buffer overflows in libc calls" is a kconfig choice,
163# ensuring at most *one* of CONFIG_FORTIFY_SOURCE_{COMPILE_TIME,RUN_TIME} is
164# set. Refer to Kconfig.zephyr for selection logic and description of these
165# choices. Toolchains set both of the security_fortify_{compile_time,run_time}
166# properties and the Kconfig settings are used here to select between those.
167#
168if(CONFIG_FORTIFY_SOURCE_RUN_TIME)
169 zephyr_compile_definitions($<TARGET_PROPERTY:compiler,security_fortify_run_time> )
170elseif(CONFIG_FORTIFY_SOURCE_COMPILE_TIME)
171 zephyr_compile_definitions($<TARGET_PROPERTY:compiler,security_fortify_compile_time> )
172endif()
Mark Ruvald Pedersen01592072019-01-10 12:07:51 +0100173
174# @Intent: Set compiler flags to detect general stack overflows across all functions
175if(CONFIG_STACK_CANARIES)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200176 zephyr_compile_options($<TARGET_PROPERTY:compiler,security_canaries>)
Anas Nashif34aebad2018-01-03 12:26:19 -0500177endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200178
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100179# @Intent: Obtain compiler optimizations flags and store in variables
180# @details:
181# Kconfig.zephyr "Optimization level" is a kconfig choice, ensuring
182# only *one* of CONFIG_{NO,DEBUG,SPEED,SIZE}_OPTIMIZATIONS is set.
183# Refer to Kconfig.zephyr for selection logic and description of these choices.
184# toolchain_cc_optimize_*() macros must provide the mapping from these kconfigs
185# to compiler flags. Each macro will store the flags in a CMake variable, whose
186# name is passed as argument (somewhat like by reference).
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200187#
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100188# If the user wants to tweak the optimizations, there are two ways:
189# 1) Using EXTRA_CFLAGS which is applied regardless of kconfig choice, or
190# 2) Rely on override support being implemented by your toolchain_cc_optimize_*()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200191#
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200192get_property(OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG TARGET compiler PROPERTY no_optimization)
193get_property(OPTIMIZE_FOR_DEBUG_FLAG TARGET compiler PROPERTY optimization_debug)
194get_property(OPTIMIZE_FOR_SPEED_FLAG TARGET compiler PROPERTY optimization_speed)
195get_property(OPTIMIZE_FOR_SIZE_FLAG TARGET compiler PROPERTY optimization_size)
Jonathon Penixd6041d62024-03-20 10:29:51 -0700196get_property(OPTIMIZE_FOR_SIZE_AGGRESSIVE_FLAG TARGET compiler PROPERTY optimization_size_aggressive)
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100197
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100198# From kconfig choice, pick the actual OPTIMIZATION_FLAG to use.
199# Kconfig choice ensures only one of these CONFIG_*_OPTIMIZATIONS is set.
Alberto Escolar Piedrasf60527a2018-01-22 15:35:54 +0100200if(CONFIG_NO_OPTIMIZATIONS)
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100201 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG})
202elseif(CONFIG_DEBUG_OPTIMIZATIONS)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200203 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_DEBUG_FLAG})
Aurelien Jarnoe8413d12018-06-16 23:40:04 +0200204elseif(CONFIG_SPEED_OPTIMIZATIONS)
205 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SPEED_FLAG})
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100206elseif(CONFIG_SIZE_OPTIMIZATIONS)
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100207 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SIZE_FLAG}) # Default in kconfig
Jonathon Penixd6041d62024-03-20 10:29:51 -0700208elseif(CONFIG_SIZE_OPTIMIZATIONS_AGGRESSIVE)
209 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SIZE_AGGRESSIVE_FLAG})
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100210else()
Pieter De Gendt32c96142024-04-04 10:34:46 +0200211 message(FATAL_ERROR
212 "Unreachable code. Expected optimization level to have been chosen. See Kconfig.zephyr")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200213endif()
214
Ulf Magnussonde42aea2020-02-07 00:48:22 +0100215if(NOT CONFIG_ARCH_IS_SET)
216 message(WARNING "\
217None of the CONFIG_<arch> (e.g. CONFIG_X86) symbols are set. \
218Select one of them from the SOC_SERIES_* symbol or, lacking that, from the \
219SOC_* symbol.")
220endif()
221
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100222# Apply the final optimization flag(s)
223zephyr_compile_options(${OPTIMIZATION_FLAG})
224
Radoslaw Koppel26c87762024-01-30 12:32:32 +0100225if(CONFIG_LTO)
Radoslaw Koppel118f1592024-02-27 13:17:37 +0100226 zephyr_compile_options($<TARGET_PROPERTY:compiler,optimization_lto>)
Radoslaw Koppel26c87762024-01-30 12:32:32 +0100227 add_link_options($<TARGET_PROPERTY:linker,lto_arguments>)
228endif()
229
Pieter De Gendt294939a2024-05-07 15:44:30 +0200230if(CONFIG_STD_C23)
231 set(CSTD c2x)
232elseif(CONFIG_STD_C17)
233 set(CSTD c17)
234elseif(CONFIG_STD_C11)
235 set(CSTD c11)
236elseif(CONFIG_STD_C99)
237 set(CSTD c99)
238elseif(CONFIG_STD_C90)
239 set(CSTD c90)
240else()
241 message(FATAL_ERROR "Unreachable code. Expected C standard to have been chosen.")
242endif()
243
244if(CONFIG_GNU_C_EXTENSIONS)
245 string(REPLACE "c" "gnu" CSTD "${CSTD}")
246endif()
247
248list(APPEND CMAKE_C_COMPILE_FEATURES ${compile_features_${CSTD}})
249
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100250# @Intent: Obtain compiler specific flags related to C++ that are not influenced by kconfig
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200251zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,required>>)
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200252
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100253# @Intent: Obtain compiler specific flags for compiling under different ISO standards of C++
Stephanos Ioannidis4a64bfe2022-12-09 06:16:44 +0900254if(CONFIG_CPP)
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200255 # From kconfig choice, pick a single dialect.
256 # Kconfig choice ensures only one of these CONFIG_STD_CPP* is set.
257 if(CONFIG_STD_CPP98)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200258 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp98>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200259 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp98})
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200260 elseif(CONFIG_STD_CPP11)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200261 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp11>) # Default in kconfig
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200262 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp11})
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200263 elseif(CONFIG_STD_CPP14)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200264 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp14>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200265 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp14})
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200266 elseif(CONFIG_STD_CPP17)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200267 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp17>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200268 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp17})
Alexander Wachterad130f22021-07-14 10:50:21 +0200269 elseif(CONFIG_STD_CPP2A)
270 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp2a>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200271 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp20})
Dan Kalowskyc0811e92021-07-09 10:46:46 -0700272 elseif(CONFIG_STD_CPP20)
273 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp20>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200274 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp20})
Dan Kalowsky9b333912021-07-09 10:54:11 -0700275 elseif(CONFIG_STD_CPP2B)
276 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp2b>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200277 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp20})
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200278 else()
Pieter De Gendt32c96142024-04-04 10:34:46 +0200279 message(FATAL_ERROR
280 "Unreachable code. Expected C++ standard to have been chosen. See Kconfig.zephyr.")
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200281 endif()
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200282 set(CMAKE_CXX_COMPILE_FEATURES ${CMAKE_CXX_COMPILE_FEATURES} PARENT_SCOPE)
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200283
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200284 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:${STD_CPP_DIALECT_FLAGS}>)
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200285endif()
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100286
Stephanos Ioannidis404e7a92022-12-09 19:43:43 +0900287if(NOT CONFIG_CPP_EXCEPTIONS)
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100288 # @Intent: Obtain compiler specific flags related to C++ Exceptions
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200289 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,no_exceptions>>)
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100290endif()
291
Stephanos Ioannidisa3b28ff2022-12-09 20:06:15 +0900292if(NOT CONFIG_CPP_RTTI)
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100293 # @Intent: Obtain compiler specific flags related to C++ Run Time Type Information
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200294 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,no_rtti>>)
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100295endif()
296
Andy Rossfe04adf2019-02-27 11:53:18 -0800297if(CONFIG_MISRA_SANE)
Danny Oerndrup8e5a95e2019-05-16 12:53:58 +0200298 # @Intent: Obtain toolchain compiler flags relating to MISRA.
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200299 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_misra_sane>>)
300 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_error_misra_sane>>)
Andy Rossfe04adf2019-02-27 11:53:18 -0800301endif()
302
Flavio Ceolinb587e8d2020-08-26 09:48:33 -0700303# This is intend to be temporary. Once we have fixed the violations that
304# prevents build Zephyr, these flags shall be part of the default flags.
305if(CONFIG_CODING_GUIDELINE_CHECK)
306 # @Intent: Obtain toolchain compiler flags relating to coding guideline
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200307 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_coding_guideline>>)
Flavio Ceolinb587e8d2020-08-26 09:48:33 -0700308endif()
309
Danny Oerndrup4ddbc002019-06-11 13:55:53 +0200310# @Intent: Set compiler specific macro inclusion of AUTOCONF_H
Torsten Rasmussenf109e682020-08-13 15:49:46 +0200311zephyr_compile_options("SHELL: $<TARGET_PROPERTY:compiler,imacros> ${AUTOCONF_H}")
Danny Oerndrup4ddbc002019-06-11 13:55:53 +0200312
Keith Packard6c5d8062023-02-08 14:56:50 -0800313if(CONFIG_COMPILER_FREESTANDING)
Keith Packardd0c75f32020-10-26 19:07:50 -0700314 # @Intent: Set compiler specific flag for bare metal freestanding option
Jaroslaw Stelter69913ad2022-07-04 17:22:18 +0200315 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,freestanding>>)
316 zephyr_compile_options($<$<COMPILE_LANGUAGE:ASM>:$<TARGET_PROPERTY:compiler,freestanding>>)
Keith Packardd0c75f32020-10-26 19:07:50 -0700317endif()
Danny Oerndrupfaa72b72019-06-11 15:56:57 +0200318
Keith Packardb3073f02022-10-13 15:04:00 -0700319if (CONFIG_PICOLIBC AND NOT CONFIG_PICOLIBC_IO_FLOAT)
320 # @Intent: Set compiler specific flag to disable printf-related optimizations
321 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,no_printf_return_value>>)
322endif()
323
Danny Oerndrupe34ed7c2019-06-12 14:56:46 +0200324# @Intent: Set compiler specific flag for tentative definitions, no-common
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200325zephyr_compile_options($<TARGET_PROPERTY:compiler,no_common>)
Danny Oerndrupe34ed7c2019-06-12 14:56:46 +0200326
Danny Oerndrupe0569ac2019-07-23 09:00:55 +0200327# @Intent: Set compiler specific flag for production of debug information
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200328zephyr_compile_options($<TARGET_PROPERTY:compiler,debug>)
Danny Oerndrupe0569ac2019-07-23 09:00:55 +0200329
Fabio Baltieri3f8f7132023-03-29 10:11:46 +0100330if(CONFIG_COMPILER_SAVE_TEMPS)
331 # @Intent: Set compiler specific flag for saving temporary object files
332 zephyr_compile_options($<TARGET_PROPERTY:compiler,save_temps>)
333endif()
334
Gerard Marull-Paretas99ebe392023-05-05 09:58:12 +0200335if(NOT CONFIG_COMPILER_TRACK_MACRO_EXPANSION)
336 # @Intent: Set compiler specific flags to not track macro expansion
337 zephyr_compile_options($<TARGET_PROPERTY:compiler,no_track_macro_expansion>)
338endif()
339
Arvin Farahmande430b7b2021-04-15 11:20:10 -0400340if(CONFIG_COMPILER_COLOR_DIAGNOSTICS)
Arvin Farahmandb8f59682021-04-15 11:20:10 -0400341# @Intent: Set compiler specific flag for diagnostic messages
342zephyr_compile_options($<TARGET_PROPERTY:compiler,diagnostic>)
343endif()
344
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200345zephyr_compile_options(
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530346 ${TOOLCHAIN_C_FLAGS}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200347)
348
Mark Ruvald Pedersencb0fd452019-01-30 21:48:25 +0100349# @Intent: Obtain compiler specific flags related to assembly
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200350# ToDo: Remember to get feedback from Oticon on this, as they might use the `ASM_BASE_FLAG` since this is done this way.
351zephyr_compile_options($<$<COMPILE_LANGUAGE:ASM>:$<TARGET_PROPERTY:asm,required>>)
Mark Ruvald Pedersencb0fd452019-01-30 21:48:25 +0100352
Nazar Kazakovf483b1b2022-03-16 21:07:43 +0000353# @Intent: Enforce standard integer type correspondence to match Zephyr usage.
Nicolas Pitreb86aa652019-07-02 16:22:04 -0400354# (must be after compiler specific flags)
Nicolas Pitre0a386db2022-06-20 13:18:14 -0400355if(CONFIG_ENFORCE_ZEPHYR_STDINT)
Yuval Peress53ef68d2022-03-29 13:52:59 -0600356 zephyr_compile_options("SHELL: $<TARGET_PROPERTY:compiler,imacros> ${ZEPHYR_BASE}/include/zephyr/toolchain/zephyr_stdint.h")
Stephanos Ioannidisa1a66192021-09-12 19:33:15 +0900357endif()
Nicolas Pitreb86aa652019-07-02 16:22:04 -0400358
Mark Ruvald Pedersencb0fd452019-01-30 21:48:25 +0100359# Common toolchain-agnostic assembly flags
360zephyr_compile_options(
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200361 $<$<COMPILE_LANGUAGE:ASM>:-D_ASMLANGUAGE>
362)
363
Torsten Rasmussen5db1f1a2024-09-02 13:49:18 +0200364find_package(Deprecated COMPONENTS toolchain_ld_base)
Aurelien Jarnoc6727d42018-11-26 13:48:34 +0100365
Torsten Rasmussen9a9e2522024-08-30 22:15:40 +0200366if(DEFINED TOOLCHAIN_LD_FLAGS)
367 zephyr_ld_options(${TOOLCHAIN_LD_FLAGS})
368endif()
369
370zephyr_link_libraries(PROPERTY base)
371
372zephyr_link_libraries_ifndef(CONFIG_LINKER_USE_RELAX PROPERTY no_relax)
373
374zephyr_link_libraries_ifdef(CONFIG_LINKER_USE_RELAX PROPERTY relax)
375
376# Sort the common symbols and each input section by alignment
377# in descending order to minimize padding between these symbols.
378zephyr_link_libraries_ifdef(CONFIG_LINKER_SORT_BY_ALIGNMENT PROPERTY sort_alignment)
379
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +0200380toolchain_ld_force_undefined_symbols(
381 _OffsetAbsSyms
382 _ConfigAbsSyms
383)
384
Alberto Escolar Piedras56dc20e2023-05-26 13:23:43 +0200385if(NOT CONFIG_NATIVE_BUILD)
Torsten Rasmussen5db1f1a2024-09-02 13:49:18 +0200386 find_package(Deprecated COMPONENTS toolchain_ld_baremetal)
Torsten Rasmussen5badbfe2024-09-02 13:14:43 +0200387
388 zephyr_link_libraries(PROPERTY baremetal)
389
390 # Note that some architectures will skip this flag if set to error, even
391 # though the compiler flag check passes (e.g. ARC and Xtensa). So warning
392 # should be the default for now.
393 #
394 # Skip this for native application as Zephyr only provides
395 # additions to the host toolchain linker script. The relocation
396 # sections (.rel*) requires us to override those provided
397 # by host toolchain. As we can't account for all possible
398 # combination of compiler and linker on all machines used
399 # for development, it is better to turn this off.
400 #
401 # CONFIG_LINKER_ORPHAN_SECTION_PLACE is to place the orphan sections
402 # without any warnings or errors, which is the default behavior.
403 # So there is no need to explicitly set a linker flag.
404 if(CONFIG_LINKER_ORPHAN_SECTION_WARN)
405 zephyr_link_libraries(PROPERTY orphan_warning)
406 elseif(CONFIG_LINKER_ORPHAN_SECTION_ERROR)
407 zephyr_link_libraries(PROPERTY orphan_error)
408 endif()
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200409endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200410
Torsten Rasmussen9a9e2522024-08-30 22:15:40 +0200411if(CONFIG_CPP)
412 if(NOT CONFIG_MINIMAL_LIBCPP AND NOT CONFIG_NATIVE_LIBRARY)
Torsten Rasmussen5db1f1a2024-09-02 13:49:18 +0200413 find_package(Deprecated COMPONENTS toolchain_ld_cpp)
Torsten Rasmussen9a9e2522024-08-30 22:15:40 +0200414 endif()
415
416 zephyr_link_libraries(PROPERTY cpp_base)
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200417endif()
418
Danny Oerndrup8eaa9062019-05-16 12:49:31 +0200419# @Intent: Add the basic toolchain warning flags
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200420zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_base>>)
421zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_base>>)
Danny Oerndrupbdb229f2019-05-06 15:19:27 +0200422
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200423# ==========================================================================
424#
425# cmake -DW=... settings
426#
427# W=1 - warnings that may be relevant and does not occur too often
428# W=2 - warnings that occur quite often but may still be relevant
429# W=3 - the more obscure warnings, can most likely be ignored
430# ==========================================================================
Danny Oerndrup8eaa9062019-05-16 12:49:31 +0200431# @Intent: Add cmake -DW toolchain supported warnings, if any
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200432if(W MATCHES "1")
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200433 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_dw_1>>)
434 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_dw_1>>)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200435endif()
436
437if(W MATCHES "2")
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200438 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_dw_2>>)
439 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_dw_2>>)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200440endif()
441
442if(W MATCHES "3")
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200443 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_dw_3>>)
444 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_dw_3>>)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200445endif()
446
Danny Oerndrup8eaa9062019-05-16 12:49:31 +0200447# @Intent: Add extended, more specific, toolchain warning flags
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200448zephyr_compile_options($<TARGET_PROPERTY:compiler,warning_extended>)
Benoit Leforestier04dad592019-01-25 13:57:03 +0100449
Danny Oerndrup025ffa22019-05-16 12:58:40 +0200450# @Intent: Trigger an error when a declaration does not specify a type
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200451zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_implicit_int>>)
452zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_error_implicit_int>>)
Danny Oerndrup025ffa22019-05-16 12:58:40 +0200453
Flavio Ceolin82599312022-08-22 08:47:03 -0700454# @Intent: Do not make position independent code / executable
455zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,no_position_independent>>)
Alberto Escolar Piedras01348d32023-05-04 13:43:41 +0200456zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler,no_position_independent>>)
Alberto Escolar Piedrasfeaf0ff2023-06-07 10:28:22 +0200457
458# In case of CONFIG_NATIVE_LIBRARY we also don't want position independent code,
459# but we cannot tell that to the linker yet as we are first only doing a
460# relocatable link into a static library
461zephyr_link_libraries_ifndef(CONFIG_NATIVE_LIBRARY
462 $<TARGET_PROPERTY:linker,no_position_independent>)
Flavio Ceolin82599312022-08-22 08:47:03 -0700463
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200464# Allow the user to inject options when calling cmake, e.g.
465# 'cmake -DEXTRA_CFLAGS="-Werror -Wno-deprecated-declarations" ..'
Sebastian Bøe9f590452017-11-10 12:22:23 +0100466include(cmake/extra_flags.cmake)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200467
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200468zephyr_cc_option(-fno-asynchronous-unwind-tables)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200469
Flavio Ceolinac5d45a2023-01-22 12:47:36 -0800470if(CONFIG_USERSPACE)
471 zephyr_compile_options($<TARGET_PROPERTY:compiler,no_global_merge>)
472endif()
473
Daniel Leung02b20352020-09-28 11:27:11 -0700474if(CONFIG_THREAD_LOCAL_STORAGE)
475# Only support local exec TLS model at this point.
476zephyr_cc_option(-ftls-model=local-exec)
477endif()
478
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200479if(CONFIG_OVERRIDE_FRAME_POINTER_DEFAULT)
480 if(CONFIG_OMIT_FRAME_POINTER)
481 zephyr_cc_option(-fomit-frame-pointer)
482 else()
483 zephyr_cc_option(-fno-omit-frame-pointer)
484 endif()
485endif()
486
Sebastian Bøe244451b2019-02-27 08:28:25 +0100487separate_arguments(COMPILER_OPT_AS_LIST UNIX_COMMAND ${CONFIG_COMPILER_OPT})
488zephyr_compile_options(${COMPILER_OPT_AS_LIST})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200489
490# TODO: Include arch compiler options at this point.
491
Kumar Gala2d127662023-01-20 23:21:23 +0000492if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang" AND
Kumar Gala9f8913c2023-03-16 14:58:27 +0000493 NOT CMAKE_C_COMPILER_ID STREQUAL "IntelLLVM" AND
494 NOT CMAKE_C_COMPILER_ID STREQUAL "ARMClang")
Danny Oerndrupcbbbdea2019-05-06 15:21:58 +0200495 # GCC assumed
496 zephyr_cc_option(-fno-reorder-functions)
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530497
Stephanos Ioannidis5af932f2022-08-15 22:44:06 +0900498 # GCC 11 and above may generate a warning when dereferencing a constant
499 # address pointer whose address is below the value specified by the
500 # `min-pagesize` parameter (defaults to 0x1000). The `min-pagesize` parameter
501 # is set to 0 such that GCC never generates any warnings for the constant
502 # address pointers. For more details, refer to the GCC PR99578.
503 zephyr_cc_option(--param=min-pagesize=0)
504
Anas Nashif7ee8bb92018-02-11 14:36:21 -0600505 if(NOT ${ZEPHYR_TOOLCHAIN_VARIANT} STREQUAL "xcc")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200506 zephyr_cc_option(-fno-defer-pop)
507 endif()
Nicolas Pitref0057352022-04-06 13:36:40 -0400508else()
509 # Clang produces false positive vla warnings
510 zephyr_cc_option(-Wno-vla)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200511endif()
512
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200513zephyr_cc_option_ifdef(CONFIG_STACK_USAGE -fstack-usage)
514
Marc Herbert28a56572019-04-11 16:34:04 -0700515# If the compiler supports it, strip the ${ZEPHYR_BASE} prefix from the
516# __FILE__ macro used in __ASSERT*, in the
517# .noinit."/home/joe/zephyr/fu/bar.c" section names and in any
518# application code. This saves some memory, stops leaking user locations
519# in binaries, makes failure logs more deterministic and most
520# importantly makes builds more deterministic
Martin Jägera0ffaa72023-03-29 16:06:30 +0200521if(CONFIG_BUILD_OUTPUT_STRIP_PATHS)
522 # If several match then the last one wins. This matters for instances
523 # like tests/ and samples/: they're inside all of them! Then let's
524 # strip as little as possible.
525 zephyr_cc_option(-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=CMAKE_SOURCE_DIR)
526 zephyr_cc_option(-fmacro-prefix-map=${ZEPHYR_BASE}=ZEPHYR_BASE)
527 if(WEST_TOPDIR)
528 zephyr_cc_option(-fmacro-prefix-map=${WEST_TOPDIR}=WEST_TOPDIR)
529 endif()
Marc Herberteddbf3c2019-06-11 16:57:37 -0700530endif()
Marc Herbert28a56572019-04-11 16:34:04 -0700531
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200532# TODO: Archiver arguments
533# ar_option(D)
534
Håkon Øye Amundsend6551b52018-11-29 09:08:08 +0000535# Declare MPU userspace dependencies before the linker scripts to make
536# sure the order of dependencies are met
Andrew Boie41f60112019-01-31 15:53:24 -0800537if(CONFIG_USERSPACE)
Daniel Leung2117a2a2021-07-12 13:33:32 -0700538 add_custom_target(app_smem)
Daniel Leung212ec9a2019-03-10 14:20:21 -0700539 set(APP_SMEM_ALIGNED_DEP app_smem_aligned_linker)
540 set(APP_SMEM_UNALIGNED_DEP app_smem_unaligned_linker)
Håkon Øye Amundsend6551b52018-11-29 09:08:08 +0000541endif()
542
Daniel Leung11171692021-03-18 14:00:07 -0700543if(CONFIG_USERSPACE)
544 set(KOBJECT_LINKER_DEP kobject_linker)
545endif()
546
Torsten Rasmussen91709772022-02-04 10:27:13 +0100547if(DEFINED BUILD_VERSION)
548 set(build_version_argument "-DBUILD_VERSION=${BUILD_VERSION}")
Frank Terbecka85a76d2022-03-08 20:09:51 +0100549elseif(NOT ZEPHYR_GIT_INDEX)
Frank Terbecka85a76d2022-03-08 20:09:51 +0100550 if(EXISTS ${ZEPHYR_BASE}/.git/index)
Torsten Rasmussen12c1fe42023-10-06 12:20:08 +0200551 set(ZEPHYR_GIT_INDEX ${ZEPHYR_BASE}/.git/index CACHE PATH
552 "Path to Zephyr git repository index file")
Frank Terbecka85a76d2022-03-08 20:09:51 +0100553 elseif(EXISTS ${ZEPHYR_BASE}/.git)
554 # Likely a git-submodule. Let's ask git where the real database is located.
555 find_package(Git QUIET)
556 if(GIT_FOUND)
557 execute_process(
558 COMMAND ${GIT_EXECUTABLE} rev-parse --absolute-git-dir
559 WORKING_DIRECTORY ${ZEPHYR_BASE}
560 OUTPUT_VARIABLE zephyr_git_dir
561 OUTPUT_STRIP_TRAILING_WHITESPACE
562 ERROR_STRIP_TRAILING_WHITESPACE
563 ERROR_VARIABLE stderr
564 RESULT_VARIABLE return_code)
565 if(return_code)
566 message(WARNING "BUILD_VERSION: git rev-parse failed: ${stderr}")
567 else()
568 if(NOT "${stderr}" STREQUAL "")
569 message(WARNING "BUILD_VERSION: git rev-parse warned: ${stderr}")
570 endif()
571 set(ZEPHYR_GIT_INDEX ${zephyr_git_dir}/index CACHE PATH
Torsten Rasmussen12c1fe42023-10-06 12:20:08 +0200572 "Path to Zephyr git repository index file")
Frank Terbecka85a76d2022-03-08 20:09:51 +0100573 endif()
574 else()
575 message(WARNING "Could not find git installation, "
576 "please specify '-DBUILD_VERSION=<version>'")
577 endif()
578 else()
579 message(WARNING "ZEPHYR_BASE=${ZEPHYR_BASE} doesn't appear to be a git "
580 "repository, please specify '-DBUILD_VERSION=<version>'")
581 endif()
Torsten Rasmussen91709772022-02-04 10:27:13 +0100582endif()
Frank Terbecka85a76d2022-03-08 20:09:51 +0100583
584if(ZEPHYR_GIT_INDEX)
585 set(git_dependency ${ZEPHYR_GIT_INDEX})
586endif()
587
Torsten Rasmussen91709772022-02-04 10:27:13 +0100588add_custom_command(
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +0800589 OUTPUT ${PROJECT_BINARY_DIR}/include/generated/zephyr/version.h
Torsten Rasmussen91709772022-02-04 10:27:13 +0100590 COMMAND ${CMAKE_COMMAND} -DZEPHYR_BASE=${ZEPHYR_BASE}
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +0800591 -DOUT_FILE=${PROJECT_BINARY_DIR}/include/generated/zephyr/version.h
Torsten Rasmussen2c757f92023-03-23 08:43:01 +0100592 -DVERSION_TYPE=KERNEL
593 -DVERSION_FILE=${ZEPHYR_BASE}/VERSION
Torsten Rasmussenf9f07482023-09-07 12:47:44 +0200594 -DKERNEL_VERSION_CUSTOMIZATION="$<TARGET_PROPERTY:version_h,KERNEL_VERSION_CUSTOMIZATION>"
Torsten Rasmussen91709772022-02-04 10:27:13 +0100595 ${build_version_argument}
596 -P ${ZEPHYR_BASE}/cmake/gen_version_h.cmake
597 DEPENDS ${ZEPHYR_BASE}/VERSION ${git_dependency}
Torsten Rasmussenf9f07482023-09-07 12:47:44 +0200598 COMMAND_EXPAND_LISTS
Torsten Rasmussen91709772022-02-04 10:27:13 +0100599)
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +0800600add_custom_target(version_h DEPENDS ${PROJECT_BINARY_DIR}/include/generated/zephyr/version.h)
Torsten Rasmussenf2ea8502024-11-01 15:22:23 +0100601zephyr_get(KERNEL_VERSION_CUSTOMIZATION SYSBUILD LOCAL)
602set_property(TARGET version_h PROPERTY KERNEL_VERSION_CUSTOMIZATION ${KERNEL_VERSION_CUSTOMIZATION})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200603
Torsten Rasmussen99064c22023-03-23 08:45:26 +0100604if(EXISTS ${APPLICATION_SOURCE_DIR}/VERSION)
605 add_custom_command(
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +0800606 OUTPUT ${PROJECT_BINARY_DIR}/include/generated/zephyr/app_version.h
Torsten Rasmussen99064c22023-03-23 08:45:26 +0100607 COMMAND ${CMAKE_COMMAND} -DZEPHYR_BASE=${ZEPHYR_BASE}
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +0800608 -DOUT_FILE=${PROJECT_BINARY_DIR}/include/generated/zephyr/app_version.h
Torsten Rasmussen99064c22023-03-23 08:45:26 +0100609 -DVERSION_TYPE=APP
610 -DVERSION_FILE=${APPLICATION_SOURCE_DIR}/VERSION
Torsten Rasmussenf9f07482023-09-07 12:47:44 +0200611 -DAPP_VERSION_CUSTOMIZATION="$<TARGET_PROPERTY:app_version_h,APP_VERSION_CUSTOMIZATION>"
Torsten Rasmussen99064c22023-03-23 08:45:26 +0100612 ${build_version_argument}
613 -P ${ZEPHYR_BASE}/cmake/gen_version_h.cmake
614 DEPENDS ${APPLICATION_SOURCE_DIR}/VERSION ${git_dependency}
Torsten Rasmussenf9f07482023-09-07 12:47:44 +0200615 COMMAND_EXPAND_LISTS
Torsten Rasmussen99064c22023-03-23 08:45:26 +0100616 )
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +0800617 add_custom_target(
618 app_version_h
619 DEPENDS ${PROJECT_BINARY_DIR}/include/generated/zephyr/app_version.h)
Torsten Rasmussen99064c22023-03-23 08:45:26 +0100620 add_dependencies(zephyr_interface app_version_h)
Torsten Rasmussenf2ea8502024-11-01 15:22:23 +0100621 zephyr_get(APP_VERSION_CUSTOMIZATION SYSBUILD LOCAL)
622 set_property(TARGET app_version_h PROPERTY APP_VERSION_CUSTOMIZATION ${APP_VERSION_CUSTOMIZATION})
Torsten Rasmussen99064c22023-03-23 08:45:26 +0100623endif()
624
Sebastian Bøe6f946e22018-01-09 10:52:57 +0100625# Unfortunately, the order in which CMakeLists.txt code is processed
626# matters so we need to be careful about how we order the processing
627# of subdirectories. One example is "Compiler flags added late in the
628# build are not exported to external build systems #5605"; when we
629# integrate with an external build system we read out all compiler
630# flags when the external project is created. So an external project
631# defined in subsys or ext will not get global flags added by drivers/
632# or tests/ as the subdirectories are ordered now.
633#
634# Another example of when the order matters is the reading and writing
635# of global properties such as ZEPHYR_LIBS or
636# GENERATED_KERNEL_OBJECT_FILES.
637#
638# Arch is placed early because it defines important compiler flags
639# that must be exported to external build systems defined in
640# e.g. subsys/.
641add_subdirectory(arch)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200642add_subdirectory(lib)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200643# We use include instead of add_subdirectory to avoid creating a new directory scope.
644# This is because source file properties are directory scoped, including the GENERATED
645# property which is set implicitly for custom command outputs
646include(misc/generated/CMakeLists.txt)
Anas Nashif3d1252f2018-09-03 15:20:14 -0500647
Daniel Leunge38fc6d2023-08-11 13:40:05 -0700648add_subdirectory(soc)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200649add_subdirectory(boards)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200650add_subdirectory(subsys)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200651add_subdirectory(drivers)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200652
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100653# Include zephyr modules generated CMake file.
Torsten Rasmussen2fc062b2020-08-24 11:01:04 +0200654foreach(module_name ${ZEPHYR_MODULE_NAMES})
655 # Note the second, binary_dir parameter requires the added
656 # subdirectory to have its own, local cmake target(s). If not then
657 # this binary_dir is created but stays empty. Object files land in
658 # the main binary dir instead.
659 # https://cmake.org/pipermail/cmake/2019-June/069547.html
Torsten Rasmussen3d880832021-01-19 12:01:38 +0100660 zephyr_string(SANITIZE TOUPPER MODULE_NAME_UPPER ${module_name})
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200661 if(NOT ${ZEPHYR_${MODULE_NAME_UPPER}_CMAKE_DIR} STREQUAL "")
Pieter De Gendtfcaed382024-01-16 19:16:23 +0100662 set(ZEPHYR_CURRENT_MODULE_NAME ${ZEPHYR_${MODULE_NAME_UPPER}_MODULE_NAME})
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200663 set(ZEPHYR_CURRENT_MODULE_DIR ${ZEPHYR_${MODULE_NAME_UPPER}_MODULE_DIR})
664 set(ZEPHYR_CURRENT_CMAKE_DIR ${ZEPHYR_${MODULE_NAME_UPPER}_CMAKE_DIR})
665 add_subdirectory(${ZEPHYR_CURRENT_CMAKE_DIR} ${CMAKE_BINARY_DIR}/modules/${module_name})
666 endif()
Torsten Rasmussen2fc062b2020-08-24 11:01:04 +0200667endforeach()
Pieter De Gendtfcaed382024-01-16 19:16:23 +0100668# Done processing modules, clear module variables
669set(ZEPHYR_CURRENT_MODULE_NAME)
Torsten Rasmussen2fc062b2020-08-24 11:01:04 +0200670set(ZEPHYR_CURRENT_MODULE_DIR)
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200671set(ZEPHYR_CURRENT_CMAKE_DIR)
Torsten Rasmussen7e9d1bd2019-02-05 10:36:22 +0100672
Keith Packard87a30602022-11-02 14:49:23 -0700673get_property(LIBC_LINK_LIBRARIES TARGET zephyr_interface PROPERTY LIBC_LINK_LIBRARIES)
674zephyr_link_libraries(${LIBC_LINK_LIBRARIES})
675
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +0800676set(syscall_list_h ${CMAKE_CURRENT_BINARY_DIR}/include/generated/zephyr/syscall_list.h)
677set(edk_syscall_list_h ${CMAKE_CURRENT_BINARY_DIR}/edk/include/generated/zephyr/syscall_list.h)
Andrew Boie59601192020-05-29 13:24:51 -0700678set(syscalls_json ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls.json)
679set(struct_tags_json ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/struct_tags.json)
Sebastian Bøe13a68402017-11-20 13:03:55 +0100680
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200681# The syscalls subdirs txt file is constructed by python containing a list of folders to use for
682# dependency handling, including empty folders.
683# Windows: The list is used to specify DIRECTORY list with CMAKE_CONFIGURE_DEPENDS attribute.
684# Other OS: The list will update whenever a file is added/removed/modified and ensure a re-build.
685set(syscalls_subdirs_txt ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_subdirs.txt)
686
687# As syscalls_subdirs_txt is updated whenever a file is modified, this file can not be used for
688# monitoring of added / removed folders. A trigger file is thus used for correct dependency
689# handling. The trigger file will update when a folder is added / removed.
690set(syscalls_subdirs_trigger ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_subdirs.trigger)
691
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200692if(NOT (${CMAKE_HOST_SYSTEM_NAME} STREQUAL Windows))
693 set(syscalls_links --create-links ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_links)
694endif()
695
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200696# When running CMake it must be ensured that all dependencies are correctly acquired.
697execute_process(
698 COMMAND
699 ${PYTHON_EXECUTABLE}
Anas Nashif9ee1e322022-07-11 10:57:02 -0400700 ${ZEPHYR_BASE}/scripts/build/subfolder_list.py
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200701 --directory ${ZEPHYR_BASE}/include # Walk this directory
702 --out-file ${syscalls_subdirs_txt} # Write file with discovered folder
Jamie McCraeec704442023-01-04 16:08:36 +0000703 --trigger-file ${syscalls_subdirs_trigger} # Trigger file that is used for json generation
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200704 ${syscalls_links} # If defined, create symlinks for dependencies
705)
Carles Cufi3ad1f272019-07-18 10:38:25 +0200706file(STRINGS ${syscalls_subdirs_txt} PARSE_SYSCALLS_PATHS_DEPENDS ENCODING UTF-8)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200707
708if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL Windows)
709 # On windows only adding/removing files or folders will be reflected in depends.
710 # Hence adding a file requires CMake to re-run to add this file to the file list.
711 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${PARSE_SYSCALLS_PATHS_DEPENDS})
712
713 # Also On Windows each header file must be monitored as file modifications are not reflected
714 # on directory level.
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200715 file(GLOB_RECURSE PARSE_SYSCALLS_HEADER_DEPENDS ${ZEPHYR_BASE}/include/*.h)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200716else()
717 # The syscall parsing depends on the folders in order to detect add/removed/modified files.
718 # When a folder is removed, CMake will try to find a target that creates that dependency.
719 # This command sets up the target for CMake to find.
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200720 # Without this code, CMake will fail with the following error:
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200721 # <folder> needed by '<target>', missing and no known rule to make it
722 # when a folder is removed.
723 add_custom_command(OUTPUT ${PARSE_SYSCALLS_PATHS_DEPENDS}
724 COMMAND ${CMAKE_COMMAND} -E echo ""
725 COMMENT "Preparing syscall dependency handling"
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200726 )
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200727
728 add_custom_command(
729 OUTPUT
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200730 ${syscalls_subdirs_trigger}
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200731 COMMAND
732 ${PYTHON_EXECUTABLE}
Anas Nashif9ee1e322022-07-11 10:57:02 -0400733 ${ZEPHYR_BASE}/scripts/build/subfolder_list.py
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200734 --directory ${ZEPHYR_BASE}/include # Walk this directory
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200735 --out-file ${syscalls_subdirs_txt} # Write file with discovered folder
Jamie McCraeec704442023-01-04 16:08:36 +0000736 --trigger-file ${syscalls_subdirs_trigger} # Trigger file that is used for json generation
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200737 ${syscalls_links} # If defined, create symlinks for dependencies
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200738 DEPENDS ${PARSE_SYSCALLS_PATHS_DEPENDS}
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200739 )
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200740
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200741 # Ensure subdir file always exists when specifying CMake dependency.
742 if(NOT EXISTS ${syscalls_subdirs_txt})
743 file(WRITE ${syscalls_subdirs_txt} "")
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200744 endif()
745
746 # On other OS'es, modifying a file is reflected on the folder timestamp and hence detected
747 # when using depend on directory level.
748 # Thus CMake only needs to re-run when sub-directories are added / removed, which is indicated
749 # using a trigger file.
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200750 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${syscalls_subdirs_txt})
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200751endif()
752
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200753# syscall declarations are searched for in the SYSCALL_INCLUDE_DIRS
Adithya Baglodye67720b2018-07-02 14:59:19 +0530754if(CONFIG_APPLICATION_DEFINED_SYSCALL)
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200755 list(APPEND SYSCALL_INCLUDE_DIRS ${APPLICATION_SOURCE_DIR})
Adithya Baglodye67720b2018-07-02 14:59:19 +0530756endif()
757
Andrew Boiec1863872019-11-21 23:11:29 -0800758if(CONFIG_ZTEST)
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200759 list(APPEND SYSCALL_INCLUDE_DIRS ${ZEPHYR_BASE}/subsys/testsuite/ztest/include)
Fabio Baltierif5830f32023-05-17 10:28:16 +0000760
Fabio Baltierib9ea2c22023-05-23 08:15:58 +0000761 if(CONFIG_NO_OPTIMIZATIONS AND CONFIG_ZTEST_WARN_NO_OPTIMIZATIONS)
762 message(WARNING "Running tests with CONFIG_NO_OPTIMIZATIONS is generally "
763 "not supported and known to break in many cases due to stack overflow or "
764 "other problems. Please do not file issues about it unless the test is "
765 "specifically tuned to run in this configuration. To disable this warning "
766 "set CONFIG_ZTEST_WARN_NO_OPTIMIZATIONS=n.")
Fabio Baltierif5830f32023-05-17 10:28:16 +0000767 endif()
768
Andrew Boiec1863872019-11-21 23:11:29 -0800769endif()
770
Gerard Marull-Paretas80a6a202024-04-05 12:21:31 +0200771get_property(
772 syscalls_include_list
773 TARGET syscalls_interface
774 PROPERTY INTERFACE_INCLUDE_DIRECTORIES
775)
776list(APPEND SYSCALL_INCLUDE_DIRS ${syscalls_include_list})
777
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200778foreach(d ${SYSCALL_INCLUDE_DIRS})
779 list(APPEND parse_syscalls_include_args
780 --include ${d}
781 )
782endforeach()
783
Sebastian Bøe13a68402017-11-20 13:03:55 +0100784add_custom_command(
785 OUTPUT
786 ${syscalls_json}
Andrew Boie59601192020-05-29 13:24:51 -0700787 ${struct_tags_json}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100788 COMMAND
789 ${PYTHON_EXECUTABLE}
Anas Nashif92575fd2022-07-11 10:58:14 -0400790 ${ZEPHYR_BASE}/scripts/build/parse_syscalls.py
Daniel Leung80e78202023-05-25 11:41:48 -0700791 --scan ${ZEPHYR_BASE}/include # Read files from this dir
792 --scan ${ZEPHYR_BASE}/drivers # For net sockets
793 --scan ${ZEPHYR_BASE}/subsys/net # More net sockets
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200794 ${parse_syscalls_include_args} # Read files from these dirs also
Corey Whartonccd15df2020-02-29 14:51:42 -0800795 --json-file ${syscalls_json} # Write this file
Andrew Boie59601192020-05-29 13:24:51 -0700796 --tag-struct-file ${struct_tags_json} # Write subsystem list to this file
Daniel Leung80e78202023-05-25 11:41:48 -0700797 --file-list ${syscalls_file_list_output}
798 $<$<BOOL:${CONFIG_EMIT_ALL_SYSCALLS}>:--emit-all-syscalls>
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200799 DEPENDS ${syscalls_subdirs_trigger} ${PARSE_SYSCALLS_HEADER_DEPENDS}
Daniel Leung80e78202023-05-25 11:41:48 -0700800 ${syscalls_file_list_output} ${syscalls_interface}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100801 )
802
Keith Packardd0c75f32020-10-26 19:07:50 -0700803# Make sure Picolibc is built before the rest of the system; there's no explicit
804# reference to any of the files as they're all picked up by various compiler
805# settings
806if(CONFIG_PICOLIBC_USE_MODULE)
807 set(picolibc_dependency PicolibcBuild)
808endif()
809
810add_custom_target(${SYSCALL_LIST_H_TARGET} DEPENDS ${syscall_list_h} ${picolibc_dependency})
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +0100811
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +0100812set_property(TARGET ${SYSCALL_LIST_H_TARGET}
813 APPEND PROPERTY
814 ADDITIONAL_CLEAN_FILES
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +0800815 ${CMAKE_CURRENT_BINARY_DIR}/include/generated/zephyr/syscalls
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +0100816)
817
Andrew Boiec1c54b12020-03-16 12:48:00 -0700818add_custom_target(${PARSE_SYSCALLS_TARGET}
Joakim Andersson08d2f482020-08-04 18:26:43 +0200819 DEPENDS
820 ${syscalls_json}
Joakim Anderssond268f822020-08-04 18:31:48 +0200821 ${struct_tags_json}
Joakim Andersson08d2f482020-08-04 18:26:43 +0200822 )
Andrew Boie9ff64bb2019-11-05 09:39:05 -0800823
824# 64-bit systems do not require special handling of 64-bit system call
825# parameters or return values, indicate this to the system call boilerplate
826# generation script.
827if(CONFIG_64BIT)
828 set(SYSCALL_LONG_REGISTERS_ARG --long-registers)
829endif()
830
Andy Rosscfeb07e2020-03-05 21:14:02 -0800831if(CONFIG_TIMEOUT_64BIT)
Nicolas Pitre2cdac332022-03-04 21:21:38 -0500832 set(SYSCALL_SPLIT_TIMEOUT_ARG --split-type k_timeout_t --split-type k_ticks_t)
Andy Rosscfeb07e2020-03-05 21:14:02 -0800833endif()
834
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +0800835# percepio/TraceRecorder/kernelports/Zephyr/scripts/tz_parse_syscalls.py hardcodes the path
836# to the `syscall_list.h`, make a copy of the generated file so that percepio is able to build
837if(CONFIG_LEGACY_GENERATED_INCLUDE_PATH)
838 set(LEGACY_SYSCALL_LIST_H_ARGS
839 ${CMAKE_COMMAND} -E copy
840 ${syscall_list_h}
841 ${CMAKE_CURRENT_BINARY_DIR}/include/generated/syscall_list.h)
842endif()
843
Yong Cong Sin35704082024-05-24 22:09:31 +0800844add_custom_command(OUTPUT include/generated/zephyr/syscall_dispatch.c ${syscall_list_h}
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +0800845 # Also, some files are written to include/generated/zephyr/syscalls/
Sebastian Bøe13a68402017-11-20 13:03:55 +0100846 COMMAND
847 ${PYTHON_EXECUTABLE}
Anas Nashifc74d20e2022-07-11 10:54:14 -0400848 ${ZEPHYR_BASE}/scripts/build/gen_syscalls.py
Sebastian Bøe13a68402017-11-20 13:03:55 +0100849 --json-file ${syscalls_json} # Read this file
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +0800850 --base-output include/generated/zephyr/syscalls # Write to this dir
Yong Cong Sin35704082024-05-24 22:09:31 +0800851 --syscall-dispatch include/generated/zephyr/syscall_dispatch.c # Write this file
852 --syscall-export-llext include/generated/zephyr/syscall_export_llext.c
Andrew Boie353acf42018-07-23 18:10:15 -0700853 --syscall-list ${syscall_list_h}
Daniel Leung751de222023-03-10 14:07:59 -0800854 $<$<BOOL:${CONFIG_USERSPACE}>:--gen-mrsh-files>
Andrew Boie9ff64bb2019-11-05 09:39:05 -0800855 ${SYSCALL_LONG_REGISTERS_ARG}
Andy Rosscfeb07e2020-03-05 21:14:02 -0800856 ${SYSCALL_SPLIT_TIMEOUT_ARG}
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +0800857 COMMAND
858 ${LEGACY_SYSCALL_LIST_H_ARGS}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100859 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
Andrew Boiec1c54b12020-03-16 12:48:00 -0700860 DEPENDS ${PARSE_SYSCALLS_TARGET}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100861 )
862
Corey Whartonccd15df2020-02-29 14:51:42 -0800863# This is passed into all calls to the gen_kobject_list.py script.
Jamie McCraeec704442023-01-04 16:08:36 +0000864set(gen_kobject_list_include_args --include-subsystem-list ${struct_tags_json})
Corey Whartonccd15df2020-02-29 14:51:42 -0800865
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +0800866set(DRV_VALIDATION ${PROJECT_BINARY_DIR}/include/generated/zephyr/driver-validation.h)
Leandro Pereirac2003672018-04-04 13:50:32 -0700867add_custom_command(
868 OUTPUT ${DRV_VALIDATION}
869 COMMAND
870 ${PYTHON_EXECUTABLE}
Anas Nashifefbadbb2022-07-11 10:53:29 -0400871 ${ZEPHYR_BASE}/scripts/build/gen_kobject_list.py
Leandro Pereirac2003672018-04-04 13:50:32 -0700872 --validation-output ${DRV_VALIDATION}
Corey Whartonccd15df2020-02-29 14:51:42 -0800873 ${gen_kobject_list_include_args}
Leandro Pereirac2003672018-04-04 13:50:32 -0700874 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Corey Whartonccd15df2020-02-29 14:51:42 -0800875 DEPENDS
Anas Nashifefbadbb2022-07-11 10:53:29 -0400876 ${ZEPHYR_BASE}/scripts/build/gen_kobject_list.py
Andrew Boiec1c54b12020-03-16 12:48:00 -0700877 ${PARSE_SYSCALLS_TARGET}
Leandro Pereirac2003672018-04-04 13:50:32 -0700878 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
879 )
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100880add_custom_target(${DRIVER_VALIDATION_H_TARGET} DEPENDS ${DRV_VALIDATION})
Leandro Pereirac2003672018-04-04 13:50:32 -0700881
Torsten Rasmussend7862cf2020-02-12 15:42:09 +0100882include(${ZEPHYR_BASE}/cmake/kobj.cmake)
Leandro Pereira39dc7d02018-04-05 13:59:33 -0700883gen_kobj(KOBJ_INCLUDE_PATH)
Leandro Pereirac2003672018-04-04 13:50:32 -0700884
Sebastian Bøefdac7b32020-01-23 15:39:17 +0100885# Add a pseudo-target that is up-to-date when all generated headers
886# are up-to-date.
887
888add_custom_target(zephyr_generated_headers)
889add_dependencies(zephyr_generated_headers
Torsten Rasmussen91709772022-02-04 10:27:13 +0100890 offsets_h version_h
Sebastian Bøefdac7b32020-01-23 15:39:17 +0100891 )
892
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200893# Generate offsets.c.obj from offsets.c
894# Generate offsets.h from offsets.c.obj
895
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100896set(OFFSETS_LIB offsets)
897
Klaus Petersenc66cb762018-11-15 10:37:46 +0100898set(OFFSETS_C_PATH ${ARCH_DIR}/${ARCH}/core/offsets/offsets.c)
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +0800899set(OFFSETS_H_PATH ${PROJECT_BINARY_DIR}/include/generated/zephyr/offsets.h)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200900
Klaus Petersen62e55e52019-02-04 12:10:57 +0100901add_library( ${OFFSETS_LIB} OBJECT ${OFFSETS_C_PATH})
Stephanos Ioannidis2d746042019-10-25 00:08:21 +0900902target_include_directories(${OFFSETS_LIB} PRIVATE
903 kernel/include
904 ${ARCH_DIR}/${ARCH}/include
905 )
Radoslaw Koppel26c87762024-01-30 12:32:32 +0100906
907# Make sure that LTO will never be enabled when compiling offsets.c
908set_source_files_properties(${OFFSETS_C_PATH} PROPERTIES COMPILE_OPTIONS $<TARGET_PROPERTY:compiler,prohibit_lto>)
909
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100910target_link_libraries(${OFFSETS_LIB} zephyr_interface)
Joakim Anderssond268f822020-08-04 18:31:48 +0200911add_dependencies(zephyr_interface
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100912 ${SYSCALL_LIST_H_TARGET}
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100913 ${DRIVER_VALIDATION_H_TARGET}
914 ${KOBJ_TYPES_H_TARGET}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100915 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200916
917add_custom_command(
918 OUTPUT ${OFFSETS_H_PATH}
Anas Nashife234c212022-07-11 10:53:45 -0400919 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/build/gen_offset_header.py
Klaus Petersen62e55e52019-02-04 12:10:57 +0100920 -i $<TARGET_OBJECTS:${OFFSETS_LIB}>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200921 -o ${OFFSETS_H_PATH}
Sebastian Bøe5962aab2019-08-15 14:45:59 +0200922 DEPENDS
923 ${OFFSETS_LIB}
924 $<TARGET_OBJECTS:${OFFSETS_LIB}>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200925)
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100926add_custom_target(${OFFSETS_H_TARGET} DEPENDS ${OFFSETS_H_PATH})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200927
Sebastian Bøe89516fb2017-12-01 15:25:06 +0100928zephyr_get_include_directories_for_lang(C ZEPHYR_INCLUDES)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200929
930add_subdirectory(kernel)
931
Daniel Leung80e78202023-05-25 11:41:48 -0700932get_property(
933 syscalls_file_list
934 TARGET syscalls_interface
Gerard Marull-Paretasca4e5d92024-04-05 12:12:50 +0200935 PROPERTY INTERFACE_SOURCES
Daniel Leung80e78202023-05-25 11:41:48 -0700936)
937file(CONFIGURE OUTPUT ${syscalls_file_list_output}
938 CONTENT "@syscalls_file_list@" @ONLY)
939
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200940# Read list content
941get_property(ZEPHYR_LIBS_PROPERTY GLOBAL PROPERTY ZEPHYR_LIBS)
942
943foreach(zephyr_lib ${ZEPHYR_LIBS_PROPERTY})
Torsten Rasmussend9520042021-04-14 17:11:39 +0200944 get_property(lib_type TARGET ${zephyr_lib} PROPERTY TYPE)
Torsten Rasmussend9520042021-04-14 17:11:39 +0200945 # To prevent CMake failure when a driver is enabled, for example: REGULATOR=y
946 # we disable any Zephyr libraries without sources and adds the `empty_file.c`.
947 if(${lib_type} STREQUAL STATIC_LIBRARY
Torsten Rasmussend9520042021-04-14 17:11:39 +0200948 AND NOT ${zephyr_lib} STREQUAL app
949 )
Torsten Rasmussen25578be2021-04-23 09:09:49 +0200950 get_property(source_list TARGET ${zephyr_lib} PROPERTY SOURCES)
951 get_property(lib_imported TARGET ${zephyr_lib} PROPERTY IMPORTED)
952 if(NOT source_list
953 AND NOT ${lib_imported}
Torsten Rasmussend9520042021-04-14 17:11:39 +0200954 )
Torsten Rasmussen153196b2021-09-06 16:42:21 +0200955 get_property(allow_empty TARGET ${zephyr_lib} PROPERTY ALLOW_EMPTY)
956 if(NOT "${allow_empty}")
957 message(WARNING
958 "No SOURCES given to Zephyr library: ${zephyr_lib}\nExcluding target from build."
959 )
960 endif()
Torsten Rasmussen25578be2021-04-23 09:09:49 +0200961 target_sources(${zephyr_lib} PRIVATE ${ZEPHYR_BASE}/misc/empty_file.c)
962 set_property(TARGET ${zephyr_lib} PROPERTY EXCLUDE_FROM_ALL TRUE)
963 list(REMOVE_ITEM ZEPHYR_LIBS_PROPERTY ${zephyr_lib})
964 continue()
965 endif()
Torsten Rasmussend9520042021-04-14 17:11:39 +0200966 endif()
Torsten Rasmussen25578be2021-04-23 09:09:49 +0200967
968 # TODO: Could this become an INTERFACE property of zephyr_interface?
969 add_dependencies(${zephyr_lib} zephyr_generated_headers)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200970endforeach()
971
Daniel Apperloo9fc26802024-05-03 01:55:49 +0000972if(CONFIG_KERNEL_WHOLE_ARCHIVE)
Ederson de Souza321e3952024-03-21 15:50:13 -0700973 set(WHOLE_ARCHIVE_LIBS ${ZEPHYR_LIBS_PROPERTY} kernel)
974else()
975 set(WHOLE_ARCHIVE_LIBS ${ZEPHYR_LIBS_PROPERTY})
976 set(NO_WHOLE_ARCHIVE_LIBS kernel)
977endif()
978
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200979get_property(OUTPUT_FORMAT GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT)
980
Adithya Baglody62e152a2018-11-13 15:34:02 +0530981if (CONFIG_CODE_DATA_RELOCATION)
982 set(CODE_RELOCATION_DEP code_relocation_source_lib)
983endif() # CONFIG_CODE_DATA_RELOCATION
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100984
Daniel Leungc7459952021-03-19 12:09:05 -0700985# Give the linker script targets all of the include directories so
986# that cmake can successfully find the linker scripts' header
Sebastian Bøeb1ab5012017-12-14 13:03:23 +0100987# dependencies.
988zephyr_get_include_directories_for_lang(C
989 ZEPHYR_INCLUDE_DIRS
990 STRIP_PREFIX # Don't use a -I prefix
991 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200992
Gerard Marull-Paretas48b201c2023-06-14 14:30:41 +0200993if(CONFIG_DEVICE_DEPS)
Gerard Marull-Paretas319fbe52023-06-14 12:57:29 +0200994 if(CONFIG_DEVICE_DEPS_DYNAMIC)
Gerard Marull-Paretasb6d5d242023-06-14 17:12:55 +0200995 set(dynamic_deps --dynamic-deps)
Gerard Marull-Paretase5335f32023-06-14 10:15:12 +0200996 endif()
997
Gerard Marull-Paretas9f25fa22023-06-14 10:24:51 +0200998 if(CONFIG_PM_DEVICE_POWER_DOMAIN_DYNAMIC)
999 set(number_of_dynamic_devices ${CONFIG_PM_DEVICE_POWER_DOMAIN_DYNAMIC_NUM})
1000 else()
1001 set(number_of_dynamic_devices 0)
1002 endif()
1003
Gerard Marull-Paretas92707862023-06-14 11:51:36 +02001004 # device_deps.c is generated from ${ZEPHYR_LINK_STAGE_EXECUTABLE} by
1005 # gen_device_deps.py
Morten Priessa846e722021-04-21 09:06:02 +02001006 add_custom_command(
Gerard Marull-Paretas92707862023-06-14 11:51:36 +02001007 OUTPUT device_deps.c
Morten Priessa846e722021-04-21 09:06:02 +02001008 COMMAND
1009 ${PYTHON_EXECUTABLE}
Gerard Marull-Paretas92707862023-06-14 11:51:36 +02001010 ${ZEPHYR_BASE}/scripts/build/gen_device_deps.py
1011 --output-source device_deps.c
Jordan Yates29942472022-07-10 13:46:17 +10001012 --output-graphviz dev_graph.dot
Gerard Marull-Paretasb6d5d242023-06-14 17:12:55 +02001013 ${dynamic_deps}
Flavio Ceolin0b13b442022-01-05 17:19:53 -08001014 --num-dynamic-devices ${number_of_dynamic_devices}
Jordan Yates28b2e552021-10-20 20:19:28 +10001015 --kernel $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
Morten Priessa846e722021-04-21 09:06:02 +02001016 --zephyr-base ${ZEPHYR_BASE}
Torsten Rasmussenc9804d22021-05-21 21:34:58 +02001017 --start-symbol "$<TARGET_PROPERTY:linker,devices_start_symbol>"
Torsten Rasmussen6d72d912022-01-05 11:25:26 +01001018 VERBATIM
Jordan Yates28b2e552021-10-20 20:19:28 +10001019 DEPENDS ${ZEPHYR_LINK_STAGE_EXECUTABLE}
Morten Priessa846e722021-04-21 09:06:02 +02001020 )
Gerard Marull-Paretas92707862023-06-14 11:51:36 +02001021 set_property(GLOBAL APPEND PROPERTY GENERATED_APP_SOURCE_FILES device_deps.c)
Jordan Yates28b2e552021-10-20 20:19:28 +10001022
Gerard Marull-Paretas8bee39e2023-06-14 12:10:40 +02001023 # gen_device_deps runs on `__device_deps_pass1` so pass this info to the linker script generator
1024 list(APPEND LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE "LINKER_DEVICE_DEPS_PASS1")
Morten Priessa846e722021-04-21 09:06:02 +02001025endif()
Peter Bigotd554d342020-06-30 10:05:35 -05001026
Adithya Baglody62e152a2018-11-13 15:34:02 +05301027if(CONFIG_CODE_DATA_RELOCATION)
Mark Ruvald Pedersen86a3e8f2019-05-03 10:33:03 +02001028 # @Intent: Linker script to relocate .text, data and .bss sections
1029 toolchain_ld_relocation()
Adithya Baglody62e152a2018-11-13 15:34:02 +05301030endif()
1031
Adithya Baglody4b3c7b32018-11-21 14:31:56 +05301032if(CONFIG_USERSPACE)
Torsten Rasmussene37d9e62020-11-20 18:39:30 +01001033 zephyr_get_compile_options_for_lang_as_string(C compiler_flags_priv)
Torsten Rasmussene0758c32020-08-21 19:13:53 +02001034 string(REPLACE "$<TARGET_PROPERTY:compiler,coverage>" ""
1035 NO_COVERAGE_FLAGS "${compiler_flags_priv}"
1036 )
Adithya Baglody4b3c7b32018-11-21 14:31:56 +05301037
Anas Nashifefbadbb2022-07-11 10:53:29 -04001038 set(GEN_KOBJ_LIST ${ZEPHYR_BASE}/scripts/build/gen_kobject_list.py)
Anas Nashifd8599972022-07-11 10:56:46 -04001039 set(PROCESS_GPERF ${ZEPHYR_BASE}/scripts/build/process_gperf.py)
Jordan Yates28b2e552021-10-20 20:19:28 +10001040endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001041
Pieter De Gendt294939a2024-05-07 15:44:30 +02001042get_property(GLOBAL_CSTD GLOBAL PROPERTY CSTD)
1043if(DEFINED GLOBAL_CSTD)
Pieter De Gendt86b721c2024-04-03 09:32:10 +02001044 message(DEPRECATION
1045 "Global CSTD property is deprecated, see Kconfig.zephyr for C Standard options.")
Pieter De Gendt294939a2024-05-07 15:44:30 +02001046 set(CSTD ${GLOBAL_CSTD})
1047 list(APPEND CMAKE_C_COMPILE_FEATURES ${compile_features_${CSTD}})
Pieter De Gendt86b721c2024-04-03 09:32:10 +02001048endif()
Jordan Yates28b2e552021-10-20 20:19:28 +10001049
1050# @Intent: Obtain compiler specific flag for specifying the c standard
1051zephyr_compile_options(
1052 $<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,cstd>${CSTD}>
1053)
Pieter De Gendt294939a2024-05-07 15:44:30 +02001054set(CMAKE_C_COMPILE_FEATURES ${CMAKE_C_COMPILE_FEATURES} PARENT_SCOPE)
Jordan Yates28b2e552021-10-20 20:19:28 +10001055
1056# @Intent: Configure linker scripts, i.e. generate linker scripts with variables substituted
1057toolchain_ld_configure_files()
1058
Torsten Rasmussen26b649e2023-10-27 21:49:16 +02001059get_property(TOPT GLOBAL PROPERTY TOPT)
1060get_property(COMPILER_TOPT TARGET compiler PROPERTY linker_script)
1061set_ifndef( TOPT "${COMPILER_TOPT}")
1062set_ifndef( TOPT -Wl,-T) # Use this if the compiler driver doesn't set a value
1063
1064if(CONFIG_HAVE_CUSTOM_LINKER_SCRIPT)
1065 set(LINKER_SCRIPT ${APPLICATION_SOURCE_DIR}/${CONFIG_CUSTOM_LINKER_SCRIPT})
1066 if(NOT EXISTS ${LINKER_SCRIPT})
1067 set(LINKER_SCRIPT ${CONFIG_CUSTOM_LINKER_SCRIPT})
1068 assert_exists(CONFIG_CUSTOM_LINKER_SCRIPT)
1069 endif()
1070elseif(DEFINED BOARD_LINKER_SCRIPT)
1071 set(LINKER_SCRIPT ${BOARD_LINKER_SCRIPT})
1072elseif(DEFINED SOC_LINKER_SCRIPT)
1073 set(LINKER_SCRIPT ${SOC_LINKER_SCRIPT})
1074else()
1075 find_package(Deprecated COMPONENTS SEARCHED_LINKER_SCRIPT)
1076endif()
1077
1078if(NOT EXISTS ${LINKER_SCRIPT})
1079 message(FATAL_ERROR "Could not find linker script: '${LINKER_SCRIPT}'. Corrupted configuration?")
1080endif()
1081
Jordan Yates28b2e552021-10-20 20:19:28 +10001082if(CONFIG_USERSPACE)
1083 set(APP_SMEM_ALIGNED_LD "${PROJECT_BINARY_DIR}/include/generated/app_smem_aligned.ld")
1084 set(APP_SMEM_UNALIGNED_LD "${PROJECT_BINARY_DIR}/include/generated/app_smem_unaligned.ld")
1085
1086 if(CONFIG_LINKER_USE_PINNED_SECTION)
1087 set(APP_SMEM_PINNED_ALIGNED_LD
1088 "${PROJECT_BINARY_DIR}/include/generated/app_smem_pinned_aligned.ld")
1089 set(APP_SMEM_PINNED_UNALIGNED_LD
1090 "${PROJECT_BINARY_DIR}/include/generated/app_smem_pinned_unaligned.ld")
1091
1092 if(NOT CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT)
1093 # The libc partition may hold symbols that are required during boot process,
1094 # for example, stack guard (if enabled). So the libc partition must be pinned
1095 # if not sections are in physical memory at boot, as the paging mechanism is
1096 # only initialized post-kernel.
1097 set_property(TARGET app_smem APPEND PROPERTY pinned_partitions "z_libc_partition")
1098 endif()
1099
1100 get_property(APP_SMEM_PINNED_PARTITION_LIST TARGET app_smem PROPERTY pinned_partitions)
1101 if(APP_SMEM_PINNED_PARTITION_LIST)
1102 list(JOIN APP_SMEM_PINNED_PARTITION_LIST "," APP_SMEM_PINNED_PARTITION_LIST_ARG_CSL)
1103 set(APP_SMEM_PINNED_PARTITION_LIST_ARG "--pinpartitions=${APP_SMEM_PINNED_PARTITION_LIST_ARG_CSL}")
1104 endif()
1105 endif()
1106
1107 set(OBJ_FILE_DIR "${PROJECT_BINARY_DIR}/../")
1108
1109 if(CONFIG_NEWLIB_LIBC)
Keith Packardd0c75f32020-10-26 19:07:50 -07001110 set(LIBC_PART -l libc.a z_libc_partition -l libm.a z_libc_partition)
Jordan Yates28b2e552021-10-20 20:19:28 +10001111 endif()
1112 if(CONFIG_NEWLIB_LIBC_NANO)
Keith Packardd0c75f32020-10-26 19:07:50 -07001113 set(LIBC_PART -l libc_nano.a z_libc_partition -l libm_nano.a z_libc_partition)
1114 endif()
1115 if(CONFIG_PICOLIBC)
1116 set(LIBC_PART -l libc.a z_libc_partition)
Jordan Yates28b2e552021-10-20 20:19:28 +10001117 endif()
1118
1119 add_custom_command(
1120 OUTPUT ${APP_SMEM_UNALIGNED_LD} ${APP_SMEM_PINNED_UNALIGNED_LD}
1121 COMMAND ${PYTHON_EXECUTABLE}
Anas Nashif6e1a3352022-07-11 10:46:17 -04001122 ${ZEPHYR_BASE}/scripts/build/gen_app_partitions.py
Torsten Rasmussenf643b8b2021-11-24 15:35:47 +01001123 -f ${CMAKE_BINARY_DIR}/compile_commands.json
Jordan Yates28b2e552021-10-20 20:19:28 +10001124 -o ${APP_SMEM_UNALIGNED_LD}
1125 $<$<BOOL:${APP_SMEM_PINNED_UNALIGNED_LD}>:--pinoutput=${APP_SMEM_PINNED_UNALIGNED_LD}>
1126 ${APP_SMEM_PINNED_PARTITION_LIST_ARG}
Keith Packardd0c75f32020-10-26 19:07:50 -07001127 ${LIBC_PART}
Jordan Yates28b2e552021-10-20 20:19:28 +10001128 $<TARGET_PROPERTY:zephyr_property_target,COMPILE_OPTIONS>
1129 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1130 DEPENDS
1131 kernel
Andy Ross6cfb1862022-08-11 16:45:57 -07001132 ${CMAKE_BINARY_DIR}/compile_commands.json
Jordan Yates28b2e552021-10-20 20:19:28 +10001133 ${ZEPHYR_LIBS_PROPERTY}
1134 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
1135 COMMAND_EXPAND_LISTS
1136 COMMENT "Generating app_smem_unaligned linker section"
1137 )
1138
1139 add_custom_target(
1140 ${APP_SMEM_ALIGNED_DEP}
1141 DEPENDS
1142 ${APP_SMEM_ALIGNED_LD}
1143 ${APP_SMEM_PINNED_ALIGNED_LD}
1144 )
1145
1146 add_custom_target(
1147 ${APP_SMEM_UNALIGNED_DEP}
1148 DEPENDS
1149 ${APP_SMEM_UNALIGNED_LD}
1150 ${APP_SMEM_PINNED_UNALIGNED_LD}
1151 )
1152
1153 set(APP_SMEM_UNALIGNED_LIB app_smem_unaligned_output_obj_renamed_lib)
1154 list(APPEND LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE "LINKER_APP_SMEM_UNALIGNED")
1155endif()
1156
1157if (CONFIG_USERSPACE)
1158 add_custom_command(
1159 OUTPUT ${APP_SMEM_ALIGNED_LD} ${APP_SMEM_PINNED_ALIGNED_LD}
1160 COMMAND ${PYTHON_EXECUTABLE}
Anas Nashif6e1a3352022-07-11 10:46:17 -04001161 ${ZEPHYR_BASE}/scripts/build/gen_app_partitions.py
Jordan Yates28b2e552021-10-20 20:19:28 +10001162 -e $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
1163 -o ${APP_SMEM_ALIGNED_LD}
1164 $<$<BOOL:${APP_SMEM_PINNED_ALIGNED_LD}>:--pinoutput=${APP_SMEM_PINNED_ALIGNED_LD}>
1165 ${APP_SMEM_PINNED_PARTITION_LIST_ARG}
Keith Packardd0c75f32020-10-26 19:07:50 -07001166 ${LIBC_PART}
Jordan Yates28b2e552021-10-20 20:19:28 +10001167 $<TARGET_PROPERTY:zephyr_property_target,COMPILE_OPTIONS>
1168 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1169 DEPENDS
1170 kernel
1171 ${ZEPHYR_LIBS_PROPERTY}
1172 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1173 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
1174 COMMAND_EXPAND_LISTS
1175 COMMENT "Generating app_smem_aligned linker section"
1176 )
1177endif()
1178
1179if(CONFIG_USERSPACE)
1180 # This CONFIG_USERSPACE block is to create place holders to reserve space
1181 # for the gperf generated structures for zephyr_prebuilt.elf.
1182 # These place holders are there so that the placement of kobjects would be
1183 # the same between linking zephyr_prebuilt.elf and zephyr.elf, as
1184 # the gperf hash table is hashed on the addresses of kobjects.
1185 # The placeholders are generated from app_smem_unaligned_prebuilt.elf.
1186
1187 set(KOBJECT_PREBUILT_HASH_LIST kobject_prebuilt_hash.gperf)
1188 set(KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE kobject_prebuilt_hash_preprocessed.c)
1189 set(KOBJECT_PREBUILT_HASH_OUTPUT_SRC kobject_prebuilt_hash.c)
1190
1191 add_custom_command(
1192 OUTPUT ${KOBJECT_PREBUILT_HASH_LIST}
1193 COMMAND
1194 ${PYTHON_EXECUTABLE}
1195 ${GEN_KOBJ_LIST}
1196 --kernel $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
1197 --gperf-output ${KOBJECT_PREBUILT_HASH_LIST}
1198 ${gen_kobject_list_include_args}
1199 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1200 DEPENDS
1201 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1202 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1203 )
1204 add_custom_target(
1205 kobj_prebuilt_hash_list
1206 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_LIST}
1207 )
1208
1209 add_custom_command(
1210 OUTPUT ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
1211 COMMAND
1212 ${GPERF}
1213 --output-file ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
1214 --multiple-iterations 10
1215 ${KOBJECT_PREBUILT_HASH_LIST}
1216 DEPENDS kobj_prebuilt_hash_list ${KOBJECT_PREBUILT_HASH_LIST}
1217 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1218 )
1219 add_custom_target(
1220 kobj_prebuilt_hash_output_src_pre
1221 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
1222 )
1223
1224 add_custom_command(
1225 OUTPUT ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1226 COMMAND
1227 ${PYTHON_EXECUTABLE}
1228 ${PROCESS_GPERF}
1229 -i ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
1230 -o ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
Anas Nashifa6b49002023-09-26 21:37:25 +00001231 -p "struct k_object"
Jordan Yates28b2e552021-10-20 20:19:28 +10001232 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1233 DEPENDS kobj_prebuilt_hash_output_src_pre ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
1234 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1235 )
1236 add_custom_target(
1237 kobj_prebuilt_hash_output_src
1238 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1239 )
1240
1241 add_library(
1242 kobj_prebuilt_hash_output_lib
1243 OBJECT ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1244 )
1245
1246 set_source_files_properties(${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1247 PROPERTIES COMPILE_FLAGS
1248 "${NO_COVERAGE_FLAGS} -fno-function-sections -fno-data-sections")
1249
1250 target_compile_definitions(kobj_prebuilt_hash_output_lib
1251 PRIVATE $<TARGET_PROPERTY:zephyr_interface,INTERFACE_COMPILE_DEFINITIONS>
1252 )
1253
1254 target_include_directories(kobj_prebuilt_hash_output_lib
1255 PUBLIC $<TARGET_PROPERTY:zephyr_interface,INTERFACE_INCLUDE_DIRECTORIES>
1256 )
1257
1258 target_include_directories(kobj_prebuilt_hash_output_lib SYSTEM
1259 PUBLIC $<TARGET_PROPERTY:zephyr_interface,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
1260 )
1261
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +08001262 set(KOBJECT_LINKER_HEADER_DATA "${PROJECT_BINARY_DIR}/include/generated/zephyr/linker-kobject-prebuilt-data.h")
Jordan Yates28b2e552021-10-20 20:19:28 +10001263
1264 add_custom_command(
1265 OUTPUT ${KOBJECT_LINKER_HEADER_DATA}
1266 COMMAND
1267 ${PYTHON_EXECUTABLE}
Anas Nashifd5dcf202022-07-11 10:53:38 -04001268 ${ZEPHYR_BASE}/scripts/build/gen_kobject_placeholders.py
Jordan Yates28b2e552021-10-20 20:19:28 +10001269 --object $<TARGET_OBJECTS:kobj_prebuilt_hash_output_lib>
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +08001270 --outdir ${PROJECT_BINARY_DIR}/include/generated/zephyr
Jordan Yates28b2e552021-10-20 20:19:28 +10001271 --datapct ${CONFIG_KOBJECT_DATA_AREA_RESERVE_EXTRA_PERCENT}
1272 --rodata ${CONFIG_KOBJECT_RODATA_AREA_EXTRA_BYTES}
1273 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1274 DEPENDS
Torsten Rasmussen04543512022-02-03 15:14:44 +01001275 kobj_prebuilt_hash_output_lib $<TARGET_OBJECTS:kobj_prebuilt_hash_output_lib>
Jordan Yates28b2e552021-10-20 20:19:28 +10001276 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1277 )
1278
1279 add_custom_target(
1280 ${KOBJECT_LINKER_DEP}
1281 DEPENDS
1282 ${KOBJECT_LINKER_HEADER_DATA}
1283 )
1284endif()
1285
Gerard Marull-Paretas48b201c2023-06-14 14:30:41 +02001286if(CONFIG_USERSPACE OR CONFIG_DEVICE_DEPS)
Jordan Yates28b2e552021-10-20 20:19:28 +10001287 configure_linker_script(
1288 ${ZEPHYR_CURRENT_LINKER_CMD}
1289 "${LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE}"
1290 ${CODE_RELOCATION_DEP}
1291 ${APP_SMEM_UNALIGNED_DEP}
1292 ${APP_SMEM_UNALIGNED_LD}
1293 ${APP_SMEM_PINNED_UNALIGNED_LD}
1294 zephyr_generated_headers
1295 )
1296
1297 add_custom_target(
1298 linker_zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS}_script
1299 DEPENDS
1300 ${ZEPHYR_CURRENT_LINKER_CMD}
1301 )
1302
1303 set_property(TARGET
1304 linker_zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS}_script
1305 PROPERTY INCLUDE_DIRECTORIES
1306 ${ZEPHYR_INCLUDE_DIRS}
1307 )
1308
1309 add_executable(${ZEPHYR_LINK_STAGE_EXECUTABLE} misc/empty_file.c)
1310 toolchain_ld_link_elf(
1311 TARGET_ELF ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1312 OUTPUT_MAP ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
1313 LIBRARIES_PRE_SCRIPT ""
1314 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD}
1315 LIBRARIES_POST_SCRIPT ""
1316 DEPENDENCIES ${CODE_RELOCATION_DEP}
1317 )
Alberto Escolar Piedrasfeaf0ff2023-06-07 10:28:22 +02001318 target_link_libraries_ifdef(CONFIG_NATIVE_LIBRARY ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1319 $<TARGET_PROPERTY:linker,no_position_independent>)
Jordan Yates28b2e552021-10-20 20:19:28 +10001320 target_byproducts(TARGET ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1321 BYPRODUCTS ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
1322 )
1323 set_property(TARGET ${ZEPHYR_LINK_STAGE_EXECUTABLE} PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD})
1324 add_dependencies(${ZEPHYR_LINK_STAGE_EXECUTABLE} linker_zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS}_script ${OFFSETS_LIB})
1325
1326 math(EXPR ZEPHYR_CURRENT_LINKER_PASS "1 + ${ZEPHYR_CURRENT_LINKER_PASS}")
1327endif()
1328
1329set(ZEPHYR_CURRENT_LINKER_CMD linker_zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS}.cmd)
1330set(ZEPHYR_LINK_STAGE_EXECUTABLE zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS})
1331list(APPEND LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE "LINKER_ZEPHYR_PREBUILT")
1332
1333if(CONFIG_GEN_ISR_TABLES)
1334 if(CONFIG_GEN_SW_ISR_TABLE)
1335 list(APPEND GEN_ISR_TABLE_EXTRA_ARG --sw-isr-table)
1336 endif()
1337
1338 if(CONFIG_GEN_IRQ_VECTOR_TABLE)
1339 list(APPEND GEN_ISR_TABLE_EXTRA_ARG --vector-table)
1340 endif()
1341
1342 # isr_tables.c is generated from ${ZEPHYR_LINK_STAGE_EXECUTABLE} by
1343 # gen_isr_tables.py
1344 add_custom_command(
Radoslaw Koppel0ae48ec2023-12-02 23:09:11 +01001345 OUTPUT isr_tables.c isr_tables_vt.ld isr_tables_swi.ld
Jordan Yates28b2e552021-10-20 20:19:28 +10001346 COMMAND ${PYTHON_EXECUTABLE}
Anas Nashifc36307e2022-07-11 10:51:50 -04001347 ${ZEPHYR_BASE}/scripts/build/gen_isr_tables.py
Jordan Yates28b2e552021-10-20 20:19:28 +10001348 --output-source isr_tables.c
Radoslaw Koppel0ae48ec2023-12-02 23:09:11 +01001349 --linker-output-files isr_tables_vt.ld isr_tables_swi.ld
Jordan Yates28b2e552021-10-20 20:19:28 +10001350 --kernel $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
Radoslaw Koppel48b93ee2023-07-26 16:52:21 +02001351 --intlist-section .intList
1352 --intlist-section intList
Jordan Yates28b2e552021-10-20 20:19:28 +10001353 $<$<BOOL:${CONFIG_BIG_ENDIAN}>:--big-endian>
1354 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--debug>
1355 ${GEN_ISR_TABLE_EXTRA_ARG}
1356 DEPENDS ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1357 COMMAND_EXPAND_LISTS
1358 )
1359 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_SOURCE_FILES isr_tables.c)
1360endif()
1361
Yong Cong Sine1ce0ae2024-05-18 16:45:44 +08001362if(CONFIG_SYMTAB)
1363 add_custom_command(
1364 OUTPUT symtab.c
1365 COMMAND
1366 ${PYTHON_EXECUTABLE}
1367 ${ZEPHYR_BASE}/scripts/build/gen_symtab.py
1368 -k $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
1369 -o symtab.c
1370 DEPENDS ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1371 COMMAND_EXPAND_LISTS
1372 )
1373 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_SOURCE_FILES symtab.c)
1374endif()
1375
Jordan Yates28b2e552021-10-20 20:19:28 +10001376if(CONFIG_USERSPACE)
Daniel Leung28c35122021-03-18 12:02:19 -07001377 set(KOBJECT_HASH_LIST kobject_hash.gperf)
1378 set(KOBJECT_HASH_OUTPUT_SRC_PRE kobject_hash_preprocessed.c)
1379 set(KOBJECT_HASH_OUTPUT_SRC kobject_hash.c)
Daniel Leung28c35122021-03-18 12:02:19 -07001380 set(KOBJECT_HASH_OUTPUT_OBJ_RENAMED kobject_hash_renamed.o)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001381
1382 # Essentially what we are doing here is extracting some information
1383 # out of the nearly finished elf file, generating the source code
1384 # for a hash table based on that information, and then compiling and
1385 # linking the hash table back into a now even more nearly finished
Marc Herbert4a10eea2019-04-16 15:39:45 -07001386 # elf file. More information in gen_kobject_list.py --help.
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001387
1388 # Use the script GEN_KOBJ_LIST to scan the kernel binary's
Jordan Yates28b2e552021-10-20 20:19:28 +10001389 # (${ZEPHYR_LINK_STAGE_EXECUTABLE}) DWARF information to produce a table of kernel
Daniel Leung28c35122021-03-18 12:02:19 -07001390 # objects (KOBJECT_HASH_LIST) which we will then pass to gperf
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001391 add_custom_command(
Daniel Leung28c35122021-03-18 12:02:19 -07001392 OUTPUT ${KOBJECT_HASH_LIST}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001393 COMMAND
1394 ${PYTHON_EXECUTABLE}
1395 ${GEN_KOBJ_LIST}
Jordan Yates28b2e552021-10-20 20:19:28 +10001396 --kernel $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
Daniel Leung28c35122021-03-18 12:02:19 -07001397 --gperf-output ${KOBJECT_HASH_LIST}
Corey Whartonccd15df2020-02-29 14:51:42 -08001398 ${gen_kobject_list_include_args}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001399 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Corey Whartonccd15df2020-02-29 14:51:42 -08001400 DEPENDS
Jordan Yates28b2e552021-10-20 20:19:28 +10001401 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001402 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1403 )
Daniel Leung28c35122021-03-18 12:02:19 -07001404 add_custom_target(
1405 kobj_hash_list
1406 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_LIST}
1407 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001408
Daniel Leung28c35122021-03-18 12:02:19 -07001409 # Use gperf to generate C code (KOBJECT_HASH_OUTPUT_SRC_PRE) which implements a
1410 # perfect hashtable based on KOBJECT_HASH_LIST
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001411 add_custom_command(
Torsten Rasmussen04543512022-02-03 15:14:44 +01001412 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC_PRE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001413 COMMAND
1414 ${GPERF}
Daniel Leung28c35122021-03-18 12:02:19 -07001415 --output-file ${KOBJECT_HASH_OUTPUT_SRC_PRE}
Daniel Leung11171692021-03-18 14:00:07 -07001416 --multiple-iterations 10
Daniel Leung28c35122021-03-18 12:02:19 -07001417 ${KOBJECT_HASH_LIST}
1418 DEPENDS kobj_hash_list ${KOBJECT_HASH_LIST}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001419 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1420 )
Daniel Leung28c35122021-03-18 12:02:19 -07001421 add_custom_target(
1422 kobj_hash_output_src_pre
1423 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC_PRE}
1424 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001425
1426 # For our purposes the code/data generated by gperf is not optimal.
1427 #
Daniel Leung28c35122021-03-18 12:02:19 -07001428 # The script PROCESS_GPERF creates a new c file KOBJECT_HASH_OUTPUT_SRC based on
1429 # KOBJECT_HASH_OUTPUT_SRC_PRE to greatly reduce the amount of code/data generated
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001430 # since we know we are always working with pointer values
1431 add_custom_command(
Daniel Leung28c35122021-03-18 12:02:19 -07001432 OUTPUT ${KOBJECT_HASH_OUTPUT_SRC}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001433 COMMAND
Sebastian Bøe1b600702018-06-21 14:34:42 +02001434 ${PYTHON_EXECUTABLE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001435 ${PROCESS_GPERF}
Daniel Leung28c35122021-03-18 12:02:19 -07001436 -i ${KOBJECT_HASH_OUTPUT_SRC_PRE}
1437 -o ${KOBJECT_HASH_OUTPUT_SRC}
Anas Nashifa6b49002023-09-26 21:37:25 +00001438 -p "struct k_object"
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001439 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Torsten Rasmussen04543512022-02-03 15:14:44 +01001440 DEPENDS kobj_hash_output_src_pre ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC_PRE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001441 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1442 )
Daniel Leung28c35122021-03-18 12:02:19 -07001443 add_custom_target(
1444 kobj_hash_output_src
1445 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC}
1446 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001447
1448 # We need precise control of where generated text/data ends up in the final
1449 # kernel image. Disable function/data sections and use objcopy to move
1450 # generated data into special section names
Daniel Leung28c35122021-03-18 12:02:19 -07001451 add_library(
1452 kobj_hash_output_lib
Torsten Rasmussen47304d42021-11-02 12:06:05 +01001453 OBJECT ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001454 )
1455
Daniel Leung28c35122021-03-18 12:02:19 -07001456 set_source_files_properties(${KOBJECT_HASH_OUTPUT_SRC}
1457 PROPERTIES COMPILE_FLAGS
Andrew Boiea5148982019-03-14 17:04:11 -07001458 "${NO_COVERAGE_FLAGS} -fno-function-sections -fno-data-sections")
Adithya Baglody4b3c7b32018-11-21 14:31:56 +05301459
Torsten Rasmussen47304d42021-11-02 12:06:05 +01001460 target_compile_definitions(kobj_hash_output_lib
1461 PRIVATE $<TARGET_PROPERTY:zephyr_interface,INTERFACE_COMPILE_DEFINITIONS>
1462 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001463
Torsten Rasmussen47304d42021-11-02 12:06:05 +01001464 target_include_directories(kobj_hash_output_lib
1465 PUBLIC $<TARGET_PROPERTY:zephyr_interface,INTERFACE_INCLUDE_DIRECTORIES>
1466 )
Adithya Baglody4b3c7b32018-11-21 14:31:56 +05301467
Torsten Rasmussen47304d42021-11-02 12:06:05 +01001468 target_include_directories(kobj_hash_output_lib SYSTEM
1469 PUBLIC $<TARGET_PROPERTY:zephyr_interface,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
Daniel Leung28c35122021-03-18 12:02:19 -07001470 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001471
1472 add_custom_command(
Daniel Leung28c35122021-03-18 12:02:19 -07001473 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001474 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1475 $<TARGET_PROPERTY:bintools,elfconvert_flag>
Flavio Ceolin02d52902022-12-20 13:29:51 -08001476 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.literal=.kobject_data.literal
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001477 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.data=.kobject_data.data
Jim Shu70e1dee2021-07-08 01:11:34 +08001478 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.sdata=.kobject_data.sdata
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001479 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.text=.kobject_data.text
1480 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.rodata=.kobject_data.rodata
Torsten Rasmussen47304d42021-11-02 12:06:05 +01001481 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>$<TARGET_OBJECTS:kobj_hash_output_lib>
Daniel Leung28c35122021-03-18 12:02:19 -07001482 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001483 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Torsten Rasmussen04543512022-02-03 15:14:44 +01001484 DEPENDS kobj_hash_output_lib $<TARGET_OBJECTS:kobj_hash_output_lib>
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001485 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001486 COMMAND_EXPAND_LISTS
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001487 )
Daniel Leung28c35122021-03-18 12:02:19 -07001488 add_custom_target(
1489 kobj_hash_output_obj_renamed
1490 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
1491 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001492
Daniel Leung28c35122021-03-18 12:02:19 -07001493 add_library(kobj_hash_output_obj_renamed_lib STATIC IMPORTED GLOBAL)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001494 set_property(
Daniel Leung28c35122021-03-18 12:02:19 -07001495 TARGET kobj_hash_output_obj_renamed_lib
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001496 PROPERTY
Daniel Leung28c35122021-03-18 12:02:19 -07001497 IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001498 )
1499 add_dependencies(
Daniel Leung28c35122021-03-18 12:02:19 -07001500 kobj_hash_output_obj_renamed_lib
1501 kobj_hash_output_obj_renamed
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001502 )
1503
Daniel Leung28c35122021-03-18 12:02:19 -07001504 set_property(
1505 GLOBAL APPEND PROPERTY
1506 GENERATED_KERNEL_OBJECT_FILES kobj_hash_output_obj_renamed_lib
1507 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001508endif()
1509
Daniel Leungc7459952021-03-19 12:09:05 -07001510configure_linker_script(
Jordan Yates28b2e552021-10-20 20:19:28 +10001511 ${ZEPHYR_CURRENT_LINKER_CMD}
1512 "${LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE}"
Daniel Leungc7459952021-03-19 12:09:05 -07001513 ${APP_SMEM_ALIGNED_DEP}
Daniel Leung11171692021-03-18 14:00:07 -07001514 ${KOBJECT_LINKER_DEP}
Daniel Leungc7459952021-03-19 12:09:05 -07001515 ${CODE_RELOCATION_DEP}
1516 zephyr_generated_headers
1517 )
1518
1519add_custom_target(
1520 linker_zephyr_prebuilt_script_target
1521 DEPENDS
Jordan Yates28b2e552021-10-20 20:19:28 +10001522 ${ZEPHYR_CURRENT_LINKER_CMD}
Daniel Leungc7459952021-03-19 12:09:05 -07001523 )
1524
1525set_property(TARGET
1526 linker_zephyr_prebuilt_script_target
1527 PROPERTY INCLUDE_DIRECTORIES
1528 ${ZEPHYR_INCLUDE_DIRS}
1529 )
1530
Jordan Yates28b2e552021-10-20 20:19:28 +10001531# Read global variables into local variables
1532get_property(GASF GLOBAL PROPERTY GENERATED_APP_SOURCE_FILES)
1533get_property(GKOF GLOBAL PROPERTY GENERATED_KERNEL_OBJECT_FILES)
1534get_property(GKSF GLOBAL PROPERTY GENERATED_KERNEL_SOURCE_FILES)
1535
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001536# FIXME: Is there any way to get rid of empty_file.c?
Jordan Yates28b2e552021-10-20 20:19:28 +10001537add_executable( ${ZEPHYR_LINK_STAGE_EXECUTABLE} misc/empty_file.c ${GASF})
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001538toolchain_ld_link_elf(
Jordan Yates28b2e552021-10-20 20:19:28 +10001539 TARGET_ELF ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1540 OUTPUT_MAP ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001541 LIBRARIES_PRE_SCRIPT ""
Jordan Yates28b2e552021-10-20 20:19:28 +10001542 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD}
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001543 DEPENDENCIES ${CODE_RELOCATION_DEP}
1544)
Alberto Escolar Piedrasfeaf0ff2023-06-07 10:28:22 +02001545target_link_libraries_ifdef(CONFIG_NATIVE_LIBRARY ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1546 $<TARGET_PROPERTY:linker,partial_linking>)
Jordan Yates28b2e552021-10-20 20:19:28 +10001547target_byproducts(TARGET ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1548 BYPRODUCTS ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +01001549)
Torsten Rasmussenb6095a82023-04-13 12:10:08 +02001550set(BYPRODUCT_KERNEL_ELF_NAME "${PROJECT_BINARY_DIR}/${KERNEL_ELF_NAME}" CACHE FILEPATH "Kernel elf file" FORCE)
Daniel Leungc7459952021-03-19 12:09:05 -07001551set_property(TARGET
Jordan Yates28b2e552021-10-20 20:19:28 +10001552 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1553 PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD}
Daniel Leungc7459952021-03-19 12:09:05 -07001554 )
1555add_dependencies(
Jordan Yates28b2e552021-10-20 20:19:28 +10001556 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
Daniel Leungc7459952021-03-19 12:09:05 -07001557 linker_zephyr_prebuilt_script_target
1558 ${OFFSETS_LIB}
1559 )
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +02001560
Marc Herbert498b4942019-04-16 23:30:52 -07001561set(generated_kernel_files ${GKSF} ${GKOF})
1562if(NOT generated_kernel_files)
1563 # Use the prebuilt elf as the final elf since we don't have a
1564 # generation stage.
Jordan Yates28b2e552021-10-20 20:19:28 +10001565 set(logical_target_for_zephyr_elf ${ZEPHYR_LINK_STAGE_EXECUTABLE})
Marc Herbert498b4942019-04-16 23:30:52 -07001566else()
Daniel Leungcdd02a92021-03-19 12:18:52 -07001567 # The final linker pass uses the same source linker script of the
1568 # previous passes, but this time with a different output
1569 # file and preprocessed with the define LINKER_ZEPHYR_FINAL.
Mark Ruvald Pedersen4c811972019-04-29 17:16:54 +02001570 configure_linker_script(
Daniel Leungcdd02a92021-03-19 12:18:52 -07001571 linker.cmd
Torsten Rasmussen1fa3f152021-11-01 12:53:28 +01001572 "LINKER_ZEPHYR_FINAL"
Sebastian Bøe2a963122019-02-08 15:49:57 +01001573 ${CODE_RELOCATION_DEP}
Jordan Yates28b2e552021-10-20 20:19:28 +10001574 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
Sebastian Bøefdac7b32020-01-23 15:39:17 +01001575 zephyr_generated_headers
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +01001576 )
Andy Grosse8860fe2018-02-01 01:12:32 -06001577
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001578 add_custom_target(
Daniel Leungcdd02a92021-03-19 12:18:52 -07001579 linker_zephyr_final_script_target
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001580 DEPENDS
Daniel Leungcdd02a92021-03-19 12:18:52 -07001581 linker.cmd
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001582 )
Sebastian Bøeb1ab5012017-12-14 13:03:23 +01001583 set_property(TARGET
Daniel Leungcdd02a92021-03-19 12:18:52 -07001584 linker_zephyr_final_script_target
Sebastian Bøeb1ab5012017-12-14 13:03:23 +01001585 PROPERTY INCLUDE_DIRECTORIES
1586 ${ZEPHYR_INCLUDE_DIRS}
1587 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001588
Jordan Yates8d932172021-10-20 20:09:39 +10001589 add_executable( ${ZEPHYR_FINAL_EXECUTABLE} misc/empty_file.c ${GASF} ${GKSF})
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001590 toolchain_ld_link_elf(
1591 TARGET_ELF ${ZEPHYR_FINAL_EXECUTABLE}
Marc Herbert0370c9b2019-06-13 16:15:44 -07001592 OUTPUT_MAP ${PROJECT_BINARY_DIR}/${ZEPHYR_FINAL_EXECUTABLE}.map
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001593 LIBRARIES_PRE_SCRIPT ${GKOF}
Daniel Leungcdd02a92021-03-19 12:18:52 -07001594 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/linker.cmd
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001595 LIBRARIES_POST_SCRIPT ""
1596 DEPENDENCIES ${CODE_RELOCATION_DEP}
1597 )
Daniel Leungcdd02a92021-03-19 12:18:52 -07001598 set_property(TARGET ${ZEPHYR_FINAL_EXECUTABLE} PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker.cmd)
1599 add_dependencies( ${ZEPHYR_FINAL_EXECUTABLE} linker_zephyr_final_script_target)
Mark Ruvald Pedersen37d49472019-05-07 15:20:20 +02001600
1601 # Use the pass2 elf as the final elf
1602 set(logical_target_for_zephyr_elf ${ZEPHYR_FINAL_EXECUTABLE})
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001603endif()
1604
Sebastian Bøe0fc39342018-10-16 13:25:04 +02001605# Export the variable to the application's scope to allow the
1606# application to know what the name of the final elf target is.
1607set(logical_target_for_zephyr_elf ${logical_target_for_zephyr_elf} PARENT_SCOPE)
1608
Marc Herbert0370c9b2019-06-13 16:15:44 -07001609# Override the base name of the last, "logical" .elf output (and last .map) so:
Marc Herbert498b4942019-04-16 23:30:52 -07001610# 1. it doesn't depend on the number of passes above and the
1611# post_build_commands below can always find it no matter which is it;
1612# 2. it can be defined in Kconfig
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001613set_target_properties(${logical_target_for_zephyr_elf} PROPERTIES OUTPUT_NAME ${KERNEL_NAME})
1614
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001615set(post_build_commands "")
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001616set(post_build_byproducts "")
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001617
Marc Herbert0370c9b2019-06-13 16:15:44 -07001618list(APPEND
1619 post_build_commands
1620 COMMAND
Mark Ruvald Pedersend8df8012022-03-16 13:21:39 +01001621 ${CMAKE_COMMAND} -E copy ${logical_target_for_zephyr_elf}.map ${KERNEL_MAP_NAME}
Marc Herbert0370c9b2019-06-13 16:15:44 -07001622)
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +01001623list(APPEND post_build_byproducts ${KERNEL_MAP_NAME})
Marc Herbert0370c9b2019-06-13 16:15:44 -07001624
Håkon Øye Amundsenc086b932018-11-26 09:47:16 +00001625if(NOT CONFIG_BUILD_NO_GAP_FILL)
1626 # Use ';' as separator to get proper space in resulting command.
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001627 set(GAP_FILL "$<TARGET_PROPERTY:bintools,elfconvert_flag_gapfill>0xff")
Håkon Øye Amundsenc086b932018-11-26 09:47:16 +00001628endif()
1629
Danny Oerndrupc41e7122019-07-18 15:16:39 +02001630if(CONFIG_OUTPUT_PRINT_MEMORY_USAGE)
Torsten Rasmussend537be02021-02-02 21:06:11 +01001631 target_link_libraries(${logical_target_for_zephyr_elf} $<TARGET_PROPERTY:linker,memusage>)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001632
1633 get_property(memusage_build_command TARGET bintools PROPERTY memusage_command)
1634 if(memusage_build_command)
1635 # Note: The use of generator expressions allows downstream extensions to add/change the post build.
1636 # Unfortunately, the BYPRODUCTS does not allow for generator expression, so question is if we
1637 # should remove the downstream ability from start.
1638 # Or fix the output name, by the use of `get_property`
1639 list(APPEND
1640 post_build_commands
Torsten Rasmussen571f48f2020-09-04 21:07:46 +02001641 COMMAND $<TARGET_PROPERTY:bintools,memusage_command>
1642 $<TARGET_PROPERTY:bintools,memusage_flag>
1643 $<TARGET_PROPERTY:bintools,memusage_infile>${KERNEL_ELF_NAME}
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001644 )
1645
1646 # For now, the byproduct can only be supported upstream on byproducts name,
1647 # cause byproduct does not support generator expressions
1648 get_property(memusage_byproducts TARGET bintools PROPERTY memusage_byproducts)
1649 list(APPEND
1650 post_build_byproducts
1651 ${memusage_byproducts}
1652 )
1653 endif()
Danny Oerndrupc41e7122019-07-18 15:16:39 +02001654endif()
1655
Torsten Rasmussend51a67b2022-01-12 14:21:07 +01001656if(CONFIG_BUILD_OUTPUT_ADJUST_LMA)
1657 math(EXPR adjustment "${CONFIG_BUILD_OUTPUT_ADJUST_LMA}" OUTPUT_FORMAT DECIMAL)
Grzegorz Swiderskif2dc4a02024-01-24 09:41:20 +01001658 set(args_adjustment ${CONFIG_BUILD_OUTPUT_ADJUST_LMA_SECTIONS})
1659 list(TRANSFORM args_adjustment PREPEND $<TARGET_PROPERTY:bintools,elfconvert_flag_lma_adjust>)
1660 list(TRANSFORM args_adjustment APPEND +${adjustment})
Torsten Rasmussend51a67b2022-01-12 14:21:07 +01001661 list(APPEND
1662 post_build_commands
1663 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1664 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Grzegorz Swiderskif2dc4a02024-01-24 09:41:20 +01001665 ${args_adjustment}
Torsten Rasmussend51a67b2022-01-12 14:21:07 +01001666 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1667 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_ELF_NAME}
1668 )
1669endif()
1670
Stephanos Ioannidis404e7a92022-12-09 19:43:43 +09001671if(NOT CONFIG_CPP_EXCEPTIONS)
Chen Peng18c069c32022-03-07 18:36:53 +08001672 set(eh_frame_section ".eh_frame")
1673else()
1674 set(eh_frame_section "")
1675endif()
1676set(remove_sections_argument_list "")
1677foreach(section .comment COMMON ${eh_frame_section})
1678 list(APPEND remove_sections_argument_list
1679 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>${section})
1680endforeach()
1681
Kumar Galad5419132019-08-13 13:44:20 -05001682if(CONFIG_BUILD_OUTPUT_HEX OR BOARD_FLASH_RUNNER STREQUAL openocd)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001683 get_property(elfconvert_formats TARGET bintools PROPERTY elfconvert_formats)
1684 if(ihex IN_LIST elfconvert_formats)
1685 list(APPEND
1686 post_build_commands
1687 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1688 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1689 ${GAP_FILL}
1690 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>ihex
Chen Peng18c069c32022-03-07 18:36:53 +08001691 ${remove_sections_argument_list}
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001692 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1693 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_HEX_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001694 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001695 )
1696 list(APPEND
1697 post_build_byproducts
1698 ${KERNEL_HEX_NAME}
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001699 )
Torsten Rasmussenb6095a82023-04-13 12:10:08 +02001700 set(BYPRODUCT_KERNEL_HEX_NAME "${PROJECT_BINARY_DIR}/${KERNEL_HEX_NAME}" CACHE FILEPATH "Kernel hex file" FORCE)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001701 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001702endif()
Anas Nashif4592ff22017-11-23 07:54:26 -05001703
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001704if(CONFIG_BUILD_OUTPUT_BIN)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001705 get_property(elfconvert_formats TARGET bintools PROPERTY elfconvert_formats)
1706 if(binary IN_LIST elfconvert_formats)
1707 list(APPEND
1708 post_build_commands
1709 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1710 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1711 ${GAP_FILL}
1712 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>binary
Chen Peng18c069c32022-03-07 18:36:53 +08001713 ${remove_sections_argument_list}
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001714 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1715 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_BIN_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001716 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001717 )
1718 list(APPEND
1719 post_build_byproducts
1720 ${KERNEL_BIN_NAME}
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001721 )
Torsten Rasmussenb6095a82023-04-13 12:10:08 +02001722 set(BYPRODUCT_KERNEL_BIN_NAME "${PROJECT_BINARY_DIR}/${KERNEL_BIN_NAME}" CACHE FILEPATH "Kernel binary file" FORCE)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001723 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001724endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001725
Pete Johanson310a4642020-12-31 16:51:52 -05001726if(CONFIG_BUILD_OUTPUT_BIN AND CONFIG_BUILD_OUTPUT_UF2)
Peter Johanson3f332072022-01-27 00:45:27 -05001727 if(CONFIG_BUILD_OUTPUT_UF2_USE_FLASH_BASE)
1728 set(flash_addr "${CONFIG_FLASH_BASE_ADDRESS}")
1729 else()
1730 set(flash_addr "${CONFIG_FLASH_LOAD_OFFSET}")
1731 endif()
1732
1733 if(CONFIG_BUILD_OUTPUT_UF2_USE_FLASH_OFFSET)
1734 # Note, the `+ 0` in formula below avoids errors in cases where a Kconfig
1735 # variable is undefined and thus expands to nothing.
1736 math(EXPR flash_addr
1737 "${flash_addr} + ${CONFIG_FLASH_LOAD_OFFSET} + 0"
1738 OUTPUT_FORMAT HEXADECIMAL
1739 )
1740 endif()
1741
Pete Johanson310a4642020-12-31 16:51:52 -05001742 list(APPEND
1743 post_build_commands
Anas Nashifa8a97662022-07-11 10:57:15 -04001744 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/build/uf2conv.py
Pete Johanson310a4642020-12-31 16:51:52 -05001745 -c
1746 -f ${CONFIG_BUILD_OUTPUT_UF2_FAMILY_ID}
Peter Johanson3f332072022-01-27 00:45:27 -05001747 -b ${flash_addr}
Pete Johanson310a4642020-12-31 16:51:52 -05001748 -o ${KERNEL_UF2_NAME}
1749 ${KERNEL_BIN_NAME}
1750 )
1751 list(APPEND
1752 post_build_byproducts
1753 ${KERNEL_UF2_NAME}
1754 )
Torsten Rasmussenb6095a82023-04-13 12:10:08 +02001755 set(BYPRODUCT_KERNEL_UF2_NAME "${PROJECT_BINARY_DIR}/${KERNEL_UF2_NAME}" CACHE FILEPATH "Kernel uf2 file" FORCE)
Pete Johanson310a4642020-12-31 16:51:52 -05001756endif()
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001757
Thomas Gagneret0d053182024-01-18 17:31:46 +01001758set(KERNEL_META_PATH ${PROJECT_BINARY_DIR}/${KERNEL_META_NAME} CACHE INTERNAL "")
Torsten Rasmussenfffaf052021-10-12 23:08:36 +02001759if(CONFIG_BUILD_OUTPUT_META)
1760 list(APPEND
1761 post_build_commands
1762 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/zephyr_module.py
Torsten Rasmussenfffaf052021-10-12 23:08:36 +02001763 ${ZEPHYR_MODULES_ARG}
Torsten Rasmussena8850272023-05-17 13:59:28 +02001764 ${EXTRA_ZEPHYR_MODULES_ARG}
Thomas Gagneret7bde51b2023-12-05 17:41:06 +01001765 --meta-out ${KERNEL_META_PATH}
Thomas Gagneretc62dbb12024-01-17 15:56:59 +01001766 --zephyr-base=${ZEPHYR_BASE}
Torsten Rasmussen1a519932021-11-04 18:35:50 +01001767 $<$<BOOL:${CONFIG_BUILD_OUTPUT_META_STATE_PROPAGATE}>:--meta-state-propagate>
Torsten Rasmussenfffaf052021-10-12 23:08:36 +02001768 )
1769 list(APPEND
1770 post_build_byproducts
Thomas Gagneret7bde51b2023-12-05 17:41:06 +01001771 ${KERNEL_META_PATH}
Torsten Rasmussenfffaf052021-10-12 23:08:36 +02001772 )
Thomas Gagneret0d053182024-01-18 17:31:46 +01001773else(CONFIG_BUILD_OUTPUT_META)
1774 # Prevent spdx to use invalid data
1775 file(REMOVE ${KERNEL_META_PATH})
Torsten Rasmussenfffaf052021-10-12 23:08:36 +02001776endif()
1777
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001778# Cleanup intermediate files
1779if(CONFIG_CLEANUP_INTERMEDIATE_FILES)
Torsten Rasmussend2cdee52021-11-25 12:23:34 +01001780 foreach(index RANGE ${ZEPHYR_CURRENT_LINKER_PASS})
1781 # Those files can be very large in some cases, delete them as we do not need them.
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001782 list(APPEND
1783 post_build_commands
1784 COMMAND
Torsten Rasmussend2cdee52021-11-25 12:23:34 +01001785 ${CMAKE_COMMAND} -E remove zephyr_pre${index}.elf
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001786 )
Torsten Rasmussend2cdee52021-11-25 12:23:34 +01001787 endforeach()
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001788endif()
1789
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001790if(CONFIG_BUILD_OUTPUT_S19)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001791 get_property(elfconvert_formats TARGET bintools PROPERTY elfconvert_formats)
1792 if(srec IN_LIST elfconvert_formats)
1793 # Should we print a warning if case the tools does not support converting to s19 ?
1794 list(APPEND
1795 post_build_commands
1796 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1797 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1798 ${GAP_FILL}
1799 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>srec
1800 $<TARGET_PROPERTY:bintools,elfconvert_flag_srec_len>1
1801 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1802 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_S19_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001803 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001804 )
1805 list(APPEND
1806 post_build_byproducts
1807 ${KERNEL_S19_NAME}
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001808 )
Torsten Rasmussenb6095a82023-04-13 12:10:08 +02001809 set(BYPRODUCT_KERNEL_S19_NAME "${PROJECT_BINARY_DIR}/${KERNEL_S19_NAME}" CACHE FILEPATH "Kernel s19 file" FORCE)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001810 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001811endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001812
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001813if(CONFIG_OUTPUT_DISASSEMBLY)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001814if(CONFIG_OUTPUT_DISASSEMBLE_ALL)
1815 set(disassembly_type "$<TARGET_PROPERTY:bintools,disassembly_flag_all>")
Laurentiu Mihalceafb7e9372024-04-16 11:26:01 +03001816 elseif (CONFIG_OUTPUT_DISASSEMBLY_WITH_SOURCE)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001817 set(disassembly_type "$<TARGET_PROPERTY:bintools,disassembly_flag_inline_source>")
Rohit Gujarathi35713f22020-05-07 10:08:37 +05301818 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001819 list(APPEND
1820 post_build_commands
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001821 COMMAND $<TARGET_PROPERTY:bintools,disassembly_command>
1822 $<TARGET_PROPERTY:bintools,disassembly_flag>
1823 ${disassembly_type}
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001824 $<TARGET_PROPERTY:bintools,disassembly_flag_infile>${KERNEL_ELF_NAME}
1825 $<TARGET_PROPERTY:bintools,disassembly_flag_outfile>${KERNEL_LST_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001826 $<TARGET_PROPERTY:bintools,disassembly_flag_final>
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001827 )
1828 list(APPEND
1829 post_build_byproducts
1830 ${KERNEL_LST_NAME}
1831 )
1832endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001833
Anas Nashif47a673f2022-06-27 10:08:37 -04001834if(CONFIG_OUTPUT_SYMBOLS)
1835 list(APPEND
1836 post_build_commands
1837 COMMAND $<TARGET_PROPERTY:bintools,symbols_command>
1838 $<TARGET_PROPERTY:bintools,symbols_flag>
1839 $<TARGET_PROPERTY:bintools,symbols_infile>${KERNEL_ELF_NAME}
1840 $<TARGET_PROPERTY:bintools,symbols_outfile>${KERNEL_SYMBOLS_NAME}
1841 $<TARGET_PROPERTY:bintools,symbols_final>
1842 )
1843 list(APPEND
1844 post_build_byproducts
1845 ${KERNEL_SYMBOLS_NAME}
1846 )
1847endif()
1848
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001849if(CONFIG_OUTPUT_STAT)
1850 list(APPEND
1851 post_build_commands
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001852 COMMAND $<TARGET_PROPERTY:bintools,readelf_command>
1853 $<TARGET_PROPERTY:bintools,readelf_flag>
1854 $<TARGET_PROPERTY:bintools,readelf_flag_headers>
Torsten Rasmussen2d1a3d92021-05-20 17:38:57 +02001855 $<TARGET_PROPERTY:bintools,readelf_flag_infile>${KERNEL_ELF_NAME}
1856 $<TARGET_PROPERTY:bintools,readelf_flag_outfile>${KERNEL_STAT_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001857 $<TARGET_PROPERTY:bintools,readelf_flag_final>
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001858 )
1859 list(APPEND
1860 post_build_byproducts
1861 ${KERNEL_STAT_NAME}
1862 )
1863endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001864
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001865if(CONFIG_BUILD_OUTPUT_STRIPPED)
1866 list(APPEND
1867 post_build_commands
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001868 COMMAND $<TARGET_PROPERTY:bintools,strip_command>
1869 $<TARGET_PROPERTY:bintools,strip_flag>
1870 $<TARGET_PROPERTY:bintools,strip_flag_all>
1871 $<TARGET_PROPERTY:bintools,strip_flag_infile>${KERNEL_ELF_NAME}
1872 $<TARGET_PROPERTY:bintools,strip_flag_outfile>${KERNEL_STRIP_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001873 $<TARGET_PROPERTY:bintools,strip_flag_final>
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001874 )
1875 list(APPEND
1876 post_build_byproducts
1877 ${KERNEL_STRIP_NAME}
1878 )
1879endif()
1880
Noah Pendleton265da0d2024-05-08 06:21:52 -04001881if(CONFIG_BUILD_OUTPUT_COMPRESS_DEBUG_SECTIONS)
1882 list(APPEND
1883 post_build_commands
1884 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1885 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1886 $<TARGET_PROPERTY:bintools,elfconvert_flag_compress_debug_sections>
1887 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1888 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
1889)
1890endif()
1891
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001892if(CONFIG_BUILD_OUTPUT_EXE)
Alberto Escolar Piedrasece74b72023-06-07 10:39:48 +02001893 if (NOT CONFIG_NATIVE_LIBRARY)
1894 list(APPEND
1895 post_build_commands
1896 COMMAND
1897 ${CMAKE_COMMAND} -E copy ${KERNEL_ELF_NAME} ${KERNEL_EXE_NAME}
1898 )
1899 list(APPEND
1900 post_build_byproducts
1901 ${KERNEL_EXE_NAME}
1902 )
1903 else()
1904 if(CMAKE_GENERATOR STREQUAL "Unix Makefiles")
1905 set(MAKE "${CMAKE_MAKE_PROGRAM}" CACHE FILEPATH "cmake defined make")
1906 endif()
1907 find_program(MAKE make REQUIRED)
1908 add_custom_target(native_runner_executable
1909 ALL
1910 COMMENT "Building native simulator runner, and linking final executable"
1911 COMMAND
1912 ${MAKE} -f ${ZEPHYR_BASE}/scripts/native_simulator/Makefile all --warn-undefined-variables
Alberto Escolar Piedrasbc08e802023-11-14 15:23:55 +01001913 -r NSI_CONFIG_FILE=${APPLICATION_BINARY_DIR}/zephyr/NSI/nsi_config
Alberto Escolar Piedrasece74b72023-06-07 10:39:48 +02001914 # nsi_config is created by the board cmake file
1915 DEPENDS ${logical_target_for_zephyr_elf}
1916 BYPRODUCTS ${KERNEL_EXE_NAME}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001917 )
Alberto Escolar Piedrasece74b72023-06-07 10:39:48 +02001918 endif()
Alberto Escolar Piedras9bb708e2023-11-23 15:27:09 +01001919 set(BYPRODUCT_KERNEL_EXE_NAME "${PROJECT_BINARY_DIR}/${KERNEL_EXE_NAME}" CACHE FILEPATH "Kernel exe file" FORCE)
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001920endif()
Anas Nashif4592ff22017-11-23 07:54:26 -05001921
Torsten Rasmussenc8ddc342022-01-10 11:02:26 +01001922if(CONFIG_BUILD_OUTPUT_INFO_HEADER)
1923 list(APPEND
1924 post_build_commands
Anas Nashif09b4bec2022-07-11 10:55:13 -04001925 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/build/gen_image_info.py
Torsten Rasmussenc8ddc342022-01-10 11:02:26 +01001926 --elf-file=${KERNEL_ELF_NAME}
1927 --header-file=${PROJECT_BINARY_DIR}/include/public/zephyr_image_info.h
Torsten Rasmussend51a67b2022-01-12 14:21:07 +01001928 $<$<BOOL:${adjustment}>:--adjusted-lma=${adjustment}>
Torsten Rasmussenc8ddc342022-01-10 11:02:26 +01001929 )
1930 list(APPEND
1931 post_build_byproducts
1932 ${PROJECT_BINARY_DIR}/include/public/zephyr_image_info.h
1933 )
1934endif()
1935
Mathieu Choplain8aa6ae42024-05-06 13:03:26 +02001936if (CONFIG_LLEXT AND CONFIG_LLEXT_EXPORT_BUILTINS_BY_SLID)
1937 #slidgen must be the first post-build command to be executed
1938 #on the Zephyr ELF to ensure that all other commands, such as
1939 #binary file generation, are operating on a preparated ELF.
1940 list(PREPEND
1941 post_build_commands
1942 COMMAND ${PYTHON_EXECUTABLE}
1943 ${ZEPHYR_BASE}/scripts/build/llext_prepare_exptab.py
1944 --elf-file ${PROJECT_BINARY_DIR}/${KERNEL_ELF_NAME}
1945 --slid-listing ${PROJECT_BINARY_DIR}/slid_listing.txt
Mathieu Choplain8aa6ae42024-05-06 13:03:26 +02001946 )
1947
1948endif()
1949
Fabio Baltieri4a556a92023-09-18 11:35:35 +00001950if(NOT CMAKE_C_COMPILER_ID STREQUAL "ARMClang")
Alberto Escolar Piedras9bb708e2023-11-23 15:27:09 +01001951 set(check_init_priorities_input
1952 $<IF:$<TARGET_EXISTS:native_runner_executable>,${BYPRODUCT_KERNEL_EXE_NAME},${BYPRODUCT_KERNEL_ELF_NAME}>
1953 )
1954 set(check_init_priorities_command
1955 ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/build/check_init_priorities.py
1956 --elf-file=${check_init_priorities_input}
1957 )
1958 set(check_init_priorities_dependencies
1959 ${logical_target_for_zephyr_elf}
1960 $<$<TARGET_EXISTS:native_runner_executable>:native_runner_executable>
1961 )
1962
1963 if(CONFIG_CHECK_INIT_PRIORITIES)
Torsten Rasmussen9104afa2024-03-18 11:49:03 +01001964 if(TARGET native_runner_executable)
1965 add_custom_command(TARGET native_runner_executable POST_BUILD
1966 COMMAND ${check_init_priorities_command}
1967 )
1968 else()
1969 list(APPEND post_build_commands COMMAND ${check_init_priorities_command})
1970 endif()
Alberto Escolar Piedras9bb708e2023-11-23 15:27:09 +01001971 endif()
1972
Fabio Baltieri4a556a92023-09-18 11:35:35 +00001973 add_custom_target(
1974 initlevels
Alberto Escolar Piedras9bb708e2023-11-23 15:27:09 +01001975 COMMAND ${check_init_priorities_command} --initlevels
1976 DEPENDS ${check_init_priorities_dependencies}
Fabio Baltieri4a556a92023-09-18 11:35:35 +00001977 USES_TERMINAL
1978 )
1979endif()
Fabio Baltieri79ed6da2023-09-11 15:09:14 +00001980
Jamie McCraed6762342023-10-13 11:08:32 +01001981# Generate signed (MCUboot or other) related artifacts as needed. Priority is:
1982# * Sysbuild (if set)
1983# * SIGNING_SCRIPT target property (if set)
1984# * MCUboot signing script (if MCUboot is enabled)
1985zephyr_get(signing_script VAR SIGNING_SCRIPT SYSBUILD)
1986
1987if(NOT signing_script)
Jamie McCrae176c8052023-03-23 13:42:33 +00001988 get_target_property(signing_script zephyr_property_target SIGNING_SCRIPT)
Jamie McCraed6762342023-10-13 11:08:32 +01001989
1990 if(NOT signing_script AND CONFIG_BOOTLOADER_MCUBOOT)
1991 set(signing_script ${CMAKE_CURRENT_LIST_DIR}/cmake/mcuboot.cmake)
Jamie McCrae176c8052023-03-23 13:42:33 +00001992 endif()
1993endif()
1994
1995# Include signing script, if set
Jamie McCrae176c8052023-03-23 13:42:33 +00001996if(signing_script)
1997 message(STATUS "Including signing script: ${signing_script}")
1998
1999 include(${signing_script})
Martí Bolívarf66a0c32020-08-18 11:28:04 -07002000endif()
2001
Madhurima Paruchurifa738b02022-10-26 13:34:09 +00002002# Generate USB-C VIF policies in XML format
2003if (CONFIG_BUILD_OUTPUT_VIF)
2004 include(${CMAKE_CURRENT_LIST_DIR}/cmake/vif.cmake)
2005endif()
2006
Rajavardhan Gundiecdea1c2019-01-25 11:53:13 +05302007get_property(extra_post_build_commands
2008 GLOBAL PROPERTY
2009 extra_post_build_commands
2010 )
2011
2012list(APPEND
2013 post_build_commands
2014 ${extra_post_build_commands}
2015 )
2016
Sebastian Bøef483e5b2019-05-10 10:06:32 +02002017get_property(extra_post_build_byproducts
2018 GLOBAL PROPERTY
2019 extra_post_build_byproducts
2020 )
2021
2022list(APPEND
2023 post_build_byproducts
2024 ${extra_post_build_byproducts}
2025 )
2026
Krzysztof Chruscinski33923012022-03-29 15:47:01 +02002027if(CONFIG_LOG_DICTIONARY_DB)
Joakim Anderssonad58b392024-02-01 13:21:29 +01002028 set(LOG_DICT_DB_NAME ${PROJECT_BINARY_DIR}/log_dictionary.json)
2029 set(LOG_DICT_DB_NAME_ARG --json)
Daniel Leungba488d12022-02-04 13:18:13 -08002030elseif(CONFIG_LOG_MIPI_SYST_USE_CATALOG)
Joakim Anderssonad58b392024-02-01 13:21:29 +01002031 set(LOG_DICT_DB_NAME ${PROJECT_BINARY_DIR}/mipi_syst_collateral.xml)
2032 set(LOG_DICT_DB_NAME_ARG --syst)
Daniel Leungba488d12022-02-04 13:18:13 -08002033endif()
Daniel Leunga5ab1a72021-04-02 12:54:53 -07002034
Joakim Anderssonad58b392024-02-01 13:21:29 +01002035if(LOG_DICT_DB_NAME_ARG)
Daniel Leung531557a2024-05-16 10:42:02 -07002036 set(log_dict_gen_command
Joakim Anderssonad58b392024-02-01 13:21:29 +01002037 ${PYTHON_EXECUTABLE}
2038 ${ZEPHYR_BASE}/scripts/logging/dictionary/database_gen.py
2039 ${KERNEL_ELF_NAME}
2040 ${LOG_DICT_DB_NAME_ARG}=${LOG_DICT_DB_NAME}
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +08002041 --build-header ${PROJECT_BINARY_DIR}/include/generated/zephyr/version.h
Joakim Anderssonad58b392024-02-01 13:21:29 +01002042 )
Daniel Leung531557a2024-05-16 10:42:02 -07002043
2044 if (NOT CONFIG_LOG_DICTIONARY_DB_TARGET)
2045 # If not using a separate target for generating logging dictionary
2046 # database, add the generation to post build command to make sure
2047 # the database is actually being generated.
2048 list(APPEND
2049 post_build_commands
2050 COMMAND ${CMAKE_COMMAND} -E echo "Generating logging dictionary database: ${LOG_DICT_DB_NAME}"
2051 COMMAND ${log_dict_gen_command}
2052 )
2053 list(APPEND
2054 post_build_byproducts
2055 ${LOG_DICT_DB_NAME}
2056 )
2057 else()
2058 # Seprate build target for generating logging dictionary database.
2059 # This needs to be explicitly called/used to generate the database.
2060 add_custom_command(
2061 OUTPUT ${LOG_DICT_DB_NAME}
2062 COMMAND ${log_dict_gen_command}
2063 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
2064 COMMENT "Generating logging dictionary database: ${LOG_DICT_DB_NAME}"
2065 DEPENDS ${logical_target_for_zephyr_elf}
2066 )
2067 add_custom_target(log_dict_db_gen DEPENDS ${LOG_DICT_DB_NAME})
2068 endif()
Daniel Leunga5ab1a72021-04-02 12:54:53 -07002069endif()
2070
Marc Herbert498b4942019-04-16 23:30:52 -07002071# Add post_build_commands to post-process the final .elf file produced by
Jordan Yates28b2e552021-10-20 20:19:28 +10002072# either the ZEPHYR_LINK_STAGE_EXECUTABLE or the KERNEL_ELF executable
Marc Herbert498b4942019-04-16 23:30:52 -07002073# targets above.
Sebastian Bøe12f8f762017-10-27 15:43:34 +02002074add_custom_command(
2075 TARGET ${logical_target_for_zephyr_elf}
2076 POST_BUILD
Sigurd Hellesviked040f12023-12-06 10:20:34 +01002077 COMMAND ${CMAKE_COMMAND} -E echo "Generating files from ${PROJECT_BINARY_DIR}/${KERNEL_ELF_NAME} for board: ${BOARD}"
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01002078 ${post_build_commands}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02002079 BYPRODUCTS
2080 ${post_build_byproducts}
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02002081 COMMAND_EXPAND_LISTS
Rajavardhan Gundiecdea1c2019-01-25 11:53:13 +05302082 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02002083
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00002084# To populate with hex files to merge, do the following:
2085# set_property(GLOBAL APPEND PROPERTY HEX_FILES_TO_MERGE ${my_local_list})
2086# Note that the zephyr.hex file will not be included automatically.
2087get_property(HEX_FILES_TO_MERGE GLOBAL PROPERTY HEX_FILES_TO_MERGE)
2088if(HEX_FILES_TO_MERGE)
2089 # Merge in out-of-tree hex files.
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00002090 set(MERGED_HEX_NAME merged.hex)
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00002091
2092 add_custom_command(
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00002093 OUTPUT ${MERGED_HEX_NAME}
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00002094 COMMAND
2095 ${PYTHON_EXECUTABLE}
Anas Nashif72e7fa82022-07-11 10:55:37 -04002096 ${ZEPHYR_BASE}/scripts/build/mergehex.py
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00002097 -o ${MERGED_HEX_NAME}
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00002098 ${HEX_FILES_TO_MERGE}
2099 DEPENDS ${HEX_FILES_TO_MERGE} ${logical_target_for_zephyr_elf}
2100 )
2101
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00002102 add_custom_target(mergehex ALL DEPENDS ${MERGED_HEX_NAME})
Torsten Rasmussend38da9d2020-06-30 09:55:54 +02002103 list(APPEND RUNNERS_DEPS mergehex)
Håkon Øye Amundsenc9a2a5e2020-01-03 08:25:03 +00002104
2105 message(VERBOSE "Merging hex files: ${HEX_FILES_TO_MERGE}")
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00002106endif()
2107
Filip Kokosinski94428042021-08-30 13:09:48 +02002108if(SUPPORTED_EMU_PLATFORMS)
2109 list(GET SUPPORTED_EMU_PLATFORMS 0 default_emu)
Anas Nashifa1d18102022-02-14 22:08:42 -05002110 if(EXISTS ${ZEPHYR_BASE}/cmake/emu/${default_emu}.cmake)
2111 add_custom_target(run DEPENDS run_${default_emu})
2112 endif()
Filip Kokosinski94428042021-08-30 13:09:48 +02002113
2114 foreach(EMU_PLATFORM ${SUPPORTED_EMU_PLATFORMS})
Anas Nashifa1d18102022-02-14 22:08:42 -05002115 if(EXISTS ${ZEPHYR_BASE}/cmake/emu/${EMU_PLATFORM}.cmake)
2116 include(${ZEPHYR_BASE}/cmake/emu/${EMU_PLATFORM}.cmake)
2117 endif()
Filip Kokosinski94428042021-08-30 13:09:48 +02002118 endforeach()
2119
2120 if(TARGET debugserver_${default_emu})
2121 add_custom_target(debugserver DEPENDS debugserver_${default_emu})
2122 endif()
Anas Nashiffd276ae2017-12-21 16:45:45 -05002123else()
2124 add_custom_target(run
2125 COMMAND
2126 ${CMAKE_COMMAND} -E echo
2127 "==================================================="
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +01002128 "Emulation/Simulation not supported with this board."
Anas Nashiffd276ae2017-12-21 16:45:45 -05002129 "==================================================="
2130 )
Anas Nashifc15d3c92017-11-21 18:54:55 -05002131endif()
2132
Sebastian Bøe12f8f762017-10-27 15:43:34 +02002133add_subdirectory(cmake/flash)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02002134add_subdirectory(cmake/usage)
2135add_subdirectory(cmake/reports)
2136
Marc Herbert83723102019-06-17 13:26:11 -07002137if(NOT CONFIG_TEST)
Andrew Boie411686f2018-05-24 13:18:36 -07002138if(CONFIG_ASSERT AND (NOT CONFIG_FORCE_NO_ASSERT))
Sebastian Bøefa8f9d42019-12-06 12:54:53 +01002139 message(WARNING "__ASSERT() statements are globally ENABLED")
Sebastian Bøe12f8f762017-10-27 15:43:34 +02002140endif()
Marc Herbert83723102019-06-17 13:26:11 -07002141endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +02002142
Vincent Wana2bc5142020-01-09 14:20:44 -08002143if(CONFIG_BOARD_DEPRECATED_RELEASE)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02002144 message(WARNING "
2145 WARNING: The board '${BOARD}' is deprecated and will be
Vincent Wana2bc5142020-01-09 14:20:44 -08002146 removed in version ${CONFIG_BOARD_DEPRECATED_RELEASE}"
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +01002147 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02002148endif()
Paul Sokolovsky1c6a7d72019-06-06 21:12:14 +03002149
Vincent Wan180b4df2020-01-08 17:10:51 -08002150if(CONFIG_SOC_DEPRECATED_RELEASE)
2151 message(WARNING "
2152 WARNING: The SoC '${SOC_NAME}' is deprecated and will be
2153 removed in version ${CONFIG_SOC_DEPRECATED_RELEASE}"
2154 )
2155endif()
2156
Sebastian Bøee50e12d2019-08-29 16:19:32 +02002157# In CMake projects, 'CMAKE_BUILD_TYPE' usually determines the
2158# optimization flag, but in Zephyr it is determined through
2159# Kconfig. Here we give a warning when there is a mismatch between the
2160# two in case the user is not aware of this.
2161set(build_types None Debug Release RelWithDebInfo MinSizeRel)
2162
2163if((CMAKE_BUILD_TYPE IN_LIST build_types) AND (NOT NO_BUILD_TYPE_WARNING))
2164 string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_uppercase)
Torsten Rasmussen05bb8552021-12-10 15:06:30 +01002165 # The CMAKE_C_FLAGS_<build_type> is a string, so we do a regex to see if the
2166 # optimization flag is present in that string.
2167 # To avoid false-positive matches, the flag must either be matched first
2168 # or last in string, or come after / followed by minimum a space.
2169 if(NOT (CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_uppercase} MATCHES "(^| )${OPTIMIZATION_FLAG}($| )"))
Sebastian Bøee50e12d2019-08-29 16:19:32 +02002170 message(WARNING "
2171 The CMake build type was set to '${CMAKE_BUILD_TYPE}', but the optimization flag was set to '${OPTIMIZATION_FLAG}'.
2172 This may be intentional and the warning can be turned off by setting the CMake variable 'NO_BUILD_TYPE_WARNING'"
2173 )
2174 endif()
2175endif()
2176
Ederson de Souzad156a032024-01-18 20:28:46 -08002177# Extension Development Kit (EDK) generation.
2178set(llext_edk_file ${PROJECT_BINARY_DIR}/${CONFIG_LLEXT_EDK_NAME}.tar.xz)
Ederson de Souzacd535b72024-04-13 16:03:10 -07002179
2180# TODO maybe generate flags for C CXX ASM
Luca Burellie3cd6fc2024-08-06 17:27:24 +02002181zephyr_get_compile_definitions_for_lang(C zephyr_defs)
Ederson de Souzacd535b72024-04-13 16:03:10 -07002182zephyr_get_compile_options_for_lang(C zephyr_flags)
2183
2184# Filter out non LLEXT and LLEXT_EDK flags - and add required ones
Luca Burellie3cd6fc2024-08-06 17:27:24 +02002185llext_filter_zephyr_flags(LLEXT_REMOVE_FLAGS ${zephyr_flags} llext_filt_flags)
2186llext_filter_zephyr_flags(LLEXT_EDK_REMOVE_FLAGS ${llext_filt_flags} llext_filt_flags)
Ederson de Souzacd535b72024-04-13 16:03:10 -07002187
Luca Burellie3cd6fc2024-08-06 17:27:24 +02002188set(llext_edk_cflags ${zephyr_defs} -DLL_EXTENSION_BUILD)
2189list(APPEND llext_edk_cflags ${llext_filt_flags})
Ederson de Souzacd535b72024-04-13 16:03:10 -07002190list(APPEND llext_edk_cflags ${LLEXT_APPEND_FLAGS})
2191list(APPEND llext_edk_cflags ${LLEXT_EDK_APPEND_FLAGS})
2192
Ederson de Souzad156a032024-01-18 20:28:46 -08002193add_custom_command(
2194 OUTPUT ${llext_edk_file}
Ederson de Souza967168a2024-02-27 15:05:47 -08002195 # Regenerate syscalls in case CONFIG_LLEXT_EDK_USERSPACE_ONLY
2196 COMMAND ${CMAKE_COMMAND}
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +08002197 -E make_directory edk/include/generated/zephyr
Ederson de Souza967168a2024-02-27 15:05:47 -08002198 COMMAND
2199 ${PYTHON_EXECUTABLE}
2200 ${ZEPHYR_BASE}/scripts/build/gen_syscalls.py
2201 --json-file ${syscalls_json} # Read this file
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +08002202 --base-output edk/include/generated/zephyr/syscalls # Write to this dir
Yong Cong Sin35704082024-05-24 22:09:31 +08002203 --syscall-dispatch edk/include/generated/zephyr/syscall_dispatch.c # Write this file
Ederson de Souza967168a2024-02-27 15:05:47 -08002204 --syscall-list ${edk_syscall_list_h}
2205 $<$<BOOL:${CONFIG_LLEXT_EDK_USERSPACE_ONLY}>:--userspace-only>
2206 ${SYSCALL_LONG_REGISTERS_ARG}
2207 ${SYSCALL_SPLIT_TIMEOUT_ARG}
Ederson de Souzad156a032024-01-18 20:28:46 -08002208 COMMAND ${CMAKE_COMMAND}
2209 -DPROJECT_BINARY_DIR=${PROJECT_BINARY_DIR}
2210 -DAPPLICATION_SOURCE_DIR=${APPLICATION_SOURCE_DIR}
Ederson de Souzaa7ac0492024-05-23 16:38:24 -07002211 -DINTERFACE_INCLUDE_DIRECTORIES="$<TARGET_PROPERTY:zephyr_interface,INTERFACE_INCLUDE_DIRECTORIES>"
Ederson de Souzad156a032024-01-18 20:28:46 -08002212 -Dllext_edk_file=${llext_edk_file}
Luca Burellic77bcd22024-08-06 17:24:20 +02002213 -Dllext_edk_cflags="${llext_edk_cflags}"
Ederson de Souzad156a032024-01-18 20:28:46 -08002214 -Dllext_edk_name=${CONFIG_LLEXT_EDK_NAME}
2215 -DWEST_TOPDIR=${WEST_TOPDIR}
2216 -DZEPHYR_BASE=${ZEPHYR_BASE}
Ederson de Souza967168a2024-02-27 15:05:47 -08002217 -DCONFIG_LLEXT_EDK_USERSPACE_ONLY=${CONFIG_LLEXT_EDK_USERSPACE_ONLY}
Ederson de Souzad156a032024-01-18 20:28:46 -08002218 -P ${ZEPHYR_BASE}/cmake/llext-edk.cmake
2219 DEPENDS ${logical_target_for_zephyr_elf}
2220 COMMAND_EXPAND_LISTS
2221)
2222add_custom_target(llext-edk DEPENDS ${llext_edk_file})
2223
Stephanos Ioannidisfd4700d2021-09-11 22:06:28 +09002224# @Intent: Set compiler specific flags for standard C/C++ includes
Paul Sokolovsky1c6a7d72019-06-06 21:12:14 +03002225# Done at the very end, so any other system includes which may
2226# be added by Zephyr components were first in list.
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02002227# Note, the compile flags are moved, but the system include is still present here.
2228zephyr_compile_options($<TARGET_PROPERTY:compiler,nostdinc>)
2229target_include_directories(zephyr_interface SYSTEM INTERFACE $<TARGET_PROPERTY:compiler,nostdinc_include>)
Torsten Rasmussena5917f02020-09-09 15:42:32 +02002230
Stephanos Ioannidiscf211aa2022-12-10 03:19:58 +09002231if(CONFIG_MINIMAL_LIBCPP)
Stephanos Ioannidisfd4700d2021-09-11 22:06:28 +09002232 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,nostdincxx>>)
2233endif()
2234
Torsten Rasmussena5917f02020-09-09 15:42:32 +02002235# Finally export all build flags from Zephyr
2236add_subdirectory_ifdef(
2237 CONFIG_MAKEFILE_EXPORTS
2238 cmake/makefile_exports
2239 )
Torsten Rasmussen2e3873a2024-08-06 10:13:31 +02002240
2241toolchain_linker_finalize()
Torsten Rasmussen09faf532024-08-09 16:43:20 +02002242
2243yaml_context(EXISTS NAME build_info result)
2244if(result)
2245 build_info(zephyr version VALUE ${PROJECT_VERSION_STR})
2246 build_info(zephyr zephyr-base VALUE ${ZEPHYR_BASE})
2247 yaml_save(NAME build_info)
2248endif()