@@ -72,6 +72,29 @@ function(internal_yaml_context_free)
7272 endif ()
7373endfunction ()
7474
75+ # Internal helper function to provide the correct initializer for a list in the
76+ # JSON content.
77+ function (internal_yaml_list_initializer var)
78+ set (${var} "[]" PARENT_SCOPE)
79+ endfunction ()
80+
81+ # Internal helper function to append items to a list in the JSON content.
82+ # Unassigned arguments are the values to be appended.
83+ function (internal_yaml_list_append var key)
84+ set (json_content "${${var} }" )
85+ string (JSON subjson GET "${json_content} " ${key} )
86+ string (JSON index LENGTH "${subjson} " )
87+ list (LENGTH ARGN length )
88+ math (EXPR stop "${index} + ${length} - 1" )
89+ if (NOT length EQUAL 0)
90+ foreach (i RANGE ${index} ${stop} )
91+ list (POP_FRONT ARGN value )
92+ string (JSON json_content SET "${json_content} " ${key} ${i} "\" ${value} \" " )
93+ endforeach ()
94+ endif ()
95+ set (${var} "${json_content} " PARENT_SCOPE)
96+ endfunction ()
97+
7598# Usage
7699# yaml_context(EXISTS NAME <name> <result>)
77100#
@@ -265,6 +288,18 @@ function(yaml_set)
265288
266289 zephyr_get_scoped(json_content ${ARG_YAML_NAME} JSON)
267290
291+ if (DEFINED ARG_YAML_LIST
292+ OR LIST IN_LIST ARG_YAML_KEYWORDS_MISSING_VALUES)
293+ set (key_is_list TRUE )
294+ endif ()
295+
296+ if (ARG_YAML_APPEND AND NOT key_is_list)
297+ message (FATAL_ERROR "${CMAKE_CURRENT_FUNCTION} (APPEND ...) can only be used with argument: LIST" )
298+ endif ()
299+
300+ zephyr_get_scoped(json_content ${ARG_YAML_NAME} JSON)
301+ zephyr_get_scoped(genex ${ARG_YAML_NAME} GENEX)
302+
268303 set (yaml_key_undefined ${ARG_YAML_KEY} )
269304 foreach (k ${yaml_key_undefined} )
270305 list (REMOVE_AT yaml_key_undefined 0)
@@ -281,8 +316,8 @@ function(yaml_set)
281316
282317 list (REVERSE yaml_key_undefined)
283318 if (NOT "${yaml_key_undefined} " STREQUAL "" )
284- if (ARG_YAML_APPEND )
285- set (json_string "[]" )
319+ if (key_is_list )
320+ internal_yaml_list_initializer (json_string)
286321 else ()
287322 set (json_string "\"\" " )
288323 endif ()
@@ -295,21 +330,13 @@ function(yaml_set)
295330 )
296331 endif ()
297332
298- if (DEFINED ARG_YAML_LIST OR LIST IN_LIST ARG_YAML_KEYWORDS_MISSING_VALUES )
333+ if (key_is_list )
299334 if (NOT ARG_YAML_APPEND)
300- string (JSON json_content SET "${json_content} " ${ARG_YAML_KEY} "[]" )
335+ internal_yaml_list_initializer(json_string)
336+ string (JSON json_content SET "${json_content} " ${ARG_YAML_KEY} "${json_string} " )
301337 endif ()
302338
303- string (JSON subjson GET "${json_content} " ${ARG_YAML_KEY} )
304- string (JSON index LENGTH "${subjson} " )
305- list (LENGTH ARG_YAML_LIST length )
306- math (EXPR stop "${index} + ${length} - 1" )
307- if (NOT length EQUAL 0)
308- foreach (i RANGE ${index} ${stop} )
309- list (POP_FRONT ARG_YAML_LIST value )
310- string (JSON json_content SET "${json_content} " ${ARG_YAML_KEY} ${i} "\" ${value} \" " )
311- endforeach ()
312- endif ()
339+ internal_yaml_list_append(json_content "${ARG_YAML_KEY} " ${ARG_YAML_LIST} )
313340 else ()
314341 string (JSON json_content SET "${json_content} " ${ARG_YAML_KEY} "\" ${ARG_YAML_VALUE} \" " )
315342 endif ()
0 commit comments