added CMake modules
[gpivtools.git] / CMake / Modules / LibFindMacros.cmake
blob580705011b783ec2420a2fea8ccb213246e51d2b
1 # Works the same as find_package, but forwards the "REQUIRED" and "QUIET" arguments
2 # used for the current package. For this to work, the first parameter must be the
3 # prefix of the current package, then the prefix of the new package etc, which are
4 # passed to find_package.
5 macro (libfind_package PREFIX)
6   set (LIBFIND_PACKAGE_ARGS ${ARGN})
7   if (${PREFIX}_FIND_QUIETLY)
8     set (LIBFIND_PACKAGE_ARGS ${LIBFIND_PACKAGE_ARGS} QUIET)
9   endif (${PREFIX}_FIND_QUIETLY)
10   if (${PREFIX}_FIND_REQUIRED)
11     set (LIBFIND_PACKAGE_ARGS ${LIBFIND_PACKAGE_ARGS} REQUIRED)
12   endif (${PREFIX}_FIND_REQUIRED)
13   find_package(${LIBFIND_PACKAGE_ARGS})
14 endmacro (libfind_package)
16 # Damn CMake developers made the UsePkgConfig system deprecated in the same release (2.6)
17 # where they added pkg_check_modules. Consequently I need to support both in my scripts
18 # to avoid those deprecated warnings. Here's a helper that does just that.
19 # Works identically to pkg_check_modules, except that no checks are needed prior to use.
20 macro (libfind_pkg_check_modules PREFIX PKGNAME)
21   if (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
22     include(UsePkgConfig)
23     pkgconfig(${PKGNAME} ${PREFIX}_INCLUDE_DIRS ${PREFIX}_LIBRARY_DIRS ${PREFIX}_LDFLAGS ${PREFIX}_CFLAGS)
24   else (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
25     find_package(PkgConfig)
26     if (PKG_CONFIG_FOUND)
27       pkg_check_modules(${PREFIX} ${PKGNAME})
28     endif (PKG_CONFIG_FOUND)
29   endif (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
30 endmacro (libfind_pkg_check_modules)
32 # Do the final processing once the paths have been detected.
33 # If include dirs are needed, ${PREFIX}_PROCESS_INCLUDES should be set to contain
34 # all the variables, each of which contain one include directory.
35 # Ditto for ${PREFIX}_PROCESS_LIBS and library files.
36 # Will set ${PREFIX}_FOUND, ${PREFIX}_INCLUDE_DIRS and ${PREFIX}_LIBRARIES.
37 # Also handles errors in case library detection was required, etc.
38 macro (libfind_process PREFIX)
39   # Skip processing if already processed during this run
40   if (NOT ${PREFIX}_FOUND)
41     # Start with the assumption that the library was found
42     set (${PREFIX}_FOUND TRUE)
44     # Process all includes and set _FOUND to false if any are missing
45     foreach (i ${${PREFIX}_PROCESS_INCLUDES})
46       if (${i})
47         set (${PREFIX}_INCLUDE_DIRS ${${PREFIX}_INCLUDE_DIRS} ${${i}})
48       else (${i})
49         set (${PREFIX}_FOUND FALSE)
50       endif (${i})
51     endforeach (i)
53     # Process all libraries and set _FOUND to false if any are missing
54     foreach (i ${${PREFIX}_PROCESS_LIBS})
55       if (${i})
56         set (${PREFIX}_LIBRARIES ${${PREFIX}_LIBRARIES} ${${i}})
57       else (${i})
58         set (${PREFIX}_FOUND FALSE)
59       endif (${i})
60     endforeach (i)
62     # Print message and/or exit on fatal error
63     if (${PREFIX}_FOUND)
64       if (NOT ${PREFIX}_FIND_QUIETLY)
65         message (STATUS "Found ${PREFIX}")
66       endif (NOT ${PREFIX}_FIND_QUIETLY)
67     else (${PREFIX}_FOUND)
68       if (${PREFIX}_FIND_REQUIRED)
69         foreach (i ${${PREFIX}_PROCESS_INCLUDES} ${${PREFIX}_PROCESS_LIBS})
70           message("${i}=${${i}}")
71         endforeach (i)
72         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.")
73       endif (${PREFIX}_FIND_REQUIRED)
74     endif (${PREFIX}_FOUND)
75   endif (NOT ${PREFIX}_FOUND)
76 endmacro (libfind_process)