blob: d9f07cef23bf222df4f47027eb2c3d7e51b1ac29 [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:
57# - device handles structs must be generated (CONFIG_HAS_DTS=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_HAS_DTS)
79 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
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +010095# "zephyr_interface" is a source-less library that encapsulates all the global
Sebastian Bøe12f8f762017-10-27 15:43:34 +020096# compiler options needed by all source files. All zephyr libraries,
97# including the library named "zephyr" link with this library to
98# obtain these flags.
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +010099# https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#interface-libraries
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200100add_library(zephyr_interface INTERFACE)
101
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +0100102# "zephyr" is a catch-all CMake library for source files that can be
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200103# built purely with the include paths, defines, and other compiler
104# flags that come with zephyr_interface.
105zephyr_library_named(zephyr)
106
107zephyr_include_directories(
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200108 include
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200109 ${PROJECT_BINARY_DIR}/include/generated
110 ${USERINCLUDE}
111 ${STDINCLUDE}
112)
113
Torsten Rasmussenc2842c32021-06-10 15:38:20 +0200114include(${ZEPHYR_BASE}/cmake/linker_script/${ARCH}/linker.cmake OPTIONAL)
115
Sebastian Bøe40363392019-01-25 10:14:13 +0100116# Don't add non-existing include directories, it creates noise and
117# warnings in some tooling
118foreach(optional_include_dir
119 ${SOC_DIR}/${ARCH}/${SOC_PATH}
120 ${SOC_DIR}/${ARCH}/${SOC_PATH}/include
Anas Nashifc8080d82022-07-21 10:22:13 -0400121 ${SOC_DIR}/${ARCH}/${SOC_PATH}/include/${SOC_NAME}
Sebastian Bøe40363392019-01-25 10:14:13 +0100122 ${SOC_DIR}/${ARCH}/${SOC_FAMILY}/include
Andy Ross544a38e2020-06-25 17:42:51 -0700123 ${SOC_DIR}/${ARCH}/${SOC_FAMILY}/common/include
Sebastian Bøe40363392019-01-25 10:14:13 +0100124 )
125 if(EXISTS ${optional_include_dir})
126 zephyr_include_directories(${optional_include_dir})
127 endif()
128endforeach()
129
Torsten Rasmussen9a12f8b2022-08-22 16:45:23 +0200130# Don't inherit compiler flags from the environment
131foreach(var AFLAGS CFLAGS CXXFLAGS CPPFLAGS LDFLAGS)
132 if(DEFINED ENV{${var}})
133 message(WARNING "The environment variable '${var}' was set to $ENV{${var}}, "
134 "but Zephyr ignores flags from the environment. Use 'cmake "
135 "-DEXTRA_${var}=$ENV{${var}}' instead."
136 )
137 unset(ENV{${var}})
138 endif()
139endforeach()
140
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200141zephyr_compile_definitions(
142 KERNEL
143 __ZEPHYR__=1
Anas Nashif34aebad2018-01-03 12:26:19 -0500144)
145
Yuval Peress53ef68d2022-03-29 13:52:59 -0600146# Ensure that include/zephyr/toolchain.h includes toolchain/other.h for all off-tree toolchains
Danny Oerndrup94202f32021-07-16 11:50:40 +0200147if(TOOLCHAIN_USE_CUSTOM)
148 zephyr_compile_definitions(__TOOLCHAIN_CUSTOM__)
149endif()
150
Stephanos Ioannidis96728582022-08-11 03:06:46 +0900151# @Intent: Set compiler specific flag for disabling strict aliasing rule
152zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,no_strict_aliasing>>)
153zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,no_strict_aliasing>>)
154
Nikolay Agishev0dec4cf2022-12-22 15:46:04 +0400155# Extra warnings options for twister run
156if (CONFIG_COMPILER_WARNINGS_AS_ERRORS)
157 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warnings_as_errors>>)
158 zephyr_compile_options($<$<COMPILE_LANGUAGE:ASM>:$<TARGET_PROPERTY:asm,warnings_as_errors>>)
159 zephyr_link_libraries($<TARGET_PROPERTY:linker,warnings_as_errors>)
160endif()
161
Mark Ruvald Pedersen01592072019-01-10 12:07:51 +0100162# @Intent: Set compiler flags to enable buffer overflow checks in libc functions
Keith Packard62bc9bf2022-04-26 19:24:11 -0700163# @details:
164# Kconfig.zephyr "Detect buffer overflows in libc calls" is a kconfig choice,
165# ensuring at most *one* of CONFIG_FORTIFY_SOURCE_{COMPILE_TIME,RUN_TIME} is
166# set. Refer to Kconfig.zephyr for selection logic and description of these
167# choices. Toolchains set both of the security_fortify_{compile_time,run_time}
168# properties and the Kconfig settings are used here to select between those.
169#
170if(CONFIG_FORTIFY_SOURCE_RUN_TIME)
171 zephyr_compile_definitions($<TARGET_PROPERTY:compiler,security_fortify_run_time> )
172elseif(CONFIG_FORTIFY_SOURCE_COMPILE_TIME)
173 zephyr_compile_definitions($<TARGET_PROPERTY:compiler,security_fortify_compile_time> )
174endif()
Mark Ruvald Pedersen01592072019-01-10 12:07:51 +0100175
176# @Intent: Set compiler flags to detect general stack overflows across all functions
177if(CONFIG_STACK_CANARIES)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200178 zephyr_compile_options($<TARGET_PROPERTY:compiler,security_canaries>)
Anas Nashif34aebad2018-01-03 12:26:19 -0500179endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200180
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100181# @Intent: Obtain compiler optimizations flags and store in variables
182# @details:
183# Kconfig.zephyr "Optimization level" is a kconfig choice, ensuring
184# only *one* of CONFIG_{NO,DEBUG,SPEED,SIZE}_OPTIMIZATIONS is set.
185# Refer to Kconfig.zephyr for selection logic and description of these choices.
186# toolchain_cc_optimize_*() macros must provide the mapping from these kconfigs
187# to compiler flags. Each macro will store the flags in a CMake variable, whose
188# name is passed as argument (somewhat like by reference).
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200189#
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100190# If the user wants to tweak the optimizations, there are two ways:
191# 1) Using EXTRA_CFLAGS which is applied regardless of kconfig choice, or
192# 2) Rely on override support being implemented by your toolchain_cc_optimize_*()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200193#
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200194get_property(OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG TARGET compiler PROPERTY no_optimization)
195get_property(OPTIMIZE_FOR_DEBUG_FLAG TARGET compiler PROPERTY optimization_debug)
196get_property(OPTIMIZE_FOR_SPEED_FLAG TARGET compiler PROPERTY optimization_speed)
197get_property(OPTIMIZE_FOR_SIZE_FLAG TARGET compiler PROPERTY optimization_size)
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100198
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100199# From kconfig choice, pick the actual OPTIMIZATION_FLAG to use.
200# Kconfig choice ensures only one of these CONFIG_*_OPTIMIZATIONS is set.
Alberto Escolar Piedrasf60527a2018-01-22 15:35:54 +0100201if(CONFIG_NO_OPTIMIZATIONS)
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100202 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG})
203elseif(CONFIG_DEBUG_OPTIMIZATIONS)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200204 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_DEBUG_FLAG})
Aurelien Jarnoe8413d12018-06-16 23:40:04 +0200205elseif(CONFIG_SPEED_OPTIMIZATIONS)
206 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SPEED_FLAG})
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100207elseif(CONFIG_SIZE_OPTIMIZATIONS)
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100208 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SIZE_FLAG}) # Default in kconfig
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100209else()
Anas Nashif885aaf22019-01-18 19:15:19 -0500210 assert(0 "Unreachable code. Expected optimization level to have been chosen. See Kconfig.zephyr")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200211endif()
212
Ulf Magnussonde42aea2020-02-07 00:48:22 +0100213if(NOT CONFIG_ARCH_IS_SET)
214 message(WARNING "\
215None of the CONFIG_<arch> (e.g. CONFIG_X86) symbols are set. \
216Select one of them from the SOC_SERIES_* symbol or, lacking that, from the \
217SOC_* symbol.")
218endif()
219
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100220# Apply the final optimization flag(s)
221zephyr_compile_options(${OPTIMIZATION_FLAG})
222
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100223# @Intent: Obtain compiler specific flags related to C++ that are not influenced by kconfig
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200224zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,required>>)
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200225
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100226# @Intent: Obtain compiler specific flags for compiling under different ISO standards of C++
Stephanos Ioannidis4a64bfe2022-12-09 06:16:44 +0900227if(CONFIG_CPP)
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200228 # From kconfig choice, pick a single dialect.
229 # Kconfig choice ensures only one of these CONFIG_STD_CPP* is set.
230 if(CONFIG_STD_CPP98)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200231 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp98>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200232 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp98})
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200233 elseif(CONFIG_STD_CPP11)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200234 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp11>) # Default in kconfig
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200235 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp11})
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200236 elseif(CONFIG_STD_CPP14)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200237 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp14>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200238 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp14})
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200239 elseif(CONFIG_STD_CPP17)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200240 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp17>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200241 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp17})
Alexander Wachterad130f22021-07-14 10:50:21 +0200242 elseif(CONFIG_STD_CPP2A)
243 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp2a>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200244 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp20})
Dan Kalowskyc0811e92021-07-09 10:46:46 -0700245 elseif(CONFIG_STD_CPP20)
246 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp20>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200247 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp20})
Dan Kalowsky9b333912021-07-09 10:54:11 -0700248 elseif(CONFIG_STD_CPP2B)
249 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp2b>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200250 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp20})
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200251 else()
252 assert(0 "Unreachable code. Expected C++ standard to have been chosen. See Kconfig.zephyr.")
253 endif()
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200254 set(CMAKE_CXX_COMPILE_FEATURES ${CMAKE_CXX_COMPILE_FEATURES} PARENT_SCOPE)
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200255
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200256 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:${STD_CPP_DIALECT_FLAGS}>)
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200257endif()
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100258
Stephanos Ioannidis404e7a92022-12-09 19:43:43 +0900259if(NOT CONFIG_CPP_EXCEPTIONS)
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100260 # @Intent: Obtain compiler specific flags related to C++ Exceptions
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200261 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,no_exceptions>>)
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100262endif()
263
Stephanos Ioannidisa3b28ff2022-12-09 20:06:15 +0900264if(NOT CONFIG_CPP_RTTI)
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100265 # @Intent: Obtain compiler specific flags related to C++ Run Time Type Information
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200266 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,no_rtti>>)
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100267endif()
268
Andy Rossfe04adf2019-02-27 11:53:18 -0800269if(CONFIG_MISRA_SANE)
Danny Oerndrup8e5a95e2019-05-16 12:53:58 +0200270 # @Intent: Obtain toolchain compiler flags relating to MISRA.
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200271 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_misra_sane>>)
272 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_error_misra_sane>>)
Andy Rossfe04adf2019-02-27 11:53:18 -0800273endif()
274
Flavio Ceolinb587e8d2020-08-26 09:48:33 -0700275# This is intend to be temporary. Once we have fixed the violations that
276# prevents build Zephyr, these flags shall be part of the default flags.
277if(CONFIG_CODING_GUIDELINE_CHECK)
278 # @Intent: Obtain toolchain compiler flags relating to coding guideline
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200279 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_coding_guideline>>)
Flavio Ceolinb587e8d2020-08-26 09:48:33 -0700280endif()
281
Danny Oerndrup4ddbc002019-06-11 13:55:53 +0200282# @Intent: Set compiler specific macro inclusion of AUTOCONF_H
Torsten Rasmussenf109e682020-08-13 15:49:46 +0200283zephyr_compile_options("SHELL: $<TARGET_PROPERTY:compiler,imacros> ${AUTOCONF_H}")
Danny Oerndrup4ddbc002019-06-11 13:55:53 +0200284
Keith Packard6c5d8062023-02-08 14:56:50 -0800285if(CONFIG_COMPILER_FREESTANDING)
Keith Packardd0c75f32020-10-26 19:07:50 -0700286 # @Intent: Set compiler specific flag for bare metal freestanding option
Jaroslaw Stelter69913ad2022-07-04 17:22:18 +0200287 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,freestanding>>)
288 zephyr_compile_options($<$<COMPILE_LANGUAGE:ASM>:$<TARGET_PROPERTY:compiler,freestanding>>)
Keith Packardd0c75f32020-10-26 19:07:50 -0700289endif()
Danny Oerndrupfaa72b72019-06-11 15:56:57 +0200290
Keith Packardb3073f02022-10-13 15:04:00 -0700291if (CONFIG_PICOLIBC AND NOT CONFIG_PICOLIBC_IO_FLOAT)
292 # @Intent: Set compiler specific flag to disable printf-related optimizations
293 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,no_printf_return_value>>)
294endif()
295
Danny Oerndrupe34ed7c2019-06-12 14:56:46 +0200296# @Intent: Set compiler specific flag for tentative definitions, no-common
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200297zephyr_compile_options($<TARGET_PROPERTY:compiler,no_common>)
Danny Oerndrupe34ed7c2019-06-12 14:56:46 +0200298
Danny Oerndrupe0569ac2019-07-23 09:00:55 +0200299# @Intent: Set compiler specific flag for production of debug information
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200300zephyr_compile_options($<TARGET_PROPERTY:compiler,debug>)
Danny Oerndrupe0569ac2019-07-23 09:00:55 +0200301
Fabio Baltieri3f8f7132023-03-29 10:11:46 +0100302if(CONFIG_COMPILER_SAVE_TEMPS)
303 # @Intent: Set compiler specific flag for saving temporary object files
304 zephyr_compile_options($<TARGET_PROPERTY:compiler,save_temps>)
305endif()
306
Gerard Marull-Paretas99ebe392023-05-05 09:58:12 +0200307if(NOT CONFIG_COMPILER_TRACK_MACRO_EXPANSION)
308 # @Intent: Set compiler specific flags to not track macro expansion
309 zephyr_compile_options($<TARGET_PROPERTY:compiler,no_track_macro_expansion>)
310endif()
311
Arvin Farahmande430b7b2021-04-15 11:20:10 -0400312if(CONFIG_COMPILER_COLOR_DIAGNOSTICS)
Arvin Farahmandb8f59682021-04-15 11:20:10 -0400313# @Intent: Set compiler specific flag for diagnostic messages
314zephyr_compile_options($<TARGET_PROPERTY:compiler,diagnostic>)
315endif()
316
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200317zephyr_compile_options(
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530318 ${TOOLCHAIN_C_FLAGS}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200319)
320
Mark Ruvald Pedersencb0fd452019-01-30 21:48:25 +0100321# @Intent: Obtain compiler specific flags related to assembly
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200322# ToDo: Remember to get feedback from Oticon on this, as they might use the `ASM_BASE_FLAG` since this is done this way.
323zephyr_compile_options($<$<COMPILE_LANGUAGE:ASM>:$<TARGET_PROPERTY:asm,required>>)
Mark Ruvald Pedersencb0fd452019-01-30 21:48:25 +0100324
Nazar Kazakovf483b1b2022-03-16 21:07:43 +0000325# @Intent: Enforce standard integer type correspondence to match Zephyr usage.
Nicolas Pitreb86aa652019-07-02 16:22:04 -0400326# (must be after compiler specific flags)
Nicolas Pitre0a386db2022-06-20 13:18:14 -0400327if(CONFIG_ENFORCE_ZEPHYR_STDINT)
Yuval Peress53ef68d2022-03-29 13:52:59 -0600328 zephyr_compile_options("SHELL: $<TARGET_PROPERTY:compiler,imacros> ${ZEPHYR_BASE}/include/zephyr/toolchain/zephyr_stdint.h")
Stephanos Ioannidisa1a66192021-09-12 19:33:15 +0900329endif()
Nicolas Pitreb86aa652019-07-02 16:22:04 -0400330
Mark Ruvald Pedersencb0fd452019-01-30 21:48:25 +0100331# Common toolchain-agnostic assembly flags
332zephyr_compile_options(
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200333 $<$<COMPILE_LANGUAGE:ASM>:-D_ASMLANGUAGE>
334)
335
Mark Ruvald Pedersen1f013252019-04-25 15:46:11 +0200336# @Intent: Set fundamental linker specific flags
337toolchain_ld_base()
Aurelien Jarnoc6727d42018-11-26 13:48:34 +0100338
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +0200339toolchain_ld_force_undefined_symbols(
340 _OffsetAbsSyms
341 _ConfigAbsSyms
342)
343
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200344if(NOT CONFIG_NATIVE_APPLICATION)
Mark Ruvald Pedersen65f02c02019-04-25 16:31:30 +0200345 # @Intent: Set linker specific flags for bare metal target
346 toolchain_ld_baremetal()
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200347endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200348
Stephanos Ioannidisa9b35f02023-01-17 01:53:59 +0900349if(CONFIG_CPP AND NOT CONFIG_MINIMAL_LIBCPP)
Mark Ruvald Pedersen3db09aa2019-04-26 08:43:04 +0200350 # @Intent: Set linker specific flags for C++
351 toolchain_ld_cpp()
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200352endif()
353
Danny Oerndrup8eaa9062019-05-16 12:49:31 +0200354# @Intent: Add the basic toolchain warning flags
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200355zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_base>>)
356zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_base>>)
Danny Oerndrupbdb229f2019-05-06 15:19:27 +0200357
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200358# ==========================================================================
359#
360# cmake -DW=... settings
361#
362# W=1 - warnings that may be relevant and does not occur too often
363# W=2 - warnings that occur quite often but may still be relevant
364# W=3 - the more obscure warnings, can most likely be ignored
365# ==========================================================================
Danny Oerndrup8eaa9062019-05-16 12:49:31 +0200366# @Intent: Add cmake -DW toolchain supported warnings, if any
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200367if(W MATCHES "1")
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200368 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_dw_1>>)
369 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_dw_1>>)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200370endif()
371
372if(W MATCHES "2")
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200373 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_dw_2>>)
374 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_dw_2>>)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200375endif()
376
377if(W MATCHES "3")
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200378 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_dw_3>>)
379 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_dw_3>>)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200380endif()
381
Danny Oerndrup8eaa9062019-05-16 12:49:31 +0200382# @Intent: Add extended, more specific, toolchain warning flags
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200383zephyr_compile_options($<TARGET_PROPERTY:compiler,warning_extended>)
Benoit Leforestier04dad592019-01-25 13:57:03 +0100384
Danny Oerndrup025ffa22019-05-16 12:58:40 +0200385# @Intent: Trigger an error when a declaration does not specify a type
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200386zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_implicit_int>>)
387zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_error_implicit_int>>)
Danny Oerndrup025ffa22019-05-16 12:58:40 +0200388
Flavio Ceolin82599312022-08-22 08:47:03 -0700389# @Intent: Do not make position independent code / executable
390zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,no_position_independent>>)
Alberto Escolar Piedras01348d32023-05-04 13:43:41 +0200391zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler,no_position_independent>>)
Daniel Leung81c3b312023-02-24 17:39:07 -0800392zephyr_link_libraries($<TARGET_PROPERTY:linker,no_position_independent>)
Flavio Ceolin82599312022-08-22 08:47:03 -0700393
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200394# Allow the user to inject options when calling cmake, e.g.
395# 'cmake -DEXTRA_CFLAGS="-Werror -Wno-deprecated-declarations" ..'
Sebastian Bøe9f590452017-11-10 12:22:23 +0100396include(cmake/extra_flags.cmake)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200397
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200398zephyr_cc_option(-fno-asynchronous-unwind-tables)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200399
Flavio Ceolinac5d45a2023-01-22 12:47:36 -0800400if(CONFIG_USERSPACE)
401 zephyr_compile_options($<TARGET_PROPERTY:compiler,no_global_merge>)
402endif()
403
Daniel Leung02b20352020-09-28 11:27:11 -0700404if(CONFIG_THREAD_LOCAL_STORAGE)
405# Only support local exec TLS model at this point.
406zephyr_cc_option(-ftls-model=local-exec)
407endif()
408
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200409if(CONFIG_OVERRIDE_FRAME_POINTER_DEFAULT)
410 if(CONFIG_OMIT_FRAME_POINTER)
411 zephyr_cc_option(-fomit-frame-pointer)
412 else()
413 zephyr_cc_option(-fno-omit-frame-pointer)
414 endif()
415endif()
416
Sebastian Bøe244451b2019-02-27 08:28:25 +0100417separate_arguments(COMPILER_OPT_AS_LIST UNIX_COMMAND ${CONFIG_COMPILER_OPT})
418zephyr_compile_options(${COMPILER_OPT_AS_LIST})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200419
420# TODO: Include arch compiler options at this point.
421
Kumar Gala2d127662023-01-20 23:21:23 +0000422if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang" AND
Kumar Gala9f8913c2023-03-16 14:58:27 +0000423 NOT CMAKE_C_COMPILER_ID STREQUAL "IntelLLVM" AND
424 NOT CMAKE_C_COMPILER_ID STREQUAL "ARMClang")
Danny Oerndrupcbbbdea2019-05-06 15:21:58 +0200425 # GCC assumed
426 zephyr_cc_option(-fno-reorder-functions)
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530427
Stephanos Ioannidis5af932f2022-08-15 22:44:06 +0900428 # GCC 11 and above may generate a warning when dereferencing a constant
429 # address pointer whose address is below the value specified by the
430 # `min-pagesize` parameter (defaults to 0x1000). The `min-pagesize` parameter
431 # is set to 0 such that GCC never generates any warnings for the constant
432 # address pointers. For more details, refer to the GCC PR99578.
433 zephyr_cc_option(--param=min-pagesize=0)
434
Anas Nashif7ee8bb92018-02-11 14:36:21 -0600435 if(NOT ${ZEPHYR_TOOLCHAIN_VARIANT} STREQUAL "xcc")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200436 zephyr_cc_option(-fno-defer-pop)
437 endif()
Nicolas Pitref0057352022-04-06 13:36:40 -0400438else()
439 # Clang produces false positive vla warnings
440 zephyr_cc_option(-Wno-vla)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200441endif()
442
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200443zephyr_cc_option_ifdef(CONFIG_STACK_USAGE -fstack-usage)
444
Marc Herbert28a56572019-04-11 16:34:04 -0700445# If the compiler supports it, strip the ${ZEPHYR_BASE} prefix from the
446# __FILE__ macro used in __ASSERT*, in the
447# .noinit."/home/joe/zephyr/fu/bar.c" section names and in any
448# application code. This saves some memory, stops leaking user locations
449# in binaries, makes failure logs more deterministic and most
450# importantly makes builds more deterministic
Martin Jägera0ffaa72023-03-29 16:06:30 +0200451if(CONFIG_BUILD_OUTPUT_STRIP_PATHS)
452 # If several match then the last one wins. This matters for instances
453 # like tests/ and samples/: they're inside all of them! Then let's
454 # strip as little as possible.
455 zephyr_cc_option(-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=CMAKE_SOURCE_DIR)
456 zephyr_cc_option(-fmacro-prefix-map=${ZEPHYR_BASE}=ZEPHYR_BASE)
457 if(WEST_TOPDIR)
458 zephyr_cc_option(-fmacro-prefix-map=${WEST_TOPDIR}=WEST_TOPDIR)
459 endif()
Marc Herberteddbf3c2019-06-11 16:57:37 -0700460endif()
Marc Herbert28a56572019-04-11 16:34:04 -0700461
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200462# TODO: Archiver arguments
463# ar_option(D)
464
Håkon Øye Amundsend6551b52018-11-29 09:08:08 +0000465# Declare MPU userspace dependencies before the linker scripts to make
466# sure the order of dependencies are met
Andrew Boie41f60112019-01-31 15:53:24 -0800467if(CONFIG_USERSPACE)
Daniel Leung2117a2a2021-07-12 13:33:32 -0700468 add_custom_target(app_smem)
Daniel Leung212ec9a2019-03-10 14:20:21 -0700469 set(APP_SMEM_ALIGNED_DEP app_smem_aligned_linker)
470 set(APP_SMEM_UNALIGNED_DEP app_smem_unaligned_linker)
Håkon Øye Amundsend6551b52018-11-29 09:08:08 +0000471endif()
472
Daniel Leung11171692021-03-18 14:00:07 -0700473if(CONFIG_USERSPACE)
474 set(KOBJECT_LINKER_DEP kobject_linker)
475endif()
476
Håkon Øye Amundsena4494392018-11-29 09:14:27 +0000477get_property(TOPT GLOBAL PROPERTY TOPT)
Oleg Zhurakivskyy22119352019-03-08 11:29:33 +0200478set_ifndef( TOPT -Wl,-T) # clang doesn't pick -T for some reason and complains,
479 # while -Wl,-T works for both, gcc and clang
Håkon Øye Amundsena4494392018-11-29 09:14:27 +0000480
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200481if(CONFIG_HAVE_CUSTOM_LINKER_SCRIPT)
482 set(LINKER_SCRIPT ${APPLICATION_SOURCE_DIR}/${CONFIG_CUSTOM_LINKER_SCRIPT})
Sebastian Bøec1aa9d12018-04-12 14:48:05 +0200483 if(NOT EXISTS ${LINKER_SCRIPT})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200484 set(LINKER_SCRIPT ${CONFIG_CUSTOM_LINKER_SCRIPT})
Sebastian Bøec1aa9d12018-04-12 14:48:05 +0200485 assert_exists(CONFIG_CUSTOM_LINKER_SCRIPT)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200486 endif()
487else()
488 # Try a board specific linker file
489 set(LINKER_SCRIPT ${BOARD_DIR}/linker.ld)
490 if(NOT EXISTS ${LINKER_SCRIPT})
491 # If not available, try an SoC specific linker file
Anas Nashif96455d52018-09-04 14:34:06 -0500492 set(LINKER_SCRIPT ${SOC_DIR}/${ARCH}/${SOC_PATH}/linker.ld)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200493 endif()
494endif()
495
496if(NOT EXISTS ${LINKER_SCRIPT})
497 message(FATAL_ERROR "Could not find linker script: '${LINKER_SCRIPT}'. Corrupted configuration?")
498endif()
499
Torsten Rasmussen91709772022-02-04 10:27:13 +0100500if(DEFINED BUILD_VERSION)
501 set(build_version_argument "-DBUILD_VERSION=${BUILD_VERSION}")
Frank Terbecka85a76d2022-03-08 20:09:51 +0100502elseif(NOT ZEPHYR_GIT_INDEX)
503 set(ZEPHYR_GIT_INDEX ZEPHYR_GIT_INDEX-NOTFOUND CACHE PATH
504 "Path to Zephyr git repository index file")
505 if(EXISTS ${ZEPHYR_BASE}/.git/index)
506 set(ZEPHYR_GIT_DIR ${ZEPHYR_BASE}/.git/index CACHE PATH
507 "Path to Zephyr git repository index file" FORCE)
508 elseif(EXISTS ${ZEPHYR_BASE}/.git)
509 # Likely a git-submodule. Let's ask git where the real database is located.
510 find_package(Git QUIET)
511 if(GIT_FOUND)
512 execute_process(
513 COMMAND ${GIT_EXECUTABLE} rev-parse --absolute-git-dir
514 WORKING_DIRECTORY ${ZEPHYR_BASE}
515 OUTPUT_VARIABLE zephyr_git_dir
516 OUTPUT_STRIP_TRAILING_WHITESPACE
517 ERROR_STRIP_TRAILING_WHITESPACE
518 ERROR_VARIABLE stderr
519 RESULT_VARIABLE return_code)
520 if(return_code)
521 message(WARNING "BUILD_VERSION: git rev-parse failed: ${stderr}")
522 else()
523 if(NOT "${stderr}" STREQUAL "")
524 message(WARNING "BUILD_VERSION: git rev-parse warned: ${stderr}")
525 endif()
526 set(ZEPHYR_GIT_INDEX ${zephyr_git_dir}/index CACHE PATH
527 "Path to Zephyr git repository index file" FORCE)
528 endif()
529 else()
530 message(WARNING "Could not find git installation, "
531 "please specify '-DBUILD_VERSION=<version>'")
532 endif()
533 else()
534 message(WARNING "ZEPHYR_BASE=${ZEPHYR_BASE} doesn't appear to be a git "
535 "repository, please specify '-DBUILD_VERSION=<version>'")
536 endif()
Torsten Rasmussen91709772022-02-04 10:27:13 +0100537endif()
Frank Terbecka85a76d2022-03-08 20:09:51 +0100538
539if(ZEPHYR_GIT_INDEX)
540 set(git_dependency ${ZEPHYR_GIT_INDEX})
541endif()
542
Torsten Rasmussen91709772022-02-04 10:27:13 +0100543add_custom_command(
544 OUTPUT ${PROJECT_BINARY_DIR}/include/generated/version.h
545 COMMAND ${CMAKE_COMMAND} -DZEPHYR_BASE=${ZEPHYR_BASE}
546 -DOUT_FILE=${PROJECT_BINARY_DIR}/include/generated/version.h
Torsten Rasmussen2c757f92023-03-23 08:43:01 +0100547 -DVERSION_TYPE=KERNEL
548 -DVERSION_FILE=${ZEPHYR_BASE}/VERSION
Torsten Rasmussen91709772022-02-04 10:27:13 +0100549 ${build_version_argument}
550 -P ${ZEPHYR_BASE}/cmake/gen_version_h.cmake
551 DEPENDS ${ZEPHYR_BASE}/VERSION ${git_dependency}
552)
553add_custom_target(version_h DEPENDS ${PROJECT_BINARY_DIR}/include/generated/version.h)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200554
Torsten Rasmussen99064c22023-03-23 08:45:26 +0100555if(EXISTS ${APPLICATION_SOURCE_DIR}/VERSION)
556 add_custom_command(
557 OUTPUT ${PROJECT_BINARY_DIR}/include/generated/app_version.h
558 COMMAND ${CMAKE_COMMAND} -DZEPHYR_BASE=${ZEPHYR_BASE}
559 -DOUT_FILE=${PROJECT_BINARY_DIR}/include/generated/app_version.h
560 -DVERSION_TYPE=APP
561 -DVERSION_FILE=${APPLICATION_SOURCE_DIR}/VERSION
562 ${build_version_argument}
563 -P ${ZEPHYR_BASE}/cmake/gen_version_h.cmake
564 DEPENDS ${APPLICATION_SOURCE_DIR}/VERSION ${git_dependency}
565 )
566 add_custom_target(app_version_h DEPENDS ${PROJECT_BINARY_DIR}/include/generated/app_version.h)
567 add_dependencies(zephyr_interface app_version_h)
568endif()
569
Sebastian Bøe6f946e22018-01-09 10:52:57 +0100570# Unfortunately, the order in which CMakeLists.txt code is processed
571# matters so we need to be careful about how we order the processing
572# of subdirectories. One example is "Compiler flags added late in the
573# build are not exported to external build systems #5605"; when we
574# integrate with an external build system we read out all compiler
575# flags when the external project is created. So an external project
576# defined in subsys or ext will not get global flags added by drivers/
577# or tests/ as the subdirectories are ordered now.
578#
579# Another example of when the order matters is the reading and writing
580# of global properties such as ZEPHYR_LIBS or
581# GENERATED_KERNEL_OBJECT_FILES.
582#
583# Arch is placed early because it defines important compiler flags
584# that must be exported to external build systems defined in
585# e.g. subsys/.
586add_subdirectory(arch)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200587add_subdirectory(lib)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200588# We use include instead of add_subdirectory to avoid creating a new directory scope.
589# This is because source file properties are directory scoped, including the GENERATED
590# property which is set implicitly for custom command outputs
591include(misc/generated/CMakeLists.txt)
Anas Nashif3d1252f2018-09-03 15:20:14 -0500592
Erwan Gouriouba31cb52018-09-13 16:25:53 +0200593if(EXISTS ${SOC_DIR}/${ARCH}/CMakeLists.txt)
Anas Nashif96455d52018-09-04 14:34:06 -0500594 add_subdirectory(${SOC_DIR}/${ARCH} soc/${ARCH})
Anas Nashif3d1252f2018-09-03 15:20:14 -0500595else()
Anas Nashif96455d52018-09-04 14:34:06 -0500596 add_subdirectory(${SOC_DIR}/${ARCH}/${SOC_PATH} soc/${ARCH}/${SOC_PATH})
Anas Nashif3d1252f2018-09-03 15:20:14 -0500597endif()
598
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200599add_subdirectory(boards)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200600add_subdirectory(subsys)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200601add_subdirectory(drivers)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200602
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100603# Include zephyr modules generated CMake file.
Torsten Rasmussen2fc062b2020-08-24 11:01:04 +0200604foreach(module_name ${ZEPHYR_MODULE_NAMES})
605 # Note the second, binary_dir parameter requires the added
606 # subdirectory to have its own, local cmake target(s). If not then
607 # this binary_dir is created but stays empty. Object files land in
608 # the main binary dir instead.
609 # https://cmake.org/pipermail/cmake/2019-June/069547.html
Torsten Rasmussen3d880832021-01-19 12:01:38 +0100610 zephyr_string(SANITIZE TOUPPER MODULE_NAME_UPPER ${module_name})
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200611 if(NOT ${ZEPHYR_${MODULE_NAME_UPPER}_CMAKE_DIR} STREQUAL "")
612 set(ZEPHYR_CURRENT_MODULE_DIR ${ZEPHYR_${MODULE_NAME_UPPER}_MODULE_DIR})
613 set(ZEPHYR_CURRENT_CMAKE_DIR ${ZEPHYR_${MODULE_NAME_UPPER}_CMAKE_DIR})
614 add_subdirectory(${ZEPHYR_CURRENT_CMAKE_DIR} ${CMAKE_BINARY_DIR}/modules/${module_name})
615 endif()
Torsten Rasmussen2fc062b2020-08-24 11:01:04 +0200616endforeach()
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200617# Done processing modules, clear ZEPHYR_CURRENT_MODULE_DIR and ZEPHYR_CURRENT_CMAKE_DIR.
Torsten Rasmussen2fc062b2020-08-24 11:01:04 +0200618set(ZEPHYR_CURRENT_MODULE_DIR)
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200619set(ZEPHYR_CURRENT_CMAKE_DIR)
Torsten Rasmussen7e9d1bd2019-02-05 10:36:22 +0100620
Keith Packard87a30602022-11-02 14:49:23 -0700621get_property(LIBC_LINK_LIBRARIES TARGET zephyr_interface PROPERTY LIBC_LINK_LIBRARIES)
622zephyr_link_libraries(${LIBC_LINK_LIBRARIES})
623
Andrew Boie59601192020-05-29 13:24:51 -0700624set(syscall_list_h ${CMAKE_CURRENT_BINARY_DIR}/include/generated/syscall_list.h)
625set(syscalls_json ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls.json)
626set(struct_tags_json ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/struct_tags.json)
Sebastian Bøe13a68402017-11-20 13:03:55 +0100627
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200628# The syscalls subdirs txt file is constructed by python containing a list of folders to use for
629# dependency handling, including empty folders.
630# Windows: The list is used to specify DIRECTORY list with CMAKE_CONFIGURE_DEPENDS attribute.
631# Other OS: The list will update whenever a file is added/removed/modified and ensure a re-build.
632set(syscalls_subdirs_txt ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_subdirs.txt)
633
634# As syscalls_subdirs_txt is updated whenever a file is modified, this file can not be used for
635# monitoring of added / removed folders. A trigger file is thus used for correct dependency
636# handling. The trigger file will update when a folder is added / removed.
637set(syscalls_subdirs_trigger ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_subdirs.trigger)
638
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200639if(NOT (${CMAKE_HOST_SYSTEM_NAME} STREQUAL Windows))
640 set(syscalls_links --create-links ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_links)
641endif()
642
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200643# When running CMake it must be ensured that all dependencies are correctly acquired.
644execute_process(
645 COMMAND
646 ${PYTHON_EXECUTABLE}
Anas Nashif9ee1e322022-07-11 10:57:02 -0400647 ${ZEPHYR_BASE}/scripts/build/subfolder_list.py
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200648 --directory ${ZEPHYR_BASE}/include # Walk this directory
649 --out-file ${syscalls_subdirs_txt} # Write file with discovered folder
Jamie McCraeec704442023-01-04 16:08:36 +0000650 --trigger-file ${syscalls_subdirs_trigger} # Trigger file that is used for json generation
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200651 ${syscalls_links} # If defined, create symlinks for dependencies
652)
Carles Cufi3ad1f272019-07-18 10:38:25 +0200653file(STRINGS ${syscalls_subdirs_txt} PARSE_SYSCALLS_PATHS_DEPENDS ENCODING UTF-8)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200654
655if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL Windows)
656 # On windows only adding/removing files or folders will be reflected in depends.
657 # Hence adding a file requires CMake to re-run to add this file to the file list.
658 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${PARSE_SYSCALLS_PATHS_DEPENDS})
659
660 # Also On Windows each header file must be monitored as file modifications are not reflected
661 # on directory level.
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200662 file(GLOB_RECURSE PARSE_SYSCALLS_HEADER_DEPENDS ${ZEPHYR_BASE}/include/*.h)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200663else()
664 # The syscall parsing depends on the folders in order to detect add/removed/modified files.
665 # When a folder is removed, CMake will try to find a target that creates that dependency.
666 # This command sets up the target for CMake to find.
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200667 # Without this code, CMake will fail with the following error:
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200668 # <folder> needed by '<target>', missing and no known rule to make it
669 # when a folder is removed.
670 add_custom_command(OUTPUT ${PARSE_SYSCALLS_PATHS_DEPENDS}
671 COMMAND ${CMAKE_COMMAND} -E echo ""
672 COMMENT "Preparing syscall dependency handling"
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200673 )
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200674
675 add_custom_command(
676 OUTPUT
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200677 ${syscalls_subdirs_trigger}
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200678 COMMAND
679 ${PYTHON_EXECUTABLE}
Anas Nashif9ee1e322022-07-11 10:57:02 -0400680 ${ZEPHYR_BASE}/scripts/build/subfolder_list.py
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200681 --directory ${ZEPHYR_BASE}/include # Walk this directory
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200682 --out-file ${syscalls_subdirs_txt} # Write file with discovered folder
Jamie McCraeec704442023-01-04 16:08:36 +0000683 --trigger-file ${syscalls_subdirs_trigger} # Trigger file that is used for json generation
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200684 ${syscalls_links} # If defined, create symlinks for dependencies
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200685 DEPENDS ${PARSE_SYSCALLS_PATHS_DEPENDS}
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200686 )
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200687
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200688 # Ensure subdir file always exists when specifying CMake dependency.
689 if(NOT EXISTS ${syscalls_subdirs_txt})
690 file(WRITE ${syscalls_subdirs_txt} "")
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200691 endif()
692
693 # On other OS'es, modifying a file is reflected on the folder timestamp and hence detected
694 # when using depend on directory level.
695 # Thus CMake only needs to re-run when sub-directories are added / removed, which is indicated
696 # using a trigger file.
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200697 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${syscalls_subdirs_txt})
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200698endif()
699
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200700# syscall declarations are searched for in the SYSCALL_INCLUDE_DIRS
Adithya Baglodye67720b2018-07-02 14:59:19 +0530701if(CONFIG_APPLICATION_DEFINED_SYSCALL)
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200702 list(APPEND SYSCALL_INCLUDE_DIRS ${APPLICATION_SOURCE_DIR})
Adithya Baglodye67720b2018-07-02 14:59:19 +0530703endif()
704
Andrew Boiec1863872019-11-21 23:11:29 -0800705if(CONFIG_ZTEST)
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200706 list(APPEND SYSCALL_INCLUDE_DIRS ${ZEPHYR_BASE}/subsys/testsuite/ztest/include)
Andrew Boiec1863872019-11-21 23:11:29 -0800707endif()
708
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200709foreach(d ${SYSCALL_INCLUDE_DIRS})
710 list(APPEND parse_syscalls_include_args
711 --include ${d}
712 )
713endforeach()
714
Sebastian Bøe13a68402017-11-20 13:03:55 +0100715add_custom_command(
716 OUTPUT
717 ${syscalls_json}
Andrew Boie59601192020-05-29 13:24:51 -0700718 ${struct_tags_json}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100719 COMMAND
720 ${PYTHON_EXECUTABLE}
Anas Nashif92575fd2022-07-11 10:58:14 -0400721 ${ZEPHYR_BASE}/scripts/build/parse_syscalls.py
Adithya Baglodye67720b2018-07-02 14:59:19 +0530722 --include ${ZEPHYR_BASE}/include # Read files from this dir
Andrew Boiefed960b2020-05-29 13:33:12 -0700723 --include ${ZEPHYR_BASE}/drivers # For net sockets
724 --include ${ZEPHYR_BASE}/subsys/net # More net sockets
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200725 ${parse_syscalls_include_args} # Read files from these dirs also
Corey Whartonccd15df2020-02-29 14:51:42 -0800726 --json-file ${syscalls_json} # Write this file
Andrew Boie59601192020-05-29 13:24:51 -0700727 --tag-struct-file ${struct_tags_json} # Write subsystem list to this file
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200728 DEPENDS ${syscalls_subdirs_trigger} ${PARSE_SYSCALLS_HEADER_DEPENDS}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100729 )
730
Keith Packardd0c75f32020-10-26 19:07:50 -0700731# Make sure Picolibc is built before the rest of the system; there's no explicit
732# reference to any of the files as they're all picked up by various compiler
733# settings
734if(CONFIG_PICOLIBC_USE_MODULE)
735 set(picolibc_dependency PicolibcBuild)
736endif()
737
738add_custom_target(${SYSCALL_LIST_H_TARGET} DEPENDS ${syscall_list_h} ${picolibc_dependency})
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +0100739
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +0100740set_property(TARGET ${SYSCALL_LIST_H_TARGET}
741 APPEND PROPERTY
742 ADDITIONAL_CLEAN_FILES
743 ${CMAKE_CURRENT_BINARY_DIR}/include/generated/syscalls
744)
745
Andrew Boiec1c54b12020-03-16 12:48:00 -0700746add_custom_target(${PARSE_SYSCALLS_TARGET}
Joakim Andersson08d2f482020-08-04 18:26:43 +0200747 DEPENDS
748 ${syscalls_json}
Joakim Anderssond268f822020-08-04 18:31:48 +0200749 ${struct_tags_json}
Joakim Andersson08d2f482020-08-04 18:26:43 +0200750 )
Andrew Boie9ff64bb2019-11-05 09:39:05 -0800751
752# 64-bit systems do not require special handling of 64-bit system call
753# parameters or return values, indicate this to the system call boilerplate
754# generation script.
755if(CONFIG_64BIT)
756 set(SYSCALL_LONG_REGISTERS_ARG --long-registers)
757endif()
758
Andy Rosscfeb07e2020-03-05 21:14:02 -0800759if(CONFIG_TIMEOUT_64BIT)
Nicolas Pitre2cdac332022-03-04 21:21:38 -0500760 set(SYSCALL_SPLIT_TIMEOUT_ARG --split-type k_timeout_t --split-type k_ticks_t)
Andy Rosscfeb07e2020-03-05 21:14:02 -0800761endif()
762
Sebastian Bøe13a68402017-11-20 13:03:55 +0100763add_custom_command(OUTPUT include/generated/syscall_dispatch.c ${syscall_list_h}
764 # Also, some files are written to include/generated/syscalls/
765 COMMAND
766 ${PYTHON_EXECUTABLE}
Anas Nashifc74d20e2022-07-11 10:54:14 -0400767 ${ZEPHYR_BASE}/scripts/build/gen_syscalls.py
Sebastian Bøe13a68402017-11-20 13:03:55 +0100768 --json-file ${syscalls_json} # Read this file
769 --base-output include/generated/syscalls # Write to this dir
770 --syscall-dispatch include/generated/syscall_dispatch.c # Write this file
Andrew Boie353acf42018-07-23 18:10:15 -0700771 --syscall-list ${syscall_list_h}
Daniel Leung751de222023-03-10 14:07:59 -0800772 $<$<BOOL:${CONFIG_USERSPACE}>:--gen-mrsh-files>
Andrew Boie9ff64bb2019-11-05 09:39:05 -0800773 ${SYSCALL_LONG_REGISTERS_ARG}
Andy Rosscfeb07e2020-03-05 21:14:02 -0800774 ${SYSCALL_SPLIT_TIMEOUT_ARG}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100775 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
Andrew Boiec1c54b12020-03-16 12:48:00 -0700776 DEPENDS ${PARSE_SYSCALLS_TARGET}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100777 )
778
Corey Whartonccd15df2020-02-29 14:51:42 -0800779# This is passed into all calls to the gen_kobject_list.py script.
Jamie McCraeec704442023-01-04 16:08:36 +0000780set(gen_kobject_list_include_args --include-subsystem-list ${struct_tags_json})
Corey Whartonccd15df2020-02-29 14:51:42 -0800781
Leandro Pereirac2003672018-04-04 13:50:32 -0700782set(DRV_VALIDATION ${PROJECT_BINARY_DIR}/include/generated/driver-validation.h)
783add_custom_command(
784 OUTPUT ${DRV_VALIDATION}
785 COMMAND
786 ${PYTHON_EXECUTABLE}
Anas Nashifefbadbb2022-07-11 10:53:29 -0400787 ${ZEPHYR_BASE}/scripts/build/gen_kobject_list.py
Leandro Pereirac2003672018-04-04 13:50:32 -0700788 --validation-output ${DRV_VALIDATION}
Corey Whartonccd15df2020-02-29 14:51:42 -0800789 ${gen_kobject_list_include_args}
Leandro Pereirac2003672018-04-04 13:50:32 -0700790 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Corey Whartonccd15df2020-02-29 14:51:42 -0800791 DEPENDS
Anas Nashifefbadbb2022-07-11 10:53:29 -0400792 ${ZEPHYR_BASE}/scripts/build/gen_kobject_list.py
Andrew Boiec1c54b12020-03-16 12:48:00 -0700793 ${PARSE_SYSCALLS_TARGET}
Leandro Pereirac2003672018-04-04 13:50:32 -0700794 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
795 )
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100796add_custom_target(${DRIVER_VALIDATION_H_TARGET} DEPENDS ${DRV_VALIDATION})
Leandro Pereirac2003672018-04-04 13:50:32 -0700797
Torsten Rasmussend7862cf2020-02-12 15:42:09 +0100798include(${ZEPHYR_BASE}/cmake/kobj.cmake)
Leandro Pereira39dc7d02018-04-05 13:59:33 -0700799gen_kobj(KOBJ_INCLUDE_PATH)
Leandro Pereirac2003672018-04-04 13:50:32 -0700800
Sebastian Bøefdac7b32020-01-23 15:39:17 +0100801# Add a pseudo-target that is up-to-date when all generated headers
802# are up-to-date.
803
804add_custom_target(zephyr_generated_headers)
805add_dependencies(zephyr_generated_headers
Torsten Rasmussen91709772022-02-04 10:27:13 +0100806 offsets_h version_h
Sebastian Bøefdac7b32020-01-23 15:39:17 +0100807 )
808
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200809# Generate offsets.c.obj from offsets.c
810# Generate offsets.h from offsets.c.obj
811
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100812set(OFFSETS_LIB offsets)
813
Klaus Petersenc66cb762018-11-15 10:37:46 +0100814set(OFFSETS_C_PATH ${ARCH_DIR}/${ARCH}/core/offsets/offsets.c)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200815set(OFFSETS_H_PATH ${PROJECT_BINARY_DIR}/include/generated/offsets.h)
816
Klaus Petersen62e55e52019-02-04 12:10:57 +0100817add_library( ${OFFSETS_LIB} OBJECT ${OFFSETS_C_PATH})
Stephanos Ioannidis2d746042019-10-25 00:08:21 +0900818target_include_directories(${OFFSETS_LIB} PRIVATE
819 kernel/include
820 ${ARCH_DIR}/${ARCH}/include
821 )
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100822target_link_libraries(${OFFSETS_LIB} zephyr_interface)
Joakim Anderssond268f822020-08-04 18:31:48 +0200823add_dependencies(zephyr_interface
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100824 ${SYSCALL_LIST_H_TARGET}
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100825 ${DRIVER_VALIDATION_H_TARGET}
826 ${KOBJ_TYPES_H_TARGET}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100827 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200828
829add_custom_command(
830 OUTPUT ${OFFSETS_H_PATH}
Anas Nashife234c212022-07-11 10:53:45 -0400831 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/build/gen_offset_header.py
Klaus Petersen62e55e52019-02-04 12:10:57 +0100832 -i $<TARGET_OBJECTS:${OFFSETS_LIB}>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200833 -o ${OFFSETS_H_PATH}
Sebastian Bøe5962aab2019-08-15 14:45:59 +0200834 DEPENDS
835 ${OFFSETS_LIB}
836 $<TARGET_OBJECTS:${OFFSETS_LIB}>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200837)
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100838add_custom_target(${OFFSETS_H_TARGET} DEPENDS ${OFFSETS_H_PATH})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200839
Sebastian Bøe89516fb2017-12-01 15:25:06 +0100840zephyr_get_include_directories_for_lang(C ZEPHYR_INCLUDES)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200841
842add_subdirectory(kernel)
843
844# Read list content
845get_property(ZEPHYR_LIBS_PROPERTY GLOBAL PROPERTY ZEPHYR_LIBS)
846
847foreach(zephyr_lib ${ZEPHYR_LIBS_PROPERTY})
Torsten Rasmussend9520042021-04-14 17:11:39 +0200848 get_property(lib_type TARGET ${zephyr_lib} PROPERTY TYPE)
Torsten Rasmussend9520042021-04-14 17:11:39 +0200849 # To prevent CMake failure when a driver is enabled, for example: REGULATOR=y
850 # we disable any Zephyr libraries without sources and adds the `empty_file.c`.
851 if(${lib_type} STREQUAL STATIC_LIBRARY
Torsten Rasmussend9520042021-04-14 17:11:39 +0200852 AND NOT ${zephyr_lib} STREQUAL app
853 )
Torsten Rasmussen25578be2021-04-23 09:09:49 +0200854 get_property(source_list TARGET ${zephyr_lib} PROPERTY SOURCES)
855 get_property(lib_imported TARGET ${zephyr_lib} PROPERTY IMPORTED)
856 if(NOT source_list
857 AND NOT ${lib_imported}
Torsten Rasmussend9520042021-04-14 17:11:39 +0200858 )
Torsten Rasmussen153196b2021-09-06 16:42:21 +0200859 get_property(allow_empty TARGET ${zephyr_lib} PROPERTY ALLOW_EMPTY)
860 if(NOT "${allow_empty}")
861 message(WARNING
862 "No SOURCES given to Zephyr library: ${zephyr_lib}\nExcluding target from build."
863 )
864 endif()
Torsten Rasmussen25578be2021-04-23 09:09:49 +0200865 target_sources(${zephyr_lib} PRIVATE ${ZEPHYR_BASE}/misc/empty_file.c)
866 set_property(TARGET ${zephyr_lib} PROPERTY EXCLUDE_FROM_ALL TRUE)
867 list(REMOVE_ITEM ZEPHYR_LIBS_PROPERTY ${zephyr_lib})
868 continue()
869 endif()
Torsten Rasmussend9520042021-04-14 17:11:39 +0200870 endif()
Torsten Rasmussen25578be2021-04-23 09:09:49 +0200871
872 # TODO: Could this become an INTERFACE property of zephyr_interface?
873 add_dependencies(${zephyr_lib} zephyr_generated_headers)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200874endforeach()
875
876get_property(OUTPUT_FORMAT GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT)
877
Adithya Baglody62e152a2018-11-13 15:34:02 +0530878if (CONFIG_CODE_DATA_RELOCATION)
879 set(CODE_RELOCATION_DEP code_relocation_source_lib)
880endif() # CONFIG_CODE_DATA_RELOCATION
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100881
Daniel Leungc7459952021-03-19 12:09:05 -0700882# Give the linker script targets all of the include directories so
883# that cmake can successfully find the linker scripts' header
Sebastian Bøeb1ab5012017-12-14 13:03:23 +0100884# dependencies.
885zephyr_get_include_directories_for_lang(C
886 ZEPHYR_INCLUDE_DIRS
887 STRIP_PREFIX # Don't use a -I prefix
888 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200889
Flavio Ceolin0b13b442022-01-05 17:19:53 -0800890if(CONFIG_PM_DEVICE_POWER_DOMAIN_DYNAMIC)
891 set(number_of_dynamic_devices ${CONFIG_PM_DEVICE_POWER_DOMAIN_DYNAMIC_NUM})
892else()
893 set(number_of_dynamic_devices 0)
894endif()
895
Morten Priessa846e722021-04-21 09:06:02 +0200896if(CONFIG_HAS_DTS)
Jordan Yates28b2e552021-10-20 20:19:28 +1000897 # dev_handles.c is generated from ${ZEPHYR_LINK_STAGE_EXECUTABLE} by
Morten Priessa846e722021-04-21 09:06:02 +0200898 # gen_handles.py
899 add_custom_command(
900 OUTPUT dev_handles.c
901 COMMAND
902 ${PYTHON_EXECUTABLE}
Anas Nashif80f4b5d2022-07-11 10:48:04 -0400903 ${ZEPHYR_BASE}/scripts/build/gen_handles.py
Morten Priessa846e722021-04-21 09:06:02 +0200904 --output-source dev_handles.c
Jordan Yates29942472022-07-10 13:46:17 +1000905 --output-graphviz dev_graph.dot
Flavio Ceolin0b13b442022-01-05 17:19:53 -0800906 --num-dynamic-devices ${number_of_dynamic_devices}
Jordan Yates28b2e552021-10-20 20:19:28 +1000907 --kernel $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
Morten Priessa846e722021-04-21 09:06:02 +0200908 --zephyr-base ${ZEPHYR_BASE}
Torsten Rasmussenc9804d22021-05-21 21:34:58 +0200909 --start-symbol "$<TARGET_PROPERTY:linker,devices_start_symbol>"
Torsten Rasmussen6d72d912022-01-05 11:25:26 +0100910 VERBATIM
Jordan Yates28b2e552021-10-20 20:19:28 +1000911 DEPENDS ${ZEPHYR_LINK_STAGE_EXECUTABLE}
Morten Priessa846e722021-04-21 09:06:02 +0200912 )
Jordan Yates28b2e552021-10-20 20:19:28 +1000913 set_property(GLOBAL APPEND PROPERTY GENERATED_APP_SOURCE_FILES dev_handles.c)
914
915 # gen_handles runs on `__device_handles_pass1` so pass this info to the linker script generator
916 list(APPEND LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE "LINKER_DEVICE_HANDLES_PASS1")
Morten Priessa846e722021-04-21 09:06:02 +0200917endif()
Peter Bigotd554d342020-06-30 10:05:35 -0500918
Adithya Baglody62e152a2018-11-13 15:34:02 +0530919if(CONFIG_CODE_DATA_RELOCATION)
Mark Ruvald Pedersen86a3e8f2019-05-03 10:33:03 +0200920 # @Intent: Linker script to relocate .text, data and .bss sections
921 toolchain_ld_relocation()
Adithya Baglody62e152a2018-11-13 15:34:02 +0530922endif()
923
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530924if(CONFIG_USERSPACE)
Torsten Rasmussene37d9e62020-11-20 18:39:30 +0100925 zephyr_get_compile_options_for_lang_as_string(C compiler_flags_priv)
Torsten Rasmussene0758c32020-08-21 19:13:53 +0200926 string(REPLACE "$<TARGET_PROPERTY:compiler,coverage>" ""
927 NO_COVERAGE_FLAGS "${compiler_flags_priv}"
928 )
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530929
Anas Nashifefbadbb2022-07-11 10:53:29 -0400930 set(GEN_KOBJ_LIST ${ZEPHYR_BASE}/scripts/build/gen_kobject_list.py)
Anas Nashifd8599972022-07-11 10:56:46 -0400931 set(PROCESS_GPERF ${ZEPHYR_BASE}/scripts/build/process_gperf.py)
Jordan Yates28b2e552021-10-20 20:19:28 +1000932endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200933
Jordan Yates28b2e552021-10-20 20:19:28 +1000934get_property(CSTD GLOBAL PROPERTY CSTD)
935set_ifndef(CSTD c99)
936
937# @Intent: Obtain compiler specific flag for specifying the c standard
938zephyr_compile_options(
939 $<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,cstd>${CSTD}>
940)
941set(CMAKE_C_COMPILE_FEATURES ${compile_features_${CSTD}} PARENT_SCOPE)
942
943# @Intent: Configure linker scripts, i.e. generate linker scripts with variables substituted
944toolchain_ld_configure_files()
945
946if(CONFIG_USERSPACE)
947 set(APP_SMEM_ALIGNED_LD "${PROJECT_BINARY_DIR}/include/generated/app_smem_aligned.ld")
948 set(APP_SMEM_UNALIGNED_LD "${PROJECT_BINARY_DIR}/include/generated/app_smem_unaligned.ld")
949
950 if(CONFIG_LINKER_USE_PINNED_SECTION)
951 set(APP_SMEM_PINNED_ALIGNED_LD
952 "${PROJECT_BINARY_DIR}/include/generated/app_smem_pinned_aligned.ld")
953 set(APP_SMEM_PINNED_UNALIGNED_LD
954 "${PROJECT_BINARY_DIR}/include/generated/app_smem_pinned_unaligned.ld")
955
956 if(NOT CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT)
957 # The libc partition may hold symbols that are required during boot process,
958 # for example, stack guard (if enabled). So the libc partition must be pinned
959 # if not sections are in physical memory at boot, as the paging mechanism is
960 # only initialized post-kernel.
961 set_property(TARGET app_smem APPEND PROPERTY pinned_partitions "z_libc_partition")
962 endif()
963
964 get_property(APP_SMEM_PINNED_PARTITION_LIST TARGET app_smem PROPERTY pinned_partitions)
965 if(APP_SMEM_PINNED_PARTITION_LIST)
966 list(JOIN APP_SMEM_PINNED_PARTITION_LIST "," APP_SMEM_PINNED_PARTITION_LIST_ARG_CSL)
967 set(APP_SMEM_PINNED_PARTITION_LIST_ARG "--pinpartitions=${APP_SMEM_PINNED_PARTITION_LIST_ARG_CSL}")
968 endif()
969 endif()
970
971 set(OBJ_FILE_DIR "${PROJECT_BINARY_DIR}/../")
972
973 if(CONFIG_NEWLIB_LIBC)
Keith Packardd0c75f32020-10-26 19:07:50 -0700974 set(LIBC_PART -l libc.a z_libc_partition -l libm.a z_libc_partition)
Jordan Yates28b2e552021-10-20 20:19:28 +1000975 endif()
976 if(CONFIG_NEWLIB_LIBC_NANO)
Keith Packardd0c75f32020-10-26 19:07:50 -0700977 set(LIBC_PART -l libc_nano.a z_libc_partition -l libm_nano.a z_libc_partition)
978 endif()
979 if(CONFIG_PICOLIBC)
980 set(LIBC_PART -l libc.a z_libc_partition)
Jordan Yates28b2e552021-10-20 20:19:28 +1000981 endif()
982
983 add_custom_command(
984 OUTPUT ${APP_SMEM_UNALIGNED_LD} ${APP_SMEM_PINNED_UNALIGNED_LD}
985 COMMAND ${PYTHON_EXECUTABLE}
Anas Nashif6e1a3352022-07-11 10:46:17 -0400986 ${ZEPHYR_BASE}/scripts/build/gen_app_partitions.py
Torsten Rasmussenf643b8b2021-11-24 15:35:47 +0100987 -f ${CMAKE_BINARY_DIR}/compile_commands.json
Jordan Yates28b2e552021-10-20 20:19:28 +1000988 -o ${APP_SMEM_UNALIGNED_LD}
989 $<$<BOOL:${APP_SMEM_PINNED_UNALIGNED_LD}>:--pinoutput=${APP_SMEM_PINNED_UNALIGNED_LD}>
990 ${APP_SMEM_PINNED_PARTITION_LIST_ARG}
Keith Packardd0c75f32020-10-26 19:07:50 -0700991 ${LIBC_PART}
Jordan Yates28b2e552021-10-20 20:19:28 +1000992 $<TARGET_PROPERTY:zephyr_property_target,COMPILE_OPTIONS>
993 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
994 DEPENDS
995 kernel
Andy Ross6cfb1862022-08-11 16:45:57 -0700996 ${CMAKE_BINARY_DIR}/compile_commands.json
Jordan Yates28b2e552021-10-20 20:19:28 +1000997 ${ZEPHYR_LIBS_PROPERTY}
998 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
999 COMMAND_EXPAND_LISTS
1000 COMMENT "Generating app_smem_unaligned linker section"
1001 )
1002
1003 add_custom_target(
1004 ${APP_SMEM_ALIGNED_DEP}
1005 DEPENDS
1006 ${APP_SMEM_ALIGNED_LD}
1007 ${APP_SMEM_PINNED_ALIGNED_LD}
1008 )
1009
1010 add_custom_target(
1011 ${APP_SMEM_UNALIGNED_DEP}
1012 DEPENDS
1013 ${APP_SMEM_UNALIGNED_LD}
1014 ${APP_SMEM_PINNED_UNALIGNED_LD}
1015 )
1016
1017 set(APP_SMEM_UNALIGNED_LIB app_smem_unaligned_output_obj_renamed_lib)
1018 list(APPEND LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE "LINKER_APP_SMEM_UNALIGNED")
1019endif()
1020
1021if (CONFIG_USERSPACE)
1022 add_custom_command(
1023 OUTPUT ${APP_SMEM_ALIGNED_LD} ${APP_SMEM_PINNED_ALIGNED_LD}
1024 COMMAND ${PYTHON_EXECUTABLE}
Anas Nashif6e1a3352022-07-11 10:46:17 -04001025 ${ZEPHYR_BASE}/scripts/build/gen_app_partitions.py
Jordan Yates28b2e552021-10-20 20:19:28 +10001026 -e $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
1027 -o ${APP_SMEM_ALIGNED_LD}
1028 $<$<BOOL:${APP_SMEM_PINNED_ALIGNED_LD}>:--pinoutput=${APP_SMEM_PINNED_ALIGNED_LD}>
1029 ${APP_SMEM_PINNED_PARTITION_LIST_ARG}
Keith Packardd0c75f32020-10-26 19:07:50 -07001030 ${LIBC_PART}
Jordan Yates28b2e552021-10-20 20:19:28 +10001031 $<TARGET_PROPERTY:zephyr_property_target,COMPILE_OPTIONS>
1032 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1033 DEPENDS
1034 kernel
1035 ${ZEPHYR_LIBS_PROPERTY}
1036 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1037 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
1038 COMMAND_EXPAND_LISTS
1039 COMMENT "Generating app_smem_aligned linker section"
1040 )
1041endif()
1042
1043if(CONFIG_USERSPACE)
1044 # This CONFIG_USERSPACE block is to create place holders to reserve space
1045 # for the gperf generated structures for zephyr_prebuilt.elf.
1046 # These place holders are there so that the placement of kobjects would be
1047 # the same between linking zephyr_prebuilt.elf and zephyr.elf, as
1048 # the gperf hash table is hashed on the addresses of kobjects.
1049 # The placeholders are generated from app_smem_unaligned_prebuilt.elf.
1050
1051 set(KOBJECT_PREBUILT_HASH_LIST kobject_prebuilt_hash.gperf)
1052 set(KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE kobject_prebuilt_hash_preprocessed.c)
1053 set(KOBJECT_PREBUILT_HASH_OUTPUT_SRC kobject_prebuilt_hash.c)
1054
1055 add_custom_command(
1056 OUTPUT ${KOBJECT_PREBUILT_HASH_LIST}
1057 COMMAND
1058 ${PYTHON_EXECUTABLE}
1059 ${GEN_KOBJ_LIST}
1060 --kernel $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
1061 --gperf-output ${KOBJECT_PREBUILT_HASH_LIST}
1062 ${gen_kobject_list_include_args}
1063 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1064 DEPENDS
1065 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1066 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1067 )
1068 add_custom_target(
1069 kobj_prebuilt_hash_list
1070 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_LIST}
1071 )
1072
1073 add_custom_command(
1074 OUTPUT ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
1075 COMMAND
1076 ${GPERF}
1077 --output-file ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
1078 --multiple-iterations 10
1079 ${KOBJECT_PREBUILT_HASH_LIST}
1080 DEPENDS kobj_prebuilt_hash_list ${KOBJECT_PREBUILT_HASH_LIST}
1081 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1082 )
1083 add_custom_target(
1084 kobj_prebuilt_hash_output_src_pre
1085 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
1086 )
1087
1088 add_custom_command(
1089 OUTPUT ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1090 COMMAND
1091 ${PYTHON_EXECUTABLE}
1092 ${PROCESS_GPERF}
1093 -i ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
1094 -o ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1095 -p "struct z_object"
1096 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1097 DEPENDS kobj_prebuilt_hash_output_src_pre ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
1098 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1099 )
1100 add_custom_target(
1101 kobj_prebuilt_hash_output_src
1102 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1103 )
1104
1105 add_library(
1106 kobj_prebuilt_hash_output_lib
1107 OBJECT ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1108 )
1109
1110 set_source_files_properties(${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1111 PROPERTIES COMPILE_FLAGS
1112 "${NO_COVERAGE_FLAGS} -fno-function-sections -fno-data-sections")
1113
1114 target_compile_definitions(kobj_prebuilt_hash_output_lib
1115 PRIVATE $<TARGET_PROPERTY:zephyr_interface,INTERFACE_COMPILE_DEFINITIONS>
1116 )
1117
1118 target_include_directories(kobj_prebuilt_hash_output_lib
1119 PUBLIC $<TARGET_PROPERTY:zephyr_interface,INTERFACE_INCLUDE_DIRECTORIES>
1120 )
1121
1122 target_include_directories(kobj_prebuilt_hash_output_lib SYSTEM
1123 PUBLIC $<TARGET_PROPERTY:zephyr_interface,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
1124 )
1125
1126 set(KOBJECT_LINKER_HEADER_DATA "${PROJECT_BINARY_DIR}/include/generated/linker-kobject-prebuilt-data.h")
1127
1128 add_custom_command(
1129 OUTPUT ${KOBJECT_LINKER_HEADER_DATA}
1130 COMMAND
1131 ${PYTHON_EXECUTABLE}
Anas Nashifd5dcf202022-07-11 10:53:38 -04001132 ${ZEPHYR_BASE}/scripts/build/gen_kobject_placeholders.py
Jordan Yates28b2e552021-10-20 20:19:28 +10001133 --object $<TARGET_OBJECTS:kobj_prebuilt_hash_output_lib>
1134 --outdir ${PROJECT_BINARY_DIR}/include/generated
1135 --datapct ${CONFIG_KOBJECT_DATA_AREA_RESERVE_EXTRA_PERCENT}
1136 --rodata ${CONFIG_KOBJECT_RODATA_AREA_EXTRA_BYTES}
1137 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1138 DEPENDS
Torsten Rasmussen04543512022-02-03 15:14:44 +01001139 kobj_prebuilt_hash_output_lib $<TARGET_OBJECTS:kobj_prebuilt_hash_output_lib>
Jordan Yates28b2e552021-10-20 20:19:28 +10001140 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1141 )
1142
1143 add_custom_target(
1144 ${KOBJECT_LINKER_DEP}
1145 DEPENDS
1146 ${KOBJECT_LINKER_HEADER_DATA}
1147 )
1148endif()
1149
1150if(CONFIG_USERSPACE OR CONFIG_HAS_DTS)
1151 configure_linker_script(
1152 ${ZEPHYR_CURRENT_LINKER_CMD}
1153 "${LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE}"
1154 ${CODE_RELOCATION_DEP}
1155 ${APP_SMEM_UNALIGNED_DEP}
1156 ${APP_SMEM_UNALIGNED_LD}
1157 ${APP_SMEM_PINNED_UNALIGNED_LD}
1158 zephyr_generated_headers
1159 )
1160
1161 add_custom_target(
1162 linker_zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS}_script
1163 DEPENDS
1164 ${ZEPHYR_CURRENT_LINKER_CMD}
1165 )
1166
1167 set_property(TARGET
1168 linker_zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS}_script
1169 PROPERTY INCLUDE_DIRECTORIES
1170 ${ZEPHYR_INCLUDE_DIRS}
1171 )
1172
1173 add_executable(${ZEPHYR_LINK_STAGE_EXECUTABLE} misc/empty_file.c)
1174 toolchain_ld_link_elf(
1175 TARGET_ELF ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1176 OUTPUT_MAP ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
1177 LIBRARIES_PRE_SCRIPT ""
1178 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD}
1179 LIBRARIES_POST_SCRIPT ""
1180 DEPENDENCIES ${CODE_RELOCATION_DEP}
1181 )
1182 target_byproducts(TARGET ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1183 BYPRODUCTS ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
1184 )
1185 set_property(TARGET ${ZEPHYR_LINK_STAGE_EXECUTABLE} PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD})
1186 add_dependencies(${ZEPHYR_LINK_STAGE_EXECUTABLE} linker_zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS}_script ${OFFSETS_LIB})
1187
1188 math(EXPR ZEPHYR_CURRENT_LINKER_PASS "1 + ${ZEPHYR_CURRENT_LINKER_PASS}")
1189endif()
1190
1191set(ZEPHYR_CURRENT_LINKER_CMD linker_zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS}.cmd)
1192set(ZEPHYR_LINK_STAGE_EXECUTABLE zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS})
1193list(APPEND LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE "LINKER_ZEPHYR_PREBUILT")
1194
1195if(CONFIG_GEN_ISR_TABLES)
1196 if(CONFIG_GEN_SW_ISR_TABLE)
1197 list(APPEND GEN_ISR_TABLE_EXTRA_ARG --sw-isr-table)
1198 endif()
1199
1200 if(CONFIG_GEN_IRQ_VECTOR_TABLE)
1201 list(APPEND GEN_ISR_TABLE_EXTRA_ARG --vector-table)
1202 endif()
1203
1204 # isr_tables.c is generated from ${ZEPHYR_LINK_STAGE_EXECUTABLE} by
1205 # gen_isr_tables.py
1206 add_custom_command(
1207 OUTPUT isr_tables.c isrList.bin
1208 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1209 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1210 $<TARGET_PROPERTY:bintools,elfconvert_flag_intarget>${OUTPUT_FORMAT}
1211 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>binary
1212 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_only>.intList
1213 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>$<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
1214 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>isrList.bin
1215 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
1216 COMMAND ${PYTHON_EXECUTABLE}
Anas Nashifc36307e2022-07-11 10:51:50 -04001217 ${ZEPHYR_BASE}/scripts/build/gen_isr_tables.py
Jordan Yates28b2e552021-10-20 20:19:28 +10001218 --output-source isr_tables.c
1219 --kernel $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
1220 --intlist isrList.bin
1221 $<$<BOOL:${CONFIG_BIG_ENDIAN}>:--big-endian>
1222 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--debug>
1223 ${GEN_ISR_TABLE_EXTRA_ARG}
1224 DEPENDS ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1225 COMMAND_EXPAND_LISTS
1226 )
1227 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_SOURCE_FILES isr_tables.c)
1228endif()
1229
1230if(CONFIG_USERSPACE)
Daniel Leung28c35122021-03-18 12:02:19 -07001231 set(KOBJECT_HASH_LIST kobject_hash.gperf)
1232 set(KOBJECT_HASH_OUTPUT_SRC_PRE kobject_hash_preprocessed.c)
1233 set(KOBJECT_HASH_OUTPUT_SRC kobject_hash.c)
Daniel Leung28c35122021-03-18 12:02:19 -07001234 set(KOBJECT_HASH_OUTPUT_OBJ_RENAMED kobject_hash_renamed.o)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001235
1236 # Essentially what we are doing here is extracting some information
1237 # out of the nearly finished elf file, generating the source code
1238 # for a hash table based on that information, and then compiling and
1239 # linking the hash table back into a now even more nearly finished
Marc Herbert4a10eea2019-04-16 15:39:45 -07001240 # elf file. More information in gen_kobject_list.py --help.
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001241
1242 # Use the script GEN_KOBJ_LIST to scan the kernel binary's
Jordan Yates28b2e552021-10-20 20:19:28 +10001243 # (${ZEPHYR_LINK_STAGE_EXECUTABLE}) DWARF information to produce a table of kernel
Daniel Leung28c35122021-03-18 12:02:19 -07001244 # objects (KOBJECT_HASH_LIST) which we will then pass to gperf
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001245 add_custom_command(
Daniel Leung28c35122021-03-18 12:02:19 -07001246 OUTPUT ${KOBJECT_HASH_LIST}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001247 COMMAND
1248 ${PYTHON_EXECUTABLE}
1249 ${GEN_KOBJ_LIST}
Jordan Yates28b2e552021-10-20 20:19:28 +10001250 --kernel $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
Daniel Leung28c35122021-03-18 12:02:19 -07001251 --gperf-output ${KOBJECT_HASH_LIST}
Corey Whartonccd15df2020-02-29 14:51:42 -08001252 ${gen_kobject_list_include_args}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001253 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Corey Whartonccd15df2020-02-29 14:51:42 -08001254 DEPENDS
Jordan Yates28b2e552021-10-20 20:19:28 +10001255 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001256 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1257 )
Daniel Leung28c35122021-03-18 12:02:19 -07001258 add_custom_target(
1259 kobj_hash_list
1260 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_LIST}
1261 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001262
Daniel Leung28c35122021-03-18 12:02:19 -07001263 # Use gperf to generate C code (KOBJECT_HASH_OUTPUT_SRC_PRE) which implements a
1264 # perfect hashtable based on KOBJECT_HASH_LIST
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001265 add_custom_command(
Torsten Rasmussen04543512022-02-03 15:14:44 +01001266 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC_PRE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001267 COMMAND
1268 ${GPERF}
Daniel Leung28c35122021-03-18 12:02:19 -07001269 --output-file ${KOBJECT_HASH_OUTPUT_SRC_PRE}
Daniel Leung11171692021-03-18 14:00:07 -07001270 --multiple-iterations 10
Daniel Leung28c35122021-03-18 12:02:19 -07001271 ${KOBJECT_HASH_LIST}
1272 DEPENDS kobj_hash_list ${KOBJECT_HASH_LIST}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001273 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1274 )
Daniel Leung28c35122021-03-18 12:02:19 -07001275 add_custom_target(
1276 kobj_hash_output_src_pre
1277 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC_PRE}
1278 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001279
1280 # For our purposes the code/data generated by gperf is not optimal.
1281 #
Daniel Leung28c35122021-03-18 12:02:19 -07001282 # The script PROCESS_GPERF creates a new c file KOBJECT_HASH_OUTPUT_SRC based on
1283 # KOBJECT_HASH_OUTPUT_SRC_PRE to greatly reduce the amount of code/data generated
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001284 # since we know we are always working with pointer values
1285 add_custom_command(
Daniel Leung28c35122021-03-18 12:02:19 -07001286 OUTPUT ${KOBJECT_HASH_OUTPUT_SRC}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001287 COMMAND
Sebastian Bøe1b600702018-06-21 14:34:42 +02001288 ${PYTHON_EXECUTABLE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001289 ${PROCESS_GPERF}
Daniel Leung28c35122021-03-18 12:02:19 -07001290 -i ${KOBJECT_HASH_OUTPUT_SRC_PRE}
1291 -o ${KOBJECT_HASH_OUTPUT_SRC}
Andrew Boie2dc2ecf2020-03-11 07:13:07 -07001292 -p "struct z_object"
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001293 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Torsten Rasmussen04543512022-02-03 15:14:44 +01001294 DEPENDS kobj_hash_output_src_pre ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC_PRE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001295 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1296 )
Daniel Leung28c35122021-03-18 12:02:19 -07001297 add_custom_target(
1298 kobj_hash_output_src
1299 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC}
1300 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001301
1302 # We need precise control of where generated text/data ends up in the final
1303 # kernel image. Disable function/data sections and use objcopy to move
1304 # generated data into special section names
Daniel Leung28c35122021-03-18 12:02:19 -07001305 add_library(
1306 kobj_hash_output_lib
Torsten Rasmussen47304d42021-11-02 12:06:05 +01001307 OBJECT ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001308 )
1309
Daniel Leung28c35122021-03-18 12:02:19 -07001310 set_source_files_properties(${KOBJECT_HASH_OUTPUT_SRC}
1311 PROPERTIES COMPILE_FLAGS
Andrew Boiea5148982019-03-14 17:04:11 -07001312 "${NO_COVERAGE_FLAGS} -fno-function-sections -fno-data-sections")
Adithya Baglody4b3c7b32018-11-21 14:31:56 +05301313
Torsten Rasmussen47304d42021-11-02 12:06:05 +01001314 target_compile_definitions(kobj_hash_output_lib
1315 PRIVATE $<TARGET_PROPERTY:zephyr_interface,INTERFACE_COMPILE_DEFINITIONS>
1316 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001317
Torsten Rasmussen47304d42021-11-02 12:06:05 +01001318 target_include_directories(kobj_hash_output_lib
1319 PUBLIC $<TARGET_PROPERTY:zephyr_interface,INTERFACE_INCLUDE_DIRECTORIES>
1320 )
Adithya Baglody4b3c7b32018-11-21 14:31:56 +05301321
Torsten Rasmussen47304d42021-11-02 12:06:05 +01001322 target_include_directories(kobj_hash_output_lib SYSTEM
1323 PUBLIC $<TARGET_PROPERTY:zephyr_interface,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
Daniel Leung28c35122021-03-18 12:02:19 -07001324 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001325
1326 add_custom_command(
Daniel Leung28c35122021-03-18 12:02:19 -07001327 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001328 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1329 $<TARGET_PROPERTY:bintools,elfconvert_flag>
Flavio Ceolin02d52902022-12-20 13:29:51 -08001330 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.literal=.kobject_data.literal
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001331 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.data=.kobject_data.data
Jim Shu70e1dee2021-07-08 01:11:34 +08001332 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.sdata=.kobject_data.sdata
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001333 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.text=.kobject_data.text
1334 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.rodata=.kobject_data.rodata
Torsten Rasmussen47304d42021-11-02 12:06:05 +01001335 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>$<TARGET_OBJECTS:kobj_hash_output_lib>
Daniel Leung28c35122021-03-18 12:02:19 -07001336 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001337 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Torsten Rasmussen04543512022-02-03 15:14:44 +01001338 DEPENDS kobj_hash_output_lib $<TARGET_OBJECTS:kobj_hash_output_lib>
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001339 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001340 COMMAND_EXPAND_LISTS
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001341 )
Daniel Leung28c35122021-03-18 12:02:19 -07001342 add_custom_target(
1343 kobj_hash_output_obj_renamed
1344 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
1345 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001346
Daniel Leung28c35122021-03-18 12:02:19 -07001347 add_library(kobj_hash_output_obj_renamed_lib STATIC IMPORTED GLOBAL)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001348 set_property(
Daniel Leung28c35122021-03-18 12:02:19 -07001349 TARGET kobj_hash_output_obj_renamed_lib
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001350 PROPERTY
Daniel Leung28c35122021-03-18 12:02:19 -07001351 IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001352 )
1353 add_dependencies(
Daniel Leung28c35122021-03-18 12:02:19 -07001354 kobj_hash_output_obj_renamed_lib
1355 kobj_hash_output_obj_renamed
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001356 )
1357
Daniel Leung28c35122021-03-18 12:02:19 -07001358 set_property(
1359 GLOBAL APPEND PROPERTY
1360 GENERATED_KERNEL_OBJECT_FILES kobj_hash_output_obj_renamed_lib
1361 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001362endif()
1363
Daniel Leungc7459952021-03-19 12:09:05 -07001364configure_linker_script(
Jordan Yates28b2e552021-10-20 20:19:28 +10001365 ${ZEPHYR_CURRENT_LINKER_CMD}
1366 "${LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE}"
Daniel Leungc7459952021-03-19 12:09:05 -07001367 ${APP_SMEM_ALIGNED_DEP}
Daniel Leung11171692021-03-18 14:00:07 -07001368 ${KOBJECT_LINKER_DEP}
Daniel Leungc7459952021-03-19 12:09:05 -07001369 ${CODE_RELOCATION_DEP}
1370 zephyr_generated_headers
1371 )
1372
1373add_custom_target(
1374 linker_zephyr_prebuilt_script_target
1375 DEPENDS
Jordan Yates28b2e552021-10-20 20:19:28 +10001376 ${ZEPHYR_CURRENT_LINKER_CMD}
Daniel Leungc7459952021-03-19 12:09:05 -07001377 )
1378
1379set_property(TARGET
1380 linker_zephyr_prebuilt_script_target
1381 PROPERTY INCLUDE_DIRECTORIES
1382 ${ZEPHYR_INCLUDE_DIRS}
1383 )
1384
Jordan Yates28b2e552021-10-20 20:19:28 +10001385# Read global variables into local variables
1386get_property(GASF GLOBAL PROPERTY GENERATED_APP_SOURCE_FILES)
1387get_property(GKOF GLOBAL PROPERTY GENERATED_KERNEL_OBJECT_FILES)
1388get_property(GKSF GLOBAL PROPERTY GENERATED_KERNEL_SOURCE_FILES)
1389
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001390# FIXME: Is there any way to get rid of empty_file.c?
Jordan Yates28b2e552021-10-20 20:19:28 +10001391add_executable( ${ZEPHYR_LINK_STAGE_EXECUTABLE} misc/empty_file.c ${GASF})
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001392toolchain_ld_link_elf(
Jordan Yates28b2e552021-10-20 20:19:28 +10001393 TARGET_ELF ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1394 OUTPUT_MAP ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001395 LIBRARIES_PRE_SCRIPT ""
Jordan Yates28b2e552021-10-20 20:19:28 +10001396 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD}
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001397 DEPENDENCIES ${CODE_RELOCATION_DEP}
1398)
Jordan Yates28b2e552021-10-20 20:19:28 +10001399target_byproducts(TARGET ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1400 BYPRODUCTS ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +01001401)
Daniel Leungc7459952021-03-19 12:09:05 -07001402set_property(TARGET
Jordan Yates28b2e552021-10-20 20:19:28 +10001403 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1404 PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD}
Daniel Leungc7459952021-03-19 12:09:05 -07001405 )
1406add_dependencies(
Jordan Yates28b2e552021-10-20 20:19:28 +10001407 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
Daniel Leungc7459952021-03-19 12:09:05 -07001408 linker_zephyr_prebuilt_script_target
1409 ${OFFSETS_LIB}
1410 )
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +02001411
Marc Herbert498b4942019-04-16 23:30:52 -07001412set(generated_kernel_files ${GKSF} ${GKOF})
1413if(NOT generated_kernel_files)
1414 # Use the prebuilt elf as the final elf since we don't have a
1415 # generation stage.
Jordan Yates28b2e552021-10-20 20:19:28 +10001416 set(logical_target_for_zephyr_elf ${ZEPHYR_LINK_STAGE_EXECUTABLE})
Marc Herbert498b4942019-04-16 23:30:52 -07001417else()
Daniel Leungcdd02a92021-03-19 12:18:52 -07001418 # The final linker pass uses the same source linker script of the
1419 # previous passes, but this time with a different output
1420 # file and preprocessed with the define LINKER_ZEPHYR_FINAL.
Mark Ruvald Pedersen4c811972019-04-29 17:16:54 +02001421 configure_linker_script(
Daniel Leungcdd02a92021-03-19 12:18:52 -07001422 linker.cmd
Torsten Rasmussen1fa3f152021-11-01 12:53:28 +01001423 "LINKER_ZEPHYR_FINAL"
Sebastian Bøe2a963122019-02-08 15:49:57 +01001424 ${CODE_RELOCATION_DEP}
Jordan Yates28b2e552021-10-20 20:19:28 +10001425 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
Sebastian Bøefdac7b32020-01-23 15:39:17 +01001426 zephyr_generated_headers
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +01001427 )
Andy Grosse8860fe2018-02-01 01:12:32 -06001428
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001429 add_custom_target(
Daniel Leungcdd02a92021-03-19 12:18:52 -07001430 linker_zephyr_final_script_target
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001431 DEPENDS
Daniel Leungcdd02a92021-03-19 12:18:52 -07001432 linker.cmd
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001433 )
Sebastian Bøeb1ab5012017-12-14 13:03:23 +01001434 set_property(TARGET
Daniel Leungcdd02a92021-03-19 12:18:52 -07001435 linker_zephyr_final_script_target
Sebastian Bøeb1ab5012017-12-14 13:03:23 +01001436 PROPERTY INCLUDE_DIRECTORIES
1437 ${ZEPHYR_INCLUDE_DIRS}
1438 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001439
Jordan Yates8d932172021-10-20 20:09:39 +10001440 add_executable( ${ZEPHYR_FINAL_EXECUTABLE} misc/empty_file.c ${GASF} ${GKSF})
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001441 toolchain_ld_link_elf(
1442 TARGET_ELF ${ZEPHYR_FINAL_EXECUTABLE}
Marc Herbert0370c9b2019-06-13 16:15:44 -07001443 OUTPUT_MAP ${PROJECT_BINARY_DIR}/${ZEPHYR_FINAL_EXECUTABLE}.map
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001444 LIBRARIES_PRE_SCRIPT ${GKOF}
Daniel Leungcdd02a92021-03-19 12:18:52 -07001445 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/linker.cmd
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001446 LIBRARIES_POST_SCRIPT ""
1447 DEPENDENCIES ${CODE_RELOCATION_DEP}
1448 )
Daniel Leungcdd02a92021-03-19 12:18:52 -07001449 set_property(TARGET ${ZEPHYR_FINAL_EXECUTABLE} PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker.cmd)
1450 add_dependencies( ${ZEPHYR_FINAL_EXECUTABLE} linker_zephyr_final_script_target)
Mark Ruvald Pedersen37d49472019-05-07 15:20:20 +02001451
1452 # Use the pass2 elf as the final elf
1453 set(logical_target_for_zephyr_elf ${ZEPHYR_FINAL_EXECUTABLE})
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001454endif()
1455
Sebastian Bøe0fc39342018-10-16 13:25:04 +02001456# Export the variable to the application's scope to allow the
1457# application to know what the name of the final elf target is.
1458set(logical_target_for_zephyr_elf ${logical_target_for_zephyr_elf} PARENT_SCOPE)
1459
Marc Herbert0370c9b2019-06-13 16:15:44 -07001460# Override the base name of the last, "logical" .elf output (and last .map) so:
Marc Herbert498b4942019-04-16 23:30:52 -07001461# 1. it doesn't depend on the number of passes above and the
1462# post_build_commands below can always find it no matter which is it;
1463# 2. it can be defined in Kconfig
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001464set_target_properties(${logical_target_for_zephyr_elf} PROPERTIES OUTPUT_NAME ${KERNEL_NAME})
1465
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001466set(post_build_commands "")
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001467set(post_build_byproducts "")
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001468
Marc Herbert0370c9b2019-06-13 16:15:44 -07001469list(APPEND
1470 post_build_commands
1471 COMMAND
Mark Ruvald Pedersend8df8012022-03-16 13:21:39 +01001472 ${CMAKE_COMMAND} -E copy ${logical_target_for_zephyr_elf}.map ${KERNEL_MAP_NAME}
Marc Herbert0370c9b2019-06-13 16:15:44 -07001473)
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +01001474list(APPEND post_build_byproducts ${KERNEL_MAP_NAME})
Marc Herbert0370c9b2019-06-13 16:15:44 -07001475
Håkon Øye Amundsenc086b932018-11-26 09:47:16 +00001476if(NOT CONFIG_BUILD_NO_GAP_FILL)
1477 # Use ';' as separator to get proper space in resulting command.
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001478 set(GAP_FILL "$<TARGET_PROPERTY:bintools,elfconvert_flag_gapfill>0xff")
Håkon Øye Amundsenc086b932018-11-26 09:47:16 +00001479endif()
1480
Danny Oerndrupc41e7122019-07-18 15:16:39 +02001481if(CONFIG_OUTPUT_PRINT_MEMORY_USAGE)
Torsten Rasmussend537be02021-02-02 21:06:11 +01001482 target_link_libraries(${logical_target_for_zephyr_elf} $<TARGET_PROPERTY:linker,memusage>)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001483
1484 get_property(memusage_build_command TARGET bintools PROPERTY memusage_command)
1485 if(memusage_build_command)
1486 # Note: The use of generator expressions allows downstream extensions to add/change the post build.
1487 # Unfortunately, the BYPRODUCTS does not allow for generator expression, so question is if we
1488 # should remove the downstream ability from start.
1489 # Or fix the output name, by the use of `get_property`
1490 list(APPEND
1491 post_build_commands
Torsten Rasmussen571f48f2020-09-04 21:07:46 +02001492 COMMAND $<TARGET_PROPERTY:bintools,memusage_command>
1493 $<TARGET_PROPERTY:bintools,memusage_flag>
1494 $<TARGET_PROPERTY:bintools,memusage_infile>${KERNEL_ELF_NAME}
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001495 )
1496
1497 # For now, the byproduct can only be supported upstream on byproducts name,
1498 # cause byproduct does not support generator expressions
1499 get_property(memusage_byproducts TARGET bintools PROPERTY memusage_byproducts)
1500 list(APPEND
1501 post_build_byproducts
1502 ${memusage_byproducts}
1503 )
1504 endif()
Danny Oerndrupc41e7122019-07-18 15:16:39 +02001505endif()
1506
Torsten Rasmussend51a67b2022-01-12 14:21:07 +01001507if(CONFIG_BUILD_OUTPUT_ADJUST_LMA)
1508 math(EXPR adjustment "${CONFIG_BUILD_OUTPUT_ADJUST_LMA}" OUTPUT_FORMAT DECIMAL)
1509 list(APPEND
1510 post_build_commands
1511 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1512 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
1513 $<TARGET_PROPERTY:bintools,elfconvert_flag_lma_adjust>${adjustment}
1514 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1515 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_ELF_NAME}
1516 )
1517endif()
1518
Stephanos Ioannidis404e7a92022-12-09 19:43:43 +09001519if(NOT CONFIG_CPP_EXCEPTIONS)
Chen Peng18c069c32022-03-07 18:36:53 +08001520 set(eh_frame_section ".eh_frame")
1521else()
1522 set(eh_frame_section "")
1523endif()
1524set(remove_sections_argument_list "")
1525foreach(section .comment COMMON ${eh_frame_section})
1526 list(APPEND remove_sections_argument_list
1527 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>${section})
1528endforeach()
1529
Kumar Galad5419132019-08-13 13:44:20 -05001530if(CONFIG_BUILD_OUTPUT_HEX OR BOARD_FLASH_RUNNER STREQUAL openocd)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001531 get_property(elfconvert_formats TARGET bintools PROPERTY elfconvert_formats)
1532 if(ihex IN_LIST elfconvert_formats)
1533 list(APPEND
1534 post_build_commands
1535 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1536 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1537 ${GAP_FILL}
1538 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>ihex
Chen Peng18c069c32022-03-07 18:36:53 +08001539 ${remove_sections_argument_list}
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001540 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1541 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_HEX_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001542 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001543 )
1544 list(APPEND
1545 post_build_byproducts
1546 ${KERNEL_HEX_NAME}
1547 # ${out_hex_byprod} # Is this needed ?
1548 )
1549 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001550endif()
Anas Nashif4592ff22017-11-23 07:54:26 -05001551
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001552if(CONFIG_BUILD_OUTPUT_BIN)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001553 get_property(elfconvert_formats TARGET bintools PROPERTY elfconvert_formats)
1554 if(binary IN_LIST elfconvert_formats)
1555 list(APPEND
1556 post_build_commands
1557 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1558 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1559 ${GAP_FILL}
1560 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>binary
Chen Peng18c069c32022-03-07 18:36:53 +08001561 ${remove_sections_argument_list}
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001562 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1563 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_BIN_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001564 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001565 )
1566 list(APPEND
1567 post_build_byproducts
1568 ${KERNEL_BIN_NAME}
1569 # ${out_hex_byprod} # Is this needed ?
1570 )
1571 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001572endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001573
Pete Johanson310a4642020-12-31 16:51:52 -05001574if(CONFIG_BUILD_OUTPUT_BIN AND CONFIG_BUILD_OUTPUT_UF2)
Peter Johanson3f332072022-01-27 00:45:27 -05001575 if(CONFIG_BUILD_OUTPUT_UF2_USE_FLASH_BASE)
1576 set(flash_addr "${CONFIG_FLASH_BASE_ADDRESS}")
1577 else()
1578 set(flash_addr "${CONFIG_FLASH_LOAD_OFFSET}")
1579 endif()
1580
1581 if(CONFIG_BUILD_OUTPUT_UF2_USE_FLASH_OFFSET)
1582 # Note, the `+ 0` in formula below avoids errors in cases where a Kconfig
1583 # variable is undefined and thus expands to nothing.
1584 math(EXPR flash_addr
1585 "${flash_addr} + ${CONFIG_FLASH_LOAD_OFFSET} + 0"
1586 OUTPUT_FORMAT HEXADECIMAL
1587 )
1588 endif()
1589
Pete Johanson310a4642020-12-31 16:51:52 -05001590 list(APPEND
1591 post_build_commands
Anas Nashifa8a97662022-07-11 10:57:15 -04001592 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/build/uf2conv.py
Pete Johanson310a4642020-12-31 16:51:52 -05001593 -c
1594 -f ${CONFIG_BUILD_OUTPUT_UF2_FAMILY_ID}
Peter Johanson3f332072022-01-27 00:45:27 -05001595 -b ${flash_addr}
Pete Johanson310a4642020-12-31 16:51:52 -05001596 -o ${KERNEL_UF2_NAME}
1597 ${KERNEL_BIN_NAME}
1598 )
1599 list(APPEND
1600 post_build_byproducts
1601 ${KERNEL_UF2_NAME}
1602 )
1603endif()
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001604
Torsten Rasmussenfffaf052021-10-12 23:08:36 +02001605if(CONFIG_BUILD_OUTPUT_META)
1606 list(APPEND
1607 post_build_commands
1608 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/zephyr_module.py
1609 ${WEST_ARG}
1610 ${ZEPHYR_MODULES_ARG}
1611 ${ZEPHYR_EXTRA_MODULES_ARG}
1612 --meta-out ${KERNEL_META_NAME}
Torsten Rasmussen1a519932021-11-04 18:35:50 +01001613 $<$<BOOL:${CONFIG_BUILD_OUTPUT_META_STATE_PROPAGATE}>:--meta-state-propagate>
Torsten Rasmussenfffaf052021-10-12 23:08:36 +02001614 )
1615 list(APPEND
1616 post_build_byproducts
1617 ${KERNEL_META_NAME}
1618 )
1619endif()
1620
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001621# Cleanup intermediate files
1622if(CONFIG_CLEANUP_INTERMEDIATE_FILES)
Torsten Rasmussend2cdee52021-11-25 12:23:34 +01001623 foreach(index RANGE ${ZEPHYR_CURRENT_LINKER_PASS})
1624 # Those files can be very large in some cases, delete them as we do not need them.
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001625 list(APPEND
1626 post_build_commands
1627 COMMAND
Torsten Rasmussend2cdee52021-11-25 12:23:34 +01001628 ${CMAKE_COMMAND} -E remove zephyr_pre${index}.elf
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001629 )
Torsten Rasmussend2cdee52021-11-25 12:23:34 +01001630 endforeach()
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001631endif()
1632
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001633if(CONFIG_BUILD_OUTPUT_S19)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001634 get_property(elfconvert_formats TARGET bintools PROPERTY elfconvert_formats)
1635 if(srec IN_LIST elfconvert_formats)
1636 # Should we print a warning if case the tools does not support converting to s19 ?
1637 list(APPEND
1638 post_build_commands
1639 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1640 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1641 ${GAP_FILL}
1642 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>srec
1643 $<TARGET_PROPERTY:bintools,elfconvert_flag_srec_len>1
1644 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1645 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_S19_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001646 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001647 )
1648 list(APPEND
1649 post_build_byproducts
1650 ${KERNEL_S19_NAME}
1651 # ${out_S19_byprod} # Is this needed ?
1652
1653 )
1654 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001655endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001656
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001657if(CONFIG_OUTPUT_DISASSEMBLY)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001658if(CONFIG_OUTPUT_DISASSEMBLE_ALL)
1659 set(disassembly_type "$<TARGET_PROPERTY:bintools,disassembly_flag_all>")
Rohit Gujarathi35713f22020-05-07 10:08:37 +05301660 else()
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001661 set(disassembly_type "$<TARGET_PROPERTY:bintools,disassembly_flag_inline_source>")
Rohit Gujarathi35713f22020-05-07 10:08:37 +05301662 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001663 list(APPEND
1664 post_build_commands
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001665 COMMAND $<TARGET_PROPERTY:bintools,disassembly_command>
1666 $<TARGET_PROPERTY:bintools,disassembly_flag>
1667 ${disassembly_type}
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001668 $<TARGET_PROPERTY:bintools,disassembly_flag_infile>${KERNEL_ELF_NAME}
1669 $<TARGET_PROPERTY:bintools,disassembly_flag_outfile>${KERNEL_LST_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001670 $<TARGET_PROPERTY:bintools,disassembly_flag_final>
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001671 )
1672 list(APPEND
1673 post_build_byproducts
1674 ${KERNEL_LST_NAME}
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001675# ${out_disassembly_byprod} # Needed ??
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001676 )
1677endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001678
Anas Nashif47a673f2022-06-27 10:08:37 -04001679if(CONFIG_OUTPUT_SYMBOLS)
1680 list(APPEND
1681 post_build_commands
1682 COMMAND $<TARGET_PROPERTY:bintools,symbols_command>
1683 $<TARGET_PROPERTY:bintools,symbols_flag>
1684 $<TARGET_PROPERTY:bintools,symbols_infile>${KERNEL_ELF_NAME}
1685 $<TARGET_PROPERTY:bintools,symbols_outfile>${KERNEL_SYMBOLS_NAME}
1686 $<TARGET_PROPERTY:bintools,symbols_final>
1687 )
1688 list(APPEND
1689 post_build_byproducts
1690 ${KERNEL_SYMBOLS_NAME}
1691 )
1692endif()
1693
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001694if(CONFIG_OUTPUT_STAT)
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001695# zephyr_post_build(TOOLS bintools COMMAND readelf FLAGS headers INFILE file OUTFILE outfile)
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001696 list(APPEND
1697 post_build_commands
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001698 COMMAND $<TARGET_PROPERTY:bintools,readelf_command>
1699 $<TARGET_PROPERTY:bintools,readelf_flag>
1700 $<TARGET_PROPERTY:bintools,readelf_flag_headers>
Torsten Rasmussen2d1a3d92021-05-20 17:38:57 +02001701 $<TARGET_PROPERTY:bintools,readelf_flag_infile>${KERNEL_ELF_NAME}
1702 $<TARGET_PROPERTY:bintools,readelf_flag_outfile>${KERNEL_STAT_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001703 $<TARGET_PROPERTY:bintools,readelf_flag_final>
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001704 )
1705 list(APPEND
1706 post_build_byproducts
1707 ${KERNEL_STAT_NAME}
1708 )
1709endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001710
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001711if(CONFIG_BUILD_OUTPUT_STRIPPED)
1712 list(APPEND
1713 post_build_commands
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001714 COMMAND $<TARGET_PROPERTY:bintools,strip_command>
1715 $<TARGET_PROPERTY:bintools,strip_flag>
1716 $<TARGET_PROPERTY:bintools,strip_flag_all>
1717 $<TARGET_PROPERTY:bintools,strip_flag_infile>${KERNEL_ELF_NAME}
1718 $<TARGET_PROPERTY:bintools,strip_flag_outfile>${KERNEL_STRIP_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001719 $<TARGET_PROPERTY:bintools,strip_flag_final>
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001720 )
1721 list(APPEND
1722 post_build_byproducts
1723 ${KERNEL_STRIP_NAME}
1724 )
1725endif()
1726
1727if(CONFIG_BUILD_OUTPUT_EXE)
1728 list(APPEND
1729 post_build_commands
1730 COMMAND
1731 ${CMAKE_COMMAND} -E copy ${KERNEL_ELF_NAME} ${KERNEL_EXE_NAME}
1732 )
1733 list(APPEND
1734 post_build_byproducts
1735 ${KERNEL_EXE_NAME}
1736 )
1737endif()
Anas Nashif4592ff22017-11-23 07:54:26 -05001738
Torsten Rasmussenc8ddc342022-01-10 11:02:26 +01001739if(CONFIG_BUILD_OUTPUT_INFO_HEADER)
1740 list(APPEND
1741 post_build_commands
Anas Nashif09b4bec2022-07-11 10:55:13 -04001742 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/build/gen_image_info.py
Torsten Rasmussenc8ddc342022-01-10 11:02:26 +01001743 --elf-file=${KERNEL_ELF_NAME}
1744 --header-file=${PROJECT_BINARY_DIR}/include/public/zephyr_image_info.h
Torsten Rasmussend51a67b2022-01-12 14:21:07 +01001745 $<$<BOOL:${adjustment}>:--adjusted-lma=${adjustment}>
Torsten Rasmussenc8ddc342022-01-10 11:02:26 +01001746 )
1747 list(APPEND
1748 post_build_byproducts
1749 ${PROJECT_BINARY_DIR}/include/public/zephyr_image_info.h
1750 )
1751endif()
1752
Martí Bolívarf66a0c32020-08-18 11:28:04 -07001753# Generate and use MCUboot related artifacts as needed.
1754if(CONFIG_BOOTLOADER_MCUBOOT)
Jamie McCrae176c8052023-03-23 13:42:33 +00001755 get_target_property(signing_script zephyr_property_target SIGNING_SCRIPT)
1756 if(NOT signing_script)
1757 set_target_properties(zephyr_property_target PROPERTIES SIGNING_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/cmake/mcuboot.cmake)
1758 endif()
1759endif()
1760
1761# Include signing script, if set
1762get_target_property(signing_script zephyr_property_target SIGNING_SCRIPT)
1763if(signing_script)
1764 message(STATUS "Including signing script: ${signing_script}")
1765
1766 include(${signing_script})
Martí Bolívarf66a0c32020-08-18 11:28:04 -07001767endif()
1768
Madhurima Paruchurifa738b02022-10-26 13:34:09 +00001769# Generate USB-C VIF policies in XML format
1770if (CONFIG_BUILD_OUTPUT_VIF)
1771 include(${CMAKE_CURRENT_LIST_DIR}/cmake/vif.cmake)
1772endif()
1773
Rajavardhan Gundiecdea1c2019-01-25 11:53:13 +05301774get_property(extra_post_build_commands
1775 GLOBAL PROPERTY
1776 extra_post_build_commands
1777 )
1778
1779list(APPEND
1780 post_build_commands
1781 ${extra_post_build_commands}
1782 )
1783
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001784get_property(extra_post_build_byproducts
1785 GLOBAL PROPERTY
1786 extra_post_build_byproducts
1787 )
1788
1789list(APPEND
1790 post_build_byproducts
1791 ${extra_post_build_byproducts}
1792 )
1793
Krzysztof Chruscinski33923012022-03-29 15:47:01 +02001794if(CONFIG_LOG_DICTIONARY_DB)
Daniel Leung1c9e89c2022-01-26 14:44:55 -08001795 set(log_dict_db_output --json=${PROJECT_BINARY_DIR}/log_dictionary.json)
Daniel Leungba488d12022-02-04 13:18:13 -08001796elseif(CONFIG_LOG_MIPI_SYST_USE_CATALOG)
1797 set(log_dict_db_output --syst=${PROJECT_BINARY_DIR}/mipi_syst_collateral.xml)
1798endif()
Daniel Leunga5ab1a72021-04-02 12:54:53 -07001799
Daniel Leungba488d12022-02-04 13:18:13 -08001800if(log_dict_db_output)
Daniel Leunga5ab1a72021-04-02 12:54:53 -07001801 list(APPEND
1802 post_build_commands
1803 COMMAND
1804 ${PYTHON_EXECUTABLE}
1805 ${ZEPHYR_BASE}/scripts/logging/dictionary/database_gen.py
1806 ${KERNEL_ELF_NAME}
Daniel Leung1c9e89c2022-01-26 14:44:55 -08001807 ${log_dict_db_output}
Torsten Rasmussen91709772022-02-04 10:27:13 +01001808 --build-header ${PROJECT_BINARY_DIR}/include/generated/version.h
Daniel Leunga5ab1a72021-04-02 12:54:53 -07001809 )
1810 list(APPEND
1811 post_build_byproducts
1812 ${LOG_DICT_DB_NAME}
1813 )
Daniel Leung1c9e89c2022-01-26 14:44:55 -08001814
1815 unset(log_dict_db_output)
Daniel Leunga5ab1a72021-04-02 12:54:53 -07001816endif()
1817
Marc Herbert498b4942019-04-16 23:30:52 -07001818# Add post_build_commands to post-process the final .elf file produced by
Jordan Yates28b2e552021-10-20 20:19:28 +10001819# either the ZEPHYR_LINK_STAGE_EXECUTABLE or the KERNEL_ELF executable
Marc Herbert498b4942019-04-16 23:30:52 -07001820# targets above.
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001821add_custom_command(
1822 TARGET ${logical_target_for_zephyr_elf}
1823 POST_BUILD
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001824 ${post_build_commands}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001825 BYPRODUCTS
1826 ${post_build_byproducts}
Marc Herbert498b4942019-04-16 23:30:52 -07001827 COMMENT "Generating files from ${KERNEL_ELF_NAME} for board: ${BOARD}"
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001828 COMMAND_EXPAND_LISTS
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001829 # NB: COMMENT only works for some CMake-Generators
Rajavardhan Gundiecdea1c2019-01-25 11:53:13 +05301830 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001831
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001832# To populate with hex files to merge, do the following:
1833# set_property(GLOBAL APPEND PROPERTY HEX_FILES_TO_MERGE ${my_local_list})
1834# Note that the zephyr.hex file will not be included automatically.
1835get_property(HEX_FILES_TO_MERGE GLOBAL PROPERTY HEX_FILES_TO_MERGE)
1836if(HEX_FILES_TO_MERGE)
1837 # Merge in out-of-tree hex files.
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001838 set(MERGED_HEX_NAME merged.hex)
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001839
1840 add_custom_command(
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001841 OUTPUT ${MERGED_HEX_NAME}
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001842 COMMAND
1843 ${PYTHON_EXECUTABLE}
Anas Nashif72e7fa82022-07-11 10:55:37 -04001844 ${ZEPHYR_BASE}/scripts/build/mergehex.py
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001845 -o ${MERGED_HEX_NAME}
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001846 ${HEX_FILES_TO_MERGE}
1847 DEPENDS ${HEX_FILES_TO_MERGE} ${logical_target_for_zephyr_elf}
1848 )
1849
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001850 add_custom_target(mergehex ALL DEPENDS ${MERGED_HEX_NAME})
Torsten Rasmussend38da9d2020-06-30 09:55:54 +02001851 list(APPEND RUNNERS_DEPS mergehex)
Håkon Øye Amundsenc9a2a5e2020-01-03 08:25:03 +00001852
1853 message(VERBOSE "Merging hex files: ${HEX_FILES_TO_MERGE}")
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001854endif()
1855
Filip Kokosinski94428042021-08-30 13:09:48 +02001856if(SUPPORTED_EMU_PLATFORMS)
1857 list(GET SUPPORTED_EMU_PLATFORMS 0 default_emu)
Anas Nashifa1d18102022-02-14 22:08:42 -05001858 if(EXISTS ${ZEPHYR_BASE}/cmake/emu/${default_emu}.cmake)
1859 add_custom_target(run DEPENDS run_${default_emu})
1860 endif()
Filip Kokosinski94428042021-08-30 13:09:48 +02001861
1862 foreach(EMU_PLATFORM ${SUPPORTED_EMU_PLATFORMS})
Anas Nashifa1d18102022-02-14 22:08:42 -05001863 if(EXISTS ${ZEPHYR_BASE}/cmake/emu/${EMU_PLATFORM}.cmake)
1864 include(${ZEPHYR_BASE}/cmake/emu/${EMU_PLATFORM}.cmake)
1865 endif()
Filip Kokosinski94428042021-08-30 13:09:48 +02001866 endforeach()
1867
1868 if(TARGET debugserver_${default_emu})
1869 add_custom_target(debugserver DEPENDS debugserver_${default_emu})
1870 endif()
Anas Nashiffd276ae2017-12-21 16:45:45 -05001871else()
1872 add_custom_target(run
1873 COMMAND
1874 ${CMAKE_COMMAND} -E echo
1875 "==================================================="
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +01001876 "Emulation/Simulation not supported with this board."
Anas Nashiffd276ae2017-12-21 16:45:45 -05001877 "==================================================="
1878 )
Anas Nashifc15d3c92017-11-21 18:54:55 -05001879endif()
1880
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001881add_subdirectory(cmake/flash)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001882add_subdirectory(cmake/usage)
1883add_subdirectory(cmake/reports)
1884
Marc Herbert83723102019-06-17 13:26:11 -07001885if(NOT CONFIG_TEST)
Andrew Boie411686f2018-05-24 13:18:36 -07001886if(CONFIG_ASSERT AND (NOT CONFIG_FORCE_NO_ASSERT))
Sebastian Bøefa8f9d42019-12-06 12:54:53 +01001887 message(WARNING "__ASSERT() statements are globally ENABLED")
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001888endif()
Marc Herbert83723102019-06-17 13:26:11 -07001889endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001890
Vincent Wana2bc5142020-01-09 14:20:44 -08001891if(CONFIG_BOARD_DEPRECATED_RELEASE)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001892 message(WARNING "
1893 WARNING: The board '${BOARD}' is deprecated and will be
Vincent Wana2bc5142020-01-09 14:20:44 -08001894 removed in version ${CONFIG_BOARD_DEPRECATED_RELEASE}"
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +01001895 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001896endif()
Paul Sokolovsky1c6a7d72019-06-06 21:12:14 +03001897
Vincent Wan180b4df2020-01-08 17:10:51 -08001898if(CONFIG_SOC_DEPRECATED_RELEASE)
1899 message(WARNING "
1900 WARNING: The SoC '${SOC_NAME}' is deprecated and will be
1901 removed in version ${CONFIG_SOC_DEPRECATED_RELEASE}"
1902 )
1903endif()
1904
Sebastian Bøee50e12d2019-08-29 16:19:32 +02001905# In CMake projects, 'CMAKE_BUILD_TYPE' usually determines the
1906# optimization flag, but in Zephyr it is determined through
1907# Kconfig. Here we give a warning when there is a mismatch between the
1908# two in case the user is not aware of this.
1909set(build_types None Debug Release RelWithDebInfo MinSizeRel)
1910
1911if((CMAKE_BUILD_TYPE IN_LIST build_types) AND (NOT NO_BUILD_TYPE_WARNING))
1912 string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_uppercase)
Torsten Rasmussen05bb8552021-12-10 15:06:30 +01001913 # The CMAKE_C_FLAGS_<build_type> is a string, so we do a regex to see if the
1914 # optimization flag is present in that string.
1915 # To avoid false-positive matches, the flag must either be matched first
1916 # or last in string, or come after / followed by minimum a space.
1917 if(NOT (CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_uppercase} MATCHES "(^| )${OPTIMIZATION_FLAG}($| )"))
Sebastian Bøee50e12d2019-08-29 16:19:32 +02001918 message(WARNING "
1919 The CMake build type was set to '${CMAKE_BUILD_TYPE}', but the optimization flag was set to '${OPTIMIZATION_FLAG}'.
1920 This may be intentional and the warning can be turned off by setting the CMake variable 'NO_BUILD_TYPE_WARNING'"
1921 )
1922 endif()
1923endif()
1924
Stephanos Ioannidisfd4700d2021-09-11 22:06:28 +09001925# @Intent: Set compiler specific flags for standard C/C++ includes
Paul Sokolovsky1c6a7d72019-06-06 21:12:14 +03001926# Done at the very end, so any other system includes which may
1927# be added by Zephyr components were first in list.
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001928# Note, the compile flags are moved, but the system include is still present here.
1929zephyr_compile_options($<TARGET_PROPERTY:compiler,nostdinc>)
1930target_include_directories(zephyr_interface SYSTEM INTERFACE $<TARGET_PROPERTY:compiler,nostdinc_include>)
Torsten Rasmussena5917f02020-09-09 15:42:32 +02001931
Stephanos Ioannidiscf211aa2022-12-10 03:19:58 +09001932if(CONFIG_MINIMAL_LIBCPP)
Stephanos Ioannidisfd4700d2021-09-11 22:06:28 +09001933 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,nostdincxx>>)
1934endif()
1935
Torsten Rasmussena5917f02020-09-09 15:42:32 +02001936# Finally export all build flags from Zephyr
1937add_subdirectory_ifdef(
1938 CONFIG_MAKEFILE_EXPORTS
1939 cmake/makefile_exports
1940 )