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