Blame cmake/FindGlog.cmake

Packit ea1746
# Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
# Copyright 2015 Google Inc. All rights reserved.
Packit ea1746
# http://ceres-solver.org/
Packit ea1746
#
Packit ea1746
# Redistribution and use in source and binary forms, with or without
Packit ea1746
# modification, are permitted provided that the following conditions are met:
Packit ea1746
#
Packit ea1746
# * Redistributions of source code must retain the above copyright notice,
Packit ea1746
#   this list of conditions and the following disclaimer.
Packit ea1746
# * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
#   this list of conditions and the following disclaimer in the documentation
Packit ea1746
#   and/or other materials provided with the distribution.
Packit ea1746
# * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
#   used to endorse or promote products derived from this software without
Packit ea1746
#   specific prior written permission.
Packit ea1746
#
Packit ea1746
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
# POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
#
Packit ea1746
# Author: alexs.mac@gmail.com (Alex Stewart)
Packit ea1746
#
Packit ea1746
Packit ea1746
# FindGlog.cmake - Find Google glog logging library.
Packit ea1746
#
Packit ea1746
# This module defines the following variables:
Packit ea1746
#
Packit ea1746
# GLOG_FOUND: TRUE iff glog is found.
Packit ea1746
# GLOG_INCLUDE_DIRS: Include directories for glog.
Packit ea1746
# GLOG_LIBRARIES: Libraries required to link glog.
Packit ea1746
# FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION: True iff the version of glog found
Packit ea1746
#                                           was built & installed / exported
Packit ea1746
#                                           as a CMake package.
Packit ea1746
#
Packit ea1746
# The following variables control the behaviour of this module:
Packit ea1746
#
Packit ea1746
# GLOG_PREFER_EXPORTED_GLOG_CMAKE_CONFIGURATION: TRUE/FALSE, iff TRUE then
Packit ea1746
#                           then prefer using an exported CMake configuration
Packit ea1746
#                           generated by glog > 0.3.4 over searching for the
Packit ea1746
#                           glog components manually.  Otherwise (FALSE)
Packit ea1746
#                           ignore any exported glog CMake configurations and
Packit ea1746
#                           always perform a manual search for the components.
Packit ea1746
#                           Default: TRUE iff user does not define this variable
Packit ea1746
#                           before we are called, and does NOT specify either
Packit ea1746
#                           GLOG_INCLUDE_DIR_HINTS or GLOG_LIBRARY_DIR_HINTS
Packit ea1746
#                           otherwise FALSE.
Packit ea1746
# GLOG_INCLUDE_DIR_HINTS: List of additional directories in which to
Packit ea1746
#                         search for glog includes, e.g: /timbuktu/include.
Packit ea1746
# GLOG_LIBRARY_DIR_HINTS: List of additional directories in which to
Packit ea1746
#                         search for glog libraries, e.g: /timbuktu/lib.
Packit ea1746
#
Packit ea1746
# The following variables are also defined by this module, but in line with
Packit ea1746
# CMake recommended FindPackage() module style should NOT be referenced directly
Packit ea1746
# by callers (use the plural variables detailed above instead).  These variables
Packit ea1746
# do however affect the behaviour of the module via FIND_[PATH/LIBRARY]() which
Packit ea1746
# are NOT re-called (i.e. search for library is not repeated) if these variables
Packit ea1746
# are set with valid values _in the CMake cache_. This means that if these
Packit ea1746
# variables are set directly in the cache, either by the user in the CMake GUI,
Packit ea1746
# or by the user passing -DVAR=VALUE directives to CMake when called (which
Packit ea1746
# explicitly defines a cache variable), then they will be used verbatim,
Packit ea1746
# bypassing the HINTS variables and other hard-coded search locations.
Packit ea1746
#
Packit ea1746
# GLOG_INCLUDE_DIR: Include directory for glog, not including the
Packit ea1746
#                   include directory of any dependencies.
Packit ea1746
# GLOG_LIBRARY: glog library, not including the libraries of any
Packit ea1746
#               dependencies.
Packit ea1746
Packit ea1746
# Reset CALLERS_CMAKE_FIND_LIBRARY_PREFIXES to its value when
Packit ea1746
# FindGlog was invoked.
Packit ea1746
macro(GLOG_RESET_FIND_LIBRARY_PREFIX)
Packit ea1746
  if (MSVC AND CALLERS_CMAKE_FIND_LIBRARY_PREFIXES)
Packit ea1746
    set(CMAKE_FIND_LIBRARY_PREFIXES "${CALLERS_CMAKE_FIND_LIBRARY_PREFIXES}")
Packit ea1746
  endif()
Packit ea1746
endmacro(GLOG_RESET_FIND_LIBRARY_PREFIX)
Packit ea1746
Packit ea1746
# Called if we failed to find glog or any of it's required dependencies,
Packit ea1746
# unsets all public (designed to be used externally) variables and reports
Packit ea1746
# error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
Packit ea1746
macro(GLOG_REPORT_NOT_FOUND REASON_MSG)
Packit ea1746
  unset(GLOG_FOUND)
Packit ea1746
  unset(GLOG_INCLUDE_DIRS)
Packit ea1746
  unset(GLOG_LIBRARIES)
Packit ea1746
  # Make results of search visible in the CMake GUI if glog has not
Packit ea1746
  # been found so that user does not have to toggle to advanced view.
Packit ea1746
  mark_as_advanced(CLEAR GLOG_INCLUDE_DIR
Packit ea1746
                         GLOG_LIBRARY)
Packit ea1746
Packit ea1746
  glog_reset_find_library_prefix()
Packit ea1746
Packit ea1746
  # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
Packit ea1746
  # use the camelcase library name, not uppercase.
Packit ea1746
  if (Glog_FIND_QUIETLY)
Packit ea1746
    message(STATUS "Failed to find glog - " ${REASON_MSG} ${ARGN})
Packit ea1746
  elseif (Glog_FIND_REQUIRED)
Packit ea1746
    message(FATAL_ERROR "Failed to find glog - " ${REASON_MSG} ${ARGN})
Packit ea1746
  else()
Packit ea1746
    # Neither QUIETLY nor REQUIRED, use no priority which emits a message
Packit ea1746
    # but continues configuration and allows generation.
Packit ea1746
    message("-- Failed to find glog - " ${REASON_MSG} ${ARGN})
Packit ea1746
  endif ()
Packit ea1746
  return()
Packit ea1746
endmacro(GLOG_REPORT_NOT_FOUND)
Packit ea1746
Packit ea1746
# Protect against any alternative find_package scripts for this library having
Packit ea1746
# been called previously (in a client project) which set GLOG_FOUND, but not
Packit ea1746
# the other variables we require / set here which could cause the search logic
Packit ea1746
# here to fail.
Packit ea1746
unset(GLOG_FOUND)
Packit ea1746
Packit ea1746
# -----------------------------------------------------------------
Packit ea1746
# By default, if the user has expressed no preference for using an exported
Packit ea1746
# glog CMake configuration over performing a search for the installed
Packit ea1746
# components, and has not specified any hints for the search locations, then
Packit ea1746
# prefer a glog exported configuration if available.
Packit ea1746
if (NOT DEFINED GLOG_PREFER_EXPORTED_GLOG_CMAKE_CONFIGURATION
Packit ea1746
    AND NOT GLOG_INCLUDE_DIR_HINTS
Packit ea1746
    AND NOT GLOG_LIBRARY_DIR_HINTS)
Packit ea1746
  message(STATUS "No preference for use of exported glog CMake configuration "
Packit ea1746
    "set, and no hints for include/library directories provided. "
Packit ea1746
    "Defaulting to preferring an installed/exported glog CMake configuration "
Packit ea1746
    "if available.")
Packit ea1746
  set(GLOG_PREFER_EXPORTED_GLOG_CMAKE_CONFIGURATION TRUE)
Packit ea1746
endif()
Packit ea1746
Packit ea1746
if (GLOG_PREFER_EXPORTED_GLOG_CMAKE_CONFIGURATION)
Packit ea1746
  # Try to find an exported CMake configuration for glog, as generated by
Packit ea1746
  # glog versions > 0.3.4
Packit ea1746
  #
Packit ea1746
  # We search twice, s/t we can invert the ordering of precedence used by
Packit ea1746
  # find_package() for exported package build directories, and installed
Packit ea1746
  # packages (found via CMAKE_SYSTEM_PREFIX_PATH), listed as items 6) and 7)
Packit ea1746
  # respectively in [1].
Packit ea1746
  #
Packit ea1746
  # By default, exported build directories are (in theory) detected first, and
Packit ea1746
  # this is usually the case on Windows.  However, on OS X & Linux, the install
Packit ea1746
  # path (/usr/local) is typically present in the PATH environment variable
Packit ea1746
  # which is checked in item 4) in [1] (i.e. before both of the above, unless
Packit ea1746
  # NO_SYSTEM_ENVIRONMENT_PATH is passed).  As such on those OSs installed
Packit ea1746
  # packages are usually detected in preference to exported package build
Packit ea1746
  # directories.
Packit ea1746
  #
Packit ea1746
  # To ensure a more consistent response across all OSs, and as users usually
Packit ea1746
  # want to prefer an installed version of a package over a locally built one
Packit ea1746
  # where both exist (esp. as the exported build directory might be removed
Packit ea1746
  # after installation), we first search with NO_CMAKE_PACKAGE_REGISTRY which
Packit ea1746
  # means any build directories exported by the user are ignored, and thus
Packit ea1746
  # installed directories are preferred.  If this fails to find the package
Packit ea1746
  # we then research again, but without NO_CMAKE_PACKAGE_REGISTRY, so any
Packit ea1746
  # exported build directories will now be detected.
Packit ea1746
  #
Packit ea1746
  # To prevent confusion on Windows, we also pass NO_CMAKE_BUILDS_PATH (which
Packit ea1746
  # is item 5) in [1]), to not preferentially use projects that were built
Packit ea1746
  # recently with the CMake GUI to ensure that we always prefer an installed
Packit ea1746
  # version if available.
Packit ea1746
  #
Packit ea1746
  # NOTE: We use the NAMES option as glog erroneously uses 'google-glog' as its
Packit ea1746
  #       project name when built with CMake, but exports itself as just 'glog'.
Packit ea1746
  #       On Linux/OS X this does not break detection as the project name is
Packit ea1746
  #       not used as part of the install path for the CMake package files,
Packit ea1746
  #       e.g. /usr/local/lib/cmake/glog, where the <glog> suffix is hardcoded
Packit ea1746
  #       in glog's CMakeLists.  However, on Windows the project name *is*
Packit ea1746
  #       part of the install prefix: C:/Program Files/google-glog/[include,lib].
Packit ea1746
  #       However, by default CMake checks:
Packit ea1746
  #       C:/Program Files/<FIND_PACKAGE_ARGUMENT_NAME='glog'> which does not
Packit ea1746
  #       exist and thus detection fails.  Thus we use the NAMES to force the
Packit ea1746
  #       search to use both google-glog & glog.
Packit ea1746
  #
Packit ea1746
  # [1] http://www.cmake.org/cmake/help/v2.8.11/cmake.html#command:find_package
Packit ea1746
  find_package(glog QUIET
Packit ea1746
                    NAMES google-glog glog
Packit ea1746
                    NO_MODULE
Packit ea1746
                    NO_CMAKE_PACKAGE_REGISTRY
Packit ea1746
                    NO_CMAKE_BUILDS_PATH)
Packit ea1746
  if (glog_FOUND)
Packit ea1746
    message(STATUS "Found installed version of glog: ${glog_DIR}")
Packit ea1746
  else()
Packit ea1746
    # Failed to find an installed version of glog, repeat search allowing
Packit ea1746
    # exported build directories.
Packit ea1746
    message(STATUS "Failed to find installed glog CMake configuration, "
Packit ea1746
      "searching for glog build directories exported with CMake.")
Packit ea1746
    # Again pass NO_CMAKE_BUILDS_PATH, as we know that glog is exported and
Packit ea1746
    # do not want to treat projects built with the CMake GUI preferentially.
Packit ea1746
    find_package(glog QUIET
Packit ea1746
                      NAMES google-glog glog
Packit ea1746
                      NO_MODULE
Packit ea1746
                      NO_CMAKE_BUILDS_PATH)
Packit ea1746
    if (glog_FOUND)
Packit ea1746
      message(STATUS "Found exported glog build directory: ${glog_DIR}")
Packit ea1746
    endif(glog_FOUND)
Packit ea1746
  endif(glog_FOUND)
Packit ea1746
Packit ea1746
  set(FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION ${glog_FOUND})
Packit ea1746
Packit ea1746
  if (FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION)
Packit ea1746
    message(STATUS "Detected glog version: ${glog_VERSION}")
Packit ea1746
    set(GLOG_FOUND ${glog_FOUND})
Packit ea1746
    # glog wraps the include directories into the exported glog::glog target.
Packit ea1746
    set(GLOG_INCLUDE_DIR "")
Packit ea1746
    set(GLOG_LIBRARY glog::glog)
Packit ea1746
  else (FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION)
Packit ea1746
    message(STATUS "Failed to find an installed/exported CMake configuration "
Packit ea1746
      "for glog, will perform search for installed glog components.")
Packit ea1746
  endif (FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION)
Packit ea1746
endif(GLOG_PREFER_EXPORTED_GLOG_CMAKE_CONFIGURATION)
Packit ea1746
Packit ea1746
if (NOT GLOG_FOUND)
Packit ea1746
  # Either failed to find an exported glog CMake configuration, or user
Packit ea1746
  # told us not to use one.  Perform a manual search for all glog components.
Packit ea1746
Packit ea1746
  # Handle possible presence of lib prefix for libraries on MSVC, see
Packit ea1746
  # also GLOG_RESET_FIND_LIBRARY_PREFIX().
Packit ea1746
  if (MSVC)
Packit ea1746
    # Preserve the caller's original values for CMAKE_FIND_LIBRARY_PREFIXES
Packit ea1746
    # s/t we can set it back before returning.
Packit ea1746
    set(CALLERS_CMAKE_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
Packit ea1746
    # The empty string in this list is important, it represents the case when
Packit ea1746
    # the libraries have no prefix (shared libraries / DLLs).
Packit ea1746
    set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "" "${CMAKE_FIND_LIBRARY_PREFIXES}")
Packit ea1746
  endif (MSVC)
Packit ea1746
Packit ea1746
  # Search user-installed locations first, so that we prefer user installs
Packit ea1746
  # to system installs where both exist.
Packit ea1746
  list(APPEND GLOG_CHECK_INCLUDE_DIRS
Packit ea1746
    /usr/local/include
Packit ea1746
    /usr/local/homebrew/include # Mac OS X
Packit ea1746
    /opt/local/var/macports/software # Mac OS X.
Packit ea1746
    /opt/local/include
Packit ea1746
    /usr/include)
Packit ea1746
  # Windows (for C:/Program Files prefix).
Packit ea1746
  list(APPEND GLOG_CHECK_PATH_SUFFIXES
Packit ea1746
    glog/include
Packit ea1746
    glog/Include
Packit ea1746
    Glog/include
Packit ea1746
    Glog/Include
Packit ea1746
    google-glog/include # CMake installs with project name prefix.
Packit ea1746
    google-glog/Include)
Packit ea1746
Packit ea1746
  list(APPEND GLOG_CHECK_LIBRARY_DIRS
Packit ea1746
    /usr/local/lib
Packit ea1746
    /usr/local/homebrew/lib # Mac OS X.
Packit ea1746
    /opt/local/lib
Packit ea1746
    /usr/lib)
Packit ea1746
  # Windows (for C:/Program Files prefix).
Packit ea1746
  list(APPEND GLOG_CHECK_LIBRARY_SUFFIXES
Packit ea1746
    glog/lib
Packit ea1746
    glog/Lib
Packit ea1746
    Glog/lib
Packit ea1746
    Glog/Lib
Packit ea1746
    google-glog/lib # CMake installs with project name prefix.
Packit ea1746
    google-glog/Lib)
Packit ea1746
Packit ea1746
  # Search supplied hint directories first if supplied.
Packit ea1746
  find_path(GLOG_INCLUDE_DIR
Packit ea1746
    NAMES glog/logging.h
Packit ea1746
    HINTS ${GLOG_INCLUDE_DIR_HINTS}
Packit ea1746
    PATHS ${GLOG_CHECK_INCLUDE_DIRS}
Packit ea1746
    PATH_SUFFIXES ${GLOG_CHECK_PATH_SUFFIXES})
Packit ea1746
  if (NOT GLOG_INCLUDE_DIR OR
Packit ea1746
      NOT EXISTS ${GLOG_INCLUDE_DIR})
Packit ea1746
    glog_report_not_found(
Packit ea1746
      "Could not find glog include directory, set GLOG_INCLUDE_DIR "
Packit ea1746
      "to directory containing glog/logging.h")
Packit ea1746
  endif (NOT GLOG_INCLUDE_DIR OR
Packit ea1746
    NOT EXISTS ${GLOG_INCLUDE_DIR})
Packit ea1746
Packit ea1746
  find_library(GLOG_LIBRARY NAMES glog
Packit ea1746
    HINTS ${GLOG_LIBRARY_DIR_HINTS}
Packit ea1746
    PATHS ${GLOG_CHECK_LIBRARY_DIRS}
Packit ea1746
    PATH_SUFFIXES ${GLOG_CHECK_LIBRARY_SUFFIXES})
Packit ea1746
  if (NOT GLOG_LIBRARY OR
Packit ea1746
      NOT EXISTS ${GLOG_LIBRARY})
Packit ea1746
    glog_report_not_found(
Packit ea1746
      "Could not find glog library, set GLOG_LIBRARY "
Packit ea1746
      "to full path to libglog.")
Packit ea1746
  endif (NOT GLOG_LIBRARY OR
Packit ea1746
    NOT EXISTS ${GLOG_LIBRARY})
Packit ea1746
Packit ea1746
  # Mark internally as found, then verify. GLOG_REPORT_NOT_FOUND() unsets
Packit ea1746
  # if called.
Packit ea1746
  set(GLOG_FOUND TRUE)
Packit ea1746
Packit ea1746
  # Glog does not seem to provide any record of the version in its
Packit ea1746
  # source tree, thus cannot extract version.
Packit ea1746
Packit ea1746
  # Catch case when caller has set GLOG_INCLUDE_DIR in the cache / GUI and
Packit ea1746
  # thus FIND_[PATH/LIBRARY] are not called, but specified locations are
Packit ea1746
  # invalid, otherwise we would report the library as found.
Packit ea1746
  if (GLOG_INCLUDE_DIR AND
Packit ea1746
      NOT EXISTS ${GLOG_INCLUDE_DIR}/glog/logging.h)
Packit ea1746
    glog_report_not_found(
Packit ea1746
      "Caller defined GLOG_INCLUDE_DIR:"
Packit ea1746
      " ${GLOG_INCLUDE_DIR} does not contain glog/logging.h header.")
Packit ea1746
  endif (GLOG_INCLUDE_DIR AND
Packit ea1746
    NOT EXISTS ${GLOG_INCLUDE_DIR}/glog/logging.h)
Packit ea1746
  # TODO: This regex for glog library is pretty primitive, we use lowercase
Packit ea1746
  #       for comparison to handle Windows using CamelCase library names, could
Packit ea1746
  #       this check be better?
Packit ea1746
  string(TOLOWER "${GLOG_LIBRARY}" LOWERCASE_GLOG_LIBRARY)
Packit ea1746
  if (GLOG_LIBRARY AND
Packit ea1746
      NOT "${LOWERCASE_GLOG_LIBRARY}" MATCHES ".*glog[^/]*")
Packit ea1746
    glog_report_not_found(
Packit ea1746
      "Caller defined GLOG_LIBRARY: "
Packit ea1746
      "${GLOG_LIBRARY} does not match glog.")
Packit ea1746
  endif (GLOG_LIBRARY AND
Packit ea1746
    NOT "${LOWERCASE_GLOG_LIBRARY}" MATCHES ".*glog[^/]*")
Packit ea1746
Packit ea1746
  glog_reset_find_library_prefix()
Packit ea1746
Packit ea1746
endif(NOT GLOG_FOUND)
Packit ea1746
Packit ea1746
# Set standard CMake FindPackage variables if found.
Packit ea1746
if (GLOG_FOUND)
Packit ea1746
  set(GLOG_INCLUDE_DIRS ${GLOG_INCLUDE_DIR})
Packit ea1746
  set(GLOG_LIBRARIES ${GLOG_LIBRARY})
Packit ea1746
endif (GLOG_FOUND)
Packit ea1746
Packit ea1746
# If we are using an exported CMake glog target, the include directories are
Packit ea1746
# wrapped into the target itself, and do not have to be (and are not)
Packit ea1746
# separately specified.  In which case, we should not add GLOG_INCLUDE_DIRS
Packit ea1746
# to the list of required variables in order that glog be reported as found.
Packit ea1746
if (FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION)
Packit ea1746
  set(GLOG_REQUIRED_VARIABLES GLOG_LIBRARIES)
Packit ea1746
else()
Packit ea1746
  set(GLOG_REQUIRED_VARIABLES GLOG_INCLUDE_DIRS GLOG_LIBRARIES)
Packit ea1746
endif()
Packit ea1746
Packit ea1746
# Handle REQUIRED / QUIET optional arguments.
Packit ea1746
include(FindPackageHandleStandardArgs)
Packit ea1746
find_package_handle_standard_args(Glog DEFAULT_MSG
Packit ea1746
  ${GLOG_REQUIRED_VARIABLES})
Packit ea1746
Packit ea1746
# Only mark internal variables as advanced if we found glog, otherwise
Packit ea1746
# leave them visible in the standard GUI for the user to set manually.
Packit ea1746
if (GLOG_FOUND)
Packit ea1746
  mark_as_advanced(FORCE GLOG_INCLUDE_DIR
Packit ea1746
                         GLOG_LIBRARY
Packit ea1746
                         glog_DIR) # Autogenerated by find_package(glog)
Packit ea1746
endif (GLOG_FOUND)