0002-CMake-modules-update-our-copy-of-LibFindMacros.patch 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. From c508a40d9b3fb0e113b0d2618c71b744c715899d Mon Sep 17 00:00:00 2001
  2. From: Roman Lebedev <lebedev.ri@gmail.com>
  3. Date: Thu, 9 Jun 2016 12:50:02 +0300
  4. Subject: [PATCH 2/3] CMake modules: update our copy of LibFindMacros.
  5. (cherry picked from commit 7bd5417fd20308b0334dfd4bf96a71f6361ae3e4)
  6. ---
  7. cmake/modules/LibFindMacros.cmake | 324 ++++++++++++++++++++++++++++++--------
  8. 1 file changed, 257 insertions(+), 67 deletions(-)
  9. diff --git a/cmake/modules/LibFindMacros.cmake b/cmake/modules/LibFindMacros.cmake
  10. index 5a83443..1e29796 100644
  11. --- a/cmake/modules/LibFindMacros.cmake
  12. +++ b/cmake/modules/LibFindMacros.cmake
  13. @@ -1,30 +1,97 @@
  14. -# Works the same as find_package, but forwards the "REQUIRED" argument used for
  15. -# the current package and always uses the "QUIET" flag. For this to work, the
  16. -# first parameter must be the prefix of the current package, then the prefix of
  17. -# the new package etc, which are passed to find_package.
  18. -macro (libfind_package PREFIX)
  19. - set (LIBFIND_PACKAGE_ARGS ${ARGN} QUIET)
  20. +# Version 2.2
  21. +# Public Domain, originally written by Lasse Kärkkäinen <tronic>
  22. +# Maintained at https://github.com/Tronic/cmake-modules
  23. +# Please send your improvements as pull requests on Github.
  24. +
  25. +# Find another package and make it a dependency of the current package.
  26. +# This also automatically forwards the "REQUIRED" argument.
  27. +# Usage: libfind_package(<prefix> <another package> [extra args to find_package])
  28. +macro (libfind_package PREFIX PKG)
  29. + set(${PREFIX}_args ${PKG} ${ARGN})
  30. if (${PREFIX}_FIND_REQUIRED)
  31. - set (LIBFIND_PACKAGE_ARGS ${LIBFIND_PACKAGE_ARGS} REQUIRED)
  32. - endif (${PREFIX}_FIND_REQUIRED)
  33. - find_package(${LIBFIND_PACKAGE_ARGS})
  34. -endmacro (libfind_package)
  35. -
  36. -# Damn CMake developers made the UsePkgConfig system deprecated in the same release (2.6)
  37. -# where they added pkg_check_modules. Consequently I need to support both in my scripts
  38. -# to avoid those deprecated warnings. Here's a helper that does just that.
  39. -# Works identically to pkg_check_modules, except that no checks are needed prior to use.
  40. -macro (libfind_pkg_check_modules PREFIX PKGNAME)
  41. - if (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
  42. - include(UsePkgConfig)
  43. - pkgconfig(${PKGNAME} ${PREFIX}_INCLUDE_DIRS ${PREFIX}_LIBRARY_DIRS ${PREFIX}_LDFLAGS ${PREFIX}_CFLAGS)
  44. - else (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
  45. - find_package(PkgConfig)
  46. - if (PKG_CONFIG_FOUND)
  47. - pkg_check_modules(${PREFIX} ${PKGNAME})
  48. - endif (PKG_CONFIG_FOUND)
  49. - endif (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
  50. -endmacro (libfind_pkg_check_modules)
  51. + set(${PREFIX}_args ${${PREFIX}_args} REQUIRED)
  52. + endif()
  53. + find_package(${${PREFIX}_args})
  54. + set(${PREFIX}_DEPENDENCIES ${${PREFIX}_DEPENDENCIES};${PKG})
  55. + unset(${PREFIX}_args)
  56. +endmacro()
  57. +
  58. +# A simple wrapper to make pkg-config searches a bit easier.
  59. +# Works the same as CMake's internal pkg_check_modules but is always quiet.
  60. +macro (libfind_pkg_check_modules)
  61. + find_package(PkgConfig QUIET)
  62. + if (PKG_CONFIG_FOUND)
  63. + pkg_check_modules(${ARGN} QUIET)
  64. + endif()
  65. +endmacro()
  66. +
  67. +# Avoid useless copy&pasta by doing what most simple libraries do anyway:
  68. +# pkg-config, find headers, find library.
  69. +# Usage: libfind_pkg_detect(<prefix> <pkg-config args> FIND_PATH <name> [other args] FIND_LIBRARY <name> [other args])
  70. +# E.g. libfind_pkg_detect(SDL2 sdl2 FIND_PATH SDL.h PATH_SUFFIXES SDL2 FIND_LIBRARY SDL2)
  71. +function (libfind_pkg_detect PREFIX)
  72. + # Parse arguments
  73. + set(argname pkgargs)
  74. + foreach (i ${ARGN})
  75. + if ("${i}" STREQUAL "FIND_PATH")
  76. + set(argname pathargs)
  77. + elseif ("${i}" STREQUAL "FIND_LIBRARY")
  78. + set(argname libraryargs)
  79. + else()
  80. + set(${argname} ${${argname}} ${i})
  81. + endif()
  82. + endforeach()
  83. + if (NOT pkgargs)
  84. + message(FATAL_ERROR "libfind_pkg_detect requires at least a pkg_config package name to be passed.")
  85. + endif()
  86. + # Find library
  87. + libfind_pkg_check_modules(${PREFIX}_PKGCONF ${pkgargs})
  88. + if (pathargs)
  89. + find_path(${PREFIX}_INCLUDE_DIR NAMES ${pathargs} HINTS ${${PREFIX}_PKGCONF_INCLUDE_DIRS})
  90. + endif()
  91. + if (libraryargs)
  92. + find_library(${PREFIX}_LIBRARY NAMES ${libraryargs} HINTS ${${PREFIX}_PKGCONF_LIBRARY_DIRS})
  93. + endif()
  94. +endfunction()
  95. +
  96. +# Extracts a version #define from a version.h file, output stored to <PREFIX>_VERSION.
  97. +# Usage: libfind_version_header(Foobar foobar/version.h FOOBAR_VERSION_STR)
  98. +# Fourth argument "QUIET" may be used for silently testing different define names.
  99. +# This function does nothing if the version variable is already defined.
  100. +function (libfind_version_header PREFIX VERSION_H DEFINE_NAME)
  101. + # Skip processing if we already have a version or if the include dir was not found
  102. + if (${PREFIX}_VERSION OR NOT ${PREFIX}_INCLUDE_DIR)
  103. + return()
  104. + endif()
  105. + set(quiet ${${PREFIX}_FIND_QUIETLY})
  106. + # Process optional arguments
  107. + foreach(arg ${ARGN})
  108. + if (arg STREQUAL "QUIET")
  109. + set(quiet TRUE)
  110. + else()
  111. + message(AUTHOR_WARNING "Unknown argument ${arg} to libfind_version_header ignored.")
  112. + endif()
  113. + endforeach()
  114. + # Read the header and parse for version number
  115. + set(filename "${${PREFIX}_INCLUDE_DIR}/${VERSION_H}")
  116. + if (NOT EXISTS ${filename})
  117. + if (NOT quiet)
  118. + message(AUTHOR_WARNING "Unable to find ${${PREFIX}_INCLUDE_DIR}/${VERSION_H}")
  119. + endif()
  120. + return()
  121. + endif()
  122. + file(READ "${filename}" header)
  123. + string(REGEX REPLACE ".*#[ \t]*define[ \t]*${DEFINE_NAME}[ \t]*\"([^\n]*)\".*" "\\1" match "${header}")
  124. + # No regex match?
  125. + if (match STREQUAL header)
  126. + if (NOT quiet)
  127. + message(AUTHOR_WARNING "Unable to find \#define ${DEFINE_NAME} \"<version>\" from ${${PREFIX}_INCLUDE_DIR}/${VERSION_H}")
  128. + endif()
  129. + return()
  130. + endif()
  131. + # Export the version string
  132. + set(${PREFIX}_VERSION "${match}" PARENT_SCOPE)
  133. +endfunction()
  134. # Do the final processing once the paths have been detected.
  135. # If include dirs are needed, ${PREFIX}_PROCESS_INCLUDES should be set to contain
  136. @@ -32,45 +99,168 @@ endmacro (libfind_pkg_check_modules)
  137. # Ditto for ${PREFIX}_PROCESS_LIBS and library files.
  138. # Will set ${PREFIX}_FOUND, ${PREFIX}_INCLUDE_DIRS and ${PREFIX}_LIBRARIES.
  139. # Also handles errors in case library detection was required, etc.
  140. -macro (libfind_process PREFIX)
  141. - # Skip processing if already processed during this run
  142. - if (NOT ${PREFIX}_FOUND)
  143. - # Start with the assumption that the library was found
  144. - set (${PREFIX}_FOUND TRUE)
  145. -
  146. - # Process all includes and set _FOUND to false if any are missing
  147. - foreach (i ${${PREFIX}_PROCESS_INCLUDES})
  148. - if (i)
  149. - set (${PREFIX}_INCLUDE_DIRS ${${PREFIX}_INCLUDE_DIRS} ${${i}})
  150. - mark_as_advanced(${i})
  151. - else (i)
  152. - set (${PREFIX}_FOUND FALSE)
  153. - endif (i)
  154. - endforeach (i)
  155. -
  156. - # Process all libraries and set _FOUND to false if any are missing
  157. - foreach (i ${${PREFIX}_PROCESS_LIBS})
  158. - if (i)
  159. - set (${PREFIX}_LIBRARIES ${${PREFIX}_LIBRARIES} ${${i}})
  160. - mark_as_advanced(${i})
  161. - else (i)
  162. - set (${PREFIX}_FOUND FALSE)
  163. - endif (i)
  164. - endforeach (i)
  165. -
  166. - # Print message and/or exit on fatal error
  167. - if (${PREFIX}_FOUND)
  168. - if (NOT ${PREFIX}_FIND_QUIETLY)
  169. - message (STATUS "Found ${PREFIX} ${${PREFIX}_VERSION}")
  170. - endif (NOT ${PREFIX}_FIND_QUIETLY)
  171. - else (${PREFIX}_FOUND)
  172. - if (${PREFIX}_FIND_REQUIRED)
  173. - foreach (i ${${PREFIX}_PROCESS_INCLUDES} ${${PREFIX}_PROCESS_LIBS})
  174. - message("${i}=${${i}}")
  175. - endforeach (i)
  176. - 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.")
  177. - endif (${PREFIX}_FIND_REQUIRED)
  178. - endif (${PREFIX}_FOUND)
  179. - endif (NOT ${PREFIX}_FOUND)
  180. -endmacro (libfind_process)
  181. +function (libfind_process PREFIX)
  182. + # Skip processing if already processed during this configuration run
  183. + if (${PREFIX}_FOUND)
  184. + return()
  185. + endif()
  186. +
  187. + set(found TRUE) # Start with the assumption that the package was found
  188. +
  189. + # Did we find any files? Did we miss includes? These are for formatting better error messages.
  190. + set(some_files FALSE)
  191. + set(missing_headers FALSE)
  192. +
  193. + # Shorthands for some variables that we need often
  194. + set(quiet ${${PREFIX}_FIND_QUIETLY})
  195. + set(required ${${PREFIX}_FIND_REQUIRED})
  196. + set(exactver ${${PREFIX}_FIND_VERSION_EXACT})
  197. + set(findver "${${PREFIX}_FIND_VERSION}")
  198. + set(version "${${PREFIX}_VERSION}")
  199. +
  200. + # Lists of config option names (all, includes, libs)
  201. + unset(configopts)
  202. + set(includeopts ${${PREFIX}_PROCESS_INCLUDES})
  203. + set(libraryopts ${${PREFIX}_PROCESS_LIBS})
  204. +
  205. + # Process deps to add to
  206. + foreach (i ${PREFIX} ${${PREFIX}_DEPENDENCIES})
  207. + if (DEFINED ${i}_INCLUDE_OPTS OR DEFINED ${i}_LIBRARY_OPTS)
  208. + # The package seems to export option lists that we can use, woohoo!
  209. + list(APPEND includeopts ${${i}_INCLUDE_OPTS})
  210. + list(APPEND libraryopts ${${i}_LIBRARY_OPTS})
  211. + else()
  212. + # If plural forms don't exist or they equal singular forms
  213. + if ((NOT DEFINED ${i}_INCLUDE_DIRS AND NOT DEFINED ${i}_LIBRARIES) OR
  214. + ({i}_INCLUDE_DIR STREQUAL ${i}_INCLUDE_DIRS AND ${i}_LIBRARY STREQUAL ${i}_LIBRARIES))
  215. + # Singular forms can be used
  216. + if (DEFINED ${i}_INCLUDE_DIR)
  217. + list(APPEND includeopts ${i}_INCLUDE_DIR)
  218. + endif()
  219. + if (DEFINED ${i}_LIBRARY)
  220. + list(APPEND libraryopts ${i}_LIBRARY)
  221. + endif()
  222. + else()
  223. + # Oh no, we don't know the option names
  224. + message(FATAL_ERROR "We couldn't determine config variable names for ${i} includes and libs. Aieeh!")
  225. + endif()
  226. + endif()
  227. + endforeach()
  228. +
  229. + if (includeopts)
  230. + list(REMOVE_DUPLICATES includeopts)
  231. + endif()
  232. +
  233. + if (libraryopts)
  234. + list(REMOVE_DUPLICATES libraryopts)
  235. + endif()
  236. +
  237. + string(REGEX REPLACE ".*[ ;]([^ ;]*(_INCLUDE_DIRS|_LIBRARIES))" "\\1" tmp "${includeopts} ${libraryopts}")
  238. + if (NOT tmp STREQUAL "${includeopts} ${libraryopts}")
  239. + 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).")
  240. + endif()
  241. +
  242. + # Include/library names separated by spaces (notice: not CMake lists)
  243. + unset(includes)
  244. + unset(libs)
  245. +
  246. + # Process all includes and set found false if any are missing
  247. + foreach (i ${includeopts})
  248. + list(APPEND configopts ${i})
  249. + if (NOT "${${i}}" STREQUAL "${i}-NOTFOUND")
  250. + list(APPEND includes "${${i}}")
  251. + else()
  252. + set(found FALSE)
  253. + set(missing_headers TRUE)
  254. + endif()
  255. + endforeach()
  256. +
  257. + # Process all libraries and set found false if any are missing
  258. + foreach (i ${libraryopts})
  259. + list(APPEND configopts ${i})
  260. + if (NOT "${${i}}" STREQUAL "${i}-NOTFOUND")
  261. + list(APPEND libs "${${i}}")
  262. + else()
  263. + set (found FALSE)
  264. + endif()
  265. + endforeach()
  266. +
  267. + # Version checks
  268. + if (found AND findver)
  269. + if (NOT version)
  270. + 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.")
  271. + elseif (version VERSION_LESS findver OR (exactver AND NOT version VERSION_EQUAL findver))
  272. + set(found FALSE)
  273. + set(version_unsuitable TRUE)
  274. + endif()
  275. + endif()
  276. +
  277. + # If all-OK, hide all config options, export variables, print status and exit
  278. + if (found)
  279. + foreach (i ${configopts})
  280. + mark_as_advanced(${i})
  281. + endforeach()
  282. + if (NOT quiet)
  283. + message(STATUS "Found ${PREFIX} ${${PREFIX}_VERSION}")
  284. + if (LIBFIND_DEBUG)
  285. + message(STATUS " ${PREFIX}_DEPENDENCIES=${${PREFIX}_DEPENDENCIES}")
  286. + message(STATUS " ${PREFIX}_INCLUDE_OPTS=${includeopts}")
  287. + message(STATUS " ${PREFIX}_INCLUDE_DIRS=${includes}")
  288. + message(STATUS " ${PREFIX}_LIBRARY_OPTS=${libraryopts}")
  289. + message(STATUS " ${PREFIX}_LIBRARIES=${libs}")
  290. + endif()
  291. + set (${PREFIX}_INCLUDE_OPTS ${includeopts} PARENT_SCOPE)
  292. + set (${PREFIX}_LIBRARY_OPTS ${libraryopts} PARENT_SCOPE)
  293. + set (${PREFIX}_INCLUDE_DIRS ${includes} PARENT_SCOPE)
  294. + set (${PREFIX}_LIBRARIES ${libs} PARENT_SCOPE)
  295. + set (${PREFIX}_FOUND TRUE PARENT_SCOPE)
  296. + endif()
  297. + return()
  298. + endif()
  299. +
  300. + # Format messages for debug info and the type of error
  301. + set(vars "Relevant CMake configuration variables:\n")
  302. + foreach (i ${configopts})
  303. + mark_as_advanced(CLEAR ${i})
  304. + set(val ${${i}})
  305. + if ("${val}" STREQUAL "${i}-NOTFOUND")
  306. + set (val "<not found>")
  307. + elseif (val AND NOT EXISTS ${val})
  308. + set (val "${val} (does not exist)")
  309. + else()
  310. + set(some_files TRUE)
  311. + endif()
  312. + set(vars "${vars} ${i}=${val}\n")
  313. + endforeach()
  314. + 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")
  315. + if (version_unsuitable)
  316. + set(msg "${PREFIX} ${${PREFIX}_VERSION} was found but")
  317. + if (exactver)
  318. + set(msg "${msg} only version ${findver} is acceptable.")
  319. + else()
  320. + set(msg "${msg} version ${findver} is the minimum requirement.")
  321. + endif()
  322. + else()
  323. + if (missing_headers)
  324. + set(msg "We could not find development headers for ${PREFIX}. Do you have the necessary dev package installed?")
  325. + elseif (some_files)
  326. + 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?")
  327. + if(findver)
  328. + set(msg "${msg} This could also be caused by incompatible version (if it helps, at least ${PREFIX} ${findver} should work).")
  329. + endif()
  330. + else()
  331. + set(msg "We were unable to find package ${PREFIX}.")
  332. + endif()
  333. + endif()
  334. +
  335. + # Fatal error out if REQUIRED
  336. + if (required)
  337. + 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}.")
  338. + message(FATAL_ERROR "${msg}\n${vars}")
  339. + endif()
  340. + # Otherwise just print a nasty warning
  341. + if (NOT quiet)
  342. + 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}")
  343. + endif()
  344. +endfunction()