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 METIS include dirs and libraries
12# Use this module by invoking find_package with the form:
13#  find_package(METIS
14#               [REQUIRED]             # Fail with error if metis is not found
15#              )
16#
17# This module finds headers and metis library.
18# Results are reported in variables:
19#  METIS_FOUND           - True if headers and requested libraries were found
20#  METIS_INCLUDE_DIRS    - metis include directories
21#  METIS_LIBRARY_DIRS    - Link directories for metis libraries
22#  METIS_LIBRARIES       - metis component libraries to be linked
23#
24# The user can give specific paths where to find the libraries adding cmake
25# options at configure (ex: cmake path/to/project -DMETIS_DIR=path/to/metis):
26#  METIS_DIR             - Where to find the base directory of metis
27#  METIS_INCDIR          - Where to find the header files
28#  METIS_LIBDIR          - Where to find the library files
29# The module can also look for the following environment variables if paths
30# are not given as cmake variable: METIS_DIR, METIS_INCDIR, METIS_LIBDIR
31
32#=============================================================================
33# Copyright 2012-2013 Inria
34# Copyright 2012-2013 Emmanuel Agullo
35# Copyright 2012-2013 Mathieu Faverge
36# Copyright 2012      Cedric Castagnede
37# Copyright 2013      Florent Pruvost
38#
39# Distributed under the OSI-approved BSD License (the "License");
40# see accompanying file MORSE-Copyright.txt for details.
41#
42# This software is distributed WITHOUT ANY WARRANTY; without even the
43# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
44# See the License for more information.
45#=============================================================================
46# (To distribute this file outside of Morse, substitute the full
47#  License text for the above reference.)
48
49if (NOT METIS_FOUND)
50  set(METIS_DIR "" CACHE PATH "Installation directory of METIS library")
51  if (NOT METIS_FIND_QUIETLY)
52    message(STATUS "A cache variable, namely METIS_DIR, has been set to specify the install directory of METIS")
53  endif()
54endif()
55
56# Looking for include
57# -------------------
58
59# Add system include paths to search include
60# ------------------------------------------
61unset(_inc_env)
62set(ENV_METIS_DIR "$ENV{METIS_DIR}")
63set(ENV_METIS_INCDIR "$ENV{METIS_INCDIR}")
64if(ENV_METIS_INCDIR)
65  list(APPEND _inc_env "${ENV_METIS_INCDIR}")
66elseif(ENV_METIS_DIR)
67  list(APPEND _inc_env "${ENV_METIS_DIR}")
68  list(APPEND _inc_env "${ENV_METIS_DIR}/include")
69  list(APPEND _inc_env "${ENV_METIS_DIR}/include/metis")
70else()
71  if(WIN32)
72    string(REPLACE ":" ";" _inc_env "$ENV{INCLUDE}")
73  else()
74    string(REPLACE ":" ";" _path_env "$ENV{INCLUDE}")
75    list(APPEND _inc_env "${_path_env}")
76    string(REPLACE ":" ";" _path_env "$ENV{C_INCLUDE_PATH}")
77    list(APPEND _inc_env "${_path_env}")
78    string(REPLACE ":" ";" _path_env "$ENV{CPATH}")
79    list(APPEND _inc_env "${_path_env}")
80    string(REPLACE ":" ";" _path_env "$ENV{INCLUDE_PATH}")
81    list(APPEND _inc_env "${_path_env}")
82  endif()
83endif()
84list(APPEND _inc_env "${CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES}")
85list(APPEND _inc_env "${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}")
86list(REMOVE_DUPLICATES _inc_env)
87
88
89# Try to find the metis header in the given paths
90# -------------------------------------------------
91# call cmake macro to find the header path
92if(METIS_INCDIR)
93  set(METIS_metis.h_DIRS "METIS_metis.h_DIRS-NOTFOUND")
94  find_path(METIS_metis.h_DIRS
95    NAMES metis.h
96    HINTS ${METIS_INCDIR})
97else()
98  if(METIS_DIR)
99    set(METIS_metis.h_DIRS "METIS_metis.h_DIRS-NOTFOUND")
100    find_path(METIS_metis.h_DIRS
101      NAMES metis.h
102      HINTS ${METIS_DIR}
103      PATH_SUFFIXES "include" "include/metis")
104  else()
105    set(METIS_metis.h_DIRS "METIS_metis.h_DIRS-NOTFOUND")
106    find_path(METIS_metis.h_DIRS
107      NAMES metis.h
108      HINTS ${_inc_env})
109  endif()
110endif()
111mark_as_advanced(METIS_metis.h_DIRS)
112
113
114# If found, add path to cmake variable
115# ------------------------------------
116if (METIS_metis.h_DIRS)
117  set(METIS_INCLUDE_DIRS "${METIS_metis.h_DIRS}")
118else ()
119  set(METIS_INCLUDE_DIRS "METIS_INCLUDE_DIRS-NOTFOUND")
120  if(NOT METIS_FIND_QUIETLY)
121    message(STATUS "Looking for metis -- metis.h not found")
122  endif()
123endif()
124
125
126# Looking for lib
127# ---------------
128
129# Add system library paths to search lib
130# --------------------------------------
131unset(_lib_env)
132set(ENV_METIS_LIBDIR "$ENV{METIS_LIBDIR}")
133if(ENV_METIS_LIBDIR)
134  list(APPEND _lib_env "${ENV_METIS_LIBDIR}")
135elseif(ENV_METIS_DIR)
136  list(APPEND _lib_env "${ENV_METIS_DIR}")
137  list(APPEND _lib_env "${ENV_METIS_DIR}/lib")
138else()
139  if(WIN32)
140    string(REPLACE ":" ";" _lib_env "$ENV{LIB}")
141  else()
142    if(APPLE)
143      string(REPLACE ":" ";" _lib_env "$ENV{DYLD_LIBRARY_PATH}")
144    else()
145      string(REPLACE ":" ";" _lib_env "$ENV{LD_LIBRARY_PATH}")
146    endif()
147    list(APPEND _lib_env "${CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES}")
148    list(APPEND _lib_env "${CMAKE_C_IMPLICIT_LINK_DIRECTORIES}")
149  endif()
150endif()
151list(REMOVE_DUPLICATES _lib_env)
152
153# Try to find the metis lib in the given paths
154# ----------------------------------------------
155# call cmake macro to find the lib path
156if(METIS_LIBDIR)
157  set(METIS_metis_LIBRARY "METIS_metis_LIBRARY-NOTFOUND")
158  find_library(METIS_metis_LIBRARY
159    NAMES metis
160    HINTS ${METIS_LIBDIR})
161else()
162  if(METIS_DIR)
163    set(METIS_metis_LIBRARY "METIS_metis_LIBRARY-NOTFOUND")
164    find_library(METIS_metis_LIBRARY
165      NAMES metis
166      HINTS ${METIS_DIR}
167      PATH_SUFFIXES lib lib32 lib64)
168  else()
169    set(METIS_metis_LIBRARY "METIS_metis_LIBRARY-NOTFOUND")
170    find_library(METIS_metis_LIBRARY
171      NAMES metis
172      HINTS ${_lib_env})
173  endif()
174endif()
175mark_as_advanced(METIS_metis_LIBRARY)
176
177
178# If found, add path to cmake variable
179# ------------------------------------
180if (METIS_metis_LIBRARY)
181  get_filename_component(metis_lib_path "${METIS_metis_LIBRARY}" PATH)
182  # set cmake variables
183  set(METIS_LIBRARIES    "${METIS_metis_LIBRARY}")
184  set(METIS_LIBRARY_DIRS "${metis_lib_path}")
185else ()
186  set(METIS_LIBRARIES    "METIS_LIBRARIES-NOTFOUND")
187  set(METIS_LIBRARY_DIRS "METIS_LIBRARY_DIRS-NOTFOUND")
188  if(NOT METIS_FIND_QUIETLY)
189    message(STATUS "Looking for metis -- lib metis not found")
190  endif()
191endif ()
192
193# check a function to validate the find
194if(METIS_LIBRARIES)
195
196  set(REQUIRED_INCDIRS)
197  set(REQUIRED_LIBDIRS)
198  set(REQUIRED_LIBS)
199
200  # METIS
201  if (METIS_INCLUDE_DIRS)
202    set(REQUIRED_INCDIRS  "${METIS_INCLUDE_DIRS}")
203  endif()
204  if (METIS_LIBRARY_DIRS)
205    set(REQUIRED_LIBDIRS "${METIS_LIBRARY_DIRS}")
206  endif()
207  set(REQUIRED_LIBS "${METIS_LIBRARIES}")
208  # m
209  find_library(M_LIBRARY NAMES m)
210  mark_as_advanced(M_LIBRARY)
211  if(M_LIBRARY)
212    list(APPEND REQUIRED_LIBS "-lm")
213  endif()
214
215  # set required libraries for link
216  set(CMAKE_REQUIRED_INCLUDES "${REQUIRED_INCDIRS}")
217  set(CMAKE_REQUIRED_LIBRARIES)
218  foreach(lib_dir ${REQUIRED_LIBDIRS})
219    list(APPEND CMAKE_REQUIRED_LIBRARIES "-L${lib_dir}")
220  endforeach()
221  list(APPEND CMAKE_REQUIRED_LIBRARIES "${REQUIRED_LIBS}")
222  string(REGEX REPLACE "^ -" "-" CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES}")
223
224  # test link
225  unset(METIS_WORKS CACHE)
226  include(CheckFunctionExists)
227  check_function_exists(METIS_NodeND METIS_WORKS)
228  mark_as_advanced(METIS_WORKS)
229
230  if(NOT METIS_WORKS)
231    if(NOT METIS_FIND_QUIETLY)
232      message(STATUS "Looking for METIS : test of METIS_NodeND with METIS library fails")
233      message(STATUS "CMAKE_REQUIRED_LIBRARIES: ${CMAKE_REQUIRED_LIBRARIES}")
234      message(STATUS "CMAKE_REQUIRED_INCLUDES: ${CMAKE_REQUIRED_INCLUDES}")
235      message(STATUS "Check in CMakeFiles/CMakeError.log to figure out why it fails")
236    endif()
237  endif()
238  set(CMAKE_REQUIRED_INCLUDES)
239  set(CMAKE_REQUIRED_FLAGS)
240  set(CMAKE_REQUIRED_LIBRARIES)
241endif(METIS_LIBRARIES)
242
243if (METIS_LIBRARIES)
244  list(GET METIS_LIBRARIES 0 first_lib)
245  get_filename_component(first_lib_path "${first_lib}" PATH)
246  if (${first_lib_path} MATCHES "/lib(32|64)?$")
247    string(REGEX REPLACE "/lib(32|64)?$" "" not_cached_dir "${first_lib_path}")
248    set(METIS_DIR_FOUND "${not_cached_dir}" CACHE PATH "Installation directory of METIS library" FORCE)
249  else()
250    set(METIS_DIR_FOUND "${first_lib_path}" CACHE PATH "Installation directory of METIS library" FORCE)
251  endif()
252endif()
253mark_as_advanced(METIS_DIR)
254mark_as_advanced(METIS_DIR_FOUND)
255
256# check that METIS has been found
257# ---------------------------------
258include(FindPackageHandleStandardArgs)
259find_package_handle_standard_args(METIS DEFAULT_MSG
260  METIS_LIBRARIES
261  METIS_WORKS)
262#
263# TODO: Add possibility to check for specific functions in the library
264#
265