FindOpenMP.cmake 5.3 KB

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