Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Modules / FindMPI.cmake
blob815cda8ab491b1abce379496f99d90c482cad6c4
1 # This module looks for the Message Passing Interface (MPI).
3 # This module will set the following variables:
4 #   MPI_FOUND                  TRUE if we have found MPI
5 #   MPI_COMPILE_FLAGS          Compilation flags for MPI programs
6 #   MPI_INCLUDE_PATH           Include path for MPI header
7 #   MPI_LINK_FLAGS             Linking flags for MPI programs
8 #   MPI_LIBRARIES              Libraries to link MPI programs against
9 #   MPI_LIBRARY                Deprecated; first MPI library to link against
10 #   MPI_EXTRA_LIBRARY          Deprecated; second MPI library to link against
11 #   MPIEXEC                    Executable for running MPI programs
13 # This module will attempt to auto-detect these settings, first by
14 # looking for a C++ MPI driver (e.g., mpic++, mpiCC, or mpicxx; set by
15 # MPICXX) and later by checking common MPI paths and library names.
17 # Try to find the MPI driver program
18 find_program(MPICXX 
19   NAMES mpic++ mpicxx mpiCC
20   DOC "MPI C++ compiler. Used only to detect MPI compilation flags.")
21 mark_as_advanced(MPICXX)
23 find_program(MPIEXEC
24   NAMES mpiexec mpirun
25   DOC "Executable for running MPI programs.")
26 mark_as_advanced(MPIEXEC)
28 if (NOT OLD_MPICXX STREQUAL MPICXX)
29   set(MPI_FORCE_RECONFIGURE TRUE)
30   set(OLD_MPICXX ${MPICXX} CACHE INTERNAL "Previous value of MPICXX" FORCE)
31 endif (NOT OLD_MPICXX STREQUAL MPICXX)
32   
33 if (NOT MPICXX)
34   # If there is no MPI C++ compiler, we force ourselves to configure
35   # MPI the old way.
36   set(MPI_FORCE_RECONFIGURE TRUE)
37 endif (NOT MPICXX)
39 if (MPICXX)
40   # Check whether the -showme:compile option works. This indicates
41   # that we have either Open MPI or a newer version of LAM-MPI, and
42   # implies that -showme:link will also work.
43   exec_program(${MPICXX} 
44     ARGS -showme:compile 
45     OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
46     RETURN_VALUE MPICXX_RETURN)
48   if (MPICXX_RETURN EQUAL 0)
49     # If we appear to have -showme:compile, then we should also have
50     # -showme:link. Try it.
51     exec_program(${MPICXX} 
52       ARGS -showme:link
53       OUTPUT_VARIABLE MPI_LINK_CMDLINE
54       RETURN_VALUE MPICXX_RETURN)
55   endif (MPICXX_RETURN EQUAL 0)
57   if (MPICXX_RETURN EQUAL 0)
58     # Do nothing: we have our command lines now
59   else (MPICXX_RETURN EQUAL 0)
60     # Older versions of LAM-MPI have "-showme". Try it.
61     exec_program(${MPICXX} 
62       ARGS -showme
63       OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
64       RETURN_VALUE MPICXX_RETURN)
65   endif (MPICXX_RETURN EQUAL 0)  
67   if (MPICXX_RETURN EQUAL 0)
68     # Do nothing: we have our command lines now
69   else (MPICXX_RETURN EQUAL 0)
70     # MPICH uses "-show". Try it.
71     exec_program(${MPICXX} 
72       ARGS -show
73       OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
74       RETURN_VALUE MPICXX_RETURN)
75   endif (MPICXX_RETURN EQUAL 0)  
77   if (MPICXX_RETURN EQUAL 0)
78     # We have our command lines, but we might need to copy
79     # MPI_COMPILE_CMDLINE into MPI_LINK_CMDLINE, if the underlying
80     if (NOT MPI_LINK_CMDLINE)
81       SET(MPI_LINK_CMDLINE ${MPI_COMPILE_CMDLINE})
82     endif (NOT MPI_LINK_CMDLINE)
83   else (MPICXX_RETURN EQUAL 0)
84     message(STATUS "Unable to determine MPI from MPI driver ${MPICXX}")
85   endif (MPICXX_RETURN EQUAL 0)
86 endif (MPICXX)
88 if (NOT MPI_FORCE_RECONFIGURE)
89   # We don't actually have to reconfigure anything
90 elseif (MPI_COMPILE_CMDLINE)
91   # Extract compile flags from the compile command line.
92   string(REGEX MATCHALL "-D([^\" ]+|\"[^\"]+\")" MPI_ALL_COMPILE_FLAGS ${MPI_COMPILE_CMDLINE})
93   set(MPI_COMPILE_FLAGS_WORK)
94   foreach(FLAG ${MPI_ALL_COMPILE_FLAGS})
95     if (MPI_COMPILE_FLAGS_WORK)
96       set(MPI_COMPILE_FLAGS_WORK "${MPI_COMPILE_FLAGS_WORK} ${FLAG}")
97     else(MPI_COMPILE_FLAGS_WORK)
98       set(MPI_COMPILE_FLAGS_WORK ${FLAG})
99     endif(MPI_COMPILE_FLAGS_WORK)
100   endforeach(FLAG)
102   # Extract include paths from compile command line
103   string(REGEX MATCH "-I([^\" ]+|\"[^\"]+\")" MPI_INCLUDE_PATH ${MPI_COMPILE_CMDLINE})
104   string(REGEX REPLACE "^-I" "" MPI_INCLUDE_PATH ${MPI_INCLUDE_PATH})
105   string(REGEX REPLACE "//" "/" MPI_INCLUDE_PATH ${MPI_INCLUDE_PATH})
107   # Extract linker paths from the link command line
108   string(REGEX MATCH "-L([^\" ]+|\"[^\"]+\")" MPI_LINK_PATH ${MPI_LINK_CMDLINE})
109   string(REGEX REPLACE "^-L" "" MPI_LINK_PATH ${MPI_LINK_PATH})
110   string(REGEX REPLACE "//" "/" MPI_LINK_PATH ${MPI_LINK_PATH})
112   # Extract linker flags from the link command line
113   string(REGEX MATCHALL "-Wl,([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_FLAGS ${MPI_LINK_CMDLINE})
114   set(MPI_LINK_FLAGS_WORK)
115   foreach(FLAG ${MPI_ALL_LINK_FLAGS})
116     if (MPI_LINK_FLAGS_WORK)
117       set(MPI_LINK_FLAGS_WORK "${MPI_LINK_FLAGS_WORK} ${FLAG}")
118     else(MPI_LINK_FLAGS_WORK)
119       set(MPI_LINK_FLAGS_WORK ${FLAG})
120     endif(MPI_LINK_FLAGS_WORK)
121   endforeach(FLAG)
123   # Extract the set of libraries to link against from the link command
124   # line
125   string(REGEX MATCHALL "-l([^\" ]+|\"[^\"]+\")" MPI_LIBNAMES ${MPI_LINK_CMDLINE})
127   # Determine full path names for all of the libraries that one needs
128   # to link against in an MPI program
129   set(MPI_LIBRARIES)
130   foreach(LIB ${MPI_LIBNAMES})
131     string(REGEX REPLACE "^-l" "" LIB ${LIB})
132     set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE FILEPATH "Cleared" FORCE)
133     find_library(MPI_LIB ${LIB} PATHS ${MPI_LINK_PATH})
134     if (MPI_LIB)
135       list(APPEND MPI_LIBRARIES ${MPI_LIB})
136     else (MPI_LIB)
137       status(ERROR "Unable to find MPI library ${LIB}")
138     endif (MPI_LIB)
139   endforeach(LIB)
140   set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE INTERNAL "Scratch variable for MPI detection" FORCE)
142   # Chop MPI_LIBRARIES into the old-style MPI_LIBRARY and
143   # MPI_EXTRA_LIBRARY.
144   list(LENGTH MPI_LIBRARIES MPI_NUMLIBS)
145   if (MPI_NUMLIBS GREATER 0)
146     list(GET MPI_LIBRARIES 0 MPI_LIBRARY)
147   else (MPI_NUMLIBS GREATER 0)
148     set(MPI_LIBRARY "MPI_LIBRARY-NOTFOUND")
149   endif (MPI_NUMLIBS GREATER 0)
150   if (MPI_NUMLIBS GREATER 1)
151     cdr(MPI_EXTRA_LIBRARY ${MPI_LIBRARIES})
152   else (MPI_NUMLIBS GREATER 1)
153     set(MPI_EXTRA_LIBRARY "MPI_EXTRA_LIBRARY-NOTFOUND")
154   endif (MPI_NUMLIBS GREATER 1)
156   # Set up all of the appropriate cache entries
157   set(MPI_FOUND TRUE CACHE INTERNAL "Whether MPI was found" FORCE)
159   if (NOT MPI_FORCE_RECONFIGURE)
160     set(MPI_COMPILE_FLAGS ${MPI_COMPILE_FLAGS_WORK} CACHE STRING "MPI compilation flags")
161     set(MPI_INCLUDE_PATH ${MPI_INCLUDE_PATH} CACHE STRING "MPI include path")
162     set(MPI_LINK_FLAGS ${MPI_LINK_FLAGS_WORK} CACHE STRING "MPI linking flags")
163     set(MPI_LIBRARIES ${MPI_LIBRARIES} CACHE STRING "MPI libraries to link against, separated by semicolons")
164   else (NOT MPI_FORCE_RECONFIGURE)
165     set(MPI_COMPILE_FLAGS ${MPI_COMPILE_FLAGS_WORK} CACHE STRING "MPI compilation flags" FORCE)
166     set(MPI_INCLUDE_PATH ${MPI_INCLUDE_PATH} CACHE STRING "MPI include path" FORCE)
167     set(MPI_LINK_FLAGS ${MPI_LINK_FLAGS_WORK} CACHE STRING "MPI linking flags" FORCE)
168     set(MPI_LIBRARIES ${MPI_LIBRARIES} CACHE STRING "MPI libraries to link against, separated by semicolons" FORCE)
169   endif (NOT MPI_FORCE_RECONFIGURE)
170 else (MPI_COMPILE_CMDLINE)
171   find_path(MPI_INCLUDE_PATH mpi.h 
172     /usr/local/include 
173     /usr/include 
174     /usr/include/mpi
175     /usr/local/mpi/include
176     "C:/Program Files/MPICH/SDK/Include" 
177     "$ENV{SystemDrive}/Program Files/MPICH2/include"
178     "C:/Program Files/Microsoft Compute Cluster Pack/Include"
179     )
180   
181   # TODO: How do we know whether we're building 32-bit vs. 64-bit?
182   find_library(MPI_LIBRARY 
183     NAMES mpi mpich
184     PATHS /usr/lib /usr/local/lib /usr/local/mpi/lib
185     "C:/Program Files/MPICH/SDK/Lib" 
186     "$ENV{SystemDrive}/Program Files/MPICH/SDK/Lib"
187     "C:/Program Files/Microsoft Compute Cluster Pack/Lib/i386"
188     )
189   find_library(MPI_LIBRARY 
190     NAMES mpich2
191     PATHS
192     "$ENV{SystemDrive}/Program Files/MPICH2/Lib")
194   find_library(MPI_EXTRA_LIBRARY 
195     NAMES mpi++
196     PATHS /usr/lib /usr/local/lib /usr/local/mpi/lib
197     "C:/Program Files/MPICH/SDK/Lib" 
198     "C:/Program Files/Microsoft Compute Cluster Pack/Lib/i386"
199     DOC "If a second MPI library is necessary, specify it here.")
201   set(MPI_COMPILE_FLAGS "" CACHE STRING "MPI compilation flags")
202   set(MPI_LINK_FLAGS "" CACHE STRING "MPI linking flags")
204   if (MPI_EXTRA_LIBRARY)
205     set(MPI_LIBRARIES "${MPI_LIBRARY};${MPI_EXTRA_LIBRARY}" CACHE STRING "MPI libraries to link against, separated by semicolons")
206   else (MPI_EXTRA_LIBRARY)
207     set(MPI_LIBRARIES ${MPI_LIBRARY} CACHE STRING "MPI libraries to link against, separated by semicolons")
208   endif (MPI_EXTRA_LIBRARY)
210   if (MPI_LIBRARY)
211     set(MPI_FOUND TRUE CACHE INTERNAL "Whether MPI was found" FORCE)
212   else (MPI_LIBRARY)
213     set(MPI_FOUND FALSE CACHE INTERNAL "Whether MPI was found" FORCE)
214   endif (MPI_LIBRARY)
215 endif (NOT MPI_FORCE_RECONFIGURE)
217 # on BlueGene/L the MPI lib is named libmpich.rts.a, there also these additional libs are required
218 if("${MPI_LIBRARY}" MATCHES "mpich.rts")
219    set(MPI_EXTRA_LIBRARY ${MPI_EXTRA_LIBRARY} msglayer.rts devices.rts rts.rts devices.rts)
220    set(MPI_LIBRARY ${MPI_LIBRARY}  msglayer.rts devices.rts rts.rts devices.rts)
221 endif("${MPI_LIBRARY}" MATCHES "mpich.rts")
223 set(MPI_LIBRARY ${MPI_LIBRARY} CACHE INTERNAL "MPI library to link against. Deprecated: use MPI_LIBRARIES instead")
224 set(MPI_EXTRA_LIBRARY ${MPI_EXTRA_LIBRARY} CACHE INTERNAL "Second MPI library to link against. Deprecated: use MPI_LIBRARIES instead")
226 include(FindPackageHandleStandardArgs)
227 # handle the QUIETLY and REQUIRED arguments 
228 find_package_handle_standard_args(MPI DEFAULT_MSG MPI_LIBRARY MPI_INCLUDE_PATH)
229 mark_as_advanced(MPI_INCLUDE_PATH MPI_COMPILE_FLAGS MPI_LINK_FLAGS MPI_LIBRARIES)