blob: 4a61537277524fef53cc1e74c00d10086dc7e868 [file] [log] [blame]
Sebastian Bøeee9af862018-06-04 11:47:45 +02001if(NOT DEFINED ZEPHYR_BINARY_DIR)
2 message(FATAL_ERROR "A user error has occured.
3cmake was invoked with '${CMAKE_CURRENT_LIST_DIR}' specified as the source directory,
4but it must be invoked with an application source directory,
5such as '${CMAKE_CURRENT_LIST_DIR}/samples/hello_world'.
6Debug variables:
7CMAKE_CACHEFILE_DIR: ${CMAKE_CACHEFILE_DIR}
8")
9endif()
10
Sebastian Bøe12f8f762017-10-27 15:43:34 +020011project(Zephyr-Kernel VERSION ${PROJECT_VERSION})
12enable_language(C CXX ASM)
13
14# *DOCUMENTATION*
15#
16# Note that this is *NOT* the top-level CMakeLists.txt. That's in the
17# application. See the Application Development Primer documentation
18# for details.
19#
20# To see a list of typical targets execute "make usage"
21# More info can be located in ./README.rst
22# Comments in this file are targeted only to the developer, do not
23# expect to learn how to build the kernel reading this file.
24
25# Verify that the toolchain can compile a dummy file, if it is not we
26# won't be able to test for compatiblity with certain C flags.
27check_c_compiler_flag("" toolchain_is_ok)
28assert(toolchain_is_ok "The toolchain is unable to build a dummy C file. See CMakeError.log.")
29
Sebastian Bøe12f8f762017-10-27 15:43:34 +020030set(CMAKE_EXECUTABLE_SUFFIX .elf)
31
Sebastian Bøe12f8f762017-10-27 15:43:34 +020032if(NOT PROPERTY_LINKER_SCRIPT_DEFINES)
33 set_property(GLOBAL PROPERTY PROPERTY_LINKER_SCRIPT_DEFINES -D__GCC_LINKER_CMD__)
34endif()
35
36define_property(GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT BRIEF_DOCS " " FULL_DOCS " ")
37set_property( GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT elf32-little${ARCH})
38
39# zephyr_interface is a source-less library that has all the global
40# compiler options needed by all source files. All zephyr libraries,
41# including the library named "zephyr" link with this library to
42# obtain these flags.
43add_library(zephyr_interface INTERFACE)
44
45# zephyr is a catchall CMake library for source files that can be
46# built purely with the include paths, defines, and other compiler
47# flags that come with zephyr_interface.
48zephyr_library_named(zephyr)
49
50zephyr_include_directories(
51 kernel/include
52 arch/${ARCH}/include
Anas Nashif96455d52018-09-04 14:34:06 -050053 ${SOC_DIR}/${ARCH}/${SOC_PATH}
54 ${SOC_DIR}/${ARCH}/${SOC_PATH}/include
55 ${SOC_DIR}/${ARCH}/${SOC_FAMILY}/include
Sebastian Bøe12f8f762017-10-27 15:43:34 +020056 include
57 include/drivers
58 ${PROJECT_BINARY_DIR}/include/generated
59 ${USERINCLUDE}
60 ${STDINCLUDE}
61)
62
Sebastian Bøe12f8f762017-10-27 15:43:34 +020063zephyr_compile_definitions(
64 KERNEL
65 __ZEPHYR__=1
Anas Nashif34aebad2018-01-03 12:26:19 -050066)
67
Thomas Ebert Hansen15bc6152018-07-12 12:01:55 +020068if(NOT CONFIG_NO_OPTIMIZATIONS)
Anas Nashif34aebad2018-01-03 12:26:19 -050069zephyr_compile_definitions(
Sebastian Bøe12f8f762017-10-27 15:43:34 +020070 _FORTIFY_SOURCE=2
71)
Anas Nashif34aebad2018-01-03 12:26:19 -050072endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +020073
Anas Nashifdaf77162018-04-09 21:53:26 -050074if(BUILD_VERSION)
75 zephyr_compile_definitions(
76 BUILD_VERSION=${BUILD_VERSION}
77 )
78endif()
79
Sebastian Bøe12f8f762017-10-27 15:43:34 +020080# We need to set an optimization level.
81# Default to -Os
Alberto Escolar Piedrasf60527a2018-01-22 15:35:54 +010082# unless CONFIG_NO_OPTIMIZATIONS is set, then it is -O0
83# or unless CONFIG_DEBUG is set, then it is -Og
Sebastian Bøe12f8f762017-10-27 15:43:34 +020084#
85# also, some toolchain's break with -Os, and some toolchain's break
86# with -Og so allow them to override what flag to use
87#
88# Finally, the user can use Kconfig to add compiler options that will
89# come after these options and override them
Alberto Escolar Piedrasf60527a2018-01-22 15:35:54 +010090set_ifndef(OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG "-O0")
Sebastian Bøe600c8f72018-01-24 10:40:32 +010091set_ifndef(OPTIMIZE_FOR_DEBUG_FLAG "-Og")
92set_ifndef(OPTIMIZE_FOR_SIZE_FLAG "-Os")
Aurelien Jarnoe8413d12018-06-16 23:40:04 +020093set_ifndef(OPTIMIZE_FOR_SPEED_FLAG "-O2")
Sebastian Bøe600c8f72018-01-24 10:40:32 +010094
Alberto Escolar Piedrasf60527a2018-01-22 15:35:54 +010095if(CONFIG_NO_OPTIMIZATIONS)
Sebastian Bøe600c8f72018-01-24 10:40:32 +010096 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG})
97elseif(CONFIG_DEBUG_OPTIMIZATIONS)
Sebastian Bøe12f8f762017-10-27 15:43:34 +020098 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_DEBUG_FLAG})
Aurelien Jarnoe8413d12018-06-16 23:40:04 +020099elseif(CONFIG_SPEED_OPTIMIZATIONS)
100 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SPEED_FLAG})
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100101elseif(CONFIG_SIZE_OPTIMIZATIONS)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200102 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SIZE_FLAG}) # Default
Sebastian Bøe600c8f72018-01-24 10:40:32 +0100103else()
104 assert(0 "Unreachable code. Expected optimization level to have been chosen. See misc/Kconfig.")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200105endif()
106
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200107# Dialects of C++, corresponding to the multiple published ISO standards.
108# Which standard it implements can be selected using the -std= command-line option.
109set_ifndef(DIALECT_STD_CPP98 "c++98")
110set_ifndef(DIALECT_STD_CPP11 "c++11")
111set_ifndef(DIALECT_STD_CPP14 "c++14")
112set_ifndef(DIALECT_STD_CPP17 "c++17")
113set_ifndef(DIALECT_STD_CPP2A "c++2a")
114
115if(CONFIG_STD_CPP98)
116 set(STD_CPP_DIALECT ${DIALECT_STD_CPP98})
117elseif(CONFIG_STD_CPP11)
118 set(STD_CPP_DIALECT ${DIALECT_STD_CPP11}) # Default
119elseif(CONFIG_STD_CPP14)
120 set(STD_CPP_DIALECT ${DIALECT_STD_CPP14})
121elseif(CONFIG_STD_CPP17)
122 set(STD_CPP_DIALECT ${DIALECT_STD_CPP17})
123elseif(CONFIG_STD_CPP2A)
124 set(STD_CPP_DIALECT ${DIALECT_STD_CPP2A})
125else()
126 assert(0 "Unreachable code. Expected C++ standard to have been chosen. See misc/Kconfig.")
127endif()
128
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200129zephyr_compile_options(
130 ${OPTIMIZATION_FLAG} # Usually -Os
131 -g # TODO: build configuration enough?
132 -Wall
133 -Wformat
134 -Wformat-security
135 -Wno-format-zero-length
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200136 -imacros ${AUTOCONF_H}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200137 -ffreestanding
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200138 -Wno-main
139 ${NOSTDINC_F}
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530140 ${TOOLCHAIN_C_FLAGS}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200141)
142
143zephyr_compile_options(
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200144 $<$<COMPILE_LANGUAGE:CXX>:-std=${STD_CPP_DIALECT}>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200145 $<$<COMPILE_LANGUAGE:CXX>:-fcheck-new>
146 $<$<COMPILE_LANGUAGE:CXX>:-ffunction-sections>
147 $<$<COMPILE_LANGUAGE:CXX>:-fdata-sections>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200148
149 $<$<COMPILE_LANGUAGE:ASM>:-xassembler-with-cpp>
150 $<$<COMPILE_LANGUAGE:ASM>:-D_ASMLANGUAGE>
151)
152
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200153if(NOT CONFIG_RTTI)
154zephyr_compile_options(
155 $<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>
156)
157endif()
158
159if(NOT CONFIG_EXCEPTIONS)
160zephyr_compile_options(
161 $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
162)
163endif()
164
Aurelien Jarnoc6727d42018-11-26 13:48:34 +0100165zephyr_ld_options(
166 ${TOOLCHAIN_LD_FLAGS}
167)
168
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200169if(NOT CONFIG_NATIVE_APPLICATION)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200170zephyr_ld_options(
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200171 -nostdlib
172 -static
173 -no-pie
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200174)
175endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200176
Benoit Leforestier26e0f9a2018-10-23 18:20:51 +0200177if(CONFIG_LIB_CPLUSPLUS)
178zephyr_ld_options(
179 -lstdc++
180)
181endif()
182
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200183# ==========================================================================
184#
185# cmake -DW=... settings
186#
187# W=1 - warnings that may be relevant and does not occur too often
188# W=2 - warnings that occur quite often but may still be relevant
189# W=3 - the more obscure warnings, can most likely be ignored
190# ==========================================================================
191if(W MATCHES "1")
192 zephyr_compile_options(
193 -Wextra
194 -Wunused
195 -Wno-unused-parameter
196 -Wmissing-declarations
197 -Wmissing-format-attribute
198 -Wold-style-definition
199 )
200 zephyr_cc_option(
201 -Wmissing-prototypes
202 -Wmissing-include-dirs
203 -Wunused-but-set-variable
204 -Wno-missing-field-initializers
205 )
206endif()
207
208if(W MATCHES "2")
209 zephyr_compile_options(
210 -Waggregate-return
211 -Wcast-align
212 -Wdisabled-optimization
213 -Wnested-externs
214 -Wshadow
215 )
216 zephyr_cc_option(
217 -Wlogical-op
218 -Wmissing-field-initializers
219 )
220endif()
221
222if(W MATCHES "3")
223 zephyr_compile_options(
224 -Wbad-function-cast
225 -Wcast-qual
226 -Wconversion
227 -Wpacked
228 -Wpadded
229 -Wpointer-arith
230 -Wredundant-decls
231 -Wswitch-default
232 )
233 zephyr_cc_option(
234 -Wpacked-bitfield-compat
235 -Wvla
236 )
237endif()
238
239# Allow the user to inject options when calling cmake, e.g.
240# 'cmake -DEXTRA_CFLAGS="-Werror -Wno-deprecated-declarations" ..'
Sebastian Bøe9f590452017-11-10 12:22:23 +0100241include(cmake/extra_flags.cmake)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200242
243if(CONFIG_READABLE_ASM)
244 zephyr_cc_option(-fno-reorder-blocks)
245 zephyr_cc_option(-fno-ipa-cp-clone)
246 zephyr_cc_option(-fno-partial-inlining)
247endif()
248
249zephyr_cc_option(-fno-asynchronous-unwind-tables)
250zephyr_cc_option(-fno-pie)
251zephyr_cc_option(-fno-pic)
252zephyr_cc_option(-fno-strict-overflow)
253zephyr_cc_option(-Wno-pointer-sign)
254
Sebastian Bøe703dc592017-11-29 10:19:50 +0100255zephyr_compile_options_ifdef(CONFIG_STACK_CANARIES -fstack-protector-all)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200256
257if(CONFIG_OVERRIDE_FRAME_POINTER_DEFAULT)
258 if(CONFIG_OMIT_FRAME_POINTER)
259 zephyr_cc_option(-fomit-frame-pointer)
260 else()
261 zephyr_cc_option(-fno-omit-frame-pointer)
262 endif()
263endif()
264
Aurelien Jarno92a68982018-06-16 23:40:04 +0200265separate_arguments(CONFIG_COMPILER_OPT_AS_LIST UNIX_COMMAND ${CONFIG_COMPILER_OPT})
266zephyr_compile_options(${CONFIG_COMPILER_OPT_AS_LIST})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200267
268# TODO: Include arch compiler options at this point.
269
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200270if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
271 zephyr_cc_option(
Anas Nashif72edc4e2018-05-02 00:09:31 -0500272 #FIXME: need to fix all of those
273 -Wno-sometimes-uninitialized
274 -Wno-shift-overflow
275 -Wno-missing-braces
276 -Wno-self-assign
277 -Wno-address-of-packed-member
278 -Wno-unused-function
279 -Wno-initializer-overrides
280 -Wno-section
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200281 -Wno-unknown-warning-option
282 -Wno-unused-variable
283 -Wno-format-invalid-specifier
284 -Wno-gnu
285 # comparison of unsigned expression < 0 is always false
286 -Wno-tautological-compare
287 )
288else() # GCC assumed
289 zephyr_cc_option(
290 -Wno-unused-but-set-variable
291 -fno-reorder-functions
292 )
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530293
Anas Nashif7ee8bb92018-02-11 14:36:21 -0600294 if(NOT ${ZEPHYR_TOOLCHAIN_VARIANT} STREQUAL "xcc")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200295 zephyr_cc_option(-fno-defer-pop)
296 endif()
297endif()
298
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200299zephyr_cc_option_ifdef(CONFIG_STACK_USAGE -fstack-usage)
300
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200301zephyr_system_include_directories(${NOSTDINC})
302
303# Force an error when things like SYS_INIT(foo, ...) occur with a missing header.
304zephyr_cc_option(-Werror=implicit-int)
305
Mark Ruvald Pedersen28394cc2018-09-14 14:24:53 +0200306# Prohibit void pointer arithmetic. Illegal in C99
307zephyr_cc_option(-Wpointer-arith)
308
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200309# Prohibit date/time macros, which would make the build non-deterministic
310# cc-option(-Werror=date-time)
311
312# TODO: Archiver arguments
313# ar_option(D)
314
Håkon Øye Amundsend6551b52018-11-29 09:08:08 +0000315# Declare MPU userspace dependencies before the linker scripts to make
316# sure the order of dependencies are met
317if(CONFIG_CPU_HAS_MPU AND CONFIG_USERSPACE)
318 if(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT AND CONFIG_APP_SHARED_MEM )
319 set(APP_SMEM_DEP app_smem_linker)
320 endif()
321 if(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT AND CONFIG_ARC AND CONFIG_APPLICATION_MEMORY)
322 set(ALIGN_SIZING_DEP app_sizing_prebuilt linker_app_sizing_script)
323 endif()
324 if(CONFIG_ARM)
325 set(PRIV_STACK_DEP priv_stacks_prebuilt)
326 endif()
327endif()
328
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200329set_ifndef(LINKERFLAGPREFIX -Wl)
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200330
331if(NOT CONFIG_NATIVE_APPLICATION)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200332zephyr_ld_options(
333 ${LINKERFLAGPREFIX},-X
334 ${LINKERFLAGPREFIX},-N
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200335 )
336endif()
337
338zephyr_ld_options(
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200339 ${LINKERFLAGPREFIX},--gc-sections
340 ${LINKERFLAGPREFIX},--build-id=none
341 )
342
Daniel Leung6600c642018-10-19 10:15:19 -0700343if(NOT CONFIG_NATIVE_APPLICATION)
Håkon Øye Amundsen2a3f4342018-11-29 09:12:38 +0000344 set(NOSTDINC_F -nostdinc)
345endif()
346
Håkon Øye Amundsena4494392018-11-29 09:14:27 +0000347get_property(TOPT GLOBAL PROPERTY TOPT)
348set_ifndef( TOPT -T)
349
Håkon Øye Amundsen2a3f4342018-11-29 09:12:38 +0000350if(NOT CONFIG_NATIVE_APPLICATION)
Daniel Leung6600c642018-10-19 10:15:19 -0700351 # Funny thing is if this is set to =error, some architectures will
352 # skip this flag even though the compiler flag check passes
353 # (e.g. ARC and Xtensa). So warning should be the default for now.
354 #
355 # Skip this for native application as Zephyr only provides
356 # additions to the host toolchain linker script. The relocation
357 # sections (.rel*) requires us to override those provided
358 # by host toolchain. As we can't account for all possible
359 # combination of compiler and linker on all machines used
360 # for development, it is better to turn this off.
361 #
362 # CONFIG_LINKER_ORPHAN_SECTION_PLACE is to place the orphan sections
363 # without any warnings or errors, which is the default behavior.
364 # So there is no need to explicity set a linker flag.
365 if(CONFIG_LINKER_ORPHAN_SECTION_WARN)
366 zephyr_ld_options(
367 ${LINKERFLAGPREFIX},--orphan-handling=warn
368 )
369 elseif(CONFIG_LINKER_ORPHAN_SECTION_ERROR)
370 zephyr_ld_options(
371 ${LINKERFLAGPREFIX},--orphan-handling=error
372 )
373 endif()
374endif()
375
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200376if(CONFIG_HAVE_CUSTOM_LINKER_SCRIPT)
377 set(LINKER_SCRIPT ${APPLICATION_SOURCE_DIR}/${CONFIG_CUSTOM_LINKER_SCRIPT})
Sebastian Bøec1aa9d12018-04-12 14:48:05 +0200378 if(NOT EXISTS ${LINKER_SCRIPT})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200379 set(LINKER_SCRIPT ${CONFIG_CUSTOM_LINKER_SCRIPT})
Sebastian Bøec1aa9d12018-04-12 14:48:05 +0200380 assert_exists(CONFIG_CUSTOM_LINKER_SCRIPT)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200381 endif()
382else()
383 # Try a board specific linker file
384 set(LINKER_SCRIPT ${BOARD_DIR}/linker.ld)
385 if(NOT EXISTS ${LINKER_SCRIPT})
386 # If not available, try an SoC specific linker file
Anas Nashif96455d52018-09-04 14:34:06 -0500387 set(LINKER_SCRIPT ${SOC_DIR}/${ARCH}/${SOC_PATH}/linker.ld)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200388 endif()
389endif()
390
391if(NOT EXISTS ${LINKER_SCRIPT})
392 message(FATAL_ERROR "Could not find linker script: '${LINKER_SCRIPT}'. Corrupted configuration?")
393endif()
394
Kristian Klomsten Skordal0225e952018-01-30 11:26:42 +0100395# Custom section support in linker scripts requires that the application source
396# directory is in the preprocessor search path, in order to find the custom
397# linker script fragments.
398if(CONFIG_CUSTOM_RODATA_LD OR CONFIG_CUSTOM_RWDATA_LD OR CONFIG_CUSTOM_SECTIONS_LD)
399 zephyr_include_directories(${APPLICATION_SOURCE_DIR})
400endif()
401
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200402configure_file(version.h.in ${PROJECT_BINARY_DIR}/include/generated/version.h)
403
Håkon Øye Amundsen611b0d12018-11-29 09:15:33 +0000404function(construct_add_custom_command_for_linker_pass linker_output_name output_variable)
405 set(linker_cmd_file_name ${linker_output_name}.cmd)
406
407 if (${linker_output_name} MATCHES "^linker_pass_final$")
408 set(linker_pass_define -DLINKER_PASS2)
409 else()
410 set(linker_pass_define "")
411 endif()
412
413 # Different generators deal with depfiles differently.
414 if(CMAKE_GENERATOR STREQUAL "Unix Makefiles")
415 # Note that the IMPLICIT_DEPENDS option is currently supported only
416 # for Makefile generators and will be ignored by other generators.
417 set(linker_script_dep IMPLICIT_DEPENDS C ${LINKER_SCRIPT})
418 elseif(CMAKE_GENERATOR STREQUAL "Ninja")
419 # Using DEPFILE with other generators than Ninja is an error.
420 set(linker_script_dep DEPFILE ${PROJECT_BINARY_DIR}/${linker_cmd_file_name}.dep)
421 else()
422 # TODO: How would the linker script dependencies work for non-linker
423 # script generators.
424 message(STATUS "Warning; this generator is not well supported. The
425 Linker script may not be regenerated when it should.")
426 set(linker_script_dep "")
427 endif()
428
429 zephyr_get_include_directories_for_lang(C current_includes)
430 get_filename_component(base_name ${CMAKE_CURRENT_BINARY_DIR} NAME)
431 get_property(current_defines GLOBAL PROPERTY PROPERTY_LINKER_SCRIPT_DEFINES)
432
433 set(${output_variable}
434 OUTPUT ${linker_cmd_file_name}
435 DEPENDS ${LINKER_SCRIPT}
436 ${linker_script_dep}
437 COMMAND ${CMAKE_C_COMPILER}
438 -x assembler-with-cpp
439 ${NOSTDINC_F}
440 ${NOSYSDEF_CFLAG}
441 -MD -MF ${linker_cmd_file_name}.dep -MT ${base_name}/${linker_cmd_file_name}
442 ${current_includes}
443 ${current_defines}
444 ${linker_pass_define}
445 -E ${LINKER_SCRIPT}
446 -P # Prevent generation of debug `#line' directives.
447 -o ${linker_cmd_file_name}
448 VERBATIM
449 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
450
451 PARENT_SCOPE
452 )
453endfunction()
454
Sebastian Bøe6f946e22018-01-09 10:52:57 +0100455# Unfortunately, the order in which CMakeLists.txt code is processed
456# matters so we need to be careful about how we order the processing
457# of subdirectories. One example is "Compiler flags added late in the
458# build are not exported to external build systems #5605"; when we
459# integrate with an external build system we read out all compiler
460# flags when the external project is created. So an external project
461# defined in subsys or ext will not get global flags added by drivers/
462# or tests/ as the subdirectories are ordered now.
463#
464# Another example of when the order matters is the reading and writing
465# of global properties such as ZEPHYR_LIBS or
466# GENERATED_KERNEL_OBJECT_FILES.
467#
468# Arch is placed early because it defines important compiler flags
469# that must be exported to external build systems defined in
470# e.g. subsys/.
471add_subdirectory(arch)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200472add_subdirectory(lib)
473add_subdirectory(misc)
474# We use include instead of add_subdirectory to avoid creating a new directory scope.
475# This is because source file properties are directory scoped, including the GENERATED
476# property which is set implicitly for custom command outputs
477include(misc/generated/CMakeLists.txt)
Anas Nashif3d1252f2018-09-03 15:20:14 -0500478
Erwan Gouriouba31cb52018-09-13 16:25:53 +0200479if(EXISTS ${SOC_DIR}/${ARCH}/CMakeLists.txt)
Anas Nashif96455d52018-09-04 14:34:06 -0500480 add_subdirectory(${SOC_DIR}/${ARCH} soc/${ARCH})
Anas Nashif3d1252f2018-09-03 15:20:14 -0500481else()
Anas Nashif96455d52018-09-04 14:34:06 -0500482 add_subdirectory(${SOC_DIR}/${ARCH}/${SOC_PATH} soc/${ARCH}/${SOC_PATH})
Anas Nashif3d1252f2018-09-03 15:20:14 -0500483endif()
484
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200485add_subdirectory(boards)
486add_subdirectory(ext)
487add_subdirectory(subsys)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200488add_subdirectory(drivers)
489add_subdirectory(tests)
490
Sebastian Bøe2ead15d2017-11-29 17:46:37 +0100491set(syscall_macros_h ${ZEPHYR_BINARY_DIR}/include/generated/syscall_macros.h)
492
493add_custom_target(syscall_macros_h_target DEPENDS ${syscall_macros_h})
494add_custom_command( OUTPUT ${syscall_macros_h}
495 COMMAND
496 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200497 ${ZEPHYR_BASE}/scripts/gen_syscall_header.py
Sebastian Bøe2ead15d2017-11-29 17:46:37 +0100498 > ${syscall_macros_h}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200499 DEPENDS ${ZEPHYR_BASE}/scripts/gen_syscall_header.py
Sebastian Bøe2ead15d2017-11-29 17:46:37 +0100500 )
501
Sebastian Bøe13a68402017-11-20 13:03:55 +0100502
503set(syscall_list_h ${CMAKE_CURRENT_BINARY_DIR}/include/generated/syscall_list.h)
504set(syscalls_json ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls.json)
505
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200506# The syscalls subdirs txt file is constructed by python containing a list of folders to use for
507# dependency handling, including empty folders.
508# Windows: The list is used to specify DIRECTORY list with CMAKE_CONFIGURE_DEPENDS attribute.
509# Other OS: The list will update whenever a file is added/removed/modified and ensure a re-build.
510set(syscalls_subdirs_txt ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_subdirs.txt)
511
512# As syscalls_subdirs_txt is updated whenever a file is modified, this file can not be used for
513# monitoring of added / removed folders. A trigger file is thus used for correct dependency
514# handling. The trigger file will update when a folder is added / removed.
515set(syscalls_subdirs_trigger ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_subdirs.trigger)
516
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200517if(NOT (${CMAKE_HOST_SYSTEM_NAME} STREQUAL Windows))
518 set(syscalls_links --create-links ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls_links)
519endif()
520
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200521# When running CMake it must be ensured that all dependencies are correctly acquired.
522execute_process(
523 COMMAND
524 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200525 ${ZEPHYR_BASE}/scripts/subfolder_list.py
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200526 --directory ${ZEPHYR_BASE}/include # Walk this directory
527 --out-file ${syscalls_subdirs_txt} # Write file with discovered folder
528 --trigger ${syscalls_subdirs_trigger} # Trigger file that is used for json generation
529 ${syscalls_links} # If defined, create symlinks for dependencies
530)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200531file(STRINGS ${syscalls_subdirs_txt} PARSE_SYSCALLS_PATHS_DEPENDS)
532
533if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL Windows)
534 # On windows only adding/removing files or folders will be reflected in depends.
535 # Hence adding a file requires CMake to re-run to add this file to the file list.
536 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${PARSE_SYSCALLS_PATHS_DEPENDS})
537
538 # Also On Windows each header file must be monitored as file modifications are not reflected
539 # on directory level.
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200540 file(GLOB_RECURSE PARSE_SYSCALLS_HEADER_DEPENDS ${ZEPHYR_BASE}/include/*.h)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200541else()
542 # The syscall parsing depends on the folders in order to detect add/removed/modified files.
543 # When a folder is removed, CMake will try to find a target that creates that dependency.
544 # This command sets up the target for CMake to find.
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200545 # Without this code, CMake will fail with the following error:
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200546 # <folder> needed by '<target>', missing and no known rule to make it
547 # when a folder is removed.
548 add_custom_command(OUTPUT ${PARSE_SYSCALLS_PATHS_DEPENDS}
549 COMMAND ${CMAKE_COMMAND} -E echo ""
550 COMMENT "Preparing syscall dependency handling"
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200551 )
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200552
553 add_custom_command(
554 OUTPUT
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200555 ${syscalls_subdirs_trigger}
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200556 COMMAND
557 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200558 ${ZEPHYR_BASE}/scripts/subfolder_list.py
559 --directory ${ZEPHYR_BASE}/include # Walk this directory
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200560 --out-file ${syscalls_subdirs_txt} # Write file with discovered folder
561 --trigger ${syscalls_subdirs_trigger} # Trigger file that is used for json generation
562 ${syscalls_links} # If defined, create symlinks for dependencies
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200563 DEPENDS ${PARSE_SYSCALLS_PATHS_DEPENDS}
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200564 )
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200565
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200566 # Ensure subdir file always exists when specifying CMake dependency.
567 if(NOT EXISTS ${syscalls_subdirs_txt})
568 file(WRITE ${syscalls_subdirs_txt} "")
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200569 endif()
570
571 # On other OS'es, modifying a file is reflected on the folder timestamp and hence detected
572 # when using depend on directory level.
573 # Thus CMake only needs to re-run when sub-directories are added / removed, which is indicated
574 # using a trigger file.
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200575 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${syscalls_subdirs_txt})
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200576endif()
577
Adithya Baglodye67720b2018-07-02 14:59:19 +0530578# SYSCALL_INCLUDE_DIRECTORY will include the directories that needs to be
579# searched for syscall declarations if CONFIG_APPLICATION_DEFINED_SYSCALL is set
580if(CONFIG_APPLICATION_DEFINED_SYSCALL)
581 set(SYSCALL_INCLUDE_DIRECTORY --include ${APPLICATION_SOURCE_DIR})
582endif()
583
Sebastian Bøe13a68402017-11-20 13:03:55 +0100584add_custom_command(
585 OUTPUT
586 ${syscalls_json}
587 COMMAND
588 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200589 ${ZEPHYR_BASE}/scripts/parse_syscalls.py
Adithya Baglodye67720b2018-07-02 14:59:19 +0530590 --include ${ZEPHYR_BASE}/include # Read files from this dir
591 ${SYSCALL_INCLUDE_DIRECTORY}
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200592 --json-file ${syscalls_json} # Write this file
593 DEPENDS ${syscalls_subdirs_trigger} ${PARSE_SYSCALLS_HEADER_DEPENDS}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100594 )
595
596add_custom_target(syscall_list_h_target DEPENDS ${syscall_list_h})
597add_custom_command(OUTPUT include/generated/syscall_dispatch.c ${syscall_list_h}
598 # Also, some files are written to include/generated/syscalls/
599 COMMAND
600 ${PYTHON_EXECUTABLE}
Alex Tereschenko3c1a78e2018-06-14 20:21:18 +0200601 ${ZEPHYR_BASE}/scripts/gen_syscalls.py
Sebastian Bøe13a68402017-11-20 13:03:55 +0100602 --json-file ${syscalls_json} # Read this file
603 --base-output include/generated/syscalls # Write to this dir
604 --syscall-dispatch include/generated/syscall_dispatch.c # Write this file
Andrew Boie353acf42018-07-23 18:10:15 -0700605 --syscall-list ${syscall_list_h}
Sebastian Bøe13a68402017-11-20 13:03:55 +0100606 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
607 DEPENDS ${syscalls_json}
608 )
609
Leandro Pereirac2003672018-04-04 13:50:32 -0700610set(DRV_VALIDATION ${PROJECT_BINARY_DIR}/include/generated/driver-validation.h)
611add_custom_command(
612 OUTPUT ${DRV_VALIDATION}
613 COMMAND
614 ${PYTHON_EXECUTABLE}
615 ${ZEPHYR_BASE}/scripts/gen_kobject_list.py
616 --validation-output ${DRV_VALIDATION}
617 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
618 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
619 )
620add_custom_target(driver_validation_h_target DEPENDS ${DRV_VALIDATION})
621
Leandro Pereira39dc7d02018-04-05 13:59:33 -0700622include($ENV{ZEPHYR_BASE}/cmake/kobj.cmake)
623gen_kobj(KOBJ_INCLUDE_PATH)
Leandro Pereirac2003672018-04-04 13:50:32 -0700624
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200625# Generate offsets.c.obj from offsets.c
626# Generate offsets.h from offsets.c.obj
627
Anas Nashif644b31d2018-11-17 21:15:14 -0500628set(OFFSETS_C_PATH ${ZEPHYR_BASE}/arch/${ARCH}/core/offsets/offsets.c)
629set(OFFSETS_O_PATH ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/offsets.dir/arch/${ARCH}/core/offsets/offsets.c.obj)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200630set(OFFSETS_H_PATH ${PROJECT_BINARY_DIR}/include/generated/offsets.h)
631
Anas Nashif644b31d2018-11-17 21:15:14 -0500632add_library( offsets STATIC ${OFFSETS_C_PATH})
633target_link_libraries(offsets zephyr_interface)
Sebastian Bøe13a68402017-11-20 13:03:55 +0100634add_dependencies( offsets
635 syscall_list_h_target
636 syscall_macros_h_target
Leandro Pereirac2003672018-04-04 13:50:32 -0700637 driver_validation_h_target
Leandro Pereira39dc7d02018-04-05 13:59:33 -0700638 kobj_types_h_target
Sebastian Bøe13a68402017-11-20 13:03:55 +0100639 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200640
641add_custom_command(
642 OUTPUT ${OFFSETS_H_PATH}
Carles Cufi7d764b32018-01-11 15:46:44 +0100643 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/gen_offset_header.py
Anas Nashif644b31d2018-11-17 21:15:14 -0500644 -i ${OFFSETS_O_PATH}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200645 -o ${OFFSETS_H_PATH}
646 DEPENDS offsets
647)
648add_custom_target(offsets_h DEPENDS ${OFFSETS_H_PATH})
649
650zephyr_include_directories(${TOOLCHAIN_INCLUDES})
651
Sebastian Bøe89516fb2017-12-01 15:25:06 +0100652zephyr_get_include_directories_for_lang(C ZEPHYR_INCLUDES)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200653
654add_subdirectory(kernel)
655
656# Read list content
657get_property(ZEPHYR_LIBS_PROPERTY GLOBAL PROPERTY ZEPHYR_LIBS)
658
659foreach(zephyr_lib ${ZEPHYR_LIBS_PROPERTY})
660 # TODO: Could this become an INTERFACE property of zephyr_interface?
661 add_dependencies(${zephyr_lib} offsets_h)
Sebastian Bøe4ece94d2017-12-05 13:22:19 +0100662
Sebastian Bøe64edbaf2018-01-09 12:45:19 +0100663 # Verify that all (non-imported) libraries have source
664 # files. Libraries without source files are not supported because
665 # they are an indication that something has been misconfigured.
666 get_target_property(lib_imported ${zephyr_lib} IMPORTED)
667 get_target_property(lib_sources ${zephyr_lib} SOURCES)
Sebastian Bøe4ece94d2017-12-05 13:22:19 +0100668 if(lib_sources STREQUAL lib_sources-NOTFOUND
669 AND (NOT (${zephyr_lib} STREQUAL app))
Sebastian Bøe64edbaf2018-01-09 12:45:19 +0100670 AND (NOT lib_imported)
Sebastian Bøe4ece94d2017-12-05 13:22:19 +0100671 )
672 # app is not checked because it's sources are added to it after
673 # this CMakeLists.txt file has been processed
674 message(FATAL_ERROR "\
675The Zephyr library '${zephyr_lib}' was created without source files. \
Sebastian Bøe64edbaf2018-01-09 12:45:19 +0100676Empty (non-imported) libraries are not supported. \
Sebastian Bøe4ece94d2017-12-05 13:22:19 +0100677Either make sure that the library has the sources it should have, \
678or make sure it is not created when it has no source files.")
679 endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200680endforeach()
681
682get_property(OUTPUT_FORMAT GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT)
683
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200684if(CONFIG_APPLICATION_MEMORY)
685 # Objects default to being in kernel space, and then we exclude
686 # certain items.
687 set(kernel_object_file_list
688 ${ZEPHYR_LIBS_PROPERTY}
689 kernel
690 )
691 list(
692 REMOVE_ITEM
693 kernel_object_file_list
694 app
695 )
696
697 # The zephyr libraries in zephyr/lib/ and zephyr/test/ belong in
698 # userspace.
699
700 # NB: The business logic for determing what source files are in
701 # kernel space and what source files are in user space is
702 # fragile. Fix ASAP.
703 #
704 # The intended design is that certain directories are designated as
705 # containing userspace code and others for kernel space code. The
706 # implementation we have however is not working on directories of
707 # code, it is working on zephyr libraries. It is exploiting the fact
708 # that zephyr libraries follow a naming scheme as described in
709 # extensions.cmake:zephyr_library_get_current_dir_lib_name
710 #
711 # But code from test/ and lib/ that is placed in the "zephyr"
712 # library (with zephyr_sources()) will not be in a library that is
713 # prefixed with lib__ or test__ and will end up in the wrong address
714 # space.
715 set(application_space_dirs
716 lib
717 tests
718 )
719 foreach(f ${kernel_object_file_list})
720 foreach(app_dir ${application_space_dirs})
721 if(${f} MATCHES "^${app_dir}__") # Begins with ${app_dir}__, e.g. lib__libc
722 list(
723 REMOVE_ITEM
724 kernel_object_file_list
725 ${f}
726 )
727 endif()
728 endforeach()
729 endforeach()
730
731 # Create a list ks, with relative paths to kernel space libs.
732 foreach(f ${kernel_object_file_list})
733 get_target_property(target_name ${f} NAME)
734 get_target_property(target_binary_dir ${f} BINARY_DIR)
735
736 string(REPLACE
737 ${PROJECT_BINARY_DIR}
738 ""
739 fixed_path
740 ${target_binary_dir}
741 )
742
743 # Append / if not empty
744 if(fixed_path)
745 set(fixed_path "${fixed_path}/")
746 endif()
747
748 # Cut off leading / if present
749 if(fixed_path MATCHES "^/.+")
750 string(SUBSTRING ${fixed_path} 1 -1 fixed_path)
751 endif()
752
Sebastian Bøe1c2de102018-01-02 15:54:16 +0100753 set(fixed_path "${fixed_path}lib${target_name}.a")
754
755 if(CMAKE_GENERATOR STREQUAL "Ninja")
756 # Ninja invokes the linker from the root of the build directory
757 # (APPLICATION_BINARY_DIR) instead of from the build/zephyr
758 # directory (PROJECT_BINARY_DIR). So for linker-defs.h to get
759 # the correct path we need to prefix with zephyr/.
760 set(fixed_path "zephyr/${fixed_path}")
761 endif()
762
763 list(APPEND ks ${fixed_path})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200764 endforeach()
765
Sebastian Bøecbe7b4f2018-08-03 16:15:05 +0200766 # We are done constructing kernel_object_file_list, now we inject
767 # this list into the linker script through the define
768 # KERNELSPACE_OBJECT_FILES
769 set(def -DKERNELSPACE_OBJECT_FILES=)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200770 foreach(f ${ks})
Sebastian Bøecbe7b4f2018-08-03 16:15:05 +0200771 set(def "${def} ${f}")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200772 endforeach()
Håkon Øye Amundsen611b0d12018-11-29 09:15:33 +0000773 set_property(GLOBAL APPEND PROPERTY
774 PROPERTY_LINKER_SCRIPT_DEFINES
775 ${def}
776 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200777endif() # CONFIG_APPLICATION_MEMORY
778
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100779
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100780
Andy Gross1f0ff062018-01-25 11:07:03 -0600781construct_add_custom_command_for_linker_pass(linker custom_command)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200782add_custom_command(
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100783 ${custom_command}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200784)
Andy Grosse8860fe2018-02-01 01:12:32 -0600785
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200786add_custom_target(
787 linker_script
788 DEPENDS
Chunlin Han18560a02018-02-01 01:19:49 -0600789 ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP}
Adithya Baglodyc69fb0d2018-08-04 19:48:52 +0530790 ${APP_SMEM_DEP}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200791 linker.cmd
792 offsets_h
Sebastian Bøeb1ab5012017-12-14 13:03:23 +0100793 )
794
795# Give the 'linker_script' target all of the include directories so
796# that cmake can successfully find the linker_script's header
797# dependencies.
798zephyr_get_include_directories_for_lang(C
799 ZEPHYR_INCLUDE_DIRS
800 STRIP_PREFIX # Don't use a -I prefix
801 )
802set_property(TARGET
803 linker_script
804 PROPERTY INCLUDE_DIRECTORIES
805 ${ZEPHYR_INCLUDE_DIRS}
806 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200807
808set(zephyr_lnk
809 ${LINKERFLAGPREFIX},-Map=${PROJECT_BINARY_DIR}/${KERNEL_MAP_NAME}
810 -u_OffsetAbsSyms
811 -u_ConfigAbsSyms
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200812 ${LINKERFLAGPREFIX},--whole-archive
813 ${ZEPHYR_LIBS_PROPERTY}
814 ${LINKERFLAGPREFIX},--no-whole-archive
815 kernel
Anas Nashif644b31d2018-11-17 21:15:14 -0500816 ${OFFSETS_O_PATH}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200817 ${LIB_INCLUDE_DIR}
818 -L${PROJECT_BINARY_DIR}
819 ${TOOLCHAIN_LIBS}
820 )
821
822if(CONFIG_GEN_ISR_TABLES)
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530823 if(CONFIG_GEN_SW_ISR_TABLE)
824 list(APPEND GEN_ISR_TABLE_EXTRA_ARG --sw-isr-table)
825 endif()
826
827 if(CONFIG_GEN_IRQ_VECTOR_TABLE)
828 list(APPEND GEN_ISR_TABLE_EXTRA_ARG --vector-table)
829 endif()
830
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200831 # isr_tables.c is generated from zephyr_prebuilt by
832 # gen_isr_tables.py
833 add_custom_command(
834 OUTPUT isr_tables.c
835 COMMAND ${CMAKE_OBJCOPY}
836 -I ${OUTPUT_FORMAT}
837 -O binary
838 --only-section=.intList
839 $<TARGET_FILE:zephyr_prebuilt>
840 isrList.bin
841 COMMAND ${PYTHON_EXECUTABLE}
Carles Cufi7d764b32018-01-11 15:46:44 +0100842 ${ZEPHYR_BASE}/arch/common/gen_isr_tables.py
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200843 --output-source isr_tables.c
Rajavardhan Gundi1e6adba2017-12-24 15:18:57 +0530844 --kernel $<TARGET_FILE:zephyr_prebuilt>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200845 --intlist isrList.bin
Yasushi SHOJI6fc0d772018-10-09 18:59:16 +0900846 $<$<BOOL:${CONFIG_BIG_ENDIAN}>:--big-endian>
Sebastian Bøea55279a2018-01-04 14:08:39 +0100847 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--debug>
Rajavardhan Gundi08172cd2017-12-12 23:29:37 +0530848 ${GEN_ISR_TABLE_EXTRA_ARG}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200849 DEPENDS zephyr_prebuilt
850 )
851 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_SOURCE_FILES isr_tables.c)
852endif()
853
Chunlin Han18560a02018-02-01 01:19:49 -0600854if(CONFIG_ARM AND CONFIG_USERSPACE)
855 set(GEN_PRIV_STACKS $ENV{ZEPHYR_BASE}/scripts/gen_priv_stacks.py)
856 set(PROCESS_PRIV_STACKS_GPERF $ENV{ZEPHYR_BASE}/scripts/process_gperf.py)
857
858 set(PRIV_STACKS priv_stacks_hash.gperf)
859 set(PRIV_STACKS_OUTPUT_SRC_PRE priv_stacks_hash_preprocessed.c)
860 set(PRIV_STACKS_OUTPUT_SRC priv_stacks_hash.c)
861 set(PRIV_STACKS_OUTPUT_OBJ priv_stacks_hash.c.obj)
862 set(PRIV_STACKS_OUTPUT_OBJ_RENAMED priv_stacks_hash_renamed.o)
863
864 # Essentially what we are doing here is extracting some information
865 # out of the nearly finished elf file, generating the source code
866 # for a hash table based on that information, and then compiling and
867 # linking the hash table back into a now even more nearly finished
868 # elf file.
869
870 # Use the script GEN_PRIV_STACKS to scan the kernel binary's
871 # (zephyr_prebuilt) DWARF information to produce a table of kernel
872 # objects (PRIV_STACKS) which we will then pass to gperf
873 add_custom_command(
874 OUTPUT ${PRIV_STACKS}
875 COMMAND
876 ${PYTHON_EXECUTABLE}
877 ${GEN_PRIV_STACKS}
878 --kernel $<TARGET_FILE:priv_stacks_prebuilt>
879 --output ${PRIV_STACKS}
880 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
881 DEPENDS priv_stacks_prebuilt
882 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
883 )
884 add_custom_target(priv_stacks DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS})
885
886 # Use gperf to generate C code (PRIV_STACKS_OUTPUT_SRC_PRE) which implements a
887 # perfect hashtable based on PRIV_STACKS
888 add_custom_command(
889 OUTPUT ${PRIV_STACKS_OUTPUT_SRC_PRE}
890 COMMAND
891 ${GPERF} -C
892 --output-file ${PRIV_STACKS_OUTPUT_SRC_PRE}
893 ${PRIV_STACKS}
Andy Gross878f39c2018-05-01 01:10:26 -0500894 DEPENDS priv_stacks ${PRIV_STACKS}
Chunlin Han18560a02018-02-01 01:19:49 -0600895 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
896 )
897 add_custom_target(priv_stacks_output_src_pre DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_SRC_PRE})
898
899 # For our purposes the code/data generated by gperf is not optimal.
900 #
901 # The script PROCESS_GPERF creates a new c file OUTPUT_SRC based on
902 # OUTPUT_SRC_PRE to greatly reduce the amount of code/data generated
903 # since we know we are always working with pointer values
904 add_custom_command(
905 OUTPUT ${PRIV_STACKS_OUTPUT_SRC}
906 COMMAND
Sebastian Bøe1b600702018-06-21 14:34:42 +0200907 ${PYTHON_EXECUTABLE}
Chunlin Han18560a02018-02-01 01:19:49 -0600908 ${PROCESS_PRIV_STACKS_GPERF}
909 -i ${PRIV_STACKS_OUTPUT_SRC_PRE}
910 -o ${PRIV_STACKS_OUTPUT_SRC}
911 -p "struct _k_priv_stack_map"
912 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Andy Gross878f39c2018-05-01 01:10:26 -0500913 DEPENDS priv_stacks_output_src_pre ${PRIV_STACKS_OUTPUT_SRC_PRE}
Chunlin Han18560a02018-02-01 01:19:49 -0600914 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
915 )
916 add_custom_target(priv_stacks_output_src DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_SRC})
917
918 # We need precise control of where generated text/data ends up in the final
919 # kernel image. Disable function/data sections and use objcopy to move
920 # generated data into special section names
921 add_library(priv_stacks_output_lib STATIC
922 ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_SRC}
923 )
924
925 target_link_libraries(priv_stacks_output_lib zephyr_interface)
926
927 # Turn off -ffunction-sections, etc.
928 # NB: Using a library instead of target_compile_options(priv_stacks_output_lib
929 # [...]) because a library's options have precedence
930 add_library(priv_stacks_output_lib_interface INTERFACE)
931 target_compile_options(priv_stacks_output_lib_interface INTERFACE
932 -fno-function-sections
933 -fno-data-sections
934 )
935 target_link_libraries(priv_stacks_output_lib priv_stacks_output_lib_interface)
936
937 set(PRIV_STACKS_OUTPUT_OBJ_PATH ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/priv_stacks_output_lib.dir/${PRIV_STACKS_OUTPUT_OBJ})
938
939 add_custom_command(
940 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_OBJ_RENAMED}
941 COMMAND
942 ${CMAKE_OBJCOPY}
943 --rename-section .bss=.priv_stacks.noinit
944 --rename-section .data=.priv_stacks.data
945 --rename-section .text=.priv_stacks.text
946 --rename-section .rodata=.priv_stacks.rodata
947 ${PRIV_STACKS_OUTPUT_OBJ_PATH}
948 ${PRIV_STACKS_OUTPUT_OBJ_RENAMED}
949 DEPENDS priv_stacks_output_lib
950 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
951 )
952 add_custom_target(priv_stacks_output_obj_renamed DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_OBJ_RENAMED})
953
954 add_library(priv_stacks_output_obj_renamed_lib STATIC IMPORTED GLOBAL)
955 set_property(
956 TARGET priv_stacks_output_obj_renamed_lib
957 PROPERTY
958 IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_OBJ_RENAMED}
959 )
960 add_dependencies(
961 priv_stacks_output_obj_renamed_lib
962 priv_stacks_output_obj_renamed
963 )
964
965 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_OBJECT_FILES priv_stacks_output_obj_renamed_lib)
966endif()
967
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200968if(CONFIG_USERSPACE)
Carles Cufi7d764b32018-01-11 15:46:44 +0100969 set(GEN_KOBJ_LIST ${ZEPHYR_BASE}/scripts/gen_kobject_list.py)
970 set(PROCESS_GPERF ${ZEPHYR_BASE}/scripts/process_gperf.py)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200971
972 set(OBJ_LIST kobject_hash.gperf)
973 set(OUTPUT_SRC_PRE kobject_hash_preprocessed.c)
974 set(OUTPUT_SRC kobject_hash.c)
975 set(OUTPUT_OBJ kobject_hash.c.obj)
976 set(OUTPUT_OBJ_RENAMED kobject_hash_renamed.o)
977
978 # Essentially what we are doing here is extracting some information
979 # out of the nearly finished elf file, generating the source code
980 # for a hash table based on that information, and then compiling and
981 # linking the hash table back into a now even more nearly finished
982 # elf file.
983
984 # Use the script GEN_KOBJ_LIST to scan the kernel binary's
985 # (zephyr_prebuilt) DWARF information to produce a table of kernel
986 # objects (OBJ_LIST) which we will then pass to gperf
987 add_custom_command(
988 OUTPUT ${OBJ_LIST}
989 COMMAND
990 ${PYTHON_EXECUTABLE}
991 ${GEN_KOBJ_LIST}
992 --kernel $<TARGET_FILE:zephyr_prebuilt>
Leandro Pereirac2003672018-04-04 13:50:32 -0700993 --gperf-output ${OBJ_LIST}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200994 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
995 DEPENDS zephyr_prebuilt
996 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
997 )
998 add_custom_target(obj_list DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OBJ_LIST})
999
1000 # Use gperf to generate C code (OUTPUT_SRC_PRE) which implements a
1001 # perfect hashtable based on OBJ_LIST
1002 add_custom_command(
1003 OUTPUT ${OUTPUT_SRC_PRE}
1004 COMMAND
1005 ${GPERF}
1006 --output-file ${OUTPUT_SRC_PRE}
1007 ${OBJ_LIST}
Sebastian Bøef5758b52018-01-31 10:42:46 +01001008 DEPENDS obj_list ${OBJ_LIST}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001009 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1010 )
1011 add_custom_target(output_src_pre DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_SRC_PRE})
1012
1013 # For our purposes the code/data generated by gperf is not optimal.
1014 #
1015 # The script PROCESS_GPERF creates a new c file OUTPUT_SRC based on
1016 # OUTPUT_SRC_PRE to greatly reduce the amount of code/data generated
1017 # since we know we are always working with pointer values
1018 add_custom_command(
1019 OUTPUT ${OUTPUT_SRC}
1020 COMMAND
Sebastian Bøe1b600702018-06-21 14:34:42 +02001021 ${PYTHON_EXECUTABLE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001022 ${PROCESS_GPERF}
1023 -i ${OUTPUT_SRC_PRE}
1024 -o ${OUTPUT_SRC}
Chunlin Han18560a02018-02-01 01:19:49 -06001025 -p "struct _k_object"
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001026 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Sebastian Bøef5758b52018-01-31 10:42:46 +01001027 DEPENDS output_src_pre ${OUTPUT_SRC_PRE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001028 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1029 )
1030 add_custom_target(output_src DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_SRC})
1031
1032 # We need precise control of where generated text/data ends up in the final
1033 # kernel image. Disable function/data sections and use objcopy to move
1034 # generated data into special section names
1035 add_library(output_lib STATIC
1036 ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_SRC}
1037 )
1038
Adithya Baglodyce9a5a22018-01-29 15:58:51 +05301039 # always compile kobject_hash.c at optimization -Os
1040 set_source_files_properties(${OUTPUT_SRC} PROPERTIES COMPILE_FLAGS -Os)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001041 target_link_libraries(output_lib zephyr_interface)
1042
1043 # Turn off -ffunction-sections, etc.
1044 # NB: Using a library instead of target_compile_options(output_lib
1045 # [...]) because a library's options have precedence
1046 add_library(output_lib_interface INTERFACE)
1047 target_compile_options(output_lib_interface INTERFACE
1048 -fno-function-sections
1049 -fno-data-sections
1050 )
1051 target_link_libraries(output_lib output_lib_interface)
1052
1053 set(OUTPUT_OBJ_PATH ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/output_lib.dir/${OUTPUT_OBJ})
1054
1055 add_custom_command(
1056 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_OBJ_RENAMED}
1057 COMMAND
1058 ${CMAKE_OBJCOPY}
1059 --rename-section .data=.kobject_data.data
1060 --rename-section .text=.kobject_data.text
1061 --rename-section .rodata=.kobject_data.rodata
1062 ${OUTPUT_OBJ_PATH}
1063 ${OUTPUT_OBJ_RENAMED}
1064 DEPENDS output_lib
1065 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
1066 )
1067 add_custom_target(output_obj_renamed DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_OBJ_RENAMED})
1068
1069 add_library(output_obj_renamed_lib STATIC IMPORTED GLOBAL)
1070 set_property(
1071 TARGET output_obj_renamed_lib
1072 PROPERTY
1073 IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_OBJ_RENAMED}
1074 )
1075 add_dependencies(
1076 output_obj_renamed_lib
1077 output_obj_renamed
1078 )
1079
1080 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_OBJECT_FILES output_obj_renamed_lib)
1081endif()
1082
1083# Read global variables into local variables
1084get_property(GKOF GLOBAL PROPERTY GENERATED_KERNEL_OBJECT_FILES)
1085get_property(GKSF GLOBAL PROPERTY GENERATED_KERNEL_SOURCE_FILES)
1086
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +02001087
Alberto Escolar Piedrasc2882412018-07-05 10:51:03 +02001088get_property(CSTD GLOBAL PROPERTY CSTD)
1089set_ifndef(CSTD c99)
1090
1091zephyr_compile_options(
1092 $<$<COMPILE_LANGUAGE:C>:-std=${CSTD}>
1093)
1094
Andy Grosse8860fe2018-02-01 01:12:32 -06001095configure_file(
Ioannis Glaropoulosc15c4912018-11-29 14:26:01 +01001096 $ENV{ZEPHYR_BASE}/include/arch/arc/v2/app_data_alignment.ld
Andy Grosse8860fe2018-02-01 01:12:32 -06001097 ${PROJECT_BINARY_DIR}/include/generated/app_data_alignment.ld)
1098
Shawn Mosley573f32b2018-04-26 10:14:02 -04001099configure_file(
1100 $ENV{ZEPHYR_BASE}/include/arch/arm/cortex_m/scripts/app_smem.ld
1101 ${PROJECT_BINARY_DIR}/include/generated/app_smem.ld)
1102
Andy Grosse8860fe2018-02-01 01:12:32 -06001103if(CONFIG_CPU_HAS_MPU AND CONFIG_USERSPACE)
1104
Shawn Mosley573f32b2018-04-26 10:14:02 -04001105 if(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT AND CONFIG_APP_SHARED_MEM)
Shawn Mosley573f32b2018-04-26 10:14:02 -04001106 set(APP_SMEM_LD "${PROJECT_BINARY_DIR}/include/generated/app_smem.ld")
1107 set(OBJ_FILE_DIR "${PROJECT_BINARY_DIR}/../")
1108
1109 add_custom_target(
1110 ${APP_SMEM_DEP} ALL
Adithya Baglodyc69fb0d2018-08-04 19:48:52 +05301111 DEPENDS app
Adithya Baglodyc764e022018-09-19 11:28:27 +05301112 kernel
Shawn Mosley573f32b2018-04-26 10:14:02 -04001113 )
1114
1115 add_custom_command(
1116 TARGET ${APP_SMEM_DEP}
Adithya Baglodyc69fb0d2018-08-04 19:48:52 +05301117 COMMAND ${PYTHON_EXECUTABLE}
1118 ${ZEPHYR_BASE}/scripts/gen_app_partitions.py
Shawn Mosley573f32b2018-04-26 10:14:02 -04001119 -d ${OBJ_FILE_DIR}
1120 -o ${APP_SMEM_LD}
Adithya Baglodyc69fb0d2018-08-04 19:48:52 +05301121 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Shawn Mosley573f32b2018-04-26 10:14:02 -04001122 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
1123 COMMENT "Generating power of 2 aligned app_smem linker section"
1124 )
1125 endif()
1126
1127
Ioannis Glaropoulosc15c4912018-11-29 14:26:01 +01001128 if(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT AND CONFIG_ARC AND CONFIG_APPLICATION_MEMORY)
Andy Grosse8860fe2018-02-01 01:12:32 -06001129
1130 construct_add_custom_command_for_linker_pass(linker_app_sizing custom_command)
1131 add_custom_command(
1132 ${custom_command}
1133 )
1134
1135 add_custom_target(
1136 linker_app_sizing_script
1137 DEPENDS
1138 linker_app_sizing.cmd
1139 offsets_h
Adithya Baglodyc764e022018-09-19 11:28:27 +05301140 ${APP_SMEM_DEP}
Andy Grosse8860fe2018-02-01 01:12:32 -06001141 )
1142
1143 set_property(TARGET
1144 linker_app_sizing_script
1145 PROPERTY INCLUDE_DIRECTORIES
1146 ${ZEPHYR_INCLUDE_DIRS}
1147 )
1148
1149 # For systems with MPUs, the size of the application data section must
1150 # be determined so that MPU alignment requirements can be met.
1151 # Create a app_sizing_prebuilt target so we can do this before the
1152 # other ELF files are built
1153 set(GEN_APP_ALIGN $ENV{ZEPHYR_BASE}/scripts/gen_alignment_script.py)
1154 add_executable( app_sizing_prebuilt misc/empty_file.c)
1155 target_link_libraries(app_sizing_prebuilt ${TOPT} ${PROJECT_BINARY_DIR}/linker_app_sizing.cmd ${zephyr_lnk})
1156 set_property(TARGET app_sizing_prebuilt PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker_app_sizing.cmd)
Shawn Mosley573f32b2018-04-26 10:14:02 -04001157 add_dependencies( app_sizing_prebuilt linker_app_sizing_script offsets )
Andy Grosse8860fe2018-02-01 01:12:32 -06001158
1159 add_custom_command(
1160 TARGET app_sizing_prebuilt
1161 POST_BUILD
1162 COMMAND ${PYTHON_EXECUTABLE} ${GEN_APP_ALIGN}
1163 --output ./include/generated/app_data_alignment.ld
1164 --kernel $<TARGET_FILE:app_sizing_prebuilt>
1165 VERBATIM
1166 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
1167 )
1168 endif()
Chunlin Han18560a02018-02-01 01:19:49 -06001169
Wayne Ren5a0ba2f2018-02-12 19:17:04 +08001170if(CONFIG_ARM)
Chunlin Han18560a02018-02-01 01:19:49 -06001171 construct_add_custom_command_for_linker_pass(linker_priv_stacks custom_command)
1172 add_custom_command(
1173 ${custom_command}
1174 )
1175
1176 add_custom_target(
1177 linker_priv_stacks_script
1178 DEPENDS
Adithya Baglodyd4b5ab62018-09-18 11:52:52 +05301179 ${ALIGN_SIZING_DEP} ${APP_SMEM_DEP}
Chunlin Han18560a02018-02-01 01:19:49 -06001180 linker_priv_stacks.cmd
1181 offsets_h
1182 )
1183
1184 set_property(TARGET
1185 linker_priv_stacks_script
1186 PROPERTY INCLUDE_DIRECTORIES
1187 ${ZEPHYR_INCLUDE_DIRS}
1188 )
1189
1190 set(PRIV_STACK_LIB priv_stacks_output_obj_renamed_lib)
1191 add_executable( priv_stacks_prebuilt misc/empty_file.c)
1192 target_link_libraries(priv_stacks_prebuilt ${TOPT} ${PROJECT_BINARY_DIR}/linker_priv_stacks.cmd ${zephyr_lnk})
1193 set_property(TARGET priv_stacks_prebuilt PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker_priv_stacks.cmd)
1194 add_dependencies( priv_stacks_prebuilt ${ALIGN_SIZING_DEP} linker_priv_stacks_script offsets)
Wayne Ren5a0ba2f2018-02-12 19:17:04 +08001195endif()
Chunlin Han18560a02018-02-01 01:19:49 -06001196
Andy Grosse8860fe2018-02-01 01:12:32 -06001197endif()
1198
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001199# FIXME: Is there any way to get rid of empty_file.c?
1200add_executable( zephyr_prebuilt misc/empty_file.c)
Chunlin Han18560a02018-02-01 01:19:49 -06001201target_link_libraries(zephyr_prebuilt ${TOPT} ${PROJECT_BINARY_DIR}/linker.cmd ${PRIV_STACK_LIB} ${zephyr_lnk})
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001202set_property(TARGET zephyr_prebuilt PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker.cmd)
Chunlin Han18560a02018-02-01 01:19:49 -06001203add_dependencies( zephyr_prebuilt ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP} linker_script offsets)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001204
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +02001205
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001206if(GKOF OR GKSF)
1207 set(logical_target_for_zephyr_elf kernel_elf)
1208
1209 # The second linker pass uses the same source linker script of the
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +01001210 # first pass (LINKER_SCRIPT), but this time with a different output
1211 # file and preprocessed with the define LINKER_PASS2.
Andy Gross1f0ff062018-01-25 11:07:03 -06001212 construct_add_custom_command_for_linker_pass(linker_pass_final custom_command)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001213 add_custom_command(
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +01001214 ${custom_command}
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001215 )
Andy Grosse8860fe2018-02-01 01:12:32 -06001216
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001217 add_custom_target(
Andy Gross1f0ff062018-01-25 11:07:03 -06001218 linker_pass_final_script
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001219 DEPENDS
Chunlin Han18560a02018-02-01 01:19:49 -06001220 ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP}
Andy Grosse8860fe2018-02-01 01:12:32 -06001221 zephyr_prebuilt
Andy Gross1f0ff062018-01-25 11:07:03 -06001222 linker_pass_final.cmd
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001223 offsets_h
1224 )
Sebastian Bøeb1ab5012017-12-14 13:03:23 +01001225 set_property(TARGET
Andy Gross1f0ff062018-01-25 11:07:03 -06001226 linker_pass_final_script
Sebastian Bøeb1ab5012017-12-14 13:03:23 +01001227 PROPERTY INCLUDE_DIRECTORIES
1228 ${ZEPHYR_INCLUDE_DIRS}
1229 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001230
1231 add_executable( kernel_elf misc/empty_file.c ${GKSF})
Andy Gross1f0ff062018-01-25 11:07:03 -06001232 target_link_libraries(kernel_elf ${GKOF} ${TOPT} ${PROJECT_BINARY_DIR}/linker_pass_final.cmd ${zephyr_lnk})
1233 set_property(TARGET kernel_elf PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker_pass_final.cmd)
Adithya Baglodyc69fb0d2018-08-04 19:48:52 +05301234 add_dependencies( kernel_elf ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP} linker_pass_final_script)
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001235else()
1236 set(logical_target_for_zephyr_elf zephyr_prebuilt)
1237 # Use the prebuilt elf as the final elf since we don't have a
1238 # generation stage.
1239endif()
1240
Sebastian Bøe0fc39342018-10-16 13:25:04 +02001241# Export the variable to the application's scope to allow the
1242# application to know what the name of the final elf target is.
1243set(logical_target_for_zephyr_elf ${logical_target_for_zephyr_elf} PARENT_SCOPE)
1244
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001245# To avoid having the same logical target name for the zephyr lib and
1246# the zephyr elf, we set the kernel_elf file name to zephyr.elf.
1247set_target_properties(${logical_target_for_zephyr_elf} PROPERTIES OUTPUT_NAME ${KERNEL_NAME})
1248
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001249set(post_build_commands "")
1250
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001251list_append_ifdef(CONFIG_CHECK_LINK_MAP
1252 post_build_commands
Carles Cufi7d764b32018-01-11 15:46:44 +01001253 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/check_link_map.py ${KERNEL_MAP_NAME}
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001254 )
1255
Håkon Øye Amundsenc086b932018-11-26 09:47:16 +00001256if(NOT CONFIG_BUILD_NO_GAP_FILL)
1257 # Use ';' as separator to get proper space in resulting command.
1258 set(GAP_FILL "--gap-fill;0xff")
1259endif()
1260
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001261list_append_ifdef(
1262 CONFIG_BUILD_OUTPUT_HEX
1263 post_build_commands
Håkon Øye Amundsenc086b932018-11-26 09:47:16 +00001264 COMMAND ${CMAKE_OBJCOPY} -S -Oihex ${GAP_FILL} -R .comment -R COMMON -R .eh_frame ${KERNEL_ELF_NAME} ${KERNEL_HEX_NAME}
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001265 )
1266
1267list_append_ifdef(
1268 CONFIG_BUILD_OUTPUT_BIN
1269 post_build_commands
Håkon Øye Amundsenc086b932018-11-26 09:47:16 +00001270 COMMAND ${CMAKE_OBJCOPY} -S -Obinary ${GAP_FILL} -R .comment -R COMMON -R .eh_frame ${KERNEL_ELF_NAME} ${KERNEL_BIN_NAME}
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001271 )
Anas Nashif4592ff22017-11-23 07:54:26 -05001272
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001273list_append_ifdef(
1274 CONFIG_BUILD_OUTPUT_S19
1275 post_build_commands
Håkon Øye Amundsenc086b932018-11-26 09:47:16 +00001276 COMMAND ${CMAKE_OBJCOPY} ${GAP_FILL} --srec-len 1 --output-target=srec ${KERNEL_ELF_NAME} ${KERNEL_S19_NAME}
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001277 )
1278
1279list_append_ifdef(
Anas Nashif1f1143a2017-11-22 13:03:53 -05001280 CONFIG_OUTPUT_DISASSEMBLY
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001281 post_build_commands
1282 COMMAND ${CMAKE_OBJDUMP} -S ${KERNEL_ELF_NAME} > ${KERNEL_LST_NAME}
1283 )
1284
1285list_append_ifdef(
Anas Nashif1f1143a2017-11-22 13:03:53 -05001286 CONFIG_OUTPUT_STAT
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001287 post_build_commands
1288 COMMAND ${CMAKE_READELF} -e ${KERNEL_ELF_NAME} > ${KERNEL_STAT_NAME}
1289 )
1290
1291list_append_ifdef(
1292 CONFIG_BUILD_OUTPUT_STRIPPED
1293 post_build_commands
1294 COMMAND ${CMAKE_STRIP} --strip-all ${KERNEL_ELF_NAME} -o ${KERNEL_STRIP_NAME}
1295 )
1296
Anas Nashif4592ff22017-11-23 07:54:26 -05001297list_append_ifdef(
1298 CONFIG_BUILD_OUTPUT_EXE
1299 post_build_commands
Anas Nashif951393f2018-10-16 10:54:53 -04001300 COMMAND ${CMAKE_COMMAND} -E copy ${KERNEL_ELF_NAME} ${KERNEL_EXE_NAME}
Anas Nashif4592ff22017-11-23 07:54:26 -05001301 )
1302
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001303add_custom_command(
1304 TARGET ${logical_target_for_zephyr_elf}
1305 POST_BUILD
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001306 ${post_build_commands}
1307 COMMENT "Generating files from zephyr.elf for board: ${BOARD}"
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001308 # NB: COMMENT only works for some CMake-Generators
1309)
1310
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001311# To populate with hex files to merge, do the following:
1312# set_property(GLOBAL APPEND PROPERTY HEX_FILES_TO_MERGE ${my_local_list})
1313# Note that the zephyr.hex file will not be included automatically.
1314get_property(HEX_FILES_TO_MERGE GLOBAL PROPERTY HEX_FILES_TO_MERGE)
1315if(HEX_FILES_TO_MERGE)
1316 # Merge in out-of-tree hex files.
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001317 set(MERGED_HEX_NAME merged.hex)
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001318
1319 add_custom_command(
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001320 OUTPUT ${MERGED_HEX_NAME}
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001321 COMMAND
1322 ${PYTHON_EXECUTABLE}
1323 ${ZEPHYR_BASE}/scripts/mergehex.py
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001324 -o ${MERGED_HEX_NAME}
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001325 ${HEX_FILES_TO_MERGE}
1326 DEPENDS ${HEX_FILES_TO_MERGE} ${logical_target_for_zephyr_elf}
1327 )
1328
Håkon Øye Amundsen2a5a02e2018-12-05 08:32:32 +00001329 add_custom_target(mergehex ALL DEPENDS ${MERGED_HEX_NAME})
Håkon Øye Amundsen0da5d242018-12-05 09:15:40 +00001330 list(APPEND FLASH_DEPS mergehex)
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001331endif()
1332
Sebastian Bøedbdd7222017-12-19 13:20:10 +01001333if(CONFIG_OUTPUT_PRINT_MEMORY_USAGE)
1334 # Use --print-memory-usage with the first link.
1335 #
1336 # Don't use this option with the second link because seeing it twice
1337 # could confuse users and using it on the second link would suppress
1338 # it when the first link has a ram/flash-usage issue.
1339 set(option ${LINKERFLAGPREFIX},--print-memory-usage)
1340 string(MAKE_C_IDENTIFIER check${option} check)
Sebastian Bøeba0b2832017-12-21 10:16:43 +01001341
Sebastian Bøec34b7a32017-12-27 15:21:54 +01001342 set(SAVED_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
Sebastian Bøeba0b2832017-12-21 10:16:43 +01001343 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${option}")
Sebastian Bøe71b849f2018-04-17 15:08:58 +02001344 zephyr_check_compiler_flag(C "" ${check})
Sebastian Bøec34b7a32017-12-27 15:21:54 +01001345 set(CMAKE_REQUIRED_FLAGS ${SAVED_CMAKE_REQUIRED_FLAGS})
Sebastian Bøeba0b2832017-12-21 10:16:43 +01001346
Sebastian Bøedbdd7222017-12-19 13:20:10 +01001347 target_link_libraries_ifdef(${check} zephyr_prebuilt ${option})
1348endif()
1349
Anas Nashifc15d3c92017-11-21 18:54:55 -05001350if(EMU_PLATFORM)
Carles Cufi7d764b32018-01-11 15:46:44 +01001351 include(${ZEPHYR_BASE}/cmake/emu/${EMU_PLATFORM}.cmake)
Anas Nashiffd276ae2017-12-21 16:45:45 -05001352else()
1353 add_custom_target(run
1354 COMMAND
1355 ${CMAKE_COMMAND} -E echo
1356 "==================================================="
1357 "Emulation/Simulation not supported with this board."
1358 "==================================================="
1359 )
Anas Nashifc15d3c92017-11-21 18:54:55 -05001360endif()
1361
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001362add_subdirectory(cmake/flash)
1363
1364add_subdirectory(cmake/usage)
1365add_subdirectory(cmake/reports)
1366
Andrew Boie411686f2018-05-24 13:18:36 -07001367if(CONFIG_ASSERT AND (NOT CONFIG_FORCE_NO_ASSERT))
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001368 message(WARNING "
1369 ------------------------------------------------------------
1370 --- WARNING: __ASSERT() statements are globally ENABLED ---
Ioannis Glaropoulosf90416c2018-05-31 11:05:12 +02001371 --- The kernel will run more slowly and use more memory ---
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001372 ------------------------------------------------------------"
1373)
1374endif()
1375
1376if(CONFIG_BOARD_DEPRECATED)
1377 message(WARNING "
1378 WARNING: The board '${BOARD}' is deprecated and will be
1379 removed in version ${CONFIG_BOARD_DEPRECATED}"
1380)
1381endif()
Andrew Boiedf48e112018-01-12 09:54:24 -08001382
1383if(CONFIG_X86 AND CONFIG_USERSPACE AND NOT CONFIG_X86_NO_MELTDOWN)
1384 message(WARNING "
1385 WARNING: You have enabled CONFIG_USERSPACE on an x86-based target.
1386 If your CPU is vulnerable to the Meltdown CPU bug, security of
1387 supervisor-only memory pages is not guaranteed. This version of Zephyr
1388 does not contain a fix for this issue."
1389)
1390endif()