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