1 # from http://zi.fi/cmake/Modules/LibFindMacros.cmake
3 # Works the same as find_package, but forwards the "REQUIRED" and "QUIET" arguments
4 # used for the current package. For this to work, the first parameter must be the
5 # prefix of the current package, then the prefix of the new package etc, which are
6 # passed to find_package.
7 macro (libfind_package PREFIX)
8 set (LIBFIND_PACKAGE_ARGS ${ARGN})
9 if (${PREFIX}_FIND_QUIETLY)
10 set (LIBFIND_PACKAGE_ARGS ${LIBFIND_PACKAGE_ARGS} QUIET)
11 endif (${PREFIX}_FIND_QUIETLY)
12 if (${PREFIX}_FIND_REQUIRED)
13 set (LIBFIND_PACKAGE_ARGS ${LIBFIND_PACKAGE_ARGS} REQUIRED)
14 endif (${PREFIX}_FIND_REQUIRED)
15 find_package(${LIBFIND_PACKAGE_ARGS})
16 endmacro (libfind_package)
18 # Damn CMake developers made the UsePkgConfig system deprecated in the same release (2.6)
19 # where they added pkg_check_modules. Consequently I need to support both in my scripts
20 # to avoid those deprecated warnings. Here's a helper that does just that.
21 # Works identically to pkg_check_modules, except that no checks are needed prior to use.
22 macro (libfind_pkg_check_modules PREFIX PKGNAME)
23 if (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
25 pkgconfig(${PKGNAME} ${PREFIX}_INCLUDE_DIRS ${PREFIX}_LIBRARY_DIRS ${PREFIX}_LDFLAGS ${PREFIX}_CFLAGS)
26 else (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
27 find_package(PkgConfig)
29 pkg_check_modules(${PREFIX} ${PKGNAME})
30 endif (PKG_CONFIG_FOUND)
31 endif (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
32 endmacro (libfind_pkg_check_modules)
34 # Do the final processing once the paths have been detected.
35 # If include dirs are needed, ${PREFIX}_PROCESS_INCLUDES should be set to contain
36 # all the variables, each of which contain one include directory.
37 # Ditto for ${PREFIX}_PROCESS_LIBS and library files.
38 # Will set ${PREFIX}_FOUND, ${PREFIX}_INCLUDE_DIRS and ${PREFIX}_LIBRARIES.
39 # Also handles errors in case library detection was required, etc.
40 macro (libfind_process PREFIX)
41 # Skip processing if already processed during this run
42 if (NOT ${PREFIX}_FOUND)
43 # Start with the assumption that the library was found
44 set (${PREFIX}_FOUND TRUE)
46 # Process all includes and set _FOUND to false if any are missing
47 foreach (i ${${PREFIX}_PROCESS_INCLUDES})
49 set (${PREFIX}_INCLUDE_DIRS ${${PREFIX}_INCLUDE_DIRS} ${${i}})
50 mark_as_advanced(${i})
52 set (${PREFIX}_FOUND FALSE)
56 # Process all libraries and set _FOUND to false if any are missing
57 foreach (i ${${PREFIX}_PROCESS_LIBS})
59 set (${PREFIX}_LIBRARIES ${${PREFIX}_LIBRARIES} ${${i}})
60 mark_as_advanced(${i})
62 set (${PREFIX}_FOUND FALSE)
66 # Print message and/or exit on fatal error
68 if (NOT ${PREFIX}_FIND_QUIETLY)
69 message (STATUS "Found ${PREFIX} ${${PREFIX}_VERSION}")
70 endif (NOT ${PREFIX}_FIND_QUIETLY)
71 else (${PREFIX}_FOUND)
72 if (${PREFIX}_FIND_REQUIRED)
73 foreach (i ${${PREFIX}_PROCESS_INCLUDES} ${${PREFIX}_PROCESS_LIBS})
74 message("${i}=${${i}}")
76 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.")
77 endif (${PREFIX}_FIND_REQUIRED)
78 endif (${PREFIX}_FOUND)
79 endif (NOT ${PREFIX}_FOUND)
80 endmacro (libfind_process)
82 macro(libfind_library PREFIX basename)
90 set(${PREFIX}_LIBNAMES ${basename}${TMP})
92 set(${PREFIX}_LIBNAMES ${basename}${TMP}-${ARGV2})
93 string(REGEX REPLACE "\\." "_" TMP ${${PREFIX}_LIBNAMES})
94 set(${PREFIX}_LIBNAMES ${${PREFIX}_LIBNAMES} ${TMP})
95 endif(${ARGC} GREATER 2)
96 find_library(${PREFIX}_LIBRARY
97 NAMES ${${PREFIX}_LIBNAMES}
98 PATHS ${${PREFIX}_PKGCONF_LIBRARY_DIRS}
100 endmacro(libfind_library)