You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.cmake 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. # Functions to help with the OpenBLAS build
  2. # Reads string from getarch into CMake vars. Format of getarch vars is VARNAME=VALUE
  3. function(ParseGetArchVars GETARCH_IN)
  4. string(REGEX MATCHALL "[0-9_a-zA-Z]+=[0-9_a-zA-Z]+" GETARCH_RESULT_LIST "${GETARCH_IN}")
  5. foreach (GETARCH_LINE ${GETARCH_RESULT_LIST})
  6. # split the line into var and value, then assign the value to a CMake var
  7. string(REGEX MATCHALL "[0-9_a-zA-Z]+" SPLIT_VAR "${GETARCH_LINE}")
  8. list(GET SPLIT_VAR 0 VAR_NAME)
  9. list(GET SPLIT_VAR 1 VAR_VALUE)
  10. set(${VAR_NAME} ${VAR_VALUE} PARENT_SCOPE)
  11. endforeach ()
  12. endfunction ()
  13. # Reads a Makefile into CMake vars.
  14. # TODO: respect IFDEF/IFNDEF?
  15. # TODO: regex replace makefile vars, e.g. $(TSUFFIX) is set to the target arch in the var CGEMMOTCOPYOBJ = cgemm_otcopy$(TSUFFIX).$(SUFFIX)
  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. set(${var_name} ${var_value})
  25. message(STATUS "found var ${var_name} = ${var_value}")
  26. else ()
  27. string(REGEX MATCH "include \\$\\(KERNELDIR\\)/(.+)$" line_match "${makefile_line}")
  28. if (NOT "${line_match}" STREQUAL "")
  29. ParseMakefileVars(${KERNELDIR}/${CMAKE_MATCH_1})
  30. else ()
  31. message(STATUS "couldn't parse ${makefile_line} into a var")
  32. endif ()
  33. endif ()
  34. endforeach ()
  35. endmacro ()
  36. # Returns all combinations of the input list, as a list with colon-separated combinations
  37. # E.g. input of A B C returns A B C A:B A:C B:C
  38. # 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}")).
  39. # #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.
  40. # @returns LIST_OUT a list of combinations
  41. # 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
  42. function(AllCombinations list_in absent_codes_in)
  43. list(LENGTH list_in list_count)
  44. set(num_combos 1)
  45. # subtract 1 since we will iterate from 0 to num_combos
  46. math(EXPR num_combos "(${num_combos} << ${list_count}) - 1")
  47. set(LIST_OUT "")
  48. set(CODES_OUT "")
  49. foreach (c RANGE 0 ${num_combos})
  50. set(current_combo "")
  51. set(current_code "")
  52. # this is a little ridiculous just to iterate through a list w/ indices
  53. math(EXPR last_list_index "${list_count} - 1")
  54. foreach (list_index RANGE 0 ${last_list_index})
  55. math(EXPR bit "1 << ${list_index}")
  56. math(EXPR combo_has_bit "${c} & ${bit}")
  57. list(GET list_in ${list_index} list_elem)
  58. if (combo_has_bit)
  59. if (current_combo)
  60. set(current_combo "${current_combo}:${list_elem}")
  61. else ()
  62. set(current_combo ${list_elem})
  63. endif ()
  64. string(SUBSTRING ${list_elem} 0 1 code_char)
  65. else ()
  66. list(GET absent_codes_in ${list_index} code_char)
  67. endif ()
  68. set(current_code "${current_code}${code_char}")
  69. endforeach ()
  70. if (current_combo STREQUAL "")
  71. list(APPEND LIST_OUT " ") # Empty set is a valid combination, but CMake isn't appending the empty string for some reason, use a space
  72. else ()
  73. list(APPEND LIST_OUT ${current_combo})
  74. endif ()
  75. list(APPEND CODES_OUT ${current_code})
  76. endforeach ()
  77. set(LIST_OUT ${LIST_OUT} PARENT_SCOPE)
  78. set(CODES_OUT ${CODES_OUT} PARENT_SCOPE)
  79. endfunction ()
  80. # generates object files for each of the sources, using the BLAS naming scheme to pass the funciton name as a preprocessor definition
  81. # @param sources_in the source files to build from
  82. # @param defines_in (optional) preprocessor definitions that will be applied to all objects
  83. # @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.
  84. # e.g. with DOUBLE set, "i*max" will generate the name "idmax", and "max" will be "dmax"
  85. # @param replace_last_with replaces the last character in the filename with this string (e.g. symm_k should be symm_TU)
  86. # @param append_with appends the filename with this string (e.g. trmm_R should be trmm_RTUU or some other combination of characters)
  87. # @param no_float_type turns off the float type define for this build (e.g. SINGLE/DOUBLE/etc)
  88. # @param complex_filename_scheme some routines have separate source files for complex and non-complex float types.
  89. # 0 - compiles for all types
  90. # 1 - compiles the sources for non-complex types only (SINGLE/DOUBLE)
  91. # 2 - compiles for complex types only (COMPLEX/DOUBLE COMPLEX)
  92. # 3 - compiles for all types, but changes source names for complex by prepending z (e.g. axpy.c becomes zaxpy.c)
  93. # STRING - compiles only the given type (e.g. DOUBLE)
  94. function(GenerateNamedObjects sources_in)
  95. if (DEFINED ARGV1)
  96. set(defines_in ${ARGV1})
  97. endif ()
  98. if (DEFINED ARGV2)
  99. set(name_in ${ARGV2})
  100. endif ()
  101. if (DEFINED ARGV3)
  102. set(use_cblas ${ARGV3})
  103. else ()
  104. set(use_cblas false)
  105. endif ()
  106. if (DEFINED ARGV4)
  107. set(replace_last_with ${ARGV4})
  108. endif ()
  109. if (DEFINED ARGV5)
  110. set(append_with ${ARGV5})
  111. endif ()
  112. if (DEFINED ARGV6)
  113. set(no_float_type ${ARGV6})
  114. else ()
  115. set(no_float_type false)
  116. endif ()
  117. if (no_float_type)
  118. set(float_list "DUMMY") # still need to loop once
  119. else ()
  120. set(float_list "${FLOAT_TYPES}")
  121. endif ()
  122. set(real_only false)
  123. set(complex_only false)
  124. set(mangle_complex_sources false)
  125. if (DEFINED ARGV7 AND NOT "${ARGV7}" STREQUAL "")
  126. if (${ARGV7} EQUAL 1)
  127. set(real_only true)
  128. elseif (${ARGV7} EQUAL 2)
  129. set(complex_only true)
  130. elseif (${ARGV7} EQUAL 3)
  131. set(mangle_complex_sources true)
  132. elseif (NOT ${ARGV7} EQUAL 0)
  133. set(float_list ${ARGV7})
  134. endif ()
  135. endif ()
  136. if (complex_only)
  137. list(REMOVE_ITEM float_list "SINGLE")
  138. list(REMOVE_ITEM float_list "DOUBLE")
  139. elseif (real_only)
  140. list(REMOVE_ITEM float_list "COMPLEX")
  141. list(REMOVE_ITEM float_list "ZCOMPLEX")
  142. endif ()
  143. set(OBJ_LIST_OUT "")
  144. foreach (float_type ${float_list})
  145. foreach (source_file ${sources_in})
  146. if (NOT no_float_type)
  147. string(SUBSTRING ${float_type} 0 1 float_char)
  148. string(TOLOWER ${float_char} float_char)
  149. endif ()
  150. if (NOT name_in)
  151. get_filename_component(source_name ${source_file} NAME_WE)
  152. set(obj_name "${float_char}${source_name}")
  153. else ()
  154. # replace * with float_char
  155. if (${name_in} MATCHES "\\*")
  156. string(REPLACE "*" ${float_char} obj_name ${name_in})
  157. else ()
  158. set(obj_name "${float_char}${name_in}")
  159. endif ()
  160. endif ()
  161. if (replace_last_with)
  162. string(REGEX REPLACE ".$" ${replace_last_with} obj_name ${obj_name})
  163. else ()
  164. set(obj_name "${obj_name}${append_with}")
  165. endif ()
  166. # now add the object and set the defines
  167. set(obj_defines ${defines_in})
  168. if (use_cblas)
  169. set(obj_name "cblas_${obj_name}")
  170. list(APPEND obj_defines "CBLAS")
  171. endif ()
  172. 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}\"")
  173. list(APPEND obj_defines ${defines_in})
  174. if (${float_type} STREQUAL "DOUBLE" OR ${float_type} STREQUAL "ZCOMPLEX")
  175. list(APPEND obj_defines "DOUBLE")
  176. endif ()
  177. if (${float_type} STREQUAL "COMPLEX" OR ${float_type} STREQUAL "ZCOMPLEX")
  178. list(APPEND obj_defines "COMPLEX")
  179. if (mangle_complex_sources)
  180. # add a z to the filename
  181. get_filename_component(source_name ${source_file} NAME)
  182. get_filename_component(source_dir ${source_file} DIRECTORY)
  183. string(REPLACE ${source_name} "z${source_name}" source_file ${source_file})
  184. endif ()
  185. endif ()
  186. add_library(${obj_name} OBJECT ${source_file})
  187. set_target_properties(${obj_name} PROPERTIES COMPILE_DEFINITIONS "${obj_defines}")
  188. list(APPEND OBJ_LIST_OUT ${obj_name})
  189. endforeach ()
  190. endforeach ()
  191. list(APPEND DBLAS_OBJS ${OBJ_LIST_OUT})
  192. set(DBLAS_OBJS ${DBLAS_OBJS} PARENT_SCOPE)
  193. endfunction ()
  194. # generates object files for each of the sources for each of the combinations of the preprocessor definitions passed in
  195. # @param sources_in the source files to build from
  196. # @param defines_in the preprocessor definitions that will be combined to create the object files
  197. # @param all_defines_in (optional) preprocessor definitions that will be applied to all objects
  198. # @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.
  199. # If 0, it will simply append the code, e.g. symm_L.c with TRANS and UNIT will be symm_LTU.
  200. # If 2, it will append the code with an underscore, e.g. symm.c with TRANS and UNIT will be symm_TU.
  201. # 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).
  202. # If 4, it will insert the code before the last underscore. E.g. trtri_U_parallel with TRANS will be trtri_UT_parallel
  203. # @param alternate_name replaces the source name as the object name (define codes are still appended)
  204. # @param no_float_type turns off the float type define for this build (e.g. SINGLE/DOUBLE/etc)
  205. # @param complex_filename_scheme see GenerateNamedObjects
  206. function(GenerateCombinationObjects sources_in defines_in absent_codes_in all_defines_in replace_scheme)
  207. if (DEFINED ARGV5)
  208. set(alternate_name ${ARGV5})
  209. endif ()
  210. if (DEFINED ARGV6)
  211. set(no_float_type ${ARGV6})
  212. endif ()
  213. if (DEFINED ARGV7)
  214. set(complex_filename_scheme ${ARGV7})
  215. endif ()
  216. AllCombinations("${defines_in}" "${absent_codes_in}")
  217. set(define_combos ${LIST_OUT})
  218. set(define_codes ${CODES_OUT})
  219. set(COMBO_OBJ_LIST_OUT "")
  220. list(LENGTH define_combos num_combos)
  221. math(EXPR num_combos "${num_combos} - 1")
  222. foreach (c RANGE 0 ${num_combos})
  223. list(GET define_combos ${c} define_combo)
  224. list(GET define_codes ${c} define_code)
  225. foreach (source_file ${sources_in})
  226. # replace colon separated list with semicolons, this turns it into a CMake list that we can use foreach with
  227. string(REPLACE ":" ";" define_combo ${define_combo})
  228. # now add the object and set the defines
  229. set(cur_defines ${define_combo})
  230. if ("${cur_defines}" STREQUAL " ")
  231. set(cur_defines ${all_defines_in})
  232. else ()
  233. list(APPEND cur_defines ${all_defines_in})
  234. endif ()
  235. set(replace_code "")
  236. set(append_code "")
  237. if (replace_scheme EQUAL 1)
  238. set(replace_code ${define_code})
  239. else ()
  240. if (replace_scheme EQUAL 2)
  241. set(append_code "_${define_code}")
  242. elseif (replace_scheme EQUAL 3)
  243. # first extract the last letter
  244. string(REGEX MATCH "[a-zA-Z]\\." last_letter ${source_file})
  245. string(SUBSTRING ${last_letter} 0 1 last_letter) # remove period from match
  246. # break the code up into the first letter and the remaining (should only be 2 anyway)
  247. string(SUBSTRING ${define_code} 0 1 define_code_first)
  248. string(SUBSTRING ${define_code} 1 -1 define_code_second)
  249. set(replace_code "${define_code_first}${last_letter}${define_code_second}")
  250. elseif (replace_scheme EQUAL 4)
  251. # insert code before the last underscore and pass that in as the alternate_name
  252. get_filename_component(alternate_name ${source_file} NAME_WE)
  253. set(extra_underscore "")
  254. # check if filename has two underscores, insert another if not (e.g. getrs_parallel needs to become getrs_U_parallel not getrsU_parallel)
  255. string(REGEX MATCH "_[a-zA-Z]+_" underscores ${alternate_name})
  256. string(LENGTH "${underscores}" underscores)
  257. if (underscores EQUAL 0)
  258. set(extra_underscore "_")
  259. endif ()
  260. string(REGEX REPLACE "(.+)(_[^_]+)$" "\\1${extra_underscore}${define_code}\\2" alternate_name ${alternate_name})
  261. else()
  262. set(append_code ${define_code}) # replace_scheme should be 0
  263. endif ()
  264. endif ()
  265. GenerateNamedObjects("${source_file}" "${cur_defines}" "${alternate_name}" 0 "${replace_code}" "${append_code}" "${no_float_type}" "${complex_filename_scheme}")
  266. list(APPEND COMBO_OBJ_LIST_OUT "${OBJ_LIST_OUT}")
  267. endforeach ()
  268. endforeach ()
  269. set(DBLAS_OBJS ${DBLAS_OBJS} PARENT_SCOPE)
  270. endfunction ()