blob: 242e30cd2e4ecaf7afcdbf6a22544465cf7fc2f2 [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
Sebastian Bøe12f8f762017-10-27 15:43:34 +020039# Verify that the toolchain can compile a dummy file, if it is not we
Anas Nashiff2cb20c2019-06-18 14:45:40 -040040# won't be able to test for compatibility with certain C flags.
Sebastian Bøe6212ec92019-08-28 13:02:51 +020041zephyr_check_compiler_flag(C "" toolchain_is_ok)
Sebastian Bøe12f8f762017-10-27 15:43:34 +020042assert(toolchain_is_ok "The toolchain is unable to build a dummy C file. See CMakeError.log.")
43
Marc Herbert0370c9b2019-06-13 16:15:44 -070044# In some cases the "final" things are not used at all and "_prebuilt"
45# is the last station. See "logical_target_for_zephyr_elf" below for
46# details.
Sebastian Bøe12f8f762017-10-27 15:43:34 +020047set(CMAKE_EXECUTABLE_SUFFIX .elf)
Sebastian Bøe15260702019-01-14 16:31:02 +010048set(ZEPHYR_PREBUILT_EXECUTABLE zephyr_prebuilt)
Mark Ruvald Pedersen37d49472019-05-07 15:20:20 +020049set(ZEPHYR_FINAL_EXECUTABLE zephyr_final)
Sebastian Bøe12f8f762017-10-27 15:43:34 +020050
Mark Ruvald Pedersen11d6bae2019-04-29 16:57:37 +020051# Set some phony targets to collect dependencies
Sebastian Bøe1b86fb92019-01-14 16:39:33 +010052set(OFFSETS_H_TARGET offsets_h)
Sebastian Bøe1b86fb92019-01-14 16:39:33 +010053set(SYSCALL_LIST_H_TARGET syscall_list_h_target)
54set(DRIVER_VALIDATION_H_TARGET driver_validation_h_target)
55set(KOBJ_TYPES_H_TARGET kobj_types_h_target)
Andrew Boiec1c54b12020-03-16 12:48:00 -070056set(PARSE_SYSCALLS_TARGET parse_syscalls_target)
Sebastian Bøe1b86fb92019-01-14 16:39:33 +010057
Sebastian Bøe12f8f762017-10-27 15:43:34 +020058define_property(GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT BRIEF_DOCS " " FULL_DOCS " ")
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +010059set_property( GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT elf32-little${ARCH}) # BFD format
Sebastian Bøe12f8f762017-10-27 15:43:34 +020060
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +010061# "zephyr_interface" is a source-less library that encapsulates all the global
Sebastian Bøe12f8f762017-10-27 15:43:34 +020062# compiler options needed by all source files. All zephyr libraries,
63# including the library named "zephyr" link with this library to
64# obtain these flags.
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +010065# https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#interface-libraries
Sebastian Bøe12f8f762017-10-27 15:43:34 +020066add_library(zephyr_interface INTERFACE)
67
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +010068# "zephyr" is a catch-all CMake library for source files that can be
Sebastian Bøe12f8f762017-10-27 15:43:34 +020069# built purely with the include paths, defines, and other compiler
70# flags that come with zephyr_interface.
71zephyr_library_named(zephyr)
72
73zephyr_include_directories(
Sebastian Bøe12f8f762017-10-27 15:43:34 +020074 include
Sebastian Bøe12f8f762017-10-27 15:43:34 +020075 ${PROJECT_BINARY_DIR}/include/generated
76 ${USERINCLUDE}
77 ${STDINCLUDE}
78)
79
Sebastian Bøe40363392019-01-25 10:14:13 +010080# Don't add non-existing include directories, it creates noise and
81# warnings in some tooling
82foreach(optional_include_dir
83 ${SOC_DIR}/${ARCH}/${SOC_PATH}
84 ${SOC_DIR}/${ARCH}/${SOC_PATH}/include
85 ${SOC_DIR}/${ARCH}/${SOC_FAMILY}/include
Andy Ross544a38e2020-06-25 17:42:51 -070086 ${SOC_DIR}/${ARCH}/${SOC_FAMILY}/common/include
Sebastian Bøe40363392019-01-25 10:14:13 +010087 )
88 if(EXISTS ${optional_include_dir})
89 zephyr_include_directories(${optional_include_dir})
90 endif()
91endforeach()
92
Sebastian Bøe12f8f762017-10-27 15:43:34 +020093zephyr_compile_definitions(
94 KERNEL
95 __ZEPHYR__=1
Anas Nashif34aebad2018-01-03 12:26:19 -050096)
97
Mark Ruvald Pedersen01592072019-01-10 12:07:51 +010098# @Intent: Set compiler flags to enable buffer overflow checks in libc functions
99# @config in CONFIG_NO_OPTIMIZATIONS optional : Optimizations may affect security
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200100zephyr_compile_definitions($<TARGET_PROPERTY:compiler,security_fortify> )
Mark Ruvald Pedersen01592072019-01-10 12:07:51 +0100101
102# @Intent: Set compiler flags to detect general stack overflows across all functions
103if(CONFIG_STACK_CANARIES)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200104 zephyr_compile_options($<TARGET_PROPERTY:compiler,security_canaries>)
Anas Nashif34aebad2018-01-03 12:26:19 -0500105endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200106
Anas Nashifdaf77162018-04-09 21:53:26 -0500107if(BUILD_VERSION)
108 zephyr_compile_definitions(
109 BUILD_VERSION=${BUILD_VERSION}
110 )
111endif()
112
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100113# @Intent: Obtain compiler optimizations flags and store in variables
114# @details:
115# Kconfig.zephyr "Optimization level" is a kconfig choice, ensuring
116# only *one* of CONFIG_{NO,DEBUG,SPEED,SIZE}_OPTIMIZATIONS is set.
117# Refer to Kconfig.zephyr for selection logic and description of these choices.
118# toolchain_cc_optimize_*() macros must provide the mapping from these kconfigs
119# to compiler flags. Each macro will store the flags in a CMake variable, whose
120# name is passed as argument (somewhat like by reference).
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200121#
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100122# If the user wants to tweak the optimizations, there are two ways:
123# 1) Using EXTRA_CFLAGS which is applied regardless of kconfig choice, or
124# 2) Rely on override support being implemented by your toolchain_cc_optimize_*()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200125#
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200126get_property(OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG TARGET compiler PROPERTY no_optimization)
127get_property(OPTIMIZE_FOR_DEBUG_FLAG TARGET compiler PROPERTY optimization_debug)
128get_property(OPTIMIZE_FOR_SPEED_FLAG TARGET compiler PROPERTY optimization_speed)
129get_property(OPTIMIZE_FOR_SIZE_FLAG TARGET compiler PROPERTY optimization_size)
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100130
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100131# From kconfig choice, pick the actual OPTIMIZATION_FLAG to use.
132# Kconfig choice ensures only one of these CONFIG_*_OPTIMIZATIONS is set.
Alberto Escolar Piedrasf60527a2018-01-22 15:35:54 +0100133if(CONFIG_NO_OPTIMIZATIONS)
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100134 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG})
135elseif(CONFIG_DEBUG_OPTIMIZATIONS)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200136 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_DEBUG_FLAG})
Aurelien Jarnoe8413d12018-06-16 23:40:04 +0200137elseif(CONFIG_SPEED_OPTIMIZATIONS)
138 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SPEED_FLAG})
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100139elseif(CONFIG_SIZE_OPTIMIZATIONS)
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100140 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SIZE_FLAG}) # Default in kconfig
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100141else()
Anas Nashif885aaf22019-01-18 19:15:19 -0500142 assert(0 "Unreachable code. Expected optimization level to have been chosen. See Kconfig.zephyr")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200143endif()
144
Ulf Magnussonde42aea2020-02-07 00:48:22 +0100145if(NOT CONFIG_ARCH_IS_SET)
146 message(WARNING "\
147None of the CONFIG_<arch> (e.g. CONFIG_X86) symbols are set. \
148Select one of them from the SOC_SERIES_* symbol or, lacking that, from the \
149SOC_* symbol.")
150endif()
151
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100152# Apply the final optimization flag(s)
153zephyr_compile_options(${OPTIMIZATION_FLAG})
154
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100155# @Intent: Obtain compiler specific flags related to C++ that are not influenced by kconfig
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200156zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,required>>)
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200157
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100158# @Intent: Obtain compiler specific flags for compiling under different ISO standards of C++
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200159if(CONFIG_CPLUSPLUS)
160 # From kconfig choice, pick a single dialect.
161 # Kconfig choice ensures only one of these CONFIG_STD_CPP* is set.
162 if(CONFIG_STD_CPP98)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200163 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp98>)
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200164 elseif(CONFIG_STD_CPP11)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200165 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp11>) # Default in kconfig
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200166 elseif(CONFIG_STD_CPP14)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200167 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp14>)
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200168 elseif(CONFIG_STD_CPP17)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200169 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp17>)
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200170 elseif(CONFIG_STD_CPP2A)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200171 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp2a>)
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200172 else()
173 assert(0 "Unreachable code. Expected C++ standard to have been chosen. See Kconfig.zephyr.")
174 endif()
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200175
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200176 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:${STD_CPP_DIALECT_FLAGS}>)
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200177endif()
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100178
179if(NOT CONFIG_EXCEPTIONS)
180 # @Intent: Obtain compiler specific flags related to C++ Exceptions
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200181 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,no_exceptions>>)
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100182endif()
183
184if(NOT CONFIG_RTTI)
185 # @Intent: Obtain compiler specific flags related to C++ Run Time Type Information
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200186 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,no_rtti>>)
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100187endif()
188
Andy Rossfe04adf2019-02-27 11:53:18 -0800189if(CONFIG_MISRA_SANE)
Danny Oerndrup8e5a95e2019-05-16 12:53:58 +0200190 # @Intent: Obtain toolchain compiler flags relating to MISRA.
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200191 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_misra_sane>>)
192 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_error_misra_sane>>)
Andy Rossfe04adf2019-02-27 11:53:18 -0800193endif()
194
Flavio Ceolinb587e8d2020-08-26 09:48:33 -0700195# This is intend to be temporary. Once we have fixed the violations that
196# prevents build Zephyr, these flags shall be part of the default flags.
197if(CONFIG_CODING_GUIDELINE_CHECK)
198 # @Intent: Obtain toolchain compiler flags relating to coding guideline
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200199 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_coding_guideline>>)
Flavio Ceolinb587e8d2020-08-26 09:48:33 -0700200endif()
201
Danny Oerndrup4ddbc002019-06-11 13:55:53 +0200202# @Intent: Set compiler specific macro inclusion of AUTOCONF_H
Torsten Rasmussenf109e682020-08-13 15:49:46 +0200203zephyr_compile_options("SHELL: $<TARGET_PROPERTY:compiler,imacros> ${AUTOCONF_H}")
Danny Oerndrup4ddbc002019-06-11 13:55:53 +0200204
Danny Oerndrupfaa72b72019-06-11 15:56:57 +0200205# @Intent: Set compiler specific flag for bare metal freestanding option
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200206zephyr_compile_options($<TARGET_PROPERTY:compiler,freestanding>)
Danny Oerndrupfaa72b72019-06-11 15:56:57 +0200207
Danny Oerndrupe34ed7c2019-06-12 14:56:46 +0200208# @Intent: Set compiler specific flag for tentative definitions, no-common
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200209zephyr_compile_options($<TARGET_PROPERTY:compiler,no_common>)
Danny Oerndrupe34ed7c2019-06-12 14:56:46 +0200210
Danny Oerndrupe0569ac2019-07-23 09:00:55 +0200211# @Intent: Set compiler specific flag for production of debug information
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200212zephyr_compile_options($<TARGET_PROPERTY:compiler,debug>)
Danny Oerndrupe0569ac2019-07-23 09:00:55 +0200213
Arvin Farahmande430b7b2021-04-15 11:20:10 -0400214if(CONFIG_COMPILER_COLOR_DIAGNOSTICS)
Arvin Farahmandb8f59682021-04-15 11:20:10 -0400215# @Intent: Set compiler specific flag for diagnostic messages
216zephyr_compile_options($<TARGET_PROPERTY:compiler,diagnostic>)
217endif()
218
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200219zephyr_compile_options(
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530220 ${TOOLCHAIN_C_FLAGS}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200221)
222
Mark Ruvald Pedersencb0fd452019-01-30 21:48:25 +0100223# @Intent: Obtain compiler specific flags related to assembly
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200224# ToDo: Remember to get feedback from Oticon on this, as they might use the `ASM_BASE_FLAG` since this is done this way.
225zephyr_compile_options($<$<COMPILE_LANGUAGE:ASM>:$<TARGET_PROPERTY:asm,required>>)
Mark Ruvald Pedersencb0fd452019-01-30 21:48:25 +0100226
Nicolas Pitreb86aa652019-07-02 16:22:04 -0400227# @Intent: Enforce standard integer type correspondance to match Zephyr usage.
228# (must be after compiler specific flags)
Torsten Rasmussenf109e682020-08-13 15:49:46 +0200229zephyr_compile_options("SHELL: $<TARGET_PROPERTY:compiler,imacros> ${ZEPHYR_BASE}/include/toolchain/zephyr_stdint.h")
Nicolas Pitreb86aa652019-07-02 16:22:04 -0400230
Mark Ruvald Pedersencb0fd452019-01-30 21:48:25 +0100231# Common toolchain-agnostic assembly flags
232zephyr_compile_options(
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200233 $<$<COMPILE_LANGUAGE:ASM>:-D_ASMLANGUAGE>
234)
235
Mark Ruvald Pedersen1f013252019-04-25 15:46:11 +0200236# @Intent: Set fundamental linker specific flags
237toolchain_ld_base()
Aurelien Jarnoc6727d42018-11-26 13:48:34 +0100238
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +0200239toolchain_ld_force_undefined_symbols(
240 _OffsetAbsSyms
241 _ConfigAbsSyms
242)
243
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200244if(NOT CONFIG_NATIVE_APPLICATION)
Mark Ruvald Pedersen65f02c02019-04-25 16:31:30 +0200245 # @Intent: Set linker specific flags for bare metal target
246 toolchain_ld_baremetal()
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200247endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200248
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200249if(CONFIG_LIB_CPLUSPLUS)
Mark Ruvald Pedersen3db09aa2019-04-26 08:43:04 +0200250 # @Intent: Set linker specific flags for C++
251 toolchain_ld_cpp()
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200252endif()
253
Danny Oerndrup8eaa9062019-05-16 12:49:31 +0200254# @Intent: Add the basic toolchain warning flags
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200255zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_base>>)
256zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_base>>)
Danny Oerndrupbdb229f2019-05-06 15:19:27 +0200257
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200258# ==========================================================================
259#
260# cmake -DW=... settings
261#
262# W=1 - warnings that may be relevant and does not occur too often
263# W=2 - warnings that occur quite often but may still be relevant
264# W=3 - the more obscure warnings, can most likely be ignored
265# ==========================================================================
Danny Oerndrup8eaa9062019-05-16 12:49:31 +0200266# @Intent: Add cmake -DW toolchain supported warnings, if any
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200267if(W MATCHES "1")
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200268 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_dw_1>>)
269 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_dw_1>>)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200270endif()
271
272if(W MATCHES "2")
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200273 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_dw_2>>)
274 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_dw_2>>)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200275endif()
276
277if(W MATCHES "3")
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200278 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_dw_3>>)
279 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_dw_3>>)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200280endif()
281
Danny Oerndrup8eaa9062019-05-16 12:49:31 +0200282# @Intent: Add extended, more specific, toolchain warning flags
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200283zephyr_compile_options($<TARGET_PROPERTY:compiler,warning_extended>)
Benoit Leforestier04dad592019-01-25 13:57:03 +0100284
Danny Oerndrup025ffa22019-05-16 12:58:40 +0200285# @Intent: Trigger an error when a declaration does not specify a type
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200286zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_implicit_int>>)
287zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_error_implicit_int>>)
Danny Oerndrup025ffa22019-05-16 12:58:40 +0200288
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200289# Allow the user to inject options when calling cmake, e.g.
290# 'cmake -DEXTRA_CFLAGS="-Werror -Wno-deprecated-declarations" ..'
Sebastian Bøe9f590452017-11-10 12:22:23 +0100291include(cmake/extra_flags.cmake)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200292
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200293zephyr_cc_option(-fno-asynchronous-unwind-tables)
294zephyr_cc_option(-fno-pie)
295zephyr_cc_option(-fno-pic)
296zephyr_cc_option(-fno-strict-overflow)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200297
Daniel Leung02b20352020-09-28 11:27:11 -0700298if(CONFIG_THREAD_LOCAL_STORAGE)
299# Only support local exec TLS model at this point.
300zephyr_cc_option(-ftls-model=local-exec)
301endif()
302
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200303if(CONFIG_OVERRIDE_FRAME_POINTER_DEFAULT)
304 if(CONFIG_OMIT_FRAME_POINTER)
305 zephyr_cc_option(-fomit-frame-pointer)
306 else()
307 zephyr_cc_option(-fno-omit-frame-pointer)
308 endif()
309endif()
310
Sebastian Bøe244451b2019-02-27 08:28:25 +0100311separate_arguments(COMPILER_OPT_AS_LIST UNIX_COMMAND ${CONFIG_COMPILER_OPT})
312zephyr_compile_options(${COMPILER_OPT_AS_LIST})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200313
314# TODO: Include arch compiler options at this point.
315
Danny Oerndrupcbbbdea2019-05-06 15:21:58 +0200316if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
317 # GCC assumed
318 zephyr_cc_option(-fno-reorder-functions)
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530319
Anas Nashif7ee8bb92018-02-11 14:36:21 -0600320 if(NOT ${ZEPHYR_TOOLCHAIN_VARIANT} STREQUAL "xcc")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200321 zephyr_cc_option(-fno-defer-pop)
322 endif()
323endif()
324
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200325zephyr_cc_option_ifdef(CONFIG_STACK_USAGE -fstack-usage)
326
Marc Herbert28a56572019-04-11 16:34:04 -0700327# If the compiler supports it, strip the ${ZEPHYR_BASE} prefix from the
328# __FILE__ macro used in __ASSERT*, in the
329# .noinit."/home/joe/zephyr/fu/bar.c" section names and in any
330# application code. This saves some memory, stops leaking user locations
331# in binaries, makes failure logs more deterministic and most
332# importantly makes builds more deterministic
Marc Herbertf67dcdb2019-05-31 15:28:38 -0700333
Marc Herberteddbf3c2019-06-11 16:57:37 -0700334# If several match then the last one wins. This matters for instances
335# like tests/ and samples/: they're inside all of them! Then let's
336# strip as little as possible.
Marc Herbertf67dcdb2019-05-31 15:28:38 -0700337zephyr_cc_option(-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=CMAKE_SOURCE_DIR)
338zephyr_cc_option(-fmacro-prefix-map=${ZEPHYR_BASE}=ZEPHYR_BASE)
Marc Herberteddbf3c2019-06-11 16:57:37 -0700339if(WEST_TOPDIR)
340 zephyr_cc_option(-fmacro-prefix-map=${WEST_TOPDIR}=WEST_TOPDIR)
341endif()
Marc Herbert28a56572019-04-11 16:34:04 -0700342
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200343# TODO: Archiver arguments
344# ar_option(D)
345
Håkon Øye Amundsend6551b52018-11-29 09:08:08 +0000346# Declare MPU userspace dependencies before the linker scripts to make
347# sure the order of dependencies are met
Andrew Boie41f60112019-01-31 15:53:24 -0800348if(CONFIG_USERSPACE)
Daniel Leung212ec9a2019-03-10 14:20:21 -0700349 set(APP_SMEM_ALIGNED_DEP app_smem_aligned_linker)
350 set(APP_SMEM_UNALIGNED_DEP app_smem_unaligned_linker)
Håkon Øye Amundsend6551b52018-11-29 09:08:08 +0000351endif()
352
Daniel Leung11171692021-03-18 14:00:07 -0700353if(CONFIG_USERSPACE)
354 set(KOBJECT_LINKER_DEP kobject_linker)
355endif()
356
Håkon Øye Amundsena4494392018-11-29 09:14:27 +0000357get_property(TOPT GLOBAL PROPERTY TOPT)
Oleg Zhurakivskyy22119352019-03-08 11:29:33 +0200358set_ifndef( TOPT -Wl,-T) # clang doesn't pick -T for some reason and complains,
359 # while -Wl,-T works for both, gcc and clang
Håkon Øye Amundsena4494392018-11-29 09:14:27 +0000360
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200361if(CONFIG_HAVE_CUSTOM_LINKER_SCRIPT)
362 set(LINKER_SCRIPT ${APPLICATION_SOURCE_DIR}/${CONFIG_CUSTOM_LINKER_SCRIPT})
Sebastian Bøec1aa9d12018-04-12 14:48:05 +0200363 if(NOT EXISTS ${LINKER_SCRIPT})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200364 set(LINKER_SCRIPT ${CONFIG_CUSTOM_LINKER_SCRIPT})
Sebastian Bøec1aa9d12018-04-12 14:48:05 +0200365 assert_exists(CONFIG_CUSTOM_LINKER_SCRIPT)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200366 endif()
367else()
368 # Try a board specific linker file
369 set(LINKER_SCRIPT ${BOARD_DIR}/linker.ld)
370 if(NOT EXISTS ${LINKER_SCRIPT})
371 # If not available, try an SoC specific linker file
Anas Nashif96455d52018-09-04 14:34:06 -0500372 set(LINKER_SCRIPT ${SOC_DIR}/${ARCH}/${SOC_PATH}/linker.ld)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200373 endif()
374endif()
375
376if(NOT EXISTS ${LINKER_SCRIPT})
377 message(FATAL_ERROR "Could not find linker script: '${LINKER_SCRIPT}'. Corrupted configuration?")
378endif()
379
380configure_file(version.h.in ${PROJECT_BINARY_DIR}/include/generated/version.h)
381
Sebastian Bøec23cc262018-10-09 16:03:29 +0200382# Error-out when the deprecated naming convention is found (until
383# after 1.14.0 has been released)
384foreach(path
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100385 ${BOARD_DIR}/dts.fixup
386 ${PROJECT_SOURCE_DIR}/soc/${ARCH}/${SOC_PATH}/dts.fixup
Sebastian Bøec23cc262018-10-09 16:03:29 +0200387 ${APPLICATION_SOURCE_DIR}/dts.fixup
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100388 )
Sebastian Bøec23cc262018-10-09 16:03:29 +0200389 if(EXISTS ${path})
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100390 message(FATAL_ERROR
391 "A deprecated filename has been detected. Porting is required."
392 "The file '${path}' exists, but it should be named dts_fixup.h instead."
393 "See https://github.com/zephyrproject-rtos/zephyr/pull/10352 for more details"
394 )
Sebastian Bøec23cc262018-10-09 16:03:29 +0200395 endif()
396endforeach()
397
398set_ifndef( DTS_BOARD_FIXUP_FILE ${BOARD_DIR}/dts_fixup.h)
399set_ifndef( DTS_SOC_FIXUP_FILE ${SOC_DIR}/${ARCH}/${SOC_PATH}/dts_fixup.h)
400set( DTS_APP_FIXUP_FILE ${APPLICATION_SOURCE_DIR}/dts_fixup.h)
Sebastian Bøec23cc262018-10-09 16:03:29 +0200401
Ulf Magnusson4e850062020-01-16 13:29:53 +0100402set_ifndef(DTS_CAT_OF_FIXUP_FILES ${ZEPHYR_BINARY_DIR}/include/generated/devicetree_fixups.h)
Sebastian Bøe361fdaa2019-01-28 13:40:50 +0100403
404# Concatenate the fixups into a single header file for easy
Sebastian Bøec23cc262018-10-09 16:03:29 +0200405# #include'ing
Ulf Magnusson4e850062020-01-16 13:29:53 +0100406file(WRITE ${DTS_CAT_OF_FIXUP_FILES} "/* May only be included by devicetree.h */\n\n")
Martí Bolívar21c7d422020-05-08 16:06:48 -0700407set(DISCOVERED_FIXUP_FILES)
Sebastian Bøe361fdaa2019-01-28 13:40:50 +0100408foreach(fixup_file
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100409 ${DTS_BOARD_FIXUP_FILE}
410 ${DTS_SOC_FIXUP_FILE}
411 ${DTS_APP_FIXUP_FILE}
412 ${shield_dts_fixups}
413 )
Sebastian Bøe361fdaa2019-01-28 13:40:50 +0100414 if(EXISTS ${fixup_file})
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100415 file(READ ${fixup_file} contents)
416 file(APPEND ${DTS_CAT_OF_FIXUP_FILES} "${contents}")
Martí Bolívar21c7d422020-05-08 16:06:48 -0700417 string(APPEND DISCOVERED_FIXUP_FILES "- ${fixup_file}\n")
Sebastian Bøec23cc262018-10-09 16:03:29 +0200418 endif()
419endforeach()
420
Martí Bolívar21c7d422020-05-08 16:06:48 -0700421if (DISCOVERED_FIXUP_FILES)
422 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.")
423endif()
424
Sebastian Bøe6f946e22018-01-09 10:52:57 +0100425# Unfortunately, the order in which CMakeLists.txt code is processed
426# matters so we need to be careful about how we order the processing
427# of subdirectories. One example is "Compiler flags added late in the
428# build are not exported to external build systems #5605"; when we
429# integrate with an external build system we read out all compiler
430# flags when the external project is created. So an external project
431# defined in subsys or ext will not get global flags added by drivers/
432# or tests/ as the subdirectories are ordered now.
433#
434# Another example of when the order matters is the reading and writing
435# of global properties such as ZEPHYR_LIBS or
436# GENERATED_KERNEL_OBJECT_FILES.
437#
438# Arch is placed early because it defines important compiler flags
439# that must be exported to external build systems defined in
440# e.g. subsys/.
441add_subdirectory(arch)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200442add_subdirectory(lib)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200443# We use include instead of add_subdirectory to avoid creating a new directory scope.
444# This is because source file properties are directory scoped, including the GENERATED
445# property which is set implicitly for custom command outputs
446include(misc/generated/CMakeLists.txt)
Anas Nashif3d1252f2018-09-03 15:20:14 -0500447
Erwan Gouriouba31cb52018-09-13 16:25:53 +0200448if(EXISTS ${SOC_DIR}/${ARCH}/CMakeLists.txt)
Anas Nashif96455d52018-09-04 14:34:06 -0500449 add_subdirectory(${SOC_DIR}/${ARCH} soc/${ARCH})
Anas Nashif3d1252f2018-09-03 15:20:14 -0500450else()
Anas Nashif96455d52018-09-04 14:34:06 -0500451 add_subdirectory(${SOC_DIR}/${ARCH}/${SOC_PATH} soc/${ARCH}/${SOC_PATH})
Anas Nashif3d1252f2018-09-03 15:20:14 -0500452endif()
453
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200454add_subdirectory(boards)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200455add_subdirectory(subsys)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200456add_subdirectory(drivers)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200457
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100458# Include zephyr modules generated CMake file.
Torsten Rasmussen2fc062b2020-08-24 11:01:04 +0200459foreach(module_name ${ZEPHYR_MODULE_NAMES})
460 # Note the second, binary_dir parameter requires the added
461 # subdirectory to have its own, local cmake target(s). If not then
462 # this binary_dir is created but stays empty. Object files land in
463 # the main binary dir instead.
464 # https://cmake.org/pipermail/cmake/2019-June/069547.html
Torsten Rasmussen3d880832021-01-19 12:01:38 +0100465 zephyr_string(SANITIZE TOUPPER MODULE_NAME_UPPER ${module_name})
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200466 if(NOT ${ZEPHYR_${MODULE_NAME_UPPER}_CMAKE_DIR} STREQUAL "")
467 set(ZEPHYR_CURRENT_MODULE_DIR ${ZEPHYR_${MODULE_NAME_UPPER}_MODULE_DIR})
468 set(ZEPHYR_CURRENT_CMAKE_DIR ${ZEPHYR_${MODULE_NAME_UPPER}_CMAKE_DIR})
469 add_subdirectory(${ZEPHYR_CURRENT_CMAKE_DIR} ${CMAKE_BINARY_DIR}/modules/${module_name})
470 endif()
Torsten Rasmussen2fc062b2020-08-24 11:01:04 +0200471endforeach()
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200472# Done processing modules, clear ZEPHYR_CURRENT_MODULE_DIR and ZEPHYR_CURRENT_CMAKE_DIR.
Torsten Rasmussen2fc062b2020-08-24 11:01:04 +0200473set(ZEPHYR_CURRENT_MODULE_DIR)
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200474set(ZEPHYR_CURRENT_CMAKE_DIR)
Torsten Rasmussen7e9d1bd2019-02-05 10:36:22 +0100475
Andrew Boie59601192020-05-29 13:24:51 -0700476set(syscall_list_h ${CMAKE_CURRENT_BINARY_DIR}/include/generated/syscall_list.h)
477set(syscalls_json ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls.json)
478set(struct_tags_json ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/struct_tags.json)
Sebastian Bøe13a68402017-11-20 13:03:55 +0100479
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200480# The syscalls subdirs txt file is constructed by python containing a list of folders to use for
481# dependency handling, including empty folders.
482# Windows: The list is used to specify DIRECTORY list with CMAKE_CONFIGURE_DEPENDS attribute.
483# Other OS: The list will update whenever a file is added/removed/modified and ensure a re-build.
484set(syscalls_subdirs_txt ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_subdirs.txt)
485
486# As syscalls_subdirs_txt is updated whenever a file is modified, this file can not be used for
487# monitoring of added / removed folders. A trigger file is thus used for correct dependency
488# handling. The trigger file will update when a folder is added / removed.
489set(syscalls_subdirs_trigger ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_subdirs.trigger)
490
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200491if(NOT (${CMAKE_HOST_SYSTEM_NAME} STREQUAL Windows))
492 set(syscalls_links --create-links ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_links)
493endif()
494
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200495# When running CMake it must be ensured that all dependencies are correctly acquired.
496execute_process(
497 COMMAND
498 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200499 ${ZEPHYR_BASE}/scripts/subfolder_list.py
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200500 --directory ${ZEPHYR_BASE}/include # Walk this directory
501 --out-file ${syscalls_subdirs_txt} # Write file with discovered folder
502 --trigger ${syscalls_subdirs_trigger} # Trigger file that is used for json generation
503 ${syscalls_links} # If defined, create symlinks for dependencies
504)
Carles Cufi3ad1f272019-07-18 10:38:25 +0200505file(STRINGS ${syscalls_subdirs_txt} PARSE_SYSCALLS_PATHS_DEPENDS ENCODING UTF-8)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200506
507if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL Windows)
508 # On windows only adding/removing files or folders will be reflected in depends.
509 # Hence adding a file requires CMake to re-run to add this file to the file list.
510 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${PARSE_SYSCALLS_PATHS_DEPENDS})
511
512 # Also On Windows each header file must be monitored as file modifications are not reflected
513 # on directory level.
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200514 file(GLOB_RECURSE PARSE_SYSCALLS_HEADER_DEPENDS ${ZEPHYR_BASE}/include/*.h)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200515else()
516 # The syscall parsing depends on the folders in order to detect add/removed/modified files.
517 # When a folder is removed, CMake will try to find a target that creates that dependency.
518 # This command sets up the target for CMake to find.
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200519 # Without this code, CMake will fail with the following error:
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200520 # <folder> needed by '<target>', missing and no known rule to make it
521 # when a folder is removed.
522 add_custom_command(OUTPUT ${PARSE_SYSCALLS_PATHS_DEPENDS}
523 COMMAND ${CMAKE_COMMAND} -E echo ""
524 COMMENT "Preparing syscall dependency handling"
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200525 )
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200526
527 add_custom_command(
528 OUTPUT
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200529 ${syscalls_subdirs_trigger}
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200530 COMMAND
531 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200532 ${ZEPHYR_BASE}/scripts/subfolder_list.py
533 --directory ${ZEPHYR_BASE}/include # Walk this directory
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200534 --out-file ${syscalls_subdirs_txt} # Write file with discovered folder
535 --trigger ${syscalls_subdirs_trigger} # Trigger file that is used for json generation
536 ${syscalls_links} # If defined, create symlinks for dependencies
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200537 DEPENDS ${PARSE_SYSCALLS_PATHS_DEPENDS}
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200538 )
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200539
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200540 # Ensure subdir file always exists when specifying CMake dependency.
541 if(NOT EXISTS ${syscalls_subdirs_txt})
542 file(WRITE ${syscalls_subdirs_txt} "")
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200543 endif()
544
545 # On other OS'es, modifying a file is reflected on the folder timestamp and hence detected
546 # when using depend on directory level.
547 # Thus CMake only needs to re-run when sub-directories are added / removed, which is indicated
548 # using a trigger file.
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200549 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${syscalls_subdirs_txt})
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200550endif()
551
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200552# syscall declarations are searched for in the SYSCALL_INCLUDE_DIRS
Adithya Baglodye67720b2018-07-02 14:59:19 +0530553if(CONFIG_APPLICATION_DEFINED_SYSCALL)
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200554 list(APPEND SYSCALL_INCLUDE_DIRS ${APPLICATION_SOURCE_DIR})
Adithya Baglodye67720b2018-07-02 14:59:19 +0530555endif()
556
Andrew Boiec1863872019-11-21 23:11:29 -0800557if(CONFIG_ZTEST)
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200558 list(APPEND SYSCALL_INCLUDE_DIRS ${ZEPHYR_BASE}/subsys/testsuite/ztest/include)
Andrew Boiec1863872019-11-21 23:11:29 -0800559endif()
560
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200561foreach(d ${SYSCALL_INCLUDE_DIRS})
562 list(APPEND parse_syscalls_include_args
563 --include ${d}
564 )
565endforeach()
566
Sebastian Bøe13a68402017-11-20 13:03:55 +0100567add_custom_command(
568 OUTPUT
569 ${syscalls_json}
Andrew Boie59601192020-05-29 13:24:51 -0700570 ${struct_tags_json}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100571 COMMAND
572 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200573 ${ZEPHYR_BASE}/scripts/parse_syscalls.py
Adithya Baglodye67720b2018-07-02 14:59:19 +0530574 --include ${ZEPHYR_BASE}/include # Read files from this dir
Andrew Boiefed960b2020-05-29 13:33:12 -0700575 --include ${ZEPHYR_BASE}/drivers # For net sockets
576 --include ${ZEPHYR_BASE}/subsys/net # More net sockets
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200577 ${parse_syscalls_include_args} # Read files from these dirs also
Corey Whartonccd15df2020-02-29 14:51:42 -0800578 --json-file ${syscalls_json} # Write this file
Andrew Boie59601192020-05-29 13:24:51 -0700579 --tag-struct-file ${struct_tags_json} # Write subsystem list to this file
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200580 DEPENDS ${syscalls_subdirs_trigger} ${PARSE_SYSCALLS_HEADER_DEPENDS}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100581 )
582
Joakim Andersson08d2f482020-08-04 18:26:43 +0200583add_custom_target(${SYSCALL_LIST_H_TARGET} DEPENDS ${syscall_list_h})
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +0100584
585# This only works for CMake version >=3.15, but as the property is ignored on
586# older CMake versions there is no reason to check for version.
587set_property(TARGET ${SYSCALL_LIST_H_TARGET}
588 APPEND PROPERTY
589 ADDITIONAL_CLEAN_FILES
590 ${CMAKE_CURRENT_BINARY_DIR}/include/generated/syscalls
591)
592
593# Only works with make.
594if(${CMAKE_VERSION} VERSION_LESS 3.15)
595 set_property(DIRECTORY
596 APPEND PROPERTY
597 ADDITIONAL_MAKE_CLEAN_FILES
598 ${CMAKE_CURRENT_BINARY_DIR}/include/generated/syscalls
599 )
600endif()
601
Andrew Boiec1c54b12020-03-16 12:48:00 -0700602add_custom_target(${PARSE_SYSCALLS_TARGET}
Joakim Andersson08d2f482020-08-04 18:26:43 +0200603 DEPENDS
604 ${syscalls_json}
Joakim Anderssond268f822020-08-04 18:31:48 +0200605 ${struct_tags_json}
Joakim Andersson08d2f482020-08-04 18:26:43 +0200606 )
Andrew Boie9ff64bb2019-11-05 09:39:05 -0800607
608# 64-bit systems do not require special handling of 64-bit system call
609# parameters or return values, indicate this to the system call boilerplate
610# generation script.
611if(CONFIG_64BIT)
612 set(SYSCALL_LONG_REGISTERS_ARG --long-registers)
613endif()
614
Andy Rosscfeb07e2020-03-05 21:14:02 -0800615if(CONFIG_TIMEOUT_64BIT)
616 set(SYSCALL_SPLIT_TIMEOUT_ARG --split-type k_timeout_t)
617endif()
618
Sebastian Bøe13a68402017-11-20 13:03:55 +0100619add_custom_command(OUTPUT include/generated/syscall_dispatch.c ${syscall_list_h}
620 # Also, some files are written to include/generated/syscalls/
621 COMMAND
622 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200623 ${ZEPHYR_BASE}/scripts/gen_syscalls.py
Sebastian Bøe13a68402017-11-20 13:03:55 +0100624 --json-file ${syscalls_json} # Read this file
625 --base-output include/generated/syscalls # Write to this dir
626 --syscall-dispatch include/generated/syscall_dispatch.c # Write this file
Andrew Boie353acf42018-07-23 18:10:15 -0700627 --syscall-list ${syscall_list_h}
Andrew Boie9ff64bb2019-11-05 09:39:05 -0800628 ${SYSCALL_LONG_REGISTERS_ARG}
Andy Rosscfeb07e2020-03-05 21:14:02 -0800629 ${SYSCALL_SPLIT_TIMEOUT_ARG}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100630 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
Andrew Boiec1c54b12020-03-16 12:48:00 -0700631 DEPENDS ${PARSE_SYSCALLS_TARGET}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100632 )
633
Corey Whartonccd15df2020-02-29 14:51:42 -0800634# This is passed into all calls to the gen_kobject_list.py script.
Andrew Boie59601192020-05-29 13:24:51 -0700635set(gen_kobject_list_include_args --include ${struct_tags_json})
Corey Whartonccd15df2020-02-29 14:51:42 -0800636
Leandro Pereirac2003672018-04-04 13:50:32 -0700637set(DRV_VALIDATION ${PROJECT_BINARY_DIR}/include/generated/driver-validation.h)
638add_custom_command(
639 OUTPUT ${DRV_VALIDATION}
640 COMMAND
641 ${PYTHON_EXECUTABLE}
642 ${ZEPHYR_BASE}/scripts/gen_kobject_list.py
643 --validation-output ${DRV_VALIDATION}
Corey Whartonccd15df2020-02-29 14:51:42 -0800644 ${gen_kobject_list_include_args}
Leandro Pereirac2003672018-04-04 13:50:32 -0700645 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Corey Whartonccd15df2020-02-29 14:51:42 -0800646 DEPENDS
647 ${ZEPHYR_BASE}/scripts/gen_kobject_list.py
Andrew Boiec1c54b12020-03-16 12:48:00 -0700648 ${PARSE_SYSCALLS_TARGET}
Leandro Pereirac2003672018-04-04 13:50:32 -0700649 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
650 )
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100651add_custom_target(${DRIVER_VALIDATION_H_TARGET} DEPENDS ${DRV_VALIDATION})
Leandro Pereirac2003672018-04-04 13:50:32 -0700652
Torsten Rasmussend7862cf2020-02-12 15:42:09 +0100653include(${ZEPHYR_BASE}/cmake/kobj.cmake)
Leandro Pereira39dc7d02018-04-05 13:59:33 -0700654gen_kobj(KOBJ_INCLUDE_PATH)
Leandro Pereirac2003672018-04-04 13:50:32 -0700655
Sebastian Bøefdac7b32020-01-23 15:39:17 +0100656# Add a pseudo-target that is up-to-date when all generated headers
657# are up-to-date.
658
659add_custom_target(zephyr_generated_headers)
660add_dependencies(zephyr_generated_headers
661 offsets_h
662 )
663
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200664# Generate offsets.c.obj from offsets.c
665# Generate offsets.h from offsets.c.obj
666
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100667set(OFFSETS_LIB offsets)
668
Klaus Petersenc66cb762018-11-15 10:37:46 +0100669set(OFFSETS_C_PATH ${ARCH_DIR}/${ARCH}/core/offsets/offsets.c)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200670set(OFFSETS_H_PATH ${PROJECT_BINARY_DIR}/include/generated/offsets.h)
671
Klaus Petersen62e55e52019-02-04 12:10:57 +0100672add_library( ${OFFSETS_LIB} OBJECT ${OFFSETS_C_PATH})
Stephanos Ioannidis2d746042019-10-25 00:08:21 +0900673target_include_directories(${OFFSETS_LIB} PRIVATE
674 kernel/include
675 ${ARCH_DIR}/${ARCH}/include
676 )
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100677target_link_libraries(${OFFSETS_LIB} zephyr_interface)
Joakim Anderssond268f822020-08-04 18:31:48 +0200678add_dependencies(zephyr_interface
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100679 ${SYSCALL_LIST_H_TARGET}
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100680 ${DRIVER_VALIDATION_H_TARGET}
681 ${KOBJ_TYPES_H_TARGET}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100682 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200683
684add_custom_command(
685 OUTPUT ${OFFSETS_H_PATH}
Carles Cufi7d764b32018-01-11 15:46:44 +0100686 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/gen_offset_header.py
Klaus Petersen62e55e52019-02-04 12:10:57 +0100687 -i $<TARGET_OBJECTS:${OFFSETS_LIB}>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200688 -o ${OFFSETS_H_PATH}
Sebastian Bøe5962aab2019-08-15 14:45:59 +0200689 DEPENDS
690 ${OFFSETS_LIB}
691 $<TARGET_OBJECTS:${OFFSETS_LIB}>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200692)
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100693add_custom_target(${OFFSETS_H_TARGET} DEPENDS ${OFFSETS_H_PATH})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200694
Sebastian Bøe89516fb2017-12-01 15:25:06 +0100695zephyr_get_include_directories_for_lang(C ZEPHYR_INCLUDES)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200696
697add_subdirectory(kernel)
698
699# Read list content
700get_property(ZEPHYR_LIBS_PROPERTY GLOBAL PROPERTY ZEPHYR_LIBS)
701
702foreach(zephyr_lib ${ZEPHYR_LIBS_PROPERTY})
Torsten Rasmussend9520042021-04-14 17:11:39 +0200703 get_property(lib_type TARGET ${zephyr_lib} PROPERTY TYPE)
Torsten Rasmussend9520042021-04-14 17:11:39 +0200704 # To prevent CMake failure when a driver is enabled, for example: REGULATOR=y
705 # we disable any Zephyr libraries without sources and adds the `empty_file.c`.
706 if(${lib_type} STREQUAL STATIC_LIBRARY
Torsten Rasmussend9520042021-04-14 17:11:39 +0200707 AND NOT ${zephyr_lib} STREQUAL app
708 )
Torsten Rasmussen25578be2021-04-23 09:09:49 +0200709 get_property(source_list TARGET ${zephyr_lib} PROPERTY SOURCES)
710 get_property(lib_imported TARGET ${zephyr_lib} PROPERTY IMPORTED)
711 if(NOT source_list
712 AND NOT ${lib_imported}
Torsten Rasmussend9520042021-04-14 17:11:39 +0200713 )
Torsten Rasmussen25578be2021-04-23 09:09:49 +0200714 message(WARNING
715 "No SOURCES given to Zephyr library: ${zephyr_lib}\nExcluding target from build."
716 )
717 target_sources(${zephyr_lib} PRIVATE ${ZEPHYR_BASE}/misc/empty_file.c)
718 set_property(TARGET ${zephyr_lib} PROPERTY EXCLUDE_FROM_ALL TRUE)
719 list(REMOVE_ITEM ZEPHYR_LIBS_PROPERTY ${zephyr_lib})
720 continue()
721 endif()
Torsten Rasmussend9520042021-04-14 17:11:39 +0200722 endif()
Torsten Rasmussen25578be2021-04-23 09:09:49 +0200723
724 # TODO: Could this become an INTERFACE property of zephyr_interface?
725 add_dependencies(${zephyr_lib} zephyr_generated_headers)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200726endforeach()
727
728get_property(OUTPUT_FORMAT GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT)
729
Adithya Baglody62e152a2018-11-13 15:34:02 +0530730if (CONFIG_CODE_DATA_RELOCATION)
731 set(CODE_RELOCATION_DEP code_relocation_source_lib)
732endif() # CONFIG_CODE_DATA_RELOCATION
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100733
Daniel Leungc7459952021-03-19 12:09:05 -0700734# Give the linker script targets all of the include directories so
735# that cmake can successfully find the linker scripts' header
Sebastian Bøeb1ab5012017-12-14 13:03:23 +0100736# dependencies.
737zephyr_get_include_directories_for_lang(C
738 ZEPHYR_INCLUDE_DIRS
739 STRIP_PREFIX # Don't use a -I prefix
740 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200741
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200742if(CONFIG_GEN_ISR_TABLES)
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530743 if(CONFIG_GEN_SW_ISR_TABLE)
744 list(APPEND GEN_ISR_TABLE_EXTRA_ARG --sw-isr-table)
745 endif()
746
747 if(CONFIG_GEN_IRQ_VECTOR_TABLE)
748 list(APPEND GEN_ISR_TABLE_EXTRA_ARG --vector-table)
749 endif()
750
Sebastian Bøe15260702019-01-14 16:31:02 +0100751 # isr_tables.c is generated from ${ZEPHYR_PREBUILT_EXECUTABLE} by
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200752 # gen_isr_tables.py
753 add_custom_command(
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +0100754 OUTPUT isr_tables.c isrList.bin
Torsten Rasmussenc060b072020-08-18 14:46:06 +0200755 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
756 $<TARGET_PROPERTY:bintools,elfconvert_flag>
757 $<TARGET_PROPERTY:bintools,elfconvert_flag_intarget>${OUTPUT_FORMAT}
758 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>binary
759 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_only>.intList
760 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>$<TARGET_FILE:${ZEPHYR_PREBUILT_EXECUTABLE}>
761 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>isrList.bin
Torsten Rasmussenf160dee2020-09-04 10:05:00 +0200762 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200763 COMMAND ${PYTHON_EXECUTABLE}
Carles Cufi7d764b32018-01-11 15:46:44 +0100764 ${ZEPHYR_BASE}/arch/common/gen_isr_tables.py
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200765 --output-source isr_tables.c
Sebastian Bøe15260702019-01-14 16:31:02 +0100766 --kernel $<TARGET_FILE:${ZEPHYR_PREBUILT_EXECUTABLE}>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200767 --intlist isrList.bin
Yasushi SHOJI6fc0d772018-10-09 18:59:16 +0900768 $<$<BOOL:${CONFIG_BIG_ENDIAN}>:--big-endian>
Sebastian Bøea55279a2018-01-04 14:08:39 +0100769 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--debug>
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530770 ${GEN_ISR_TABLE_EXTRA_ARG}
Sebastian Bøe15260702019-01-14 16:31:02 +0100771 DEPENDS ${ZEPHYR_PREBUILT_EXECUTABLE}
Torsten Rasmussenc060b072020-08-18 14:46:06 +0200772 COMMAND_EXPAND_LISTS
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200773 )
774 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_SOURCE_FILES isr_tables.c)
775endif()
776
Morten Priessa846e722021-04-21 09:06:02 +0200777if(CONFIG_HAS_DTS)
778 # dev_handles.c is generated from ${ZEPHYR_PREBUILT_EXECUTABLE} by
779 # gen_handles.py
780 add_custom_command(
781 OUTPUT dev_handles.c
782 COMMAND
783 ${PYTHON_EXECUTABLE}
784 ${ZEPHYR_BASE}/scripts/gen_handles.py
785 --output-source dev_handles.c
786 --kernel $<TARGET_FILE:${ZEPHYR_PREBUILT_EXECUTABLE}>
787 --zephyr-base ${ZEPHYR_BASE}
788 DEPENDS ${ZEPHYR_PREBUILT_EXECUTABLE}
789 )
790 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_SOURCE_FILES dev_handles.c)
791endif()
Peter Bigotd554d342020-06-30 10:05:35 -0500792
Adithya Baglody62e152a2018-11-13 15:34:02 +0530793if(CONFIG_CODE_DATA_RELOCATION)
Mark Ruvald Pedersen86a3e8f2019-05-03 10:33:03 +0200794 # @Intent: Linker script to relocate .text, data and .bss sections
795 toolchain_ld_relocation()
Adithya Baglody62e152a2018-11-13 15:34:02 +0530796endif()
797
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530798if(CONFIG_USERSPACE)
Torsten Rasmussene37d9e62020-11-20 18:39:30 +0100799 zephyr_get_compile_options_for_lang_as_string(C compiler_flags_priv)
Torsten Rasmussene0758c32020-08-21 19:13:53 +0200800 string(REPLACE "$<TARGET_PROPERTY:compiler,coverage>" ""
801 NO_COVERAGE_FLAGS "${compiler_flags_priv}"
802 )
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530803
804 get_property(include_dir_in_interface TARGET zephyr_interface
805 PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
806
807 get_property(sys_include_dir_in_interface TARGET zephyr_interface
808 PROPERTY INTERFACE_SYSTEM_INCLUDE_DIRECTORIES)
809
810 get_property(compile_definitions_interface TARGET zephyr_interface
811 PROPERTY INTERFACE_COMPILE_DEFINITIONS)
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530812
Carles Cufi7d764b32018-01-11 15:46:44 +0100813 set(GEN_KOBJ_LIST ${ZEPHYR_BASE}/scripts/gen_kobject_list.py)
814 set(PROCESS_GPERF ${ZEPHYR_BASE}/scripts/process_gperf.py)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200815
Daniel Leung28c35122021-03-18 12:02:19 -0700816 set(KOBJECT_HASH_LIST kobject_hash.gperf)
817 set(KOBJECT_HASH_OUTPUT_SRC_PRE kobject_hash_preprocessed.c)
818 set(KOBJECT_HASH_OUTPUT_SRC kobject_hash.c)
819 set(KOBJECT_HASH_OUTPUT_OBJ kobject_hash.c.obj)
820 set(KOBJECT_HASH_OUTPUT_OBJ_RENAMED kobject_hash_renamed.o)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200821
822 # Essentially what we are doing here is extracting some information
823 # out of the nearly finished elf file, generating the source code
824 # for a hash table based on that information, and then compiling and
825 # linking the hash table back into a now even more nearly finished
Marc Herbert4a10eea2019-04-16 15:39:45 -0700826 # elf file. More information in gen_kobject_list.py --help.
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200827
828 # Use the script GEN_KOBJ_LIST to scan the kernel binary's
Sebastian Bøe15260702019-01-14 16:31:02 +0100829 # (${ZEPHYR_PREBUILT_EXECUTABLE}) DWARF information to produce a table of kernel
Daniel Leung28c35122021-03-18 12:02:19 -0700830 # objects (KOBJECT_HASH_LIST) which we will then pass to gperf
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200831 add_custom_command(
Daniel Leung28c35122021-03-18 12:02:19 -0700832 OUTPUT ${KOBJECT_HASH_LIST}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200833 COMMAND
834 ${PYTHON_EXECUTABLE}
835 ${GEN_KOBJ_LIST}
Sebastian Bøe15260702019-01-14 16:31:02 +0100836 --kernel $<TARGET_FILE:${ZEPHYR_PREBUILT_EXECUTABLE}>
Daniel Leung28c35122021-03-18 12:02:19 -0700837 --gperf-output ${KOBJECT_HASH_LIST}
Corey Whartonccd15df2020-02-29 14:51:42 -0800838 ${gen_kobject_list_include_args}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200839 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Corey Whartonccd15df2020-02-29 14:51:42 -0800840 DEPENDS
841 ${ZEPHYR_PREBUILT_EXECUTABLE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200842 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
843 )
Daniel Leung28c35122021-03-18 12:02:19 -0700844 add_custom_target(
845 kobj_hash_list
846 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_LIST}
847 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200848
Daniel Leung28c35122021-03-18 12:02:19 -0700849 # Use gperf to generate C code (KOBJECT_HASH_OUTPUT_SRC_PRE) which implements a
850 # perfect hashtable based on KOBJECT_HASH_LIST
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200851 add_custom_command(
Daniel Leung28c35122021-03-18 12:02:19 -0700852 OUTPUT ${KOBJECT_HASH_OUTPUT_SRC_PRE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200853 COMMAND
854 ${GPERF}
Daniel Leung28c35122021-03-18 12:02:19 -0700855 --output-file ${KOBJECT_HASH_OUTPUT_SRC_PRE}
Daniel Leung11171692021-03-18 14:00:07 -0700856 --multiple-iterations 10
Daniel Leung28c35122021-03-18 12:02:19 -0700857 ${KOBJECT_HASH_LIST}
858 DEPENDS kobj_hash_list ${KOBJECT_HASH_LIST}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200859 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
860 )
Daniel Leung28c35122021-03-18 12:02:19 -0700861 add_custom_target(
862 kobj_hash_output_src_pre
863 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC_PRE}
864 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200865
866 # For our purposes the code/data generated by gperf is not optimal.
867 #
Daniel Leung28c35122021-03-18 12:02:19 -0700868 # The script PROCESS_GPERF creates a new c file KOBJECT_HASH_OUTPUT_SRC based on
869 # KOBJECT_HASH_OUTPUT_SRC_PRE to greatly reduce the amount of code/data generated
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200870 # since we know we are always working with pointer values
871 add_custom_command(
Daniel Leung28c35122021-03-18 12:02:19 -0700872 OUTPUT ${KOBJECT_HASH_OUTPUT_SRC}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200873 COMMAND
Sebastian Bøe1b600702018-06-21 14:34:42 +0200874 ${PYTHON_EXECUTABLE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200875 ${PROCESS_GPERF}
Daniel Leung28c35122021-03-18 12:02:19 -0700876 -i ${KOBJECT_HASH_OUTPUT_SRC_PRE}
877 -o ${KOBJECT_HASH_OUTPUT_SRC}
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700878 -p "struct z_object"
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200879 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Daniel Leung28c35122021-03-18 12:02:19 -0700880 DEPENDS kobj_hash_output_src_pre ${KOBJECT_HASH_OUTPUT_SRC_PRE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200881 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
882 )
Daniel Leung28c35122021-03-18 12:02:19 -0700883 add_custom_target(
884 kobj_hash_output_src
885 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC}
886 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200887
888 # We need precise control of where generated text/data ends up in the final
889 # kernel image. Disable function/data sections and use objcopy to move
890 # generated data into special section names
Daniel Leung28c35122021-03-18 12:02:19 -0700891 add_library(
892 kobj_hash_output_lib
893 STATIC ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_SRC}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200894 )
895
Daniel Leung28c35122021-03-18 12:02:19 -0700896 set_source_files_properties(${KOBJECT_HASH_OUTPUT_SRC}
897 PROPERTIES COMPILE_FLAGS
Andrew Boiea5148982019-03-14 17:04:11 -0700898 "${NO_COVERAGE_FLAGS} -fno-function-sections -fno-data-sections")
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530899
Daniel Leung28c35122021-03-18 12:02:19 -0700900 set_source_files_properties(${KOBJECT_HASH_OUTPUT_SRC}
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530901 PROPERTIES COMPILE_DEFINITIONS "${compile_definitions_interface}")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200902
903 # Turn off -ffunction-sections, etc.
Daniel Leung28c35122021-03-18 12:02:19 -0700904 # NB: Using a library instead of target_compile_options(kobj_hash_output_lib
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200905 # [...]) because a library's options have precedence
Daniel Leung28c35122021-03-18 12:02:19 -0700906 add_library(kobj_hash_output_lib_interface INTERFACE)
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530907
Daniel Leung28c35122021-03-18 12:02:19 -0700908 target_link_libraries(kobj_hash_output_lib kobj_hash_output_lib_interface)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200909
Kumar Gala3713ea42019-03-14 11:50:08 -0500910 foreach(incl ${include_dir_in_interface})
Daniel Leung28c35122021-03-18 12:02:19 -0700911 target_include_directories(kobj_hash_output_lib_interface INTERFACE ${incl})
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530912 endforeach()
913
Kumar Gala3713ea42019-03-14 11:50:08 -0500914 foreach(incl ${sys_include_dir_in_interface})
Daniel Leung28c35122021-03-18 12:02:19 -0700915 target_include_directories(kobj_hash_output_lib_interface SYSTEM INTERFACE ${incl})
Kumar Gala3713ea42019-03-14 11:50:08 -0500916 endforeach()
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530917
Daniel Leung28c35122021-03-18 12:02:19 -0700918 set(
919 KOBJECT_HASH_OUTPUT_OBJ_PATH
920 ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/kobj_hash_output_lib.dir/${KOBJECT_HASH_OUTPUT_OBJ}
921 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200922
923 add_custom_command(
Daniel Leung28c35122021-03-18 12:02:19 -0700924 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
Torsten Rasmussenc060b072020-08-18 14:46:06 +0200925 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
926 $<TARGET_PROPERTY:bintools,elfconvert_flag>
927 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.data=.kobject_data.data
928 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.text=.kobject_data.text
929 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.rodata=.kobject_data.rodata
Daniel Leung28c35122021-03-18 12:02:19 -0700930 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KOBJECT_HASH_OUTPUT_OBJ_PATH}
931 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +0200932 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Daniel Leung28c35122021-03-18 12:02:19 -0700933 DEPENDS kobj_hash_output_lib
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200934 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
Torsten Rasmussenc060b072020-08-18 14:46:06 +0200935 COMMAND_EXPAND_LISTS
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200936 )
Daniel Leung28c35122021-03-18 12:02:19 -0700937 add_custom_target(
938 kobj_hash_output_obj_renamed
939 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
940 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200941
Daniel Leung28c35122021-03-18 12:02:19 -0700942 add_library(kobj_hash_output_obj_renamed_lib STATIC IMPORTED GLOBAL)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200943 set_property(
Daniel Leung28c35122021-03-18 12:02:19 -0700944 TARGET kobj_hash_output_obj_renamed_lib
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200945 PROPERTY
Daniel Leung28c35122021-03-18 12:02:19 -0700946 IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_HASH_OUTPUT_OBJ_RENAMED}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200947 )
948 add_dependencies(
Daniel Leung28c35122021-03-18 12:02:19 -0700949 kobj_hash_output_obj_renamed_lib
950 kobj_hash_output_obj_renamed
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200951 )
952
Daniel Leung28c35122021-03-18 12:02:19 -0700953 set_property(
954 GLOBAL APPEND PROPERTY
955 GENERATED_KERNEL_OBJECT_FILES kobj_hash_output_obj_renamed_lib
956 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200957endif()
958
959# Read global variables into local variables
960get_property(GKOF GLOBAL PROPERTY GENERATED_KERNEL_OBJECT_FILES)
961get_property(GKSF GLOBAL PROPERTY GENERATED_KERNEL_SOURCE_FILES)
962
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200963
Alberto Escolar Piedrasc2882412018-07-05 10:51:03 +0200964get_property(CSTD GLOBAL PROPERTY CSTD)
965set_ifndef(CSTD c99)
966
Danny Oerndrup6331dae2019-06-13 15:33:03 +0200967# @Intent: Obtain compiler specific flag for specifying the c standard
Alberto Escolar Piedrasc2882412018-07-05 10:51:03 +0200968zephyr_compile_options(
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200969 $<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,cstd>${CSTD}>
Alberto Escolar Piedrasc2882412018-07-05 10:51:03 +0200970)
971
Mark Ruvald Pedersen197197a2019-05-03 11:02:56 +0200972# @Intent: Configure linker scripts, i.e. generate linker scripts with variables substituted
973toolchain_ld_configure_files()
Daniel Leung212ec9a2019-03-10 14:20:21 -0700974
Andrew Boie4ce652e2019-02-22 16:08:44 -0800975if(CONFIG_USERSPACE)
Daniel Leung212ec9a2019-03-10 14:20:21 -0700976 set(APP_SMEM_ALIGNED_LD "${PROJECT_BINARY_DIR}/include/generated/app_smem_aligned.ld")
977 set(APP_SMEM_UNALIGNED_LD "${PROJECT_BINARY_DIR}/include/generated/app_smem_unaligned.ld")
Adithya Baglody50950eb2018-12-20 15:47:42 +0530978 set(OBJ_FILE_DIR "${PROJECT_BINARY_DIR}/../")
979
980 add_custom_target(
Daniel Leung212ec9a2019-03-10 14:20:21 -0700981 ${APP_SMEM_ALIGNED_DEP}
Sebastian Bøe2a963122019-02-08 15:49:57 +0100982 DEPENDS
Daniel Leung212ec9a2019-03-10 14:20:21 -0700983 ${APP_SMEM_ALIGNED_LD}
984 )
985
986 add_custom_target(
987 ${APP_SMEM_UNALIGNED_DEP}
988 DEPENDS
989 ${APP_SMEM_UNALIGNED_LD}
Adithya Baglody50950eb2018-12-20 15:47:42 +0530990 )
991
Andrew Boie4b4f7732019-02-01 12:18:31 -0800992 if(CONFIG_NEWLIB_LIBC)
Andrew Boie17ce8222019-02-21 13:44:54 -0800993 set(NEWLIB_PART -l libc.a z_libc_partition)
Andrew Boie4b4f7732019-02-01 12:18:31 -0800994 endif()
Ioannis Glaropoulosd58f8be2019-11-15 14:07:51 +0100995 if(CONFIG_NEWLIB_LIBC_NANO)
996 set(NEWLIB_PART -l libc_nano.a z_libc_partition)
997 endif()
Andrew Boiee686aef2019-02-27 14:41:45 -0800998
Adithya Baglody50950eb2018-12-20 15:47:42 +0530999 add_custom_command(
Daniel Leung212ec9a2019-03-10 14:20:21 -07001000 OUTPUT ${APP_SMEM_UNALIGNED_LD}
Adithya Baglody50950eb2018-12-20 15:47:42 +05301001 COMMAND ${PYTHON_EXECUTABLE}
1002 ${ZEPHYR_BASE}/scripts/gen_app_partitions.py
1003 -d ${OBJ_FILE_DIR}
Daniel Leung212ec9a2019-03-10 14:20:21 -07001004 -o ${APP_SMEM_UNALIGNED_LD}
Torsten Rasmussen5a703c82019-11-05 10:24:08 +01001005 ${NEWLIB_PART}
Torsten Rasmussen1f9723a2019-11-04 14:30:24 +01001006 $<TARGET_PROPERTY:zephyr_property_target,COMPILE_OPTIONS>
Adithya Baglody50950eb2018-12-20 15:47:42 +05301007 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Sebastian Bøe2a963122019-02-08 15:49:57 +01001008 DEPENDS
1009 kernel
1010 ${ZEPHYR_LIBS_PROPERTY}
Adithya Baglody50950eb2018-12-20 15:47:42 +05301011 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
Torsten Rasmussen1f9723a2019-11-04 14:30:24 +01001012 COMMAND_EXPAND_LISTS
Daniel Leung212ec9a2019-03-10 14:20:21 -07001013 COMMENT "Generating app_smem_unaligned linker section"
1014 )
1015
Mark Ruvald Pedersen4c811972019-04-29 17:16:54 +02001016 configure_linker_script(
Mark Ruvald Pedersen1073fba2019-04-29 20:27:23 +02001017 linker_app_smem_unaligned.cmd
Mark Ruvald Pedersenfbcea172019-04-29 20:35:12 +02001018 "-DLINKER_APP_SMEM_UNALIGNED"
Daniel Leung212ec9a2019-03-10 14:20:21 -07001019 ${CODE_RELOCATION_DEP}
1020 ${APP_SMEM_UNALIGNED_DEP}
1021 ${APP_SMEM_UNALIGNED_LD}
Sebastian Bøefdac7b32020-01-23 15:39:17 +01001022 zephyr_generated_headers
Daniel Leung212ec9a2019-03-10 14:20:21 -07001023 )
Daniel Leung212ec9a2019-03-10 14:20:21 -07001024
1025 add_custom_target(
1026 linker_app_smem_unaligned_script
1027 DEPENDS
1028 linker_app_smem_unaligned.cmd
1029 )
1030
1031 set_property(TARGET
1032 linker_app_smem_unaligned_script
1033 PROPERTY INCLUDE_DIRECTORIES
1034 ${ZEPHYR_INCLUDE_DIRS}
1035 )
1036
1037 set(APP_SMEM_UNALIGNED_LIB app_smem_unaligned_output_obj_renamed_lib)
1038 add_executable( app_smem_unaligned_prebuilt misc/empty_file.c)
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001039 toolchain_ld_link_elf(
1040 TARGET_ELF app_smem_unaligned_prebuilt
Marc Herbert0370c9b2019-06-13 16:15:44 -07001041 OUTPUT_MAP ${PROJECT_BINARY_DIR}/app_smem_unaligned_prebuilt.map
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001042 LIBRARIES_PRE_SCRIPT ""
1043 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/linker_app_smem_unaligned.cmd
1044 LIBRARIES_POST_SCRIPT ""
1045 DEPENDENCIES ${CODE_RELOCATION_DEP}
1046 )
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +01001047 target_byproducts(TARGET app_smem_unaligned_prebuilt
1048 BYPRODUCTS ${PROJECT_BINARY_DIR}/app_smem_unaligned_prebuilt.map
1049 )
Daniel Leung212ec9a2019-03-10 14:20:21 -07001050 set_property(TARGET app_smem_unaligned_prebuilt PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker_app_smem_unaligned.cmd)
Mark Ruvald Pedersen85af2802019-04-29 14:49:33 +02001051 add_dependencies( app_smem_unaligned_prebuilt linker_app_smem_unaligned_script ${OFFSETS_LIB})
Daniel Leung212ec9a2019-03-10 14:20:21 -07001052
1053 add_custom_command(
1054 OUTPUT ${APP_SMEM_ALIGNED_LD}
1055 COMMAND ${PYTHON_EXECUTABLE}
1056 ${ZEPHYR_BASE}/scripts/gen_app_partitions.py
1057 -e $<TARGET_FILE:app_smem_unaligned_prebuilt>
1058 -o ${APP_SMEM_ALIGNED_LD}
Torsten Rasmussen5a703c82019-11-05 10:24:08 +01001059 ${NEWLIB_PART}
Torsten Rasmussen1f9723a2019-11-04 14:30:24 +01001060 $<TARGET_PROPERTY:zephyr_property_target,COMPILE_OPTIONS>
Daniel Leung212ec9a2019-03-10 14:20:21 -07001061 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1062 DEPENDS
1063 kernel
1064 ${ZEPHYR_LIBS_PROPERTY}
1065 app_smem_unaligned_prebuilt
1066 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
Torsten Rasmussen1f9723a2019-11-04 14:30:24 +01001067 COMMAND_EXPAND_LISTS
Daniel Leung212ec9a2019-03-10 14:20:21 -07001068 COMMENT "Generating app_smem_aligned linker section"
Adithya Baglody50950eb2018-12-20 15:47:42 +05301069 )
1070endif()
1071
Daniel Leung11171692021-03-18 14:00:07 -07001072if(CONFIG_USERSPACE)
1073 # This CONFIG_USERSPACE block is to create place holders to reserve space
1074 # for the gperf generated structures for zephyr_prebuilt.elf.
1075 # These place holders are there so that the placement of kobjects would be
1076 # the same between linking zephyr_prebuilt.elf and zephyr.elf, as
1077 # the gperf hash table is hashed on the addresses of kobjects.
1078 # The placeholders are generated from app_smem_unaligned_prebuilt.elf.
1079
1080 set(KOBJECT_PREBUILT_HASH_LIST kobject_prebuilt_hash.gperf)
1081 set(KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE kobject_prebuilt_hash_preprocessed.c)
1082 set(KOBJECT_PREBUILT_HASH_OUTPUT_SRC kobject_prebuilt_hash.c)
1083 set(KOBJECT_PREBUILT_HASH_OUTPUT_OBJ kobject_prebuilt_hash.c.obj)
1084
1085 add_custom_command(
1086 OUTPUT ${KOBJECT_PREBUILT_HASH_LIST}
1087 COMMAND
1088 ${PYTHON_EXECUTABLE}
1089 ${GEN_KOBJ_LIST}
1090 --kernel $<TARGET_FILE:app_smem_unaligned_prebuilt>
1091 --gperf-output ${KOBJECT_PREBUILT_HASH_LIST}
1092 ${gen_kobject_list_include_args}
1093 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1094 DEPENDS
1095 app_smem_unaligned_prebuilt
1096 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1097 )
1098 add_custom_target(
1099 kobj_prebuilt_hash_list
1100 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_LIST}
1101 )
1102
1103 add_custom_command(
1104 OUTPUT ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
1105 COMMAND
1106 ${GPERF}
1107 --output-file ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
1108 --multiple-iterations 10
1109 ${KOBJECT_PREBUILT_HASH_LIST}
1110 DEPENDS kobj_prebuilt_hash_list ${KOBJECT_PREBUILT_HASH_LIST}
1111 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1112 )
1113 add_custom_target(
1114 kobj_prebuilt_hash_output_src_pre
1115 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
1116 )
1117
1118 add_custom_command(
1119 OUTPUT ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1120 COMMAND
1121 ${PYTHON_EXECUTABLE}
1122 ${PROCESS_GPERF}
1123 -i ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
1124 -o ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1125 -p "struct z_object"
1126 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1127 DEPENDS kobj_prebuilt_hash_output_src_pre ${KOBJECT_PREBUILT_HASH_OUTPUT_SRC_PRE}
1128 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1129 )
1130 add_custom_target(
1131 kobj_prebuilt_hash_output_src
1132 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1133 )
1134
1135 add_library(
1136 kobj_prebuilt_hash_output_lib
1137 STATIC ${CMAKE_CURRENT_BINARY_DIR}/${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1138 )
1139
1140 set_source_files_properties(${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1141 PROPERTIES COMPILE_FLAGS
1142 "${NO_COVERAGE_FLAGS} -fno-function-sections -fno-data-sections")
1143
1144 set_source_files_properties(${KOBJECT_PREBUILT_HASH_OUTPUT_SRC}
1145 PROPERTIES COMPILE_DEFINITIONS "${compile_definitions_interface}")
1146
1147 add_library(kobj_prebuilt_hash_output_lib_interface INTERFACE)
1148
1149 target_link_libraries(
1150 kobj_prebuilt_hash_output_lib
1151 kobj_prebuilt_hash_output_lib_interface
1152 )
1153
1154 foreach(incl ${include_dir_in_interface})
1155 target_include_directories(
1156 kobj_prebuilt_hash_output_lib_interface
1157 INTERFACE ${incl}
1158 )
1159 endforeach()
1160
1161 foreach(incl ${sys_include_dir_in_interface})
1162 target_include_directories(
1163 kobj_prebuilt_hash_output_lib_interface
1164 SYSTEM INTERFACE ${incl}
1165 )
1166 endforeach()
1167
1168 set(
1169 KOBJECT_PREBUILT_HASH_OUTPUT_OBJ_PATH
1170 ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/kobj_prebuilt_hash_output_lib.dir/${KOBJECT_PREBUILT_HASH_OUTPUT_OBJ}
1171 )
1172
1173 set(KOBJECT_LINKER_HEADER_DATA "${PROJECT_BINARY_DIR}/include/generated/linker-kobject-prebuilt-data.h")
1174
1175 add_custom_command(
1176 OUTPUT ${KOBJECT_LINKER_HEADER_DATA}
1177 COMMAND
1178 ${PYTHON_EXECUTABLE}
1179 ${ZEPHYR_BASE}/scripts/gen_kobject_placeholders.py
1180 --object ${KOBJECT_PREBUILT_HASH_OUTPUT_OBJ_PATH}
1181 --outdir ${PROJECT_BINARY_DIR}/include/generated
1182 --datapct ${CONFIG_KOBJECT_DATA_AREA_RESERVE_EXTRA_PERCENT}
1183 --rodata ${CONFIG_KOBJECT_RODATA_AREA_EXTRA_BYTES}
1184 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1185 DEPENDS
1186 kobj_prebuilt_hash_output_lib
1187 ${KOBJECT_PREBUILT_HASH_OUTPUT_OBJ_PATH}
1188 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1189 )
1190
1191 add_custom_target(
1192 ${KOBJECT_LINKER_DEP}
1193 DEPENDS
1194 ${KOBJECT_LINKER_HEADER_DATA}
1195 )
1196endif()
1197
Daniel Leungc7459952021-03-19 12:09:05 -07001198configure_linker_script(
1199 linker_zephyr_prebuilt.cmd
1200 "-DLINKER_ZEPHYR_PREBUILT"
1201 ${APP_SMEM_ALIGNED_DEP}
Daniel Leung11171692021-03-18 14:00:07 -07001202 ${KOBJECT_LINKER_DEP}
Daniel Leungc7459952021-03-19 12:09:05 -07001203 ${CODE_RELOCATION_DEP}
1204 zephyr_generated_headers
1205 )
1206
1207add_custom_target(
1208 linker_zephyr_prebuilt_script_target
1209 DEPENDS
1210 linker_zephyr_prebuilt.cmd
1211 )
1212
1213set_property(TARGET
1214 linker_zephyr_prebuilt_script_target
1215 PROPERTY INCLUDE_DIRECTORIES
1216 ${ZEPHYR_INCLUDE_DIRS}
1217 )
1218
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001219# FIXME: Is there any way to get rid of empty_file.c?
Sebastian Bøe15260702019-01-14 16:31:02 +01001220add_executable( ${ZEPHYR_PREBUILT_EXECUTABLE} misc/empty_file.c)
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001221toolchain_ld_link_elf(
1222 TARGET_ELF ${ZEPHYR_PREBUILT_EXECUTABLE}
Marc Herbert0370c9b2019-06-13 16:15:44 -07001223 OUTPUT_MAP ${PROJECT_BINARY_DIR}/${ZEPHYR_PREBUILT_EXECUTABLE}.map
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001224 LIBRARIES_PRE_SCRIPT ""
Daniel Leungc7459952021-03-19 12:09:05 -07001225 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/linker_zephyr_prebuilt.cmd
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001226 DEPENDENCIES ${CODE_RELOCATION_DEP}
1227)
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +01001228target_byproducts(TARGET ${ZEPHYR_PREBUILT_EXECUTABLE}
1229 BYPRODUCTS ${PROJECT_BINARY_DIR}/${ZEPHYR_PREBUILT_EXECUTABLE}.map
1230)
Daniel Leungc7459952021-03-19 12:09:05 -07001231set_property(TARGET
1232 ${ZEPHYR_PREBUILT_EXECUTABLE}
1233 PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker_zephyr_prebuilt.cmd
1234 )
1235add_dependencies(
1236 ${ZEPHYR_PREBUILT_EXECUTABLE}
1237 linker_zephyr_prebuilt_script_target
1238 ${OFFSETS_LIB}
1239 )
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +02001240
Marc Herbert498b4942019-04-16 23:30:52 -07001241set(generated_kernel_files ${GKSF} ${GKOF})
1242if(NOT generated_kernel_files)
1243 # Use the prebuilt elf as the final elf since we don't have a
1244 # generation stage.
1245 set(logical_target_for_zephyr_elf ${ZEPHYR_PREBUILT_EXECUTABLE})
1246else()
Daniel Leungcdd02a92021-03-19 12:18:52 -07001247 # The final linker pass uses the same source linker script of the
1248 # previous passes, but this time with a different output
1249 # file and preprocessed with the define LINKER_ZEPHYR_FINAL.
1250 #
1251 # LINKER_PASS2 is deprecated but being kept to avoid breaking
1252 # external projects. It will be removed in the future.
Mark Ruvald Pedersen4c811972019-04-29 17:16:54 +02001253 configure_linker_script(
Daniel Leungcdd02a92021-03-19 12:18:52 -07001254 linker.cmd
1255 "-DLINKER_ZEPHYR_FINAL;-DLINKER_PASS2"
Sebastian Bøe2a963122019-02-08 15:49:57 +01001256 ${CODE_RELOCATION_DEP}
1257 ${ZEPHYR_PREBUILT_EXECUTABLE}
Sebastian Bøefdac7b32020-01-23 15:39:17 +01001258 zephyr_generated_headers
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +01001259 )
Andy Grosse8860fe2018-02-01 01:12:32 -06001260
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001261 add_custom_target(
Daniel Leungcdd02a92021-03-19 12:18:52 -07001262 linker_zephyr_final_script_target
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001263 DEPENDS
Daniel Leungcdd02a92021-03-19 12:18:52 -07001264 linker.cmd
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001265 )
Sebastian Bøeb1ab5012017-12-14 13:03:23 +01001266 set_property(TARGET
Daniel Leungcdd02a92021-03-19 12:18:52 -07001267 linker_zephyr_final_script_target
Sebastian Bøeb1ab5012017-12-14 13:03:23 +01001268 PROPERTY INCLUDE_DIRECTORIES
1269 ${ZEPHYR_INCLUDE_DIRS}
1270 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001271
Mark Ruvald Pedersen37d49472019-05-07 15:20:20 +02001272 add_executable( ${ZEPHYR_FINAL_EXECUTABLE} misc/empty_file.c ${GKSF})
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001273 toolchain_ld_link_elf(
1274 TARGET_ELF ${ZEPHYR_FINAL_EXECUTABLE}
Marc Herbert0370c9b2019-06-13 16:15:44 -07001275 OUTPUT_MAP ${PROJECT_BINARY_DIR}/${ZEPHYR_FINAL_EXECUTABLE}.map
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001276 LIBRARIES_PRE_SCRIPT ${GKOF}
Daniel Leungcdd02a92021-03-19 12:18:52 -07001277 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/linker.cmd
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001278 LIBRARIES_POST_SCRIPT ""
1279 DEPENDENCIES ${CODE_RELOCATION_DEP}
1280 )
Daniel Leungcdd02a92021-03-19 12:18:52 -07001281 set_property(TARGET ${ZEPHYR_FINAL_EXECUTABLE} PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker.cmd)
1282 add_dependencies( ${ZEPHYR_FINAL_EXECUTABLE} linker_zephyr_final_script_target)
Mark Ruvald Pedersen37d49472019-05-07 15:20:20 +02001283
1284 # Use the pass2 elf as the final elf
1285 set(logical_target_for_zephyr_elf ${ZEPHYR_FINAL_EXECUTABLE})
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001286endif()
1287
Sebastian Bøe0fc39342018-10-16 13:25:04 +02001288# Export the variable to the application's scope to allow the
1289# application to know what the name of the final elf target is.
1290set(logical_target_for_zephyr_elf ${logical_target_for_zephyr_elf} PARENT_SCOPE)
1291
Marc Herbert0370c9b2019-06-13 16:15:44 -07001292# Override the base name of the last, "logical" .elf output (and last .map) so:
Marc Herbert498b4942019-04-16 23:30:52 -07001293# 1. it doesn't depend on the number of passes above and the
1294# post_build_commands below can always find it no matter which is it;
1295# 2. it can be defined in Kconfig
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001296set_target_properties(${logical_target_for_zephyr_elf} PROPERTIES OUTPUT_NAME ${KERNEL_NAME})
1297
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001298set(post_build_commands "")
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001299set(post_build_byproducts "")
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001300
Marc Herbert0370c9b2019-06-13 16:15:44 -07001301list(APPEND
1302 post_build_commands
1303 COMMAND
Torsten Rasmussen4df38c72020-06-11 10:48:06 +02001304 ${CMAKE_COMMAND} -E rename ${logical_target_for_zephyr_elf}.map ${KERNEL_MAP_NAME}
Marc Herbert0370c9b2019-06-13 16:15:44 -07001305)
Torsten Rasmussenc4c79f52021-02-09 22:27:59 +01001306list(APPEND post_build_byproducts ${KERNEL_MAP_NAME})
Marc Herbert0370c9b2019-06-13 16:15:44 -07001307
Håkon Øye Amundsenc086b932018-11-26 09:47:16 +00001308if(NOT CONFIG_BUILD_NO_GAP_FILL)
1309 # Use ';' as separator to get proper space in resulting command.
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001310 set(GAP_FILL "$<TARGET_PROPERTY:bintools,elfconvert_flag_gapfill>0xff")
Håkon Øye Amundsenc086b932018-11-26 09:47:16 +00001311endif()
1312
Danny Oerndrupc41e7122019-07-18 15:16:39 +02001313if(CONFIG_OUTPUT_PRINT_MEMORY_USAGE)
Torsten Rasmussend537be02021-02-02 21:06:11 +01001314 target_link_libraries(${logical_target_for_zephyr_elf} $<TARGET_PROPERTY:linker,memusage>)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001315
1316 get_property(memusage_build_command TARGET bintools PROPERTY memusage_command)
1317 if(memusage_build_command)
1318 # Note: The use of generator expressions allows downstream extensions to add/change the post build.
1319 # Unfortunately, the BYPRODUCTS does not allow for generator expression, so question is if we
1320 # should remove the downstream ability from start.
1321 # Or fix the output name, by the use of `get_property`
1322 list(APPEND
1323 post_build_commands
Torsten Rasmussen571f48f2020-09-04 21:07:46 +02001324 COMMAND $<TARGET_PROPERTY:bintools,memusage_command>
1325 $<TARGET_PROPERTY:bintools,memusage_flag>
1326 $<TARGET_PROPERTY:bintools,memusage_infile>${KERNEL_ELF_NAME}
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001327 )
1328
1329 # For now, the byproduct can only be supported upstream on byproducts name,
1330 # cause byproduct does not support generator expressions
1331 get_property(memusage_byproducts TARGET bintools PROPERTY memusage_byproducts)
1332 list(APPEND
1333 post_build_byproducts
1334 ${memusage_byproducts}
1335 )
1336 endif()
Danny Oerndrupc41e7122019-07-18 15:16:39 +02001337endif()
1338
Kumar Galad5419132019-08-13 13:44:20 -05001339if(CONFIG_BUILD_OUTPUT_HEX OR BOARD_FLASH_RUNNER STREQUAL openocd)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001340 get_property(elfconvert_formats TARGET bintools PROPERTY elfconvert_formats)
1341 if(ihex IN_LIST elfconvert_formats)
1342 list(APPEND
1343 post_build_commands
1344 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1345 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1346 ${GAP_FILL}
1347 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>ihex
1348 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>.comment
1349 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>COMMON
1350 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>.eh_frame
1351 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1352 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_HEX_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001353 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001354 )
1355 list(APPEND
1356 post_build_byproducts
1357 ${KERNEL_HEX_NAME}
1358 # ${out_hex_byprod} # Is this needed ?
1359 )
1360 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001361endif()
Anas Nashif4592ff22017-11-23 07:54:26 -05001362
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001363if(CONFIG_BUILD_OUTPUT_BIN)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001364 get_property(elfconvert_formats TARGET bintools PROPERTY elfconvert_formats)
1365 if(binary IN_LIST elfconvert_formats)
1366 list(APPEND
1367 post_build_commands
1368 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1369 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1370 ${GAP_FILL}
1371 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>binary
1372 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>.comment
1373 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>COMMON
1374 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>.eh_frame
1375 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1376 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_BIN_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001377 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001378 )
1379 list(APPEND
1380 post_build_byproducts
1381 ${KERNEL_BIN_NAME}
1382 # ${out_hex_byprod} # Is this needed ?
1383 )
1384 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001385endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001386
Pete Johanson310a4642020-12-31 16:51:52 -05001387if(CONFIG_BUILD_OUTPUT_BIN AND CONFIG_BUILD_OUTPUT_UF2)
1388 list(APPEND
1389 post_build_commands
1390 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/uf2conv.py
1391 -c
1392 -f ${CONFIG_BUILD_OUTPUT_UF2_FAMILY_ID}
1393 -b ${CONFIG_FLASH_LOAD_OFFSET}
1394 -o ${KERNEL_UF2_NAME}
1395 ${KERNEL_BIN_NAME}
1396 )
1397 list(APPEND
1398 post_build_byproducts
1399 ${KERNEL_UF2_NAME}
1400 )
1401endif()
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001402
1403# Cleanup intermediate files
1404if(CONFIG_CLEANUP_INTERMEDIATE_FILES)
1405 list(APPEND
1406 post_build_commands
1407 COMMAND
1408 # This file can be very large in some cases, delete it as we do not need it.
1409 ${CMAKE_COMMAND} -E remove ${ZEPHYR_PREBUILT_EXECUTABLE}.elf
1410 )
1411endif()
1412
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001413if(CONFIG_BUILD_OUTPUT_S19)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001414 get_property(elfconvert_formats TARGET bintools PROPERTY elfconvert_formats)
1415 if(srec IN_LIST elfconvert_formats)
1416 # Should we print a warning if case the tools does not support converting to s19 ?
1417 list(APPEND
1418 post_build_commands
1419 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1420 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1421 ${GAP_FILL}
1422 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>srec
1423 $<TARGET_PROPERTY:bintools,elfconvert_flag_srec_len>1
1424 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1425 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_S19_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001426 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001427 )
1428 list(APPEND
1429 post_build_byproducts
1430 ${KERNEL_S19_NAME}
1431 # ${out_S19_byprod} # Is this needed ?
1432
1433 )
1434 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001435endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001436
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001437if(CONFIG_OUTPUT_DISASSEMBLY)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001438if(CONFIG_OUTPUT_DISASSEMBLE_ALL)
1439 set(disassembly_type "$<TARGET_PROPERTY:bintools,disassembly_flag_all>")
Rohit Gujarathi35713f22020-05-07 10:08:37 +05301440 else()
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001441 set(disassembly_type "$<TARGET_PROPERTY:bintools,disassembly_flag_inline_source>")
Rohit Gujarathi35713f22020-05-07 10:08:37 +05301442 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001443 list(APPEND
1444 post_build_commands
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001445 COMMAND $<TARGET_PROPERTY:bintools,disassembly_command>
1446 $<TARGET_PROPERTY:bintools,disassembly_flag>
1447 ${disassembly_type}
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001448 $<TARGET_PROPERTY:bintools,disassembly_flag_infile>${KERNEL_ELF_NAME}
1449 $<TARGET_PROPERTY:bintools,disassembly_flag_outfile>${KERNEL_LST_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001450 $<TARGET_PROPERTY:bintools,disassembly_flag_final>
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001451 )
1452 list(APPEND
1453 post_build_byproducts
1454 ${KERNEL_LST_NAME}
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001455# ${out_disassembly_byprod} # Needed ??
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001456 )
1457endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001458
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001459if(CONFIG_OUTPUT_STAT)
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001460# zephyr_post_build(TOOLS bintools COMMAND readelf FLAGS headers INFILE file OUTFILE outfile)
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001461 list(APPEND
1462 post_build_commands
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001463 COMMAND $<TARGET_PROPERTY:bintools,readelf_command>
1464 $<TARGET_PROPERTY:bintools,readelf_flag>
1465 $<TARGET_PROPERTY:bintools,readelf_flag_headers>
1466 $<TARGET_PROPERTY:bintools,readelf_flag_infile> ${KERNEL_ELF_NAME}
1467 $<TARGET_PROPERTY:bintools,readelf_flag_outfile> ${KERNEL_STAT_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001468 $<TARGET_PROPERTY:bintools,readelf_flag_final>
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001469 )
1470 list(APPEND
1471 post_build_byproducts
1472 ${KERNEL_STAT_NAME}
1473 )
1474endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001475
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001476if(CONFIG_BUILD_OUTPUT_STRIPPED)
1477 list(APPEND
1478 post_build_commands
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001479 COMMAND $<TARGET_PROPERTY:bintools,strip_command>
1480 $<TARGET_PROPERTY:bintools,strip_flag>
1481 $<TARGET_PROPERTY:bintools,strip_flag_all>
1482 $<TARGET_PROPERTY:bintools,strip_flag_infile>${KERNEL_ELF_NAME}
1483 $<TARGET_PROPERTY:bintools,strip_flag_outfile>${KERNEL_STRIP_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001484 $<TARGET_PROPERTY:bintools,strip_flag_final>
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001485 )
1486 list(APPEND
1487 post_build_byproducts
1488 ${KERNEL_STRIP_NAME}
1489 )
1490endif()
1491
1492if(CONFIG_BUILD_OUTPUT_EXE)
1493 list(APPEND
1494 post_build_commands
1495 COMMAND
1496 ${CMAKE_COMMAND} -E copy ${KERNEL_ELF_NAME} ${KERNEL_EXE_NAME}
1497 )
1498 list(APPEND
1499 post_build_byproducts
1500 ${KERNEL_EXE_NAME}
1501 )
1502endif()
Anas Nashif4592ff22017-11-23 07:54:26 -05001503
Martí Bolívarf66a0c32020-08-18 11:28:04 -07001504# Generate and use MCUboot related artifacts as needed.
1505if(CONFIG_BOOTLOADER_MCUBOOT)
1506 include(${CMAKE_CURRENT_LIST_DIR}/cmake/mcuboot.cmake)
1507endif()
1508
Rajavardhan Gundiecdea1c2019-01-25 11:53:13 +05301509get_property(extra_post_build_commands
1510 GLOBAL PROPERTY
1511 extra_post_build_commands
1512 )
1513
1514list(APPEND
1515 post_build_commands
1516 ${extra_post_build_commands}
1517 )
1518
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001519get_property(extra_post_build_byproducts
1520 GLOBAL PROPERTY
1521 extra_post_build_byproducts
1522 )
1523
1524list(APPEND
1525 post_build_byproducts
1526 ${extra_post_build_byproducts}
1527 )
1528
Daniel Leunga5ab1a72021-04-02 12:54:53 -07001529if(CONFIG_LOG_DICTIONARY_SUPPORT)
1530 set(LOG_DICT_DB_NAME ${PROJECT_BINARY_DIR}/log_dictionary.json)
1531
1532 list(APPEND
1533 post_build_commands
1534 COMMAND
1535 ${PYTHON_EXECUTABLE}
1536 ${ZEPHYR_BASE}/scripts/logging/dictionary/database_gen.py
1537 ${KERNEL_ELF_NAME}
1538 ${LOG_DICT_DB_NAME}
1539 --build ${BUILD_VERSION}
1540 )
1541 list(APPEND
1542 post_build_byproducts
1543 ${LOG_DICT_DB_NAME}
1544 )
1545endif()
1546
Marc Herbert498b4942019-04-16 23:30:52 -07001547# Add post_build_commands to post-process the final .elf file produced by
1548# either the ZEPHYR_PREBUILT_EXECUTABLE or the KERNEL_ELF executable
1549# targets above.
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001550add_custom_command(
1551 TARGET ${logical_target_for_zephyr_elf}
1552 POST_BUILD
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001553 ${post_build_commands}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001554 BYPRODUCTS
1555 ${post_build_byproducts}
Marc Herbert498b4942019-04-16 23:30:52 -07001556 COMMENT "Generating files from ${KERNEL_ELF_NAME} for board: ${BOARD}"
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001557 COMMAND_EXPAND_LISTS
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001558 # NB: COMMENT only works for some CMake-Generators
Rajavardhan Gundiecdea1c2019-01-25 11:53:13 +05301559 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001560
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001561# To populate with hex files to merge, do the following:
1562# set_property(GLOBAL APPEND PROPERTY HEX_FILES_TO_MERGE ${my_local_list})
1563# Note that the zephyr.hex file will not be included automatically.
1564get_property(HEX_FILES_TO_MERGE GLOBAL PROPERTY HEX_FILES_TO_MERGE)
1565if(HEX_FILES_TO_MERGE)
1566 # Merge in out-of-tree hex files.
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001567 set(MERGED_HEX_NAME merged.hex)
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001568
1569 add_custom_command(
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001570 OUTPUT ${MERGED_HEX_NAME}
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001571 COMMAND
1572 ${PYTHON_EXECUTABLE}
1573 ${ZEPHYR_BASE}/scripts/mergehex.py
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001574 -o ${MERGED_HEX_NAME}
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001575 ${HEX_FILES_TO_MERGE}
1576 DEPENDS ${HEX_FILES_TO_MERGE} ${logical_target_for_zephyr_elf}
1577 )
1578
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001579 add_custom_target(mergehex ALL DEPENDS ${MERGED_HEX_NAME})
Torsten Rasmussend38da9d2020-06-30 09:55:54 +02001580 list(APPEND RUNNERS_DEPS mergehex)
Håkon Øye Amundsenc9a2a5e2020-01-03 08:25:03 +00001581
1582 message(VERBOSE "Merging hex files: ${HEX_FILES_TO_MERGE}")
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001583endif()
1584
Anas Nashifc15d3c92017-11-21 18:54:55 -05001585if(EMU_PLATFORM)
Carles Cufi7d764b32018-01-11 15:46:44 +01001586 include(${ZEPHYR_BASE}/cmake/emu/${EMU_PLATFORM}.cmake)
Anas Nashiffd276ae2017-12-21 16:45:45 -05001587else()
1588 add_custom_target(run
1589 COMMAND
1590 ${CMAKE_COMMAND} -E echo
1591 "==================================================="
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +01001592 "Emulation/Simulation not supported with this board."
Anas Nashiffd276ae2017-12-21 16:45:45 -05001593 "==================================================="
1594 )
Anas Nashifc15d3c92017-11-21 18:54:55 -05001595endif()
1596
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001597add_subdirectory(cmake/flash)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001598add_subdirectory(cmake/usage)
1599add_subdirectory(cmake/reports)
1600
Marc Herbert83723102019-06-17 13:26:11 -07001601if(NOT CONFIG_TEST)
Andrew Boie411686f2018-05-24 13:18:36 -07001602if(CONFIG_ASSERT AND (NOT CONFIG_FORCE_NO_ASSERT))
Sebastian Bøefa8f9d42019-12-06 12:54:53 +01001603 message(WARNING "__ASSERT() statements are globally ENABLED")
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001604endif()
Marc Herbert83723102019-06-17 13:26:11 -07001605endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001606
Vincent Wana2bc5142020-01-09 14:20:44 -08001607if(CONFIG_BOARD_DEPRECATED_RELEASE)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001608 message(WARNING "
1609 WARNING: The board '${BOARD}' is deprecated and will be
Vincent Wana2bc5142020-01-09 14:20:44 -08001610 removed in version ${CONFIG_BOARD_DEPRECATED_RELEASE}"
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +01001611 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001612endif()
Paul Sokolovsky1c6a7d72019-06-06 21:12:14 +03001613
Vincent Wan180b4df2020-01-08 17:10:51 -08001614if(CONFIG_SOC_DEPRECATED_RELEASE)
1615 message(WARNING "
1616 WARNING: The SoC '${SOC_NAME}' is deprecated and will be
1617 removed in version ${CONFIG_SOC_DEPRECATED_RELEASE}"
1618 )
1619endif()
1620
Sebastian Bøee50e12d2019-08-29 16:19:32 +02001621# In CMake projects, 'CMAKE_BUILD_TYPE' usually determines the
1622# optimization flag, but in Zephyr it is determined through
1623# Kconfig. Here we give a warning when there is a mismatch between the
1624# two in case the user is not aware of this.
1625set(build_types None Debug Release RelWithDebInfo MinSizeRel)
1626
1627if((CMAKE_BUILD_TYPE IN_LIST build_types) AND (NOT NO_BUILD_TYPE_WARNING))
1628 string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_uppercase)
1629
Jack Dähne41bdcd2019-11-15 19:01:39 +01001630 if(NOT (${OPTIMIZATION_FLAG} IN_LIST CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_uppercase}))
Sebastian Bøee50e12d2019-08-29 16:19:32 +02001631 message(WARNING "
1632 The CMake build type was set to '${CMAKE_BUILD_TYPE}', but the optimization flag was set to '${OPTIMIZATION_FLAG}'.
1633 This may be intentional and the warning can be turned off by setting the CMake variable 'NO_BUILD_TYPE_WARNING'"
1634 )
1635 endif()
1636endif()
1637
Paul Sokolovsky1c6a7d72019-06-06 21:12:14 +03001638# @Intent: Set compiler specific flags for standard C includes
1639# Done at the very end, so any other system includes which may
1640# be added by Zephyr components were first in list.
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001641# Note, the compile flags are moved, but the system include is still present here.
1642zephyr_compile_options($<TARGET_PROPERTY:compiler,nostdinc>)
1643target_include_directories(zephyr_interface SYSTEM INTERFACE $<TARGET_PROPERTY:compiler,nostdinc_include>)
Torsten Rasmussena5917f02020-09-09 15:42:32 +02001644
1645# Finally export all build flags from Zephyr
1646add_subdirectory_ifdef(
1647 CONFIG_MAKEFILE_EXPORTS
1648 cmake/makefile_exports
1649 )