Skip to content

Commit e0fb12f

Browse files
committed
cmake: zephyr_get_build_property: implement additional options
Extended the zephyr_get_build_property() CMake function with several convenience options: - AS_STRING: Returns the result as a string rather than a list. Equivalent to specifying DELIMITER " ". - GENEX: Uses generator expressions to get the property entries at build time, for targets that are not yet fully configured. Cannot be used with LANG. Also, CMake will complain if the requested property includes $<COMPILE_OPTIONS:...> expressions. - PREFIX <prefix>: Specifies a custom prefix to use for each entry in the output list. Defaults to the standard compiler flag prefix for the given property. - TARGET <target>: Specifies the target to get the property from. Defaults to "zephyr_interface" as before. Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
1 parent f25d2c3 commit e0fb12f

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

cmake/modules/extensions.cmake

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ function(zephyr_ld_options)
164164
target_ld_options(zephyr_interface INTERFACE ${ARGV})
165165
endfunction()
166166

167-
# Getter function for extracting build information from
167+
# Getter function for extracting build information from a target, by default
168168
# 'zephyr_interface'. The function supports filtering for specific compile
169169
# languages, custom prefixes, and different output formats.
170170
#
@@ -179,29 +179,51 @@ endfunction()
179179
# - 'prop' is the CMake target property to get
180180
#
181181
# Options:
182+
# - AS_STRING:
183+
# Return the result as a string rather than a list. Equivalent to
184+
# specifying DELIMITER " ".
182185
# - DELIMITER <delimiter>:
183186
# Specify the output delimiter to use for the list entries. Defaults to
184187
# "$<SEMICOLON>".
188+
# - GENEX:
189+
# Use generator expressions to get the property entries at build time,
190+
# for targets that are not yet fully configured.
191+
# Cannot be used with LANG. Also, CMake will complain if the requested
192+
# property includes $<COMPILE_OPTIONS:...> expressions.
185193
# - LANG <C|CXX|ASM>:
186194
# When set, the property value is filtered for $<COMPILE_LANGUAGE:lang>
187195
# entries, and only those matching the given language are kept.
196+
# Cannot be used with GENEX.
197+
# - PREFIX <prefix>:
198+
# Specify a custom prefix to use for each entry in the output list.
199+
# Defaults to the standard compiler flag prefix for the given property.
188200
# - STRIP_PREFIX:
189-
# Omit the compiler flag prefix (-I, -D, etc.).
201+
# Omit the compiler flag prefix (-I, -D, etc.). Equivalent to PREFIX "".
202+
# - TARGET <target>:
203+
# Specify the target to get the property from. Defaults to
204+
# "zephyr_interface".
190205

191206
function(zephyr_get_build_property output)
192-
set(options AS_STRING STRIP_PREFIX)
193-
set(single_args DELIMITER LANG PROPERTY)
207+
set(options AS_STRING GENEX STRIP_PREFIX)
208+
set(single_args DELIMITER LANG PREFIX PROPERTY TARGET)
194209
cmake_parse_arguments(args "${options}" "${single_args}" "" ${ARGN})
195210
zephyr_check_arguments_required(zephyr_get_build_property PROPERTY)
196211
if(args_UNPARSED_ARGUMENTS)
197212
message(FATAL_ERROR "zephyr_get_build_prop_for_lang() given unknown "
198213
"arguments: ${args_UNPARSED_ARGUMENTS}")
199214
endif()
215+
set_ifndef(args_TARGET "zephyr_interface")
200216

201-
set_ifndef(args_DELIMITER "$<SEMICOLON>")
217+
if(args_AS_STRING)
218+
set(args_DELIMITER " ")
219+
else()
220+
set_ifndef(args_DELIMITER "$<SEMICOLON>")
221+
endif()
202222

203223
if(args_STRIP_PREFIX)
204224
set(maybe_prefix "")
225+
elseif(args_PREFIX)
226+
set(maybe_prefix "${args_PREFIX}")
205227
elseif(args_PROPERTY MATCHES "^(INTERFACE_)?COMPILE_DEFINITIONS$")
206228
set(maybe_prefix "-D")
207229
elseif(args_PROPERTY MATCHES "^(INTERFACE_)?INCLUDE_DIRECTORIES$")
@@ -212,13 +234,21 @@ function(zephyr_get_build_property output)
212234
set(maybe_prefix "")
213235
endif()
214236

215-
get_property(flags TARGET zephyr_interface PROPERTY ${args_PROPERTY})
216-
if(args_LANG)
217-
process_flags(${args_LANG} flags output_list)
237+
if(args_LANG AND args_GENEX)
238+
message(FATAL_ERROR "zephyr_get_build_property(): GENEX cannot be used with LANG.")
239+
endif()
240+
241+
if(args_GENEX)
242+
set(genexp_output_list "$<TARGET_PROPERTY:${args_TARGET},${args_PROPERTY}>")
218243
else()
219-
set(output_list "${flags}")
244+
get_property(flags TARGET ${args_TARGET} PROPERTY ${args_PROPERTY})
245+
if(args_LANG)
246+
process_flags(${args_LANG} flags output_list)
247+
else()
248+
set(output_list "${flags}")
249+
endif()
250+
string(REPLACE ";" "$<SEMICOLON>" genexp_output_list "${output_list}")
220251
endif()
221-
string(REPLACE ";" "$<SEMICOLON>" genexp_output_list "${output_list}")
222252

223253
set(result_output_list "${maybe_prefix}$<JOIN:${genexp_output_list},${args_DELIMITER}${maybe_prefix}>")
224254
set(maybe_result_output_list "$<$<BOOL:${genexp_output_list}>:${result_output_list}>")

0 commit comments

Comments
 (0)