FindOpenMP.cmake 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindOpenMP
  5. ----------
  6. Finds Open Multi-Processing (OpenMP) support.
  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. Variables
  13. ^^^^^^^^^
  14. The module exposes the components ``C``, ``CXX``, and ``Fortran``.
  15. Each of these controls the various languages to search OpenMP support for.
  16. Depending on the enabled components the following variables will be set:
  17. ``OpenMP_FOUND``
  18. Variable indicating that OpenMP flags for all requested languages have been found.
  19. If no components are specified, this is true if OpenMP settings for all enabled languages
  20. were detected.
  21. ``OpenMP_VERSION``
  22. Minimal version of the OpenMP standard detected among the requested languages,
  23. or all enabled languages if no components were specified.
  24. This module will set the following variables per language in your
  25. project, where ``<lang>`` is one of C, CXX, or Fortran:
  26. ``OpenMP_<lang>_FOUND``
  27. Variable indicating if OpenMP support for ``<lang>`` was detected.
  28. ``OpenMP_<lang>_FLAGS``
  29. OpenMP compiler flags for ``<lang>``, separated by spaces.
  30. ``OpenMP_<lang>_INCLUDE_DIRS``
  31. Directories that must be added to the header search path for ``<lang>``
  32. when using OpenMP.
  33. For linking with OpenMP code written in ``<lang>``, the following
  34. variables are provided:
  35. ``OpenMP_<lang>_LIB_NAMES``
  36. :ref:`;-list <CMake Language Lists>` of libraries for OpenMP programs for ``<lang>``.
  37. ``OpenMP_<libname>_LIBRARY``
  38. Location of the individual libraries needed for OpenMP support in ``<lang>``.
  39. ``OpenMP_<lang>_LIBRARIES``
  40. A list of libraries needed to link with OpenMP code written in ``<lang>``.
  41. Additionally, the module provides :prop_tgt:`IMPORTED` targets:
  42. ``OpenMP::OpenMP_<lang>``
  43. Target for using OpenMP from ``<lang>``.
  44. Specifically for Fortran, the module sets the following variables:
  45. ``OpenMP_Fortran_HAVE_OMPLIB_HEADER``
  46. Boolean indicating if OpenMP is accessible through ``omp_lib.h``.
  47. ``OpenMP_Fortran_HAVE_OMPLIB_MODULE``
  48. Boolean indicating if OpenMP is accessible through the ``omp_lib`` Fortran module.
  49. The module will also try to provide the OpenMP version variables:
  50. ``OpenMP_<lang>_SPEC_DATE``
  51. Date of the OpenMP specification implemented by the ``<lang>`` compiler.
  52. ``OpenMP_<lang>_VERSION_MAJOR``
  53. Major version of OpenMP implemented by the ``<lang>`` compiler.
  54. ``OpenMP_<lang>_VERSION_MINOR``
  55. Minor version of OpenMP implemented by the ``<lang>`` compiler.
  56. ``OpenMP_<lang>_VERSION``
  57. OpenMP version implemented by the ``<lang>`` compiler.
  58. The specification date is formatted as given in the OpenMP standard:
  59. ``yyyymm`` where ``yyyy`` and ``mm`` represents the year and month of
  60. the OpenMP specification implemented by the ``<lang>`` compiler.
  61. For some compilers, it may be necessary to add a header search path to find
  62. the relevant OpenMP headers. This location may be language-specific. Where
  63. this is needed, the module may attempt to find the location, but it can be
  64. provided directly by setting the ``OpenMP_<lang>_INCLUDE_DIR`` cache variable.
  65. Note that this variable is an _input_ control to the module. Project code
  66. should use the ``OpenMP_<lang>_INCLUDE_DIRS`` _output_ variable if it needs
  67. to know what include directories are needed.
  68. #]=======================================================================]
  69. cmake_policy(PUSH)
  70. cmake_policy(SET CMP0012 NEW) # if() recognizes numbers and booleans
  71. cmake_policy(SET CMP0054 NEW) # if() quoted variables not dereferenced
  72. cmake_policy(SET CMP0057 NEW) # if IN_LIST
  73. function(_OPENMP_FLAG_CANDIDATES LANG)
  74. if(NOT OpenMP_${LANG}_FLAG)
  75. unset(OpenMP_FLAG_CANDIDATES)
  76. set(OMP_FLAG_GNU "-fopenmp")
  77. set(OMP_FLAG_Clang "-fopenmp=libomp" "-fopenmp=libiomp5" "-fopenmp")
  78. set(OMP_FLAG_AppleClang "-Xclang -fopenmp")
  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_MSVC "-openmp")
  89. set(OMP_FLAG_PathScale "-openmp")
  90. set(OMP_FLAG_NAG "-openmp")
  91. set(OMP_FLAG_Absoft "-openmp")
  92. set(OMP_FLAG_PGI "-mp")
  93. set(OMP_FLAG_Flang "-fopenmp")
  94. set(OMP_FLAG_SunPro "-xopenmp")
  95. set(OMP_FLAG_XL "-qsmp=omp")
  96. # Cray compiler activate OpenMP with -h omp, which is enabled by default.
  97. set(OMP_FLAG_Cray " " "-h omp")
  98. # If we know the correct flags, use those
  99. if(DEFINED OMP_FLAG_${CMAKE_${LANG}_COMPILER_ID})
  100. set(OpenMP_FLAG_CANDIDATES "${OMP_FLAG_${CMAKE_${LANG}_COMPILER_ID}}")
  101. # Fall back to reasonable default tries otherwise
  102. else()
  103. set(OpenMP_FLAG_CANDIDATES "-openmp" "-fopenmp" "-mp" " ")
  104. endif()
  105. set(OpenMP_${LANG}_FLAG_CANDIDATES "${OpenMP_FLAG_CANDIDATES}" PARENT_SCOPE)
  106. else()
  107. set(OpenMP_${LANG}_FLAG_CANDIDATES "${OpenMP_${LANG}_FLAG}" PARENT_SCOPE)
  108. endif()
  109. endfunction()
  110. # sample openmp source code to test
  111. set(OpenMP_C_CXX_TEST_SOURCE
  112. "
  113. #include <omp.h>
  114. int main(void) {
  115. #ifdef _OPENMP
  116. omp_get_max_threads();
  117. return 0;
  118. #elif defined(__HIP_DEVICE_COMPILE__)
  119. return 0;
  120. #else
  121. breaks_on_purpose
  122. #endif
  123. }
  124. ")
  125. # in Fortran, an implementation may provide an omp_lib.h header
  126. # or omp_lib module, or both (OpenMP standard, section 3.1)
  127. # Furthmore !$ is the Fortran equivalent of #ifdef _OPENMP (OpenMP standard, 2.2.2)
  128. # Without the conditional compilation, some compilers (e.g. PGI) might compile OpenMP code
  129. # while not actually enabling OpenMP, building code sequentially
  130. set(OpenMP_Fortran_TEST_SOURCE
  131. "
  132. program test
  133. @OpenMP_Fortran_INCLUDE_LINE@
  134. !$ integer :: n
  135. n = omp_get_num_threads()
  136. end program test
  137. "
  138. )
  139. function(_OPENMP_WRITE_SOURCE_FILE LANG SRC_FILE_CONTENT_VAR SRC_FILE_NAME SRC_FILE_FULLPATH)
  140. set(WORK_DIR ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/FindOpenMP)
  141. if("${LANG}" STREQUAL "C")
  142. set(SRC_FILE "${WORK_DIR}/${SRC_FILE_NAME}.c")
  143. file(WRITE "${SRC_FILE}" "${OpenMP_C_CXX_${SRC_FILE_CONTENT_VAR}}")
  144. elseif("${LANG}" STREQUAL "CXX")
  145. set(SRC_FILE "${WORK_DIR}/${SRC_FILE_NAME}.cpp")
  146. file(WRITE "${SRC_FILE}" "${OpenMP_C_CXX_${SRC_FILE_CONTENT_VAR}}")
  147. elseif("${LANG}" STREQUAL "Fortran")
  148. set(SRC_FILE "${WORK_DIR}/${SRC_FILE_NAME}.f90")
  149. file(WRITE "${SRC_FILE}_in" "${OpenMP_Fortran_${SRC_FILE_CONTENT_VAR}}")
  150. configure_file("${SRC_FILE}_in" "${SRC_FILE}" @ONLY)
  151. endif()
  152. set(${SRC_FILE_FULLPATH} "${SRC_FILE}" PARENT_SCOPE)
  153. endfunction()
  154. include(${CMAKE_CURRENT_LIST_DIR}/CMakeParseImplicitLinkInfo.cmake)
  155. function(_OPENMP_GET_FLAGS LANG FLAG_MODE OPENMP_FLAG_VAR OPENMP_LIB_NAMES_VAR)
  156. _OPENMP_FLAG_CANDIDATES("${LANG}")
  157. _OPENMP_WRITE_SOURCE_FILE("${LANG}" "TEST_SOURCE" OpenMPTryFlag _OPENMP_TEST_SRC)
  158. unset(OpenMP_VERBOSE_COMPILE_OPTIONS)
  159. separate_arguments(OpenMP_VERBOSE_OPTIONS NATIVE_COMMAND "${CMAKE_${LANG}_VERBOSE_FLAG}")
  160. foreach(_VERBOSE_OPTION IN LISTS OpenMP_VERBOSE_OPTIONS)
  161. if(NOT _VERBOSE_OPTION MATCHES "^-Wl,")
  162. list(APPEND OpenMP_VERBOSE_COMPILE_OPTIONS ${_VERBOSE_OPTION})
  163. endif()
  164. endforeach()
  165. foreach(OPENMP_FLAG IN LISTS OpenMP_${LANG}_FLAG_CANDIDATES)
  166. set(OPENMP_FLAGS_TEST "${OPENMP_FLAG}")
  167. if(OpenMP_VERBOSE_COMPILE_OPTIONS)
  168. string(APPEND OPENMP_FLAGS_TEST " ${OpenMP_VERBOSE_COMPILE_OPTIONS}")
  169. endif()
  170. string(REGEX REPLACE "[-/=+]" "" OPENMP_PLAIN_FLAG "${OPENMP_FLAG}")
  171. try_compile( OpenMP_COMPILE_RESULT_${FLAG_MODE}_${OPENMP_PLAIN_FLAG} ${CMAKE_BINARY_DIR} ${_OPENMP_TEST_SRC}
  172. CMAKE_FLAGS "-DCOMPILE_DEFINITIONS:STRING=${OPENMP_FLAGS_TEST}"
  173. LINK_LIBRARIES ${CMAKE_${LANG}_VERBOSE_FLAG}
  174. OUTPUT_VARIABLE OpenMP_TRY_COMPILE_OUTPUT
  175. )
  176. if(OpenMP_COMPILE_RESULT_${FLAG_MODE}_${OPENMP_PLAIN_FLAG})
  177. set("${OPENMP_FLAG_VAR}" "${OPENMP_FLAG}" PARENT_SCOPE)
  178. if(CMAKE_${LANG}_VERBOSE_FLAG)
  179. unset(OpenMP_${LANG}_IMPLICIT_LIBRARIES)
  180. unset(OpenMP_${LANG}_IMPLICIT_LINK_DIRS)
  181. unset(OpenMP_${LANG}_IMPLICIT_FWK_DIRS)
  182. unset(OpenMP_${LANG}_LOG_VAR)
  183. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  184. "Detecting ${LANG} OpenMP compiler ABI info compiled with the following output:\n${OpenMP_TRY_COMPILE_OUTPUT}\n\n")
  185. cmake_parse_implicit_link_info("${OpenMP_TRY_COMPILE_OUTPUT}"
  186. OpenMP_${LANG}_IMPLICIT_LIBRARIES
  187. OpenMP_${LANG}_IMPLICIT_LINK_DIRS
  188. OpenMP_${LANG}_IMPLICIT_FWK_DIRS
  189. OpenMP_${LANG}_LOG_VAR
  190. "${CMAKE_${LANG}_IMPLICIT_OBJECT_REGEX}"
  191. )
  192. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  193. "Parsed ${LANG} OpenMP implicit link information from above output:\n${OpenMP_${LANG}_LOG_VAR}\n\n")
  194. unset(_OPENMP_LIB_NAMES)
  195. foreach(_OPENMP_IMPLICIT_LIB IN LISTS OpenMP_${LANG}_IMPLICIT_LIBRARIES)
  196. get_filename_component(_OPENMP_IMPLICIT_LIB_DIR "${_OPENMP_IMPLICIT_LIB}" DIRECTORY)
  197. get_filename_component(_OPENMP_IMPLICIT_LIB_NAME "${_OPENMP_IMPLICIT_LIB}" NAME)
  198. get_filename_component(_OPENMP_IMPLICIT_LIB_PLAIN "${_OPENMP_IMPLICIT_LIB}" NAME_WE)
  199. string(REGEX REPLACE "([][+.*?()^$])" "\\\\\\1" _OPENMP_IMPLICIT_LIB_PLAIN_ESC "${_OPENMP_IMPLICIT_LIB_PLAIN}")
  200. string(REGEX REPLACE "([][+.*?()^$])" "\\\\\\1" _OPENMP_IMPLICIT_LIB_PATH_ESC "${_OPENMP_IMPLICIT_LIB}")
  201. if(NOT ( "${_OPENMP_IMPLICIT_LIB}" IN_LIST CMAKE_${LANG}_IMPLICIT_LINK_LIBRARIES
  202. OR "${CMAKE_${LANG}_STANDARD_LIBRARIES}" MATCHES "(^| )(-Wl,)?(-l)?(${_OPENMP_IMPLICIT_LIB_PLAIN_ESC}|${_OPENMP_IMPLICIT_LIB_PATH_ESC})( |$)"
  203. OR "${CMAKE_${LANG}_LINK_EXECUTABLE}" MATCHES "(^| )(-Wl,)?(-l)?(${_OPENMP_IMPLICIT_LIB_PLAIN_ESC}|${_OPENMP_IMPLICIT_LIB_PATH_ESC})( |$)" ) )
  204. if(_OPENMP_IMPLICIT_LIB_DIR)
  205. set(OpenMP_${_OPENMP_IMPLICIT_LIB_PLAIN}_LIBRARY "${_OPENMP_IMPLICIT_LIB}" CACHE FILEPATH
  206. "Path to the ${_OPENMP_IMPLICIT_LIB_PLAIN} library for OpenMP")
  207. else()
  208. find_library(OpenMP_${_OPENMP_IMPLICIT_LIB_PLAIN}_LIBRARY
  209. NAMES "${_OPENMP_IMPLICIT_LIB_NAME}"
  210. DOC "Path to the ${_OPENMP_IMPLICIT_LIB_PLAIN} library for OpenMP"
  211. HINTS ${OpenMP_${LANG}_IMPLICIT_LINK_DIRS}
  212. CMAKE_FIND_ROOT_PATH_BOTH
  213. NO_DEFAULT_PATH
  214. )
  215. endif()
  216. mark_as_advanced(OpenMP_${_OPENMP_IMPLICIT_LIB_PLAIN}_LIBRARY)
  217. list(APPEND _OPENMP_LIB_NAMES ${_OPENMP_IMPLICIT_LIB_PLAIN})
  218. endif()
  219. endforeach()
  220. set("${OPENMP_LIB_NAMES_VAR}" "${_OPENMP_LIB_NAMES}" PARENT_SCOPE)
  221. else()
  222. # We do not know how to extract implicit OpenMP libraries for this compiler.
  223. # Assume that it handles them automatically, e.g. the Intel Compiler on
  224. # Windows should put the dependency in its object files.
  225. set("${OPENMP_LIB_NAMES_VAR}" "" PARENT_SCOPE)
  226. endif()
  227. break()
  228. elseif(CMAKE_${LANG}_COMPILER_ID STREQUAL "AppleClang"
  229. AND CMAKE_${LANG}_COMPILER_VERSION VERSION_GREATER_EQUAL "7.0")
  230. # Check for separate OpenMP library on AppleClang 7+
  231. find_library(OpenMP_libomp_LIBRARY
  232. NAMES omp gomp iomp5
  233. HINTS ${CMAKE_${LANG}_IMPLICIT_LINK_DIRECTORIES}
  234. )
  235. mark_as_advanced(OpenMP_libomp_LIBRARY)
  236. if(OpenMP_libomp_LIBRARY)
  237. # Try without specifying include directory first. We only want to
  238. # explicitly add a search path if the header can't be found on the
  239. # default header search path already.
  240. try_compile( OpenMP_COMPILE_RESULT_${FLAG_MODE}_${OPENMP_PLAIN_FLAG} ${CMAKE_BINARY_DIR} ${_OPENMP_TEST_SRC}
  241. CMAKE_FLAGS "-DCOMPILE_DEFINITIONS:STRING=${OPENMP_FLAGS_TEST}"
  242. LINK_LIBRARIES ${CMAKE_${LANG}_VERBOSE_FLAG} ${OpenMP_libomp_LIBRARY}
  243. OUTPUT_VARIABLE OpenMP_TRY_COMPILE_OUTPUT
  244. )
  245. if(NOT OpenMP_COMPILE_RESULT_${FLAG_MODE}_${OPENMP_PLAIN_FLAG})
  246. find_path(OpenMP_${LANG}_INCLUDE_DIR omp.h)
  247. mark_as_advanced(OpenMP_${LANG}_INCLUDE_DIR)
  248. set(OpenMP_${LANG}_INCLUDE_DIR "${OpenMP_${LANG}_INCLUDE_DIR}" PARENT_SCOPE)
  249. if(OpenMP_${LANG}_INCLUDE_DIR)
  250. try_compile( OpenMP_COMPILE_RESULT_${FLAG_MODE}_${OPENMP_PLAIN_FLAG} ${CMAKE_BINARY_DIR} ${_OPENMP_TEST_SRC}
  251. CMAKE_FLAGS "-DCOMPILE_DEFINITIONS:STRING=${OPENMP_FLAGS_TEST}"
  252. "-DINCLUDE_DIRECTORIES:STRING=${OpenMP_${LANG}_INCLUDE_DIR}"
  253. LINK_LIBRARIES ${CMAKE_${LANG}_VERBOSE_FLAG} ${OpenMP_libomp_LIBRARY}
  254. OUTPUT_VARIABLE OpenMP_TRY_COMPILE_OUTPUT
  255. )
  256. endif()
  257. endif()
  258. if(OpenMP_COMPILE_RESULT_${FLAG_MODE}_${OPENMP_PLAIN_FLAG})
  259. set("${OPENMP_FLAG_VAR}" "${OPENMP_FLAG}" PARENT_SCOPE)
  260. set("${OPENMP_LIB_NAMES_VAR}" "libomp" PARENT_SCOPE)
  261. break()
  262. endif()
  263. endif()
  264. else()
  265. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  266. "Detecting ${LANG} OpenMP failed with the following output:\n${OpenMP_TRY_COMPILE_OUTPUT}\n\n")
  267. endif()
  268. set("${OPENMP_LIB_NAMES_VAR}" "NOTFOUND" PARENT_SCOPE)
  269. set("${OPENMP_FLAG_VAR}" "NOTFOUND" PARENT_SCOPE)
  270. endforeach()
  271. unset(OpenMP_VERBOSE_COMPILE_OPTIONS)
  272. endfunction()
  273. set(OpenMP_C_CXX_CHECK_VERSION_SOURCE
  274. "
  275. #include <stdio.h>
  276. #include <omp.h>
  277. const char ompver_str[] = { 'I', 'N', 'F', 'O', ':', 'O', 'p', 'e', 'n', 'M',
  278. 'P', '-', 'd', 'a', 't', 'e', '[',
  279. ('0' + ((_OPENMP/100000)%10)),
  280. ('0' + ((_OPENMP/10000)%10)),
  281. ('0' + ((_OPENMP/1000)%10)),
  282. ('0' + ((_OPENMP/100)%10)),
  283. ('0' + ((_OPENMP/10)%10)),
  284. ('0' + ((_OPENMP/1)%10)),
  285. ']', '\\0' };
  286. int main(void)
  287. {
  288. puts(ompver_str);
  289. return 0;
  290. }
  291. ")
  292. set(OpenMP_Fortran_CHECK_VERSION_SOURCE
  293. "
  294. program omp_ver
  295. @OpenMP_Fortran_INCLUDE_LINE@
  296. integer, parameter :: zero = ichar('0')
  297. integer, parameter :: ompv = openmp_version
  298. character, dimension(24), parameter :: ompver_str =&
  299. (/ 'I', 'N', 'F', 'O', ':', 'O', 'p', 'e', 'n', 'M', 'P', '-',&
  300. 'd', 'a', 't', 'e', '[',&
  301. char(zero + mod(ompv/100000, 10)),&
  302. char(zero + mod(ompv/10000, 10)),&
  303. char(zero + mod(ompv/1000, 10)),&
  304. char(zero + mod(ompv/100, 10)),&
  305. char(zero + mod(ompv/10, 10)),&
  306. char(zero + mod(ompv/1, 10)), ']' /)
  307. print *, ompver_str
  308. end program omp_ver
  309. ")
  310. function(_OPENMP_GET_SPEC_DATE LANG SPEC_DATE)
  311. _OPENMP_WRITE_SOURCE_FILE("${LANG}" "CHECK_VERSION_SOURCE" OpenMPCheckVersion _OPENMP_TEST_SRC)
  312. unset(_includeDirFlags)
  313. if(OpenMP_${LANG}_INCLUDE_DIR)
  314. set(_includeDirFlags "-DINCLUDE_DIRECTORIES:STRING=${OpenMP_${LANG}_INCLUDE_DIR}")
  315. endif()
  316. set(BIN_FILE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/FindOpenMP/ompver_${LANG}.bin")
  317. string(REGEX REPLACE "[-/=+]" "" OPENMP_PLAIN_FLAG "${OPENMP_FLAG}")
  318. try_compile(OpenMP_SPECTEST_${LANG}_${OPENMP_PLAIN_FLAG} "${CMAKE_BINARY_DIR}" "${_OPENMP_TEST_SRC}"
  319. CMAKE_FLAGS "-DCOMPILE_DEFINITIONS:STRING=${OpenMP_${LANG}_FLAGS}" ${_includeDirFlags}
  320. COPY_FILE ${BIN_FILE}
  321. OUTPUT_VARIABLE OpenMP_TRY_COMPILE_OUTPUT)
  322. if(${OpenMP_SPECTEST_${LANG}_${OPENMP_PLAIN_FLAG}})
  323. file(STRINGS ${BIN_FILE} specstr LIMIT_COUNT 1 REGEX "INFO:OpenMP-date")
  324. set(regex_spec_date ".*INFO:OpenMP-date\\[0*([^]]*)\\].*")
  325. if("${specstr}" MATCHES "${regex_spec_date}")
  326. set(${SPEC_DATE} "${CMAKE_MATCH_1}" PARENT_SCOPE)
  327. endif()
  328. else()
  329. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  330. "Detecting ${LANG} OpenMP version failed with the following output:\n${OpenMP_TRY_COMPILE_OUTPUT}\n\n")
  331. endif()
  332. endfunction()
  333. macro(_OPENMP_SET_VERSION_BY_SPEC_DATE LANG)
  334. set(OpenMP_SPEC_DATE_MAP
  335. # Preview versions
  336. "201611=5.0" # OpenMP 5.0 preview 1
  337. # Combined versions, 2.5 onwards
  338. "201811=5.0"
  339. "201511=4.5"
  340. "201307=4.0"
  341. "201107=3.1"
  342. "200805=3.0"
  343. "200505=2.5"
  344. # C/C++ version 2.0
  345. "200203=2.0"
  346. # Fortran version 2.0
  347. "200011=2.0"
  348. # Fortran version 1.1
  349. "199911=1.1"
  350. # C/C++ version 1.0 (there's no 1.1 for C/C++)
  351. "199810=1.0"
  352. # Fortran version 1.0
  353. "199710=1.0"
  354. )
  355. if(MSVC)
  356. list(APPEND OpenMP_SPEC_DATE_MAP "2019=2.0")
  357. endif()
  358. if(OpenMP_${LANG}_SPEC_DATE)
  359. string(REGEX MATCHALL "${OpenMP_${LANG}_SPEC_DATE}=([0-9]+)\\.([0-9]+)" _version_match "${OpenMP_SPEC_DATE_MAP}")
  360. else()
  361. set(_version_match "")
  362. endif()
  363. if(NOT _version_match STREQUAL "")
  364. set(OpenMP_${LANG}_VERSION_MAJOR ${CMAKE_MATCH_1})
  365. set(OpenMP_${LANG}_VERSION_MINOR ${CMAKE_MATCH_2})
  366. set(OpenMP_${LANG}_VERSION "${OpenMP_${LANG}_VERSION_MAJOR}.${OpenMP_${LANG}_VERSION_MINOR}")
  367. else()
  368. unset(OpenMP_${LANG}_VERSION_MAJOR)
  369. unset(OpenMP_${LANG}_VERSION_MINOR)
  370. unset(OpenMP_${LANG}_VERSION)
  371. endif()
  372. unset(_version_match)
  373. unset(OpenMP_SPEC_DATE_MAP)
  374. endmacro()
  375. foreach(LANG IN ITEMS C CXX)
  376. if(CMAKE_${LANG}_COMPILER_LOADED)
  377. if(NOT DEFINED OpenMP_${LANG}_FLAGS OR "${OpenMP_${LANG}_FLAGS}" STREQUAL "NOTFOUND"
  378. OR NOT DEFINED OpenMP_${LANG}_LIB_NAMES OR "${OpenMP_${LANG}_LIB_NAMES}" STREQUAL "NOTFOUND")
  379. _OPENMP_GET_FLAGS("${LANG}" "${LANG}" OpenMP_${LANG}_FLAGS_WORK OpenMP_${LANG}_LIB_NAMES_WORK)
  380. endif()
  381. set(OpenMP_${LANG}_FLAGS "${OpenMP_${LANG}_FLAGS_WORK}"
  382. CACHE STRING "${LANG} compiler flags for OpenMP parallelization")
  383. set(OpenMP_${LANG}_LIB_NAMES "${OpenMP_${LANG}_LIB_NAMES_WORK}"
  384. CACHE STRING "${LANG} compiler libraries for OpenMP parallelization")
  385. mark_as_advanced(OpenMP_${LANG}_FLAGS OpenMP_${LANG}_LIB_NAMES)
  386. endif()
  387. endforeach()
  388. if(CMAKE_Fortran_COMPILER_LOADED)
  389. if(NOT DEFINED OpenMP_Fortran_FLAGS OR "${OpenMP_Fortran_FLAGS}" STREQUAL "NOTFOUND"
  390. OR NOT DEFINED OpenMP_Fortran_LIB_NAMES OR "${OpenMP_Fortran_LIB_NAMES}" STREQUAL "NOTFOUND"
  391. OR NOT DEFINED OpenMP_Fortran_HAVE_OMPLIB_MODULE)
  392. set(OpenMP_Fortran_INCLUDE_LINE "use omp_lib\n implicit none")
  393. _OPENMP_GET_FLAGS("Fortran" "FortranHeader" OpenMP_Fortran_FLAGS_WORK OpenMP_Fortran_LIB_NAMES_WORK)
  394. if(OpenMP_Fortran_FLAGS_WORK)
  395. set(OpenMP_Fortran_HAVE_OMPLIB_MODULE TRUE CACHE BOOL INTERNAL "")
  396. endif()
  397. set(OpenMP_Fortran_FLAGS "${OpenMP_Fortran_FLAGS_WORK}"
  398. CACHE STRING "Fortran compiler flags for OpenMP parallelization")
  399. set(OpenMP_Fortran_LIB_NAMES "${OpenMP_Fortran_LIB_NAMES_WORK}"
  400. CACHE STRING "Fortran compiler libraries for OpenMP parallelization")
  401. mark_as_advanced(OpenMP_Fortran_FLAGS OpenMP_Fortran_LIB_NAMES)
  402. endif()
  403. if(NOT DEFINED OpenMP_Fortran_FLAGS OR "${OpenMP_Fortran_FLAGS}" STREQUAL "NOTFOUND"
  404. OR NOT DEFINED OpenMP_Fortran_LIB_NAMES OR "${OpenMP_Fortran_LIB_NAMES}" STREQUAL "NOTFOUND"
  405. OR NOT DEFINED OpenMP_Fortran_HAVE_OMPLIB_HEADER)
  406. set(OpenMP_Fortran_INCLUDE_LINE "implicit none\n include 'omp_lib.h'")
  407. _OPENMP_GET_FLAGS("Fortran" "FortranModule" OpenMP_Fortran_FLAGS_WORK OpenMP_Fortran_LIB_NAMES_WORK)
  408. if(OpenMP_Fortran_FLAGS_WORK)
  409. set(OpenMP_Fortran_HAVE_OMPLIB_HEADER TRUE CACHE BOOL INTERNAL "")
  410. endif()
  411. set(OpenMP_Fortran_FLAGS "${OpenMP_Fortran_FLAGS_WORK}"
  412. CACHE STRING "Fortran compiler flags for OpenMP parallelization")
  413. set(OpenMP_Fortran_LIB_NAMES "${OpenMP_Fortran_LIB_NAMES}"
  414. CACHE STRING "Fortran compiler libraries for OpenMP parallelization")
  415. endif()
  416. if(OpenMP_Fortran_HAVE_OMPLIB_MODULE)
  417. set(OpenMP_Fortran_INCLUDE_LINE "use omp_lib\n implicit none")
  418. else()
  419. set(OpenMP_Fortran_INCLUDE_LINE "implicit none\n include 'omp_lib.h'")
  420. endif()
  421. endif()
  422. if(NOT OpenMP_FIND_COMPONENTS)
  423. set(OpenMP_FINDLIST C CXX Fortran)
  424. else()
  425. set(OpenMP_FINDLIST ${OpenMP_FIND_COMPONENTS})
  426. endif()
  427. unset(_OpenMP_MIN_VERSION)
  428. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  429. foreach(LANG IN LISTS OpenMP_FINDLIST)
  430. if(CMAKE_${LANG}_COMPILER_LOADED)
  431. if (NOT OpenMP_${LANG}_SPEC_DATE AND OpenMP_${LANG}_FLAGS)
  432. _OPENMP_GET_SPEC_DATE("${LANG}" OpenMP_${LANG}_SPEC_DATE_INTERNAL)
  433. set(OpenMP_${LANG}_SPEC_DATE "${OpenMP_${LANG}_SPEC_DATE_INTERNAL}" CACHE
  434. INTERNAL "${LANG} compiler's OpenMP specification date")
  435. _OPENMP_SET_VERSION_BY_SPEC_DATE("${LANG}")
  436. endif()
  437. set(OpenMP_${LANG}_FIND_QUIETLY ${OpenMP_FIND_QUIETLY})
  438. set(OpenMP_${LANG}_FIND_REQUIRED ${OpenMP_FIND_REQUIRED})
  439. set(OpenMP_${LANG}_FIND_VERSION ${OpenMP_FIND_VERSION})
  440. set(OpenMP_${LANG}_FIND_VERSION_EXACT ${OpenMP_FIND_VERSION_EXACT})
  441. set(_OPENMP_${LANG}_REQUIRED_VARS OpenMP_${LANG}_FLAGS)
  442. if("${OpenMP_${LANG}_LIB_NAMES}" STREQUAL "NOTFOUND")
  443. set(_OPENMP_${LANG}_REQUIRED_LIB_VARS OpenMP_${LANG}_LIB_NAMES)
  444. else()
  445. foreach(_OPENMP_IMPLICIT_LIB IN LISTS OpenMP_${LANG}_LIB_NAMES)
  446. list(APPEND _OPENMP_${LANG}_REQUIRED_LIB_VARS OpenMP_${_OPENMP_IMPLICIT_LIB}_LIBRARY)
  447. endforeach()
  448. endif()
  449. find_package_handle_standard_args(OpenMP_${LANG}
  450. REQUIRED_VARS OpenMP_${LANG}_FLAGS ${_OPENMP_${LANG}_REQUIRED_LIB_VARS}
  451. VERSION_VAR OpenMP_${LANG}_VERSION
  452. )
  453. if(OpenMP_${LANG}_FOUND)
  454. if(DEFINED OpenMP_${LANG}_VERSION)
  455. if(NOT _OpenMP_MIN_VERSION OR _OpenMP_MIN_VERSION VERSION_GREATER OpenMP_${LANG}_VERSION)
  456. set(_OpenMP_MIN_VERSION OpenMP_${LANG}_VERSION)
  457. endif()
  458. endif()
  459. set(OpenMP_${LANG}_LIBRARIES "")
  460. foreach(_OPENMP_IMPLICIT_LIB IN LISTS OpenMP_${LANG}_LIB_NAMES)
  461. list(APPEND OpenMP_${LANG}_LIBRARIES "${OpenMP_${_OPENMP_IMPLICIT_LIB}_LIBRARY}")
  462. endforeach()
  463. if(OpenMP_${LANG}_INCLUDE_DIR)
  464. set(OpenMP_${LANG}_INCLUDE_DIRS ${OpenMP_${LANG}_INCLUDE_DIR})
  465. else()
  466. set(OpenMP_${LANG}_INCLUDE_DIRS "")
  467. endif()
  468. if(NOT TARGET OpenMP::OpenMP_${LANG})
  469. add_library(OpenMP::OpenMP_${LANG} INTERFACE IMPORTED)
  470. endif()
  471. if(OpenMP_${LANG}_FLAGS)
  472. separate_arguments(_OpenMP_${LANG}_OPTIONS NATIVE_COMMAND "${OpenMP_${LANG}_FLAGS}")
  473. set_property(TARGET OpenMP::OpenMP_${LANG} PROPERTY
  474. INTERFACE_COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:${LANG}>:${_OpenMP_${LANG}_OPTIONS}>")
  475. unset(_OpenMP_${LANG}_OPTIONS)
  476. endif()
  477. if(OpenMP_${LANG}_INCLUDE_DIRS)
  478. set_property(TARGET OpenMP::OpenMP_${LANG} PROPERTY
  479. INTERFACE_INCLUDE_DIRECTORIES "$<BUILD_INTERFACE:${OpenMP_${LANG}_INCLUDE_DIRS}>")
  480. endif()
  481. if(OpenMP_${LANG}_LIBRARIES)
  482. set_property(TARGET OpenMP::OpenMP_${LANG} PROPERTY
  483. INTERFACE_LINK_LIBRARIES "${OpenMP_${LANG}_LIBRARIES}")
  484. endif()
  485. endif()
  486. endif()
  487. endforeach()
  488. unset(_OpenMP_REQ_VARS)
  489. foreach(LANG IN ITEMS C CXX Fortran)
  490. if((NOT OpenMP_FIND_COMPONENTS AND CMAKE_${LANG}_COMPILER_LOADED) OR LANG IN_LIST OpenMP_FIND_COMPONENTS)
  491. list(APPEND _OpenMP_REQ_VARS "OpenMP_${LANG}_FOUND")
  492. endif()
  493. endforeach()
  494. find_package_handle_standard_args(OpenMP
  495. REQUIRED_VARS ${_OpenMP_REQ_VARS}
  496. VERSION_VAR ${_OpenMP_MIN_VERSION}
  497. HANDLE_COMPONENTS)
  498. set(OPENMP_FOUND ${OpenMP_FOUND})
  499. if(CMAKE_Fortran_COMPILER_LOADED AND OpenMP_Fortran_FOUND)
  500. if(NOT DEFINED OpenMP_Fortran_HAVE_OMPLIB_MODULE)
  501. set(OpenMP_Fortran_HAVE_OMPLIB_MODULE FALSE CACHE BOOL INTERNAL "")
  502. endif()
  503. if(NOT DEFINED OpenMP_Fortran_HAVE_OMPLIB_HEADER)
  504. set(OpenMP_Fortran_HAVE_OMPLIB_HEADER FALSE CACHE BOOL INTERNAL "")
  505. endif()
  506. endif()
  507. if(NOT ( CMAKE_C_COMPILER_LOADED OR CMAKE_CXX_COMPILER_LOADED OR CMAKE_Fortran_COMPILER_LOADED ))
  508. message(SEND_ERROR "FindOpenMP requires the C, CXX or Fortran languages to be enabled")
  509. endif()
  510. unset(OpenMP_C_CXX_TEST_SOURCE)
  511. unset(OpenMP_Fortran_TEST_SOURCE)
  512. unset(OpenMP_C_CXX_CHECK_VERSION_SOURCE)
  513. unset(OpenMP_Fortran_CHECK_VERSION_SOURCE)
  514. unset(OpenMP_Fortran_INCLUDE_LINE)
  515. cmake_policy(POP)