FindGlog.cmake 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #
  39. # The following variables control the behaviour of this module:
  40. #
  41. # GLOG_INCLUDE_DIR_HINTS: List of additional directories in which to
  42. # search for glog includes, e.g: /timbuktu/include.
  43. # GLOG_LIBRARY_DIR_HINTS: List of additional directories in which to
  44. # search for glog libraries, e.g: /timbuktu/lib.
  45. # GFLOG_ROOT_DIR, The base directory to search for Glog.
  46. # This can also be an environment variable.
  47. #
  48. # The following variables are also defined by this module, but in line with
  49. # CMake recommended FindPackage() module style should NOT be referenced directly
  50. # by callers (use the plural variables detailed above instead). These variables
  51. # do however affect the behaviour of the module via FIND_[PATH/LIBRARY]() which
  52. # are NOT re-called (i.e. search for library is not repeated) if these variables
  53. # are set with valid values _in the CMake cache_. This means that if these
  54. # variables are set directly in the cache, either by the user in the CMake GUI,
  55. # or by the user passing -DVAR=VALUE directives to CMake when called (which
  56. # explicitly defines a cache variable), then they will be used verbatim,
  57. # bypassing the HINTS variables and other hard-coded search locations.
  58. #
  59. # GLOG_INCLUDE_DIR: Include directory for glog, not including the
  60. # include directory of any dependencies.
  61. # GLOG_LIBRARY: glog library, not including the libraries of any
  62. # dependencies.
  63. # If GLOG_ROOT_DIR was defined in the environment, use it.
  64. if(NOT GLOG_ROOT_DIR AND NOT $ENV{GLOG_ROOT_DIR} STREQUAL "")
  65. set(GLOG_ROOT_DIR $ENV{GLOG_ROOT_DIR})
  66. endif()
  67. if(DEFINED GLOG_ROOT_DIR)
  68. set(GLOG_ROOT_DIR_INCLUDE "${GLOG_ROOT_DIR}/include")
  69. set(GLOG_ROOT_DIR_LIB "${GLOG_ROOT_DIR}/lib")
  70. endif()
  71. # Reset CALLERS_CMAKE_FIND_LIBRARY_PREFIXES to its value when
  72. # FindGlog was invoked.
  73. macro(GLOG_RESET_FIND_LIBRARY_PREFIX)
  74. if(MSVC)
  75. set(CMAKE_FIND_LIBRARY_PREFIXES "${CALLERS_CMAKE_FIND_LIBRARY_PREFIXES}")
  76. endif()
  77. endmacro()
  78. # Called if we failed to find glog or any of it's required dependencies,
  79. # unsets all public (designed to be used externally) variables and reports
  80. # error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
  81. macro(GLOG_REPORT_NOT_FOUND REASON_MSG)
  82. unset(GLOG_FOUND)
  83. unset(GLOG_INCLUDE_DIRS)
  84. unset(GLOG_LIBRARIES)
  85. # Make results of search visible in the CMake GUI if glog has not
  86. # been found so that user does not have to toggle to advanced view.
  87. mark_as_advanced(CLEAR GLOG_INCLUDE_DIR
  88. GLOG_LIBRARY)
  89. glog_reset_find_library_prefix()
  90. # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
  91. # use the camelcase library name, not uppercase.
  92. if(Glog_FIND_QUIETLY)
  93. message(STATUS "Failed to find glog - " ${REASON_MSG} ${ARGN})
  94. elseif(Glog_FIND_REQUIRED)
  95. message(FATAL_ERROR "Failed to find glog - " ${REASON_MSG} ${ARGN})
  96. else()
  97. # Neither QUIETLY nor REQUIRED, use no priority which emits a message
  98. # but continues configuration and allows generation.
  99. message("-- Failed to find glog - " ${REASON_MSG} ${ARGN})
  100. endif()
  101. return()
  102. endmacro()
  103. # Handle possible presence of lib prefix for libraries on MSVC, see
  104. # also GLOG_RESET_FIND_LIBRARY_PREFIX().
  105. if(MSVC)
  106. # Preserve the caller's original values for CMAKE_FIND_LIBRARY_PREFIXES
  107. # s/t we can set it back before returning.
  108. set(CALLERS_CMAKE_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
  109. # The empty string in this list is important, it represents the case when
  110. # the libraries have no prefix (shared libraries / DLLs).
  111. set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "" "${CMAKE_FIND_LIBRARY_PREFIXES}")
  112. endif()
  113. # Search user-installed locations first, so that we prefer user installs
  114. # to system installs where both exist.
  115. list(APPEND GLOG_CHECK_INCLUDE_DIRS
  116. ${GLOG_ROOT_DIR_INCLUDE}
  117. /usr/local/include
  118. /usr/local/homebrew/include # Mac OS X
  119. /opt/local/var/macports/software # Mac OS X.
  120. /opt/local/include
  121. /usr/include
  122. /sw/include # Fink
  123. /opt/csw/include # Blastwave
  124. /opt/lib/glog/include)
  125. # Windows (for C:/Program Files prefix).
  126. list(APPEND GLOG_CHECK_PATH_SUFFIXES
  127. glog/include
  128. glog/Include
  129. Glog/include
  130. Glog/Include)
  131. list(APPEND GLOG_CHECK_LIBRARY_DIRS
  132. ${GLOG_ROOT_DIR_LIB}
  133. /usr/local/lib
  134. /usr/local/homebrew/lib # Mac OS X.
  135. /opt/local/lib
  136. /usr/lib
  137. /sw/lib # Fink
  138. /opt/csw/lib # Blastwave
  139. /opt/lib/gflags/lib)
  140. # Windows (for C:/Program Files prefix).
  141. list(APPEND GLOG_CHECK_LIBRARY_SUFFIXES
  142. glog/lib
  143. glog/Lib
  144. Glog/lib
  145. Glog/Lib)
  146. # Search supplied hint directories first if supplied.
  147. find_path(GLOG_INCLUDE_DIR
  148. NAMES glog/logging.h
  149. PATHS ${GLOG_INCLUDE_DIR_HINTS}
  150. ${GLOG_CHECK_INCLUDE_DIRS}
  151. PATH_SUFFIXES ${GLOG_CHECK_PATH_SUFFIXES})
  152. if(NOT GLOG_INCLUDE_DIR OR
  153. NOT EXISTS ${GLOG_INCLUDE_DIR})
  154. glog_report_not_found(
  155. "Could not find glog include directory, set GLOG_INCLUDE_DIR "
  156. "to directory containing glog/logging.h")
  157. endif()
  158. find_library(GLOG_LIBRARY NAMES glog
  159. PATHS ${GLOG_LIBRARY_DIR_HINTS}
  160. ${GLOG_CHECK_LIBRARY_DIRS}
  161. PATH_SUFFIXES ${GLOG_CHECK_LIBRARY_SUFFIXES})
  162. if(NOT GLOG_LIBRARY OR
  163. NOT EXISTS ${GLOG_LIBRARY})
  164. glog_report_not_found(
  165. "Could not find glog library, set GLOG_LIBRARY "
  166. "to full path to libglog.")
  167. endif()
  168. # Mark internally as found, then verify. GLOG_REPORT_NOT_FOUND() unsets
  169. # if called.
  170. set(GLOG_FOUND TRUE)
  171. # Glog does not seem to provide any record of the version in its
  172. # source tree, thus cannot extract version.
  173. # Catch case when caller has set GLOG_INCLUDE_DIR in the cache / GUI and
  174. # thus FIND_[PATH/LIBRARY] are not called, but specified locations are
  175. # invalid, otherwise we would report the library as found.
  176. if(GLOG_INCLUDE_DIR AND
  177. NOT EXISTS ${GLOG_INCLUDE_DIR}/glog/logging.h)
  178. glog_report_not_found(
  179. "Caller defined GLOG_INCLUDE_DIR:"
  180. " ${GLOG_INCLUDE_DIR} does not contain glog/logging.h header.")
  181. endif()
  182. # TODO: This regex for glog library is pretty primitive, we use lowercase
  183. # for comparison to handle Windows using CamelCase library names, could
  184. # this check be better?
  185. string(TOLOWER "${GLOG_LIBRARY}" LOWERCASE_GLOG_LIBRARY)
  186. if(GLOG_LIBRARY AND
  187. NOT "${LOWERCASE_GLOG_LIBRARY}" MATCHES ".*glog[^/]*")
  188. glog_report_not_found(
  189. "Caller defined GLOG_LIBRARY: "
  190. "${GLOG_LIBRARY} does not match glog.")
  191. endif()
  192. # Set standard CMake FindPackage variables if found.
  193. if(GLOG_FOUND)
  194. set(GLOG_INCLUDE_DIRS ${GLOG_INCLUDE_DIR})
  195. set(GLOG_LIBRARIES ${GLOG_LIBRARY})
  196. endif()
  197. glog_reset_find_library_prefix()
  198. # Handle REQUIRED / QUIET optional arguments.
  199. include(FindPackageHandleStandardArgs)
  200. find_package_handle_standard_args(GLOG DEFAULT_MSG
  201. GLOG_INCLUDE_DIRS GLOG_LIBRARIES)
  202. # Only mark internal variables as advanced if we found glog, otherwise
  203. # leave them visible in the standard GUI for the user to set manually.
  204. if(GLOG_FOUND)
  205. mark_as_advanced(FORCE GLOG_INCLUDE_DIR
  206. GLOG_LIBRARY)
  207. endif()