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 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)
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")
166 list(REMOVE_ITEM float_list "HALF")
168 list(REMOVE_ITEM float_list "COMPLEX")
169 list(REMOVE_ITEM float_list "ZCOMPLEX")
174 foreach (float_type ${float_list})
175 foreach (source_file ${sources_in})
177 if (NOT no_float_type)
178 string(SUBSTRING ${float_type} 0 1 float_char)
179 string(TOLOWER ${float_char} float_char)
180 if (${float_type} STREQUAL "HALF")
181 set (float_char "sh")
186 get_filename_component(source_name ${source_file} NAME_WE)
187 set(obj_name "${float_char}${source_name}")
189 # replace * with float_char
190 if (${name_in} MATCHES "\\*")
191 string(REPLACE "*" ${float_char} obj_name ${name_in})
193 set(obj_name "${float_char}${name_in}")
197 if (replace_last_with)
198 string(REGEX REPLACE ".$" ${replace_last_with} obj_name ${obj_name})
200 set(obj_name "${obj_name}${append_with}")
203 # now add the object and set the defines
204 set(obj_defines ${defines_in})
207 set(obj_name "cblas_${obj_name}")
208 list(APPEND obj_defines "CBLAS")
209 elseif (NOT "${obj_name}" MATCHES "${ARCH_SUFFIX}")
210 set(obj_name "${obj_name}${ARCH_SUFFIX}")
213 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}\"")
214 if (${float_type} STREQUAL "DOUBLE" OR ${float_type} STREQUAL "ZCOMPLEX")
215 list(APPEND obj_defines "DOUBLE")
217 if (${float_type} STREQUAL "HALF")
218 list(APPEND obj_defines "HALF")
220 if (${float_type} STREQUAL "COMPLEX" OR ${float_type} STREQUAL "ZCOMPLEX")
221 list(APPEND obj_defines "COMPLEX")
222 if (mangle_complex_sources)
223 # add a z to the filename
224 get_filename_component(source_name ${source_file} NAME)
225 get_filename_component(source_dir ${source_file} DIRECTORY)
226 string(REPLACE ${source_name} "z${source_name}" source_file ${source_file})
231 message(STATUS "${obj_name}:${source_file}")
232 message(STATUS "${obj_defines}")
235 # create a copy of the source to avoid duplicate obj filename problem with ar.exe
236 get_filename_component(source_extension ${source_file} EXT)
237 set(new_source_file "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${obj_name}${source_extension}")
238 if (IS_ABSOLUTE ${source_file})
239 set(old_source_file ${source_file})
241 set(old_source_file "${CMAKE_CURRENT_LIST_DIR}/${source_file}")
244 string(REPLACE ";" "\n#define " define_source "${obj_defines}")
245 string(REPLACE "=" " " define_source "${define_source}")
246 file(WRITE ${new_source_file}.tmp "#define ${define_source}\n#include \"${old_source_file}\"")
247 configure_file(${new_source_file}.tmp ${new_source_file} COPYONLY)
248 file(REMOVE ${new_source_file}.tmp)
249 list(APPEND SRC_LIST_OUT ${new_source_file})
254 list(APPEND OPENBLAS_SRC ${SRC_LIST_OUT})
255 set(OPENBLAS_SRC ${OPENBLAS_SRC} PARENT_SCOPE)
258 # generates object files for each of the sources for each of the combinations of the preprocessor definitions passed in
259 # @param sources_in the source files to build from
260 # @param defines_in the preprocessor definitions that will be combined to create the object files
261 # @param all_defines_in (optional) preprocessor definitions that will be applied to all objects
262 # @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.
263 # If 0, it will simply append the code, e.g. symm_L.c with TRANS and UNIT will be symm_LTU.
264 # If 2, it will append the code with an underscore, e.g. symm.c with TRANS and UNIT will be symm_TU.
265 # 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).
266 # If 4, it will insert the code before the last underscore. E.g. trtri_U_parallel with TRANS will be trtri_UT_parallel
267 # @param alternate_name replaces the source name as the object name (define codes are still appended)
268 # @param no_float_type turns off the float type define for this build (e.g. SINGLE/DOUBLE/etc)
269 # @param complex_filename_scheme see GenerateNamedObjects
270 function(GenerateCombinationObjects sources_in defines_in absent_codes_in all_defines_in replace_scheme)
272 set(alternate_name_in "")
274 set(alternate_name_in ${ARGV5})
277 set(no_float_type false)
279 set(no_float_type ${ARGV6})
282 set(complex_filename_scheme "")
284 set(complex_filename_scheme ${ARGV7})
287 AllCombinations("${defines_in}" "${absent_codes_in}")
288 set(define_combos ${LIST_OUT})
289 set(define_codes ${CODES_OUT})
291 list(LENGTH define_combos num_combos)
292 math(EXPR num_combos "${num_combos} - 1")
294 foreach (c RANGE 0 ${num_combos})
296 list(GET define_combos ${c} define_combo)
297 list(GET define_codes ${c} define_code)
299 foreach (source_file ${sources_in})
301 set(alternate_name ${alternate_name_in})
303 # replace colon separated list with semicolons, this turns it into a CMake list that we can use foreach with
304 string(REPLACE ":" ";" define_combo ${define_combo})
306 # now add the object and set the defines
307 set(cur_defines ${define_combo})
308 if ("${cur_defines}" STREQUAL " ")
309 set(cur_defines ${all_defines_in})
311 list(APPEND cur_defines ${all_defines_in})
316 if (replace_scheme EQUAL 1)
317 set(replace_code ${define_code})
319 if (replace_scheme EQUAL 2)
320 set(append_code "_${define_code}")
321 elseif (replace_scheme EQUAL 3)
322 if ("${alternate_name}" STREQUAL "")
323 string(REGEX MATCH "[a-zA-Z]\\." last_letter ${source_file})
325 string(REGEX MATCH "[a-zA-Z]$" last_letter ${alternate_name})
327 # first extract the last letter
328 string(SUBSTRING ${last_letter} 0 1 last_letter) # remove period from match
329 # break the code up into the first letter and the remaining (should only be 2 anyway)
330 string(SUBSTRING ${define_code} 0 1 define_code_first)
331 string(SUBSTRING ${define_code} 1 -1 define_code_second)
332 set(replace_code "${define_code_first}${last_letter}${define_code_second}")
333 elseif (replace_scheme EQUAL 4)
334 # insert code before the last underscore and pass that in as the alternate_name
335 if ("${alternate_name}" STREQUAL "")
336 get_filename_component(alternate_name ${source_file} NAME_WE)
338 set(extra_underscore "")
339 # check if filename has two underscores, insert another if not (e.g. getrs_parallel needs to become getrs_U_parallel not getrsU_parallel)
340 string(REGEX MATCH "_[a-zA-Z]+_" underscores ${alternate_name})
341 string(LENGTH "${underscores}" underscores)
342 if (underscores EQUAL 0)
343 set(extra_underscore "_")
345 string(REGEX REPLACE "(.+)(_[^_]+)$" "\\1${extra_underscore}${define_code}\\2" alternate_name ${alternate_name})
347 set(append_code ${define_code}) # replace_scheme should be 0
351 GenerateNamedObjects("${source_file}" "${cur_defines}" "${alternate_name}" false "${replace_code}" "${append_code}" "${no_float_type}" "${complex_filename_scheme}")
355 set(OPENBLAS_SRC ${OPENBLAS_SRC} PARENT_SCOPE)