Skip to content

Commit f992a7c

Browse files
committed
Add var_dump()
1 parent 996bc18 commit f992a7c

File tree

1 file changed

+113
-1
lines changed

1 file changed

+113
-1
lines changed

cmake/rsp/debug.cmake

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ include_guard(GLOBAL)
77
# Debug
88
message(VERBOSE "rsp/debug module included")
99

10+
include("rsp/helpers")
11+
1012
if (NOT COMMAND "dump")
1113

1214
#! dump : Outputs given variables' name and value
@@ -37,4 +39,114 @@ if (NOT COMMAND "dd")
3739
# Output as fatal error to ensure that build stops.
3840
message(FATAL_ERROR " ${CMAKE_CURRENT_FUNCTION}() called from ${CMAKE_CURRENT_LIST_FILE}")
3941
endfunction()
40-
endif ()
42+
endif ()
43+
44+
if (NOT COMMAND "var_dump")
45+
46+
#! var_dump : Output human readable information about given properties
47+
#
48+
#
49+
# @param [OUTPUT <variable>] Optional - If specified, information is assigned to output variable
50+
# instead of being printed to stderr.
51+
# @param [WITHOUT_NAMES] Option, if given then property names are omitted from the output
52+
# @param [PROPERTIES <variable>...] One or more variables to dump information about.
53+
#
54+
# @return
55+
# [OUTPUT] The resulting output variable, if OUTPUT was specified.
56+
#
57+
function(var_dump)
58+
set(options WITHOUT_NAMES)
59+
set(oneValueArgs OUTPUT)
60+
set(multiValueArgs PROPERTIES)
61+
62+
cmake_parse_arguments(INPUT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
63+
requires_arguments("PROPERTIES" INPUT)
64+
65+
# ---------------------------------------------------------------------------------------------- #
66+
67+
set(buffer "")
68+
69+
foreach (key IN LISTS INPUT_PROPERTIES)
70+
# Attempt to resolve value and it's datatype
71+
set(value "${${key}}")
72+
get_type(key type)
73+
74+
# If key is defined as an environment variable, the value
75+
# must be obtained via ENV{}.
76+
if (DEFINED ENV{${key}})
77+
set(value "$ENV{${key}}")
78+
get_type("${value}" type)
79+
elseif (NOT DEFINED ${key} AND type STREQUAL "string")
80+
# We ONLY deal with variables, meaning that if key isn't
81+
# defined, and the type is determined to be a string,
82+
# then we must assume that it's an undefined property!
83+
84+
set(type "${COLOR_RED}${TEXT_ITALIC}undefined${TEXT_ITALIC_RESTORE}${COLOR_DEFAULT}")
85+
endif ()
86+
87+
# Format the value...
88+
if (type STREQUAL "string")
89+
string(LENGTH "${value}" str_length)
90+
set(type "${type} ${str_length}")
91+
set(value "${COLOR_GREEN}\"${value}\"${RESTORE}")
92+
elseif (type STREQUAL "int" OR type STREQUAL "float")
93+
set(value "${COLOR_BRIGHT_BLUE}${value}${RESTORE}")
94+
elseif (type STREQUAL "bool")
95+
set(value "${COLOR_CYAN}${value}${RESTORE}")
96+
elseif (type STREQUAL "command")
97+
set(value "${COLOR_BLUE}${key}()${RESTORE}")
98+
elseif (type STREQUAL "list")
99+
list(LENGTH value lst_length)
100+
set(type "${type} ${lst_length}")
101+
set(list_buffer "")
102+
103+
set(i 0) # index counter
104+
foreach (item IN LISTS value)
105+
# Get property information about the "item", but without key name.
106+
var_dump(OUTPUT list_item WITHOUT_NAMES PROPERTIES item)
107+
108+
# Append to list buffer and increment the "index" counter.
109+
list(APPEND list_buffer "${COLOR_MAGENTA}${i}:${RESTORE} ${list_item}")
110+
math(EXPR i "${i}+1" OUTPUT_FORMAT DECIMAL)
111+
endforeach ()
112+
113+
string(REPLACE ";" "\n " list_buffer "${list_buffer}")
114+
set(value "[ \n ${list_buffer}\n]")
115+
endif ()
116+
117+
# Mark the key as cached, if needed...
118+
if(DEFINED CACHE{${key}})
119+
set(type "${type}, ${TEXT_ITALIC}${TEXT_BOLD}cached${TEXT_BOLD_RESTORE}${TEXT_ITALIC_RESTORE}")
120+
endif ()
121+
122+
# Mark the key an environment variable, if needed...
123+
if(DEFINED ENV{${key}})
124+
set(type "${type}, ${TEXT_ITALIC}${TEXT_BOLD}ENV${TEXT_BOLD_RESTORE}${TEXT_ITALIC_RESTORE}")
125+
endif ()
126+
127+
# The output format: <key> = (<type>) <value>
128+
# Unless key is omitted.
129+
set(formatted_key "${COLOR_BRIGHT_MAGENTA}${key}${RESTORE} = ")
130+
if (INPUT_WITHOUT_NAMES)
131+
set(formatted_key "")
132+
endif ()
133+
134+
list(APPEND buffer "${formatted_key}${COLOR_WHITE}(${type}${COLOR_WHITE})${RESTORE} ${value}")
135+
endforeach ()
136+
137+
string(REPLACE ";" "\n" buffer "${buffer}")
138+
139+
# ---------------------------------------------------------------------------------------------- #
140+
141+
# Assign to output variable, if requested and stop any further processing.
142+
if (DEFINED INPUT_OUTPUT)
143+
set("${INPUT_OUTPUT}" "${buffer}")
144+
return(PROPAGATE "${INPUT_OUTPUT}")
145+
endif ()
146+
147+
# ---------------------------------------------------------------------------------------------- #
148+
149+
message(NOTICE "${buffer}")
150+
151+
endfunction()
152+
endif ()

0 commit comments

Comments
 (0)