FindOpenMP.cmake 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #
  16. # Distributed under the OSI-approved BSD License (the "License");
  17. # see accompanying file Copyright.txt for details.
  18. #
  19. # This software is distributed WITHOUT ANY WARRANTY; without even the
  20. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  21. # See the License for more information.
  22. #=============================================================================
  23. # (To distribute this file outside of CMake, substitute the full
  24. # License text for the above reference.)
  25. include(CheckCSourceCompiles)
  26. include(CheckCXXSourceCompiles)
  27. include(FindPackageHandleStandardArgs)
  28. set(OpenMP_C_FLAG_CANDIDATES
  29. #Gnu
  30. "-fopenmp"
  31. #Microsoft Visual Studio
  32. "/openmp"
  33. #Intel windows
  34. "-Qopenmp"
  35. #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
  46. "-mp"
  47. )
  48. set(OpenMP_CXX_FLAG_CANDIDATES ${OpenMP_C_FLAG_CANDIDATES})
  49. # sample openmp source code to test
  50. set(OpenMP_C_TEST_SOURCE
  51. "
  52. #include <omp.h>
  53. int main() {
  54. #ifdef _OPENMP
  55. return 0;
  56. #else
  57. breaks_on_purpose
  58. #endif
  59. }
  60. ")
  61. # use the same source for CXX as C for now
  62. set(OpenMP_CXX_TEST_SOURCE ${OpenMP_C_TEST_SOURCE})
  63. # if these are set then do not try to find them again,
  64. # by avoiding any try_compiles for the flags
  65. if(DEFINED OpenMP_C_FLAGS AND DEFINED OpenMP_CXX_FLAGS)
  66. set(OpenMP_C_FLAG_CANDIDATES)
  67. set(OpenMP_CXX_FLAG_CANDIDATES)
  68. endif(DEFINED OpenMP_C_FLAGS AND DEFINED OpenMP_CXX_FLAGS)
  69. # check c compiler
  70. foreach(FLAG ${OpenMP_C_FLAG_CANDIDATES})
  71. set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
  72. set(CMAKE_REQUIRED_FLAGS "${FLAG}")
  73. unset(OpenMP_FLAG_DETECTED CACHE)
  74. message(STATUS "Try OpenMP C flag = [${FLAG}]")
  75. check_c_source_compiles("${OpenMP_CXX_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
  76. set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
  77. if(OpenMP_FLAG_DETECTED)
  78. set(OpenMP_C_FLAGS_INTERNAL "${FLAG}")
  79. break()
  80. endif(OpenMP_FLAG_DETECTED)
  81. endforeach(FLAG ${OpenMP_C_FLAG_CANDIDATES})
  82. # check cxx compiler
  83. foreach(FLAG ${OpenMP_CXX_FLAG_CANDIDATES})
  84. set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
  85. set(CMAKE_REQUIRED_FLAGS "${FLAG}")
  86. unset(OpenMP_FLAG_DETECTED CACHE)
  87. message(STATUS "Try OpenMP CXX flag = [${FLAG}]")
  88. check_cxx_source_compiles("${OpenMP_C_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
  89. set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
  90. if(OpenMP_FLAG_DETECTED)
  91. set(OpenMP_CXX_FLAGS_INTERNAL "${FLAG}")
  92. break()
  93. endif(OpenMP_FLAG_DETECTED)
  94. endforeach(FLAG ${OpenMP_CXX_FLAG_CANDIDATES})
  95. set(OpenMP_C_FLAGS "${OpenMP_C_FLAGS_INTERNAL}"
  96. CACHE STRING "C compiler flags for OpenMP parallization")
  97. set(OpenMP_CXX_FLAGS "${OpenMP_CXX_FLAGS_INTERNAL}"
  98. CACHE STRING "C++ compiler flags for OpenMP parallization")
  99. # handle the standard arguments for find_package
  100. find_package_handle_standard_args(OpenMP DEFAULT_MSG
  101. OpenMP_C_FLAGS OpenMP_CXX_FLAGS )
  102. mark_as_advanced(
  103. OpenMP_C_FLAGS
  104. OpenMP_CXX_FLAGS
  105. )