cmake/Modules/LibFindMacros.cmake
author Tero Marttila <terom@fixme.fi>
Thu, 28 May 2009 01:17:36 +0300
branchnew-lib-errors
changeset 219 cefec18b8268
parent 59 375a3b5c3a46
permissions -rw-r--r--
some of the lib/transport stuff compiles
59
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
# Works the same as find_package, but forwards the "REQUIRED" and "QUIET" arguments
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
# used for the current package. For this to work, the first parameter must be the
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
# prefix of the current package, then the prefix of the new package etc, which are
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
# passed to find_package.
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
macro (libfind_package PREFIX)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
  set (LIBFIND_PACKAGE_ARGS ${ARGN})
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
  if (${PREFIX}_FIND_QUIETLY)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
    set (LIBFIND_PACKAGE_ARGS ${LIBFIND_PACKAGE_ARGS} QUIET)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
  endif (${PREFIX}_FIND_QUIETLY)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
  if (${PREFIX}_FIND_REQUIRED)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
    set (LIBFIND_PACKAGE_ARGS ${LIBFIND_PACKAGE_ARGS} REQUIRED)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
  endif (${PREFIX}_FIND_REQUIRED)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
  find_package(${LIBFIND_PACKAGE_ARGS})
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
endmacro (libfind_package)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
# Damn CMake developers made the UsePkgConfig system deprecated in the same release (2.6)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
# where they added pkg_check_modules. Consequently I need to support both in my scripts
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
# to avoid those deprecated warnings. Here's a helper that does just that.
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
# Works identically to pkg_check_modules, except that no checks are needed prior to use.
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
macro (libfind_pkg_check_modules PREFIX PKGNAME)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
  if (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
    include(UsePkgConfig)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
    pkgconfig(${PKGNAME} ${PREFIX}_INCLUDE_DIRS ${PREFIX}_LIBRARY_DIRS ${PREFIX}_LDFLAGS ${PREFIX}_CFLAGS)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
  else (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
    find_package(PkgConfig)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
    if (PKG_CONFIG_FOUND)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
      pkg_check_modules(${PREFIX} ${PKGNAME})
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
    endif (PKG_CONFIG_FOUND)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
  endif (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
endmacro (libfind_pkg_check_modules)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
# Do the final processing once the paths have been detected.
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
# If include dirs are needed, ${PREFIX}_PROCESS_INCLUDES should be set to contain
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
# all the variables, each of which contain one include directory.
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
# Ditto for ${PREFIX}_PROCESS_LIBS and library files.
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
# Will set ${PREFIX}_FOUND, ${PREFIX}_INCLUDE_DIRS and ${PREFIX}_LIBRARIES.
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
# Also handles errors in case library detection was required, etc.
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
macro (libfind_process PREFIX)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
  # Skip processing if already processed during this run
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
  if (NOT ${PREFIX}_FOUND)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
    # Start with the assumption that the library was found
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
    set (${PREFIX}_FOUND TRUE)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
    # Process all includes and set _FOUND to false if any are missing
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
    foreach (i ${${PREFIX}_PROCESS_INCLUDES})
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
      if (${i})
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
        set (${PREFIX}_INCLUDE_DIRS ${${PREFIX}_INCLUDE_DIRS} ${${i}})
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
        mark_as_advanced(${i})
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
      else (${i})
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
        set (${PREFIX}_FOUND FALSE)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
      endif (${i})
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
    endforeach (i)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
    # Process all libraries and set _FOUND to false if any are missing
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
    foreach (i ${${PREFIX}_PROCESS_LIBS})
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
      if (${i})
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
        set (${PREFIX}_LIBRARIES ${${PREFIX}_LIBRARIES} ${${i}})
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
        mark_as_advanced(${i})
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
      else (${i})
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
        set (${PREFIX}_FOUND FALSE)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
      endif (${i})
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
    endforeach (i)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
    # Print message and/or exit on fatal error
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
    if (${PREFIX}_FOUND)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
      if (NOT ${PREFIX}_FIND_QUIETLY)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
        message (STATUS "Found ${PREFIX} ${${PREFIX}_VERSION}")
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
      endif (NOT ${PREFIX}_FIND_QUIETLY)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
    else (${PREFIX}_FOUND)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
      if (${PREFIX}_FIND_REQUIRED)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
        foreach (i ${${PREFIX}_PROCESS_INCLUDES} ${${PREFIX}_PROCESS_LIBS})
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
          message("${i}=${${i}}")
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
        endforeach (i)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    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.")
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
      endif (${PREFIX}_FIND_REQUIRED)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
    endif (${PREFIX}_FOUND)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
  endif (NOT ${PREFIX}_FOUND)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
endmacro (libfind_process)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
macro(libfind_library PREFIX basename)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
  set(TMP "")
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
  if(MSVC80)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
    set(TMP -vc80)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
  endif(MSVC80)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
  if(MSVC90)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
    set(TMP -vc90)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
  endif(MSVC90)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
  set(${PREFIX}_LIBNAMES ${basename}${TMP})
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
  if(${ARGC} GREATER 2)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
    set(${PREFIX}_LIBNAMES ${basename}${TMP}-${ARGV2})
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
    string(REGEX REPLACE "\\." "_" TMP ${${PREFIX}_LIBNAMES})
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
    set(${PREFIX}_LIBNAMES ${${PREFIX}_LIBNAMES} ${TMP})
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
  endif(${ARGC} GREATER 2)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
  find_library(${PREFIX}_LIBRARY
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
    NAMES ${${PREFIX}_LIBNAMES}
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
    PATHS ${${PREFIX}_PKGCONF_LIBRARY_DIRS}
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
  )
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
endmacro(libfind_library)
375a3b5c3a46 add CMake build stuff... that was easy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99