LibFindMacros.cmake 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. # CMake developers made the UsePkgConfig system deprecated in the same release (2.6)
  16. # where they added pkg_check_modules. Consequently I need to support both in my scripts
  17. # to avoid those deprecated warnings. Here's a helper that does just that.
  18. # Works identically to pkg_check_modules, except that no checks are needed prior to use.
  19. macro (libfind_pkg_check_modules PREFIX PKGNAME)
  20. if (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
  21. include(UsePkgConfig)
  22. pkgconfig(${PKGNAME} ${PREFIX}_INCLUDE_DIRS ${PREFIX}_LIBRARY_DIRS ${PREFIX}_LDFLAGS ${PREFIX}_CFLAGS)
  23. else (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
  24. find_package(PkgConfig)
  25. if (PKG_CONFIG_FOUND)
  26. pkg_check_modules(${PREFIX} ${PKGNAME})
  27. endif (PKG_CONFIG_FOUND)
  28. endif (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
  29. endmacro (libfind_pkg_check_modules)
  30. # Do the final processing once the paths have been detected.
  31. # If include dirs are needed, ${PREFIX}_PROCESS_INCLUDES should be set to contain
  32. # all the variables, each of which contain one include directory.
  33. # Ditto for ${PREFIX}_PROCESS_LIBS and library files.
  34. # Will set ${PREFIX}_FOUND, ${PREFIX}_INCLUDE_DIRS and ${PREFIX}_LIBRARIES.
  35. # Also handles errors in case library detection was required, etc.
  36. macro (libfind_process PREFIX)
  37. # Skip processing if already processed during this run
  38. if (NOT ${PREFIX}_FOUND)
  39. # Start with the assumption that the library was found
  40. set (${PREFIX}_FOUND TRUE)
  41. # Process all includes and set _FOUND to false if any are missing
  42. foreach (i ${${PREFIX}_PROCESS_INCLUDES})
  43. if (${i})
  44. set (${PREFIX}_INCLUDE_DIRS ${${PREFIX}_INCLUDE_DIRS} ${${i}})
  45. mark_as_advanced(${i})
  46. else (${i})
  47. set (${PREFIX}_FOUND FALSE)
  48. endif (${i})
  49. endforeach (i)
  50. # Process all libraries and set _FOUND to false if any are missing
  51. foreach (i ${${PREFIX}_PROCESS_LIBS})
  52. if (${i})
  53. set (${PREFIX}_LIBRARIES ${${PREFIX}_LIBRARIES} ${${i}})
  54. mark_as_advanced(${i})
  55. else (${i})
  56. set (${PREFIX}_FOUND FALSE)
  57. endif (${i})
  58. endforeach (i)
  59. # Print message and/or exit on fatal error
  60. if (${PREFIX}_FOUND)
  61. if (NOT ${PREFIX}_FIND_QUIETLY)
  62. message (STATUS "Found ${PREFIX} ${${PREFIX}_VERSION}")
  63. endif (NOT ${PREFIX}_FIND_QUIETLY)
  64. else (${PREFIX}_FOUND)
  65. if (${PREFIX}_FIND_REQUIRED)
  66. foreach (i ${${PREFIX}_PROCESS_INCLUDES} ${${PREFIX}_PROCESS_LIBS})
  67. message("${i}=${${i}}")
  68. endforeach (i)
  69. 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.")
  70. endif (${PREFIX}_FIND_REQUIRED)
  71. endif (${PREFIX}_FOUND)
  72. endif (NOT ${PREFIX}_FOUND)
  73. endmacro (libfind_process)
  74. macro(libfind_library PREFIX basename)
  75. set(TMP "")
  76. if(MSVC80)
  77. set(TMP -vc80)
  78. endif(MSVC80)
  79. if(MSVC90)
  80. set(TMP -vc90)
  81. endif(MSVC90)
  82. set(${PREFIX}_LIBNAMES ${basename}${TMP})
  83. if(${ARGC} GREATER 2)
  84. set(${PREFIX}_LIBNAMES ${basename}${TMP}-${ARGV2})
  85. string(REGEX REPLACE "\\." "_" TMP ${${PREFIX}_LIBNAMES})
  86. set(${PREFIX}_LIBNAMES ${${PREFIX}_LIBNAMES} ${TMP})
  87. endif(${ARGC} GREATER 2)
  88. find_library(${PREFIX}_LIBRARY
  89. NAMES ${${PREFIX}_LIBNAMES}
  90. PATHS ${${PREFIX}_PKGCONF_LIBRARY_DIRS}
  91. )
  92. endmacro(libfind_library)