FindGlog.cmake 8.4 KB

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