FindGlog.cmake 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. # Ceres Solver - A fast non-linear least squares minimizer
  2. # Copyright 2015 Google Inc. All rights reserved.
  3. # http://ceres-solver.org/
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are met:
  7. #
  8. # * Redistributions of source code must retain the above copyright notice,
  9. # this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above copyright notice,
  11. # this list of conditions and the following disclaimer in the documentation
  12. # and/or other materials provided with the distribution.
  13. # * Neither the name of Google Inc. nor the names of its contributors may be
  14. # used to endorse or promote products derived from this software without
  15. # specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. # POSSIBILITY OF SUCH DAMAGE.
  28. #
  29. # Author: alexs.mac@gmail.com (Alex Stewart)
  30. #
  31. # FindGlog.cmake - Find Google glog logging library.
  32. #
  33. # This module defines the following variables:
  34. #
  35. # GLOG_FOUND: TRUE iff glog is found.
  36. # GLOG_INCLUDE_DIRS: Include directories for glog.
  37. # GLOG_LIBRARIES: Libraries required to link glog.
  38. # FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION: True iff the version of glog found
  39. # was built & installed / exported
  40. # as a CMake package.
  41. #
  42. # The following variables control the behaviour of this module:
  43. #
  44. # GLOG_PREFER_EXPORTED_GLOG_CMAKE_CONFIGURATION: TRUE/FALSE, iff TRUE then
  45. # then prefer using an exported CMake configuration
  46. # generated by glog > 0.3.4 over searching for the
  47. # glog components manually. Otherwise (FALSE)
  48. # ignore any exported glog CMake configurations and
  49. # always perform a manual search for the components.
  50. # Default: TRUE iff user does not define this variable
  51. # before we are called, and does NOT specify either
  52. # GLOG_INCLUDE_DIR_HINTS or GLOG_LIBRARY_DIR_HINTS
  53. # otherwise FALSE.
  54. # GLOG_INCLUDE_DIR_HINTS: List of additional directories in which to
  55. # search for glog includes, e.g: /timbuktu/include.
  56. # GLOG_LIBRARY_DIR_HINTS: List of additional directories in which to
  57. # search for glog libraries, e.g: /timbuktu/lib.
  58. #
  59. # The following variables are also defined by this module, but in line with
  60. # CMake recommended FindPackage() module style should NOT be referenced directly
  61. # by callers (use the plural variables detailed above instead). These variables
  62. # do however affect the behaviour of the module via FIND_[PATH/LIBRARY]() which
  63. # are NOT re-called (i.e. search for library is not repeated) if these variables
  64. # are set with valid values _in the CMake cache_. This means that if these
  65. # variables are set directly in the cache, either by the user in the CMake GUI,
  66. # or by the user passing -DVAR=VALUE directives to CMake when called (which
  67. # explicitly defines a cache variable), then they will be used verbatim,
  68. # bypassing the HINTS variables and other hard-coded search locations.
  69. #
  70. # GLOG_INCLUDE_DIR: Include directory for glog, not including the
  71. # include directory of any dependencies.
  72. # GLOG_LIBRARY: glog library, not including the libraries of any
  73. # dependencies.
  74. # Reset CALLERS_CMAKE_FIND_LIBRARY_PREFIXES to its value when
  75. # FindGlog was invoked.
  76. macro(GLOG_RESET_FIND_LIBRARY_PREFIX)
  77. if (MSVC AND CALLERS_CMAKE_FIND_LIBRARY_PREFIXES)
  78. set(CMAKE_FIND_LIBRARY_PREFIXES "${CALLERS_CMAKE_FIND_LIBRARY_PREFIXES}")
  79. endif()
  80. endmacro(GLOG_RESET_FIND_LIBRARY_PREFIX)
  81. # Called if we failed to find glog or any of it's required dependencies,
  82. # unsets all public (designed to be used externally) variables and reports
  83. # error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
  84. macro(GLOG_REPORT_NOT_FOUND REASON_MSG)
  85. unset(GLOG_FOUND)
  86. unset(GLOG_INCLUDE_DIRS)
  87. unset(GLOG_LIBRARIES)
  88. # Make results of search visible in the CMake GUI if glog has not
  89. # been found so that user does not have to toggle to advanced view.
  90. mark_as_advanced(CLEAR GLOG_INCLUDE_DIR
  91. GLOG_LIBRARY)
  92. glog_reset_find_library_prefix()
  93. # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
  94. # use the camelcase library name, not uppercase.
  95. if (Glog_FIND_QUIETLY)
  96. message(STATUS "Failed to find glog - " ${REASON_MSG} ${ARGN})
  97. elseif (Glog_FIND_REQUIRED)
  98. message(FATAL_ERROR "Failed to find glog - " ${REASON_MSG} ${ARGN})
  99. else()
  100. # Neither QUIETLY nor REQUIRED, use no priority which emits a message
  101. # but continues configuration and allows generation.
  102. message("-- Failed to find glog - " ${REASON_MSG} ${ARGN})
  103. endif ()
  104. return()
  105. endmacro(GLOG_REPORT_NOT_FOUND)
  106. # glog_message([mode] "message text")
  107. #
  108. # Wraps the standard cmake 'message' command, but suppresses output
  109. # if the QUIET flag was passed to the find_package(Glog ...) call.
  110. function(GLOG_MESSAGE)
  111. if (NOT Glog_FIND_QUIETLY)
  112. message(${ARGN})
  113. endif()
  114. endfunction()
  115. # Protect against any alternative find_package scripts for this library having
  116. # been called previously (in a client project) which set GLOG_FOUND, but not
  117. # the other variables we require / set here which could cause the search logic
  118. # here to fail.
  119. unset(GLOG_FOUND)
  120. # -----------------------------------------------------------------
  121. # By default, if the user has expressed no preference for using an exported
  122. # glog CMake configuration over performing a search for the installed
  123. # components, and has not specified any hints for the search locations, then
  124. # prefer a glog exported configuration if available.
  125. if (NOT DEFINED GLOG_PREFER_EXPORTED_GLOG_CMAKE_CONFIGURATION
  126. AND NOT GLOG_INCLUDE_DIR_HINTS
  127. AND NOT GLOG_LIBRARY_DIR_HINTS)
  128. glog_message(STATUS "No preference for use of exported glog CMake "
  129. "configuration set, and no hints for include/library directories provided. "
  130. "Defaulting to preferring an installed/exported glog CMake configuration "
  131. "if available.")
  132. set(GLOG_PREFER_EXPORTED_GLOG_CMAKE_CONFIGURATION TRUE)
  133. endif()
  134. # On macOS, add the Homebrew prefix (with appropriate suffixes) to the
  135. # respective HINTS directories (after any user-specified locations). This
  136. # handles Homebrew installations into non-standard locations (not /usr/local).
  137. # We do not use CMAKE_PREFIX_PATH for this as given the search ordering of
  138. # find_xxx(), doing so would override any user-specified HINTS locations with
  139. # the Homebrew version if it exists.
  140. if (CMAKE_SYSTEM_NAME MATCHES "Darwin")
  141. find_program(HOMEBREW_EXECUTABLE brew)
  142. mark_as_advanced(FORCE HOMEBREW_EXECUTABLE)
  143. if (HOMEBREW_EXECUTABLE)
  144. # Detected a Homebrew install, query for its install prefix.
  145. execute_process(COMMAND ${HOMEBREW_EXECUTABLE} --prefix
  146. OUTPUT_VARIABLE HOMEBREW_INSTALL_PREFIX
  147. OUTPUT_STRIP_TRAILING_WHITESPACE)
  148. glog_message(STATUS "Detected Homebrew with install prefix: "
  149. "${HOMEBREW_INSTALL_PREFIX}, adding to CMake search paths.")
  150. list(APPEND GLOG_INCLUDE_DIR_HINTS "${HOMEBREW_INSTALL_PREFIX}/include")
  151. list(APPEND GLOG_LIBRARY_DIR_HINTS "${HOMEBREW_INSTALL_PREFIX}/lib")
  152. endif()
  153. endif()
  154. if (GLOG_PREFER_EXPORTED_GLOG_CMAKE_CONFIGURATION)
  155. # Try to find an exported CMake configuration for glog, as generated by
  156. # glog versions > 0.3.4
  157. #
  158. # We search twice, s/t we can invert the ordering of precedence used by
  159. # find_package() for exported package build directories, and installed
  160. # packages (found via CMAKE_SYSTEM_PREFIX_PATH), listed as items 6) and 7)
  161. # respectively in [1].
  162. #
  163. # By default, exported build directories are (in theory) detected first, and
  164. # this is usually the case on Windows. However, on OS X & Linux, the install
  165. # path (/usr/local) is typically present in the PATH environment variable
  166. # which is checked in item 4) in [1] (i.e. before both of the above, unless
  167. # NO_SYSTEM_ENVIRONMENT_PATH is passed). As such on those OSs installed
  168. # packages are usually detected in preference to exported package build
  169. # directories.
  170. #
  171. # To ensure a more consistent response across all OSs, and as users usually
  172. # want to prefer an installed version of a package over a locally built one
  173. # where both exist (esp. as the exported build directory might be removed
  174. # after installation), we first search with NO_CMAKE_PACKAGE_REGISTRY which
  175. # means any build directories exported by the user are ignored, and thus
  176. # installed directories are preferred. If this fails to find the package
  177. # we then research again, but without NO_CMAKE_PACKAGE_REGISTRY, so any
  178. # exported build directories will now be detected.
  179. #
  180. # To prevent confusion on Windows, we also pass NO_CMAKE_BUILDS_PATH (which
  181. # is item 5) in [1]), to not preferentially use projects that were built
  182. # recently with the CMake GUI to ensure that we always prefer an installed
  183. # version if available.
  184. #
  185. # NOTE: We use the NAMES option as glog erroneously uses 'google-glog' as its
  186. # project name when built with CMake, but exports itself as just 'glog'.
  187. # On Linux/OS X this does not break detection as the project name is
  188. # not used as part of the install path for the CMake package files,
  189. # e.g. /usr/local/lib/cmake/glog, where the <glog> suffix is hardcoded
  190. # in glog's CMakeLists. However, on Windows the project name *is*
  191. # part of the install prefix: C:/Program Files/google-glog/[include,lib].
  192. # However, by default CMake checks:
  193. # C:/Program Files/<FIND_PACKAGE_ARGUMENT_NAME='glog'> which does not
  194. # exist and thus detection fails. Thus we use the NAMES to force the
  195. # search to use both google-glog & glog.
  196. #
  197. # [1] http://www.cmake.org/cmake/help/v2.8.11/cmake.html#command:find_package
  198. find_package(glog QUIET
  199. NAMES google-glog glog
  200. HINTS ${glog_DIR} ${HOMEBREW_INSTALL_PREFIX}
  201. NO_MODULE
  202. NO_CMAKE_PACKAGE_REGISTRY
  203. NO_CMAKE_BUILDS_PATH)
  204. if (glog_FOUND)
  205. glog_message(STATUS "Found installed version of glog: ${glog_DIR}")
  206. else()
  207. # Failed to find an installed version of glog, repeat search allowing
  208. # exported build directories.
  209. glog_message(STATUS "Failed to find installed glog CMake configuration, "
  210. "searching for glog build directories exported with CMake.")
  211. # Again pass NO_CMAKE_BUILDS_PATH, as we know that glog is exported and
  212. # do not want to treat projects built with the CMake GUI preferentially.
  213. find_package(glog QUIET
  214. NAMES google-glog glog
  215. NO_MODULE
  216. NO_CMAKE_BUILDS_PATH)
  217. if (glog_FOUND)
  218. glog_message(STATUS "Found exported glog build directory: ${glog_DIR}")
  219. endif(glog_FOUND)
  220. endif(glog_FOUND)
  221. set(FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION ${glog_FOUND})
  222. if (FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION)
  223. glog_message(STATUS "Detected glog version: ${glog_VERSION}")
  224. set(GLOG_FOUND ${glog_FOUND})
  225. # glog wraps the include directories into the exported glog::glog target.
  226. set(GLOG_INCLUDE_DIR "")
  227. set(GLOG_LIBRARY glog::glog)
  228. else (FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION)
  229. glog_message(STATUS "Failed to find an installed/exported CMake "
  230. "configuration for glog, will perform search for installed glog "
  231. "components.")
  232. endif (FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION)
  233. endif(GLOG_PREFER_EXPORTED_GLOG_CMAKE_CONFIGURATION)
  234. if (NOT GLOG_FOUND)
  235. # Either failed to find an exported glog CMake configuration, or user
  236. # told us not to use one. Perform a manual search for all glog components.
  237. # Handle possible presence of lib prefix for libraries on MSVC, see
  238. # also GLOG_RESET_FIND_LIBRARY_PREFIX().
  239. if (MSVC)
  240. # Preserve the caller's original values for CMAKE_FIND_LIBRARY_PREFIXES
  241. # s/t we can set it back before returning.
  242. set(CALLERS_CMAKE_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
  243. # The empty string in this list is important, it represents the case when
  244. # the libraries have no prefix (shared libraries / DLLs).
  245. set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "" "${CMAKE_FIND_LIBRARY_PREFIXES}")
  246. endif (MSVC)
  247. # Search user-installed locations first, so that we prefer user installs
  248. # to system installs where both exist.
  249. list(APPEND GLOG_CHECK_INCLUDE_DIRS
  250. /usr/local/include
  251. /usr/local/homebrew/include # Mac OS X
  252. /opt/local/var/macports/software # Mac OS X.
  253. /opt/local/include
  254. /usr/include)
  255. # Windows (for C:/Program Files prefix).
  256. list(APPEND GLOG_CHECK_PATH_SUFFIXES
  257. glog/include
  258. glog/Include
  259. Glog/include
  260. Glog/Include
  261. google-glog/include # CMake installs with project name prefix.
  262. google-glog/Include)
  263. list(APPEND GLOG_CHECK_LIBRARY_DIRS
  264. /usr/local/lib
  265. /usr/local/homebrew/lib # Mac OS X.
  266. /opt/local/lib
  267. /usr/lib)
  268. # Windows (for C:/Program Files prefix).
  269. list(APPEND GLOG_CHECK_LIBRARY_SUFFIXES
  270. glog/lib
  271. glog/Lib
  272. Glog/lib
  273. Glog/Lib
  274. google-glog/lib # CMake installs with project name prefix.
  275. google-glog/Lib)
  276. # Search supplied hint directories first if supplied.
  277. find_path(GLOG_INCLUDE_DIR
  278. NAMES glog/logging.h
  279. HINTS ${GLOG_INCLUDE_DIR_HINTS}
  280. PATHS ${GLOG_CHECK_INCLUDE_DIRS}
  281. PATH_SUFFIXES ${GLOG_CHECK_PATH_SUFFIXES})
  282. if (NOT GLOG_INCLUDE_DIR OR
  283. NOT EXISTS ${GLOG_INCLUDE_DIR})
  284. glog_report_not_found(
  285. "Could not find glog include directory, set GLOG_INCLUDE_DIR "
  286. "to directory containing glog/logging.h")
  287. endif (NOT GLOG_INCLUDE_DIR OR
  288. NOT EXISTS ${GLOG_INCLUDE_DIR})
  289. find_library(GLOG_LIBRARY NAMES glog
  290. HINTS ${GLOG_LIBRARY_DIR_HINTS}
  291. PATHS ${GLOG_CHECK_LIBRARY_DIRS}
  292. PATH_SUFFIXES ${GLOG_CHECK_LIBRARY_SUFFIXES})
  293. if (NOT GLOG_LIBRARY OR
  294. NOT EXISTS ${GLOG_LIBRARY})
  295. glog_report_not_found(
  296. "Could not find glog library, set GLOG_LIBRARY "
  297. "to full path to libglog.")
  298. endif (NOT GLOG_LIBRARY OR
  299. NOT EXISTS ${GLOG_LIBRARY})
  300. # Mark internally as found, then verify. GLOG_REPORT_NOT_FOUND() unsets
  301. # if called.
  302. set(GLOG_FOUND TRUE)
  303. # Glog does not seem to provide any record of the version in its
  304. # source tree, thus cannot extract version.
  305. # Catch case when caller has set GLOG_INCLUDE_DIR in the cache / GUI and
  306. # thus FIND_[PATH/LIBRARY] are not called, but specified locations are
  307. # invalid, otherwise we would report the library as found.
  308. if (GLOG_INCLUDE_DIR AND
  309. NOT EXISTS ${GLOG_INCLUDE_DIR}/glog/logging.h)
  310. glog_report_not_found(
  311. "Caller defined GLOG_INCLUDE_DIR:"
  312. " ${GLOG_INCLUDE_DIR} does not contain glog/logging.h header.")
  313. endif (GLOG_INCLUDE_DIR AND
  314. NOT EXISTS ${GLOG_INCLUDE_DIR}/glog/logging.h)
  315. # TODO: This regex for glog library is pretty primitive, we use lowercase
  316. # for comparison to handle Windows using CamelCase library names, could
  317. # this check be better?
  318. string(TOLOWER "${GLOG_LIBRARY}" LOWERCASE_GLOG_LIBRARY)
  319. if (GLOG_LIBRARY AND
  320. NOT "${LOWERCASE_GLOG_LIBRARY}" MATCHES ".*glog[^/]*")
  321. glog_report_not_found(
  322. "Caller defined GLOG_LIBRARY: "
  323. "${GLOG_LIBRARY} does not match glog.")
  324. endif (GLOG_LIBRARY AND
  325. NOT "${LOWERCASE_GLOG_LIBRARY}" MATCHES ".*glog[^/]*")
  326. # add glog::glog target
  327. add_library(glog::glog INTERFACE IMPORTED)
  328. target_include_directories(glog::glog INTERFACE ${GLOG_INCLUDE_DIRS})
  329. target_link_libraries(glog::glog INTERFACE ${GLOG_LIBRARY})
  330. glog_reset_find_library_prefix()
  331. endif(NOT GLOG_FOUND)
  332. # Set standard CMake FindPackage variables if found.
  333. if (GLOG_FOUND)
  334. set(GLOG_INCLUDE_DIRS ${GLOG_INCLUDE_DIR})
  335. set(GLOG_LIBRARIES ${GLOG_LIBRARY})
  336. endif (GLOG_FOUND)
  337. # If we are using an exported CMake glog target, the include directories are
  338. # wrapped into the target itself, and do not have to be (and are not)
  339. # separately specified. In which case, we should not add GLOG_INCLUDE_DIRS
  340. # to the list of required variables in order that glog be reported as found.
  341. if (FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION)
  342. set(GLOG_REQUIRED_VARIABLES GLOG_LIBRARIES)
  343. else()
  344. set(GLOG_REQUIRED_VARIABLES GLOG_INCLUDE_DIRS GLOG_LIBRARIES)
  345. endif()
  346. # Handle REQUIRED / QUIET optional arguments.
  347. include(FindPackageHandleStandardArgs)
  348. find_package_handle_standard_args(Glog DEFAULT_MSG
  349. ${GLOG_REQUIRED_VARIABLES})
  350. # Only mark internal variables as advanced if we found glog, otherwise
  351. # leave them visible in the standard GUI for the user to set manually.
  352. if (GLOG_FOUND)
  353. mark_as_advanced(FORCE GLOG_INCLUDE_DIR
  354. GLOG_LIBRARY
  355. glog_DIR) # Autogenerated by find_package(glog)
  356. endif (GLOG_FOUND)