xref: /freebsd/contrib/libcbor/CMakeModules/LibFindMacros.cmake (revision 5956d97f4b3204318ceb6aa9c77bd0bc6ea87a41)
1#    Fetched from https://github.com/JonathanSalwan/Triton/blob/master/CMakeModules/LibFindMacros.cmake
2#
3#    Copyright:
4#
5#    * Jonathan Salwan (Quarkslab)
6#    * Pierrick Brunet (Quarkslab)
7#    * Romain Thomas (Quarkslab)
8#    * Florent Saudel (Bordeaux University)
9#
10#    Licensed under the Apache License, Version 2.0 (the "License");
11#    you may not use this file except in compliance with the License.
12#    You may obtain a copy of the License at
13#
14#    https://www.apache.org/licenses/LICENSE-2.0
15#
16#    Unless required by applicable law or agreed to in writing, software
17#    distributed under the License is distributed on an "AS IS" BASIS,
18#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19#    See the License for the specific language governing permissions and
20#    limitations under the License.
21
22# Works the same as find_package, but forwards the "REQUIRED" and "QUIET" arguments
23# used for the current package. For this to work, the first parameter must be the
24# prefix of the current package, then the prefix of the new package etc, which are
25# passed to find_package.
26macro (libfind_package PREFIX)
27    set (LIBFIND_PACKAGE_ARGS ${ARGN})
28    if (${PREFIX}_FIND_QUIETLY)
29        set (LIBFIND_PACKAGE_ARGS ${LIBFIND_PACKAGE_ARGS} QUIET)
30    endif (${PREFIX}_FIND_QUIETLY)
31    if (${PREFIX}_FIND_REQUIRED)
32        set (LIBFIND_PACKAGE_ARGS ${LIBFIND_PACKAGE_ARGS} REQUIRED)
33    endif (${PREFIX}_FIND_REQUIRED)
34    find_package(${LIBFIND_PACKAGE_ARGS})
35endmacro (libfind_package)
36
37# CMake developers made the UsePkgConfig system deprecated in the same release (2.6)
38# where they added pkg_check_modules. Consequently I need to support both in my scripts
39# to avoid those deprecated warnings. Here's a helper that does just that.
40# Works identically to pkg_check_modules, except that no checks are needed prior to use.
41macro (libfind_pkg_check_modules PREFIX PKGNAME)
42    if (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
43        include(UsePkgConfig)
44        pkgconfig(${PKGNAME} ${PREFIX}_INCLUDE_DIRS ${PREFIX}_LIBRARY_DIRS ${PREFIX}_LDFLAGS ${PREFIX}_CFLAGS)
45    else (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
46        find_package(PkgConfig)
47        if (PKG_CONFIG_FOUND)
48            pkg_check_modules(${PREFIX} ${PKGNAME})
49        endif (PKG_CONFIG_FOUND)
50    endif (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
51endmacro (libfind_pkg_check_modules)
52
53# Do the final processing once the paths have been detected.
54# If include dirs are needed, ${PREFIX}_PROCESS_INCLUDES should be set to contain
55# all the variables, each of which contain one include directory.
56# Ditto for ${PREFIX}_PROCESS_LIBS and library files.
57# Will set ${PREFIX}_FOUND, ${PREFIX}_INCLUDE_DIRS and ${PREFIX}_LIBRARIES.
58# Also handles errors in case library detection was required, etc.
59macro (libfind_process PREFIX)
60    # Skip processing if already processed during this run
61    if (NOT ${PREFIX}_FOUND)
62        # Start with the assumption that the library was found
63        set (${PREFIX}_FOUND TRUE)
64
65        # Process all includes and set _FOUND to false if any are missing
66        foreach (i ${${PREFIX}_PROCESS_INCLUDES})
67            if (${i})
68                set (${PREFIX}_INCLUDE_DIRS ${${PREFIX}_INCLUDE_DIRS} ${${i}})
69                mark_as_advanced(${i})
70            else (${i})
71                set (${PREFIX}_FOUND FALSE)
72            endif (${i})
73        endforeach (i)
74
75        # Process all libraries and set _FOUND to false if any are missing
76        foreach (i ${${PREFIX}_PROCESS_LIBS})
77            if (${i})
78                set (${PREFIX}_LIBRARIES ${${PREFIX}_LIBRARIES} ${${i}})
79                mark_as_advanced(${i})
80            else (${i})
81                set (${PREFIX}_FOUND FALSE)
82            endif (${i})
83        endforeach (i)
84
85        # Print message and/or exit on fatal error
86        if (${PREFIX}_FOUND)
87            if (NOT ${PREFIX}_FIND_QUIETLY)
88                message (STATUS "Found ${PREFIX} include directory: ${${PREFIX}_INCLUDE_DIR}")
89                message (STATUS "Found ${PREFIX} library: ${${PREFIX}_LIBRARY}")
90            endif (NOT ${PREFIX}_FIND_QUIETLY)
91        else (${PREFIX}_FOUND)
92            if (${PREFIX}_FIND_REQUIRED)
93                foreach (i ${${PREFIX}_PROCESS_INCLUDES} ${${PREFIX}_PROCESS_LIBS})
94                    message("${i}=${${i}}")
95                endforeach (i)
96                message (FATAL_ERROR "Required library ${PREFIX} NOT FOUND.\nInstall the library (dev version) and try again. If the library is already installed, use ccmake to set the missing variables manually.")
97            endif (${PREFIX}_FIND_REQUIRED)
98        endif (${PREFIX}_FOUND)
99    endif (NOT ${PREFIX}_FOUND)
100endmacro (libfind_process)
101
102macro(libfind_library PREFIX basename)
103    set(TMP "")
104    if(MSVC80)
105        set(TMP -vc80)
106    endif(MSVC80)
107    if(MSVC90)
108        set(TMP -vc90)
109    endif(MSVC90)
110    set(${PREFIX}_LIBNAMES ${basename}${TMP})
111    if(${ARGC} GREATER 2)
112        set(${PREFIX}_LIBNAMES ${basename}${TMP}-${ARGV2})
113        string(REGEX REPLACE "\\." "_" TMP ${${PREFIX}_LIBNAMES})
114        set(${PREFIX}_LIBNAMES ${${PREFIX}_LIBNAMES} ${TMP})
115    endif(${ARGC} GREATER 2)
116    find_library(${PREFIX}_LIBRARY
117            NAMES ${${PREFIX}_LIBNAMES}
118            PATHS ${${PREFIX}_PKGCONF_LIBRARY_DIRS}
119            )
120endmacro(libfind_library)
121