cmake: PROPERTY flag support on compile options and link libraries

Extend zephyr_link_libraries to allow an optional value together with
the `zephyr_link_libraries(PROPERTY <property> [<value>])`.

This allow setting linker property combined with a value when linking
Zephyr. The value will only be applied if the property is defined.

Extend zephyr_compile_options to support the same PROPERTY flag that
has been introduced for zephyr_link_libraries().
This remove the need for developers to write complex generator
expressions for compiler flags and thus minimizes mistakes.

The following syntax is now supported in addition to the existing
syntax: `zephyr_compile_options(PROPERTY <property> [<value>])`

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
This commit is contained in:
Torsten Rasmussen 2024-09-11 08:51:47 +02:00 committed by Fabio Baltieri
commit 718b726b37

View file

@ -108,16 +108,37 @@ endfunction()
# https://cmake.org/cmake/help/latest/command/target_compile_options.html # https://cmake.org/cmake/help/latest/command/target_compile_options.html
function(zephyr_compile_options) function(zephyr_compile_options)
target_compile_options(zephyr_interface INTERFACE ${ARGV}) if(ARGV0 STREQUAL "PROPERTY")
set(property $<TARGET_PROPERTY:compiler,${ARGV1}>)
set(property_defined $<BOOL:${property}>)
if(ARGC GREATER 3)
message(FATAL_ERROR "zephyr_compile_options(PROPERTY <prop> [<var>]) "
"called with too many arguments."
)
elseif(ARGC EQUAL 3)
target_compile_options(zephyr_interface INTERFACE $<${property_defined}:${property}${ARGV2}>)
else()
target_compile_options(zephyr_interface INTERFACE ${property})
endif()
else()
target_compile_options(zephyr_interface INTERFACE ${ARGV})
endif()
endfunction() endfunction()
# https://cmake.org/cmake/help/latest/command/target_link_libraries.html # https://cmake.org/cmake/help/latest/command/target_link_libraries.html
function(zephyr_link_libraries) function(zephyr_link_libraries)
if(ARGV0 STREQUAL "PROPERTY") if(ARGV0 STREQUAL "PROPERTY")
if(ARGC GREATER 2) set(property $<TARGET_PROPERTY:linker,${ARGV1}>)
message(FATAL_ERROR "zephyr_link_libraries(PROPERTY <prop>) only allows a single property.") set(property_defined $<BOOL:${property}>)
if(ARGC GREATER 3)
message(FATAL_ERROR "zephyr_link_options(PROPERTY <prop> [<val>]) "
"called with too many arguments."
)
elseif(ARGC EQUAL 3)
target_link_libraries(zephyr_interface INTERFACE $<${property_defined}:${property}${ARGV2}>)
else()
target_link_libraries(zephyr_interface INTERFACE ${property})
endif() endif()
target_link_libraries(zephyr_interface INTERFACE $<TARGET_PROPERTY:linker,${ARGV1}>)
else() else()
target_link_libraries(zephyr_interface INTERFACE ${ARGV}) target_link_libraries(zephyr_interface INTERFACE ${ARGV})
endif() endif()