1 # Functions to help with the OpenBLAS build
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)
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})
32 set(${var_name} ${var_value})
34 string(REGEX MATCH "include \\$\\(KERNELDIR\\)/(.+)$" line_match "${makefile_line}")
35 if (NOT "${line_match}" STREQUAL "")
36 ParseMakefileVars(${KERNELDIR}/${CMAKE_MATCH_1})
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)
51 # subtract 1 since we will iterate from 0 to num_combos
52 math(EXPR num_combos "(${num_combos} << ${list_count}) - 1")
55 foreach (c RANGE 0 ${num_combos})
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)
68 set(current_combo "${current_combo}:${list_elem}")
70 set(current_combo ${list_elem})
72 string(SUBSTRING ${list_elem} 0 1 code_char)
74 list(GET absent_codes_in ${list_index} code_char)
76 set(current_code "${current_code}${code_char}")
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
82 list(APPEND LIST_OUT ${current_combo})
84 list(APPEND CODES_OUT ${current_code})
88 set(LIST_OUT ${LIST_OUT} PARENT_SCOPE)
89 set(CODES_OUT ${CODES_OUT} PARENT_SCOPE)
92 # generates object files for each of the sources, using the BLAS naming scheme to pass the funciton 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)
110 set(defines_in ${ARGV1})
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)
120 set(use_cblas ${ARGV3})
126 set(replace_last_with ${ARGV4})
130 set(append_with ${ARGV5})
134 set(no_float_type ${ARGV6})
136 set(no_float_type false)
140 set(float_list "DUMMY") # still need to loop once
142 set(float_list "${FLOAT_TYPES}")
146 set(complex_only false)
147 set(mangle_complex_sources false)
148 if (DEFINED ARGV7 AND NOT "${ARGV7}" STREQUAL "")
149 if (${ARGV7} EQUAL 1)
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})
164 list(REMOVE_ITEM float_list "SINGLE")
165 list(REMOVE_ITEM float_list "DOUBLE")
167 list(REMOVE_ITEM float_list "COMPLEX")
168 list(REMOVE_ITEM float_list "ZCOMPLEX")
173 foreach (float_type ${float_list})
174 foreach (source_file ${sources_in})
176 if (NOT no_float_type)
177 string(SUBSTRING ${float_type} 0 1 float_char)
178 string(TOLOWER ${float_char} float_char)
182 get_filename_component(source_name ${source_file} NAME_WE)
183 set(obj_name "${float_char}${source_name}")
185 # replace * with float_char
186 if (${name_in} MATCHES "\\*")
187 string(REPLACE "*" ${float_char} obj_name ${name_in})
189 set(obj_name "${float_char}${name_in}")
193 if (replace_last_with)
194 string(REGEX REPLACE ".$" ${replace_last_with} obj_name ${obj_name})
196 set(obj_name "${obj_name}${append_with}")
199 # now add the object and set the defines
200 set(obj_defines ${defines_in})
203 set(obj_name "cblas_${obj_name}")
204 list(APPEND obj_defines "CBLAS")
207 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}\"")
208 if (${float_type} STREQUAL "DOUBLE" OR ${float_type} STREQUAL "ZCOMPLEX")
209 list(APPEND obj_defines "DOUBLE")
211 if (${float_type} STREQUAL "COMPLEX" OR ${float_type} STREQUAL "ZCOMPLEX")
212 list(APPEND obj_defines "COMPLEX")
213 if (mangle_complex_sources)
214 # add a z to the filename
215 get_filename_component(source_name ${source_file} NAME)
216 get_filename_component(source_dir ${source_file} DIRECTORY)
217 string(REPLACE ${source_name} "z${source_name}" source_file ${source_file})
222 message(STATUS "${obj_name}:${source_file}")
223 message(STATUS "${obj_defines}")
226 # create a copy of the source to avoid duplicate obj filename problem with ar.exe
227 get_filename_component(source_extension ${source_file} EXT)
228 set(new_source_file "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${obj_name}${source_extension}")
229 if (IS_ABSOLUTE ${source_file})
230 set(old_source_file ${source_file})
232 set(old_source_file "${CMAKE_CURRENT_LIST_DIR}/${source_file}")
235 string(REPLACE ";" "\n#define " define_source "${obj_defines}")
236 string(REPLACE "=" " " define_source "${define_source}")
237 file(WRITE ${new_source_file} "#define ${define_source}\n#include \"${old_source_file}\"")
238 list(APPEND SRC_LIST_OUT ${new_source_file})
243 list(APPEND OPENBLAS_SRC ${SRC_LIST_OUT})
244 set(OPENBLAS_SRC ${OPENBLAS_SRC} PARENT_SCOPE)
247 # generates object files for each of the sources for each of the combinations of the preprocessor definitions passed in
248 # @param sources_in the source files to build from
249 # @param defines_in the preprocessor definitions that will be combined to create the object files
250 # @param all_defines_in (optional) preprocessor definitions that will be applied to all objects
251 # @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.
252 # If 0, it will simply append the code, e.g. symm_L.c with TRANS and UNIT will be symm_LTU.
253 # If 2, it will append the code with an underscore, e.g. symm.c with TRANS and UNIT will be symm_TU.
254 # 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).
255 # If 4, it will insert the code before the last underscore. E.g. trtri_U_parallel with TRANS will be trtri_UT_parallel
256 # @param alternate_name replaces the source name as the object name (define codes are still appended)
257 # @param no_float_type turns off the float type define for this build (e.g. SINGLE/DOUBLE/etc)
258 # @param complex_filename_scheme see GenerateNamedObjects
259 function(GenerateCombinationObjects sources_in defines_in absent_codes_in all_defines_in replace_scheme)
261 set(alternate_name_in "")
263 set(alternate_name_in ${ARGV5})
266 set(no_float_type false)
268 set(no_float_type ${ARGV6})
271 set(complex_filename_scheme "")
273 set(complex_filename_scheme ${ARGV7})
276 AllCombinations("${defines_in}" "${absent_codes_in}")
277 set(define_combos ${LIST_OUT})
278 set(define_codes ${CODES_OUT})
280 list(LENGTH define_combos num_combos)
281 math(EXPR num_combos "${num_combos} - 1")
283 foreach (c RANGE 0 ${num_combos})
285 list(GET define_combos ${c} define_combo)
286 list(GET define_codes ${c} define_code)
288 foreach (source_file ${sources_in})
290 set(alternate_name ${alternate_name_in})
292 # replace colon separated list with semicolons, this turns it into a CMake list that we can use foreach with
293 string(REPLACE ":" ";" define_combo ${define_combo})
295 # now add the object and set the defines
296 set(cur_defines ${define_combo})
297 if ("${cur_defines}" STREQUAL " ")
298 set(cur_defines ${all_defines_in})
300 list(APPEND cur_defines ${all_defines_in})
305 if (replace_scheme EQUAL 1)
306 set(replace_code ${define_code})
308 if (replace_scheme EQUAL 2)
309 set(append_code "_${define_code}")
310 elseif (replace_scheme EQUAL 3)
311 if ("${alternate_name}" STREQUAL "")
312 string(REGEX MATCH "[a-zA-Z]\\." last_letter ${source_file})
314 string(REGEX MATCH "[a-zA-Z]$" last_letter ${alternate_name})
316 # first extract the last letter
317 string(SUBSTRING ${last_letter} 0 1 last_letter) # remove period from match
318 # break the code up into the first letter and the remaining (should only be 2 anyway)
319 string(SUBSTRING ${define_code} 0 1 define_code_first)
320 string(SUBSTRING ${define_code} 1 -1 define_code_second)
321 set(replace_code "${define_code_first}${last_letter}${define_code_second}")
322 elseif (replace_scheme EQUAL 4)
323 # insert code before the last underscore and pass that in as the alternate_name
324 if ("${alternate_name}" STREQUAL "")
325 get_filename_component(alternate_name ${source_file} NAME_WE)
327 set(extra_underscore "")
328 # check if filename has two underscores, insert another if not (e.g. getrs_parallel needs to become getrs_U_parallel not getrsU_parallel)
329 string(REGEX MATCH "_[a-zA-Z]+_" underscores ${alternate_name})
330 string(LENGTH "${underscores}" underscores)
331 if (underscores EQUAL 0)
332 set(extra_underscore "_")
334 string(REGEX REPLACE "(.+)(_[^_]+)$" "\\1${extra_underscore}${define_code}\\2" alternate_name ${alternate_name})
336 set(append_code ${define_code}) # replace_scheme should be 0
340 GenerateNamedObjects("${source_file}" "${cur_defines}" "${alternate_name}" false "${replace_code}" "${append_code}" "${no_float_type}" "${complex_filename_scheme}")
344 set(OPENBLAS_SRC ${OPENBLAS_SRC} PARENT_SCOPE)