FindMetis.cmake 8.8 KB

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