1###
2#
3# @copyright (c) 2009-2014 The University of Tennessee and The University
4#                          of Tennessee Research Foundation.
5#                          All rights reserved.
6# @copyright (c) 2012-2014 Inria. All rights reserved.
7# @copyright (c) 2012-2014 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria, Univ. Bordeaux. All rights reserved.
8#
9###
10#
11# - Find SCOTCH include dirs and libraries
12# Use this module by invoking find_package with the form:
13#  find_package(SCOTCH
14#               [REQUIRED]             # Fail with error if scotch is not found
15#               [COMPONENTS <comp1> <comp2> ...] # dependencies
16#              )
17#
18#  COMPONENTS can be some of the following:
19#   - ESMUMPS: to activate detection of Scotch with the esmumps interface
20#
21# This module finds headers and scotch library.
22# Results are reported in variables:
23#  SCOTCH_FOUND           - True if headers and requested libraries were found
24#  SCOTCH_INCLUDE_DIRS    - scotch include directories
25#  SCOTCH_LIBRARY_DIRS    - Link directories for scotch libraries
26#  SCOTCH_LIBRARIES       - scotch component libraries to be linked
27#  SCOTCH_INTSIZE         - Number of octets occupied by a SCOTCH_Num
28#
29# The user can give specific paths where to find the libraries adding cmake
30# options at configure (ex: cmake path/to/project -DSCOTCH=path/to/scotch):
31#  SCOTCH_DIR             - Where to find the base directory of scotch
32#  SCOTCH_INCDIR          - Where to find the header files
33#  SCOTCH_LIBDIR          - Where to find the library files
34# The module can also look for the following environment variables if paths
35# are not given as cmake variable: SCOTCH_DIR, SCOTCH_INCDIR, SCOTCH_LIBDIR
36
37#=============================================================================
38# Copyright 2012-2013 Inria
39# Copyright 2012-2013 Emmanuel Agullo
40# Copyright 2012-2013 Mathieu Faverge
41# Copyright 2012      Cedric Castagnede
42# Copyright 2013      Florent Pruvost
43#
44# Distributed under the OSI-approved BSD License (the "License");
45# see accompanying file MORSE-Copyright.txt for details.
46#
47# This software is distributed WITHOUT ANY WARRANTY; without even the
48# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
49# See the License for more information.
50#=============================================================================
51# (To distribute this file outside of Morse, substitute the full
52#  License text for the above reference.)
53
54if (NOT SCOTCH_FOUND)
55  set(SCOTCH_DIR "" CACHE PATH "Installation directory of SCOTCH library")
56  if (NOT SCOTCH_FIND_QUIETLY)
57    message(STATUS "A cache variable, namely SCOTCH_DIR, has been set to specify the install directory of SCOTCH")
58  endif()
59endif()
60
61# Set the version to find
62set(SCOTCH_LOOK_FOR_ESMUMPS OFF)
63
64if( SCOTCH_FIND_COMPONENTS )
65  foreach( component ${SCOTCH_FIND_COMPONENTS} )
66    if (${component} STREQUAL "ESMUMPS")
67      # means we look for esmumps library
68      set(SCOTCH_LOOK_FOR_ESMUMPS ON)
69    endif()
70  endforeach()
71endif()
72
73# SCOTCH may depend on Threads, try to find it
74if (NOT THREADS_FOUND)
75  if (SCOTCH_FIND_REQUIRED)
76    find_package(Threads REQUIRED)
77  else()
78    find_package(Threads)
79  endif()
80endif()
81
82# Looking for include
83# -------------------
84
85# Add system include paths to search include
86# ------------------------------------------
87unset(_inc_env)
88set(ENV_SCOTCH_DIR "$ENV{SCOTCH_DIR}")
89set(ENV_SCOTCH_INCDIR "$ENV{SCOTCH_INCDIR}")
90if(ENV_SCOTCH_INCDIR)
91  list(APPEND _inc_env "${ENV_SCOTCH_INCDIR}")
92elseif(ENV_SCOTCH_DIR)
93  list(APPEND _inc_env "${ENV_SCOTCH_DIR}")
94  list(APPEND _inc_env "${ENV_SCOTCH_DIR}/include")
95  list(APPEND _inc_env "${ENV_SCOTCH_DIR}/include/scotch")
96else()
97  if(WIN32)
98    string(REPLACE ":" ";" _inc_env "$ENV{INCLUDE}")
99  else()
100    string(REPLACE ":" ";" _path_env "$ENV{INCLUDE}")
101    list(APPEND _inc_env "${_path_env}")
102    string(REPLACE ":" ";" _path_env "$ENV{C_INCLUDE_PATH}")
103    list(APPEND _inc_env "${_path_env}")
104    string(REPLACE ":" ";" _path_env "$ENV{CPATH}")
105    list(APPEND _inc_env "${_path_env}")
106    string(REPLACE ":" ";" _path_env "$ENV{INCLUDE_PATH}")
107    list(APPEND _inc_env "${_path_env}")
108  endif()
109endif()
110list(APPEND _inc_env "${CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES}")
111list(APPEND _inc_env "${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}")
112list(REMOVE_DUPLICATES _inc_env)
113
114
115# Try to find the scotch header in the given paths
116# -------------------------------------------------
117# call cmake macro to find the header path
118if(SCOTCH_INCDIR)
119  set(SCOTCH_scotch.h_DIRS "SCOTCH_scotch.h_DIRS-NOTFOUND")
120  find_path(SCOTCH_scotch.h_DIRS
121    NAMES scotch.h
122    HINTS ${SCOTCH_INCDIR})
123else()
124  if(SCOTCH_DIR)
125    set(SCOTCH_scotch.h_DIRS "SCOTCH_scotch.h_DIRS-NOTFOUND")
126    find_path(SCOTCH_scotch.h_DIRS
127      NAMES scotch.h
128      HINTS ${SCOTCH_DIR}
129      PATH_SUFFIXES "include" "include/scotch")
130  else()
131    set(SCOTCH_scotch.h_DIRS "SCOTCH_scotch.h_DIRS-NOTFOUND")
132    find_path(SCOTCH_scotch.h_DIRS
133      NAMES scotch.h
134      HINTS ${_inc_env}
135      PATH_SUFFIXES "scotch")
136  endif()
137endif()
138mark_as_advanced(SCOTCH_scotch.h_DIRS)
139
140# If found, add path to cmake variable
141# ------------------------------------
142if (SCOTCH_scotch.h_DIRS)
143  set(SCOTCH_INCLUDE_DIRS "${SCOTCH_scotch.h_DIRS}")
144else ()
145  set(SCOTCH_INCLUDE_DIRS "SCOTCH_INCLUDE_DIRS-NOTFOUND")
146  if (NOT SCOTCH_FIND_QUIETLY)
147    message(STATUS "Looking for scotch -- scotch.h not found")
148  endif()
149endif()
150list(REMOVE_DUPLICATES SCOTCH_INCLUDE_DIRS)
151
152# Looking for lib
153# ---------------
154
155# Add system library paths to search lib
156# --------------------------------------
157unset(_lib_env)
158set(ENV_SCOTCH_LIBDIR "$ENV{SCOTCH_LIBDIR}")
159if(ENV_SCOTCH_LIBDIR)
160  list(APPEND _lib_env "${ENV_SCOTCH_LIBDIR}")
161elseif(ENV_SCOTCH_DIR)
162  list(APPEND _lib_env "${ENV_SCOTCH_DIR}")
163  list(APPEND _lib_env "${ENV_SCOTCH_DIR}/lib")
164else()
165  if(WIN32)
166    string(REPLACE ":" ";" _lib_env "$ENV{LIB}")
167  else()
168    if(APPLE)
169      string(REPLACE ":" ";" _lib_env "$ENV{DYLD_LIBRARY_PATH}")
170    else()
171      string(REPLACE ":" ";" _lib_env "$ENV{LD_LIBRARY_PATH}")
172    endif()
173    list(APPEND _lib_env "${CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES}")
174    list(APPEND _lib_env "${CMAKE_C_IMPLICIT_LINK_DIRECTORIES}")
175  endif()
176endif()
177list(REMOVE_DUPLICATES _lib_env)
178
179# Try to find the scotch lib in the given paths
180# ----------------------------------------------
181
182set(SCOTCH_libs_to_find "scotch;scotcherrexit")
183if (SCOTCH_LOOK_FOR_ESMUMPS)
184  list(INSERT SCOTCH_libs_to_find 0 "esmumps")
185endif()
186
187# call cmake macro to find the lib path
188if(SCOTCH_LIBDIR)
189  foreach(scotch_lib ${SCOTCH_libs_to_find})
190    set(SCOTCH_${scotch_lib}_LIBRARY "SCOTCH_${scotch_lib}_LIBRARY-NOTFOUND")
191    find_library(SCOTCH_${scotch_lib}_LIBRARY
192      NAMES ${scotch_lib}
193      HINTS ${SCOTCH_LIBDIR})
194  endforeach()
195else()
196  if(SCOTCH_DIR)
197    foreach(scotch_lib ${SCOTCH_libs_to_find})
198      set(SCOTCH_${scotch_lib}_LIBRARY "SCOTCH_${scotch_lib}_LIBRARY-NOTFOUND")
199      find_library(SCOTCH_${scotch_lib}_LIBRARY
200	NAMES ${scotch_lib}
201	HINTS ${SCOTCH_DIR}
202	PATH_SUFFIXES lib lib32 lib64)
203    endforeach()
204  else()
205    foreach(scotch_lib ${SCOTCH_libs_to_find})
206      set(SCOTCH_${scotch_lib}_LIBRARY "SCOTCH_${scotch_lib}_LIBRARY-NOTFOUND")
207      find_library(SCOTCH_${scotch_lib}_LIBRARY
208	NAMES ${scotch_lib}
209	HINTS ${_lib_env})
210    endforeach()
211  endif()
212endif()
213
214set(SCOTCH_LIBRARIES "")
215set(SCOTCH_LIBRARY_DIRS "")
216# If found, add path to cmake variable
217# ------------------------------------
218foreach(scotch_lib ${SCOTCH_libs_to_find})
219
220  if (SCOTCH_${scotch_lib}_LIBRARY)
221    get_filename_component(${scotch_lib}_lib_path "${SCOTCH_${scotch_lib}_LIBRARY}" PATH)
222    # set cmake variables
223    list(APPEND SCOTCH_LIBRARIES "${SCOTCH_${scotch_lib}_LIBRARY}")
224    list(APPEND SCOTCH_LIBRARY_DIRS "${${scotch_lib}_lib_path}")
225  else ()
226    list(APPEND SCOTCH_LIBRARIES "${SCOTCH_${scotch_lib}_LIBRARY}")
227    if (NOT SCOTCH_FIND_QUIETLY)
228      message(STATUS "Looking for scotch -- lib ${scotch_lib} not found")
229    endif()
230  endif ()
231
232  mark_as_advanced(SCOTCH_${scotch_lib}_LIBRARY)
233
234endforeach()
235list(REMOVE_DUPLICATES SCOTCH_LIBRARY_DIRS)
236
237# check a function to validate the find
238if(SCOTCH_LIBRARIES)
239
240  set(REQUIRED_INCDIRS)
241  set(REQUIRED_LIBDIRS)
242  set(REQUIRED_LIBS)
243
244  # SCOTCH
245  if (SCOTCH_INCLUDE_DIRS)
246    set(REQUIRED_INCDIRS  "${SCOTCH_INCLUDE_DIRS}")
247  endif()
248  if (SCOTCH_LIBRARY_DIRS)
249    set(REQUIRED_LIBDIRS "${SCOTCH_LIBRARY_DIRS}")
250  endif()
251  set(REQUIRED_LIBS "${SCOTCH_LIBRARIES}")
252  # THREADS
253  if(CMAKE_THREAD_LIBS_INIT)
254    list(APPEND REQUIRED_LIBS "${CMAKE_THREAD_LIBS_INIT}")
255  endif()
256  set(Z_LIBRARY "Z_LIBRARY-NOTFOUND")
257  find_library(Z_LIBRARY NAMES z)
258  mark_as_advanced(Z_LIBRARY)
259  if(Z_LIBRARY)
260    list(APPEND REQUIRED_LIBS "-lz")
261  endif()
262  set(M_LIBRARY "M_LIBRARY-NOTFOUND")
263  find_library(M_LIBRARY NAMES m)
264  mark_as_advanced(M_LIBRARY)
265  if(M_LIBRARY)
266    list(APPEND REQUIRED_LIBS "-lm")
267  endif()
268  set(RT_LIBRARY "RT_LIBRARY-NOTFOUND")
269  find_library(RT_LIBRARY NAMES rt)
270  mark_as_advanced(RT_LIBRARY)
271  if(RT_LIBRARY)
272    list(APPEND REQUIRED_LIBS "-lrt")
273  endif()
274
275  # set required libraries for link
276  set(CMAKE_REQUIRED_INCLUDES "${REQUIRED_INCDIRS}")
277  set(CMAKE_REQUIRED_LIBRARIES)
278  foreach(lib_dir ${REQUIRED_LIBDIRS})
279    list(APPEND CMAKE_REQUIRED_LIBRARIES "-L${lib_dir}")
280  endforeach()
281  list(APPEND CMAKE_REQUIRED_LIBRARIES "${REQUIRED_LIBS}")
282  string(REGEX REPLACE "^ -" "-" CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES}")
283
284  # test link
285  unset(SCOTCH_WORKS CACHE)
286  include(CheckFunctionExists)
287  check_function_exists(SCOTCH_graphInit SCOTCH_WORKS)
288  mark_as_advanced(SCOTCH_WORKS)
289
290  if(SCOTCH_WORKS)
291    # save link with dependencies
292    set(SCOTCH_LIBRARIES "${REQUIRED_LIBS}")
293  else()
294    if(NOT SCOTCH_FIND_QUIETLY)
295      message(STATUS "Looking for SCOTCH : test of SCOTCH_graphInit with SCOTCH library fails")
296      message(STATUS "CMAKE_REQUIRED_LIBRARIES: ${CMAKE_REQUIRED_LIBRARIES}")
297      message(STATUS "CMAKE_REQUIRED_INCLUDES: ${CMAKE_REQUIRED_INCLUDES}")
298      message(STATUS "Check in CMakeFiles/CMakeError.log to figure out why it fails")
299    endif()
300  endif()
301  set(CMAKE_REQUIRED_INCLUDES)
302  set(CMAKE_REQUIRED_FLAGS)
303  set(CMAKE_REQUIRED_LIBRARIES)
304endif(SCOTCH_LIBRARIES)
305
306if (SCOTCH_LIBRARIES)
307  list(GET SCOTCH_LIBRARIES 0 first_lib)
308  get_filename_component(first_lib_path "${first_lib}" PATH)
309  if (${first_lib_path} MATCHES "/lib(32|64)?$")
310    string(REGEX REPLACE "/lib(32|64)?$" "" not_cached_dir "${first_lib_path}")
311    set(SCOTCH_DIR_FOUND "${not_cached_dir}" CACHE PATH "Installation directory of SCOTCH library" FORCE)
312  else()
313    set(SCOTCH_DIR_FOUND "${first_lib_path}" CACHE PATH "Installation directory of SCOTCH library" FORCE)
314  endif()
315endif()
316mark_as_advanced(SCOTCH_DIR)
317mark_as_advanced(SCOTCH_DIR_FOUND)
318
319# Check the size of SCOTCH_Num
320# ---------------------------------
321set(CMAKE_REQUIRED_INCLUDES ${SCOTCH_INCLUDE_DIRS})
322
323include(CheckCSourceRuns)
324#stdio.h and stdint.h should be included by scotch.h directly
325set(SCOTCH_C_TEST_SCOTCH_Num_4 "
326#include <stdio.h>
327#include <stdint.h>
328#include <scotch.h>
329int main(int argc, char **argv) {
330  if (sizeof(SCOTCH_Num) == 4)
331    return 0;
332  else
333    return 1;
334}
335")
336
337set(SCOTCH_C_TEST_SCOTCH_Num_8 "
338#include <stdio.h>
339#include <stdint.h>
340#include <scotch.h>
341int main(int argc, char **argv) {
342  if (sizeof(SCOTCH_Num) == 8)
343    return 0;
344  else
345    return 1;
346}
347")
348check_c_source_runs("${SCOTCH_C_TEST_SCOTCH_Num_4}" SCOTCH_Num_4)
349if(NOT SCOTCH_Num_4)
350  check_c_source_runs("${SCOTCH_C_TEST_SCOTCH_Num_8}" SCOTCH_Num_8)
351  if(NOT SCOTCH_Num_8)
352    set(SCOTCH_INTSIZE -1)
353  else()
354    set(SCOTCH_INTSIZE 8)
355  endif()
356else()
357  set(SCOTCH_INTSIZE 4)
358endif()
359set(CMAKE_REQUIRED_INCLUDES "")
360
361# check that SCOTCH has been found
362# ---------------------------------
363include(FindPackageHandleStandardArgs)
364find_package_handle_standard_args(SCOTCH DEFAULT_MSG
365  SCOTCH_LIBRARIES
366  SCOTCH_WORKS)
367#
368# TODO: Add possibility to check for specific functions in the library
369#
370