Debian Codegraph @

cmake/tribits/core/package_arch/TribitsETISupport.cmake

raw
1 # @HEADER
2 # *****************************************************************************
3 # TriBITS: Tribal Build, Integrate, and Test System
4 #
5 # Copyright 2013-2016 NTESS and the TriBITS contributors.
6 # SPDX-License-Identifier: BSD-3-Clause
7 # *****************************************************************************
8 # @HEADER
9
10 include(AppendSet)
11 include(AppendGlobalSet)
12 include(Split)
13 include(Join)
14 include(PrintVar)
15
16 # this macro expands produces a list of all combinations of a variable number of lists:
17 # called like tribits_eti_type_expansion(setvar "T=t1|t2|t3" "S=s1|s2" "V=v1")
18 # returns setvar="T=T1 S=s1 V=v1;T=T1 S=s2 V=v1;
19 # T=T2 S=s1 V=v1;T=T2 S=s2 V=v1;
20 # T=T3 S=s1 V=v1;T=T3 S=s2 V=v1"
21 function(tribits_eti_type_expansion outvar first_list)
22 # extract the field name and the list of types
23 if (NOT "${first_list}" MATCHES "([^=]*)=(.*)")
24 set(${outvar} "TRIBITS_ETI_BAD_ARGUMENTS" PARENT_SCOPE)
25 return()
26 else()
27 set(label "${CMAKE_MATCH_1}")
28 set(tlist "${CMAKE_MATCH_2}")
29 endif()
30 set(eti_result "")
31 split("${tlist}" "\\|" tlist)
32 if ("${${outvar}}" STREQUAL "")
33 set(accumulate OFF)
34 else()
35 set(accumulate ON)
36 endif()
37 list(LENGTH tlist tlist_len)
38 if (${ARGC} GREATER 2)
39 tribits_eti_type_expansion(sub ${ARGN})
40 foreach(t ${tlist})
41 string(STRIP "${t}" t)
42 set(t "{${t}}")
43 foreach(s ${sub})
44 list(APPEND eti_result "${label}=${t} ${s}")
45 endforeach()
46 endforeach()
47 else()
48 foreach(t ${tlist})
49 string(STRIP "${t}" t)
50 set(t "{${t}}")
51 list(APPEND eti_result "${label}=${t}")
52 endforeach()
53 endif()
54 if (accumulate)
55 set(${outvar} "${${outvar}};${eti_result}" PARENT_SCOPE)
56 else()
57 set(${outvar} "${eti_result}" PARENT_SCOPE)
58 endif()
59 endfunction()
60
61 # Explode an ETI set into a variable number of named component fields
62 function(tribits_eti_explode fields inst outvar)
63 foreach (field ${fields})
64 if( "${inst}" MATCHES "( |^)${field}={([^{}$=]*)}( |$)")
65 list(APPEND eti_result "${CMAKE_MATCH_2}")
66 elseif("${inst}" MATCHES "( |^)${field}=([^{} $=]*)( |$)")
67 list(APPEND eti_result "${CMAKE_MATCH_2}")
68 elseif(NOT "${inst}" MATCHES "( |^)${field}=")
69 list(APPEND eti_result "TYPE-MISSING")
70 else()
71 set(eti_result "TRIBITS_ETI_BAD_PARSE")
72 break()
73 endif()
74 endforeach()
75 join(eti_result "|" FALSE ${eti_result})
76 set(${outvar} "${eti_result}" PARENT_SCOPE)
77 endfunction()
78
79 # effectively, a tupled regex, wrapped in a for loop
80 # given a list of processed excludes (a list of pipe-separated, bracket-delimited tuples of type/regexes),
81 # determine whether processed_inst (a pipe-separated, bracket tuple of types) is matched
82 # if the instantiation matches one of the exclusions, result is set true
83 function(tribits_eti_check_exclusion processed_excludes processed_inst excluded)
84 split("${processed_inst}" "\\|" processed_inst)
85 list(LENGTH processed_inst numfields)
86 math(EXPR NFm1 "${numfields}-1")
87 set(${excluded} OFF PARENT_SCOPE)
88 # check to see whether this is excluded or not
89 list(LENGTH processed_excludes numexcl)
90 foreach(excl ${processed_excludes})
91 split(${excl} "\\|" excl)
92 list(LENGTH excl excp_len)
93 if(${excp_len} EQUAL ${numfields})
94 set(lcltest ON)
95 foreach(i RANGE 0 ${NFm1})
96 list(GET processed_inst ${i} f)
97 list(GET excl ${i} e)
98 if (NOT "${f}" STREQUAL "TYPE-MISSING" AND NOT "${f}" MATCHES "${e}")
99 set(lcltest OFF)
100 break()
101 endif()
102 endforeach()
103 if(lcltest)
104 set(${excluded} ON PARENT_SCOPE)
105 if (${PROJECT}_VERBOSE_CONFIGURE)
106 message(STATUS "-- Instantiation excluded by ${excl}")
107 endif()
108 return()
109 endif()
110 endif()
111 endforeach()
112 endfunction()
113
114 # given a list of field names and list of macrofields, translate the
115 # field names in macrofields into indices corresponding to the position in
116 # the list of field names
117 function(tribits_eti_index_macro_fields etifields macrofields indexvar)
118 foreach(mf ${macrofields})
119 string(STRIP "${mf}" mf)
120 list(FIND etifields ${mf} ind)
121 if(${ind} EQUAL -1)
122 message(FATAL_ERROR "Macro variable ${mf} not found in list of fields: ${etifields}")
123 endif()
124 list(APPEND inds ${ind})
125 endforeach()
126 set(${indexvar} ${inds} PARENT_SCOPE)
127 endfunction()
128
129 # given a macro name and a list of tuples, generate a macro string
130 function(tribits_eti_build_macro_string macroname tuplelist outvar)
131 set(str "#define ${macroname}(INSTMACRO)")
132 foreach(tuple ${tuplelist})
133 string(REPLACE "|" " , " tuple "${tuple}")
134 set(str "${str}\\\n\tINSTMACRO( ${tuple} )")
135 endforeach()
136 set(str "${str}\n")
137 set(${outvar} ${str} PARENT_SCOPE)
138 endfunction()
139
140 # utility for adding eti support to another package's library set
141 function(tribits_add_eti_instantiations PACKAGE)
142 if(${PROJECT_NAME}_VERBOSE_CONFIGURE)
143 message(STATUS "Adding instantiations to ${PACKAGE} library set...")
144 endif()
145 append_global_set(${PACKAGE}_ETI_LIBRARYSET ${ARGN})
146 endfunction()
147
148 # utility for mangling type names to make them safe for the C preprocessor
149 function(tribits_eti_mangle_symbol
150 mangledvar
151 input)
152 set(newvar "${input}")
153 string(REPLACE "::" "_" newvar "${newvar}")
154 string(REPLACE "," "_" newvar "${newvar}")
155 set(num_brackets 0)
156 while("${newvar}" MATCHES "(.*)<([^<>]*)>(.*)")
157 set(newvar "${CMAKE_MATCH_1}${num_brackets}${CMAKE_MATCH_2}${num_brackets}${CMAKE_MATCH_3}")
158 math(EXPR num_brackets "${num_brackets}+1")
159 endwhile()
160 string(REPLACE " " "" newvar "${newvar}")
161 set(${mangledvar} "${newvar}" PARENT_SCOPE)
162 endfunction()
163
164 # utility for mangling type names to make them safe for the C preprocessor
165 # upon mangling, it automatically inserts an ifdef into the mangling macro
166 function(tribits_eti_mangle_symbol_augment_macro
167 typedefvar
168 symbolvar
169 manglistvar)
170 set(oldsymbol "${${symbolvar}}")
171 if(NOT "${oldsymbol}" MATCHES "::|[<>, ]")
172 return()
173 endif()
174 tribits_eti_mangle_symbol(newsymbol "${oldsymbol}")
175 set(${symbolvar} "${newsymbol}" PARENT_SCOPE)
176 list(FIND ${manglistvar} ${newsymbol} already_mangled)
177 if(${already_mangled} EQUAL -1)
178 list(APPEND ${manglistvar} ${newsymbol})
179 set(${manglistvar} ${${manglistvar}} PARENT_SCOPE)
180 list(APPEND ${typedefvar} "typedef ${oldsymbol} ${newsymbol}")
181 set(${typedefvar} ${${typedefvar}} PARENT_SCOPE)
182 endif()
183 endfunction()
184
185 # generate the macros
186 function(tribits_eti_generate_macros etifields etisetvar etiexcludelist manglinglistvar typedeflistvar)
187 set(manglinglist "${${manglinglistvar}}")
188 set(typedeflist "${${typedeflistvar}}")
189 split("${etifields}" "\\|" etifields)
190 if(${PROJECT}_VERBOSE_CONFIGURE)
191 message(STATUS "ETI fields: ${etifields}")
192 message(STATUS "ETI set: ${etisetvar}")
193 message(STATUS "ETI excludes: ${etiexcludelist}")
194 endif()
195 # we make lists of tuples first, because we want to make sure they don't have duplicates
196 # this algorithm is o(N^2) in the number of instantiations in etisetvar
197 math(EXPR num_macros "(${ARGC}-5)/2")
198 if(${num_macros} EQUAL 0)
199 return()
200 endif()
201 # process macro fields into a list of indices
202 foreach(m RANGE 1 ${num_macros})
203 math(EXPR m2 "(${m}-1)*2")
204 math(EXPR m2p1 "${m2}+1")
205 list(GET ARGN ${m2} macroarg)
206 list(GET ARGN ${m2p1} macrovar${m})
207 string(REGEX REPLACE "^(.*)\\(.*" "\\1" macroname${m} ${macroarg})
208 string(REGEX REPLACE "^.*\\((.*)\\)$" "\\1" macrofields${m} ${macroarg})
209 split("${macrofields${m}}" "," macrofields)
210 tribits_eti_index_macro_fields("${etifields}" "${macrofields}" macroindex${m})
211 if(${PROJECT}_VERBOSE_CONFIGURE)
212 message(STATUS "Parsed macro ${macroname${m}}(${macrofields${m}}) into variable ${macrovar${m}} (index list ${macroindex${m}})")
213 endif()
214 endforeach()
215 # process the exclusions once
216 foreach(excl ${etiexcludelist})
217 tribits_eti_explode("${etifields}" "${excl}" e_excl)
218 if("${e_excl}" STREQUAL "TRIBITS_ETI_BAD_PARSE")
219 message(FATAL_ERROR "TRIBITS_GENERATE_ETI_MACROS: exclusion did not parse: ${excl}")
220 endif()
221 list(APPEND processed_excludes "${e_excl}")
222 endforeach()
223 list(LENGTH etifields numfields)
224 math(EXPR NFm1 "${numfields}-1")
225 foreach(inst ${etisetvar})
226 if (${PROJECT}_VERBOSE_CONFIGURE)
227 message(STATUS "Processing instantiation: ${inst}") # comment
228 endif()
229 tribits_eti_explode("${etifields}" "${inst}" tmp)
230 if("${tmp}" STREQUAL "TRIBITS_ETI_BAD_PARSE")
231 message(FATAL_ERROR "TRIBITS_GENERATE_ETI_MACROS: instantiation did not parse: ${inst}")
232 else()
233 set(inst "${tmp}")
234 endif()
235 # check whether it is on the exclude list
236 tribits_eti_check_exclusion("${processed_excludes}" "${inst}" excluded)
237 if(NOT excluded)
238 split("${inst}" "\\|" inst)
239 # append tuple to list
240 foreach(m RANGE 1 ${num_macros})
241 set(tuple "")
242 foreach(ind ${macroindex${m}})
243 list(GET inst ${ind} t)
244 if("${t}" STREQUAL "TYPE-MISSING")
245 set(tuple "SKIP-TUPLE")
246 break()
247 endif()
248 # mangle the types in the instantiation
249 tribits_eti_mangle_symbol_augment_macro(typedeflist t manglinglist)
250 list(APPEND tuple ${t})
251 endforeach()
252 if(NOT "${tuple}" STREQUAL "SKIP-TUPLE")
253 join(tuple "|" FALSE "${tuple}")
254 list(APPEND macrotuples${m} ${tuple})
255 endif()
256 endforeach()
257 endif()
258 endforeach()
259 # remove duplicates from lists
260 foreach(m RANGE 1 ${num_macros})
261 if(DEFINED macrotuples${m})
262 list(REMOVE_DUPLICATES macrotuples${m})
263 endif()
264 endforeach()
265 # build the macro strings
266 foreach(m RANGE 1 ${num_macros})
267 tribits_eti_build_macro_string("${macroname${m}}" "${macrotuples${m}}" mac)
268 set(${macrovar${m}} "${mac}" PARENT_SCOPE)
269 endforeach()
270 # build the typedef string
271 set(${manglinglistvar} ${manglinglist} PARENT_SCOPE)
272 set(${typedeflistvar} ${typedeflist} PARENT_SCOPE)
273 endfunction()
274
275 # generate the typedef macro
276 function(tribits_eti_generate_typedef_macro outputvar macroname typedeflist)
277 set(mac "#define ${macroname}() ")
278 foreach(td ${typedeflist})
279 set(mac "${mac} \\\n\t${td};")
280 endforeach()
281 set(${outputvar} "${mac}" PARENT_SCOPE)
282 endfunction()