-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogVars.cmake
203 lines (161 loc) · 5.23 KB
/
LogVars.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
include_guard()
#[[
log_vars(var_names... [MODE <mode>] [RAW_LISTS])
Print a table of all given variables and their values.
Parameters
----------
var_names... :
Names of variables to log.
Options
----------
MODE <mode> :
Message mode. Defaults to STATUS
TO_VAR <var> :
Store the message in <var> instead of printing it.
Depending on MODE, message lines are prefixed with e.g. "--"
RAW_LISTS :
Do not print list values on separate lines
]]
function(log_vars)
set(prefix "__${CMAKE_CURRENT_FUNCTION}")
# cmake_parse_arguments() adds one escape backslash to semicolons in arguments
cmake_parse_arguments(PARSE_ARGV 0 ${prefix} "RAW_LISTS" "MODE;TO_VAR" "") # +1 escape backslash = 1
set(raw_lists "${${prefix}_RAW_LISTS}")
set(mode "${${prefix}_MODE}")
set(to_var ${${prefix}_TO_VAR})
unset(msg)
if(mode MATCHES "^(STATUS|VERBOSE|DEBUG|TRACE)$")
set(line_prefix "-- ")
elseif(mode MATCHES "WARNING|ERROR")
set(indent " ")
endif()
foreach(var IN LISTS ${prefix}_UNPARSED_ARGUMENTS)
if(NOT DEFINED ${var})
set(${var} "(undefined)")
elseif("${${var}}" STREQUAL "")
set(${var} "(empty)")
endif()
if(raw_lists)
string(REPLACE ";" "\;" ${var} "${${var}}")
else()
list(LENGTH ${var} length)
if(length GREATER 1)
string(PREPEND ${var} "(list:);")
string(REPLACE ";" "\n${indent}${line_prefix} - " ${var} "${${var}}")
endif()
endif()
list(APPEND msg "${indent}\${${var}} : ${${var}}")
endforeach()
if(to_var)
string(REPLACE "\;" "\\\;" msg "${msg}") # +1 escape backslash = 2
list(TRANSFORM msg PREPEND "${line_prefix}") # -1 escape backslash = 1
list(JOIN msg "\n" msg) # -1 escape backslash = 0
set(${to_var} "${msg}" PARENT_SCOPE)
else()
# message() prefixes the first line
list(JOIN msg "\n${line_prefix}" msg) # -1 escape backslash = 0
message(${mode} "${msg}")
endif()
endfunction()
# For immediate developer utility, force_log_vars() calls log_vars() in FATAL_ERROR mode.
function(force_log_vars)
list(APPEND ARGV "MODE" "FATAL_ERROR")
log_vars(${ARGV})
endfunction()
#[[
log_all_vars([FILTER <regex>] [MODE <mode>])
Log all known variables.
Options
----------
FILTER <regex> :
Only log variables matching the given regex.
MODE <mode> :
Message mode. Defaults to STATUS
]]
macro(log_all_vars)
# This is a macro and not a function, because function() introduces new
# variables into scope before get_cmake_property(VARIABLES) is called.
get_cmake_property(_vars VARIABLES)
cmake_parse_arguments(_args "" "FILTER;MODE;MESSAGE" "" ${ARGN})
if(_args_MESSAGE)
set(_notice "${_args_MESSAGE}")
else()
set(_notice "All variables")
endif()
if(_args_FILTER)
if(NOT _args_MESSAGE)
set(_notice "${_notice} matching regex '${_args_FILTER}'")
endif()
list(FILTER _vars INCLUDE REGEX "${_args_FILTER}")
endif()
if(_vars)
if(_args_MODE)
set(_forward_mode "MODE;${_args_MODE}")
endif()
log_vars(TO_VAR _msg ${_forward_mode} ${_vars})
message(${_args_MODE} "${_notice}:\n${_msg}")
else()
message(${_args_MODE} "${_notice}: (none)")
endif()
unset(_args)
unset(_args_FILTER)
unset(_args_MESSAGE)
unset(_args_MODE)
unset(_forward_mode)
unset(_msg)
unset(_notice)
unset(_vars)
endmacro()
include(Expect)
expect_test_preamble()
function(test_log_does_not_replace_empty_values)
set(my_var "")
expect(my_var STREQUAL "")
endfunction()
test_log_does_not_replace_empty_values()
function(test_log_vars_output)
return() # comment-out to view output
set(a "value")
set(b "a;b;c")
set(c "")
message("----------------------------------------")
log_vars(a b a RAW_LISTS)
log_vars(c d RAW_LISTS)
message("----------------------------------------")
log_vars(a b a)
log_vars(c d)
message("----------------------------------------")
log_vars(a b a RAW_LISTS MODE STATUS)
log_vars(c d RAW_LISTS MODE STATUS)
message("----------------------------------------")
log_vars(a b a MODE STATUS)
log_vars(c d MODE STATUS)
message("----------------------------------------")
log_vars(a b a RAW_LISTS MODE WARNING)
log_vars(c d RAW_LISTS MODE WARNING)
message("----------------------------------------")
log_vars(a b a MODE WARNING)
log_vars(c d MODE WARNING)
message("----------------------------------------")
endfunction()
test_log_vars_output()
function(test_log_vars_to_var)
set(a "value")
set(b "a;b;c")
log_vars(a b a RAW_LISTS TO_VAR msg)
expect("${msg}" STREQUAL "\${a} : value\n\${b} : a;b;c\n\${a} : value")
log_vars(a b a TO_VAR msg)
expect("${msg}" STREQUAL "\${a} : value\n\${b} : (list:)\n - a\n - b\n - c\n\${a} : value")
log_vars(a b a RAW_LISTS TO_VAR msg MODE STATUS)
expect("${msg}" STREQUAL "-- \${a} : value\n-- \${b} : a;b;c\n-- \${a} : value")
log_vars(a b a TO_VAR msg MODE STATUS)
expect("${msg}" STREQUAL "-- \${a} : value\n-- \${b} : (list:)\n-- - a\n-- - b\n-- - c\n-- \${a} : value")
log_vars(a b a RAW_LISTS TO_VAR msg MODE WARNING)
expect("${msg}" STREQUAL " \${a} : value\n \${b} : a;b;c\n \${a} : value")
log_vars(a b a TO_VAR msg MODE WARNING)
expect("${msg}" STREQUAL " \${a} : value\n \${b} : (list:)\n - a\n - b\n - c\n \${a} : value")
set(empty_var "")
log_vars(empty_var undefined_var TO_VAR msg)
expect("${msg}" STREQUAL "\${empty_var} : (empty)\n\${undefined_var} : (undefined)")
endfunction()
test_log_vars_to_var()