FindOpenMP.cmake 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # - Finds OpenMP support
  2. # This module can be used to detect OpenMP support in a compiler.
  3. # If the compiler supports OpenMP, the flags required to compile with
  4. # openmp support are set.
  5. #
  6. # The following variables are set:
  7. # OpenMP_C_FLAGS - flags to add to the C compiler for OpenMP support
  8. # OpenMP_CXX_FLAGS - flags to add to the CXX compiler for OpenMP support
  9. # OPENMP_FOUND - true if openmp is detected
  10. #
  11. # Supported compilers can be found at http://openmp.org/wp/openmp-compilers/
  12. #=============================================================================
  13. # Copyright 2009 Kitware, Inc.
  14. # Copyright 2008-2009 André Rigland Brodtkorb <[email protected]>
  15. # Copyright 2012 Rolf Eike Beer <[email protected]>
  16. #
  17. # Distributed under the OSI-approved BSD License (the "License");
  18. # see accompanying file Copyright.txt for details.
  19. #
  20. # This software is distributed WITHOUT ANY WARRANTY; without even the
  21. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  22. # See the License for more information.
  23. #=============================================================================
  24. # (To distribute this file outside of CMake, substitute the full
  25. # License text for the above reference.)
  26. get_property(_ENABLED_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
  27. list(FIND _ENABLED_LANGUAGES "C" _HAVE_LANGUAGE_C)
  28. list(FIND _ENABLED_LANGUAGES "CXX" _HAVE_LANGUAGE_CXX)
  29. unset(_ENABLED_LANGUAGES)
  30. set(_OPENMP_REQUIRED_VARS)
  31. function(_OPENMP_FLAG_CANDIDATES LANG)
  32. set(OpenMP_FLAG_CANDIDATES
  33. #GNU
  34. "-fopenmp"
  35. #Microsoft Visual Studio
  36. "/openmp"
  37. #Intel windows
  38. "-Qopenmp"
  39. #PathScale, Intel
  40. "-openmp"
  41. #Empty, if compiler automatically accepts openmp
  42. " "
  43. #Sun
  44. "-xopenmp"
  45. #HP
  46. "+Oopenmp"
  47. #IBM XL C/c++
  48. "-qsmp"
  49. #Portland Group, MIPSpro
  50. "-mp"
  51. )
  52. set(OMP_FLAG_GNU "-fopenmp")
  53. set(OMP_FLAG_HP "+Oopenmp")
  54. if(WIN32)
  55. set(OMP_FLAG_Intel "-Qopenmp")
  56. else()
  57. set(OMP_FLAG_Intel "-openmp")
  58. endif()
  59. set(OMP_FLAG_MIPSpro "-mp")
  60. set(OMP_FLAG_MSVC "/openmp")
  61. set(OMP_FLAG_PathScale "-openmp")
  62. set(OMP_FLAG_PGI "-mp")
  63. set(OMP_FLAG_SunPro "-xopenmp")
  64. set(OMP_FLAG_XL "-qsmp")
  65. # Move the flag that matches the compiler to the head of the list,
  66. # this is faster and doesn't clutter the output that much. If that
  67. # flag doesn't work we will still try all.
  68. if(OMP_FLAG_${CMAKE_${LANG}_COMPILER_ID})
  69. list(REMOVE_ITEM OpenMP_FLAG_CANDIDATES "${OMP_FLAG_${CMAKE_${LANG}_COMPILER_ID}}")
  70. list(INSERT OpenMP_FLAG_CANDIDATES 0 "${OMP_FLAG_${CMAKE_${LANG}_COMPILER_ID}}")
  71. endif()
  72. set(OpenMP_${LANG}_FLAG_CANDIDATES "${OpenMP_FLAG_CANDIDATES}" PARENT_SCOPE)
  73. endfunction(_OPENMP_FLAG_CANDIDATES)
  74. # sample openmp source code to test
  75. set(OpenMP_C_TEST_SOURCE
  76. "
  77. #include <omp.h>
  78. int main() {
  79. #ifdef _OPENMP
  80. return 0;
  81. #else
  82. breaks_on_purpose
  83. #endif
  84. }
  85. ")
  86. # check c compiler
  87. if(NOT _HAVE_LANGUAGE_C EQUAL -1)
  88. # if these are set then do not try to find them again,
  89. # by avoiding any try_compiles for the flags
  90. if(OpenMP_C_FLAGS)
  91. unset(OpenMP_C_FLAG_CANDIDATES)
  92. else()
  93. _OPENMP_FLAG_CANDIDATES("C")
  94. include(CheckCSourceCompiles)
  95. endif()
  96. foreach(FLAG ${OpenMP_C_FLAG_CANDIDATES})
  97. set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
  98. set(CMAKE_REQUIRED_FLAGS "${FLAG}")
  99. unset(OpenMP_FLAG_DETECTED CACHE)
  100. message(STATUS "Try OpenMP C flag = [${FLAG}]")
  101. check_c_source_compiles("${OpenMP_C_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
  102. set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
  103. if(OpenMP_FLAG_DETECTED)
  104. set(OpenMP_C_FLAGS_INTERNAL "${FLAG}")
  105. break()
  106. endif(OpenMP_FLAG_DETECTED)
  107. endforeach(FLAG ${OpenMP_C_FLAG_CANDIDATES})
  108. set(OpenMP_C_FLAGS "${OpenMP_C_FLAGS_INTERNAL}"
  109. CACHE STRING "C compiler flags for OpenMP parallization")
  110. list(APPEND _OPENMP_REQUIRED_VARS OpenMP_C_FLAGS)
  111. unset(OpenMP_C_FLAG_CANDIDATES)
  112. endif()
  113. # check cxx compiler
  114. if(NOT _HAVE_LANGUAGE_CXX EQUAL -1)
  115. # if these are set then do not try to find them again,
  116. # by avoiding any try_compiles for the flags
  117. if(OpenMP_CXX_FLAGS)
  118. unset(OpenMP_CXX_FLAG_CANDIDATES)
  119. else()
  120. _OPENMP_FLAG_CANDIDATES("CXX")
  121. include(CheckCXXSourceCompiles)
  122. # use the same source for CXX as C for now
  123. set(OpenMP_CXX_TEST_SOURCE ${OpenMP_C_TEST_SOURCE})
  124. endif()
  125. foreach(FLAG ${OpenMP_CXX_FLAG_CANDIDATES})
  126. set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
  127. set(CMAKE_REQUIRED_FLAGS "${FLAG}")
  128. unset(OpenMP_FLAG_DETECTED CACHE)
  129. message(STATUS "Try OpenMP CXX flag = [${FLAG}]")
  130. check_cxx_source_compiles("${OpenMP_CXX_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
  131. set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
  132. if(OpenMP_FLAG_DETECTED)
  133. set(OpenMP_CXX_FLAGS_INTERNAL "${FLAG}")
  134. break()
  135. endif(OpenMP_FLAG_DETECTED)
  136. endforeach(FLAG ${OpenMP_CXX_FLAG_CANDIDATES})
  137. set(OpenMP_CXX_FLAGS "${OpenMP_CXX_FLAGS_INTERNAL}"
  138. CACHE STRING "C++ compiler flags for OpenMP parallization")
  139. list(APPEND _OPENMP_REQUIRED_VARS OpenMP_CXX_FLAGS)
  140. unset(OpenMP_CXX_FLAG_CANDIDATES)
  141. unset(OpenMP_CXX_TEST_SOURCE)
  142. endif()
  143. unset(_HAVE_LANGUAGE_C)
  144. unset(_HAVE_LANGUAGE_CXX)
  145. if(_OPENMP_REQUIRED_VARS)
  146. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  147. find_package_handle_standard_args(OpenMP
  148. REQUIRED_VARS ${_OPENMP_REQUIRED_VARS})
  149. mark_as_advanced(${_OPENMP_REQUIRED_VARS})
  150. unset(_OPENMP_REQUIRED_VARS)
  151. else()
  152. message(SEND_ERROR "FindOpenMP requires C or CXX language to be enabled")
  153. endif()