FindHQR.cmake 10.5 KB
Newer Older
1
2
3
4
5
###
#
# @copyright (c) 2009-2014 The University of Tennessee and The University
#                          of Tennessee Research Foundation.
#                          All rights reserved.
6
# @copyright (c) 2012-2017 Inria. All rights reserved.
7
8
9
10
# @copyright (c) 2012-2014 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria, Univ. Bordeaux. All rights reserved.
#
###
#
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# - Find HQR include dirs and libraries
# Use this module by invoking find_package with the form:
#  find_package(HQR
#               [REQUIRED]) # Fail with error if hqr is not found
#
# This module finds headers and hqr library.
# Results are reported in variables:
#  HQR_FOUND           - True if headers and requested libraries were found
#  HQR_INCLUDE_DIRS    - hqr include directories
#  HQR_LIBRARY_DIRS    - Link directories for hqr libraries
#  HQR_LIBRARIES       - hqr component libraries to be linked
#
# The user can give specific paths where to find the libraries adding cmake
# options at configure (ex: cmake path/to/project -DHQR_DIR=path/to/hqr):
#  HQR_DIR             - Where to find the base directory of hqr
#  HQR_INCDIR          - Where to find the header files
#  HQR_LIBDIR          - Where to find the library files
# The module can also look for the following environment variables if paths
# are not given as cmake variable: HQR_DIR, HQR_INCDIR, HQR_LIBDIR
30
31

#=============================================================================
32
33
34
35
36
# Copyright 2012-2017 Inria
# Copyright 2012-2013 Emmanuel Agullo
# Copyright 2012-2013 Mathieu Faverge
# Copyright 2012      Cedric Castagnede
# Copyright 2013-2017 Florent Pruvost
37
38
#
# Distributed under the OSI-approved BSD License (the "License");
39
# see accompanying file MORSE-Copyright.txt for details.
40
41
42
43
44
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
45
# (To distribute this file outside of Morse, substitute the full
46
47
#  License text for the above reference.)

48
49
50
51
52
if (NOT HQR_FOUND)
  set(HQR_DIR "" CACHE PATH "Installation directory of HQR library")
  if (NOT HQR_FIND_QUIETLY)
    message(STATUS "A cache variable, namely HQR_DIR, has been set to specify the install directory of HQR")
  endif()
53
54
endif()

55
56
57
58
59
60
61
set(ENV_HQR_DIR "$ENV{HQR_DIR}")
set(ENV_HQR_INCDIR "$ENV{HQR_INCDIR}")
set(ENV_HQR_LIBDIR "$ENV{HQR_LIBDIR}")
set(HQR_GIVEN_BY_USER "FALSE")
if ( HQR_DIR OR ( HQR_INCDIR AND HQR_LIBDIR) OR ENV_HQR_DIR OR (ENV_HQR_INCDIR AND ENV_HQR_LIBDIR) )
  set(HQR_GIVEN_BY_USER "TRUE")
endif()
62

63
64
65
66
67
# Optionally use pkg-config to detect include/library dirs (if pkg-config is available)
# -------------------------------------------------------------------------------------
include(FindPkgConfig)
find_package(PkgConfig QUIET)
if(PKG_CONFIG_EXECUTABLE AND NOT HQR_GIVEN_BY_USER)
68

69
70
71
72
73
74
75
76
77
78
79
  pkg_search_module(HQR hqr)
  if (NOT HQR_FIND_QUIETLY)
    if (HQR_FOUND AND HQR_LIBRARIES)
      message(STATUS "Looking for HQR - found using PkgConfig")
      #if(NOT HQR_INCLUDE_DIRS)
      #    message("${Magenta}HQR_INCLUDE_DIRS is empty using PkgConfig."
      #        "Perhaps the path to hqr headers is already present in your"
      #        "C(PLUS)_INCLUDE_PATH environment variable.${ColourReset}")
      #endif()
    else()
      message(STATUS "${Magenta}Looking for HQR - not found using PkgConfig."
PRUVOST Florent's avatar
PRUVOST Florent committed
80
81
        "\n   Perhaps you should add the directory containing hqr.pc to the"
        "\n   PKG_CONFIG_PATH environment variable.${ColourReset}")
82
83
    endif()
  endif()
84

85
endif(PKG_CONFIG_EXECUTABLE AND NOT HQR_GIVEN_BY_USER)
86

87
if( (NOT PKG_CONFIG_EXECUTABLE) OR (PKG_CONFIG_EXECUTABLE AND NOT HQR_FOUND) OR (HQR_GIVEN_BY_USER) )
88

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
  if (NOT HQR_FIND_QUIETLY)
    message(STATUS "Looking for HQR - PkgConfig not used")
  endif()

  # Looking for include
  # -------------------

  # Add system include paths to search include
  # ------------------------------------------
  unset(_inc_env)
  set(ENV_HQR_DIR "$ENV{HQR_DIR}")
  set(ENV_HQR_INCDIR "$ENV{HQR_INCDIR}")
  if(ENV_HQR_INCDIR)
    list(APPEND _inc_env "${ENV_HQR_INCDIR}")
  elseif(ENV_HQR_DIR)
    list(APPEND _inc_env "${ENV_HQR_DIR}")
    list(APPEND _inc_env "${ENV_HQR_DIR}/include")
  else()
    if(WIN32)
      string(REPLACE ":" ";" _inc_env "$ENV{INCLUDE}")
    else()
      string(REPLACE ":" ";" _path_env "$ENV{INCLUDE}")
      list(APPEND _inc_env "${_path_env}")
      string(REPLACE ":" ";" _path_env "$ENV{C_INCLUDE_PATH}")
      list(APPEND _inc_env "${_path_env}")
      string(REPLACE ":" ";" _path_env "$ENV{CPATH}")
      list(APPEND _inc_env "${_path_env}")
      string(REPLACE ":" ";" _path_env "$ENV{INCLUDE_PATH}")
      list(APPEND _inc_env "${_path_env}")
    endif()
  endif()
  list(APPEND _inc_env "${CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES}")
  list(APPEND _inc_env "${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}")
  list(REMOVE_DUPLICATES _inc_env)

  # Try to find the hqr header in the given paths
  # -------------------------------------------------
  # call cmake macro to find the header path
  if(HQR_INCDIR)
    set(HQR_HQR.h_DIRS "HQR_libhqr.h_DIRS-NOTFOUND")
    find_path(HQR_libhqr.h_DIRS
      NAMES libhqr.h
      HINTS ${HQR_INCDIR})
  else()
    if(HQR_DIR)
      set(HQR_libhqr.h_DIRS "HQR_libhqr.h_DIRS-NOTFOUND")
      find_path(HQR_libhqr.h_DIRS
PRUVOST Florent's avatar
PRUVOST Florent committed
136
137
        NAMES libhqr.h
        HINTS ${HQR_DIR})
138
139
140
    else()
      set(HQR_libhqr.h_DIRS "HQR_libhqr.h_DIRS-NOTFOUND")
      find_path(HQR_libhqr.h_DIRS
PRUVOST Florent's avatar
PRUVOST Florent committed
141
142
143
        NAMES libhqr.h
        HINTS ${_inc_env}
        PATH_SUFFIXES "hqr")
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    endif()
  endif()
  mark_as_advanced(HQR_libhqr.h_DIRS)

  # Add path to cmake variable
  # ------------------------------------
  if (HQR_libhqr.h_DIRS)
    set(HQR_INCLUDE_DIRS "${HQR_libhqr.h_DIRS}")
  else ()
    set(HQR_INCLUDE_DIRS "HQR_INCLUDE_DIRS-NOTFOUND")
    if(NOT HQR_FIND_QUIETLY)
      message(STATUS "Looking for hqr -- libhqr.h not found")
    endif()
  endif ()

  if (HQR_INCLUDE_DIRS)
    list(REMOVE_DUPLICATES HQR_INCLUDE_DIRS)
  endif ()

  # Looking for lib
  # ---------------

  # Add system library paths to search lib
  # --------------------------------------
  unset(_lib_env)
  set(ENV_HQR_LIBDIR "$ENV{HQR_LIBDIR}")
  if(ENV_HQR_LIBDIR)
    list(APPEND _lib_env "${ENV_HQR_LIBDIR}")
  elseif(ENV_HQR_DIR)
    list(APPEND _lib_env "${ENV_HQR_DIR}")
    list(APPEND _lib_env "${ENV_HQR_DIR}/lib")
  else()
    if(WIN32)
      string(REPLACE ":" ";" _lib_env "$ENV{LIB}")
    else()
      if(APPLE)
PRUVOST Florent's avatar
PRUVOST Florent committed
180
        string(REPLACE ":" ";" _lib_env "$ENV{DYLD_LIBRARY_PATH}")
181
      else()
PRUVOST Florent's avatar
PRUVOST Florent committed
182
        string(REPLACE ":" ";" _lib_env "$ENV{LD_LIBRARY_PATH}")
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
      endif()
      list(APPEND _lib_env "${CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES}")
      list(APPEND _lib_env "${CMAKE_C_IMPLICIT_LINK_DIRECTORIES}")
    endif()
  endif()
  list(REMOVE_DUPLICATES _lib_env)

  # Try to find the hqr lib in the given paths
  # ----------------------------------------------

  # call cmake macro to find the lib path
  if(HQR_LIBDIR)
    set(HQR_hqr_LIBRARY "HQR_hqr_LIBRARY-NOTFOUND")
    find_library(HQR_hqr_LIBRARY
      NAMES hqr
      HINTS ${HQR_LIBDIR})
  else()
    if(HQR_DIR)
      set(HQR_hqr_LIBRARY "HQR_hqr_LIBRARY-NOTFOUND")
      find_library(HQR_hqr_LIBRARY
PRUVOST Florent's avatar
PRUVOST Florent committed
203
204
205
        NAMES hqr
        HINTS ${HQR_DIR}
        PATH_SUFFIXES lib lib32 lib64)
206
207
208
    else()
      set(HQR_hqr_LIBRARY "HQR_hqr_LIBRARY-NOTFOUND")
      find_library(HQR_hqr_LIBRARY
PRUVOST Florent's avatar
PRUVOST Florent committed
209
210
        NAMES hqr
        HINTS ${_lib_env})
211
212
213
    endif()
  endif()
  mark_as_advanced(HQR_hqr_LIBRARY)
214

215
216
217
218
219
220
221
222
223
224
225
226
227
228
  # If found, add path to cmake variable
  # ------------------------------------
  if (HQR_hqr_LIBRARY)
    get_filename_component(hqr_lib_path ${HQR_hqr_LIBRARY} PATH)
    # set cmake variables (respects naming convention)
    set(HQR_LIBRARIES    "${HQR_hqr_LIBRARY}")
    set(HQR_LIBRARY_DIRS "${hqr_lib_path}")
  else ()
    set(HQR_LIBRARIES    "HQR_LIBRARIES-NOTFOUND")
    set(HQR_LIBRARY_DIRS "HQR_LIBRARY_DIRS-NOTFOUND")
    if(NOT HQR_FIND_QUIETLY)
      message(STATUS "Looking for hqr -- lib hqr not found")
    endif()
  endif ()
229

230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
  if (HQR_LIBRARY_DIRS)
    list(REMOVE_DUPLICATES HQR_LIBRARY_DIRS)
  endif ()

  # check a function to validate the find
  if(HQR_LIBRARIES)

    set(REQUIRED_INCDIRS)
    set(REQUIRED_LIBDIRS)
    set(REQUIRED_LIBS)

    # HQR
    if (HQR_INCLUDE_DIRS)
      set(REQUIRED_INCDIRS "${HQR_INCLUDE_DIRS}")
    endif()
    if (HQR_LIBRARY_DIRS)
      set(REQUIRED_LIBDIRS "${HQR_LIBRARY_DIRS}")
    endif()
    set(REQUIRED_LIBS "${HQR_LIBRARIES}")

    # set required libraries for link
    set(CMAKE_REQUIRED_INCLUDES "${REQUIRED_INCDIRS}")
    set(CMAKE_REQUIRED_LIBRARIES)
    foreach(lib_dir ${REQUIRED_LIBDIRS})
      list(APPEND CMAKE_REQUIRED_LIBRARIES "-L${lib_dir}")
    endforeach()
    list(APPEND CMAKE_REQUIRED_LIBRARIES "${REQUIRED_LIBS}")
    string(REGEX REPLACE "^ -" "-" CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES}")

    # test link
    unset(HQR_WORKS CACHE)
    include(CheckFunctionExists)
262
    check_function_exists(libhqr_init_hqr HQR_WORKS)
263
264
265
266
    mark_as_advanced(HQR_WORKS)

    if(NOT HQR_WORKS)
      if(NOT HQR_FIND_QUIETLY)
PRUVOST Florent's avatar
PRUVOST Florent committed
267
268
269
270
        message(STATUS "Looking for hqr : test of libhqr_hqr_init with hqr library fails")
        message(STATUS "CMAKE_REQUIRED_LIBRARIES: ${CMAKE_REQUIRED_LIBRARIES}")
        message(STATUS "CMAKE_REQUIRED_INCLUDES: ${CMAKE_REQUIRED_INCLUDES}")
        message(STATUS "Check in CMakeFiles/CMakeError.log to figure out why it fails")
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
      endif()
    endif()
    set(CMAKE_REQUIRED_INCLUDES)
    set(CMAKE_REQUIRED_FLAGS)
    set(CMAKE_REQUIRED_LIBRARIES)
  endif(HQR_LIBRARIES)

endif( (NOT PKG_CONFIG_EXECUTABLE) OR (PKG_CONFIG_EXECUTABLE AND NOT HQR_FOUND) OR (HQR_GIVEN_BY_USER) )

if (HQR_LIBRARIES)
  if (HQR_LIBRARY_DIRS)
    list(GET HQR_LIBRARY_DIRS 0 first_lib_path)
  else()
    list(GET HQR_LIBRARIES 0 first_lib)
    get_filename_component(first_lib_path "${first_lib}" PATH)
  endif()
  if (${first_lib_path} MATCHES "/lib(32|64)?$")
    string(REGEX REPLACE "/lib(32|64)?$" "" not_cached_dir "${first_lib_path}")
    set(HQR_DIR_FOUND "${not_cached_dir}" CACHE PATH "Installation directory of HQR library" FORCE)
  else()
    set(HQR_DIR_FOUND "${first_lib_path}" CACHE PATH "Installation directory of HQR library" FORCE)
  endif()
endif()
mark_as_advanced(HQR_DIR)
mark_as_advanced(HQR_DIR_FOUND)

# check that HQR has been found
# -------------------------------
include(FindPackageHandleStandardArgs)
if (PKG_CONFIG_EXECUTABLE AND HQR_FOUND)
  find_package_handle_standard_args(HQR DEFAULT_MSG
    HQR_LIBRARIES)
else()
  find_package_handle_standard_args(HQR DEFAULT_MSG
    HQR_LIBRARIES
    HQR_WORKS)
endif()