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 17 kB

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