blob: bc32fa4854195b20254981d16f6565aa61dfa607 [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
Anas Nashifdaf77162018-04-09 21:53:26 -050065if(BUILD_VERSION)
66 zephyr_compile_definitions(
67 BUILD_VERSION=${BUILD_VERSION}
68 )
69endif()
70
Sebastian Bøe12f8f762017-10-27 15:43:34 +020071# We need to set an optimization level.
72# Default to -Os
Alberto Escolar Piedrasf60527a2018-01-22 15:35:54 +010073# unless CONFIG_NO_OPTIMIZATIONS is set, then it is -O0
74# or unless CONFIG_DEBUG is set, then it is -Og
Sebastian Bøe12f8f762017-10-27 15:43:34 +020075#
76# also, some toolchain's break with -Os, and some toolchain's break
77# with -Og so allow them to override what flag to use
78#
79# Finally, the user can use Kconfig to add compiler options that will
80# come after these options and override them
Alberto Escolar Piedrasf60527a2018-01-22 15:35:54 +010081set_ifndef(OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG "-O0")
Sebastian Bøe600c8f72018-01-24 10:40:32 +010082set_ifndef(OPTIMIZE_FOR_DEBUG_FLAG "-Og")
83set_ifndef(OPTIMIZE_FOR_SIZE_FLAG "-Os")
84
Alberto Escolar Piedrasf60527a2018-01-22 15:35:54 +010085if(CONFIG_NO_OPTIMIZATIONS)
Sebastian Bøe600c8f72018-01-24 10:40:32 +010086 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_NO_OPTIMIZATIONS_FLAG})
87elseif(CONFIG_DEBUG_OPTIMIZATIONS)
Sebastian Bøe12f8f762017-10-27 15:43:34 +020088 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_DEBUG_FLAG})
Sebastian Bøe600c8f72018-01-24 10:40:32 +010089elseif(CONFIG_SIZE_OPTIMIZATIONS)
Sebastian Bøe12f8f762017-10-27 15:43:34 +020090 set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SIZE_FLAG}) # Default
Sebastian Bøe600c8f72018-01-24 10:40:32 +010091else()
92 assert(0 "Unreachable code. Expected optimization level to have been chosen. See misc/Kconfig.")
Sebastian Bøe12f8f762017-10-27 15:43:34 +020093endif()
94
95zephyr_compile_options(
96 ${OPTIMIZATION_FLAG} # Usually -Os
97 -g # TODO: build configuration enough?
98 -Wall
99 -Wformat
100 -Wformat-security
101 -Wno-format-zero-length
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200102 -imacros ${AUTOCONF_H}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200103 -ffreestanding
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200104 -Wno-main
105 ${NOSTDINC_F}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200106)
107
108zephyr_compile_options(
109 $<$<COMPILE_LANGUAGE:C>:-std=c99>
110
111 $<$<COMPILE_LANGUAGE:CXX>:-std=c++11>
112 $<$<COMPILE_LANGUAGE:CXX>:-fcheck-new>
113 $<$<COMPILE_LANGUAGE:CXX>:-ffunction-sections>
114 $<$<COMPILE_LANGUAGE:CXX>:-fdata-sections>
115 $<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>
116 $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
117
118 $<$<COMPILE_LANGUAGE:ASM>:-xassembler-with-cpp>
119 $<$<COMPILE_LANGUAGE:ASM>:-D_ASMLANGUAGE>
120)
121
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200122if(NOT CONFIG_NATIVE_APPLICATION)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200123zephyr_ld_options(
124 -nostartfiles
125 -nodefaultlibs
126 -nostdlib
127 -static
128 -no-pie
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200129)
130endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200131
132# ==========================================================================
133#
134# cmake -DW=... settings
135#
136# W=1 - warnings that may be relevant and does not occur too often
137# W=2 - warnings that occur quite often but may still be relevant
138# W=3 - the more obscure warnings, can most likely be ignored
139# ==========================================================================
140if(W MATCHES "1")
141 zephyr_compile_options(
142 -Wextra
143 -Wunused
144 -Wno-unused-parameter
145 -Wmissing-declarations
146 -Wmissing-format-attribute
147 -Wold-style-definition
148 )
149 zephyr_cc_option(
150 -Wmissing-prototypes
151 -Wmissing-include-dirs
152 -Wunused-but-set-variable
153 -Wno-missing-field-initializers
154 )
155endif()
156
157if(W MATCHES "2")
158 zephyr_compile_options(
159 -Waggregate-return
160 -Wcast-align
161 -Wdisabled-optimization
162 -Wnested-externs
163 -Wshadow
164 )
165 zephyr_cc_option(
166 -Wlogical-op
167 -Wmissing-field-initializers
168 )
169endif()
170
171if(W MATCHES "3")
172 zephyr_compile_options(
173 -Wbad-function-cast
174 -Wcast-qual
175 -Wconversion
176 -Wpacked
177 -Wpadded
178 -Wpointer-arith
179 -Wredundant-decls
180 -Wswitch-default
181 )
182 zephyr_cc_option(
183 -Wpacked-bitfield-compat
184 -Wvla
185 )
186endif()
187
188# Allow the user to inject options when calling cmake, e.g.
189# 'cmake -DEXTRA_CFLAGS="-Werror -Wno-deprecated-declarations" ..'
Sebastian Bøe9f590452017-11-10 12:22:23 +0100190include(cmake/extra_flags.cmake)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200191
192if(CONFIG_READABLE_ASM)
193 zephyr_cc_option(-fno-reorder-blocks)
194 zephyr_cc_option(-fno-ipa-cp-clone)
195 zephyr_cc_option(-fno-partial-inlining)
196endif()
197
198zephyr_cc_option(-fno-asynchronous-unwind-tables)
199zephyr_cc_option(-fno-pie)
200zephyr_cc_option(-fno-pic)
201zephyr_cc_option(-fno-strict-overflow)
202zephyr_cc_option(-Wno-pointer-sign)
203
Sebastian Bøe703dc592017-11-29 10:19:50 +0100204zephyr_compile_options_ifdef(CONFIG_STACK_CANARIES -fstack-protector-all)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200205
206if(CONFIG_OVERRIDE_FRAME_POINTER_DEFAULT)
207 if(CONFIG_OMIT_FRAME_POINTER)
208 zephyr_cc_option(-fomit-frame-pointer)
209 else()
210 zephyr_cc_option(-fno-omit-frame-pointer)
211 endif()
212endif()
213
214zephyr_compile_options(${CONFIG_COMPILER_OPT})
215
216# TODO: Include arch compiler options at this point.
217
218# TODO: This Clang check is broken
219if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
220 zephyr_cc_option(
221 -Wno-unknown-warning-option
222 -Wno-unused-variable
223 -Wno-format-invalid-specifier
224 -Wno-gnu
225 # comparison of unsigned expression < 0 is always false
226 -Wno-tautological-compare
227 )
228else() # GCC assumed
229 zephyr_cc_option(
230 -Wno-unused-but-set-variable
231 -fno-reorder-functions
232 )
Anas Nashif7ee8bb92018-02-11 14:36:21 -0600233 if(NOT ${ZEPHYR_TOOLCHAIN_VARIANT} STREQUAL "xcc")
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200234 zephyr_cc_option(-fno-defer-pop)
235 endif()
236endif()
237
238zephyr_cc_option_ifdef(CONFIG_DEBUG_SECTION_MISMATCH -fno-inline-functions-called-once)
239zephyr_cc_option_ifdef(CONFIG_STACK_USAGE -fstack-usage)
240
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200241zephyr_system_include_directories(${NOSTDINC})
242
243# Force an error when things like SYS_INIT(foo, ...) occur with a missing header.
244zephyr_cc_option(-Werror=implicit-int)
245
246# Prohibit date/time macros, which would make the build non-deterministic
247# cc-option(-Werror=date-time)
248
249# TODO: Archiver arguments
250# ar_option(D)
251
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200252set_ifndef(LINKERFLAGPREFIX -Wl)
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200253
254if(NOT CONFIG_NATIVE_APPLICATION)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200255zephyr_ld_options(
256 ${LINKERFLAGPREFIX},-X
257 ${LINKERFLAGPREFIX},-N
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200258 )
259endif()
260
261zephyr_ld_options(
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200262 ${LINKERFLAGPREFIX},--gc-sections
263 ${LINKERFLAGPREFIX},--build-id=none
264 )
265
266if(CONFIG_HAVE_CUSTOM_LINKER_SCRIPT)
267 set(LINKER_SCRIPT ${APPLICATION_SOURCE_DIR}/${CONFIG_CUSTOM_LINKER_SCRIPT})
Sebastian Bøec1aa9d12018-04-12 14:48:05 +0200268 if(NOT EXISTS ${LINKER_SCRIPT})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200269 set(LINKER_SCRIPT ${CONFIG_CUSTOM_LINKER_SCRIPT})
Sebastian Bøec1aa9d12018-04-12 14:48:05 +0200270 assert_exists(CONFIG_CUSTOM_LINKER_SCRIPT)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200271 endif()
272else()
273 # Try a board specific linker file
274 set(LINKER_SCRIPT ${BOARD_DIR}/linker.ld)
275 if(NOT EXISTS ${LINKER_SCRIPT})
276 # If not available, try an SoC specific linker file
Carles Cufi7d764b32018-01-11 15:46:44 +0100277 set(LINKER_SCRIPT ${ZEPHYR_BASE}/arch/${ARCH}/soc/${SOC_PATH}/linker.ld)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200278 endif()
279endif()
280
281if(NOT EXISTS ${LINKER_SCRIPT})
282 message(FATAL_ERROR "Could not find linker script: '${LINKER_SCRIPT}'. Corrupted configuration?")
283endif()
284
Kristian Klomsten Skordal0225e952018-01-30 11:26:42 +0100285# Custom section support in linker scripts requires that the application source
286# directory is in the preprocessor search path, in order to find the custom
287# linker script fragments.
288if(CONFIG_CUSTOM_RODATA_LD OR CONFIG_CUSTOM_RWDATA_LD OR CONFIG_CUSTOM_SECTIONS_LD)
289 zephyr_include_directories(${APPLICATION_SOURCE_DIR})
290endif()
291
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200292configure_file(version.h.in ${PROJECT_BINARY_DIR}/include/generated/version.h)
293
Sebastian Bøe6f946e22018-01-09 10:52:57 +0100294# Unfortunately, the order in which CMakeLists.txt code is processed
295# matters so we need to be careful about how we order the processing
296# of subdirectories. One example is "Compiler flags added late in the
297# build are not exported to external build systems #5605"; when we
298# integrate with an external build system we read out all compiler
299# flags when the external project is created. So an external project
300# defined in subsys or ext will not get global flags added by drivers/
301# or tests/ as the subdirectories are ordered now.
302#
303# Another example of when the order matters is the reading and writing
304# of global properties such as ZEPHYR_LIBS or
305# GENERATED_KERNEL_OBJECT_FILES.
306#
307# Arch is placed early because it defines important compiler flags
308# that must be exported to external build systems defined in
309# e.g. subsys/.
310add_subdirectory(arch)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200311add_subdirectory(lib)
312add_subdirectory(misc)
313# We use include instead of add_subdirectory to avoid creating a new directory scope.
314# This is because source file properties are directory scoped, including the GENERATED
315# property which is set implicitly for custom command outputs
316include(misc/generated/CMakeLists.txt)
317add_subdirectory(boards)
318add_subdirectory(ext)
319add_subdirectory(subsys)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200320add_subdirectory(drivers)
321add_subdirectory(tests)
322
Sebastian Bøe2ead15d2017-11-29 17:46:37 +0100323set(syscall_macros_h ${ZEPHYR_BINARY_DIR}/include/generated/syscall_macros.h)
324
325add_custom_target(syscall_macros_h_target DEPENDS ${syscall_macros_h})
326add_custom_command( OUTPUT ${syscall_macros_h}
327 COMMAND
328 ${PYTHON_EXECUTABLE}
329 ${PROJECT_SOURCE_DIR}/scripts/gen_syscall_header.py
330 > ${syscall_macros_h}
331 DEPENDS ${PROJECT_SOURCE_DIR}/scripts/gen_syscall_header.py
332 )
333
Sebastian Bøe13a68402017-11-20 13:03:55 +0100334# This command is a hack to support commands that are always run. Any
335# target that depends on always_rebuild will always be rebuilt.
336add_custom_command(OUTPUT always_rebuild COMMAND cmake -E echo Building for board ${BOARD})
337
338set(syscall_list_h ${CMAKE_CURRENT_BINARY_DIR}/include/generated/syscall_list.h)
339set(syscalls_json ${CMAKE_CURRENT_BINARY_DIR}/misc/generated/syscalls.json)
340
341add_custom_command(
342 OUTPUT
343 ${syscalls_json}
344 COMMAND
345 ${PYTHON_EXECUTABLE}
346 ${PROJECT_SOURCE_DIR}/scripts/parse_syscalls.py
347 --include ${PROJECT_SOURCE_DIR}/include # Read files from this dir
348 --json-file ${syscalls_json} # Write this file
349 DEPENDS always_rebuild
350 )
351
352add_custom_target(syscall_list_h_target DEPENDS ${syscall_list_h})
353add_custom_command(OUTPUT include/generated/syscall_dispatch.c ${syscall_list_h}
354 # Also, some files are written to include/generated/syscalls/
355 COMMAND
356 ${PYTHON_EXECUTABLE}
357 ${PROJECT_SOURCE_DIR}/scripts/gen_syscalls.py
358 --json-file ${syscalls_json} # Read this file
359 --base-output include/generated/syscalls # Write to this dir
360 --syscall-dispatch include/generated/syscall_dispatch.c # Write this file
361 > ${syscall_list_h} # Write stdout to this file
362 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
363 DEPENDS ${syscalls_json}
364 )
365
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200366# Generate offsets.c.obj from offsets.c
367# Generate offsets.h from offsets.c.obj
368
Carles Cufi7d764b32018-01-11 15:46:44 +0100369set(OFFSETS_C_PATH ${ZEPHYR_BASE}/arch/${ARCH}/core/offsets/offsets.c)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200370set(OFFSETS_O_PATH ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/offsets.dir/arch/${ARCH}/core/offsets/offsets.c.obj)
371set(OFFSETS_H_PATH ${PROJECT_BINARY_DIR}/include/generated/offsets.h)
372
Sebastian Bøe13a68402017-11-20 13:03:55 +0100373add_library( offsets STATIC ${OFFSETS_C_PATH})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200374target_link_libraries(offsets zephyr_interface)
Sebastian Bøe13a68402017-11-20 13:03:55 +0100375add_dependencies( offsets
376 syscall_list_h_target
377 syscall_macros_h_target
378 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200379
380add_custom_command(
381 OUTPUT ${OFFSETS_H_PATH}
Carles Cufi7d764b32018-01-11 15:46:44 +0100382 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/gen_offset_header.py
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200383 -i ${OFFSETS_O_PATH}
384 -o ${OFFSETS_H_PATH}
385 DEPENDS offsets
386)
387add_custom_target(offsets_h DEPENDS ${OFFSETS_H_PATH})
388
389zephyr_include_directories(${TOOLCHAIN_INCLUDES})
390
Sebastian Bøe89516fb2017-12-01 15:25:06 +0100391zephyr_get_include_directories_for_lang(C ZEPHYR_INCLUDES)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200392
393add_subdirectory(kernel)
394
395# Read list content
396get_property(ZEPHYR_LIBS_PROPERTY GLOBAL PROPERTY ZEPHYR_LIBS)
397
398foreach(zephyr_lib ${ZEPHYR_LIBS_PROPERTY})
399 # TODO: Could this become an INTERFACE property of zephyr_interface?
400 add_dependencies(${zephyr_lib} offsets_h)
Sebastian Bøe4ece94d2017-12-05 13:22:19 +0100401
Sebastian Bøe64edbaf2018-01-09 12:45:19 +0100402 # Verify that all (non-imported) libraries have source
403 # files. Libraries without source files are not supported because
404 # they are an indication that something has been misconfigured.
405 get_target_property(lib_imported ${zephyr_lib} IMPORTED)
406 get_target_property(lib_sources ${zephyr_lib} SOURCES)
Sebastian Bøe4ece94d2017-12-05 13:22:19 +0100407 if(lib_sources STREQUAL lib_sources-NOTFOUND
408 AND (NOT (${zephyr_lib} STREQUAL app))
Sebastian Bøe64edbaf2018-01-09 12:45:19 +0100409 AND (NOT lib_imported)
Sebastian Bøe4ece94d2017-12-05 13:22:19 +0100410 )
411 # app is not checked because it's sources are added to it after
412 # this CMakeLists.txt file has been processed
413 message(FATAL_ERROR "\
414The Zephyr library '${zephyr_lib}' was created without source files. \
Sebastian Bøe64edbaf2018-01-09 12:45:19 +0100415Empty (non-imported) libraries are not supported. \
Sebastian Bøe4ece94d2017-12-05 13:22:19 +0100416Either make sure that the library has the sources it should have, \
417or make sure it is not created when it has no source files.")
418 endif()
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200419endforeach()
420
421get_property(OUTPUT_FORMAT GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT)
422
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200423get_property(LINKER_SCRIPT_DEFINES GLOBAL PROPERTY PROPERTY_LINKER_SCRIPT_DEFINES)
424
425if(CONFIG_APPLICATION_MEMORY)
426 # Objects default to being in kernel space, and then we exclude
427 # certain items.
428 set(kernel_object_file_list
429 ${ZEPHYR_LIBS_PROPERTY}
430 kernel
431 )
432 list(
433 REMOVE_ITEM
434 kernel_object_file_list
435 app
436 )
437
438 # The zephyr libraries in zephyr/lib/ and zephyr/test/ belong in
439 # userspace.
440
441 # NB: The business logic for determing what source files are in
442 # kernel space and what source files are in user space is
443 # fragile. Fix ASAP.
444 #
445 # The intended design is that certain directories are designated as
446 # containing userspace code and others for kernel space code. The
447 # implementation we have however is not working on directories of
448 # code, it is working on zephyr libraries. It is exploiting the fact
449 # that zephyr libraries follow a naming scheme as described in
450 # extensions.cmake:zephyr_library_get_current_dir_lib_name
451 #
452 # But code from test/ and lib/ that is placed in the "zephyr"
453 # library (with zephyr_sources()) will not be in a library that is
454 # prefixed with lib__ or test__ and will end up in the wrong address
455 # space.
456 set(application_space_dirs
457 lib
458 tests
459 )
460 foreach(f ${kernel_object_file_list})
461 foreach(app_dir ${application_space_dirs})
462 if(${f} MATCHES "^${app_dir}__") # Begins with ${app_dir}__, e.g. lib__libc
463 list(
464 REMOVE_ITEM
465 kernel_object_file_list
466 ${f}
467 )
468 endif()
469 endforeach()
470 endforeach()
471
472 # Create a list ks, with relative paths to kernel space libs.
473 foreach(f ${kernel_object_file_list})
474 get_target_property(target_name ${f} NAME)
475 get_target_property(target_binary_dir ${f} BINARY_DIR)
476
477 string(REPLACE
478 ${PROJECT_BINARY_DIR}
479 ""
480 fixed_path
481 ${target_binary_dir}
482 )
483
484 # Append / if not empty
485 if(fixed_path)
486 set(fixed_path "${fixed_path}/")
487 endif()
488
489 # Cut off leading / if present
490 if(fixed_path MATCHES "^/.+")
491 string(SUBSTRING ${fixed_path} 1 -1 fixed_path)
492 endif()
493
Sebastian Bøe1c2de102018-01-02 15:54:16 +0100494 set(fixed_path "${fixed_path}lib${target_name}.a")
495
496 if(CMAKE_GENERATOR STREQUAL "Ninja")
497 # Ninja invokes the linker from the root of the build directory
498 # (APPLICATION_BINARY_DIR) instead of from the build/zephyr
499 # directory (PROJECT_BINARY_DIR). So for linker-defs.h to get
500 # the correct path we need to prefix with zephyr/.
501 set(fixed_path "zephyr/${fixed_path}")
502 endif()
503
504 list(APPEND ks ${fixed_path})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200505 endforeach()
506
507 # We are done constructing kernel_object_file_list, now we inject this
508 # information into the linker script through -D's
509 list(LENGTH kernel_object_file_list NUM_KERNEL_OBJECT_FILES)
510 list(APPEND LINKER_SCRIPT_DEFINES -DNUM_KERNEL_OBJECT_FILES=${NUM_KERNEL_OBJECT_FILES})
511 set(i 0)
512 foreach(f ${ks})
513 list(APPEND LINKER_SCRIPT_DEFINES -DKERNEL_OBJECT_FILE_${i}=${f})
514 math(EXPR i "${i}+1")
515 endforeach()
516endif() # CONFIG_APPLICATION_MEMORY
517
Andy Grosse8860fe2018-02-01 01:12:32 -0600518# Declare MPU userspace dependencies before the linker scripts to make
519# sure the order of dependencies are met
520if(CONFIG_CPU_HAS_MPU AND CONFIG_USERSPACE)
Andy Grosse1fc5c22018-02-15 08:07:17 -0600521 if(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT AND CONFIG_APPLICATION_MEMORY)
Andy Grosse8860fe2018-02-01 01:12:32 -0600522 set(ALIGN_SIZING_DEP app_sizing_prebuilt linker_app_sizing_script)
523 endif()
Wayne Ren5a0ba2f2018-02-12 19:17:04 +0800524 if(CONFIG_ARM)
Chunlin Han18560a02018-02-01 01:19:49 -0600525 set(PRIV_STACK_DEP priv_stacks_prebuilt)
Wayne Ren5a0ba2f2018-02-12 19:17:04 +0800526 endif()
Andy Grosse8860fe2018-02-01 01:12:32 -0600527endif()
528
Andy Gross1f0ff062018-01-25 11:07:03 -0600529function(construct_add_custom_command_for_linker_pass linker_output_name output_variable)
530 set(linker_cmd_file_name ${linker_output_name}.cmd)
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100531
Andy Gross1f0ff062018-01-25 11:07:03 -0600532 if (${linker_output_name} MATCHES "^linker_pass_final$")
533 set(LINKER_PASS_DEFINE -DLINKER_PASS2)
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100534 else()
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100535 set(LINKER_PASS_DEFINE "")
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100536 endif()
537
Sebastian Bøea0b91292017-12-31 10:56:32 +0100538 # Different generators deal with depfiles differently.
539 if(CMAKE_GENERATOR STREQUAL "Unix Makefiles")
540 # Note that the IMPLICIT_DEPENDS option is currently supported only
541 # for Makefile generators and will be ignored by other generators.
542 set(LINKER_SCRIPT_DEP IMPLICIT_DEPENDS C ${LINKER_SCRIPT})
543 elseif(CMAKE_GENERATOR STREQUAL "Ninja")
544 # Using DEPFILE with other generators than Ninja is an error.
545 set(LINKER_SCRIPT_DEP DEPFILE ${PROJECT_BINARY_DIR}/${linker_cmd_file_name}.dep)
546 else()
547 # TODO: How would the linker script dependencies work for non-linker
548 # script generators.
549 message(STATUS "Warning; this generator is not well supported. The
550 Linker script may not be regenerated when it should.")
551 set(LINKER_SCRIPT_DEP "")
552 endif()
553
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100554 set(${output_variable}
555 OUTPUT ${linker_cmd_file_name}
556 DEPENDS ${LINKER_SCRIPT}
557 ${LINKER_SCRIPT_DEP}
558 COMMAND ${CMAKE_C_COMPILER}
559 -x assembler-with-cpp
560 ${NOSTDINC_F}
561 -undef
562 -MD -MF ${linker_cmd_file_name}.dep -MT ${BASE_NAME}/${linker_cmd_file_name}
563 ${ZEPHYR_INCLUDES}
564 ${LINKER_SCRIPT_DEFINES}
565 ${LINKER_PASS_DEFINE}
Sebastian Bøe8062a882018-01-03 16:02:49 +0100566 -E ${LINKER_SCRIPT}
567 -P # Prevent generation of debug `#line' directives.
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100568 -o ${linker_cmd_file_name}
569 VERBATIM
570 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
571
572 PARENT_SCOPE
573 )
574endfunction()
575
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200576get_filename_component(BASE_NAME ${CMAKE_CURRENT_BINARY_DIR} NAME)
Andy Gross1f0ff062018-01-25 11:07:03 -0600577construct_add_custom_command_for_linker_pass(linker custom_command)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200578add_custom_command(
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100579 ${custom_command}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200580)
Andy Grosse8860fe2018-02-01 01:12:32 -0600581
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200582add_custom_target(
583 linker_script
584 DEPENDS
Chunlin Han18560a02018-02-01 01:19:49 -0600585 ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200586 linker.cmd
587 offsets_h
Sebastian Bøeb1ab5012017-12-14 13:03:23 +0100588 )
589
590# Give the 'linker_script' target all of the include directories so
591# that cmake can successfully find the linker_script's header
592# dependencies.
593zephyr_get_include_directories_for_lang(C
594 ZEPHYR_INCLUDE_DIRS
595 STRIP_PREFIX # Don't use a -I prefix
596 )
597set_property(TARGET
598 linker_script
599 PROPERTY INCLUDE_DIRECTORIES
600 ${ZEPHYR_INCLUDE_DIRS}
601 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200602
Anas Nashif5146dbb2017-12-21 23:43:48 -0500603get_property(E_KERNEL_ENTRY GLOBAL PROPERTY E_KERNEL_ENTRY)
604
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200605set(zephyr_lnk
606 ${LINKERFLAGPREFIX},-Map=${PROJECT_BINARY_DIR}/${KERNEL_MAP_NAME}
607 -u_OffsetAbsSyms
608 -u_ConfigAbsSyms
Anas Nashif5146dbb2017-12-21 23:43:48 -0500609 ${E_KERNEL_ENTRY}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200610 ${LINKERFLAGPREFIX},--start-group
611 ${LINKERFLAGPREFIX},--whole-archive
612 ${ZEPHYR_LIBS_PROPERTY}
613 ${LINKERFLAGPREFIX},--no-whole-archive
614 kernel
615 ${OFFSETS_O_PATH}
616 ${LINKERFLAGPREFIX},--end-group
617 ${LIB_INCLUDE_DIR}
618 -L${PROJECT_BINARY_DIR}
619 ${TOOLCHAIN_LIBS}
620 )
621
622if(CONFIG_GEN_ISR_TABLES)
623 # isr_tables.c is generated from zephyr_prebuilt by
624 # gen_isr_tables.py
625 add_custom_command(
626 OUTPUT isr_tables.c
627 COMMAND ${CMAKE_OBJCOPY}
628 -I ${OUTPUT_FORMAT}
629 -O binary
630 --only-section=.intList
631 $<TARGET_FILE:zephyr_prebuilt>
632 isrList.bin
633 COMMAND ${PYTHON_EXECUTABLE}
Carles Cufi7d764b32018-01-11 15:46:44 +0100634 ${ZEPHYR_BASE}/arch/common/gen_isr_tables.py
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200635 --output-source isr_tables.c
Rajavardhan Gundi1e6adba2017-12-24 15:18:57 +0530636 --kernel $<TARGET_FILE:zephyr_prebuilt>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200637 --intlist isrList.bin
Sebastian Bøea55279a2018-01-04 14:08:39 +0100638 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--debug>
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200639 --sw-isr-table
640 --vector-table
641 DEPENDS zephyr_prebuilt
642 )
643 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_SOURCE_FILES isr_tables.c)
644endif()
645
Chunlin Han18560a02018-02-01 01:19:49 -0600646if(CONFIG_ARM AND CONFIG_USERSPACE)
647 set(GEN_PRIV_STACKS $ENV{ZEPHYR_BASE}/scripts/gen_priv_stacks.py)
648 set(PROCESS_PRIV_STACKS_GPERF $ENV{ZEPHYR_BASE}/scripts/process_gperf.py)
649
650 set(PRIV_STACKS priv_stacks_hash.gperf)
651 set(PRIV_STACKS_OUTPUT_SRC_PRE priv_stacks_hash_preprocessed.c)
652 set(PRIV_STACKS_OUTPUT_SRC priv_stacks_hash.c)
653 set(PRIV_STACKS_OUTPUT_OBJ priv_stacks_hash.c.obj)
654 set(PRIV_STACKS_OUTPUT_OBJ_RENAMED priv_stacks_hash_renamed.o)
655
656 # Essentially what we are doing here is extracting some information
657 # out of the nearly finished elf file, generating the source code
658 # for a hash table based on that information, and then compiling and
659 # linking the hash table back into a now even more nearly finished
660 # elf file.
661
662 # Use the script GEN_PRIV_STACKS to scan the kernel binary's
663 # (zephyr_prebuilt) DWARF information to produce a table of kernel
664 # objects (PRIV_STACKS) which we will then pass to gperf
665 add_custom_command(
666 OUTPUT ${PRIV_STACKS}
667 COMMAND
668 ${PYTHON_EXECUTABLE}
669 ${GEN_PRIV_STACKS}
670 --kernel $<TARGET_FILE:priv_stacks_prebuilt>
671 --output ${PRIV_STACKS}
672 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
673 DEPENDS priv_stacks_prebuilt
674 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
675 )
676 add_custom_target(priv_stacks DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS})
677
678 # Use gperf to generate C code (PRIV_STACKS_OUTPUT_SRC_PRE) which implements a
679 # perfect hashtable based on PRIV_STACKS
680 add_custom_command(
681 OUTPUT ${PRIV_STACKS_OUTPUT_SRC_PRE}
682 COMMAND
683 ${GPERF} -C
684 --output-file ${PRIV_STACKS_OUTPUT_SRC_PRE}
685 ${PRIV_STACKS}
686 DEPENDS priv_stacks
687 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
688 )
689 add_custom_target(priv_stacks_output_src_pre DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_SRC_PRE})
690
691 # For our purposes the code/data generated by gperf is not optimal.
692 #
693 # The script PROCESS_GPERF creates a new c file OUTPUT_SRC based on
694 # OUTPUT_SRC_PRE to greatly reduce the amount of code/data generated
695 # since we know we are always working with pointer values
696 add_custom_command(
697 OUTPUT ${PRIV_STACKS_OUTPUT_SRC}
698 COMMAND
699 ${PROCESS_PRIV_STACKS_GPERF}
700 -i ${PRIV_STACKS_OUTPUT_SRC_PRE}
701 -o ${PRIV_STACKS_OUTPUT_SRC}
702 -p "struct _k_priv_stack_map"
703 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
704 DEPENDS priv_stacks_output_src_pre
705 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
706 )
707 add_custom_target(priv_stacks_output_src DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_SRC})
708
709 # We need precise control of where generated text/data ends up in the final
710 # kernel image. Disable function/data sections and use objcopy to move
711 # generated data into special section names
712 add_library(priv_stacks_output_lib STATIC
713 ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_SRC}
714 )
715
716 target_link_libraries(priv_stacks_output_lib zephyr_interface)
717
718 # Turn off -ffunction-sections, etc.
719 # NB: Using a library instead of target_compile_options(priv_stacks_output_lib
720 # [...]) because a library's options have precedence
721 add_library(priv_stacks_output_lib_interface INTERFACE)
722 target_compile_options(priv_stacks_output_lib_interface INTERFACE
723 -fno-function-sections
724 -fno-data-sections
725 )
726 target_link_libraries(priv_stacks_output_lib priv_stacks_output_lib_interface)
727
728 set(PRIV_STACKS_OUTPUT_OBJ_PATH ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/priv_stacks_output_lib.dir/${PRIV_STACKS_OUTPUT_OBJ})
729
730 add_custom_command(
731 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_OBJ_RENAMED}
732 COMMAND
733 ${CMAKE_OBJCOPY}
734 --rename-section .bss=.priv_stacks.noinit
735 --rename-section .data=.priv_stacks.data
736 --rename-section .text=.priv_stacks.text
737 --rename-section .rodata=.priv_stacks.rodata
738 ${PRIV_STACKS_OUTPUT_OBJ_PATH}
739 ${PRIV_STACKS_OUTPUT_OBJ_RENAMED}
740 DEPENDS priv_stacks_output_lib
741 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
742 )
743 add_custom_target(priv_stacks_output_obj_renamed DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_OBJ_RENAMED})
744
745 add_library(priv_stacks_output_obj_renamed_lib STATIC IMPORTED GLOBAL)
746 set_property(
747 TARGET priv_stacks_output_obj_renamed_lib
748 PROPERTY
749 IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${PRIV_STACKS_OUTPUT_OBJ_RENAMED}
750 )
751 add_dependencies(
752 priv_stacks_output_obj_renamed_lib
753 priv_stacks_output_obj_renamed
754 )
755
756 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_OBJECT_FILES priv_stacks_output_obj_renamed_lib)
757endif()
758
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200759if(CONFIG_USERSPACE)
Carles Cufi7d764b32018-01-11 15:46:44 +0100760 set(GEN_KOBJ_LIST ${ZEPHYR_BASE}/scripts/gen_kobject_list.py)
761 set(PROCESS_GPERF ${ZEPHYR_BASE}/scripts/process_gperf.py)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200762
763 set(OBJ_LIST kobject_hash.gperf)
764 set(OUTPUT_SRC_PRE kobject_hash_preprocessed.c)
765 set(OUTPUT_SRC kobject_hash.c)
766 set(OUTPUT_OBJ kobject_hash.c.obj)
767 set(OUTPUT_OBJ_RENAMED kobject_hash_renamed.o)
768
769 # Essentially what we are doing here is extracting some information
770 # out of the nearly finished elf file, generating the source code
771 # for a hash table based on that information, and then compiling and
772 # linking the hash table back into a now even more nearly finished
773 # elf file.
774
775 # Use the script GEN_KOBJ_LIST to scan the kernel binary's
776 # (zephyr_prebuilt) DWARF information to produce a table of kernel
777 # objects (OBJ_LIST) which we will then pass to gperf
778 add_custom_command(
779 OUTPUT ${OBJ_LIST}
780 COMMAND
781 ${PYTHON_EXECUTABLE}
782 ${GEN_KOBJ_LIST}
783 --kernel $<TARGET_FILE:zephyr_prebuilt>
784 --output ${OBJ_LIST}
785 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
786 DEPENDS zephyr_prebuilt
787 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
788 )
789 add_custom_target(obj_list DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OBJ_LIST})
790
791 # Use gperf to generate C code (OUTPUT_SRC_PRE) which implements a
792 # perfect hashtable based on OBJ_LIST
793 add_custom_command(
794 OUTPUT ${OUTPUT_SRC_PRE}
795 COMMAND
796 ${GPERF}
797 --output-file ${OUTPUT_SRC_PRE}
798 ${OBJ_LIST}
Sebastian Bøef5758b52018-01-31 10:42:46 +0100799 DEPENDS obj_list ${OBJ_LIST}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200800 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
801 )
802 add_custom_target(output_src_pre DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_SRC_PRE})
803
804 # For our purposes the code/data generated by gperf is not optimal.
805 #
806 # The script PROCESS_GPERF creates a new c file OUTPUT_SRC based on
807 # OUTPUT_SRC_PRE to greatly reduce the amount of code/data generated
808 # since we know we are always working with pointer values
809 add_custom_command(
810 OUTPUT ${OUTPUT_SRC}
811 COMMAND
812 ${PROCESS_GPERF}
813 -i ${OUTPUT_SRC_PRE}
814 -o ${OUTPUT_SRC}
Chunlin Han18560a02018-02-01 01:19:49 -0600815 -p "struct _k_object"
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200816 $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
Sebastian Bøef5758b52018-01-31 10:42:46 +0100817 DEPENDS output_src_pre ${OUTPUT_SRC_PRE}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200818 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
819 )
820 add_custom_target(output_src DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_SRC})
821
822 # We need precise control of where generated text/data ends up in the final
823 # kernel image. Disable function/data sections and use objcopy to move
824 # generated data into special section names
825 add_library(output_lib STATIC
826 ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_SRC}
827 )
828
Adithya Baglodyce9a5a22018-01-29 15:58:51 +0530829 # always compile kobject_hash.c at optimization -Os
830 set_source_files_properties(${OUTPUT_SRC} PROPERTIES COMPILE_FLAGS -Os)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200831 target_link_libraries(output_lib zephyr_interface)
832
833 # Turn off -ffunction-sections, etc.
834 # NB: Using a library instead of target_compile_options(output_lib
835 # [...]) because a library's options have precedence
836 add_library(output_lib_interface INTERFACE)
837 target_compile_options(output_lib_interface INTERFACE
838 -fno-function-sections
839 -fno-data-sections
840 )
841 target_link_libraries(output_lib output_lib_interface)
842
843 set(OUTPUT_OBJ_PATH ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/output_lib.dir/${OUTPUT_OBJ})
844
845 add_custom_command(
846 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_OBJ_RENAMED}
847 COMMAND
848 ${CMAKE_OBJCOPY}
849 --rename-section .data=.kobject_data.data
850 --rename-section .text=.kobject_data.text
851 --rename-section .rodata=.kobject_data.rodata
852 ${OUTPUT_OBJ_PATH}
853 ${OUTPUT_OBJ_RENAMED}
854 DEPENDS output_lib
855 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
856 )
857 add_custom_target(output_obj_renamed DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_OBJ_RENAMED})
858
859 add_library(output_obj_renamed_lib STATIC IMPORTED GLOBAL)
860 set_property(
861 TARGET output_obj_renamed_lib
862 PROPERTY
863 IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_OBJ_RENAMED}
864 )
865 add_dependencies(
866 output_obj_renamed_lib
867 output_obj_renamed
868 )
869
870 set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_OBJECT_FILES output_obj_renamed_lib)
871endif()
872
873# Read global variables into local variables
874get_property(GKOF GLOBAL PROPERTY GENERATED_KERNEL_OBJECT_FILES)
875get_property(GKSF GLOBAL PROPERTY GENERATED_KERNEL_SOURCE_FILES)
876
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200877get_property(TOPT GLOBAL PROPERTY TOPT)
878set_ifndef( TOPT -T)
879
Andy Grosse8860fe2018-02-01 01:12:32 -0600880configure_file(
881 $ENV{ZEPHYR_BASE}/include/arch/arm/cortex_m/scripts/app_data_alignment.ld
882 ${PROJECT_BINARY_DIR}/include/generated/app_data_alignment.ld)
883
884if(CONFIG_CPU_HAS_MPU AND CONFIG_USERSPACE)
885
Andy Grosse1fc5c22018-02-15 08:07:17 -0600886 if(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT AND CONFIG_APPLICATION_MEMORY)
Andy Grosse8860fe2018-02-01 01:12:32 -0600887
888 construct_add_custom_command_for_linker_pass(linker_app_sizing custom_command)
889 add_custom_command(
890 ${custom_command}
891 )
892
893 add_custom_target(
894 linker_app_sizing_script
895 DEPENDS
896 linker_app_sizing.cmd
897 offsets_h
898 )
899
900 set_property(TARGET
901 linker_app_sizing_script
902 PROPERTY INCLUDE_DIRECTORIES
903 ${ZEPHYR_INCLUDE_DIRS}
904 )
905
906 # For systems with MPUs, the size of the application data section must
907 # be determined so that MPU alignment requirements can be met.
908 # Create a app_sizing_prebuilt target so we can do this before the
909 # other ELF files are built
910 set(GEN_APP_ALIGN $ENV{ZEPHYR_BASE}/scripts/gen_alignment_script.py)
911 add_executable( app_sizing_prebuilt misc/empty_file.c)
912 target_link_libraries(app_sizing_prebuilt ${TOPT} ${PROJECT_BINARY_DIR}/linker_app_sizing.cmd ${zephyr_lnk})
913 set_property(TARGET app_sizing_prebuilt PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker_app_sizing.cmd)
914 add_dependencies( app_sizing_prebuilt linker_app_sizing_script offsets)
915
916 add_custom_command(
917 TARGET app_sizing_prebuilt
918 POST_BUILD
919 COMMAND ${PYTHON_EXECUTABLE} ${GEN_APP_ALIGN}
920 --output ./include/generated/app_data_alignment.ld
921 --kernel $<TARGET_FILE:app_sizing_prebuilt>
922 VERBATIM
923 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
924 )
925 endif()
Chunlin Han18560a02018-02-01 01:19:49 -0600926
Wayne Ren5a0ba2f2018-02-12 19:17:04 +0800927if(CONFIG_ARM)
Chunlin Han18560a02018-02-01 01:19:49 -0600928 construct_add_custom_command_for_linker_pass(linker_priv_stacks custom_command)
929 add_custom_command(
930 ${custom_command}
931 )
932
933 add_custom_target(
934 linker_priv_stacks_script
935 DEPENDS
936 ${ALIGN_SIZING_DEP}
937 linker_priv_stacks.cmd
938 offsets_h
939 )
940
941 set_property(TARGET
942 linker_priv_stacks_script
943 PROPERTY INCLUDE_DIRECTORIES
944 ${ZEPHYR_INCLUDE_DIRS}
945 )
946
947 set(PRIV_STACK_LIB priv_stacks_output_obj_renamed_lib)
948 add_executable( priv_stacks_prebuilt misc/empty_file.c)
949 target_link_libraries(priv_stacks_prebuilt ${TOPT} ${PROJECT_BINARY_DIR}/linker_priv_stacks.cmd ${zephyr_lnk})
950 set_property(TARGET priv_stacks_prebuilt PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker_priv_stacks.cmd)
951 add_dependencies( priv_stacks_prebuilt ${ALIGN_SIZING_DEP} linker_priv_stacks_script offsets)
Wayne Ren5a0ba2f2018-02-12 19:17:04 +0800952endif()
Chunlin Han18560a02018-02-01 01:19:49 -0600953
Andy Grosse8860fe2018-02-01 01:12:32 -0600954endif()
955
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200956# FIXME: Is there any way to get rid of empty_file.c?
957add_executable( zephyr_prebuilt misc/empty_file.c)
Chunlin Han18560a02018-02-01 01:19:49 -0600958target_link_libraries(zephyr_prebuilt ${TOPT} ${PROJECT_BINARY_DIR}/linker.cmd ${PRIV_STACK_LIB} ${zephyr_lnk})
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200959set_property(TARGET zephyr_prebuilt PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker.cmd)
Chunlin Han18560a02018-02-01 01:19:49 -0600960add_dependencies( zephyr_prebuilt ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP} linker_script offsets)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200961
Alberto Escolar Piedras76f764412017-10-03 16:31:55 +0200962
963if(NOT CONFIG_NATIVE_APPLICATION)
964 set(NOSTDINC_F -nostdinc)
965endif()
966
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200967if(GKOF OR GKSF)
968 set(logical_target_for_zephyr_elf kernel_elf)
969
970 # The second linker pass uses the same source linker script of the
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100971 # first pass (LINKER_SCRIPT), but this time with a different output
972 # file and preprocessed with the define LINKER_PASS2.
Andy Gross1f0ff062018-01-25 11:07:03 -0600973 construct_add_custom_command_for_linker_pass(linker_pass_final custom_command)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200974 add_custom_command(
Sebastian Bøeb85dd3c2017-12-31 10:39:23 +0100975 ${custom_command}
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200976 )
Andy Grosse8860fe2018-02-01 01:12:32 -0600977
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200978 add_custom_target(
Andy Gross1f0ff062018-01-25 11:07:03 -0600979 linker_pass_final_script
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200980 DEPENDS
Chunlin Han18560a02018-02-01 01:19:49 -0600981 ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP}
Andy Grosse8860fe2018-02-01 01:12:32 -0600982 zephyr_prebuilt
Andy Gross1f0ff062018-01-25 11:07:03 -0600983 linker_pass_final.cmd
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200984 offsets_h
985 )
Sebastian Bøeb1ab5012017-12-14 13:03:23 +0100986 set_property(TARGET
Andy Gross1f0ff062018-01-25 11:07:03 -0600987 linker_pass_final_script
Sebastian Bøeb1ab5012017-12-14 13:03:23 +0100988 PROPERTY INCLUDE_DIRECTORIES
989 ${ZEPHYR_INCLUDE_DIRS}
990 )
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200991
992 add_executable( kernel_elf misc/empty_file.c ${GKSF})
Andy Gross1f0ff062018-01-25 11:07:03 -0600993 target_link_libraries(kernel_elf ${GKOF} ${TOPT} ${PROJECT_BINARY_DIR}/linker_pass_final.cmd ${zephyr_lnk})
994 set_property(TARGET kernel_elf PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker_pass_final.cmd)
Chunlin Han18560a02018-02-01 01:19:49 -0600995 add_dependencies( kernel_elf ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP} linker_pass_final_script)
Sebastian Bøe12f8f762017-10-27 15:43:34 +0200996else()
997 set(logical_target_for_zephyr_elf zephyr_prebuilt)
998 # Use the prebuilt elf as the final elf since we don't have a
999 # generation stage.
1000endif()
1001
1002# To avoid having the same logical target name for the zephyr lib and
1003# the zephyr elf, we set the kernel_elf file name to zephyr.elf.
1004set_target_properties(${logical_target_for_zephyr_elf} PROPERTIES OUTPUT_NAME ${KERNEL_NAME})
1005
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001006set(post_build_commands "")
1007
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001008list_append_ifdef(CONFIG_CHECK_LINK_MAP
1009 post_build_commands
Carles Cufi7d764b32018-01-11 15:46:44 +01001010 COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/check_link_map.py ${KERNEL_MAP_NAME}
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001011 )
1012
1013list_append_ifdef(
1014 CONFIG_BUILD_OUTPUT_HEX
1015 post_build_commands
1016 COMMAND ${CMAKE_OBJCOPY} -S -Oihex -R .comment -R COMMON -R .eh_frame ${KERNEL_ELF_NAME} ${KERNEL_HEX_NAME}
1017 )
1018
1019list_append_ifdef(
1020 CONFIG_BUILD_OUTPUT_BIN
1021 post_build_commands
1022 COMMAND ${CMAKE_OBJCOPY} -S -Obinary -R .comment -R COMMON -R .eh_frame ${KERNEL_ELF_NAME} ${KERNEL_BIN_NAME}
1023 )
Anas Nashif4592ff22017-11-23 07:54:26 -05001024
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001025list_append_ifdef(
1026 CONFIG_BUILD_OUTPUT_S19
1027 post_build_commands
1028 COMMAND ${CMAKE_OBJCOPY} --srec-len 1 --output-target=srec ${KERNEL_ELF_NAME} ${KERNEL_S19_NAME}
1029 )
1030
1031list_append_ifdef(
Anas Nashif1f1143a2017-11-22 13:03:53 -05001032 CONFIG_OUTPUT_DISASSEMBLY
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001033 post_build_commands
1034 COMMAND ${CMAKE_OBJDUMP} -S ${KERNEL_ELF_NAME} > ${KERNEL_LST_NAME}
1035 )
1036
1037list_append_ifdef(
Anas Nashif1f1143a2017-11-22 13:03:53 -05001038 CONFIG_OUTPUT_STAT
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001039 post_build_commands
1040 COMMAND ${CMAKE_READELF} -e ${KERNEL_ELF_NAME} > ${KERNEL_STAT_NAME}
1041 )
1042
1043list_append_ifdef(
1044 CONFIG_BUILD_OUTPUT_STRIPPED
1045 post_build_commands
1046 COMMAND ${CMAKE_STRIP} --strip-all ${KERNEL_ELF_NAME} -o ${KERNEL_STRIP_NAME}
1047 )
1048
Anas Nashif4592ff22017-11-23 07:54:26 -05001049list_append_ifdef(
1050 CONFIG_BUILD_OUTPUT_EXE
1051 post_build_commands
1052 COMMAND ${CMAKE_COMMAND} -E rename ${KERNEL_ELF_NAME} ${KERNEL_EXE_NAME}
1053 )
1054
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001055add_custom_command(
1056 TARGET ${logical_target_for_zephyr_elf}
1057 POST_BUILD
Sebastian Bøee51ce4d2017-11-20 15:37:59 +01001058 ${post_build_commands}
1059 COMMENT "Generating files from zephyr.elf for board: ${BOARD}"
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001060 # NB: COMMENT only works for some CMake-Generators
1061)
1062
Sebastian Bøedbdd7222017-12-19 13:20:10 +01001063if(CONFIG_OUTPUT_PRINT_MEMORY_USAGE)
1064 # Use --print-memory-usage with the first link.
1065 #
1066 # Don't use this option with the second link because seeing it twice
1067 # could confuse users and using it on the second link would suppress
1068 # it when the first link has a ram/flash-usage issue.
1069 set(option ${LINKERFLAGPREFIX},--print-memory-usage)
1070 string(MAKE_C_IDENTIFIER check${option} check)
Sebastian Bøeba0b2832017-12-21 10:16:43 +01001071
Sebastian Bøec34b7a32017-12-27 15:21:54 +01001072 set(SAVED_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
Sebastian Bøeba0b2832017-12-21 10:16:43 +01001073 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${option}")
1074 check_c_compiler_flag("" ${check})
Sebastian Bøec34b7a32017-12-27 15:21:54 +01001075 set(CMAKE_REQUIRED_FLAGS ${SAVED_CMAKE_REQUIRED_FLAGS})
Sebastian Bøeba0b2832017-12-21 10:16:43 +01001076
Sebastian Bøedbdd7222017-12-19 13:20:10 +01001077 target_link_libraries_ifdef(${check} zephyr_prebuilt ${option})
1078endif()
1079
Anas Nashifc15d3c92017-11-21 18:54:55 -05001080if(EMU_PLATFORM)
Carles Cufi7d764b32018-01-11 15:46:44 +01001081 include(${ZEPHYR_BASE}/cmake/emu/${EMU_PLATFORM}.cmake)
Anas Nashiffd276ae2017-12-21 16:45:45 -05001082else()
1083 add_custom_target(run
1084 COMMAND
1085 ${CMAKE_COMMAND} -E echo
1086 "==================================================="
1087 "Emulation/Simulation not supported with this board."
1088 "==================================================="
1089 )
Anas Nashifc15d3c92017-11-21 18:54:55 -05001090endif()
1091
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001092add_subdirectory(cmake/flash)
1093
1094add_subdirectory(cmake/usage)
1095add_subdirectory(cmake/reports)
1096
Sebastian Bøe12f8f762017-10-27 15:43:34 +02001097if(CONFIG_ASSERT)
1098 message(WARNING "
1099 ------------------------------------------------------------
1100 --- WARNING: __ASSERT() statements are globally ENABLED ---
1101 --- The kernel will run more slowly and uses more memory ---
1102 ------------------------------------------------------------"
1103)
1104endif()
1105
1106if(CONFIG_BOARD_DEPRECATED)
1107 message(WARNING "
1108 WARNING: The board '${BOARD}' is deprecated and will be
1109 removed in version ${CONFIG_BOARD_DEPRECATED}"
1110)
1111endif()
Andrew Boiedf48e112018-01-12 09:54:24 -08001112
1113if(CONFIG_X86 AND CONFIG_USERSPACE AND NOT CONFIG_X86_NO_MELTDOWN)
1114 message(WARNING "
1115 WARNING: You have enabled CONFIG_USERSPACE on an x86-based target.
1116 If your CPU is vulnerable to the Meltdown CPU bug, security of
1117 supervisor-only memory pages is not guaranteed. This version of Zephyr
1118 does not contain a fix for this issue."
1119)
1120endif()