blob: c786770bee4f2f051fa17fdb5135287d7c408796 [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)
57
58
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 +0000354 if(CONFIG_ARM)
355 set(PRIV_STACK_DEP priv_stacks_prebuilt)
356 endif()
357endif()
358
Håkon Øye Amundsena4494392018-11-29 09:14:27 +0000359get_property(TOPT GLOBAL PROPERTY TOPT)
Oleg Zhurakivskyy22119352019-03-08 11:29:33 +0200360set_ifndef( TOPT -Wl,-T) # clang doesn't pick -T for some reason and complains,
361 # while -Wl,-T works for both, gcc and clang
Håkon Øye Amundsena4494392018-11-29 09:14:27 +0000362
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200363if(CONFIG_HAVE_CUSTOM_LINKER_SCRIPT)
364 set(LINKER_SCRIPT ${APPLICATION_SOURCE_DIR}/${CONFIG_CUSTOM_LINKER_SCRIPT})
Sebastian Bøec1aa9d12018-04-12 14:48:05 +0200365 if(NOT EXISTS ${LINKER_SCRIPT})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200366 set(LINKER_SCRIPT ${CONFIG_CUSTOM_LINKER_SCRIPT})
Sebastian Bøec1aa9d12018-04-12 14:48:05 +0200367 assert_exists(CONFIG_CUSTOM_LINKER_SCRIPT)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200368 endif()
369else()
370 # Try a board specific linker file
371 set(LINKER_SCRIPT ${BOARD_DIR}/linker.ld)
372 if(NOT EXISTS ${LINKER_SCRIPT})
373 # If not available, try an SoC specific linker file
Anas Nashif96455d52018-09-04 14:34:06 -0500374 set(LINKER_SCRIPT ${SOC_DIR}/${ARCH}/${SOC_PATH}/linker.ld)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200375 endif()
376endif()
377
378if(NOT EXISTS ${LINKER_SCRIPT})
379 message(FATAL_ERROR "Could not find linker script: '${LINKER_SCRIPT}'. Corrupted configuration?")
380endif()
381
Kristian Klomsten Skordal0225e952018-01-30 11:26:42 +0100382# Custom section support in linker scripts requires that the application source
383# directory is in the preprocessor search path, in order to find the custom
384# linker script fragments.
385if(CONFIG_CUSTOM_RODATA_LD OR CONFIG_CUSTOM_RWDATA_LD OR CONFIG_CUSTOM_SECTIONS_LD)
386 zephyr_include_directories(${APPLICATION_SOURCE_DIR})
387endif()
388
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200389configure_file(version.h.in ${PROJECT_BINARY_DIR}/include/generated/version.h)
390
Sebastian Bøec23cc262018-10-09 16:03:29 +0200391# Error-out when the deprecated naming convention is found (until
392# after 1.14.0 has been released)
393foreach(path
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100394 ${BOARD_DIR}/dts.fixup
395 ${PROJECT_SOURCE_DIR}/soc/${ARCH}/${SOC_PATH}/dts.fixup
Sebastian Bøec23cc262018-10-09 16:03:29 +0200396 ${APPLICATION_SOURCE_DIR}/dts.fixup
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100397 )
Sebastian Bøec23cc262018-10-09 16:03:29 +0200398 if(EXISTS ${path})
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100399 message(FATAL_ERROR
400 "A deprecated filename has been detected. Porting is required."
401 "The file '${path}' exists, but it should be named dts_fixup.h instead."
402 "See https://github.com/zephyrproject-rtos/zephyr/pull/10352 for more details"
403 )
Sebastian Bøec23cc262018-10-09 16:03:29 +0200404 endif()
405endforeach()
406
407set_ifndef( DTS_BOARD_FIXUP_FILE ${BOARD_DIR}/dts_fixup.h)
408set_ifndef( DTS_SOC_FIXUP_FILE ${SOC_DIR}/${ARCH}/${SOC_PATH}/dts_fixup.h)
409set( DTS_APP_FIXUP_FILE ${APPLICATION_SOURCE_DIR}/dts_fixup.h)
Sebastian Bøec23cc262018-10-09 16:03:29 +0200410
Ulf Magnusson4e850062020-01-16 13:29:53 +0100411set_ifndef(DTS_CAT_OF_FIXUP_FILES ${ZEPHYR_BINARY_DIR}/include/generated/devicetree_fixups.h)
Sebastian Bøe361fdaa2019-01-28 13:40:50 +0100412
413# Concatenate the fixups into a single header file for easy
Sebastian Bøec23cc262018-10-09 16:03:29 +0200414# #include'ing
Ulf Magnusson4e850062020-01-16 13:29:53 +0100415file(WRITE ${DTS_CAT_OF_FIXUP_FILES} "/* May only be included by devicetree.h */\n\n")
Sebastian Bøe361fdaa2019-01-28 13:40:50 +0100416foreach(fixup_file
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100417 ${DTS_BOARD_FIXUP_FILE}
418 ${DTS_SOC_FIXUP_FILE}
419 ${DTS_APP_FIXUP_FILE}
420 ${shield_dts_fixups}
421 )
Sebastian Bøe361fdaa2019-01-28 13:40:50 +0100422 if(EXISTS ${fixup_file})
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100423 file(READ ${fixup_file} contents)
424 file(APPEND ${DTS_CAT_OF_FIXUP_FILES} "${contents}")
Sebastian Bøec23cc262018-10-09 16:03:29 +0200425 endif()
426endforeach()
427
Sebastian Bøe6f946e22018-01-09 10:52:57 +0100428# Unfortunately, the order in which CMakeLists.txt code is processed
429# matters so we need to be careful about how we order the processing
430# of subdirectories. One example is "Compiler flags added late in the
431# build are not exported to external build systems #5605"; when we
432# integrate with an external build system we read out all compiler
433# flags when the external project is created. So an external project
434# defined in subsys or ext will not get global flags added by drivers/
435# or tests/ as the subdirectories are ordered now.
436#
437# Another example of when the order matters is the reading and writing
438# of global properties such as ZEPHYR_LIBS or
439# GENERATED_KERNEL_OBJECT_FILES.
440#
441# Arch is placed early because it defines important compiler flags
442# that must be exported to external build systems defined in
443# e.g. subsys/.
444add_subdirectory(arch)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200445add_subdirectory(lib)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200446# We use include instead of add_subdirectory to avoid creating a new directory scope.
447# This is because source file properties are directory scoped, including the GENERATED
448# property which is set implicitly for custom command outputs
449include(misc/generated/CMakeLists.txt)
Anas Nashif3d1252f2018-09-03 15:20:14 -0500450
Erwan Gouriouba31cb52018-09-13 16:25:53 +0200451if(EXISTS ${SOC_DIR}/${ARCH}/CMakeLists.txt)
Anas Nashif96455d52018-09-04 14:34:06 -0500452 add_subdirectory(${SOC_DIR}/${ARCH} soc/${ARCH})
Anas Nashif3d1252f2018-09-03 15:20:14 -0500453else()
Anas Nashif96455d52018-09-04 14:34:06 -0500454 add_subdirectory(${SOC_DIR}/${ARCH}/${SOC_PATH} soc/${ARCH}/${SOC_PATH})
Anas Nashif3d1252f2018-09-03 15:20:14 -0500455endif()
456
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200457add_subdirectory(boards)
458add_subdirectory(ext)
459add_subdirectory(subsys)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200460add_subdirectory(drivers)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200461
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100462# Include zephyr modules generated CMake file.
463if(EXISTS ${CMAKE_BINARY_DIR}/zephyr_modules.txt)
Carles Cufi3ad1f272019-07-18 10:38:25 +0200464 file(STRINGS ${CMAKE_BINARY_DIR}/zephyr_modules.txt ZEPHYR_MODULES_TXT
465 ENCODING UTF-8)
Torsten Rasmussen08eabb82019-12-06 12:19:35 +0100466 set(module_names)
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100467
468 foreach(module ${ZEPHYR_MODULES_TXT})
Carles Cufi766edd42019-03-31 22:29:30 +0200469 # Match "<name>":"<path>" for each line of file, each corresponding to
470 # one module. The use of quotes is required due to CMake not supporting
471 # lazy regexes (it supports greedy only).
472 string(REGEX REPLACE "\"(.*)\":\".*\"" "\\1" module_name ${module})
473 string(REGEX REPLACE "\".*\":\"(.*)\"" "\\1" module_path ${module})
Torsten Rasmussen08eabb82019-12-06 12:19:35 +0100474
475 list(APPEND module_names ${module_name})
476
477 string(TOUPPER ${module_name} MODULE_NAME_UPPER)
478 set(ZEPHYR_${MODULE_NAME_UPPER}_MODULE_DIR ${module_path})
479 endforeach()
480
481 foreach(module_name ${module_names})
Marc Herbertd3d33942019-05-31 15:37:40 -0700482 # Note the second, binary_dir parameter requires the added
483 # subdirectory to have its own, local cmake target(s). If not then
484 # this binary_dir is created but stays empty. Object files land in
485 # the main binary dir instead.
486 # https://cmake.org/pipermail/cmake/2019-June/069547.html
Torsten Rasmussen08eabb82019-12-06 12:19:35 +0100487 string(TOUPPER ${module_name} MODULE_NAME_UPPER)
488 set(ZEPHYR_CURRENT_MODULE_DIR ${ZEPHYR_${MODULE_NAME_UPPER}_MODULE_DIR})
Torsten Rasmussen4b68f952020-01-09 12:31:58 +0100489 add_subdirectory(${ZEPHYR_CURRENT_MODULE_DIR} ${CMAKE_BINARY_DIR}/modules/${module_name})
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100490 endforeach()
Torsten Rasmussenf0fa7b82019-10-21 10:08:20 +0200491 # Done processing modules, clear ZEPHYR_CURRENT_MODULE_DIR.
492 set(ZEPHYR_CURRENT_MODULE_DIR)
Torsten Rasmussen605ae102019-02-12 10:19:38 +0100493endif()
Torsten Rasmussen7e9d1bd2019-02-05 10:36:22 +0100494
Sebastian Bøe13a68402017-11-20 13:03:55 +0100495set(syscall_list_h ${CMAKE_CURRENT_BINARY_DIR}/include/generated/syscall_list.h)
496set(syscalls_json ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls.json)
497
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200498# The syscalls subdirs txt file is constructed by python containing a list of folders to use for
499# dependency handling, including empty folders.
500# Windows: The list is used to specify DIRECTORY list with CMAKE_CONFIGURE_DEPENDS attribute.
501# Other OS: The list will update whenever a file is added/removed/modified and ensure a re-build.
502set(syscalls_subdirs_txt ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_subdirs.txt)
503
504# As syscalls_subdirs_txt is updated whenever a file is modified, this file can not be used for
505# monitoring of added / removed folders. A trigger file is thus used for correct dependency
506# handling. The trigger file will update when a folder is added / removed.
507set(syscalls_subdirs_trigger ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_subdirs.trigger)
508
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200509if(NOT (${CMAKE_HOST_SYSTEM_NAME} STREQUAL Windows))
510 set(syscalls_links --create-links ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_links)
511endif()
512
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200513# When running CMake it must be ensured that all dependencies are correctly acquired.
514execute_process(
515 COMMAND
516 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200517 ${ZEPHYR_BASE}/scripts/subfolder_list.py
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200518 --directory ${ZEPHYR_BASE}/include # Walk this directory
519 --out-file ${syscalls_subdirs_txt} # Write file with discovered folder
520 --trigger ${syscalls_subdirs_trigger} # Trigger file that is used for json generation
521 ${syscalls_links} # If defined, create symlinks for dependencies
522)
Carles Cufi3ad1f272019-07-18 10:38:25 +0200523file(STRINGS ${syscalls_subdirs_txt} PARSE_SYSCALLS_PATHS_DEPENDS ENCODING UTF-8)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200524
525if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL Windows)
526 # On windows only adding/removing files or folders will be reflected in depends.
527 # Hence adding a file requires CMake to re-run to add this file to the file list.
528 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${PARSE_SYSCALLS_PATHS_DEPENDS})
529
530 # Also On Windows each header file must be monitored as file modifications are not reflected
531 # on directory level.
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200532 file(GLOB_RECURSE PARSE_SYSCALLS_HEADER_DEPENDS ${ZEPHYR_BASE}/include/*.h)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200533else()
534 # The syscall parsing depends on the folders in order to detect add/removed/modified files.
535 # When a folder is removed, CMake will try to find a target that creates that dependency.
536 # This command sets up the target for CMake to find.
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200537 # Without this code, CMake will fail with the following error:
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200538 # <folder> needed by '<target>', missing and no known rule to make it
539 # when a folder is removed.
540 add_custom_command(OUTPUT ${PARSE_SYSCALLS_PATHS_DEPENDS}
541 COMMAND ${CMAKE_COMMAND} -E echo ""
542 COMMENT "Preparing syscall dependency handling"
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200543 )
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200544
545 add_custom_command(
546 OUTPUT
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200547 ${syscalls_subdirs_trigger}
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200548 COMMAND
549 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200550 ${ZEPHYR_BASE}/scripts/subfolder_list.py
551 --directory ${ZEPHYR_BASE}/include # Walk this directory
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200552 --out-file ${syscalls_subdirs_txt} # Write file with discovered folder
553 --trigger ${syscalls_subdirs_trigger} # Trigger file that is used for json generation
554 ${syscalls_links} # If defined, create symlinks for dependencies
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200555 DEPENDS ${PARSE_SYSCALLS_PATHS_DEPENDS}
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200556 )
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200557
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200558 # Ensure subdir file always exists when specifying CMake dependency.
559 if(NOT EXISTS ${syscalls_subdirs_txt})
560 file(WRITE ${syscalls_subdirs_txt} "")
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200561 endif()
562
563 # On other OS'es, modifying a file is reflected on the folder timestamp and hence detected
564 # when using depend on directory level.
565 # Thus CMake only needs to re-run when sub-directories are added / removed, which is indicated
566 # using a trigger file.
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200567 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${syscalls_subdirs_txt})
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200568endif()
569
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200570# syscall declarations are searched for in the SYSCALL_INCLUDE_DIRS
Adithya Baglodye67720b2018-07-02 14:59:19 +0530571if(CONFIG_APPLICATION_DEFINED_SYSCALL)
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200572 list(APPEND SYSCALL_INCLUDE_DIRS ${APPLICATION_SOURCE_DIR})
Adithya Baglodye67720b2018-07-02 14:59:19 +0530573endif()
574
Andrew Boiec1863872019-11-21 23:11:29 -0800575if(CONFIG_ZTEST)
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200576 list(APPEND SYSCALL_INCLUDE_DIRS ${ZEPHYR_BASE}/subsys/testsuite/ztest/include)
Andrew Boiec1863872019-11-21 23:11:29 -0800577endif()
578
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200579foreach(d ${SYSCALL_INCLUDE_DIRS})
580 list(APPEND parse_syscalls_include_args
581 --include ${d}
582 )
583endforeach()
584
Sebastian Bøe13a68402017-11-20 13:03:55 +0100585add_custom_command(
586 OUTPUT
587 ${syscalls_json}
588 COMMAND
589 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200590 ${ZEPHYR_BASE}/scripts/parse_syscalls.py
Adithya Baglodye67720b2018-07-02 14:59:19 +0530591 --include ${ZEPHYR_BASE}/include # Read files from this dir
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200592 ${parse_syscalls_include_args} # Read files from these dirs also
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200593 --json-file ${syscalls_json} # Write this file
594 DEPENDS ${syscalls_subdirs_trigger} ${PARSE_SYSCALLS_HEADER_DEPENDS}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100595 )
596
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100597add_custom_target(${SYSCALL_LIST_H_TARGET} DEPENDS ${syscall_list_h})
Andrew Boie9ff64bb2019-11-05 09:39:05 -0800598
599# 64-bit systems do not require special handling of 64-bit system call
600# parameters or return values, indicate this to the system call boilerplate
601# generation script.
602if(CONFIG_64BIT)
603 set(SYSCALL_LONG_REGISTERS_ARG --long-registers)
604endif()
605
Sebastian Bøe13a68402017-11-20 13:03:55 +0100606add_custom_command(OUTPUT include/generated/syscall_dispatch.c ${syscall_list_h}
607 # Also, some files are written to include/generated/syscalls/
608 COMMAND
609 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200610 ${ZEPHYR_BASE}/scripts/gen_syscalls.py
Sebastian Bøe13a68402017-11-20 13:03:55 +0100611 --json-file ${syscalls_json} # Read this file
612 --base-output include/generated/syscalls # Write to this dir
613 --syscall-dispatch include/generated/syscall_dispatch.c # Write this file
Andrew Boie353acf42018-07-23 18:10:15 -0700614 --syscall-list ${syscall_list_h}
Andrew Boie9ff64bb2019-11-05 09:39:05 -0800615 ${SYSCALL_LONG_REGISTERS_ARG}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100616 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
617 DEPENDS ${syscalls_json}
618 )
619
Leandro Pereirac2003672018-04-04 13:50:32 -0700620set(DRV_VALIDATION ${PROJECT_BINARY_DIR}/include/generated/driver-validation.h)
621add_custom_command(
622 OUTPUT ${DRV_VALIDATION}
623 COMMAND
624 ${PYTHON_EXECUTABLE}
625 ${ZEPHYR_BASE}/scripts/gen_kobject_list.py
626 --validation-output ${DRV_VALIDATION}
627 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Bradley Bolen4198ba72019-04-12 12:00:52 -0400628 DEPENDS ${ZEPHYR_BASE}/scripts/gen_kobject_list.py
Leandro Pereirac2003672018-04-04 13:50:32 -0700629 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
630 )
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100631add_custom_target(${DRIVER_VALIDATION_H_TARGET} DEPENDS ${DRV_VALIDATION})
Leandro Pereirac2003672018-04-04 13:50:32 -0700632
Leandro Pereira39dc7d02018-04-05 13:59:33 -0700633include($ENV{ZEPHYR_BASE}/cmake/kobj.cmake)
634gen_kobj(KOBJ_INCLUDE_PATH)
Leandro Pereirac2003672018-04-04 13:50:32 -0700635
Sebastian Bøefdac7b32020-01-23 15:39:17 +0100636# Add a pseudo-target that is up-to-date when all generated headers
637# are up-to-date.
638
639add_custom_target(zephyr_generated_headers)
640add_dependencies(zephyr_generated_headers
641 offsets_h
642 )
643
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200644# Generate offsets.c.obj from offsets.c
645# Generate offsets.h from offsets.c.obj
646
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100647set(OFFSETS_LIB offsets)
648
Klaus Petersenc66cb762018-11-15 10:37:46 +0100649set(OFFSETS_C_PATH ${ARCH_DIR}/${ARCH}/core/offsets/offsets.c)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200650set(OFFSETS_H_PATH ${PROJECT_BINARY_DIR}/include/generated/offsets.h)
651
Klaus Petersen62e55e52019-02-04 12:10:57 +0100652add_library( ${OFFSETS_LIB} OBJECT ${OFFSETS_C_PATH})
Stephanos Ioannidis2d746042019-10-25 00:08:21 +0900653target_include_directories(${OFFSETS_LIB} PRIVATE
654 kernel/include
655 ${ARCH_DIR}/${ARCH}/include
656 )
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100657target_link_libraries(${OFFSETS_LIB} zephyr_interface)
658add_dependencies( ${OFFSETS_LIB}
659 ${SYSCALL_LIST_H_TARGET}
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100660 ${DRIVER_VALIDATION_H_TARGET}
661 ${KOBJ_TYPES_H_TARGET}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100662 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200663
664add_custom_command(
665 OUTPUT ${OFFSETS_H_PATH}
Carles Cufi7d764b32018-01-11 15:46:44 +0100666 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/gen_offset_header.py
Klaus Petersen62e55e52019-02-04 12:10:57 +0100667 -i $<TARGET_OBJECTS:${OFFSETS_LIB}>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200668 -o ${OFFSETS_H_PATH}
Sebastian Bøe5962aab2019-08-15 14:45:59 +0200669 DEPENDS
670 ${OFFSETS_LIB}
671 $<TARGET_OBJECTS:${OFFSETS_LIB}>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200672)
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100673add_custom_target(${OFFSETS_H_TARGET} DEPENDS ${OFFSETS_H_PATH})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200674
675zephyr_include_directories(${TOOLCHAIN_INCLUDES})
676
Sebastian Bøe89516fb2017-12-01 15:25:06 +0100677zephyr_get_include_directories_for_lang(C ZEPHYR_INCLUDES)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200678
679add_subdirectory(kernel)
680
681# Read list content
682get_property(ZEPHYR_LIBS_PROPERTY GLOBAL PROPERTY ZEPHYR_LIBS)
683
684foreach(zephyr_lib ${ZEPHYR_LIBS_PROPERTY})
685 # TODO: Could this become an INTERFACE property of zephyr_interface?
Sebastian Bøefdac7b32020-01-23 15:39:17 +0100686 add_dependencies(${zephyr_lib} zephyr_generated_headers)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200687endforeach()
688
689get_property(OUTPUT_FORMAT GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT)
690
Adithya Baglody62e152a2018-11-13 15:34:02 +0530691if (CONFIG_CODE_DATA_RELOCATION)
692 set(CODE_RELOCATION_DEP code_relocation_source_lib)
693endif() # CONFIG_CODE_DATA_RELOCATION
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100694
Mark Ruvald Pedersen4c811972019-04-29 17:16:54 +0200695configure_linker_script(
Mark Ruvald Pedersen1073fba2019-04-29 20:27:23 +0200696 linker.cmd
Mark Ruvald Pedersenfbcea172019-04-29 20:35:12 +0200697 ""
Sebastian Bøe2a963122019-02-08 15:49:57 +0100698 ${PRIV_STACK_DEP}
Daniel Leung212ec9a2019-03-10 14:20:21 -0700699 ${APP_SMEM_ALIGNED_DEP}
Sebastian Bøe2a963122019-02-08 15:49:57 +0100700 ${CODE_RELOCATION_DEP}
Sebastian Bøefdac7b32020-01-23 15:39:17 +0100701 zephyr_generated_headers
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100702 )
Andy Grosse8860fe2018-02-01 01:12:32 -0600703
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200704add_custom_target(
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100705 ${LINKER_SCRIPT_TARGET}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200706 DEPENDS
707 linker.cmd
Sebastian Bøeb1ab5012017-12-14 13:03:23 +0100708 )
709
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100710# Give the '${LINKER_SCRIPT_TARGET}' target all of the include directories so
Sebastian Bøeb1ab5012017-12-14 13:03:23 +0100711# that cmake can successfully find the linker_script's header
712# dependencies.
713zephyr_get_include_directories_for_lang(C
714 ZEPHYR_INCLUDE_DIRS
715 STRIP_PREFIX # Don't use a -I prefix
716 )
717set_property(TARGET
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100718 ${LINKER_SCRIPT_TARGET}
Sebastian Bøeb1ab5012017-12-14 13:03:23 +0100719 PROPERTY INCLUDE_DIRECTORIES
720 ${ZEPHYR_INCLUDE_DIRS}
721 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200722
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200723if(CONFIG_GEN_ISR_TABLES)
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530724 if(CONFIG_GEN_SW_ISR_TABLE)
725 list(APPEND GEN_ISR_TABLE_EXTRA_ARG --sw-isr-table)
726 endif()
727
728 if(CONFIG_GEN_IRQ_VECTOR_TABLE)
729 list(APPEND GEN_ISR_TABLE_EXTRA_ARG --vector-table)
730 endif()
731
Sebastian Bøe15260702019-01-14 16:31:02 +0100732 # isr_tables.c is generated from ${ZEPHYR_PREBUILT_EXECUTABLE} by
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200733 # gen_isr_tables.py
Danny Oerndrup51634cd2019-08-01 07:56:45 +0200734 set(obj_copy_cmd "")
735 bintools_objcopy(
736 RESULT_CMD_LIST obj_copy_cmd
737 TARGET_INPUT ${OUTPUT_FORMAT}
738 TARGET_OUTPUT "binary"
739 SECTION_ONLY ".intList"
740 FILE_INPUT $<TARGET_FILE:${ZEPHYR_PREBUILT_EXECUTABLE}>
741 FILE_OUTPUT "isrList.bin"
742 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200743 add_custom_command(
744 OUTPUT isr_tables.c
Danny Oerndrup51634cd2019-08-01 07:56:45 +0200745 ${obj_copy_cmd}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200746 COMMAND ${PYTHON_EXECUTABLE}
Carles Cufi7d764b32018-01-11 15:46:44 +0100747 ${ZEPHYR_BASE}/arch/common/gen_isr_tables.py
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200748 --output-source isr_tables.c
Sebastian Bøe15260702019-01-14 16:31:02 +0100749 --kernel $<TARGET_FILE:${ZEPHYR_PREBUILT_EXECUTABLE}>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200750 --intlist isrList.bin
Yasushi SHOJI6fc0d772018-10-09 18:59:16 +0900751 $<$<BOOL:${CONFIG_BIG_ENDIAN}>:--big-endian>
Sebastian Bøea55279a2018-01-04 14:08:39 +0100752 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--debug>
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530753 ${GEN_ISR_TABLE_EXTRA_ARG}
Sebastian Bøe15260702019-01-14 16:31:02 +0100754 DEPENDS ${ZEPHYR_PREBUILT_EXECUTABLE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200755 )
756 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_SOURCE_FILES isr_tables.c)
757endif()
758
Adithya Baglody62e152a2018-11-13 15:34:02 +0530759if(CONFIG_CODE_DATA_RELOCATION)
Mark Ruvald Pedersen86a3e8f2019-05-03 10:33:03 +0200760 # @Intent: Linker script to relocate .text, data and .bss sections
761 toolchain_ld_relocation()
Adithya Baglody62e152a2018-11-13 15:34:02 +0530762endif()
763
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530764if(CONFIG_USERSPACE)
765 zephyr_get_compile_options_for_lang_as_string(C compiler_flags_priv)
766 string(REPLACE "-ftest-coverage" "" NO_COVERAGE_FLAGS "${compiler_flags_priv}")
767 string(REPLACE "-fprofile-arcs" "" NO_COVERAGE_FLAGS "${NO_COVERAGE_FLAGS}")
Daniel Leung84b1bba2019-04-01 15:57:35 -0700768 string(REPLACE "-fno-inline" "" NO_COVERAGE_FLAGS "${NO_COVERAGE_FLAGS}")
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530769
770 get_property(include_dir_in_interface TARGET zephyr_interface
771 PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
772
773 get_property(sys_include_dir_in_interface TARGET zephyr_interface
774 PROPERTY INTERFACE_SYSTEM_INCLUDE_DIRECTORIES)
775
776 get_property(compile_definitions_interface TARGET zephyr_interface
777 PROPERTY INTERFACE_COMPILE_DEFINITIONS)
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530778endif()
779
780
Marc Herbert4a10eea2019-04-16 15:39:45 -0700781# Warning most of this gperf code is duplicated below for
782# gen_kobject_list.py / output_lib
Chunlin Han18560a02018-02-01 01:19:49 -0600783if(CONFIG_ARM AND CONFIG_USERSPACE)
784 set(GEN_PRIV_STACKS $ENV{ZEPHYR_BASE}/scripts/gen_priv_stacks.py)
785 set(PROCESS_PRIV_STACKS_GPERF $ENV{ZEPHYR_BASE}/scripts/process_gperf.py)
786
787 set(PRIV_STACKS priv_stacks_hash.gperf)
788 set(PRIV_STACKS_OUTPUT_SRC_PRE priv_stacks_hash_preprocessed.c)
789 set(PRIV_STACKS_OUTPUT_SRC priv_stacks_hash.c)
790 set(PRIV_STACKS_OUTPUT_OBJ priv_stacks_hash.c.obj)
791 set(PRIV_STACKS_OUTPUT_OBJ_RENAMED priv_stacks_hash_renamed.o)
792
793 # Essentially what we are doing here is extracting some information
794 # out of the nearly finished elf file, generating the source code
795 # for a hash table based on that information, and then compiling and
796 # linking the hash table back into a now even more nearly finished
797 # elf file.
798
799 # Use the script GEN_PRIV_STACKS to scan the kernel binary's
Sebastian Bøe15260702019-01-14 16:31:02 +0100800 # (${ZEPHYR_PREBUILT_EXECUTABLE}) DWARF information to produce a table of kernel
Chunlin Han18560a02018-02-01 01:19:49 -0600801 # objects (PRIV_STACKS) which we will then pass to gperf
802 add_custom_command(
803 OUTPUT ${PRIV_STACKS}
804 COMMAND
805 ${PYTHON_EXECUTABLE}
806 ${GEN_PRIV_STACKS}
807 --kernel $<TARGET_FILE:priv_stacks_prebuilt>
808 --output ${PRIV_STACKS}
809 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
810 DEPENDS priv_stacks_prebuilt
811 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
812 )
813 add_custom_target(priv_stacks DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS})
814
Sebastian Bøef17428a2020-01-29 15:39:33 +0100815 if(${GPERF} STREQUAL GPERF-NOTFOUND)
816 message(FATAL_ERROR "Unable to find gperf")
817 endif()
818
Chunlin Han18560a02018-02-01 01:19:49 -0600819 # Use gperf to generate C code (PRIV_STACKS_OUTPUT_SRC_PRE) which implements a
820 # perfect hashtable based on PRIV_STACKS
821 add_custom_command(
822 OUTPUT ${PRIV_STACKS_OUTPUT_SRC_PRE}
823 COMMAND
824 ${GPERF} -C
825 --output-file ${PRIV_STACKS_OUTPUT_SRC_PRE}
826 ${PRIV_STACKS}
Andy Gross878f39c2018-05-01 01:10:26 -0500827 DEPENDS priv_stacks ${PRIV_STACKS}
Chunlin Han18560a02018-02-01 01:19:49 -0600828 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
829 )
830 add_custom_target(priv_stacks_output_src_pre DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_SRC_PRE})
831
832 # For our purposes the code/data generated by gperf is not optimal.
833 #
834 # The script PROCESS_GPERF creates a new c file OUTPUT_SRC based on
835 # OUTPUT_SRC_PRE to greatly reduce the amount of code/data generated
836 # since we know we are always working with pointer values
837 add_custom_command(
838 OUTPUT ${PRIV_STACKS_OUTPUT_SRC}
839 COMMAND
Sebastian Bøe1b600702018-06-21 14:34:42 +0200840 ${PYTHON_EXECUTABLE}
Chunlin Han18560a02018-02-01 01:19:49 -0600841 ${PROCESS_PRIV_STACKS_GPERF}
842 -i ${PRIV_STACKS_OUTPUT_SRC_PRE}
843 -o ${PRIV_STACKS_OUTPUT_SRC}
844 -p "struct _k_priv_stack_map"
845 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Andy Gross878f39c2018-05-01 01:10:26 -0500846 DEPENDS priv_stacks_output_src_pre ${PRIV_STACKS_OUTPUT_SRC_PRE}
Chunlin Han18560a02018-02-01 01:19:49 -0600847 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
848 )
849 add_custom_target(priv_stacks_output_src DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_SRC})
850
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530851 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_SRC}
852 PROPERTIES COMPILE_DEFINITIONS "${compile_definitions_interface}")
853
854 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_SRC}
855 PROPERTIES COMPILE_FLAGS
Andrew Boiea5148982019-03-14 17:04:11 -0700856 "${NO_COVERAGE_FLAGS} -fno-function-sections -fno-data-sections ")
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530857
Chunlin Han18560a02018-02-01 01:19:49 -0600858 # We need precise control of where generated text/data ends up in the final
859 # kernel image. Disable function/data sections and use objcopy to move
860 # generated data into special section names
861 add_library(priv_stacks_output_lib STATIC
862 ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_SRC}
863 )
864
Chunlin Han18560a02018-02-01 01:19:49 -0600865 # Turn off -ffunction-sections, etc.
866 # NB: Using a library instead of target_compile_options(priv_stacks_output_lib
867 # [...]) because a library's options have precedence
868 add_library(priv_stacks_output_lib_interface INTERFACE)
Kumar Gala3713ea42019-03-14 11:50:08 -0500869 foreach(incl ${include_dir_in_interface})
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530870 target_include_directories(priv_stacks_output_lib_interface INTERFACE ${incl})
871 endforeach()
872
Kumar Gala3713ea42019-03-14 11:50:08 -0500873 foreach(incl ${sys_include_dir_in_interface})
874 target_include_directories(priv_stacks_output_lib_interface SYSTEM INTERFACE ${incl})
875 endforeach()
876
Chunlin Han18560a02018-02-01 01:19:49 -0600877 target_link_libraries(priv_stacks_output_lib priv_stacks_output_lib_interface)
878
879 set(PRIV_STACKS_OUTPUT_OBJ_PATH ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/priv_stacks_output_lib.dir/${PRIV_STACKS_OUTPUT_OBJ})
880
Danny Oerndrup51634cd2019-08-01 07:56:45 +0200881 set(obj_copy_cmd "")
882 set(obj_copy_sections_rename
883 .bss=.priv_stacks.noinit
884 .data=.priv_stacks.data
885 .text=.priv_stacks.text
886 .rodata=.priv_stacks.rodata
887 )
888 bintools_objcopy(
889 RESULT_CMD_LIST obj_copy_cmd
890 SECTION_RENAME ${obj_copy_sections_rename}
891 FILE_INPUT ${PRIV_STACKS_OUTPUT_OBJ_PATH}
892 FILE_OUTPUT ${PRIV_STACKS_OUTPUT_OBJ_RENAMED}
893 )
Chunlin Han18560a02018-02-01 01:19:49 -0600894 add_custom_command(
895 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_OBJ_RENAMED}
Danny Oerndrup51634cd2019-08-01 07:56:45 +0200896 ${obj_copy_cmd}
Chunlin Han18560a02018-02-01 01:19:49 -0600897 DEPENDS priv_stacks_output_lib
898 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
899 )
900 add_custom_target(priv_stacks_output_obj_renamed DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_OBJ_RENAMED})
901
902 add_library(priv_stacks_output_obj_renamed_lib STATIC IMPORTED GLOBAL)
903 set_property(
904 TARGET priv_stacks_output_obj_renamed_lib
905 PROPERTY
906 IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_OBJ_RENAMED}
907 )
908 add_dependencies(
909 priv_stacks_output_obj_renamed_lib
910 priv_stacks_output_obj_renamed
911 )
912
913 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_OBJECT_FILES priv_stacks_output_obj_renamed_lib)
914endif()
915
Marc Herbert4a10eea2019-04-16 15:39:45 -0700916# Warning: most of this gperf code is duplicated above for
917# gen_priv_stacks.py / priv_stacks_output_lib
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200918if(CONFIG_USERSPACE)
Carles Cufi7d764b32018-01-11 15:46:44 +0100919 set(GEN_KOBJ_LIST ${ZEPHYR_BASE}/scripts/gen_kobject_list.py)
920 set(PROCESS_GPERF ${ZEPHYR_BASE}/scripts/process_gperf.py)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200921
922 set(OBJ_LIST kobject_hash.gperf)
923 set(OUTPUT_SRC_PRE kobject_hash_preprocessed.c)
924 set(OUTPUT_SRC kobject_hash.c)
925 set(OUTPUT_OBJ kobject_hash.c.obj)
926 set(OUTPUT_OBJ_RENAMED kobject_hash_renamed.o)
927
928 # Essentially what we are doing here is extracting some information
929 # out of the nearly finished elf file, generating the source code
930 # for a hash table based on that information, and then compiling and
931 # linking the hash table back into a now even more nearly finished
Marc Herbert4a10eea2019-04-16 15:39:45 -0700932 # elf file. More information in gen_kobject_list.py --help.
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200933
934 # Use the script GEN_KOBJ_LIST to scan the kernel binary's
Sebastian Bøe15260702019-01-14 16:31:02 +0100935 # (${ZEPHYR_PREBUILT_EXECUTABLE}) DWARF information to produce a table of kernel
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200936 # objects (OBJ_LIST) which we will then pass to gperf
937 add_custom_command(
938 OUTPUT ${OBJ_LIST}
939 COMMAND
940 ${PYTHON_EXECUTABLE}
941 ${GEN_KOBJ_LIST}
Sebastian Bøe15260702019-01-14 16:31:02 +0100942 --kernel $<TARGET_FILE:${ZEPHYR_PREBUILT_EXECUTABLE}>
Leandro Pereirac2003672018-04-04 13:50:32 -0700943 --gperf-output ${OBJ_LIST}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200944 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Sebastian Bøe15260702019-01-14 16:31:02 +0100945 DEPENDS ${ZEPHYR_PREBUILT_EXECUTABLE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200946 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
947 )
948 add_custom_target(obj_list DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OBJ_LIST})
949
950 # Use gperf to generate C code (OUTPUT_SRC_PRE) which implements a
951 # perfect hashtable based on OBJ_LIST
952 add_custom_command(
953 OUTPUT ${OUTPUT_SRC_PRE}
954 COMMAND
955 ${GPERF}
956 --output-file ${OUTPUT_SRC_PRE}
957 ${OBJ_LIST}
Sebastian Bøef5758b52018-01-31 10:42:46 +0100958 DEPENDS obj_list ${OBJ_LIST}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200959 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
960 )
961 add_custom_target(output_src_pre DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_SRC_PRE})
962
963 # For our purposes the code/data generated by gperf is not optimal.
964 #
965 # The script PROCESS_GPERF creates a new c file OUTPUT_SRC based on
966 # OUTPUT_SRC_PRE to greatly reduce the amount of code/data generated
967 # since we know we are always working with pointer values
968 add_custom_command(
969 OUTPUT ${OUTPUT_SRC}
970 COMMAND
Sebastian Bøe1b600702018-06-21 14:34:42 +0200971 ${PYTHON_EXECUTABLE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200972 ${PROCESS_GPERF}
973 -i ${OUTPUT_SRC_PRE}
974 -o ${OUTPUT_SRC}
Chunlin Han18560a02018-02-01 01:19:49 -0600975 -p "struct _k_object"
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200976 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Sebastian Bøef5758b52018-01-31 10:42:46 +0100977 DEPENDS output_src_pre ${OUTPUT_SRC_PRE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200978 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
979 )
980 add_custom_target(output_src DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_SRC})
981
982 # We need precise control of where generated text/data ends up in the final
983 # kernel image. Disable function/data sections and use objcopy to move
984 # generated data into special section names
985 add_library(output_lib STATIC
986 ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_SRC}
987 )
988
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530989 set_source_files_properties(${OUTPUT_SRC} PROPERTIES COMPILE_FLAGS
Andrew Boiea5148982019-03-14 17:04:11 -0700990 "${NO_COVERAGE_FLAGS} -fno-function-sections -fno-data-sections")
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530991
992 set_source_files_properties(${OUTPUT_SRC}
993 PROPERTIES COMPILE_DEFINITIONS "${compile_definitions_interface}")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200994
995 # Turn off -ffunction-sections, etc.
996 # NB: Using a library instead of target_compile_options(output_lib
997 # [...]) because a library's options have precedence
998 add_library(output_lib_interface INTERFACE)
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530999
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001000 target_link_libraries(output_lib output_lib_interface)
1001
Kumar Gala3713ea42019-03-14 11:50:08 -05001002 foreach(incl ${include_dir_in_interface})
Adithya Baglody4b3c7b32018-11-21 14:31:56 +05301003 target_include_directories(output_lib_interface INTERFACE ${incl})
1004 endforeach()
1005
Kumar Gala3713ea42019-03-14 11:50:08 -05001006 foreach(incl ${sys_include_dir_in_interface})
1007 target_include_directories(output_lib_interface SYSTEM INTERFACE ${incl})
1008 endforeach()
Adithya Baglody4b3c7b32018-11-21 14:31:56 +05301009
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001010 set(OUTPUT_OBJ_PATH ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/output_lib.dir/${OUTPUT_OBJ})
1011
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001012 set(obj_copy_cmd "")
1013 set(obj_copy_sections_rename
1014 .data=.kobject_data.data
1015 .text=.kobject_data.text
1016 .rodata=.kobject_data.rodata
1017 )
1018 bintools_objcopy(
1019 RESULT_CMD_LIST obj_copy_cmd
1020 SECTION_RENAME ${obj_copy_sections_rename}
1021 FILE_INPUT ${OUTPUT_OBJ_PATH}
1022 FILE_OUTPUT ${OUTPUT_OBJ_RENAMED}
1023 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001024 add_custom_command(
1025 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_OBJ_RENAMED}
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001026 ${obj_copy_cmd}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001027 DEPENDS output_lib
1028 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1029 )
1030 add_custom_target(output_obj_renamed DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_OBJ_RENAMED})
1031
1032 add_library(output_obj_renamed_lib STATIC IMPORTED GLOBAL)
1033 set_property(
1034 TARGET output_obj_renamed_lib
1035 PROPERTY
1036 IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_OBJ_RENAMED}
1037 )
1038 add_dependencies(
1039 output_obj_renamed_lib
1040 output_obj_renamed
1041 )
1042
1043 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_OBJECT_FILES output_obj_renamed_lib)
1044endif()
1045
1046# Read global variables into local variables
1047get_property(GKOF GLOBAL PROPERTY GENERATED_KERNEL_OBJECT_FILES)
1048get_property(GKSF GLOBAL PROPERTY GENERATED_KERNEL_SOURCE_FILES)
1049
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +02001050
Alberto Escolar Piedrasc2882412018-07-05 10:51:03 +02001051get_property(CSTD GLOBAL PROPERTY CSTD)
1052set_ifndef(CSTD c99)
1053
Danny Oerndrup6331dae2019-06-13 15:33:03 +02001054# @Intent: Obtain compiler specific flag for specifying the c standard
1055toolchain_cc_cstd_flag(CC_CSTD ${CSTD})
Alberto Escolar Piedrasc2882412018-07-05 10:51:03 +02001056zephyr_compile_options(
Danny Oerndrup6331dae2019-06-13 15:33:03 +02001057 $<$<COMPILE_LANGUAGE:C>:${CC_CSTD}>
Alberto Escolar Piedrasc2882412018-07-05 10:51:03 +02001058)
1059
Mark Ruvald Pedersen197197a2019-05-03 11:02:56 +02001060# @Intent: Configure linker scripts, i.e. generate linker scripts with variables substituted
1061toolchain_ld_configure_files()
Daniel Leung212ec9a2019-03-10 14:20:21 -07001062
Andrew Boie4ce652e2019-02-22 16:08:44 -08001063if(CONFIG_USERSPACE)
Daniel Leung212ec9a2019-03-10 14:20:21 -07001064 set(APP_SMEM_ALIGNED_LD "${PROJECT_BINARY_DIR}/include/generated/app_smem_aligned.ld")
1065 set(APP_SMEM_UNALIGNED_LD "${PROJECT_BINARY_DIR}/include/generated/app_smem_unaligned.ld")
Adithya Baglody50950eb2018-12-20 15:47:42 +05301066 set(OBJ_FILE_DIR "${PROJECT_BINARY_DIR}/../")
1067
1068 add_custom_target(
Daniel Leung212ec9a2019-03-10 14:20:21 -07001069 ${APP_SMEM_ALIGNED_DEP}
Sebastian Bøe2a963122019-02-08 15:49:57 +01001070 DEPENDS
Daniel Leung212ec9a2019-03-10 14:20:21 -07001071 ${APP_SMEM_ALIGNED_LD}
1072 )
1073
1074 add_custom_target(
1075 ${APP_SMEM_UNALIGNED_DEP}
1076 DEPENDS
1077 ${APP_SMEM_UNALIGNED_LD}
Adithya Baglody50950eb2018-12-20 15:47:42 +05301078 )
1079
Andrew Boie4b4f7732019-02-01 12:18:31 -08001080 if(CONFIG_NEWLIB_LIBC)
Andrew Boie17ce8222019-02-21 13:44:54 -08001081 set(NEWLIB_PART -l libc.a z_libc_partition)
Andrew Boie4b4f7732019-02-01 12:18:31 -08001082 endif()
Ioannis Glaropoulosd58f8be2019-11-15 14:07:51 +01001083 if(CONFIG_NEWLIB_LIBC_NANO)
1084 set(NEWLIB_PART -l libc_nano.a z_libc_partition)
1085 endif()
Andrew Boiee686aef2019-02-27 14:41:45 -08001086
Adithya Baglody50950eb2018-12-20 15:47:42 +05301087 add_custom_command(
Daniel Leung212ec9a2019-03-10 14:20:21 -07001088 OUTPUT ${APP_SMEM_UNALIGNED_LD}
Adithya Baglody50950eb2018-12-20 15:47:42 +05301089 COMMAND ${PYTHON_EXECUTABLE}
1090 ${ZEPHYR_BASE}/scripts/gen_app_partitions.py
1091 -d ${OBJ_FILE_DIR}
Daniel Leung212ec9a2019-03-10 14:20:21 -07001092 -o ${APP_SMEM_UNALIGNED_LD}
Torsten Rasmussen5a703c82019-11-05 10:24:08 +01001093 ${NEWLIB_PART}
Torsten Rasmussen1f9723a2019-11-04 14:30:24 +01001094 $<TARGET_PROPERTY:zephyr_property_target,COMPILE_OPTIONS>
Adithya Baglody50950eb2018-12-20 15:47:42 +05301095 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Sebastian Bøe2a963122019-02-08 15:49:57 +01001096 DEPENDS
1097 kernel
1098 ${ZEPHYR_LIBS_PROPERTY}
Adithya Baglody50950eb2018-12-20 15:47:42 +05301099 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
Torsten Rasmussen1f9723a2019-11-04 14:30:24 +01001100 COMMAND_EXPAND_LISTS
Daniel Leung212ec9a2019-03-10 14:20:21 -07001101 COMMENT "Generating app_smem_unaligned linker section"
1102 )
1103
Mark Ruvald Pedersen4c811972019-04-29 17:16:54 +02001104 configure_linker_script(
Mark Ruvald Pedersen1073fba2019-04-29 20:27:23 +02001105 linker_app_smem_unaligned.cmd
Mark Ruvald Pedersenfbcea172019-04-29 20:35:12 +02001106 "-DLINKER_APP_SMEM_UNALIGNED"
Daniel Leung212ec9a2019-03-10 14:20:21 -07001107 ${CODE_RELOCATION_DEP}
1108 ${APP_SMEM_UNALIGNED_DEP}
1109 ${APP_SMEM_UNALIGNED_LD}
Sebastian Bøefdac7b32020-01-23 15:39:17 +01001110 zephyr_generated_headers
Daniel Leung212ec9a2019-03-10 14:20:21 -07001111 )
Daniel Leung212ec9a2019-03-10 14:20:21 -07001112
1113 add_custom_target(
1114 linker_app_smem_unaligned_script
1115 DEPENDS
1116 linker_app_smem_unaligned.cmd
1117 )
1118
1119 set_property(TARGET
1120 linker_app_smem_unaligned_script
1121 PROPERTY INCLUDE_DIRECTORIES
1122 ${ZEPHYR_INCLUDE_DIRS}
1123 )
1124
1125 set(APP_SMEM_UNALIGNED_LIB app_smem_unaligned_output_obj_renamed_lib)
1126 add_executable( app_smem_unaligned_prebuilt misc/empty_file.c)
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001127 toolchain_ld_link_elf(
1128 TARGET_ELF app_smem_unaligned_prebuilt
Marc Herbert0370c9b2019-06-13 16:15:44 -07001129 OUTPUT_MAP ${PROJECT_BINARY_DIR}/app_smem_unaligned_prebuilt.map
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001130 LIBRARIES_PRE_SCRIPT ""
1131 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/linker_app_smem_unaligned.cmd
1132 LIBRARIES_POST_SCRIPT ""
1133 DEPENDENCIES ${CODE_RELOCATION_DEP}
1134 )
Daniel Leung212ec9a2019-03-10 14:20:21 -07001135 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 +02001136 add_dependencies( app_smem_unaligned_prebuilt linker_app_smem_unaligned_script ${OFFSETS_LIB})
Daniel Leung212ec9a2019-03-10 14:20:21 -07001137
1138 add_custom_command(
1139 OUTPUT ${APP_SMEM_ALIGNED_LD}
1140 COMMAND ${PYTHON_EXECUTABLE}
1141 ${ZEPHYR_BASE}/scripts/gen_app_partitions.py
1142 -e $<TARGET_FILE:app_smem_unaligned_prebuilt>
1143 -o ${APP_SMEM_ALIGNED_LD}
Torsten Rasmussen5a703c82019-11-05 10:24:08 +01001144 ${NEWLIB_PART}
Torsten Rasmussen1f9723a2019-11-04 14:30:24 +01001145 $<TARGET_PROPERTY:zephyr_property_target,COMPILE_OPTIONS>
Daniel Leung212ec9a2019-03-10 14:20:21 -07001146 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
1147 DEPENDS
1148 kernel
1149 ${ZEPHYR_LIBS_PROPERTY}
1150 app_smem_unaligned_prebuilt
1151 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
Torsten Rasmussen1f9723a2019-11-04 14:30:24 +01001152 COMMAND_EXPAND_LISTS
Daniel Leung212ec9a2019-03-10 14:20:21 -07001153 COMMENT "Generating app_smem_aligned linker section"
Adithya Baglody50950eb2018-12-20 15:47:42 +05301154 )
1155endif()
1156
Andrew Boie41f60112019-01-31 15:53:24 -08001157if(CONFIG_USERSPACE AND CONFIG_ARM)
Mark Ruvald Pedersen4c811972019-04-29 17:16:54 +02001158 configure_linker_script(
Mark Ruvald Pedersen1073fba2019-04-29 20:27:23 +02001159 linker_priv_stacks.cmd
Mark Ruvald Pedersenfbcea172019-04-29 20:35:12 +02001160 ""
Sebastian Bøe2a963122019-02-08 15:49:57 +01001161 ${CODE_RELOCATION_DEP}
Daniel Leung212ec9a2019-03-10 14:20:21 -07001162 ${APP_SMEM_ALIGNED_DEP}
1163 ${APP_SMEM_ALIGNED_LD}
Sebastian Bøefdac7b32020-01-23 15:39:17 +01001164 zephyr_generated_headers
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +01001165 )
Chunlin Han18560a02018-02-01 01:19:49 -06001166
1167 add_custom_target(
1168 linker_priv_stacks_script
1169 DEPENDS
Chunlin Han18560a02018-02-01 01:19:49 -06001170 linker_priv_stacks.cmd
Chunlin Han18560a02018-02-01 01:19:49 -06001171 )
1172
1173 set_property(TARGET
1174 linker_priv_stacks_script
1175 PROPERTY INCLUDE_DIRECTORIES
1176 ${ZEPHYR_INCLUDE_DIRS}
1177 )
1178
1179 set(PRIV_STACK_LIB priv_stacks_output_obj_renamed_lib)
1180 add_executable( priv_stacks_prebuilt misc/empty_file.c)
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001181 toolchain_ld_link_elf(
1182 TARGET_ELF priv_stacks_prebuilt
Marc Herbert0370c9b2019-06-13 16:15:44 -07001183 OUTPUT_MAP ${PROJECT_BINARY_DIR}/priv_stacks_prebuilt.map
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001184 LIBRARIES_PRE_SCRIPT ""
1185 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/linker_priv_stacks.cmd
1186 LIBRARIES_POST_SCRIPT ""
1187 DEPENDENCIES ${CODE_RELOCATION_DEP}
1188 )
Chunlin Han18560a02018-02-01 01:19:49 -06001189 set_property(TARGET priv_stacks_prebuilt PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker_priv_stacks.cmd)
Mark Ruvald Pedersen85af2802019-04-29 14:49:33 +02001190 add_dependencies( priv_stacks_prebuilt linker_priv_stacks_script ${OFFSETS_LIB})
Wayne Ren5a0ba2f2018-02-12 19:17:04 +08001191endif()
Chunlin Han18560a02018-02-01 01:19:49 -06001192
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001193# FIXME: Is there any way to get rid of empty_file.c?
Sebastian Bøe15260702019-01-14 16:31:02 +01001194add_executable( ${ZEPHYR_PREBUILT_EXECUTABLE} misc/empty_file.c)
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001195toolchain_ld_link_elf(
1196 TARGET_ELF ${ZEPHYR_PREBUILT_EXECUTABLE}
Marc Herbert0370c9b2019-06-13 16:15:44 -07001197 OUTPUT_MAP ${PROJECT_BINARY_DIR}/${ZEPHYR_PREBUILT_EXECUTABLE}.map
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001198 LIBRARIES_PRE_SCRIPT ""
1199 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/linker.cmd
1200 LIBRARIES_POST_SCRIPT ${PRIV_STACK_LIB}
1201 DEPENDENCIES ${CODE_RELOCATION_DEP}
1202)
Sebastian Bøe15260702019-01-14 16:31:02 +01001203set_property(TARGET ${ZEPHYR_PREBUILT_EXECUTABLE} PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker.cmd)
Mark Ruvald Pedersen85af2802019-04-29 14:49:33 +02001204add_dependencies( ${ZEPHYR_PREBUILT_EXECUTABLE} ${PRIV_STACK_DEP} ${LINKER_SCRIPT_TARGET} ${OFFSETS_LIB})
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001205
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +02001206
Marc Herbert498b4942019-04-16 23:30:52 -07001207set(generated_kernel_files ${GKSF} ${GKOF})
1208if(NOT generated_kernel_files)
1209 # Use the prebuilt elf as the final elf since we don't have a
1210 # generation stage.
1211 set(logical_target_for_zephyr_elf ${ZEPHYR_PREBUILT_EXECUTABLE})
1212else()
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001213 # The second linker pass uses the same source linker script of the
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +01001214 # first pass (LINKER_SCRIPT), but this time with a different output
1215 # file and preprocessed with the define LINKER_PASS2.
Mark Ruvald Pedersen4c811972019-04-29 17:16:54 +02001216 configure_linker_script(
Mark Ruvald Pedersen1073fba2019-04-29 20:27:23 +02001217 linker_pass_final.cmd
Mark Ruvald Pedersenfbcea172019-04-29 20:35:12 +02001218 "-DLINKER_PASS2"
Sebastian Bøe2a963122019-02-08 15:49:57 +01001219 ${PRIV_STACK_DEP}
1220 ${CODE_RELOCATION_DEP}
1221 ${ZEPHYR_PREBUILT_EXECUTABLE}
Sebastian Bøefdac7b32020-01-23 15:39:17 +01001222 zephyr_generated_headers
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +01001223 )
Andy Grosse8860fe2018-02-01 01:12:32 -06001224
Sebastian Bøe1b86fb92019-01-14 16:39:33 +01001225 set(LINKER_PASS_FINAL_SCRIPT_TARGET linker_pass_final_script_target)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001226 add_custom_target(
Sebastian Bøe1b86fb92019-01-14 16:39:33 +01001227 ${LINKER_PASS_FINAL_SCRIPT_TARGET}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001228 DEPENDS
Andy Gross1f0ff062018-01-25 11:07:03 -06001229 linker_pass_final.cmd
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001230 )
Sebastian Bøeb1ab5012017-12-14 13:03:23 +01001231 set_property(TARGET
Sebastian Bøe1b86fb92019-01-14 16:39:33 +01001232 ${LINKER_PASS_FINAL_SCRIPT_TARGET}
Sebastian Bøeb1ab5012017-12-14 13:03:23 +01001233 PROPERTY INCLUDE_DIRECTORIES
1234 ${ZEPHYR_INCLUDE_DIRS}
1235 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001236
Mark Ruvald Pedersen37d49472019-05-07 15:20:20 +02001237 add_executable( ${ZEPHYR_FINAL_EXECUTABLE} misc/empty_file.c ${GKSF})
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001238 toolchain_ld_link_elf(
1239 TARGET_ELF ${ZEPHYR_FINAL_EXECUTABLE}
Marc Herbert0370c9b2019-06-13 16:15:44 -07001240 OUTPUT_MAP ${PROJECT_BINARY_DIR}/${ZEPHYR_FINAL_EXECUTABLE}.map
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001241 LIBRARIES_PRE_SCRIPT ${GKOF}
1242 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/linker_pass_final.cmd
1243 LIBRARIES_POST_SCRIPT ""
1244 DEPENDENCIES ${CODE_RELOCATION_DEP}
1245 )
Mark Ruvald Pedersen37d49472019-05-07 15:20:20 +02001246 set_property(TARGET ${ZEPHYR_FINAL_EXECUTABLE} PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker_pass_final.cmd)
1247 add_dependencies( ${ZEPHYR_FINAL_EXECUTABLE} ${PRIV_STACK_DEP} ${LINKER_PASS_FINAL_SCRIPT_TARGET})
1248
1249 # Use the pass2 elf as the final elf
1250 set(logical_target_for_zephyr_elf ${ZEPHYR_FINAL_EXECUTABLE})
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001251endif()
1252
Sebastian Bøe0fc39342018-10-16 13:25:04 +02001253# Export the variable to the application's scope to allow the
1254# application to know what the name of the final elf target is.
1255set(logical_target_for_zephyr_elf ${logical_target_for_zephyr_elf} PARENT_SCOPE)
1256
Marc Herbert0370c9b2019-06-13 16:15:44 -07001257# Override the base name of the last, "logical" .elf output (and last .map) so:
Marc Herbert498b4942019-04-16 23:30:52 -07001258# 1. it doesn't depend on the number of passes above and the
1259# post_build_commands below can always find it no matter which is it;
1260# 2. it can be defined in Kconfig
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001261set_target_properties(${logical_target_for_zephyr_elf} PROPERTIES OUTPUT_NAME ${KERNEL_NAME})
1262
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001263set(post_build_commands "")
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001264set(post_build_byproducts "")
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001265
Marc Herbert0370c9b2019-06-13 16:15:44 -07001266list(APPEND
1267 post_build_commands
1268 COMMAND
1269 cmake -E rename ${logical_target_for_zephyr_elf}.map ${KERNEL_MAP_NAME}
1270)
1271
Håkon Øye Amundsenc086b932018-11-26 09:47:16 +00001272if(NOT CONFIG_BUILD_NO_GAP_FILL)
1273 # Use ';' as separator to get proper space in resulting command.
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001274 set(GAP_FILL "0xff")
Håkon Øye Amundsenc086b932018-11-26 09:47:16 +00001275endif()
1276
Danny Oerndrupc41e7122019-07-18 15:16:39 +02001277if(CONFIG_OUTPUT_PRINT_MEMORY_USAGE)
1278 # @Intent: Use the toolchain bintools method for printing memory usage
1279 set(memUsageCmd "")
1280 set(memUsageByProd "")
1281 bintools_print_mem_usage(
1282 RESULT_CMD_LIST memUsageCmd
1283 RESULT_BYPROD_LIST memUsageByProd
1284 )
1285 list(APPEND
1286 post_build_commands
1287 ${memUsageCmd}
1288 )
1289 list(APPEND
1290 post_build_byproducts
1291 ${memUsageByProd}
1292 )
1293endif()
1294
Kumar Galad5419132019-08-13 13:44:20 -05001295if(CONFIG_BUILD_OUTPUT_HEX OR BOARD_FLASH_RUNNER STREQUAL openocd)
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001296 set(out_hex_cmd "")
1297 set(out_hex_byprod "")
1298 set(out_hex_sections_remove
1299 .comment
1300 COMMON
1301 .eh_frame
1302 )
1303 bintools_objcopy(
1304 RESULT_CMD_LIST out_hex_cmd
1305 RESULT_BYPROD_LIST out_hex_byprod
1306 STRIP_ALL
1307 GAP_FILL ${GAP_FILL}
1308 TARGET_OUTPUT "ihex"
1309 SECTION_REMOVE ${out_hex_sections_remove}
1310 FILE_INPUT ${KERNEL_ELF_NAME}
1311 FILE_OUTPUT ${KERNEL_HEX_NAME}
1312 )
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001313 list(APPEND
1314 post_build_commands
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001315 ${out_hex_cmd}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001316 )
1317 list(APPEND
1318 post_build_byproducts
1319 ${KERNEL_HEX_NAME}
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001320 ${out_hex_byprod}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001321 )
1322endif()
Anas Nashif4592ff22017-11-23 07:54:26 -05001323
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001324if(CONFIG_BUILD_OUTPUT_BIN)
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001325 set(out_bin_cmd "")
1326 set(out_bin_byprod "")
1327 set(out_bin_sections_remove
1328 .comment
1329 COMMON
1330 .eh_frame
1331 )
1332 bintools_objcopy(
1333 RESULT_CMD_LIST out_bin_cmd
1334 RESULT_BYPROD_LIST out_bin_byprod
1335 STRIP_ALL
1336 GAP_FILL ${GAP_FILL}
1337 TARGET_OUTPUT "binary"
1338 SECTION_REMOVE ${out_bin_sections_remove}
1339 FILE_INPUT ${KERNEL_ELF_NAME}
1340 FILE_OUTPUT ${KERNEL_BIN_NAME}
1341 )
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001342 list(APPEND
1343 post_build_commands
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001344 ${out_bin_cmd}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001345 )
1346 list(APPEND
1347 post_build_byproducts
1348 ${KERNEL_BIN_NAME}
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001349 ${out_bin_byprod}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001350 )
1351endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001352
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001353if(CONFIG_BUILD_OUTPUT_S19)
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001354 set(out_S19_cmd "")
1355 set(out_S19_byprod "")
1356 bintools_objcopy(
1357 RESULT_CMD_LIST out_S19_cmd
1358 RESULT_BYPROD_LIST out_S19_byprod
1359 GAP_FILL ${GAP_FILL}
1360 TARGET_OUTPUT "srec"
1361 SREC_LEN 1
1362 FILE_INPUT ${KERNEL_ELF_NAME}
1363 FILE_OUTPUT ${KERNEL_S19_NAME}
1364 )
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001365 list(APPEND
1366 post_build_commands
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001367 ${out_S19_cmd}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001368 )
1369 list(APPEND
1370 post_build_byproducts
1371 ${KERNEL_S19_NAME}
Danny Oerndrup51634cd2019-08-01 07:56:45 +02001372 ${out_S19_byprod}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001373 )
1374endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001375
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001376if(CONFIG_OUTPUT_DISASSEMBLY)
Danny Oerndrup0760a532019-08-01 08:01:39 +02001377 set(out_disassembly_cmd "")
1378 set(out_disassembly_byprod "")
1379 bintools_objdump(
1380 RESULT_CMD_LIST out_disassembly_cmd
1381 RESULT_BYPROD_LIST out_disassembly_byprod
1382 DISASSEMBLE_SOURCE
1383 FILE_INPUT ${KERNEL_ELF_NAME}
1384 FILE_OUTPUT ${KERNEL_LST_NAME}
1385 )
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001386 list(APPEND
1387 post_build_commands
Danny Oerndrup0760a532019-08-01 08:01:39 +02001388 ${out_disassembly_cmd}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001389 )
1390 list(APPEND
1391 post_build_byproducts
1392 ${KERNEL_LST_NAME}
Danny Oerndrup0760a532019-08-01 08:01:39 +02001393 ${out_disassembly_byprod}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001394 )
1395endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001396
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001397if(CONFIG_OUTPUT_STAT)
Danny Oerndrup919df012019-08-01 08:04:12 +02001398 set(out_stat_cmd "")
1399 set(out_stat_byprod "")
1400 bintools_readelf(
1401 RESULT_CMD_LIST out_stat_cmd
1402 RESULT_BYPROD_LIST out_stat_byprod
1403 HEADERS
1404 FILE_INPUT ${KERNEL_ELF_NAME}
1405 FILE_OUTPUT ${KERNEL_STAT_NAME}
1406 )
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001407 list(APPEND
1408 post_build_commands
Danny Oerndrup919df012019-08-01 08:04:12 +02001409 ${out_stat_cmd}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001410 )
1411 list(APPEND
1412 post_build_byproducts
1413 ${KERNEL_STAT_NAME}
Danny Oerndrup919df012019-08-01 08:04:12 +02001414 ${out_stat_byprod}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001415 )
1416endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001417
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001418if(CONFIG_BUILD_OUTPUT_STRIPPED)
Danny Oerndrup9336e3e2019-08-01 08:06:54 +02001419 set(out_stripped_cmd "")
1420 set(out_stripped_byprod "")
1421 bintools_strip(
1422 RESULT_CMD_LIST out_stripped_cmd
1423 RESULT_BYPROD_LIST out_stripped_byprod
1424 STRIP_ALL
1425 FILE_INPUT ${KERNEL_ELF_NAME}
1426 FILE_OUTPUT ${KERNEL_STRIP_NAME}
1427 )
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001428 list(APPEND
1429 post_build_commands
Danny Oerndrup9336e3e2019-08-01 08:06:54 +02001430 ${out_stripped_cmd}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001431 )
1432 list(APPEND
1433 post_build_byproducts
1434 ${KERNEL_STRIP_NAME}
Danny Oerndrup9336e3e2019-08-01 08:06:54 +02001435 ${out_stripped_byprod}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001436 )
1437endif()
1438
1439if(CONFIG_BUILD_OUTPUT_EXE)
1440 list(APPEND
1441 post_build_commands
1442 COMMAND
1443 ${CMAKE_COMMAND} -E copy ${KERNEL_ELF_NAME} ${KERNEL_EXE_NAME}
1444 )
1445 list(APPEND
1446 post_build_byproducts
1447 ${KERNEL_EXE_NAME}
1448 )
1449endif()
Anas Nashif4592ff22017-11-23 07:54:26 -05001450
Rajavardhan Gundiecdea1c2019-01-25 11:53:13 +05301451get_property(extra_post_build_commands
1452 GLOBAL PROPERTY
1453 extra_post_build_commands
1454 )
1455
1456list(APPEND
1457 post_build_commands
1458 ${extra_post_build_commands}
1459 )
1460
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001461get_property(extra_post_build_byproducts
1462 GLOBAL PROPERTY
1463 extra_post_build_byproducts
1464 )
1465
1466list(APPEND
1467 post_build_byproducts
1468 ${extra_post_build_byproducts}
1469 )
1470
Marc Herbert498b4942019-04-16 23:30:52 -07001471# Add post_build_commands to post-process the final .elf file produced by
1472# either the ZEPHYR_PREBUILT_EXECUTABLE or the KERNEL_ELF executable
1473# targets above.
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001474add_custom_command(
1475 TARGET ${logical_target_for_zephyr_elf}
1476 POST_BUILD
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001477 ${post_build_commands}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001478 BYPRODUCTS
1479 ${post_build_byproducts}
Marc Herbert498b4942019-04-16 23:30:52 -07001480 COMMENT "Generating files from ${KERNEL_ELF_NAME} for board: ${BOARD}"
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001481 # NB: COMMENT only works for some CMake-Generators
Rajavardhan Gundiecdea1c2019-01-25 11:53:13 +05301482 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001483
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001484# To populate with hex files to merge, do the following:
1485# set_property(GLOBAL APPEND PROPERTY HEX_FILES_TO_MERGE ${my_local_list})
1486# Note that the zephyr.hex file will not be included automatically.
1487get_property(HEX_FILES_TO_MERGE GLOBAL PROPERTY HEX_FILES_TO_MERGE)
1488if(HEX_FILES_TO_MERGE)
1489 # Merge in out-of-tree hex files.
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001490 set(MERGED_HEX_NAME merged.hex)
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001491
1492 add_custom_command(
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001493 OUTPUT ${MERGED_HEX_NAME}
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001494 COMMAND
1495 ${PYTHON_EXECUTABLE}
1496 ${ZEPHYR_BASE}/scripts/mergehex.py
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001497 -o ${MERGED_HEX_NAME}
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001498 ${HEX_FILES_TO_MERGE}
1499 DEPENDS ${HEX_FILES_TO_MERGE} ${logical_target_for_zephyr_elf}
1500 )
1501
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001502 add_custom_target(mergehex ALL DEPENDS ${MERGED_HEX_NAME})
Håkon Øye Amundsen0da5d242018-12-05 09:15:40 +00001503 list(APPEND FLASH_DEPS mergehex)
Håkon Øye Amundsenc9a2a5e2020-01-03 08:25:03 +00001504
1505 message(VERBOSE "Merging hex files: ${HEX_FILES_TO_MERGE}")
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001506endif()
1507
Anas Nashifc15d3c92017-11-21 18:54:55 -05001508if(EMU_PLATFORM)
Carles Cufi7d764b32018-01-11 15:46:44 +01001509 include(${ZEPHYR_BASE}/cmake/emu/${EMU_PLATFORM}.cmake)
Anas Nashiffd276ae2017-12-21 16:45:45 -05001510else()
1511 add_custom_target(run
1512 COMMAND
1513 ${CMAKE_COMMAND} -E echo
1514 "==================================================="
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +01001515 "Emulation/Simulation not supported with this board."
Anas Nashiffd276ae2017-12-21 16:45:45 -05001516 "==================================================="
1517 )
Anas Nashifc15d3c92017-11-21 18:54:55 -05001518endif()
1519
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001520add_subdirectory(cmake/flash)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001521add_subdirectory(cmake/usage)
1522add_subdirectory(cmake/reports)
1523
Sebastian Bøed3a8fd42019-10-17 14:25:34 +02001524add_subdirectory_ifdef(
1525 CONFIG_MAKEFILE_EXPORTS
1526 cmake/makefile_exports
1527 )
1528
Marc Herbert83723102019-06-17 13:26:11 -07001529if(NOT CONFIG_TEST)
Andrew Boie411686f2018-05-24 13:18:36 -07001530if(CONFIG_ASSERT AND (NOT CONFIG_FORCE_NO_ASSERT))
Sebastian Bøefa8f9d42019-12-06 12:54:53 +01001531 message(WARNING "__ASSERT() statements are globally ENABLED")
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001532endif()
Marc Herbert83723102019-06-17 13:26:11 -07001533endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001534
Vincent Wana2bc5142020-01-09 14:20:44 -08001535if(CONFIG_BOARD_DEPRECATED_RELEASE)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001536 message(WARNING "
1537 WARNING: The board '${BOARD}' is deprecated and will be
Vincent Wana2bc5142020-01-09 14:20:44 -08001538 removed in version ${CONFIG_BOARD_DEPRECATED_RELEASE}"
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +01001539 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001540endif()
Paul Sokolovsky1c6a7d72019-06-06 21:12:14 +03001541
Vincent Wan180b4df2020-01-08 17:10:51 -08001542if(CONFIG_SOC_DEPRECATED_RELEASE)
1543 message(WARNING "
1544 WARNING: The SoC '${SOC_NAME}' is deprecated and will be
1545 removed in version ${CONFIG_SOC_DEPRECATED_RELEASE}"
1546 )
1547endif()
1548
Sebastian Bøee50e12d2019-08-29 16:19:32 +02001549# In CMake projects, 'CMAKE_BUILD_TYPE' usually determines the
1550# optimization flag, but in Zephyr it is determined through
1551# Kconfig. Here we give a warning when there is a mismatch between the
1552# two in case the user is not aware of this.
1553set(build_types None Debug Release RelWithDebInfo MinSizeRel)
1554
1555if((CMAKE_BUILD_TYPE IN_LIST build_types) AND (NOT NO_BUILD_TYPE_WARNING))
1556 string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_uppercase)
1557
Jack Dähne41bdcd2019-11-15 19:01:39 +01001558 if(NOT (${OPTIMIZATION_FLAG} IN_LIST CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_uppercase}))
Sebastian Bøee50e12d2019-08-29 16:19:32 +02001559 message(WARNING "
1560 The CMake build type was set to '${CMAKE_BUILD_TYPE}', but the optimization flag was set to '${OPTIMIZATION_FLAG}'.
1561 This may be intentional and the warning can be turned off by setting the CMake variable 'NO_BUILD_TYPE_WARNING'"
1562 )
1563 endif()
1564endif()
1565
Paul Sokolovsky1c6a7d72019-06-06 21:12:14 +03001566# @Intent: Set compiler specific flags for standard C includes
1567# Done at the very end, so any other system includes which may
1568# be added by Zephyr components were first in list.
1569toolchain_cc_nostdinc()