Skip to content

Commit 4dfc28b

Browse files
committed
Fix issue where list values were treated as "nested" lists
1 parent a58a4f2 commit 4dfc28b

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

cmake/rsp/debug.cmake

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ if (NOT COMMAND "var_dump")
5050
# instead of being printed to stderr.
5151
# @param [PROPERTIES <variable>...] One or more variables to dump information about.
5252
# @param [WITHOUT_NAMES] Option, if given then property names are omitted from the output
53+
# @param [IGNORE_LIST] Option, if specified the variable values of the type "list" are
54+
# treated as "string".
5355
#
5456
# @return
5557
# [OUTPUT] The resulting output variable, if OUTPUT was specified.
5658
#
5759
function(var_dump)
58-
set(options WITHOUT_NAMES)
60+
set(options WITHOUT_NAMES IGNORE_LIST)
5961
set(oneValueArgs OUTPUT)
6062
set(multiValueArgs PROPERTIES)
6163

@@ -69,7 +71,20 @@ if (NOT COMMAND "var_dump")
6971
foreach (key IN LISTS INPUT_PROPERTIES)
7072
# Attempt to resolve value and it's datatype
7173
set(value "${${key}}")
72-
get_type(key type)
74+
get_type("${key}" type)
75+
76+
# ---------------------------------------------------------------------------------------------- #
77+
78+
set(tmp_list_separator "<!list!>")
79+
if (INPUT_IGNORE_LIST AND type STREQUAL "list")
80+
# Debug
81+
#message("Ignoring list: ${key} | ${value}")
82+
83+
set(type "string")
84+
string(REPLACE ";" "${tmp_list_separator}" value "${value}")
85+
endif ()
86+
87+
# ---------------------------------------------------------------------------------------------- #
7388

7489
# If key is defined as an environment variable, the value
7590
# must be obtained via ENV{}.
@@ -86,7 +101,12 @@ if (NOT COMMAND "var_dump")
86101

87102
# Format the value...
88103
if (type STREQUAL "string")
89-
string(LENGTH "${value}" str_length)
104+
# Resolve string length, by ensuring to count the length of
105+
# the original value, without list separator replacement.
106+
set(tmp_str "${value}")
107+
string(REPLACE "${tmp_list_separator}" ";" tmp_str "${tmp_str}")
108+
string(LENGTH "${tmp_str}" str_length)
109+
90110
set(type "${type} ${str_length}")
91111
set(value "${COLOR_GREEN}\"${value}\"${RESTORE}")
92112
elseif (type STREQUAL "int" OR type STREQUAL "float")
@@ -103,7 +123,10 @@ if (NOT COMMAND "var_dump")
103123
set(i 0) # index counter
104124
foreach (item IN LISTS value)
105125
# Get property information about the "item", but without key name.
106-
var_dump(OUTPUT list_item WITHOUT_NAMES PROPERTIES item)
126+
# Also, ensure to ignore values of the type "list", to avoid
127+
# strange behaviour (caused by cmake's variable scopes...)
128+
set("list_item_${i}" "${item}")
129+
var_dump(OUTPUT list_item WITHOUT_NAMES IGNORE_LIST PROPERTIES "list_item_${i}")
107130

108131
# Append to list buffer and increment the "index" counter.
109132
list(APPEND list_buffer "${COLOR_MAGENTA}${i}:${RESTORE} ${list_item}")
@@ -134,8 +157,15 @@ if (NOT COMMAND "var_dump")
134157
list(APPEND buffer "${formatted_key}${COLOR_WHITE}(${type}${COLOR_WHITE})${RESTORE} ${value}")
135158
endforeach ()
136159

160+
# ---------------------------------------------------------------------------------------------- #
161+
137162
string(REPLACE ";" "\n" buffer "${buffer}")
138163

164+
# Restore list value (as a string) if needed.
165+
if (INPUT_IGNORE_LIST)
166+
string(REPLACE "${tmp_list_separator}" ";" buffer "${buffer}")
167+
endif ()
168+
139169
# ---------------------------------------------------------------------------------------------- #
140170

141171
# Assign to output variable, if requested and stop any further processing.

0 commit comments

Comments
 (0)