-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcth_assertions.cmake
More file actions
402 lines (300 loc) · 11.5 KB
/
cth_assertions.cmake
File metadata and controls
402 lines (300 loc) · 11.5 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# Copyright (c) 2026 Lukas Thomann
# Licensed under the MIT License
cmake_minimum_required(VERSION 4.1)
#[[.rst:
.. command:: _cth_assertion_failure (internal)
.. code-block:: cmake
_cth_assertion_failure(<level> <reason> <args...>)
Internal macro to report an assertion failure.
:param level: Message level (e.g., FATAL_ERROR, WARNING)
:type level: string
:param reason: Error message describing the failure
:type reason: string
:param args: Additional context to append to error message
:type args: optional arguments
.. warning::
This is an internal function. Use the public assertion functions instead.
#]]
macro(_cth_assertion_failure level reason)
if("${level}" STREQUAL "")
set(level FATAL_ERROR)
endif()
if("${reason}" STREQUAL "")
set(reason "unknown reason")
endif()
if("${ARGN}" STREQUAL "")
set(ARG_STR "")
else()
set(ARG_STR " [args: ${ARGN}]")
endif()
message(${level} "ERROR: ${reason}${ARG_STR}")
endmacro()
#[[.rst:
.. command:: cth_assert_true
.. code-block:: cmake
cth_assert_true(<condition...> [FATAL|WARNING] REASON <reason>)
Asserts that a boolean condition evaluates to TRUE.
:param condition: CMake boolean expression to evaluate
:type condition: boolean expression
:param FATAL: (Default) Terminate configuration with FATAL_ERROR if condition is FALSE
:param WARNING: Issue a WARNING if condition is FALSE
:param REASON: Error message to display if the condition is FALSE
:type REASON: string
:pre: condition is a valid CMake boolean expression
:post: condition evaluates to TRUE, or configuration reports failure based on level
#]]
function(cth_assert_true)
set(options FATAL WARNING)
set(oneValueArgs REASON)
cmake_parse_arguments(PARSE_ARGV 0 ARG "${options}" "${oneValueArgs}" "")
set(LEVEL FATAL_ERROR)
if(ARG_WARNING)
set(LEVEL WARNING)
endif()
if(NOT ${ARG_UNPARSED_ARGUMENTS})
_cth_assertion_failure(${LEVEL} "${ARG_REASON}")
endif()
endfunction()
#[[.rst:
.. command:: cth_assert_false
.. code-block:: cmake
cth_assert_false(<condition...> [FATAL|WARNING] REASON <reason>)
Asserts that a boolean condition evaluates to FALSE.
:param condition: CMake boolean expression to evaluate
:type condition: boolean expression
:param FATAL: (Default) Terminate configuration with FATAL_ERROR if condition is TRUE
:param WARNING: Issue a WARNING if condition is TRUE
:param REASON: Error message to display if the condition is TRUE
:type REASON: string
:pre: condition is a valid CMake boolean expression
:post: condition evaluates to FALSE, or configuration reports failure based on level
#]]
function(cth_assert_false)
set(options FATAL WARNING)
set(oneValueArgs REASON)
cmake_parse_arguments(PARSE_ARGV 0 ARG "${options}" "${oneValueArgs}" "")
set(LEVEL FATAL_ERROR)
if(ARG_WARNING)
set(LEVEL WARNING)
endif()
if(${ARG_UNPARSED_ARGUMENTS})
_cth_assertion_failure(${LEVEL} "${ARG_REASON}")
endif()
endfunction()
#[[.rst:
.. command:: cth_assert_not_cmd
.. code-block:: cmake
cth_assert_not_cmd(<cmd> [FATAL|WARNING] [REASON <reason>])
Asserts that a CMake command, function, or macro is NOT defined.
:param cmd: Name of the command to check
:type cmd: string
:param FATAL: (Default) Terminate configuration with FATAL_ERROR if assertion fails
:param WARNING: Issue a WARNING if assertion fails
:param REASON: Optional error message to display if the assertion fails
:type REASON: string
:post: cmd is NOT a defined command/function/macro, or configuration reports failure based on level
#]]
function(cth_assert_not_cmd cmd)
set(options FATAL WARNING)
set(oneValueArgs REASON)
cmake_parse_arguments(PARSE_ARGV 1 ARG "${options}" "${oneValueArgs}" "")
set(LEVEL_FLAG FATAL)
if(ARG_WARNING)
set(LEVEL_FLAG WARNING)
endif()
if(NOT ARG_REASON)
set(ARG_REASON "Command '${cmd}' already defined")
endif()
cth_assert_false(COMMAND ${cmd} ${LEVEL_FLAG} REASON "${ARG_REASON}")
endfunction()
#[[.rst:
.. command:: cth_assert_cmd
.. code-block:: cmake
cth_assert_cmd(<cmd> [FATAL|WARNING] [REASON <reason>])
Asserts that a CMake command, function, or macro is defined.
:param cmd: Name of the command to check
:type cmd: string
:param FATAL: (Default) Terminate configuration with FATAL_ERROR if assertion fails
:param WARNING: Issue a WARNING if assertion fails
:param REASON: Optional error message to display if the assertion fails
:type REASON: string
:post: cmd is a defined command/function/macro, or configuration reports failure based on level
#]]
function(cth_assert_cmd cmd)
set(options FATAL WARNING)
set(oneValueArgs REASON)
cmake_parse_arguments(PARSE_ARGV 1 ARG "${options}" "${oneValueArgs}" "")
set(LEVEL_FLAG FATAL)
if(ARG_WARNING)
set(LEVEL_FLAG WARNING)
endif()
if(NOT ARG_REASON)
set(ARG_REASON "Command '${cmd}' not defined")
endif()
cth_assert_true(COMMAND ${cmd} ${LEVEL_FLAG} REASON "${ARG_REASON}")
endfunction()
#[[.rst:
.. command:: cth_assert_target
.. code-block:: cmake
cth_assert_target(<target> [FATAL|WARNING] [REASON <reason>])
Asserts that a CMake target exists in the current scope.
:param target: Name of the target to check
:type target: string
:param FATAL: (Default) Terminate configuration with FATAL_ERROR if assertion fails
:param WARNING: Issue a WARNING if assertion fails
:param REASON: Optional error message to display if the assertion fails
:type REASON: string
:post: target exists, or configuration reports failure based on level
#]]
function(cth_assert_target target)
set(options FATAL WARNING)
set(oneValueArgs REASON)
cmake_parse_arguments(PARSE_ARGV 1 ARG "${options}" "${oneValueArgs}" "")
set(LEVEL_FLAG FATAL)
if(ARG_WARNING)
set(LEVEL_FLAG WARNING)
endif()
if(NOT ARG_REASON)
set(ARG_REASON "Target '${target}' does not exist")
endif()
cth_assert_true(TARGET ${target} ${LEVEL_FLAG} REASON "${ARG_REASON}")
endfunction()
#[[.rst:
.. command:: cth_assert_not_target
.. code-block:: cmake
cth_assert_not_target(<target> [FATAL|WARNING] [REASON <reason>])
Asserts that a CMake target does NOT exist in the current scope.
:param target: Name of the target to check
:type target: string
:param FATAL: (Default) Terminate configuration with FATAL_ERROR if assertion fails
:param WARNING: Issue a WARNING if assertion fails
:param REASON: Optional error message to display if the assertion fails
:type REASON: string
:post: target does NOT exist, or configuration reports failure based on level
#]]
function(cth_assert_not_target target)
set(options FATAL WARNING)
set(oneValueArgs REASON)
cmake_parse_arguments(PARSE_ARGV 1 ARG "${options}" "${oneValueArgs}" "")
set(LEVEL_FLAG FATAL)
if(ARG_WARNING)
set(LEVEL_FLAG WARNING)
endif()
if(NOT ARG_REASON)
set(ARG_REASON "Target '${target}' already exists")
endif()
cth_assert_false(TARGET ${target} ${LEVEL_FLAG} REASON "${ARG_REASON}")
endfunction()
#[[.rst:
.. command:: cth_assert_empty
.. code-block:: cmake
cth_assert_empty(<value> [FATAL|WARNING] [REASON <reason>])
Asserts that a value is an empty string.
:param value: Value to check for emptiness
:type value: string
:param FATAL: (Default) Terminate configuration with FATAL_ERROR if assertion fails
:param WARNING: Issue a WARNING if assertion fails
:param REASON: Optional error message to display if the assertion fails
:type REASON: string
:post: value is an empty string, or configuration reports failure based on level
#]]
function(cth_assert_empty value)
set(options FATAL WARNING)
set(oneValueArgs REASON)
cmake_parse_arguments(PARSE_ARGV 1 ARG "${options}" "${oneValueArgs}" "")
set(LEVEL FATAL_ERROR)
if(ARG_WARNING)
set(LEVEL WARNING)
endif()
if(NOT ("${value}" STREQUAL ""))
if(NOT ARG_REASON)
set(ARG_REASON "Value not empty: '${value}'")
endif()
_cth_assertion_failure(${LEVEL} "${ARG_REASON}")
endif()
endfunction()
#[[.rst:
.. command:: cth_assert_not_empty
.. code-block:: cmake
cth_assert_not_empty(<value> [FATAL|WARNING] [REASON <reason>])
Asserts that a value is NOT an empty string.
:param value: Value to check for non-emptiness
:type value: string
:param FATAL: (Default) Terminate configuration with FATAL_ERROR if assertion fails
:param WARNING: Issue a WARNING if assertion fails
:param REASON: Optional error message to display if the assertion fails
:type REASON: string
:post: value is NOT an empty string, or configuration reports failure based on level
#]]
function(cth_assert_not_empty value)
set(options FATAL WARNING)
set(oneValueArgs REASON)
cmake_parse_arguments(PARSE_ARGV 1 ARG "${options}" "${oneValueArgs}" "")
set(LEVEL FATAL_ERROR)
if(ARG_WARNING)
set(LEVEL WARNING)
endif()
if("${value}" STREQUAL "")
if(NOT ARG_REASON)
set(ARG_REASON "Value is empty")
endif()
_cth_assertion_failure(${LEVEL} "${ARG_REASON}")
endif()
endfunction()
#[[.rst:
.. command:: cth_assert_program
.. code-block:: cmake
cth_assert_program(<prog> [FATAL|WARNING] [REASON <reason>] [args...])
Asserts an external program exists.
:param prog: Name of the program to find
:type prog: string
:param FATAL: (Default) Terminate configuration with FATAL_ERROR if assertion fails
:param WARNING: Issue a WARNING if assertion fails
:param REASON: Optional error message to display if the assertion fails
:type REASON: string
:param args: Additional arguments to pass to find_program (e.g., PATHS, HINTS)
:type args: optional arguments
:post: program found or configuration reports failure based on level
#]]
function(cth_assert_program prog)
set(options FATAL WARNING)
set(oneValueArgs REASON)
cmake_parse_arguments(PARSE_ARGV 1 ARG "${options}" "${oneValueArgs}" "")
set(LEVEL_FLAG FATAL)
if(ARG_WARNING)
set(LEVEL_FLAG WARNING)
endif()
find_program(TEMP "${prog}" ${ARG_UNPARSED_ARGUMENTS})
if(NOT ARG_REASON)
set(ARG_REASON "Program '${prog}' not found")
endif()
cth_assert_true(TEMP ${LEVEL_FLAG} REASON "${ARG_REASON}")
endfunction()
#[[.rst:
.. command:: cth_assert_file
.. code-block:: cmake
cth_assert_file(<file> [FATAL|WARNING] [REASON <reason>])
Asserts that a file exists and is not a directory.
:param file: Path to the file to check
:type file: string
:param FATAL: (Default) Terminate configuration with FATAL_ERROR if assertion fails
:param WARNING: Issue a WARNING if assertion fails
:param REASON: Optional error message to display if the assertion fails
:type REASON: string
:post: file exists and is not a directory or configuration reports failure based on level
#]]
function(cth_assert_file file)
set(options FATAL WARNING)
set(oneValueArgs REASON)
cmake_parse_arguments(PARSE_ARGV 1 ARG "${options}" "${oneValueArgs}" "")
set(LEVEL FATAL_ERROR)
if(ARG_WARNING)
set(LEVEL WARNING)
endif()
if(NOT EXISTS "${file}" OR IS_DIRECTORY "${file}")
if(NOT ARG_REASON)
set(ARG_REASON "File '${file}' does not exist or is a directory")
endif()
_cth_assertion_failure(${LEVEL} "${ARG_REASON}")
endif()
endfunction()