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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. # Returns all combinations of the input list, as a list with colon-separated combinations
  14. # E.g. input of A B C returns A B C A:B A:C B:C
  15. # 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}")).
  16. # #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.
  17. # @returns LIST_OUT a list of combinations
  18. # 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
  19. function(AllCombinations list_in absent_codes_in)
  20. list(LENGTH list_in list_count)
  21. set(num_combos 1)
  22. # subtract 1 since we will iterate from 0 to num_combos
  23. math(EXPR num_combos "(${num_combos} << ${list_count}) - 1")
  24. set(LIST_OUT "")
  25. set(CODES_OUT "")
  26. foreach (c RANGE 0 ${num_combos})
  27. set(current_combo "")
  28. set(current_code "")
  29. # this is a little ridiculous just to iterate through a list w/ indices
  30. math(EXPR last_list_index "${list_count} - 1")
  31. foreach (list_index RANGE 0 ${last_list_index})
  32. math(EXPR bit "1 << ${list_index}")
  33. math(EXPR combo_has_bit "${c} & ${bit}")
  34. list(GET list_in ${list_index} list_elem)
  35. if (combo_has_bit)
  36. if (current_combo)
  37. set(current_combo "${current_combo}:${list_elem}")
  38. else ()
  39. set(current_combo ${list_elem})
  40. endif ()
  41. string(SUBSTRING ${list_elem} 0 1 code_char)
  42. else ()
  43. list(GET absent_codes_in ${list_index} code_char)
  44. endif ()
  45. set(current_code "${current_code}${code_char}")
  46. endforeach ()
  47. if (current_combo STREQUAL "")
  48. list(APPEND LIST_OUT " ") # Empty set is a valid combination, but CMake isn't appending the empty string for some reason, use a space
  49. else ()
  50. list(APPEND LIST_OUT ${current_combo})
  51. endif ()
  52. list(APPEND CODES_OUT ${current_code})
  53. endforeach ()
  54. set(LIST_OUT ${LIST_OUT} PARENT_SCOPE)
  55. set(CODES_OUT ${CODES_OUT} PARENT_SCOPE)
  56. endfunction ()
  57. # generates object files for each of the sources, using the BLAS naming scheme to pass the funciton name as a preprocessor definition
  58. # @param sources_in the source files to build from
  59. # @param float_type_in the float type to define for this build (e.g. SINGLE/DOUBLE/etc)
  60. # @param defines_in (optional) preprocessor definitions that will be applied to all objects
  61. # @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.
  62. # e.g. with DOUBLE set, "i*max" will generate the name "idmax", and "max" will be "dmax"
  63. # @param replace_k_with replaces the "k" in the filename with this string (e.g. symm_k should be symm_TU)
  64. # @param append_with appends the filename with this string (e.g. trmm_R should be trmm_RTUU or some other combination of characters)
  65. function(GenerateNamedObjects sources_in float_type_in defines_in name_in use_cblas replace_k_with append_with)
  66. set(OBJ_LIST_OUT "")
  67. foreach (source_file ${sources_in})
  68. string(SUBSTRING ${float_type_in} 0 1 float_char)
  69. string(TOLOWER ${float_char} float_char)
  70. if (NOT name_in)
  71. get_filename_component(source_name ${source_file} NAME_WE)
  72. set(obj_name "${float_char}${source_name}")
  73. else ()
  74. # replace * with float_char
  75. if (${name_in} MATCHES "\\*")
  76. string(REPLACE "*" ${float_char} obj_name ${name_in})
  77. else ()
  78. set(obj_name "${float_char}${name_in}")
  79. endif ()
  80. endif ()
  81. if (replace_k_with)
  82. string(REGEX REPLACE "k$" ${replace_k_with} obj_name ${obj_name})
  83. else ()
  84. set(obj_name "${obj_name}${append_with}")
  85. endif ()
  86. # now add the object and set the defines
  87. set(obj_defines ${defines_in})
  88. if (use_cblas)
  89. set(obj_name "cblas_${obj_name}")
  90. list(APPEND obj_defines "CBLAS")
  91. endif ()
  92. list(APPEND obj_defines "ASMNAME=${FU}${obj_name};ASMFNAME=${FU}${obj_name}${BU};NAME=${obj_name}${BU};CNAME=${obj_name};CAR_NAME=\"${obj_name}${BU}\";CHAR_CNAME=\"${obj_name}\"")
  93. list(APPEND obj_defines ${defines_in})
  94. list(APPEND obj_defines ${float_type_in})
  95. add_library(${obj_name} OBJECT ${source_file})
  96. set_target_properties(${obj_name} PROPERTIES COMPILE_DEFINITIONS "${obj_defines}")
  97. list(APPEND OBJ_LIST_OUT ${obj_name})
  98. endforeach ()
  99. set(OBJ_LIST_OUT ${OBJ_LIST_OUT} PARENT_SCOPE)
  100. endfunction ()
  101. # generates object files for each of the sources for each of the combinations of the preprocessor definitions passed in
  102. # @param sources_in the source files to build from
  103. # @param defines_in the preprocessor definitions that will be combined to create the object files
  104. # @param float_type_in the float type to define for this build (e.g. SINGLE/DOUBLE/etc)
  105. # @param all_defines_in (optional) preprocessor definitions that will be applied to all objects
  106. # @param replace_k If 1, replace the "k" in the filename with the define combo letters. E.g. symm_k with TRANS and UNIT defined will be symm_TU. If 0, appends, or if 2 appends with an underscore.
  107. function(GenerateCombinationObjects sources_in defines_in absent_codes_in float_type_in all_defines_in replace_k)
  108. AllCombinations("${defines_in}" "${absent_codes_in}")
  109. set(define_combos ${LIST_OUT})
  110. set(define_codes ${CODES_OUT})
  111. set(COMBO_OBJ_LIST_OUT "")
  112. list(LENGTH define_combos num_combos)
  113. math(EXPR num_combos "${num_combos} - 1")
  114. foreach (c RANGE 0 ${num_combos})
  115. list(GET define_combos ${c} define_combo)
  116. list(GET define_codes ${c} define_code)
  117. foreach (source_file ${sources_in})
  118. # replace colon separated list with semicolons, this turns it into a CMake list that we can use foreach with
  119. string(REPLACE ":" ";" define_combo ${define_combo})
  120. # now add the object and set the defines
  121. set(cur_defines ${define_combo})
  122. if ("${cur_defines}" STREQUAL " ")
  123. set(cur_defines ${all_defines_in})
  124. else ()
  125. list(APPEND cur_defines ${all_defines_in})
  126. endif ()
  127. set(replace_k_name "")
  128. set(append_name "")
  129. if (replace_k EQUAL 1)
  130. set(replace_k_name ${define_code})
  131. else ()
  132. if (replace_k EQUAL 2)
  133. set(append_name "_${define_code}")
  134. else ()
  135. set(append_name ${define_code})
  136. endif ()
  137. endif ()
  138. GenerateNamedObjects("${source_file}" "${float_type_in}" "${cur_defines}" "" 0 "${replace_k_name}" "${append_name}")
  139. list(APPEND COMBO_OBJ_LIST_OUT ${obj_name})
  140. endforeach ()
  141. endforeach ()
  142. set(COMBO_OBJ_LIST_OUT ${COMBO_OBJ_LIST_OUT} PARENT_SCOPE)
  143. endfunction ()