1# SPDX-License-Identifier: Apache-2.0
2
3# *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
14if(NOT DEFINED ZEPHYR_BINARY_DIR)
15  message(FATAL_ERROR "A user error has occurred.
16cmake 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
24
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
38
39# 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.
42set(CMAKE_EXECUTABLE_SUFFIX .elf)
43
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:
57# - device dependencies structs must be generated (CONFIG_DEVICE_DEPS=y)
58# - 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`
78if(CONFIG_USERSPACE OR CONFIG_DEVICE_DEPS)
79  set(ZEPHYR_PREBUILT_EXECUTABLE zephyr_pre1)
80else()
81  set(ZEPHYR_PREBUILT_EXECUTABLE zephyr_pre0)
82endif()
83set(ZEPHYR_FINAL_EXECUTABLE zephyr_final)
84
85# Set some phony targets to collect dependencies
86set(OFFSETS_H_TARGET           offsets_h)
87set(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)
90set(PARSE_SYSCALLS_TARGET      parse_syscalls_target)
91
92define_property(GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT BRIEF_DOCS " " FULL_DOCS " ")
93set_property(   GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT elf32-little${ARCH}) # BFD format
94
95# 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
100# "zephyr_interface" is a source-less library that encapsulates all the global
101# 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.
104# https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#interface-libraries
105add_library(zephyr_interface INTERFACE)
106
107# "zephyr" is a catch-all CMake library for source files that can be
108# built purely with the include paths, defines, and other compiler
109# flags that come with zephyr_interface.
110zephyr_library_named(zephyr)
111
112zephyr_include_directories(
113  include
114  ${PROJECT_BINARY_DIR}/include/generated
115  ${USERINCLUDE}
116  ${STDINCLUDE}
117)
118
119include(${ZEPHYR_BASE}/cmake/linker_script/${ARCH}/linker.cmake OPTIONAL)
120
121zephyr_include_directories(${SOC_DIR}/${ARCH}/${SOC_PATH})
122
123# Don't inherit compiler flags from the environment
124foreach(var AFLAGS CFLAGS CXXFLAGS CPPFLAGS LDFLAGS)
125  if(DEFINED ENV{${var}})
126    message(WARNING "The environment variable '${var}' was set to $ENV{${var}}, "
127                    "but Zephyr ignores flags from the environment. Use 'cmake "
128                    "-DEXTRA_${var}=$ENV{${var}}' instead."
129    )
130    unset(ENV{${var}})
131  endif()
132endforeach()
133
134zephyr_compile_definitions(
135  KERNEL
136  __ZEPHYR__=1
137)
138
139# Ensure that include/zephyr/toolchain.h includes toolchain/other.h for all off-tree toolchains
140if(TOOLCHAIN_USE_CUSTOM)
141  zephyr_compile_definitions(__TOOLCHAIN_CUSTOM__)
142endif()
143
144# @Intent: Set compiler specific flag for disabling strict aliasing rule
145zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,no_strict_aliasing>>)
146zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,no_strict_aliasing>>)
147
148# Extra warnings options for twister run
149if (CONFIG_COMPILER_WARNINGS_AS_ERRORS)
150  zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warnings_as_errors>>)
151  zephyr_compile_options($<$<COMPILE_LANGUAGE:ASM>:$<TARGET_PROPERTY:asm,warnings_as_errors>>)
152  zephyr_link_libraries($<TARGET_PROPERTY:linker,warnings_as_errors>)
153endif()
154
155# @Intent: Set compiler flags to enable buffer overflow checks in libc functions
156# @details:
157#  Kconfig.zephyr "Detect buffer overflows in libc calls" is a kconfig choice,
158#  ensuring at most *one* of CONFIG_FORTIFY_SOURCE_{COMPILE_TIME,RUN_TIME} is
159#  set. Refer to Kconfig.zephyr for selection logic and description of these
160#  choices. Toolchains set both of the security_fortify_{compile_time,run_time}
161#  properties and the Kconfig settings are used here to select between those.
162#
163if(CONFIG_FORTIFY_SOURCE_RUN_TIME)
164  zephyr_compile_definitions($<TARGET_PROPERTY:compiler,security_fortify_run_time> )
165elseif(CONFIG_FORTIFY_SOURCE_COMPILE_TIME)
166  zephyr_compile_definitions($<TARGET_PROPERTY:compiler,security_fortify_compile_time> )
167endif()
168
169# @Intent: Set compiler flags to detect general stack overflows across all functions
170if(CONFIG_STACK_CANARIES)
171  zephyr_compile_options($<TARGET_PROPERTY:compiler,security_canaries>)
172endif()
173
174# @Intent: Obtain compiler optimizations flags and store in variables
175# @details:
176#   Kconfig.zephyr "Optimization level" is a kconfig choice, ensuring
177#   only *one* of CONFIG_{NO,DEBUG,SPEED,SIZE}_OPTIMIZATIONS is set.
178#   Refer to Kconfig.zephyr for selection logic and description of these choices.
179#   toolchain_cc_optimize_*() macros must provide the mapping from these kconfigs
180#   to compiler flags. Each macro will store the flags in a CMake variable, whose
181#   name is passed as argument (somewhat like by reference).
182#
183#   If the user wants to tweak the optimizations, there are two ways:
184#    1) Using EXTRA_CFLAGS which is applied regardless of kconfig choice, or
185#    2) Rely on override support being implemented by your toolchain_cc_optimize_*()
186#
187get_property(OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG TARGET compiler PROPERTY no_optimization)
188get_property(OPTIMIZE_FOR_DEBUG_FLAG TARGET compiler PROPERTY optimization_debug)
189get_property(OPTIMIZE_FOR_SPEED_FLAG TARGET compiler PROPERTY optimization_speed)
190get_property(OPTIMIZE_FOR_SIZE_FLAG  TARGET compiler PROPERTY optimization_size)
191
192# From kconfig choice, pick the actual OPTIMIZATION_FLAG to use.
193# Kconfig choice ensures only one of these CONFIG_*_OPTIMIZATIONS is set.
194if(CONFIG_NO_OPTIMIZATIONS)
195  set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG})
196elseif(CONFIG_DEBUG_OPTIMIZATIONS)
197  set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_DEBUG_FLAG})
198elseif(CONFIG_SPEED_OPTIMIZATIONS)
199  set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SPEED_FLAG})
200elseif(CONFIG_SIZE_OPTIMIZATIONS)
201  set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SIZE_FLAG}) # Default in kconfig
202else()
203  assert(0 "Unreachable code. Expected optimization level to have been chosen. See Kconfig.zephyr")
204endif()
205
206if(NOT CONFIG_ARCH_IS_SET)
207  message(WARNING "\
208None of the CONFIG_<arch> (e.g. CONFIG_X86) symbols are set. \
209Select one of them from the SOC_SERIES_* symbol or, lacking that, from the \
210SOC_* symbol.")
211endif()
212
213# Apply the final optimization flag(s)
214zephyr_compile_options(${OPTIMIZATION_FLAG})
215
216if(CONFIG_LTO)
217  add_compile_options($<TARGET_PROPERTY:compiler,optimization_lto>)
218  add_link_options($<TARGET_PROPERTY:linker,lto_arguments>)
219endif()
220
221# @Intent: Obtain compiler specific flags related to C++ that are not influenced by kconfig
222zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,required>>)
223
224# @Intent: Obtain compiler specific flags for compiling under different ISO standards of C++
225if(CONFIG_CPP)
226  # From kconfig choice, pick a single dialect.
227  # Kconfig choice ensures only one of these CONFIG_STD_CPP* is set.
228  if(CONFIG_STD_CPP98)
229    set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp98>)
230    list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp98})
231  elseif(CONFIG_STD_CPP11)
232    set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp11>) # Default in kconfig
233    list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp11})
234  elseif(CONFIG_STD_CPP14)
235    set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp14>)
236    list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp14})
237  elseif(CONFIG_STD_CPP17)
238    set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp17>)
239    list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp17})
240  elseif(CONFIG_STD_CPP2A)
241    set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp2a>)
242    list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp20})
243  elseif(CONFIG_STD_CPP20)
244    set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp20>)
245    list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp20})
246  elseif(CONFIG_STD_CPP2B)
247    set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp2b>)
248    list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp20})
249  else()
250    assert(0 "Unreachable code. Expected C++ standard to have been chosen. See Kconfig.zephyr.")
251  endif()
252  set(CMAKE_CXX_COMPILE_FEATURES ${CMAKE_CXX_COMPILE_FEATURES} PARENT_SCOPE)
253
254  zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:${STD_CPP_DIALECT_FLAGS}>)
255endif()
256
257if(NOT CONFIG_CPP_EXCEPTIONS)
258  # @Intent: Obtain compiler specific flags related to C++ Exceptions
259  zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,no_exceptions>>)
260endif()
261
262if(NOT CONFIG_CPP_RTTI)
263  # @Intent: Obtain compiler specific flags related to C++ Run Time Type Information
264  zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,no_rtti>>)
265endif()
266
267if(CONFIG_MISRA_SANE)
268  # @Intent: Obtain toolchain compiler flags relating to MISRA.
269  zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_misra_sane>>)
270  zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_error_misra_sane>>)
271endif()
272
273# This is intend to be temporary. Once we have fixed the violations that
274# prevents build Zephyr, these flags shall be part of the default flags.
275if(CONFIG_CODING_GUIDELINE_CHECK)
276  # @Intent: Obtain toolchain compiler flags relating to coding guideline
277  zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_coding_guideline>>)
278endif()
279
280# @Intent: Set compiler specific macro inclusion of AUTOCONF_H
281zephyr_compile_options("SHELL: $<TARGET_PROPERTY:compiler,imacros> ${AUTOCONF_H}")
282
283if(CONFIG_COMPILER_FREESTANDING)
284  # @Intent: Set compiler specific flag for bare metal freestanding option
285  zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,freestanding>>)
286  zephyr_compile_options($<$<COMPILE_LANGUAGE:ASM>:$<TARGET_PROPERTY:compiler,freestanding>>)
287endif()
288
289if (CONFIG_PICOLIBC AND NOT CONFIG_PICOLIBC_IO_FLOAT)
290  # @Intent: Set compiler specific flag to disable printf-related optimizations
291  zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,no_printf_return_value>>)
292endif()
293
294# @Intent: Set compiler specific flag for tentative definitions, no-common
295zephyr_compile_options($<TARGET_PROPERTY:compiler,no_common>)
296
297# @Intent: Set compiler specific flag for production of debug information
298zephyr_compile_options($<TARGET_PROPERTY:compiler,debug>)
299
300if(CONFIG_COMPILER_SAVE_TEMPS)
301  # @Intent: Set compiler specific flag for saving temporary object files
302  zephyr_compile_options($<TARGET_PROPERTY:compiler,save_temps>)
303endif()
304
305if(NOT CONFIG_COMPILER_TRACK_MACRO_EXPANSION)
306  # @Intent: Set compiler specific flags to not track macro expansion
307  zephyr_compile_options($<TARGET_PROPERTY:compiler,no_track_macro_expansion>)
308endif()
309
310if(CONFIG_COMPILER_COLOR_DIAGNOSTICS)
311# @Intent: Set compiler specific flag for diagnostic messages
312zephyr_compile_options($<TARGET_PROPERTY:compiler,diagnostic>)
313endif()
314
315zephyr_compile_options(
316  ${TOOLCHAIN_C_FLAGS}
317)
318
319# @Intent: Obtain compiler specific flags related to assembly
320# ToDo: Remember to get feedback from Oticon on this, as they might use the `ASM_BASE_FLAG` since this is done this way.
321zephyr_compile_options($<$<COMPILE_LANGUAGE:ASM>:$<TARGET_PROPERTY:asm,required>>)
322
323# @Intent: Enforce standard integer type correspondence to match Zephyr usage.
324# (must be after compiler specific flags)
325if(CONFIG_ENFORCE_ZEPHYR_STDINT)
326  zephyr_compile_options("SHELL: $<TARGET_PROPERTY:compiler,imacros> ${ZEPHYR_BASE}/include/zephyr/toolchain/zephyr_stdint.h")
327endif()
328
329# Common toolchain-agnostic assembly flags
330zephyr_compile_options(
331  $<$<COMPILE_LANGUAGE:ASM>:-D_ASMLANGUAGE>
332)
333
334# @Intent: Set fundamental linker specific flags
335toolchain_ld_base()
336
337toolchain_ld_force_undefined_symbols(
338  _OffsetAbsSyms
339  _ConfigAbsSyms
340)
341
342if(NOT CONFIG_NATIVE_BUILD)
343  # @Intent: Set linker specific flags for bare metal target
344  toolchain_ld_baremetal()
345endif()
346
347if(CONFIG_CPP AND NOT CONFIG_MINIMAL_LIBCPP AND NOT CONFIG_NATIVE_LIBRARY)
348  # @Intent: Set linker specific flags for C++
349  toolchain_ld_cpp()
350endif()
351
352# @Intent: Add the basic toolchain warning flags
353zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_base>>)
354zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_base>>)
355
356# ==========================================================================
357#
358# cmake -DW=... settings
359#
360# W=1 - warnings that may be relevant and does not occur too often
361# W=2 - warnings that occur quite often but may still be relevant
362# W=3 - the more obscure warnings, can most likely be ignored
363# ==========================================================================
364# @Intent: Add cmake -DW toolchain supported warnings, if any
365if(W MATCHES "1")
366  zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_dw_1>>)
367  zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_dw_1>>)
368endif()
369
370if(W MATCHES "2")
371  zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_dw_2>>)
372  zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_dw_2>>)
373endif()
374
375if(W MATCHES "3")
376  zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_dw_3>>)
377  zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_dw_3>>)
378endif()
379
380# @Intent: Add extended, more specific, toolchain warning flags
381zephyr_compile_options($<TARGET_PROPERTY:compiler,warning_extended>)
382
383# @Intent: Trigger an error when a declaration does not specify a type
384zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_implicit_int>>)
385zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_error_implicit_int>>)
386
387# @Intent: Do not make position independent code / executable
388zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,no_position_independent>>)
389zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler,no_position_independent>>)
390
391# In case of CONFIG_NATIVE_LIBRARY we also don't want position independent code,
392# but we cannot tell that to the linker yet as we are first only doing a
393# relocatable link into a static library
394zephyr_link_libraries_ifndef(CONFIG_NATIVE_LIBRARY
395                             $<TARGET_PROPERTY:linker,no_position_independent>)
396
397# Allow the user to inject options when calling cmake, e.g.
398# 'cmake -DEXTRA_CFLAGS="-Werror -Wno-deprecated-declarations" ..'
399include(cmake/extra_flags.cmake)
400
401zephyr_cc_option(-fno-asynchronous-unwind-tables)
402
403if(CONFIG_USERSPACE)
404  zephyr_compile_options($<TARGET_PROPERTY:compiler,no_global_merge>)
405endif()
406
407if(CONFIG_THREAD_LOCAL_STORAGE)
408# Only support local exec TLS model at this point.
409zephyr_cc_option(-ftls-model=local-exec)
410endif()
411
412if(CONFIG_OVERRIDE_FRAME_POINTER_DEFAULT)
413  if(CONFIG_OMIT_FRAME_POINTER)
414    zephyr_cc_option(-fomit-frame-pointer)
415  else()
416    zephyr_cc_option(-fno-omit-frame-pointer)
417  endif()
418endif()
419
420separate_arguments(COMPILER_OPT_AS_LIST UNIX_COMMAND ${CONFIG_COMPILER_OPT})
421zephyr_compile_options(${COMPILER_OPT_AS_LIST})
422
423# TODO: Include arch compiler options at this point.
424
425if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang" AND
426   NOT CMAKE_C_COMPILER_ID STREQUAL "IntelLLVM" AND
427   NOT CMAKE_C_COMPILER_ID STREQUAL "ARMClang")
428  # GCC assumed
429  zephyr_cc_option(-fno-reorder-functions)
430
431  # GCC 11 and above may generate a warning when dereferencing a constant
432  # address pointer whose address is below the value specified by the
433  # `min-pagesize` parameter (defaults to 0x1000). The `min-pagesize` parameter
434  # is set to 0 such that GCC never generates any warnings for the constant
435  # address pointers. For more details, refer to the GCC PR99578.
436  zephyr_cc_option(--param=min-pagesize=0)
437
438  if(NOT ${ZEPHYR_TOOLCHAIN_VARIANT} STREQUAL "xcc")
439    zephyr_cc_option(-fno-defer-pop)
440  endif()
441else()
442  # Clang produces false positive vla warnings
443  zephyr_cc_option(-Wno-vla)
444endif()
445
446zephyr_cc_option_ifdef(CONFIG_STACK_USAGE            -fstack-usage)
447
448# If the compiler supports it, strip the ${ZEPHYR_BASE} prefix from the
449# __FILE__ macro used in __ASSERT*, in the
450# .noinit."/home/joe/zephyr/fu/bar.c" section names and in any
451# application code. This saves some memory, stops leaking user locations
452# in binaries, makes failure logs more deterministic and most
453# importantly makes builds more deterministic
454if(CONFIG_BUILD_OUTPUT_STRIP_PATHS)
455  # If several match then the last one wins. This matters for instances
456  # like tests/ and samples/: they're inside all of them! Then let's
457  # strip as little as possible.
458  zephyr_cc_option(-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=CMAKE_SOURCE_DIR)
459  zephyr_cc_option(-fmacro-prefix-map=${ZEPHYR_BASE}=ZEPHYR_BASE)
460  if(WEST_TOPDIR)
461    zephyr_cc_option(-fmacro-prefix-map=${WEST_TOPDIR}=WEST_TOPDIR)
462  endif()
463endif()
464
465# TODO: Archiver arguments
466# ar_option(D)
467
468# Declare MPU userspace dependencies before the linker scripts to make
469# sure the order of dependencies are met
470if(CONFIG_USERSPACE)
471  add_custom_target(app_smem)
472  set(APP_SMEM_ALIGNED_DEP app_smem_aligned_linker)
473  set(APP_SMEM_UNALIGNED_DEP app_smem_unaligned_linker)
474endif()
475
476if(CONFIG_USERSPACE)
477  set(KOBJECT_LINKER_DEP kobject_linker)
478endif()
479
480if(DEFINED BUILD_VERSION)
481  set(build_version_argument "-DBUILD_VERSION=${BUILD_VERSION}")
482elseif(NOT ZEPHYR_GIT_INDEX)
483  if(EXISTS ${ZEPHYR_BASE}/.git/index)
484    set(ZEPHYR_GIT_INDEX ${ZEPHYR_BASE}/.git/index CACHE PATH
485      "Path to Zephyr git repository index file")
486  elseif(EXISTS ${ZEPHYR_BASE}/.git)
487    # Likely a git-submodule. Let's ask git where the real database is located.
488    find_package(Git QUIET)
489    if(GIT_FOUND)
490      execute_process(
491        COMMAND ${GIT_EXECUTABLE} rev-parse --absolute-git-dir
492        WORKING_DIRECTORY ${ZEPHYR_BASE}
493        OUTPUT_VARIABLE zephyr_git_dir
494        OUTPUT_STRIP_TRAILING_WHITESPACE
495        ERROR_STRIP_TRAILING_WHITESPACE
496        ERROR_VARIABLE stderr
497        RESULT_VARIABLE return_code)
498      if(return_code)
499        message(WARNING "BUILD_VERSION: git rev-parse failed: ${stderr}")
500      else()
501        if(NOT "${stderr}" STREQUAL "")
502          message(WARNING "BUILD_VERSION: git rev-parse warned: ${stderr}")
503        endif()
504        set(ZEPHYR_GIT_INDEX ${zephyr_git_dir}/index CACHE PATH
505          "Path to Zephyr git repository index file")
506      endif()
507    else()
508      message(WARNING "Could not find git installation, "
509        "please specify '-DBUILD_VERSION=<version>'")
510    endif()
511  else()
512    message(WARNING "ZEPHYR_BASE=${ZEPHYR_BASE} doesn't appear to be a git "
513      "repository, please specify '-DBUILD_VERSION=<version>'")
514  endif()
515endif()
516
517if(ZEPHYR_GIT_INDEX)
518  set(git_dependency ${ZEPHYR_GIT_INDEX})
519endif()
520
521add_custom_command(
522  OUTPUT ${PROJECT_BINARY_DIR}/include/generated/version.h
523  COMMAND ${CMAKE_COMMAND} -DZEPHYR_BASE=${ZEPHYR_BASE}
524    -DOUT_FILE=${PROJECT_BINARY_DIR}/include/generated/version.h
525    -DVERSION_TYPE=KERNEL
526    -DVERSION_FILE=${ZEPHYR_BASE}/VERSION
527    -DKERNEL_VERSION_CUSTOMIZATION="$<TARGET_PROPERTY:version_h,KERNEL_VERSION_CUSTOMIZATION>"
528    ${build_version_argument}
529    -P ${ZEPHYR_BASE}/cmake/gen_version_h.cmake
530  DEPENDS ${ZEPHYR_BASE}/VERSION ${git_dependency}
531  COMMAND_EXPAND_LISTS
532)
533add_custom_target(version_h DEPENDS ${PROJECT_BINARY_DIR}/include/generated/version.h)
534
535if(EXISTS ${APPLICATION_SOURCE_DIR}/VERSION)
536  add_custom_command(
537    OUTPUT ${PROJECT_BINARY_DIR}/include/generated/app_version.h
538    COMMAND ${CMAKE_COMMAND} -DZEPHYR_BASE=${ZEPHYR_BASE}
539      -DOUT_FILE=${PROJECT_BINARY_DIR}/include/generated/app_version.h
540      -DVERSION_TYPE=APP
541      -DVERSION_FILE=${APPLICATION_SOURCE_DIR}/VERSION
542      -DAPP_VERSION_CUSTOMIZATION="$<TARGET_PROPERTY:app_version_h,APP_VERSION_CUSTOMIZATION>"
543      ${build_version_argument}
544      -P ${ZEPHYR_BASE}/cmake/gen_version_h.cmake
545    DEPENDS ${APPLICATION_SOURCE_DIR}/VERSION ${git_dependency}
546    COMMAND_EXPAND_LISTS
547  )
548  add_custom_target(app_version_h DEPENDS ${PROJECT_BINARY_DIR}/include/generated/app_version.h)
549  add_dependencies(zephyr_interface app_version_h)
550endif()
551
552# Unfortunately, the order in which CMakeLists.txt code is processed
553# matters so we need to be careful about how we order the processing
554# of subdirectories. One example is "Compiler flags added late in the
555# build are not exported to external build systems #5605"; when we
556# integrate with an external build system we read out all compiler
557# flags when the external project is created. So an external project
558# defined in subsys or ext will not get global flags added by drivers/
559# or tests/ as the subdirectories are ordered now.
560#
561# Another example of when the order matters is the reading and writing
562# of global properties such as ZEPHYR_LIBS or
563# GENERATED_KERNEL_OBJECT_FILES.
564#
565# Arch is placed early because it defines important compiler flags
566# that must be exported to external build systems defined in
567# e.g. subsys/.
568add_subdirectory(arch)
569add_subdirectory(lib)
570# We use include instead of add_subdirectory to avoid creating a new directory scope.
571# This is because source file properties are directory scoped, including the GENERATED
572# property which is set implicitly for custom command outputs
573include(misc/generated/CMakeLists.txt)
574
575add_subdirectory(soc)
576add_subdirectory(boards)
577add_subdirectory(subsys)
578add_subdirectory(drivers)
579
580# Include zephyr modules generated CMake file.
581foreach(module_name ${ZEPHYR_MODULE_NAMES})
582  # Note the second, binary_dir parameter requires the added
583  # subdirectory to have its own, local cmake target(s). If not then
584  # this binary_dir is created but stays empty. Object files land in
585  # the main binary dir instead.
586  # https://cmake.org/pipermail/cmake/2019-June/069547.html
587  zephyr_string(SANITIZE TOUPPER MODULE_NAME_UPPER ${module_name})
588  if(NOT ${ZEPHYR_${MODULE_NAME_UPPER}_CMAKE_DIR} STREQUAL "")
589    set(ZEPHYR_CURRENT_MODULE_NAME ${ZEPHYR_${MODULE_NAME_UPPER}_MODULE_NAME})
590    set(ZEPHYR_CURRENT_MODULE_DIR ${ZEPHYR_${MODULE_NAME_UPPER}_MODULE_DIR})
591    set(ZEPHYR_CURRENT_CMAKE_DIR ${ZEPHYR_${MODULE_NAME_UPPER}_CMAKE_DIR})
592    add_subdirectory(${ZEPHYR_CURRENT_CMAKE_DIR} ${CMAKE_BINARY_DIR}/modules/${module_name})
593  endif()
594endforeach()
595# Done processing modules, clear module variables
596set(ZEPHYR_CURRENT_MODULE_NAME)
597set(ZEPHYR_CURRENT_MODULE_DIR)
598set(ZEPHYR_CURRENT_CMAKE_DIR)
599
600get_property(LIBC_LINK_LIBRARIES TARGET zephyr_interface PROPERTY LIBC_LINK_LIBRARIES)
601zephyr_link_libraries(${LIBC_LINK_LIBRARIES})
602
603set(syscall_list_h   ${CMAKE_CURRENT_BINARY_DIR}/include/generated/syscall_list.h)
604set(syscalls_json    ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls.json)
605set(struct_tags_json ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/struct_tags.json)
606
607# The syscalls subdirs txt file is constructed by python containing a list of folders to use for
608# dependency handling, including empty folders.
609# Windows:  The list is used to specify DIRECTORY list with CMAKE_CONFIGURE_DEPENDS attribute.
610# Other OS: The list will update whenever a file is added/removed/modified and ensure a re-build.
611set(syscalls_subdirs_txt ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_subdirs.txt)
612
613# As syscalls_subdirs_txt is updated whenever a file is modified, this file can not be used for
614# monitoring of added / removed folders. A trigger file is thus used for correct dependency
615# handling. The trigger file will update when a folder is added / removed.
616set(syscalls_subdirs_trigger ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_subdirs.trigger)
617
618if(NOT (${CMAKE_HOST_SYSTEM_NAME} STREQUAL Windows))
619  set(syscalls_links --create-links ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_links)
620endif()
621
622# When running CMake it must be ensured that all dependencies are correctly acquired.
623execute_process(
624  COMMAND
625  ${PYTHON_EXECUTABLE}
626  ${ZEPHYR_BASE}/scripts/build/subfolder_list.py
627  --directory        ${ZEPHYR_BASE}/include      # Walk this directory
628  --out-file         ${syscalls_subdirs_txt}     # Write file with discovered folder
629  --trigger-file     ${syscalls_subdirs_trigger} # Trigger file that is used for json generation
630  ${syscalls_links}                              # If defined, create symlinks for dependencies
631)
632file(STRINGS ${syscalls_subdirs_txt} PARSE_SYSCALLS_PATHS_DEPENDS ENCODING UTF-8)
633
634if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL Windows)
635  # On windows only adding/removing files or folders will be reflected in depends.
636  # Hence adding a file requires CMake to re-run to add this file to the file list.
637  set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${PARSE_SYSCALLS_PATHS_DEPENDS})
638
639  # Also On Windows each header file must be monitored as file modifications are not reflected
640  # on directory level.
641  file(GLOB_RECURSE PARSE_SYSCALLS_HEADER_DEPENDS ${ZEPHYR_BASE}/include/*.h)
642else()
643  # The syscall parsing depends on the folders in order to detect add/removed/modified files.
644  # When a folder is removed, CMake will try to find a target that creates that dependency.
645  # This command sets up the target for CMake to find.
646  # Without this code, CMake will fail with the following error:
647  #   <folder> needed by '<target>', missing and no known rule to make it
648  # when a folder is removed.
649  add_custom_command(OUTPUT ${PARSE_SYSCALLS_PATHS_DEPENDS}
650    COMMAND ${CMAKE_COMMAND} -E echo ""
651    COMMENT "Preparing syscall dependency handling"
652  )
653
654  add_custom_command(
655    OUTPUT
656    ${syscalls_subdirs_trigger}
657    COMMAND
658    ${PYTHON_EXECUTABLE}
659    ${ZEPHYR_BASE}/scripts/build/subfolder_list.py
660    --directory        ${ZEPHYR_BASE}/include      # Walk this directory
661    --out-file         ${syscalls_subdirs_txt}     # Write file with discovered folder
662    --trigger-file     ${syscalls_subdirs_trigger} # Trigger file that is used for json generation
663    ${syscalls_links}                              # If defined, create symlinks for dependencies
664    DEPENDS ${PARSE_SYSCALLS_PATHS_DEPENDS}
665  )
666
667  # Ensure subdir file always exists when specifying CMake dependency.
668  if(NOT EXISTS ${syscalls_subdirs_txt})
669    file(WRITE ${syscalls_subdirs_txt} "")
670  endif()
671
672  # On other OS'es, modifying a file is reflected on the folder timestamp and hence detected
673  # when using depend on directory level.
674  # Thus CMake only needs to re-run when sub-directories are added / removed, which is indicated
675  # using a trigger file.
676  set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${syscalls_subdirs_txt})
677endif()
678
679# syscall declarations are searched for in the SYSCALL_INCLUDE_DIRS
680if(CONFIG_APPLICATION_DEFINED_SYSCALL)
681  list(APPEND SYSCALL_INCLUDE_DIRS ${APPLICATION_SOURCE_DIR})
682endif()
683
684if(CONFIG_ZTEST)
685  list(APPEND SYSCALL_INCLUDE_DIRS ${ZEPHYR_BASE}/subsys/testsuite/ztest/include)
686
687  if(CONFIG_NO_OPTIMIZATIONS AND CONFIG_ZTEST_WARN_NO_OPTIMIZATIONS)
688    message(WARNING "Running tests with CONFIG_NO_OPTIMIZATIONS is generally "
689    "not supported and known to break in many cases due to stack overflow or "
690    "other problems. Please do not file issues about it unless the test is "
691    "specifically tuned to run in this configuration. To disable this warning "
692    "set CONFIG_ZTEST_WARN_NO_OPTIMIZATIONS=n.")
693  endif()
694
695endif()
696
697foreach(d ${SYSCALL_INCLUDE_DIRS})
698  list(APPEND parse_syscalls_include_args
699    --include ${d}
700    )
701endforeach()
702
703add_custom_command(
704  OUTPUT
705  ${syscalls_json}
706  ${struct_tags_json}
707  COMMAND
708  ${PYTHON_EXECUTABLE}
709  ${ZEPHYR_BASE}/scripts/build/parse_syscalls.py
710  --scan             ${ZEPHYR_BASE}/include         # Read files from this dir
711  --scan             ${ZEPHYR_BASE}/drivers         # For net sockets
712  --scan             ${ZEPHYR_BASE}/subsys/net      # More net sockets
713  ${parse_syscalls_include_args}                    # Read files from these dirs also
714  --json-file        ${syscalls_json}               # Write this file
715  --tag-struct-file  ${struct_tags_json}            # Write subsystem list to this file
716  --file-list        ${syscalls_file_list_output}
717  $<$<BOOL:${CONFIG_EMIT_ALL_SYSCALLS}>:--emit-all-syscalls>
718  DEPENDS ${syscalls_subdirs_trigger} ${PARSE_SYSCALLS_HEADER_DEPENDS}
719          ${syscalls_file_list_output} ${syscalls_interface}
720  )
721
722# Make sure Picolibc is built before the rest of the system; there's no explicit
723# reference to any of the files as they're all picked up by various compiler
724# settings
725if(CONFIG_PICOLIBC_USE_MODULE)
726  set(picolibc_dependency PicolibcBuild)
727endif()
728
729add_custom_target(${SYSCALL_LIST_H_TARGET} DEPENDS ${syscall_list_h} ${picolibc_dependency})
730
731set_property(TARGET ${SYSCALL_LIST_H_TARGET}
732             APPEND PROPERTY
733             ADDITIONAL_CLEAN_FILES
734             ${CMAKE_CURRENT_BINARY_DIR}/include/generated/syscalls
735)
736
737add_custom_target(${PARSE_SYSCALLS_TARGET}
738  DEPENDS
739  ${syscalls_json}
740  ${struct_tags_json}
741  )
742
743# 64-bit systems do not require special handling of 64-bit system call
744# parameters or return values, indicate this to the system call boilerplate
745# generation script.
746if(CONFIG_64BIT)
747  set(SYSCALL_LONG_REGISTERS_ARG --long-registers)
748endif()
749
750if(CONFIG_TIMEOUT_64BIT)
751  set(SYSCALL_SPLIT_TIMEOUT_ARG --split-type k_timeout_t --split-type k_ticks_t)
752endif()
753
754add_custom_command(OUTPUT include/generated/syscall_dispatch.c ${syscall_list_h}
755  # Also, some files are written to include/generated/syscalls/
756  COMMAND
757  ${PYTHON_EXECUTABLE}
758  ${ZEPHYR_BASE}/scripts/build/gen_syscalls.py
759  --json-file        ${syscalls_json}                     # Read this file
760  --base-output      include/generated/syscalls           # Write to this dir
761  --syscall-dispatch include/generated/syscall_dispatch.c # Write this file
762  --syscall-list     ${syscall_list_h}
763  $<$<BOOL:${CONFIG_USERSPACE}>:--gen-mrsh-files>
764  ${SYSCALL_LONG_REGISTERS_ARG}
765  ${SYSCALL_SPLIT_TIMEOUT_ARG}
766  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
767  DEPENDS ${PARSE_SYSCALLS_TARGET}
768  )
769
770# This is passed into all calls to the gen_kobject_list.py script.
771set(gen_kobject_list_include_args --include-subsystem-list ${struct_tags_json})
772
773set(DRV_VALIDATION ${PROJECT_BINARY_DIR}/include/generated/driver-validation.h)
774add_custom_command(
775  OUTPUT ${DRV_VALIDATION}
776  COMMAND
777  ${PYTHON_EXECUTABLE}
778  ${ZEPHYR_BASE}/scripts/build/gen_kobject_list.py
779  --validation-output ${DRV_VALIDATION}
780  ${gen_kobject_list_include_args}
781  $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
782  DEPENDS
783  ${ZEPHYR_BASE}/scripts/build/gen_kobject_list.py
784  ${PARSE_SYSCALLS_TARGET}
785  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
786  )
787add_custom_target(${DRIVER_VALIDATION_H_TARGET} DEPENDS ${DRV_VALIDATION})
788
789include(${ZEPHYR_BASE}/cmake/kobj.cmake)
790gen_kobj(KOBJ_INCLUDE_PATH)
791
792# Add a pseudo-target that is up-to-date when all generated headers
793# are up-to-date.
794
795add_custom_target(zephyr_generated_headers)
796add_dependencies(zephyr_generated_headers
797  offsets_h version_h
798  )
799
800# Generate offsets.c.obj from offsets.c
801# Generate offsets.h     from offsets.c.obj
802
803set(OFFSETS_LIB offsets)
804
805set(OFFSETS_C_PATH ${ARCH_DIR}/${ARCH}/core/offsets/offsets.c)
806set(OFFSETS_H_PATH ${PROJECT_BINARY_DIR}/include/generated/offsets.h)
807
808add_library(          ${OFFSETS_LIB} OBJECT ${OFFSETS_C_PATH})
809target_include_directories(${OFFSETS_LIB} PRIVATE
810  kernel/include
811  ${ARCH_DIR}/${ARCH}/include
812  )
813
814# Make sure that LTO will never be enabled when compiling offsets.c
815set_source_files_properties(${OFFSETS_C_PATH} PROPERTIES COMPILE_OPTIONS $<TARGET_PROPERTY:compiler,prohibit_lto>)
816
817target_link_libraries(${OFFSETS_LIB} zephyr_interface)
818add_dependencies(zephyr_interface
819  ${SYSCALL_LIST_H_TARGET}
820  ${DRIVER_VALIDATION_H_TARGET}
821  ${KOBJ_TYPES_H_TARGET}
822  )
823
824add_custom_command(
825  OUTPUT ${OFFSETS_H_PATH}
826  COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/build/gen_offset_header.py
827  -i $<TARGET_OBJECTS:${OFFSETS_LIB}>
828  -o ${OFFSETS_H_PATH}
829  DEPENDS
830  ${OFFSETS_LIB}
831  $<TARGET_OBJECTS:${OFFSETS_LIB}>
832)
833add_custom_target(${OFFSETS_H_TARGET} DEPENDS ${OFFSETS_H_PATH})
834
835zephyr_get_include_directories_for_lang(C ZEPHYR_INCLUDES)
836
837add_subdirectory(kernel)
838
839get_property(
840  syscalls_file_list
841  TARGET syscalls_interface
842  PROPERTY INTERFACE_INCLUDE_DIRECTORIES
843)
844file(CONFIGURE OUTPUT ${syscalls_file_list_output}
845     CONTENT "@syscalls_file_list@" @ONLY)
846
847# Read list content
848get_property(ZEPHYR_LIBS_PROPERTY GLOBAL PROPERTY ZEPHYR_LIBS)
849
850foreach(zephyr_lib ${ZEPHYR_LIBS_PROPERTY})
851  get_property(lib_type     TARGET ${zephyr_lib} PROPERTY TYPE)
852  # To prevent CMake failure when a driver is enabled, for example: REGULATOR=y
853  # we disable any Zephyr libraries without sources and adds the `empty_file.c`.
854  if(${lib_type} STREQUAL STATIC_LIBRARY
855     AND NOT ${zephyr_lib} STREQUAL app
856  )
857    get_property(source_list  TARGET ${zephyr_lib} PROPERTY SOURCES)
858    get_property(lib_imported TARGET ${zephyr_lib} PROPERTY IMPORTED)
859    if(NOT source_list
860       AND NOT ${lib_imported}
861    )
862      get_property(allow_empty TARGET ${zephyr_lib} PROPERTY ALLOW_EMPTY)
863      if(NOT "${allow_empty}")
864        message(WARNING
865          "No SOURCES given to Zephyr library: ${zephyr_lib}\nExcluding target from build."
866        )
867      endif()
868      target_sources(${zephyr_lib} PRIVATE ${ZEPHYR_BASE}/misc/empty_file.c)
869      set_property(TARGET ${zephyr_lib} PROPERTY EXCLUDE_FROM_ALL TRUE)
870      list(REMOVE_ITEM ZEPHYR_LIBS_PROPERTY ${zephyr_lib})
871      continue()
872    endif()
873  endif()
874
875  # TODO: Could this become an INTERFACE property of zephyr_interface?
876  add_dependencies(${zephyr_lib} zephyr_generated_headers)
877endforeach()
878
879get_property(OUTPUT_FORMAT        GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT)
880
881if (CONFIG_CODE_DATA_RELOCATION)
882  set(CODE_RELOCATION_DEP code_relocation_source_lib)
883endif() # CONFIG_CODE_DATA_RELOCATION
884
885# Give the linker script targets all of the include directories so
886# that cmake can successfully find the linker scripts' header
887# dependencies.
888zephyr_get_include_directories_for_lang(C
889  ZEPHYR_INCLUDE_DIRS
890  STRIP_PREFIX # Don't use a -I prefix
891  )
892
893if(CONFIG_DEVICE_DEPS)
894  if(CONFIG_DEVICE_DEPS_DYNAMIC)
895    set(dynamic_deps --dynamic-deps)
896  endif()
897
898  if(CONFIG_PM_DEVICE_POWER_DOMAIN_DYNAMIC)
899    set(number_of_dynamic_devices ${CONFIG_PM_DEVICE_POWER_DOMAIN_DYNAMIC_NUM})
900  else()
901    set(number_of_dynamic_devices 0)
902  endif()
903
904  # device_deps.c is generated from ${ZEPHYR_LINK_STAGE_EXECUTABLE} by
905  # gen_device_deps.py
906  add_custom_command(
907    OUTPUT device_deps.c
908    COMMAND
909    ${PYTHON_EXECUTABLE}
910    ${ZEPHYR_BASE}/scripts/build/gen_device_deps.py
911    --output-source device_deps.c
912    --output-graphviz dev_graph.dot
913    ${dynamic_deps}
914    --num-dynamic-devices ${number_of_dynamic_devices}
915    --kernel $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
916    --zephyr-base ${ZEPHYR_BASE}
917    --start-symbol "$<TARGET_PROPERTY:linker,devices_start_symbol>"
918    VERBATIM
919    DEPENDS ${ZEPHYR_LINK_STAGE_EXECUTABLE}
920    )
921  set_property(GLOBAL APPEND PROPERTY GENERATED_APP_SOURCE_FILES device_deps.c)
922
923  # gen_device_deps runs on `__device_deps_pass1` so pass this info to the linker script generator
924  list(APPEND LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE "LINKER_DEVICE_DEPS_PASS1")
925endif()
926
927if(CONFIG_CODE_DATA_RELOCATION)
928  # @Intent: Linker script to relocate .text, data and .bss sections
929  toolchain_ld_relocation()
930endif()
931
932if(CONFIG_USERSPACE)
933  zephyr_get_compile_options_for_lang_as_string(C compiler_flags_priv)
934  string(REPLACE "$<TARGET_PROPERTY:compiler,coverage>" ""
935         NO_COVERAGE_FLAGS "${compiler_flags_priv}"
936  )
937
938  set(GEN_KOBJ_LIST ${ZEPHYR_BASE}/scripts/build/gen_kobject_list.py)
939  set(PROCESS_GPERF ${ZEPHYR_BASE}/scripts/build/process_gperf.py)
940endif()
941
942get_property(CSTD GLOBAL PROPERTY CSTD)
943set_ifndef(CSTD c99)
944
945# @Intent: Obtain compiler specific flag for specifying the c standard
946zephyr_compile_options(
947  $<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,cstd>${CSTD}>
948)
949set(CMAKE_C_COMPILE_FEATURES ${compile_features_${CSTD}} PARENT_SCOPE)
950
951# @Intent: Configure linker scripts, i.e. generate linker scripts with variables substituted
952toolchain_ld_configure_files()
953
954get_property(TOPT GLOBAL PROPERTY TOPT)
955get_property(COMPILER_TOPT TARGET compiler PROPERTY linker_script)
956set_ifndef(  TOPT "${COMPILER_TOPT}")
957set_ifndef(  TOPT -Wl,-T) # Use this if the compiler driver doesn't set a value
958
959if(CONFIG_HAVE_CUSTOM_LINKER_SCRIPT)
960  set(LINKER_SCRIPT ${APPLICATION_SOURCE_DIR}/${CONFIG_CUSTOM_LINKER_SCRIPT})
961  if(NOT EXISTS ${LINKER_SCRIPT})
962    set(LINKER_SCRIPT ${CONFIG_CUSTOM_LINKER_SCRIPT})
963    assert_exists(CONFIG_CUSTOM_LINKER_SCRIPT)
964  endif()
965elseif(DEFINED BOARD_LINKER_SCRIPT)
966  set(LINKER_SCRIPT ${BOARD_LINKER_SCRIPT})
967elseif(DEFINED SOC_LINKER_SCRIPT)
968  set(LINKER_SCRIPT ${SOC_LINKER_SCRIPT})
969else()
970  find_package(Deprecated COMPONENTS SEARCHED_LINKER_SCRIPT)
971endif()
972
973if(NOT EXISTS ${LINKER_SCRIPT})
974  message(FATAL_ERROR "Could not find linker script: '${LINKER_SCRIPT}'. Corrupted configuration?")
975endif()
976
977if(CONFIG_USERSPACE)
978  set(APP_SMEM_ALIGNED_LD "${PROJECT_BINARY_DIR}/include/generated/app_smem_aligned.ld")
979  set(APP_SMEM_UNALIGNED_LD "${PROJECT_BINARY_DIR}/include/generated/app_smem_unaligned.ld")
980
981  if(CONFIG_LINKER_USE_PINNED_SECTION)
982    set(APP_SMEM_PINNED_ALIGNED_LD
983        "${PROJECT_BINARY_DIR}/include/generated/app_smem_pinned_aligned.ld")
984    set(APP_SMEM_PINNED_UNALIGNED_LD
985        "${PROJECT_BINARY_DIR}/include/generated/app_smem_pinned_unaligned.ld")
986
987    if(NOT CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT)
988      # The libc partition may hold symbols that are required during boot process,
989      # for example, stack guard (if enabled). So the libc partition must be pinned
990      # if not sections are in physical memory at boot, as the paging mechanism is
991      # only initialized post-kernel.
992      set_property(TARGET app_smem APPEND PROPERTY pinned_partitions "z_libc_partition")
993    endif()
994
995    get_property(APP_SMEM_PINNED_PARTITION_LIST TARGET app_smem PROPERTY pinned_partitions)
996    if(APP_SMEM_PINNED_PARTITION_LIST)
997      list(JOIN APP_SMEM_PINNED_PARTITION_LIST "," APP_SMEM_PINNED_PARTITION_LIST_ARG_CSL)
998      set(APP_SMEM_PINNED_PARTITION_LIST_ARG "--pinpartitions=${APP_SMEM_PINNED_PARTITION_LIST_ARG_CSL}")
999    endif()
1000  endif()
1001
1002  set(OBJ_FILE_DIR "${PROJECT_BINARY_DIR}/../")
1003
1004  if(CONFIG_NEWLIB_LIBC)
1005    set(LIBC_PART -l libc.a z_libc_partition -l libm.a z_libc_partition)
1006  endif()
1007  if(CONFIG_NEWLIB_LIBC_NANO)
1008    set(LIBC_PART -l libc_nano.a z_libc_partition -l libm_nano.a z_libc_partition)
1009  endif()
1010  if(CONFIG_PICOLIBC)
1011    set(LIBC_PART -l libc.a z_libc_partition)
1012  endif()
1013
1014  add_custom_command(
1015    OUTPUT ${APP_SMEM_UNALIGNED_LD} ${APP_SMEM_PINNED_UNALIGNED_LD}
1016    COMMAND ${PYTHON_EXECUTABLE}
1017    ${ZEPHYR_BASE}/scripts/build/gen_app_partitions.py
1018    -f ${CMAKE_BINARY_DIR}/compile_commands.json
1019    -o ${APP_SMEM_UNALIGNED_LD}
1020    $<$<BOOL:${APP_SMEM_PINNED_UNALIGNED_LD}>:--pinoutput=${APP_SMEM_PINNED_UNALIGNED_LD}>
1021    ${APP_SMEM_PINNED_PARTITION_LIST_ARG}
1022    ${LIBC_PART}
1023    $<TARGET_PROPERTY:zephyr_property_target,COMPILE_OPTIONS>
1024    $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1025    DEPENDS
1026    kernel
1027    ${CMAKE_BINARY_DIR}/compile_commands.json
1028    ${ZEPHYR_LIBS_PROPERTY}
1029    WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
1030    COMMAND_EXPAND_LISTS
1031    COMMENT "Generating app_smem_unaligned linker section"
1032    )
1033
1034  add_custom_target(
1035    ${APP_SMEM_ALIGNED_DEP}
1036    DEPENDS
1037    ${APP_SMEM_ALIGNED_LD}
1038    ${APP_SMEM_PINNED_ALIGNED_LD}
1039    )
1040
1041  add_custom_target(
1042    ${APP_SMEM_UNALIGNED_DEP}
1043    DEPENDS
1044    ${APP_SMEM_UNALIGNED_LD}
1045    ${APP_SMEM_PINNED_UNALIGNED_LD}
1046    )
1047
1048  set(APP_SMEM_UNALIGNED_LIB app_smem_unaligned_output_obj_renamed_lib)
1049  list(APPEND LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE "LINKER_APP_SMEM_UNALIGNED")
1050endif()
1051
1052if (CONFIG_USERSPACE)
1053  add_custom_command(
1054    OUTPUT ${APP_SMEM_ALIGNED_LD} ${APP_SMEM_PINNED_ALIGNED_LD}
1055    COMMAND ${PYTHON_EXECUTABLE}
1056    ${ZEPHYR_BASE}/scripts/build/gen_app_partitions.py
1057    -e $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
1058    -o ${APP_SMEM_ALIGNED_LD}
1059    $<$<BOOL:${APP_SMEM_PINNED_ALIGNED_LD}>:--pinoutput=${APP_SMEM_PINNED_ALIGNED_LD}>
1060    ${APP_SMEM_PINNED_PARTITION_LIST_ARG}
1061    ${LIBC_PART}
1062    $<TARGET_PROPERTY:zephyr_property_target,COMPILE_OPTIONS>
1063    $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1064    DEPENDS
1065    kernel
1066    ${ZEPHYR_LIBS_PROPERTY}
1067    ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1068    WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
1069    COMMAND_EXPAND_LISTS
1070    COMMENT "Generating app_smem_aligned linker section"
1071    )
1072endif()
1073
1074if(CONFIG_USERSPACE)
1075  # This CONFIG_USERSPACE block is to create place holders to reserve space
1076  # for the gperf generated structures for zephyr_prebuilt.elf.
1077  # These place holders are there so that the placement of kobjects would be
1078  # the same between linking zephyr_prebuilt.elf and zephyr.elf, as
1079  # the gperf hash table is hashed on the addresses of kobjects.
1080  # The placeholders are generated from app_smem_unaligned_prebuilt.elf.
1081
1082  set(KOBJECT_PREBUILT_HASH_LIST           kobject_prebuilt_hash.gperf)
1083  set(KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE kobject_prebuilt_hash_preprocessed.c)
1084  set(KOBJECT_PREBUILT_HASH_OUTPUT_SRC     kobject_prebuilt_hash.c)
1085
1086  add_custom_command(
1087    OUTPUT ${KOBJECT_PREBUILT_HASH_LIST}
1088    COMMAND
1089    ${PYTHON_EXECUTABLE}
1090    ${GEN_KOBJ_LIST}
1091    --kernel $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
1092    --gperf-output ${KOBJECT_PREBUILT_HASH_LIST}
1093    ${gen_kobject_list_include_args}
1094    $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1095    DEPENDS
1096    ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1097    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1098    )
1099  add_custom_target(
1100    kobj_prebuilt_hash_list
1101    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_LIST}
1102  )
1103
1104  add_custom_command(
1105    OUTPUT ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
1106    COMMAND
1107    ${GPERF}
1108    --output-file ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
1109    --multiple-iterations 10
1110    ${KOBJECT_PREBUILT_HASH_LIST}
1111    DEPENDS kobj_prebuilt_hash_list ${KOBJECT_PREBUILT_HASH_LIST}
1112    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1113    )
1114  add_custom_target(
1115    kobj_prebuilt_hash_output_src_pre
1116    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
1117  )
1118
1119  add_custom_command(
1120    OUTPUT ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1121    COMMAND
1122    ${PYTHON_EXECUTABLE}
1123    ${PROCESS_GPERF}
1124    -i ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
1125    -o ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1126    -p "struct k_object"
1127    $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1128    DEPENDS kobj_prebuilt_hash_output_src_pre ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
1129    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1130    )
1131  add_custom_target(
1132    kobj_prebuilt_hash_output_src
1133    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1134  )
1135
1136  add_library(
1137    kobj_prebuilt_hash_output_lib
1138    OBJECT ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1139    )
1140
1141  set_source_files_properties(${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1142    PROPERTIES COMPILE_FLAGS
1143    "${NO_COVERAGE_FLAGS} -fno-function-sections -fno-data-sections")
1144
1145  target_compile_definitions(kobj_prebuilt_hash_output_lib
1146    PRIVATE $<TARGET_PROPERTY:zephyr_interface,INTERFACE_COMPILE_DEFINITIONS>
1147  )
1148
1149  target_include_directories(kobj_prebuilt_hash_output_lib
1150    PUBLIC $<TARGET_PROPERTY:zephyr_interface,INTERFACE_INCLUDE_DIRECTORIES>
1151  )
1152
1153  target_include_directories(kobj_prebuilt_hash_output_lib SYSTEM
1154    PUBLIC $<TARGET_PROPERTY:zephyr_interface,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
1155  )
1156
1157  set(KOBJECT_LINKER_HEADER_DATA "${PROJECT_BINARY_DIR}/include/generated/linker-kobject-prebuilt-data.h")
1158
1159  add_custom_command(
1160    OUTPUT ${KOBJECT_LINKER_HEADER_DATA}
1161    COMMAND
1162    ${PYTHON_EXECUTABLE}
1163    ${ZEPHYR_BASE}/scripts/build/gen_kobject_placeholders.py
1164    --object $<TARGET_OBJECTS:kobj_prebuilt_hash_output_lib>
1165    --outdir ${PROJECT_BINARY_DIR}/include/generated
1166    --datapct ${CONFIG_KOBJECT_DATA_AREA_RESERVE_EXTRA_PERCENT}
1167    --rodata ${CONFIG_KOBJECT_RODATA_AREA_EXTRA_BYTES}
1168    $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1169    DEPENDS
1170    kobj_prebuilt_hash_output_lib $<TARGET_OBJECTS:kobj_prebuilt_hash_output_lib>
1171    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1172    )
1173
1174  add_custom_target(
1175    ${KOBJECT_LINKER_DEP}
1176    DEPENDS
1177    ${KOBJECT_LINKER_HEADER_DATA}
1178  )
1179endif()
1180
1181if(CONFIG_USERSPACE OR CONFIG_DEVICE_DEPS)
1182  configure_linker_script(
1183    ${ZEPHYR_CURRENT_LINKER_CMD}
1184    "${LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE}"
1185    ${CODE_RELOCATION_DEP}
1186    ${APP_SMEM_UNALIGNED_DEP}
1187    ${APP_SMEM_UNALIGNED_LD}
1188    ${APP_SMEM_PINNED_UNALIGNED_LD}
1189    zephyr_generated_headers
1190    )
1191
1192  add_custom_target(
1193    linker_zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS}_script
1194    DEPENDS
1195    ${ZEPHYR_CURRENT_LINKER_CMD}
1196    )
1197
1198  set_property(TARGET
1199    linker_zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS}_script
1200    PROPERTY INCLUDE_DIRECTORIES
1201    ${ZEPHYR_INCLUDE_DIRS}
1202    )
1203
1204  add_executable(${ZEPHYR_LINK_STAGE_EXECUTABLE} misc/empty_file.c)
1205  toolchain_ld_link_elf(
1206    TARGET_ELF            ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1207    OUTPUT_MAP            ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
1208    LIBRARIES_PRE_SCRIPT  ""
1209    LINKER_SCRIPT         ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD}
1210    LIBRARIES_POST_SCRIPT ""
1211    DEPENDENCIES          ${CODE_RELOCATION_DEP}
1212  )
1213  target_link_libraries_ifdef(CONFIG_NATIVE_LIBRARY ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1214                              $<TARGET_PROPERTY:linker,no_position_independent>)
1215  target_byproducts(TARGET ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1216                    BYPRODUCTS ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
1217  )
1218  set_property(TARGET ${ZEPHYR_LINK_STAGE_EXECUTABLE} PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD})
1219  add_dependencies(${ZEPHYR_LINK_STAGE_EXECUTABLE} linker_zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS}_script ${OFFSETS_LIB})
1220
1221  math(EXPR ZEPHYR_CURRENT_LINKER_PASS "1 + ${ZEPHYR_CURRENT_LINKER_PASS}")
1222endif()
1223
1224set(ZEPHYR_CURRENT_LINKER_CMD  linker_zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS}.cmd)
1225set(ZEPHYR_LINK_STAGE_EXECUTABLE zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS})
1226list(APPEND LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE "LINKER_ZEPHYR_PREBUILT")
1227
1228if(CONFIG_GEN_ISR_TABLES)
1229  if(CONFIG_GEN_SW_ISR_TABLE)
1230    list(APPEND GEN_ISR_TABLE_EXTRA_ARG --sw-isr-table)
1231  endif()
1232
1233  if(CONFIG_GEN_IRQ_VECTOR_TABLE)
1234    list(APPEND GEN_ISR_TABLE_EXTRA_ARG --vector-table)
1235  endif()
1236
1237  # isr_tables.c is generated from ${ZEPHYR_LINK_STAGE_EXECUTABLE} by
1238  # gen_isr_tables.py
1239  add_custom_command(
1240    OUTPUT isr_tables.c isr_tables_vt.ld isr_tables_swi.ld
1241    COMMAND ${PYTHON_EXECUTABLE}
1242    ${ZEPHYR_BASE}/scripts/build/gen_isr_tables.py
1243    --output-source isr_tables.c
1244    --linker-output-files isr_tables_vt.ld isr_tables_swi.ld
1245    --kernel $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
1246    --intlist-section .intList
1247    --intlist-section intList
1248    $<$<BOOL:${CONFIG_BIG_ENDIAN}>:--big-endian>
1249    $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--debug>
1250    ${GEN_ISR_TABLE_EXTRA_ARG}
1251    DEPENDS ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1252    COMMAND_EXPAND_LISTS
1253    )
1254  set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_SOURCE_FILES isr_tables.c)
1255endif()
1256
1257if(CONFIG_USERSPACE)
1258  set(KOBJECT_HASH_LIST               kobject_hash.gperf)
1259  set(KOBJECT_HASH_OUTPUT_SRC_PRE     kobject_hash_preprocessed.c)
1260  set(KOBJECT_HASH_OUTPUT_SRC         kobject_hash.c)
1261  set(KOBJECT_HASH_OUTPUT_OBJ_RENAMED kobject_hash_renamed.o)
1262
1263  # Essentially what we are doing here is extracting some information
1264  # out of the nearly finished elf file, generating the source code
1265  # for a hash table based on that information, and then compiling and
1266  # linking the hash table back into a now even more nearly finished
1267  # elf file. More information in gen_kobject_list.py --help.
1268
1269  # Use the script GEN_KOBJ_LIST to scan the kernel binary's
1270  # (${ZEPHYR_LINK_STAGE_EXECUTABLE}) DWARF information to produce a table of kernel
1271  # objects (KOBJECT_HASH_LIST) which we will then pass to gperf
1272  add_custom_command(
1273    OUTPUT ${KOBJECT_HASH_LIST}
1274    COMMAND
1275    ${PYTHON_EXECUTABLE}
1276    ${GEN_KOBJ_LIST}
1277    --kernel $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
1278    --gperf-output ${KOBJECT_HASH_LIST}
1279    ${gen_kobject_list_include_args}
1280    $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1281    DEPENDS
1282    ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1283    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1284    )
1285  add_custom_target(
1286    kobj_hash_list
1287    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_LIST}
1288  )
1289
1290  # Use gperf to generate C code (KOBJECT_HASH_OUTPUT_SRC_PRE) which implements a
1291  # perfect hashtable based on KOBJECT_HASH_LIST
1292  add_custom_command(
1293    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC_PRE}
1294    COMMAND
1295    ${GPERF}
1296    --output-file ${KOBJECT_HASH_OUTPUT_SRC_PRE}
1297    --multiple-iterations 10
1298    ${KOBJECT_HASH_LIST}
1299    DEPENDS kobj_hash_list ${KOBJECT_HASH_LIST}
1300    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1301    )
1302  add_custom_target(
1303    kobj_hash_output_src_pre
1304    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC_PRE}
1305  )
1306
1307  # For our purposes the code/data generated by gperf is not optimal.
1308  #
1309  # The script PROCESS_GPERF creates a new c file KOBJECT_HASH_OUTPUT_SRC based on
1310  # KOBJECT_HASH_OUTPUT_SRC_PRE to greatly reduce the amount of code/data generated
1311  # since we know we are always working with pointer values
1312  add_custom_command(
1313    OUTPUT ${KOBJECT_HASH_OUTPUT_SRC}
1314    COMMAND
1315    ${PYTHON_EXECUTABLE}
1316    ${PROCESS_GPERF}
1317    -i ${KOBJECT_HASH_OUTPUT_SRC_PRE}
1318    -o ${KOBJECT_HASH_OUTPUT_SRC}
1319    -p "struct k_object"
1320    $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1321    DEPENDS kobj_hash_output_src_pre ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC_PRE}
1322    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1323    )
1324  add_custom_target(
1325    kobj_hash_output_src
1326    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC}
1327  )
1328
1329  # We need precise control of where generated text/data ends up in the final
1330  # kernel image. Disable function/data sections and use objcopy to move
1331  # generated data into special section names
1332  add_library(
1333    kobj_hash_output_lib
1334    OBJECT ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC}
1335    )
1336
1337  set_source_files_properties(${KOBJECT_HASH_OUTPUT_SRC}
1338    PROPERTIES COMPILE_FLAGS
1339    "${NO_COVERAGE_FLAGS} -fno-function-sections -fno-data-sections")
1340
1341  target_compile_definitions(kobj_hash_output_lib
1342    PRIVATE $<TARGET_PROPERTY:zephyr_interface,INTERFACE_COMPILE_DEFINITIONS>
1343  )
1344
1345  target_include_directories(kobj_hash_output_lib
1346    PUBLIC $<TARGET_PROPERTY:zephyr_interface,INTERFACE_INCLUDE_DIRECTORIES>
1347  )
1348
1349  target_include_directories(kobj_hash_output_lib SYSTEM
1350    PUBLIC $<TARGET_PROPERTY:zephyr_interface,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
1351  )
1352
1353  add_custom_command(
1354    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
1355    COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1356            $<TARGET_PROPERTY:bintools,elfconvert_flag>
1357            $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.literal=.kobject_data.literal
1358            $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.data=.kobject_data.data
1359            $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.sdata=.kobject_data.sdata
1360            $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.text=.kobject_data.text
1361            $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.rodata=.kobject_data.rodata
1362            $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>$<TARGET_OBJECTS:kobj_hash_output_lib>
1363            $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
1364            $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
1365    DEPENDS kobj_hash_output_lib $<TARGET_OBJECTS:kobj_hash_output_lib>
1366    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1367    COMMAND_EXPAND_LISTS
1368    )
1369  add_custom_target(
1370    kobj_hash_output_obj_renamed
1371    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
1372  )
1373
1374  add_library(kobj_hash_output_obj_renamed_lib STATIC IMPORTED GLOBAL)
1375  set_property(
1376    TARGET kobj_hash_output_obj_renamed_lib
1377    PROPERTY
1378    IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
1379    )
1380  add_dependencies(
1381    kobj_hash_output_obj_renamed_lib
1382    kobj_hash_output_obj_renamed
1383    )
1384
1385  set_property(
1386    GLOBAL APPEND PROPERTY
1387    GENERATED_KERNEL_OBJECT_FILES kobj_hash_output_obj_renamed_lib
1388  )
1389endif()
1390
1391configure_linker_script(
1392  ${ZEPHYR_CURRENT_LINKER_CMD}
1393  "${LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE}"
1394  ${APP_SMEM_ALIGNED_DEP}
1395  ${KOBJECT_LINKER_DEP}
1396  ${CODE_RELOCATION_DEP}
1397  zephyr_generated_headers
1398  )
1399
1400add_custom_target(
1401  linker_zephyr_prebuilt_script_target
1402  DEPENDS
1403  ${ZEPHYR_CURRENT_LINKER_CMD}
1404  )
1405
1406set_property(TARGET
1407  linker_zephyr_prebuilt_script_target
1408  PROPERTY INCLUDE_DIRECTORIES
1409  ${ZEPHYR_INCLUDE_DIRS}
1410  )
1411
1412# Read global variables into local variables
1413get_property(GASF GLOBAL PROPERTY GENERATED_APP_SOURCE_FILES)
1414get_property(GKOF GLOBAL PROPERTY GENERATED_KERNEL_OBJECT_FILES)
1415get_property(GKSF GLOBAL PROPERTY GENERATED_KERNEL_SOURCE_FILES)
1416
1417# FIXME: Is there any way to get rid of empty_file.c?
1418add_executable(       ${ZEPHYR_LINK_STAGE_EXECUTABLE} misc/empty_file.c ${GASF})
1419toolchain_ld_link_elf(
1420  TARGET_ELF            ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1421  OUTPUT_MAP            ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
1422  LIBRARIES_PRE_SCRIPT  ""
1423  LINKER_SCRIPT         ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD}
1424  DEPENDENCIES          ${CODE_RELOCATION_DEP}
1425)
1426target_link_libraries_ifdef(CONFIG_NATIVE_LIBRARY ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1427                            $<TARGET_PROPERTY:linker,partial_linking>)
1428target_byproducts(TARGET ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1429                  BYPRODUCTS ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
1430)
1431set(BYPRODUCT_KERNEL_ELF_NAME "${PROJECT_BINARY_DIR}/${KERNEL_ELF_NAME}" CACHE FILEPATH "Kernel elf file" FORCE)
1432set_property(TARGET
1433  ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1434  PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD}
1435  )
1436add_dependencies(
1437  ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1438  linker_zephyr_prebuilt_script_target
1439  ${OFFSETS_LIB}
1440  )
1441
1442set(generated_kernel_files ${GKSF} ${GKOF})
1443if(NOT generated_kernel_files)
1444  # Use the prebuilt elf as the final elf since we don't have a
1445  # generation stage.
1446  set(logical_target_for_zephyr_elf ${ZEPHYR_LINK_STAGE_EXECUTABLE})
1447else()
1448  # The final linker pass uses the same source linker script of the
1449  # previous passes, but this time with a different output
1450  # file and preprocessed with the define LINKER_ZEPHYR_FINAL.
1451  configure_linker_script(
1452    linker.cmd
1453    "LINKER_ZEPHYR_FINAL"
1454    ${CODE_RELOCATION_DEP}
1455    ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1456    zephyr_generated_headers
1457    )
1458
1459  add_custom_target(
1460    linker_zephyr_final_script_target
1461    DEPENDS
1462    linker.cmd
1463    )
1464  set_property(TARGET
1465    linker_zephyr_final_script_target
1466    PROPERTY INCLUDE_DIRECTORIES
1467    ${ZEPHYR_INCLUDE_DIRS}
1468  )
1469
1470  add_executable(       ${ZEPHYR_FINAL_EXECUTABLE} misc/empty_file.c ${GASF} ${GKSF})
1471  toolchain_ld_link_elf(
1472    TARGET_ELF            ${ZEPHYR_FINAL_EXECUTABLE}
1473    OUTPUT_MAP            ${PROJECT_BINARY_DIR}/${ZEPHYR_FINAL_EXECUTABLE}.map
1474    LIBRARIES_PRE_SCRIPT  ${GKOF}
1475    LINKER_SCRIPT         ${PROJECT_BINARY_DIR}/linker.cmd
1476    LIBRARIES_POST_SCRIPT ""
1477    DEPENDENCIES          ${CODE_RELOCATION_DEP}
1478  )
1479  set_property(TARGET   ${ZEPHYR_FINAL_EXECUTABLE} PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker.cmd)
1480  add_dependencies(     ${ZEPHYR_FINAL_EXECUTABLE} linker_zephyr_final_script_target)
1481
1482  # Use the pass2 elf as the final elf
1483  set(logical_target_for_zephyr_elf ${ZEPHYR_FINAL_EXECUTABLE})
1484endif()
1485
1486# Export the variable to the application's scope to allow the
1487# application to know what the name of the final elf target is.
1488set(logical_target_for_zephyr_elf ${logical_target_for_zephyr_elf} PARENT_SCOPE)
1489
1490# Override the base name of the last, "logical" .elf output (and last .map) so:
1491# 1. it doesn't depend on the number of passes above and the
1492#    post_build_commands below can always find it no matter which is it;
1493# 2. it can be defined in Kconfig
1494set_target_properties(${logical_target_for_zephyr_elf} PROPERTIES OUTPUT_NAME ${KERNEL_NAME})
1495
1496set(post_build_commands "")
1497set(post_build_byproducts "")
1498
1499list(APPEND
1500  post_build_commands
1501  COMMAND
1502  ${CMAKE_COMMAND} -E copy ${logical_target_for_zephyr_elf}.map ${KERNEL_MAP_NAME}
1503)
1504list(APPEND post_build_byproducts ${KERNEL_MAP_NAME})
1505
1506if(NOT CONFIG_BUILD_NO_GAP_FILL)
1507  # Use ';' as separator to get proper space in resulting command.
1508  set(GAP_FILL "$<TARGET_PROPERTY:bintools,elfconvert_flag_gapfill>0xff")
1509endif()
1510
1511if(CONFIG_OUTPUT_PRINT_MEMORY_USAGE)
1512  target_link_libraries(${logical_target_for_zephyr_elf} $<TARGET_PROPERTY:linker,memusage>)
1513
1514  get_property(memusage_build_command TARGET bintools PROPERTY memusage_command)
1515  if(memusage_build_command)
1516    # Note: The use of generator expressions allows downstream extensions to add/change the post build.
1517    # Unfortunately, the BYPRODUCTS does not allow for generator expression, so question is if we
1518    # should remove the downstream ability from start.
1519    # Or fix the output name, by the use of `get_property`
1520    list(APPEND
1521      post_build_commands
1522      COMMAND $<TARGET_PROPERTY:bintools,memusage_command>
1523              $<TARGET_PROPERTY:bintools,memusage_flag>
1524              $<TARGET_PROPERTY:bintools,memusage_infile>${KERNEL_ELF_NAME}
1525      )
1526
1527    # For now, the byproduct can only be supported upstream on byproducts name,
1528    # cause byproduct does not support generator expressions
1529    get_property(memusage_byproducts TARGET bintools PROPERTY memusage_byproducts)
1530    list(APPEND
1531      post_build_byproducts
1532      ${memusage_byproducts}
1533      )
1534  endif()
1535endif()
1536
1537if(CONFIG_BUILD_OUTPUT_ADJUST_LMA)
1538  math(EXPR adjustment "${CONFIG_BUILD_OUTPUT_ADJUST_LMA}" OUTPUT_FORMAT DECIMAL)
1539  list(APPEND
1540    post_build_commands
1541    COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1542            $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
1543            $<TARGET_PROPERTY:bintools,elfconvert_flag_lma_adjust>${adjustment}
1544            $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1545            $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_ELF_NAME}
1546    )
1547endif()
1548
1549if(NOT CONFIG_CPP_EXCEPTIONS)
1550  set(eh_frame_section ".eh_frame")
1551else()
1552  set(eh_frame_section "")
1553endif()
1554set(remove_sections_argument_list "")
1555foreach(section .comment COMMON ${eh_frame_section})
1556  list(APPEND remove_sections_argument_list
1557    $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>${section})
1558endforeach()
1559
1560if(CONFIG_BUILD_OUTPUT_HEX OR BOARD_FLASH_RUNNER STREQUAL openocd)
1561  get_property(elfconvert_formats TARGET bintools PROPERTY elfconvert_formats)
1562  if(ihex IN_LIST elfconvert_formats)
1563    list(APPEND
1564      post_build_commands
1565      COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1566              $<TARGET_PROPERTY:bintools,elfconvert_flag>
1567              ${GAP_FILL}
1568              $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>ihex
1569              ${remove_sections_argument_list}
1570              $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1571              $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_HEX_NAME}
1572              $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
1573      )
1574    list(APPEND
1575      post_build_byproducts
1576      ${KERNEL_HEX_NAME}
1577      )
1578    set(BYPRODUCT_KERNEL_HEX_NAME "${PROJECT_BINARY_DIR}/${KERNEL_HEX_NAME}" CACHE FILEPATH "Kernel hex file" FORCE)
1579  endif()
1580endif()
1581
1582if(CONFIG_BUILD_OUTPUT_BIN)
1583  get_property(elfconvert_formats TARGET bintools PROPERTY elfconvert_formats)
1584  if(binary IN_LIST elfconvert_formats)
1585    list(APPEND
1586      post_build_commands
1587      COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1588              $<TARGET_PROPERTY:bintools,elfconvert_flag>
1589              ${GAP_FILL}
1590              $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>binary
1591              ${remove_sections_argument_list}
1592              $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1593              $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_BIN_NAME}
1594              $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
1595      )
1596    list(APPEND
1597      post_build_byproducts
1598      ${KERNEL_BIN_NAME}
1599      )
1600    set(BYPRODUCT_KERNEL_BIN_NAME "${PROJECT_BINARY_DIR}/${KERNEL_BIN_NAME}" CACHE FILEPATH "Kernel binary file" FORCE)
1601  endif()
1602endif()
1603
1604if(CONFIG_BUILD_OUTPUT_BIN AND CONFIG_BUILD_OUTPUT_UF2)
1605  if(CONFIG_BUILD_OUTPUT_UF2_USE_FLASH_BASE)
1606    set(flash_addr "${CONFIG_FLASH_BASE_ADDRESS}")
1607  else()
1608    set(flash_addr "${CONFIG_FLASH_LOAD_OFFSET}")
1609  endif()
1610
1611  if(CONFIG_BUILD_OUTPUT_UF2_USE_FLASH_OFFSET)
1612    # Note, the `+ 0` in formula below avoids errors in cases where a Kconfig
1613    #       variable is undefined and thus expands to nothing.
1614    math(EXPR flash_addr
1615        "${flash_addr} + ${CONFIG_FLASH_LOAD_OFFSET} + 0"
1616         OUTPUT_FORMAT HEXADECIMAL
1617    )
1618  endif()
1619
1620  list(APPEND
1621    post_build_commands
1622    COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/build/uf2conv.py
1623            -c
1624            -f ${CONFIG_BUILD_OUTPUT_UF2_FAMILY_ID}
1625            -b ${flash_addr}
1626            -o ${KERNEL_UF2_NAME}
1627            ${KERNEL_BIN_NAME}
1628  )
1629  list(APPEND
1630    post_build_byproducts
1631    ${KERNEL_UF2_NAME}
1632  )
1633  set(BYPRODUCT_KERNEL_UF2_NAME "${PROJECT_BINARY_DIR}/${KERNEL_UF2_NAME}" CACHE FILEPATH "Kernel uf2 file" FORCE)
1634endif()
1635
1636if(CONFIG_BUILD_OUTPUT_META)
1637  set(KERNEL_META_PATH  ${PROJECT_BINARY_DIR}/${KERNEL_META_NAME} CACHE INTERNAL "")
1638
1639  list(APPEND
1640    post_build_commands
1641    COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/zephyr_module.py
1642            ${ZEPHYR_MODULES_ARG}
1643            ${EXTRA_ZEPHYR_MODULES_ARG}
1644            --meta-out ${KERNEL_META_PATH}
1645            --zephyr-base=${ZEPHYR_BASE}
1646            $<$<BOOL:${CONFIG_BUILD_OUTPUT_META_STATE_PROPAGATE}>:--meta-state-propagate>
1647  )
1648  list(APPEND
1649    post_build_byproducts
1650    ${KERNEL_META_PATH}
1651  )
1652endif()
1653
1654# Cleanup intermediate files
1655if(CONFIG_CLEANUP_INTERMEDIATE_FILES)
1656  foreach(index RANGE ${ZEPHYR_CURRENT_LINKER_PASS})
1657    # Those files can be very large in some cases, delete them as we do not need them.
1658    list(APPEND
1659      post_build_commands
1660      COMMAND
1661      ${CMAKE_COMMAND} -E remove zephyr_pre${index}.elf
1662      )
1663  endforeach()
1664endif()
1665
1666if(CONFIG_BUILD_OUTPUT_S19)
1667  get_property(elfconvert_formats TARGET bintools PROPERTY elfconvert_formats)
1668  if(srec IN_LIST elfconvert_formats)
1669    # Should we print a warning if case the tools does not support converting to s19 ?
1670    list(APPEND
1671      post_build_commands
1672      COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1673              $<TARGET_PROPERTY:bintools,elfconvert_flag>
1674              ${GAP_FILL}
1675              $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>srec
1676              $<TARGET_PROPERTY:bintools,elfconvert_flag_srec_len>1
1677              $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1678              $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_S19_NAME}
1679              $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
1680      )
1681    list(APPEND
1682      post_build_byproducts
1683      ${KERNEL_S19_NAME}
1684      )
1685    set(BYPRODUCT_KERNEL_S19_NAME "${PROJECT_BINARY_DIR}/${KERNEL_S19_NAME}" CACHE FILEPATH "Kernel s19 file" FORCE)
1686  endif()
1687endif()
1688
1689if(CONFIG_OUTPUT_DISASSEMBLY)
1690if(CONFIG_OUTPUT_DISASSEMBLE_ALL)
1691    set(disassembly_type "$<TARGET_PROPERTY:bintools,disassembly_flag_all>")
1692  else()
1693    set(disassembly_type "$<TARGET_PROPERTY:bintools,disassembly_flag_inline_source>")
1694  endif()
1695  list(APPEND
1696    post_build_commands
1697    COMMAND $<TARGET_PROPERTY:bintools,disassembly_command>
1698            $<TARGET_PROPERTY:bintools,disassembly_flag>
1699            ${disassembly_type}
1700            $<TARGET_PROPERTY:bintools,disassembly_flag_infile>${KERNEL_ELF_NAME}
1701            $<TARGET_PROPERTY:bintools,disassembly_flag_outfile>${KERNEL_LST_NAME}
1702            $<TARGET_PROPERTY:bintools,disassembly_flag_final>
1703    )
1704  list(APPEND
1705    post_build_byproducts
1706    ${KERNEL_LST_NAME}
1707    )
1708endif()
1709
1710if(CONFIG_OUTPUT_SYMBOLS)
1711  list(APPEND
1712    post_build_commands
1713    COMMAND $<TARGET_PROPERTY:bintools,symbols_command>
1714            $<TARGET_PROPERTY:bintools,symbols_flag>
1715            $<TARGET_PROPERTY:bintools,symbols_infile>${KERNEL_ELF_NAME}
1716            $<TARGET_PROPERTY:bintools,symbols_outfile>${KERNEL_SYMBOLS_NAME}
1717            $<TARGET_PROPERTY:bintools,symbols_final>
1718    )
1719  list(APPEND
1720    post_build_byproducts
1721    ${KERNEL_SYMBOLS_NAME}
1722    )
1723endif()
1724
1725if(CONFIG_OUTPUT_STAT)
1726  list(APPEND
1727    post_build_commands
1728    COMMAND $<TARGET_PROPERTY:bintools,readelf_command>
1729            $<TARGET_PROPERTY:bintools,readelf_flag>
1730            $<TARGET_PROPERTY:bintools,readelf_flag_headers>
1731            $<TARGET_PROPERTY:bintools,readelf_flag_infile>${KERNEL_ELF_NAME}
1732            $<TARGET_PROPERTY:bintools,readelf_flag_outfile>${KERNEL_STAT_NAME}
1733            $<TARGET_PROPERTY:bintools,readelf_flag_final>
1734    )
1735  list(APPEND
1736    post_build_byproducts
1737    ${KERNEL_STAT_NAME}
1738    )
1739endif()
1740
1741if(CONFIG_BUILD_OUTPUT_STRIPPED)
1742  list(APPEND
1743    post_build_commands
1744    COMMAND $<TARGET_PROPERTY:bintools,strip_command>
1745            $<TARGET_PROPERTY:bintools,strip_flag>
1746            $<TARGET_PROPERTY:bintools,strip_flag_all>
1747            $<TARGET_PROPERTY:bintools,strip_flag_infile>${KERNEL_ELF_NAME}
1748            $<TARGET_PROPERTY:bintools,strip_flag_outfile>${KERNEL_STRIP_NAME}
1749            $<TARGET_PROPERTY:bintools,strip_flag_final>
1750    )
1751  list(APPEND
1752    post_build_byproducts
1753    ${KERNEL_STRIP_NAME}
1754    )
1755endif()
1756
1757if(CONFIG_BUILD_OUTPUT_EXE)
1758  if (NOT CONFIG_NATIVE_LIBRARY)
1759    list(APPEND
1760      post_build_commands
1761      COMMAND
1762      ${CMAKE_COMMAND} -E copy ${KERNEL_ELF_NAME} ${KERNEL_EXE_NAME}
1763      )
1764    list(APPEND
1765      post_build_byproducts
1766      ${KERNEL_EXE_NAME}
1767      )
1768  else()
1769    if(CMAKE_GENERATOR STREQUAL "Unix Makefiles")
1770      set(MAKE "${CMAKE_MAKE_PROGRAM}" CACHE FILEPATH "cmake defined make")
1771    endif()
1772    find_program(MAKE make REQUIRED)
1773    add_custom_target(native_runner_executable
1774      ALL
1775      COMMENT "Building native simulator runner, and linking final executable"
1776      COMMAND
1777      ${MAKE} -f ${ZEPHYR_BASE}/scripts/native_simulator/Makefile all --warn-undefined-variables
1778        -r NSI_CONFIG_FILE=${APPLICATION_BINARY_DIR}/zephyr/NSI/nsi_config
1779      # nsi_config is created by the board cmake file
1780      DEPENDS ${logical_target_for_zephyr_elf}
1781      BYPRODUCTS ${KERNEL_EXE_NAME}
1782    )
1783  endif()
1784  set(BYPRODUCT_KERNEL_EXE_NAME "${PROJECT_BINARY_DIR}/${KERNEL_EXE_NAME}" CACHE FILEPATH "Kernel exe file" FORCE)
1785endif()
1786
1787if(CONFIG_BUILD_OUTPUT_INFO_HEADER)
1788  list(APPEND
1789    post_build_commands
1790    COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/build/gen_image_info.py
1791    --elf-file=${KERNEL_ELF_NAME}
1792    --header-file=${PROJECT_BINARY_DIR}/include/public/zephyr_image_info.h
1793    $<$<BOOL:${adjustment}>:--adjusted-lma=${adjustment}>
1794    )
1795  list(APPEND
1796    post_build_byproducts
1797    ${PROJECT_BINARY_DIR}/include/public/zephyr_image_info.h
1798    )
1799endif()
1800
1801if(NOT CMAKE_C_COMPILER_ID STREQUAL "ARMClang")
1802  set(check_init_priorities_input
1803      $<IF:$<TARGET_EXISTS:native_runner_executable>,${BYPRODUCT_KERNEL_EXE_NAME},${BYPRODUCT_KERNEL_ELF_NAME}>
1804  )
1805  set(check_init_priorities_command
1806      ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/build/check_init_priorities.py
1807      --elf-file=${check_init_priorities_input}
1808  )
1809  set(check_init_priorities_dependencies
1810      ${logical_target_for_zephyr_elf}
1811      $<$<TARGET_EXISTS:native_runner_executable>:native_runner_executable>
1812  )
1813
1814  if(CONFIG_CHECK_INIT_PRIORITIES)
1815    add_custom_target(
1816      check_init_priorities
1817      ALL
1818      COMMAND ${check_init_priorities_command}
1819      DEPENDS ${check_init_priorities_dependencies}
1820    )
1821  endif()
1822
1823  add_custom_target(
1824    initlevels
1825    COMMAND ${check_init_priorities_command} --initlevels
1826    DEPENDS ${check_init_priorities_dependencies}
1827    USES_TERMINAL
1828  )
1829endif()
1830
1831# Generate signed (MCUboot or other) related artifacts as needed. Priority is:
1832# * Sysbuild (if set)
1833# * SIGNING_SCRIPT target property (if set)
1834# * MCUboot signing script (if MCUboot is enabled)
1835zephyr_get(signing_script VAR SIGNING_SCRIPT SYSBUILD)
1836
1837if(NOT signing_script)
1838  get_target_property(signing_script zephyr_property_target SIGNING_SCRIPT)
1839
1840  if(NOT signing_script AND CONFIG_BOOTLOADER_MCUBOOT)
1841    set(signing_script ${CMAKE_CURRENT_LIST_DIR}/cmake/mcuboot.cmake)
1842  endif()
1843endif()
1844
1845# Include signing script, if set
1846if(signing_script)
1847  message(STATUS "Including signing script: ${signing_script}")
1848
1849  include(${signing_script})
1850endif()
1851
1852# Generate USB-C VIF policies in XML format
1853if (CONFIG_BUILD_OUTPUT_VIF)
1854  include(${CMAKE_CURRENT_LIST_DIR}/cmake/vif.cmake)
1855endif()
1856
1857get_property(extra_post_build_commands
1858  GLOBAL PROPERTY
1859  extra_post_build_commands
1860  )
1861
1862list(APPEND
1863  post_build_commands
1864  ${extra_post_build_commands}
1865  )
1866
1867get_property(extra_post_build_byproducts
1868  GLOBAL PROPERTY
1869  extra_post_build_byproducts
1870  )
1871
1872list(APPEND
1873  post_build_byproducts
1874  ${extra_post_build_byproducts}
1875  )
1876
1877if(CONFIG_LOG_DICTIONARY_DB)
1878  set(log_dict_db_output --json=${PROJECT_BINARY_DIR}/log_dictionary.json)
1879elseif(CONFIG_LOG_MIPI_SYST_USE_CATALOG)
1880  set(log_dict_db_output --syst=${PROJECT_BINARY_DIR}/mipi_syst_collateral.xml)
1881endif()
1882
1883if(log_dict_db_output)
1884  list(APPEND
1885    post_build_commands
1886    COMMAND
1887    ${PYTHON_EXECUTABLE}
1888    ${ZEPHYR_BASE}/scripts/logging/dictionary/database_gen.py
1889    ${KERNEL_ELF_NAME}
1890    ${log_dict_db_output}
1891    --build-header ${PROJECT_BINARY_DIR}/include/generated/version.h
1892    )
1893  list(APPEND
1894    post_build_byproducts
1895    ${LOG_DICT_DB_NAME}
1896    )
1897
1898  unset(log_dict_db_output)
1899endif()
1900
1901# Add post_build_commands to post-process the final .elf file produced by
1902# either the ZEPHYR_LINK_STAGE_EXECUTABLE or the KERNEL_ELF executable
1903# targets above.
1904add_custom_command(
1905  TARGET ${logical_target_for_zephyr_elf}
1906  POST_BUILD
1907  COMMAND ${CMAKE_COMMAND} -E echo "Generating files from ${PROJECT_BINARY_DIR}/${KERNEL_ELF_NAME} for board: ${BOARD}"
1908  ${post_build_commands}
1909  BYPRODUCTS
1910  ${post_build_byproducts}
1911  COMMAND_EXPAND_LISTS
1912  )
1913
1914# To populate with hex files to merge, do the following:
1915#  set_property(GLOBAL APPEND PROPERTY HEX_FILES_TO_MERGE ${my_local_list})
1916# Note that the zephyr.hex file will not be included automatically.
1917get_property(HEX_FILES_TO_MERGE GLOBAL PROPERTY HEX_FILES_TO_MERGE)
1918if(HEX_FILES_TO_MERGE)
1919  # Merge in out-of-tree hex files.
1920  set(MERGED_HEX_NAME merged.hex)
1921
1922  add_custom_command(
1923    OUTPUT ${MERGED_HEX_NAME}
1924    COMMAND
1925    ${PYTHON_EXECUTABLE}
1926    ${ZEPHYR_BASE}/scripts/build/mergehex.py
1927    -o ${MERGED_HEX_NAME}
1928    ${HEX_FILES_TO_MERGE}
1929    DEPENDS ${HEX_FILES_TO_MERGE} ${logical_target_for_zephyr_elf}
1930    )
1931
1932  add_custom_target(mergehex ALL DEPENDS ${MERGED_HEX_NAME})
1933  list(APPEND RUNNERS_DEPS mergehex)
1934
1935  message(VERBOSE "Merging hex files: ${HEX_FILES_TO_MERGE}")
1936endif()
1937
1938if(SUPPORTED_EMU_PLATFORMS)
1939  list(GET SUPPORTED_EMU_PLATFORMS 0 default_emu)
1940  if(EXISTS ${ZEPHYR_BASE}/cmake/emu/${default_emu}.cmake)
1941    add_custom_target(run DEPENDS run_${default_emu})
1942  endif()
1943
1944  foreach(EMU_PLATFORM ${SUPPORTED_EMU_PLATFORMS})
1945    if(EXISTS ${ZEPHYR_BASE}/cmake/emu/${EMU_PLATFORM}.cmake)
1946      include(${ZEPHYR_BASE}/cmake/emu/${EMU_PLATFORM}.cmake)
1947    endif()
1948  endforeach()
1949
1950  if(TARGET debugserver_${default_emu})
1951    add_custom_target(debugserver DEPENDS debugserver_${default_emu})
1952  endif()
1953else()
1954  add_custom_target(run
1955    COMMAND
1956    ${CMAKE_COMMAND} -E echo
1957    "==================================================="
1958    "Emulation/Simulation not supported with this board."
1959    "==================================================="
1960    )
1961endif()
1962
1963add_subdirectory(cmake/flash)
1964add_subdirectory(cmake/usage)
1965add_subdirectory(cmake/reports)
1966
1967if(NOT CONFIG_TEST)
1968if(CONFIG_ASSERT AND (NOT CONFIG_FORCE_NO_ASSERT))
1969  message(WARNING "__ASSERT() statements are globally ENABLED")
1970endif()
1971endif()
1972
1973if(CONFIG_BOARD_DEPRECATED_RELEASE)
1974  message(WARNING "
1975      WARNING:  The board '${BOARD}' is deprecated and will be
1976      removed in version ${CONFIG_BOARD_DEPRECATED_RELEASE}"
1977  )
1978endif()
1979
1980if(CONFIG_SOC_DEPRECATED_RELEASE)
1981  message(WARNING "
1982      WARNING:  The SoC '${SOC_NAME}' is deprecated and will be
1983      removed in version ${CONFIG_SOC_DEPRECATED_RELEASE}"
1984  )
1985endif()
1986
1987# In CMake projects, 'CMAKE_BUILD_TYPE' usually determines the
1988# optimization flag, but in Zephyr it is determined through
1989# Kconfig. Here we give a warning when there is a mismatch between the
1990# two in case the user is not aware of this.
1991set(build_types None Debug Release RelWithDebInfo MinSizeRel)
1992
1993if((CMAKE_BUILD_TYPE IN_LIST build_types) AND (NOT NO_BUILD_TYPE_WARNING))
1994  string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_uppercase)
1995  # The CMAKE_C_FLAGS_<build_type> is a string, so we do a regex to see if the
1996  # optimization flag is present in that string.
1997  # To avoid false-positive matches, the flag must either be matched first
1998  # or last in string, or come after / followed by minimum a space.
1999  if(NOT (CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_uppercase} MATCHES "(^| )${OPTIMIZATION_FLAG}($| )"))
2000    message(WARNING "
2001      The CMake build type was set to '${CMAKE_BUILD_TYPE}', but the optimization flag was set to '${OPTIMIZATION_FLAG}'.
2002      This may be intentional and the warning can be turned off by setting the CMake variable 'NO_BUILD_TYPE_WARNING'"
2003      )
2004  endif()
2005endif()
2006
2007# @Intent: Set compiler specific flags for standard C/C++ includes
2008# Done at the very end, so any other system includes which may
2009# be added by Zephyr components were first in list.
2010# Note, the compile flags are moved, but the system include is still present here.
2011zephyr_compile_options($<TARGET_PROPERTY:compiler,nostdinc>)
2012target_include_directories(zephyr_interface SYSTEM INTERFACE $<TARGET_PROPERTY:compiler,nostdinc_include>)
2013
2014if(CONFIG_MINIMAL_LIBCPP)
2015  zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,nostdincxx>>)
2016endif()
2017
2018# Finally export all build flags from Zephyr
2019add_subdirectory_ifdef(
2020  CONFIG_MAKEFILE_EXPORTS
2021  cmake/makefile_exports
2022  )
2023