FindOpenMP.cmake 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. set(_OPENMP_REQUIRED_VARS)
  27. function(_OPENMP_FLAG_CANDIDATES LANG)
  28. set(OpenMP_FLAG_CANDIDATES
  29. #GNU
  30. "-fopenmp"
  31. #Microsoft Visual Studio
  32. "/openmp"
  33. #Intel windows
  34. "-Qopenmp"
  35. #PathScale, Intel
  36. "-openmp"
  37. #Empty, if compiler automatically accepts openmp
  38. " "
  39. #Sun
  40. "-xopenmp"
  41. #HP
  42. "+Oopenmp"
  43. #IBM XL C/c++
  44. "-qsmp"
  45. #Portland Group, MIPSpro
  46. "-mp"
  47. )
  48. set(OMP_FLAG_GNU "-fopenmp")
  49. set(OMP_FLAG_HP "+Oopenmp")
  50. if(WIN32)
  51. set(OMP_FLAG_Intel "-Qopenmp")
  52. else()
  53. set(OMP_FLAG_Intel "-openmp")
  54. endif()
  55. set(OMP_FLAG_MIPSpro "-mp")
  56. set(OMP_FLAG_MSVC "/openmp")
  57. set(OMP_FLAG_PathScale "-openmp")
  58. set(OMP_FLAG_PGI "-mp")
  59. set(OMP_FLAG_SunPro "-xopenmp")
  60. set(OMP_FLAG_XL "-qsmp")
  61. # Move the flag that matches the compiler to the head of the list,
  62. # this is faster and doesn't clutter the output that much. If that
  63. # flag doesn't work we will still try all.
  64. if(OMP_FLAG_${CMAKE_${LANG}_COMPILER_ID})
  65. list(REMOVE_ITEM OpenMP_FLAG_CANDIDATES "${OMP_FLAG_${CMAKE_${LANG}_COMPILER_ID}}")
  66. list(INSERT OpenMP_FLAG_CANDIDATES 0 "${OMP_FLAG_${CMAKE_${LANG}_COMPILER_ID}}")
  67. endif()
  68. set(OpenMP_${LANG}_FLAG_CANDIDATES "${OpenMP_FLAG_CANDIDATES}" PARENT_SCOPE)
  69. endfunction(_OPENMP_FLAG_CANDIDATES)
  70. # sample openmp source code to test
  71. set(OpenMP_C_TEST_SOURCE
  72. "
  73. #include <omp.h>
  74. int main() {
  75. #ifdef _OPENMP
  76. return 0;
  77. #else
  78. breaks_on_purpose
  79. #endif
  80. }
  81. ")
  82. # check c compiler
  83. if(CMAKE_C_COMPILER_LOADED)
  84. # if these are set then do not try to find them again,
  85. # by avoiding any try_compiles for the flags
  86. if(OpenMP_C_FLAGS)
  87. unset(OpenMP_C_FLAG_CANDIDATES)
  88. else()
  89. _OPENMP_FLAG_CANDIDATES("C")
  90. include(CheckCSourceCompiles)
  91. endif()
  92. foreach(FLAG ${OpenMP_C_FLAG_CANDIDATES})
  93. set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
  94. set(CMAKE_REQUIRED_FLAGS "${FLAG}")
  95. unset(OpenMP_FLAG_DETECTED CACHE)
  96. message(STATUS "Try OpenMP C flag = [${FLAG}]")
  97. check_c_source_compiles("${OpenMP_C_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
  98. set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
  99. if(OpenMP_FLAG_DETECTED)
  100. set(OpenMP_C_FLAGS_INTERNAL "${FLAG}")
  101. break()
  102. endif(OpenMP_FLAG_DETECTED)
  103. endforeach(FLAG ${OpenMP_C_FLAG_CANDIDATES})
  104. set(OpenMP_C_FLAGS "${OpenMP_C_FLAGS_INTERNAL}"
  105. CACHE STRING "C compiler flags for OpenMP parallization")
  106. list(APPEND _OPENMP_REQUIRED_VARS OpenMP_C_FLAGS)
  107. unset(OpenMP_C_FLAG_CANDIDATES)
  108. endif()
  109. # check cxx compiler
  110. if(CMAKE_CXX_COMPILER_LOADED)
  111. # if these are set then do not try to find them again,
  112. # by avoiding any try_compiles for the flags
  113. if(OpenMP_CXX_FLAGS)
  114. unset(OpenMP_CXX_FLAG_CANDIDATES)
  115. else()
  116. _OPENMP_FLAG_CANDIDATES("CXX")
  117. include(CheckCXXSourceCompiles)
  118. # use the same source for CXX as C for now
  119. set(OpenMP_CXX_TEST_SOURCE ${OpenMP_C_TEST_SOURCE})
  120. endif()
  121. foreach(FLAG ${OpenMP_CXX_FLAG_CANDIDATES})
  122. set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
  123. set(CMAKE_REQUIRED_FLAGS "${FLAG}")
  124. unset(OpenMP_FLAG_DETECTED CACHE)
  125. message(STATUS "Try OpenMP CXX flag = [${FLAG}]")
  126. check_cxx_source_compiles("${OpenMP_CXX_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
  127. set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
  128. if(OpenMP_FLAG_DETECTED)
  129. set(OpenMP_CXX_FLAGS_INTERNAL "${FLAG}")
  130. break()
  131. endif(OpenMP_FLAG_DETECTED)
  132. endforeach(FLAG ${OpenMP_CXX_FLAG_CANDIDATES})
  133. set(OpenMP_CXX_FLAGS "${OpenMP_CXX_FLAGS_INTERNAL}"
  134. CACHE STRING "C++ compiler flags for OpenMP parallization")
  135. list(APPEND _OPENMP_REQUIRED_VARS OpenMP_CXX_FLAGS)
  136. unset(OpenMP_CXX_FLAG_CANDIDATES)
  137. unset(OpenMP_CXX_TEST_SOURCE)
  138. endif()
  139. if(_OPENMP_REQUIRED_VARS)
  140. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  141. find_package_handle_standard_args(OpenMP
  142. REQUIRED_VARS ${_OPENMP_REQUIRED_VARS})
  143. mark_as_advanced(${_OPENMP_REQUIRED_VARS})
  144. unset(_OPENMP_REQUIRED_VARS)
  145. else()
  146. message(SEND_ERROR "FindOpenMP requires C or CXX language to be enabled")
  147. endif()