Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Modules / FindOpenMP.cmake
blob848f57ff9f192ed54f710e25828c34a0536073d4
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 
17 # BSD license. 
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
26   #Gnu
27   "-fopenmp"
28   #Microsoft Visual Studio
29   "/openmp"
30   #Intel windows
31   "-Qopenmp" 
32   #Intel
33   "-openmp" 
34   #Empty, if compiler automatically accepts openmp
35   " "
36   #Sun
37   "-xopenmp"
38   #HP
39   "+Oopenmp"
40   #IBM XL C/c++
41   "-qsmp"
42   #Portland Group
43   "-mp"
45 set(OpenMP_CXX_FLAG_CANDIDATES ${OpenMP_C_FLAG_CANDIDATES})
47 # sample openmp source code to test
48 set(OpenMP_C_TEST_SOURCE 
50 #include <omp.h>
51 int main() { 
52 #ifdef _OPENMP
53   return 0; 
54 #else
55   breaks_on_purpose
56 #endif
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)
68 # check c compiler
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}")
78     break()
79   endif(OpenMP_FLAG_DETECTED) 
80 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})
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 )
105 mark_as_advanced(
106   OpenMP_C_FLAGS
107   OpenMP_CXX_FLAGS