@@ -7,34 +7,278 @@ include_guard(GLOBAL)
77# Debug
88message (VERBOSE "rsp/debug module included" )
99
10+ include ("rsp/output/utils" )
11+ include ("rsp/helpers" )
12+
1013if (NOT COMMAND "dump" )
1114
1215 #! dump : Outputs given variables' name and value
1316 #
17+ # Note: function outputs using cmake's WARNING message mode
18+ #
19+ # @see https://cmake.org/cmake/help/latest/command/message.html#general-messages
20+ # @see var_dump()
21+ #
1422 # @param ... Variables to output
1523 #
1624 function (dump)
17- foreach (var ${ARGN} )
18- message ("${var} = ${${var} }" )
19- endforeach ()
25+ var_dump(OUTPUT output PROPERTIES ${ARGN} )
26+
27+ # Attempt to keep the formatting - see details in rsp/output::output()
28+ string (ASCII 13 CR)
29+ set (formatted_output "${CR}${COLOR_WHITE} dump:${RESTORE} \n ${output} " )
30+ string (REPLACE "\n " "\n " formatted_output "${formatted_output} " )
2031
21- # Output as warning so that the developer is able to see call stack!
22- message (WARNING " ${CMAKE_CURRENT_FUNCTION} () called from ${CMAKE_CURRENT_LIST_FILE} " )
32+ message (WARNING "${formatted_output} " )
2333 endfunction ()
2434endif ()
2535
2636if (NOT COMMAND "dd" )
2737
28- #! dump and die: Outputs given variables' name and value and stops build
38+ #! dd: Outputs given variables' name and value and stops build (dump and die)
39+ #
40+ # Note: function outputs using cmake's FATAL_ERROR message mode
41+ #
42+ # @see https://cmake.org/cmake/help/latest/command/message.html#general-messages
43+ # @see var_dump()
2944 #
3045 # @param ... Variables to output
3146 #
3247 function (dd)
33- foreach (var ${ARGN} )
34- message ("${var} = ${${var} }" )
48+ var_dump(OUTPUT output PROPERTIES ${ARGN} )
49+
50+ # Attempt to keep the formatting - see details in rsp/output::output()
51+ string (ASCII 13 CR)
52+ set (formatted_output "${CR}${COLOR_WHITE} dd:${RESTORE} \n ${output} " )
53+ string (REPLACE "\n " "\n " formatted_output "${formatted_output} " )
54+
55+ message (FATAL_ERROR "${formatted_output} " )
56+ endfunction ()
57+ endif ()
58+
59+ if (NOT COMMAND "var_dump" )
60+
61+ #! var_dump : Outputs human-readable information about given properties
62+ #
63+ #
64+ # @param [OUTPUT <variable>] Optional - If specified, information is assigned to output variable
65+ # instead of being printed to stderr.
66+ # @param [PROPERTIES <variable>...] One or more variables to dump information about.
67+ # @param [WITHOUT_NAMES] Option, if given then property names are omitted from the output
68+ # @param [IGNORE_LIST] Option, if specified the variable values of the type "list" are
69+ # treated as "string".
70+ #
71+ # @return
72+ # [OUTPUT] The resulting output variable, if OUTPUT was specified.
73+ #
74+ function (var_dump)
75+ set (options WITHOUT_NAMES IGNORE_LIST)
76+ set (oneValueArgs OUTPUT )
77+ set (multiValueArgs PROPERTIES)
78+
79+ cmake_parse_arguments (INPUT "${options} " "${oneValueArgs} " "${multiValueArgs} " ${ARGN} )
80+ requires_arguments("PROPERTIES" INPUT )
81+
82+ # ---------------------------------------------------------------------------------------------- #
83+
84+ set (buffer "" )
85+
86+ foreach (key IN LISTS INPUT_PROPERTIES)
87+ # Attempt to resolve value and it's datatype
88+ set (value "${${key} }" )
89+ get_type("${key} " type )
90+
91+ # ---------------------------------------------------------------------------------------------- #
92+
93+ set (tmp_list_separator "<!list!>" )
94+ if (INPUT_IGNORE_LIST AND type STREQUAL "list" )
95+ # Debug
96+ #message("Ignoring list: ${key} | ${value}")
97+
98+ set (type "string" )
99+ string (REPLACE ";" "${tmp_list_separator} " value "${value} " )
100+ endif ()
101+
102+ # ---------------------------------------------------------------------------------------------- #
103+
104+ # If key is defined as an environment variable, the value
105+ # must be obtained via ENV{}.
106+ if (DEFINED ENV{${key} })
107+ set (value "$ENV{${key} }" )
108+ get_type("${value} " type )
109+ elseif (NOT DEFINED ${key} AND type STREQUAL "string" )
110+ # We ONLY deal with variables, meaning that if key isn't
111+ # defined, and the type is determined to be a string,
112+ # then we must assume that it's an undefined property!
113+
114+ set (type "${COLOR_RED}${TEXT_ITALIC} undefined${TEXT_ITALIC_RESTORE}${COLOR_DEFAULT} " )
115+ endif ()
116+
117+ # Format the value...
118+ if (type STREQUAL "string" )
119+ # Resolve string length, by ensuring to count the length of
120+ # the original value, without list separator replacement.
121+ set (tmp_str "${value} " )
122+ string (REPLACE "${tmp_list_separator} " ";" tmp_str "${tmp_str} " )
123+ string (LENGTH "${tmp_str} " str_length)
124+
125+ set (type "${type} ${str_length} " )
126+ set (value "${COLOR_GREEN} \" ${value} \" ${RESTORE} " )
127+ elseif (type STREQUAL "int" OR type STREQUAL "float" )
128+ set (value "${COLOR_BRIGHT_BLUE}${value}${RESTORE} " )
129+ elseif (type STREQUAL "bool" )
130+ set (value "${COLOR_CYAN}${value}${RESTORE} " )
131+ elseif (type STREQUAL "command" )
132+ set (value "${COLOR_BLUE}${key} ()${RESTORE} " )
133+ elseif (type STREQUAL "list" )
134+ list (LENGTH value lst_length)
135+ set (type "${type} ${lst_length} " )
136+ set (list_buffer "" )
137+
138+ set (i 0) # index counter
139+ foreach (item IN LISTS value )
140+ # Get property information about the "item", but without key name.
141+ # Also, ensure to ignore values of the type "list", to avoid
142+ # strange behaviour (caused by cmake's variable scopes...)
143+ set ("list_item_${i} " "${item} " )
144+ var_dump(OUTPUT list_item WITHOUT_NAMES IGNORE_LIST PROPERTIES "list_item_${i} " )
145+
146+ # Append to list buffer and increment the "index" counter.
147+ list (APPEND list_buffer "${COLOR_MAGENTA}${i} :${RESTORE} ${list_item} " )
148+ math (EXPR i "${i} +1" OUTPUT_FORMAT DECIMAL)
149+ endforeach ()
150+
151+ string (REPLACE ";" "\n " list_buffer "${list_buffer} " )
152+ set (value "[ \n ${list_buffer} \n ]" )
153+ endif ()
154+
155+ # Mark the key as cached, if needed...
156+ if (DEFINED CACHE {${key} })
157+ set (type "${type} , ${TEXT_ITALIC}${TEXT_BOLD} cached${TEXT_BOLD_RESTORE}${TEXT_ITALIC_RESTORE} " )
158+ endif ()
159+
160+ # Mark the key an environment variable, if needed...
161+ if (DEFINED ENV{${key} })
162+ set (type "${type} , ${TEXT_ITALIC}${TEXT_BOLD} ENV${TEXT_BOLD_RESTORE}${TEXT_ITALIC_RESTORE} " )
163+ endif ()
164+
165+ # The output format: <key> = (<type>) <value>
166+ # Unless key is omitted.
167+ set (formatted_key "${COLOR_BRIGHT_MAGENTA}${key}${RESTORE} = " )
168+ if (INPUT_WITHOUT_NAMES)
169+ set (formatted_key "" )
170+ endif ()
171+
172+ list (APPEND buffer "${formatted_key}${COLOR_WHITE} (${type}${COLOR_WHITE} )${RESTORE} ${value} " )
35173 endforeach ()
36174
37- # Output as fatal error to ensure that build stops.
38- message (FATAL_ERROR " ${CMAKE_CURRENT_FUNCTION} () called from ${CMAKE_CURRENT_LIST_FILE} " )
175+ # ---------------------------------------------------------------------------------------------- #
176+
177+ string (REPLACE ";" "\n " buffer "${buffer} " )
178+
179+ # Restore list value (as a string) if needed.
180+ if (INPUT_IGNORE_LIST)
181+ string (REPLACE "${tmp_list_separator} " ";" buffer "${buffer} " )
182+ endif ()
183+
184+ # ---------------------------------------------------------------------------------------------- #
185+
186+ # Assign to output variable, if requested and stop any further processing.
187+ if (DEFINED INPUT_OUTPUT)
188+ set ("${INPUT_OUTPUT} " "${buffer} " )
189+ return (PROPAGATE "${INPUT_OUTPUT} " )
190+ endif ()
191+
192+ # ---------------------------------------------------------------------------------------------- #
193+
194+ message (NOTICE "${buffer} " )
195+
39196 endfunction ()
40- endif ()
197+ endif ()
198+
199+ if (NOT COMMAND "build_info" )
200+
201+ #! build_info : Output build information to stdout or stderr (Cmake's message type specific)
202+ #
203+ # @see https://cmake.org/cmake/help/latest/command/message.html
204+ #
205+ # @param [<mode>] Option - Cmake's message type. Defaults to NOTICE, when not specified.
206+ # @param [OUTPUT <variable>] Optional - If specified, message is assigned to output variable instead of
207+ # being printed to stdout or stderr.
208+ #
209+ # @return
210+ # [OUTPUT] The resulting output variable, if OUTPUT was specified.
211+ #
212+ function (build_info)
213+ set (options "${RSP_CMAKE_MESSAGE_MODES} " )
214+ set (oneValueArgs OUTPUT )
215+ set (multiValueArgs "" )
216+
217+ cmake_parse_arguments (INPUT "${options} " "${oneValueArgs} " "${multiValueArgs} " ${ARGN} )
218+
219+ # ---------------------------------------------------------------------------------------------- #
220+
221+ # Message mode
222+ resolve_msg_mode("NOTICE" )
223+
224+ # ---------------------------------------------------------------------------------------------- #
225+
226+ set (info_list
227+ # Build Type
228+ # @see https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html
229+ "Type|${CMAKE_BUILD_TYPE} "
230+
231+ # Library Type (NOTE: LIB_TYPE is NOT a predefined cmake variable)
232+ "Library Type|${LIB_TYPE} "
233+
234+ # Compiler flags
235+ "Compiler flags|${CMAKE_CXX_COMPILE_FLAGS} "
236+
237+ # Compiler cxx debug flags
238+ "Compiler cxx debug flags|${CMAKE_CXX_FLAGS_DEBUG} "
239+
240+ # Compiler cxx release flags
241+ "Compiler cxx release flags|${CMAKE_CXX_FLAGS_RELEASE} "
242+
243+ # Compiler cxx min size flags
244+ "Compiler cxx min size flags|${CMAKE_CXX_FLAGS_MINSIZEREL} "
245+
246+ # Compiler cxx flags
247+ "Compiler cxx flags|${CMAKE_CXX_FLAGS} "
248+ )
249+
250+ # ---------------------------------------------------------------------------------------------- #
251+
252+ set (buffer "" )
253+
254+ foreach (item IN LISTS info_list)
255+ string (REPLACE "|" ";" parts "${item} " )
256+ list (GET parts 0 label)
257+ list (GET parts 1 value )
258+
259+ list (APPEND buffer "${COLOR_MAGENTA}${label} :${RESTORE} ${value} " )
260+ endforeach ()
261+
262+ # ---------------------------------------------------------------------------------------------- #
263+
264+ # Convert list to a string with newlines...
265+ string (REPLACE ";" "\n " buffer "${buffer} " )
266+
267+ # Attempt to keep the formatting - see details in rsp/output::output()
268+ string (ASCII 13 CR)
269+ set (formatted_output "${CR}${COLOR_BRIGHT_MAGENTA} build info:${RESTORE} \n ${buffer} " )
270+ string (REPLACE "\n " "\n " formatted_output "${formatted_output} " )
271+
272+ # ---------------------------------------------------------------------------------------------- #
273+
274+ # Assign to output variable, if requested and stop any further processing.
275+ if (DEFINED INPUT_OUTPUT)
276+ set ("${INPUT_OUTPUT} " "${formatted_output} " )
277+ return (PROPAGATE "${INPUT_OUTPUT} " )
278+ endif ()
279+
280+ # ---------------------------------------------------------------------------------------------- #
281+
282+ message (${msg_mode} "${formatted_output} " )
283+ endfunction ()
284+ endif ()
0 commit comments