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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. function(AllCombinations list_in)
  17. list(LENGTH list_in list_count)
  18. set(num_combos 1)
  19. # subtract 1 since we will iterate from 0 to num_combos
  20. math(EXPR num_combos "(${num_combos} << ${list_count}) - 1")
  21. set(LIST_OUT "")
  22. foreach (c RANGE 0 ${num_combos})
  23. set(current_combo "")
  24. # this is a little ridiculous just to iterate through a list w/ indices
  25. math(EXPR last_list_index "${list_count} - 1")
  26. foreach (list_index RANGE 0 ${last_list_index})
  27. math(EXPR bit "1 << ${list_index}")
  28. math(EXPR combo_has_bit "${c} & ${bit}")
  29. list(GET list_in ${list_index} list_elem)
  30. if (combo_has_bit)
  31. if (current_combo)
  32. set(current_combo "${current_combo}:${list_elem}")
  33. else ()
  34. set(current_combo ${list_elem})
  35. endif ()
  36. endif ()
  37. endforeach ()
  38. list(APPEND LIST_OUT ${current_combo})
  39. endforeach ()
  40. list(APPEND LIST_OUT " ") # Empty set is a valic combination, but CMake isn't appending the empty string for some reason, use a space
  41. set(LIST_OUT ${LIST_OUT} PARENT_SCOPE)
  42. endfunction ()
  43. # generates object files for each of the sources for each of the combinations of the preprocessor definitions passed in
  44. # @param sources_in the source files to build from
  45. # @param defines_in the preprocessor definitions that will be combined to create the object files
  46. # @param all_defines_in (optional) preprocessor definitions that will be applied to all objects
  47. function(GenerateObjects sources_in defines_in all_defines_in)
  48. AllCombinations("${defines_in}")
  49. set(define_combos ${LIST_OUT})
  50. set(OBJ_LIST_OUT "")
  51. foreach (source_file ${sources_in})
  52. foreach (def_combo ${define_combos})
  53. # replace colon separated list with semicolons, this turns it into a CMake list that we can use foreach with
  54. string(REPLACE ":" ";" def_combo ${def_combo})
  55. # build a unique variable name for this obj file by picking two letters from the defines (can't use one in this case)
  56. set(obj_name "")
  57. foreach (combo_elem ${def_combo})
  58. string(REGEX MATCH "^[A-Z][A-Z]" letter ${combo_elem})
  59. set(obj_name "${obj_name}${letter}")
  60. endforeach ()
  61. # parse file name
  62. string(REGEX MATCH "^[a-zA-Z_0-9]+" source_name ${source_file})
  63. string(TOUPPER ${source_name} source_name)
  64. # prepend the uppercased file name to the obj name
  65. set(obj_name "${source_name}_${obj_name}_OBJS")
  66. # now add the object and set the defines
  67. add_library(${obj_name} OBJECT ${source_file})
  68. set(cur_defines ${def_combo})
  69. if ("${cur_defines}" STREQUAL " ")
  70. set(cur_defines ${all_defines_in})
  71. else ()
  72. list(APPEND cur_defines ${all_defines_in})
  73. endif ()
  74. if (cur_defines AND NOT "${cur_defines}" STREQUAL " ") # using space as the empty set
  75. set_target_properties(${obj_name} PROPERTIES COMPILE_DEFINITIONS "${cur_defines}")
  76. endif ()
  77. list(APPEND OBJ_LIST_OUT ${obj_name})
  78. endforeach ()
  79. endforeach ()
  80. set(OBJ_LIST_OUT ${OBJ_LIST_OUT} PARENT_SCOPE)
  81. endfunction ()
  82. # generates object files for each of the sources, using the BLAS naming scheme to pass the funciton name as a preprocessor definition
  83. # @param sources_in the source files to build from
  84. # @param float_type_in the float type to define for this build (e.g. SINGLE/DOUBLE/etc)
  85. # @param defines_in (optional) preprocessor definitions that will be applied to all objects
  86. # @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.
  87. # e.g. with DOUBLE set, "i*max" will generate the name "idmax", and "max" will be "dmax"
  88. function(GenerateNamedObjects sources_in float_type_in defines_in name_in use_cblas)
  89. set(OBJ_LIST_OUT "")
  90. foreach (source_file ${sources_in})
  91. string(SUBSTRING ${float_type_in} 0 1 float_char)
  92. string(TOLOWER ${float_char} float_char)
  93. if (NOT name_in)
  94. get_filename_component(source_name ${source_file} NAME_WE)
  95. set(obj_name "${float_char}${source_name}")
  96. else ()
  97. # replace * with float_char
  98. if (${name_in} MATCHES "\\*")
  99. string(REPLACE "*" ${float_char} obj_name ${name_in})
  100. else ()
  101. set(obj_name "${float_char}${name_in}")
  102. endif ()
  103. endif ()
  104. # now add the object and set the defines
  105. set(obj_defines ${defines_in})
  106. if (use_cblas)
  107. set(obj_name "cblas_${obj_name}")
  108. list(APPEND obj_defines "CBLAS")
  109. endif ()
  110. 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}\"")
  111. list(APPEND obj_defines ${defines_in})
  112. list(APPEND obj_defines ${float_type_in})
  113. add_library(${obj_name} OBJECT ${source_file})
  114. set_target_properties(${obj_name} PROPERTIES COMPILE_DEFINITIONS "${obj_defines}")
  115. list(APPEND OBJ_LIST_OUT ${obj_name})
  116. endforeach ()
  117. set(OBJ_LIST_OUT ${OBJ_LIST_OUT} PARENT_SCOPE)
  118. endfunction ()