Skip to content

Commit

Permalink
cmake: Zephyr ALLOW_EMPTY library property
Browse files Browse the repository at this point in the history
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(<setting> <file.c>)`
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 <[email protected]>
  • Loading branch information
tejlmand authored and carlescufi committed Sep 7, 2021
1 parent eaf6cf7 commit 153196b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
9 changes: 6 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
25 changes: 25 additions & 0 deletions cmake/extensions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,31 @@ function(zephyr_library_app_memory partition)
"-l" $<TARGET_FILE_NAME:${ZEPHYR_CURRENT_LIBRARY}> "${partition}")
endfunction()

# Configure a Zephyr library specific property.
#
# Usage:
# zephyr_library_property(<property> <value>)
#
# Current Zephyr library specific properties that are supported:
# ALLOW_EMPTY <TRUE:FALSE>: 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
Expand Down

0 comments on commit 153196b

Please sign in to comment.