Sebastian Bøe | 12f8f76 | 2017-10-27 15:43:34 +0200 | [diff] [blame] | 1 | project(Zephyr-Kernel VERSION ${PROJECT_VERSION}) |
| 2 | enable_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. |
| 17 | check_c_compiler_flag("" toolchain_is_ok) |
| 18 | assert(toolchain_is_ok "The toolchain is unable to build a dummy C file. See CMakeError.log.") |
| 19 | |
| 20 | # Do not generate make install target. |
| 21 | set(CMAKE_SKIP_INSTALL_RULES ON) |
| 22 | |
| 23 | set(CMAKE_EXECUTABLE_SUFFIX .elf) |
| 24 | |
| 25 | set(SOC_NAME ${CONFIG_SOC}) |
| 26 | set(SOC_SERIES ${CONFIG_SOC_SERIES}) |
| 27 | set(SOC_FAMILY ${CONFIG_SOC_FAMILY}) |
| 28 | |
| 29 | if("${SOC_SERIES}" STREQUAL "") |
| 30 | set(SOC_PATH ${SOC_NAME}) |
| 31 | else() |
| 32 | set(SOC_PATH ${SOC_FAMILY}/${SOC_SERIES}) |
| 33 | endif() |
| 34 | |
| 35 | if(NOT PROPERTY_LINKER_SCRIPT_DEFINES) |
| 36 | set_property(GLOBAL PROPERTY PROPERTY_LINKER_SCRIPT_DEFINES -D__GCC_LINKER_CMD__) |
| 37 | endif() |
| 38 | |
| 39 | define_property(GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT BRIEF_DOCS " " FULL_DOCS " ") |
| 40 | set_property( GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT elf32-little${ARCH}) |
| 41 | |
| 42 | # zephyr_interface is a source-less library that has all the global |
| 43 | # compiler options needed by all source files. All zephyr libraries, |
| 44 | # including the library named "zephyr" link with this library to |
| 45 | # obtain these flags. |
| 46 | add_library(zephyr_interface INTERFACE) |
| 47 | |
| 48 | # zephyr is a catchall CMake library for source files that can be |
| 49 | # built purely with the include paths, defines, and other compiler |
| 50 | # flags that come with zephyr_interface. |
| 51 | zephyr_library_named(zephyr) |
| 52 | |
| 53 | zephyr_include_directories( |
| 54 | kernel/include |
| 55 | arch/${ARCH}/include |
| 56 | arch/${ARCH}/soc/${SOC_PATH} |
| 57 | arch/${ARCH}/soc/${SOC_PATH}/include |
| 58 | arch/${ARCH}/soc/${SOC_FAMILY}/include |
| 59 | ${BOARD_DIR} |
| 60 | include |
| 61 | include/drivers |
| 62 | ${PROJECT_BINARY_DIR}/include/generated |
| 63 | ${USERINCLUDE} |
| 64 | ${STDINCLUDE} |
| 65 | ) |
| 66 | |
| 67 | |
| 68 | zephyr_compile_definitions( |
| 69 | KERNEL |
| 70 | __ZEPHYR__=1 |
| 71 | _FORTIFY_SOURCE=2 |
| 72 | ) |
| 73 | |
| 74 | # We need to set an optimization level. |
| 75 | # Default to -Os |
| 76 | # unless CONFIG_DEBUG is set, then it is -Og |
| 77 | # |
| 78 | # also, some toolchain's break with -Os, and some toolchain's break |
| 79 | # with -Og so allow them to override what flag to use |
| 80 | # |
| 81 | # Finally, the user can use Kconfig to add compiler options that will |
| 82 | # come after these options and override them |
| 83 | set_ifndef(OPTIMIZE_FOR_SIZE_FLAG "-Os") |
| 84 | set_ifndef(OPTIMIZE_FOR_DEBUG_FLAG "-Og") |
| 85 | if(CONFIG_DEBUG) |
| 86 | set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_DEBUG_FLAG}) |
| 87 | else() |
| 88 | set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SIZE_FLAG}) # Default |
| 89 | endif() |
| 90 | |
| 91 | zephyr_compile_options( |
| 92 | ${OPTIMIZATION_FLAG} # Usually -Os |
| 93 | -g # TODO: build configuration enough? |
| 94 | -Wall |
| 95 | -Wformat |
| 96 | -Wformat-security |
| 97 | -Wno-format-zero-length |
| 98 | -Wno-main |
| 99 | -ffreestanding |
| 100 | -include ${AUTOCONF_H} |
| 101 | ) |
| 102 | |
| 103 | zephyr_compile_options( |
| 104 | $<$<COMPILE_LANGUAGE:C>:-std=c99> |
| 105 | |
| 106 | $<$<COMPILE_LANGUAGE:CXX>:-std=c++11> |
| 107 | $<$<COMPILE_LANGUAGE:CXX>:-fcheck-new> |
| 108 | $<$<COMPILE_LANGUAGE:CXX>:-ffunction-sections> |
| 109 | $<$<COMPILE_LANGUAGE:CXX>:-fdata-sections> |
| 110 | $<$<COMPILE_LANGUAGE:CXX>:-fno-rtti> |
| 111 | $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions> |
| 112 | |
| 113 | $<$<COMPILE_LANGUAGE:ASM>:-xassembler-with-cpp> |
| 114 | $<$<COMPILE_LANGUAGE:ASM>:-D_ASMLANGUAGE> |
| 115 | ) |
| 116 | |
| 117 | zephyr_ld_options( |
| 118 | -nostartfiles |
| 119 | -nodefaultlibs |
| 120 | -nostdlib |
| 121 | -static |
| 122 | -no-pie |
| 123 | ) |
| 124 | |
| 125 | # ========================================================================== |
| 126 | # |
| 127 | # cmake -DW=... settings |
| 128 | # |
| 129 | # W=1 - warnings that may be relevant and does not occur too often |
| 130 | # W=2 - warnings that occur quite often but may still be relevant |
| 131 | # W=3 - the more obscure warnings, can most likely be ignored |
| 132 | # ========================================================================== |
| 133 | if(W MATCHES "1") |
| 134 | zephyr_compile_options( |
| 135 | -Wextra |
| 136 | -Wunused |
| 137 | -Wno-unused-parameter |
| 138 | -Wmissing-declarations |
| 139 | -Wmissing-format-attribute |
| 140 | -Wold-style-definition |
| 141 | ) |
| 142 | zephyr_cc_option( |
| 143 | -Wmissing-prototypes |
| 144 | -Wmissing-include-dirs |
| 145 | -Wunused-but-set-variable |
| 146 | -Wno-missing-field-initializers |
| 147 | ) |
| 148 | endif() |
| 149 | |
| 150 | if(W MATCHES "2") |
| 151 | zephyr_compile_options( |
| 152 | -Waggregate-return |
| 153 | -Wcast-align |
| 154 | -Wdisabled-optimization |
| 155 | -Wnested-externs |
| 156 | -Wshadow |
| 157 | ) |
| 158 | zephyr_cc_option( |
| 159 | -Wlogical-op |
| 160 | -Wmissing-field-initializers |
| 161 | ) |
| 162 | endif() |
| 163 | |
| 164 | if(W MATCHES "3") |
| 165 | zephyr_compile_options( |
| 166 | -Wbad-function-cast |
| 167 | -Wcast-qual |
| 168 | -Wconversion |
| 169 | -Wpacked |
| 170 | -Wpadded |
| 171 | -Wpointer-arith |
| 172 | -Wredundant-decls |
| 173 | -Wswitch-default |
| 174 | ) |
| 175 | zephyr_cc_option( |
| 176 | -Wpacked-bitfield-compat |
| 177 | -Wvla |
| 178 | ) |
| 179 | endif() |
| 180 | |
| 181 | # Allow the user to inject options when calling cmake, e.g. |
| 182 | # 'cmake -DEXTRA_CFLAGS="-Werror -Wno-deprecated-declarations" ..' |
| 183 | |
| 184 | separate_arguments(EXTRA_CPPFLAGS_AS_LIST UNIX_COMMAND ${EXTRA_CPPFLAGS}) |
| 185 | separate_arguments(EXTRA_LD_FLAGS_AS_LIST UNIX_COMMAND ${EXTRA_LD_FLAGS}) |
| 186 | separate_arguments(EXTRA_CFLAGS_AS_LIST UNIX_COMMAND ${EXTRA_CFLAGS}) |
| 187 | separate_arguments(EXTRA_CXXFLAGS_AS_LIST UNIX_COMMAND ${EXTRA_CXXFLAGS}) |
| 188 | separate_arguments(EXTRA_AFLAGS_AS_LIST UNIX_COMMAND ${EXTRA_AFLAGS}) |
| 189 | |
| 190 | if(EXTRA_CPPFLAGS) |
| 191 | zephyr_compile_definitions(${EXTRA_CPPFLAGS_AS_LIST}) |
| 192 | endif() |
| 193 | if(EXTRA_LDFLAGS) |
| 194 | zephyr_link_libraries(${EXTRA_LDFLAGS_AS_LIST}) |
| 195 | endif() |
| 196 | if(EXTRA_CFLAGS) |
| 197 | foreach(F ${EXTRA_CFLAGS_AS_LIST}) |
| 198 | zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:${F}>) |
| 199 | endforeach() |
| 200 | endif() |
| 201 | if(EXTRA_CXXFLAGS) |
| 202 | foreach(F ${EXTRA_CXXFLAGS_AS_LIST}) |
| 203 | zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:${F}>) |
| 204 | endforeach() |
| 205 | endif() |
| 206 | if(EXTRA_AFLAGS) |
| 207 | foreach(F ${EXTRA_AFLAGS_AS_LIST}) |
| 208 | zephyr_compile_options($<$<COMPILE_LANGUAGE:ASM>:${F}>) |
| 209 | endforeach() |
| 210 | endif() |
| 211 | |
| 212 | if(CONFIG_READABLE_ASM) |
| 213 | zephyr_cc_option(-fno-reorder-blocks) |
| 214 | zephyr_cc_option(-fno-ipa-cp-clone) |
| 215 | zephyr_cc_option(-fno-partial-inlining) |
| 216 | endif() |
| 217 | |
| 218 | zephyr_cc_option(-fno-asynchronous-unwind-tables) |
| 219 | zephyr_cc_option(-fno-pie) |
| 220 | zephyr_cc_option(-fno-pic) |
| 221 | zephyr_cc_option(-fno-strict-overflow) |
| 222 | zephyr_cc_option(-Wno-pointer-sign) |
| 223 | |
| 224 | if(CONFIG_STACK_CANARIES) |
| 225 | zephyr_cc_option(-fstack-protector-all) |
| 226 | else() |
| 227 | zephyr_cc_option(-fno-stack-protector) |
| 228 | endif() |
| 229 | |
| 230 | if(CONFIG_OVERRIDE_FRAME_POINTER_DEFAULT) |
| 231 | if(CONFIG_OMIT_FRAME_POINTER) |
| 232 | zephyr_cc_option(-fomit-frame-pointer) |
| 233 | else() |
| 234 | zephyr_cc_option(-fno-omit-frame-pointer) |
| 235 | endif() |
| 236 | endif() |
| 237 | |
| 238 | zephyr_compile_options(${CONFIG_COMPILER_OPT}) |
| 239 | |
| 240 | # TODO: Include arch compiler options at this point. |
| 241 | |
| 242 | # TODO: This Clang check is broken |
| 243 | if(CMAKE_C_COMPILER_ID STREQUAL "Clang") |
| 244 | zephyr_cc_option( |
| 245 | -Wno-unknown-warning-option |
| 246 | -Wno-unused-variable |
| 247 | -Wno-format-invalid-specifier |
| 248 | -Wno-gnu |
| 249 | # comparison of unsigned expression < 0 is always false |
| 250 | -Wno-tautological-compare |
| 251 | ) |
| 252 | else() # GCC assumed |
| 253 | zephyr_cc_option( |
| 254 | -Wno-unused-but-set-variable |
| 255 | -fno-reorder-functions |
| 256 | ) |
| 257 | if(NOT ${ZEPHYR_GCC_VARIANT} STREQUAL "xcc") |
| 258 | zephyr_cc_option(-fno-defer-pop) |
| 259 | endif() |
| 260 | endif() |
| 261 | |
| 262 | zephyr_cc_option_ifdef(CONFIG_DEBUG_SECTION_MISMATCH -fno-inline-functions-called-once) |
| 263 | zephyr_cc_option_ifdef(CONFIG_STACK_USAGE -fstack-usage) |
| 264 | |
| 265 | zephyr_compile_options(-nostdinc) |
| 266 | zephyr_system_include_directories(${NOSTDINC}) |
| 267 | |
| 268 | # Force an error when things like SYS_INIT(foo, ...) occur with a missing header. |
| 269 | zephyr_cc_option(-Werror=implicit-int) |
| 270 | |
| 271 | # Prohibit date/time macros, which would make the build non-deterministic |
| 272 | # cc-option(-Werror=date-time) |
| 273 | |
| 274 | # TODO: Archiver arguments |
| 275 | # ar_option(D) |
| 276 | |
| 277 | if(IS_TEST) |
| 278 | add_subdirectory(cmake/test) |
| 279 | endif() |
| 280 | |
| 281 | set_ifndef(LINKERFLAGPREFIX -Wl) |
| 282 | zephyr_ld_options( |
| 283 | ${LINKERFLAGPREFIX},-X |
| 284 | ${LINKERFLAGPREFIX},-N |
| 285 | ${LINKERFLAGPREFIX},--gc-sections |
| 286 | ${LINKERFLAGPREFIX},--build-id=none |
| 287 | ) |
| 288 | |
| 289 | if(CONFIG_HAVE_CUSTOM_LINKER_SCRIPT) |
| 290 | set(LINKER_SCRIPT ${APPLICATION_SOURCE_DIR}/${CONFIG_CUSTOM_LINKER_SCRIPT}) |
| 291 | if(NOT EXISTS LINKER_SCRIPT) |
| 292 | set(LINKER_SCRIPT ${CONFIG_CUSTOM_LINKER_SCRIPT}) |
| 293 | if(NOT EXISTS LINKER_SCRIPT) |
| 294 | message(FATAL_ERROR "CONFIG_HAVE_CUSTOM_LINKER_SCRIPT was set, but no linker script was found at '${CONFIG_CUSTOM_LINKER_SCRIPT}'") |
| 295 | endif() |
| 296 | endif() |
| 297 | else() |
| 298 | # Try a board specific linker file |
| 299 | set(LINKER_SCRIPT ${BOARD_DIR}/linker.ld) |
| 300 | if(NOT EXISTS ${LINKER_SCRIPT}) |
| 301 | # If not available, try an SoC specific linker file |
| 302 | set(LINKER_SCRIPT $ENV{ZEPHYR_BASE}/arch/${ARCH}/soc/${SOC_PATH}/linker.ld) |
| 303 | endif() |
| 304 | endif() |
| 305 | |
| 306 | if(NOT EXISTS ${LINKER_SCRIPT}) |
| 307 | message(FATAL_ERROR "Could not find linker script: '${LINKER_SCRIPT}'. Corrupted configuration?") |
| 308 | endif() |
| 309 | |
| 310 | configure_file(version.h.in ${PROJECT_BINARY_DIR}/include/generated/version.h) |
| 311 | |
| 312 | add_subdirectory(lib) |
| 313 | add_subdirectory(misc) |
| 314 | # We use include instead of add_subdirectory to avoid creating a new directory scope. |
| 315 | # This is because source file properties are directory scoped, including the GENERATED |
| 316 | # property which is set implicitly for custom command outputs |
| 317 | include(misc/generated/CMakeLists.txt) |
| 318 | add_subdirectory(boards) |
| 319 | add_subdirectory(ext) |
| 320 | add_subdirectory(subsys) |
| 321 | add_subdirectory(arch) |
| 322 | add_subdirectory(drivers) |
| 323 | add_subdirectory(tests) |
| 324 | |
| 325 | # Generate offsets.c.obj from offsets.c |
| 326 | # Generate offsets.h from offsets.c.obj |
| 327 | |
| 328 | set(OFFSETS_C_PATH $ENV{ZEPHYR_BASE}/arch/${ARCH}/core/offsets/offsets.c) |
| 329 | set(OFFSETS_O_PATH ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/offsets.dir/arch/${ARCH}/core/offsets/offsets.c.obj) |
| 330 | set(OFFSETS_H_PATH ${PROJECT_BINARY_DIR}/include/generated/offsets.h) |
| 331 | |
| 332 | add_library(offsets STATIC ${OFFSETS_C_PATH}) |
| 333 | target_link_libraries(offsets zephyr_interface) |
| 334 | |
| 335 | add_custom_command( |
| 336 | OUTPUT ${OFFSETS_H_PATH} |
| 337 | COMMAND ${PYTHON_EXECUTABLE} $ENV{ZEPHYR_BASE}/scripts/gen_offset_header.py |
| 338 | -i ${OFFSETS_O_PATH} |
| 339 | -o ${OFFSETS_H_PATH} |
| 340 | DEPENDS offsets |
| 341 | ) |
| 342 | add_custom_target(offsets_h DEPENDS ${OFFSETS_H_PATH}) |
| 343 | |
| 344 | zephyr_include_directories(${TOOLCHAIN_INCLUDES}) |
| 345 | |
| 346 | zephyr_get_include_directories(ZEPHYR_INCLUDES) |
| 347 | |
| 348 | add_subdirectory(kernel) |
| 349 | |
| 350 | # Read list content |
| 351 | get_property(ZEPHYR_LIBS_PROPERTY GLOBAL PROPERTY ZEPHYR_LIBS) |
| 352 | |
| 353 | foreach(zephyr_lib ${ZEPHYR_LIBS_PROPERTY}) |
| 354 | # TODO: Could this become an INTERFACE property of zephyr_interface? |
| 355 | add_dependencies(${zephyr_lib} offsets_h) |
| 356 | endforeach() |
| 357 | |
| 358 | get_property(OUTPUT_FORMAT GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT) |
| 359 | |
| 360 | # Run the pre-processor on the linker script |
| 361 | # |
| 362 | # Deal with the un-preprocessed linker scripts differently with |
| 363 | # different generators. |
| 364 | if(CMAKE_GENERATOR STREQUAL "Unix Makefiles") |
| 365 | # Note that the IMPLICIT_DEPENDS option is currently supported only |
| 366 | # for Makefile generators and will be ignored by other generators. |
| 367 | set(LINKER_SCRIPT_DEP IMPLICIT_DEPENDS C ${LINKER_SCRIPT}) |
| 368 | elseif(CMAKE_GENERATOR STREQUAL "Ninja") |
| 369 | # Using DEPFILE with other generators than Ninja is an error. |
| 370 | set(LINKER_SCRIPT_DEP DEPFILE ${PROJECT_BINARY_DIR}/linker.cmd.dep) |
| 371 | else() |
| 372 | # TODO: How would the linker script dependencies work for non-linker |
| 373 | # script generators. |
| 374 | message(STATUS "Warning; this generator is not well supported. The |
| 375 | Linker script may not be regenerated when it should.") |
| 376 | set(LINKER_SCRIPT_DEP "") |
| 377 | endif() |
| 378 | |
| 379 | get_property(LINKER_SCRIPT_DEFINES GLOBAL PROPERTY PROPERTY_LINKER_SCRIPT_DEFINES) |
| 380 | |
| 381 | if(CONFIG_APPLICATION_MEMORY) |
| 382 | # Objects default to being in kernel space, and then we exclude |
| 383 | # certain items. |
| 384 | set(kernel_object_file_list |
| 385 | ${ZEPHYR_LIBS_PROPERTY} |
| 386 | kernel |
| 387 | ) |
| 388 | list( |
| 389 | REMOVE_ITEM |
| 390 | kernel_object_file_list |
| 391 | app |
| 392 | ) |
| 393 | |
| 394 | # The zephyr libraries in zephyr/lib/ and zephyr/test/ belong in |
| 395 | # userspace. |
| 396 | |
| 397 | # NB: The business logic for determing what source files are in |
| 398 | # kernel space and what source files are in user space is |
| 399 | # fragile. Fix ASAP. |
| 400 | # |
| 401 | # The intended design is that certain directories are designated as |
| 402 | # containing userspace code and others for kernel space code. The |
| 403 | # implementation we have however is not working on directories of |
| 404 | # code, it is working on zephyr libraries. It is exploiting the fact |
| 405 | # that zephyr libraries follow a naming scheme as described in |
| 406 | # extensions.cmake:zephyr_library_get_current_dir_lib_name |
| 407 | # |
| 408 | # But code from test/ and lib/ that is placed in the "zephyr" |
| 409 | # library (with zephyr_sources()) will not be in a library that is |
| 410 | # prefixed with lib__ or test__ and will end up in the wrong address |
| 411 | # space. |
| 412 | set(application_space_dirs |
| 413 | lib |
| 414 | tests |
| 415 | ) |
| 416 | foreach(f ${kernel_object_file_list}) |
| 417 | foreach(app_dir ${application_space_dirs}) |
| 418 | if(${f} MATCHES "^${app_dir}__") # Begins with ${app_dir}__, e.g. lib__libc |
| 419 | list( |
| 420 | REMOVE_ITEM |
| 421 | kernel_object_file_list |
| 422 | ${f} |
| 423 | ) |
| 424 | endif() |
| 425 | endforeach() |
| 426 | endforeach() |
| 427 | |
| 428 | # Create a list ks, with relative paths to kernel space libs. |
| 429 | foreach(f ${kernel_object_file_list}) |
| 430 | get_target_property(target_name ${f} NAME) |
| 431 | get_target_property(target_binary_dir ${f} BINARY_DIR) |
| 432 | |
| 433 | string(REPLACE |
| 434 | ${PROJECT_BINARY_DIR} |
| 435 | "" |
| 436 | fixed_path |
| 437 | ${target_binary_dir} |
| 438 | ) |
| 439 | |
| 440 | # Append / if not empty |
| 441 | if(fixed_path) |
| 442 | set(fixed_path "${fixed_path}/") |
| 443 | endif() |
| 444 | |
| 445 | # Cut off leading / if present |
| 446 | if(fixed_path MATCHES "^/.+") |
| 447 | string(SUBSTRING ${fixed_path} 1 -1 fixed_path) |
| 448 | endif() |
| 449 | |
| 450 | list(APPEND ks "${fixed_path}lib${target_name}.a") |
| 451 | endforeach() |
| 452 | |
| 453 | # We are done constructing kernel_object_file_list, now we inject this |
| 454 | # information into the linker script through -D's |
| 455 | list(LENGTH kernel_object_file_list NUM_KERNEL_OBJECT_FILES) |
| 456 | list(APPEND LINKER_SCRIPT_DEFINES -DNUM_KERNEL_OBJECT_FILES=${NUM_KERNEL_OBJECT_FILES}) |
| 457 | set(i 0) |
| 458 | foreach(f ${ks}) |
| 459 | list(APPEND LINKER_SCRIPT_DEFINES -DKERNEL_OBJECT_FILE_${i}=${f}) |
| 460 | math(EXPR i "${i}+1") |
| 461 | endforeach() |
| 462 | endif() # CONFIG_APPLICATION_MEMORY |
| 463 | |
| 464 | get_filename_component(BASE_NAME ${CMAKE_CURRENT_BINARY_DIR} NAME) |
| 465 | add_custom_command( |
| 466 | OUTPUT linker.cmd |
| 467 | DEPENDS ${LINKER_SCRIPT} |
| 468 | ${LINKER_SCRIPT_DEP} |
| 469 | # NB: This COMMAND is copy-pasted to generate linker_pass2.cmd |
| 470 | # TODO: Remove duplication |
| 471 | COMMAND ${CMAKE_C_COMPILER} |
| 472 | -x assembler-with-cpp |
| 473 | -nostdinc |
| 474 | -undef |
| 475 | -MD -MF linker.cmd.dep -MT ${BASE_NAME}/linker.cmd |
| 476 | ${ZEPHYR_INCLUDES} |
| 477 | ${LINKER_SCRIPT_DEFINES} |
| 478 | -E ${LINKER_SCRIPT} -P |
| 479 | -o linker.cmd |
| 480 | VERBATIM |
| 481 | WORKING_DIRECTORY ${PROJECT_BINARY_DIR} |
| 482 | ) |
| 483 | add_custom_target( |
| 484 | linker_script |
| 485 | DEPENDS |
| 486 | linker.cmd |
| 487 | offsets_h |
| 488 | ) |
| 489 | |
| 490 | set(zephyr_lnk |
| 491 | ${LINKERFLAGPREFIX},-Map=${PROJECT_BINARY_DIR}/${KERNEL_MAP_NAME} |
| 492 | -u_OffsetAbsSyms |
| 493 | -u_ConfigAbsSyms |
| 494 | -e__start |
| 495 | ${LINKERFLAGPREFIX},--start-group |
| 496 | ${LINKERFLAGPREFIX},--whole-archive |
| 497 | ${ZEPHYR_LIBS_PROPERTY} |
| 498 | ${LINKERFLAGPREFIX},--no-whole-archive |
| 499 | kernel |
| 500 | ${OFFSETS_O_PATH} |
| 501 | ${LINKERFLAGPREFIX},--end-group |
| 502 | ${LIB_INCLUDE_DIR} |
| 503 | -L${PROJECT_BINARY_DIR} |
| 504 | ${TOOLCHAIN_LIBS} |
| 505 | ) |
| 506 | |
| 507 | if(CONFIG_GEN_ISR_TABLES) |
| 508 | # isr_tables.c is generated from zephyr_prebuilt by |
| 509 | # gen_isr_tables.py |
| 510 | add_custom_command( |
| 511 | OUTPUT isr_tables.c |
| 512 | COMMAND ${CMAKE_OBJCOPY} |
| 513 | -I ${OUTPUT_FORMAT} |
| 514 | -O binary |
| 515 | --only-section=.intList |
| 516 | $<TARGET_FILE:zephyr_prebuilt> |
| 517 | isrList.bin |
| 518 | COMMAND ${PYTHON_EXECUTABLE} |
| 519 | $ENV{ZEPHYR_BASE}/arch/common/gen_isr_tables.py |
| 520 | --output-source isr_tables.c |
| 521 | --intlist isrList.bin |
| 522 | --debug |
| 523 | --sw-isr-table |
| 524 | --vector-table |
| 525 | DEPENDS zephyr_prebuilt |
| 526 | ) |
| 527 | set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_SOURCE_FILES isr_tables.c) |
| 528 | endif() |
| 529 | |
| 530 | if(CONFIG_USERSPACE) |
| 531 | set(GEN_KOBJ_LIST $ENV{ZEPHYR_BASE}/scripts/gen_kobject_list.py) |
| 532 | set(PROCESS_GPERF $ENV{ZEPHYR_BASE}/scripts/process_gperf.py) |
| 533 | |
| 534 | set(OBJ_LIST kobject_hash.gperf) |
| 535 | set(OUTPUT_SRC_PRE kobject_hash_preprocessed.c) |
| 536 | set(OUTPUT_SRC kobject_hash.c) |
| 537 | set(OUTPUT_OBJ kobject_hash.c.obj) |
| 538 | set(OUTPUT_OBJ_RENAMED kobject_hash_renamed.o) |
| 539 | |
| 540 | # Essentially what we are doing here is extracting some information |
| 541 | # out of the nearly finished elf file, generating the source code |
| 542 | # for a hash table based on that information, and then compiling and |
| 543 | # linking the hash table back into a now even more nearly finished |
| 544 | # elf file. |
| 545 | |
| 546 | # Use the script GEN_KOBJ_LIST to scan the kernel binary's |
| 547 | # (zephyr_prebuilt) DWARF information to produce a table of kernel |
| 548 | # objects (OBJ_LIST) which we will then pass to gperf |
| 549 | add_custom_command( |
| 550 | OUTPUT ${OBJ_LIST} |
| 551 | COMMAND |
| 552 | ${PYTHON_EXECUTABLE} |
| 553 | ${GEN_KOBJ_LIST} |
| 554 | --kernel $<TARGET_FILE:zephyr_prebuilt> |
| 555 | --output ${OBJ_LIST} |
| 556 | $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose> |
| 557 | DEPENDS zephyr_prebuilt |
| 558 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} |
| 559 | ) |
| 560 | add_custom_target(obj_list DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OBJ_LIST}) |
| 561 | |
| 562 | # Use gperf to generate C code (OUTPUT_SRC_PRE) which implements a |
| 563 | # perfect hashtable based on OBJ_LIST |
| 564 | add_custom_command( |
| 565 | OUTPUT ${OUTPUT_SRC_PRE} |
| 566 | COMMAND |
| 567 | ${GPERF} |
| 568 | --output-file ${OUTPUT_SRC_PRE} |
| 569 | ${OBJ_LIST} |
| 570 | DEPENDS obj_list |
| 571 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} |
| 572 | ) |
| 573 | add_custom_target(output_src_pre DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_SRC_PRE}) |
| 574 | |
| 575 | # For our purposes the code/data generated by gperf is not optimal. |
| 576 | # |
| 577 | # The script PROCESS_GPERF creates a new c file OUTPUT_SRC based on |
| 578 | # OUTPUT_SRC_PRE to greatly reduce the amount of code/data generated |
| 579 | # since we know we are always working with pointer values |
| 580 | add_custom_command( |
| 581 | OUTPUT ${OUTPUT_SRC} |
| 582 | COMMAND |
| 583 | ${PROCESS_GPERF} |
| 584 | -i ${OUTPUT_SRC_PRE} |
| 585 | -o ${OUTPUT_SRC} |
| 586 | $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose> |
| 587 | DEPENDS output_src_pre |
| 588 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} |
| 589 | ) |
| 590 | add_custom_target(output_src DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_SRC}) |
| 591 | |
| 592 | # We need precise control of where generated text/data ends up in the final |
| 593 | # kernel image. Disable function/data sections and use objcopy to move |
| 594 | # generated data into special section names |
| 595 | add_library(output_lib STATIC |
| 596 | ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_SRC} |
| 597 | ) |
| 598 | |
| 599 | target_link_libraries(output_lib zephyr_interface) |
| 600 | |
| 601 | # Turn off -ffunction-sections, etc. |
| 602 | # NB: Using a library instead of target_compile_options(output_lib |
| 603 | # [...]) because a library's options have precedence |
| 604 | add_library(output_lib_interface INTERFACE) |
| 605 | target_compile_options(output_lib_interface INTERFACE |
| 606 | -fno-function-sections |
| 607 | -fno-data-sections |
| 608 | ) |
| 609 | target_link_libraries(output_lib output_lib_interface) |
| 610 | |
| 611 | set(OUTPUT_OBJ_PATH ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/output_lib.dir/${OUTPUT_OBJ}) |
| 612 | |
| 613 | add_custom_command( |
| 614 | OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_OBJ_RENAMED} |
| 615 | COMMAND |
| 616 | ${CMAKE_OBJCOPY} |
| 617 | --rename-section .data=.kobject_data.data |
| 618 | --rename-section .text=.kobject_data.text |
| 619 | --rename-section .rodata=.kobject_data.rodata |
| 620 | ${OUTPUT_OBJ_PATH} |
| 621 | ${OUTPUT_OBJ_RENAMED} |
| 622 | DEPENDS output_lib |
| 623 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} |
| 624 | ) |
| 625 | add_custom_target(output_obj_renamed DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_OBJ_RENAMED}) |
| 626 | |
| 627 | add_library(output_obj_renamed_lib STATIC IMPORTED GLOBAL) |
| 628 | set_property( |
| 629 | TARGET output_obj_renamed_lib |
| 630 | PROPERTY |
| 631 | IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_OBJ_RENAMED} |
| 632 | ) |
| 633 | add_dependencies( |
| 634 | output_obj_renamed_lib |
| 635 | output_obj_renamed |
| 636 | ) |
| 637 | |
| 638 | set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_OBJECT_FILES output_obj_renamed_lib) |
| 639 | endif() |
| 640 | |
| 641 | # Read global variables into local variables |
| 642 | get_property(GKOF GLOBAL PROPERTY GENERATED_KERNEL_OBJECT_FILES) |
| 643 | get_property(GKSF GLOBAL PROPERTY GENERATED_KERNEL_SOURCE_FILES) |
| 644 | |
| 645 | # FIXME: Is there any way to get rid of empty_file.c? |
| 646 | add_executable( zephyr_prebuilt misc/empty_file.c) |
| 647 | target_link_libraries(zephyr_prebuilt -T${PROJECT_BINARY_DIR}/linker.cmd ${zephyr_lnk}) |
| 648 | set_property(TARGET zephyr_prebuilt PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker.cmd) |
| 649 | add_dependencies( zephyr_prebuilt linker_script offsets) |
| 650 | |
| 651 | if(GKOF OR GKSF) |
| 652 | set(logical_target_for_zephyr_elf kernel_elf) |
| 653 | |
| 654 | # The second linker pass uses the same source linker script of the |
| 655 | # first pass (LINKER_SCRIPT), but this time preprocessed with the |
| 656 | # define LINKER_PASS2. |
| 657 | add_custom_command( |
| 658 | OUTPUT linker_pass2.cmd |
| 659 | DEPENDS ${LINKER_SCRIPT} |
| 660 | ${LINKER_SCRIPT_DEP} |
| 661 | COMMAND ${CMAKE_C_COMPILER} |
| 662 | -x assembler-with-cpp |
| 663 | -nostdinc |
| 664 | -undef |
| 665 | -MD -MF linker_pass2.cmd.dep -MT ${BASE_NAME}/linker_pass2.cmd |
| 666 | ${ZEPHYR_INCLUDES} |
| 667 | ${LINKER_SCRIPT_DEFINES} |
| 668 | -DLINKER_PASS2 |
| 669 | -E ${LINKER_SCRIPT} -P |
| 670 | -o linker_pass2.cmd |
| 671 | VERBATIM |
| 672 | WORKING_DIRECTORY ${PROJECT_BINARY_DIR} |
| 673 | ) |
| 674 | add_custom_target( |
| 675 | linker_pass2_script |
| 676 | DEPENDS |
| 677 | linker_pass2.cmd |
| 678 | offsets_h |
| 679 | ) |
| 680 | |
| 681 | add_executable( kernel_elf misc/empty_file.c ${GKSF}) |
| 682 | target_link_libraries(kernel_elf ${GKOF} -T${PROJECT_BINARY_DIR}/linker_pass2.cmd ${zephyr_lnk}) |
| 683 | set_property(TARGET kernel_elf PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker_pass2.cmd) |
| 684 | add_dependencies( kernel_elf linker_pass2_script) |
| 685 | else() |
| 686 | set(logical_target_for_zephyr_elf zephyr_prebuilt) |
| 687 | # Use the prebuilt elf as the final elf since we don't have a |
| 688 | # generation stage. |
| 689 | endif() |
| 690 | |
| 691 | # To avoid having the same logical target name for the zephyr lib and |
| 692 | # the zephyr elf, we set the kernel_elf file name to zephyr.elf. |
| 693 | set_target_properties(${logical_target_for_zephyr_elf} PROPERTIES OUTPUT_NAME ${KERNEL_NAME}) |
| 694 | |
| 695 | add_custom_command( |
| 696 | TARGET ${logical_target_for_zephyr_elf} |
| 697 | POST_BUILD |
| 698 | COMMAND ${PYTHON_EXECUTABLE} $ENV{ZEPHYR_BASE}/scripts/check_link_map.py ${KERNEL_MAP_NAME} |
| 699 | COMMAND ${CMAKE_OBJCOPY} -S -Oihex -R .comment -R COMMON -R .eh_frame ${KERNEL_ELF_NAME} ${KERNEL_HEX_NAME} |
| 700 | COMMAND ${CMAKE_OBJCOPY} -S -Obinary -R .comment -R COMMON -R .eh_frame ${KERNEL_ELF_NAME} ${KERNEL_BIN_NAME} |
| 701 | COMMAND ${CMAKE_OBJCOPY} --srec-len 1 --output-target=srec ${KERNEL_ELF_NAME} ${KERNEL_S19_NAME} |
| 702 | COMMAND ${CMAKE_OBJDUMP} -S ${KERNEL_ELF_NAME} > ${KERNEL_LST_NAME} |
| 703 | COMMAND ${CMAKE_READELF} -e ${KERNEL_ELF_NAME} > ${KERNEL_STAT_NAME} |
| 704 | COMMAND ${CMAKE_STRIP} --strip-all ${KERNEL_ELF_NAME} -o ${KERNEL_STRIP_NAME} |
| 705 | COMMENT "Generating zephyr.{hex,bin,lst,strip,stat,s19} from zephyr.elf for board: ${BOARD}" |
| 706 | # NB: COMMENT only works for some CMake-Generators |
| 707 | ) |
| 708 | |
| 709 | add_subdirectory(cmake/qemu) |
| 710 | add_subdirectory(cmake/flash) |
| 711 | |
| 712 | add_subdirectory(cmake/usage) |
| 713 | add_subdirectory(cmake/reports) |
| 714 | |
| 715 | include(cmake/ccache.cmake) |
| 716 | |
| 717 | if(CONFIG_ASSERT) |
| 718 | message(WARNING " |
| 719 | ------------------------------------------------------------ |
| 720 | --- WARNING: __ASSERT() statements are globally ENABLED --- |
| 721 | --- The kernel will run more slowly and uses more memory --- |
| 722 | ------------------------------------------------------------" |
| 723 | ) |
| 724 | endif() |
| 725 | |
| 726 | if(CONFIG_BOARD_DEPRECATED) |
| 727 | message(WARNING " |
| 728 | WARNING: The board '${BOARD}' is deprecated and will be |
| 729 | removed in version ${CONFIG_BOARD_DEPRECATED}" |
| 730 | ) |
| 731 | endif() |