cmake/Modules/LibFindMacros.cmake
changeset 57 527d23bf6441
equal deleted inserted replaced
56:9dfc861273e5 57:527d23bf6441
       
     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)
       
    15 
       
    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)
       
    31 
       
    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)
       
    43 
       
    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         mark_as_advanced(${i})
       
    49       else (${i})
       
    50         set (${PREFIX}_FOUND FALSE)
       
    51       endif (${i})
       
    52     endforeach (i)
       
    53 
       
    54     # Process all libraries and set _FOUND to false if any are missing
       
    55     foreach (i ${${PREFIX}_PROCESS_LIBS})
       
    56       if (${i})
       
    57         set (${PREFIX}_LIBRARIES ${${PREFIX}_LIBRARIES} ${${i}})
       
    58         mark_as_advanced(${i})
       
    59       else (${i})
       
    60         set (${PREFIX}_FOUND FALSE)
       
    61       endif (${i})
       
    62     endforeach (i)
       
    63 
       
    64     # Print message and/or exit on fatal error
       
    65     if (${PREFIX}_FOUND)
       
    66       if (NOT ${PREFIX}_FIND_QUIETLY)
       
    67         message (STATUS "Found ${PREFIX} ${${PREFIX}_VERSION}")
       
    68       endif (NOT ${PREFIX}_FIND_QUIETLY)
       
    69     else (${PREFIX}_FOUND)
       
    70       if (${PREFIX}_FIND_REQUIRED)
       
    71         foreach (i ${${PREFIX}_PROCESS_INCLUDES} ${${PREFIX}_PROCESS_LIBS})
       
    72           message("${i}=${${i}}")
       
    73         endforeach (i)
       
    74         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.")
       
    75       endif (${PREFIX}_FIND_REQUIRED)
       
    76     endif (${PREFIX}_FOUND)
       
    77   endif (NOT ${PREFIX}_FOUND)
       
    78 endmacro (libfind_process)
       
    79 
       
    80 macro(libfind_library PREFIX basename)
       
    81   set(TMP "")
       
    82   if(MSVC80)
       
    83     set(TMP -vc80)
       
    84   endif(MSVC80)
       
    85   if(MSVC90)
       
    86     set(TMP -vc90)
       
    87   endif(MSVC90)
       
    88   set(${PREFIX}_LIBNAMES ${basename}${TMP})
       
    89   if(${ARGC} GREATER 2)
       
    90     set(${PREFIX}_LIBNAMES ${basename}${TMP}-${ARGV2})
       
    91     string(REGEX REPLACE "\\." "_" TMP ${${PREFIX}_LIBNAMES})
       
    92     set(${PREFIX}_LIBNAMES ${${PREFIX}_LIBNAMES} ${TMP})
       
    93   endif(${ARGC} GREATER 2)
       
    94   find_library(${PREFIX}_LIBRARY
       
    95     NAMES ${${PREFIX}_LIBNAMES}
       
    96     PATHS ${${PREFIX}_PKGCONF_LIBRARY_DIRS}
       
    97   )
       
    98 endmacro(libfind_library)
       
    99