From 153196b3784032b9dc706c9e2ac3a25e1aedfdbb Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Mon, 6 Sep 2021 16:42:21 +0200 Subject: [PATCH] cmake: Zephyr ALLOW_EMPTY library property This introduces a dedicated zephyr_library_property() function which provides a common way to set Zephyr build system known properties on Zephyr libraries. As a first common property is the `ALLOW_EMPTY` property which allows to create a `zephyr_library()` without putting any content inside of it. In general libraries should not be created unless there are files to places inside of it, however in some cases there are so many `zephyr_library_sources_ifdef( )` where testing each setting before creating the library may not be desired. This commit allows the following construct in those cases: ``` zephyr_library() zephyr_library_property(ALLOW_EMPTY TRUE) zephyr_library_sources_ifdef(CONFIG_SETTING_1 file_1.c) zephyr_library_sources_ifdef(CONFIG_SETTING_2 file_2.c) ... zephyr_library_sources_ifdef(CONFIG_SETTING_n file_n.c) ``` Signed-off-by: Torsten Rasmussen --- CMakeLists.txt | 9 ++++++--- cmake/extensions.cmake | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fcea2d054eb7..f9246ace5b56 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -712,9 +712,12 @@ foreach(zephyr_lib ${ZEPHYR_LIBS_PROPERTY}) if(NOT source_list AND NOT ${lib_imported} ) - message(WARNING - "No SOURCES given to Zephyr library: ${zephyr_lib}\nExcluding target from build." - ) + get_property(allow_empty TARGET ${zephyr_lib} PROPERTY ALLOW_EMPTY) + if(NOT "${allow_empty}") + message(WARNING + "No SOURCES given to Zephyr library: ${zephyr_lib}\nExcluding target from build." + ) + endif() target_sources(${zephyr_lib} PRIVATE ${ZEPHYR_BASE}/misc/empty_file.c) set_property(TARGET ${zephyr_lib} PROPERTY EXCLUDE_FROM_ALL TRUE) list(REMOVE_ITEM ZEPHYR_LIBS_PROPERTY ${zephyr_lib}) diff --git a/cmake/extensions.cmake b/cmake/extensions.cmake index eaf768478e3e..452749c502c3 100644 --- a/cmake/extensions.cmake +++ b/cmake/extensions.cmake @@ -565,6 +565,31 @@ function(zephyr_library_app_memory partition) "-l" $ "${partition}") endfunction() +# Configure a Zephyr library specific property. +# +# Usage: +# zephyr_library_property( ) +# +# Current Zephyr library specific properties that are supported: +# ALLOW_EMPTY : Allow a Zephyr library to be empty. +# An empty Zephyr library will generate a CMake +# configure time warning unless `ALLOW_EMPTY` is TRUE. +function(zephyr_library_property) + set(single_args "ALLOW_EMPTY") + cmake_parse_arguments(LIB_PROP "" "${single_args}" "" ${ARGN}) + target_compile_definitions(${ZEPHYR_CURRENT_LIBRARY} PRIVATE ${item} ${ARGN}) + + if(LIB_PROP_UNPARSED_ARGUMENTS) + message(FATAL_ERROR "zephyr_library_property(${ARGV0} ...) given unknown arguments: ${FILE_UNPARSED_ARGUMENTS}") + endif() + + foreach(arg ${single_args}) + if(DEFINED LIB_PROP_${arg}) + set_property(TARGET ${ZEPHYR_CURRENT_LIBRARY} PROPERTY ${arg} ${LIB_PROP_${arg}}) + endif() + endforeach() +endfunction() + # 1.2.1 zephyr_interface_library_* # # A Zephyr interface library is a thin wrapper over a CMake INTERFACE