blob: f95c97ba3dec7dde7e74f44705a7d8c3ed953202 [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
121 ${SOC_DIR}/${ARCH}/${SOC_FAMILY}/include
Andy Ross544a38e2020-06-25 17:42:51 -0700122 ${SOC_DIR}/${ARCH}/${SOC_FAMILY}/common/include
Sebastian Bøe40363392019-01-25 10:14:13 +0100123 )
124 if(EXISTS ${optional_include_dir})
125 zephyr_include_directories(${optional_include_dir})
126 endif()
127endforeach()
128
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200129zephyr_compile_definitions(
130 KERNEL
131 __ZEPHYR__=1
Anas Nashif34aebad2018-01-03 12:26:19 -0500132)
133
Danny Oerndrup94202f32021-07-16 11:50:40 +0200134# Ensure that include/toolchain.h includes toolchain/other.h for all off-tree toolchains
135if(TOOLCHAIN_USE_CUSTOM)
136 zephyr_compile_definitions(__TOOLCHAIN_CUSTOM__)
137endif()
138
Mark Ruvald Pedersen01592072019-01-10 12:07:51 +0100139# @Intent: Set compiler flags to enable buffer overflow checks in libc functions
140# @config in CONFIG_NO_OPTIMIZATIONS optional : Optimizations may affect security
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200141zephyr_compile_definitions($<TARGET_PROPERTY:compiler,security_fortify> )
Mark Ruvald Pedersen01592072019-01-10 12:07:51 +0100142
143# @Intent: Set compiler flags to detect general stack overflows across all functions
144if(CONFIG_STACK_CANARIES)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200145 zephyr_compile_options($<TARGET_PROPERTY:compiler,security_canaries>)
Anas Nashif34aebad2018-01-03 12:26:19 -0500146endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200147
Anas Nashifdaf77162018-04-09 21:53:26 -0500148if(BUILD_VERSION)
149 zephyr_compile_definitions(
150 BUILD_VERSION=${BUILD_VERSION}
151 )
152endif()
153
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100154# @Intent: Obtain compiler optimizations flags and store in variables
155# @details:
156# Kconfig.zephyr "Optimization level" is a kconfig choice, ensuring
157# only *one* of CONFIG_{NO,DEBUG,SPEED,SIZE}_OPTIMIZATIONS is set.
158# Refer to Kconfig.zephyr for selection logic and description of these choices.
159# toolchain_cc_optimize_*() macros must provide the mapping from these kconfigs
160# to compiler flags. Each macro will store the flags in a CMake variable, whose
161# name is passed as argument (somewhat like by reference).
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200162#
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100163# If the user wants to tweak the optimizations, there are two ways:
164# 1) Using EXTRA_CFLAGS which is applied regardless of kconfig choice, or
165# 2) Rely on override support being implemented by your toolchain_cc_optimize_*()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200166#
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200167get_property(OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG TARGET compiler PROPERTY no_optimization)
168get_property(OPTIMIZE_FOR_DEBUG_FLAG TARGET compiler PROPERTY optimization_debug)
169get_property(OPTIMIZE_FOR_SPEED_FLAG TARGET compiler PROPERTY optimization_speed)
170get_property(OPTIMIZE_FOR_SIZE_FLAG TARGET compiler PROPERTY optimization_size)
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100171
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100172# From kconfig choice, pick the actual OPTIMIZATION_FLAG to use.
173# Kconfig choice ensures only one of these CONFIG_*_OPTIMIZATIONS is set.
Alberto Escolar Piedrasf60527a2018-01-22 15:35:54 +0100174if(CONFIG_NO_OPTIMIZATIONS)
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100175 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG})
176elseif(CONFIG_DEBUG_OPTIMIZATIONS)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200177 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_DEBUG_FLAG})
Aurelien Jarnoe8413d12018-06-16 23:40:04 +0200178elseif(CONFIG_SPEED_OPTIMIZATIONS)
179 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SPEED_FLAG})
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100180elseif(CONFIG_SIZE_OPTIMIZATIONS)
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100181 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SIZE_FLAG}) # Default in kconfig
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100182else()
Anas Nashif885aaf22019-01-18 19:15:19 -0500183 assert(0 "Unreachable code. Expected optimization level to have been chosen. See Kconfig.zephyr")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200184endif()
185
Ulf Magnussonde42aea2020-02-07 00:48:22 +0100186if(NOT CONFIG_ARCH_IS_SET)
187 message(WARNING "\
188None of the CONFIG_<arch> (e.g. CONFIG_X86) symbols are set. \
189Select one of them from the SOC_SERIES_* symbol or, lacking that, from the \
190SOC_* symbol.")
191endif()
192
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100193# Apply the final optimization flag(s)
194zephyr_compile_options(${OPTIMIZATION_FLAG})
195
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100196# @Intent: Obtain compiler specific flags related to C++ that are not influenced by kconfig
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200197zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,required>>)
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200198
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100199# @Intent: Obtain compiler specific flags for compiling under different ISO standards of C++
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200200if(CONFIG_CPLUSPLUS)
201 # From kconfig choice, pick a single dialect.
202 # Kconfig choice ensures only one of these CONFIG_STD_CPP* is set.
203 if(CONFIG_STD_CPP98)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200204 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp98>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200205 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp98})
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200206 elseif(CONFIG_STD_CPP11)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200207 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp11>) # Default in kconfig
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200208 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp11})
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200209 elseif(CONFIG_STD_CPP14)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200210 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp14>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200211 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp14})
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200212 elseif(CONFIG_STD_CPP17)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200213 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp17>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200214 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp17})
Alexander Wachterad130f22021-07-14 10:50:21 +0200215 elseif(CONFIG_STD_CPP2A)
216 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp2a>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200217 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp20})
Dan Kalowskyc0811e92021-07-09 10:46:46 -0700218 elseif(CONFIG_STD_CPP20)
219 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp20>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200220 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp20})
Dan Kalowsky9b333912021-07-09 10:54:11 -0700221 elseif(CONFIG_STD_CPP2B)
222 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp2b>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200223 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp20})
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200224 else()
225 assert(0 "Unreachable code. Expected C++ standard to have been chosen. See Kconfig.zephyr.")
226 endif()
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200227 set(CMAKE_CXX_COMPILE_FEATURES ${CMAKE_CXX_COMPILE_FEATURES} PARENT_SCOPE)
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200228
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200229 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:${STD_CPP_DIALECT_FLAGS}>)
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200230endif()
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100231
232if(NOT CONFIG_EXCEPTIONS)
233 # @Intent: Obtain compiler specific flags related to C++ Exceptions
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200234 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,no_exceptions>>)
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100235endif()
236
237if(NOT CONFIG_RTTI)
238 # @Intent: Obtain compiler specific flags related to C++ Run Time Type Information
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200239 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,no_rtti>>)
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100240endif()
241
Andy Rossfe04adf2019-02-27 11:53:18 -0800242if(CONFIG_MISRA_SANE)
Danny Oerndrup8e5a95e2019-05-16 12:53:58 +0200243 # @Intent: Obtain toolchain compiler flags relating to MISRA.
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200244 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_misra_sane>>)
245 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_error_misra_sane>>)
Andy Rossfe04adf2019-02-27 11:53:18 -0800246endif()
247
Flavio Ceolinb587e8d2020-08-26 09:48:33 -0700248# This is intend to be temporary. Once we have fixed the violations that
249# prevents build Zephyr, these flags shall be part of the default flags.
250if(CONFIG_CODING_GUIDELINE_CHECK)
251 # @Intent: Obtain toolchain compiler flags relating to coding guideline
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200252 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_coding_guideline>>)
Flavio Ceolinb587e8d2020-08-26 09:48:33 -0700253endif()
254
Danny Oerndrup4ddbc002019-06-11 13:55:53 +0200255# @Intent: Set compiler specific macro inclusion of AUTOCONF_H
Torsten Rasmussenf109e682020-08-13 15:49:46 +0200256zephyr_compile_options("SHELL: $<TARGET_PROPERTY:compiler,imacros> ${AUTOCONF_H}")
Danny Oerndrup4ddbc002019-06-11 13:55:53 +0200257
Danny Oerndrupfaa72b72019-06-11 15:56:57 +0200258# @Intent: Set compiler specific flag for bare metal freestanding option
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200259zephyr_compile_options($<TARGET_PROPERTY:compiler,freestanding>)
Danny Oerndrupfaa72b72019-06-11 15:56:57 +0200260
Danny Oerndrupe34ed7c2019-06-12 14:56:46 +0200261# @Intent: Set compiler specific flag for tentative definitions, no-common
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200262zephyr_compile_options($<TARGET_PROPERTY:compiler,no_common>)
Danny Oerndrupe34ed7c2019-06-12 14:56:46 +0200263
Danny Oerndrupe0569ac2019-07-23 09:00:55 +0200264# @Intent: Set compiler specific flag for production of debug information
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200265zephyr_compile_options($<TARGET_PROPERTY:compiler,debug>)
Danny Oerndrupe0569ac2019-07-23 09:00:55 +0200266
Arvin Farahmande430b7b2021-04-15 11:20:10 -0400267if(CONFIG_COMPILER_COLOR_DIAGNOSTICS)
Arvin Farahmandb8f59682021-04-15 11:20:10 -0400268# @Intent: Set compiler specific flag for diagnostic messages
269zephyr_compile_options($<TARGET_PROPERTY:compiler,diagnostic>)
270endif()
271
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200272zephyr_compile_options(
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530273 ${TOOLCHAIN_C_FLAGS}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200274)
275
Mark Ruvald Pedersencb0fd452019-01-30 21:48:25 +0100276# @Intent: Obtain compiler specific flags related to assembly
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200277# ToDo: Remember to get feedback from Oticon on this, as they might use the `ASM_BASE_FLAG` since this is done this way.
278zephyr_compile_options($<$<COMPILE_LANGUAGE:ASM>:$<TARGET_PROPERTY:asm,required>>)
Mark Ruvald Pedersencb0fd452019-01-30 21:48:25 +0100279
Nicolas Pitreb86aa652019-07-02 16:22:04 -0400280# @Intent: Enforce standard integer type correspondance to match Zephyr usage.
281# (must be after compiler specific flags)
Stephanos Ioannidisa1a66192021-09-12 19:33:15 +0900282if(NOT CONFIG_ARCH_POSIX)
283 # `zephyr_stdint.h` is not included for the POSIX (native) arch because it
284 # compiles with the host toolchain/headers and there can be conflicts if we
285 # arbitrarily redefine our own type system (see #37718).
286 zephyr_compile_options("SHELL: $<TARGET_PROPERTY:compiler,imacros> ${ZEPHYR_BASE}/include/toolchain/zephyr_stdint.h")
287endif()
Nicolas Pitreb86aa652019-07-02 16:22:04 -0400288
Mark Ruvald Pedersencb0fd452019-01-30 21:48:25 +0100289# Common toolchain-agnostic assembly flags
290zephyr_compile_options(
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200291 $<$<COMPILE_LANGUAGE:ASM>:-D_ASMLANGUAGE>
292)
293
Mark Ruvald Pedersen1f013252019-04-25 15:46:11 +0200294# @Intent: Set fundamental linker specific flags
295toolchain_ld_base()
Aurelien Jarnoc6727d42018-11-26 13:48:34 +0100296
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +0200297toolchain_ld_force_undefined_symbols(
298 _OffsetAbsSyms
299 _ConfigAbsSyms
300)
301
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200302if(NOT CONFIG_NATIVE_APPLICATION)
Mark Ruvald Pedersen65f02c02019-04-25 16:31:30 +0200303 # @Intent: Set linker specific flags for bare metal target
304 toolchain_ld_baremetal()
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200305endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200306
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200307if(CONFIG_LIB_CPLUSPLUS)
Mark Ruvald Pedersen3db09aa2019-04-26 08:43:04 +0200308 # @Intent: Set linker specific flags for C++
309 toolchain_ld_cpp()
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200310endif()
311
Danny Oerndrup8eaa9062019-05-16 12:49:31 +0200312# @Intent: Add the basic toolchain warning flags
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200313zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_base>>)
314zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_base>>)
Danny Oerndrupbdb229f2019-05-06 15:19:27 +0200315
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200316# ==========================================================================
317#
318# cmake -DW=... settings
319#
320# W=1 - warnings that may be relevant and does not occur too often
321# W=2 - warnings that occur quite often but may still be relevant
322# W=3 - the more obscure warnings, can most likely be ignored
323# ==========================================================================
Danny Oerndrup8eaa9062019-05-16 12:49:31 +0200324# @Intent: Add cmake -DW toolchain supported warnings, if any
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200325if(W MATCHES "1")
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200326 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_dw_1>>)
327 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_dw_1>>)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200328endif()
329
330if(W MATCHES "2")
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200331 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_dw_2>>)
332 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_dw_2>>)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200333endif()
334
335if(W MATCHES "3")
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200336 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_dw_3>>)
337 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_dw_3>>)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200338endif()
339
Danny Oerndrup8eaa9062019-05-16 12:49:31 +0200340# @Intent: Add extended, more specific, toolchain warning flags
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200341zephyr_compile_options($<TARGET_PROPERTY:compiler,warning_extended>)
Benoit Leforestier04dad592019-01-25 13:57:03 +0100342
Danny Oerndrup025ffa22019-05-16 12:58:40 +0200343# @Intent: Trigger an error when a declaration does not specify a type
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200344zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_implicit_int>>)
345zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_error_implicit_int>>)
Danny Oerndrup025ffa22019-05-16 12:58:40 +0200346
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200347# Allow the user to inject options when calling cmake, e.g.
348# 'cmake -DEXTRA_CFLAGS="-Werror -Wno-deprecated-declarations" ..'
Sebastian Bøe9f590452017-11-10 12:22:23 +0100349include(cmake/extra_flags.cmake)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200350
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200351zephyr_cc_option(-fno-asynchronous-unwind-tables)
352zephyr_cc_option(-fno-pie)
353zephyr_cc_option(-fno-pic)
354zephyr_cc_option(-fno-strict-overflow)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200355
Daniel Leung02b20352020-09-28 11:27:11 -0700356if(CONFIG_THREAD_LOCAL_STORAGE)
357# Only support local exec TLS model at this point.
358zephyr_cc_option(-ftls-model=local-exec)
359endif()
360
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200361if(CONFIG_OVERRIDE_FRAME_POINTER_DEFAULT)
362 if(CONFIG_OMIT_FRAME_POINTER)
363 zephyr_cc_option(-fomit-frame-pointer)
364 else()
365 zephyr_cc_option(-fno-omit-frame-pointer)
366 endif()
367endif()
368
Sebastian Bøe244451b2019-02-27 08:28:25 +0100369separate_arguments(COMPILER_OPT_AS_LIST UNIX_COMMAND ${CONFIG_COMPILER_OPT})
370zephyr_compile_options(${COMPILER_OPT_AS_LIST})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200371
372# TODO: Include arch compiler options at this point.
373
Danny Oerndrupcbbbdea2019-05-06 15:21:58 +0200374if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
375 # GCC assumed
376 zephyr_cc_option(-fno-reorder-functions)
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530377
Anas Nashif7ee8bb92018-02-11 14:36:21 -0600378 if(NOT ${ZEPHYR_TOOLCHAIN_VARIANT} STREQUAL "xcc")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200379 zephyr_cc_option(-fno-defer-pop)
380 endif()
381endif()
382
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200383zephyr_cc_option_ifdef(CONFIG_STACK_USAGE -fstack-usage)
384
Marc Herbert28a56572019-04-11 16:34:04 -0700385# If the compiler supports it, strip the ${ZEPHYR_BASE} prefix from the
386# __FILE__ macro used in __ASSERT*, in the
387# .noinit."/home/joe/zephyr/fu/bar.c" section names and in any
388# application code. This saves some memory, stops leaking user locations
389# in binaries, makes failure logs more deterministic and most
390# importantly makes builds more deterministic
Marc Herbertf67dcdb2019-05-31 15:28:38 -0700391
Marc Herberteddbf3c2019-06-11 16:57:37 -0700392# If several match then the last one wins. This matters for instances
393# like tests/ and samples/: they're inside all of them! Then let's
394# strip as little as possible.
Marc Herbertf67dcdb2019-05-31 15:28:38 -0700395zephyr_cc_option(-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=CMAKE_SOURCE_DIR)
396zephyr_cc_option(-fmacro-prefix-map=${ZEPHYR_BASE}=ZEPHYR_BASE)
Marc Herberteddbf3c2019-06-11 16:57:37 -0700397if(WEST_TOPDIR)
398 zephyr_cc_option(-fmacro-prefix-map=${WEST_TOPDIR}=WEST_TOPDIR)
399endif()
Marc Herbert28a56572019-04-11 16:34:04 -0700400
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200401# TODO: Archiver arguments
402# ar_option(D)
403
Håkon Øye Amundsend6551b52018-11-29 09:08:08 +0000404# Declare MPU userspace dependencies before the linker scripts to make
405# sure the order of dependencies are met
Andrew Boie41f60112019-01-31 15:53:24 -0800406if(CONFIG_USERSPACE)
Daniel Leung2117a2a2021-07-12 13:33:32 -0700407 add_custom_target(app_smem)
Daniel Leung212ec9a2019-03-10 14:20:21 -0700408 set(APP_SMEM_ALIGNED_DEP app_smem_aligned_linker)
409 set(APP_SMEM_UNALIGNED_DEP app_smem_unaligned_linker)
Håkon Øye Amundsend6551b52018-11-29 09:08:08 +0000410endif()
411
Daniel Leung11171692021-03-18 14:00:07 -0700412if(CONFIG_USERSPACE)
413 set(KOBJECT_LINKER_DEP kobject_linker)
414endif()
415
Håkon Øye Amundsena4494392018-11-29 09:14:27 +0000416get_property(TOPT GLOBAL PROPERTY TOPT)
Oleg Zhurakivskyy22119352019-03-08 11:29:33 +0200417set_ifndef( TOPT -Wl,-T) # clang doesn't pick -T for some reason and complains,
418 # while -Wl,-T works for both, gcc and clang
Håkon Øye Amundsena4494392018-11-29 09:14:27 +0000419
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200420if(CONFIG_HAVE_CUSTOM_LINKER_SCRIPT)
421 set(LINKER_SCRIPT ${APPLICATION_SOURCE_DIR}/${CONFIG_CUSTOM_LINKER_SCRIPT})
Sebastian Bøec1aa9d12018-04-12 14:48:05 +0200422 if(NOT EXISTS ${LINKER_SCRIPT})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200423 set(LINKER_SCRIPT ${CONFIG_CUSTOM_LINKER_SCRIPT})
Sebastian Bøec1aa9d12018-04-12 14:48:05 +0200424 assert_exists(CONFIG_CUSTOM_LINKER_SCRIPT)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200425 endif()
426else()
427 # Try a board specific linker file
428 set(LINKER_SCRIPT ${BOARD_DIR}/linker.ld)
429 if(NOT EXISTS ${LINKER_SCRIPT})
430 # If not available, try an SoC specific linker file
Anas Nashif96455d52018-09-04 14:34:06 -0500431 set(LINKER_SCRIPT ${SOC_DIR}/${ARCH}/${SOC_PATH}/linker.ld)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200432 endif()
433endif()
434
435if(NOT EXISTS ${LINKER_SCRIPT})
436 message(FATAL_ERROR "Could not find linker script: '${LINKER_SCRIPT}'. Corrupted configuration?")
437endif()
438
439configure_file(version.h.in ${PROJECT_BINARY_DIR}/include/generated/version.h)
440
Sebastian Bøec23cc262018-10-09 16:03:29 +0200441# Error-out when the deprecated naming convention is found (until
442# after 1.14.0 has been released)
443foreach(path
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100444 ${BOARD_DIR}/dts.fixup
445 ${PROJECT_SOURCE_DIR}/soc/${ARCH}/${SOC_PATH}/dts.fixup
Sebastian Bøec23cc262018-10-09 16:03:29 +0200446 ${APPLICATION_SOURCE_DIR}/dts.fixup
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100447 )
Sebastian Bøec23cc262018-10-09 16:03:29 +0200448 if(EXISTS ${path})
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100449 message(FATAL_ERROR
450 "A deprecated filename has been detected. Porting is required."
451 "The file '${path}' exists, but it should be named dts_fixup.h instead."
452 "See https://github.com/zephyrproject-rtos/zephyr/pull/10352 for more details"
453 )
Sebastian Bøec23cc262018-10-09 16:03:29 +0200454 endif()
455endforeach()
456
457set_ifndef( DTS_BOARD_FIXUP_FILE ${BOARD_DIR}/dts_fixup.h)
458set_ifndef( DTS_SOC_FIXUP_FILE ${SOC_DIR}/${ARCH}/${SOC_PATH}/dts_fixup.h)
459set( DTS_APP_FIXUP_FILE ${APPLICATION_SOURCE_DIR}/dts_fixup.h)
Sebastian Bøec23cc262018-10-09 16:03:29 +0200460
Ulf Magnusson4e850062020-01-16 13:29:53 +0100461set_ifndef(DTS_CAT_OF_FIXUP_FILES ${ZEPHYR_BINARY_DIR}/include/generated/devicetree_fixups.h)
Sebastian Bøe361fdaa2019-01-28 13:40:50 +0100462
463# Concatenate the fixups into a single header file for easy
Sebastian Bøec23cc262018-10-09 16:03:29 +0200464# #include'ing
Ulf Magnusson4e850062020-01-16 13:29:53 +0100465file(WRITE ${DTS_CAT_OF_FIXUP_FILES} "/* May only be included by devicetree.h */\n\n")
Martí Bolívar21c7d422020-05-08 16:06:48 -0700466set(DISCOVERED_FIXUP_FILES)
Sebastian Bøe361fdaa2019-01-28 13:40:50 +0100467foreach(fixup_file
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100468 ${DTS_BOARD_FIXUP_FILE}
469 ${DTS_SOC_FIXUP_FILE}
470 ${DTS_APP_FIXUP_FILE}
471 ${shield_dts_fixups}
472 )
Sebastian Bøe361fdaa2019-01-28 13:40:50 +0100473 if(EXISTS ${fixup_file})
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100474 file(READ ${fixup_file} contents)
475 file(APPEND ${DTS_CAT_OF_FIXUP_FILES} "${contents}")
Martí Bolívar21c7d422020-05-08 16:06:48 -0700476 string(APPEND DISCOVERED_FIXUP_FILES "- ${fixup_file}\n")
Sebastian Bøec23cc262018-10-09 16:03:29 +0200477 endif()
478endforeach()
479
Martí Bolívar21c7d422020-05-08 16:06:48 -0700480if (DISCOVERED_FIXUP_FILES)
481 message(WARNING "One or more dts_fixup.h files detected:\n${DISCOVERED_FIXUP_FILES}Use of these files is deprecated; use the devicetree.h API instead.")
482endif()
483
Sebastian Bøe6f946e22018-01-09 10:52:57 +0100484# Unfortunately, the order in which CMakeLists.txt code is processed
485# matters so we need to be careful about how we order the processing
486# of subdirectories. One example is "Compiler flags added late in the
487# build are not exported to external build systems #5605"; when we
488# integrate with an external build system we read out all compiler
489# flags when the external project is created. So an external project
490# defined in subsys or ext will not get global flags added by drivers/
491# or tests/ as the subdirectories are ordered now.
492#
493# Another example of when the order matters is the reading and writing
494# of global properties such as ZEPHYR_LIBS or
495# GENERATED_KERNEL_OBJECT_FILES.
496#
497# Arch is placed early because it defines important compiler flags
498# that must be exported to external build systems defined in
499# e.g. subsys/.
500add_subdirectory(arch)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200501add_subdirectory(lib)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200502# We use include instead of add_subdirectory to avoid creating a new directory scope.
503# This is because source file properties are directory scoped, including the GENERATED
504# property which is set implicitly for custom command outputs
505include(misc/generated/CMakeLists.txt)
Anas Nashif3d1252f2018-09-03 15:20:14 -0500506
Erwan Gouriouba31cb52018-09-13 16:25:53 +0200507if(EXISTS ${SOC_DIR}/${ARCH}/CMakeLists.txt)
Anas Nashif96455d52018-09-04 14:34:06 -0500508 add_subdirectory(${SOC_DIR}/${ARCH} soc/${ARCH})
Anas Nashif3d1252f2018-09-03 15:20:14 -0500509else()
Anas Nashif96455d52018-09-04 14:34:06 -0500510 add_subdirectory(${SOC_DIR}/${ARCH}/${SOC_PATH} soc/${ARCH}/${SOC_PATH})
Anas Nashif3d1252f2018-09-03 15:20:14 -0500511endif()
512
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200513add_subdirectory(boards)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200514add_subdirectory(subsys)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200515add_subdirectory(drivers)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200516
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100517# Include zephyr modules generated CMake file.
Torsten Rasmussen2fc062b2020-08-24 11:01:04 +0200518foreach(module_name ${ZEPHYR_MODULE_NAMES})
519 # Note the second, binary_dir parameter requires the added
520 # subdirectory to have its own, local cmake target(s). If not then
521 # this binary_dir is created but stays empty. Object files land in
522 # the main binary dir instead.
523 # https://cmake.org/pipermail/cmake/2019-June/069547.html
Torsten Rasmussen3d880832021-01-19 12:01:38 +0100524 zephyr_string(SANITIZE TOUPPER MODULE_NAME_UPPER ${module_name})
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200525 if(NOT ${ZEPHYR_${MODULE_NAME_UPPER}_CMAKE_DIR} STREQUAL "")
526 set(ZEPHYR_CURRENT_MODULE_DIR ${ZEPHYR_${MODULE_NAME_UPPER}_MODULE_DIR})
527 set(ZEPHYR_CURRENT_CMAKE_DIR ${ZEPHYR_${MODULE_NAME_UPPER}_CMAKE_DIR})
528 add_subdirectory(${ZEPHYR_CURRENT_CMAKE_DIR} ${CMAKE_BINARY_DIR}/modules/${module_name})
529 endif()
Torsten Rasmussen2fc062b2020-08-24 11:01:04 +0200530endforeach()
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200531# Done processing modules, clear ZEPHYR_CURRENT_MODULE_DIR and ZEPHYR_CURRENT_CMAKE_DIR.
Torsten Rasmussen2fc062b2020-08-24 11:01:04 +0200532set(ZEPHYR_CURRENT_MODULE_DIR)
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200533set(ZEPHYR_CURRENT_CMAKE_DIR)
Torsten Rasmussen7e9d1bd2019-02-05 10:36:22 +0100534
Andrew Boie59601192020-05-29 13:24:51 -0700535set(syscall_list_h ${CMAKE_CURRENT_BINARY_DIR}/include/generated/syscall_list.h)
536set(syscalls_json ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls.json)
537set(struct_tags_json ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/struct_tags.json)
Sebastian Bøe13a68402017-11-20 13:03:55 +0100538
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200539# The syscalls subdirs txt file is constructed by python containing a list of folders to use for
540# dependency handling, including empty folders.
541# Windows: The list is used to specify DIRECTORY list with CMAKE_CONFIGURE_DEPENDS attribute.
542# Other OS: The list will update whenever a file is added/removed/modified and ensure a re-build.
543set(syscalls_subdirs_txt ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_subdirs.txt)
544
545# As syscalls_subdirs_txt is updated whenever a file is modified, this file can not be used for
546# monitoring of added / removed folders. A trigger file is thus used for correct dependency
547# handling. The trigger file will update when a folder is added / removed.
548set(syscalls_subdirs_trigger ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_subdirs.trigger)
549
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200550if(NOT (${CMAKE_HOST_SYSTEM_NAME} STREQUAL Windows))
551 set(syscalls_links --create-links ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_links)
552endif()
553
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200554# When running CMake it must be ensured that all dependencies are correctly acquired.
555execute_process(
556 COMMAND
557 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200558 ${ZEPHYR_BASE}/scripts/subfolder_list.py
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200559 --directory ${ZEPHYR_BASE}/include # Walk this directory
560 --out-file ${syscalls_subdirs_txt} # Write file with discovered folder
561 --trigger ${syscalls_subdirs_trigger} # Trigger file that is used for json generation
562 ${syscalls_links} # If defined, create symlinks for dependencies
563)
Carles Cufi3ad1f272019-07-18 10:38:25 +0200564file(STRINGS ${syscalls_subdirs_txt} PARSE_SYSCALLS_PATHS_DEPENDS ENCODING UTF-8)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200565
566if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL Windows)
567 # On windows only adding/removing files or folders will be reflected in depends.
568 # Hence adding a file requires CMake to re-run to add this file to the file list.
569 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${PARSE_SYSCALLS_PATHS_DEPENDS})
570
571 # Also On Windows each header file must be monitored as file modifications are not reflected
572 # on directory level.
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200573 file(GLOB_RECURSE PARSE_SYSCALLS_HEADER_DEPENDS ${ZEPHYR_BASE}/include/*.h)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200574else()
575 # The syscall parsing depends on the folders in order to detect add/removed/modified files.
576 # When a folder is removed, CMake will try to find a target that creates that dependency.
577 # This command sets up the target for CMake to find.
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200578 # Without this code, CMake will fail with the following error:
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200579 # <folder> needed by '<target>', missing and no known rule to make it
580 # when a folder is removed.
581 add_custom_command(OUTPUT ${PARSE_SYSCALLS_PATHS_DEPENDS}
582 COMMAND ${CMAKE_COMMAND} -E echo ""
583 COMMENT "Preparing syscall dependency handling"
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200584 )
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200585
586 add_custom_command(
587 OUTPUT
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200588 ${syscalls_subdirs_trigger}
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200589 COMMAND
590 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200591 ${ZEPHYR_BASE}/scripts/subfolder_list.py
592 --directory ${ZEPHYR_BASE}/include # Walk this directory
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200593 --out-file ${syscalls_subdirs_txt} # Write file with discovered folder
594 --trigger ${syscalls_subdirs_trigger} # Trigger file that is used for json generation
595 ${syscalls_links} # If defined, create symlinks for dependencies
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200596 DEPENDS ${PARSE_SYSCALLS_PATHS_DEPENDS}
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200597 )
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200598
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200599 # Ensure subdir file always exists when specifying CMake dependency.
600 if(NOT EXISTS ${syscalls_subdirs_txt})
601 file(WRITE ${syscalls_subdirs_txt} "")
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200602 endif()
603
604 # On other OS'es, modifying a file is reflected on the folder timestamp and hence detected
605 # when using depend on directory level.
606 # Thus CMake only needs to re-run when sub-directories are added / removed, which is indicated
607 # using a trigger file.
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200608 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${syscalls_subdirs_txt})
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200609endif()
610
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200611# syscall declarations are searched for in the SYSCALL_INCLUDE_DIRS
Adithya Baglodye67720b2018-07-02 14:59:19 +0530612if(CONFIG_APPLICATION_DEFINED_SYSCALL)
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200613 list(APPEND SYSCALL_INCLUDE_DIRS ${APPLICATION_SOURCE_DIR})
Adithya Baglodye67720b2018-07-02 14:59:19 +0530614endif()
615
Andrew Boiec1863872019-11-21 23:11:29 -0800616if(CONFIG_ZTEST)
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200617 list(APPEND SYSCALL_INCLUDE_DIRS ${ZEPHYR_BASE}/subsys/testsuite/ztest/include)
Andrew Boiec1863872019-11-21 23:11:29 -0800618endif()
619
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200620foreach(d ${SYSCALL_INCLUDE_DIRS})
621 list(APPEND parse_syscalls_include_args
622 --include ${d}
623 )
624endforeach()
625
Sebastian Bøe13a68402017-11-20 13:03:55 +0100626add_custom_command(
627 OUTPUT
628 ${syscalls_json}
Andrew Boie59601192020-05-29 13:24:51 -0700629 ${struct_tags_json}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100630 COMMAND
631 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200632 ${ZEPHYR_BASE}/scripts/parse_syscalls.py
Adithya Baglodye67720b2018-07-02 14:59:19 +0530633 --include ${ZEPHYR_BASE}/include # Read files from this dir
Andrew Boiefed960b2020-05-29 13:33:12 -0700634 --include ${ZEPHYR_BASE}/drivers # For net sockets
635 --include ${ZEPHYR_BASE}/subsys/net # More net sockets
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200636 ${parse_syscalls_include_args} # Read files from these dirs also
Corey Whartonccd15df2020-02-29 14:51:42 -0800637 --json-file ${syscalls_json} # Write this file
Andrew Boie59601192020-05-29 13:24:51 -0700638 --tag-struct-file ${struct_tags_json} # Write subsystem list to this file
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200639 DEPENDS ${syscalls_subdirs_trigger} ${PARSE_SYSCALLS_HEADER_DEPENDS}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100640 )
641
Joakim Andersson08d2f482020-08-04 18:26:43 +0200642add_custom_target(${SYSCALL_LIST_H_TARGET} DEPENDS ${syscall_list_h})
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +0100643
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +0100644set_property(TARGET ${SYSCALL_LIST_H_TARGET}
645 APPEND PROPERTY
646 ADDITIONAL_CLEAN_FILES
647 ${CMAKE_CURRENT_BINARY_DIR}/include/generated/syscalls
648)
649
Andrew Boiec1c54b12020-03-16 12:48:00 -0700650add_custom_target(${PARSE_SYSCALLS_TARGET}
Joakim Andersson08d2f482020-08-04 18:26:43 +0200651 DEPENDS
652 ${syscalls_json}
Joakim Anderssond268f822020-08-04 18:31:48 +0200653 ${struct_tags_json}
Joakim Andersson08d2f482020-08-04 18:26:43 +0200654 )
Andrew Boie9ff64bb2019-11-05 09:39:05 -0800655
656# 64-bit systems do not require special handling of 64-bit system call
657# parameters or return values, indicate this to the system call boilerplate
658# generation script.
659if(CONFIG_64BIT)
660 set(SYSCALL_LONG_REGISTERS_ARG --long-registers)
661endif()
662
Andy Rosscfeb07e2020-03-05 21:14:02 -0800663if(CONFIG_TIMEOUT_64BIT)
664 set(SYSCALL_SPLIT_TIMEOUT_ARG --split-type k_timeout_t)
665endif()
666
Sebastian Bøe13a68402017-11-20 13:03:55 +0100667add_custom_command(OUTPUT include/generated/syscall_dispatch.c ${syscall_list_h}
668 # Also, some files are written to include/generated/syscalls/
669 COMMAND
670 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200671 ${ZEPHYR_BASE}/scripts/gen_syscalls.py
Sebastian Bøe13a68402017-11-20 13:03:55 +0100672 --json-file ${syscalls_json} # Read this file
673 --base-output include/generated/syscalls # Write to this dir
674 --syscall-dispatch include/generated/syscall_dispatch.c # Write this file
Andrew Boie353acf42018-07-23 18:10:15 -0700675 --syscall-list ${syscall_list_h}
Andrew Boie9ff64bb2019-11-05 09:39:05 -0800676 ${SYSCALL_LONG_REGISTERS_ARG}
Andy Rosscfeb07e2020-03-05 21:14:02 -0800677 ${SYSCALL_SPLIT_TIMEOUT_ARG}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100678 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
Andrew Boiec1c54b12020-03-16 12:48:00 -0700679 DEPENDS ${PARSE_SYSCALLS_TARGET}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100680 )
681
Corey Whartonccd15df2020-02-29 14:51:42 -0800682# This is passed into all calls to the gen_kobject_list.py script.
Andrew Boie59601192020-05-29 13:24:51 -0700683set(gen_kobject_list_include_args --include ${struct_tags_json})
Corey Whartonccd15df2020-02-29 14:51:42 -0800684
Leandro Pereirac2003672018-04-04 13:50:32 -0700685set(DRV_VALIDATION ${PROJECT_BINARY_DIR}/include/generated/driver-validation.h)
686add_custom_command(
687 OUTPUT ${DRV_VALIDATION}
688 COMMAND
689 ${PYTHON_EXECUTABLE}
690 ${ZEPHYR_BASE}/scripts/gen_kobject_list.py
691 --validation-output ${DRV_VALIDATION}
Corey Whartonccd15df2020-02-29 14:51:42 -0800692 ${gen_kobject_list_include_args}
Leandro Pereirac2003672018-04-04 13:50:32 -0700693 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Corey Whartonccd15df2020-02-29 14:51:42 -0800694 DEPENDS
695 ${ZEPHYR_BASE}/scripts/gen_kobject_list.py
Andrew Boiec1c54b12020-03-16 12:48:00 -0700696 ${PARSE_SYSCALLS_TARGET}
Leandro Pereirac2003672018-04-04 13:50:32 -0700697 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
698 )
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100699add_custom_target(${DRIVER_VALIDATION_H_TARGET} DEPENDS ${DRV_VALIDATION})
Leandro Pereirac2003672018-04-04 13:50:32 -0700700
Torsten Rasmussend7862cf2020-02-12 15:42:09 +0100701include(${ZEPHYR_BASE}/cmake/kobj.cmake)
Leandro Pereira39dc7d02018-04-05 13:59:33 -0700702gen_kobj(KOBJ_INCLUDE_PATH)
Leandro Pereirac2003672018-04-04 13:50:32 -0700703
Sebastian Bøefdac7b32020-01-23 15:39:17 +0100704# Add a pseudo-target that is up-to-date when all generated headers
705# are up-to-date.
706
707add_custom_target(zephyr_generated_headers)
708add_dependencies(zephyr_generated_headers
709 offsets_h
710 )
711
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200712# Generate offsets.c.obj from offsets.c
713# Generate offsets.h from offsets.c.obj
714
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100715set(OFFSETS_LIB offsets)
716
Klaus Petersenc66cb762018-11-15 10:37:46 +0100717set(OFFSETS_C_PATH ${ARCH_DIR}/${ARCH}/core/offsets/offsets.c)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200718set(OFFSETS_H_PATH ${PROJECT_BINARY_DIR}/include/generated/offsets.h)
719
Klaus Petersen62e55e52019-02-04 12:10:57 +0100720add_library( ${OFFSETS_LIB} OBJECT ${OFFSETS_C_PATH})
Stephanos Ioannidis2d746042019-10-25 00:08:21 +0900721target_include_directories(${OFFSETS_LIB} PRIVATE
722 kernel/include
723 ${ARCH_DIR}/${ARCH}/include
724 )
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100725target_link_libraries(${OFFSETS_LIB} zephyr_interface)
Joakim Anderssond268f822020-08-04 18:31:48 +0200726add_dependencies(zephyr_interface
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100727 ${SYSCALL_LIST_H_TARGET}
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100728 ${DRIVER_VALIDATION_H_TARGET}
729 ${KOBJ_TYPES_H_TARGET}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100730 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200731
732add_custom_command(
733 OUTPUT ${OFFSETS_H_PATH}
Carles Cufi7d764b32018-01-11 15:46:44 +0100734 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/gen_offset_header.py
Klaus Petersen62e55e52019-02-04 12:10:57 +0100735 -i $<TARGET_OBJECTS:${OFFSETS_LIB}>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200736 -o ${OFFSETS_H_PATH}
Sebastian Bøe5962aab2019-08-15 14:45:59 +0200737 DEPENDS
738 ${OFFSETS_LIB}
739 $<TARGET_OBJECTS:${OFFSETS_LIB}>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200740)
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100741add_custom_target(${OFFSETS_H_TARGET} DEPENDS ${OFFSETS_H_PATH})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200742
Sebastian Bøe89516fb2017-12-01 15:25:06 +0100743zephyr_get_include_directories_for_lang(C ZEPHYR_INCLUDES)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200744
745add_subdirectory(kernel)
746
747# Read list content
748get_property(ZEPHYR_LIBS_PROPERTY GLOBAL PROPERTY ZEPHYR_LIBS)
749
750foreach(zephyr_lib ${ZEPHYR_LIBS_PROPERTY})
Torsten Rasmussend9520042021-04-14 17:11:39 +0200751 get_property(lib_type TARGET ${zephyr_lib} PROPERTY TYPE)
Torsten Rasmussend9520042021-04-14 17:11:39 +0200752 # To prevent CMake failure when a driver is enabled, for example: REGULATOR=y
753 # we disable any Zephyr libraries without sources and adds the `empty_file.c`.
754 if(${lib_type} STREQUAL STATIC_LIBRARY
Torsten Rasmussend9520042021-04-14 17:11:39 +0200755 AND NOT ${zephyr_lib} STREQUAL app
756 )
Torsten Rasmussen25578be2021-04-23 09:09:49 +0200757 get_property(source_list TARGET ${zephyr_lib} PROPERTY SOURCES)
758 get_property(lib_imported TARGET ${zephyr_lib} PROPERTY IMPORTED)
759 if(NOT source_list
760 AND NOT ${lib_imported}
Torsten Rasmussend9520042021-04-14 17:11:39 +0200761 )
Torsten Rasmussen153196b2021-09-06 16:42:21 +0200762 get_property(allow_empty TARGET ${zephyr_lib} PROPERTY ALLOW_EMPTY)
763 if(NOT "${allow_empty}")
764 message(WARNING
765 "No SOURCES given to Zephyr library: ${zephyr_lib}\nExcluding target from build."
766 )
767 endif()
Torsten Rasmussen25578be2021-04-23 09:09:49 +0200768 target_sources(${zephyr_lib} PRIVATE ${ZEPHYR_BASE}/misc/empty_file.c)
769 set_property(TARGET ${zephyr_lib} PROPERTY EXCLUDE_FROM_ALL TRUE)
770 list(REMOVE_ITEM ZEPHYR_LIBS_PROPERTY ${zephyr_lib})
771 continue()
772 endif()
Torsten Rasmussend9520042021-04-14 17:11:39 +0200773 endif()
Torsten Rasmussen25578be2021-04-23 09:09:49 +0200774
775 # TODO: Could this become an INTERFACE property of zephyr_interface?
776 add_dependencies(${zephyr_lib} zephyr_generated_headers)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200777endforeach()
778
779get_property(OUTPUT_FORMAT GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT)
780
Adithya Baglody62e152a2018-11-13 15:34:02 +0530781if (CONFIG_CODE_DATA_RELOCATION)
782 set(CODE_RELOCATION_DEP code_relocation_source_lib)
783endif() # CONFIG_CODE_DATA_RELOCATION
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100784
Daniel Leungc7459952021-03-19 12:09:05 -0700785# Give the linker script targets all of the include directories so
786# that cmake can successfully find the linker scripts' header
Sebastian Bøeb1ab5012017-12-14 13:03:23 +0100787# dependencies.
788zephyr_get_include_directories_for_lang(C
789 ZEPHYR_INCLUDE_DIRS
790 STRIP_PREFIX # Don't use a -I prefix
791 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200792
Morten Priessa846e722021-04-21 09:06:02 +0200793if(CONFIG_HAS_DTS)
Jordan Yates28b2e552021-10-20 20:19:28 +1000794 # dev_handles.c is generated from ${ZEPHYR_LINK_STAGE_EXECUTABLE} by
Morten Priessa846e722021-04-21 09:06:02 +0200795 # gen_handles.py
796 add_custom_command(
797 OUTPUT dev_handles.c
798 COMMAND
799 ${PYTHON_EXECUTABLE}
800 ${ZEPHYR_BASE}/scripts/gen_handles.py
801 --output-source dev_handles.c
Jordan Yates28b2e552021-10-20 20:19:28 +1000802 --kernel $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
Morten Priessa846e722021-04-21 09:06:02 +0200803 --zephyr-base ${ZEPHYR_BASE}
Torsten Rasmussenc9804d22021-05-21 21:34:58 +0200804 --start-symbol "$<TARGET_PROPERTY:linker,devices_start_symbol>"
Jordan Yates28b2e552021-10-20 20:19:28 +1000805 DEPENDS ${ZEPHYR_LINK_STAGE_EXECUTABLE}
Morten Priessa846e722021-04-21 09:06:02 +0200806 )
Jordan Yates28b2e552021-10-20 20:19:28 +1000807 set_property(GLOBAL APPEND PROPERTY GENERATED_APP_SOURCE_FILES dev_handles.c)
808
809 # gen_handles runs on `__device_handles_pass1` so pass this info to the linker script generator
810 list(APPEND LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE "LINKER_DEVICE_HANDLES_PASS1")
Morten Priessa846e722021-04-21 09:06:02 +0200811endif()
Peter Bigotd554d342020-06-30 10:05:35 -0500812
Adithya Baglody62e152a2018-11-13 15:34:02 +0530813if(CONFIG_CODE_DATA_RELOCATION)
Mark Ruvald Pedersen86a3e8f2019-05-03 10:33:03 +0200814 # @Intent: Linker script to relocate .text, data and .bss sections
815 toolchain_ld_relocation()
Adithya Baglody62e152a2018-11-13 15:34:02 +0530816endif()
817
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530818if(CONFIG_USERSPACE)
Torsten Rasmussene37d9e62020-11-20 18:39:30 +0100819 zephyr_get_compile_options_for_lang_as_string(C compiler_flags_priv)
Torsten Rasmussene0758c32020-08-21 19:13:53 +0200820 string(REPLACE "$<TARGET_PROPERTY:compiler,coverage>" ""
821 NO_COVERAGE_FLAGS "${compiler_flags_priv}"
822 )
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530823
Carles Cufi7d764b32018-01-11 15:46:44 +0100824 set(GEN_KOBJ_LIST ${ZEPHYR_BASE}/scripts/gen_kobject_list.py)
825 set(PROCESS_GPERF ${ZEPHYR_BASE}/scripts/process_gperf.py)
Jordan Yates28b2e552021-10-20 20:19:28 +1000826endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200827
Jordan Yates28b2e552021-10-20 20:19:28 +1000828get_property(CSTD GLOBAL PROPERTY CSTD)
829set_ifndef(CSTD c99)
830
831# @Intent: Obtain compiler specific flag for specifying the c standard
832zephyr_compile_options(
833 $<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,cstd>${CSTD}>
834)
835set(CMAKE_C_COMPILE_FEATURES ${compile_features_${CSTD}} PARENT_SCOPE)
836
837# @Intent: Configure linker scripts, i.e. generate linker scripts with variables substituted
838toolchain_ld_configure_files()
839
840if(CONFIG_USERSPACE)
841 set(APP_SMEM_ALIGNED_LD "${PROJECT_BINARY_DIR}/include/generated/app_smem_aligned.ld")
842 set(APP_SMEM_UNALIGNED_LD "${PROJECT_BINARY_DIR}/include/generated/app_smem_unaligned.ld")
843
844 if(CONFIG_LINKER_USE_PINNED_SECTION)
845 set(APP_SMEM_PINNED_ALIGNED_LD
846 "${PROJECT_BINARY_DIR}/include/generated/app_smem_pinned_aligned.ld")
847 set(APP_SMEM_PINNED_UNALIGNED_LD
848 "${PROJECT_BINARY_DIR}/include/generated/app_smem_pinned_unaligned.ld")
849
850 if(NOT CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT)
851 # The libc partition may hold symbols that are required during boot process,
852 # for example, stack guard (if enabled). So the libc partition must be pinned
853 # if not sections are in physical memory at boot, as the paging mechanism is
854 # only initialized post-kernel.
855 set_property(TARGET app_smem APPEND PROPERTY pinned_partitions "z_libc_partition")
856 endif()
857
858 get_property(APP_SMEM_PINNED_PARTITION_LIST TARGET app_smem PROPERTY pinned_partitions)
859 if(APP_SMEM_PINNED_PARTITION_LIST)
860 list(JOIN APP_SMEM_PINNED_PARTITION_LIST "," APP_SMEM_PINNED_PARTITION_LIST_ARG_CSL)
861 set(APP_SMEM_PINNED_PARTITION_LIST_ARG "--pinpartitions=${APP_SMEM_PINNED_PARTITION_LIST_ARG_CSL}")
862 endif()
863 endif()
864
865 set(OBJ_FILE_DIR "${PROJECT_BINARY_DIR}/../")
866
867 if(CONFIG_NEWLIB_LIBC)
868 set(NEWLIB_PART -l libc.a z_libc_partition)
869 endif()
870 if(CONFIG_NEWLIB_LIBC_NANO)
871 set(NEWLIB_PART -l libc_nano.a z_libc_partition)
872 endif()
873
874 add_custom_command(
875 OUTPUT ${APP_SMEM_UNALIGNED_LD} ${APP_SMEM_PINNED_UNALIGNED_LD}
876 COMMAND ${PYTHON_EXECUTABLE}
877 ${ZEPHYR_BASE}/scripts/gen_app_partitions.py
Torsten Rasmussenf643b8b2021-11-24 15:35:47 +0100878 -f ${CMAKE_BINARY_DIR}/compile_commands.json
Jordan Yates28b2e552021-10-20 20:19:28 +1000879 -o ${APP_SMEM_UNALIGNED_LD}
880 $<$<BOOL:${APP_SMEM_PINNED_UNALIGNED_LD}>:--pinoutput=${APP_SMEM_PINNED_UNALIGNED_LD}>
881 ${APP_SMEM_PINNED_PARTITION_LIST_ARG}
882 ${NEWLIB_PART}
883 $<TARGET_PROPERTY:zephyr_property_target,COMPILE_OPTIONS>
884 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
885 DEPENDS
886 kernel
887 ${ZEPHYR_LIBS_PROPERTY}
888 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
889 COMMAND_EXPAND_LISTS
890 COMMENT "Generating app_smem_unaligned linker section"
891 )
892
893 add_custom_target(
894 ${APP_SMEM_ALIGNED_DEP}
895 DEPENDS
896 ${APP_SMEM_ALIGNED_LD}
897 ${APP_SMEM_PINNED_ALIGNED_LD}
898 )
899
900 add_custom_target(
901 ${APP_SMEM_UNALIGNED_DEP}
902 DEPENDS
903 ${APP_SMEM_UNALIGNED_LD}
904 ${APP_SMEM_PINNED_UNALIGNED_LD}
905 )
906
907 set(APP_SMEM_UNALIGNED_LIB app_smem_unaligned_output_obj_renamed_lib)
908 list(APPEND LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE "LINKER_APP_SMEM_UNALIGNED")
909endif()
910
911if (CONFIG_USERSPACE)
912 add_custom_command(
913 OUTPUT ${APP_SMEM_ALIGNED_LD} ${APP_SMEM_PINNED_ALIGNED_LD}
914 COMMAND ${PYTHON_EXECUTABLE}
915 ${ZEPHYR_BASE}/scripts/gen_app_partitions.py
916 -e $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
917 -o ${APP_SMEM_ALIGNED_LD}
918 $<$<BOOL:${APP_SMEM_PINNED_ALIGNED_LD}>:--pinoutput=${APP_SMEM_PINNED_ALIGNED_LD}>
919 ${APP_SMEM_PINNED_PARTITION_LIST_ARG}
920 ${NEWLIB_PART}
921 $<TARGET_PROPERTY:zephyr_property_target,COMPILE_OPTIONS>
922 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
923 DEPENDS
924 kernel
925 ${ZEPHYR_LIBS_PROPERTY}
926 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
927 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
928 COMMAND_EXPAND_LISTS
929 COMMENT "Generating app_smem_aligned linker section"
930 )
931endif()
932
933if(CONFIG_USERSPACE)
934 # This CONFIG_USERSPACE block is to create place holders to reserve space
935 # for the gperf generated structures for zephyr_prebuilt.elf.
936 # These place holders are there so that the placement of kobjects would be
937 # the same between linking zephyr_prebuilt.elf and zephyr.elf, as
938 # the gperf hash table is hashed on the addresses of kobjects.
939 # The placeholders are generated from app_smem_unaligned_prebuilt.elf.
940
941 set(KOBJECT_PREBUILT_HASH_LIST kobject_prebuilt_hash.gperf)
942 set(KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE kobject_prebuilt_hash_preprocessed.c)
943 set(KOBJECT_PREBUILT_HASH_OUTPUT_SRC kobject_prebuilt_hash.c)
944
945 add_custom_command(
946 OUTPUT ${KOBJECT_PREBUILT_HASH_LIST}
947 COMMAND
948 ${PYTHON_EXECUTABLE}
949 ${GEN_KOBJ_LIST}
950 --kernel $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
951 --gperf-output ${KOBJECT_PREBUILT_HASH_LIST}
952 ${gen_kobject_list_include_args}
953 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
954 DEPENDS
955 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
956 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
957 )
958 add_custom_target(
959 kobj_prebuilt_hash_list
960 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_LIST}
961 )
962
963 add_custom_command(
964 OUTPUT ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
965 COMMAND
966 ${GPERF}
967 --output-file ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
968 --multiple-iterations 10
969 ${KOBJECT_PREBUILT_HASH_LIST}
970 DEPENDS kobj_prebuilt_hash_list ${KOBJECT_PREBUILT_HASH_LIST}
971 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
972 )
973 add_custom_target(
974 kobj_prebuilt_hash_output_src_pre
975 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
976 )
977
978 add_custom_command(
979 OUTPUT ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
980 COMMAND
981 ${PYTHON_EXECUTABLE}
982 ${PROCESS_GPERF}
983 -i ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
984 -o ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
985 -p "struct z_object"
986 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
987 DEPENDS kobj_prebuilt_hash_output_src_pre ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
988 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
989 )
990 add_custom_target(
991 kobj_prebuilt_hash_output_src
992 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
993 )
994
995 add_library(
996 kobj_prebuilt_hash_output_lib
997 OBJECT ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
998 )
999
1000 set_source_files_properties(${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1001 PROPERTIES COMPILE_FLAGS
1002 "${NO_COVERAGE_FLAGS} -fno-function-sections -fno-data-sections")
1003
1004 target_compile_definitions(kobj_prebuilt_hash_output_lib
1005 PRIVATE $<TARGET_PROPERTY:zephyr_interface,INTERFACE_COMPILE_DEFINITIONS>
1006 )
1007
1008 target_include_directories(kobj_prebuilt_hash_output_lib
1009 PUBLIC $<TARGET_PROPERTY:zephyr_interface,INTERFACE_INCLUDE_DIRECTORIES>
1010 )
1011
1012 target_include_directories(kobj_prebuilt_hash_output_lib SYSTEM
1013 PUBLIC $<TARGET_PROPERTY:zephyr_interface,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
1014 )
1015
1016 set(KOBJECT_LINKER_HEADER_DATA "${PROJECT_BINARY_DIR}/include/generated/linker-kobject-prebuilt-data.h")
1017
1018 add_custom_command(
1019 OUTPUT ${KOBJECT_LINKER_HEADER_DATA}
1020 COMMAND
1021 ${PYTHON_EXECUTABLE}
1022 ${ZEPHYR_BASE}/scripts/gen_kobject_placeholders.py
1023 --object $<TARGET_OBJECTS:kobj_prebuilt_hash_output_lib>
1024 --outdir ${PROJECT_BINARY_DIR}/include/generated
1025 --datapct ${CONFIG_KOBJECT_DATA_AREA_RESERVE_EXTRA_PERCENT}
1026 --rodata ${CONFIG_KOBJECT_RODATA_AREA_EXTRA_BYTES}
1027 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1028 DEPENDS
1029 kobj_prebuilt_hash_output_lib
1030 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1031 )
1032
1033 add_custom_target(
1034 ${KOBJECT_LINKER_DEP}
1035 DEPENDS
1036 ${KOBJECT_LINKER_HEADER_DATA}
1037 )
1038endif()
1039
1040if(CONFIG_USERSPACE OR CONFIG_HAS_DTS)
1041 configure_linker_script(
1042 ${ZEPHYR_CURRENT_LINKER_CMD}
1043 "${LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE}"
1044 ${CODE_RELOCATION_DEP}
1045 ${APP_SMEM_UNALIGNED_DEP}
1046 ${APP_SMEM_UNALIGNED_LD}
1047 ${APP_SMEM_PINNED_UNALIGNED_LD}
1048 zephyr_generated_headers
1049 )
1050
1051 add_custom_target(
1052 linker_zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS}_script
1053 DEPENDS
1054 ${ZEPHYR_CURRENT_LINKER_CMD}
1055 )
1056
1057 set_property(TARGET
1058 linker_zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS}_script
1059 PROPERTY INCLUDE_DIRECTORIES
1060 ${ZEPHYR_INCLUDE_DIRS}
1061 )
1062
1063 add_executable(${ZEPHYR_LINK_STAGE_EXECUTABLE} misc/empty_file.c)
1064 toolchain_ld_link_elf(
1065 TARGET_ELF ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1066 OUTPUT_MAP ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
1067 LIBRARIES_PRE_SCRIPT ""
1068 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD}
1069 LIBRARIES_POST_SCRIPT ""
1070 DEPENDENCIES ${CODE_RELOCATION_DEP}
1071 )
1072 target_byproducts(TARGET ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1073 BYPRODUCTS ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
1074 )
1075 set_property(TARGET ${ZEPHYR_LINK_STAGE_EXECUTABLE} PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD})
1076 add_dependencies(${ZEPHYR_LINK_STAGE_EXECUTABLE} linker_zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS}_script ${OFFSETS_LIB})
1077
1078 math(EXPR ZEPHYR_CURRENT_LINKER_PASS "1 + ${ZEPHYR_CURRENT_LINKER_PASS}")
1079endif()
1080
1081set(ZEPHYR_CURRENT_LINKER_CMD linker_zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS}.cmd)
1082set(ZEPHYR_LINK_STAGE_EXECUTABLE zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS})
1083list(APPEND LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE "LINKER_ZEPHYR_PREBUILT")
1084
1085if(CONFIG_GEN_ISR_TABLES)
1086 if(CONFIG_GEN_SW_ISR_TABLE)
1087 list(APPEND GEN_ISR_TABLE_EXTRA_ARG --sw-isr-table)
1088 endif()
1089
1090 if(CONFIG_GEN_IRQ_VECTOR_TABLE)
1091 list(APPEND GEN_ISR_TABLE_EXTRA_ARG --vector-table)
1092 endif()
1093
1094 # isr_tables.c is generated from ${ZEPHYR_LINK_STAGE_EXECUTABLE} by
1095 # gen_isr_tables.py
1096 add_custom_command(
1097 OUTPUT isr_tables.c isrList.bin
1098 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1099 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1100 $<TARGET_PROPERTY:bintools,elfconvert_flag_intarget>${OUTPUT_FORMAT}
1101 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>binary
1102 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_only>.intList
1103 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>$<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
1104 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>isrList.bin
1105 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
1106 COMMAND ${PYTHON_EXECUTABLE}
1107 ${ZEPHYR_BASE}/arch/common/gen_isr_tables.py
1108 --output-source isr_tables.c
1109 --kernel $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
1110 --intlist isrList.bin
1111 $<$<BOOL:${CONFIG_BIG_ENDIAN}>:--big-endian>
1112 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--debug>
1113 ${GEN_ISR_TABLE_EXTRA_ARG}
1114 DEPENDS ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1115 COMMAND_EXPAND_LISTS
1116 )
1117 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_SOURCE_FILES isr_tables.c)
1118endif()
1119
1120if(CONFIG_USERSPACE)
Daniel Leung28c35122021-03-18 12:02:19 -07001121 set(KOBJECT_HASH_LIST kobject_hash.gperf)
1122 set(KOBJECT_HASH_OUTPUT_SRC_PRE kobject_hash_preprocessed.c)
1123 set(KOBJECT_HASH_OUTPUT_SRC kobject_hash.c)
Daniel Leung28c35122021-03-18 12:02:19 -07001124 set(KOBJECT_HASH_OUTPUT_OBJ_RENAMED kobject_hash_renamed.o)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001125
1126 # Essentially what we are doing here is extracting some information
1127 # out of the nearly finished elf file, generating the source code
1128 # for a hash table based on that information, and then compiling and
1129 # linking the hash table back into a now even more nearly finished
Marc Herbert4a10eea2019-04-16 15:39:45 -07001130 # elf file. More information in gen_kobject_list.py --help.
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001131
1132 # Use the script GEN_KOBJ_LIST to scan the kernel binary's
Jordan Yates28b2e552021-10-20 20:19:28 +10001133 # (${ZEPHYR_LINK_STAGE_EXECUTABLE}) DWARF information to produce a table of kernel
Daniel Leung28c35122021-03-18 12:02:19 -07001134 # objects (KOBJECT_HASH_LIST) which we will then pass to gperf
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001135 add_custom_command(
Daniel Leung28c35122021-03-18 12:02:19 -07001136 OUTPUT ${KOBJECT_HASH_LIST}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001137 COMMAND
1138 ${PYTHON_EXECUTABLE}
1139 ${GEN_KOBJ_LIST}
Jordan Yates28b2e552021-10-20 20:19:28 +10001140 --kernel $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
Daniel Leung28c35122021-03-18 12:02:19 -07001141 --gperf-output ${KOBJECT_HASH_LIST}
Corey Whartonccd15df2020-02-29 14:51:42 -08001142 ${gen_kobject_list_include_args}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001143 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Corey Whartonccd15df2020-02-29 14:51:42 -08001144 DEPENDS
Jordan Yates28b2e552021-10-20 20:19:28 +10001145 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001146 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1147 )
Daniel Leung28c35122021-03-18 12:02:19 -07001148 add_custom_target(
1149 kobj_hash_list
1150 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_LIST}
1151 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001152
Daniel Leung28c35122021-03-18 12:02:19 -07001153 # Use gperf to generate C code (KOBJECT_HASH_OUTPUT_SRC_PRE) which implements a
1154 # perfect hashtable based on KOBJECT_HASH_LIST
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001155 add_custom_command(
Daniel Leung28c35122021-03-18 12:02:19 -07001156 OUTPUT ${KOBJECT_HASH_OUTPUT_SRC_PRE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001157 COMMAND
1158 ${GPERF}
Daniel Leung28c35122021-03-18 12:02:19 -07001159 --output-file ${KOBJECT_HASH_OUTPUT_SRC_PRE}
Daniel Leung11171692021-03-18 14:00:07 -07001160 --multiple-iterations 10
Daniel Leung28c35122021-03-18 12:02:19 -07001161 ${KOBJECT_HASH_LIST}
1162 DEPENDS kobj_hash_list ${KOBJECT_HASH_LIST}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001163 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1164 )
Daniel Leung28c35122021-03-18 12:02:19 -07001165 add_custom_target(
1166 kobj_hash_output_src_pre
1167 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC_PRE}
1168 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001169
1170 # For our purposes the code/data generated by gperf is not optimal.
1171 #
Daniel Leung28c35122021-03-18 12:02:19 -07001172 # The script PROCESS_GPERF creates a new c file KOBJECT_HASH_OUTPUT_SRC based on
1173 # KOBJECT_HASH_OUTPUT_SRC_PRE to greatly reduce the amount of code/data generated
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001174 # since we know we are always working with pointer values
1175 add_custom_command(
Daniel Leung28c35122021-03-18 12:02:19 -07001176 OUTPUT ${KOBJECT_HASH_OUTPUT_SRC}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001177 COMMAND
Sebastian Bøe1b600702018-06-21 14:34:42 +02001178 ${PYTHON_EXECUTABLE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001179 ${PROCESS_GPERF}
Daniel Leung28c35122021-03-18 12:02:19 -07001180 -i ${KOBJECT_HASH_OUTPUT_SRC_PRE}
1181 -o ${KOBJECT_HASH_OUTPUT_SRC}
Andrew Boie2dc2ecf2020-03-11 07:13:07 -07001182 -p "struct z_object"
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001183 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Daniel Leung28c35122021-03-18 12:02:19 -07001184 DEPENDS kobj_hash_output_src_pre ${KOBJECT_HASH_OUTPUT_SRC_PRE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001185 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1186 )
Daniel Leung28c35122021-03-18 12:02:19 -07001187 add_custom_target(
1188 kobj_hash_output_src
1189 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC}
1190 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001191
1192 # We need precise control of where generated text/data ends up in the final
1193 # kernel image. Disable function/data sections and use objcopy to move
1194 # generated data into special section names
Daniel Leung28c35122021-03-18 12:02:19 -07001195 add_library(
1196 kobj_hash_output_lib
Torsten Rasmussen47304d42021-11-02 12:06:05 +01001197 OBJECT ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001198 )
1199
Daniel Leung28c35122021-03-18 12:02:19 -07001200 set_source_files_properties(${KOBJECT_HASH_OUTPUT_SRC}
1201 PROPERTIES COMPILE_FLAGS
Andrew Boiea5148982019-03-14 17:04:11 -07001202 "${NO_COVERAGE_FLAGS} -fno-function-sections -fno-data-sections")
Adithya Baglody4b3c7b32018-11-21 14:31:56 +05301203
Torsten Rasmussen47304d42021-11-02 12:06:05 +01001204 target_compile_definitions(kobj_hash_output_lib
1205 PRIVATE $<TARGET_PROPERTY:zephyr_interface,INTERFACE_COMPILE_DEFINITIONS>
1206 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001207
Torsten Rasmussen47304d42021-11-02 12:06:05 +01001208 target_include_directories(kobj_hash_output_lib
1209 PUBLIC $<TARGET_PROPERTY:zephyr_interface,INTERFACE_INCLUDE_DIRECTORIES>
1210 )
Adithya Baglody4b3c7b32018-11-21 14:31:56 +05301211
Torsten Rasmussen47304d42021-11-02 12:06:05 +01001212 target_include_directories(kobj_hash_output_lib SYSTEM
1213 PUBLIC $<TARGET_PROPERTY:zephyr_interface,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
Daniel Leung28c35122021-03-18 12:02:19 -07001214 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001215
1216 add_custom_command(
Daniel Leung28c35122021-03-18 12:02:19 -07001217 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001218 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1219 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1220 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.data=.kobject_data.data
Jim Shu70e1dee2021-07-08 01:11:34 +08001221 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.sdata=.kobject_data.sdata
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001222 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.text=.kobject_data.text
1223 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.rodata=.kobject_data.rodata
Torsten Rasmussen47304d42021-11-02 12:06:05 +01001224 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>$<TARGET_OBJECTS:kobj_hash_output_lib>
Daniel Leung28c35122021-03-18 12:02:19 -07001225 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001226 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Daniel Leung28c35122021-03-18 12:02:19 -07001227 DEPENDS kobj_hash_output_lib
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001228 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001229 COMMAND_EXPAND_LISTS
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001230 )
Daniel Leung28c35122021-03-18 12:02:19 -07001231 add_custom_target(
1232 kobj_hash_output_obj_renamed
1233 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
1234 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001235
Daniel Leung28c35122021-03-18 12:02:19 -07001236 add_library(kobj_hash_output_obj_renamed_lib STATIC IMPORTED GLOBAL)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001237 set_property(
Daniel Leung28c35122021-03-18 12:02:19 -07001238 TARGET kobj_hash_output_obj_renamed_lib
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001239 PROPERTY
Daniel Leung28c35122021-03-18 12:02:19 -07001240 IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001241 )
1242 add_dependencies(
Daniel Leung28c35122021-03-18 12:02:19 -07001243 kobj_hash_output_obj_renamed_lib
1244 kobj_hash_output_obj_renamed
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001245 )
1246
Daniel Leung28c35122021-03-18 12:02:19 -07001247 set_property(
1248 GLOBAL APPEND PROPERTY
1249 GENERATED_KERNEL_OBJECT_FILES kobj_hash_output_obj_renamed_lib
1250 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001251endif()
1252
Daniel Leungc7459952021-03-19 12:09:05 -07001253configure_linker_script(
Jordan Yates28b2e552021-10-20 20:19:28 +10001254 ${ZEPHYR_CURRENT_LINKER_CMD}
1255 "${LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE}"
Daniel Leungc7459952021-03-19 12:09:05 -07001256 ${APP_SMEM_ALIGNED_DEP}
Daniel Leung11171692021-03-18 14:00:07 -07001257 ${KOBJECT_LINKER_DEP}
Daniel Leungc7459952021-03-19 12:09:05 -07001258 ${CODE_RELOCATION_DEP}
1259 zephyr_generated_headers
1260 )
1261
1262add_custom_target(
1263 linker_zephyr_prebuilt_script_target
1264 DEPENDS
Jordan Yates28b2e552021-10-20 20:19:28 +10001265 ${ZEPHYR_CURRENT_LINKER_CMD}
Daniel Leungc7459952021-03-19 12:09:05 -07001266 )
1267
1268set_property(TARGET
1269 linker_zephyr_prebuilt_script_target
1270 PROPERTY INCLUDE_DIRECTORIES
1271 ${ZEPHYR_INCLUDE_DIRS}
1272 )
1273
Jordan Yates28b2e552021-10-20 20:19:28 +10001274# Read global variables into local variables
1275get_property(GASF GLOBAL PROPERTY GENERATED_APP_SOURCE_FILES)
1276get_property(GKOF GLOBAL PROPERTY GENERATED_KERNEL_OBJECT_FILES)
1277get_property(GKSF GLOBAL PROPERTY GENERATED_KERNEL_SOURCE_FILES)
1278
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001279# FIXME: Is there any way to get rid of empty_file.c?
Jordan Yates28b2e552021-10-20 20:19:28 +10001280add_executable( ${ZEPHYR_LINK_STAGE_EXECUTABLE} misc/empty_file.c ${GASF})
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001281toolchain_ld_link_elf(
Jordan Yates28b2e552021-10-20 20:19:28 +10001282 TARGET_ELF ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1283 OUTPUT_MAP ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001284 LIBRARIES_PRE_SCRIPT ""
Jordan Yates28b2e552021-10-20 20:19:28 +10001285 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD}
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001286 DEPENDENCIES ${CODE_RELOCATION_DEP}
1287)
Jordan Yates28b2e552021-10-20 20:19:28 +10001288target_byproducts(TARGET ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1289 BYPRODUCTS ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +01001290)
Daniel Leungc7459952021-03-19 12:09:05 -07001291set_property(TARGET
Jordan Yates28b2e552021-10-20 20:19:28 +10001292 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1293 PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD}
Daniel Leungc7459952021-03-19 12:09:05 -07001294 )
1295add_dependencies(
Jordan Yates28b2e552021-10-20 20:19:28 +10001296 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
Daniel Leungc7459952021-03-19 12:09:05 -07001297 linker_zephyr_prebuilt_script_target
1298 ${OFFSETS_LIB}
1299 )
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +02001300
Marc Herbert498b4942019-04-16 23:30:52 -07001301set(generated_kernel_files ${GKSF} ${GKOF})
1302if(NOT generated_kernel_files)
1303 # Use the prebuilt elf as the final elf since we don't have a
1304 # generation stage.
Jordan Yates28b2e552021-10-20 20:19:28 +10001305 set(logical_target_for_zephyr_elf ${ZEPHYR_LINK_STAGE_EXECUTABLE})
Marc Herbert498b4942019-04-16 23:30:52 -07001306else()
Daniel Leungcdd02a92021-03-19 12:18:52 -07001307 # The final linker pass uses the same source linker script of the
1308 # previous passes, but this time with a different output
1309 # file and preprocessed with the define LINKER_ZEPHYR_FINAL.
Mark Ruvald Pedersen4c811972019-04-29 17:16:54 +02001310 configure_linker_script(
Daniel Leungcdd02a92021-03-19 12:18:52 -07001311 linker.cmd
Torsten Rasmussen1fa3f152021-11-01 12:53:28 +01001312 "LINKER_ZEPHYR_FINAL"
Sebastian Bøe2a963122019-02-08 15:49:57 +01001313 ${CODE_RELOCATION_DEP}
Jordan Yates28b2e552021-10-20 20:19:28 +10001314 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
Sebastian Bøefdac7b32020-01-23 15:39:17 +01001315 zephyr_generated_headers
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +01001316 )
Andy Grosse8860fe2018-02-01 01:12:32 -06001317
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001318 add_custom_target(
Daniel Leungcdd02a92021-03-19 12:18:52 -07001319 linker_zephyr_final_script_target
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001320 DEPENDS
Daniel Leungcdd02a92021-03-19 12:18:52 -07001321 linker.cmd
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001322 )
Sebastian Bøeb1ab5012017-12-14 13:03:23 +01001323 set_property(TARGET
Daniel Leungcdd02a92021-03-19 12:18:52 -07001324 linker_zephyr_final_script_target
Sebastian Bøeb1ab5012017-12-14 13:03:23 +01001325 PROPERTY INCLUDE_DIRECTORIES
1326 ${ZEPHYR_INCLUDE_DIRS}
1327 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001328
Jordan Yates8d932172021-10-20 20:09:39 +10001329 add_executable( ${ZEPHYR_FINAL_EXECUTABLE} misc/empty_file.c ${GASF} ${GKSF})
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001330 toolchain_ld_link_elf(
1331 TARGET_ELF ${ZEPHYR_FINAL_EXECUTABLE}
Marc Herbert0370c9b2019-06-13 16:15:44 -07001332 OUTPUT_MAP ${PROJECT_BINARY_DIR}/${ZEPHYR_FINAL_EXECUTABLE}.map
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001333 LIBRARIES_PRE_SCRIPT ${GKOF}
Daniel Leungcdd02a92021-03-19 12:18:52 -07001334 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/linker.cmd
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001335 LIBRARIES_POST_SCRIPT ""
1336 DEPENDENCIES ${CODE_RELOCATION_DEP}
1337 )
Daniel Leungcdd02a92021-03-19 12:18:52 -07001338 set_property(TARGET ${ZEPHYR_FINAL_EXECUTABLE} PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker.cmd)
1339 add_dependencies( ${ZEPHYR_FINAL_EXECUTABLE} linker_zephyr_final_script_target)
Mark Ruvald Pedersen37d49472019-05-07 15:20:20 +02001340
1341 # Use the pass2 elf as the final elf
1342 set(logical_target_for_zephyr_elf ${ZEPHYR_FINAL_EXECUTABLE})
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001343endif()
1344
Sebastian Bøe0fc39342018-10-16 13:25:04 +02001345# Export the variable to the application's scope to allow the
1346# application to know what the name of the final elf target is.
1347set(logical_target_for_zephyr_elf ${logical_target_for_zephyr_elf} PARENT_SCOPE)
1348
Marc Herbert0370c9b2019-06-13 16:15:44 -07001349# Override the base name of the last, "logical" .elf output (and last .map) so:
Marc Herbert498b4942019-04-16 23:30:52 -07001350# 1. it doesn't depend on the number of passes above and the
1351# post_build_commands below can always find it no matter which is it;
1352# 2. it can be defined in Kconfig
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001353set_target_properties(${logical_target_for_zephyr_elf} PROPERTIES OUTPUT_NAME ${KERNEL_NAME})
1354
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001355set(post_build_commands "")
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001356set(post_build_byproducts "")
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001357
Marc Herbert0370c9b2019-06-13 16:15:44 -07001358list(APPEND
1359 post_build_commands
1360 COMMAND
Torsten Rasmussen4df38c72020-06-11 10:48:06 +02001361 ${CMAKE_COMMAND} -E rename ${logical_target_for_zephyr_elf}.map ${KERNEL_MAP_NAME}
Marc Herbert0370c9b2019-06-13 16:15:44 -07001362)
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +01001363list(APPEND post_build_byproducts ${KERNEL_MAP_NAME})
Marc Herbert0370c9b2019-06-13 16:15:44 -07001364
Håkon Øye Amundsenc086b932018-11-26 09:47:16 +00001365if(NOT CONFIG_BUILD_NO_GAP_FILL)
1366 # Use ';' as separator to get proper space in resulting command.
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001367 set(GAP_FILL "$<TARGET_PROPERTY:bintools,elfconvert_flag_gapfill>0xff")
Håkon Øye Amundsenc086b932018-11-26 09:47:16 +00001368endif()
1369
Danny Oerndrupc41e7122019-07-18 15:16:39 +02001370if(CONFIG_OUTPUT_PRINT_MEMORY_USAGE)
Torsten Rasmussend537be02021-02-02 21:06:11 +01001371 target_link_libraries(${logical_target_for_zephyr_elf} $<TARGET_PROPERTY:linker,memusage>)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001372
1373 get_property(memusage_build_command TARGET bintools PROPERTY memusage_command)
1374 if(memusage_build_command)
1375 # Note: The use of generator expressions allows downstream extensions to add/change the post build.
1376 # Unfortunately, the BYPRODUCTS does not allow for generator expression, so question is if we
1377 # should remove the downstream ability from start.
1378 # Or fix the output name, by the use of `get_property`
1379 list(APPEND
1380 post_build_commands
Torsten Rasmussen571f48f2020-09-04 21:07:46 +02001381 COMMAND $<TARGET_PROPERTY:bintools,memusage_command>
1382 $<TARGET_PROPERTY:bintools,memusage_flag>
1383 $<TARGET_PROPERTY:bintools,memusage_infile>${KERNEL_ELF_NAME}
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001384 )
1385
1386 # For now, the byproduct can only be supported upstream on byproducts name,
1387 # cause byproduct does not support generator expressions
1388 get_property(memusage_byproducts TARGET bintools PROPERTY memusage_byproducts)
1389 list(APPEND
1390 post_build_byproducts
1391 ${memusage_byproducts}
1392 )
1393 endif()
Danny Oerndrupc41e7122019-07-18 15:16:39 +02001394endif()
1395
Kumar Galad5419132019-08-13 13:44:20 -05001396if(CONFIG_BUILD_OUTPUT_HEX OR BOARD_FLASH_RUNNER STREQUAL openocd)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001397 get_property(elfconvert_formats TARGET bintools PROPERTY elfconvert_formats)
1398 if(ihex IN_LIST elfconvert_formats)
1399 list(APPEND
1400 post_build_commands
1401 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1402 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1403 ${GAP_FILL}
1404 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>ihex
1405 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>.comment
1406 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>COMMON
1407 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>.eh_frame
1408 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1409 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_HEX_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001410 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001411 )
1412 list(APPEND
1413 post_build_byproducts
1414 ${KERNEL_HEX_NAME}
1415 # ${out_hex_byprod} # Is this needed ?
1416 )
1417 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001418endif()
Anas Nashif4592ff22017-11-23 07:54:26 -05001419
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001420if(CONFIG_BUILD_OUTPUT_BIN)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001421 get_property(elfconvert_formats TARGET bintools PROPERTY elfconvert_formats)
1422 if(binary IN_LIST elfconvert_formats)
1423 list(APPEND
1424 post_build_commands
1425 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1426 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1427 ${GAP_FILL}
1428 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>binary
1429 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>.comment
1430 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>COMMON
1431 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>.eh_frame
1432 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1433 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_BIN_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001434 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001435 )
1436 list(APPEND
1437 post_build_byproducts
1438 ${KERNEL_BIN_NAME}
1439 # ${out_hex_byprod} # Is this needed ?
1440 )
1441 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001442endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001443
Pete Johanson310a4642020-12-31 16:51:52 -05001444if(CONFIG_BUILD_OUTPUT_BIN AND CONFIG_BUILD_OUTPUT_UF2)
1445 list(APPEND
1446 post_build_commands
1447 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/uf2conv.py
1448 -c
1449 -f ${CONFIG_BUILD_OUTPUT_UF2_FAMILY_ID}
1450 -b ${CONFIG_FLASH_LOAD_OFFSET}
1451 -o ${KERNEL_UF2_NAME}
1452 ${KERNEL_BIN_NAME}
1453 )
1454 list(APPEND
1455 post_build_byproducts
1456 ${KERNEL_UF2_NAME}
1457 )
1458endif()
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001459
Torsten Rasmussenfffaf052021-10-12 23:08:36 +02001460if(CONFIG_BUILD_OUTPUT_META)
1461 list(APPEND
1462 post_build_commands
1463 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/zephyr_module.py
1464 ${WEST_ARG}
1465 ${ZEPHYR_MODULES_ARG}
1466 ${ZEPHYR_EXTRA_MODULES_ARG}
1467 --meta-out ${KERNEL_META_NAME}
Torsten Rasmussen1a519932021-11-04 18:35:50 +01001468 $<$<BOOL:${CONFIG_BUILD_OUTPUT_META_STATE_PROPAGATE}>:--meta-state-propagate>
Torsten Rasmussenfffaf052021-10-12 23:08:36 +02001469 )
1470 list(APPEND
1471 post_build_byproducts
1472 ${KERNEL_META_NAME}
1473 )
1474endif()
1475
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001476# Cleanup intermediate files
1477if(CONFIG_CLEANUP_INTERMEDIATE_FILES)
Torsten Rasmussend2cdee52021-11-25 12:23:34 +01001478 foreach(index RANGE ${ZEPHYR_CURRENT_LINKER_PASS})
1479 # Those files can be very large in some cases, delete them as we do not need them.
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001480 list(APPEND
1481 post_build_commands
1482 COMMAND
Torsten Rasmussend2cdee52021-11-25 12:23:34 +01001483 ${CMAKE_COMMAND} -E remove zephyr_pre${index}.elf
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001484 )
Torsten Rasmussend2cdee52021-11-25 12:23:34 +01001485 endforeach()
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001486endif()
1487
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001488if(CONFIG_BUILD_OUTPUT_S19)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001489 get_property(elfconvert_formats TARGET bintools PROPERTY elfconvert_formats)
1490 if(srec IN_LIST elfconvert_formats)
1491 # Should we print a warning if case the tools does not support converting to s19 ?
1492 list(APPEND
1493 post_build_commands
1494 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1495 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1496 ${GAP_FILL}
1497 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>srec
1498 $<TARGET_PROPERTY:bintools,elfconvert_flag_srec_len>1
1499 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1500 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_S19_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001501 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001502 )
1503 list(APPEND
1504 post_build_byproducts
1505 ${KERNEL_S19_NAME}
1506 # ${out_S19_byprod} # Is this needed ?
1507
1508 )
1509 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001510endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001511
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001512if(CONFIG_OUTPUT_DISASSEMBLY)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001513if(CONFIG_OUTPUT_DISASSEMBLE_ALL)
1514 set(disassembly_type "$<TARGET_PROPERTY:bintools,disassembly_flag_all>")
Rohit Gujarathi35713f22020-05-07 10:08:37 +05301515 else()
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001516 set(disassembly_type "$<TARGET_PROPERTY:bintools,disassembly_flag_inline_source>")
Rohit Gujarathi35713f22020-05-07 10:08:37 +05301517 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001518 list(APPEND
1519 post_build_commands
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001520 COMMAND $<TARGET_PROPERTY:bintools,disassembly_command>
1521 $<TARGET_PROPERTY:bintools,disassembly_flag>
1522 ${disassembly_type}
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001523 $<TARGET_PROPERTY:bintools,disassembly_flag_infile>${KERNEL_ELF_NAME}
1524 $<TARGET_PROPERTY:bintools,disassembly_flag_outfile>${KERNEL_LST_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001525 $<TARGET_PROPERTY:bintools,disassembly_flag_final>
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001526 )
1527 list(APPEND
1528 post_build_byproducts
1529 ${KERNEL_LST_NAME}
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001530# ${out_disassembly_byprod} # Needed ??
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001531 )
1532endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001533
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001534if(CONFIG_OUTPUT_STAT)
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001535# zephyr_post_build(TOOLS bintools COMMAND readelf FLAGS headers INFILE file OUTFILE outfile)
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001536 list(APPEND
1537 post_build_commands
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001538 COMMAND $<TARGET_PROPERTY:bintools,readelf_command>
1539 $<TARGET_PROPERTY:bintools,readelf_flag>
1540 $<TARGET_PROPERTY:bintools,readelf_flag_headers>
Torsten Rasmussen2d1a3d92021-05-20 17:38:57 +02001541 $<TARGET_PROPERTY:bintools,readelf_flag_infile>${KERNEL_ELF_NAME}
1542 $<TARGET_PROPERTY:bintools,readelf_flag_outfile>${KERNEL_STAT_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001543 $<TARGET_PROPERTY:bintools,readelf_flag_final>
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001544 )
1545 list(APPEND
1546 post_build_byproducts
1547 ${KERNEL_STAT_NAME}
1548 )
1549endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001550
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001551if(CONFIG_BUILD_OUTPUT_STRIPPED)
1552 list(APPEND
1553 post_build_commands
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001554 COMMAND $<TARGET_PROPERTY:bintools,strip_command>
1555 $<TARGET_PROPERTY:bintools,strip_flag>
1556 $<TARGET_PROPERTY:bintools,strip_flag_all>
1557 $<TARGET_PROPERTY:bintools,strip_flag_infile>${KERNEL_ELF_NAME}
1558 $<TARGET_PROPERTY:bintools,strip_flag_outfile>${KERNEL_STRIP_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001559 $<TARGET_PROPERTY:bintools,strip_flag_final>
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001560 )
1561 list(APPEND
1562 post_build_byproducts
1563 ${KERNEL_STRIP_NAME}
1564 )
1565endif()
1566
1567if(CONFIG_BUILD_OUTPUT_EXE)
1568 list(APPEND
1569 post_build_commands
1570 COMMAND
1571 ${CMAKE_COMMAND} -E copy ${KERNEL_ELF_NAME} ${KERNEL_EXE_NAME}
1572 )
1573 list(APPEND
1574 post_build_byproducts
1575 ${KERNEL_EXE_NAME}
1576 )
1577endif()
Anas Nashif4592ff22017-11-23 07:54:26 -05001578
Martí Bolívarf66a0c32020-08-18 11:28:04 -07001579# Generate and use MCUboot related artifacts as needed.
1580if(CONFIG_BOOTLOADER_MCUBOOT)
1581 include(${CMAKE_CURRENT_LIST_DIR}/cmake/mcuboot.cmake)
1582endif()
1583
Rajavardhan Gundiecdea1c2019-01-25 11:53:13 +05301584get_property(extra_post_build_commands
1585 GLOBAL PROPERTY
1586 extra_post_build_commands
1587 )
1588
1589list(APPEND
1590 post_build_commands
1591 ${extra_post_build_commands}
1592 )
1593
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001594get_property(extra_post_build_byproducts
1595 GLOBAL PROPERTY
1596 extra_post_build_byproducts
1597 )
1598
1599list(APPEND
1600 post_build_byproducts
1601 ${extra_post_build_byproducts}
1602 )
1603
Daniel Leunga5ab1a72021-04-02 12:54:53 -07001604if(CONFIG_LOG_DICTIONARY_SUPPORT)
1605 set(LOG_DICT_DB_NAME ${PROJECT_BINARY_DIR}/log_dictionary.json)
1606
1607 list(APPEND
1608 post_build_commands
1609 COMMAND
1610 ${PYTHON_EXECUTABLE}
1611 ${ZEPHYR_BASE}/scripts/logging/dictionary/database_gen.py
1612 ${KERNEL_ELF_NAME}
1613 ${LOG_DICT_DB_NAME}
1614 --build ${BUILD_VERSION}
1615 )
1616 list(APPEND
1617 post_build_byproducts
1618 ${LOG_DICT_DB_NAME}
1619 )
1620endif()
1621
Marc Herbert498b4942019-04-16 23:30:52 -07001622# Add post_build_commands to post-process the final .elf file produced by
Jordan Yates28b2e552021-10-20 20:19:28 +10001623# either the ZEPHYR_LINK_STAGE_EXECUTABLE or the KERNEL_ELF executable
Marc Herbert498b4942019-04-16 23:30:52 -07001624# targets above.
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001625add_custom_command(
1626 TARGET ${logical_target_for_zephyr_elf}
1627 POST_BUILD
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001628 ${post_build_commands}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001629 BYPRODUCTS
1630 ${post_build_byproducts}
Marc Herbert498b4942019-04-16 23:30:52 -07001631 COMMENT "Generating files from ${KERNEL_ELF_NAME} for board: ${BOARD}"
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001632 COMMAND_EXPAND_LISTS
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001633 # NB: COMMENT only works for some CMake-Generators
Rajavardhan Gundiecdea1c2019-01-25 11:53:13 +05301634 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001635
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001636# To populate with hex files to merge, do the following:
1637# set_property(GLOBAL APPEND PROPERTY HEX_FILES_TO_MERGE ${my_local_list})
1638# Note that the zephyr.hex file will not be included automatically.
1639get_property(HEX_FILES_TO_MERGE GLOBAL PROPERTY HEX_FILES_TO_MERGE)
1640if(HEX_FILES_TO_MERGE)
1641 # Merge in out-of-tree hex files.
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001642 set(MERGED_HEX_NAME merged.hex)
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001643
1644 add_custom_command(
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001645 OUTPUT ${MERGED_HEX_NAME}
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001646 COMMAND
1647 ${PYTHON_EXECUTABLE}
1648 ${ZEPHYR_BASE}/scripts/mergehex.py
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001649 -o ${MERGED_HEX_NAME}
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001650 ${HEX_FILES_TO_MERGE}
1651 DEPENDS ${HEX_FILES_TO_MERGE} ${logical_target_for_zephyr_elf}
1652 )
1653
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001654 add_custom_target(mergehex ALL DEPENDS ${MERGED_HEX_NAME})
Torsten Rasmussend38da9d2020-06-30 09:55:54 +02001655 list(APPEND RUNNERS_DEPS mergehex)
Håkon Øye Amundsenc9a2a5e2020-01-03 08:25:03 +00001656
1657 message(VERBOSE "Merging hex files: ${HEX_FILES_TO_MERGE}")
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001658endif()
1659
Filip Kokosinski94428042021-08-30 13:09:48 +02001660if(SUPPORTED_EMU_PLATFORMS)
1661 list(GET SUPPORTED_EMU_PLATFORMS 0 default_emu)
1662 add_custom_target(run DEPENDS run_${default_emu})
1663
1664 foreach(EMU_PLATFORM ${SUPPORTED_EMU_PLATFORMS})
1665 include(${ZEPHYR_BASE}/cmake/emu/${EMU_PLATFORM}.cmake)
1666 endforeach()
1667
1668 if(TARGET debugserver_${default_emu})
1669 add_custom_target(debugserver DEPENDS debugserver_${default_emu})
1670 endif()
Anas Nashiffd276ae2017-12-21 16:45:45 -05001671else()
1672 add_custom_target(run
1673 COMMAND
1674 ${CMAKE_COMMAND} -E echo
1675 "==================================================="
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +01001676 "Emulation/Simulation not supported with this board."
Anas Nashiffd276ae2017-12-21 16:45:45 -05001677 "==================================================="
1678 )
Anas Nashifc15d3c92017-11-21 18:54:55 -05001679endif()
1680
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001681add_subdirectory(cmake/flash)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001682add_subdirectory(cmake/usage)
1683add_subdirectory(cmake/reports)
1684
Marc Herbert83723102019-06-17 13:26:11 -07001685if(NOT CONFIG_TEST)
Andrew Boie411686f2018-05-24 13:18:36 -07001686if(CONFIG_ASSERT AND (NOT CONFIG_FORCE_NO_ASSERT))
Sebastian Bøefa8f9d42019-12-06 12:54:53 +01001687 message(WARNING "__ASSERT() statements are globally ENABLED")
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001688endif()
Marc Herbert83723102019-06-17 13:26:11 -07001689endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001690
Vincent Wana2bc5142020-01-09 14:20:44 -08001691if(CONFIG_BOARD_DEPRECATED_RELEASE)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001692 message(WARNING "
1693 WARNING: The board '${BOARD}' is deprecated and will be
Vincent Wana2bc5142020-01-09 14:20:44 -08001694 removed in version ${CONFIG_BOARD_DEPRECATED_RELEASE}"
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +01001695 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001696endif()
Paul Sokolovsky1c6a7d72019-06-06 21:12:14 +03001697
Vincent Wan180b4df2020-01-08 17:10:51 -08001698if(CONFIG_SOC_DEPRECATED_RELEASE)
1699 message(WARNING "
1700 WARNING: The SoC '${SOC_NAME}' is deprecated and will be
1701 removed in version ${CONFIG_SOC_DEPRECATED_RELEASE}"
1702 )
1703endif()
1704
Sebastian Bøee50e12d2019-08-29 16:19:32 +02001705# In CMake projects, 'CMAKE_BUILD_TYPE' usually determines the
1706# optimization flag, but in Zephyr it is determined through
1707# Kconfig. Here we give a warning when there is a mismatch between the
1708# two in case the user is not aware of this.
1709set(build_types None Debug Release RelWithDebInfo MinSizeRel)
1710
1711if((CMAKE_BUILD_TYPE IN_LIST build_types) AND (NOT NO_BUILD_TYPE_WARNING))
1712 string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_uppercase)
Torsten Rasmussen05bb8552021-12-10 15:06:30 +01001713 # The CMAKE_C_FLAGS_<build_type> is a string, so we do a regex to see if the
1714 # optimization flag is present in that string.
1715 # To avoid false-positive matches, the flag must either be matched first
1716 # or last in string, or come after / followed by minimum a space.
1717 if(NOT (CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_uppercase} MATCHES "(^| )${OPTIMIZATION_FLAG}($| )"))
Sebastian Bøee50e12d2019-08-29 16:19:32 +02001718 message(WARNING "
1719 The CMake build type was set to '${CMAKE_BUILD_TYPE}', but the optimization flag was set to '${OPTIMIZATION_FLAG}'.
1720 This may be intentional and the warning can be turned off by setting the CMake variable 'NO_BUILD_TYPE_WARNING'"
1721 )
1722 endif()
1723endif()
1724
Stephanos Ioannidisfd4700d2021-09-11 22:06:28 +09001725# @Intent: Set compiler specific flags for standard C/C++ includes
Paul Sokolovsky1c6a7d72019-06-06 21:12:14 +03001726# Done at the very end, so any other system includes which may
1727# be added by Zephyr components were first in list.
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001728# Note, the compile flags are moved, but the system include is still present here.
1729zephyr_compile_options($<TARGET_PROPERTY:compiler,nostdinc>)
1730target_include_directories(zephyr_interface SYSTEM INTERFACE $<TARGET_PROPERTY:compiler,nostdinc_include>)
Torsten Rasmussena5917f02020-09-09 15:42:32 +02001731
Stephanos Ioannidisfd4700d2021-09-11 22:06:28 +09001732if(NOT CONFIG_LIB_CPLUSPLUS)
1733 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,nostdincxx>>)
1734endif()
1735
Torsten Rasmussena5917f02020-09-09 15:42:32 +02001736# Finally export all build flags from Zephyr
1737add_subdirectory_ifdef(
1738 CONFIG_MAKEFILE_EXPORTS
1739 cmake/makefile_exports
1740 )