]> www.ginac.de Git - ginac.git/blob - cmake/modules/FindCLN.cmake
Really finalize 1.8.4 release.
[ginac.git] / cmake / modules / FindCLN.cmake
1 # CLN_FOUND             CLN has been successfully found
2 # CLN_INCLUDE_DIR       the include directories
3 # CLN_LIBRARIES         CLN library and its dependencies (if any)
4
5 if (CLN_INCLUDE_DIR AND CLN_LIBRARIES)
6         set(CLN_FIND_QUIETLY TRUE)
7 endif()
8
9 function(_cl_get_version _out_major _out_minor _out_patch _cl_version_h)
10         file(STRINGS ${_cl_version_h} _cl_vinfo REGEX "^#define[\t ]+CL_VERSION_.*")
11         if (NOT _cl_vinfo)
12                 message(FATAL_ERROR "include file ${_cl_version_h} does not exist")
13         endif()
14         string(REGEX REPLACE "^.*CL_VERSION_MAJOR[ \t]+([0-9]+).*" "\\1" ${_out_major} "${_cl_vinfo}")
15         string(REGEX REPLACE "^.*CL_VERSION_MINOR[ \t]+([0-9]+).*" "\\1" ${_out_minor} "${_cl_vinfo}")
16         string(REGEX REPLACE "^.*CL_VERSION_PATCHLEVEL[ \t]+([0-9]+).*" "\\1" ${_out_patch} "${_cl_vinfo}")
17         if (NOT ${_out_major} MATCHES "[0-9]+")
18                 message(FATAL_ERROR "failed to determine CL_VERSION_MAJOR, "
19                                     "expected a number, got ${${_out_major}}")
20         endif()
21         if (NOT ${_out_minor} MATCHES "[0-9]+")
22                 message(FATAL_ERROR "failed to determine CL_VERSION_MINOR, "
23                                     "expected a number, got ${${_out_minor}}")
24         endif()
25         if (NOT ${_out_patch} MATCHES "[0-9]+")
26                 message(FATAL_ERROR "failed to determine CL_VERSION_PATCHLEVEL, "
27                                     "expected a number, got ${${_out_patch}}")
28         endif()
29         message(STATUS "found CLN [${_cl_version_h}], version ${${_out_major}}.${${_out_minor}}.${${_out_patch}}")
30         set(${_out_major} ${${_out_major}} PARENT_SCOPE)
31         set(${_out_minor} ${${_out_minor}} PARENT_SCOPE)
32         set(${_out_patch} ${${_out_patch}} PARENT_SCOPE)
33 endfunction()
34
35 set(CLN_FOUND)
36 set(CLN_INCLUDE_DIR)
37 set(CLN_LIBRARIES)
38
39 include(FindPkgConfig)
40 if (PKG_CONFIG_FOUND)
41         pkg_check_modules(_cln cln)
42 endif()
43
44 find_path(CLN_INCLUDE_DIR NAMES cln/cln.h
45                           HINTS ${_cln_INCLUDE_DIRS}
46                                 $ENV{CLN_DIR}/include)
47 find_library(CLN_LIBRARIES NAMES libcln cln
48                            HINTS ${_cln_LIBRARY_DIR}
49                                  ${_cln_LIBRARY_DIRS}
50                                  $ENV{CLN_DIR}/lib)
51
52 if (CLN_INCLUDE_DIR)
53         _cl_get_version(CLN_VERSION_MAJOR
54                         CLN_VERSION_MINOR
55                         CLN_VERSION_PATCHLEVEL
56                         ${CLN_INCLUDE_DIR}/cln/version.h)
57         set(CLN_VERSION ${CLN_VERSION_MAJOR}.${CLN_VERSION_MINOR}.${CLN_VERSION_PATCHLEVEL})
58         # Check if the version reported by pkg-config is the same
59         # as the one read from the header. This prevents us from
60         # picking the wrong version of CLN (say, if several versions
61         # are installed)
62         if (_cln_FOUND AND NOT CLN_VERSION VERSION_EQUAL _cln_VERSION)
63                 if (NOT CLN_FIND_QUIETLY)
64                         message(ERROR "pkg-config and version.h disagree, "
65                                       "${_cln_VERSION} vs ${CLN_VERSION}, "
66                                       "please check your installation")
67                 endif()
68                 set(CLN_LIBRARIES CLN-NOTFOUND)
69                 set(CLN_INCLUDE_DIR CLN-NOTFOUND)
70                 set(CLN_LIBRARY_DIRS)
71                 set(CLN_VERSION)
72         endif()
73 endif()
74
75 # Check if the version embedded into the library is the same as the one in the headers.
76 if (CLN_INCLUDE_DIR AND CLN_LIBRARIES AND NOT CMAKE_CROSSCOMPILING)
77         include(CheckCXXSourceRuns)
78         set(_save_required_includes ${CMAKE_REQUIRED_INCLUDES})
79         set(_save_required_libraries ${CMAKE_REQUIRED_LIBRARIES})
80         set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES} ${CLN_INCLUDE_DIR})
81         set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${CLN_LIBRARIES})
82         check_cxx_source_runs("
83                 #include <cln/version.h>
84                 int main() {
85                         return (CL_VERSION_MAJOR == cln::version_major) &&
86                                (CL_VERSION_MINOR == cln::version_minor) &&
87                                (CL_VERSION_PATCHLEVEL == cln::version_patchlevel) ? 0 : 1;
88                 }
89                 "
90                 _cl_version_matches)
91         set(CMAKE_REQUIRED_LIBRARIES ${_save_required_libraries})
92         set(CMAKE_REQUIRED_INCLUDES ${_save_required_includes})
93         if (NOT _cl_version_matches)
94                 if (NOT CLN_FIND_QUIETLY)
95                         message(ERROR "header (version differs from the library one, "
96                                       "please check your installation.")
97                 endif()
98                 set(CLN_INCLUDE_DIR CLN-NOTFOUND)
99                 set(CLN_LIBRARIES CLN-NOTFOUND)
100                 set(CLN_LIBRARY_DIRS)
101                 set(CLN_VERSION)
102         endif()
103 endif()
104
105 if (CLN_LIBRARIES AND CLN_INCLUDE_DIR)
106         set(_cln_library_dirs)
107         foreach(_l ${CLN_LIBRARIES})
108                 get_filename_component(_d "${_l}" PATH)
109                 list(APPEND _cln_library_dirs "${_d}")
110         endforeach()
111         list(REMOVE_DUPLICATES _cln_library_dirs)
112         set(CLN_LIBRARY_DIRS ${_cln_library_dirs})
113 endif()
114
115 include(FindPackageHandleStandardArgs)
116 FIND_PACKAGE_HANDLE_STANDARD_ARGS(CLN REQUIRED_VARS CLN_LIBRARIES CLN_INCLUDE_DIR
117                                       VERSION_VAR CLN_VERSION)
118
119 if (CLN_FOUND AND NOT TARGET cln::cln)
120         set(_found_shared_libcln FALSE)
121         get_filename_component(_libcln_suffix ${CLN_LIBRARIES} EXT)
122         if (_libcln_suffix STREQUAL ${CMAKE_STATIC_LIBRARY_SUFFIX})
123                 # XXX: msvc uses the same suffix for both static and import libraries
124                 add_library(cln::cln STATIC IMPORTED)
125         else()
126                 set(_found_shared_libcln TRUE)
127                 add_library(cln::cln SHARED IMPORTED)
128         endif()
129         set_target_properties(cln::cln PROPERTIES
130                 INTERFACE_INCLUDE_DIRECTORIES ${CLN_INCLUDE_DIR}
131         )
132         if (WIN32 AND _found_shared_libcln)
133                 set_target_properties(cln::cln PROPERTIES
134                         IMPORTED_IMPLIB ${CLN_LIBRARIES}
135                 )
136         else()
137                 set_target_properties(cln::cln PROPERTIES
138                         IMPORTED_LOCATION ${CLN_LIBRARIES}
139                 )
140         endif()
141 endif()