FindOpenMP.cmake 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. # Copyright 2008, 2009 <André Rigland Brodtkorb> [email protected]
  13. #
  14. # Redistribution AND use is allowed according to the terms of the New
  15. # BSD license.
  16. # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
  17. include(CheckCSourceCompiles)
  18. include(CheckCXXSourceCompiles)
  19. include(FindPackageHandleStandardArgs)
  20. set(OpenMP_C_FLAG_CANDIDATES
  21. #Gnu
  22. "-fopenmp"
  23. #Microsoft Visual Studio
  24. "/openmp"
  25. #Intel windows
  26. "-Qopenmp"
  27. #Intel
  28. "-openmp"
  29. #Empty, if compiler automatically accepts openmp
  30. " "
  31. #Sun
  32. "-xopenmp"
  33. #HP
  34. "+Oopenmp"
  35. #IBM XL C/c++
  36. "-qsmp"
  37. #Portland Group
  38. "-mp"
  39. )
  40. set(OpenMP_CXX_FLAG_CANDIDATES ${OpenMP_C_FLAG_CANDIDATES})
  41. # sample openmp source code to test
  42. set(OpenMP_C_TEST_SOURCE
  43. "
  44. #include <omp.h>
  45. int main() {
  46. #ifdef _OPENMP
  47. return 0;
  48. #else
  49. breaks_on_purpose
  50. #endif
  51. }
  52. ")
  53. # use the same source for CXX as C for now
  54. set(OpenMP_CXX_TEST_SOURCE ${OpenMP_C_TEST_SOURCE})
  55. # if these are set then do not try to find them again,
  56. # by avoiding any try_compiles for the flags
  57. if(DEFINED OpenMP_C_FLAGS AND DEFINED OpenMP_CXX_FLAGS)
  58. set(OpenMP_C_FLAG_CANDIDATES)
  59. set(OpenMP_CXX_FLAG_CANDIDATES)
  60. endif(DEFINED OpenMP_C_FLAGS AND DEFINED OpenMP_CXX_FLAGS)
  61. # check c compiler
  62. foreach(FLAG ${OpenMP_C_FLAG_CANDIDATES})
  63. set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
  64. set(CMAKE_REQUIRED_FLAGS "${FLAG}")
  65. unset(OpenMP_FLAG_DETECTED CACHE)
  66. message(STATUS "Try OpenMP C flag = [${FLAG}]")
  67. check_c_source_compiles("${OpenMP_CXX_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
  68. set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
  69. if(OpenMP_FLAG_DETECTED)
  70. set(OpenMP_C_FLAGS_INTERNAL "${FLAG}")
  71. break()
  72. endif(OpenMP_FLAG_DETECTED)
  73. endforeach(FLAG ${OpenMP_C_FLAG_CANDIDATES})
  74. # check cxx compiler
  75. foreach(FLAG ${OpenMP_CXX_FLAG_CANDIDATES})
  76. set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
  77. set(CMAKE_REQUIRED_FLAGS "${FLAG}")
  78. unset(OpenMP_FLAG_DETECTED CACHE)
  79. message(STATUS "Try OpenMP CXX flag = [${FLAG}]")
  80. check_cxx_source_compiles("${OpenMP_C_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
  81. set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
  82. if(OpenMP_FLAG_DETECTED)
  83. set(OpenMP_CXX_FLAGS_INTERNAL "${FLAG}")
  84. break()
  85. endif(OpenMP_FLAG_DETECTED)
  86. endforeach(FLAG ${OpenMP_CXX_FLAG_CANDIDATES})
  87. set(OpenMP_C_FLAGS "${OpenMP_C_FLAGS_INTERNAL}"
  88. CACHE STRING "C compiler flags for OpenMP parallization")
  89. set(OpenMP_CXX_FLAGS "${OpenMP_CXX_FLAGS_INTERNAL}"
  90. CACHE STRING "C++ compiler flags for OpenMP parallization")
  91. # handle the standard arguments for find_package
  92. find_package_handle_standard_args(OpenMP DEFAULT_MSG
  93. OpenMP_C_FLAGS OpenMP_CXX_FLAGS )
  94. mark_as_advanced(
  95. OpenMP_C_FLAGS
  96. OpenMP_CXX_FLAGS
  97. )