FindPTSCOTCH.cmake 14 KB

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