blob: 9ed7c187331696a9ee3ce27a9b0b62d8b45972d6 [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
Andy Ross544a38e2020-06-25 17:42:51 -070087 ${SOC_DIR}/${ARCH}/${SOC_FAMILY}/common/include
Sebastian Bøe40363392019-01-25 10:14:13 +010088 )
89 if(EXISTS ${optional_include_dir})
90 zephyr_include_directories(${optional_include_dir})
91 endif()
92endforeach()
93
Sebastian Bøe12f8f762017-10-27 15:43:34 +020094zephyr_compile_definitions(
95 KERNEL
96 __ZEPHYR__=1
Anas Nashif34aebad2018-01-03 12:26:19 -050097)
98
Mark Ruvald Pedersen01592072019-01-10 12:07:51 +010099# @Intent: Set compiler flags to enable buffer overflow checks in libc functions
100# @config in CONFIG_NO_OPTIMIZATIONS optional : Optimizations may affect security
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200101zephyr_compile_definitions($<TARGET_PROPERTY:compiler,security_fortify> )
Mark Ruvald Pedersen01592072019-01-10 12:07:51 +0100102
103# @Intent: Set compiler flags to detect general stack overflows across all functions
104if(CONFIG_STACK_CANARIES)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200105 zephyr_compile_options($<TARGET_PROPERTY:compiler,security_canaries>)
Anas Nashif34aebad2018-01-03 12:26:19 -0500106endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200107
Anas Nashifdaf77162018-04-09 21:53:26 -0500108if(BUILD_VERSION)
109 zephyr_compile_definitions(
110 BUILD_VERSION=${BUILD_VERSION}
111 )
112endif()
113
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100114# @Intent: Obtain compiler optimizations flags and store in variables
115# @details:
116# Kconfig.zephyr "Optimization level" is a kconfig choice, ensuring
117# only *one* of CONFIG_{NO,DEBUG,SPEED,SIZE}_OPTIMIZATIONS is set.
118# Refer to Kconfig.zephyr for selection logic and description of these choices.
119# toolchain_cc_optimize_*() macros must provide the mapping from these kconfigs
120# to compiler flags. Each macro will store the flags in a CMake variable, whose
121# name is passed as argument (somewhat like by reference).
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200122#
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100123# If the user wants to tweak the optimizations, there are two ways:
124# 1) Using EXTRA_CFLAGS which is applied regardless of kconfig choice, or
125# 2) Rely on override support being implemented by your toolchain_cc_optimize_*()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200126#
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200127get_property(OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG TARGET compiler PROPERTY no_optimization)
128get_property(OPTIMIZE_FOR_DEBUG_FLAG TARGET compiler PROPERTY optimization_debug)
129get_property(OPTIMIZE_FOR_SPEED_FLAG TARGET compiler PROPERTY optimization_speed)
130get_property(OPTIMIZE_FOR_SIZE_FLAG TARGET compiler PROPERTY optimization_size)
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100131
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100132# From kconfig choice, pick the actual OPTIMIZATION_FLAG to use.
133# Kconfig choice ensures only one of these CONFIG_*_OPTIMIZATIONS is set.
Alberto Escolar Piedrasf60527a2018-01-22 15:35:54 +0100134if(CONFIG_NO_OPTIMIZATIONS)
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100135 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG})
136elseif(CONFIG_DEBUG_OPTIMIZATIONS)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200137 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_DEBUG_FLAG})
Aurelien Jarnoe8413d12018-06-16 23:40:04 +0200138elseif(CONFIG_SPEED_OPTIMIZATIONS)
139 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SPEED_FLAG})
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100140elseif(CONFIG_SIZE_OPTIMIZATIONS)
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100141 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SIZE_FLAG}) # Default in kconfig
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100142else()
Anas Nashif885aaf22019-01-18 19:15:19 -0500143 assert(0 "Unreachable code. Expected optimization level to have been chosen. See Kconfig.zephyr")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200144endif()
145
Ulf Magnussonde42aea2020-02-07 00:48:22 +0100146if(NOT CONFIG_ARCH_IS_SET)
147 message(WARNING "\
148None of the CONFIG_<arch> (e.g. CONFIG_X86) symbols are set. \
149Select one of them from the SOC_SERIES_* symbol or, lacking that, from the \
150SOC_* symbol.")
151endif()
152
Mark Ruvald Pedersen0b3c65f2019-01-30 10:12:30 +0100153# Apply the final optimization flag(s)
154zephyr_compile_options(${OPTIMIZATION_FLAG})
155
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100156# @Intent: Obtain compiler specific flags related to C++ that are not influenced by kconfig
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200157zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,required>>)
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200158
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100159# @Intent: Obtain compiler specific flags for compiling under different ISO standards of C++
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200160if(CONFIG_CPLUSPLUS)
161 # From kconfig choice, pick a single dialect.
162 # Kconfig choice ensures only one of these CONFIG_STD_CPP* is set.
163 if(CONFIG_STD_CPP98)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200164 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp98>)
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200165 elseif(CONFIG_STD_CPP11)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200166 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp11>) # Default in kconfig
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200167 elseif(CONFIG_STD_CPP14)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200168 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp14>)
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200169 elseif(CONFIG_STD_CPP17)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200170 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp17>)
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200171 elseif(CONFIG_STD_CPP2A)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200172 set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp2a>)
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200173 else()
174 assert(0 "Unreachable code. Expected C++ standard to have been chosen. See Kconfig.zephyr.")
175 endif()
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200176
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200177 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:${STD_CPP_DIALECT_FLAGS}>)
Ulf Magnusson9b6c2f42019-05-15 23:01:58 +0200178endif()
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100179
180if(NOT CONFIG_EXCEPTIONS)
181 # @Intent: Obtain compiler specific flags related to C++ Exceptions
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200182 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,no_exceptions>>)
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100183endif()
184
185if(NOT CONFIG_RTTI)
186 # @Intent: Obtain compiler specific flags related to C++ Run Time Type Information
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200187 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,no_rtti>>)
Mark Ruvald Pedersen63df4092019-02-18 23:54:30 +0100188endif()
189
Andy Rossfe04adf2019-02-27 11:53:18 -0800190if(CONFIG_MISRA_SANE)
Danny Oerndrup8e5a95e2019-05-16 12:53:58 +0200191 # @Intent: Obtain toolchain compiler flags relating to MISRA.
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200192 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_misra_sane>>)
193 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_error_misra_sane>>)
Andy Rossfe04adf2019-02-27 11:53:18 -0800194endif()
195
Flavio Ceolinb587e8d2020-08-26 09:48:33 -0700196# This is intend to be temporary. Once we have fixed the violations that
197# prevents build Zephyr, these flags shall be part of the default flags.
198if(CONFIG_CODING_GUIDELINE_CHECK)
199 # @Intent: Obtain toolchain compiler flags relating to coding guideline
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200200 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_coding_guideline>>)
Flavio Ceolinb587e8d2020-08-26 09:48:33 -0700201endif()
202
Danny Oerndrup4ddbc002019-06-11 13:55:53 +0200203# @Intent: Set compiler specific macro inclusion of AUTOCONF_H
Torsten Rasmussenf109e682020-08-13 15:49:46 +0200204zephyr_compile_options("SHELL: $<TARGET_PROPERTY:compiler,imacros> ${AUTOCONF_H}")
Danny Oerndrup4ddbc002019-06-11 13:55:53 +0200205
Danny Oerndrupfaa72b72019-06-11 15:56:57 +0200206# @Intent: Set compiler specific flag for bare metal freestanding option
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200207zephyr_compile_options($<TARGET_PROPERTY:compiler,freestanding>)
Danny Oerndrupfaa72b72019-06-11 15:56:57 +0200208
Danny Oerndrupe34ed7c2019-06-12 14:56:46 +0200209# @Intent: Set compiler specific flag for tentative definitions, no-common
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200210zephyr_compile_options($<TARGET_PROPERTY:compiler,no_common>)
Danny Oerndrupe34ed7c2019-06-12 14:56:46 +0200211
Danny Oerndrupe0569ac2019-07-23 09:00:55 +0200212# @Intent: Set compiler specific flag for production of debug information
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200213zephyr_compile_options($<TARGET_PROPERTY:compiler,debug>)
Danny Oerndrupe0569ac2019-07-23 09:00:55 +0200214
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200215zephyr_compile_options(
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530216 ${TOOLCHAIN_C_FLAGS}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200217)
218
Mark Ruvald Pedersencb0fd452019-01-30 21:48:25 +0100219# @Intent: Obtain compiler specific flags related to assembly
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200220# ToDo: Remember to get feedback from Oticon on this, as they might use the `ASM_BASE_FLAG` since this is done this way.
221zephyr_compile_options($<$<COMPILE_LANGUAGE:ASM>:$<TARGET_PROPERTY:asm,required>>)
Mark Ruvald Pedersencb0fd452019-01-30 21:48:25 +0100222
Nicolas Pitreb86aa652019-07-02 16:22:04 -0400223# @Intent: Enforce standard integer type correspondance to match Zephyr usage.
224# (must be after compiler specific flags)
Torsten Rasmussenf109e682020-08-13 15:49:46 +0200225zephyr_compile_options("SHELL: $<TARGET_PROPERTY:compiler,imacros> ${ZEPHYR_BASE}/include/toolchain/zephyr_stdint.h")
Nicolas Pitreb86aa652019-07-02 16:22:04 -0400226
Mark Ruvald Pedersencb0fd452019-01-30 21:48:25 +0100227# Common toolchain-agnostic assembly flags
228zephyr_compile_options(
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200229 $<$<COMPILE_LANGUAGE:ASM>:-D_ASMLANGUAGE>
230)
231
Mark Ruvald Pedersen1f013252019-04-25 15:46:11 +0200232# @Intent: Set fundamental linker specific flags
233toolchain_ld_base()
Aurelien Jarnoc6727d42018-11-26 13:48:34 +0100234
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +0200235toolchain_ld_force_undefined_symbols(
236 _OffsetAbsSyms
237 _ConfigAbsSyms
238)
239
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200240if(NOT CONFIG_NATIVE_APPLICATION)
Mark Ruvald Pedersen65f02c02019-04-25 16:31:30 +0200241 # @Intent: Set linker specific flags for bare metal target
242 toolchain_ld_baremetal()
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200243endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200244
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200245if(CONFIG_LIB_CPLUSPLUS)
Mark Ruvald Pedersen3db09aa2019-04-26 08:43:04 +0200246 # @Intent: Set linker specific flags for C++
247 toolchain_ld_cpp()
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200248endif()
249
Danny Oerndrup8eaa9062019-05-16 12:49:31 +0200250# @Intent: Add the basic toolchain warning flags
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200251zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_base>>)
252zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_base>>)
Danny Oerndrupbdb229f2019-05-06 15:19:27 +0200253
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200254# ==========================================================================
255#
256# cmake -DW=... settings
257#
258# W=1 - warnings that may be relevant and does not occur too often
259# W=2 - warnings that occur quite often but may still be relevant
260# W=3 - the more obscure warnings, can most likely be ignored
261# ==========================================================================
Danny Oerndrup8eaa9062019-05-16 12:49:31 +0200262# @Intent: Add cmake -DW toolchain supported warnings, if any
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200263if(W MATCHES "1")
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200264 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_dw_1>>)
265 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_dw_1>>)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200266endif()
267
268if(W MATCHES "2")
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200269 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_dw_2>>)
270 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_dw_2>>)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200271endif()
272
273if(W MATCHES "3")
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200274 zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_dw_3>>)
275 zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_dw_3>>)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200276endif()
277
Danny Oerndrup8eaa9062019-05-16 12:49:31 +0200278# @Intent: Add extended, more specific, toolchain warning flags
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200279zephyr_compile_options($<TARGET_PROPERTY:compiler,warning_extended>)
Benoit Leforestier04dad592019-01-25 13:57:03 +0100280
Danny Oerndrup025ffa22019-05-16 12:58:40 +0200281# @Intent: Trigger an error when a declaration does not specify a type
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200282zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_implicit_int>>)
283zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_error_implicit_int>>)
Danny Oerndrup025ffa22019-05-16 12:58:40 +0200284
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200285# Allow the user to inject options when calling cmake, e.g.
286# 'cmake -DEXTRA_CFLAGS="-Werror -Wno-deprecated-declarations" ..'
Sebastian Bøe9f590452017-11-10 12:22:23 +0100287include(cmake/extra_flags.cmake)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200288
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200289zephyr_cc_option(-fno-asynchronous-unwind-tables)
290zephyr_cc_option(-fno-pie)
291zephyr_cc_option(-fno-pic)
292zephyr_cc_option(-fno-strict-overflow)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200293
Daniel Leung02b20352020-09-28 11:27:11 -0700294if(CONFIG_THREAD_LOCAL_STORAGE)
295# Only support local exec TLS model at this point.
296zephyr_cc_option(-ftls-model=local-exec)
297endif()
298
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200299if(CONFIG_OVERRIDE_FRAME_POINTER_DEFAULT)
300 if(CONFIG_OMIT_FRAME_POINTER)
301 zephyr_cc_option(-fomit-frame-pointer)
302 else()
303 zephyr_cc_option(-fno-omit-frame-pointer)
304 endif()
305endif()
306
Sebastian Bøe244451b2019-02-27 08:28:25 +0100307separate_arguments(COMPILER_OPT_AS_LIST UNIX_COMMAND ${CONFIG_COMPILER_OPT})
308zephyr_compile_options(${COMPILER_OPT_AS_LIST})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200309
310# TODO: Include arch compiler options at this point.
311
Danny Oerndrupcbbbdea2019-05-06 15:21:58 +0200312if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
313 # GCC assumed
314 zephyr_cc_option(-fno-reorder-functions)
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530315
Anas Nashif7ee8bb92018-02-11 14:36:21 -0600316 if(NOT ${ZEPHYR_TOOLCHAIN_VARIANT} STREQUAL "xcc")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200317 zephyr_cc_option(-fno-defer-pop)
318 endif()
319endif()
320
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200321zephyr_cc_option_ifdef(CONFIG_STACK_USAGE -fstack-usage)
322
Marc Herbert28a56572019-04-11 16:34:04 -0700323# If the compiler supports it, strip the ${ZEPHYR_BASE} prefix from the
324# __FILE__ macro used in __ASSERT*, in the
325# .noinit."/home/joe/zephyr/fu/bar.c" section names and in any
326# application code. This saves some memory, stops leaking user locations
327# in binaries, makes failure logs more deterministic and most
328# importantly makes builds more deterministic
Marc Herbertf67dcdb2019-05-31 15:28:38 -0700329
Marc Herberteddbf3c2019-06-11 16:57:37 -0700330# If several match then the last one wins. This matters for instances
331# like tests/ and samples/: they're inside all of them! Then let's
332# strip as little as possible.
Marc Herbertf67dcdb2019-05-31 15:28:38 -0700333zephyr_cc_option(-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=CMAKE_SOURCE_DIR)
334zephyr_cc_option(-fmacro-prefix-map=${ZEPHYR_BASE}=ZEPHYR_BASE)
Marc Herberteddbf3c2019-06-11 16:57:37 -0700335if(WEST_TOPDIR)
336 zephyr_cc_option(-fmacro-prefix-map=${WEST_TOPDIR}=WEST_TOPDIR)
337endif()
Marc Herbert28a56572019-04-11 16:34:04 -0700338
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200339# TODO: Archiver arguments
340# ar_option(D)
341
Håkon Øye Amundsend6551b52018-11-29 09:08:08 +0000342# Declare MPU userspace dependencies before the linker scripts to make
343# sure the order of dependencies are met
Andrew Boie41f60112019-01-31 15:53:24 -0800344if(CONFIG_USERSPACE)
Daniel Leung212ec9a2019-03-10 14:20:21 -0700345 set(APP_SMEM_ALIGNED_DEP app_smem_aligned_linker)
346 set(APP_SMEM_UNALIGNED_DEP app_smem_unaligned_linker)
Håkon Øye Amundsend6551b52018-11-29 09:08:08 +0000347endif()
348
Håkon Øye Amundsena4494392018-11-29 09:14:27 +0000349get_property(TOPT GLOBAL PROPERTY TOPT)
Oleg Zhurakivskyy22119352019-03-08 11:29:33 +0200350set_ifndef( TOPT -Wl,-T) # clang doesn't pick -T for some reason and complains,
351 # while -Wl,-T works for both, gcc and clang
Håkon Øye Amundsena4494392018-11-29 09:14:27 +0000352
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200353if(CONFIG_HAVE_CUSTOM_LINKER_SCRIPT)
354 set(LINKER_SCRIPT ${APPLICATION_SOURCE_DIR}/${CONFIG_CUSTOM_LINKER_SCRIPT})
Sebastian Bøec1aa9d12018-04-12 14:48:05 +0200355 if(NOT EXISTS ${LINKER_SCRIPT})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200356 set(LINKER_SCRIPT ${CONFIG_CUSTOM_LINKER_SCRIPT})
Sebastian Bøec1aa9d12018-04-12 14:48:05 +0200357 assert_exists(CONFIG_CUSTOM_LINKER_SCRIPT)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200358 endif()
359else()
360 # Try a board specific linker file
361 set(LINKER_SCRIPT ${BOARD_DIR}/linker.ld)
362 if(NOT EXISTS ${LINKER_SCRIPT})
363 # If not available, try an SoC specific linker file
Anas Nashif96455d52018-09-04 14:34:06 -0500364 set(LINKER_SCRIPT ${SOC_DIR}/${ARCH}/${SOC_PATH}/linker.ld)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200365 endif()
366endif()
367
368if(NOT EXISTS ${LINKER_SCRIPT})
369 message(FATAL_ERROR "Could not find linker script: '${LINKER_SCRIPT}'. Corrupted configuration?")
370endif()
371
372configure_file(version.h.in ${PROJECT_BINARY_DIR}/include/generated/version.h)
373
Sebastian Bøec23cc262018-10-09 16:03:29 +0200374# Error-out when the deprecated naming convention is found (until
375# after 1.14.0 has been released)
376foreach(path
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100377 ${BOARD_DIR}/dts.fixup
378 ${PROJECT_SOURCE_DIR}/soc/${ARCH}/${SOC_PATH}/dts.fixup
Sebastian Bøec23cc262018-10-09 16:03:29 +0200379 ${APPLICATION_SOURCE_DIR}/dts.fixup
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100380 )
Sebastian Bøec23cc262018-10-09 16:03:29 +0200381 if(EXISTS ${path})
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100382 message(FATAL_ERROR
383 "A deprecated filename has been detected. Porting is required."
384 "The file '${path}' exists, but it should be named dts_fixup.h instead."
385 "See https://github.com/zephyrproject-rtos/zephyr/pull/10352 for more details"
386 )
Sebastian Bøec23cc262018-10-09 16:03:29 +0200387 endif()
388endforeach()
389
390set_ifndef( DTS_BOARD_FIXUP_FILE ${BOARD_DIR}/dts_fixup.h)
391set_ifndef( DTS_SOC_FIXUP_FILE ${SOC_DIR}/${ARCH}/${SOC_PATH}/dts_fixup.h)
392set( DTS_APP_FIXUP_FILE ${APPLICATION_SOURCE_DIR}/dts_fixup.h)
Sebastian Bøec23cc262018-10-09 16:03:29 +0200393
Ulf Magnusson4e850062020-01-16 13:29:53 +0100394set_ifndef(DTS_CAT_OF_FIXUP_FILES ${ZEPHYR_BINARY_DIR}/include/generated/devicetree_fixups.h)
Sebastian Bøe361fdaa2019-01-28 13:40:50 +0100395
396# Concatenate the fixups into a single header file for easy
Sebastian Bøec23cc262018-10-09 16:03:29 +0200397# #include'ing
Ulf Magnusson4e850062020-01-16 13:29:53 +0100398file(WRITE ${DTS_CAT_OF_FIXUP_FILES} "/* May only be included by devicetree.h */\n\n")
Martí Bolívar21c7d422020-05-08 16:06:48 -0700399set(DISCOVERED_FIXUP_FILES)
Sebastian Bøe361fdaa2019-01-28 13:40:50 +0100400foreach(fixup_file
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100401 ${DTS_BOARD_FIXUP_FILE}
402 ${DTS_SOC_FIXUP_FILE}
403 ${DTS_APP_FIXUP_FILE}
404 ${shield_dts_fixups}
405 )
Sebastian Bøe361fdaa2019-01-28 13:40:50 +0100406 if(EXISTS ${fixup_file})
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100407 file(READ ${fixup_file} contents)
408 file(APPEND ${DTS_CAT_OF_FIXUP_FILES} "${contents}")
Martí Bolívar21c7d422020-05-08 16:06:48 -0700409 string(APPEND DISCOVERED_FIXUP_FILES "- ${fixup_file}\n")
Sebastian Bøec23cc262018-10-09 16:03:29 +0200410 endif()
411endforeach()
412
Martí Bolívar21c7d422020-05-08 16:06:48 -0700413if (DISCOVERED_FIXUP_FILES)
414 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.")
415endif()
416
Sebastian Bøe6f946e22018-01-09 10:52:57 +0100417# Unfortunately, the order in which CMakeLists.txt code is processed
418# matters so we need to be careful about how we order the processing
419# of subdirectories. One example is "Compiler flags added late in the
420# build are not exported to external build systems #5605"; when we
421# integrate with an external build system we read out all compiler
422# flags when the external project is created. So an external project
423# defined in subsys or ext will not get global flags added by drivers/
424# or tests/ as the subdirectories are ordered now.
425#
426# Another example of when the order matters is the reading and writing
427# of global properties such as ZEPHYR_LIBS or
428# GENERATED_KERNEL_OBJECT_FILES.
429#
430# Arch is placed early because it defines important compiler flags
431# that must be exported to external build systems defined in
432# e.g. subsys/.
433add_subdirectory(arch)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200434add_subdirectory(lib)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200435# We use include instead of add_subdirectory to avoid creating a new directory scope.
436# This is because source file properties are directory scoped, including the GENERATED
437# property which is set implicitly for custom command outputs
438include(misc/generated/CMakeLists.txt)
Anas Nashif3d1252f2018-09-03 15:20:14 -0500439
Erwan Gouriouba31cb52018-09-13 16:25:53 +0200440if(EXISTS ${SOC_DIR}/${ARCH}/CMakeLists.txt)
Anas Nashif96455d52018-09-04 14:34:06 -0500441 add_subdirectory(${SOC_DIR}/${ARCH} soc/${ARCH})
Anas Nashif3d1252f2018-09-03 15:20:14 -0500442else()
Anas Nashif96455d52018-09-04 14:34:06 -0500443 add_subdirectory(${SOC_DIR}/${ARCH}/${SOC_PATH} soc/${ARCH}/${SOC_PATH})
Anas Nashif3d1252f2018-09-03 15:20:14 -0500444endif()
445
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200446add_subdirectory(boards)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200447add_subdirectory(subsys)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200448add_subdirectory(drivers)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200449
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100450# Include zephyr modules generated CMake file.
Torsten Rasmussen2fc062b2020-08-24 11:01:04 +0200451foreach(module_name ${ZEPHYR_MODULE_NAMES})
452 # Note the second, binary_dir parameter requires the added
453 # subdirectory to have its own, local cmake target(s). If not then
454 # this binary_dir is created but stays empty. Object files land in
455 # the main binary dir instead.
456 # https://cmake.org/pipermail/cmake/2019-June/069547.html
457 string(TOUPPER ${module_name} MODULE_NAME_UPPER)
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200458 if(NOT ${ZEPHYR_${MODULE_NAME_UPPER}_CMAKE_DIR} STREQUAL "")
459 set(ZEPHYR_CURRENT_MODULE_DIR ${ZEPHYR_${MODULE_NAME_UPPER}_MODULE_DIR})
460 set(ZEPHYR_CURRENT_CMAKE_DIR ${ZEPHYR_${MODULE_NAME_UPPER}_CMAKE_DIR})
461 add_subdirectory(${ZEPHYR_CURRENT_CMAKE_DIR} ${CMAKE_BINARY_DIR}/modules/${module_name})
462 endif()
Torsten Rasmussen2fc062b2020-08-24 11:01:04 +0200463endforeach()
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200464# Done processing modules, clear ZEPHYR_CURRENT_MODULE_DIR and ZEPHYR_CURRENT_CMAKE_DIR.
Torsten Rasmussen2fc062b2020-08-24 11:01:04 +0200465set(ZEPHYR_CURRENT_MODULE_DIR)
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200466set(ZEPHYR_CURRENT_CMAKE_DIR)
Torsten Rasmussen7e9d1bd2019-02-05 10:36:22 +0100467
Andrew Boie59601192020-05-29 13:24:51 -0700468set(syscall_list_h ${CMAKE_CURRENT_BINARY_DIR}/include/generated/syscall_list.h)
469set(syscalls_json ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls.json)
470set(struct_tags_json ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/struct_tags.json)
Sebastian Bøe13a68402017-11-20 13:03:55 +0100471
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200472# The syscalls subdirs txt file is constructed by python containing a list of folders to use for
473# dependency handling, including empty folders.
474# Windows: The list is used to specify DIRECTORY list with CMAKE_CONFIGURE_DEPENDS attribute.
475# Other OS: The list will update whenever a file is added/removed/modified and ensure a re-build.
476set(syscalls_subdirs_txt ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_subdirs.txt)
477
478# As syscalls_subdirs_txt is updated whenever a file is modified, this file can not be used for
479# monitoring of added / removed folders. A trigger file is thus used for correct dependency
480# handling. The trigger file will update when a folder is added / removed.
481set(syscalls_subdirs_trigger ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_subdirs.trigger)
482
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200483if(NOT (${CMAKE_HOST_SYSTEM_NAME} STREQUAL Windows))
484 set(syscalls_links --create-links ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_links)
485endif()
486
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200487# When running CMake it must be ensured that all dependencies are correctly acquired.
488execute_process(
489 COMMAND
490 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200491 ${ZEPHYR_BASE}/scripts/subfolder_list.py
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200492 --directory ${ZEPHYR_BASE}/include # Walk this directory
493 --out-file ${syscalls_subdirs_txt} # Write file with discovered folder
494 --trigger ${syscalls_subdirs_trigger} # Trigger file that is used for json generation
495 ${syscalls_links} # If defined, create symlinks for dependencies
496)
Carles Cufi3ad1f272019-07-18 10:38:25 +0200497file(STRINGS ${syscalls_subdirs_txt} PARSE_SYSCALLS_PATHS_DEPENDS ENCODING UTF-8)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200498
499if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL Windows)
500 # On windows only adding/removing files or folders will be reflected in depends.
501 # Hence adding a file requires CMake to re-run to add this file to the file list.
502 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${PARSE_SYSCALLS_PATHS_DEPENDS})
503
504 # Also On Windows each header file must be monitored as file modifications are not reflected
505 # on directory level.
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200506 file(GLOB_RECURSE PARSE_SYSCALLS_HEADER_DEPENDS ${ZEPHYR_BASE}/include/*.h)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200507else()
508 # The syscall parsing depends on the folders in order to detect add/removed/modified files.
509 # When a folder is removed, CMake will try to find a target that creates that dependency.
510 # This command sets up the target for CMake to find.
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200511 # Without this code, CMake will fail with the following error:
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200512 # <folder> needed by '<target>', missing and no known rule to make it
513 # when a folder is removed.
514 add_custom_command(OUTPUT ${PARSE_SYSCALLS_PATHS_DEPENDS}
515 COMMAND ${CMAKE_COMMAND} -E echo ""
516 COMMENT "Preparing syscall dependency handling"
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200517 )
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200518
519 add_custom_command(
520 OUTPUT
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200521 ${syscalls_subdirs_trigger}
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200522 COMMAND
523 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200524 ${ZEPHYR_BASE}/scripts/subfolder_list.py
525 --directory ${ZEPHYR_BASE}/include # Walk this directory
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200526 --out-file ${syscalls_subdirs_txt} # Write file with discovered folder
527 --trigger ${syscalls_subdirs_trigger} # Trigger file that is used for json generation
528 ${syscalls_links} # If defined, create symlinks for dependencies
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200529 DEPENDS ${PARSE_SYSCALLS_PATHS_DEPENDS}
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200530 )
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200531
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200532 # Ensure subdir file always exists when specifying CMake dependency.
533 if(NOT EXISTS ${syscalls_subdirs_txt})
534 file(WRITE ${syscalls_subdirs_txt} "")
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200535 endif()
536
537 # On other OS'es, modifying a file is reflected on the folder timestamp and hence detected
538 # when using depend on directory level.
539 # Thus CMake only needs to re-run when sub-directories are added / removed, which is indicated
540 # using a trigger file.
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200541 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${syscalls_subdirs_txt})
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200542endif()
543
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200544# syscall declarations are searched for in the SYSCALL_INCLUDE_DIRS
Adithya Baglodye67720b2018-07-02 14:59:19 +0530545if(CONFIG_APPLICATION_DEFINED_SYSCALL)
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200546 list(APPEND SYSCALL_INCLUDE_DIRS ${APPLICATION_SOURCE_DIR})
Adithya Baglodye67720b2018-07-02 14:59:19 +0530547endif()
548
Andrew Boiec1863872019-11-21 23:11:29 -0800549if(CONFIG_ZTEST)
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200550 list(APPEND SYSCALL_INCLUDE_DIRS ${ZEPHYR_BASE}/subsys/testsuite/ztest/include)
Andrew Boiec1863872019-11-21 23:11:29 -0800551endif()
552
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200553foreach(d ${SYSCALL_INCLUDE_DIRS})
554 list(APPEND parse_syscalls_include_args
555 --include ${d}
556 )
557endforeach()
558
Sebastian Bøe13a68402017-11-20 13:03:55 +0100559add_custom_command(
560 OUTPUT
561 ${syscalls_json}
Andrew Boie59601192020-05-29 13:24:51 -0700562 ${struct_tags_json}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100563 COMMAND
564 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200565 ${ZEPHYR_BASE}/scripts/parse_syscalls.py
Adithya Baglodye67720b2018-07-02 14:59:19 +0530566 --include ${ZEPHYR_BASE}/include # Read files from this dir
Andrew Boiefed960b2020-05-29 13:33:12 -0700567 --include ${ZEPHYR_BASE}/drivers # For net sockets
568 --include ${ZEPHYR_BASE}/subsys/net # More net sockets
Sebastian Bøe56f6e352018-04-30 15:59:16 +0200569 ${parse_syscalls_include_args} # Read files from these dirs also
Corey Whartonccd15df2020-02-29 14:51:42 -0800570 --json-file ${syscalls_json} # Write this file
Andrew Boie59601192020-05-29 13:24:51 -0700571 --tag-struct-file ${struct_tags_json} # Write subsystem list to this file
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200572 DEPENDS ${syscalls_subdirs_trigger} ${PARSE_SYSCALLS_HEADER_DEPENDS}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100573 )
574
Joakim Andersson08d2f482020-08-04 18:26:43 +0200575add_custom_target(${SYSCALL_LIST_H_TARGET} DEPENDS ${syscall_list_h})
Andrew Boiec1c54b12020-03-16 12:48:00 -0700576add_custom_target(${PARSE_SYSCALLS_TARGET}
Joakim Andersson08d2f482020-08-04 18:26:43 +0200577 DEPENDS
578 ${syscalls_json}
Joakim Anderssond268f822020-08-04 18:31:48 +0200579 ${struct_tags_json}
Joakim Andersson08d2f482020-08-04 18:26:43 +0200580 )
Andrew Boie9ff64bb2019-11-05 09:39:05 -0800581
582# 64-bit systems do not require special handling of 64-bit system call
583# parameters or return values, indicate this to the system call boilerplate
584# generation script.
585if(CONFIG_64BIT)
586 set(SYSCALL_LONG_REGISTERS_ARG --long-registers)
587endif()
588
Andy Rosscfeb07e2020-03-05 21:14:02 -0800589if(CONFIG_TIMEOUT_64BIT)
590 set(SYSCALL_SPLIT_TIMEOUT_ARG --split-type k_timeout_t)
591endif()
592
Sebastian Bøe13a68402017-11-20 13:03:55 +0100593add_custom_command(OUTPUT include/generated/syscall_dispatch.c ${syscall_list_h}
594 # Also, some files are written to include/generated/syscalls/
595 COMMAND
596 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200597 ${ZEPHYR_BASE}/scripts/gen_syscalls.py
Sebastian Bøe13a68402017-11-20 13:03:55 +0100598 --json-file ${syscalls_json} # Read this file
599 --base-output include/generated/syscalls # Write to this dir
600 --syscall-dispatch include/generated/syscall_dispatch.c # Write this file
Andrew Boie353acf42018-07-23 18:10:15 -0700601 --syscall-list ${syscall_list_h}
Andrew Boie9ff64bb2019-11-05 09:39:05 -0800602 ${SYSCALL_LONG_REGISTERS_ARG}
Andy Rosscfeb07e2020-03-05 21:14:02 -0800603 ${SYSCALL_SPLIT_TIMEOUT_ARG}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100604 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
Andrew Boiec1c54b12020-03-16 12:48:00 -0700605 DEPENDS ${PARSE_SYSCALLS_TARGET}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100606 )
607
Corey Whartonccd15df2020-02-29 14:51:42 -0800608# This is passed into all calls to the gen_kobject_list.py script.
Andrew Boie59601192020-05-29 13:24:51 -0700609set(gen_kobject_list_include_args --include ${struct_tags_json})
Corey Whartonccd15df2020-02-29 14:51:42 -0800610
Leandro Pereirac2003672018-04-04 13:50:32 -0700611set(DRV_VALIDATION ${PROJECT_BINARY_DIR}/include/generated/driver-validation.h)
612add_custom_command(
613 OUTPUT ${DRV_VALIDATION}
614 COMMAND
615 ${PYTHON_EXECUTABLE}
616 ${ZEPHYR_BASE}/scripts/gen_kobject_list.py
617 --validation-output ${DRV_VALIDATION}
Corey Whartonccd15df2020-02-29 14:51:42 -0800618 ${gen_kobject_list_include_args}
Leandro Pereirac2003672018-04-04 13:50:32 -0700619 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Corey Whartonccd15df2020-02-29 14:51:42 -0800620 DEPENDS
621 ${ZEPHYR_BASE}/scripts/gen_kobject_list.py
Andrew Boiec1c54b12020-03-16 12:48:00 -0700622 ${PARSE_SYSCALLS_TARGET}
Leandro Pereirac2003672018-04-04 13:50:32 -0700623 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
624 )
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100625add_custom_target(${DRIVER_VALIDATION_H_TARGET} DEPENDS ${DRV_VALIDATION})
Leandro Pereirac2003672018-04-04 13:50:32 -0700626
Torsten Rasmussend7862cf2020-02-12 15:42:09 +0100627include(${ZEPHYR_BASE}/cmake/kobj.cmake)
Leandro Pereira39dc7d02018-04-05 13:59:33 -0700628gen_kobj(KOBJ_INCLUDE_PATH)
Leandro Pereirac2003672018-04-04 13:50:32 -0700629
Sebastian Bøefdac7b32020-01-23 15:39:17 +0100630# Add a pseudo-target that is up-to-date when all generated headers
631# are up-to-date.
632
633add_custom_target(zephyr_generated_headers)
634add_dependencies(zephyr_generated_headers
635 offsets_h
636 )
637
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200638# Generate offsets.c.obj from offsets.c
639# Generate offsets.h from offsets.c.obj
640
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100641set(OFFSETS_LIB offsets)
642
Klaus Petersenc66cb762018-11-15 10:37:46 +0100643set(OFFSETS_C_PATH ${ARCH_DIR}/${ARCH}/core/offsets/offsets.c)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200644set(OFFSETS_H_PATH ${PROJECT_BINARY_DIR}/include/generated/offsets.h)
645
Klaus Petersen62e55e52019-02-04 12:10:57 +0100646add_library( ${OFFSETS_LIB} OBJECT ${OFFSETS_C_PATH})
Stephanos Ioannidis2d746042019-10-25 00:08:21 +0900647target_include_directories(${OFFSETS_LIB} PRIVATE
648 kernel/include
649 ${ARCH_DIR}/${ARCH}/include
650 )
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100651target_link_libraries(${OFFSETS_LIB} zephyr_interface)
Joakim Anderssond268f822020-08-04 18:31:48 +0200652add_dependencies(zephyr_interface
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100653 ${SYSCALL_LIST_H_TARGET}
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100654 ${DRIVER_VALIDATION_H_TARGET}
655 ${KOBJ_TYPES_H_TARGET}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100656 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200657
658add_custom_command(
659 OUTPUT ${OFFSETS_H_PATH}
Carles Cufi7d764b32018-01-11 15:46:44 +0100660 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/gen_offset_header.py
Klaus Petersen62e55e52019-02-04 12:10:57 +0100661 -i $<TARGET_OBJECTS:${OFFSETS_LIB}>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200662 -o ${OFFSETS_H_PATH}
Sebastian Bøe5962aab2019-08-15 14:45:59 +0200663 DEPENDS
664 ${OFFSETS_LIB}
665 $<TARGET_OBJECTS:${OFFSETS_LIB}>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200666)
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100667add_custom_target(${OFFSETS_H_TARGET} DEPENDS ${OFFSETS_H_PATH})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200668
669zephyr_include_directories(${TOOLCHAIN_INCLUDES})
670
Sebastian Bøe89516fb2017-12-01 15:25:06 +0100671zephyr_get_include_directories_for_lang(C ZEPHYR_INCLUDES)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200672
673add_subdirectory(kernel)
674
675# Read list content
676get_property(ZEPHYR_LIBS_PROPERTY GLOBAL PROPERTY ZEPHYR_LIBS)
677
678foreach(zephyr_lib ${ZEPHYR_LIBS_PROPERTY})
679 # TODO: Could this become an INTERFACE property of zephyr_interface?
Sebastian Bøefdac7b32020-01-23 15:39:17 +0100680 add_dependencies(${zephyr_lib} zephyr_generated_headers)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200681endforeach()
682
683get_property(OUTPUT_FORMAT GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT)
684
Adithya Baglody62e152a2018-11-13 15:34:02 +0530685if (CONFIG_CODE_DATA_RELOCATION)
686 set(CODE_RELOCATION_DEP code_relocation_source_lib)
687endif() # CONFIG_CODE_DATA_RELOCATION
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100688
Mark Ruvald Pedersen4c811972019-04-29 17:16:54 +0200689configure_linker_script(
Mark Ruvald Pedersen1073fba2019-04-29 20:27:23 +0200690 linker.cmd
Mark Ruvald Pedersenfbcea172019-04-29 20:35:12 +0200691 ""
Daniel Leung212ec9a2019-03-10 14:20:21 -0700692 ${APP_SMEM_ALIGNED_DEP}
Sebastian Bøe2a963122019-02-08 15:49:57 +0100693 ${CODE_RELOCATION_DEP}
Sebastian Bøefdac7b32020-01-23 15:39:17 +0100694 zephyr_generated_headers
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +0100695 )
Andy Grosse8860fe2018-02-01 01:12:32 -0600696
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200697add_custom_target(
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100698 ${LINKER_SCRIPT_TARGET}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200699 DEPENDS
700 linker.cmd
Sebastian Bøeb1ab5012017-12-14 13:03:23 +0100701 )
702
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100703# Give the '${LINKER_SCRIPT_TARGET}' target all of the include directories so
Sebastian Bøeb1ab5012017-12-14 13:03:23 +0100704# that cmake can successfully find the linker_script's header
705# dependencies.
706zephyr_get_include_directories_for_lang(C
707 ZEPHYR_INCLUDE_DIRS
708 STRIP_PREFIX # Don't use a -I prefix
709 )
710set_property(TARGET
Sebastian Bøe1b86fb92019-01-14 16:39:33 +0100711 ${LINKER_SCRIPT_TARGET}
Sebastian Bøeb1ab5012017-12-14 13:03:23 +0100712 PROPERTY INCLUDE_DIRECTORIES
713 ${ZEPHYR_INCLUDE_DIRS}
714 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200715
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200716if(CONFIG_GEN_ISR_TABLES)
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530717 if(CONFIG_GEN_SW_ISR_TABLE)
718 list(APPEND GEN_ISR_TABLE_EXTRA_ARG --sw-isr-table)
719 endif()
720
721 if(CONFIG_GEN_IRQ_VECTOR_TABLE)
722 list(APPEND GEN_ISR_TABLE_EXTRA_ARG --vector-table)
723 endif()
724
Sebastian Bøe15260702019-01-14 16:31:02 +0100725 # isr_tables.c is generated from ${ZEPHYR_PREBUILT_EXECUTABLE} by
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200726 # gen_isr_tables.py
727 add_custom_command(
728 OUTPUT isr_tables.c
Torsten Rasmussenc060b072020-08-18 14:46:06 +0200729 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
730 $<TARGET_PROPERTY:bintools,elfconvert_flag>
731 $<TARGET_PROPERTY:bintools,elfconvert_flag_intarget>${OUTPUT_FORMAT}
732 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>binary
733 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_only>.intList
734 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>$<TARGET_FILE:${ZEPHYR_PREBUILT_EXECUTABLE}>
735 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>isrList.bin
Torsten Rasmussenf160dee2020-09-04 10:05:00 +0200736 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200737 COMMAND ${PYTHON_EXECUTABLE}
Carles Cufi7d764b32018-01-11 15:46:44 +0100738 ${ZEPHYR_BASE}/arch/common/gen_isr_tables.py
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200739 --output-source isr_tables.c
Sebastian Bøe15260702019-01-14 16:31:02 +0100740 --kernel $<TARGET_FILE:${ZEPHYR_PREBUILT_EXECUTABLE}>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200741 --intlist isrList.bin
Yasushi SHOJI6fc0d772018-10-09 18:59:16 +0900742 $<$<BOOL:${CONFIG_BIG_ENDIAN}>:--big-endian>
Sebastian Bøea55279a2018-01-04 14:08:39 +0100743 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--debug>
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530744 ${GEN_ISR_TABLE_EXTRA_ARG}
Sebastian Bøe15260702019-01-14 16:31:02 +0100745 DEPENDS ${ZEPHYR_PREBUILT_EXECUTABLE}
Torsten Rasmussenc060b072020-08-18 14:46:06 +0200746 COMMAND_EXPAND_LISTS
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200747 )
748 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_SOURCE_FILES isr_tables.c)
749endif()
750
Adithya Baglody62e152a2018-11-13 15:34:02 +0530751if(CONFIG_CODE_DATA_RELOCATION)
Mark Ruvald Pedersen86a3e8f2019-05-03 10:33:03 +0200752 # @Intent: Linker script to relocate .text, data and .bss sections
753 toolchain_ld_relocation()
Adithya Baglody62e152a2018-11-13 15:34:02 +0530754endif()
755
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530756if(CONFIG_USERSPACE)
Torsten Rasmussene37d9e62020-11-20 18:39:30 +0100757 zephyr_get_compile_options_for_lang_as_string(C compiler_flags_priv)
Torsten Rasmussene0758c32020-08-21 19:13:53 +0200758 string(REPLACE "$<TARGET_PROPERTY:compiler,coverage>" ""
759 NO_COVERAGE_FLAGS "${compiler_flags_priv}"
760 )
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530761
762 get_property(include_dir_in_interface TARGET zephyr_interface
763 PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
764
765 get_property(sys_include_dir_in_interface TARGET zephyr_interface
766 PROPERTY INTERFACE_SYSTEM_INCLUDE_DIRECTORIES)
767
768 get_property(compile_definitions_interface TARGET zephyr_interface
769 PROPERTY INTERFACE_COMPILE_DEFINITIONS)
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530770
Carles Cufi7d764b32018-01-11 15:46:44 +0100771 set(GEN_KOBJ_LIST ${ZEPHYR_BASE}/scripts/gen_kobject_list.py)
772 set(PROCESS_GPERF ${ZEPHYR_BASE}/scripts/process_gperf.py)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200773
774 set(OBJ_LIST kobject_hash.gperf)
775 set(OUTPUT_SRC_PRE kobject_hash_preprocessed.c)
776 set(OUTPUT_SRC kobject_hash.c)
777 set(OUTPUT_OBJ kobject_hash.c.obj)
778 set(OUTPUT_OBJ_RENAMED kobject_hash_renamed.o)
779
780 # Essentially what we are doing here is extracting some information
781 # out of the nearly finished elf file, generating the source code
782 # for a hash table based on that information, and then compiling and
783 # linking the hash table back into a now even more nearly finished
Marc Herbert4a10eea2019-04-16 15:39:45 -0700784 # elf file. More information in gen_kobject_list.py --help.
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200785
786 # Use the script GEN_KOBJ_LIST to scan the kernel binary's
Sebastian Bøe15260702019-01-14 16:31:02 +0100787 # (${ZEPHYR_PREBUILT_EXECUTABLE}) DWARF information to produce a table of kernel
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200788 # objects (OBJ_LIST) which we will then pass to gperf
789 add_custom_command(
790 OUTPUT ${OBJ_LIST}
791 COMMAND
792 ${PYTHON_EXECUTABLE}
793 ${GEN_KOBJ_LIST}
Sebastian Bøe15260702019-01-14 16:31:02 +0100794 --kernel $<TARGET_FILE:${ZEPHYR_PREBUILT_EXECUTABLE}>
Leandro Pereirac2003672018-04-04 13:50:32 -0700795 --gperf-output ${OBJ_LIST}
Corey Whartonccd15df2020-02-29 14:51:42 -0800796 ${gen_kobject_list_include_args}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200797 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Corey Whartonccd15df2020-02-29 14:51:42 -0800798 DEPENDS
799 ${ZEPHYR_PREBUILT_EXECUTABLE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200800 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
801 )
802 add_custom_target(obj_list DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OBJ_LIST})
803
804 # Use gperf to generate C code (OUTPUT_SRC_PRE) which implements a
805 # perfect hashtable based on OBJ_LIST
806 add_custom_command(
807 OUTPUT ${OUTPUT_SRC_PRE}
808 COMMAND
809 ${GPERF}
810 --output-file ${OUTPUT_SRC_PRE}
811 ${OBJ_LIST}
Sebastian Bøef5758b52018-01-31 10:42:46 +0100812 DEPENDS obj_list ${OBJ_LIST}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200813 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
814 )
815 add_custom_target(output_src_pre DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_SRC_PRE})
816
817 # For our purposes the code/data generated by gperf is not optimal.
818 #
819 # The script PROCESS_GPERF creates a new c file OUTPUT_SRC based on
820 # OUTPUT_SRC_PRE to greatly reduce the amount of code/data generated
821 # since we know we are always working with pointer values
822 add_custom_command(
823 OUTPUT ${OUTPUT_SRC}
824 COMMAND
Sebastian Bøe1b600702018-06-21 14:34:42 +0200825 ${PYTHON_EXECUTABLE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200826 ${PROCESS_GPERF}
827 -i ${OUTPUT_SRC_PRE}
828 -o ${OUTPUT_SRC}
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700829 -p "struct z_object"
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200830 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Sebastian Bøef5758b52018-01-31 10:42:46 +0100831 DEPENDS output_src_pre ${OUTPUT_SRC_PRE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200832 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
833 )
834 add_custom_target(output_src DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_SRC})
835
836 # We need precise control of where generated text/data ends up in the final
837 # kernel image. Disable function/data sections and use objcopy to move
838 # generated data into special section names
839 add_library(output_lib STATIC
840 ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_SRC}
841 )
842
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530843 set_source_files_properties(${OUTPUT_SRC} PROPERTIES COMPILE_FLAGS
Andrew Boiea5148982019-03-14 17:04:11 -0700844 "${NO_COVERAGE_FLAGS} -fno-function-sections -fno-data-sections")
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530845
846 set_source_files_properties(${OUTPUT_SRC}
847 PROPERTIES COMPILE_DEFINITIONS "${compile_definitions_interface}")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200848
849 # Turn off -ffunction-sections, etc.
850 # NB: Using a library instead of target_compile_options(output_lib
851 # [...]) because a library's options have precedence
852 add_library(output_lib_interface INTERFACE)
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530853
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200854 target_link_libraries(output_lib output_lib_interface)
855
Kumar Gala3713ea42019-03-14 11:50:08 -0500856 foreach(incl ${include_dir_in_interface})
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530857 target_include_directories(output_lib_interface INTERFACE ${incl})
858 endforeach()
859
Kumar Gala3713ea42019-03-14 11:50:08 -0500860 foreach(incl ${sys_include_dir_in_interface})
861 target_include_directories(output_lib_interface SYSTEM INTERFACE ${incl})
862 endforeach()
Adithya Baglody4b3c7b32018-11-21 14:31:56 +0530863
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200864 set(OUTPUT_OBJ_PATH ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/output_lib.dir/${OUTPUT_OBJ})
865
866 add_custom_command(
867 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_OBJ_RENAMED}
Torsten Rasmussenc060b072020-08-18 14:46:06 +0200868 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
869 $<TARGET_PROPERTY:bintools,elfconvert_flag>
870 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.data=.kobject_data.data
871 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.text=.kobject_data.text
872 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_rename>.rodata=.kobject_data.rodata
873 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${OUTPUT_OBJ_PATH}
874 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${OUTPUT_OBJ_RENAMED}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +0200875 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200876 DEPENDS output_lib
877 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
Torsten Rasmussenc060b072020-08-18 14:46:06 +0200878 COMMAND_EXPAND_LISTS
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200879 )
880 add_custom_target(output_obj_renamed DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_OBJ_RENAMED})
881
882 add_library(output_obj_renamed_lib STATIC IMPORTED GLOBAL)
883 set_property(
884 TARGET output_obj_renamed_lib
885 PROPERTY
886 IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_OBJ_RENAMED}
887 )
888 add_dependencies(
889 output_obj_renamed_lib
890 output_obj_renamed
891 )
892
893 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_OBJECT_FILES output_obj_renamed_lib)
894endif()
895
896# Read global variables into local variables
897get_property(GKOF GLOBAL PROPERTY GENERATED_KERNEL_OBJECT_FILES)
898get_property(GKSF GLOBAL PROPERTY GENERATED_KERNEL_SOURCE_FILES)
899
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200900
Alberto Escolar Piedrasc2882412018-07-05 10:51:03 +0200901get_property(CSTD GLOBAL PROPERTY CSTD)
902set_ifndef(CSTD c99)
903
Danny Oerndrup6331dae2019-06-13 15:33:03 +0200904# @Intent: Obtain compiler specific flag for specifying the c standard
Alberto Escolar Piedrasc2882412018-07-05 10:51:03 +0200905zephyr_compile_options(
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200906 $<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,cstd>${CSTD}>
Alberto Escolar Piedrasc2882412018-07-05 10:51:03 +0200907)
908
Mark Ruvald Pedersen197197a2019-05-03 11:02:56 +0200909# @Intent: Configure linker scripts, i.e. generate linker scripts with variables substituted
910toolchain_ld_configure_files()
Daniel Leung212ec9a2019-03-10 14:20:21 -0700911
Andrew Boie4ce652e2019-02-22 16:08:44 -0800912if(CONFIG_USERSPACE)
Daniel Leung212ec9a2019-03-10 14:20:21 -0700913 set(APP_SMEM_ALIGNED_LD "${PROJECT_BINARY_DIR}/include/generated/app_smem_aligned.ld")
914 set(APP_SMEM_UNALIGNED_LD "${PROJECT_BINARY_DIR}/include/generated/app_smem_unaligned.ld")
Adithya Baglody50950eb2018-12-20 15:47:42 +0530915 set(OBJ_FILE_DIR "${PROJECT_BINARY_DIR}/../")
916
917 add_custom_target(
Daniel Leung212ec9a2019-03-10 14:20:21 -0700918 ${APP_SMEM_ALIGNED_DEP}
Sebastian Bøe2a963122019-02-08 15:49:57 +0100919 DEPENDS
Daniel Leung212ec9a2019-03-10 14:20:21 -0700920 ${APP_SMEM_ALIGNED_LD}
921 )
922
923 add_custom_target(
924 ${APP_SMEM_UNALIGNED_DEP}
925 DEPENDS
926 ${APP_SMEM_UNALIGNED_LD}
Adithya Baglody50950eb2018-12-20 15:47:42 +0530927 )
928
Andrew Boie4b4f7732019-02-01 12:18:31 -0800929 if(CONFIG_NEWLIB_LIBC)
Andrew Boie17ce8222019-02-21 13:44:54 -0800930 set(NEWLIB_PART -l libc.a z_libc_partition)
Andrew Boie4b4f7732019-02-01 12:18:31 -0800931 endif()
Ioannis Glaropoulosd58f8be2019-11-15 14:07:51 +0100932 if(CONFIG_NEWLIB_LIBC_NANO)
933 set(NEWLIB_PART -l libc_nano.a z_libc_partition)
934 endif()
Andrew Boiee686aef2019-02-27 14:41:45 -0800935
Adithya Baglody50950eb2018-12-20 15:47:42 +0530936 add_custom_command(
Daniel Leung212ec9a2019-03-10 14:20:21 -0700937 OUTPUT ${APP_SMEM_UNALIGNED_LD}
Adithya Baglody50950eb2018-12-20 15:47:42 +0530938 COMMAND ${PYTHON_EXECUTABLE}
939 ${ZEPHYR_BASE}/scripts/gen_app_partitions.py
940 -d ${OBJ_FILE_DIR}
Daniel Leung212ec9a2019-03-10 14:20:21 -0700941 -o ${APP_SMEM_UNALIGNED_LD}
Torsten Rasmussen5a703c82019-11-05 10:24:08 +0100942 ${NEWLIB_PART}
Torsten Rasmussen1f9723a2019-11-04 14:30:24 +0100943 $<TARGET_PROPERTY:zephyr_property_target,COMPILE_OPTIONS>
Adithya Baglody50950eb2018-12-20 15:47:42 +0530944 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Sebastian Bøe2a963122019-02-08 15:49:57 +0100945 DEPENDS
946 kernel
947 ${ZEPHYR_LIBS_PROPERTY}
Adithya Baglody50950eb2018-12-20 15:47:42 +0530948 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
Torsten Rasmussen1f9723a2019-11-04 14:30:24 +0100949 COMMAND_EXPAND_LISTS
Daniel Leung212ec9a2019-03-10 14:20:21 -0700950 COMMENT "Generating app_smem_unaligned linker section"
951 )
952
Mark Ruvald Pedersen4c811972019-04-29 17:16:54 +0200953 configure_linker_script(
Mark Ruvald Pedersen1073fba2019-04-29 20:27:23 +0200954 linker_app_smem_unaligned.cmd
Mark Ruvald Pedersenfbcea172019-04-29 20:35:12 +0200955 "-DLINKER_APP_SMEM_UNALIGNED"
Daniel Leung212ec9a2019-03-10 14:20:21 -0700956 ${CODE_RELOCATION_DEP}
957 ${APP_SMEM_UNALIGNED_DEP}
958 ${APP_SMEM_UNALIGNED_LD}
Sebastian Bøefdac7b32020-01-23 15:39:17 +0100959 zephyr_generated_headers
Daniel Leung212ec9a2019-03-10 14:20:21 -0700960 )
Daniel Leung212ec9a2019-03-10 14:20:21 -0700961
962 add_custom_target(
963 linker_app_smem_unaligned_script
964 DEPENDS
965 linker_app_smem_unaligned.cmd
966 )
967
968 set_property(TARGET
969 linker_app_smem_unaligned_script
970 PROPERTY INCLUDE_DIRECTORIES
971 ${ZEPHYR_INCLUDE_DIRS}
972 )
973
974 set(APP_SMEM_UNALIGNED_LIB app_smem_unaligned_output_obj_renamed_lib)
975 add_executable( app_smem_unaligned_prebuilt misc/empty_file.c)
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +0200976 toolchain_ld_link_elf(
977 TARGET_ELF app_smem_unaligned_prebuilt
Marc Herbert0370c9b2019-06-13 16:15:44 -0700978 OUTPUT_MAP ${PROJECT_BINARY_DIR}/app_smem_unaligned_prebuilt.map
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +0200979 LIBRARIES_PRE_SCRIPT ""
980 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/linker_app_smem_unaligned.cmd
981 LIBRARIES_POST_SCRIPT ""
982 DEPENDENCIES ${CODE_RELOCATION_DEP}
983 )
Daniel Leung212ec9a2019-03-10 14:20:21 -0700984 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 +0200985 add_dependencies( app_smem_unaligned_prebuilt linker_app_smem_unaligned_script ${OFFSETS_LIB})
Daniel Leung212ec9a2019-03-10 14:20:21 -0700986
987 add_custom_command(
988 OUTPUT ${APP_SMEM_ALIGNED_LD}
989 COMMAND ${PYTHON_EXECUTABLE}
990 ${ZEPHYR_BASE}/scripts/gen_app_partitions.py
991 -e $<TARGET_FILE:app_smem_unaligned_prebuilt>
992 -o ${APP_SMEM_ALIGNED_LD}
Torsten Rasmussen5a703c82019-11-05 10:24:08 +0100993 ${NEWLIB_PART}
Torsten Rasmussen1f9723a2019-11-04 14:30:24 +0100994 $<TARGET_PROPERTY:zephyr_property_target,COMPILE_OPTIONS>
Daniel Leung212ec9a2019-03-10 14:20:21 -0700995 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
996 DEPENDS
997 kernel
998 ${ZEPHYR_LIBS_PROPERTY}
999 app_smem_unaligned_prebuilt
1000 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
Torsten Rasmussen1f9723a2019-11-04 14:30:24 +01001001 COMMAND_EXPAND_LISTS
Daniel Leung212ec9a2019-03-10 14:20:21 -07001002 COMMENT "Generating app_smem_aligned linker section"
Adithya Baglody50950eb2018-12-20 15:47:42 +05301003 )
1004endif()
1005
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001006# FIXME: Is there any way to get rid of empty_file.c?
Sebastian Bøe15260702019-01-14 16:31:02 +01001007add_executable( ${ZEPHYR_PREBUILT_EXECUTABLE} misc/empty_file.c)
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001008toolchain_ld_link_elf(
1009 TARGET_ELF ${ZEPHYR_PREBUILT_EXECUTABLE}
Marc Herbert0370c9b2019-06-13 16:15:44 -07001010 OUTPUT_MAP ${PROJECT_BINARY_DIR}/${ZEPHYR_PREBUILT_EXECUTABLE}.map
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001011 LIBRARIES_PRE_SCRIPT ""
1012 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/linker.cmd
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001013 DEPENDENCIES ${CODE_RELOCATION_DEP}
1014)
Sebastian Bøe15260702019-01-14 16:31:02 +01001015set_property(TARGET ${ZEPHYR_PREBUILT_EXECUTABLE} PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker.cmd)
Andrew Boie28be7932020-03-11 10:56:19 -07001016add_dependencies( ${ZEPHYR_PREBUILT_EXECUTABLE} ${LINKER_SCRIPT_TARGET} ${OFFSETS_LIB})
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001017
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +02001018
Marc Herbert498b4942019-04-16 23:30:52 -07001019set(generated_kernel_files ${GKSF} ${GKOF})
1020if(NOT generated_kernel_files)
1021 # Use the prebuilt elf as the final elf since we don't have a
1022 # generation stage.
1023 set(logical_target_for_zephyr_elf ${ZEPHYR_PREBUILT_EXECUTABLE})
1024else()
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001025 # The second linker pass uses the same source linker script of the
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +01001026 # first pass (LINKER_SCRIPT), but this time with a different output
1027 # file and preprocessed with the define LINKER_PASS2.
Mark Ruvald Pedersen4c811972019-04-29 17:16:54 +02001028 configure_linker_script(
Mark Ruvald Pedersen1073fba2019-04-29 20:27:23 +02001029 linker_pass_final.cmd
Mark Ruvald Pedersenfbcea172019-04-29 20:35:12 +02001030 "-DLINKER_PASS2"
Sebastian Bøe2a963122019-02-08 15:49:57 +01001031 ${CODE_RELOCATION_DEP}
1032 ${ZEPHYR_PREBUILT_EXECUTABLE}
Sebastian Bøefdac7b32020-01-23 15:39:17 +01001033 zephyr_generated_headers
Sebastian Bøe7a6afcd2019-02-08 15:39:37 +01001034 )
Andy Grosse8860fe2018-02-01 01:12:32 -06001035
Sebastian Bøe1b86fb92019-01-14 16:39:33 +01001036 set(LINKER_PASS_FINAL_SCRIPT_TARGET linker_pass_final_script_target)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001037 add_custom_target(
Sebastian Bøe1b86fb92019-01-14 16:39:33 +01001038 ${LINKER_PASS_FINAL_SCRIPT_TARGET}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001039 DEPENDS
Andy Gross1f0ff062018-01-25 11:07:03 -06001040 linker_pass_final.cmd
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001041 )
Sebastian Bøeb1ab5012017-12-14 13:03:23 +01001042 set_property(TARGET
Sebastian Bøe1b86fb92019-01-14 16:39:33 +01001043 ${LINKER_PASS_FINAL_SCRIPT_TARGET}
Sebastian Bøeb1ab5012017-12-14 13:03:23 +01001044 PROPERTY INCLUDE_DIRECTORIES
1045 ${ZEPHYR_INCLUDE_DIRS}
1046 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001047
Mark Ruvald Pedersen37d49472019-05-07 15:20:20 +02001048 add_executable( ${ZEPHYR_FINAL_EXECUTABLE} misc/empty_file.c ${GKSF})
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001049 toolchain_ld_link_elf(
1050 TARGET_ELF ${ZEPHYR_FINAL_EXECUTABLE}
Marc Herbert0370c9b2019-06-13 16:15:44 -07001051 OUTPUT_MAP ${PROJECT_BINARY_DIR}/${ZEPHYR_FINAL_EXECUTABLE}.map
Mark Ruvald Pedersen4052bac2019-05-07 16:32:36 +02001052 LIBRARIES_PRE_SCRIPT ${GKOF}
1053 LINKER_SCRIPT ${PROJECT_BINARY_DIR}/linker_pass_final.cmd
1054 LIBRARIES_POST_SCRIPT ""
1055 DEPENDENCIES ${CODE_RELOCATION_DEP}
1056 )
Mark Ruvald Pedersen37d49472019-05-07 15:20:20 +02001057 set_property(TARGET ${ZEPHYR_FINAL_EXECUTABLE} PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker_pass_final.cmd)
Andrew Boie28be7932020-03-11 10:56:19 -07001058 add_dependencies( ${ZEPHYR_FINAL_EXECUTABLE} ${LINKER_PASS_FINAL_SCRIPT_TARGET})
Mark Ruvald Pedersen37d49472019-05-07 15:20:20 +02001059
1060 # Use the pass2 elf as the final elf
1061 set(logical_target_for_zephyr_elf ${ZEPHYR_FINAL_EXECUTABLE})
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001062endif()
1063
Sebastian Bøe0fc39342018-10-16 13:25:04 +02001064# Export the variable to the application's scope to allow the
1065# application to know what the name of the final elf target is.
1066set(logical_target_for_zephyr_elf ${logical_target_for_zephyr_elf} PARENT_SCOPE)
1067
Marc Herbert0370c9b2019-06-13 16:15:44 -07001068# Override the base name of the last, "logical" .elf output (and last .map) so:
Marc Herbert498b4942019-04-16 23:30:52 -07001069# 1. it doesn't depend on the number of passes above and the
1070# post_build_commands below can always find it no matter which is it;
1071# 2. it can be defined in Kconfig
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001072set_target_properties(${logical_target_for_zephyr_elf} PROPERTIES OUTPUT_NAME ${KERNEL_NAME})
1073
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001074set(post_build_commands "")
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001075set(post_build_byproducts "")
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001076
Marc Herbert0370c9b2019-06-13 16:15:44 -07001077list(APPEND
1078 post_build_commands
1079 COMMAND
Torsten Rasmussen4df38c72020-06-11 10:48:06 +02001080 ${CMAKE_COMMAND} -E rename ${logical_target_for_zephyr_elf}.map ${KERNEL_MAP_NAME}
Marc Herbert0370c9b2019-06-13 16:15:44 -07001081)
1082
Håkon Øye Amundsenc086b932018-11-26 09:47:16 +00001083if(NOT CONFIG_BUILD_NO_GAP_FILL)
1084 # Use ';' as separator to get proper space in resulting command.
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001085 set(GAP_FILL "$<TARGET_PROPERTY:bintools,elfconvert_flag_gapfill>0xff")
Håkon Øye Amundsenc086b932018-11-26 09:47:16 +00001086endif()
1087
Danny Oerndrupc41e7122019-07-18 15:16:39 +02001088if(CONFIG_OUTPUT_PRINT_MEMORY_USAGE)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001089 target_link_libraries(${ZEPHYR_PREBUILT_EXECUTABLE} $<TARGET_PROPERTY:linker,memusage>)
1090
1091 get_property(memusage_build_command TARGET bintools PROPERTY memusage_command)
1092 if(memusage_build_command)
1093 # Note: The use of generator expressions allows downstream extensions to add/change the post build.
1094 # Unfortunately, the BYPRODUCTS does not allow for generator expression, so question is if we
1095 # should remove the downstream ability from start.
1096 # Or fix the output name, by the use of `get_property`
1097 list(APPEND
1098 post_build_commands
Torsten Rasmussen571f48f2020-09-04 21:07:46 +02001099 COMMAND $<TARGET_PROPERTY:bintools,memusage_command>
1100 $<TARGET_PROPERTY:bintools,memusage_flag>
1101 $<TARGET_PROPERTY:bintools,memusage_infile>${KERNEL_ELF_NAME}
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001102 )
1103
1104 # For now, the byproduct can only be supported upstream on byproducts name,
1105 # cause byproduct does not support generator expressions
1106 get_property(memusage_byproducts TARGET bintools PROPERTY memusage_byproducts)
1107 list(APPEND
1108 post_build_byproducts
1109 ${memusage_byproducts}
1110 )
1111 endif()
Danny Oerndrupc41e7122019-07-18 15:16:39 +02001112endif()
1113
Kumar Galad5419132019-08-13 13:44:20 -05001114if(CONFIG_BUILD_OUTPUT_HEX OR BOARD_FLASH_RUNNER STREQUAL openocd)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001115 get_property(elfconvert_formats TARGET bintools PROPERTY elfconvert_formats)
1116 if(ihex IN_LIST elfconvert_formats)
1117 list(APPEND
1118 post_build_commands
1119 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1120 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1121 ${GAP_FILL}
1122 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>ihex
1123 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>.comment
1124 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>COMMON
1125 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>.eh_frame
1126 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1127 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_HEX_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001128 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001129 )
1130 list(APPEND
1131 post_build_byproducts
1132 ${KERNEL_HEX_NAME}
1133 # ${out_hex_byprod} # Is this needed ?
1134 )
1135 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001136endif()
Anas Nashif4592ff22017-11-23 07:54:26 -05001137
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001138if(CONFIG_BUILD_OUTPUT_BIN)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001139 get_property(elfconvert_formats TARGET bintools PROPERTY elfconvert_formats)
1140 if(binary IN_LIST elfconvert_formats)
1141 list(APPEND
1142 post_build_commands
1143 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1144 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1145 ${GAP_FILL}
1146 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>binary
1147 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>.comment
1148 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>COMMON
1149 $<TARGET_PROPERTY:bintools,elfconvert_flag_section_remove>.eh_frame
1150 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1151 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_BIN_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001152 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001153 )
1154 list(APPEND
1155 post_build_byproducts
1156 ${KERNEL_BIN_NAME}
1157 # ${out_hex_byprod} # Is this needed ?
1158 )
1159 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001160endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001161
Anas Nashiffdbf2db2020-10-20 14:31:56 -04001162
1163# Cleanup intermediate files
1164if(CONFIG_CLEANUP_INTERMEDIATE_FILES)
1165 list(APPEND
1166 post_build_commands
1167 COMMAND
1168 # This file can be very large in some cases, delete it as we do not need it.
1169 ${CMAKE_COMMAND} -E remove ${ZEPHYR_PREBUILT_EXECUTABLE}.elf
1170 )
1171endif()
1172
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001173if(CONFIG_BUILD_OUTPUT_S19)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001174 get_property(elfconvert_formats TARGET bintools PROPERTY elfconvert_formats)
1175 if(srec IN_LIST elfconvert_formats)
1176 # Should we print a warning if case the tools does not support converting to s19 ?
1177 list(APPEND
1178 post_build_commands
1179 COMMAND $<TARGET_PROPERTY:bintools,elfconvert_command>
1180 $<TARGET_PROPERTY:bintools,elfconvert_flag>
1181 ${GAP_FILL}
1182 $<TARGET_PROPERTY:bintools,elfconvert_flag_outtarget>srec
1183 $<TARGET_PROPERTY:bintools,elfconvert_flag_srec_len>1
1184 $<TARGET_PROPERTY:bintools,elfconvert_flag_infile>${KERNEL_ELF_NAME}
1185 $<TARGET_PROPERTY:bintools,elfconvert_flag_outfile>${KERNEL_S19_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001186 $<TARGET_PROPERTY:bintools,elfconvert_flag_final>
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001187 )
1188 list(APPEND
1189 post_build_byproducts
1190 ${KERNEL_S19_NAME}
1191 # ${out_S19_byprod} # Is this needed ?
1192
1193 )
1194 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001195endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001196
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001197if(CONFIG_OUTPUT_DISASSEMBLY)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001198if(CONFIG_OUTPUT_DISASSEMBLE_ALL)
1199 set(disassembly_type "$<TARGET_PROPERTY:bintools,disassembly_flag_all>")
Rohit Gujarathi35713f22020-05-07 10:08:37 +05301200 else()
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001201 set(disassembly_type "$<TARGET_PROPERTY:bintools,disassembly_flag_inline_source>")
Rohit Gujarathi35713f22020-05-07 10:08:37 +05301202 endif()
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001203 list(APPEND
1204 post_build_commands
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001205 COMMAND $<TARGET_PROPERTY:bintools,disassembly_command>
1206 $<TARGET_PROPERTY:bintools,disassembly_flag>
1207 ${disassembly_type}
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001208 $<TARGET_PROPERTY:bintools,disassembly_flag_infile>${KERNEL_ELF_NAME}
1209 $<TARGET_PROPERTY:bintools,disassembly_flag_outfile>${KERNEL_LST_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001210 $<TARGET_PROPERTY:bintools,disassembly_flag_final>
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001211 )
1212 list(APPEND
1213 post_build_byproducts
1214 ${KERNEL_LST_NAME}
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001215# ${out_disassembly_byprod} # Needed ??
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001216 )
1217endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001218
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001219if(CONFIG_OUTPUT_STAT)
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001220# zephyr_post_build(TOOLS bintools COMMAND readelf FLAGS headers INFILE file OUTFILE outfile)
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001221 list(APPEND
1222 post_build_commands
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001223 COMMAND $<TARGET_PROPERTY:bintools,readelf_command>
1224 $<TARGET_PROPERTY:bintools,readelf_flag>
1225 $<TARGET_PROPERTY:bintools,readelf_flag_headers>
1226 $<TARGET_PROPERTY:bintools,readelf_flag_infile> ${KERNEL_ELF_NAME}
1227 $<TARGET_PROPERTY:bintools,readelf_flag_outfile> ${KERNEL_STAT_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001228 $<TARGET_PROPERTY:bintools,readelf_flag_final>
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001229 )
1230 list(APPEND
1231 post_build_byproducts
1232 ${KERNEL_STAT_NAME}
1233 )
1234endif()
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001235
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001236if(CONFIG_BUILD_OUTPUT_STRIPPED)
1237 list(APPEND
1238 post_build_commands
Torsten Rasmussenc060b072020-08-18 14:46:06 +02001239 COMMAND $<TARGET_PROPERTY:bintools,strip_command>
1240 $<TARGET_PROPERTY:bintools,strip_flag>
1241 $<TARGET_PROPERTY:bintools,strip_flag_all>
1242 $<TARGET_PROPERTY:bintools,strip_flag_infile>${KERNEL_ELF_NAME}
1243 $<TARGET_PROPERTY:bintools,strip_flag_outfile>${KERNEL_STRIP_NAME}
Torsten Rasmussenf160dee2020-09-04 10:05:00 +02001244 $<TARGET_PROPERTY:bintools,strip_flag_final>
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001245 )
1246 list(APPEND
1247 post_build_byproducts
1248 ${KERNEL_STRIP_NAME}
1249 )
1250endif()
1251
1252if(CONFIG_BUILD_OUTPUT_EXE)
1253 list(APPEND
1254 post_build_commands
1255 COMMAND
1256 ${CMAKE_COMMAND} -E copy ${KERNEL_ELF_NAME} ${KERNEL_EXE_NAME}
1257 )
1258 list(APPEND
1259 post_build_byproducts
1260 ${KERNEL_EXE_NAME}
1261 )
1262endif()
Anas Nashif4592ff22017-11-23 07:54:26 -05001263
Martí Bolívarf66a0c32020-08-18 11:28:04 -07001264# Generate and use MCUboot related artifacts as needed.
1265if(CONFIG_BOOTLOADER_MCUBOOT)
1266 include(${CMAKE_CURRENT_LIST_DIR}/cmake/mcuboot.cmake)
1267endif()
1268
Rajavardhan Gundiecdea1c2019-01-25 11:53:13 +05301269get_property(extra_post_build_commands
1270 GLOBAL PROPERTY
1271 extra_post_build_commands
1272 )
1273
1274list(APPEND
1275 post_build_commands
1276 ${extra_post_build_commands}
1277 )
1278
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001279get_property(extra_post_build_byproducts
1280 GLOBAL PROPERTY
1281 extra_post_build_byproducts
1282 )
1283
1284list(APPEND
1285 post_build_byproducts
1286 ${extra_post_build_byproducts}
1287 )
1288
Marc Herbert498b4942019-04-16 23:30:52 -07001289# Add post_build_commands to post-process the final .elf file produced by
1290# either the ZEPHYR_PREBUILT_EXECUTABLE or the KERNEL_ELF executable
1291# targets above.
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001292add_custom_command(
1293 TARGET ${logical_target_for_zephyr_elf}
1294 POST_BUILD
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001295 ${post_build_commands}
Sebastian Bøef483e5b2019-05-10 10:06:32 +02001296 BYPRODUCTS
1297 ${post_build_byproducts}
Marc Herbert498b4942019-04-16 23:30:52 -07001298 COMMENT "Generating files from ${KERNEL_ELF_NAME} for board: ${BOARD}"
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001299 COMMAND_EXPAND_LISTS
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001300 # NB: COMMENT only works for some CMake-Generators
Rajavardhan Gundiecdea1c2019-01-25 11:53:13 +05301301 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001302
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001303# To populate with hex files to merge, do the following:
1304# set_property(GLOBAL APPEND PROPERTY HEX_FILES_TO_MERGE ${my_local_list})
1305# Note that the zephyr.hex file will not be included automatically.
1306get_property(HEX_FILES_TO_MERGE GLOBAL PROPERTY HEX_FILES_TO_MERGE)
1307if(HEX_FILES_TO_MERGE)
1308 # Merge in out-of-tree hex files.
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001309 set(MERGED_HEX_NAME merged.hex)
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001310
1311 add_custom_command(
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001312 OUTPUT ${MERGED_HEX_NAME}
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001313 COMMAND
1314 ${PYTHON_EXECUTABLE}
1315 ${ZEPHYR_BASE}/scripts/mergehex.py
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001316 -o ${MERGED_HEX_NAME}
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001317 ${HEX_FILES_TO_MERGE}
1318 DEPENDS ${HEX_FILES_TO_MERGE} ${logical_target_for_zephyr_elf}
1319 )
1320
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001321 add_custom_target(mergehex ALL DEPENDS ${MERGED_HEX_NAME})
Torsten Rasmussend38da9d2020-06-30 09:55:54 +02001322 list(APPEND RUNNERS_DEPS mergehex)
Håkon Øye Amundsenc9a2a5e2020-01-03 08:25:03 +00001323
1324 message(VERBOSE "Merging hex files: ${HEX_FILES_TO_MERGE}")
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001325endif()
1326
Anas Nashifc15d3c92017-11-21 18:54:55 -05001327if(EMU_PLATFORM)
Carles Cufi7d764b32018-01-11 15:46:44 +01001328 include(${ZEPHYR_BASE}/cmake/emu/${EMU_PLATFORM}.cmake)
Anas Nashiffd276ae2017-12-21 16:45:45 -05001329else()
1330 add_custom_target(run
1331 COMMAND
1332 ${CMAKE_COMMAND} -E echo
1333 "==================================================="
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +01001334 "Emulation/Simulation not supported with this board."
Anas Nashiffd276ae2017-12-21 16:45:45 -05001335 "==================================================="
1336 )
Anas Nashifc15d3c92017-11-21 18:54:55 -05001337endif()
1338
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001339add_subdirectory(cmake/flash)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001340add_subdirectory(cmake/usage)
1341add_subdirectory(cmake/reports)
1342
Marc Herbert83723102019-06-17 13:26:11 -07001343if(NOT CONFIG_TEST)
Andrew Boie411686f2018-05-24 13:18:36 -07001344if(CONFIG_ASSERT AND (NOT CONFIG_FORCE_NO_ASSERT))
Sebastian Bøefa8f9d42019-12-06 12:54:53 +01001345 message(WARNING "__ASSERT() statements are globally ENABLED")
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001346endif()
Marc Herbert83723102019-06-17 13:26:11 -07001347endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001348
Vincent Wana2bc5142020-01-09 14:20:44 -08001349if(CONFIG_BOARD_DEPRECATED_RELEASE)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001350 message(WARNING "
1351 WARNING: The board '${BOARD}' is deprecated and will be
Vincent Wana2bc5142020-01-09 14:20:44 -08001352 removed in version ${CONFIG_BOARD_DEPRECATED_RELEASE}"
Mark Ruvald Pedersen0efad5f2018-12-19 10:40:57 +01001353 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001354endif()
Paul Sokolovsky1c6a7d72019-06-06 21:12:14 +03001355
Vincent Wan180b4df2020-01-08 17:10:51 -08001356if(CONFIG_SOC_DEPRECATED_RELEASE)
1357 message(WARNING "
1358 WARNING: The SoC '${SOC_NAME}' is deprecated and will be
1359 removed in version ${CONFIG_SOC_DEPRECATED_RELEASE}"
1360 )
1361endif()
1362
Sebastian Bøee50e12d2019-08-29 16:19:32 +02001363# In CMake projects, 'CMAKE_BUILD_TYPE' usually determines the
1364# optimization flag, but in Zephyr it is determined through
1365# Kconfig. Here we give a warning when there is a mismatch between the
1366# two in case the user is not aware of this.
1367set(build_types None Debug Release RelWithDebInfo MinSizeRel)
1368
1369if((CMAKE_BUILD_TYPE IN_LIST build_types) AND (NOT NO_BUILD_TYPE_WARNING))
1370 string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_uppercase)
1371
Jack Dähne41bdcd2019-11-15 19:01:39 +01001372 if(NOT (${OPTIMIZATION_FLAG} IN_LIST CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_uppercase}))
Sebastian Bøee50e12d2019-08-29 16:19:32 +02001373 message(WARNING "
1374 The CMake build type was set to '${CMAKE_BUILD_TYPE}', but the optimization flag was set to '${OPTIMIZATION_FLAG}'.
1375 This may be intentional and the warning can be turned off by setting the CMake variable 'NO_BUILD_TYPE_WARNING'"
1376 )
1377 endif()
1378endif()
1379
Paul Sokolovsky1c6a7d72019-06-06 21:12:14 +03001380# @Intent: Set compiler specific flags for standard C includes
1381# Done at the very end, so any other system includes which may
1382# be added by Zephyr components were first in list.
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001383# Note, the compile flags are moved, but the system include is still present here.
1384zephyr_compile_options($<TARGET_PROPERTY:compiler,nostdinc>)
1385target_include_directories(zephyr_interface SYSTEM INTERFACE $<TARGET_PROPERTY:compiler,nostdinc_include>)
Torsten Rasmussena5917f02020-09-09 15:42:32 +02001386
1387# Finally export all build flags from Zephyr
1388add_subdirectory_ifdef(
1389 CONFIG_MAKEFILE_EXPORTS
1390 cmake/makefile_exports
1391 )