Blame cmake/FindGflags.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
# FindGflags.cmake - Find Google gflags logging library.
Packit ea1746
#
Packit ea1746
# This module will attempt to find gflags, either via an exported CMake
Packit ea1746
# configuration (generated by gflags >= 2.1 which are built with CMake), or
Packit ea1746
# by performing a standard search for all gflags components.  The order of
Packit ea1746
# precedence for these two methods of finding gflags is controlled by:
Packit ea1746
# GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION.
Packit ea1746
#
Packit ea1746
# This module defines the following variables:
Packit ea1746
#
Packit ea1746
# GFLAGS_FOUND: TRUE iff gflags is found.
Packit ea1746
# GFLAGS_INCLUDE_DIRS: Include directories for gflags.
Packit ea1746
# GFLAGS_LIBRARIES: Libraries required to link gflags.
Packit ea1746
# GFLAGS_NAMESPACE: The namespace in which gflags is defined.  In versions of
Packit ea1746
#                   gflags < 2.1, this was google, for versions >= 2.1 it is
Packit ea1746
#                   by default gflags, although can be configured when building
Packit ea1746
#                   gflags to be something else (i.e. google for legacy
Packit ea1746
#                   compatibility).
Packit ea1746
# FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION: True iff the version of gflags
Packit ea1746
#                                             found was built & installed /
Packit ea1746
#                                             exported as a CMake package.
Packit ea1746
#
Packit ea1746
# The following variables control the behaviour of this module when an exported
Packit ea1746
# gflags CMake configuration is not found.
Packit ea1746
#
Packit ea1746
# GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION: TRUE/FALSE, iff TRUE then
Packit ea1746
#                           then prefer using an exported CMake configuration
Packit ea1746
#                           generated by gflags >= 2.1 over searching for the
Packit ea1746
#                           gflags components manually.  Otherwise (FALSE)
Packit ea1746
#                           ignore any exported gflags 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
#                           GFLAGS_INCLUDE_DIR_HINTS or GFLAGS_LIBRARY_DIR_HINTS
Packit ea1746
#                           otherwise FALSE.
Packit ea1746
# GFLAGS_INCLUDE_DIR_HINTS: List of additional directories in which to
Packit ea1746
#                           search for gflags includes, e.g: /timbuktu/include.
Packit ea1746
# GFLAGS_LIBRARY_DIR_HINTS: List of additional directories in which to
Packit ea1746
#                           search for gflags 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
# GFLAGS_INCLUDE_DIR: Include directory for gflags, not including the
Packit ea1746
#                     include directory of any dependencies.
Packit ea1746
# GFLAGS_LIBRARY: gflags library, not including the libraries of any
Packit ea1746
#                 dependencies.
Packit ea1746
Packit ea1746
# Reset CALLERS_CMAKE_FIND_LIBRARY_PREFIXES to its value when FindGflags was
Packit ea1746
# invoked, necessary for MSVC.
Packit ea1746
macro(GFLAGS_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(GFLAGS_RESET_FIND_LIBRARY_PREFIX)
Packit ea1746
Packit ea1746
# Called if we failed to find gflags 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(GFLAGS_REPORT_NOT_FOUND REASON_MSG)
Packit ea1746
  unset(GFLAGS_FOUND)
Packit ea1746
  unset(GFLAGS_INCLUDE_DIRS)
Packit ea1746
  unset(GFLAGS_LIBRARIES)
Packit ea1746
  # Do not use unset, as we want to keep GFLAGS_NAMESPACE in the cache,
Packit ea1746
  # but simply clear its value.
Packit ea1746
  set(GFLAGS_NAMESPACE "" CACHE STRING
Packit ea1746
    "gflags namespace (google or gflags)" FORCE)
Packit ea1746
Packit ea1746
  # Make results of search visible in the CMake GUI if gflags has not
Packit ea1746
  # been found so that user does not have to toggle to advanced view.
Packit ea1746
  mark_as_advanced(CLEAR GFLAGS_INCLUDE_DIR
Packit ea1746
                         GFLAGS_LIBRARY
Packit ea1746
                         GFLAGS_NAMESPACE)
Packit ea1746
Packit ea1746
  gflags_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 (Gflags_FIND_QUIETLY)
Packit ea1746
    message(STATUS "Failed to find gflags - " ${REASON_MSG} ${ARGN})
Packit ea1746
  elseif (Gflags_FIND_REQUIRED)
Packit ea1746
    message(FATAL_ERROR "Failed to find gflags - " ${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 gflags - " ${REASON_MSG} ${ARGN})
Packit ea1746
  endif ()
Packit ea1746
  return()
Packit ea1746
endmacro(GFLAGS_REPORT_NOT_FOUND)
Packit ea1746
Packit ea1746
# Verify that all variable names passed as arguments are defined (can be empty
Packit ea1746
# but must be defined) or raise a fatal error.
Packit ea1746
macro(GFLAGS_CHECK_VARS_DEFINED)
Packit ea1746
  foreach(CHECK_VAR ${ARGN})
Packit ea1746
    if (NOT DEFINED ${CHECK_VAR})
Packit ea1746
      message(FATAL_ERROR "Ceres Bug: ${CHECK_VAR} is not defined.")
Packit ea1746
    endif()
Packit ea1746
  endforeach()
Packit ea1746
endmacro(GFLAGS_CHECK_VARS_DEFINED)
Packit ea1746
Packit ea1746
# Use check_cxx_source_compiles() to compile trivial test programs to determine
Packit ea1746
# the gflags namespace.  This works on all OSs except Windows.  If using Visual
Packit ea1746
# Studio, it fails because msbuild forces check_cxx_source_compiles() to use
Packit ea1746
# CMAKE_BUILD_TYPE=Debug for the test project, which usually breaks detection
Packit ea1746
# because MSVC requires that the test project use the same build type as gflags,
Packit ea1746
# which would normally be built in Release.
Packit ea1746
#
Packit ea1746
# Defines: GFLAGS_NAMESPACE in the caller's scope with the detected namespace,
Packit ea1746
#          which is blank (empty string, will test FALSE is CMake conditionals)
Packit ea1746
#          if detection failed.
Packit ea1746
function(GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_TRY_COMPILE)
Packit ea1746
  # Verify that all required variables are defined.
Packit ea1746
  gflags_check_vars_defined(
Packit ea1746
    GFLAGS_INCLUDE_DIR GFLAGS_LIBRARY)
Packit ea1746
  # Ensure that GFLAGS_NAMESPACE is always unset on completion unless
Packit ea1746
  # we explicitly set if after having the correct namespace.
Packit ea1746
  set(GFLAGS_NAMESPACE "" PARENT_SCOPE)
Packit ea1746
Packit ea1746
  include(CheckCXXSourceCompiles)
Packit ea1746
  # Setup include path & link library for gflags for CHECK_CXX_SOURCE_COMPILES.
Packit ea1746
  set(CMAKE_REQUIRED_INCLUDES ${GFLAGS_INCLUDE_DIR})
Packit ea1746
  set(CMAKE_REQUIRED_LIBRARIES ${GFLAGS_LIBRARY} ${GFLAGS_LINK_LIBRARIES})
Packit ea1746
  # First try the (older) google namespace.  Note that the output variable
Packit ea1746
  # MUST be unique to the build type as otherwise the test is not repeated as
Packit ea1746
  # it is assumed to have already been performed.
Packit ea1746
  check_cxx_source_compiles(
Packit ea1746
    "#include <gflags/gflags.h>
Packit ea1746
     int main(int argc, char * argv[]) {
Packit ea1746
       google::ParseCommandLineFlags(&argc, &argv, true);
Packit ea1746
       return 0;
Packit ea1746
     }"
Packit ea1746
     GFLAGS_IN_GOOGLE_NAMESPACE)
Packit ea1746
  if (GFLAGS_IN_GOOGLE_NAMESPACE)
Packit ea1746
    set(GFLAGS_NAMESPACE google PARENT_SCOPE)
Packit ea1746
    return()
Packit ea1746
  endif()
Packit ea1746
Packit ea1746
  # Try (newer) gflags namespace instead.  Note that the output variable
Packit ea1746
  # MUST be unique to the build type as otherwise the test is not repeated as
Packit ea1746
  # it is assumed to have already been performed.
Packit ea1746
  set(CMAKE_REQUIRED_INCLUDES ${GFLAGS_INCLUDE_DIR})
Packit ea1746
  set(CMAKE_REQUIRED_LIBRARIES ${GFLAGS_LIBRARY} ${GFLAGS_LINK_LIBRARIES})
Packit ea1746
  check_cxx_source_compiles(
Packit ea1746
    "#include <gflags/gflags.h>
Packit ea1746
     int main(int argc, char * argv[]) {
Packit ea1746
        gflags::ParseCommandLineFlags(&argc, &argv, true);
Packit ea1746
        return 0;
Packit ea1746
     }"
Packit ea1746
     GFLAGS_IN_GFLAGS_NAMESPACE)
Packit ea1746
  if (GFLAGS_IN_GFLAGS_NAMESPACE)
Packit ea1746
    set(GFLAGS_NAMESPACE gflags PARENT_SCOPE)
Packit ea1746
    return()
Packit ea1746
  endif (GFLAGS_IN_GFLAGS_NAMESPACE)
Packit ea1746
endfunction(GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_TRY_COMPILE)
Packit ea1746
Packit ea1746
# Use regex on the gflags headers to attempt to determine the gflags namespace.
Packit ea1746
# Checks both gflags.h (contained namespace on versions < 2.1.2) and
Packit ea1746
# gflags_declare.h, which contains the namespace on versions >= 2.1.2.
Packit ea1746
# In general, this method should only be used when
Packit ea1746
# GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_TRY_COMPILE() cannot be used, or has
Packit ea1746
# failed.
Packit ea1746
#
Packit ea1746
# Defines: GFLAGS_NAMESPACE in the caller's scope with the detected namespace,
Packit ea1746
#          which is blank (empty string, will test FALSE is CMake conditionals)
Packit ea1746
#          if detection failed.
Packit ea1746
function(GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_REGEX)
Packit ea1746
  # Verify that all required variables are defined.
Packit ea1746
  gflags_check_vars_defined(GFLAGS_INCLUDE_DIR)
Packit ea1746
  # Ensure that GFLAGS_NAMESPACE is always undefined on completion unless
Packit ea1746
  # we explicitly set if after having the correct namespace.
Packit ea1746
  set(GFLAGS_NAMESPACE "" PARENT_SCOPE)
Packit ea1746
Packit ea1746
  # Scan gflags.h to identify what namespace gflags was built with.  On
Packit ea1746
  # versions of gflags < 2.1.2, gflags.h was configured with the namespace
Packit ea1746
  # directly, on >= 2.1.2, gflags.h uses the GFLAGS_NAMESPACE #define which
Packit ea1746
  # is defined in gflags_declare.h, we try each location in turn.
Packit ea1746
  set(GFLAGS_HEADER_FILE ${GFLAGS_INCLUDE_DIR}/gflags/gflags.h)
Packit ea1746
  if (NOT EXISTS ${GFLAGS_HEADER_FILE})
Packit ea1746
    gflags_report_not_found(
Packit ea1746
      "Could not find file: ${GFLAGS_HEADER_FILE} "
Packit ea1746
      "containing namespace information in gflags install located at: "
Packit ea1746
      "${GFLAGS_INCLUDE_DIR}.")
Packit ea1746
  endif()
Packit ea1746
  file(READ ${GFLAGS_HEADER_FILE} GFLAGS_HEADER_FILE_CONTENTS)
Packit ea1746
Packit ea1746
  string(REGEX MATCH "namespace [A-Za-z]+"
Packit ea1746
    GFLAGS_NAMESPACE "${GFLAGS_HEADER_FILE_CONTENTS}")
Packit ea1746
  string(REGEX REPLACE "namespace ([A-Za-z]+)" "\\1"
Packit ea1746
    GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}")
Packit ea1746
Packit ea1746
  if (NOT GFLAGS_NAMESPACE)
Packit ea1746
    gflags_report_not_found(
Packit ea1746
      "Failed to extract gflags namespace from header file: "
Packit ea1746
      "${GFLAGS_HEADER_FILE}.")
Packit ea1746
  endif (NOT GFLAGS_NAMESPACE)
Packit ea1746
Packit ea1746
  if (GFLAGS_NAMESPACE STREQUAL "google" OR
Packit ea1746
      GFLAGS_NAMESPACE STREQUAL "gflags")
Packit ea1746
    # Found valid gflags namespace from gflags.h.
Packit ea1746
    set(GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}" PARENT_SCOPE)
Packit ea1746
    return()
Packit ea1746
  endif()
Packit ea1746
Packit ea1746
  # Failed to find gflags namespace from gflags.h, gflags is likely a new
Packit ea1746
  # version, check gflags_declare.h, which in newer versions (>= 2.1.2) contains
Packit ea1746
  # the GFLAGS_NAMESPACE #define, which is then referenced in gflags.h.
Packit ea1746
  set(GFLAGS_DECLARE_FILE ${GFLAGS_INCLUDE_DIR}/gflags/gflags_declare.h)
Packit ea1746
  if (NOT EXISTS ${GFLAGS_DECLARE_FILE})
Packit ea1746
    gflags_report_not_found(
Packit ea1746
      "Could not find file: ${GFLAGS_DECLARE_FILE} "
Packit ea1746
      "containing namespace information in gflags install located at: "
Packit ea1746
      "${GFLAGS_INCLUDE_DIR}.")
Packit ea1746
  endif()
Packit ea1746
  file(READ ${GFLAGS_DECLARE_FILE} GFLAGS_DECLARE_FILE_CONTENTS)
Packit ea1746
Packit ea1746
  string(REGEX MATCH "#define GFLAGS_NAMESPACE [A-Za-z]+"
Packit ea1746
    GFLAGS_NAMESPACE "${GFLAGS_DECLARE_FILE_CONTENTS}")
Packit ea1746
  string(REGEX REPLACE "#define GFLAGS_NAMESPACE ([A-Za-z]+)" "\\1"
Packit ea1746
    GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}")
Packit ea1746
Packit ea1746
  if (NOT GFLAGS_NAMESPACE)
Packit ea1746
    gflags_report_not_found(
Packit ea1746
      "Failed to extract gflags namespace from declare file: "
Packit ea1746
      "${GFLAGS_DECLARE_FILE}.")
Packit ea1746
  endif (NOT GFLAGS_NAMESPACE)
Packit ea1746
Packit ea1746
  if (GFLAGS_NAMESPACE STREQUAL "google" OR
Packit ea1746
      GFLAGS_NAMESPACE STREQUAL "gflags")
Packit ea1746
    # Found valid gflags namespace from gflags.h.
Packit ea1746
    set(GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}" PARENT_SCOPE)
Packit ea1746
    return()
Packit ea1746
  endif()
Packit ea1746
endfunction(GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_REGEX)
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 GFLAGS_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(GFLAGS_FOUND)
Packit ea1746
Packit ea1746
# -----------------------------------------------------------------
Packit ea1746
# By default, if the user has expressed no preference for using an exported
Packit ea1746
# gflags 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 gflags exported configuration if available.
Packit ea1746
if (NOT DEFINED GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION
Packit ea1746
    AND NOT GFLAGS_INCLUDE_DIR_HINTS
Packit ea1746
    AND NOT GFLAGS_LIBRARY_DIR_HINTS)
Packit ea1746
  message(STATUS "No preference for use of exported gflags CMake configuration "
Packit ea1746
    "set, and no hints for include/library directories provided. "
Packit ea1746
    "Defaulting to preferring an installed/exported gflags CMake configuration "
Packit ea1746
    "if available.")
Packit ea1746
  set(GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION TRUE)
Packit ea1746
endif()
Packit ea1746
Packit ea1746
if (GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION)
Packit ea1746
  # Try to find an exported CMake configuration for gflags, as generated by
Packit ea1746
  # gflags versions >= 2.1.
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
  # [1] http://www.cmake.org/cmake/help/v2.8.11/cmake.html#command:find_package
Packit ea1746
  find_package(gflags QUIET
Packit ea1746
                      NO_MODULE
Packit ea1746
                      NO_CMAKE_PACKAGE_REGISTRY
Packit ea1746
                      NO_CMAKE_BUILDS_PATH)
Packit ea1746
  if (gflags_FOUND)
Packit ea1746
    message(STATUS "Found installed version of gflags: ${gflags_DIR}")
Packit ea1746
  else(gflags_FOUND)
Packit ea1746
    # Failed to find an installed version of gflags, repeat search allowing
Packit ea1746
    # exported build directories.
Packit ea1746
    message(STATUS "Failed to find installed gflags CMake configuration, "
Packit ea1746
      "searching for gflags build directories exported with CMake.")
Packit ea1746
    # Again pass NO_CMAKE_BUILDS_PATH, as we know that gflags is exported and
Packit ea1746
    # do not want to treat projects built with the CMake GUI preferentially.
Packit ea1746
    find_package(gflags QUIET
Packit ea1746
                        NO_MODULE
Packit ea1746
                        NO_CMAKE_BUILDS_PATH)
Packit ea1746
    if (gflags_FOUND)
Packit ea1746
      message(STATUS "Found exported gflags build directory: ${gflags_DIR}")
Packit ea1746
    endif(gflags_FOUND)
Packit ea1746
  endif(gflags_FOUND)
Packit ea1746
Packit ea1746
  set(FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION ${gflags_FOUND})
Packit ea1746
Packit ea1746
  # gflags v2.1 - 2.1.2 shipped with a bug in their gflags-config.cmake [1]
Packit ea1746
  # whereby gflags_LIBRARIES = "gflags", but there was no imported target
Packit ea1746
  # called "gflags", they were called: gflags[_nothreads]-[static/shared].
Packit ea1746
  # As this causes linker errors when gflags is not installed in a location
Packit ea1746
  # on the current library paths, detect if this problem is present and
Packit ea1746
  # fix it.
Packit ea1746
  #
Packit ea1746
  # [1] https://github.com/gflags/gflags/issues/110
Packit ea1746
  if (gflags_FOUND)
Packit ea1746
    # NOTE: This is not written as additional conditions in the outer
Packit ea1746
    #       if (gflags_FOUND) as the NOT TARGET "${gflags_LIBRARIES}"
Packit ea1746
    #       condition causes problems if gflags is not found.
Packit ea1746
    if (${gflags_VERSION} VERSION_LESS 2.1.3 AND
Packit ea1746
        NOT TARGET "${gflags_LIBRARIES}")
Packit ea1746
      message(STATUS "Detected broken gflags install in: ${gflags_DIR}, "
Packit ea1746
        "version: ${gflags_VERSION} <= 2.1.2 which defines gflags_LIBRARIES = "
Packit ea1746
        "${gflags_LIBRARIES} which is not an imported CMake target, see: "
Packit ea1746
        "https://github.com/gflags/gflags/issues/110.  Attempting to fix by "
Packit ea1746
        "detecting correct gflags target.")
Packit ea1746
      # Ordering here expresses preference for detection, specifically we do not
Packit ea1746
      # want to use the _nothreads variants if the full library is available.
Packit ea1746
      list(APPEND CHECK_GFLAGS_IMPORTED_TARGET_NAMES
Packit ea1746
        gflags-shared gflags-static
Packit ea1746
        gflags_nothreads-shared gflags_nothreads-static)
Packit ea1746
      foreach(CHECK_GFLAGS_TARGET ${CHECK_GFLAGS_IMPORTED_TARGET_NAMES})
Packit ea1746
        if (TARGET ${CHECK_GFLAGS_TARGET})
Packit ea1746
          message(STATUS "Found valid gflags target: ${CHECK_GFLAGS_TARGET}, "
Packit ea1746
            "updating gflags_LIBRARIES.")
Packit ea1746
          set(gflags_LIBRARIES ${CHECK_GFLAGS_TARGET})
Packit ea1746
          break()
Packit ea1746
        endif()
Packit ea1746
      endforeach()
Packit ea1746
      if (NOT TARGET ${gflags_LIBRARIES})
Packit ea1746
        message(STATUS "Failed to fix detected broken gflags install in: "
Packit ea1746
          "${gflags_DIR}, version: ${gflags_VERSION} <= 2.1.2, none of the "
Packit ea1746
          "imported targets for gflags: ${CHECK_GFLAGS_IMPORTED_TARGET_NAMES} "
Packit ea1746
          "are defined.  Will continue with a manual search for gflags "
Packit ea1746
          "components.  We recommend you build/install a version of gflags > "
Packit ea1746
          "2.1.2 (or master).")
Packit ea1746
        set(FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION FALSE)
Packit ea1746
      endif()
Packit ea1746
    endif()
Packit ea1746
  endif()
Packit ea1746
Packit ea1746
  if (FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION)
Packit ea1746
    message(STATUS "Detected gflags version: ${gflags_VERSION}")
Packit ea1746
    set(GFLAGS_FOUND ${gflags_FOUND})
Packit ea1746
    set(GFLAGS_INCLUDE_DIR ${gflags_INCLUDE_DIR})
Packit ea1746
    set(GFLAGS_LIBRARY ${gflags_LIBRARIES})
Packit ea1746
Packit ea1746
    # gflags does not export the namespace in their CMake configuration, so
Packit ea1746
    # use our function to determine what it should be, as it can be either
Packit ea1746
    # gflags or google dependent upon version & configuration.
Packit ea1746
    #
Packit ea1746
    # NOTE: We use the regex method to determine the namespace here, as
Packit ea1746
    #       check_cxx_source_compiles() will not use imported targets, which
Packit ea1746
    #       is what gflags will be in this case.
Packit ea1746
    gflags_check_gflags_namespace_using_regex()
Packit ea1746
Packit ea1746
    if (NOT GFLAGS_NAMESPACE)
Packit ea1746
      gflags_report_not_found(
Packit ea1746
        "Failed to determine gflags namespace using regex for gflags "
Packit ea1746
        "version: ${gflags_VERSION} exported here: ${gflags_DIR} using CMake.")
Packit ea1746
    endif (NOT GFLAGS_NAMESPACE)
Packit ea1746
  else (FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION)
Packit ea1746
    message(STATUS "Failed to find an installed/exported CMake configuration "
Packit ea1746
      "for gflags, will perform search for installed gflags components.")
Packit ea1746
  endif (FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION)
Packit ea1746
endif(GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION)
Packit ea1746
Packit ea1746
if (NOT GFLAGS_FOUND)
Packit ea1746
  # Either failed to find an exported gflags CMake configuration, or user
Packit ea1746
  # told us not to use one.  Perform a manual search for all gflags components.
Packit ea1746
Packit ea1746
  # Handle possible presence of lib prefix for libraries on MSVC, see
Packit ea1746
  # also GFLAGS_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 GFLAGS_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
  list(APPEND GFLAGS_CHECK_PATH_SUFFIXES
Packit ea1746
    gflags/include # Windows (for C:/Program Files prefix).
Packit ea1746
    gflags/Include ) # Windows (for C:/Program Files prefix).
Packit ea1746
Packit ea1746
  list(APPEND GFLAGS_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
  list(APPEND GFLAGS_CHECK_LIBRARY_SUFFIXES
Packit ea1746
    gflags/lib # Windows (for C:/Program Files prefix).
Packit ea1746
    gflags/Lib ) # Windows (for C:/Program Files prefix).
Packit ea1746
Packit ea1746
  # Search supplied hint directories first if supplied.
Packit ea1746
  find_path(GFLAGS_INCLUDE_DIR
Packit ea1746
    NAMES gflags/gflags.h
Packit ea1746
    HINTS ${GFLAGS_INCLUDE_DIR_HINTS}
Packit ea1746
    PATHS ${GFLAGS_CHECK_INCLUDE_DIRS}
Packit ea1746
    PATH_SUFFIXES ${GFLAGS_CHECK_PATH_SUFFIXES})
Packit ea1746
  if (NOT GFLAGS_INCLUDE_DIR OR
Packit ea1746
      NOT EXISTS ${GFLAGS_INCLUDE_DIR})
Packit ea1746
    gflags_report_not_found(
Packit ea1746
      "Could not find gflags include directory, set GFLAGS_INCLUDE_DIR "
Packit ea1746
      "to directory containing gflags/gflags.h")
Packit ea1746
  endif (NOT GFLAGS_INCLUDE_DIR OR
Packit ea1746
    NOT EXISTS ${GFLAGS_INCLUDE_DIR})
Packit ea1746
Packit ea1746
  find_library(GFLAGS_LIBRARY NAMES gflags
Packit ea1746
    HINTS ${GFLAGS_LIBRARY_DIR_HINTS}
Packit ea1746
    PATHS ${GFLAGS_CHECK_LIBRARY_DIRS}
Packit ea1746
    PATH_SUFFIXES ${GFLAGS_CHECK_LIBRARY_SUFFIXES})
Packit ea1746
  if (NOT GFLAGS_LIBRARY OR
Packit ea1746
      NOT EXISTS ${GFLAGS_LIBRARY})
Packit ea1746
    gflags_report_not_found(
Packit ea1746
      "Could not find gflags library, set GFLAGS_LIBRARY "
Packit ea1746
      "to full path to libgflags.")
Packit ea1746
  endif (NOT GFLAGS_LIBRARY OR
Packit ea1746
    NOT EXISTS ${GFLAGS_LIBRARY})
Packit ea1746
Packit ea1746
  # gflags typically requires a threading library (which is OS dependent), note
Packit ea1746
  # that this defines the CMAKE_THREAD_LIBS_INIT variable.  If we are able to
Packit ea1746
  # detect threads, we assume that gflags requires it.
Packit ea1746
  find_package(Threads QUIET)
Packit ea1746
  set(GFLAGS_LINK_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
Packit ea1746
  # On Windows (including MinGW), the Shlwapi library is used by gflags if
Packit ea1746
  # available.
Packit ea1746
  if (WIN32)
Packit ea1746
    include(CheckIncludeFileCXX)
Packit ea1746
    check_include_file_cxx("shlwapi.h" HAVE_SHLWAPI)
Packit ea1746
    if (HAVE_SHLWAPI)
Packit ea1746
      list(APPEND GFLAGS_LINK_LIBRARIES shlwapi.lib)
Packit ea1746
    endif(HAVE_SHLWAPI)
Packit ea1746
  endif (WIN32)
Packit ea1746
Packit ea1746
  # Mark internally as found, then verify. GFLAGS_REPORT_NOT_FOUND() unsets
Packit ea1746
  # if called.
Packit ea1746
  set(GFLAGS_FOUND TRUE)
Packit ea1746
Packit ea1746
  # Identify what namespace gflags was built with.
Packit ea1746
  if (GFLAGS_INCLUDE_DIR AND NOT GFLAGS_NAMESPACE)
Packit ea1746
    # To handle Windows peculiarities / CMake bugs on MSVC we try two approaches
Packit ea1746
    # to detect the gflags namespace:
Packit ea1746
    #
Packit ea1746
    # 1) Try to use check_cxx_source_compiles() to compile a trivial program
Packit ea1746
    #    with the two choices for the gflags namespace.
Packit ea1746
    #
Packit ea1746
    # 2) [In the event 1) fails] Use regex on the gflags headers to try to
Packit ea1746
    #    determine the gflags namespace.  Whilst this is less robust than 1),
Packit ea1746
    #    it does avoid any interaction with msbuild.
Packit ea1746
    gflags_check_gflags_namespace_using_try_compile()
Packit ea1746
Packit ea1746
    if (NOT GFLAGS_NAMESPACE)
Packit ea1746
      # Failed to determine gflags namespace using check_cxx_source_compiles()
Packit ea1746
      # method, try and obtain it using regex on the gflags headers instead.
Packit ea1746
      message(STATUS "Failed to find gflags namespace using using "
Packit ea1746
        "check_cxx_source_compiles(), trying namespace regex instead, "
Packit ea1746
        "this is expected on Windows.")
Packit ea1746
      gflags_check_gflags_namespace_using_regex()
Packit ea1746
Packit ea1746
      if (NOT GFLAGS_NAMESPACE)
Packit ea1746
        gflags_report_not_found(
Packit ea1746
          "Failed to determine gflags namespace either by "
Packit ea1746
          "check_cxx_source_compiles(), or namespace regex.")
Packit ea1746
      endif (NOT GFLAGS_NAMESPACE)
Packit ea1746
    endif (NOT GFLAGS_NAMESPACE)
Packit ea1746
  endif (GFLAGS_INCLUDE_DIR AND NOT GFLAGS_NAMESPACE)
Packit ea1746
Packit ea1746
  # Make the GFLAGS_NAMESPACE a cache variable s/t the user can view it, and could
Packit ea1746
  # overwrite it in the CMake GUI.
Packit ea1746
  set(GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}" CACHE STRING
Packit ea1746
    "gflags namespace (google or gflags)" FORCE)
Packit ea1746
Packit ea1746
  # gflags 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 GFLAGS_NAMESPACE in the cache / GUI
Packit ea1746
  # with an invalid value.
Packit ea1746
  if (GFLAGS_NAMESPACE AND
Packit ea1746
      NOT GFLAGS_NAMESPACE STREQUAL "google" AND
Packit ea1746
      NOT GFLAGS_NAMESPACE STREQUAL "gflags")
Packit ea1746
    gflags_report_not_found(
Packit ea1746
      "Caller defined GFLAGS_NAMESPACE:"
Packit ea1746
      " ${GFLAGS_NAMESPACE} is not valid, not google or gflags.")
Packit ea1746
  endif ()
Packit ea1746
  # Catch case when caller has set GFLAGS_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 (GFLAGS_INCLUDE_DIR AND
Packit ea1746
      NOT EXISTS ${GFLAGS_INCLUDE_DIR}/gflags/gflags.h)
Packit ea1746
    gflags_report_not_found(
Packit ea1746
      "Caller defined GFLAGS_INCLUDE_DIR:"
Packit ea1746
      " ${GFLAGS_INCLUDE_DIR} does not contain gflags/gflags.h header.")
Packit ea1746
  endif (GFLAGS_INCLUDE_DIR AND
Packit ea1746
    NOT EXISTS ${GFLAGS_INCLUDE_DIR}/gflags/gflags.h)
Packit ea1746
  # TODO: This regex for gflags 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 "${GFLAGS_LIBRARY}" LOWERCASE_GFLAGS_LIBRARY)
Packit ea1746
  if (GFLAGS_LIBRARY AND
Packit ea1746
      NOT "${LOWERCASE_GFLAGS_LIBRARY}" MATCHES ".*gflags[^/]*")
Packit ea1746
    gflags_report_not_found(
Packit ea1746
      "Caller defined GFLAGS_LIBRARY: "
Packit ea1746
      "${GFLAGS_LIBRARY} does not match gflags.")
Packit ea1746
  endif (GFLAGS_LIBRARY AND
Packit ea1746
    NOT "${LOWERCASE_GFLAGS_LIBRARY}" MATCHES ".*gflags[^/]*")
Packit ea1746
Packit ea1746
  gflags_reset_find_library_prefix()
Packit ea1746
Packit ea1746
endif(NOT GFLAGS_FOUND)
Packit ea1746
Packit ea1746
# Set standard CMake FindPackage variables if found.
Packit ea1746
if (GFLAGS_FOUND)
Packit ea1746
  set(GFLAGS_INCLUDE_DIRS ${GFLAGS_INCLUDE_DIR})
Packit ea1746
  set(GFLAGS_LIBRARIES ${GFLAGS_LIBRARY} ${GFLAGS_LINK_LIBRARIES})
Packit ea1746
endif (GFLAGS_FOUND)
Packit ea1746
Packit ea1746
# Handle REQUIRED / QUIET optional arguments.
Packit ea1746
include(FindPackageHandleStandardArgs)
Packit ea1746
find_package_handle_standard_args(Gflags DEFAULT_MSG
Packit ea1746
  GFLAGS_INCLUDE_DIRS GFLAGS_LIBRARIES GFLAGS_NAMESPACE)
Packit ea1746
Packit ea1746
# Only mark internal variables as advanced if we found gflags, otherwise
Packit ea1746
# leave them visible in the standard GUI for the user to set manually.
Packit ea1746
if (GFLAGS_FOUND)
Packit ea1746
  mark_as_advanced(FORCE GFLAGS_INCLUDE_DIR
Packit ea1746
    GFLAGS_LIBRARY
Packit ea1746
    GFLAGS_NAMESPACE
Packit ea1746
    gflags_DIR) # Autogenerated by find_package(gflags)
Packit ea1746
endif (GFLAGS_FOUND)