Debian Codegraph @

cmake/modules/FindSCOTCH.cmake

raw
1 # - Try to find SCOTCH
2 # Once done this will define
3 #
4 # SCOTCH_FOUND - system has found SCOTCH
5 # SCOTCH_INCLUDE_DIRS - include directories for SCOTCH
6 # SCOTCH_LIBARIES - libraries for SCOTCH
7 # SCOTCH_VERSION - version for SCOTCH
8 #
9 # Variables used by this module, they can change the default behaviour and
10 # need to be set before calling find_package:
11 #
12 # SCOTCH_DEBUG - Set this to TRUE to enable debugging output
13 # of FindScotchPT.cmake if you are having problems.
14 # Please enable this before filing any bug reports.
15
16 #=============================================================================
17 # Copyright (C) 2010-2011 Garth N. Wells, Johannes Ring and Anders Logg
18 # All rights reserved.
19 #
20 # Redistribution and use in source and binary forms, with or without
21 # modification, are permitted provided that the following conditions
22 # are met:
23 #
24 # 1. Redistributions of source code must retain the above copyright
25 # notice, this list of conditions and the following disclaimer.
26 # 2. Redistributions in binary form must reproduce the above copyright
27 # notice, this list of conditions and the following disclaimer in
28 # the documentation and/or other materials provided with the
29 # distribution.
30 #
31 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
34 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
35 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
36 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
37 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
41 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 # POSSIBILITY OF SUCH DAMAGE.
43 #=============================================================================
44
45 set(SCOTCH_FOUND FALSE)
46
47 message(STATUS "Checking for package 'SCOTCH-PT'")
48
49 # Check for header file
50 find_path(SCOTCH_INCLUDE_DIRS ptscotch.h
51 HINTS ${SCOTCH_DIR}/include $ENV{SCOTCH_DIR}/include ${PETSC_INCLUDE_DIRS}
52 PATH_SUFFIXES scotch
53 DOC "Directory where the SCOTCH-PT header is located"
54 )
55
56 # Check for scotch
57 find_library(SCOTCH_LIBRARY
58 NAMES scotch
59 HINTS ${SCOTCH_DIR}/lib $ENV{SCOTCH_DIR}/lib ${PETSC_LIBRARY_DIRS}
60 NO_DEFAULT_PATH
61 DOC "The SCOTCH library"
62 )
63 find_library(SCOTCH_LIBRARY
64 NAMES scotch
65 DOC "The SCOTCH library"
66 )
67
68 # Check for scotcherr
69 find_library(SCOTCHERR_LIBRARY
70 NAMES scotcherr
71 HINTS ${SCOTCH_DIR}/lib $ENV{SCOTCH_DIR}/lib
72 NO_DEFAULT_PATH
73 DOC "The SCOTCH-ERROR library"
74 )
75 find_library(SCOTCHERR_LIBRARY
76 NAMES scotcherr
77 DOC "The SCOTCH-ERROR library"
78 )
79
80 # Check for ptscotch
81 find_library(PTSCOTCH_LIBRARY
82 NAMES ptscotch
83 HINTS ${SCOTCH_DIR}/lib $ENV{SCOTCH_DIR}/lib ${PETSC_LIBRARY_DIRS}
84 NO_DEFAULT_PATH
85 DOC "The PTSCOTCH library"
86 )
87 find_library(PTSCOTCH_LIBRARY
88 NAMES ptscotch
89 DOC "The PTSCOTCH library"
90 )
91
92 # Check for ptesmumps
93 find_library(PTESMUMPS_LIBRARY
94 NAMES ptesmumps esmumps
95 HINTS ${SCOTCH_DIR}/lib $ENV{SCOTCH_DIR}/lib ${PETSC_LIBRARY_DIRS}
96 NO_DEFAULT_PATH
97 DOC "The PTSCOTCH-ESMUMPS library"
98 )
99 find_library(PTESMUMPS_LIBRARY
100 NAMES ptesmumps esmumps
101 DOC "The PTSCOTCH-ESMUMPS library"
102 )
103
104 # Check for ptscotcherr
105 find_library(PTSCOTCHERR_LIBRARY
106 NAMES ptscotcherr
107 HINTS ${SCOTCH_DIR}/lib $ENV{SCOTCH_DIR}/lib ${PETSC_LIBRARY_DIRS}
108 NO_DEFAULT_PATH
109 DOC "The PTSCOTCH-ERROR library"
110 )
111 find_library(PTSCOTCHERR_LIBRARY
112 NAMES ptscotcherr
113 DOC "The PTSCOTCH-ERROR library"
114 )
115
116 set(SCOTCH_LIBRARIES ${PTSCOTCH_LIBRARY})
117 if (PTESMUMPS_LIBRARY)
118 set(SCOTCH_LIBRARIES ${SCOTCH_LIBRARIES} ${PTESMUMPS_LIBRARY})
119 endif()
120 set(SCOTCH_LIBRARIES ${SCOTCH_LIBRARIES} ${PTSCOTCHERR_LIBRARY})
121
122 # Basic check of SCOTCH_VERSION which does not require compilation
123 if (SCOTCH_INCLUDE_DIRS)
124 file(STRINGS "${SCOTCH_INCLUDE_DIRS}/ptscotch.h" PTSCOTCH_H)
125 string(REGEX MATCH "SCOTCH_VERSION [0-9]+" SCOTCH_VERSION "${PTSCOTCH_H}")
126 string(REGEX MATCH "[0-9]+" SCOTCH_VERSION "${SCOTCH_VERSION}")
127 endif()
128
129 # If SCOTCH_VERSION was not found in ptscotch.h, look in scotch.h
130 if (SCOTCH_INCLUDE_DIRS AND NOT SCOTCH_VERSION)
131 file(STRINGS "${SCOTCH_INCLUDE_DIRS}/scotch.h" SCOTCH_H)
132 string(REGEX MATCH "SCOTCH_VERSION [0-9]+" SCOTCH_VERSION "${SCOTCH_H}")
133 string(REGEX MATCH "[0-9]+" SCOTCH_VERSION "${SCOTCH_VERSION}")
134 endif()
135
136 # For SCOTCH version > 6, need to add libraries scotch and ptscotch
137 if (NOT "${SCOTCH_VERSION}" VERSION_LESS "6")
138 set(SCOTCH_LIBRARIES ${PTSCOTCH_LIBRARY} ${SCOTCH_LIBRARY} ${PTSCOTCHERR_LIBRARY})
139 set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${SCOTCH_LIBRARY})
140 endif()
141
142 # Try compiling and running test program
143 if (DOLFIN_SKIP_BUILD_TESTS)
144 message(STATUS "Found SCOTCH (version ${SCOTCH_VERSION})")
145 set(SCOTCH_TEST_RUNS TRUE)
146 elseif (SCOTCH_INCLUDE_DIRS AND SCOTCH_LIBRARIES)
147 if (SCOTCH_DEBUG)
148 message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
149 "location of ptscotch.h: ${SCOTCH_INCLUDE_DIRS}/ptscotch.h")
150 message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
151 "location of libscotch: ${SCOTCH_LIBRARY}")
152 message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
153 "location of libptscotch: ${PTSCOTCH_LIBRARY}")
154 message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
155 "location of libptscotcherr: ${PTSCOTCHERR_LIBRARY}")
156 endif()
157
158 # Set flags for building test program
159 set(CMAKE_REQUIRED_INCLUDES ${SCOTCH_INCLUDE_DIRS})
160 set(CMAKE_REQUIRED_LIBRARIES ${SCOTCH_LIBRARIES})
161 #set(CMAKE_REQUIRED_LIBRARIES ${SCOTCH_LIBRARY} ${SCOTCHERR_LIBRARY})
162
163 # Add MPI variables if MPI has been found
164 if (MPI_CXX_FOUND)
165 set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES} ${MPI_CXX_INCLUDE_PATH})
166 set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${MPI_CXX_LIBRARIES})
167 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${MPI_CXX_COMPILE_FLAGS}")
168 endif()
169
170 set(SCOTCH_CONFIG_TEST_VERSION_CPP
171 "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/scotch_config_test_version.cpp")
172 file(WRITE ${SCOTCH_CONFIG_TEST_VERSION_CPP} "
173 #define MPICH_IGNORE_CXX_SEEK 1
174 #include <stdint.h>
175 #include <stdio.h>
176 #include <mpi.h>
177 #include <ptscotch.h>
178 #include <iostream>
179
180 int main() {
181 std::cout << SCOTCH_VERSION << \".\"
182 << SCOTCH_RELEASE << \".\"
183 << SCOTCH_PATCHLEVEL;
184 return 0;
185 }
186 ")
187
188 try_run(
189 SCOTCH_CONFIG_TEST_VERSION_EXITCODE
190 SCOTCH_CONFIG_TEST_VERSION_COMPILED
191 ${CMAKE_CURRENT_BINARY_DIR}
192 ${SCOTCH_CONFIG_TEST_VERSION_CPP}
193 CMAKE_FLAGS
194 "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}"
195 "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}"
196 COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT
197 RUN_OUTPUT_VARIABLE OUTPUT
198 )
199
200 # Set version number
201 if (SCOTCH_CONFIG_TEST_VERSION_EXITCODE EQUAL 0)
202 set(SCOTCH_VERSION ${OUTPUT} CACHE TYPE STRING)
203 message(STATUS "Found SCOTCH (version ${SCOTCH_VERSION})")
204 endif()
205
206 # PT-SCOTCH was first introduced in SCOTCH version 5.0
207 # FIXME: parallel graph partitioning features in PT-SCOTCH was first
208 # introduced in 5.1. Do we require version 5.1?
209 if (NOT ${SCOTCH_VERSION} VERSION_LESS "5.0")
210 set(SCOTCH_TEST_LIB_CPP
211 "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/scotch_test_lib.cpp")
212 file(WRITE ${SCOTCH_TEST_LIB_CPP} "
213 #define MPICH_IGNORE_CXX_SEEK 1
214 #include <stdint.h>
215 #include <stdio.h>
216 #include <mpi.h>
217 #include <ptscotch.h>
218 #include <iostream>
219 #include <cstdlib>
220
221 int main() {
222 int provided;
223 SCOTCH_Dgraph dgrafdat;
224
225 MPI_Init_thread(0, 0, MPI_THREAD_MULTIPLE, &provided);
226
227 if (SCOTCH_dgraphInit(&dgrafdat, MPI_COMM_WORLD) != 0) {
228 if (MPI_THREAD_MULTIPLE > provided) {
229 std::cout << \"MPI implementation is not thread-safe:\" << std::endl;
230 std::cout << \"SCOTCH should be compiled without SCOTCH_PTHREAD\" << std::endl;
231 exit(1);
232 }
233 else {
234 std::cout << \"libptscotch linked to libscotch or other unknown error\" << std::endl;
235 exit(2);
236 }
237 }
238 else {
239 SCOTCH_dgraphExit(&dgrafdat);
240 }
241
242 MPI_Finalize();
243
244 return 0;
245 }
246 ")
247
248 message(STATUS "Performing test SCOTCH_TEST_RUNS")
249 try_run(
250 SCOTCH_TEST_LIB_EXITCODE
251 SCOTCH_TEST_LIB_COMPILED
252 ${CMAKE_CURRENT_BINARY_DIR}
253 ${SCOTCH_TEST_LIB_CPP}
254 CMAKE_FLAGS
255 "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}"
256 "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}"
257 COMPILE_OUTPUT_VARIABLE SCOTCH_TEST_LIB_COMPILE_OUTPUT
258 RUN_OUTPUT_VARIABLE SCOTCH_TEST_LIB_OUTPUT
259 )
260
261 if (SCOTCH_TEST_LIB_COMPILED AND SCOTCH_TEST_LIB_EXITCODE EQUAL 0)
262 message(STATUS "Performing test SCOTCH_TEST_RUNS - Success")
263 set(SCOTCH_TEST_RUNS TRUE)
264 else()
265 message(STATUS "Performing test SCOTCH_TEST_RUNS - Failed")
266
267 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
268 "Failed to run SCOTCH test:\n"
269 "CMAKE_REQUIRED_INCLUDES: ${CMAKE_REQUIRED_INCLUDES}\n"
270 "CMAKE_REQUIRED_LIBRARIES: ${CMAKE_REQUIRED_LIBRARIES}\n"
271 "CMAKE_REQUIRED_FLAGS: ${CMAKE_REQUIRED_FLAGS}\n"
272 "SCOTCH_TEST_LIB_EXITCODE: ${SCOTCH_TEST_LIB_EXITCODE}\n"
273 "SCOTCH_TEST_LIB_COMPILED: ${SCOTCH_TEST_LIB_COMPILED}\n"
274 "SCOTCH_TEST_LIB_COMPILE_OUTPUT: ${SCOTCH_TEST_LIB_COMPILE_OUTPUT}\n"
275 "SCOTCH_TEST_LIB_OUTPUT: ${SCOTCH_TEST_LIB_OUTPUT}\n\n")
276
277 if (SCOTCH_DEBUG)
278 # Output some variables
279 message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
280 "SCOTCH_TEST_LIB_COMPILED = ${SCOTCH_TEST_LIB_COMPILED}")
281 message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
282 "SCOTCH_TEST_LIB_COMPILE_OUTPUT = ${SCOTCH_TEST_LIB_COMPILE_OUTPUT}")
283 message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
284 "SCOTCH_TEST_LIB_EXITCODE = ${SCOTCH_TEST_LIB_EXITCODE}")
285 message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
286 "SCOTCH_TEST_LIB_OUTPUT = ${SCOTCH_TEST_LIB_OUTPUT}")
287 endif()
288 endif()
289
290 # If program does not run, try adding zlib library and test again
291 if(NOT SCOTCH_TEST_RUNS)
292 if (NOT ZLIB_FOUND)
293 find_package(ZLIB)
294 endif()
295
296 if (ZLIB_INCLUDE_DIRS AND ZLIB_LIBRARIES)
297 set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES} ${ZLIB_INCLUDE_DIRS})
298 set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${ZLIB_LIBRARIES})
299
300 message(STATUS "Performing test SCOTCH_ZLIB_TEST_RUNS")
301 try_run(
302 SCOTCH_ZLIB_TEST_LIB_EXITCODE
303 SCOTCH_ZLIB_TEST_LIB_COMPILED
304 ${CMAKE_CURRENT_BINARY_DIR}
305 ${SCOTCH_TEST_LIB_CPP}
306 CMAKE_FLAGS
307 "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}"
308 "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}"
309 COMPILE_OUTPUT_VARIABLE SCOTCH_ZLIB_TEST_LIB_COMPILE_OUTPUT
310 RUN_OUTPUT_VARIABLE SCOTCH_ZLIB_TEST_LIB_OUTPUT
311 )
312
313 # Add zlib flags if required and set test run to 'true'
314 if (SCOTCH_ZLIB_TEST_LIB_COMPILED AND SCOTCH_ZLIB_TEST_LIB_EXITCODE EQUAL 0)
315 message(STATUS "Performing test SCOTCH_ZLIB_TEST_RUNS - Success")
316 set(SCOTCH_INCLUDE_DIRS ${SCOTCH_INCLUDE_DIRS} ${ZLIB_INCLUDE_DIRS})
317 set(SCOTCH_LIBRARIES ${SCOTCH_LIBRARIES} ${ZLIB_LIBRARIES})
318 set(SCOTCH_TEST_RUNS TRUE)
319 else()
320 message(STATUS "Performing test SCOTCH_ZLIB_TEST_RUNS - Failed")
321
322 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
323 "Failed to run SCOTCH test with zlib:\n"
324 "CMAKE_REQUIRED_INCLUDES: ${CMAKE_REQUIRED_INCLUDES}\n"
325 "CMAKE_REQUIRED_LIBRARIES: ${CMAKE_REQUIRED_LIBRARIES}\n"
326 "CMAKE_REQUIRED_FLAGS: ${CMAKE_REQUIRED_FLAGS}\n"
327 "SCOTCH_ZLIB_TEST_LIB_EXITCODE: ${SCOTCH_ZLIB_TEST_LIB_EXITCODE}\n"
328 "SCOTCH_ZLIB_TEST_LIB_COMPILED: ${SCOTCH_ZLIB_TEST_LIB_COMPILED}\n"
329 "SCOTCH_ZLIB_TEST_LIB_COMPILE_OUTPUT: ${SCOTCH_ZLIB_TEST_LIB_COMPILE_OUTPUT}\n"
330 "SCOTCH_ZLIB_TEST_LIB_OUTPUT: ${SCOTCH_ZLIB_TEST_LIB_OUTPUT}\n\n")
331
332 if (SCOTCH_DEBUG)
333 message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
334 "SCOTCH_ZLIB_TEST_LIB_COMPILED = ${SCOTCH_ZLIB_TEST_LIB_COMPILED}")
335 message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
336 "SCOTCH_ZLIB_TEST_LIB_COMPILE_OUTPUT = ${SCOTCH_ZLIB_TEST_LIB_COMPILE_OUTPUT}")
337 message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
338 "SCOTCH_TEST_LIB_EXITCODE = ${SCOTCH_TEST_LIB_EXITCODE}")
339 message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
340 "SCOTCH_TEST_LIB_OUTPUT = ${SCOTCH_TEST_LIB_OUTPUT}")
341 endif()
342 endif()
343
344 endif()
345 endif()
346 endif()
347 endif()
348
349 # Standard package handling
350 find_package_handle_standard_args(SCOTCH
351 "SCOTCH could not be found. Be sure to set SCOTCH_DIR."
352 SCOTCH_LIBRARIES
353 SCOTCH_INCLUDE_DIRS
354 SCOTCH_TEST_RUNS)