FindSCOTCH.cmake 12 KB

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