FindOpenMP.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #.rst:
  2. # FindOpenMP
  3. # ----------
  4. #
  5. # Finds OpenMP support
  6. #
  7. # This module can be used to detect OpenMP support in a compiler. If
  8. # the compiler supports OpenMP, the flags required to compile with
  9. # OpenMP support are returned in variables for the different languages.
  10. # The variables may be empty if the compiler does not need a special
  11. # flag to support OpenMP.
  12. #
  13. # The following variables are set:
  14. #
  15. # ``OpenMP_C_FLAGS``
  16. # Flags to add to the C compiler for OpenMP support.
  17. # ``OpenMP_CXX_FLAGS``
  18. # Flags to add to the CXX compiler for OpenMP support.
  19. # ``OpenMP_Fortran_FLAGS``
  20. # Flags to add to the Fortran compiler for OpenMP support.
  21. # ``OPENMP_FOUND``
  22. # True if openmp is detected.
  23. #
  24. # The following internal variables are set, if detected:
  25. #
  26. # ``OpenMP_C_SPEC_DATE``
  27. # Specification date of OpenMP version of C compiler.
  28. # ``OpenMP_CXX_SPEC_DATE``
  29. # Specification date of OpenMP version of CXX compiler.
  30. # ``OpenMP_Fortran_SPEC_DATE``
  31. # Specification date of OpenMP version of Fortran compiler.
  32. #
  33. # The specification dates are formatted as integers of the form
  34. # ``CCYYMM`` where these represent the decimal digits of the century,
  35. # year, and month.
  36. #=============================================================================
  37. # Copyright 2009 Kitware, Inc.
  38. # Copyright 2008-2009 André Rigland Brodtkorb <[email protected]>
  39. # Copyright 2012 Rolf Eike Beer <[email protected]>
  40. # Copyright 2014 Nicolas Bock <[email protected]>
  41. #
  42. # Distributed under the OSI-approved BSD License (the "License");
  43. # see accompanying file Copyright.txt for details.
  44. #
  45. # This software is distributed WITHOUT ANY WARRANTY; without even the
  46. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  47. # See the License for more information.
  48. #=============================================================================
  49. # (To distribute this file outside of CMake, substitute the full
  50. # License text for the above reference.)
  51. set(_OPENMP_REQUIRED_VARS)
  52. set(CMAKE_REQUIRED_QUIET_SAVE ${CMAKE_REQUIRED_QUIET})
  53. set(CMAKE_REQUIRED_QUIET ${OpenMP_FIND_QUIETLY})
  54. function(_OPENMP_FLAG_CANDIDATES LANG)
  55. set(OpenMP_FLAG_CANDIDATES
  56. #Empty, if compiler automatically accepts openmp
  57. " "
  58. #GNU
  59. "-fopenmp"
  60. #Clang
  61. "-fopenmp=libomp"
  62. #Microsoft Visual Studio
  63. "/openmp"
  64. #Intel windows
  65. "-Qopenmp"
  66. #PathScale, Intel
  67. "-openmp"
  68. #Sun
  69. "-xopenmp"
  70. #HP
  71. "+Oopenmp"
  72. #IBM XL C/c++
  73. "-qsmp"
  74. #Portland Group, MIPSpro
  75. "-mp"
  76. )
  77. set(OMP_FLAG_GNU "-fopenmp")
  78. set(OMP_FLAG_Clang "-fopenmp=libomp")
  79. set(OMP_FLAG_HP "+Oopenmp")
  80. if(WIN32)
  81. set(OMP_FLAG_Intel "-Qopenmp")
  82. elseif(CMAKE_${LANG}_COMPILER_ID STREQUAL "Intel" AND
  83. "${CMAKE_${LANG}_COMPILER_VERSION}" VERSION_LESS "15.0.0.20140528")
  84. set(OMP_FLAG_Intel "-openmp")
  85. else()
  86. set(OMP_FLAG_Intel "-qopenmp")
  87. endif()
  88. set(OMP_FLAG_MIPSpro "-mp")
  89. set(OMP_FLAG_MSVC "/openmp")
  90. set(OMP_FLAG_PathScale "-openmp")
  91. set(OMP_FLAG_PGI "-mp")
  92. set(OMP_FLAG_SunPro "-xopenmp")
  93. set(OMP_FLAG_XL "-qsmp")
  94. set(OMP_FLAG_Cray " ")
  95. # Move the flag that matches the compiler to the head of the list,
  96. # this is faster and doesn't clutter the output that much. If that
  97. # flag doesn't work we will still try all.
  98. if(OMP_FLAG_${CMAKE_${LANG}_COMPILER_ID})
  99. list(REMOVE_ITEM OpenMP_FLAG_CANDIDATES "${OMP_FLAG_${CMAKE_${LANG}_COMPILER_ID}}")
  100. list(INSERT OpenMP_FLAG_CANDIDATES 0 "${OMP_FLAG_${CMAKE_${LANG}_COMPILER_ID}}")
  101. endif()
  102. set(OpenMP_${LANG}_FLAG_CANDIDATES "${OpenMP_FLAG_CANDIDATES}" PARENT_SCOPE)
  103. endfunction()
  104. # sample openmp source code to test
  105. set(OpenMP_C_TEST_SOURCE
  106. "
  107. #include <omp.h>
  108. int main() {
  109. #ifdef _OPENMP
  110. return 0;
  111. #else
  112. breaks_on_purpose
  113. #endif
  114. }
  115. ")
  116. # same in Fortran
  117. set(OpenMP_Fortran_TEST_SOURCE
  118. "
  119. program test
  120. use omp_lib
  121. integer :: n
  122. n = omp_get_num_threads()
  123. end program test
  124. "
  125. )
  126. set(OpenMP_C_CXX_CHECK_VERSION_SOURCE
  127. "
  128. #include <stdio.h>
  129. #include <omp.h>
  130. const char ompver_str[] = { 'I', 'N', 'F', 'O', ':', 'O', 'p', 'e', 'n', 'M',
  131. 'P', '-', 'd', 'a', 't', 'e', '[',
  132. ('0' + ((_OPENMP/100000)%10)),
  133. ('0' + ((_OPENMP/10000)%10)),
  134. ('0' + ((_OPENMP/1000)%10)),
  135. ('0' + ((_OPENMP/100)%10)),
  136. ('0' + ((_OPENMP/10)%10)),
  137. ('0' + ((_OPENMP/1)%10)),
  138. ']', '\\0' };
  139. int main(int argc, char *argv[])
  140. {
  141. printf(\"%s\\n\", ompver_str);
  142. return 0;
  143. }
  144. ")
  145. set(OpenMP_Fortran_CHECK_VERSION_SOURCE
  146. "
  147. program omp_ver
  148. use omp_lib
  149. integer, parameter :: zero = ichar('0')
  150. integer, parameter :: ompv = openmp_version
  151. character, dimension(24), parameter :: ompver_str =&
  152. (/ 'I', 'N', 'F', 'O', ':', 'O', 'p', 'e', 'n', 'M', 'P', '-',&
  153. 'd', 'a', 't', 'e', '[',&
  154. char(zero + mod(ompv/100000, 10)),&
  155. char(zero + mod(ompv/10000, 10)),&
  156. char(zero + mod(ompv/1000, 10)),&
  157. char(zero + mod(ompv/100, 10)),&
  158. char(zero + mod(ompv/10, 10)),&
  159. char(zero + mod(ompv/1, 10)), ']' /)
  160. print *, ompver_str
  161. end program omp_ver
  162. ")
  163. function(_OPENMP_GET_SPEC_DATE LANG SPEC_DATE)
  164. set(WORK_DIR ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/FindOpenMP)
  165. if("${LANG}" STREQUAL "C")
  166. set(SRC_FILE ${WORK_DIR}/ompver.c)
  167. file(WRITE ${SRC_FILE} "${OpenMP_C_CXX_CHECK_VERSION_SOURCE}")
  168. elseif("${LANG}" STREQUAL "CXX")
  169. set(SRC_FILE ${WORK_DIR}/ompver.cpp)
  170. file(WRITE ${SRC_FILE} "${OpenMP_C_CXX_CHECK_VERSION_SOURCE}")
  171. else() # ("${LANG}" STREQUAL "Fortran")
  172. set(SRC_FILE ${WORK_DIR}/ompver.f90)
  173. file(WRITE ${SRC_FILE} "${OpenMP_Fortran_CHECK_VERSION_SOURCE}")
  174. endif()
  175. set(BIN_FILE ${WORK_DIR}/ompver_${LANG}.bin)
  176. try_compile(OpenMP_TRY_COMPILE_RESULT ${CMAKE_BINARY_DIR} ${SRC_FILE}
  177. CMAKE_FLAGS "-DCOMPILE_DEFINITIONS:STRING=${OpenMP_${LANG}_FLAGS}"
  178. COPY_FILE ${BIN_FILE})
  179. if(${OpenMP_TRY_COMPILE_RESULT})
  180. file(STRINGS ${BIN_FILE} specstr LIMIT_COUNT 1 REGEX "INFO:OpenMP-date")
  181. set(regex_spec_date ".*INFO:OpenMP-date\\[0*([^]]*)\\].*")
  182. if("${specstr}" MATCHES "${regex_spec_date}")
  183. set(${SPEC_DATE} "${CMAKE_MATCH_1}" PARENT_SCOPE)
  184. endif()
  185. endif()
  186. unset(OpenMP_TRY_COMPILE_RESULT CACHE)
  187. endfunction()
  188. # check c compiler
  189. if(CMAKE_C_COMPILER_LOADED)
  190. # if these are set then do not try to find them again,
  191. # by avoiding any try_compiles for the flags
  192. if(OpenMP_C_FLAGS)
  193. unset(OpenMP_C_FLAG_CANDIDATES)
  194. else()
  195. _OPENMP_FLAG_CANDIDATES("C")
  196. include(${CMAKE_CURRENT_LIST_DIR}/CheckCSourceCompiles.cmake)
  197. endif()
  198. foreach(FLAG IN LISTS OpenMP_C_FLAG_CANDIDATES)
  199. set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
  200. set(CMAKE_REQUIRED_FLAGS "${FLAG}")
  201. unset(OpenMP_FLAG_DETECTED CACHE)
  202. if(NOT CMAKE_REQUIRED_QUIET)
  203. message(STATUS "Try OpenMP C flag = [${FLAG}]")
  204. endif()
  205. check_c_source_compiles("${OpenMP_C_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
  206. set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
  207. if(OpenMP_FLAG_DETECTED)
  208. set(OpenMP_C_FLAGS_INTERNAL "${FLAG}")
  209. break()
  210. endif()
  211. endforeach()
  212. set(OpenMP_C_FLAGS "${OpenMP_C_FLAGS_INTERNAL}"
  213. CACHE STRING "C compiler flags for OpenMP parallization")
  214. list(APPEND _OPENMP_REQUIRED_VARS OpenMP_C_FLAGS)
  215. unset(OpenMP_C_FLAG_CANDIDATES)
  216. if (NOT OpenMP_C_SPEC_DATE)
  217. _OPENMP_GET_SPEC_DATE("C" OpenMP_C_SPEC_DATE_INTERNAL)
  218. set(OpenMP_C_SPEC_DATE "${OpenMP_C_SPEC_DATE_INTERNAL}" CACHE
  219. INTERNAL "C compiler's OpenMP specification date")
  220. endif()
  221. endif()
  222. # check cxx compiler
  223. if(CMAKE_CXX_COMPILER_LOADED)
  224. # if these are set then do not try to find them again,
  225. # by avoiding any try_compiles for the flags
  226. if(OpenMP_CXX_FLAGS)
  227. unset(OpenMP_CXX_FLAG_CANDIDATES)
  228. else()
  229. _OPENMP_FLAG_CANDIDATES("CXX")
  230. include(${CMAKE_CURRENT_LIST_DIR}/CheckCXXSourceCompiles.cmake)
  231. # use the same source for CXX as C for now
  232. set(OpenMP_CXX_TEST_SOURCE ${OpenMP_C_TEST_SOURCE})
  233. endif()
  234. foreach(FLAG IN LISTS OpenMP_CXX_FLAG_CANDIDATES)
  235. set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
  236. set(CMAKE_REQUIRED_FLAGS "${FLAG}")
  237. unset(OpenMP_FLAG_DETECTED CACHE)
  238. if(NOT CMAKE_REQUIRED_QUIET)
  239. message(STATUS "Try OpenMP CXX flag = [${FLAG}]")
  240. endif()
  241. check_cxx_source_compiles("${OpenMP_CXX_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
  242. set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
  243. if(OpenMP_FLAG_DETECTED)
  244. set(OpenMP_CXX_FLAGS_INTERNAL "${FLAG}")
  245. break()
  246. endif()
  247. endforeach()
  248. set(OpenMP_CXX_FLAGS "${OpenMP_CXX_FLAGS_INTERNAL}"
  249. CACHE STRING "C++ compiler flags for OpenMP parallization")
  250. list(APPEND _OPENMP_REQUIRED_VARS OpenMP_CXX_FLAGS)
  251. unset(OpenMP_CXX_FLAG_CANDIDATES)
  252. if (NOT OpenMP_CXX_SPEC_DATE)
  253. _OPENMP_GET_SPEC_DATE("CXX" OpenMP_CXX_SPEC_DATE_INTERNAL)
  254. set(OpenMP_CXX_SPEC_DATE "${OpenMP_CXX_SPEC_DATE_INTERNAL}" CACHE
  255. INTERNAL "C++ compiler's OpenMP specification date")
  256. endif()
  257. endif()
  258. # check Fortran compiler
  259. if(CMAKE_Fortran_COMPILER_LOADED)
  260. # if these are set then do not try to find them again,
  261. # by avoiding any try_compiles for the flags
  262. if(OpenMP_Fortran_FLAGS)
  263. unset(OpenMP_Fortran_FLAG_CANDIDATES)
  264. else()
  265. _OPENMP_FLAG_CANDIDATES("Fortran")
  266. include(${CMAKE_CURRENT_LIST_DIR}/CheckFortranSourceCompiles.cmake)
  267. endif()
  268. foreach(FLAG IN LISTS OpenMP_Fortran_FLAG_CANDIDATES)
  269. set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
  270. set(CMAKE_REQUIRED_FLAGS "${FLAG}")
  271. unset(OpenMP_FLAG_DETECTED CACHE)
  272. if(NOT CMAKE_REQUIRED_QUIET)
  273. message(STATUS "Try OpenMP Fortran flag = [${FLAG}]")
  274. endif()
  275. check_fortran_source_compiles("${OpenMP_Fortran_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
  276. set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
  277. if(OpenMP_FLAG_DETECTED)
  278. set(OpenMP_Fortran_FLAGS_INTERNAL "${FLAG}")
  279. break()
  280. endif()
  281. endforeach()
  282. set(OpenMP_Fortran_FLAGS "${OpenMP_Fortran_FLAGS_INTERNAL}"
  283. CACHE STRING "Fortran compiler flags for OpenMP parallization")
  284. list(APPEND _OPENMP_REQUIRED_VARS OpenMP_Fortran_FLAGS)
  285. unset(OpenMP_Fortran_FLAG_CANDIDATES)
  286. if (NOT OpenMP_Fortran_SPEC_DATE)
  287. _OPENMP_GET_SPEC_DATE("Fortran" OpenMP_Fortran_SPEC_DATE_INTERNAL)
  288. set(OpenMP_Fortran_SPEC_DATE "${OpenMP_Fortran_SPEC_DATE_INTERNAL}" CACHE
  289. INTERNAL "Fortran compiler's OpenMP specification date")
  290. endif()
  291. endif()
  292. set(CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_SAVE})
  293. if(_OPENMP_REQUIRED_VARS)
  294. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  295. find_package_handle_standard_args(OpenMP
  296. REQUIRED_VARS ${_OPENMP_REQUIRED_VARS})
  297. mark_as_advanced(${_OPENMP_REQUIRED_VARS})
  298. unset(_OPENMP_REQUIRED_VARS)
  299. else()
  300. message(SEND_ERROR "FindOpenMP requires C or CXX language to be enabled")
  301. endif()
  302. unset(OpenMP_C_TEST_SOURCE)
  303. unset(OpenMP_CXX_TEST_SOURCE)
  304. unset(OpenMP_Fortran_TEST_SOURCE)
  305. unset(OpenMP_C_CXX_CHECK_VERSION_SOURCE)
  306. unset(OpenMP_Fortran_CHECK_VERSION_SOURCE)