fd93f8a7028ba317d2b881257095c453a95d572c
[platform/upstream/openblas.git] / cmake / utils.cmake
1 # Functions to help with the OpenBLAS build
2
3 # Reads string from getarch into CMake vars. Format of getarch vars is VARNAME=VALUE
4 function(ParseGetArchVars GETARCH_IN)
5   string(REGEX MATCHALL "[0-9_a-zA-Z]+=[0-9_a-zA-Z]+" GETARCH_RESULT_LIST "${GETARCH_IN}")
6   foreach (GETARCH_LINE ${GETARCH_RESULT_LIST})
7     # split the line into var and value, then assign the value to a CMake var
8     string(REGEX MATCHALL "[0-9_a-zA-Z]+" SPLIT_VAR "${GETARCH_LINE}")
9     list(GET SPLIT_VAR 0 VAR_NAME)
10     list(GET SPLIT_VAR 1 VAR_VALUE)
11     set(${VAR_NAME} ${VAR_VALUE} PARENT_SCOPE)
12   endforeach ()
13 endfunction ()
14
15 # Reads a Makefile into CMake vars.
16 macro(ParseMakefileVars MAKEFILE_IN)
17   message(STATUS "Reading vars from ${MAKEFILE_IN}...")
18   file(STRINGS ${MAKEFILE_IN} makefile_contents)
19   foreach (makefile_line ${makefile_contents})
20     string(REGEX MATCH "([0-9_a-zA-Z]+)[ \t]*=[ \t]*(.+)$" line_match "${makefile_line}")
21     if (NOT "${line_match}" STREQUAL "")
22       set(var_name ${CMAKE_MATCH_1})
23       set(var_value ${CMAKE_MATCH_2})
24       # check for Makefile variables in the string, e.g. $(TSUFFIX)
25       string(REGEX MATCHALL "\\$\\(([0-9_a-zA-Z]+)\\)" make_var_matches ${var_value})
26       foreach (make_var ${make_var_matches})
27         # strip out Makefile $() markup
28         string(REGEX REPLACE "\\$\\(([0-9_a-zA-Z]+)\\)" "\\1" make_var ${make_var})
29         # now replace the instance of the Makefile variable with the value of the CMake variable (note the double quote)
30         string(REPLACE "$(${make_var})" "${${make_var}}" var_value ${var_value})
31       endforeach ()
32       set(${var_name} ${var_value})
33     else ()
34       string(REGEX MATCH "include \\$\\(KERNELDIR\\)/(.+)$" line_match "${makefile_line}")
35       if (NOT "${line_match}" STREQUAL "")
36         ParseMakefileVars(${KERNELDIR}/${CMAKE_MATCH_1})
37       endif ()
38     endif ()
39   endforeach ()
40 endmacro ()
41
42 # Returns all combinations of the input list, as a list with colon-separated combinations
43 # E.g. input of A B C returns A B C A:B A:C B:C
44 # N.B. The input is meant to be a list, and to past a list to a function in CMake you must quote it (e.g. AllCombinations("${LIST_VAR}")).
45 # #param absent_codes codes to use when an element is absent from a combination. For example, if you have TRANS;UNIT;UPPER you may want the code to be NNL when nothing is present.
46 # @returns LIST_OUT a list of combinations
47 #          CODES_OUT a list of codes corresponding to each combination, with N meaning the item is not present, and the first letter of the list item meaning it is presen
48 function(AllCombinations list_in absent_codes_in)
49   list(LENGTH list_in list_count)
50   set(num_combos 1)
51   # subtract 1 since we will iterate from 0 to num_combos
52   math(EXPR num_combos "(${num_combos} << ${list_count}) - 1")
53   set(LIST_OUT "")
54   set(CODES_OUT "")
55   foreach (c RANGE 0 ${num_combos})
56
57     set(current_combo "")
58     set(current_code "")
59
60     # this is a little ridiculous just to iterate through a list w/ indices
61     math(EXPR last_list_index "${list_count} - 1")
62     foreach (list_index RANGE 0 ${last_list_index})
63       math(EXPR bit "1 << ${list_index}")
64       math(EXPR combo_has_bit "${c} & ${bit}")
65       list(GET list_in ${list_index} list_elem)
66       if (combo_has_bit)
67         if (current_combo)
68           set(current_combo "${current_combo}:${list_elem}")
69         else ()
70           set(current_combo ${list_elem})
71         endif ()
72         string(SUBSTRING ${list_elem} 0 1 code_char)
73       else ()
74         list(GET absent_codes_in ${list_index} code_char)
75       endif ()
76       set(current_code "${current_code}${code_char}")
77     endforeach ()
78
79     if (current_combo STREQUAL "")
80       list(APPEND LIST_OUT " ") # Empty set is a valid combination, but CMake isn't appending the empty string for some reason, use a space
81     else ()
82       list(APPEND LIST_OUT ${current_combo})
83     endif ()
84     list(APPEND CODES_OUT ${current_code})
85
86   endforeach ()
87
88   set(LIST_OUT ${LIST_OUT} PARENT_SCOPE)
89   set(CODES_OUT ${CODES_OUT} PARENT_SCOPE)
90 endfunction ()
91
92 # generates object files for each of the sources, using the BLAS naming scheme to pass the function name as a preprocessor definition
93 # @param sources_in the source files to build from
94 # @param defines_in (optional) preprocessor definitions that will be applied to all objects
95 # @param name_in (optional) if this is set this name will be used instead of the filename. Use a * to indicate where the float character should go, if no star the character will be prepended.
96 #                           e.g. with DOUBLE set, "i*max" will generate the name "idmax", and "max" will be "dmax"
97 # @param replace_last_with replaces the last character in the filename with this string (e.g. symm_k should be symm_TU)
98 # @param append_with appends the filename with this string (e.g. trmm_R should be trmm_RTUU or some other combination of characters)
99 # @param no_float_type turns off the float type define for this build (e.g. SINGLE/DOUBLE/etc)
100 # @param complex_filename_scheme some routines have separate source files for complex and non-complex float types.
101 #                               0 - compiles for all types
102 #                               1 - compiles the sources for non-complex types only (SINGLE/DOUBLE)
103 #                               2 - compiles for complex types only (COMPLEX/DOUBLE COMPLEX)
104 #                               3 - compiles for all types, but changes source names for complex by prepending z (e.g. axpy.c becomes zaxpy.c)
105 #                               4 - compiles for complex types only, but changes source names for complex by prepending z (e.g. hemv.c becomes zhemv.c)
106 #                               STRING - compiles only the given type (e.g. DOUBLE)
107 function(GenerateNamedObjects sources_in)
108
109   if (DEFINED ARGV1)
110     set(defines_in ${ARGV1})
111   endif ()
112
113   if (DEFINED ARGV2 AND NOT "${ARGV2}" STREQUAL "")
114     set(name_in ${ARGV2})
115     # strip off extension for kernel files that pass in the object name.
116     get_filename_component(name_in ${name_in} NAME_WE)
117   endif ()
118
119   if (DEFINED ARGV3)
120     set(use_cblas ${ARGV3})
121   else ()
122     set(use_cblas false)
123   endif ()
124
125   if (DEFINED ARGV4)
126     set(replace_last_with ${ARGV4})
127   endif ()
128
129   if (DEFINED ARGV5)
130     set(append_with ${ARGV5})
131   endif ()
132
133   if (DEFINED ARGV6)
134     set(no_float_type ${ARGV6})
135   else ()
136     set(no_float_type false)
137   endif ()
138
139   if (no_float_type)
140     set(float_list "DUMMY") # still need to loop once
141   else ()
142     set(float_list "${FLOAT_TYPES}")
143   endif ()
144
145   set(real_only false)
146   set(complex_only false)
147   set(mangle_complex_sources false)
148   if (DEFINED ARGV7 AND NOT "${ARGV7}" STREQUAL "")
149     if (${ARGV7} EQUAL 1)
150       set(real_only true)
151     elseif (${ARGV7} EQUAL 2)
152       set(complex_only true)
153     elseif (${ARGV7} EQUAL 3)
154       set(mangle_complex_sources true)
155     elseif (${ARGV7} EQUAL 4)
156       set(mangle_complex_sources true)
157       set(complex_only true)
158     elseif (NOT ${ARGV7} EQUAL 0)
159       set(float_list ${ARGV7})
160     endif ()
161   endif ()
162
163   if (complex_only)
164     list(REMOVE_ITEM float_list "SINGLE")
165     list(REMOVE_ITEM float_list "DOUBLE")
166   elseif (real_only)
167     list(REMOVE_ITEM float_list "COMPLEX")
168     list(REMOVE_ITEM float_list "ZCOMPLEX")
169   endif ()
170
171   set(float_char "")
172   set(OBJ_LIST_OUT "")
173   foreach (float_type ${float_list})
174     foreach (source_file ${sources_in})
175
176       if (NOT no_float_type)
177         string(SUBSTRING ${float_type} 0 1 float_char)
178         string(TOLOWER ${float_char} float_char)
179       endif ()
180
181       if (NOT name_in)
182         get_filename_component(source_name ${source_file} NAME_WE)
183         set(obj_name "${float_char}${source_name}")
184       else ()
185         # replace * with float_char
186         if (${name_in} MATCHES "\\*")
187           string(REPLACE "*" ${float_char} obj_name ${name_in})
188         else ()
189           set(obj_name "${float_char}${name_in}")
190         endif ()
191       endif ()
192
193       if (replace_last_with)
194         string(REGEX REPLACE ".$" ${replace_last_with} obj_name ${obj_name})
195       else ()
196         set(obj_name "${obj_name}${append_with}")
197       endif ()
198
199       # now add the object and set the defines
200       set(obj_defines ${defines_in})
201
202       if (use_cblas)
203         set(obj_name "cblas_${obj_name}")
204         list(APPEND obj_defines "CBLAS")
205       elseif (NOT "${obj_name}" MATCHES "${ARCH_SUFFIX}")
206         set(obj_name "${obj_name}${ARCH_SUFFIX}")
207       endif ()
208
209       list(APPEND obj_defines "ASMNAME=${FU}${obj_name};ASMFNAME=${FU}${obj_name}${BU};NAME=${obj_name}${BU};CNAME=${obj_name};CHAR_NAME=\"${obj_name}${BU}\";CHAR_CNAME=\"${obj_name}\"")
210       if (${float_type} STREQUAL "DOUBLE" OR ${float_type} STREQUAL "ZCOMPLEX")
211         list(APPEND obj_defines "DOUBLE")
212       endif ()
213       if (${float_type} STREQUAL "COMPLEX" OR ${float_type} STREQUAL "ZCOMPLEX")
214         list(APPEND obj_defines "COMPLEX")
215         if (mangle_complex_sources)
216           # add a z to the filename
217           get_filename_component(source_name ${source_file} NAME)
218           get_filename_component(source_dir ${source_file} DIRECTORY)
219           string(REPLACE ${source_name} "z${source_name}" source_file ${source_file})
220         endif ()
221       endif ()
222
223       if (VERBOSE_GEN)
224         message(STATUS "${obj_name}:${source_file}")
225         message(STATUS "${obj_defines}")
226       endif ()
227
228       # create a copy of the source to avoid duplicate obj filename problem with ar.exe
229       get_filename_component(source_extension ${source_file} EXT)
230       set(new_source_file "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${obj_name}${source_extension}")
231       if (IS_ABSOLUTE ${source_file})
232         set(old_source_file ${source_file})
233       else ()
234         set(old_source_file "${CMAKE_CURRENT_LIST_DIR}/${source_file}")
235       endif ()
236
237       string(REPLACE ";" "\n#define " define_source "${obj_defines}")
238       string(REPLACE "=" " " define_source "${define_source}")
239       file(WRITE ${new_source_file}.tmp "#define ${define_source}\n#include \"${old_source_file}\"")
240       configure_file(${new_source_file}.tmp ${new_source_file} COPYONLY)
241       file(REMOVE ${new_source_file}.tmp)
242       list(APPEND SRC_LIST_OUT ${new_source_file})
243
244     endforeach ()
245   endforeach ()
246
247   list(APPEND OPENBLAS_SRC ${SRC_LIST_OUT})
248   set(OPENBLAS_SRC ${OPENBLAS_SRC} PARENT_SCOPE)
249 endfunction ()
250
251 # generates object files for each of the sources for each of the combinations of the preprocessor definitions passed in
252 # @param sources_in the source files to build from
253 # @param defines_in the preprocessor definitions that will be combined to create the object files
254 # @param all_defines_in (optional) preprocessor definitions that will be applied to all objects
255 # @param replace_scheme If 1, replace the "k" in the filename with the define combo letters. E.g. symm_k.c with TRANS and UNIT defined will be symm_TU.
256 #                  If 0, it will simply append the code, e.g. symm_L.c with TRANS and UNIT will be symm_LTU.
257 #                  If 2, it will append the code with an underscore, e.g. symm.c with TRANS and UNIT will be symm_TU.
258 #                  If 3, it will insert the code *around* the last character with an underscore, e.g. symm_L.c with TRANS and UNIT will be symm_TLU (required by BLAS level2 objects).
259 #                  If 4, it will insert the code before the last underscore. E.g. trtri_U_parallel with TRANS will be trtri_UT_parallel
260 # @param alternate_name replaces the source name as the object name (define codes are still appended)
261 # @param no_float_type turns off the float type define for this build (e.g. SINGLE/DOUBLE/etc)
262 # @param complex_filename_scheme see GenerateNamedObjects
263 function(GenerateCombinationObjects sources_in defines_in absent_codes_in all_defines_in replace_scheme)
264
265   set(alternate_name_in "")
266   if (DEFINED ARGV5)
267     set(alternate_name_in ${ARGV5})
268   endif ()
269
270   set(no_float_type false)
271   if (DEFINED ARGV6)
272     set(no_float_type ${ARGV6})
273   endif ()
274
275   set(complex_filename_scheme "")
276   if (DEFINED ARGV7)
277     set(complex_filename_scheme ${ARGV7})
278   endif ()
279
280   AllCombinations("${defines_in}" "${absent_codes_in}")
281   set(define_combos ${LIST_OUT})
282   set(define_codes ${CODES_OUT})
283
284   list(LENGTH define_combos num_combos)
285   math(EXPR num_combos "${num_combos} - 1")
286
287   foreach (c RANGE 0 ${num_combos})
288
289     list(GET define_combos ${c} define_combo)
290     list(GET define_codes ${c} define_code)
291
292     foreach (source_file ${sources_in})
293
294       set(alternate_name ${alternate_name_in})
295
296       # replace colon separated list with semicolons, this turns it into a CMake list that we can use foreach with
297       string(REPLACE ":" ";" define_combo ${define_combo})
298
299       # now add the object and set the defines
300       set(cur_defines ${define_combo})
301       if ("${cur_defines}" STREQUAL " ")
302         set(cur_defines ${all_defines_in})
303       else ()
304         list(APPEND cur_defines ${all_defines_in})
305       endif ()
306
307       set(replace_code "")
308       set(append_code "")
309       if (replace_scheme EQUAL 1)
310         set(replace_code ${define_code})
311       else ()
312         if (replace_scheme EQUAL 2)
313           set(append_code "_${define_code}")
314         elseif (replace_scheme EQUAL 3)
315           if ("${alternate_name}" STREQUAL "")
316             string(REGEX MATCH "[a-zA-Z]\\." last_letter ${source_file})
317           else ()
318             string(REGEX MATCH "[a-zA-Z]$" last_letter ${alternate_name})
319           endif ()
320           # first extract the last letter
321           string(SUBSTRING ${last_letter} 0 1 last_letter) # remove period from match
322           # break the code up into the first letter and the remaining (should only be 2 anyway)
323           string(SUBSTRING ${define_code} 0 1 define_code_first)
324           string(SUBSTRING ${define_code} 1 -1 define_code_second)
325           set(replace_code "${define_code_first}${last_letter}${define_code_second}")
326         elseif (replace_scheme EQUAL 4)
327           # insert code before the last underscore and pass that in as the alternate_name
328           if ("${alternate_name}" STREQUAL "")
329             get_filename_component(alternate_name ${source_file} NAME_WE)
330           endif ()
331           set(extra_underscore "")
332           # check if filename has two underscores, insert another if not (e.g. getrs_parallel needs to become getrs_U_parallel not getrsU_parallel)
333           string(REGEX MATCH "_[a-zA-Z]+_" underscores ${alternate_name})
334           string(LENGTH "${underscores}" underscores)
335           if (underscores EQUAL 0)
336             set(extra_underscore "_")
337           endif ()
338           string(REGEX REPLACE "(.+)(_[^_]+)$" "\\1${extra_underscore}${define_code}\\2" alternate_name ${alternate_name})
339         else()
340           set(append_code ${define_code}) # replace_scheme should be 0
341         endif ()
342       endif ()
343
344       GenerateNamedObjects("${source_file}" "${cur_defines}" "${alternate_name}" false "${replace_code}" "${append_code}" "${no_float_type}" "${complex_filename_scheme}")
345     endforeach ()
346   endforeach ()
347
348   set(OPENBLAS_SRC ${OPENBLAS_SRC} PARENT_SCOPE)
349 endfunction ()
350