LibFindMacros.cmake 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. # Version 2.2
  2. # Public Domain, originally written by Lasse Kärkkäinen <tronic>
  3. # Maintained at https://github.com/Tronic/cmake-modules
  4. # Please send your improvements as pull requests on Github.
  5. # Find another package and make it a dependency of the current package.
  6. # This also automatically forwards the "REQUIRED" argument.
  7. # Usage: libfind_package(<prefix> <another package> [extra args to find_package])
  8. macro (libfind_package PREFIX PKG)
  9. set(${PREFIX}_args ${PKG} ${ARGN})
  10. if (${PREFIX}_FIND_REQUIRED)
  11. set(${PREFIX}_args ${${PREFIX}_args} REQUIRED)
  12. endif()
  13. find_package(${${PREFIX}_args})
  14. set(${PREFIX}_DEPENDENCIES ${${PREFIX}_DEPENDENCIES};${PKG})
  15. unset(${PREFIX}_args)
  16. endmacro()
  17. # A simple wrapper to make pkg-config searches a bit easier.
  18. # Works the same as CMake's internal pkg_check_modules but is always quiet.
  19. macro (libfind_pkg_check_modules)
  20. find_package(PkgConfig QUIET)
  21. if (PKG_CONFIG_FOUND)
  22. pkg_check_modules(${ARGN} QUIET)
  23. endif()
  24. endmacro()
  25. # Avoid useless copy&pasta by doing what most simple libraries do anyway:
  26. # pkg-config, find headers, find library.
  27. # Usage: libfind_pkg_detect(<prefix> <pkg-config args> FIND_PATH <name> [other args] FIND_LIBRARY <name> [other args])
  28. # E.g. libfind_pkg_detect(SDL2 sdl2 FIND_PATH SDL.h PATH_SUFFIXES SDL2 FIND_LIBRARY SDL2)
  29. function (libfind_pkg_detect PREFIX)
  30. # Parse arguments
  31. set(argname pkgargs)
  32. foreach (i ${ARGN})
  33. if ("${i}" STREQUAL "FIND_PATH")
  34. set(argname pathargs)
  35. elseif ("${i}" STREQUAL "FIND_LIBRARY")
  36. set(argname libraryargs)
  37. else()
  38. set(${argname} ${${argname}} ${i})
  39. endif()
  40. endforeach()
  41. if (NOT pkgargs)
  42. message(FATAL_ERROR "libfind_pkg_detect requires at least a pkg_config package name to be passed.")
  43. endif()
  44. # Find library
  45. libfind_pkg_check_modules(${PREFIX}_PKGCONF ${pkgargs})
  46. if (pathargs)
  47. find_path(${PREFIX}_INCLUDE_DIR NAMES ${pathargs} HINTS ${${PREFIX}_PKGCONF_INCLUDE_DIRS})
  48. endif()
  49. if (libraryargs)
  50. find_library(${PREFIX}_LIBRARY NAMES ${libraryargs} HINTS ${${PREFIX}_PKGCONF_LIBRARY_DIRS})
  51. endif()
  52. endfunction()
  53. # Extracts a version #define from a version.h file, output stored to <PREFIX>_VERSION.
  54. # Usage: libfind_version_header(Foobar foobar/version.h FOOBAR_VERSION_STR)
  55. # Fourth argument "QUIET" may be used for silently testing different define names.
  56. # This function does nothing if the version variable is already defined.
  57. function (libfind_version_header PREFIX VERSION_H DEFINE_NAME)
  58. # Skip processing if we already have a version or if the include dir was not found
  59. if (${PREFIX}_VERSION OR NOT ${PREFIX}_INCLUDE_DIR)
  60. return()
  61. endif()
  62. set(quiet ${${PREFIX}_FIND_QUIETLY})
  63. # Process optional arguments
  64. foreach(arg ${ARGN})
  65. if (arg STREQUAL "QUIET")
  66. set(quiet TRUE)
  67. else()
  68. message(AUTHOR_WARNING "Unknown argument ${arg} to libfind_version_header ignored.")
  69. endif()
  70. endforeach()
  71. # Read the header and parse for version number
  72. set(filename "${${PREFIX}_INCLUDE_DIR}/${VERSION_H}")
  73. if (NOT EXISTS ${filename})
  74. if (NOT quiet)
  75. message(AUTHOR_WARNING "Unable to find ${${PREFIX}_INCLUDE_DIR}/${VERSION_H}")
  76. endif()
  77. return()
  78. endif()
  79. file(READ "${filename}" header)
  80. string(REGEX REPLACE ".*#[ \t]*define[ \t]*${DEFINE_NAME}[ \t]*\"([^\n]*)\".*" "\\1" match "${header}")
  81. # No regex match?
  82. if (match STREQUAL header)
  83. if (NOT quiet)
  84. message(AUTHOR_WARNING "Unable to find \#define ${DEFINE_NAME} \"<version>\" from ${${PREFIX}_INCLUDE_DIR}/${VERSION_H}")
  85. endif()
  86. return()
  87. endif()
  88. # Export the version string
  89. set(${PREFIX}_VERSION "${match}" PARENT_SCOPE)
  90. endfunction()
  91. # Do the final processing once the paths have been detected.
  92. # If include dirs are needed, ${PREFIX}_PROCESS_INCLUDES should be set to contain
  93. # all the variables, each of which contain one include directory.
  94. # Ditto for ${PREFIX}_PROCESS_LIBS and library files.
  95. # Will set ${PREFIX}_FOUND, ${PREFIX}_INCLUDE_DIRS and ${PREFIX}_LIBRARIES.
  96. # Also handles errors in case library detection was required, etc.
  97. function (libfind_process PREFIX)
  98. # Skip processing if already processed during this configuration run
  99. if (${PREFIX}_FOUND)
  100. return()
  101. endif()
  102. set(found TRUE) # Start with the assumption that the package was found
  103. # Did we find any files? Did we miss includes? These are for formatting better error messages.
  104. set(some_files FALSE)
  105. set(missing_headers FALSE)
  106. # Shorthands for some variables that we need often
  107. set(quiet ${${PREFIX}_FIND_QUIETLY})
  108. set(required ${${PREFIX}_FIND_REQUIRED})
  109. set(exactver ${${PREFIX}_FIND_VERSION_EXACT})
  110. set(findver "${${PREFIX}_FIND_VERSION}")
  111. set(version "${${PREFIX}_VERSION}")
  112. # Lists of config option names (all, includes, libs)
  113. unset(configopts)
  114. set(includeopts ${${PREFIX}_PROCESS_INCLUDES})
  115. set(libraryopts ${${PREFIX}_PROCESS_LIBS})
  116. # Process deps to add to
  117. foreach (i ${PREFIX} ${${PREFIX}_DEPENDENCIES})
  118. if (DEFINED ${i}_INCLUDE_OPTS OR DEFINED ${i}_LIBRARY_OPTS)
  119. # The package seems to export option lists that we can use, woohoo!
  120. list(APPEND includeopts ${${i}_INCLUDE_OPTS})
  121. list(APPEND libraryopts ${${i}_LIBRARY_OPTS})
  122. else()
  123. # If plural forms don't exist or they equal singular forms
  124. if ((NOT DEFINED ${i}_INCLUDE_DIRS AND NOT DEFINED ${i}_LIBRARIES) OR
  125. ({i}_INCLUDE_DIR STREQUAL ${i}_INCLUDE_DIRS AND ${i}_LIBRARY STREQUAL ${i}_LIBRARIES))
  126. # Singular forms can be used
  127. if (DEFINED ${i}_INCLUDE_DIR)
  128. list(APPEND includeopts ${i}_INCLUDE_DIR)
  129. endif()
  130. if (DEFINED ${i}_LIBRARY)
  131. list(APPEND libraryopts ${i}_LIBRARY)
  132. endif()
  133. else()
  134. # Oh no, we don't know the option names
  135. message(FATAL_ERROR "We couldn't determine config variable names for ${i} includes and libs. Aieeh!")
  136. endif()
  137. endif()
  138. endforeach()
  139. if (includeopts)
  140. list(REMOVE_DUPLICATES includeopts)
  141. endif()
  142. if (libraryopts)
  143. list(REMOVE_DUPLICATES libraryopts)
  144. endif()
  145. string(REGEX REPLACE ".*[ ;]([^ ;]*(_INCLUDE_DIRS|_LIBRARIES))" "\\1" tmp "${includeopts} ${libraryopts}")
  146. if (NOT tmp STREQUAL "${includeopts} ${libraryopts}")
  147. message(AUTHOR_WARNING "Plural form ${tmp} found in config options of ${PREFIX}. This works as before but is now deprecated. Please only use singular forms INCLUDE_DIR and LIBRARY, and update your find scripts for LibFindMacros > 2.0 automatic dependency system (most often you can simply remove the PROCESS variables entirely).")
  148. endif()
  149. # Include/library names separated by spaces (notice: not CMake lists)
  150. unset(includes)
  151. unset(libs)
  152. # Process all includes and set found false if any are missing
  153. foreach (i ${includeopts})
  154. list(APPEND configopts ${i})
  155. if (NOT "${${i}}" STREQUAL "${i}-NOTFOUND")
  156. list(APPEND includes "${${i}}")
  157. else()
  158. set(found FALSE)
  159. set(missing_headers TRUE)
  160. endif()
  161. endforeach()
  162. # Process all libraries and set found false if any are missing
  163. foreach (i ${libraryopts})
  164. list(APPEND configopts ${i})
  165. if (NOT "${${i}}" STREQUAL "${i}-NOTFOUND")
  166. list(APPEND libs "${${i}}")
  167. else()
  168. set (found FALSE)
  169. endif()
  170. endforeach()
  171. # Version checks
  172. if (found AND findver)
  173. if (NOT version)
  174. message(WARNING "The find module for ${PREFIX} does not provide version information, so we'll just assume that it is OK. Please fix the module or remove package version requirements to get rid of this warning.")
  175. elseif (version VERSION_LESS findver OR (exactver AND NOT version VERSION_EQUAL findver))
  176. set(found FALSE)
  177. set(version_unsuitable TRUE)
  178. endif()
  179. endif()
  180. # If all-OK, hide all config options, export variables, print status and exit
  181. if (found)
  182. foreach (i ${configopts})
  183. mark_as_advanced(${i})
  184. endforeach()
  185. if (NOT quiet)
  186. message(STATUS "Found ${PREFIX} ${${PREFIX}_VERSION}")
  187. if (LIBFIND_DEBUG)
  188. message(STATUS " ${PREFIX}_DEPENDENCIES=${${PREFIX}_DEPENDENCIES}")
  189. message(STATUS " ${PREFIX}_INCLUDE_OPTS=${includeopts}")
  190. message(STATUS " ${PREFIX}_INCLUDE_DIRS=${includes}")
  191. message(STATUS " ${PREFIX}_LIBRARY_OPTS=${libraryopts}")
  192. message(STATUS " ${PREFIX}_LIBRARIES=${libs}")
  193. endif()
  194. set (${PREFIX}_INCLUDE_OPTS ${includeopts} PARENT_SCOPE)
  195. set (${PREFIX}_LIBRARY_OPTS ${libraryopts} PARENT_SCOPE)
  196. set (${PREFIX}_INCLUDE_DIRS ${includes} PARENT_SCOPE)
  197. set (${PREFIX}_LIBRARIES ${libs} PARENT_SCOPE)
  198. set (${PREFIX}_FOUND TRUE PARENT_SCOPE)
  199. endif()
  200. return()
  201. endif()
  202. # Format messages for debug info and the type of error
  203. set(vars "Relevant CMake configuration variables:\n")
  204. foreach (i ${configopts})
  205. mark_as_advanced(CLEAR ${i})
  206. set(val ${${i}})
  207. if ("${val}" STREQUAL "${i}-NOTFOUND")
  208. set (val "<not found>")
  209. elseif (val AND NOT EXISTS ${val})
  210. set (val "${val} (does not exist)")
  211. else()
  212. set(some_files TRUE)
  213. endif()
  214. set(vars "${vars} ${i}=${val}\n")
  215. endforeach()
  216. set(vars "${vars}You may use CMake GUI, cmake -D or ccmake to modify the values. Delete CMakeCache.txt to discard all values and force full re-detection if necessary.\n")
  217. if (version_unsuitable)
  218. set(msg "${PREFIX} ${${PREFIX}_VERSION} was found but")
  219. if (exactver)
  220. set(msg "${msg} only version ${findver} is acceptable.")
  221. else()
  222. set(msg "${msg} version ${findver} is the minimum requirement.")
  223. endif()
  224. else()
  225. if (missing_headers)
  226. set(msg "We could not find development headers for ${PREFIX}. Do you have the necessary dev package installed?")
  227. elseif (some_files)
  228. set(msg "We only found some files of ${PREFIX}, not all of them. Perhaps your installation is incomplete or maybe we just didn't look in the right place?")
  229. if(findver)
  230. set(msg "${msg} This could also be caused by incompatible version (if it helps, at least ${PREFIX} ${findver} should work).")
  231. endif()
  232. else()
  233. set(msg "We were unable to find package ${PREFIX}.")
  234. endif()
  235. endif()
  236. # Fatal error out if REQUIRED
  237. if (required)
  238. set(msg "REQUIRED PACKAGE NOT FOUND\n${msg} This package is REQUIRED and you need to install it or adjust CMake configuration in order to continue building ${CMAKE_PROJECT_NAME}.")
  239. message(FATAL_ERROR "${msg}\n${vars}")
  240. endif()
  241. # Otherwise just print a nasty warning
  242. if (NOT quiet)
  243. message(WARNING "WARNING: MISSING PACKAGE\n${msg} This package is NOT REQUIRED and you may ignore this warning but by doing so you may miss some functionality of ${CMAKE_PROJECT_NAME}. \n${vars}")
  244. endif()
  245. endfunction()