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.
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
11 # Supported compilers can be found at http://openmp.org/wp/openmp-compilers/
14 # Copyright 2008, 2009 <André Rigland Brodtkorb> Andre.Brodtkorb@ifi.uio.no
16 # Redistribution AND use is allowed according to the terms of the New
18 # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
21 include(CheckCSourceCompiles)
22 include(CheckCXXSourceCompiles)
23 include(FindPackageHandleStandardArgs)
25 set(OpenMP_C_FLAG_CANDIDATES
28 #Microsoft Visual Studio
34 #Empty, if compiler automatically accepts openmp
45 set(OpenMP_CXX_FLAG_CANDIDATES ${OpenMP_C_FLAG_CANDIDATES})
47 # sample openmp source code to test
48 set(OpenMP_C_TEST_SOURCE
59 # use the same source for CXX as C for now
60 set(OpenMP_CXX_TEST_SOURCE ${OpenMP_C_TEST_SOURCE})
61 # if these are set then do not try to find them again,
62 # by avoiding any try_compiles for the flags
63 if(DEFINED OpenMP_C_FLAGS AND DEFINED OpenMP_CXX_FLAGS)
64 set(OpenMP_C_FLAG_CANDIDATES)
65 set(OpenMP_CXX_FLAG_CANDIDATES)
66 endif(DEFINED OpenMP_C_FLAGS AND DEFINED OpenMP_CXX_FLAGS)
69 foreach(FLAG ${OpenMP_C_FLAG_CANDIDATES})
70 set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
71 set(CMAKE_REQUIRED_FLAGS "${FLAG}")
72 unset(OpenMP_FLAG_DETECTED CACHE)
73 message(STATUS "Try OpenMP C flag = [${FLAG}]")
74 check_c_source_compiles("${OpenMP_CXX_TEST_SOURCE}" OpenMP_FLAG_DETECTED)
75 set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
76 if(OpenMP_FLAG_DETECTED)
77 set(OpenMP_C_FLAGS_INTERNAL "${FLAG}")
79 endif(OpenMP_FLAG_DETECTED)
80 endforeach(FLAG ${OpenMP_C_FLAG_CANDIDATES})
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}")
93 endif(OpenMP_FLAG_DETECTED)
94 endforeach(FLAG ${OpenMP_CXX_FLAG_CANDIDATES})
96 set(OpenMP_C_FLAGS "${OpenMP_C_FLAGS_INTERNAL}"
97 CACHE STRING "C compiler flags for OpenMP parallization")
99 set(OpenMP_CXX_FLAGS "${OpenMP_CXX_FLAGS_INTERNAL}"
100 CACHE STRING "C++ compiler flags for OpenMP parallization")
101 # handle the standard arguments for find_package
102 find_package_handle_standard_args(OpenMP DEFAULT_MSG
103 OpenMP_C_FLAGS OpenMP_CXX_FLAGS )