1# ---------------------------------------------------------------
2# Programmer(s): Eddy Banks and David J. Gardner @ LLNL
3# ---------------------------------------------------------------
4# SUNDIALS Copyright Start
5# Copyright (c) 2002-2021, Lawrence Livermore National Security
6# and Southern Methodist University.
7# All rights reserved.
8#
9# See the top-level LICENSE and NOTICE files for details.
10#
11# SPDX-License-Identifier: BSD-3-Clause
12# SUNDIALS Copyright End
13# ---------------------------------------------------------------
14# SuperLUMT find module that creates an imported target for
15# SuperLU_MT. The target is SUNDIALS::SUPERLUMT.
16#
17# The variable SUPERLUMT_LIBRARY_DIR can be used to control
18# where the module looks for the library.
19#
20# The variable SUPERLUMT_INCLUDE_DIR can be used to set the
21# include path for the library.
22#
23# Additional libraries can be passed in SUPERLUMT_LIBRARIES.
24#
25# This module also defines variables, but it is best to use
26# the defined target to ensure includes and compile/link
27# options are correctly passed to consumers.
28#
29#   SUPERLUMT_FOUND       - system has SuperLU_MT library
30#   SUPERLUMT_LIBRARY     - the SuperLU_MT library
31#   SUPERLUMT_THREAD_TYPE - the SuperLU_MT threading
32# ---------------------------------------------------------------
33
34# check for valid thread type
35string(TOUPPER ${SUPERLUMT_THREAD_TYPE} _upper_SUPERLUMT_THREAD_TYPE)
36force_variable(SUPERLUMT_THREAD_TYPE STRING "SuperLU_MT threading type: OPENMP or PTHREAD" ${_upper_SUPERLUMT_THREAD_TYPE})
37
38if(SUPERLUMT_THREAD_TYPE AND
39    NOT SUPERLUMT_THREAD_TYPE STREQUAL "OPENMP" AND
40    NOT SUPERLUMT_THREAD_TYPE STREQUAL "PTHREAD")
41  print_error("Unknown thread type: ${SUPERLUMT_THREAD_TYPE}" "Please enter PTHREAD or OPENMP")
42endif()
43
44# check if the threading library has been found
45if(SUPERLUMT_THREAD_TYPE STREQUAL "PTHREAD")
46  # find pthread libraries
47  if(NOT PTHREADS_FOUND)
48    find_package(Threads REQUIRED)
49    if(CMAKE_USE_PTHREADS_INIT)
50      set(PTHREADS_FOUND TRUE)
51      message(STATUS "Using Pthreads")
52    else()
53      set(PTHREADS_FOUND FALSE)
54      print_error("Could not determine Pthreads compiler flags")
55    endif()
56  endif()
57else(SUPERLUMT_THREAD_TYPE STREQUAL "OPENMP")
58  # find openmp libraries
59  if(NOT OPENMP_FOUND)
60    find_package(OpenMP REQUIRED)
61  endif()
62endif()
63
64# Set SuperLU_MT library name with thread type postfix
65set(SUPERLUMT_LIBRARY_NAME superlu_mt_${SUPERLUMT_THREAD_TYPE})
66
67if(MSVC)
68  set(CMAKE_FIND_LIBRARY_PREFIXES lib ${CMAKE_FIND_LIBRARY_PREFIXES})
69endif()
70
71# Check if SUPERLUMT_LIBRARIES contains the superlu_mt
72# library as well as TPLs. If so, extract it into the
73# SUPERLUMT_LIBRARY variable.
74if(SUPERLUMT_LIBRARIES MATCHES "${SUPERLUMT_LIBRARY_NAME}")
75  foreach(lib ${SUPERLUMT_LIBRARIES})
76    if(lib MATCHES "${SUPERLUMT_LIBRARY_NAME}")
77      set(SUPERLUMT_LIBRARY ${lib})
78    endif()
79  endforeach()
80endif()
81
82# find library
83if(NOT SUPERLUMT_LIBRARY)
84  # search user provided directory path
85  find_library(SUPERLUMT_LIBRARY ${SUPERLUMT_LIBRARY_NAME}
86    PATHS ${SUPERLUMT_LIBRARY_DIR} NO_DEFAULT_PATH)
87  # if user didn't provide a path, search anywhere
88  if(NOT (SUPERLUMT_LIBRARY_DIR OR SUPERLUMT_LIBRARY))
89    find_library(SUPERLUMT_LIBRARY ${SUPERLUMT_LIBRARY_NAME})
90  endif()
91  mark_as_advanced(SUPERLUMT_LIBRARY)
92endif()
93
94# set the libraries, stripping out 'NOTFOUND' from previous attempts
95string(REPLACE "SUPERLUMT_LIBRARY-NOTFOUND" "" SUPERLUMT_LIBRARIES "${SUPERLUMT_LIBRARIES}")
96set(SUPERLUMT_LIBRARIES "${SUPERLUMT_LIBRARY};${SUPERLUMT_LIBRARIES}" CACHE STRING "" FORCE)
97
98# set the library dir option if it wasn't preset
99if(SUPERLUMT_LIBRARY AND (NOT SUPERLUMT_LIBRARY_DIR))
100  get_filename_component(SUPERLUMT_LIBRARY_DIR ${SUPERLUMT_LIBRARY} DIRECTORY)
101  set(SUPERLUMT_LIBRARY_DIR ${SUPERLUMT_LIBRARY_DIR} CACHE PATH "" FORCE)
102endif()
103
104# set the include dir option if it wasn't preset
105if(SUPERLUMT_LIBRARY AND (NOT SUPERLUMT_INCLUDE_DIR))
106  get_filename_component(SUPERLUMT_INCLUDE_DIR ${SUPERLUMT_LIBRARY_DIR} DIRECTORY)
107  set(SUPERLUMT_INCLUDE_DIR "${SUPERLUMT_INCLUDE_DIR}/include" CACHE PATH "" FORCE)
108endif()
109
110# set a more informative error message in case the library was not found
111set(SUPERLUMT_NOT_FOUND_MESSAGE "\
112************************************************************************\n\
113ERROR: Could not find SuperLU_MT. Please check the variables:\n\
114       SUPERLUMT_INCLUDE_DIR and SUPERLUMT_LIBRARY_DIR\n\
115************************************************************************")
116
117# set package variables including SUPERLUMT_FOUND
118find_package_handle_standard_args(SUPERLUMT
119  REQUIRED_VARS
120    SUPERLUMT_LIBRARY
121    SUPERLUMT_LIBRARIES
122    SUPERLUMT_INCLUDE_DIR
123    SUPERLUMT_THREAD_TYPE
124  FAIL_MESSAGE
125    "${SUPERLUMT_NOT_FOUND_MESSAGE}"
126  )
127
128# Create target for SuperLU_MT
129if(SUPERLUMT_FOUND)
130
131  if(NOT TARGET SUNDIALS::SUPERLUMT)
132    add_library(SUNDIALS::SUPERLUMT UNKNOWN IMPORTED)
133  endif()
134
135  set_target_properties(SUNDIALS::SUPERLUMT PROPERTIES
136    INTERFACE_INCLUDE_DIRECTORIES "${SUPERLUMT_INCLUDE_DIR}"
137    INTERFACE_LINK_LIBRARIES "${SUPERLUMT_LIBRARIES}"
138    IMPORTED_LOCATION "${SUPERLUMT_LIBRARY}")
139
140endif()
141