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.1 kB

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