blob: 00b0331086546b99830682c8696559fe834e5747 [file] [log] [blame]
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001project(Zephyr-Kernel VERSION ${PROJECT_VERSION})
2enable_language(C CXX ASM)
3
4# *DOCUMENTATION*
5#
6# Note that this is *NOT* the top-level CMakeLists.txt. That's in the
7# application. See the Application Development Primer documentation
8# for details.
9#
10# To see a list of typical targets execute "make usage"
11# More info can be located in ./README.rst
12# Comments in this file are targeted only to the developer, do not
13# expect to learn how to build the kernel reading this file.
14
15# Verify that the toolchain can compile a dummy file, if it is not we
16# won't be able to test for compatiblity with certain C flags.
17check_c_compiler_flag("" toolchain_is_ok)
18assert(toolchain_is_ok "The toolchain is unable to build a dummy C file. See CMakeError.log.")
19
Sebastian Bøe12f8f762017-10-27 15:43:34 +020020set(CMAKE_EXECUTABLE_SUFFIX .elf)
21
Sebastian Bøe12f8f762017-10-27 15:43:34 +020022if(NOT PROPERTY_LINKER_SCRIPT_DEFINES)
23 set_property(GLOBAL PROPERTY PROPERTY_LINKER_SCRIPT_DEFINES -D__GCC_LINKER_CMD__)
24endif()
25
26define_property(GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT BRIEF_DOCS " " FULL_DOCS " ")
27set_property( GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT elf32-little${ARCH})
28
29# zephyr_interface is a source-less library that has all the global
30# compiler options needed by all source files. All zephyr libraries,
31# including the library named "zephyr" link with this library to
32# obtain these flags.
33add_library(zephyr_interface INTERFACE)
34
35# zephyr is a catchall CMake library for source files that can be
36# built purely with the include paths, defines, and other compiler
37# flags that come with zephyr_interface.
38zephyr_library_named(zephyr)
39
40zephyr_include_directories(
41 kernel/include
42 arch/${ARCH}/include
43 arch/${ARCH}/soc/${SOC_PATH}
44 arch/${ARCH}/soc/${SOC_PATH}/include
45 arch/${ARCH}/soc/${SOC_FAMILY}/include
46 ${BOARD_DIR}
47 include
48 include/drivers
49 ${PROJECT_BINARY_DIR}/include/generated
50 ${USERINCLUDE}
51 ${STDINCLUDE}
52)
53
Sebastian Bøe12f8f762017-10-27 15:43:34 +020054zephyr_compile_definitions(
55 KERNEL
56 __ZEPHYR__=1
Anas Nashif34aebad2018-01-03 12:26:19 -050057)
58
59if(NOT CONFIG_COVERAGE)
60zephyr_compile_definitions(
Sebastian Bøe12f8f762017-10-27 15:43:34 +020061 _FORTIFY_SOURCE=2
62)
Anas Nashif34aebad2018-01-03 12:26:19 -050063endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +020064
65# We need to set an optimization level.
66# Default to -Os
Alberto Escolar Piedrasf60527a2018-01-22 15:35:54 +010067# unless CONFIG_NO_OPTIMIZATIONS is set, then it is -O0
68# or unless CONFIG_DEBUG is set, then it is -Og
Sebastian Bøe12f8f762017-10-27 15:43:34 +020069#
70# also, some toolchain's break with -Os, and some toolchain's break
71# with -Og so allow them to override what flag to use
72#
73# Finally, the user can use Kconfig to add compiler options that will
74# come after these options and override them
Alberto Escolar Piedrasf60527a2018-01-22 15:35:54 +010075set_ifndef(OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG "-O0")
Sebastian Bøe600c8f72018-01-24 10:40:32 +010076set_ifndef(OPTIMIZE_FOR_DEBUG_FLAG "-Og")
77set_ifndef(OPTIMIZE_FOR_SIZE_FLAG "-Os")
78
Alberto Escolar Piedrasf60527a2018-01-22 15:35:54 +010079if(CONFIG_NO_OPTIMIZATIONS)
Sebastian Bøe600c8f72018-01-24 10:40:32 +010080 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG})
81elseif(CONFIG_DEBUG_OPTIMIZATIONS)
Sebastian Bøe12f8f762017-10-27 15:43:34 +020082 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_DEBUG_FLAG})
Sebastian Bøe600c8f72018-01-24 10:40:32 +010083elseif(CONFIG_SIZE_OPTIMIZATIONS)
Sebastian Bøe12f8f762017-10-27 15:43:34 +020084 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SIZE_FLAG}) # Default
Sebastian Bøe600c8f72018-01-24 10:40:32 +010085else()
86 assert(0 "Unreachable code. Expected optimization level to have been chosen. See misc/Kconfig.")
Sebastian Bøe12f8f762017-10-27 15:43:34 +020087endif()
88
89zephyr_compile_options(
90 ${OPTIMIZATION_FLAG} # Usually -Os
91 -g # TODO: build configuration enough?
92 -Wall
93 -Wformat
94 -Wformat-security
95 -Wno-format-zero-length
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +020096 -imacros ${AUTOCONF_H}
Sebastian Bøe12f8f762017-10-27 15:43:34 +020097 -ffreestanding
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +020098 -Wno-main
99 ${NOSTDINC_F}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200100)
101
102zephyr_compile_options(
103 $<$<COMPILE_LANGUAGE:C>:-std=c99>
104
105 $<$<COMPILE_LANGUAGE:CXX>:-std=c++11>
106 $<$<COMPILE_LANGUAGE:CXX>:-fcheck-new>
107 $<$<COMPILE_LANGUAGE:CXX>:-ffunction-sections>
108 $<$<COMPILE_LANGUAGE:CXX>:-fdata-sections>
109 $<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>
110 $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
111
112 $<$<COMPILE_LANGUAGE:ASM>:-xassembler-with-cpp>
113 $<$<COMPILE_LANGUAGE:ASM>:-D_ASMLANGUAGE>
114)
115
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200116if(NOT CONFIG_NATIVE_APPLICATION)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200117zephyr_ld_options(
118 -nostartfiles
119 -nodefaultlibs
120 -nostdlib
121 -static
122 -no-pie
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200123)
124endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200125
126# ==========================================================================
127#
128# cmake -DW=... settings
129#
130# W=1 - warnings that may be relevant and does not occur too often
131# W=2 - warnings that occur quite often but may still be relevant
132# W=3 - the more obscure warnings, can most likely be ignored
133# ==========================================================================
134if(W MATCHES "1")
135 zephyr_compile_options(
136 -Wextra
137 -Wunused
138 -Wno-unused-parameter
139 -Wmissing-declarations
140 -Wmissing-format-attribute
141 -Wold-style-definition
142 )
143 zephyr_cc_option(
144 -Wmissing-prototypes
145 -Wmissing-include-dirs
146 -Wunused-but-set-variable
147 -Wno-missing-field-initializers
148 )
149endif()
150
151if(W MATCHES "2")
152 zephyr_compile_options(
153 -Waggregate-return
154 -Wcast-align
155 -Wdisabled-optimization
156 -Wnested-externs
157 -Wshadow
158 )
159 zephyr_cc_option(
160 -Wlogical-op
161 -Wmissing-field-initializers
162 )
163endif()
164
165if(W MATCHES "3")
166 zephyr_compile_options(
167 -Wbad-function-cast
168 -Wcast-qual
169 -Wconversion
170 -Wpacked
171 -Wpadded
172 -Wpointer-arith
173 -Wredundant-decls
174 -Wswitch-default
175 )
176 zephyr_cc_option(
177 -Wpacked-bitfield-compat
178 -Wvla
179 )
180endif()
181
182# Allow the user to inject options when calling cmake, e.g.
183# 'cmake -DEXTRA_CFLAGS="-Werror -Wno-deprecated-declarations" ..'
Sebastian Bøe9f590452017-11-10 12:22:23 +0100184include(cmake/extra_flags.cmake)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200185
186if(CONFIG_READABLE_ASM)
187 zephyr_cc_option(-fno-reorder-blocks)
188 zephyr_cc_option(-fno-ipa-cp-clone)
189 zephyr_cc_option(-fno-partial-inlining)
190endif()
191
192zephyr_cc_option(-fno-asynchronous-unwind-tables)
193zephyr_cc_option(-fno-pie)
194zephyr_cc_option(-fno-pic)
195zephyr_cc_option(-fno-strict-overflow)
196zephyr_cc_option(-Wno-pointer-sign)
197
Sebastian Bøe703dc592017-11-29 10:19:50 +0100198zephyr_compile_options_ifdef(CONFIG_STACK_CANARIES -fstack-protector-all)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200199
200if(CONFIG_OVERRIDE_FRAME_POINTER_DEFAULT)
201 if(CONFIG_OMIT_FRAME_POINTER)
202 zephyr_cc_option(-fomit-frame-pointer)
203 else()
204 zephyr_cc_option(-fno-omit-frame-pointer)
205 endif()
206endif()
207
208zephyr_compile_options(${CONFIG_COMPILER_OPT})
209
210# TODO: Include arch compiler options at this point.
211
212# TODO: This Clang check is broken
213if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
214 zephyr_cc_option(
215 -Wno-unknown-warning-option
216 -Wno-unused-variable
217 -Wno-format-invalid-specifier
218 -Wno-gnu
219 # comparison of unsigned expression < 0 is always false
220 -Wno-tautological-compare
221 )
222else() # GCC assumed
223 zephyr_cc_option(
224 -Wno-unused-but-set-variable
225 -fno-reorder-functions
226 )
Anas Nashif7ee8bb92018-02-11 14:36:21 -0600227 if(NOT ${ZEPHYR_TOOLCHAIN_VARIANT} STREQUAL "xcc")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200228 zephyr_cc_option(-fno-defer-pop)
229 endif()
230endif()
231
232zephyr_cc_option_ifdef(CONFIG_DEBUG_SECTION_MISMATCH -fno-inline-functions-called-once)
233zephyr_cc_option_ifdef(CONFIG_STACK_USAGE -fstack-usage)
234
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200235zephyr_system_include_directories(${NOSTDINC})
236
237# Force an error when things like SYS_INIT(foo, ...) occur with a missing header.
238zephyr_cc_option(-Werror=implicit-int)
239
240# Prohibit date/time macros, which would make the build non-deterministic
241# cc-option(-Werror=date-time)
242
243# TODO: Archiver arguments
244# ar_option(D)
245
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200246set_ifndef(LINKERFLAGPREFIX -Wl)
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200247
248if(NOT CONFIG_NATIVE_APPLICATION)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200249zephyr_ld_options(
250 ${LINKERFLAGPREFIX},-X
251 ${LINKERFLAGPREFIX},-N
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200252 )
253endif()
254
255zephyr_ld_options(
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200256 ${LINKERFLAGPREFIX},--gc-sections
257 ${LINKERFLAGPREFIX},--build-id=none
258 )
259
260if(CONFIG_HAVE_CUSTOM_LINKER_SCRIPT)
261 set(LINKER_SCRIPT ${APPLICATION_SOURCE_DIR}/${CONFIG_CUSTOM_LINKER_SCRIPT})
262 if(NOT EXISTS LINKER_SCRIPT)
263 set(LINKER_SCRIPT ${CONFIG_CUSTOM_LINKER_SCRIPT})
264 if(NOT EXISTS LINKER_SCRIPT)
265 message(FATAL_ERROR "CONFIG_HAVE_CUSTOM_LINKER_SCRIPT was set, but no linker script was found at '${CONFIG_CUSTOM_LINKER_SCRIPT}'")
266 endif()
267 endif()
268else()
269 # Try a board specific linker file
270 set(LINKER_SCRIPT ${BOARD_DIR}/linker.ld)
271 if(NOT EXISTS ${LINKER_SCRIPT})
272 # If not available, try an SoC specific linker file
Carles Cufi7d764b32018-01-11 15:46:44 +0100273 set(LINKER_SCRIPT ${ZEPHYR_BASE}/arch/${ARCH}/soc/${SOC_PATH}/linker.ld)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200274 endif()
275endif()
276
277if(NOT EXISTS ${LINKER_SCRIPT})
278 message(FATAL_ERROR "Could not find linker script: '${LINKER_SCRIPT}'. Corrupted configuration?")
279endif()
280
Kristian Klomsten Skordal0225e952018-01-30 11:26:42 +0100281# Custom section support in linker scripts requires that the application source
282# directory is in the preprocessor search path, in order to find the custom
283# linker script fragments.
284if(CONFIG_CUSTOM_RODATA_LD OR CONFIG_CUSTOM_RWDATA_LD OR CONFIG_CUSTOM_SECTIONS_LD)
285 zephyr_include_directories(${APPLICATION_SOURCE_DIR})
286endif()
287
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200288configure_file(version.h.in ${PROJECT_BINARY_DIR}/include/generated/version.h)
289
Sebastian Bøe6f946e22018-01-09 10:52:57 +0100290# Unfortunately, the order in which CMakeLists.txt code is processed
291# matters so we need to be careful about how we order the processing
292# of subdirectories. One example is "Compiler flags added late in the
293# build are not exported to external build systems #5605"; when we
294# integrate with an external build system we read out all compiler
295# flags when the external project is created. So an external project
296# defined in subsys or ext will not get global flags added by drivers/
297# or tests/ as the subdirectories are ordered now.
298#
299# Another example of when the order matters is the reading and writing
300# of global properties such as ZEPHYR_LIBS or
301# GENERATED_KERNEL_OBJECT_FILES.
302#
303# Arch is placed early because it defines important compiler flags
304# that must be exported to external build systems defined in
305# e.g. subsys/.
306add_subdirectory(arch)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200307add_subdirectory(lib)
308add_subdirectory(misc)
309# We use include instead of add_subdirectory to avoid creating a new directory scope.
310# This is because source file properties are directory scoped, including the GENERATED
311# property which is set implicitly for custom command outputs
312include(misc/generated/CMakeLists.txt)
313add_subdirectory(boards)
314add_subdirectory(ext)
315add_subdirectory(subsys)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200316add_subdirectory(drivers)
317add_subdirectory(tests)
318
Sebastian Bøe2ead15d2017-11-29 17:46:37 +0100319set(syscall_macros_h ${ZEPHYR_BINARY_DIR}/include/generated/syscall_macros.h)
320
321add_custom_target(syscall_macros_h_target DEPENDS ${syscall_macros_h})
322add_custom_command( OUTPUT ${syscall_macros_h}
323 COMMAND
324 ${PYTHON_EXECUTABLE}
325 ${PROJECT_SOURCE_DIR}/scripts/gen_syscall_header.py
326 > ${syscall_macros_h}
327 DEPENDS ${PROJECT_SOURCE_DIR}/scripts/gen_syscall_header.py
328 )
329
Sebastian Bøe13a68402017-11-20 13:03:55 +0100330# This command is a hack to support commands that are always run. Any
331# target that depends on always_rebuild will always be rebuilt.
332add_custom_command(OUTPUT always_rebuild COMMAND cmake -E echo Building for board ${BOARD})
333
334set(syscall_list_h ${CMAKE_CURRENT_BINARY_DIR}/include/generated/syscall_list.h)
335set(syscalls_json ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls.json)
336
337add_custom_command(
338 OUTPUT
339 ${syscalls_json}
340 COMMAND
341 ${PYTHON_EXECUTABLE}
342 ${PROJECT_SOURCE_DIR}/scripts/parse_syscalls.py
343 --include ${PROJECT_SOURCE_DIR}/include # Read files from this dir
344 --json-file ${syscalls_json} # Write this file
345 DEPENDS always_rebuild
346 )
347
348add_custom_target(syscall_list_h_target DEPENDS ${syscall_list_h})
349add_custom_command(OUTPUT include/generated/syscall_dispatch.c ${syscall_list_h}
350 # Also, some files are written to include/generated/syscalls/
351 COMMAND
352 ${PYTHON_EXECUTABLE}
353 ${PROJECT_SOURCE_DIR}/scripts/gen_syscalls.py
354 --json-file ${syscalls_json} # Read this file
355 --base-output include/generated/syscalls # Write to this dir
356 --syscall-dispatch include/generated/syscall_dispatch.c # Write this file
357 > ${syscall_list_h} # Write stdout to this file
358 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
359 DEPENDS ${syscalls_json}
360 )
361
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200362# Generate offsets.c.obj from offsets.c
363# Generate offsets.h from offsets.c.obj
364
Carles Cufi7d764b32018-01-11 15:46:44 +0100365set(OFFSETS_C_PATH ${ZEPHYR_BASE}/arch/${ARCH}/core/offsets/offsets.c)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200366set(OFFSETS_O_PATH ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/offsets.dir/arch/${ARCH}/core/offsets/offsets.c.obj)
367set(OFFSETS_H_PATH ${PROJECT_BINARY_DIR}/include/generated/offsets.h)
368
Sebastian Bøe13a68402017-11-20 13:03:55 +0100369add_library( offsets STATIC ${OFFSETS_C_PATH})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200370target_link_libraries(offsets zephyr_interface)
Sebastian Bøe13a68402017-11-20 13:03:55 +0100371add_dependencies( offsets
372 syscall_list_h_target
373 syscall_macros_h_target
374 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200375
376add_custom_command(
377 OUTPUT ${OFFSETS_H_PATH}
Carles Cufi7d764b32018-01-11 15:46:44 +0100378 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/gen_offset_header.py
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200379 -i ${OFFSETS_O_PATH}
380 -o ${OFFSETS_H_PATH}
381 DEPENDS offsets
382)
383add_custom_target(offsets_h DEPENDS ${OFFSETS_H_PATH})
384
385zephyr_include_directories(${TOOLCHAIN_INCLUDES})
386
Sebastian Bøe89516fb2017-12-01 15:25:06 +0100387zephyr_get_include_directories_for_lang(C ZEPHYR_INCLUDES)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200388
389add_subdirectory(kernel)
390
391# Read list content
392get_property(ZEPHYR_LIBS_PROPERTY GLOBAL PROPERTY ZEPHYR_LIBS)
393
394foreach(zephyr_lib ${ZEPHYR_LIBS_PROPERTY})
395 # TODO: Could this become an INTERFACE property of zephyr_interface?
396 add_dependencies(${zephyr_lib} offsets_h)
Sebastian Bøe4ece94d2017-12-05 13:22:19 +0100397
Sebastian Bøe64edbaf2018-01-09 12:45:19 +0100398 # Verify that all (non-imported) libraries have source
399 # files. Libraries without source files are not supported because
400 # they are an indication that something has been misconfigured.
401 get_target_property(lib_imported ${zephyr_lib} IMPORTED)
402 get_target_property(lib_sources ${zephyr_lib} SOURCES)
Sebastian Bøe4ece94d2017-12-05 13:22:19 +0100403 if(lib_sources STREQUAL lib_sources-NOTFOUND
404 AND (NOT (${zephyr_lib} STREQUAL app))
Sebastian Bøe64edbaf2018-01-09 12:45:19 +0100405 AND (NOT lib_imported)
Sebastian Bøe4ece94d2017-12-05 13:22:19 +0100406 )
407 # app is not checked because it's sources are added to it after
408 # this CMakeLists.txt file has been processed
409 message(FATAL_ERROR "\
410The Zephyr library '${zephyr_lib}' was created without source files. \
Sebastian Bøe64edbaf2018-01-09 12:45:19 +0100411Empty (non-imported) libraries are not supported. \
Sebastian Bøe4ece94d2017-12-05 13:22:19 +0100412Either make sure that the library has the sources it should have, \
413or make sure it is not created when it has no source files.")
414 endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200415endforeach()
416
417get_property(OUTPUT_FORMAT GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT)
418
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200419get_property(LINKER_SCRIPT_DEFINES GLOBAL PROPERTY PROPERTY_LINKER_SCRIPT_DEFINES)
420
421if(CONFIG_APPLICATION_MEMORY)
422 # Objects default to being in kernel space, and then we exclude
423 # certain items.
424 set(kernel_object_file_list
425 ${ZEPHYR_LIBS_PROPERTY}
426 kernel
427 )
428 list(
429 REMOVE_ITEM
430 kernel_object_file_list
431 app
432 )
433
434 # The zephyr libraries in zephyr/lib/ and zephyr/test/ belong in
435 # userspace.
436
437 # NB: The business logic for determing what source files are in
438 # kernel space and what source files are in user space is
439 # fragile. Fix ASAP.
440 #
441 # The intended design is that certain directories are designated as
442 # containing userspace code and others for kernel space code. The
443 # implementation we have however is not working on directories of
444 # code, it is working on zephyr libraries. It is exploiting the fact
445 # that zephyr libraries follow a naming scheme as described in
446 # extensions.cmake:zephyr_library_get_current_dir_lib_name
447 #
448 # But code from test/ and lib/ that is placed in the "zephyr"
449 # library (with zephyr_sources()) will not be in a library that is
450 # prefixed with lib__ or test__ and will end up in the wrong address
451 # space.
452 set(application_space_dirs
453 lib
454 tests
455 )
456 foreach(f ${kernel_object_file_list})
457 foreach(app_dir ${application_space_dirs})
458 if(${f} MATCHES "^${app_dir}__") # Begins with ${app_dir}__, e.g. lib__libc
459 list(
460 REMOVE_ITEM
461 kernel_object_file_list
462 ${f}
463 )
464 endif()
465 endforeach()
466 endforeach()
467
468 # Create a list ks, with relative paths to kernel space libs.
469 foreach(f ${kernel_object_file_list})
470 get_target_property(target_name ${f} NAME)
471 get_target_property(target_binary_dir ${f} BINARY_DIR)
472
473 string(REPLACE
474 ${PROJECT_BINARY_DIR}
475 ""
476 fixed_path
477 ${target_binary_dir}
478 )
479
480 # Append / if not empty
481 if(fixed_path)
482 set(fixed_path "${fixed_path}/")
483 endif()
484
485 # Cut off leading / if present
486 if(fixed_path MATCHES "^/.+")
487 string(SUBSTRING ${fixed_path} 1 -1 fixed_path)
488 endif()
489
Sebastian Bøe1c2de102018-01-02 15:54:16 +0100490 set(fixed_path "${fixed_path}lib${target_name}.a")
491
492 if(CMAKE_GENERATOR STREQUAL "Ninja")
493 # Ninja invokes the linker from the root of the build directory
494 # (APPLICATION_BINARY_DIR) instead of from the build/zephyr
495 # directory (PROJECT_BINARY_DIR). So for linker-defs.h to get
496 # the correct path we need to prefix with zephyr/.
497 set(fixed_path "zephyr/${fixed_path}")
498 endif()
499
500 list(APPEND ks ${fixed_path})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200501 endforeach()
502
503 # We are done constructing kernel_object_file_list, now we inject this
504 # information into the linker script through -D's
505 list(LENGTH kernel_object_file_list NUM_KERNEL_OBJECT_FILES)
506 list(APPEND LINKER_SCRIPT_DEFINES -DNUM_KERNEL_OBJECT_FILES=${NUM_KERNEL_OBJECT_FILES})
507 set(i 0)
508 foreach(f ${ks})
509 list(APPEND LINKER_SCRIPT_DEFINES -DKERNEL_OBJECT_FILE_${i}=${f})
510 math(EXPR i "${i}+1")
511 endforeach()
512endif() # CONFIG_APPLICATION_MEMORY
513
Andy Grosse8860fe2018-02-01 01:12:32 -0600514# Declare MPU userspace dependencies before the linker scripts to make
515# sure the order of dependencies are met
516if(CONFIG_CPU_HAS_MPU AND CONFIG_USERSPACE)
Andy Grosse1fc5c22018-02-15 08:07:17 -0600517 if(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT AND CONFIG_APPLICATION_MEMORY)
Andy Grosse8860fe2018-02-01 01:12:32 -0600518 set(ALIGN_SIZING_DEP app_sizing_prebuilt linker_app_sizing_script)
519 endif()
Wayne Ren5a0ba2f2018-02-12 19:17:04 +0800520 if(CONFIG_ARM)
Chunlin Han18560a02018-02-01 01:19:49 -0600521 set(PRIV_STACK_DEP priv_stacks_prebuilt)
Wayne Ren5a0ba2f2018-02-12 19:17:04 +0800522 endif()
Andy Grosse8860fe2018-02-01 01:12:32 -0600523endif()
524
Andy Gross1f0ff062018-01-25 11:07:03 -0600525function(construct_add_custom_command_for_linker_pass linker_output_name output_variable)
526 set(linker_cmd_file_name ${linker_output_name}.cmd)
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100527
Andy Gross1f0ff062018-01-25 11:07:03 -0600528 if (${linker_output_name} MATCHES "^linker_pass_final$")
529 set(LINKER_PASS_DEFINE -DLINKER_PASS2)
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100530 else()
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100531 set(LINKER_PASS_DEFINE "")
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100532 endif()
533
Sebastian Bøea0b91292017-12-31 10:56:32 +0100534 # Different generators deal with depfiles differently.
535 if(CMAKE_GENERATOR STREQUAL "Unix Makefiles")
536 # Note that the IMPLICIT_DEPENDS option is currently supported only
537 # for Makefile generators and will be ignored by other generators.
538 set(LINKER_SCRIPT_DEP IMPLICIT_DEPENDS C ${LINKER_SCRIPT})
539 elseif(CMAKE_GENERATOR STREQUAL "Ninja")
540 # Using DEPFILE with other generators than Ninja is an error.
541 set(LINKER_SCRIPT_DEP DEPFILE ${PROJECT_BINARY_DIR}/${linker_cmd_file_name}.dep)
542 else()
543 # TODO: How would the linker script dependencies work for non-linker
544 # script generators.
545 message(STATUS "Warning; this generator is not well supported. The
546 Linker script may not be regenerated when it should.")
547 set(LINKER_SCRIPT_DEP "")
548 endif()
549
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100550 set(${output_variable}
551 OUTPUT ${linker_cmd_file_name}
552 DEPENDS ${LINKER_SCRIPT}
553 ${LINKER_SCRIPT_DEP}
554 COMMAND ${CMAKE_C_COMPILER}
555 -x assembler-with-cpp
556 ${NOSTDINC_F}
557 -undef
558 -MD -MF ${linker_cmd_file_name}.dep -MT ${BASE_NAME}/${linker_cmd_file_name}
559 ${ZEPHYR_INCLUDES}
560 ${LINKER_SCRIPT_DEFINES}
561 ${LINKER_PASS_DEFINE}
Sebastian Bøe8062a882018-01-03 16:02:49 +0100562 -E ${LINKER_SCRIPT}
563 -P # Prevent generation of debug `#line' directives.
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100564 -o ${linker_cmd_file_name}
565 VERBATIM
566 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
567
568 PARENT_SCOPE
569 )
570endfunction()
571
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200572get_filename_component(BASE_NAME ${CMAKE_CURRENT_BINARY_DIR} NAME)
Andy Gross1f0ff062018-01-25 11:07:03 -0600573construct_add_custom_command_for_linker_pass(linker custom_command)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200574add_custom_command(
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100575 ${custom_command}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200576)
Andy Grosse8860fe2018-02-01 01:12:32 -0600577
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200578add_custom_target(
579 linker_script
580 DEPENDS
Chunlin Han18560a02018-02-01 01:19:49 -0600581 ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200582 linker.cmd
583 offsets_h
Sebastian Bøeb1ab5012017-12-14 13:03:23 +0100584 )
585
586# Give the 'linker_script' target all of the include directories so
587# that cmake can successfully find the linker_script's header
588# dependencies.
589zephyr_get_include_directories_for_lang(C
590 ZEPHYR_INCLUDE_DIRS
591 STRIP_PREFIX # Don't use a -I prefix
592 )
593set_property(TARGET
594 linker_script
595 PROPERTY INCLUDE_DIRECTORIES
596 ${ZEPHYR_INCLUDE_DIRS}
597 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200598
Anas Nashif5146dbb2017-12-21 23:43:48 -0500599get_property(E_KERNEL_ENTRY GLOBAL PROPERTY E_KERNEL_ENTRY)
600
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200601set(zephyr_lnk
602 ${LINKERFLAGPREFIX},-Map=${PROJECT_BINARY_DIR}/${KERNEL_MAP_NAME}
603 -u_OffsetAbsSyms
604 -u_ConfigAbsSyms
Anas Nashif5146dbb2017-12-21 23:43:48 -0500605 ${E_KERNEL_ENTRY}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200606 ${LINKERFLAGPREFIX},--start-group
607 ${LINKERFLAGPREFIX},--whole-archive
608 ${ZEPHYR_LIBS_PROPERTY}
609 ${LINKERFLAGPREFIX},--no-whole-archive
610 kernel
611 ${OFFSETS_O_PATH}
612 ${LINKERFLAGPREFIX},--end-group
613 ${LIB_INCLUDE_DIR}
614 -L${PROJECT_BINARY_DIR}
615 ${TOOLCHAIN_LIBS}
616 )
617
618if(CONFIG_GEN_ISR_TABLES)
619 # isr_tables.c is generated from zephyr_prebuilt by
620 # gen_isr_tables.py
621 add_custom_command(
622 OUTPUT isr_tables.c
623 COMMAND ${CMAKE_OBJCOPY}
624 -I ${OUTPUT_FORMAT}
625 -O binary
626 --only-section=.intList
627 $<TARGET_FILE:zephyr_prebuilt>
628 isrList.bin
629 COMMAND ${PYTHON_EXECUTABLE}
Carles Cufi7d764b32018-01-11 15:46:44 +0100630 ${ZEPHYR_BASE}/arch/common/gen_isr_tables.py
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200631 --output-source isr_tables.c
Rajavardhan Gundi1e6adba2017-12-24 15:18:57 +0530632 --kernel $<TARGET_FILE:zephyr_prebuilt>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200633 --intlist isrList.bin
Sebastian Bøea55279a2018-01-04 14:08:39 +0100634 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--debug>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200635 --sw-isr-table
636 --vector-table
637 DEPENDS zephyr_prebuilt
638 )
639 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_SOURCE_FILES isr_tables.c)
640endif()
641
Chunlin Han18560a02018-02-01 01:19:49 -0600642if(CONFIG_ARM AND CONFIG_USERSPACE)
643 set(GEN_PRIV_STACKS $ENV{ZEPHYR_BASE}/scripts/gen_priv_stacks.py)
644 set(PROCESS_PRIV_STACKS_GPERF $ENV{ZEPHYR_BASE}/scripts/process_gperf.py)
645
646 set(PRIV_STACKS priv_stacks_hash.gperf)
647 set(PRIV_STACKS_OUTPUT_SRC_PRE priv_stacks_hash_preprocessed.c)
648 set(PRIV_STACKS_OUTPUT_SRC priv_stacks_hash.c)
649 set(PRIV_STACKS_OUTPUT_OBJ priv_stacks_hash.c.obj)
650 set(PRIV_STACKS_OUTPUT_OBJ_RENAMED priv_stacks_hash_renamed.o)
651
652 # Essentially what we are doing here is extracting some information
653 # out of the nearly finished elf file, generating the source code
654 # for a hash table based on that information, and then compiling and
655 # linking the hash table back into a now even more nearly finished
656 # elf file.
657
658 # Use the script GEN_PRIV_STACKS to scan the kernel binary's
659 # (zephyr_prebuilt) DWARF information to produce a table of kernel
660 # objects (PRIV_STACKS) which we will then pass to gperf
661 add_custom_command(
662 OUTPUT ${PRIV_STACKS}
663 COMMAND
664 ${PYTHON_EXECUTABLE}
665 ${GEN_PRIV_STACKS}
666 --kernel $<TARGET_FILE:priv_stacks_prebuilt>
667 --output ${PRIV_STACKS}
668 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
669 DEPENDS priv_stacks_prebuilt
670 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
671 )
672 add_custom_target(priv_stacks DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS})
673
674 # Use gperf to generate C code (PRIV_STACKS_OUTPUT_SRC_PRE) which implements a
675 # perfect hashtable based on PRIV_STACKS
676 add_custom_command(
677 OUTPUT ${PRIV_STACKS_OUTPUT_SRC_PRE}
678 COMMAND
679 ${GPERF} -C
680 --output-file ${PRIV_STACKS_OUTPUT_SRC_PRE}
681 ${PRIV_STACKS}
682 DEPENDS priv_stacks
683 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
684 )
685 add_custom_target(priv_stacks_output_src_pre DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_SRC_PRE})
686
687 # For our purposes the code/data generated by gperf is not optimal.
688 #
689 # The script PROCESS_GPERF creates a new c file OUTPUT_SRC based on
690 # OUTPUT_SRC_PRE to greatly reduce the amount of code/data generated
691 # since we know we are always working with pointer values
692 add_custom_command(
693 OUTPUT ${PRIV_STACKS_OUTPUT_SRC}
694 COMMAND
695 ${PROCESS_PRIV_STACKS_GPERF}
696 -i ${PRIV_STACKS_OUTPUT_SRC_PRE}
697 -o ${PRIV_STACKS_OUTPUT_SRC}
698 -p "struct _k_priv_stack_map"
699 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
700 DEPENDS priv_stacks_output_src_pre
701 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
702 )
703 add_custom_target(priv_stacks_output_src DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_SRC})
704
705 # We need precise control of where generated text/data ends up in the final
706 # kernel image. Disable function/data sections and use objcopy to move
707 # generated data into special section names
708 add_library(priv_stacks_output_lib STATIC
709 ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_SRC}
710 )
711
712 target_link_libraries(priv_stacks_output_lib zephyr_interface)
713
714 # Turn off -ffunction-sections, etc.
715 # NB: Using a library instead of target_compile_options(priv_stacks_output_lib
716 # [...]) because a library's options have precedence
717 add_library(priv_stacks_output_lib_interface INTERFACE)
718 target_compile_options(priv_stacks_output_lib_interface INTERFACE
719 -fno-function-sections
720 -fno-data-sections
721 )
722 target_link_libraries(priv_stacks_output_lib priv_stacks_output_lib_interface)
723
724 set(PRIV_STACKS_OUTPUT_OBJ_PATH ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/priv_stacks_output_lib.dir/${PRIV_STACKS_OUTPUT_OBJ})
725
726 add_custom_command(
727 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_OBJ_RENAMED}
728 COMMAND
729 ${CMAKE_OBJCOPY}
730 --rename-section .bss=.priv_stacks.noinit
731 --rename-section .data=.priv_stacks.data
732 --rename-section .text=.priv_stacks.text
733 --rename-section .rodata=.priv_stacks.rodata
734 ${PRIV_STACKS_OUTPUT_OBJ_PATH}
735 ${PRIV_STACKS_OUTPUT_OBJ_RENAMED}
736 DEPENDS priv_stacks_output_lib
737 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
738 )
739 add_custom_target(priv_stacks_output_obj_renamed DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_OBJ_RENAMED})
740
741 add_library(priv_stacks_output_obj_renamed_lib STATIC IMPORTED GLOBAL)
742 set_property(
743 TARGET priv_stacks_output_obj_renamed_lib
744 PROPERTY
745 IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_OBJ_RENAMED}
746 )
747 add_dependencies(
748 priv_stacks_output_obj_renamed_lib
749 priv_stacks_output_obj_renamed
750 )
751
752 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_OBJECT_FILES priv_stacks_output_obj_renamed_lib)
753endif()
754
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200755if(CONFIG_USERSPACE)
Carles Cufi7d764b32018-01-11 15:46:44 +0100756 set(GEN_KOBJ_LIST ${ZEPHYR_BASE}/scripts/gen_kobject_list.py)
757 set(PROCESS_GPERF ${ZEPHYR_BASE}/scripts/process_gperf.py)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200758
759 set(OBJ_LIST kobject_hash.gperf)
760 set(OUTPUT_SRC_PRE kobject_hash_preprocessed.c)
761 set(OUTPUT_SRC kobject_hash.c)
762 set(OUTPUT_OBJ kobject_hash.c.obj)
763 set(OUTPUT_OBJ_RENAMED kobject_hash_renamed.o)
764
765 # Essentially what we are doing here is extracting some information
766 # out of the nearly finished elf file, generating the source code
767 # for a hash table based on that information, and then compiling and
768 # linking the hash table back into a now even more nearly finished
769 # elf file.
770
771 # Use the script GEN_KOBJ_LIST to scan the kernel binary's
772 # (zephyr_prebuilt) DWARF information to produce a table of kernel
773 # objects (OBJ_LIST) which we will then pass to gperf
774 add_custom_command(
775 OUTPUT ${OBJ_LIST}
776 COMMAND
777 ${PYTHON_EXECUTABLE}
778 ${GEN_KOBJ_LIST}
779 --kernel $<TARGET_FILE:zephyr_prebuilt>
780 --output ${OBJ_LIST}
781 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
782 DEPENDS zephyr_prebuilt
783 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
784 )
785 add_custom_target(obj_list DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OBJ_LIST})
786
787 # Use gperf to generate C code (OUTPUT_SRC_PRE) which implements a
788 # perfect hashtable based on OBJ_LIST
789 add_custom_command(
790 OUTPUT ${OUTPUT_SRC_PRE}
791 COMMAND
792 ${GPERF}
793 --output-file ${OUTPUT_SRC_PRE}
794 ${OBJ_LIST}
Sebastian Bøef5758b52018-01-31 10:42:46 +0100795 DEPENDS obj_list ${OBJ_LIST}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200796 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
797 )
798 add_custom_target(output_src_pre DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_SRC_PRE})
799
800 # For our purposes the code/data generated by gperf is not optimal.
801 #
802 # The script PROCESS_GPERF creates a new c file OUTPUT_SRC based on
803 # OUTPUT_SRC_PRE to greatly reduce the amount of code/data generated
804 # since we know we are always working with pointer values
805 add_custom_command(
806 OUTPUT ${OUTPUT_SRC}
807 COMMAND
808 ${PROCESS_GPERF}
809 -i ${OUTPUT_SRC_PRE}
810 -o ${OUTPUT_SRC}
Chunlin Han18560a02018-02-01 01:19:49 -0600811 -p "struct _k_object"
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200812 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Sebastian Bøef5758b52018-01-31 10:42:46 +0100813 DEPENDS output_src_pre ${OUTPUT_SRC_PRE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200814 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
815 )
816 add_custom_target(output_src DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_SRC})
817
818 # We need precise control of where generated text/data ends up in the final
819 # kernel image. Disable function/data sections and use objcopy to move
820 # generated data into special section names
821 add_library(output_lib STATIC
822 ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_SRC}
823 )
824
Adithya Baglodyce9a5a22018-01-29 15:58:51 +0530825 # always compile kobject_hash.c at optimization -Os
826 set_source_files_properties(${OUTPUT_SRC} PROPERTIES COMPILE_FLAGS -Os)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200827 target_link_libraries(output_lib zephyr_interface)
828
829 # Turn off -ffunction-sections, etc.
830 # NB: Using a library instead of target_compile_options(output_lib
831 # [...]) because a library's options have precedence
832 add_library(output_lib_interface INTERFACE)
833 target_compile_options(output_lib_interface INTERFACE
834 -fno-function-sections
835 -fno-data-sections
836 )
837 target_link_libraries(output_lib output_lib_interface)
838
839 set(OUTPUT_OBJ_PATH ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/output_lib.dir/${OUTPUT_OBJ})
840
841 add_custom_command(
842 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_OBJ_RENAMED}
843 COMMAND
844 ${CMAKE_OBJCOPY}
845 --rename-section .data=.kobject_data.data
846 --rename-section .text=.kobject_data.text
847 --rename-section .rodata=.kobject_data.rodata
848 ${OUTPUT_OBJ_PATH}
849 ${OUTPUT_OBJ_RENAMED}
850 DEPENDS output_lib
851 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
852 )
853 add_custom_target(output_obj_renamed DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_OBJ_RENAMED})
854
855 add_library(output_obj_renamed_lib STATIC IMPORTED GLOBAL)
856 set_property(
857 TARGET output_obj_renamed_lib
858 PROPERTY
859 IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_OBJ_RENAMED}
860 )
861 add_dependencies(
862 output_obj_renamed_lib
863 output_obj_renamed
864 )
865
866 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_OBJECT_FILES output_obj_renamed_lib)
867endif()
868
869# Read global variables into local variables
870get_property(GKOF GLOBAL PROPERTY GENERATED_KERNEL_OBJECT_FILES)
871get_property(GKSF GLOBAL PROPERTY GENERATED_KERNEL_SOURCE_FILES)
872
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200873get_property(TOPT GLOBAL PROPERTY TOPT)
874set_ifndef( TOPT -T)
875
Andy Grosse8860fe2018-02-01 01:12:32 -0600876configure_file(
877 $ENV{ZEPHYR_BASE}/include/arch/arm/cortex_m/scripts/app_data_alignment.ld
878 ${PROJECT_BINARY_DIR}/include/generated/app_data_alignment.ld)
879
880if(CONFIG_CPU_HAS_MPU AND CONFIG_USERSPACE)
881
Andy Grosse1fc5c22018-02-15 08:07:17 -0600882 if(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT AND CONFIG_APPLICATION_MEMORY)
Andy Grosse8860fe2018-02-01 01:12:32 -0600883
884 construct_add_custom_command_for_linker_pass(linker_app_sizing custom_command)
885 add_custom_command(
886 ${custom_command}
887 )
888
889 add_custom_target(
890 linker_app_sizing_script
891 DEPENDS
892 linker_app_sizing.cmd
893 offsets_h
894 )
895
896 set_property(TARGET
897 linker_app_sizing_script
898 PROPERTY INCLUDE_DIRECTORIES
899 ${ZEPHYR_INCLUDE_DIRS}
900 )
901
902 # For systems with MPUs, the size of the application data section must
903 # be determined so that MPU alignment requirements can be met.
904 # Create a app_sizing_prebuilt target so we can do this before the
905 # other ELF files are built
906 set(GEN_APP_ALIGN $ENV{ZEPHYR_BASE}/scripts/gen_alignment_script.py)
907 add_executable( app_sizing_prebuilt misc/empty_file.c)
908 target_link_libraries(app_sizing_prebuilt ${TOPT} ${PROJECT_BINARY_DIR}/linker_app_sizing.cmd ${zephyr_lnk})
909 set_property(TARGET app_sizing_prebuilt PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker_app_sizing.cmd)
910 add_dependencies( app_sizing_prebuilt linker_app_sizing_script offsets)
911
912 add_custom_command(
913 TARGET app_sizing_prebuilt
914 POST_BUILD
915 COMMAND ${PYTHON_EXECUTABLE} ${GEN_APP_ALIGN}
916 --output ./include/generated/app_data_alignment.ld
917 --kernel $<TARGET_FILE:app_sizing_prebuilt>
918 VERBATIM
919 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
920 )
921 endif()
Chunlin Han18560a02018-02-01 01:19:49 -0600922
Wayne Ren5a0ba2f2018-02-12 19:17:04 +0800923if(CONFIG_ARM)
Chunlin Han18560a02018-02-01 01:19:49 -0600924 construct_add_custom_command_for_linker_pass(linker_priv_stacks custom_command)
925 add_custom_command(
926 ${custom_command}
927 )
928
929 add_custom_target(
930 linker_priv_stacks_script
931 DEPENDS
932 ${ALIGN_SIZING_DEP}
933 linker_priv_stacks.cmd
934 offsets_h
935 )
936
937 set_property(TARGET
938 linker_priv_stacks_script
939 PROPERTY INCLUDE_DIRECTORIES
940 ${ZEPHYR_INCLUDE_DIRS}
941 )
942
943 set(PRIV_STACK_LIB priv_stacks_output_obj_renamed_lib)
944 add_executable( priv_stacks_prebuilt misc/empty_file.c)
945 target_link_libraries(priv_stacks_prebuilt ${TOPT} ${PROJECT_BINARY_DIR}/linker_priv_stacks.cmd ${zephyr_lnk})
946 set_property(TARGET priv_stacks_prebuilt PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker_priv_stacks.cmd)
947 add_dependencies( priv_stacks_prebuilt ${ALIGN_SIZING_DEP} linker_priv_stacks_script offsets)
Wayne Ren5a0ba2f2018-02-12 19:17:04 +0800948endif()
Chunlin Han18560a02018-02-01 01:19:49 -0600949
Andy Grosse8860fe2018-02-01 01:12:32 -0600950endif()
951
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200952# FIXME: Is there any way to get rid of empty_file.c?
953add_executable( zephyr_prebuilt misc/empty_file.c)
Chunlin Han18560a02018-02-01 01:19:49 -0600954target_link_libraries(zephyr_prebuilt ${TOPT} ${PROJECT_BINARY_DIR}/linker.cmd ${PRIV_STACK_LIB} ${zephyr_lnk})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200955set_property(TARGET zephyr_prebuilt PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker.cmd)
Chunlin Han18560a02018-02-01 01:19:49 -0600956add_dependencies( zephyr_prebuilt ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP} linker_script offsets)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200957
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200958
959if(NOT CONFIG_NATIVE_APPLICATION)
960 set(NOSTDINC_F -nostdinc)
961endif()
962
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200963if(GKOF OR GKSF)
964 set(logical_target_for_zephyr_elf kernel_elf)
965
966 # The second linker pass uses the same source linker script of the
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100967 # first pass (LINKER_SCRIPT), but this time with a different output
968 # file and preprocessed with the define LINKER_PASS2.
Andy Gross1f0ff062018-01-25 11:07:03 -0600969 construct_add_custom_command_for_linker_pass(linker_pass_final custom_command)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200970 add_custom_command(
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100971 ${custom_command}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200972 )
Andy Grosse8860fe2018-02-01 01:12:32 -0600973
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200974 add_custom_target(
Andy Gross1f0ff062018-01-25 11:07:03 -0600975 linker_pass_final_script
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200976 DEPENDS
Chunlin Han18560a02018-02-01 01:19:49 -0600977 ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP}
Andy Grosse8860fe2018-02-01 01:12:32 -0600978 zephyr_prebuilt
Andy Gross1f0ff062018-01-25 11:07:03 -0600979 linker_pass_final.cmd
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200980 offsets_h
981 )
Sebastian Bøeb1ab5012017-12-14 13:03:23 +0100982 set_property(TARGET
Andy Gross1f0ff062018-01-25 11:07:03 -0600983 linker_pass_final_script
Sebastian Bøeb1ab5012017-12-14 13:03:23 +0100984 PROPERTY INCLUDE_DIRECTORIES
985 ${ZEPHYR_INCLUDE_DIRS}
986 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200987
988 add_executable( kernel_elf misc/empty_file.c ${GKSF})
Andy Gross1f0ff062018-01-25 11:07:03 -0600989 target_link_libraries(kernel_elf ${GKOF} ${TOPT} ${PROJECT_BINARY_DIR}/linker_pass_final.cmd ${zephyr_lnk})
990 set_property(TARGET kernel_elf PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker_pass_final.cmd)
Chunlin Han18560a02018-02-01 01:19:49 -0600991 add_dependencies( kernel_elf ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP} linker_pass_final_script)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200992else()
993 set(logical_target_for_zephyr_elf zephyr_prebuilt)
994 # Use the prebuilt elf as the final elf since we don't have a
995 # generation stage.
996endif()
997
998# To avoid having the same logical target name for the zephyr lib and
999# the zephyr elf, we set the kernel_elf file name to zephyr.elf.
1000set_target_properties(${logical_target_for_zephyr_elf} PROPERTIES OUTPUT_NAME ${KERNEL_NAME})
1001
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001002set(post_build_commands "")
1003
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001004list_append_ifdef(CONFIG_CHECK_LINK_MAP
1005 post_build_commands
Carles Cufi7d764b32018-01-11 15:46:44 +01001006 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/check_link_map.py ${KERNEL_MAP_NAME}
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001007 )
1008
1009list_append_ifdef(
1010 CONFIG_BUILD_OUTPUT_HEX
1011 post_build_commands
1012 COMMAND ${CMAKE_OBJCOPY} -S -Oihex -R .comment -R COMMON -R .eh_frame ${KERNEL_ELF_NAME} ${KERNEL_HEX_NAME}
1013 )
1014
1015list_append_ifdef(
1016 CONFIG_BUILD_OUTPUT_BIN
1017 post_build_commands
1018 COMMAND ${CMAKE_OBJCOPY} -S -Obinary -R .comment -R COMMON -R .eh_frame ${KERNEL_ELF_NAME} ${KERNEL_BIN_NAME}
1019 )
Anas Nashif4592ff22017-11-23 07:54:26 -05001020
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001021list_append_ifdef(
1022 CONFIG_BUILD_OUTPUT_S19
1023 post_build_commands
1024 COMMAND ${CMAKE_OBJCOPY} --srec-len 1 --output-target=srec ${KERNEL_ELF_NAME} ${KERNEL_S19_NAME}
1025 )
1026
1027list_append_ifdef(
Anas Nashif1f1143a2017-11-22 13:03:53 -05001028 CONFIG_OUTPUT_DISASSEMBLY
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001029 post_build_commands
1030 COMMAND ${CMAKE_OBJDUMP} -S ${KERNEL_ELF_NAME} > ${KERNEL_LST_NAME}
1031 )
1032
1033list_append_ifdef(
Anas Nashif1f1143a2017-11-22 13:03:53 -05001034 CONFIG_OUTPUT_STAT
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001035 post_build_commands
1036 COMMAND ${CMAKE_READELF} -e ${KERNEL_ELF_NAME} > ${KERNEL_STAT_NAME}
1037 )
1038
1039list_append_ifdef(
1040 CONFIG_BUILD_OUTPUT_STRIPPED
1041 post_build_commands
1042 COMMAND ${CMAKE_STRIP} --strip-all ${KERNEL_ELF_NAME} -o ${KERNEL_STRIP_NAME}
1043 )
1044
Anas Nashif4592ff22017-11-23 07:54:26 -05001045list_append_ifdef(
1046 CONFIG_BUILD_OUTPUT_EXE
1047 post_build_commands
1048 COMMAND ${CMAKE_COMMAND} -E rename ${KERNEL_ELF_NAME} ${KERNEL_EXE_NAME}
1049 )
1050
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001051add_custom_command(
1052 TARGET ${logical_target_for_zephyr_elf}
1053 POST_BUILD
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001054 ${post_build_commands}
1055 COMMENT "Generating files from zephyr.elf for board: ${BOARD}"
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001056 # NB: COMMENT only works for some CMake-Generators
1057)
1058
Sebastian Bøedbdd7222017-12-19 13:20:10 +01001059if(CONFIG_OUTPUT_PRINT_MEMORY_USAGE)
1060 # Use --print-memory-usage with the first link.
1061 #
1062 # Don't use this option with the second link because seeing it twice
1063 # could confuse users and using it on the second link would suppress
1064 # it when the first link has a ram/flash-usage issue.
1065 set(option ${LINKERFLAGPREFIX},--print-memory-usage)
1066 string(MAKE_C_IDENTIFIER check${option} check)
Sebastian Bøeba0b2832017-12-21 10:16:43 +01001067
Sebastian Bøec34b7a32017-12-27 15:21:54 +01001068 set(SAVED_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
Sebastian Bøeba0b2832017-12-21 10:16:43 +01001069 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${option}")
1070 check_c_compiler_flag("" ${check})
Sebastian Bøec34b7a32017-12-27 15:21:54 +01001071 set(CMAKE_REQUIRED_FLAGS ${SAVED_CMAKE_REQUIRED_FLAGS})
Sebastian Bøeba0b2832017-12-21 10:16:43 +01001072
Sebastian Bøedbdd7222017-12-19 13:20:10 +01001073 target_link_libraries_ifdef(${check} zephyr_prebuilt ${option})
1074endif()
1075
Anas Nashifc15d3c92017-11-21 18:54:55 -05001076if(EMU_PLATFORM)
Carles Cufi7d764b32018-01-11 15:46:44 +01001077 include(${ZEPHYR_BASE}/cmake/emu/${EMU_PLATFORM}.cmake)
Anas Nashiffd276ae2017-12-21 16:45:45 -05001078else()
1079 add_custom_target(run
1080 COMMAND
1081 ${CMAKE_COMMAND} -E echo
1082 "==================================================="
1083 "Emulation/Simulation not supported with this board."
1084 "==================================================="
1085 )
Anas Nashifc15d3c92017-11-21 18:54:55 -05001086endif()
1087
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001088add_subdirectory(cmake/flash)
1089
1090add_subdirectory(cmake/usage)
1091add_subdirectory(cmake/reports)
1092
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001093if(CONFIG_ASSERT)
1094 message(WARNING "
1095 ------------------------------------------------------------
1096 --- WARNING: __ASSERT() statements are globally ENABLED ---
1097 --- The kernel will run more slowly and uses more memory ---
1098 ------------------------------------------------------------"
1099)
1100endif()
1101
1102if(CONFIG_BOARD_DEPRECATED)
1103 message(WARNING "
1104 WARNING: The board '${BOARD}' is deprecated and will be
1105 removed in version ${CONFIG_BOARD_DEPRECATED}"
1106)
1107endif()
Andrew Boiedf48e112018-01-12 09:54:24 -08001108
1109if(CONFIG_X86 AND CONFIG_USERSPACE AND NOT CONFIG_X86_NO_MELTDOWN)
1110 message(WARNING "
1111 WARNING: You have enabled CONFIG_USERSPACE on an x86-based target.
1112 If your CPU is vulnerable to the Meltdown CPU bug, security of
1113 supervisor-only memory pages is not guaranteed. This version of Zephyr
1114 does not contain a fix for this issue."
1115)
1116endif()