FindGlog.cmake 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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/lib/glog/include)
  124. # Windows (for C:/Program Files prefix).
  125. list(APPEND GLOG_CHECK_PATH_SUFFIXES
  126. glog/include
  127. glog/Include
  128. Glog/include
  129. Glog/Include)
  130. list(APPEND GLOG_CHECK_LIBRARY_DIRS
  131. ${GLOG_ROOT_DIR_LIB}
  132. /usr/local/lib
  133. /usr/local/homebrew/lib # Mac OS X.
  134. /opt/local/lib
  135. /usr/lib
  136. /sw/lib # Fink
  137. /opt/lib/gflags/lib)
  138. # Windows (for C:/Program Files prefix).
  139. list(APPEND GLOG_CHECK_LIBRARY_SUFFIXES
  140. glog/lib
  141. glog/Lib
  142. Glog/lib
  143. Glog/Lib)
  144. # Search supplied hint directories first if supplied.
  145. find_path(GLOG_INCLUDE_DIR
  146. NAMES glog/logging.h
  147. PATHS ${GLOG_INCLUDE_DIR_HINTS}
  148. ${GLOG_CHECK_INCLUDE_DIRS}
  149. PATH_SUFFIXES ${GLOG_CHECK_PATH_SUFFIXES})
  150. if(NOT GLOG_INCLUDE_DIR OR
  151. NOT EXISTS ${GLOG_INCLUDE_DIR})
  152. glog_report_not_found(
  153. "Could not find glog include directory, set GLOG_INCLUDE_DIR "
  154. "to directory containing glog/logging.h")
  155. endif()
  156. find_library(GLOG_LIBRARY NAMES glog
  157. PATHS ${GLOG_LIBRARY_DIR_HINTS}
  158. ${GLOG_CHECK_LIBRARY_DIRS}
  159. PATH_SUFFIXES ${GLOG_CHECK_LIBRARY_SUFFIXES})
  160. if(NOT GLOG_LIBRARY OR
  161. NOT EXISTS ${GLOG_LIBRARY})
  162. glog_report_not_found(
  163. "Could not find glog library, set GLOG_LIBRARY "
  164. "to full path to libglog.")
  165. endif()
  166. # Mark internally as found, then verify. GLOG_REPORT_NOT_FOUND() unsets
  167. # if called.
  168. set(GLOG_FOUND TRUE)
  169. # Glog does not seem to provide any record of the version in its
  170. # source tree, thus cannot extract version.
  171. # Catch case when caller has set GLOG_INCLUDE_DIR in the cache / GUI and
  172. # thus FIND_[PATH/LIBRARY] are not called, but specified locations are
  173. # invalid, otherwise we would report the library as found.
  174. if(GLOG_INCLUDE_DIR AND
  175. NOT EXISTS ${GLOG_INCLUDE_DIR}/glog/logging.h)
  176. glog_report_not_found(
  177. "Caller defined GLOG_INCLUDE_DIR:"
  178. " ${GLOG_INCLUDE_DIR} does not contain glog/logging.h header.")
  179. endif()
  180. # TODO: This regex for glog library is pretty primitive, we use lowercase
  181. # for comparison to handle Windows using CamelCase library names, could
  182. # this check be better?
  183. string(TOLOWER "${GLOG_LIBRARY}" LOWERCASE_GLOG_LIBRARY)
  184. if(GLOG_LIBRARY AND
  185. NOT "${LOWERCASE_GLOG_LIBRARY}" MATCHES ".*glog[^/]*")
  186. glog_report_not_found(
  187. "Caller defined GLOG_LIBRARY: "
  188. "${GLOG_LIBRARY} does not match glog.")
  189. endif()
  190. # Set standard CMake FindPackage variables if found.
  191. if(GLOG_FOUND)
  192. set(GLOG_INCLUDE_DIRS ${GLOG_INCLUDE_DIR})
  193. set(GLOG_LIBRARIES ${GLOG_LIBRARY})
  194. endif()
  195. glog_reset_find_library_prefix()
  196. # Handle REQUIRED / QUIET optional arguments.
  197. include(FindPackageHandleStandardArgs)
  198. find_package_handle_standard_args(GLOG DEFAULT_MSG
  199. GLOG_INCLUDE_DIRS GLOG_LIBRARIES)
  200. # Only mark internal variables as advanced if we found glog, otherwise
  201. # leave them visible in the standard GUI for the user to set manually.
  202. if(GLOG_FOUND)
  203. mark_as_advanced(FORCE GLOG_INCLUDE_DIR
  204. GLOG_LIBRARY)
  205. endif()