LibFindMacros.cmake 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Version 1.0 (2013-04-12)
  2. # Public Domain, originally written by Lasse Kärkkäinen <tronic@zi.fi>
  3. # Published at http://www.cmake.org/Wiki/CMake:How_To_Find_Libraries
  4. # If you improve the script, please modify the forementioned wiki page because
  5. # I no longer maintain my scripts (hosted as static files at zi.fi). Feel free
  6. # to remove this entire header if you use real version control instead.
  7. # Changelog:
  8. # 2013-04-12 Added version number (1.0) and this header, no other changes
  9. # 2009-10-08 Originally published
  10. # Works the same as find_package, but forwards the "REQUIRED" and "QUIET" arguments
  11. # used for the current package. For this to work, the first parameter must be the
  12. # prefix of the current package, then the prefix of the new package etc, which are
  13. # passed to find_package.
  14. macro (libfind_package PREFIX)
  15. set (LIBFIND_PACKAGE_ARGS ${ARGN})
  16. if (${PREFIX}_FIND_QUIETLY)
  17. set (LIBFIND_PACKAGE_ARGS ${LIBFIND_PACKAGE_ARGS} QUIET)
  18. endif (${PREFIX}_FIND_QUIETLY)
  19. if (${PREFIX}_FIND_REQUIRED)
  20. set (LIBFIND_PACKAGE_ARGS ${LIBFIND_PACKAGE_ARGS} REQUIRED)
  21. endif (${PREFIX}_FIND_REQUIRED)
  22. find_package(${LIBFIND_PACKAGE_ARGS})
  23. endmacro (libfind_package)
  24. # CMake developers made the UsePkgConfig system deprecated in the same release (2.6)
  25. # where they added pkg_check_modules. Consequently I need to support both in my scripts
  26. # to avoid those deprecated warnings. Here's a helper that does just that.
  27. # Works identically to pkg_check_modules, except that no checks are needed prior to use.
  28. macro (libfind_pkg_check_modules PREFIX PKGNAME)
  29. if (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
  30. include(UsePkgConfig)
  31. pkgconfig(${PKGNAME} ${PREFIX}_INCLUDE_DIRS ${PREFIX}_LIBRARY_DIRS ${PREFIX}_LDFLAGS ${PREFIX}_CFLAGS)
  32. else (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
  33. find_package(PkgConfig)
  34. if (PKG_CONFIG_FOUND)
  35. pkg_check_modules(${PREFIX} ${PKGNAME})
  36. endif (PKG_CONFIG_FOUND)
  37. endif (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
  38. endmacro (libfind_pkg_check_modules)
  39. # Do the final processing once the paths have been detected.
  40. # If include dirs are needed, ${PREFIX}_PROCESS_INCLUDES should be set to contain
  41. # all the variables, each of which contain one include directory.
  42. # Ditto for ${PREFIX}_PROCESS_LIBS and library files.
  43. # Will set ${PREFIX}_FOUND, ${PREFIX}_INCLUDE_DIRS and ${PREFIX}_LIBRARIES.
  44. # Also handles errors in case library detection was required, etc.
  45. macro (libfind_process PREFIX)
  46. # Skip processing if already processed during this run
  47. if (NOT ${PREFIX}_FOUND)
  48. # Start with the assumption that the library was found
  49. set (${PREFIX}_FOUND TRUE)
  50. # Process all includes and set _FOUND to false if any are missing
  51. foreach (i ${${PREFIX}_PROCESS_INCLUDES})
  52. if (${i})
  53. set (${PREFIX}_INCLUDE_DIRS ${${PREFIX}_INCLUDE_DIRS} ${${i}})
  54. mark_as_advanced(${i})
  55. else (${i})
  56. set (${PREFIX}_FOUND FALSE)
  57. endif (${i})
  58. endforeach (i)
  59. # Process all libraries and set _FOUND to false if any are missing
  60. foreach (i ${${PREFIX}_PROCESS_LIBS})
  61. if (${i})
  62. set (${PREFIX}_LIBRARIES ${${PREFIX}_LIBRARIES} ${${i}})
  63. mark_as_advanced(${i})
  64. else (${i})
  65. set (${PREFIX}_FOUND FALSE)
  66. endif (${i})
  67. endforeach (i)
  68. # Print message and/or exit on fatal error
  69. if (${PREFIX}_FOUND)
  70. if (NOT ${PREFIX}_FIND_QUIETLY)
  71. message (STATUS "Found ${PREFIX} ${${PREFIX}_VERSION}")
  72. endif (NOT ${PREFIX}_FIND_QUIETLY)
  73. else (${PREFIX}_FOUND)
  74. if (${PREFIX}_FIND_REQUIRED)
  75. foreach (i ${${PREFIX}_PROCESS_INCLUDES} ${${PREFIX}_PROCESS_LIBS})
  76. message("${i}=${${i}}")
  77. endforeach (i)
  78. 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.")
  79. endif (${PREFIX}_FIND_REQUIRED)
  80. endif (${PREFIX}_FOUND)
  81. endif (NOT ${PREFIX}_FOUND)
  82. endmacro (libfind_process)
  83. macro(libfind_library PREFIX basename)
  84. set(TMP "")
  85. if(MSVC80)
  86. set(TMP -vc80)
  87. endif(MSVC80)
  88. if(MSVC90)
  89. set(TMP -vc90)
  90. endif(MSVC90)
  91. set(${PREFIX}_LIBNAMES ${basename}${TMP})
  92. if(${ARGC} GREATER 2)
  93. set(${PREFIX}_LIBNAMES ${basename}${TMP}-${ARGV2})
  94. string(REGEX REPLACE "\\." "_" TMP ${${PREFIX}_LIBNAMES})
  95. set(${PREFIX}_LIBNAMES ${${PREFIX}_LIBNAMES} ${TMP})
  96. endif(${ARGC} GREATER 2)
  97. find_library(${PREFIX}_LIBRARY
  98. NAMES ${${PREFIX}_LIBNAMES}
  99. PATHS ${${PREFIX}_PKGCONF_LIBRARY_DIRS}
  100. )
  101. endmacro(libfind_library)