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.

CMakeLists.txt 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. ##
  2. ## Author: Hank Anderson <hank@statease.com>
  3. ##
  4. cmake_minimum_required(VERSION 2.8.5)
  5. project(OpenBLAS C ASM)
  6. set(OpenBLAS_MAJOR_VERSION 0)
  7. set(OpenBLAS_MINOR_VERSION 3)
  8. set(OpenBLAS_PATCH_VERSION 4)
  9. set(OpenBLAS_VERSION "${OpenBLAS_MAJOR_VERSION}.${OpenBLAS_MINOR_VERSION}.${OpenBLAS_PATCH_VERSION}")
  10. # Adhere to GNU filesystem layout conventions
  11. include(GNUInstallDirs)
  12. include(CMakePackageConfigHelpers)
  13. #######
  14. if(MSVC)
  15. option(BUILD_WITHOUT_LAPACK "Do not build LAPACK and LAPACKE (Only BLAS or CBLAS)" ON)
  16. endif()
  17. option(BUILD_WITHOUT_CBLAS "Do not build the C interface (CBLAS) to the BLAS functions" OFF)
  18. option(DYNAMIC_ARCH "Include support for multiple CPU targets, with automatic selection at runtime (x86/x86_64 only)" OFF)
  19. option(DYNAMIC_OLDER "Include specific support for older cpu models (Penryn,Dunnington,Atom,Nano,Opteron) with DYNAMIC_ARCH" OFF)
  20. option(BUILD_RELAPACK "Build with ReLAPACK (recursive implementation of several LAPACK functions on top of standard LAPACK)" OFF)
  21. # Add a prefix or suffix to all exported symbol names in the shared library.
  22. # Avoids conflicts with other BLAS libraries, especially when using
  23. # 64 bit integer interfaces in OpenBLAS.
  24. set(SYMBOLPREFIX "" CACHE STRING "Add a prefix to all exported symbol names in the shared library to avoid conflicts with other BLAS libraries" )
  25. set(SYMBOLSUFFIX "" CACHE STRING "Add a suffix to all exported symbol names in the shared library, e.g. _64 for INTERFACE64 builds" )
  26. #######
  27. if(BUILD_WITHOUT_LAPACK)
  28. set(NO_LAPACK 1)
  29. set(NO_LAPACKE 1)
  30. endif()
  31. if(BUILD_WITHOUT_CBLAS)
  32. set(NO_CBLAS 1)
  33. endif()
  34. #######
  35. message(WARNING "CMake support is experimental. It does not yet support all build options and may not produce the same Makefiles that OpenBLAS ships with.")
  36. include("${PROJECT_SOURCE_DIR}/cmake/utils.cmake")
  37. include("${PROJECT_SOURCE_DIR}/cmake/system.cmake")
  38. set(OpenBLAS_LIBNAME openblas${SUFFIX64_UNDERSCORE})
  39. set(BLASDIRS interface driver/level2 driver/level3 driver/others)
  40. if (NOT DYNAMIC_ARCH)
  41. list(APPEND BLASDIRS kernel)
  42. endif ()
  43. if (DEFINED SANITY_CHECK)
  44. list(APPEND BLASDIRS reference)
  45. endif ()
  46. set(SUBDIRS ${BLASDIRS})
  47. if (NOT NO_LAPACK)
  48. list(APPEND SUBDIRS lapack)
  49. if(BUILD_RELAPACK)
  50. list(APPEND SUBDIRS relapack/src)
  51. endif()
  52. endif ()
  53. # set which float types we want to build for
  54. if (NOT DEFINED BUILD_SINGLE AND NOT DEFINED BUILD_DOUBLE AND NOT DEFINED BUILD_COMPLEX AND NOT DEFINED BUILD_COMPLEX16)
  55. # if none are defined, build for all
  56. set(BUILD_SINGLE true)
  57. set(BUILD_DOUBLE true)
  58. set(BUILD_COMPLEX true)
  59. set(BUILD_COMPLEX16 true)
  60. endif ()
  61. if (NOT DEFINED BUILD_MATGEN)
  62. set(BUILD_MATGEN true)
  63. endif()
  64. set(FLOAT_TYPES "")
  65. if (BUILD_SINGLE)
  66. message(STATUS "Building Single Precision")
  67. list(APPEND FLOAT_TYPES "SINGLE") # defines nothing
  68. endif ()
  69. if (BUILD_DOUBLE)
  70. message(STATUS "Building Double Precision")
  71. list(APPEND FLOAT_TYPES "DOUBLE") # defines DOUBLE
  72. endif ()
  73. if (BUILD_COMPLEX)
  74. message(STATUS "Building Complex Precision")
  75. list(APPEND FLOAT_TYPES "COMPLEX") # defines COMPLEX
  76. endif ()
  77. if (BUILD_COMPLEX16)
  78. message(STATUS "Building Double Complex Precision")
  79. list(APPEND FLOAT_TYPES "ZCOMPLEX") # defines COMPLEX and DOUBLE
  80. endif ()
  81. if (NOT DEFINED CORE OR "${CORE}" STREQUAL "UNKNOWN")
  82. message(FATAL_ERROR "Detecting CPU failed. Please set TARGET explicitly, e.g. make TARGET=your_cpu_target. Please read README for details.")
  83. endif ()
  84. #Set default output directory
  85. set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
  86. set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
  87. if(MSVC)
  88. set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${PROJECT_BINARY_DIR}/lib/Debug)
  89. set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${PROJECT_BINARY_DIR}/lib/Release)
  90. endif ()
  91. # get obj vars into format that add_library likes: $<TARGET_OBJS:objlib> (see http://www.cmake.org/cmake/help/v3.0/command/add_library.html)
  92. set(TARGET_OBJS "")
  93. foreach (SUBDIR ${SUBDIRS})
  94. add_subdirectory(${SUBDIR})
  95. string(REPLACE "/" "_" subdir_obj ${SUBDIR})
  96. list(APPEND TARGET_OBJS "$<TARGET_OBJECTS:${subdir_obj}>")
  97. endforeach ()
  98. # netlib:
  99. # Can't just use lapack-netlib's CMake files, since they are set up to search for BLAS, build and install a binary. We just want to build a couple of lib files out of lapack and lapacke.
  100. # Not using add_subdirectory here because lapack-netlib already has its own CMakeLists.txt. Instead include a cmake script with the sources we want.
  101. if (NOT NOFORTRAN AND NOT NO_LAPACK)
  102. include("${PROJECT_SOURCE_DIR}/cmake/lapack.cmake")
  103. if (NOT NO_LAPACKE)
  104. include("${PROJECT_SOURCE_DIR}/cmake/lapacke.cmake")
  105. endif ()
  106. endif ()
  107. # Only generate .def for dll on MSVC and always produce pdb files for debug and release
  108. if(MSVC)
  109. if (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} LESS 3.4)
  110. set(OpenBLAS_DEF_FILE "${PROJECT_BINARY_DIR}/openblas.def")
  111. endif()
  112. set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /Zi")
  113. set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
  114. endif()
  115. if (${DYNAMIC_ARCH})
  116. add_subdirectory(kernel)
  117. foreach(TARGET_CORE ${DYNAMIC_CORE})
  118. message("${TARGET_CORE}")
  119. list(APPEND TARGET_OBJS "$<TARGET_OBJECTS:kernel_${TARGET_CORE}>")
  120. endforeach()
  121. endif ()
  122. # Only build shared libs for MSVC
  123. if (MSVC)
  124. set(BUILD_SHARED_LIBS ON)
  125. endif()
  126. # add objects to the openblas lib
  127. add_library(${OpenBLAS_LIBNAME} ${LA_SOURCES} ${LAPACKE_SOURCES} ${RELA_SOURCES} ${TARGET_OBJS} ${OpenBLAS_DEF_FILE})
  128. target_include_directories(${OpenBLAS_LIBNAME} INTERFACE $<INSTALL_INTERFACE:include>)
  129. # Android needs to explicitly link against libm
  130. if(ANDROID)
  131. target_link_libraries(${OpenBLAS_LIBNAME} m)
  132. endif()
  133. # Handle MSVC exports
  134. if(MSVC AND BUILD_SHARED_LIBS)
  135. if (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} LESS 3.4)
  136. include("${PROJECT_SOURCE_DIR}/cmake/export.cmake")
  137. else()
  138. # Creates verbose .def file (51KB vs 18KB)
  139. set_target_properties(${OpenBLAS_LIBNAME} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS true)
  140. endif()
  141. endif()
  142. # Set output for libopenblas
  143. set_target_properties( ${OpenBLAS_LIBNAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
  144. set_target_properties( ${OpenBLAS_LIBNAME} PROPERTIES LIBRARY_OUTPUT_NAME_DEBUG "${OpenBLAS_LIBNAME}_d")
  145. set_target_properties( ${OpenBLAS_LIBNAME} PROPERTIES EXPORT_NAME "OpenBLAS")
  146. foreach (OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES})
  147. string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG )
  148. set_target_properties( ${OpenBLAS_LIBNAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${PROJECT_BINARY_DIR}/lib/${OUTPUTCONFIG} )
  149. set_target_properties( ${OpenBLAS_LIBNAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${PROJECT_BINARY_DIR}/lib/${OUTPUTCONFIG} )
  150. set_target_properties( ${OpenBLAS_LIBNAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${PROJECT_BINARY_DIR}/lib/${OUTPUTCONFIG} )
  151. endforeach()
  152. enable_testing()
  153. if (USE_THREAD)
  154. # Add threading library to linker
  155. find_package(Threads)
  156. if (THREADS_HAVE_PTHREAD_ARG)
  157. set_property(TARGET ${OpenBLAS_LIBNAME} PROPERTY COMPILE_OPTIONS "-pthread")
  158. set_property(TARGET ${OpenBLAS_LIBNAME} PROPERTY INTERFACE_COMPILE_OPTIONS "-pthread")
  159. endif()
  160. target_link_libraries(${OpenBLAS_LIBNAME} ${CMAKE_THREAD_LIBS_INIT})
  161. endif()
  162. if (MSVC OR NOT NOFORTRAN)
  163. # Broken without fortran on unix
  164. add_subdirectory(utest)
  165. endif()
  166. if (NOT MSVC AND NOT NOFORTRAN)
  167. # Build test and ctest
  168. add_subdirectory(test)
  169. if(NOT NO_CBLAS)
  170. add_subdirectory(ctest)
  171. endif()
  172. endif()
  173. set_target_properties(${OpenBLAS_LIBNAME} PROPERTIES
  174. VERSION ${OpenBLAS_MAJOR_VERSION}.${OpenBLAS_MINOR_VERSION}
  175. SOVERSION ${OpenBLAS_MAJOR_VERSION}
  176. )
  177. if (BUILD_SHARED_LIBS AND NOT ${SYMBOLPREFIX}${SYMBOLSUFIX} STREQUAL "")
  178. if (NOT DEFINED ARCH)
  179. set(ARCH_IN "x86_64")
  180. else()
  181. set(ARCH_IN ${ARCH})
  182. endif()
  183. if (${CORE} STREQUAL "generic")
  184. set(ARCH_IN "GENERIC")
  185. endif ()
  186. if (NOT DEFINED EXPRECISION)
  187. set(EXPRECISION_IN 0)
  188. else()
  189. set(EXPRECISION_IN ${EXPRECISION})
  190. endif()
  191. if (NOT DEFINED NO_CBLAS)
  192. set(NO_CBLAS_IN 0)
  193. else()
  194. set(NO_CBLAS_IN ${NO_CBLAS})
  195. endif()
  196. if (NOT DEFINED NO_LAPACK)
  197. set(NO_LAPACK_IN 0)
  198. else()
  199. set(NO_LAPACK_IN ${NO_LAPACK})
  200. endif()
  201. if (NOT DEFINED NO_LAPACKE)
  202. set(NO_LAPACKE_IN 0)
  203. else()
  204. set(NO_LAPACKE_IN ${NO_LAPACKE})
  205. endif()
  206. if (NOT DEFINED NEED2UNDERSCORES)
  207. set(NEED2UNDERSCORES_IN 0)
  208. else()
  209. set(NEED2UNDERSCORES_IN ${NEED2UNDERSCORES})
  210. endif()
  211. if (NOT DEFINED ONLY_CBLAS)
  212. set(ONLY_CBLAS_IN 0)
  213. else()
  214. set(ONLY_CBLAS_IN ${ONLY_CBLAS})
  215. endif()
  216. if (NOT DEFINED BU)
  217. set(BU _)
  218. endif()
  219. if (NOT ${SYMBOLPREFIX} STREQUAL "")
  220. message(STATUS "adding prefix ${SYMBOLPREFIX} to names of exported symbols in ${OpenBLAS_LIBNAME}")
  221. endif()
  222. if (NOT ${SYMBOLSUFFIX} STREQUAL "")
  223. message(STATUS "adding suffix ${SYMBOLSUFFIX} to names of exported symbols in ${OpenBLAS_LIBNAME}")
  224. endif()
  225. add_custom_command(TARGET ${OpenBLAS_LIBNAME} POST_BUILD
  226. COMMAND perl ${PROJECT_SOURCE_DIR}/exports/gensymbol "objcopy" "${ARCH}" "${BU}" "${EXPRECISION_IN}" "${NO_CBLAS_IN}" "${NO_LAPACK_IN}" "${NO_LAPACKE_IN}" "${NEED2UNDERSCORES_IN}" "${ONLY_CBLAS_IN}" \"${SYMBOLPREFIX}\" \"${SYMBOLSUFFIX}\" "${BUILD_LAPACK_DEPRECATED}" > ${PROJECT_BINARY_DIR}/objcopy.def
  227. COMMAND objcopy -v --redefine-syms ${PROJECT_BINARY_DIR}/objcopy.def ${PROJECT_BINARY_DIR}/lib/lib${OpenBLAS_LIBNAME}.so
  228. COMMENT "renaming symbols"
  229. )
  230. endif()
  231. # Install project
  232. # Install libraries
  233. install(TARGETS ${OpenBLAS_LIBNAME}
  234. EXPORT "OpenBLAS${SUFFIX64}Targets"
  235. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  236. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  237. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} )
  238. # Install headers
  239. set(CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_INCLUDEDIR}/openblas${SUFFIX64})
  240. set(CMAKE_INSTALL_FULL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR})
  241. message(STATUS "Generating openblas_config.h in ${CMAKE_INSTALL_INCLUDEDIR}")
  242. set(OPENBLAS_CONFIG_H ${CMAKE_BINARY_DIR}/openblas_config.h)
  243. file(WRITE ${OPENBLAS_CONFIG_H} "#ifndef OPENBLAS_CONFIG_H\n")
  244. file(APPEND ${OPENBLAS_CONFIG_H} "#define OPENBLAS_CONFIG_H\n")
  245. file(STRINGS ${PROJECT_BINARY_DIR}/config.h __lines)
  246. foreach(line ${__lines})
  247. string(REPLACE "#define " "" line ${line})
  248. file(APPEND ${OPENBLAS_CONFIG_H} "#define OPENBLAS_${line}\n")
  249. endforeach()
  250. file(APPEND ${OPENBLAS_CONFIG_H} "#define OPENBLAS_VERSION \"OpenBLAS ${OpenBLAS_VERSION}\"\n")
  251. file(READ ${CMAKE_CURRENT_SOURCE_DIR}/openblas_config_template.h OPENBLAS_CONFIG_TEMPLATE_H_CONTENTS)
  252. file(APPEND ${OPENBLAS_CONFIG_H} "${OPENBLAS_CONFIG_TEMPLATE_H_CONTENTS}\n")
  253. file(APPEND ${OPENBLAS_CONFIG_H} "#endif /* OPENBLAS_CONFIG_H */\n")
  254. install (FILES ${OPENBLAS_CONFIG_H} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
  255. if(NOT NOFORTRAN)
  256. message(STATUS "Generating f77blas.h in ${CMAKE_INSTALL_INCLUDEDIR}")
  257. set(F77BLAS_H ${CMAKE_BINARY_DIR}/f77blas.h)
  258. file(WRITE ${F77BLAS_H} "#ifndef OPENBLAS_F77BLAS_H\n")
  259. file(APPEND ${F77BLAS_H} "#define OPENBLAS_F77BLAS_H\n")
  260. file(APPEND ${F77BLAS_H} "#include \"openblas_config.h\"\n")
  261. file(READ ${CMAKE_CURRENT_SOURCE_DIR}/common_interface.h COMMON_INTERFACE_H_CONTENTS)
  262. file(APPEND ${F77BLAS_H} "${COMMON_INTERFACE_H_CONTENTS}\n")
  263. file(APPEND ${F77BLAS_H} "#endif")
  264. install (FILES ${F77BLAS_H} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
  265. endif()
  266. if(NOT NO_CBLAS)
  267. message (STATUS "Generating cblas.h in ${CMAKE_INSTALL_INCLUDEDIR}")
  268. file(READ ${CMAKE_CURRENT_SOURCE_DIR}/cblas.h CBLAS_H_CONTENTS)
  269. string(REPLACE "common" "openblas_config" CBLAS_H_CONTENTS_NEW "${CBLAS_H_CONTENTS}")
  270. file(WRITE ${CMAKE_BINARY_DIR}/cblas.tmp "${CBLAS_H_CONTENTS_NEW}")
  271. install (FILES ${CMAKE_BINARY_DIR}/cblas.tmp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} RENAME cblas.h)
  272. endif()
  273. if(NOT NO_LAPACKE)
  274. message (STATUS "Copying LAPACKE header files to ${CMAKE_INSTALL_INCLUDEDIR}")
  275. add_dependencies( ${OpenBLAS_LIBNAME} genlapacke)
  276. FILE(GLOB_RECURSE INCLUDE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/lapack-netlib/LAPACKE/*.h")
  277. install (FILES ${INCLUDE_FILES} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
  278. ADD_CUSTOM_TARGET(genlapacke
  279. COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/lapack-netlib/LAPACKE/include/lapacke_mangling_with_flags.h.in "${CMAKE_BINARY_DIR}/lapacke_mangling.h"
  280. )
  281. install (FILES ${CMAKE_BINARY_DIR}/lapacke_mangling.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/openblas${SUFFIX64})
  282. endif()
  283. include(FindPkgConfig QUIET)
  284. if(PKG_CONFIG_FOUND)
  285. configure_file(${PROJECT_SOURCE_DIR}/cmake/openblas.pc.in ${PROJECT_BINARY_DIR}/openblas${SUFFIX64}.pc @ONLY)
  286. install (FILES ${PROJECT_BINARY_DIR}/openblas${SUFFIX64}.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig/)
  287. endif()
  288. # GNUInstallDirs "DATADIR" wrong here; CMake search path wants "share".
  289. set(PN OpenBLAS)
  290. set(CMAKECONFIG_INSTALL_DIR "share/cmake/${PN}${SUFFIX64}")
  291. configure_package_config_file(cmake/${PN}Config.cmake.in
  292. "${CMAKE_CURRENT_BINARY_DIR}/${PN}${SUFFIX64}Config.cmake"
  293. INSTALL_DESTINATION ${CMAKECONFIG_INSTALL_DIR})
  294. write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PN}ConfigVersion.cmake
  295. VERSION ${${PN}_VERSION}
  296. COMPATIBILITY AnyNewerVersion)
  297. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PN}${SUFFIX64}Config.cmake
  298. DESTINATION ${CMAKECONFIG_INSTALL_DIR})
  299. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PN}ConfigVersion.cmake
  300. RENAME ${PN}${SUFFIX64}ConfigVersion.cmake
  301. DESTINATION ${CMAKECONFIG_INSTALL_DIR})
  302. install(EXPORT "${PN}${SUFFIX64}Targets"
  303. NAMESPACE "${PN}${SUFFIX64}::"
  304. DESTINATION ${CMAKECONFIG_INSTALL_DIR})