-
Notifications
You must be signed in to change notification settings - Fork 0
/
pm_boolean.cmake
244 lines (198 loc) · 6.9 KB
/
pm_boolean.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
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
include_guard(GLOBAL)
include(pm__core)
# pm_bool(NOT [<out>] <var>)
# pm_bool(AND [<out>] <var1> <var2>)
# pm_bool(OR [<out>] <var1> <var2>)
# pm_bool(XOR [<out>] <var1> <var2>)
# pm_bool(ANY <out> <var1> [<var2> ...])
# pm_bool(ALL <out> <var1> [<var2> ...])
# pm_bool(ONE <out> <var1> [<var2> ...])
# pm_bool(NONE <out> <var1> [<var2> ...])
function(pm_bool CMD)
# pm_bool(NOT [<out>] <var>)
# ----------------------------------------------------------------
if(CMD STREQUAL NOT)
if(${ARGC} EQUAL 2)
set(OUTn ${ARGV1})
set(VARn ${ARGV1})
elseif(${ARGC} EQUAL 3)
set(OUTn ${ARGV1})
set(VARn ${ARGV2})
else()
pm_exit(
"pm_bool(NOT [<out>] <var>) "
"Called with the wrong number of arguments"
)
endif()
if(${VARn})
set(${OUTn} FALSE PARENT_SCOPE)
else()
set(${OUTn} TRUE PARENT_SCOPE)
endif()
# pm_bool(AND <out> <var1> <var2> [<var3> ...])
# ----------------------------------------------------------------
elseif(CMD STREQUAL AND)
if(NOT ${ARGC} GREATER_EQUAL 3)
pm_exit(
"pm_bool(AND <out> <var1> <var2> [<var3> ...]) "
"Called with the wrong number of arguments"
)
endif()
list(GET ARGN 0 OUTn)
list(REMOVE_AT ARGN 0)
# Default the out var to true
set(${OUTn} TRUE PARENT_SCOPE)
# Test all provided Vars. Short-circut if any evaluate to FALSE.
foreach(V IN LISTS ARGN)
if(NOT ${V})
set(${OUTn} FALSE PARENT_SCOPE)
return()
endif()
endforeach()
# pm_bool(OR <out> <var1> <var2> [<var3> ...])
# ----------------------------------------------------------------
elseif(CMD STREQUAL OR)
if(NOT ${ARGC} GREATER_EQUAL 4)
pm_exit(
"pm_bool(OR <out> <var1> <var2> [<var3> ...]) "
"Called with the wrong number of arguments"
)
endif()
list(GET ARGN 0 OUTn)
list(REMOVE_AT ARGN 0)
# Default the out var to false
set(${OUTn} FALSE PARENT_SCOPE)
# Test all provided Vars. Short-circut if any evaluate to TRUE.
foreach(V IN LISTS ARGN)
if(${V})
set(${OUTn} TRUE PARENT_SCOPE)
return()
endif()
endforeach()
# pm_bool(XOR <out> <var1> <var2> [<var3> ...])
# ----------------------------------------------------------------
elseif(CMD STREQUAL XOR)
if(NOT ${ARGC} GREATER_EQUAL 4)
pm_exit(
"pm_bool(XOR <out> <var1> <var2> [<var3> ...]) "
"Called with the wrong number of arguments"
)
endif()
list(GET ARGN 0 OUTn)
list(GET ARGN 1 VAR1)
list(REMOVE_AT ARGN 0 1)
# XOR cannot short circuit, so we prime the out var to VAR1...
if(${VAR1})
set(RET TRUE)
else()
set(RET FALSE)
endif()
# ... then loop through all remaning vars, performing a
# left-associative XOR fold
foreach(V IN LISTS ARGN)
if((NOT (${RET} AND ${V})) AND (${RET} OR ${V}))
set(RET TRUE)
else()
set(RET FALSE)
endif()
endforeach()
set(${OUTn} ${RET} PARENT_SCOPE)
# pm_bool(ANY <out> <var>...)
# ----------------------------------------------------------------
elseif(CMD STREQUAL ANY)
if(NOT ${ARGC} GREATER_EQUAL 2)
pm_exit(
"pm_bool(ANY <out> <var>...) "
"Called with too few arguments"
)
endif()
list(GET ARGN 0 OUTn)
list(REMOVE_AT ARGN 0)
# NB. We don't need to special-case if we were given an empty list.
# Prime the out var; if we don't short circuit, this is what we'll get.
set(${OUTn} FALSE PARENT_SCOPE)
foreach(V IN LISTS ARGN)
if(${V})
set(${OUTn} TRUE PARENT_SCOPE)
return()
endif()
endforeach()
# pm_bool(ALL <out> <var>...)
# ----------------------------------------------------------------
elseif(CMD STREQUAL ALL)
if(NOT ${ARGC} GREATER_EQUAL 2)
pm_exit(
"pm_bool(ALL <out> <var>...) "
"Called with too few arguments"
)
endif()
list(GET ARGN 0 OUTn)
list(REMOVE_AT ARGN 0)
# If we were given an empty list, special-case to FALSE
if(${ARGC} EQUAL 2)
set(${OUTn} FALSE PARENT_SCOPE)
return()
endif()
# Prime the out var; if we don't short circuit, this is what we'll get.
set(${OUTn} TRUE PARENT_SCOPE)
foreach(V IN LISTS ARGN)
if(NOT ${V})
set(${OUTn} FALSE PARENT_SCOPE)
return()
endif()
endforeach()
# pm_bool(ONE <out> <var>...)
# ----------------------------------------------------------------
elseif(CMD STREQUAL ONE)
if(NOT ${ARGC} GREATER_EQUAL 2)
pm_exit(
"pm_bool(ONE <out> <var>) "
"Called with too few arguments"
)
endif()
list(GET ARGN 0 OUTn)
list(REMOVE_AT ARGN 0)
# NB. We don't need to special-case if we were given an empty list.
# Prime the out var
set(RET FALSE)
foreach(V IN LISTS ARGN)
if(${V})
if(NOT ${RET})
# If this is the first truthy val, set RET to TRUE
set(RET TRUE)
else()
# Two or more of this set are TRUE; early out
set(${OUTn} FALSE PARENT_SCOPE)
return()
endif()
endif()
endforeach()
set(${OUTn} ${RET} PARENT_SCOPE)
# pm_bool(NONE <out> <var>...)
# ----------------------------------------------------------------
elseif(CMD STREQUAL NONE)
if(NOT ${ARGC} GREATER_EQUAL 2)
pm_exit(
"pm_bool(NONE <out> <var>...) "
"Called with too few arguments"
)
endif()
list(GET ARGN 0 OUTn)
list(REMOVE_AT ARGN 0)
# If we were given an empty list, special-case to TRUE
if(${ARGC} EQUAL 2)
set(${OUTn} TRUE PARENT_SCOPE)
return()
endif()
# Prime the out var; if we don't short circuit, this is what we'll get.
set(${OUTn} TRUE PARENT_SCOPE)
foreach(V IN LISTS ARGN)
if(${V})
set(${OUTn} FALSE PARENT_SCOPE)
return()
endif()
endforeach()
else()
pm_exit("pm_boolean: given unknown commad; ${CMD}")
endif()
endfunction()