Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 | include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
########################################################
# Table of contents
########################################################
# 1. Zephyr-aware extensions
# 1.1. zephyr_*
# 1.2. zephyr_library_*
# 1.3. generate_inc_*
# 2. Kconfig-aware extensions
# 2.1 *_if_kconfig
# 2.2 Misc
# 3. CMake-generic extensions
# 3.1. *_ifdef
# 3.2. *_ifndef
# 3.3. *_option compiler compatibility checks
# 3.4. Debugging CMake
########################################################
# 1. Zephyr-aware extensions
########################################################
# 1.1. zephyr_*
#
# The following methods are for modifying the CMake library[0] called
# "zephyr". zephyr is a catchall CMake library for source files that
# can be built purely with the include paths, defines, and other
# compiler flags that all zephyr source files use.
# [0] https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html
#
# Example usage:
# zephyr_sources(
# random_esp32.c
# utils.c
# )
#
# Is short for:
# target_sources(zephyr PRIVATE
# ${CMAKE_CURRENT_SOURCE_DIR}/random_esp32.c
# ${CMAKE_CURRENT_SOURCE_DIR}/utils.c
# )
# https://cmake.org/cmake/help/latest/command/target_sources.html
function(zephyr_sources)
foreach(arg ${ARGV})
if(IS_ABSOLUTE ${arg})
set(path ${arg})
else()
set(path ${CMAKE_CURRENT_SOURCE_DIR}/${arg})
endif()
if(IS_DIRECTORY ${path})
message(FATAL_ERROR "zephyr_sources() was called on a directory")
endif()
target_sources(zephyr PRIVATE ${path})
endforeach()
endfunction()
# https://cmake.org/cmake/help/latest/command/target_include_directories.html
function(zephyr_include_directories)
foreach(arg ${ARGV})
if(IS_ABSOLUTE ${arg})
set(path ${arg})
else()
set(path ${CMAKE_CURRENT_SOURCE_DIR}/${arg})
endif()
target_include_directories(zephyr_interface INTERFACE ${path})
endforeach()
endfunction()
# https://cmake.org/cmake/help/latest/command/target_include_directories.html
function(zephyr_system_include_directories)
foreach(arg ${ARGV})
if(IS_ABSOLUTE ${arg})
set(path ${arg})
else()
set(path ${CMAKE_CURRENT_SOURCE_DIR}/${arg})
endif()
target_include_directories(zephyr_interface SYSTEM INTERFACE ${path})
endforeach()
endfunction()
# https://cmake.org/cmake/help/latest/command/target_compile_definitions.html
function(zephyr_compile_definitions)
target_compile_definitions(zephyr_interface INTERFACE ${ARGV})
endfunction()
# https://cmake.org/cmake/help/latest/command/target_compile_options.html
function(zephyr_compile_options)
target_compile_options(zephyr_interface INTERFACE ${ARGV})
endfunction()
# https://cmake.org/cmake/help/latest/command/target_link_libraries.html
function(zephyr_link_libraries)
target_link_libraries(zephyr_interface INTERFACE ${ARGV})
endfunction()
# See this file section 3.1. target_cc_option
function(zephyr_cc_option)
foreach(arg ${ARGV})
target_cc_option(zephyr_interface INTERFACE ${arg})
endforeach()
endfunction()
function(zephyr_cc_option_fallback option1 option2)
target_cc_option_fallback(zephyr_interface INTERFACE ${option1} ${option2})
endfunction()
function(zephyr_ld_options)
target_ld_options(zephyr_interface INTERFACE ${ARGV})
endfunction()
# Getter functions for extracting build information from
# zephyr_interface. Returning lists, and strings is supported, as is
# requesting specific categories of build information (defines,
# includes, options).
#
# The naming convention follows:
# zephyr_get_${build_information}_for_lang${format}(lang x)
# Where
# the argument 'x' is written with the result
# and
# ${build_information} can be one of
# - include_directories # -I directories
# - system_include_directories # -isystem directories
# - compile_definitions # -D'efines
# - compile_options # misc. compiler flags
# and
# ${format} can be
# - the empty string '', signifying that it should be returned as a list
# - _as_string signifying that it should be returned as a string
# and
# ${lang} can be one of
# - C
# - CXX
# - ASM
#
# e.g.
# zephyr_get_include_directories_for_lang(ASM x)
# writes "-Isome_dir;-Isome/other/dir" to x
function(zephyr_get_include_directories_for_lang_as_string lang i)
zephyr_get_include_directories_for_lang(${lang} list_of_flags)
convert_list_of_flags_to_string_of_flags(list_of_flags str_of_flags)
set(${i} ${str_of_flags} PARENT_SCOPE)
endfunction()
function(zephyr_get_system_include_directories_for_lang_as_string lang i)
zephyr_get_system_include_directories_for_lang(${lang} list_of_flags)
convert_list_of_flags_to_string_of_flags(list_of_flags str_of_flags)
set(${i} ${str_of_flags} PARENT_SCOPE)
endfunction()
function(zephyr_get_compile_definitions_for_lang_as_string lang i)
zephyr_get_compile_definitions_for_lang(${lang} list_of_flags)
convert_list_of_flags_to_string_of_flags(list_of_flags str_of_flags)
set(${i} ${str_of_flags} PARENT_SCOPE)
endfunction()
function(zephyr_get_compile_options_for_lang_as_string lang i)
zephyr_get_compile_options_for_lang(${lang} list_of_flags)
convert_list_of_flags_to_string_of_flags(list_of_flags str_of_flags)
set(${i} ${str_of_flags} PARENT_SCOPE)
endfunction()
function(zephyr_get_include_directories_for_lang lang i)
get_property_and_add_prefix(flags zephyr_interface INTERFACE_INCLUDE_DIRECTORIES -I)
process_flags(${lang} flags output_list)
set(${i} ${output_list} PARENT_SCOPE)
endfunction()
function(zephyr_get_system_include_directories_for_lang lang i)
get_property_and_add_prefix(flags zephyr_interface INTERFACE_SYSTEM_INCLUDE_DIRECTORIES -isystem)
process_flags(${lang} flags output_list)
set(${i} ${output_list} PARENT_SCOPE)
endfunction()
function(zephyr_get_compile_definitions_for_lang lang i)
get_property_and_add_prefix(flags zephyr_interface INTERFACE_COMPILE_DEFINITIONS -D)
process_flags(${lang} flags output_list)
set(${i} ${output_list} PARENT_SCOPE)
endfunction()
function(zephyr_get_compile_options_for_lang lang i)
get_property(flags TARGET zephyr_interface PROPERTY INTERFACE_COMPILE_OPTIONS)
process_flags(${lang} flags output_list)
set(${i} ${output_list} PARENT_SCOPE)
endfunction()
function(process_flags lang input output)
# The flags might contains compile language generator expressions that
# look like this:
# $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
#
# Flags that don't specify a language like this apply to all
# languages.
#
# See COMPILE_LANGUAGE in
# https://cmake.org/cmake/help/v3.3/manual/cmake-generator-expressions.7.html
#
# To deal with this, we apply a regex to extract the flag and also
# to find out if the language matches.
#
# If this doesn't work out we might need to ban the use of
# COMPILE_LANGUAGE and instead partition C, CXX, and ASM into
# different libraries
set(languages C CXX ASM)
set(tmp_list "")
foreach(flag ${${input}})
set(is_compile_lang_generator_expression 0)
foreach(l ${languages})
if(flag MATCHES "<COMPILE_LANGUAGE:${l}>:([^>]+)>")
set(is_compile_lang_generator_expression 1)
if(${l} STREQUAL ${lang})
list(APPEND tmp_list ${CMAKE_MATCH_1})
break()
endif()
endif()
endforeach()
if(NOT is_compile_lang_generator_expression)
list(APPEND tmp_list ${flag})
endif()
endforeach()
set(${output} ${tmp_list} PARENT_SCOPE)
endfunction()
function(convert_list_of_flags_to_string_of_flags ptr_list_of_flags string_of_flags)
# Convert the list to a string so we can do string replace
# operations on it and replace the ";" list separators with a
# whitespace so the flags are spaced out
string(REPLACE ";" " " locally_scoped_string_of_flags "${${ptr_list_of_flags}}")
# Set the output variable in the parent scope
set(${string_of_flags} ${locally_scoped_string_of_flags} PARENT_SCOPE)
endfunction()
macro(get_property_and_add_prefix result target property prefix)
get_property(target_property TARGET ${target} PROPERTY ${property})
foreach(x ${target_property})
list(APPEND ${result} ${prefix}${x})
endforeach()
endmacro()
# 1.3 generate_inc_*
# These functions are useful if there is a need to generate a file
# that can be included into the application at build time. The file
# can also be compressed automatically when embedding it.
#
# See tests/application_development/gen_inc_file for an example of
# usage.
function(generate_inc_file
source_file # The source file to be converted to hex
generated_file # The generated file
)
add_custom_command(
OUTPUT ${generated_file}
COMMAND
${PYTHON_EXECUTABLE}
$ENV{ZEPHYR_BASE}/scripts/file2hex.py
${ARGN} # Extra arguments are passed to file2hex.py
--file ${source_file}
> ${generated_file} # Does pipe redirection work on Windows?
DEPENDS ${source_file}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
endfunction()
function(generate_inc_file_for_target
target # The cmake target that depends on the generated file
source_file # The source file to be converted to hex
generated_file # The generated file
# Any additional arguments are passed on to file2hex.py
)
generate_inc_file(${source_file} ${generated_file} ${ARGN})
# Ensure 'generated_file' is generated before 'target' by creating a
# 'custom_target' for it and setting up a dependency between the two
# targets
# But first create a unique name for the custom target
# Replace / with _ (driver/serial => driver_serial) and . with _
set(generated_target_name ${generated_file})
string(REPLACE "/" "_" generated_target_name ${generated_target_name})
string(REPLACE "." "_" generated_target_name ${generated_target_name})
add_custom_target(${generated_target_name} DEPENDS ${generated_file})
add_dependencies(${target} ${generated_target_name})
endfunction()
# 1.2 zephyr_library_*
#
# Zephyr libraries use CMake's library concept and a set of
# assumptions about how zephyr code is organized to cut down on
# boilerplate code.
#
# A Zephyr library can be constructed by the function zephyr_library
# or zephyr_library_named. The constructors create a CMake library
# with a name accessible through the variable ZEPHYR_CURRENT_LIBRARY.
#
# The variable ZEPHYR_CURRENT_LIBRARY should seldomly be needed since
# the zephyr libraries have methods that modify the libraries. These
# methods have the signature: zephyr_library_<target-function>
#
# The methods are wrappers around the CMake target_* functions. See
# https://cmake.org/cmake/help/latest/manual/cmake-commands.7.html for
# documentation on the underlying target_* functions.
#
# The methods modify the CMake target_* API to reduce boilerplate;
# PRIVATE is assumed
# The target is assumed to be ZEPHYR_CURRENT_LIBRARY
#
# When a flag that is given through the zephyr_* API conflicts with
# the zephyr_library_* API then precedence will be given to the
# zephyr_library_* API. In other words, local configuration overrides
# global configuration.
# Constructor with a directory-inferred name
macro(zephyr_library)
zephyr_library_get_current_dir_lib_name(lib_name)
zephyr_library_named(${lib_name})
endmacro()
# Determines what the current directory's lib name would be and writes
# it to the argument "lib_name"
macro(zephyr_library_get_current_dir_lib_name lib_name)
# Remove the prefix (/home/sebo/zephyr/driver/serial/CMakeLists.txt => driver/serial/CMakeLists.txt)
file(RELATIVE_PATH name $ENV{ZEPHYR_BASE} ${CMAKE_CURRENT_LIST_FILE})
# Remove the filename (driver/serial/CMakeLists.txt => driver/serial)
get_filename_component(name ${name} DIRECTORY)
# Replace / with __ (driver/serial => driver__serial)
string(REGEX REPLACE "/" "__" name ${name})
set(${lib_name} ${name})
endmacro()
# Constructor with an explicitly given name.
macro(zephyr_library_named name)
# This is a macro because we need add_library() to be executed
# within the scope of the caller.
set(ZEPHYR_CURRENT_LIBRARY ${name})
add_library(${name} STATIC "")
zephyr_append_cmake_library(${name})
target_link_libraries(${name} zephyr_interface)
endmacro()
function(zephyr_link_interface interface)
target_link_libraries(interface INTERFACE zephyr_interface)
endfunction()
#
# zephyr_library versions of normal CMake target_<func> functions
#
function(zephyr_library_sources source)
target_sources(${ZEPHYR_CURRENT_LIBRARY} PRIVATE ${source} ${ARGN})
endfunction()
function(zephyr_library_include_directories)
target_include_directories(${ZEPHYR_CURRENT_LIBRARY} PRIVATE ${ARGN})
endfunction()
function(zephyr_library_link_libraries item)
target_link_libraries(${ZEPHYR_CURRENT_LIBRARY} ${item} ${ARGN})
endfunction()
function(zephyr_library_compile_definitions item)
target_compile_definitions(${ZEPHYR_CURRENT_LIBRARY} PRIVATE ${item} ${ARGN})
endfunction()
function(zephyr_library_compile_options item)
# The compiler is relied upon for sane behaviour when flags are in
# conflict. Compilers generally give precedence to flags given late
# on the command line. So to ensure that zephyr_library_* flags are
# placed late on the command line we create a dummy interface
# library and link with it to obtain the flags.
#
# Linking with a dummy interface library will place flags later on
# the command line than the the flags from zephyr_interface because
# zephyr_interface will be the first interface library that flags
# are taken from.
string(RANDOM random)
set(lib_name options_interface_lib_${random})
add_library( ${lib_name} INTERFACE)
target_compile_options(${lib_name} INTERFACE ${item} ${ARGN})
target_link_libraries(${ZEPHYR_CURRENT_LIBRARY} ${lib_name})
endfunction()
function(zephyr_library_cc_option)
foreach(option ${ARGV})
string(MAKE_C_IDENTIFIER check${option} check)
check_c_compiler_flag(${option} ${check})
if(${check})
zephyr_library_compile_options(${option})
endif()
endforeach()
endfunction()
# Add the existing CMake library 'library' to the global list of
# Zephyr CMake libraries. This is done automatically by the
# constructor but must called explicitly on CMake libraries that do
# not use a zephyr library constructor, but have source files that
# need to be included in the build.
function(zephyr_append_cmake_library library)
set_property(GLOBAL APPEND PROPERTY ZEPHYR_LIBS ${library})
endfunction()
########################################################
# 2. Kconfig-aware extensions
########################################################
#
# Kconfig is a configuration language developed for the Linux
# kernel. The below functions integrate CMake with Kconfig.
#
# 2.1 *_if_kconfig
#
# Functions for conditionally including directories and source files
# that have matching KConfig values.
#
# zephyr_library_sources_if_kconfig(fft.c)
# is the same as
# zephyr_library_sources_ifdef(CONFIG_FFT fft.c)
#
# add_subdirectory_if_kconfig(serial)
# is the same as
# add_subdirectory_ifdef(CONFIG_SERIAL serial)
function(add_subdirectory_if_kconfig dir)
string(TOUPPER config_${dir} UPPER_CASE_CONFIG)
add_subdirectory_ifdef(${UPPER_CASE_CONFIG} ${dir})
endfunction()
function(target_sources_if_kconfig target scope item)
get_filename_component(item_basename ${item} NAME_WE)
string(TOUPPER CONFIG_${item_basename} UPPER_CASE_CONFIG)
target_sources_ifdef(${UPPER_CASE_CONFIG} ${target} ${scope} ${item})
endfunction()
function(zephyr_library_sources_if_kconfig item)
get_filename_component(item_basename ${item} NAME_WE)
string(TOUPPER CONFIG_${item_basename} UPPER_CASE_CONFIG)
zephyr_library_sources_ifdef(${UPPER_CASE_CONFIG} ${item})
endfunction()
function(zephyr_sources_if_kconfig item)
get_filename_component(item_basename ${item} NAME_WE)
string(TOUPPER CONFIG_${item_basename} UPPER_CASE_CONFIG)
zephyr_sources_ifdef(${UPPER_CASE_CONFIG} ${item})
endfunction()
# 2.2 Misc
#
# Parse a KConfig formatted file (typically named *.config) and
# introduce all the CONF_ variables into the CMake namespace
function(import_kconfig config_file)
# Parse the lines prefixed with CONFIG_ in ${config_file}
file(
STRINGS
${config_file}
DOT_CONFIG_LIST
REGEX "^CONFIG_"
)
foreach (CONFIG ${DOT_CONFIG_LIST})
# CONFIG looks like: CONFIG_NET_BUF=y
# Match the first part, the variable name
string(REGEX MATCH "[^=]+" CONF_VARIABLE_NAME ${CONFIG})
# Match the second part, variable value
string(REGEX MATCH "=(.+$)" CONF_VARIABLE_VALUE ${CONFIG})
# The variable name match we just did included the '=' symbol. To just get the
# part on the RHS we use match group 1
set(CONF_VARIABLE_VALUE ${CMAKE_MATCH_1})
if("${CONF_VARIABLE_VALUE}" MATCHES "^\"(.*)\"$") # Is surrounded by quotes
set(CONF_VARIABLE_VALUE ${CMAKE_MATCH_1})
endif()
set("${CONF_VARIABLE_NAME}" "${CONF_VARIABLE_VALUE}" PARENT_SCOPE)
endforeach()
endfunction()
########################################################
# 3. CMake-generic extensions
########################################################
#
# These functions extend the CMake API in a way that is not particular
# to Zephyr. Primarily they work around limitations in the CMake
# language to allow cleaner build scripts.
# 3.1. *_ifdef
#
# Functions for conditionally executing CMake functions with oneliners
# e.g.
#
# if(CONFIG_FFT)
# zephyr_library_source(
# fft_32.c
# fft_utils.c
# )
# endif()
#
# Becomes
#
# zephyr_source_ifdef(
# CONFIG_FFT
# fft_32.c
# fft_utils.c
# )
#
# More Generally
# "<function-name>_ifdef(CONDITION args)"
# Becomes
# """
# if(CONDITION)
# <function-name>(args)
# endif()
# """
#
# ifdef functions are added on an as-need basis. See
# https://cmake.org/cmake/help/latest/manual/cmake-commands.7.html for
# a list of available functions.
function(add_subdirectory_ifdef feature_toggle dir)
if(${${feature_toggle}})
add_subdirectory(${dir})
endif()
endfunction()
function(target_sources_ifdef feature_toggle target scope item)
if(${${feature_toggle}})
target_sources(${target} ${scope} ${item} ${ARGN})
endif()
endfunction()
function(target_compile_definitions_ifdef feature_toggle target scope item)
if(${${feature_toggle}})
target_compile_definitions(${target} ${scope} ${item} ${ARGN})
endif()
endfunction()
function(target_include_directories_ifdef feature_toggle target scope item)
if(${${feature_toggle}})
target_include_directories(${target} ${scope} ${item} ${ARGN})
endif()
endfunction()
function(target_link_libraries_ifdef feature_toggle target item)
if(${${feature_toggle}})
target_link_libraries(${target} ${item} ${ARGN})
endif()
endfunction()
function(add_compile_option_ifdef feature_toggle option)
if(${${feature_toggle}})
add_compile_options(${option})
endif()
endfunction()
function(target_compile_option_ifdef feature_toggle target scope option)
if(${feature_toggle})
target_compile_options(${target} ${scope} ${option})
endif()
endfunction()
function(target_cc_option_ifdef feature_toggle target scope option)
if(${feature_toggle})
target_cc_option(${target} ${scope} ${option})
endif()
endfunction()
function(zephyr_library_sources_ifdef feature_toggle source)
if(${${feature_toggle}})
zephyr_library_sources(${source} ${ARGN})
endif()
endfunction()
function(zephyr_sources_ifdef feature_toggle)
if(${${feature_toggle}})
zephyr_sources(${ARGN})
endif()
endfunction()
function(zephyr_sources_ifndef feature_toggle)
if(NOT ${feature_toggle})
zephyr_sources(${ARGN})
endif()
endfunction()
function(zephyr_cc_option_ifdef feature_toggle)
if(${${feature_toggle}})
zephyr_cc_option(${ARGN})
endif()
endfunction()
function(zephyr_link_libraries_ifdef feature_toggle)
if(${${feature_toggle}})
zephyr_link_libraries(${ARGN})
endif()
endfunction()
function(zephyr_compile_options_ifdef feature_toggle)
if(${${feature_toggle}})
zephyr_compile_options(${ARGN})
endif()
endfunction()
function(zephyr_compile_definitions_ifdef feature_toggle)
if(${${feature_toggle}})
zephyr_compile_definitions(${ARGN})
endif()
endfunction()
function(zephyr_include_directories_ifdef feature_toggle)
if(${${feature_toggle}})
zephyr_include_directories(${ARGN})
endif()
endfunction()
function(zephyr_library_compile_definitions_ifdef feature_toggle item)
if(${${feature_toggle}})
zephyr_library_compile_definitions(${item} ${ARGN})
endif()
endfunction()
function(zephyr_library_compile_options_ifdef feature_toggle item)
if(${${feature_toggle}})
zephyr_library_compile_options(${item} ${ARGN})
endif()
endfunction()
function(zephyr_link_interface_ifdef feature_toggle interface)
if(${${feature_toggle}})
target_link_libraries(${interface} INTERFACE zephyr_interface)
endif()
endfunction()
function(zephyr_library_link_libraries_ifdef feature_toggle item)
if(${${feature_toggle}})
zephyr_library_link_libraries(${item})
endif()
endfunction()
macro(list_append_ifdef feature_toggle list)
if(${${feature_toggle}})
list(APPEND ${list} ${ARGN})
endif()
endmacro()
# 3.2. *_ifndef
# See 3.1 *_ifdef
function(set_ifndef variable value)
if(NOT ${variable})
set(${variable} ${value} PARENT_SCOPE)
endif()
endfunction()
function(target_cc_option_ifndef feature_toggle target scope option)
if(NOT ${feature_toggle})
target_cc_option(${target} ${scope} ${option})
endif()
endfunction()
function(zephyr_cc_option_ifndef feature_toggle)
if(NOT ${feature_toggle})
zephyr_cc_option(${ARGN})
endif()
endfunction()
function(zephyr_compile_options_ifndef feature_toggle)
if(NOT ${feature_toggle})
zephyr_compile_options(${ARGN})
endif()
endfunction()
# 3.2. *_option Compiler-compatibility checks
#
# Utility functions for silently omitting compiler flags when the
# compiler lacks support. *_cc_option was ported from KBuild, see
# cc-option in
# https://www.kernel.org/doc/Documentation/kbuild/makefiles.txt
# Writes 1 to the output variable 'ok' for the language 'lang' if
# the flag is supported, otherwise writes 0.
#
# lang must be C or CXX
#
# TODO: Support ASM
#
# Usage:
#
# check_compiler_flag(C "-Wall" my_check)
# print(my_check) # my_check is now 1
function(check_compiler_flag lang option ok)
string(MAKE_C_IDENTIFIER check${option}_${lang} ${ok})
if(${lang} STREQUAL C)
check_c_compiler_flag(${option} ${${ok}})
else()
check_cxx_compiler_flag(${option} ${${ok}})
endif()
if(${${${ok}}})
set(ret 1)
else()
set(ret 0)
endif()
set(${ok} ${ret} PARENT_SCOPE)
endfunction()
function(target_cc_option target scope option)
target_cc_option_fallback(${target} ${scope} ${option} "")
endfunction()
# Support an optional second option for when the first option is not
# supported.
function(target_cc_option_fallback target scope option1 option2)
if(CONFIG_CPLUSPLUS)
foreach(lang C CXX)
# For now, we assume that all flags that apply to C/CXX also
# apply to ASM.
check_compiler_flag(${lang} ${option1} check)
if(${check})
target_compile_options(${target} ${scope}
$<$<COMPILE_LANGUAGE:${lang}>:${option1}>
$<$<COMPILE_LANGUAGE:ASM>:${option1}>
)
elseif(option2)
target_compile_options(${target} ${scope}
$<$<COMPILE_LANGUAGE:${lang}>:${option2}>
$<$<COMPILE_LANGUAGE:ASM>:${option2}>
)
endif()
endforeach()
else()
check_compiler_flag(C ${option1} check)
if(${check})
target_compile_options(${target} ${scope} ${option1})
elseif(option2)
target_compile_options(${target} ${scope} ${option2})
endif()
endif()
endfunction()
function(target_ld_options target scope)
foreach(option ${ARGN})
string(MAKE_C_IDENTIFIER check${option} check)
check_c_compiler_flag(${option} ${check})
target_link_libraries_ifdef(${check} ${target} ${scope} ${option})
endforeach()
endfunction()
# 3.4. Debugging CMake
# Usage:
# print(BOARD)
#
# will print: "BOARD: nrf52_pca10040"
function(print arg)
message(STATUS "${arg}: ${${arg}}")
endfunction()
# Usage:
# assert(ZEPHYR_GCC_VARIANT "ZEPHYR_GCC_VARIANT not set.")
#
# will cause a FATAL_ERROR and print an error message if the first
# expression is false
macro(assert test comment)
if(NOT ${test})
message(FATAL_ERROR "Assertion failed: ${comment}")
endif()
endmacro()
# Usage:
# assert_exists(CMAKE_READELF)
#
# will cause a FATAL_ERROR if there is no file or directory behind the
# variable
macro(assert_exists var)
if(NOT EXISTS ${${var}})
message(FATAL_ERROR "No such file or directory: ${var}: '${${var}}'")
endif()
endmacro()
|