blob: 8f4d078677217e2c57aa25a9baba93999f6d40bd [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)
56set(LINKER_SCRIPT_TARGET linker_script_target)
Andrew Boiec1c54b12020-03-16 12:48:00 -070057set(PARSE_SYSCALLS_TARGET parse_syscalls_target)
Sebastian Bøe1b86fb92019-01-14 16:39:33 +010058
Sebastian Bøe12f8f762017-10-27 15:43:34 +020059define_property(GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT BRIEF_DOCS " " FULL_DOCS " ")
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +010060set_property( GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT elf32-little${ARCH}) # BFD format
Sebastian Bøe12f8f762017-10-27 15:43:34 +020061
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +010062# "zephyr_interface" is a source-less library that encapsulates all the global
Sebastian Bøe12f8f762017-10-27 15:43:34 +020063# compiler options needed by all source files. All zephyr libraries,
64# including the library named "zephyr" link with this library to
65# obtain these flags.
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +010066# https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#interface-libraries
Sebastian Bøe12f8f762017-10-27 15:43:34 +020067add_library(zephyr_interface INTERFACE)
68
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +010069# "zephyr" is a catch-all CMake library for source files that can be
Sebastian Bøe12f8f762017-10-27 15:43:34 +020070# built purely with the include paths, defines, and other compiler
71# flags that come with zephyr_interface.
72zephyr_library_named(zephyr)
73
74zephyr_include_directories(
Sebastian Bøe12f8f762017-10-27 15:43:34 +020075 include
Sebastian Bøe12f8f762017-10-27 15:43:34 +020076 ${PROJECT_BINARY_DIR}/include/generated
77 ${USERINCLUDE}
78 ${STDINCLUDE}
79)
80
Sebastian Bøe40363392019-01-25 10:14:13 +010081# Don't add non-existing include directories, it creates noise and
82# warnings in some tooling
83foreach(optional_include_dir
84 ${SOC_DIR}/${ARCH}/${SOC_PATH}
85 ${SOC_DIR}/${ARCH}/${SOC_PATH}/include
86 ${SOC_DIR}/${ARCH}/${SOC_FAMILY}/include
87 )
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
100toolchain_cc_security_fortify()
101
102# @Intent: Set compiler flags to detect general stack overflows across all functions
103if(CONFIG_STACK_CANARIES)
104 toolchain_cc_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#
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100126toolchain_cc_optimize_for_no_optimizations_flag(OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG)
127toolchain_cc_optimize_for_debug_flag(OPTIMIZE_FOR_DEBUG_FLAG)
128toolchain_cc_optimize_for_speed_flag(OPTIMIZE_FOR_SPEED_FLAG)
129toolchain_cc_optimize_for_size_flag(OPTIMIZE_FOR_SIZE_FLAG)
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
156toolchain_cc_cpp_base_flags(CPP_BASE_FLAGS)
157foreach(flag ${CPP_BASE_FLAGS})
158 zephyr_compile_options(
159 $<$<COMPILE_LANGUAGE:CXX>:${flag}>
160 )
161endforeach()
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200162
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100163# @Intent: Obtain compiler specific flags for compiling under different ISO standards of C++
164toolchain_cc_cpp_dialect_std_98_flags(CPP_DIALECT_STD_98_FLAGS)
165toolchain_cc_cpp_dialect_std_11_flags(CPP_DIALECT_STD_11_FLAGS)
166toolchain_cc_cpp_dialect_std_14_flags(CPP_DIALECT_STD_14_FLAGS)
167toolchain_cc_cpp_dialect_std_17_flags(CPP_DIALECT_STD_17_FLAGS)
168toolchain_cc_cpp_dialect_std_2a_flags(CPP_DIALECT_STD_2A_FLAGS)
169
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200170if(CONFIG_CPLUSPLUS)
171 # From kconfig choice, pick a single dialect.
172 # Kconfig choice ensures only one of these CONFIG_STD_CPP* is set.
173 if(CONFIG_STD_CPP98)
174 set(STD_CPP_DIALECT_FLAGS ${CPP_DIALECT_STD_98_FLAGS})
175 elseif(CONFIG_STD_CPP11)
176 set(STD_CPP_DIALECT_FLAGS ${CPP_DIALECT_STD_11_FLAGS}) # Default in kconfig
177 elseif(CONFIG_STD_CPP14)
178 set(STD_CPP_DIALECT_FLAGS ${CPP_DIALECT_STD_14_FLAGS})
179 elseif(CONFIG_STD_CPP17)
180 set(STD_CPP_DIALECT_FLAGS ${CPP_DIALECT_STD_17_FLAGS})
181 elseif(CONFIG_STD_CPP2A)
182 set(STD_CPP_DIALECT_FLAGS ${CPP_DIALECT_STD_2A_FLAGS})
183 else()
184 assert(0 "Unreachable code. Expected C++ standard to have been chosen. See Kconfig.zephyr.")
185 endif()
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200186
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200187 foreach(flag ${STD_CPP_DIALECT_FLAGS})
188 zephyr_compile_options(
189 $<$<COMPILE_LANGUAGE:CXX>:${flag}>
190 )
191 endforeach()
192endif()
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100193
194if(NOT CONFIG_EXCEPTIONS)
195 # @Intent: Obtain compiler specific flags related to C++ Exceptions
196 toolchain_cc_cpp_no_exceptions_flag(CPP_NO_EXCEPTIONS_FLAG)
197 zephyr_compile_options(
198 $<$<COMPILE_LANGUAGE:CXX>:${CPP_NO_EXCEPTIONS_FLAG}>
199 )
200endif()
201
202if(NOT CONFIG_RTTI)
203 # @Intent: Obtain compiler specific flags related to C++ Run Time Type Information
204 toolchain_cc_cpp_no_rtti_flag(CPP_NO_RTTI_FLAG)
205 zephyr_compile_options(
206 $<$<COMPILE_LANGUAGE:CXX>:${CPP_NO_RTTI_FLAG}>
207 )
208endif()
209
Andy Rossfe04adf2019-02-27 11:53:18 -0800210if(CONFIG_MISRA_SANE)
Danny Oerndrup8e5a95e2019-05-16 12:53:58 +0200211 # @Intent: Obtain toolchain compiler flags relating to MISRA.
212 toolchain_cc_warning_error_misra_sane(CC_MISRA_SANE_FLAG)
213 toolchain_cc_cpp_warning_error_misra_sane(CPP_MISRA_SANE_FLAG)
214 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:${CC_MISRA_SANE_FLAG}>)
215 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:${CPP_MISRA_SANE_FLAG}>)
Andy Rossfe04adf2019-02-27 11:53:18 -0800216endif()
217
Danny Oerndrup4ddbc002019-06-11 13:55:53 +0200218# @Intent: Set compiler specific macro inclusion of AUTOCONF_H
219toolchain_cc_imacros(${AUTOCONF_H})
220
Danny Oerndrupfaa72b72019-06-11 15:56:57 +0200221# @Intent: Set compiler specific flag for bare metal freestanding option
222toolchain_cc_freestanding()
223
Danny Oerndrupe34ed7c2019-06-12 14:56:46 +0200224# @Intent: Set compiler specific flag for tentative definitions, no-common
225toolchain_cc_nocommon()
226
Danny Oerndrupe0569ac2019-07-23 09:00:55 +0200227# @Intent: Set compiler specific flag for production of debug information
228toolchain_cc_produce_debug_info()
229
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200230zephyr_compile_options(
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530231 ${TOOLCHAIN_C_FLAGS}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200232)
233
Mark Ruvald Pedersencb0fd452019-01-30 21:48:25 +0100234# @Intent: Obtain compiler specific flags related to assembly
235toolchain_cc_asm_base_flags(ASM_BASE_FLAG)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200236zephyr_compile_options(
Mark Ruvald Pedersencb0fd452019-01-30 21:48:25 +0100237 $<$<COMPILE_LANGUAGE:ASM>:${ASM_BASE_FLAG}>
238)
239
Nicolas Pitreb86aa652019-07-02 16:22:04 -0400240# @Intent: Enforce standard integer type correspondance to match Zephyr usage.
241# (must be after compiler specific flags)
242toolchain_cc_imacros(${ZEPHYR_BASE}/include/toolchain/zephyr_stdint.h)
243
Mark Ruvald Pedersencb0fd452019-01-30 21:48:25 +0100244# Common toolchain-agnostic assembly flags
245zephyr_compile_options(
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200246 $<$<COMPILE_LANGUAGE:ASM>:-D_ASMLANGUAGE>
247)
248
Mark Ruvald Pedersen1f013252019-04-25 15:46:11 +0200249# @Intent: Set fundamental linker specific flags
250toolchain_ld_base()
Aurelien Jarnoc6727d42018-11-26 13:48:34 +0100251
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +0200252toolchain_ld_force_undefined_symbols(
253 _OffsetAbsSyms
254 _ConfigAbsSyms
255)
256
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200257if(NOT CONFIG_NATIVE_APPLICATION)
Mark Ruvald Pedersen65f02c02019-04-25 16:31:30 +0200258 # @Intent: Set linker specific flags for bare metal target
259 toolchain_ld_baremetal()
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200260endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200261
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200262if(CONFIG_LIB_CPLUSPLUS)
Mark Ruvald Pedersen3db09aa2019-04-26 08:43:04 +0200263 # @Intent: Set linker specific flags for C++
264 toolchain_ld_cpp()
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200265endif()
266
Danny Oerndrup8eaa9062019-05-16 12:49:31 +0200267# @Intent: Add the basic toolchain warning flags
Danny Oerndrupbdb229f2019-05-06 15:19:27 +0200268toolchain_cc_warning_base()
269
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200270# ==========================================================================
271#
272# cmake -DW=... settings
273#
274# W=1 - warnings that may be relevant and does not occur too often
275# W=2 - warnings that occur quite often but may still be relevant
276# W=3 - the more obscure warnings, can most likely be ignored
277# ==========================================================================
Danny Oerndrup8eaa9062019-05-16 12:49:31 +0200278# @Intent: Add cmake -DW toolchain supported warnings, if any
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200279if(W MATCHES "1")
Danny Oerndrup8650b152019-05-06 14:34:43 +0200280 toolchain_cc_warning_dw_1()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200281endif()
282
283if(W MATCHES "2")
Danny Oerndrup8650b152019-05-06 14:34:43 +0200284 toolchain_cc_warning_dw_2()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200285endif()
286
287if(W MATCHES "3")
Danny Oerndrup8650b152019-05-06 14:34:43 +0200288 toolchain_cc_warning_dw_3()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200289endif()
290
Danny Oerndrup8eaa9062019-05-16 12:49:31 +0200291# @Intent: Add extended, more specific, toolchain warning flags
Danny Oerndrupcbbbdea2019-05-06 15:21:58 +0200292toolchain_cc_warning_extended()
Benoit Leforestier04dad592019-01-25 13:57:03 +0100293
Danny Oerndrup025ffa22019-05-16 12:58:40 +0200294# @Intent: Trigger an error when a declaration does not specify a type
295toolchain_cc_warning_error_implicit_int()
296
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200297# Allow the user to inject options when calling cmake, e.g.
298# 'cmake -DEXTRA_CFLAGS="-Werror -Wno-deprecated-declarations" ..'
Sebastian Bøe9f590452017-11-10 12:22:23 +0100299include(cmake/extra_flags.cmake)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200300
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200301zephyr_cc_option(-fno-asynchronous-unwind-tables)
302zephyr_cc_option(-fno-pie)
303zephyr_cc_option(-fno-pic)
304zephyr_cc_option(-fno-strict-overflow)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200305
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200306if(CONFIG_OVERRIDE_FRAME_POINTER_DEFAULT)
307 if(CONFIG_OMIT_FRAME_POINTER)
308 zephyr_cc_option(-fomit-frame-pointer)
309 else()
310 zephyr_cc_option(-fno-omit-frame-pointer)
311 endif()
312endif()
313
Sebastian Bøe244451b2019-02-27 08:28:25 +0100314separate_arguments(COMPILER_OPT_AS_LIST UNIX_COMMAND ${CONFIG_COMPILER_OPT})
315zephyr_compile_options(${COMPILER_OPT_AS_LIST})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200316
317# TODO: Include arch compiler options at this point.
318
Danny Oerndrupcbbbdea2019-05-06 15:21:58 +0200319if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
320 # GCC assumed
321 zephyr_cc_option(-fno-reorder-functions)
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530322
Anas Nashif7ee8bb92018-02-11 14:36:21 -0600323 if(NOT ${ZEPHYR_TOOLCHAIN_VARIANT} STREQUAL "xcc")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200324 zephyr_cc_option(-fno-defer-pop)
325 endif()
326endif()
327
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200328zephyr_cc_option_ifdef(CONFIG_STACK_USAGE -fstack-usage)
329
Marc Herbert28a56572019-04-11 16:34:04 -0700330# If the compiler supports it, strip the ${ZEPHYR_BASE} prefix from the
331# __FILE__ macro used in __ASSERT*, in the
332# .noinit."/home/joe/zephyr/fu/bar.c" section names and in any
333# application code. This saves some memory, stops leaking user locations
334# in binaries, makes failure logs more deterministic and most
335# importantly makes builds more deterministic
Marc Herbertf67dcdb2019-05-31 15:28:38 -0700336
Marc Herberteddbf3c2019-06-11 16:57:37 -0700337# If several match then the last one wins. This matters for instances
338# like tests/ and samples/: they're inside all of them! Then let's
339# strip as little as possible.
Marc Herbertf67dcdb2019-05-31 15:28:38 -0700340zephyr_cc_option(-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=CMAKE_SOURCE_DIR)
341zephyr_cc_option(-fmacro-prefix-map=${ZEPHYR_BASE}=ZEPHYR_BASE)
Marc Herberteddbf3c2019-06-11 16:57:37 -0700342if(WEST_TOPDIR)
343 zephyr_cc_option(-fmacro-prefix-map=${WEST_TOPDIR}=WEST_TOPDIR)
344endif()
Marc Herbert28a56572019-04-11 16:34:04 -0700345
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200346# TODO: Archiver arguments
347# ar_option(D)
348
Håkon Øye Amundsend6551b52018-11-29 09:08:08 +0000349# Declare MPU userspace dependencies before the linker scripts to make
350# sure the order of dependencies are met
Andrew Boie41f60112019-01-31 15:53:24 -0800351if(CONFIG_USERSPACE)
Daniel Leung212ec9a2019-03-10 14:20:21 -0700352 set(APP_SMEM_ALIGNED_DEP app_smem_aligned_linker)
353 set(APP_SMEM_UNALIGNED_DEP app_smem_unaligned_linker)
Håkon Øye Amundsend6551b52018-11-29 09:08:08 +0000354endif()
355
Håkon Øye Amundsena4494392018-11-29 09:14:27 +0000356get_property(TOPT GLOBAL PROPERTY TOPT)
Oleg Zhurakivskyy22119352019-03-08 11:29:33 +0200357set_ifndef( TOPT -Wl,-T) # clang doesn't pick -T for some reason and complains,
358 # while -Wl,-T works for both, gcc and clang
Håkon Øye Amundsena4494392018-11-29 09:14:27 +0000359
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200360if(CONFIG_HAVE_CUSTOM_LINKER_SCRIPT)
361 set(LINKER_SCRIPT ${APPLICATION_SOURCE_DIR}/${CONFIG_CUSTOM_LINKER_SCRIPT})
Sebastian Bøec1aa9d12018-04-12 14:48:05 +0200362 if(NOT EXISTS ${LINKER_SCRIPT})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200363 set(LINKER_SCRIPT ${CONFIG_CUSTOM_LINKER_SCRIPT})
Sebastian Bøec1aa9d12018-04-12 14:48:05 +0200364 assert_exists(CONFIG_CUSTOM_LINKER_SCRIPT)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200365 endif()
366else()
367 # Try a board specific linker file
368 set(LINKER_SCRIPT ${BOARD_DIR}/linker.ld)
369 if(NOT EXISTS ${LINKER_SCRIPT})
370 # If not available, try an SoC specific linker file
Anas Nashif96455d52018-09-04 14:34:06 -0500371 set(LINKER_SCRIPT ${SOC_DIR}/${ARCH}/${SOC_PATH}/linker.ld)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200372 endif()
373endif()
374
375if(NOT EXISTS ${LINKER_SCRIPT})
376 message(FATAL_ERROR "Could not find linker script: '${LINKER_SCRIPT}'. Corrupted configuration?")
377endif()
378
379configure_file(version.h.in ${PROJECT_BINARY_DIR}/include/generated/version.h)
380
Sebastian Bøec23cc262018-10-09 16:03:29 +0200381# Error-out when the deprecated naming convention is found (until
382# after 1.14.0 has been released)
383foreach(path
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100384 ${BOARD_DIR}/dts.fixup
385 ${PROJECT_SOURCE_DIR}/soc/${ARCH}/${SOC_PATH}/dts.fixup
Sebastian Bøec23cc262018-10-09 16:03:29 +0200386 ${APPLICATION_SOURCE_DIR}/dts.fixup
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100387 )
Sebastian Bøec23cc262018-10-09 16:03:29 +0200388 if(EXISTS ${path})
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100389 message(FATAL_ERROR
390 "A deprecated filename has been detected. Porting is required."
391 "The file '${path}' exists, but it should be named dts_fixup.h instead."
392 "See https://github.com/zephyrproject-rtos/zephyr/pull/10352 for more details"
393 )
Sebastian Bøec23cc262018-10-09 16:03:29 +0200394 endif()
395endforeach()
396
397set_ifndef( DTS_BOARD_FIXUP_FILE ${BOARD_DIR}/dts_fixup.h)
398set_ifndef( DTS_SOC_FIXUP_FILE ${SOC_DIR}/${ARCH}/${SOC_PATH}/dts_fixup.h)
399set( DTS_APP_FIXUP_FILE ${APPLICATION_SOURCE_DIR}/dts_fixup.h)
Sebastian Bøec23cc262018-10-09 16:03:29 +0200400
Ulf Magnusson4e850062020-01-16 13:29:53 +0100401set_ifndef(DTS_CAT_OF_FIXUP_FILES ${ZEPHYR_BINARY_DIR}/include/generated/devicetree_fixups.h)
Sebastian Bøe361fdaa2019-01-28 13:40:50 +0100402
403# Concatenate the fixups into a single header file for easy
Sebastian Bøec23cc262018-10-09 16:03:29 +0200404# #include'ing
Ulf Magnusson4e850062020-01-16 13:29:53 +0100405file(WRITE ${DTS_CAT_OF_FIXUP_FILES} "/* May only be included by devicetree.h */\n\n")
Martí Bolívar21c7d422020-05-08 16:06:48 -0700406set(DISCOVERED_FIXUP_FILES)
Sebastian Bøe361fdaa2019-01-28 13:40:50 +0100407foreach(fixup_file
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100408 ${DTS_BOARD_FIXUP_FILE}
409 ${DTS_SOC_FIXUP_FILE}
410 ${DTS_APP_FIXUP_FILE}
411 ${shield_dts_fixups}
412 )
Sebastian Bøe361fdaa2019-01-28 13:40:50 +0100413 if(EXISTS ${fixup_file})
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100414 file(READ ${fixup_file} contents)
415 file(APPEND ${DTS_CAT_OF_FIXUP_FILES} "${contents}")
Martí Bolívar21c7d422020-05-08 16:06:48 -0700416 string(APPEND DISCOVERED_FIXUP_FILES "- ${fixup_file}\n")
Sebastian Bøec23cc262018-10-09 16:03:29 +0200417 endif()
418endforeach()
419
Martí Bolívar21c7d422020-05-08 16:06:48 -0700420if (DISCOVERED_FIXUP_FILES)
421 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.")
422endif()
423
Sebastian Bøe6f946e22018-01-09 10:52:57 +0100424# Unfortunately, the order in which CMakeLists.txt code is processed
425# matters so we need to be careful about how we order the processing
426# of subdirectories. One example is "Compiler flags added late in the
427# build are not exported to external build systems #5605"; when we
428# integrate with an external build system we read out all compiler
429# flags when the external project is created. So an external project
430# defined in subsys or ext will not get global flags added by drivers/
431# or tests/ as the subdirectories are ordered now.
432#
433# Another example of when the order matters is the reading and writing
434# of global properties such as ZEPHYR_LIBS or
435# GENERATED_KERNEL_OBJECT_FILES.
436#
437# Arch is placed early because it defines important compiler flags
438# that must be exported to external build systems defined in
439# e.g. subsys/.
440add_subdirectory(arch)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200441add_subdirectory(lib)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200442# We use include instead of add_subdirectory to avoid creating a new directory scope.
443# This is because source file properties are directory scoped, including the GENERATED
444# property which is set implicitly for custom command outputs
445include(misc/generated/CMakeLists.txt)
Anas Nashif3d1252f2018-09-03 15:20:14 -0500446
Erwan Gouriouba31cb52018-09-13 16:25:53 +0200447if(EXISTS ${SOC_DIR}/${ARCH}/CMakeLists.txt)
Anas Nashif96455d52018-09-04 14:34:06 -0500448 add_subdirectory(${SOC_DIR}/${ARCH} soc/${ARCH})
Anas Nashif3d1252f2018-09-03 15:20:14 -0500449else()
Anas Nashif96455d52018-09-04 14:34:06 -0500450 add_subdirectory(${SOC_DIR}/${ARCH}/${SOC_PATH} soc/${ARCH}/${SOC_PATH})
Anas Nashif3d1252f2018-09-03 15:20:14 -0500451endif()
452
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200453add_subdirectory(boards)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200454add_subdirectory(subsys)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200455add_subdirectory(drivers)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200456
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100457# Include zephyr modules generated CMake file.
458if(EXISTS ${CMAKE_BINARY_DIR}/zephyr_modules.txt)
Carles Cufi3ad1f272019-07-18 10:38:25 +0200459 file(STRINGS ${CMAKE_BINARY_DIR}/zephyr_modules.txt ZEPHYR_MODULES_TXT
460 ENCODING UTF-8)
Torsten Rasmussen08eabb82019-12-06 12:19:35 +0100461 set(module_names)
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100462
463 foreach(module ${ZEPHYR_MODULES_TXT})
Carles Cufi766edd42019-03-31 22:29:30 +0200464 # Match "<name>":"<path>" for each line of file, each corresponding to
465 # one module. The use of quotes is required due to CMake not supporting
466 # lazy regexes (it supports greedy only).
467 string(REGEX REPLACE "\"(.*)\":\".*\"" "\\1" module_name ${module})
468 string(REGEX REPLACE "\".*\":\"(.*)\"" "\\1" module_path ${module})
Torsten Rasmussen08eabb82019-12-06 12:19:35 +0100469
470 list(APPEND module_names ${module_name})
471
472 string(TOUPPER ${module_name} MODULE_NAME_UPPER)
473 set(ZEPHYR_${MODULE_NAME_UPPER}_MODULE_DIR ${module_path})
Torsten Rasmussen2da45502020-05-06 23:58:34 +0200474 set(ZEPHYR_${MODULE_NAME_UPPER}_MODULE_DIR ${module_path} PARENT_SCOPE)
Torsten Rasmussen08eabb82019-12-06 12:19:35 +0100475 endforeach()
476
477 foreach(module_name ${module_names})
Marc Herbertd3d33942019-05-31 15:37:40 -0700478 # Note the second, binary_dir parameter requires the added
479 # subdirectory to have its own, local cmake target(s). If not then
480 # this binary_dir is created but stays empty. Object files land in
481 # the main binary dir instead.
482 # https://cmake.org/pipermail/cmake/2019-June/069547.html
Torsten Rasmussen08eabb82019-12-06 12:19:35 +0100483 string(TOUPPER ${module_name} MODULE_NAME_UPPER)
484 set(ZEPHYR_CURRENT_MODULE_DIR ${ZEPHYR_${MODULE_NAME_UPPER}_MODULE_DIR})
Torsten Rasmussen4b68f952020-01-09 12:31:58 +0100485 add_subdirectory(${ZEPHYR_CURRENT_MODULE_DIR} ${CMAKE_BINARY_DIR}/modules/${module_name})
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100486 endforeach()
Torsten Rasmussenf0fa7b82019-10-21 10:08:20 +0200487 # Done processing modules, clear ZEPHYR_CURRENT_MODULE_DIR.
488 set(ZEPHYR_CURRENT_MODULE_DIR)
Torsten Rasmussen605ae102019-02-12 10:19:38 +0100489endif()
Torsten Rasmussen7e9d1bd2019-02-05 10:36:22 +0100490
Andrew Boie59601192020-05-29 13:24:51 -0700491set(syscall_list_h ${CMAKE_CURRENT_BINARY_DIR}/include/generated/syscall_list.h)
492set(syscalls_json ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls.json)
493set(struct_tags_json ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/struct_tags.json)
Sebastian Bøe13a68402017-11-20 13:03:55 +0100494
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200495# The syscalls subdirs txt file is constructed by python containing a list of folders to use for
496# dependency handling, including empty folders.
497# Windows: The list is used to specify DIRECTORY list with CMAKE_CONFIGURE_DEPENDS attribute.
498# Other OS: The list will update whenever a file is added/removed/modified and ensure a re-build.
499set(syscalls_subdirs_txt ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_subdirs.txt)
500
501# As syscalls_subdirs_txt is updated whenever a file is modified, this file can not be used for
502# monitoring of added / removed folders. A trigger file is thus used for correct dependency
503# handling. The trigger file will update when a folder is added / removed.
504set(syscalls_subdirs_trigger ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_subdirs.trigger)
505
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200506if(NOT (${CMAKE_HOST_SYSTEM_NAME} STREQUAL Windows))
507 set(syscalls_links --create-links ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_links)
508endif()
509
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200510# When running CMake it must be ensured that all dependencies are correctly acquired.
511execute_process(
512 COMMAND
513 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200514 ${ZEPHYR_BASE}/scripts/subfolder_list.py
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200515 --directory ${ZEPHYR_BASE}/include # Walk this directory
516 --out-file ${syscalls_subdirs_txt} # Write file with discovered folder
517 --trigger ${syscalls_subdirs_trigger} # Trigger file that is used for json generation
518 ${syscalls_links} # If defined, create symlinks for dependencies
519)
Carles Cufi3ad1f272019-07-18 10:38:25 +0200520file(STRINGS ${syscalls_subdirs_txt} PARSE_SYSCALLS_PATHS_DEPENDS ENCODING UTF-8)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200521
522if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL Windows)
523 # On windows only adding/removing files or folders will be reflected in depends.
524 # Hence adding a file requires CMake to re-run to add this file to the file list.
525 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${PARSE_SYSCALLS_PATHS_DEPENDS})
526
527 # Also On Windows each header file must be monitored as file modifications are not reflected
528 # on directory level.
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200529 file(GLOB_RECURSE PARSE_SYSCALLS_HEADER_DEPENDS ${ZEPHYR_BASE}/include/*.h)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200530else()
531 # The syscall parsing depends on the folders in order to detect add/removed/modified files.
532 # When a folder is removed, CMake will try to find a target that creates that dependency.
533 # This command sets up the target for CMake to find.
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200534 # Without this code, CMake will fail with the following error:
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200535 # <folder> needed by '<target>', missing and no known rule to make it
536 # when a folder is removed.
537 add_custom_command(OUTPUT ${PARSE_SYSCALLS_PATHS_DEPENDS}
538 COMMAND ${CMAKE_COMMAND} -E echo ""
539 COMMENT "Preparing syscall dependency handling"
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200540 )
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200541
542 add_custom_command(
543 OUTPUT
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200544 ${syscalls_subdirs_trigger}
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200545 COMMAND
546 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200547 ${ZEPHYR_BASE}/scripts/subfolder_list.py
548 --directory ${ZEPHYR_BASE}/include # Walk this directory
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200549 --out-file ${syscalls_subdirs_txt} # Write file with discovered folder
550 --trigger ${syscalls_subdirs_trigger} # Trigger file that is used for json generation
551 ${syscalls_links} # If defined, create symlinks for dependencies
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200552 DEPENDS ${PARSE_SYSCALLS_PATHS_DEPENDS}
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200553 )
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200554
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200555 # Ensure subdir file always exists when specifying CMake dependency.
556 if(NOT EXISTS ${syscalls_subdirs_txt})
557 file(WRITE ${syscalls_subdirs_txt} "")
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200558 endif()
559
560 # On other OS'es, modifying a file is reflected on the folder timestamp and hence detected
561 # when using depend on directory level.
562 # Thus CMake only needs to re-run when sub-directories are added / removed, which is indicated
563 # using a trigger file.
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200564 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${syscalls_subdirs_txt})
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200565endif()
566
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200567# syscall declarations are searched for in the SYSCALL_INCLUDE_DIRS
Adithya Baglodye67720b2018-07-02 14:59:19 +0530568if(CONFIG_APPLICATION_DEFINED_SYSCALL)
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200569 list(APPEND SYSCALL_INCLUDE_DIRS ${APPLICATION_SOURCE_DIR})
Adithya Baglodye67720b2018-07-02 14:59:19 +0530570endif()
571
Andrew Boiec1863872019-11-21 23:11:29 -0800572if(CONFIG_ZTEST)
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200573 list(APPEND SYSCALL_INCLUDE_DIRS ${ZEPHYR_BASE}/subsys/testsuite/ztest/include)
Andrew Boiec1863872019-11-21 23:11:29 -0800574endif()
575
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200576foreach(d ${SYSCALL_INCLUDE_DIRS})
577 list(APPEND parse_syscalls_include_args
578 --include ${d}
579 )
580endforeach()
581
Sebastian Bøe13a68402017-11-20 13:03:55 +0100582add_custom_command(
583 OUTPUT
584 ${syscalls_json}
Andrew Boie59601192020-05-29 13:24:51 -0700585 ${struct_tags_json}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100586 COMMAND
587 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200588 ${ZEPHYR_BASE}/scripts/parse_syscalls.py
Adithya Baglodye67720b2018-07-02 14:59:19 +0530589 --include ${ZEPHYR_BASE}/include # Read files from this dir
Andrew Boiefed960b2020-05-29 13:33:12 -0700590 --include ${ZEPHYR_BASE}/drivers # For net sockets
591 --include ${ZEPHYR_BASE}/subsys/net # More net sockets
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200592 ${parse_syscalls_include_args} # Read files from these dirs also
Corey Whartonccd15df2020-02-29 14:51:42 -0800593 --json-file ${syscalls_json} # Write this file
Andrew Boie59601192020-05-29 13:24:51 -0700594 --tag-struct-file ${struct_tags_json} # Write subsystem list to this file
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200595 DEPENDS ${syscalls_subdirs_trigger} ${PARSE_SYSCALLS_HEADER_DEPENDS}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100596 )
597
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100598add_custom_target(${SYSCALL_LIST_H_TARGET} DEPENDS ${syscall_list_h})
Andrew Boiec1c54b12020-03-16 12:48:00 -0700599add_custom_target(${PARSE_SYSCALLS_TARGET}
Andrew Boie59601192020-05-29 13:24:51 -0700600 DEPENDS ${syscalls_json} ${struct_tags_json})
Andrew Boie9ff64bb2019-11-05 09:39:05 -0800601
602# 64-bit systems do not require special handling of 64-bit system call
603# parameters or return values, indicate this to the system call boilerplate
604# generation script.
605if(CONFIG_64BIT)
606 set(SYSCALL_LONG_REGISTERS_ARG --long-registers)
607endif()
608
Andy Rosscfeb07e2020-03-05 21:14:02 -0800609if(CONFIG_TIMEOUT_64BIT)
610 set(SYSCALL_SPLIT_TIMEOUT_ARG --split-type k_timeout_t)
611endif()
612
Sebastian Bøe13a68402017-11-20 13:03:55 +0100613add_custom_command(OUTPUT include/generated/syscall_dispatch.c ${syscall_list_h}
614 # Also, some files are written to include/generated/syscalls/
615 COMMAND
616 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200617 ${ZEPHYR_BASE}/scripts/gen_syscalls.py
Sebastian Bøe13a68402017-11-20 13:03:55 +0100618 --json-file ${syscalls_json} # Read this file
619 --base-output include/generated/syscalls # Write to this dir
620 --syscall-dispatch include/generated/syscall_dispatch.c # Write this file
Andrew Boie353acf42018-07-23 18:10:15 -0700621 --syscall-list ${syscall_list_h}
Andrew Boie9ff64bb2019-11-05 09:39:05 -0800622 ${SYSCALL_LONG_REGISTERS_ARG}
Andy Rosscfeb07e2020-03-05 21:14:02 -0800623 ${SYSCALL_SPLIT_TIMEOUT_ARG}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100624 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
Andrew Boiec1c54b12020-03-16 12:48:00 -0700625 DEPENDS ${PARSE_SYSCALLS_TARGET}
Sebastian Bøe0d3d7252020-03-19 06:40:13 -0700626 ${syscalls_json}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100627 )
628
Corey Whartonccd15df2020-02-29 14:51:42 -0800629# This is passed into all calls to the gen_kobject_list.py script.
Andrew Boie59601192020-05-29 13:24:51 -0700630set(gen_kobject_list_include_args --include ${struct_tags_json})
Corey Whartonccd15df2020-02-29 14:51:42 -0800631
Leandro Pereirac2003672018-04-04 13:50:32 -0700632set(DRV_VALIDATION ${PROJECT_BINARY_DIR}/include/generated/driver-validation.h)
633add_custom_command(
634 OUTPUT ${DRV_VALIDATION}
635 COMMAND
636 ${PYTHON_EXECUTABLE}
637 ${ZEPHYR_BASE}/scripts/gen_kobject_list.py
638 --validation-output ${DRV_VALIDATION}
Corey Whartonccd15df2020-02-29 14:51:42 -0800639 ${gen_kobject_list_include_args}
Leandro Pereirac2003672018-04-04 13:50:32 -0700640 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Corey Whartonccd15df2020-02-29 14:51:42 -0800641 DEPENDS
642 ${ZEPHYR_BASE}/scripts/gen_kobject_list.py
Andrew Boiec1c54b12020-03-16 12:48:00 -0700643 ${PARSE_SYSCALLS_TARGET}
Andrew Boie59601192020-05-29 13:24:51 -0700644 ${struct_tags_json}
Leandro Pereirac2003672018-04-04 13:50:32 -0700645 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
646 )
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100647add_custom_target(${DRIVER_VALIDATION_H_TARGET} DEPENDS ${DRV_VALIDATION})
Leandro Pereirac2003672018-04-04 13:50:32 -0700648
Torsten Rasmussend7862cf2020-02-12 15:42:09 +0100649include(${ZEPHYR_BASE}/cmake/kobj.cmake)
Leandro Pereira39dc7d02018-04-05 13:59:33 -0700650gen_kobj(KOBJ_INCLUDE_PATH)
Leandro Pereirac2003672018-04-04 13:50:32 -0700651
Sebastian Bøefdac7b32020-01-23 15:39:17 +0100652# Add a pseudo-target that is up-to-date when all generated headers
653# are up-to-date.
654
655add_custom_target(zephyr_generated_headers)
656add_dependencies(zephyr_generated_headers
657 offsets_h
658 )
659
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200660# Generate offsets.c.obj from offsets.c
661# Generate offsets.h from offsets.c.obj
662
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100663set(OFFSETS_LIB offsets)
664
Klaus Petersenc66cb762018-11-15 10:37:46 +0100665set(OFFSETS_C_PATH ${ARCH_DIR}/${ARCH}/core/offsets/offsets.c)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200666set(OFFSETS_H_PATH ${PROJECT_BINARY_DIR}/include/generated/offsets.h)
667
Klaus Petersen62e55e52019-02-04 12:10:57 +0100668add_library( ${OFFSETS_LIB} OBJECT ${OFFSETS_C_PATH})
Stephanos Ioannidis2d746042019-10-25 00:08:21 +0900669target_include_directories(${OFFSETS_LIB} PRIVATE
670 kernel/include
671 ${ARCH_DIR}/${ARCH}/include
672 )
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100673target_link_libraries(${OFFSETS_LIB} zephyr_interface)
674add_dependencies( ${OFFSETS_LIB}
675 ${SYSCALL_LIST_H_TARGET}
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100676 ${DRIVER_VALIDATION_H_TARGET}
677 ${KOBJ_TYPES_H_TARGET}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100678 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200679
680add_custom_command(
681 OUTPUT ${OFFSETS_H_PATH}
Carles Cufi7d764b32018-01-11 15:46:44 +0100682 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/gen_offset_header.py
Klaus Petersen62e55e52019-02-04 12:10:57 +0100683 -i $<TARGET_OBJECTS:${OFFSETS_LIB}>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200684 -o ${OFFSETS_H_PATH}
Sebastian Bøe5962aab2019-08-15 14:45:59 +0200685 DEPENDS
686 ${OFFSETS_LIB}
687 $<TARGET_OBJECTS:${OFFSETS_LIB}>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200688)
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100689add_custom_target(${OFFSETS_H_TARGET} DEPENDS ${OFFSETS_H_PATH})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200690
691zephyr_include_directories(${TOOLCHAIN_INCLUDES})
692
Sebastian Bøe89516fb2017-12-01 15:25:06 +0100693zephyr_get_include_directories_for_lang(C ZEPHYR_INCLUDES)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200694
695add_subdirectory(kernel)
696
697# Read list content
698get_property(ZEPHYR_LIBS_PROPERTY GLOBAL PROPERTY ZEPHYR_LIBS)
699
700foreach(zephyr_lib ${ZEPHYR_LIBS_PROPERTY})
701 # TODO: Could this become an INTERFACE property of zephyr_interface?
Sebastian Bøefdac7b32020-01-23 15:39:17 +0100702 add_dependencies(${zephyr_lib} zephyr_generated_headers)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200703endforeach()
704
705get_property(OUTPUT_FORMAT GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT)
706
Adithya Baglody62e152a2018-11-13 15:34:02 +0530707if (CONFIG_CODE_DATA_RELOCATION)
708 set(CODE_RELOCATION_DEP code_relocation_source_lib)
709endif() # CONFIG_CODE_DATA_RELOCATION
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100710
Mark Ruvald Pedersen4c811972019-04-29 17:16:54 +0200711configure_linker_script(
Mark Ruvald Pedersen1073fba2019-04-29 20:27:23 +0200712 linker.cmd
Mark Ruvald Pedersenfbcea172019-04-29 20:35:12 +0200713 ""
Daniel Leung212ec9a2019-03-10 14:20:21 -0700714 ${APP_SMEM_ALIGNED_DEP}
Sebastian Bøe2a963122019-02-08 15:49:57 +0100715 ${CODE_RELOCATION_DEP}
Sebastian Bøefdac7b32020-01-23 15:39:17 +0100716 zephyr_generated_headers
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100717 )
Andy Grosse8860fe2018-02-01 01:12:32 -0600718
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200719add_custom_target(
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100720 ${LINKER_SCRIPT_TARGET}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200721 DEPENDS
722 linker.cmd
Sebastian Bøeb1ab5012017-12-14 13:03:23 +0100723 )
724
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100725# Give the '${LINKER_SCRIPT_TARGET}' target all of the include directories so
Sebastian Bøeb1ab5012017-12-14 13:03:23 +0100726# that cmake can successfully find the linker_script's header
727# dependencies.
728zephyr_get_include_directories_for_lang(C
729 ZEPHYR_INCLUDE_DIRS
730 STRIP_PREFIX # Don't use a -I prefix
731 )
732set_property(TARGET
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100733 ${LINKER_SCRIPT_TARGET}
Sebastian Bøeb1ab5012017-12-14 13:03:23 +0100734 PROPERTY INCLUDE_DIRECTORIES
735 ${ZEPHYR_INCLUDE_DIRS}
736 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200737
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200738if(CONFIG_GEN_ISR_TABLES)
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530739 if(CONFIG_GEN_SW_ISR_TABLE)
740 list(APPEND GEN_ISR_TABLE_EXTRA_ARG --sw-isr-table)
741 endif()
742
743 if(CONFIG_GEN_IRQ_VECTOR_TABLE)
744 list(APPEND GEN_ISR_TABLE_EXTRA_ARG --vector-table)
745 endif()
746
Sebastian Bøe15260702019-01-14 16:31:02 +0100747 # isr_tables.c is generated from ${ZEPHYR_PREBUILT_EXECUTABLE} by
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200748 # gen_isr_tables.py
Danny Oerndrup51634cd2019-08-01 07:56:45 +0200749 set(obj_copy_cmd "")
750 bintools_objcopy(
751 RESULT_CMD_LIST obj_copy_cmd
752 TARGET_INPUT ${OUTPUT_FORMAT}
753 TARGET_OUTPUT "binary"
754 SECTION_ONLY ".intList"
755 FILE_INPUT $<TARGET_FILE:${ZEPHYR_PREBUILT_EXECUTABLE}>
756 FILE_OUTPUT "isrList.bin"
757 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200758 add_custom_command(
759 OUTPUT isr_tables.c
Danny Oerndrup51634cd2019-08-01 07:56:45 +0200760 ${obj_copy_cmd}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200761 COMMAND ${PYTHON_EXECUTABLE}
Carles Cufi7d764b32018-01-11 15:46:44 +0100762 ${ZEPHYR_BASE}/arch/common/gen_isr_tables.py
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200763 --output-source isr_tables.c
Sebastian Bøe15260702019-01-14 16:31:02 +0100764 --kernel $<TARGET_FILE:${ZEPHYR_PREBUILT_EXECUTABLE}>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200765 --intlist isrList.bin
Yasushi SHOJI6fc0d772018-10-09 18:59:16 +0900766 $<$<BOOL:${CONFIG_BIG_ENDIAN}>:--big-endian>
Sebastian Bøea55279a2018-01-04 14:08:39 +0100767 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--debug>
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530768 ${GEN_ISR_TABLE_EXTRA_ARG}
Sebastian Bøe15260702019-01-14 16:31:02 +0100769 DEPENDS ${ZEPHYR_PREBUILT_EXECUTABLE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200770 )
771 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_SOURCE_FILES isr_tables.c)
772endif()
773
Adithya Baglody62e152a2018-11-13 15:34:02 +0530774if(CONFIG_CODE_DATA_RELOCATION)
Mark Ruvald Pedersen86a3e8f2019-05-03 10:33:03 +0200775 # @Intent: Linker script to relocate .text, data and .bss sections
776 toolchain_ld_relocation()
Adithya Baglody62e152a2018-11-13 15:34:02 +0530777endif()
778
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530779if(CONFIG_USERSPACE)
780 zephyr_get_compile_options_for_lang_as_string(C compiler_flags_priv)
781 string(REPLACE "-ftest-coverage" "" NO_COVERAGE_FLAGS "${compiler_flags_priv}")
782 string(REPLACE "-fprofile-arcs" "" NO_COVERAGE_FLAGS "${NO_COVERAGE_FLAGS}")
Daniel Leung84b1bba2019-04-01 15:57:35 -0700783 string(REPLACE "-fno-inline" "" NO_COVERAGE_FLAGS "${NO_COVERAGE_FLAGS}")
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530784
785 get_property(include_dir_in_interface TARGET zephyr_interface
786 PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
787
788 get_property(sys_include_dir_in_interface TARGET zephyr_interface
789 PROPERTY INTERFACE_SYSTEM_INCLUDE_DIRECTORIES)
790
791 get_property(compile_definitions_interface TARGET zephyr_interface
792 PROPERTY INTERFACE_COMPILE_DEFINITIONS)
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530793
Carles Cufi7d764b32018-01-11 15:46:44 +0100794 set(GEN_KOBJ_LIST ${ZEPHYR_BASE}/scripts/gen_kobject_list.py)
795 set(PROCESS_GPERF ${ZEPHYR_BASE}/scripts/process_gperf.py)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200796
797 set(OBJ_LIST kobject_hash.gperf)
798 set(OUTPUT_SRC_PRE kobject_hash_preprocessed.c)
799 set(OUTPUT_SRC kobject_hash.c)
800 set(OUTPUT_OBJ kobject_hash.c.obj)
801 set(OUTPUT_OBJ_RENAMED kobject_hash_renamed.o)
802
803 # Essentially what we are doing here is extracting some information
804 # out of the nearly finished elf file, generating the source code
805 # for a hash table based on that information, and then compiling and
806 # linking the hash table back into a now even more nearly finished
Marc Herbert4a10eea2019-04-16 15:39:45 -0700807 # elf file. More information in gen_kobject_list.py --help.
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200808
809 # Use the script GEN_KOBJ_LIST to scan the kernel binary's
Sebastian Bøe15260702019-01-14 16:31:02 +0100810 # (${ZEPHYR_PREBUILT_EXECUTABLE}) DWARF information to produce a table of kernel
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200811 # objects (OBJ_LIST) which we will then pass to gperf
812 add_custom_command(
813 OUTPUT ${OBJ_LIST}
814 COMMAND
815 ${PYTHON_EXECUTABLE}
816 ${GEN_KOBJ_LIST}
Sebastian Bøe15260702019-01-14 16:31:02 +0100817 --kernel $<TARGET_FILE:${ZEPHYR_PREBUILT_EXECUTABLE}>
Leandro Pereirac2003672018-04-04 13:50:32 -0700818 --gperf-output ${OBJ_LIST}
Corey Whartonccd15df2020-02-29 14:51:42 -0800819 ${gen_kobject_list_include_args}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200820 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Corey Whartonccd15df2020-02-29 14:51:42 -0800821 DEPENDS
822 ${ZEPHYR_PREBUILT_EXECUTABLE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200823 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
824 )
825 add_custom_target(obj_list DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OBJ_LIST})
826
827 # Use gperf to generate C code (OUTPUT_SRC_PRE) which implements a
828 # perfect hashtable based on OBJ_LIST
829 add_custom_command(
830 OUTPUT ${OUTPUT_SRC_PRE}
831 COMMAND
832 ${GPERF}
833 --output-file ${OUTPUT_SRC_PRE}
834 ${OBJ_LIST}
Sebastian Bøef5758b52018-01-31 10:42:46 +0100835 DEPENDS obj_list ${OBJ_LIST}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200836 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
837 )
838 add_custom_target(output_src_pre DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_SRC_PRE})
839
840 # For our purposes the code/data generated by gperf is not optimal.
841 #
842 # The script PROCESS_GPERF creates a new c file OUTPUT_SRC based on
843 # OUTPUT_SRC_PRE to greatly reduce the amount of code/data generated
844 # since we know we are always working with pointer values
845 add_custom_command(
846 OUTPUT ${OUTPUT_SRC}
847 COMMAND
Sebastian Bøe1b600702018-06-21 14:34:42 +0200848 ${PYTHON_EXECUTABLE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200849 ${PROCESS_GPERF}
850 -i ${OUTPUT_SRC_PRE}
851 -o ${OUTPUT_SRC}
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700852 -p "struct z_object"
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200853 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Sebastian Bøef5758b52018-01-31 10:42:46 +0100854 DEPENDS output_src_pre ${OUTPUT_SRC_PRE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200855 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
856 )
857 add_custom_target(output_src DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_SRC})
858
859 # We need precise control of where generated text/data ends up in the final
860 # kernel image. Disable function/data sections and use objcopy to move
861 # generated data into special section names
862 add_library(output_lib STATIC
863 ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_SRC}
864 )
865
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530866 set_source_files_properties(${OUTPUT_SRC} PROPERTIES COMPILE_FLAGS
Andrew Boiea5148982019-03-14 17:04:11 -0700867 "${NO_COVERAGE_FLAGS} -fno-function-sections -fno-data-sections")
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530868
869 set_source_files_properties(${OUTPUT_SRC}
870 PROPERTIES COMPILE_DEFINITIONS "${compile_definitions_interface}")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200871
872 # Turn off -ffunction-sections, etc.
873 # NB: Using a library instead of target_compile_options(output_lib
874 # [...]) because a library's options have precedence
875 add_library(output_lib_interface INTERFACE)
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530876
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200877 target_link_libraries(output_lib output_lib_interface)
878
Kumar Gala3713ea42019-03-14 11:50:08 -0500879 foreach(incl ${include_dir_in_interface})
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530880 target_include_directories(output_lib_interface INTERFACE ${incl})
881 endforeach()
882
Kumar Gala3713ea42019-03-14 11:50:08 -0500883 foreach(incl ${sys_include_dir_in_interface})
884 target_include_directories(output_lib_interface SYSTEM INTERFACE ${incl})
885 endforeach()
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530886
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200887 set(OUTPUT_OBJ_PATH ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/output_lib.dir/${OUTPUT_OBJ})
888
Danny Oerndrup51634cd2019-08-01 07:56:45 +0200889 set(obj_copy_cmd "")
890 set(obj_copy_sections_rename
891 .data=.kobject_data.data
892 .text=.kobject_data.text
893 .rodata=.kobject_data.rodata
894 )
895 bintools_objcopy(
896 RESULT_CMD_LIST obj_copy_cmd
897 SECTION_RENAME ${obj_copy_sections_rename}
898 FILE_INPUT ${OUTPUT_OBJ_PATH}
899 FILE_OUTPUT ${OUTPUT_OBJ_RENAMED}
900 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200901 add_custom_command(
902 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_OBJ_RENAMED}
Danny Oerndrup51634cd2019-08-01 07:56:45 +0200903 ${obj_copy_cmd}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200904 DEPENDS output_lib
905 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
906 )
907 add_custom_target(output_obj_renamed DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_OBJ_RENAMED})
908
909 add_library(output_obj_renamed_lib STATIC IMPORTED GLOBAL)
910 set_property(
911 TARGET output_obj_renamed_lib
912 PROPERTY
913 IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_OBJ_RENAMED}
914 )
915 add_dependencies(
916 output_obj_renamed_lib
917 output_obj_renamed
918 )
919
920 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_OBJECT_FILES output_obj_renamed_lib)
921endif()
922
923# Read global variables into local variables
924get_property(GKOF GLOBAL PROPERTY GENERATED_KERNEL_OBJECT_FILES)
925get_property(GKSF GLOBAL PROPERTY GENERATED_KERNEL_SOURCE_FILES)
926
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200927
Alberto Escolar Piedrasc2882412018-07-05 10:51:03 +0200928get_property(CSTD GLOBAL PROPERTY CSTD)
929set_ifndef(CSTD c99)
930
Danny Oerndrup6331dae2019-06-13 15:33:03 +0200931# @Intent: Obtain compiler specific flag for specifying the c standard
932toolchain_cc_cstd_flag(CC_CSTD ${CSTD})
Alberto Escolar Piedrasc2882412018-07-05 10:51:03 +0200933zephyr_compile_options(
Danny Oerndrup6331dae2019-06-13 15:33:03 +0200934 $<$<COMPILE_LANGUAGE:C>:${CC_CSTD}>
Alberto Escolar Piedrasc2882412018-07-05 10:51:03 +0200935)
936
Mark Ruvald Pedersen197197a2019-05-03 11:02:56 +0200937# @Intent: Configure linker scripts, i.e. generate linker scripts with variables substituted
938toolchain_ld_configure_files()
Daniel Leung212ec9a2019-03-10 14:20:21 -0700939
Andrew Boie4ce652e2019-02-22 16:08:44 -0800940if(CONFIG_USERSPACE)
Daniel Leung212ec9a2019-03-10 14:20:21 -0700941 set(APP_SMEM_ALIGNED_LD "${PROJECT_BINARY_DIR}/include/generated/app_smem_aligned.ld")
942 set(APP_SMEM_UNALIGNED_LD "${PROJECT_BINARY_DIR}/include/generated/app_smem_unaligned.ld")
Adithya Baglody50950eb2018-12-20 15:47:42 +0530943 set(OBJ_FILE_DIR "${PROJECT_BINARY_DIR}/../")
944
945 add_custom_target(
Daniel Leung212ec9a2019-03-10 14:20:21 -0700946 ${APP_SMEM_ALIGNED_DEP}
Sebastian Bøe2a963122019-02-08 15:49:57 +0100947 DEPENDS
Daniel Leung212ec9a2019-03-10 14:20:21 -0700948 ${APP_SMEM_ALIGNED_LD}
949 )
950
951 add_custom_target(
952 ${APP_SMEM_UNALIGNED_DEP}
953 DEPENDS
954 ${APP_SMEM_UNALIGNED_LD}
Adithya Baglody50950eb2018-12-20 15:47:42 +0530955 )
956
Andrew Boie4b4f7732019-02-01 12:18:31 -0800957 if(CONFIG_NEWLIB_LIBC)
Andrew Boie17ce8222019-02-21 13:44:54 -0800958 set(NEWLIB_PART -l libc.a z_libc_partition)
Andrew Boie4b4f7732019-02-01 12:18:31 -0800959 endif()
Ioannis Glaropoulosd58f8be2019-11-15 14:07:51 +0100960 if(CONFIG_NEWLIB_LIBC_NANO)
961 set(NEWLIB_PART -l libc_nano.a z_libc_partition)
962 endif()
Andrew Boiee686aef2019-02-27 14:41:45 -0800963
Adithya Baglody50950eb2018-12-20 15:47:42 +0530964 add_custom_command(
Daniel Leung212ec9a2019-03-10 14:20:21 -0700965 OUTPUT ${APP_SMEM_UNALIGNED_LD}
Adithya Baglody50950eb2018-12-20 15:47:42 +0530966 COMMAND ${PYTHON_EXECUTABLE}
967 ${ZEPHYR_BASE}/scripts/gen_app_partitions.py
968 -d ${OBJ_FILE_DIR}
Daniel Leung212ec9a2019-03-10 14:20:21 -0700969 -o ${APP_SMEM_UNALIGNED_LD}
Torsten Rasmussen5a703c82019-11-05 10:24:08 +0100970 ${NEWLIB_PART}
Torsten Rasmussen1f9723a2019-11-04 14:30:24 +0100971 $<TARGET_PROPERTY:zephyr_property_target,COMPILE_OPTIONS>
Adithya Baglody50950eb2018-12-20 15:47:42 +0530972 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Sebastian Bøe2a963122019-02-08 15:49:57 +0100973 DEPENDS
974 kernel
975 ${ZEPHYR_LIBS_PROPERTY}
Adithya Baglody50950eb2018-12-20 15:47:42 +0530976 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
Torsten Rasmussen1f9723a2019-11-04 14:30:24 +0100977 COMMAND_EXPAND_LISTS
Daniel Leung212ec9a2019-03-10 14:20:21 -0700978 COMMENT "Generating app_smem_unaligned linker section"
979 )
980
Mark Ruvald Pedersen4c811972019-04-29 17:16:54 +0200981 configure_linker_script(
Mark Ruvald Pedersen1073fba2019-04-29 20:27:23 +0200982 linker_app_smem_unaligned.cmd
Mark Ruvald Pedersenfbcea172019-04-29 20:35:12 +0200983 "-DLINKER_APP_SMEM_UNALIGNED"
Daniel Leung212ec9a2019-03-10 14:20:21 -0700984 ${CODE_RELOCATION_DEP}
985 ${APP_SMEM_UNALIGNED_DEP}
986 ${APP_SMEM_UNALIGNED_LD}
Sebastian Bøefdac7b32020-01-23 15:39:17 +0100987 zephyr_generated_headers
Daniel Leung212ec9a2019-03-10 14:20:21 -0700988 )
Daniel Leung212ec9a2019-03-10 14:20:21 -0700989
990 add_custom_target(
991 linker_app_smem_unaligned_script
992 DEPENDS
993 linker_app_smem_unaligned.cmd
994 )
995
996 set_property(TARGET
997 linker_app_smem_unaligned_script
998 PROPERTY INCLUDE_DIRECTORIES
999 ${ZEPHYR_INCLUDE_DIRS}
1000 )
1001
1002 set(APP_SMEM_UNALIGNED_LIB app_smem_unaligned_output_obj_renamed_lib)
1003 add_executable( app_smem_unaligned_prebuilt misc/empty_file.c)
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001004 toolchain_ld_link_elf(
1005 TARGET_ELF app_smem_unaligned_prebuilt
Marc Herbert0370c9b2019-06-13 16:15:44 -07001006 OUTPUT_MAP ${PROJECT_BINARY_DIR}/app_smem_unaligned_prebuilt.map
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001007 LIBRARIES_PRE_SCRIPT ""
1008 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/linker_app_smem_unaligned.cmd
1009 LIBRARIES_POST_SCRIPT ""
1010 DEPENDENCIES ${CODE_RELOCATION_DEP}
1011 )
Daniel Leung212ec9a2019-03-10 14:20:21 -07001012 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 +02001013 add_dependencies( app_smem_unaligned_prebuilt linker_app_smem_unaligned_script ${OFFSETS_LIB})
Daniel Leung212ec9a2019-03-10 14:20:21 -07001014
1015 add_custom_command(
1016 OUTPUT ${APP_SMEM_ALIGNED_LD}
1017 COMMAND ${PYTHON_EXECUTABLE}
1018 ${ZEPHYR_BASE}/scripts/gen_app_partitions.py
1019 -e $<TARGET_FILE:app_smem_unaligned_prebuilt>
1020 -o ${APP_SMEM_ALIGNED_LD}
Torsten Rasmussen5a703c82019-11-05 10:24:08 +01001021 ${NEWLIB_PART}
Torsten Rasmussen1f9723a2019-11-04 14:30:24 +01001022 $<TARGET_PROPERTY:zephyr_property_target,COMPILE_OPTIONS>
Daniel Leung212ec9a2019-03-10 14:20:21 -07001023 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1024 DEPENDS
1025 kernel
1026 ${ZEPHYR_LIBS_PROPERTY}
1027 app_smem_unaligned_prebuilt
1028 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
Torsten Rasmussen1f9723a2019-11-04 14:30:24 +01001029 COMMAND_EXPAND_LISTS
Daniel Leung212ec9a2019-03-10 14:20:21 -07001030 COMMENT "Generating app_smem_aligned linker section"
Adithya Baglody50950eb2018-12-20 15:47:42 +05301031 )
1032endif()
1033
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001034# FIXME: Is there any way to get rid of empty_file.c?
Sebastian Bøe15260702019-01-14 16:31:02 +01001035add_executable( ${ZEPHYR_PREBUILT_EXECUTABLE} misc/empty_file.c)
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001036toolchain_ld_link_elf(
1037 TARGET_ELF ${ZEPHYR_PREBUILT_EXECUTABLE}
Marc Herbert0370c9b2019-06-13 16:15:44 -07001038 OUTPUT_MAP ${PROJECT_BINARY_DIR}/${ZEPHYR_PREBUILT_EXECUTABLE}.map
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001039 LIBRARIES_PRE_SCRIPT ""
1040 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/linker.cmd
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001041 DEPENDENCIES ${CODE_RELOCATION_DEP}
1042)
Sebastian Bøe15260702019-01-14 16:31:02 +01001043set_property(TARGET ${ZEPHYR_PREBUILT_EXECUTABLE} PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker.cmd)
Andrew Boie28be7932020-03-11 10:56:19 -07001044add_dependencies( ${ZEPHYR_PREBUILT_EXECUTABLE} ${LINKER_SCRIPT_TARGET} ${OFFSETS_LIB})
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001045
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +02001046
Marc Herbert498b4942019-04-16 23:30:52 -07001047set(generated_kernel_files ${GKSF} ${GKOF})
1048if(NOT generated_kernel_files)
1049 # Use the prebuilt elf as the final elf since we don't have a
1050 # generation stage.
1051 set(logical_target_for_zephyr_elf ${ZEPHYR_PREBUILT_EXECUTABLE})
1052else()
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001053 # The second linker pass uses the same source linker script of the
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +01001054 # first pass (LINKER_SCRIPT), but this time with a different output
1055 # file and preprocessed with the define LINKER_PASS2.
Mark Ruvald Pedersen4c811972019-04-29 17:16:54 +02001056 configure_linker_script(
Mark Ruvald Pedersen1073fba2019-04-29 20:27:23 +02001057 linker_pass_final.cmd
Mark Ruvald Pedersenfbcea172019-04-29 20:35:12 +02001058 "-DLINKER_PASS2"
Sebastian Bøe2a963122019-02-08 15:49:57 +01001059 ${CODE_RELOCATION_DEP}
1060 ${ZEPHYR_PREBUILT_EXECUTABLE}
Sebastian Bøefdac7b32020-01-23 15:39:17 +01001061 zephyr_generated_headers
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +01001062 )
Andy Grosse8860fe2018-02-01 01:12:32 -06001063
Sebastian Bøe1b86fb92019-01-14 16:39:33 +01001064 set(LINKER_PASS_FINAL_SCRIPT_TARGET linker_pass_final_script_target)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001065 add_custom_target(
Sebastian Bøe1b86fb92019-01-14 16:39:33 +01001066 ${LINKER_PASS_FINAL_SCRIPT_TARGET}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001067 DEPENDS
Andy Gross1f0ff062018-01-25 11:07:03 -06001068 linker_pass_final.cmd
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001069 )
Sebastian Bøeb1ab5012017-12-14 13:03:23 +01001070 set_property(TARGET
Sebastian Bøe1b86fb92019-01-14 16:39:33 +01001071 ${LINKER_PASS_FINAL_SCRIPT_TARGET}
Sebastian Bøeb1ab5012017-12-14 13:03:23 +01001072 PROPERTY INCLUDE_DIRECTORIES
1073 ${ZEPHYR_INCLUDE_DIRS}
1074 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001075
Mark Ruvald Pedersen37d49472019-05-07 15:20:20 +02001076 add_executable( ${ZEPHYR_FINAL_EXECUTABLE} misc/empty_file.c ${GKSF})
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001077 toolchain_ld_link_elf(
1078 TARGET_ELF ${ZEPHYR_FINAL_EXECUTABLE}
Marc Herbert0370c9b2019-06-13 16:15:44 -07001079 OUTPUT_MAP ${PROJECT_BINARY_DIR}/${ZEPHYR_FINAL_EXECUTABLE}.map
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001080 LIBRARIES_PRE_SCRIPT ${GKOF}
1081 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/linker_pass_final.cmd
1082 LIBRARIES_POST_SCRIPT ""
1083 DEPENDENCIES ${CODE_RELOCATION_DEP}
1084 )
Mark Ruvald Pedersen37d49472019-05-07 15:20:20 +02001085 set_property(TARGET ${ZEPHYR_FINAL_EXECUTABLE} PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker_pass_final.cmd)
Andrew Boie28be7932020-03-11 10:56:19 -07001086 add_dependencies( ${ZEPHYR_FINAL_EXECUTABLE} ${LINKER_PASS_FINAL_SCRIPT_TARGET})
Mark Ruvald Pedersen37d49472019-05-07 15:20:20 +02001087
1088 # Use the pass2 elf as the final elf
1089 set(logical_target_for_zephyr_elf ${ZEPHYR_FINAL_EXECUTABLE})
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001090endif()
1091
Sebastian Bøe0fc39342018-10-16 13:25:04 +02001092# Export the variable to the application's scope to allow the
1093# application to know what the name of the final elf target is.
1094set(logical_target_for_zephyr_elf ${logical_target_for_zephyr_elf} PARENT_SCOPE)
1095
Marc Herbert0370c9b2019-06-13 16:15:44 -07001096# Override the base name of the last, "logical" .elf output (and last .map) so:
Marc Herbert498b4942019-04-16 23:30:52 -07001097# 1. it doesn't depend on the number of passes above and the
1098# post_build_commands below can always find it no matter which is it;
1099# 2. it can be defined in Kconfig
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001100set_target_properties(${logical_target_for_zephyr_elf} PROPERTIES OUTPUT_NAME ${KERNEL_NAME})
1101
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001102set(post_build_commands "")
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001103set(post_build_byproducts "")
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001104
Marc Herbert0370c9b2019-06-13 16:15:44 -07001105list(APPEND
1106 post_build_commands
1107 COMMAND
Torsten Rasmussen4df38c72020-06-11 10:48:06 +02001108 ${CMAKE_COMMAND} -E rename ${logical_target_for_zephyr_elf}.map ${KERNEL_MAP_NAME}
Marc Herbert0370c9b2019-06-13 16:15:44 -07001109)
1110
Håkon Øye Amundsenc086b932018-11-26 09:47:16 +00001111if(NOT CONFIG_BUILD_NO_GAP_FILL)
1112 # Use ';' as separator to get proper space in resulting command.
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001113 set(GAP_FILL "0xff")
Håkon Øye Amundsenc086b932018-11-26 09:47:16 +00001114endif()
1115
Danny Oerndrupc41e7122019-07-18 15:16:39 +02001116if(CONFIG_OUTPUT_PRINT_MEMORY_USAGE)
1117 # @Intent: Use the toolchain bintools method for printing memory usage
1118 set(memUsageCmd "")
1119 set(memUsageByProd "")
1120 bintools_print_mem_usage(
1121 RESULT_CMD_LIST memUsageCmd
1122 RESULT_BYPROD_LIST memUsageByProd
1123 )
1124 list(APPEND
1125 post_build_commands
1126 ${memUsageCmd}
1127 )
1128 list(APPEND
1129 post_build_byproducts
1130 ${memUsageByProd}
1131 )
1132endif()
1133
Kumar Galad5419132019-08-13 13:44:20 -05001134if(CONFIG_BUILD_OUTPUT_HEX OR BOARD_FLASH_RUNNER STREQUAL openocd)
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001135 set(out_hex_cmd "")
1136 set(out_hex_byprod "")
1137 set(out_hex_sections_remove
1138 .comment
1139 COMMON
1140 .eh_frame
1141 )
1142 bintools_objcopy(
1143 RESULT_CMD_LIST out_hex_cmd
1144 RESULT_BYPROD_LIST out_hex_byprod
1145 STRIP_ALL
1146 GAP_FILL ${GAP_FILL}
1147 TARGET_OUTPUT "ihex"
1148 SECTION_REMOVE ${out_hex_sections_remove}
1149 FILE_INPUT ${KERNEL_ELF_NAME}
1150 FILE_OUTPUT ${KERNEL_HEX_NAME}
1151 )
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001152 list(APPEND
1153 post_build_commands
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001154 ${out_hex_cmd}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001155 )
1156 list(APPEND
1157 post_build_byproducts
1158 ${KERNEL_HEX_NAME}
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001159 ${out_hex_byprod}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001160 )
1161endif()
Anas Nashif4592ff22017-11-23 07:54:26 -05001162
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001163if(CONFIG_BUILD_OUTPUT_BIN)
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001164 set(out_bin_cmd "")
1165 set(out_bin_byprod "")
1166 set(out_bin_sections_remove
1167 .comment
1168 COMMON
1169 .eh_frame
1170 )
1171 bintools_objcopy(
1172 RESULT_CMD_LIST out_bin_cmd
1173 RESULT_BYPROD_LIST out_bin_byprod
1174 STRIP_ALL
1175 GAP_FILL ${GAP_FILL}
1176 TARGET_OUTPUT "binary"
1177 SECTION_REMOVE ${out_bin_sections_remove}
1178 FILE_INPUT ${KERNEL_ELF_NAME}
1179 FILE_OUTPUT ${KERNEL_BIN_NAME}
1180 )
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001181 list(APPEND
1182 post_build_commands
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001183 ${out_bin_cmd}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001184 )
1185 list(APPEND
1186 post_build_byproducts
1187 ${KERNEL_BIN_NAME}
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001188 ${out_bin_byprod}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001189 )
1190endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001191
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001192if(CONFIG_BUILD_OUTPUT_S19)
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001193 set(out_S19_cmd "")
1194 set(out_S19_byprod "")
1195 bintools_objcopy(
1196 RESULT_CMD_LIST out_S19_cmd
1197 RESULT_BYPROD_LIST out_S19_byprod
1198 GAP_FILL ${GAP_FILL}
1199 TARGET_OUTPUT "srec"
1200 SREC_LEN 1
1201 FILE_INPUT ${KERNEL_ELF_NAME}
1202 FILE_OUTPUT ${KERNEL_S19_NAME}
1203 )
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001204 list(APPEND
1205 post_build_commands
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001206 ${out_S19_cmd}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001207 )
1208 list(APPEND
1209 post_build_byproducts
1210 ${KERNEL_S19_NAME}
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001211 ${out_S19_byprod}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001212 )
1213endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001214
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001215if(CONFIG_OUTPUT_DISASSEMBLY)
Danny Oerndrup0760a532019-08-01 08:01:39 +02001216 set(out_disassembly_cmd "")
1217 set(out_disassembly_byprod "")
Rohit Gujarathi35713f22020-05-07 10:08:37 +05301218 if(CONFIG_OUTPUT_DISASSEMBLE_ALL)
1219 set(disassembly_type DISASSEMBLE_ALL)
1220 else()
1221 set(disassembly_type DISASSEMBLE_SOURCE)
1222 endif()
Danny Oerndrup0760a532019-08-01 08:01:39 +02001223 bintools_objdump(
1224 RESULT_CMD_LIST out_disassembly_cmd
1225 RESULT_BYPROD_LIST out_disassembly_byprod
Rohit Gujarathi35713f22020-05-07 10:08:37 +05301226 ${disassembly_type}
Danny Oerndrup0760a532019-08-01 08:01:39 +02001227 FILE_INPUT ${KERNEL_ELF_NAME}
1228 FILE_OUTPUT ${KERNEL_LST_NAME}
1229 )
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001230 list(APPEND
1231 post_build_commands
Danny Oerndrup0760a532019-08-01 08:01:39 +02001232 ${out_disassembly_cmd}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001233 )
1234 list(APPEND
1235 post_build_byproducts
1236 ${KERNEL_LST_NAME}
Danny Oerndrup0760a532019-08-01 08:01:39 +02001237 ${out_disassembly_byprod}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001238 )
1239endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001240
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001241if(CONFIG_OUTPUT_STAT)
Danny Oerndrup919df012019-08-01 08:04:12 +02001242 set(out_stat_cmd "")
1243 set(out_stat_byprod "")
1244 bintools_readelf(
1245 RESULT_CMD_LIST out_stat_cmd
1246 RESULT_BYPROD_LIST out_stat_byprod
1247 HEADERS
1248 FILE_INPUT ${KERNEL_ELF_NAME}
1249 FILE_OUTPUT ${KERNEL_STAT_NAME}
1250 )
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001251 list(APPEND
1252 post_build_commands
Danny Oerndrup919df012019-08-01 08:04:12 +02001253 ${out_stat_cmd}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001254 )
1255 list(APPEND
1256 post_build_byproducts
1257 ${KERNEL_STAT_NAME}
Danny Oerndrup919df012019-08-01 08:04:12 +02001258 ${out_stat_byprod}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001259 )
1260endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001261
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001262if(CONFIG_BUILD_OUTPUT_STRIPPED)
Danny Oerndrup9336e3e2019-08-01 08:06:54 +02001263 set(out_stripped_cmd "")
1264 set(out_stripped_byprod "")
1265 bintools_strip(
1266 RESULT_CMD_LIST out_stripped_cmd
1267 RESULT_BYPROD_LIST out_stripped_byprod
1268 STRIP_ALL
1269 FILE_INPUT ${KERNEL_ELF_NAME}
1270 FILE_OUTPUT ${KERNEL_STRIP_NAME}
1271 )
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001272 list(APPEND
1273 post_build_commands
Danny Oerndrup9336e3e2019-08-01 08:06:54 +02001274 ${out_stripped_cmd}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001275 )
1276 list(APPEND
1277 post_build_byproducts
1278 ${KERNEL_STRIP_NAME}
Danny Oerndrup9336e3e2019-08-01 08:06:54 +02001279 ${out_stripped_byprod}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001280 )
1281endif()
1282
1283if(CONFIG_BUILD_OUTPUT_EXE)
1284 list(APPEND
1285 post_build_commands
1286 COMMAND
1287 ${CMAKE_COMMAND} -E copy ${KERNEL_ELF_NAME} ${KERNEL_EXE_NAME}
1288 )
1289 list(APPEND
1290 post_build_byproducts
1291 ${KERNEL_EXE_NAME}
1292 )
1293endif()
Anas Nashif4592ff22017-11-23 07:54:26 -05001294
Rajavardhan Gundiecdea1c2019-01-25 11:53:13 +05301295get_property(extra_post_build_commands
1296 GLOBAL PROPERTY
1297 extra_post_build_commands
1298 )
1299
1300list(APPEND
1301 post_build_commands
1302 ${extra_post_build_commands}
1303 )
1304
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001305get_property(extra_post_build_byproducts
1306 GLOBAL PROPERTY
1307 extra_post_build_byproducts
1308 )
1309
1310list(APPEND
1311 post_build_byproducts
1312 ${extra_post_build_byproducts}
1313 )
1314
Marc Herbert498b4942019-04-16 23:30:52 -07001315# Add post_build_commands to post-process the final .elf file produced by
1316# either the ZEPHYR_PREBUILT_EXECUTABLE or the KERNEL_ELF executable
1317# targets above.
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001318add_custom_command(
1319 TARGET ${logical_target_for_zephyr_elf}
1320 POST_BUILD
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001321 ${post_build_commands}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001322 BYPRODUCTS
1323 ${post_build_byproducts}
Marc Herbert498b4942019-04-16 23:30:52 -07001324 COMMENT "Generating files from ${KERNEL_ELF_NAME} for board: ${BOARD}"
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001325 # NB: COMMENT only works for some CMake-Generators
Rajavardhan Gundiecdea1c2019-01-25 11:53:13 +05301326 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001327
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001328# To populate with hex files to merge, do the following:
1329# set_property(GLOBAL APPEND PROPERTY HEX_FILES_TO_MERGE ${my_local_list})
1330# Note that the zephyr.hex file will not be included automatically.
1331get_property(HEX_FILES_TO_MERGE GLOBAL PROPERTY HEX_FILES_TO_MERGE)
1332if(HEX_FILES_TO_MERGE)
1333 # Merge in out-of-tree hex files.
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001334 set(MERGED_HEX_NAME merged.hex)
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001335
1336 add_custom_command(
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001337 OUTPUT ${MERGED_HEX_NAME}
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001338 COMMAND
1339 ${PYTHON_EXECUTABLE}
1340 ${ZEPHYR_BASE}/scripts/mergehex.py
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001341 -o ${MERGED_HEX_NAME}
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001342 ${HEX_FILES_TO_MERGE}
1343 DEPENDS ${HEX_FILES_TO_MERGE} ${logical_target_for_zephyr_elf}
1344 )
1345
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001346 add_custom_target(mergehex ALL DEPENDS ${MERGED_HEX_NAME})
Torsten Rasmussend38da9d2020-06-30 09:55:54 +02001347 list(APPEND RUNNERS_DEPS mergehex)
Håkon Øye Amundsenc9a2a5e2020-01-03 08:25:03 +00001348
1349 message(VERBOSE "Merging hex files: ${HEX_FILES_TO_MERGE}")
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001350endif()
1351
Anas Nashifc15d3c92017-11-21 18:54:55 -05001352if(EMU_PLATFORM)
Carles Cufi7d764b32018-01-11 15:46:44 +01001353 include(${ZEPHYR_BASE}/cmake/emu/${EMU_PLATFORM}.cmake)
Anas Nashiffd276ae2017-12-21 16:45:45 -05001354else()
1355 add_custom_target(run
1356 COMMAND
1357 ${CMAKE_COMMAND} -E echo
1358 "==================================================="
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +01001359 "Emulation/Simulation not supported with this board."
Anas Nashiffd276ae2017-12-21 16:45:45 -05001360 "==================================================="
1361 )
Anas Nashifc15d3c92017-11-21 18:54:55 -05001362endif()
1363
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001364add_subdirectory(cmake/flash)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001365add_subdirectory(cmake/usage)
1366add_subdirectory(cmake/reports)
1367
Sebastian Bøed3a8fd42019-10-17 14:25:34 +02001368add_subdirectory_ifdef(
1369 CONFIG_MAKEFILE_EXPORTS
1370 cmake/makefile_exports
1371 )
1372
Marc Herbert83723102019-06-17 13:26:11 -07001373if(NOT CONFIG_TEST)
Andrew Boie411686f2018-05-24 13:18:36 -07001374if(CONFIG_ASSERT AND (NOT CONFIG_FORCE_NO_ASSERT))
Sebastian Bøefa8f9d42019-12-06 12:54:53 +01001375 message(WARNING "__ASSERT() statements are globally ENABLED")
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001376endif()
Marc Herbert83723102019-06-17 13:26:11 -07001377endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001378
Vincent Wana2bc5142020-01-09 14:20:44 -08001379if(CONFIG_BOARD_DEPRECATED_RELEASE)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001380 message(WARNING "
1381 WARNING: The board '${BOARD}' is deprecated and will be
Vincent Wana2bc5142020-01-09 14:20:44 -08001382 removed in version ${CONFIG_BOARD_DEPRECATED_RELEASE}"
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +01001383 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001384endif()
Paul Sokolovsky1c6a7d72019-06-06 21:12:14 +03001385
Vincent Wan180b4df2020-01-08 17:10:51 -08001386if(CONFIG_SOC_DEPRECATED_RELEASE)
1387 message(WARNING "
1388 WARNING: The SoC '${SOC_NAME}' is deprecated and will be
1389 removed in version ${CONFIG_SOC_DEPRECATED_RELEASE}"
1390 )
1391endif()
1392
Sebastian Bøee50e12d2019-08-29 16:19:32 +02001393# In CMake projects, 'CMAKE_BUILD_TYPE' usually determines the
1394# optimization flag, but in Zephyr it is determined through
1395# Kconfig. Here we give a warning when there is a mismatch between the
1396# two in case the user is not aware of this.
1397set(build_types None Debug Release RelWithDebInfo MinSizeRel)
1398
1399if((CMAKE_BUILD_TYPE IN_LIST build_types) AND (NOT NO_BUILD_TYPE_WARNING))
1400 string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_uppercase)
1401
Jack Dähne41bdcd2019-11-15 19:01:39 +01001402 if(NOT (${OPTIMIZATION_FLAG} IN_LIST CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_uppercase}))
Sebastian Bøee50e12d2019-08-29 16:19:32 +02001403 message(WARNING "
1404 The CMake build type was set to '${CMAKE_BUILD_TYPE}', but the optimization flag was set to '${OPTIMIZATION_FLAG}'.
1405 This may be intentional and the warning can be turned off by setting the CMake variable 'NO_BUILD_TYPE_WARNING'"
1406 )
1407 endif()
1408endif()
1409
Paul Sokolovsky1c6a7d72019-06-06 21:12:14 +03001410# @Intent: Set compiler specific flags for standard C includes
1411# Done at the very end, so any other system includes which may
1412# be added by Zephyr components were first in list.
1413toolchain_cc_nostdinc()