-
Notifications
You must be signed in to change notification settings - Fork 0
/
pm_math.cmake
171 lines (134 loc) · 4.6 KB
/
pm_math.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
include_guard(GLOBAL)
include(pm__core)
include(pm_parse_arguments)
function(pm_math CMD)
# pm_math(EXPR <out> <expression>)
# ----------------------------------------------------------------
if(CMD STREQUAL EXPR)
pm_parse_arguments(
positional_arguments OUTn MATH_EXPR
argn ${ARGN}
)
pm_disallow_special_names(${OUTn})
math(EXPR RET ${MATH_EXPR})
set(${OUTn} ${RET} PARENT_SCOPE)
# pm_math(MIN <out> <num1> [<num2> ...])
# ----------------------------------------------------------------
elseif(CMD STREQUAL MIN)
pm_parse_arguments(
positional_arguments OUTn
allow_unparsed_arguments
argn ${ARGN}
)
if(NOT UNPARSED_ARGUMENTS_COUNT GREATER_EQUAL 1)
pm_exit(
"pm_math(MIN <out> <num1> [<num2> ...]) "
"called with too few arguments"
)
endif()
list(GET UNPARSED_ARGUMENTS 0 RET)
list(REMOVE_AT UNPARSED_ARGUMENTS 0)
foreach(N IN LISTS UNPARSED_ARGUMENTS)
if(N LESS RET)
set(RET ${N})
endif()
endforeach()
set(${OUTn} ${RET} PARENT_SCOPE)
# pm_math(MAX <out> <num1> [<num2> ...])
# ----------------------------------------------------------------
elseif(CMD STREQUAL MAX)
pm_parse_arguments(
positional_arguments OUTn
allow_unparsed_arguments
argn ${ARGN}
)
if(NOT UNPARSED_ARGUMENTS_COUNT GREATER_EQUAL 1)
pm_exit(
"pm_math(MAX <out> <num1> [<num2> ...]) "
"called with too few arguments"
)
endif()
list(GET UNPARSED_ARGUMENTS 0 RET)
list(REMOVE_AT UNPARSED_ARGUMENTS 0)
foreach(N IN LISTS UNPARSED_ARGUMENTS)
if(N GREATER RET)
set(RET ${N})
endif()
endforeach()
set(${OUTn} ${RET} PARENT_SCOPE)
# pm_math(IS_EVEN <out> <num>)
# ----------------------------------------------------------------
elseif(CMD STREQUAL IS_EVEN)
pm_parse_arguments(
positional_arguments OUTn NUM
argn ${ARGN}
)
math(EXPR CHECK "(${NUM} / 2) * 2")
if(${NUM} EQUAL ${CHECK})
set(${OUTn} TRUE PARENT_SCOPE)
else()
set(${OUTn} FALSE PARENT_SCOPE)
endif()
# pm_math(IS_ODD <out> <num>)
# ----------------------------------------------------------------
elseif(CMD STREQUAL IS_ODD)
pm_parse_arguments(
positional_arguments OUTn NUM
argn ${ARGN}
)
math(EXPR CHECK "(${NUM} / 2) * 2")
if(${NUM} EQUAL ${CHECK})
set(${OUTn} FALSE PARENT_SCOPE)
else()
set(${OUTn} TRUE PARENT_SCOPE)
endif()
# pm_math(INCREMENT <var> [BY <num>] [OUTPUT <out>])
# ----------------------------------------------------------------
elseif(CMD STREQUAL INCREMENT)
pm_parse_arguments(
positional_arguments VARn
arguments BY OUTPUT
argn ${ARGN}
)
pm_set_if_unset(BY 1)
pm_set_if_unset(OUTPUT ${VARn})
math(EXPR RET "${${VARn}} + ${BY}")
set(${OUTPUT} ${RET} PARENT_SCOPE)
# pm_math(DECREMENT <var> [BY <num>] [OUTPUT <out>])
# ----------------------------------------------------------------
elseif(CMD STREQUAL DECREMENT)
pm_parse_arguments(
positional_arguments VARn
arguments BY OUTPUT
argn ${ARGN}
)
pm_set_if_unset(BY 1)
pm_set_if_unset(OUTPUT ${VARn})
math(EXPR RET "${${VARn}} - ${BY}")
set(${OUTPUT} ${RET} PARENT_SCOPE)
# pm_math(DOUBLE <var> [OUTPUT <out>])
# ----------------------------------------------------------------
elseif(CMD STREQUAL DOUBLE)
pm_parse_arguments(
positional_arguments VARn
arguments OUTPUT
argn ${ARGN}
)
pm_set_if_unset(OUTPUT ${VARn})
math(EXPR RET "${${VARn}} * 2")
set(${OUTPUT} ${RET} PARENT_SCOPE)
# pm_math(HALVE <var> [OUTPUT <out>])
# ----------------------------------------------------------------
elseif(CMD STREQUAL HALVE)
pm_parse_arguments(
positional_arguments VARn
arguments OUTPUT
argn ${ARGN}
)
pm_set_if_unset(OUTPUT ${VARn})
math(EXPR RET "${${VARn}} / 2")
set(${OUTPUT} ${RET} PARENT_SCOPE)
else()
pm_exit("pm_math: given unknown commad; ${CMD}")
endif()
endfunction()