blob: 3a705f39d56809a92d97aa2cb474119bc96cac3b [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
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100148# @Intent: Obtain compiler optimizations flags and store in variables
149# @details:
150# Kconfig.zephyr "Optimization level" is a kconfig choice, ensuring
151# only *one* of CONFIG_{NO,DEBUG,SPEED,SIZE}_OPTIMIZATIONS is set.
152# Refer to Kconfig.zephyr for selection logic and description of these choices.
153# toolchain_cc_optimize_*() macros must provide the mapping from these kconfigs
154# to compiler flags. Each macro will store the flags in a CMake variable, whose
155# name is passed as argument (somewhat like by reference).
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200156#
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100157# If the user wants to tweak the optimizations, there are two ways:
158# 1) Using EXTRA_CFLAGS which is applied regardless of kconfig choice, or
159# 2) Rely on override support being implemented by your toolchain_cc_optimize_*()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200160#
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200161get_property(OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG TARGET compiler PROPERTY no_optimization)
162get_property(OPTIMIZE_FOR_DEBUG_FLAG TARGET compiler PROPERTY optimization_debug)
163get_property(OPTIMIZE_FOR_SPEED_FLAG TARGET compiler PROPERTY optimization_speed)
164get_property(OPTIMIZE_FOR_SIZE_FLAG TARGET compiler PROPERTY optimization_size)
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100165
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100166# From kconfig choice, pick the actual OPTIMIZATION_FLAG to use.
167# Kconfig choice ensures only one of these CONFIG_*_OPTIMIZATIONS is set.
Alberto Escolar Piedrasf60527a2018-01-22 15:35:54 +0100168if(CONFIG_NO_OPTIMIZATIONS)
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100169 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG})
170elseif(CONFIG_DEBUG_OPTIMIZATIONS)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200171 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_DEBUG_FLAG})
Aurelien Jarnoe8413d12018-06-16 23:40:04 +0200172elseif(CONFIG_SPEED_OPTIMIZATIONS)
173 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SPEED_FLAG})
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100174elseif(CONFIG_SIZE_OPTIMIZATIONS)
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100175 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SIZE_FLAG}) # Default in kconfig
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100176else()
Anas Nashif885aaf22019-01-18 19:15:19 -0500177 assert(0 "Unreachable code. Expected optimization level to have been chosen. See Kconfig.zephyr")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200178endif()
179
Ulf Magnussonde42aea2020-02-07 00:48:22 +0100180if(NOT CONFIG_ARCH_IS_SET)
181 message(WARNING "\
182None of the CONFIG_<arch> (e.g. CONFIG_X86) symbols are set. \
183Select one of them from the SOC_SERIES_* symbol or, lacking that, from the \
184SOC_* symbol.")
185endif()
186
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100187# Apply the final optimization flag(s)
188zephyr_compile_options(${OPTIMIZATION_FLAG})
189
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100190# @Intent: Obtain compiler specific flags related to C++ that are not influenced by kconfig
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200191zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,required>>)
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200192
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100193# @Intent: Obtain compiler specific flags for compiling under different ISO standards of C++
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200194if(CONFIG_CPLUSPLUS)
195 # From kconfig choice, pick a single dialect.
196 # Kconfig choice ensures only one of these CONFIG_STD_CPP* is set.
197 if(CONFIG_STD_CPP98)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200198 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp98>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200199 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp98})
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200200 elseif(CONFIG_STD_CPP11)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200201 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp11>) # Default in kconfig
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200202 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp11})
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200203 elseif(CONFIG_STD_CPP14)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200204 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp14>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200205 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp14})
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200206 elseif(CONFIG_STD_CPP17)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200207 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp17>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200208 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp17})
Alexander Wachterad130f22021-07-14 10:50:21 +0200209 elseif(CONFIG_STD_CPP2A)
210 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp2a>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200211 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp20})
Dan Kalowskyc0811e92021-07-09 10:46:46 -0700212 elseif(CONFIG_STD_CPP20)
213 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp20>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200214 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp20})
Dan Kalowsky9b333912021-07-09 10:54:11 -0700215 elseif(CONFIG_STD_CPP2B)
216 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp2b>)
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200217 list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp20})
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200218 else()
219 assert(0 "Unreachable code. Expected C++ standard to have been chosen. See Kconfig.zephyr.")
220 endif()
Torsten Rasmussen917d5022021-09-30 21:01:57 +0200221 set(CMAKE_CXX_COMPILE_FEATURES ${CMAKE_CXX_COMPILE_FEATURES} PARENT_SCOPE)
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200222
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200223 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:${STD_CPP_DIALECT_FLAGS}>)
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200224endif()
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100225
226if(NOT CONFIG_EXCEPTIONS)
227 # @Intent: Obtain compiler specific flags related to C++ Exceptions
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200228 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,no_exceptions>>)
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100229endif()
230
231if(NOT CONFIG_RTTI)
232 # @Intent: Obtain compiler specific flags related to C++ Run Time Type Information
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200233 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,no_rtti>>)
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100234endif()
235
Andy Rossfe04adf2019-02-27 11:53:18 -0800236if(CONFIG_MISRA_SANE)
Danny Oerndrup8e5a95e2019-05-16 12:53:58 +0200237 # @Intent: Obtain toolchain compiler flags relating to MISRA.
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200238 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_misra_sane>>)
239 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_error_misra_sane>>)
Andy Rossfe04adf2019-02-27 11:53:18 -0800240endif()
241
Flavio Ceolinb587e8d2020-08-26 09:48:33 -0700242# This is intend to be temporary. Once we have fixed the violations that
243# prevents build Zephyr, these flags shall be part of the default flags.
244if(CONFIG_CODING_GUIDELINE_CHECK)
245 # @Intent: Obtain toolchain compiler flags relating to coding guideline
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200246 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_coding_guideline>>)
Flavio Ceolinb587e8d2020-08-26 09:48:33 -0700247endif()
248
Danny Oerndrup4ddbc002019-06-11 13:55:53 +0200249# @Intent: Set compiler specific macro inclusion of AUTOCONF_H
Torsten Rasmussenf109e682020-08-13 15:49:46 +0200250zephyr_compile_options("SHELL: $<TARGET_PROPERTY:compiler,imacros> ${AUTOCONF_H}")
Danny Oerndrup4ddbc002019-06-11 13:55:53 +0200251
Danny Oerndrupfaa72b72019-06-11 15:56:57 +0200252# @Intent: Set compiler specific flag for bare metal freestanding option
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200253zephyr_compile_options($<TARGET_PROPERTY:compiler,freestanding>)
Danny Oerndrupfaa72b72019-06-11 15:56:57 +0200254
Danny Oerndrupe34ed7c2019-06-12 14:56:46 +0200255# @Intent: Set compiler specific flag for tentative definitions, no-common
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200256zephyr_compile_options($<TARGET_PROPERTY:compiler,no_common>)
Danny Oerndrupe34ed7c2019-06-12 14:56:46 +0200257
Danny Oerndrupe0569ac2019-07-23 09:00:55 +0200258# @Intent: Set compiler specific flag for production of debug information
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200259zephyr_compile_options($<TARGET_PROPERTY:compiler,debug>)
Danny Oerndrupe0569ac2019-07-23 09:00:55 +0200260
Arvin Farahmande430b7b2021-04-15 11:20:10 -0400261if(CONFIG_COMPILER_COLOR_DIAGNOSTICS)
Arvin Farahmandb8f59682021-04-15 11:20:10 -0400262# @Intent: Set compiler specific flag for diagnostic messages
263zephyr_compile_options($<TARGET_PROPERTY:compiler,diagnostic>)
264endif()
265
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200266zephyr_compile_options(
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530267 ${TOOLCHAIN_C_FLAGS}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200268)
269
Mark Ruvald Pedersencb0fd452019-01-30 21:48:25 +0100270# @Intent: Obtain compiler specific flags related to assembly
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200271# ToDo: Remember to get feedback from Oticon on this, as they might use the `ASM_BASE_FLAG` since this is done this way.
272zephyr_compile_options($<$<COMPILE_LANGUAGE:ASM>:$<TARGET_PROPERTY:asm,required>>)
Mark Ruvald Pedersencb0fd452019-01-30 21:48:25 +0100273
Nicolas Pitreb86aa652019-07-02 16:22:04 -0400274# @Intent: Enforce standard integer type correspondance to match Zephyr usage.
275# (must be after compiler specific flags)
Stephanos Ioannidisa1a66192021-09-12 19:33:15 +0900276if(NOT CONFIG_ARCH_POSIX)
277 # `zephyr_stdint.h` is not included for the POSIX (native) arch because it
278 # compiles with the host toolchain/headers and there can be conflicts if we
279 # arbitrarily redefine our own type system (see #37718).
280 zephyr_compile_options("SHELL: $<TARGET_PROPERTY:compiler,imacros> ${ZEPHYR_BASE}/include/toolchain/zephyr_stdint.h")
281endif()
Nicolas Pitreb86aa652019-07-02 16:22:04 -0400282
Mark Ruvald Pedersencb0fd452019-01-30 21:48:25 +0100283# Common toolchain-agnostic assembly flags
284zephyr_compile_options(
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200285 $<$<COMPILE_LANGUAGE:ASM>:-D_ASMLANGUAGE>
286)
287
Mark Ruvald Pedersen1f013252019-04-25 15:46:11 +0200288# @Intent: Set fundamental linker specific flags
289toolchain_ld_base()
Aurelien Jarnoc6727d42018-11-26 13:48:34 +0100290
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +0200291toolchain_ld_force_undefined_symbols(
292 _OffsetAbsSyms
293 _ConfigAbsSyms
294)
295
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200296if(NOT CONFIG_NATIVE_APPLICATION)
Mark Ruvald Pedersen65f02c02019-04-25 16:31:30 +0200297 # @Intent: Set linker specific flags for bare metal target
298 toolchain_ld_baremetal()
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200299endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200300
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200301if(CONFIG_LIB_CPLUSPLUS)
Mark Ruvald Pedersen3db09aa2019-04-26 08:43:04 +0200302 # @Intent: Set linker specific flags for C++
303 toolchain_ld_cpp()
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200304endif()
305
Danny Oerndrup8eaa9062019-05-16 12:49:31 +0200306# @Intent: Add the basic toolchain warning flags
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200307zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_base>>)
308zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_base>>)
Danny Oerndrupbdb229f2019-05-06 15:19:27 +0200309
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200310# ==========================================================================
311#
312# cmake -DW=... settings
313#
314# W=1 - warnings that may be relevant and does not occur too often
315# W=2 - warnings that occur quite often but may still be relevant
316# W=3 - the more obscure warnings, can most likely be ignored
317# ==========================================================================
Danny Oerndrup8eaa9062019-05-16 12:49:31 +0200318# @Intent: Add cmake -DW toolchain supported warnings, if any
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200319if(W MATCHES "1")
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200320 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_dw_1>>)
321 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_dw_1>>)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200322endif()
323
324if(W MATCHES "2")
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200325 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_dw_2>>)
326 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_dw_2>>)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200327endif()
328
329if(W MATCHES "3")
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200330 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_dw_3>>)
331 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_dw_3>>)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200332endif()
333
Danny Oerndrup8eaa9062019-05-16 12:49:31 +0200334# @Intent: Add extended, more specific, toolchain warning flags
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200335zephyr_compile_options($<TARGET_PROPERTY:compiler,warning_extended>)
Benoit Leforestier04dad592019-01-25 13:57:03 +0100336
Danny Oerndrup025ffa22019-05-16 12:58:40 +0200337# @Intent: Trigger an error when a declaration does not specify a type
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200338zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_implicit_int>>)
339zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_error_implicit_int>>)
Danny Oerndrup025ffa22019-05-16 12:58:40 +0200340
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200341# Allow the user to inject options when calling cmake, e.g.
342# 'cmake -DEXTRA_CFLAGS="-Werror -Wno-deprecated-declarations" ..'
Sebastian Bøe9f590452017-11-10 12:22:23 +0100343include(cmake/extra_flags.cmake)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200344
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200345zephyr_cc_option(-fno-asynchronous-unwind-tables)
346zephyr_cc_option(-fno-pie)
347zephyr_cc_option(-fno-pic)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200348
Daniel Leung02b20352020-09-28 11:27:11 -0700349if(CONFIG_THREAD_LOCAL_STORAGE)
350# Only support local exec TLS model at this point.
351zephyr_cc_option(-ftls-model=local-exec)
352endif()
353
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200354if(CONFIG_OVERRIDE_FRAME_POINTER_DEFAULT)
355 if(CONFIG_OMIT_FRAME_POINTER)
356 zephyr_cc_option(-fomit-frame-pointer)
357 else()
358 zephyr_cc_option(-fno-omit-frame-pointer)
359 endif()
360endif()
361
Sebastian Bøe244451b2019-02-27 08:28:25 +0100362separate_arguments(COMPILER_OPT_AS_LIST UNIX_COMMAND ${CONFIG_COMPILER_OPT})
363zephyr_compile_options(${COMPILER_OPT_AS_LIST})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200364
365# TODO: Include arch compiler options at this point.
366
Danny Oerndrupcbbbdea2019-05-06 15:21:58 +0200367if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
368 # GCC assumed
369 zephyr_cc_option(-fno-reorder-functions)
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530370
Anas Nashif7ee8bb92018-02-11 14:36:21 -0600371 if(NOT ${ZEPHYR_TOOLCHAIN_VARIANT} STREQUAL "xcc")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200372 zephyr_cc_option(-fno-defer-pop)
373 endif()
374endif()
375
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200376zephyr_cc_option_ifdef(CONFIG_STACK_USAGE -fstack-usage)
377
Marc Herbert28a56572019-04-11 16:34:04 -0700378# If the compiler supports it, strip the ${ZEPHYR_BASE} prefix from the
379# __FILE__ macro used in __ASSERT*, in the
380# .noinit."/home/joe/zephyr/fu/bar.c" section names and in any
381# application code. This saves some memory, stops leaking user locations
382# in binaries, makes failure logs more deterministic and most
383# importantly makes builds more deterministic
Marc Herbertf67dcdb2019-05-31 15:28:38 -0700384
Marc Herberteddbf3c2019-06-11 16:57:37 -0700385# If several match then the last one wins. This matters for instances
386# like tests/ and samples/: they're inside all of them! Then let's
387# strip as little as possible.
Marc Herbertf67dcdb2019-05-31 15:28:38 -0700388zephyr_cc_option(-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=CMAKE_SOURCE_DIR)
389zephyr_cc_option(-fmacro-prefix-map=${ZEPHYR_BASE}=ZEPHYR_BASE)
Marc Herberteddbf3c2019-06-11 16:57:37 -0700390if(WEST_TOPDIR)
391 zephyr_cc_option(-fmacro-prefix-map=${WEST_TOPDIR}=WEST_TOPDIR)
392endif()
Marc Herbert28a56572019-04-11 16:34:04 -0700393
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200394# TODO: Archiver arguments
395# ar_option(D)
396
Håkon Øye Amundsend6551b52018-11-29 09:08:08 +0000397# Declare MPU userspace dependencies before the linker scripts to make
398# sure the order of dependencies are met
Andrew Boie41f60112019-01-31 15:53:24 -0800399if(CONFIG_USERSPACE)
Daniel Leung2117a2a2021-07-12 13:33:32 -0700400 add_custom_target(app_smem)
Daniel Leung212ec9a2019-03-10 14:20:21 -0700401 set(APP_SMEM_ALIGNED_DEP app_smem_aligned_linker)
402 set(APP_SMEM_UNALIGNED_DEP app_smem_unaligned_linker)
Håkon Øye Amundsend6551b52018-11-29 09:08:08 +0000403endif()
404
Daniel Leung11171692021-03-18 14:00:07 -0700405if(CONFIG_USERSPACE)
406 set(KOBJECT_LINKER_DEP kobject_linker)
407endif()
408
Håkon Øye Amundsena4494392018-11-29 09:14:27 +0000409get_property(TOPT GLOBAL PROPERTY TOPT)
Oleg Zhurakivskyy22119352019-03-08 11:29:33 +0200410set_ifndef( TOPT -Wl,-T) # clang doesn't pick -T for some reason and complains,
411 # while -Wl,-T works for both, gcc and clang
Håkon Øye Amundsena4494392018-11-29 09:14:27 +0000412
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200413if(CONFIG_HAVE_CUSTOM_LINKER_SCRIPT)
414 set(LINKER_SCRIPT ${APPLICATION_SOURCE_DIR}/${CONFIG_CUSTOM_LINKER_SCRIPT})
Sebastian Bøec1aa9d12018-04-12 14:48:05 +0200415 if(NOT EXISTS ${LINKER_SCRIPT})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200416 set(LINKER_SCRIPT ${CONFIG_CUSTOM_LINKER_SCRIPT})
Sebastian Bøec1aa9d12018-04-12 14:48:05 +0200417 assert_exists(CONFIG_CUSTOM_LINKER_SCRIPT)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200418 endif()
419else()
420 # Try a board specific linker file
421 set(LINKER_SCRIPT ${BOARD_DIR}/linker.ld)
422 if(NOT EXISTS ${LINKER_SCRIPT})
423 # If not available, try an SoC specific linker file
Anas Nashif96455d52018-09-04 14:34:06 -0500424 set(LINKER_SCRIPT ${SOC_DIR}/${ARCH}/${SOC_PATH}/linker.ld)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200425 endif()
426endif()
427
428if(NOT EXISTS ${LINKER_SCRIPT})
429 message(FATAL_ERROR "Could not find linker script: '${LINKER_SCRIPT}'. Corrupted configuration?")
430endif()
431
Torsten Rasmussen91709772022-02-04 10:27:13 +0100432if(DEFINED BUILD_VERSION)
433 set(build_version_argument "-DBUILD_VERSION=${BUILD_VERSION}")
434elseif(EXISTS ${ZEPHYR_BASE}/.git/index)
435 set(git_dependency ${ZEPHYR_BASE}/.git/index)
436else()
437 message(WARNING "ZEPHYR_BASE=${ZEPHYR_BASE} doesn't appear to be a git "
438 "repository, please specify '-DBUILD_VERSION=<version>'")
439endif()
440add_custom_command(
441 OUTPUT ${PROJECT_BINARY_DIR}/include/generated/version.h
442 COMMAND ${CMAKE_COMMAND} -DZEPHYR_BASE=${ZEPHYR_BASE}
443 -DOUT_FILE=${PROJECT_BINARY_DIR}/include/generated/version.h
444 ${build_version_argument}
445 -P ${ZEPHYR_BASE}/cmake/gen_version_h.cmake
446 DEPENDS ${ZEPHYR_BASE}/VERSION ${git_dependency}
447)
448add_custom_target(version_h DEPENDS ${PROJECT_BINARY_DIR}/include/generated/version.h)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200449
Sebastian Bøec23cc262018-10-09 16:03:29 +0200450# Error-out when the deprecated naming convention is found (until
451# after 1.14.0 has been released)
452foreach(path
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100453 ${BOARD_DIR}/dts.fixup
454 ${PROJECT_SOURCE_DIR}/soc/${ARCH}/${SOC_PATH}/dts.fixup
Sebastian Bøec23cc262018-10-09 16:03:29 +0200455 ${APPLICATION_SOURCE_DIR}/dts.fixup
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100456 )
Sebastian Bøec23cc262018-10-09 16:03:29 +0200457 if(EXISTS ${path})
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100458 message(FATAL_ERROR
459 "A deprecated filename has been detected. Porting is required."
460 "The file '${path}' exists, but it should be named dts_fixup.h instead."
461 "See https://github.com/zephyrproject-rtos/zephyr/pull/10352 for more details"
462 )
Sebastian Bøec23cc262018-10-09 16:03:29 +0200463 endif()
464endforeach()
465
466set_ifndef( DTS_BOARD_FIXUP_FILE ${BOARD_DIR}/dts_fixup.h)
467set_ifndef( DTS_SOC_FIXUP_FILE ${SOC_DIR}/${ARCH}/${SOC_PATH}/dts_fixup.h)
468set( DTS_APP_FIXUP_FILE ${APPLICATION_SOURCE_DIR}/dts_fixup.h)
Sebastian Bøec23cc262018-10-09 16:03:29 +0200469
Ulf Magnusson4e850062020-01-16 13:29:53 +0100470set_ifndef(DTS_CAT_OF_FIXUP_FILES ${ZEPHYR_BINARY_DIR}/include/generated/devicetree_fixups.h)
Sebastian Bøe361fdaa2019-01-28 13:40:50 +0100471
472# Concatenate the fixups into a single header file for easy
Sebastian Bøec23cc262018-10-09 16:03:29 +0200473# #include'ing
Torsten Rasmussenea082ac2022-02-07 12:53:09 +0100474file(WRITE ${DTS_CAT_OF_FIXUP_FILES}.new "/* May only be included by devicetree.h */\n\n")
Martí Bolívar21c7d422020-05-08 16:06:48 -0700475set(DISCOVERED_FIXUP_FILES)
Sebastian Bøe361fdaa2019-01-28 13:40:50 +0100476foreach(fixup_file
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100477 ${DTS_BOARD_FIXUP_FILE}
478 ${DTS_SOC_FIXUP_FILE}
479 ${DTS_APP_FIXUP_FILE}
480 ${shield_dts_fixups}
481 )
Sebastian Bøe361fdaa2019-01-28 13:40:50 +0100482 if(EXISTS ${fixup_file})
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100483 file(READ ${fixup_file} contents)
Torsten Rasmussenea082ac2022-02-07 12:53:09 +0100484 file(APPEND ${DTS_CAT_OF_FIXUP_FILES}.new "${contents}")
Martí Bolívar21c7d422020-05-08 16:06:48 -0700485 string(APPEND DISCOVERED_FIXUP_FILES "- ${fixup_file}\n")
Sebastian Bøec23cc262018-10-09 16:03:29 +0200486 endif()
487endforeach()
Torsten Rasmussenea082ac2022-02-07 12:53:09 +0100488zephyr_file_copy(${DTS_CAT_OF_FIXUP_FILES}.new ${DTS_CAT_OF_FIXUP_FILES} ONLY_IF_DIFFERENT)
489file(REMOVE ${DTS_CAT_OF_FIXUP_FILES}.new)
Sebastian Bøec23cc262018-10-09 16:03:29 +0200490
Martí Bolívar21c7d422020-05-08 16:06:48 -0700491if (DISCOVERED_FIXUP_FILES)
492 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.")
493endif()
494
Sebastian Bøe6f946e22018-01-09 10:52:57 +0100495# Unfortunately, the order in which CMakeLists.txt code is processed
496# matters so we need to be careful about how we order the processing
497# of subdirectories. One example is "Compiler flags added late in the
498# build are not exported to external build systems #5605"; when we
499# integrate with an external build system we read out all compiler
500# flags when the external project is created. So an external project
501# defined in subsys or ext will not get global flags added by drivers/
502# or tests/ as the subdirectories are ordered now.
503#
504# Another example of when the order matters is the reading and writing
505# of global properties such as ZEPHYR_LIBS or
506# GENERATED_KERNEL_OBJECT_FILES.
507#
508# Arch is placed early because it defines important compiler flags
509# that must be exported to external build systems defined in
510# e.g. subsys/.
511add_subdirectory(arch)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200512add_subdirectory(lib)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200513# We use include instead of add_subdirectory to avoid creating a new directory scope.
514# This is because source file properties are directory scoped, including the GENERATED
515# property which is set implicitly for custom command outputs
516include(misc/generated/CMakeLists.txt)
Anas Nashif3d1252f2018-09-03 15:20:14 -0500517
Erwan Gouriouba31cb52018-09-13 16:25:53 +0200518if(EXISTS ${SOC_DIR}/${ARCH}/CMakeLists.txt)
Anas Nashif96455d52018-09-04 14:34:06 -0500519 add_subdirectory(${SOC_DIR}/${ARCH} soc/${ARCH})
Anas Nashif3d1252f2018-09-03 15:20:14 -0500520else()
Anas Nashif96455d52018-09-04 14:34:06 -0500521 add_subdirectory(${SOC_DIR}/${ARCH}/${SOC_PATH} soc/${ARCH}/${SOC_PATH})
Anas Nashif3d1252f2018-09-03 15:20:14 -0500522endif()
523
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200524add_subdirectory(boards)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200525add_subdirectory(subsys)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200526add_subdirectory(drivers)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200527
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100528# Include zephyr modules generated CMake file.
Torsten Rasmussen2fc062b2020-08-24 11:01:04 +0200529foreach(module_name ${ZEPHYR_MODULE_NAMES})
530 # Note the second, binary_dir parameter requires the added
531 # subdirectory to have its own, local cmake target(s). If not then
532 # this binary_dir is created but stays empty. Object files land in
533 # the main binary dir instead.
534 # https://cmake.org/pipermail/cmake/2019-June/069547.html
Torsten Rasmussen3d880832021-01-19 12:01:38 +0100535 zephyr_string(SANITIZE TOUPPER MODULE_NAME_UPPER ${module_name})
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200536 if(NOT ${ZEPHYR_${MODULE_NAME_UPPER}_CMAKE_DIR} STREQUAL "")
537 set(ZEPHYR_CURRENT_MODULE_DIR ${ZEPHYR_${MODULE_NAME_UPPER}_MODULE_DIR})
538 set(ZEPHYR_CURRENT_CMAKE_DIR ${ZEPHYR_${MODULE_NAME_UPPER}_CMAKE_DIR})
539 add_subdirectory(${ZEPHYR_CURRENT_CMAKE_DIR} ${CMAKE_BINARY_DIR}/modules/${module_name})
540 endif()
Torsten Rasmussen2fc062b2020-08-24 11:01:04 +0200541endforeach()
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200542# Done processing modules, clear ZEPHYR_CURRENT_MODULE_DIR and ZEPHYR_CURRENT_CMAKE_DIR.
Torsten Rasmussen2fc062b2020-08-24 11:01:04 +0200543set(ZEPHYR_CURRENT_MODULE_DIR)
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200544set(ZEPHYR_CURRENT_CMAKE_DIR)
Torsten Rasmussen7e9d1bd2019-02-05 10:36:22 +0100545
Andrew Boie59601192020-05-29 13:24:51 -0700546set(syscall_list_h ${CMAKE_CURRENT_BINARY_DIR}/include/generated/syscall_list.h)
547set(syscalls_json ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls.json)
548set(struct_tags_json ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/struct_tags.json)
Sebastian Bøe13a68402017-11-20 13:03:55 +0100549
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200550# The syscalls subdirs txt file is constructed by python containing a list of folders to use for
551# dependency handling, including empty folders.
552# Windows: The list is used to specify DIRECTORY list with CMAKE_CONFIGURE_DEPENDS attribute.
553# Other OS: The list will update whenever a file is added/removed/modified and ensure a re-build.
554set(syscalls_subdirs_txt ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_subdirs.txt)
555
556# As syscalls_subdirs_txt is updated whenever a file is modified, this file can not be used for
557# monitoring of added / removed folders. A trigger file is thus used for correct dependency
558# handling. The trigger file will update when a folder is added / removed.
559set(syscalls_subdirs_trigger ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_subdirs.trigger)
560
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200561if(NOT (${CMAKE_HOST_SYSTEM_NAME} STREQUAL Windows))
562 set(syscalls_links --create-links ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_links)
563endif()
564
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200565# When running CMake it must be ensured that all dependencies are correctly acquired.
566execute_process(
567 COMMAND
568 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200569 ${ZEPHYR_BASE}/scripts/subfolder_list.py
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200570 --directory ${ZEPHYR_BASE}/include # Walk this directory
571 --out-file ${syscalls_subdirs_txt} # Write file with discovered folder
572 --trigger ${syscalls_subdirs_trigger} # Trigger file that is used for json generation
573 ${syscalls_links} # If defined, create symlinks for dependencies
574)
Carles Cufi3ad1f272019-07-18 10:38:25 +0200575file(STRINGS ${syscalls_subdirs_txt} PARSE_SYSCALLS_PATHS_DEPENDS ENCODING UTF-8)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200576
577if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL Windows)
578 # On windows only adding/removing files or folders will be reflected in depends.
579 # Hence adding a file requires CMake to re-run to add this file to the file list.
580 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${PARSE_SYSCALLS_PATHS_DEPENDS})
581
582 # Also On Windows each header file must be monitored as file modifications are not reflected
583 # on directory level.
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200584 file(GLOB_RECURSE PARSE_SYSCALLS_HEADER_DEPENDS ${ZEPHYR_BASE}/include/*.h)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200585else()
586 # The syscall parsing depends on the folders in order to detect add/removed/modified files.
587 # When a folder is removed, CMake will try to find a target that creates that dependency.
588 # This command sets up the target for CMake to find.
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200589 # Without this code, CMake will fail with the following error:
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200590 # <folder> needed by '<target>', missing and no known rule to make it
591 # when a folder is removed.
592 add_custom_command(OUTPUT ${PARSE_SYSCALLS_PATHS_DEPENDS}
593 COMMAND ${CMAKE_COMMAND} -E echo ""
594 COMMENT "Preparing syscall dependency handling"
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200595 )
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200596
597 add_custom_command(
598 OUTPUT
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200599 ${syscalls_subdirs_trigger}
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200600 COMMAND
601 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200602 ${ZEPHYR_BASE}/scripts/subfolder_list.py
603 --directory ${ZEPHYR_BASE}/include # Walk this directory
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200604 --out-file ${syscalls_subdirs_txt} # Write file with discovered folder
605 --trigger ${syscalls_subdirs_trigger} # Trigger file that is used for json generation
606 ${syscalls_links} # If defined, create symlinks for dependencies
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200607 DEPENDS ${PARSE_SYSCALLS_PATHS_DEPENDS}
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200608 )
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200609
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200610 # Ensure subdir file always exists when specifying CMake dependency.
611 if(NOT EXISTS ${syscalls_subdirs_txt})
612 file(WRITE ${syscalls_subdirs_txt} "")
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200613 endif()
614
615 # On other OS'es, modifying a file is reflected on the folder timestamp and hence detected
616 # when using depend on directory level.
617 # Thus CMake only needs to re-run when sub-directories are added / removed, which is indicated
618 # using a trigger file.
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200619 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${syscalls_subdirs_txt})
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200620endif()
621
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200622# syscall declarations are searched for in the SYSCALL_INCLUDE_DIRS
Adithya Baglodye67720b2018-07-02 14:59:19 +0530623if(CONFIG_APPLICATION_DEFINED_SYSCALL)
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200624 list(APPEND SYSCALL_INCLUDE_DIRS ${APPLICATION_SOURCE_DIR})
Adithya Baglodye67720b2018-07-02 14:59:19 +0530625endif()
626
Andrew Boiec1863872019-11-21 23:11:29 -0800627if(CONFIG_ZTEST)
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200628 list(APPEND SYSCALL_INCLUDE_DIRS ${ZEPHYR_BASE}/subsys/testsuite/ztest/include)
Andrew Boiec1863872019-11-21 23:11:29 -0800629endif()
630
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200631foreach(d ${SYSCALL_INCLUDE_DIRS})
632 list(APPEND parse_syscalls_include_args
633 --include ${d}
634 )
635endforeach()
636
Sebastian Bøe13a68402017-11-20 13:03:55 +0100637add_custom_command(
638 OUTPUT
639 ${syscalls_json}
Andrew Boie59601192020-05-29 13:24:51 -0700640 ${struct_tags_json}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100641 COMMAND
642 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200643 ${ZEPHYR_BASE}/scripts/parse_syscalls.py
Adithya Baglodye67720b2018-07-02 14:59:19 +0530644 --include ${ZEPHYR_BASE}/include # Read files from this dir
Andrew Boiefed960b2020-05-29 13:33:12 -0700645 --include ${ZEPHYR_BASE}/drivers # For net sockets
646 --include ${ZEPHYR_BASE}/subsys/net # More net sockets
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200647 ${parse_syscalls_include_args} # Read files from these dirs also
Corey Whartonccd15df2020-02-29 14:51:42 -0800648 --json-file ${syscalls_json} # Write this file
Andrew Boie59601192020-05-29 13:24:51 -0700649 --tag-struct-file ${struct_tags_json} # Write subsystem list to this file
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200650 DEPENDS ${syscalls_subdirs_trigger} ${PARSE_SYSCALLS_HEADER_DEPENDS}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100651 )
652
Joakim Andersson08d2f482020-08-04 18:26:43 +0200653add_custom_target(${SYSCALL_LIST_H_TARGET} DEPENDS ${syscall_list_h})
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +0100654
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +0100655set_property(TARGET ${SYSCALL_LIST_H_TARGET}
656 APPEND PROPERTY
657 ADDITIONAL_CLEAN_FILES
658 ${CMAKE_CURRENT_BINARY_DIR}/include/generated/syscalls
659)
660
Andrew Boiec1c54b12020-03-16 12:48:00 -0700661add_custom_target(${PARSE_SYSCALLS_TARGET}
Joakim Andersson08d2f482020-08-04 18:26:43 +0200662 DEPENDS
663 ${syscalls_json}
Joakim Anderssond268f822020-08-04 18:31:48 +0200664 ${struct_tags_json}
Joakim Andersson08d2f482020-08-04 18:26:43 +0200665 )
Andrew Boie9ff64bb2019-11-05 09:39:05 -0800666
667# 64-bit systems do not require special handling of 64-bit system call
668# parameters or return values, indicate this to the system call boilerplate
669# generation script.
670if(CONFIG_64BIT)
671 set(SYSCALL_LONG_REGISTERS_ARG --long-registers)
672endif()
673
Andy Rosscfeb07e2020-03-05 21:14:02 -0800674if(CONFIG_TIMEOUT_64BIT)
Nicolas Pitre2cdac332022-03-04 21:21:38 -0500675 set(SYSCALL_SPLIT_TIMEOUT_ARG --split-type k_timeout_t --split-type k_ticks_t)
Andy Rosscfeb07e2020-03-05 21:14:02 -0800676endif()
677
Sebastian Bøe13a68402017-11-20 13:03:55 +0100678add_custom_command(OUTPUT include/generated/syscall_dispatch.c ${syscall_list_h}
679 # Also, some files are written to include/generated/syscalls/
680 COMMAND
681 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200682 ${ZEPHYR_BASE}/scripts/gen_syscalls.py
Sebastian Bøe13a68402017-11-20 13:03:55 +0100683 --json-file ${syscalls_json} # Read this file
684 --base-output include/generated/syscalls # Write to this dir
685 --syscall-dispatch include/generated/syscall_dispatch.c # Write this file
Andrew Boie353acf42018-07-23 18:10:15 -0700686 --syscall-list ${syscall_list_h}
Andrew Boie9ff64bb2019-11-05 09:39:05 -0800687 ${SYSCALL_LONG_REGISTERS_ARG}
Andy Rosscfeb07e2020-03-05 21:14:02 -0800688 ${SYSCALL_SPLIT_TIMEOUT_ARG}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100689 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
Andrew Boiec1c54b12020-03-16 12:48:00 -0700690 DEPENDS ${PARSE_SYSCALLS_TARGET}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100691 )
692
Corey Whartonccd15df2020-02-29 14:51:42 -0800693# This is passed into all calls to the gen_kobject_list.py script.
Andrew Boie59601192020-05-29 13:24:51 -0700694set(gen_kobject_list_include_args --include ${struct_tags_json})
Corey Whartonccd15df2020-02-29 14:51:42 -0800695
Leandro Pereirac2003672018-04-04 13:50:32 -0700696set(DRV_VALIDATION ${PROJECT_BINARY_DIR}/include/generated/driver-validation.h)
697add_custom_command(
698 OUTPUT ${DRV_VALIDATION}
699 COMMAND
700 ${PYTHON_EXECUTABLE}
701 ${ZEPHYR_BASE}/scripts/gen_kobject_list.py
702 --validation-output ${DRV_VALIDATION}
Corey Whartonccd15df2020-02-29 14:51:42 -0800703 ${gen_kobject_list_include_args}
Leandro Pereirac2003672018-04-04 13:50:32 -0700704 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Corey Whartonccd15df2020-02-29 14:51:42 -0800705 DEPENDS
706 ${ZEPHYR_BASE}/scripts/gen_kobject_list.py
Andrew Boiec1c54b12020-03-16 12:48:00 -0700707 ${PARSE_SYSCALLS_TARGET}
Leandro Pereirac2003672018-04-04 13:50:32 -0700708 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
709 )
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100710add_custom_target(${DRIVER_VALIDATION_H_TARGET} DEPENDS ${DRV_VALIDATION})
Leandro Pereirac2003672018-04-04 13:50:32 -0700711
Torsten Rasmussend7862cf2020-02-12 15:42:09 +0100712include(${ZEPHYR_BASE}/cmake/kobj.cmake)
Leandro Pereira39dc7d02018-04-05 13:59:33 -0700713gen_kobj(KOBJ_INCLUDE_PATH)
Leandro Pereirac2003672018-04-04 13:50:32 -0700714
Sebastian Bøefdac7b32020-01-23 15:39:17 +0100715# Add a pseudo-target that is up-to-date when all generated headers
716# are up-to-date.
717
718add_custom_target(zephyr_generated_headers)
719add_dependencies(zephyr_generated_headers
Torsten Rasmussen91709772022-02-04 10:27:13 +0100720 offsets_h version_h
Sebastian Bøefdac7b32020-01-23 15:39:17 +0100721 )
722
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200723# Generate offsets.c.obj from offsets.c
724# Generate offsets.h from offsets.c.obj
725
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100726set(OFFSETS_LIB offsets)
727
Klaus Petersenc66cb762018-11-15 10:37:46 +0100728set(OFFSETS_C_PATH ${ARCH_DIR}/${ARCH}/core/offsets/offsets.c)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200729set(OFFSETS_H_PATH ${PROJECT_BINARY_DIR}/include/generated/offsets.h)
730
Klaus Petersen62e55e52019-02-04 12:10:57 +0100731add_library( ${OFFSETS_LIB} OBJECT ${OFFSETS_C_PATH})
Stephanos Ioannidis2d746042019-10-25 00:08:21 +0900732target_include_directories(${OFFSETS_LIB} PRIVATE
733 kernel/include
734 ${ARCH_DIR}/${ARCH}/include
735 )
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100736target_link_libraries(${OFFSETS_LIB} zephyr_interface)
Joakim Anderssond268f822020-08-04 18:31:48 +0200737add_dependencies(zephyr_interface
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100738 ${SYSCALL_LIST_H_TARGET}
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100739 ${DRIVER_VALIDATION_H_TARGET}
740 ${KOBJ_TYPES_H_TARGET}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100741 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200742
743add_custom_command(
744 OUTPUT ${OFFSETS_H_PATH}
Carles Cufi7d764b32018-01-11 15:46:44 +0100745 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/gen_offset_header.py
Klaus Petersen62e55e52019-02-04 12:10:57 +0100746 -i $<TARGET_OBJECTS:${OFFSETS_LIB}>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200747 -o ${OFFSETS_H_PATH}
Sebastian Bøe5962aab2019-08-15 14:45:59 +0200748 DEPENDS
749 ${OFFSETS_LIB}
750 $<TARGET_OBJECTS:${OFFSETS_LIB}>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200751)
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100752add_custom_target(${OFFSETS_H_TARGET} DEPENDS ${OFFSETS_H_PATH})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200753
Sebastian Bøe89516fb2017-12-01 15:25:06 +0100754zephyr_get_include_directories_for_lang(C ZEPHYR_INCLUDES)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200755
756add_subdirectory(kernel)
757
758# Read list content
759get_property(ZEPHYR_LIBS_PROPERTY GLOBAL PROPERTY ZEPHYR_LIBS)
760
761foreach(zephyr_lib ${ZEPHYR_LIBS_PROPERTY})
Torsten Rasmussend9520042021-04-14 17:11:39 +0200762 get_property(lib_type TARGET ${zephyr_lib} PROPERTY TYPE)
Torsten Rasmussend9520042021-04-14 17:11:39 +0200763 # To prevent CMake failure when a driver is enabled, for example: REGULATOR=y
764 # we disable any Zephyr libraries without sources and adds the `empty_file.c`.
765 if(${lib_type} STREQUAL STATIC_LIBRARY
Torsten Rasmussend9520042021-04-14 17:11:39 +0200766 AND NOT ${zephyr_lib} STREQUAL app
767 )
Torsten Rasmussen25578be2021-04-23 09:09:49 +0200768 get_property(source_list TARGET ${zephyr_lib} PROPERTY SOURCES)
769 get_property(lib_imported TARGET ${zephyr_lib} PROPERTY IMPORTED)
770 if(NOT source_list
771 AND NOT ${lib_imported}
Torsten Rasmussend9520042021-04-14 17:11:39 +0200772 )
Torsten Rasmussen153196b2021-09-06 16:42:21 +0200773 get_property(allow_empty TARGET ${zephyr_lib} PROPERTY ALLOW_EMPTY)
774 if(NOT "${allow_empty}")
775 message(WARNING
776 "No SOURCES given to Zephyr library: ${zephyr_lib}\nExcluding target from build."
777 )
778 endif()
Torsten Rasmussen25578be2021-04-23 09:09:49 +0200779 target_sources(${zephyr_lib} PRIVATE ${ZEPHYR_BASE}/misc/empty_file.c)
780 set_property(TARGET ${zephyr_lib} PROPERTY EXCLUDE_FROM_ALL TRUE)
781 list(REMOVE_ITEM ZEPHYR_LIBS_PROPERTY ${zephyr_lib})
782 continue()
783 endif()
Torsten Rasmussend9520042021-04-14 17:11:39 +0200784 endif()
Torsten Rasmussen25578be2021-04-23 09:09:49 +0200785
786 # TODO: Could this become an INTERFACE property of zephyr_interface?
787 add_dependencies(${zephyr_lib} zephyr_generated_headers)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200788endforeach()
789
790get_property(OUTPUT_FORMAT GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT)
791
Adithya Baglody62e152a2018-11-13 15:34:02 +0530792if (CONFIG_CODE_DATA_RELOCATION)
793 set(CODE_RELOCATION_DEP code_relocation_source_lib)
794endif() # CONFIG_CODE_DATA_RELOCATION
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100795
Daniel Leungc7459952021-03-19 12:09:05 -0700796# Give the linker script targets all of the include directories so
797# that cmake can successfully find the linker scripts' header
Sebastian Bøeb1ab5012017-12-14 13:03:23 +0100798# dependencies.
799zephyr_get_include_directories_for_lang(C
800 ZEPHYR_INCLUDE_DIRS
801 STRIP_PREFIX # Don't use a -I prefix
802 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200803
Morten Priessa846e722021-04-21 09:06:02 +0200804if(CONFIG_HAS_DTS)
Jordan Yates28b2e552021-10-20 20:19:28 +1000805 # dev_handles.c is generated from ${ZEPHYR_LINK_STAGE_EXECUTABLE} by
Morten Priessa846e722021-04-21 09:06:02 +0200806 # gen_handles.py
807 add_custom_command(
808 OUTPUT dev_handles.c
809 COMMAND
810 ${PYTHON_EXECUTABLE}
811 ${ZEPHYR_BASE}/scripts/gen_handles.py
812 --output-source dev_handles.c
Jordan Yates28b2e552021-10-20 20:19:28 +1000813 --kernel $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
Morten Priessa846e722021-04-21 09:06:02 +0200814 --zephyr-base ${ZEPHYR_BASE}
Torsten Rasmussenc9804d22021-05-21 21:34:58 +0200815 --start-symbol "$<TARGET_PROPERTY:linker,devices_start_symbol>"
Torsten Rasmussen6d72d912022-01-05 11:25:26 +0100816 VERBATIM
Jordan Yates28b2e552021-10-20 20:19:28 +1000817 DEPENDS ${ZEPHYR_LINK_STAGE_EXECUTABLE}
Morten Priessa846e722021-04-21 09:06:02 +0200818 )
Jordan Yates28b2e552021-10-20 20:19:28 +1000819 set_property(GLOBAL APPEND PROPERTY GENERATED_APP_SOURCE_FILES dev_handles.c)
820
821 # gen_handles runs on `__device_handles_pass1` so pass this info to the linker script generator
822 list(APPEND LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE "LINKER_DEVICE_HANDLES_PASS1")
Morten Priessa846e722021-04-21 09:06:02 +0200823endif()
Peter Bigotd554d342020-06-30 10:05:35 -0500824
Adithya Baglody62e152a2018-11-13 15:34:02 +0530825if(CONFIG_CODE_DATA_RELOCATION)
Mark Ruvald Pedersen86a3e8f2019-05-03 10:33:03 +0200826 # @Intent: Linker script to relocate .text, data and .bss sections
827 toolchain_ld_relocation()
Adithya Baglody62e152a2018-11-13 15:34:02 +0530828endif()
829
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530830if(CONFIG_USERSPACE)
Torsten Rasmussene37d9e62020-11-20 18:39:30 +0100831 zephyr_get_compile_options_for_lang_as_string(C compiler_flags_priv)
Torsten Rasmussene0758c32020-08-21 19:13:53 +0200832 string(REPLACE "$<TARGET_PROPERTY:compiler,coverage>" ""
833 NO_COVERAGE_FLAGS "${compiler_flags_priv}"
834 )
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530835
Carles Cufi7d764b32018-01-11 15:46:44 +0100836 set(GEN_KOBJ_LIST ${ZEPHYR_BASE}/scripts/gen_kobject_list.py)
837 set(PROCESS_GPERF ${ZEPHYR_BASE}/scripts/process_gperf.py)
Jordan Yates28b2e552021-10-20 20:19:28 +1000838endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200839
Jordan Yates28b2e552021-10-20 20:19:28 +1000840get_property(CSTD GLOBAL PROPERTY CSTD)
841set_ifndef(CSTD c99)
842
843# @Intent: Obtain compiler specific flag for specifying the c standard
844zephyr_compile_options(
845 $<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,cstd>${CSTD}>
846)
847set(CMAKE_C_COMPILE_FEATURES ${compile_features_${CSTD}} PARENT_SCOPE)
848
849# @Intent: Configure linker scripts, i.e. generate linker scripts with variables substituted
850toolchain_ld_configure_files()
851
852if(CONFIG_USERSPACE)
853 set(APP_SMEM_ALIGNED_LD "${PROJECT_BINARY_DIR}/include/generated/app_smem_aligned.ld")
854 set(APP_SMEM_UNALIGNED_LD "${PROJECT_BINARY_DIR}/include/generated/app_smem_unaligned.ld")
855
856 if(CONFIG_LINKER_USE_PINNED_SECTION)
857 set(APP_SMEM_PINNED_ALIGNED_LD
858 "${PROJECT_BINARY_DIR}/include/generated/app_smem_pinned_aligned.ld")
859 set(APP_SMEM_PINNED_UNALIGNED_LD
860 "${PROJECT_BINARY_DIR}/include/generated/app_smem_pinned_unaligned.ld")
861
862 if(NOT CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT)
863 # The libc partition may hold symbols that are required during boot process,
864 # for example, stack guard (if enabled). So the libc partition must be pinned
865 # if not sections are in physical memory at boot, as the paging mechanism is
866 # only initialized post-kernel.
867 set_property(TARGET app_smem APPEND PROPERTY pinned_partitions "z_libc_partition")
868 endif()
869
870 get_property(APP_SMEM_PINNED_PARTITION_LIST TARGET app_smem PROPERTY pinned_partitions)
871 if(APP_SMEM_PINNED_PARTITION_LIST)
872 list(JOIN APP_SMEM_PINNED_PARTITION_LIST "," APP_SMEM_PINNED_PARTITION_LIST_ARG_CSL)
873 set(APP_SMEM_PINNED_PARTITION_LIST_ARG "--pinpartitions=${APP_SMEM_PINNED_PARTITION_LIST_ARG_CSL}")
874 endif()
875 endif()
876
877 set(OBJ_FILE_DIR "${PROJECT_BINARY_DIR}/../")
878
879 if(CONFIG_NEWLIB_LIBC)
880 set(NEWLIB_PART -l libc.a z_libc_partition)
881 endif()
882 if(CONFIG_NEWLIB_LIBC_NANO)
883 set(NEWLIB_PART -l libc_nano.a z_libc_partition)
884 endif()
885
886 add_custom_command(
887 OUTPUT ${APP_SMEM_UNALIGNED_LD} ${APP_SMEM_PINNED_UNALIGNED_LD}
888 COMMAND ${PYTHON_EXECUTABLE}
889 ${ZEPHYR_BASE}/scripts/gen_app_partitions.py
Torsten Rasmussenf643b8b2021-11-24 15:35:47 +0100890 -f ${CMAKE_BINARY_DIR}/compile_commands.json
Jordan Yates28b2e552021-10-20 20:19:28 +1000891 -o ${APP_SMEM_UNALIGNED_LD}
892 $<$<BOOL:${APP_SMEM_PINNED_UNALIGNED_LD}>:--pinoutput=${APP_SMEM_PINNED_UNALIGNED_LD}>
893 ${APP_SMEM_PINNED_PARTITION_LIST_ARG}
894 ${NEWLIB_PART}
895 $<TARGET_PROPERTY:zephyr_property_target,COMPILE_OPTIONS>
896 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
897 DEPENDS
898 kernel
899 ${ZEPHYR_LIBS_PROPERTY}
900 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
901 COMMAND_EXPAND_LISTS
902 COMMENT "Generating app_smem_unaligned linker section"
903 )
904
905 add_custom_target(
906 ${APP_SMEM_ALIGNED_DEP}
907 DEPENDS
908 ${APP_SMEM_ALIGNED_LD}
909 ${APP_SMEM_PINNED_ALIGNED_LD}
910 )
911
912 add_custom_target(
913 ${APP_SMEM_UNALIGNED_DEP}
914 DEPENDS
915 ${APP_SMEM_UNALIGNED_LD}
916 ${APP_SMEM_PINNED_UNALIGNED_LD}
917 )
918
919 set(APP_SMEM_UNALIGNED_LIB app_smem_unaligned_output_obj_renamed_lib)
920 list(APPEND LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE "LINKER_APP_SMEM_UNALIGNED")
921endif()
922
923if (CONFIG_USERSPACE)
924 add_custom_command(
925 OUTPUT ${APP_SMEM_ALIGNED_LD} ${APP_SMEM_PINNED_ALIGNED_LD}
926 COMMAND ${PYTHON_EXECUTABLE}
927 ${ZEPHYR_BASE}/scripts/gen_app_partitions.py
928 -e $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
929 -o ${APP_SMEM_ALIGNED_LD}
930 $<$<BOOL:${APP_SMEM_PINNED_ALIGNED_LD}>:--pinoutput=${APP_SMEM_PINNED_ALIGNED_LD}>
931 ${APP_SMEM_PINNED_PARTITION_LIST_ARG}
932 ${NEWLIB_PART}
933 $<TARGET_PROPERTY:zephyr_property_target,COMPILE_OPTIONS>
934 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
935 DEPENDS
936 kernel
937 ${ZEPHYR_LIBS_PROPERTY}
938 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
939 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
940 COMMAND_EXPAND_LISTS
941 COMMENT "Generating app_smem_aligned linker section"
942 )
943endif()
944
945if(CONFIG_USERSPACE)
946 # This CONFIG_USERSPACE block is to create place holders to reserve space
947 # for the gperf generated structures for zephyr_prebuilt.elf.
948 # These place holders are there so that the placement of kobjects would be
949 # the same between linking zephyr_prebuilt.elf and zephyr.elf, as
950 # the gperf hash table is hashed on the addresses of kobjects.
951 # The placeholders are generated from app_smem_unaligned_prebuilt.elf.
952
953 set(KOBJECT_PREBUILT_HASH_LIST kobject_prebuilt_hash.gperf)
954 set(KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE kobject_prebuilt_hash_preprocessed.c)
955 set(KOBJECT_PREBUILT_HASH_OUTPUT_SRC kobject_prebuilt_hash.c)
956
957 add_custom_command(
958 OUTPUT ${KOBJECT_PREBUILT_HASH_LIST}
959 COMMAND
960 ${PYTHON_EXECUTABLE}
961 ${GEN_KOBJ_LIST}
962 --kernel $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
963 --gperf-output ${KOBJECT_PREBUILT_HASH_LIST}
964 ${gen_kobject_list_include_args}
965 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
966 DEPENDS
967 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
968 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
969 )
970 add_custom_target(
971 kobj_prebuilt_hash_list
972 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_LIST}
973 )
974
975 add_custom_command(
976 OUTPUT ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
977 COMMAND
978 ${GPERF}
979 --output-file ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
980 --multiple-iterations 10
981 ${KOBJECT_PREBUILT_HASH_LIST}
982 DEPENDS kobj_prebuilt_hash_list ${KOBJECT_PREBUILT_HASH_LIST}
983 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
984 )
985 add_custom_target(
986 kobj_prebuilt_hash_output_src_pre
987 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
988 )
989
990 add_custom_command(
991 OUTPUT ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
992 COMMAND
993 ${PYTHON_EXECUTABLE}
994 ${PROCESS_GPERF}
995 -i ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
996 -o ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
997 -p "struct z_object"
998 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
999 DEPENDS kobj_prebuilt_hash_output_src_pre ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
1000 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1001 )
1002 add_custom_target(
1003 kobj_prebuilt_hash_output_src
1004 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1005 )
1006
1007 add_library(
1008 kobj_prebuilt_hash_output_lib
1009 OBJECT ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1010 )
1011
1012 set_source_files_properties(${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1013 PROPERTIES COMPILE_FLAGS
1014 "${NO_COVERAGE_FLAGS} -fno-function-sections -fno-data-sections")
1015
1016 target_compile_definitions(kobj_prebuilt_hash_output_lib
1017 PRIVATE $<TARGET_PROPERTY:zephyr_interface,INTERFACE_COMPILE_DEFINITIONS>
1018 )
1019
1020 target_include_directories(kobj_prebuilt_hash_output_lib
1021 PUBLIC $<TARGET_PROPERTY:zephyr_interface,INTERFACE_INCLUDE_DIRECTORIES>
1022 )
1023
1024 target_include_directories(kobj_prebuilt_hash_output_lib SYSTEM
1025 PUBLIC $<TARGET_PROPERTY:zephyr_interface,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
1026 )
1027
1028 set(KOBJECT_LINKER_HEADER_DATA "${PROJECT_BINARY_DIR}/include/generated/linker-kobject-prebuilt-data.h")
1029
1030 add_custom_command(
1031 OUTPUT ${KOBJECT_LINKER_HEADER_DATA}
1032 COMMAND
1033 ${PYTHON_EXECUTABLE}
1034 ${ZEPHYR_BASE}/scripts/gen_kobject_placeholders.py
1035 --object $<TARGET_OBJECTS:kobj_prebuilt_hash_output_lib>
1036 --outdir ${PROJECT_BINARY_DIR}/include/generated
1037 --datapct ${CONFIG_KOBJECT_DATA_AREA_RESERVE_EXTRA_PERCENT}
1038 --rodata ${CONFIG_KOBJECT_RODATA_AREA_EXTRA_BYTES}
1039 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1040 DEPENDS
Torsten Rasmussen04543512022-02-03 15:14:44 +01001041 kobj_prebuilt_hash_output_lib $<TARGET_OBJECTS:kobj_prebuilt_hash_output_lib>
Jordan Yates28b2e552021-10-20 20:19:28 +10001042 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1043 )
1044
1045 add_custom_target(
1046 ${KOBJECT_LINKER_DEP}
1047 DEPENDS
1048 ${KOBJECT_LINKER_HEADER_DATA}
1049 )
1050endif()
1051
1052if(CONFIG_USERSPACE OR CONFIG_HAS_DTS)
1053 configure_linker_script(
1054 ${ZEPHYR_CURRENT_LINKER_CMD}
1055 "${LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE}"
1056 ${CODE_RELOCATION_DEP}
1057 ${APP_SMEM_UNALIGNED_DEP}
1058 ${APP_SMEM_UNALIGNED_LD}
1059 ${APP_SMEM_PINNED_UNALIGNED_LD}
1060 zephyr_generated_headers
1061 )
1062
1063 add_custom_target(
1064 linker_zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS}_script
1065 DEPENDS
1066 ${ZEPHYR_CURRENT_LINKER_CMD}
1067 )
1068
1069 set_property(TARGET
1070 linker_zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS}_script
1071 PROPERTY INCLUDE_DIRECTORIES
1072 ${ZEPHYR_INCLUDE_DIRS}
1073 )
1074
1075 add_executable(${ZEPHYR_LINK_STAGE_EXECUTABLE} misc/empty_file.c)
1076 toolchain_ld_link_elf(
1077 TARGET_ELF ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1078 OUTPUT_MAP ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
1079 LIBRARIES_PRE_SCRIPT ""
1080 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD}
1081 LIBRARIES_POST_SCRIPT ""
1082 DEPENDENCIES ${CODE_RELOCATION_DEP}
1083 )
1084 target_byproducts(TARGET ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1085 BYPRODUCTS ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
1086 )
1087 set_property(TARGET ${ZEPHYR_LINK_STAGE_EXECUTABLE} PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD})
1088 add_dependencies(${ZEPHYR_LINK_STAGE_EXECUTABLE} linker_zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS}_script ${OFFSETS_LIB})
1089
1090 math(EXPR ZEPHYR_CURRENT_LINKER_PASS "1 + ${ZEPHYR_CURRENT_LINKER_PASS}")
1091endif()
1092
1093set(ZEPHYR_CURRENT_LINKER_CMD linker_zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS}.cmd)
1094set(ZEPHYR_LINK_STAGE_EXECUTABLE zephyr_pre${ZEPHYR_CURRENT_LINKER_PASS})
1095list(APPEND LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE "LINKER_ZEPHYR_PREBUILT")
1096
1097if(CONFIG_GEN_ISR_TABLES)
1098 if(CONFIG_GEN_SW_ISR_TABLE)
1099 list(APPEND GEN_ISR_TABLE_EXTRA_ARG --sw-isr-table)
1100 endif()
1101
1102 if(CONFIG_GEN_IRQ_VECTOR_TABLE)
1103 list(APPEND GEN_ISR_TABLE_EXTRA_ARG --vector-table)
1104 endif()
1105
1106 # isr_tables.c is generated from ${ZEPHYR_LINK_STAGE_EXECUTABLE} by
1107 # gen_isr_tables.py
1108 add_custom_command(
1109 OUTPUT isr_tables.c isrList.bin
1110 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1111 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1112 $<TARGET_PROPERTY:bintools,elfconvert_flag_intarget>${OUTPUT_FORMAT}
1113 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>binary
1114 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_only>.intList
1115 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>$<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
1116 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>isrList.bin
1117 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
1118 COMMAND ${PYTHON_EXECUTABLE}
1119 ${ZEPHYR_BASE}/arch/common/gen_isr_tables.py
1120 --output-source isr_tables.c
1121 --kernel $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
1122 --intlist isrList.bin
1123 $<$<BOOL:${CONFIG_BIG_ENDIAN}>:--big-endian>
1124 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--debug>
1125 ${GEN_ISR_TABLE_EXTRA_ARG}
1126 DEPENDS ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1127 COMMAND_EXPAND_LISTS
1128 )
1129 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_SOURCE_FILES isr_tables.c)
1130endif()
1131
1132if(CONFIG_USERSPACE)
Daniel Leung28c35122021-03-18 12:02:19 -07001133 set(KOBJECT_HASH_LIST kobject_hash.gperf)
1134 set(KOBJECT_HASH_OUTPUT_SRC_PRE kobject_hash_preprocessed.c)
1135 set(KOBJECT_HASH_OUTPUT_SRC kobject_hash.c)
Daniel Leung28c35122021-03-18 12:02:19 -07001136 set(KOBJECT_HASH_OUTPUT_OBJ_RENAMED kobject_hash_renamed.o)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001137
1138 # Essentially what we are doing here is extracting some information
1139 # out of the nearly finished elf file, generating the source code
1140 # for a hash table based on that information, and then compiling and
1141 # linking the hash table back into a now even more nearly finished
Marc Herbert4a10eea2019-04-16 15:39:45 -07001142 # elf file. More information in gen_kobject_list.py --help.
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001143
1144 # Use the script GEN_KOBJ_LIST to scan the kernel binary's
Jordan Yates28b2e552021-10-20 20:19:28 +10001145 # (${ZEPHYR_LINK_STAGE_EXECUTABLE}) DWARF information to produce a table of kernel
Daniel Leung28c35122021-03-18 12:02:19 -07001146 # objects (KOBJECT_HASH_LIST) which we will then pass to gperf
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001147 add_custom_command(
Daniel Leung28c35122021-03-18 12:02:19 -07001148 OUTPUT ${KOBJECT_HASH_LIST}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001149 COMMAND
1150 ${PYTHON_EXECUTABLE}
1151 ${GEN_KOBJ_LIST}
Jordan Yates28b2e552021-10-20 20:19:28 +10001152 --kernel $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
Daniel Leung28c35122021-03-18 12:02:19 -07001153 --gperf-output ${KOBJECT_HASH_LIST}
Corey Whartonccd15df2020-02-29 14:51:42 -08001154 ${gen_kobject_list_include_args}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001155 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Corey Whartonccd15df2020-02-29 14:51:42 -08001156 DEPENDS
Jordan Yates28b2e552021-10-20 20:19:28 +10001157 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001158 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1159 )
Daniel Leung28c35122021-03-18 12:02:19 -07001160 add_custom_target(
1161 kobj_hash_list
1162 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_LIST}
1163 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001164
Daniel Leung28c35122021-03-18 12:02:19 -07001165 # Use gperf to generate C code (KOBJECT_HASH_OUTPUT_SRC_PRE) which implements a
1166 # perfect hashtable based on KOBJECT_HASH_LIST
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001167 add_custom_command(
Torsten Rasmussen04543512022-02-03 15:14:44 +01001168 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC_PRE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001169 COMMAND
1170 ${GPERF}
Daniel Leung28c35122021-03-18 12:02:19 -07001171 --output-file ${KOBJECT_HASH_OUTPUT_SRC_PRE}
Daniel Leung11171692021-03-18 14:00:07 -07001172 --multiple-iterations 10
Daniel Leung28c35122021-03-18 12:02:19 -07001173 ${KOBJECT_HASH_LIST}
1174 DEPENDS kobj_hash_list ${KOBJECT_HASH_LIST}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001175 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1176 )
Daniel Leung28c35122021-03-18 12:02:19 -07001177 add_custom_target(
1178 kobj_hash_output_src_pre
1179 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC_PRE}
1180 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001181
1182 # For our purposes the code/data generated by gperf is not optimal.
1183 #
Daniel Leung28c35122021-03-18 12:02:19 -07001184 # The script PROCESS_GPERF creates a new c file KOBJECT_HASH_OUTPUT_SRC based on
1185 # KOBJECT_HASH_OUTPUT_SRC_PRE to greatly reduce the amount of code/data generated
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001186 # since we know we are always working with pointer values
1187 add_custom_command(
Daniel Leung28c35122021-03-18 12:02:19 -07001188 OUTPUT ${KOBJECT_HASH_OUTPUT_SRC}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001189 COMMAND
Sebastian Bøe1b600702018-06-21 14:34:42 +02001190 ${PYTHON_EXECUTABLE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001191 ${PROCESS_GPERF}
Daniel Leung28c35122021-03-18 12:02:19 -07001192 -i ${KOBJECT_HASH_OUTPUT_SRC_PRE}
1193 -o ${KOBJECT_HASH_OUTPUT_SRC}
Andrew Boie2dc2ecf2020-03-11 07:13:07 -07001194 -p "struct z_object"
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001195 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Torsten Rasmussen04543512022-02-03 15:14:44 +01001196 DEPENDS kobj_hash_output_src_pre ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC_PRE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001197 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1198 )
Daniel Leung28c35122021-03-18 12:02:19 -07001199 add_custom_target(
1200 kobj_hash_output_src
1201 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC}
1202 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001203
1204 # We need precise control of where generated text/data ends up in the final
1205 # kernel image. Disable function/data sections and use objcopy to move
1206 # generated data into special section names
Daniel Leung28c35122021-03-18 12:02:19 -07001207 add_library(
1208 kobj_hash_output_lib
Torsten Rasmussen47304d42021-11-02 12:06:05 +01001209 OBJECT ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001210 )
1211
Daniel Leung28c35122021-03-18 12:02:19 -07001212 set_source_files_properties(${KOBJECT_HASH_OUTPUT_SRC}
1213 PROPERTIES COMPILE_FLAGS
Andrew Boiea5148982019-03-14 17:04:11 -07001214 "${NO_COVERAGE_FLAGS} -fno-function-sections -fno-data-sections")
Adithya Baglody4b3c7b32018-11-21 14:31:56 +05301215
Torsten Rasmussen47304d42021-11-02 12:06:05 +01001216 target_compile_definitions(kobj_hash_output_lib
1217 PRIVATE $<TARGET_PROPERTY:zephyr_interface,INTERFACE_COMPILE_DEFINITIONS>
1218 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001219
Torsten Rasmussen47304d42021-11-02 12:06:05 +01001220 target_include_directories(kobj_hash_output_lib
1221 PUBLIC $<TARGET_PROPERTY:zephyr_interface,INTERFACE_INCLUDE_DIRECTORIES>
1222 )
Adithya Baglody4b3c7b32018-11-21 14:31:56 +05301223
Torsten Rasmussen47304d42021-11-02 12:06:05 +01001224 target_include_directories(kobj_hash_output_lib SYSTEM
1225 PUBLIC $<TARGET_PROPERTY:zephyr_interface,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
Daniel Leung28c35122021-03-18 12:02:19 -07001226 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001227
1228 add_custom_command(
Daniel Leung28c35122021-03-18 12:02:19 -07001229 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001230 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1231 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1232 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.data=.kobject_data.data
Jim Shu70e1dee2021-07-08 01:11:34 +08001233 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.sdata=.kobject_data.sdata
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001234 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.text=.kobject_data.text
1235 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.rodata=.kobject_data.rodata
Torsten Rasmussen47304d42021-11-02 12:06:05 +01001236 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>$<TARGET_OBJECTS:kobj_hash_output_lib>
Daniel Leung28c35122021-03-18 12:02:19 -07001237 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001238 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Torsten Rasmussen04543512022-02-03 15:14:44 +01001239 DEPENDS kobj_hash_output_lib $<TARGET_OBJECTS:kobj_hash_output_lib>
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001240 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001241 COMMAND_EXPAND_LISTS
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001242 )
Daniel Leung28c35122021-03-18 12:02:19 -07001243 add_custom_target(
1244 kobj_hash_output_obj_renamed
1245 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
1246 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001247
Daniel Leung28c35122021-03-18 12:02:19 -07001248 add_library(kobj_hash_output_obj_renamed_lib STATIC IMPORTED GLOBAL)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001249 set_property(
Daniel Leung28c35122021-03-18 12:02:19 -07001250 TARGET kobj_hash_output_obj_renamed_lib
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001251 PROPERTY
Daniel Leung28c35122021-03-18 12:02:19 -07001252 IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001253 )
1254 add_dependencies(
Daniel Leung28c35122021-03-18 12:02:19 -07001255 kobj_hash_output_obj_renamed_lib
1256 kobj_hash_output_obj_renamed
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001257 )
1258
Daniel Leung28c35122021-03-18 12:02:19 -07001259 set_property(
1260 GLOBAL APPEND PROPERTY
1261 GENERATED_KERNEL_OBJECT_FILES kobj_hash_output_obj_renamed_lib
1262 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001263endif()
1264
Daniel Leungc7459952021-03-19 12:09:05 -07001265configure_linker_script(
Jordan Yates28b2e552021-10-20 20:19:28 +10001266 ${ZEPHYR_CURRENT_LINKER_CMD}
1267 "${LINKER_PASS_${ZEPHYR_CURRENT_LINKER_PASS}_DEFINE}"
Daniel Leungc7459952021-03-19 12:09:05 -07001268 ${APP_SMEM_ALIGNED_DEP}
Daniel Leung11171692021-03-18 14:00:07 -07001269 ${KOBJECT_LINKER_DEP}
Daniel Leungc7459952021-03-19 12:09:05 -07001270 ${CODE_RELOCATION_DEP}
1271 zephyr_generated_headers
1272 )
1273
1274add_custom_target(
1275 linker_zephyr_prebuilt_script_target
1276 DEPENDS
Jordan Yates28b2e552021-10-20 20:19:28 +10001277 ${ZEPHYR_CURRENT_LINKER_CMD}
Daniel Leungc7459952021-03-19 12:09:05 -07001278 )
1279
1280set_property(TARGET
1281 linker_zephyr_prebuilt_script_target
1282 PROPERTY INCLUDE_DIRECTORIES
1283 ${ZEPHYR_INCLUDE_DIRS}
1284 )
1285
Jordan Yates28b2e552021-10-20 20:19:28 +10001286# Read global variables into local variables
1287get_property(GASF GLOBAL PROPERTY GENERATED_APP_SOURCE_FILES)
1288get_property(GKOF GLOBAL PROPERTY GENERATED_KERNEL_OBJECT_FILES)
1289get_property(GKSF GLOBAL PROPERTY GENERATED_KERNEL_SOURCE_FILES)
1290
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001291# FIXME: Is there any way to get rid of empty_file.c?
Jordan Yates28b2e552021-10-20 20:19:28 +10001292add_executable( ${ZEPHYR_LINK_STAGE_EXECUTABLE} misc/empty_file.c ${GASF})
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001293toolchain_ld_link_elf(
Jordan Yates28b2e552021-10-20 20:19:28 +10001294 TARGET_ELF ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1295 OUTPUT_MAP ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001296 LIBRARIES_PRE_SCRIPT ""
Jordan Yates28b2e552021-10-20 20:19:28 +10001297 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD}
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001298 DEPENDENCIES ${CODE_RELOCATION_DEP}
1299)
Jordan Yates28b2e552021-10-20 20:19:28 +10001300target_byproducts(TARGET ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1301 BYPRODUCTS ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +01001302)
Daniel Leungc7459952021-03-19 12:09:05 -07001303set_property(TARGET
Jordan Yates28b2e552021-10-20 20:19:28 +10001304 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
1305 PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD}
Daniel Leungc7459952021-03-19 12:09:05 -07001306 )
1307add_dependencies(
Jordan Yates28b2e552021-10-20 20:19:28 +10001308 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
Daniel Leungc7459952021-03-19 12:09:05 -07001309 linker_zephyr_prebuilt_script_target
1310 ${OFFSETS_LIB}
1311 )
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +02001312
Marc Herbert498b4942019-04-16 23:30:52 -07001313set(generated_kernel_files ${GKSF} ${GKOF})
1314if(NOT generated_kernel_files)
1315 # Use the prebuilt elf as the final elf since we don't have a
1316 # generation stage.
Jordan Yates28b2e552021-10-20 20:19:28 +10001317 set(logical_target_for_zephyr_elf ${ZEPHYR_LINK_STAGE_EXECUTABLE})
Marc Herbert498b4942019-04-16 23:30:52 -07001318else()
Daniel Leungcdd02a92021-03-19 12:18:52 -07001319 # The final linker pass uses the same source linker script of the
1320 # previous passes, but this time with a different output
1321 # file and preprocessed with the define LINKER_ZEPHYR_FINAL.
Mark Ruvald Pedersen4c811972019-04-29 17:16:54 +02001322 configure_linker_script(
Daniel Leungcdd02a92021-03-19 12:18:52 -07001323 linker.cmd
Torsten Rasmussen1fa3f152021-11-01 12:53:28 +01001324 "LINKER_ZEPHYR_FINAL"
Sebastian Bøe2a963122019-02-08 15:49:57 +01001325 ${CODE_RELOCATION_DEP}
Jordan Yates28b2e552021-10-20 20:19:28 +10001326 ${ZEPHYR_LINK_STAGE_EXECUTABLE}
Sebastian Bøefdac7b32020-01-23 15:39:17 +01001327 zephyr_generated_headers
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +01001328 )
Andy Grosse8860fe2018-02-01 01:12:32 -06001329
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001330 add_custom_target(
Daniel Leungcdd02a92021-03-19 12:18:52 -07001331 linker_zephyr_final_script_target
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001332 DEPENDS
Daniel Leungcdd02a92021-03-19 12:18:52 -07001333 linker.cmd
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001334 )
Sebastian Bøeb1ab5012017-12-14 13:03:23 +01001335 set_property(TARGET
Daniel Leungcdd02a92021-03-19 12:18:52 -07001336 linker_zephyr_final_script_target
Sebastian Bøeb1ab5012017-12-14 13:03:23 +01001337 PROPERTY INCLUDE_DIRECTORIES
1338 ${ZEPHYR_INCLUDE_DIRS}
1339 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001340
Jordan Yates8d932172021-10-20 20:09:39 +10001341 add_executable( ${ZEPHYR_FINAL_EXECUTABLE} misc/empty_file.c ${GASF} ${GKSF})
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001342 toolchain_ld_link_elf(
1343 TARGET_ELF ${ZEPHYR_FINAL_EXECUTABLE}
Marc Herbert0370c9b2019-06-13 16:15:44 -07001344 OUTPUT_MAP ${PROJECT_BINARY_DIR}/${ZEPHYR_FINAL_EXECUTABLE}.map
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001345 LIBRARIES_PRE_SCRIPT ${GKOF}
Daniel Leungcdd02a92021-03-19 12:18:52 -07001346 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/linker.cmd
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001347 LIBRARIES_POST_SCRIPT ""
1348 DEPENDENCIES ${CODE_RELOCATION_DEP}
1349 )
Daniel Leungcdd02a92021-03-19 12:18:52 -07001350 set_property(TARGET ${ZEPHYR_FINAL_EXECUTABLE} PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker.cmd)
1351 add_dependencies( ${ZEPHYR_FINAL_EXECUTABLE} linker_zephyr_final_script_target)
Mark Ruvald Pedersen37d49472019-05-07 15:20:20 +02001352
1353 # Use the pass2 elf as the final elf
1354 set(logical_target_for_zephyr_elf ${ZEPHYR_FINAL_EXECUTABLE})
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001355endif()
1356
Sebastian Bøe0fc39342018-10-16 13:25:04 +02001357# Export the variable to the application's scope to allow the
1358# application to know what the name of the final elf target is.
1359set(logical_target_for_zephyr_elf ${logical_target_for_zephyr_elf} PARENT_SCOPE)
1360
Marc Herbert0370c9b2019-06-13 16:15:44 -07001361# Override the base name of the last, "logical" .elf output (and last .map) so:
Marc Herbert498b4942019-04-16 23:30:52 -07001362# 1. it doesn't depend on the number of passes above and the
1363# post_build_commands below can always find it no matter which is it;
1364# 2. it can be defined in Kconfig
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001365set_target_properties(${logical_target_for_zephyr_elf} PROPERTIES OUTPUT_NAME ${KERNEL_NAME})
1366
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001367set(post_build_commands "")
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001368set(post_build_byproducts "")
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001369
Marc Herbert0370c9b2019-06-13 16:15:44 -07001370list(APPEND
1371 post_build_commands
1372 COMMAND
Torsten Rasmussen4df38c72020-06-11 10:48:06 +02001373 ${CMAKE_COMMAND} -E rename ${logical_target_for_zephyr_elf}.map ${KERNEL_MAP_NAME}
Marc Herbert0370c9b2019-06-13 16:15:44 -07001374)
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +01001375list(APPEND post_build_byproducts ${KERNEL_MAP_NAME})
Marc Herbert0370c9b2019-06-13 16:15:44 -07001376
Håkon Øye Amundsenc086b932018-11-26 09:47:16 +00001377if(NOT CONFIG_BUILD_NO_GAP_FILL)
1378 # Use ';' as separator to get proper space in resulting command.
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001379 set(GAP_FILL "$<TARGET_PROPERTY:bintools,elfconvert_flag_gapfill>0xff")
Håkon Øye Amundsenc086b932018-11-26 09:47:16 +00001380endif()
1381
Danny Oerndrupc41e7122019-07-18 15:16:39 +02001382if(CONFIG_OUTPUT_PRINT_MEMORY_USAGE)
Torsten Rasmussend537be02021-02-02 21:06:11 +01001383 target_link_libraries(${logical_target_for_zephyr_elf} $<TARGET_PROPERTY:linker,memusage>)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001384
1385 get_property(memusage_build_command TARGET bintools PROPERTY memusage_command)
1386 if(memusage_build_command)
1387 # Note: The use of generator expressions allows downstream extensions to add/change the post build.
1388 # Unfortunately, the BYPRODUCTS does not allow for generator expression, so question is if we
1389 # should remove the downstream ability from start.
1390 # Or fix the output name, by the use of `get_property`
1391 list(APPEND
1392 post_build_commands
Torsten Rasmussen571f48f2020-09-04 21:07:46 +02001393 COMMAND $<TARGET_PROPERTY:bintools,memusage_command>
1394 $<TARGET_PROPERTY:bintools,memusage_flag>
1395 $<TARGET_PROPERTY:bintools,memusage_infile>${KERNEL_ELF_NAME}
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001396 )
1397
1398 # For now, the byproduct can only be supported upstream on byproducts name,
1399 # cause byproduct does not support generator expressions
1400 get_property(memusage_byproducts TARGET bintools PROPERTY memusage_byproducts)
1401 list(APPEND
1402 post_build_byproducts
1403 ${memusage_byproducts}
1404 )
1405 endif()
Danny Oerndrupc41e7122019-07-18 15:16:39 +02001406endif()
1407
Torsten Rasmussend51a67b2022-01-12 14:21:07 +01001408if(CONFIG_BUILD_OUTPUT_ADJUST_LMA)
1409 math(EXPR adjustment "${CONFIG_BUILD_OUTPUT_ADJUST_LMA}" OUTPUT_FORMAT DECIMAL)
1410 list(APPEND
1411 post_build_commands
1412 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1413 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
1414 $<TARGET_PROPERTY:bintools,elfconvert_flag_lma_adjust>${adjustment}
1415 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1416 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_ELF_NAME}
1417 )
1418endif()
1419
Chen Peng18c069c32022-03-07 18:36:53 +08001420if(NOT CONFIG_EXCEPTIONS)
1421 set(eh_frame_section ".eh_frame")
1422else()
1423 set(eh_frame_section "")
1424endif()
1425set(remove_sections_argument_list "")
1426foreach(section .comment COMMON ${eh_frame_section})
1427 list(APPEND remove_sections_argument_list
1428 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>${section})
1429endforeach()
1430
Kumar Galad5419132019-08-13 13:44:20 -05001431if(CONFIG_BUILD_OUTPUT_HEX OR BOARD_FLASH_RUNNER STREQUAL openocd)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001432 get_property(elfconvert_formats TARGET bintools PROPERTY elfconvert_formats)
1433 if(ihex IN_LIST elfconvert_formats)
1434 list(APPEND
1435 post_build_commands
1436 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1437 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1438 ${GAP_FILL}
1439 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>ihex
Chen Peng18c069c32022-03-07 18:36:53 +08001440 ${remove_sections_argument_list}
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001441 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1442 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_HEX_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001443 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001444 )
1445 list(APPEND
1446 post_build_byproducts
1447 ${KERNEL_HEX_NAME}
1448 # ${out_hex_byprod} # Is this needed ?
1449 )
1450 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001451endif()
Anas Nashif4592ff22017-11-23 07:54:26 -05001452
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001453if(CONFIG_BUILD_OUTPUT_BIN)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001454 get_property(elfconvert_formats TARGET bintools PROPERTY elfconvert_formats)
1455 if(binary IN_LIST elfconvert_formats)
1456 list(APPEND
1457 post_build_commands
1458 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1459 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1460 ${GAP_FILL}
1461 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>binary
Chen Peng18c069c32022-03-07 18:36:53 +08001462 ${remove_sections_argument_list}
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001463 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1464 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_BIN_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001465 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001466 )
1467 list(APPEND
1468 post_build_byproducts
1469 ${KERNEL_BIN_NAME}
1470 # ${out_hex_byprod} # Is this needed ?
1471 )
1472 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001473endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001474
Pete Johanson310a4642020-12-31 16:51:52 -05001475if(CONFIG_BUILD_OUTPUT_BIN AND CONFIG_BUILD_OUTPUT_UF2)
Peter Johanson3f332072022-01-27 00:45:27 -05001476 if(CONFIG_BUILD_OUTPUT_UF2_USE_FLASH_BASE)
1477 set(flash_addr "${CONFIG_FLASH_BASE_ADDRESS}")
1478 else()
1479 set(flash_addr "${CONFIG_FLASH_LOAD_OFFSET}")
1480 endif()
1481
1482 if(CONFIG_BUILD_OUTPUT_UF2_USE_FLASH_OFFSET)
1483 # Note, the `+ 0` in formula below avoids errors in cases where a Kconfig
1484 # variable is undefined and thus expands to nothing.
1485 math(EXPR flash_addr
1486 "${flash_addr} + ${CONFIG_FLASH_LOAD_OFFSET} + 0"
1487 OUTPUT_FORMAT HEXADECIMAL
1488 )
1489 endif()
1490
Pete Johanson310a4642020-12-31 16:51:52 -05001491 list(APPEND
1492 post_build_commands
1493 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/uf2conv.py
1494 -c
1495 -f ${CONFIG_BUILD_OUTPUT_UF2_FAMILY_ID}
Peter Johanson3f332072022-01-27 00:45:27 -05001496 -b ${flash_addr}
Pete Johanson310a4642020-12-31 16:51:52 -05001497 -o ${KERNEL_UF2_NAME}
1498 ${KERNEL_BIN_NAME}
1499 )
1500 list(APPEND
1501 post_build_byproducts
1502 ${KERNEL_UF2_NAME}
1503 )
1504endif()
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001505
Torsten Rasmussenfffaf052021-10-12 23:08:36 +02001506if(CONFIG_BUILD_OUTPUT_META)
1507 list(APPEND
1508 post_build_commands
1509 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/zephyr_module.py
1510 ${WEST_ARG}
1511 ${ZEPHYR_MODULES_ARG}
1512 ${ZEPHYR_EXTRA_MODULES_ARG}
1513 --meta-out ${KERNEL_META_NAME}
Torsten Rasmussen1a519932021-11-04 18:35:50 +01001514 $<$<BOOL:${CONFIG_BUILD_OUTPUT_META_STATE_PROPAGATE}>:--meta-state-propagate>
Torsten Rasmussenfffaf052021-10-12 23:08:36 +02001515 )
1516 list(APPEND
1517 post_build_byproducts
1518 ${KERNEL_META_NAME}
1519 )
1520endif()
1521
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001522# Cleanup intermediate files
1523if(CONFIG_CLEANUP_INTERMEDIATE_FILES)
Torsten Rasmussend2cdee52021-11-25 12:23:34 +01001524 foreach(index RANGE ${ZEPHYR_CURRENT_LINKER_PASS})
1525 # Those files can be very large in some cases, delete them as we do not need them.
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001526 list(APPEND
1527 post_build_commands
1528 COMMAND
Torsten Rasmussend2cdee52021-11-25 12:23:34 +01001529 ${CMAKE_COMMAND} -E remove zephyr_pre${index}.elf
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001530 )
Torsten Rasmussend2cdee52021-11-25 12:23:34 +01001531 endforeach()
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001532endif()
1533
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001534if(CONFIG_BUILD_OUTPUT_S19)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001535 get_property(elfconvert_formats TARGET bintools PROPERTY elfconvert_formats)
1536 if(srec IN_LIST elfconvert_formats)
1537 # Should we print a warning if case the tools does not support converting to s19 ?
1538 list(APPEND
1539 post_build_commands
1540 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1541 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1542 ${GAP_FILL}
1543 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>srec
1544 $<TARGET_PROPERTY:bintools,elfconvert_flag_srec_len>1
1545 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1546 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_S19_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001547 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001548 )
1549 list(APPEND
1550 post_build_byproducts
1551 ${KERNEL_S19_NAME}
1552 # ${out_S19_byprod} # Is this needed ?
1553
1554 )
1555 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001556endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001557
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001558if(CONFIG_OUTPUT_DISASSEMBLY)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001559if(CONFIG_OUTPUT_DISASSEMBLE_ALL)
1560 set(disassembly_type "$<TARGET_PROPERTY:bintools,disassembly_flag_all>")
Rohit Gujarathi35713f22020-05-07 10:08:37 +05301561 else()
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001562 set(disassembly_type "$<TARGET_PROPERTY:bintools,disassembly_flag_inline_source>")
Rohit Gujarathi35713f22020-05-07 10:08:37 +05301563 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001564 list(APPEND
1565 post_build_commands
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001566 COMMAND $<TARGET_PROPERTY:bintools,disassembly_command>
1567 $<TARGET_PROPERTY:bintools,disassembly_flag>
1568 ${disassembly_type}
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001569 $<TARGET_PROPERTY:bintools,disassembly_flag_infile>${KERNEL_ELF_NAME}
1570 $<TARGET_PROPERTY:bintools,disassembly_flag_outfile>${KERNEL_LST_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001571 $<TARGET_PROPERTY:bintools,disassembly_flag_final>
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001572 )
1573 list(APPEND
1574 post_build_byproducts
1575 ${KERNEL_LST_NAME}
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001576# ${out_disassembly_byprod} # Needed ??
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001577 )
1578endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001579
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001580if(CONFIG_OUTPUT_STAT)
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001581# zephyr_post_build(TOOLS bintools COMMAND readelf FLAGS headers INFILE file OUTFILE outfile)
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001582 list(APPEND
1583 post_build_commands
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001584 COMMAND $<TARGET_PROPERTY:bintools,readelf_command>
1585 $<TARGET_PROPERTY:bintools,readelf_flag>
1586 $<TARGET_PROPERTY:bintools,readelf_flag_headers>
Torsten Rasmussen2d1a3d92021-05-20 17:38:57 +02001587 $<TARGET_PROPERTY:bintools,readelf_flag_infile>${KERNEL_ELF_NAME}
1588 $<TARGET_PROPERTY:bintools,readelf_flag_outfile>${KERNEL_STAT_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001589 $<TARGET_PROPERTY:bintools,readelf_flag_final>
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001590 )
1591 list(APPEND
1592 post_build_byproducts
1593 ${KERNEL_STAT_NAME}
1594 )
1595endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001596
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001597if(CONFIG_BUILD_OUTPUT_STRIPPED)
1598 list(APPEND
1599 post_build_commands
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001600 COMMAND $<TARGET_PROPERTY:bintools,strip_command>
1601 $<TARGET_PROPERTY:bintools,strip_flag>
1602 $<TARGET_PROPERTY:bintools,strip_flag_all>
1603 $<TARGET_PROPERTY:bintools,strip_flag_infile>${KERNEL_ELF_NAME}
1604 $<TARGET_PROPERTY:bintools,strip_flag_outfile>${KERNEL_STRIP_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001605 $<TARGET_PROPERTY:bintools,strip_flag_final>
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001606 )
1607 list(APPEND
1608 post_build_byproducts
1609 ${KERNEL_STRIP_NAME}
1610 )
1611endif()
1612
1613if(CONFIG_BUILD_OUTPUT_EXE)
1614 list(APPEND
1615 post_build_commands
1616 COMMAND
1617 ${CMAKE_COMMAND} -E copy ${KERNEL_ELF_NAME} ${KERNEL_EXE_NAME}
1618 )
1619 list(APPEND
1620 post_build_byproducts
1621 ${KERNEL_EXE_NAME}
1622 )
1623endif()
Anas Nashif4592ff22017-11-23 07:54:26 -05001624
Torsten Rasmussenc8ddc342022-01-10 11:02:26 +01001625if(CONFIG_BUILD_OUTPUT_INFO_HEADER)
1626 list(APPEND
1627 post_build_commands
1628 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/gen_image_info.py
1629 --elf-file=${KERNEL_ELF_NAME}
1630 --header-file=${PROJECT_BINARY_DIR}/include/public/zephyr_image_info.h
Torsten Rasmussend51a67b2022-01-12 14:21:07 +01001631 $<$<BOOL:${adjustment}>:--adjusted-lma=${adjustment}>
Torsten Rasmussenc8ddc342022-01-10 11:02:26 +01001632 )
1633 list(APPEND
1634 post_build_byproducts
1635 ${PROJECT_BINARY_DIR}/include/public/zephyr_image_info.h
1636 )
1637endif()
1638
Martí Bolívarf66a0c32020-08-18 11:28:04 -07001639# Generate and use MCUboot related artifacts as needed.
1640if(CONFIG_BOOTLOADER_MCUBOOT)
1641 include(${CMAKE_CURRENT_LIST_DIR}/cmake/mcuboot.cmake)
1642endif()
1643
Rajavardhan Gundiecdea1c2019-01-25 11:53:13 +05301644get_property(extra_post_build_commands
1645 GLOBAL PROPERTY
1646 extra_post_build_commands
1647 )
1648
1649list(APPEND
1650 post_build_commands
1651 ${extra_post_build_commands}
1652 )
1653
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001654get_property(extra_post_build_byproducts
1655 GLOBAL PROPERTY
1656 extra_post_build_byproducts
1657 )
1658
1659list(APPEND
1660 post_build_byproducts
1661 ${extra_post_build_byproducts}
1662 )
1663
Daniel Leunga5ab1a72021-04-02 12:54:53 -07001664if(CONFIG_LOG_DICTIONARY_SUPPORT)
1665 set(LOG_DICT_DB_NAME ${PROJECT_BINARY_DIR}/log_dictionary.json)
1666
1667 list(APPEND
1668 post_build_commands
1669 COMMAND
1670 ${PYTHON_EXECUTABLE}
1671 ${ZEPHYR_BASE}/scripts/logging/dictionary/database_gen.py
1672 ${KERNEL_ELF_NAME}
1673 ${LOG_DICT_DB_NAME}
Torsten Rasmussen91709772022-02-04 10:27:13 +01001674 --build-header ${PROJECT_BINARY_DIR}/include/generated/version.h
Daniel Leunga5ab1a72021-04-02 12:54:53 -07001675 )
1676 list(APPEND
1677 post_build_byproducts
1678 ${LOG_DICT_DB_NAME}
1679 )
1680endif()
1681
Marc Herbert498b4942019-04-16 23:30:52 -07001682# Add post_build_commands to post-process the final .elf file produced by
Jordan Yates28b2e552021-10-20 20:19:28 +10001683# either the ZEPHYR_LINK_STAGE_EXECUTABLE or the KERNEL_ELF executable
Marc Herbert498b4942019-04-16 23:30:52 -07001684# targets above.
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001685add_custom_command(
1686 TARGET ${logical_target_for_zephyr_elf}
1687 POST_BUILD
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001688 ${post_build_commands}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001689 BYPRODUCTS
1690 ${post_build_byproducts}
Marc Herbert498b4942019-04-16 23:30:52 -07001691 COMMENT "Generating files from ${KERNEL_ELF_NAME} for board: ${BOARD}"
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001692 COMMAND_EXPAND_LISTS
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001693 # NB: COMMENT only works for some CMake-Generators
Rajavardhan Gundiecdea1c2019-01-25 11:53:13 +05301694 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001695
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001696# To populate with hex files to merge, do the following:
1697# set_property(GLOBAL APPEND PROPERTY HEX_FILES_TO_MERGE ${my_local_list})
1698# Note that the zephyr.hex file will not be included automatically.
1699get_property(HEX_FILES_TO_MERGE GLOBAL PROPERTY HEX_FILES_TO_MERGE)
1700if(HEX_FILES_TO_MERGE)
1701 # Merge in out-of-tree hex files.
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001702 set(MERGED_HEX_NAME merged.hex)
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001703
1704 add_custom_command(
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001705 OUTPUT ${MERGED_HEX_NAME}
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001706 COMMAND
1707 ${PYTHON_EXECUTABLE}
1708 ${ZEPHYR_BASE}/scripts/mergehex.py
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001709 -o ${MERGED_HEX_NAME}
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001710 ${HEX_FILES_TO_MERGE}
1711 DEPENDS ${HEX_FILES_TO_MERGE} ${logical_target_for_zephyr_elf}
1712 )
1713
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001714 add_custom_target(mergehex ALL DEPENDS ${MERGED_HEX_NAME})
Torsten Rasmussend38da9d2020-06-30 09:55:54 +02001715 list(APPEND RUNNERS_DEPS mergehex)
Håkon Øye Amundsenc9a2a5e2020-01-03 08:25:03 +00001716
1717 message(VERBOSE "Merging hex files: ${HEX_FILES_TO_MERGE}")
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001718endif()
1719
Filip Kokosinski94428042021-08-30 13:09:48 +02001720if(SUPPORTED_EMU_PLATFORMS)
1721 list(GET SUPPORTED_EMU_PLATFORMS 0 default_emu)
Anas Nashifa1d18102022-02-14 22:08:42 -05001722 if(EXISTS ${ZEPHYR_BASE}/cmake/emu/${default_emu}.cmake)
1723 add_custom_target(run DEPENDS run_${default_emu})
1724 endif()
Filip Kokosinski94428042021-08-30 13:09:48 +02001725
1726 foreach(EMU_PLATFORM ${SUPPORTED_EMU_PLATFORMS})
Anas Nashifa1d18102022-02-14 22:08:42 -05001727 if(EXISTS ${ZEPHYR_BASE}/cmake/emu/${EMU_PLATFORM}.cmake)
1728 include(${ZEPHYR_BASE}/cmake/emu/${EMU_PLATFORM}.cmake)
1729 endif()
Filip Kokosinski94428042021-08-30 13:09:48 +02001730 endforeach()
1731
1732 if(TARGET debugserver_${default_emu})
1733 add_custom_target(debugserver DEPENDS debugserver_${default_emu})
1734 endif()
Anas Nashiffd276ae2017-12-21 16:45:45 -05001735else()
1736 add_custom_target(run
1737 COMMAND
1738 ${CMAKE_COMMAND} -E echo
1739 "==================================================="
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +01001740 "Emulation/Simulation not supported with this board."
Anas Nashiffd276ae2017-12-21 16:45:45 -05001741 "==================================================="
1742 )
Anas Nashifc15d3c92017-11-21 18:54:55 -05001743endif()
1744
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001745add_subdirectory(cmake/flash)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001746add_subdirectory(cmake/usage)
1747add_subdirectory(cmake/reports)
1748
Marc Herbert83723102019-06-17 13:26:11 -07001749if(NOT CONFIG_TEST)
Andrew Boie411686f2018-05-24 13:18:36 -07001750if(CONFIG_ASSERT AND (NOT CONFIG_FORCE_NO_ASSERT))
Sebastian Bøefa8f9d42019-12-06 12:54:53 +01001751 message(WARNING "__ASSERT() statements are globally ENABLED")
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001752endif()
Marc Herbert83723102019-06-17 13:26:11 -07001753endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001754
Vincent Wana2bc5142020-01-09 14:20:44 -08001755if(CONFIG_BOARD_DEPRECATED_RELEASE)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001756 message(WARNING "
1757 WARNING: The board '${BOARD}' is deprecated and will be
Vincent Wana2bc5142020-01-09 14:20:44 -08001758 removed in version ${CONFIG_BOARD_DEPRECATED_RELEASE}"
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +01001759 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001760endif()
Paul Sokolovsky1c6a7d72019-06-06 21:12:14 +03001761
Vincent Wan180b4df2020-01-08 17:10:51 -08001762if(CONFIG_SOC_DEPRECATED_RELEASE)
1763 message(WARNING "
1764 WARNING: The SoC '${SOC_NAME}' is deprecated and will be
1765 removed in version ${CONFIG_SOC_DEPRECATED_RELEASE}"
1766 )
1767endif()
1768
Sebastian Bøee50e12d2019-08-29 16:19:32 +02001769# In CMake projects, 'CMAKE_BUILD_TYPE' usually determines the
1770# optimization flag, but in Zephyr it is determined through
1771# Kconfig. Here we give a warning when there is a mismatch between the
1772# two in case the user is not aware of this.
1773set(build_types None Debug Release RelWithDebInfo MinSizeRel)
1774
1775if((CMAKE_BUILD_TYPE IN_LIST build_types) AND (NOT NO_BUILD_TYPE_WARNING))
1776 string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_uppercase)
Torsten Rasmussen05bb8552021-12-10 15:06:30 +01001777 # The CMAKE_C_FLAGS_<build_type> is a string, so we do a regex to see if the
1778 # optimization flag is present in that string.
1779 # To avoid false-positive matches, the flag must either be matched first
1780 # or last in string, or come after / followed by minimum a space.
1781 if(NOT (CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_uppercase} MATCHES "(^| )${OPTIMIZATION_FLAG}($| )"))
Sebastian Bøee50e12d2019-08-29 16:19:32 +02001782 message(WARNING "
1783 The CMake build type was set to '${CMAKE_BUILD_TYPE}', but the optimization flag was set to '${OPTIMIZATION_FLAG}'.
1784 This may be intentional and the warning can be turned off by setting the CMake variable 'NO_BUILD_TYPE_WARNING'"
1785 )
1786 endif()
1787endif()
1788
Stephanos Ioannidisfd4700d2021-09-11 22:06:28 +09001789# @Intent: Set compiler specific flags for standard C/C++ includes
Paul Sokolovsky1c6a7d72019-06-06 21:12:14 +03001790# Done at the very end, so any other system includes which may
1791# be added by Zephyr components were first in list.
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001792# Note, the compile flags are moved, but the system include is still present here.
1793zephyr_compile_options($<TARGET_PROPERTY:compiler,nostdinc>)
1794target_include_directories(zephyr_interface SYSTEM INTERFACE $<TARGET_PROPERTY:compiler,nostdinc_include>)
Torsten Rasmussena5917f02020-09-09 15:42:32 +02001795
Stephanos Ioannidisfd4700d2021-09-11 22:06:28 +09001796if(NOT CONFIG_LIB_CPLUSPLUS)
1797 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,nostdincxx>>)
1798endif()
1799
Torsten Rasmussena5917f02020-09-09 15:42:32 +02001800# Finally export all build flags from Zephyr
1801add_subdirectory_ifdef(
1802 CONFIG_MAKEFILE_EXPORTS
1803 cmake/makefile_exports
1804 )