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