Cleanup FindGflags & use installed gflags CMake config if present.
- Split out gflags namespace detection methods:
check_cxx_source_compiles() & regex, into separate functions.
- Use installed/exported gflags CMake configuration (present for
versions >= 2.1) if available, unless user expresses a preference not
to, or specifies search directories, in which case fall back to manual
search for components.
-- Prefer installed gflags CMake configurations over exported gflags
build directories on all OSs.
- Remove custom version of check_cxx_source_compiles() that attempted
to force the build type of the test project. This only worked for
NMake on Windows, not MSVC as msbuild ignored our attempts to force
the build type. Now we always use the regex method on Windows if
we cannot find an installed gflags CMake configuration which works
even on MSVC by bypassing msbuild.
- Add default search paths for gflags on Windows.
Change-Id: I083b267d97a7a5838a1314f3d41a61ae48d5a2d7
diff --git a/cmake/FindGflags.cmake b/cmake/FindGflags.cmake
index b144a05..3f09406 100644
--- a/cmake/FindGflags.cmake
+++ b/cmake/FindGflags.cmake
@@ -31,6 +31,12 @@
# FindGflags.cmake - Find Google gflags logging library.
#
+# This module will attempt to find gflags, either via an exported CMake
+# configuration (generated by gflags >= 2.1 which are built with CMake), or
+# by performing a standard search for all gflags components. The order of
+# precedence for these two methods of finding gflags is controlled by:
+# GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION.
+#
# This module defines the following variables:
#
# GFLAGS_FOUND: TRUE iff gflags is found.
@@ -42,8 +48,19 @@
# gflags to be something else (i.e. google for legacy
# compatibility).
#
-# The following variables control the behaviour of this module:
+# The following variables control the behaviour of this module when an exported
+# gflags CMake configuration is not found.
#
+# GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION: TRUE/FALSE, iff TRUE then
+# then prefer using an exported CMake configuration
+# generated by gflags >= 2.1 over searching for the
+# gflags components manually. Otherwise (FALSE)
+# ignore any exported gflags CMake configurations and
+# always perform a manual search for the components.
+# Default: TRUE iff user does not define this variable
+# before we are called, and does NOT specify either
+# GFLAGS_INCLUDE_DIR_HINTS or GFLAGS_LIBRARY_DIR_HINTS
+# otherwise FALSE.
# GFLAGS_INCLUDE_DIR_HINTS: List of additional directories in which to
# search for gflags includes, e.g: /timbuktu/include.
# GFLAGS_LIBRARY_DIR_HINTS: List of additional directories in which to
@@ -65,8 +82,8 @@
# GFLAGS_LIBRARY: gflags library, not including the libraries of any
# dependencies.
-# Reset CALLERS_CMAKE_FIND_LIBRARY_PREFIXES to its value when
-# FindGflags was invoked.
+# Reset CALLERS_CMAKE_FIND_LIBRARY_PREFIXES to its value when FindGflags was
+# invoked, necessary for MSVC.
MACRO(GFLAGS_RESET_FIND_LIBRARY_PREFIX)
IF (MSVC)
SET(CMAKE_FIND_LIBRARY_PREFIXES "${CALLERS_CMAKE_FIND_LIBRARY_PREFIXES}")
@@ -106,311 +123,397 @@
ENDIF ()
ENDMACRO(GFLAGS_REPORT_NOT_FOUND)
-# A cut down version of CMake's check_cxx_source_compiles() macro, which
-# supports specification of the CMAKE_BUILD_TYPE with which the test should
-# be compiled (but not REGEX matching of the output). This is important
-# on Windows, for at least the NMake generator, which otherwise chooses
-# Debug, when the gflags library will typically be compiled in Release, and
-# MSVC will then fail to build, thus making all check_cxx_source_compiles()
-# tests fail.
-#
-# As per the CMake check_cxx_source_compiles() macro, we assume the following
-# variables can be set before this is invoked:
-#
-# CMAKE_REQUIRED_FLAGS - String of compile command line flags.
-# CMAKE_REQUIRED_DEFINITIONS - List of macros to define (-DFOO=bar).
-# CMAKE_REQUIRED_INCLUDES - List of include directories.
-# CMAKE_REQUIRED_LIBRARIES - List of libraries to link.
-#
-# This macro is a derivative of the CMake check_cxx_source_compiles() macro
-# distributed under the BSD License, Copyright 2005-2009 Kitware, Inc.
-MACRO(CHECK_CXX_SOURCE_COMPILES_WITH_BUILD_TYPE
- SOURCE BUILD_TYPE VAR)
- # Do not rerun test if output variable has an assigned value, ie is not
- # an empty string.
- IF ("${VAR}" MATCHES "^${VAR}$")
- # Verify that the BUILD_TYPE is sane.
- IF (NOT "${BUILD_TYPE}" STREQUAL "Release" AND
- NOT "${BUILD_TYPE}" STREQUAL "Debug" AND
- NOT "${BUILD_TYPE}" STREQUAL "RelWithDebInfo" AND
- NOT "${BUILD_TYPE}" STREQUAL "MinSizeRel" AND
- NOT "${BUILD_TYPE}" STREQUAL "")
- MESSAGE(FATAL_ERROR "Invalid BUILD_TYPE: ${BUILD_TYPE}, is not a "
- "valid CMAKE_BUILD_TYPE option.")
+# Verify that all variable names passed as arguments are defined (can be empty
+# but must be defined) or raise a fatal error.
+MACRO(GFLAGS_CHECK_VARS_DEFINED)
+ FOREACH(CHECK_VAR ${ARGN})
+ IF (NOT DEFINED ${CHECK_VAR})
+ MESSAGE(FATAL_ERROR "Ceres Bug: ${CHECK_VAR} is not defined.")
ENDIF()
+ ENDFOREACH()
+ENDMACRO(GFLAGS_CHECK_VARS_DEFINED)
- SET(MACRO_CHECK_FUNCTION_DEFINITIONS
- "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
-
- SET(CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES)
- IF (CMAKE_REQUIRED_LIBRARIES)
- SET(CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES
- LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
- ENDIF()
-
- SET(CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES)
- IF (CMAKE_REQUIRED_INCLUDES)
- SET(CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES
- "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
- ENDIF()
-
- FILE(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx"
- "${SOURCE}\n")
-
- MESSAGE(STATUS "Performing Test ${VAR}")
- # Pass the CMAKE_BUILD_TYPE to the CMake test project created to execute
- # the test (main difference over CMake's check_cxx_source_compiles() macro).
- TRY_COMPILE(${VAR}
- ${CMAKE_BINARY_DIR}
- ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx
- COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
- ${CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES}
- CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
- "-DCMAKE_BUILD_TYPE:STRING=${BUILD_TYPE}"
- "${CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES}"
- OUTPUT_VARIABLE OUTPUT)
-
- IF (${VAR})
- MESSAGE(STATUS "Performing Test ${VAR} - Success")
- SET(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
- FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
- "Performing C++ SOURCE FILE Test ${VAR} succeded with the following output:\n"
- "${OUTPUT}\n"
- "Source file was:\n${SOURCE}\n")
- ELSE()
- MESSAGE(STATUS "Performing Test ${VAR} - Failed")
- SET(${VAR} "" CACHE INTERNAL "Test ${VAR}")
- FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
- "Performing C++ SOURCE FILE Test ${VAR} failed with the following output:\n"
- "${OUTPUT}\n"
- "Source file was:\n${SOURCE}\n")
- ENDIF()
- ENDIF()
-ENDMACRO()
-
-# Handle possible presence of lib prefix for libraries on MSVC, see
-# also GFLAGS_RESET_FIND_LIBRARY_PREFIX().
-IF (MSVC)
- # Preserve the caller's original values for CMAKE_FIND_LIBRARY_PREFIXES
- # s/t we can set it back before returning.
- SET(CALLERS_CMAKE_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
- # The empty string in this list is important, it represents the case when
- # the libraries have no prefix (shared libraries / DLLs).
- SET(CMAKE_FIND_LIBRARY_PREFIXES "lib" "" "${CMAKE_FIND_LIBRARY_PREFIXES}")
-ENDIF (MSVC)
-
-# Search user-installed locations first, so that we prefer user installs
-# to system installs where both exist.
+# Use check_cxx_source_compiles() to compile trivial test programs to determine
+# the gflags namespace. This works on all OSs except Windows. If using Visual
+# Studio, it fails because msbuild forces check_cxx_source_compiles() to use
+# CMAKE_BUILD_TYPE=Debug for the test project, which usually breaks detection
+# because MSVC requires that the test project use the same build type as gflags,
+# which would normally be built in Release.
#
-# TODO: Add standard Windows search locations for gflags.
-LIST(APPEND GFLAGS_CHECK_INCLUDE_DIRS
- /usr/local/include
- /usr/local/homebrew/include # Mac OS X
- /opt/local/var/macports/software # Mac OS X.
- /opt/local/include
- /usr/include)
-LIST(APPEND GFLAGS_CHECK_LIBRARY_DIRS
- /usr/local/lib
- /usr/local/homebrew/lib # Mac OS X.
- /opt/local/lib
- /usr/lib)
+# Defines: GFLAGS_NAMESPACE in the caller's scope with the detected namespace,
+# which is blank (empty string, will test FALSE is CMake conditionals)
+# if detection failed.
+FUNCTION(GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_TRY_COMPILE)
+ # Verify that all required variables are defined.
+ GFLAGS_CHECK_VARS_DEFINED(
+ GFLAGS_INCLUDE_DIR GFLAGS_LIBRARY)
+ # Ensure that GFLAGS_NAMESPACE is always unset on completion unless
+ # we explicitly set if after having the correct namespace.
+ SET(GFLAGS_NAMESPACE "" PARENT_SCOPE)
-# Search supplied hint directories first if supplied.
-FIND_PATH(GFLAGS_INCLUDE_DIR
- NAMES gflags/gflags.h
- PATHS ${GFLAGS_INCLUDE_DIR_HINTS}
- ${GFLAGS_CHECK_INCLUDE_DIRS})
-IF (NOT GFLAGS_INCLUDE_DIR OR
- NOT EXISTS ${GFLAGS_INCLUDE_DIR})
- GFLAGS_REPORT_NOT_FOUND(
- "Could not find gflags include directory, set GFLAGS_INCLUDE_DIR "
- "to directory containing gflags/gflags.h")
-ENDIF (NOT GFLAGS_INCLUDE_DIR OR
- NOT EXISTS ${GFLAGS_INCLUDE_DIR})
-
-FIND_LIBRARY(GFLAGS_LIBRARY NAMES gflags
- PATHS ${GFLAGS_LIBRARY_DIR_HINTS}
- ${GFLAGS_CHECK_LIBRARY_DIRS})
-IF (NOT GFLAGS_LIBRARY OR
- NOT EXISTS ${GFLAGS_LIBRARY})
- GFLAGS_REPORT_NOT_FOUND(
- "Could not find gflags library, set GFLAGS_LIBRARY "
- "to full path to libgflags.")
-ENDIF (NOT GFLAGS_LIBRARY OR
- NOT EXISTS ${GFLAGS_LIBRARY})
-
-# gflags typically requires a threading library (which is OS dependent), note
-# that this defines the CMAKE_THREAD_LIBS_INIT variable. If we are able to
-# detect threads, we assume that gflags requires it.
-FIND_PACKAGE(Threads QUIET)
-SET(GFLAGS_LINK_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
-# On Windows (including MinGW), the Shlwapi library is used by gflags if
-# available.
-IF (WIN32)
- INCLUDE(CheckIncludeFileCXX)
- CHECK_INCLUDE_FILE_CXX("shlwapi.h" HAVE_SHLWAPI)
- IF (HAVE_SHLWAPI)
- LIST(APPEND GFLAGS_LINK_LIBRARIES shlwapi.lib)
- ENDIF(HAVE_SHLWAPI)
-ENDIF (WIN32)
-
-# Mark internally as found, then verify. GFLAGS_REPORT_NOT_FOUND() unsets
-# if called.
-SET(GFLAGS_FOUND TRUE)
-
-# Identify what namespace gflags was built with.
-IF (GFLAGS_INCLUDE_DIR AND NOT GFLAGS_NAMESPACE)
- # To handle Windows peculiarities / CMake bugs on MSVC we try two approaches
- # to detect the gflags namespace:
- #
- # 1) Try to use a custom version of check_cxx_source_compiles():
- # check_cxx_source_compiles_with_build_type(),
- # to compile a trivial program with the two choices for the gflags
- # namespace.
- #
- # This works on all OSs except Windows. On Windows, if using NMake
- # generator the previous method also works, if using Visual Studio, it does
- # not, because our explicit instruction regarding the CMAKE_BUILD_TYPE is
- # ignored by msbuild. This breaks detection because MSVC requires that the
- # build type of the test project used by check_cxx_source_compiles() match
- # that of gflags. However, msbuild forces the test project to use Debug,
- # but gflags will be built in Release.
- #
- # 2) [In the event 1) fails] Use regex on the gflags.h header file to try to
- # determine the gflags namespace. Whilst this is less robust than 1),
- # it does avoid any interaction with msbuild.
-
- # On Windows, it is required that the build type of the test app match that
- # of gflags, as CMAKE_BUILD_TYPE may not be defined when this is called, we
- # try each in turn.
- LIST(APPEND TEST_BUILD_TYPES Release Debug)
- FOREACH(BUILD_TYPE ${TEST_BUILD_TYPES})
- STRING(TOUPPER "${BUILD_TYPE}" BUILD_TYPE_UPPERCASE)
- # Setup include path & link library for gflags for CHECK_CXX_SOURCE_COMPILES.
- SET(CMAKE_REQUIRED_INCLUDES ${GFLAGS_INCLUDE_DIR})
- SET(CMAKE_REQUIRED_LIBRARIES ${GFLAGS_LIBRARY} ${GFLAGS_LINK_LIBRARIES})
- # First try the (older) google namespace. Note that the output variable
- # MUST be unique to the build type as otherwise the test is not repeated as
- # it is assumed to have already been performed.
- CHECK_CXX_SOURCE_COMPILES_WITH_BUILD_TYPE(
- "#include <gflags/gflags.h>
+ INCLUDE(CheckCXXSourceCompiles)
+ # Setup include path & link library for gflags for CHECK_CXX_SOURCE_COMPILES.
+ SET(CMAKE_REQUIRED_INCLUDES ${GFLAGS_INCLUDE_DIR})
+ SET(CMAKE_REQUIRED_LIBRARIES ${GFLAGS_LIBRARY} ${GFLAGS_LINK_LIBRARIES})
+ # First try the (older) google namespace. Note that the output variable
+ # MUST be unique to the build type as otherwise the test is not repeated as
+ # it is assumed to have already been performed.
+ CHECK_CXX_SOURCE_COMPILES(
+ "#include <gflags/gflags.h>
int main(int argc, char * argv[]) {
google::ParseCommandLineFlags(&argc, &argv, true);
return 0;
}"
- ${BUILD_TYPE}
- GFLAGS_IN_GOOGLE_NAMESPACE_${BUILD_TYPE_UPPERCASE})
- IF (GFLAGS_IN_GOOGLE_NAMESPACE_${BUILD_TYPE_UPPERCASE})
- SET(GFLAGS_NAMESPACE google)
- BREAK()
- ELSE (GFLAGS_IN_GOOGLE_NAMESPACE_${BUILD_TYPE_UPPERCASE})
- # Try (newer) gflags namespace instead. Note that the output variable
- # MUST be unique to the build type as otherwise the test is not repeated as
- # it is assumed to have already been performed.
- SET(CMAKE_REQUIRED_INCLUDES ${GFLAGS_INCLUDE_DIR})
- SET(CMAKE_REQUIRED_LIBRARIES ${GFLAGS_LIBRARY} ${GFLAGS_LINK_LIBRARIES})
- CHECK_CXX_SOURCE_COMPILES_WITH_BUILD_TYPE(
- "#include <gflags/gflags.h>
- int main(int argc, char * argv[]) {
- gflags::ParseCommandLineFlags(&argc, &argv, true);
- return 0;
- }"
- ${BUILD_TYPE}
- GFLAGS_IN_GFLAGS_NAMESPACE_${BUILD_TYPE_UPPERCASE})
- IF (GFLAGS_IN_GFLAGS_NAMESPACE_${BUILD_TYPE_UPPERCASE})
- SET(GFLAGS_NAMESPACE gflags)
- BREAK()
- ENDIF (GFLAGS_IN_GFLAGS_NAMESPACE_${BUILD_TYPE_UPPERCASE})
- ENDIF (GFLAGS_IN_GOOGLE_NAMESPACE_${BUILD_TYPE_UPPERCASE})
- ENDFOREACH()
+ GFLAGS_IN_GOOGLE_NAMESPACE)
+ IF (GFLAGS_IN_GOOGLE_NAMESPACE)
+ SET(GFLAGS_NAMESPACE google PARENT_SCOPE)
+ RETURN()
+ ENDIF()
- IF (NOT GFLAGS_NAMESPACE)
- # Failed to determine gflags namespace using
- # check_cxx_source_compiles_with_build_type() method, try and obtain it
- # using regex on the gflags header instead.
- MESSAGE(STATUS "Failed to find gflags namespace using using "
- "check_cxx_source_compiles(), trying namespace regex instead, "
- "this is expected on Windows.")
- # Scan gflags.h to identify what namespace gflags was built with.
- SET(GFLAGS_HEADER_FILE ${GFLAGS_INCLUDE_DIR}/gflags/gflags.h)
- IF (NOT EXISTS ${GFLAGS_HEADER_FILE})
- GFLAGS_REPORT_NOT_FOUND(
- "Could not find file: ${GFLAGS_HEADER_FILE} "
- "containing namespace information in gflags install located at: "
- "${GFLAGS_INCLUDE_DIR}.")
- ELSE (NOT EXISTS ${GFLAGS_HEADER_FILE})
- FILE(READ ${GFLAGS_HEADER_FILE} GFLAGS_HEADER_FILE_CONTENTS)
+ # Try (newer) gflags namespace instead. Note that the output variable
+ # MUST be unique to the build type as otherwise the test is not repeated as
+ # it is assumed to have already been performed.
+ SET(CMAKE_REQUIRED_INCLUDES ${GFLAGS_INCLUDE_DIR})
+ SET(CMAKE_REQUIRED_LIBRARIES ${GFLAGS_LIBRARY} ${GFLAGS_LINK_LIBRARIES})
+ CHECK_CXX_SOURCE_COMPILES(
+ "#include <gflags/gflags.h>
+ int main(int argc, char * argv[]) {
+ gflags::ParseCommandLineFlags(&argc, &argv, true);
+ return 0;
+ }"
+ GFLAGS_IN_GFLAGS_NAMESPACE)
+ IF (GFLAGS_IN_GFLAGS_NAMESPACE)
+ SET(GFLAGS_NAMESPACE gflags PARENT_SCOPE)
+ RETURN()
+ ENDIF (GFLAGS_IN_GFLAGS_NAMESPACE)
+ENDFUNCTION(GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_TRY_COMPILE)
- STRING(REGEX MATCH "namespace [A-Za-z]+"
- GFLAGS_NAMESPACE "${GFLAGS_HEADER_FILE_CONTENTS}")
- STRING(REGEX REPLACE "namespace ([A-Za-z]+)" "\\1"
- GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}")
+# Use regex on the gflags headers to attempt to determine the gflags namespace.
+# Checks both gflags.h (contained namespace on versions < 2.1.2) and
+# gflags_declare.h, which contains the namespace on versions >= 2.1.2.
+# In general, this method should only be used when
+# GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_TRY_COMPILE() cannot be used, or has
+# failed.
+#
+# Defines: GFLAGS_NAMESPACE in the caller's scope with the detected namespace,
+# which is blank (empty string, will test FALSE is CMake conditionals)
+# if detection failed.
+FUNCTION(GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_REGEX)
+ # Verify that all required variables are defined.
+ GFLAGS_CHECK_VARS_DEFINED(GFLAGS_INCLUDE_DIR)
+ # Ensure that GFLAGS_NAMESPACE is always undefined on completion unless
+ # we explicitly set if after having the correct namespace.
+ SET(GFLAGS_NAMESPACE "" PARENT_SCOPE)
- IF (NOT GFLAGS_NAMESPACE)
+ # Scan gflags.h to identify what namespace gflags was built with. On
+ # versions of gflags < 2.1.2, gflags.h was configured with the namespace
+ # directly, on >= 2.1.2, gflags.h uses the GFLAGS_NAMESPACE #define which
+ # is defined in gflags_declare.h, we try each location in turn.
+ SET(GFLAGS_HEADER_FILE ${GFLAGS_INCLUDE_DIR}/gflags/gflags.h)
+ IF (NOT EXISTS ${GFLAGS_HEADER_FILE})
+ GFLAGS_REPORT_NOT_FOUND(
+ "Could not find file: ${GFLAGS_HEADER_FILE} "
+ "containing namespace information in gflags install located at: "
+ "${GFLAGS_INCLUDE_DIR}.")
+ ENDIF()
+ FILE(READ ${GFLAGS_HEADER_FILE} GFLAGS_HEADER_FILE_CONTENTS)
+
+ STRING(REGEX MATCH "namespace [A-Za-z]+"
+ GFLAGS_NAMESPACE "${GFLAGS_HEADER_FILE_CONTENTS}")
+ STRING(REGEX REPLACE "namespace ([A-Za-z]+)" "\\1"
+ GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}")
+
+ IF (NOT GFLAGS_NAMESPACE)
+ GFLAGS_REPORT_NOT_FOUND(
+ "Failed to extract gflags namespace from header file: "
+ "${GFLAGS_HEADER_FILE}.")
+ ENDIF (NOT GFLAGS_NAMESPACE)
+
+ IF (GFLAGS_NAMESPACE STREQUAL "google" OR
+ GFLAGS_NAMESPACE STREQUAL "gflags")
+ # Found valid gflags namespace from gflags.h.
+ SET(GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}" PARENT_SCOPE)
+ RETURN()
+ ENDIF()
+
+ # Failed to find gflags namespace from gflags.h, gflags is likely a new
+ # version, check gflags_declare.h, which in newer versions (>= 2.1.2) contains
+ # the GFLAGS_NAMESPACE #define, which is then referenced in gflags.h.
+ SET(GFLAGS_DECLARE_FILE ${GFLAGS_INCLUDE_DIR}/gflags/gflags_declare.h)
+ IF (NOT EXISTS ${GFLAGS_DECLARE_FILE})
+ GFLAGS_REPORT_NOT_FOUND(
+ "Could not find file: ${GFLAGS_DECLARE_FILE} "
+ "containing namespace information in gflags install located at: "
+ "${GFLAGS_INCLUDE_DIR}.")
+ ENDIF()
+ FILE(READ ${GFLAGS_DECLARE_FILE} GFLAGS_DECLARE_FILE_CONTENTS)
+
+ STRING(REGEX MATCH "#define GFLAGS_NAMESPACE [A-Za-z]+"
+ GFLAGS_NAMESPACE "${GFLAGS_DECLARE_FILE_CONTENTS}")
+ STRING(REGEX REPLACE "#define GFLAGS_NAMESPACE ([A-Za-z]+)" "\\1"
+ GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}")
+
+ IF (NOT GFLAGS_NAMESPACE)
+ GFLAGS_REPORT_NOT_FOUND(
+ "Failed to extract gflags namespace from declare file: "
+ "${GFLAGS_DECLARE_FILE}.")
+ ENDIF (NOT GFLAGS_NAMESPACE)
+
+ IF (GFLAGS_NAMESPACE STREQUAL "google" OR
+ GFLAGS_NAMESPACE STREQUAL "gflags")
+ # Found valid gflags namespace from gflags.h.
+ SET(GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}" PARENT_SCOPE)
+ RETURN()
+ ENDIF()
+ENDFUNCTION(GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_REGEX)
+
+# -----------------------------------------------------------------
+# By default, if the user has expressed no preference for using an exported
+# gflags CMake configuration over performing a search for the installed
+# components, and has not specified any hints for the search locations, then
+# prefer a gflags exported configuration if available.
+IF (NOT DEFINED GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION
+ AND NOT GFLAGS_INCLUDE_DIR_HINTS
+ AND NOT GFLAGS_LIBRARY_DIR_HINTS)
+ MESSAGE(STATUS "No preference for use of exported gflags CMake configuration "
+ "set, and no hints for include/library directories provided. "
+ "Defaulting to preferring an installed/exported gflags CMake configuration "
+ "if available.")
+ SET(GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION TRUE)
+ENDIF()
+
+IF (GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION)
+ # Try to find an exported CMake configuration for gflags, as generated by
+ # gflags versions >= 2.1.
+ #
+ # We search twice, s/t we can invert the ordering of precedence used by
+ # find_package() for exported package build directories, and installed
+ # packages (found via CMAKE_SYSTEM_PREFIX_PATH), listed as items 6) and 7)
+ # respectively in [1].
+ #
+ # By default, exported build directories are (in theory) detected first, and
+ # this is usually the case on Windows. However, on OS X & Linux, the install
+ # path (/usr/local) is typically present in the PATH environment variable
+ # which is checked in item 4) in [1] (i.e. before both of the above, unless
+ # NO_SYSTEM_ENVIRONMENT_PATH is passed). As such on those OSs installed
+ # packages are usually detected in preference to exported package build
+ # directories.
+ #
+ # To ensure a more consistent response across all OSs, and as users usually
+ # want to prefer an installed version of a package over a locally built one
+ # where both exist (esp. as the exported build directory might be removed
+ # after installation), we first search with NO_CMAKE_PACKAGE_REGISTRY which
+ # means any build directories exported by the user are ignored, and thus
+ # installed directories are preferred. If this fails to find the package
+ # we then research again, but without NO_CMAKE_PACKAGE_REGISTRY, so any
+ # exported build directories will now be detected.
+ #
+ # To prevent confusion on Windows, we also pass NO_CMAKE_BUILDS_PATH (which
+ # is item 5) in [1]), to not preferentially use projects that were built
+ # recently with the CMake GUI to ensure that we always prefer an installed
+ # version if available.
+ #
+ # [1] http://www.cmake.org/cmake/help/v2.8.11/cmake.html#command:find_package
+ FIND_PACKAGE(gflags QUIET
+ NO_MODULE
+ NO_CMAKE_PACKAGE_REGISTRY
+ NO_CMAKE_BUILDS_PATH)
+ IF (gflags_FOUND)
+ MESSAGE(STATUS "Found installed version of gflags: ${gflags_DIR}")
+ ELSE(gflags_FOUND)
+ # Failed to find an installed version of gflags, repeat search allowing
+ # exported build directories.
+ MESSAGE(STATUS "Failed to find installed version of gflags, searching for "
+ "gflags build directories exported with CMake.")
+ # Again pass NO_CMAKE_BUILDS_PATH, as we know that gflags is exported and
+ # do not want to treat projects built with the CMake GUI preferentially.
+ FIND_PACKAGE(gflags QUIET
+ NO_MODULE
+ NO_CMAKE_BUILDS_PATH)
+ IF (gflags_FOUND)
+ MESSAGE(STATUS "Found exported gflags build directory: ${gflags_DIR}")
+ ENDIF(gflags_FOUND)
+ ENDIF(gflags_FOUND)
+
+ SET(FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION ${gflags_FOUND})
+ IF (FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION)
+ MESSAGE(STATUS "Detected gflags version: ${gflags_VERSION}")
+ SET(GFLAGS_FOUND ${gflags_FOUND})
+ SET(GFLAGS_INCLUDE_DIR ${gflags_INCLUDE_DIR})
+ SET(GFLAGS_LIBRARY ${gflags_LIBRARIES})
+
+ # gflags does not export the namespace in their CMake configuration, so
+ # use our function to determine what it should be, as it can be either
+ # gflags or google dependent upon version & configuration.
+ #
+ # NOTE: We use the regex method to determine the namespace here, as
+ # check_cxx_source_compiles() will not use imported targets, which
+ # is what gflags will be in this case.
+ GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_REGEX()
+
+ IF (NOT GFLAGS_NAMESPACE)
GFLAGS_REPORT_NOT_FOUND(
- "Failed to extract gflags namespace from header file: "
- "${GFLAGS_HEADER_FILE}.")
- ENDIF (NOT GFLAGS_NAMESPACE)
+ "Failed to determine gflags namespace using regex for gflags "
+ "version: ${gflags_VERSION} exported here: ${gflags_DIR} using CMake.")
+ ENDIF (NOT GFLAGS_NAMESPACE)
+ ELSE (FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION)
+ MESSAGE(STATUS "Failed to find an installed/exported CMake configuration "
+ "for gflags, will perform search for installed gflags components.")
+ ENDIF (FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION)
+ENDIF(GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION)
- # Verify that the namespace is either google or gflags. Strictly
- # speaking the users can specify something else when building gflags,
- # but doing so would cause so many compatibility issues, that they should
- # not have done, so any mismatch is almost certainly a regex failure.
- IF (NOT GFLAGS_NAMESPACE STREQUAL "google" AND
- NOT GFLAGS_NAMESPACE STREQUAL "gflags")
- GFLAGS_REPORT_NOT_FOUND(
- "Failed to extract valid gflags namespace from header file: "
- "${GFLAGS_HEADER_FILE}, result: ${GFLAGS_NAMESPACE} is not "
- "google or gflags.")
- ENDIF()
- ENDIF (NOT EXISTS ${GFLAGS_HEADER_FILE})
- ENDIF (NOT GFLAGS_NAMESPACE)
+IF (NOT GFLAGS_FOUND)
+ # Either failed to find an exported gflags CMake configuration, or user
+ # told us not to use one. Perform a manual search for all gflags components.
- IF (NOT GFLAGS_NAMESPACE)
- GFLAGS_REPORT_NOT_FOUND(
- "Failed to determine gflags namespace either by "
- "check_cxx_source_compiles(), or namespace regex.")
- ENDIF (NOT GFLAGS_NAMESPACE)
-ENDIF (GFLAGS_INCLUDE_DIR AND NOT GFLAGS_NAMESPACE)
+ # Handle possible presence of lib prefix for libraries on MSVC, see
+ # also GFLAGS_RESET_FIND_LIBRARY_PREFIX().
+ IF (MSVC)
+ # Preserve the caller's original values for CMAKE_FIND_LIBRARY_PREFIXES
+ # s/t we can set it back before returning.
+ SET(CALLERS_CMAKE_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
+ # The empty string in this list is important, it represents the case when
+ # the libraries have no prefix (shared libraries / DLLs).
+ SET(CMAKE_FIND_LIBRARY_PREFIXES "lib" "" "${CMAKE_FIND_LIBRARY_PREFIXES}")
+ ENDIF (MSVC)
-# Make the GFLAGS_NAMESPACE a cache variable s/t the user can view it, and could
-# overwrite it in the CMake GUI.
-SET(GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}" CACHE STRING
- "gflags namespace (google or gflags)" FORCE)
+ # Search user-installed locations first, so that we prefer user installs
+ # to system installs where both exist.
+ LIST(APPEND GFLAGS_CHECK_INCLUDE_DIRS
+ /usr/local/include
+ /usr/local/homebrew/include # Mac OS X
+ /opt/local/var/macports/software # Mac OS X.
+ /opt/local/include
+ /usr/include)
+ LIST(APPEND GFLAGS_CHECK_PATH_SUFFIXES
+ gflags/include # Windows (for C:/Program Files prefix).
+ gflags/Include ) # Windows (for C:/Program Files prefix).
-# gflags does not seem to provide any record of the version in its
-# source tree, thus cannot extract version.
+ LIST(APPEND GFLAGS_CHECK_LIBRARY_DIRS
+ /usr/local/lib
+ /usr/local/homebrew/lib # Mac OS X.
+ /opt/local/lib
+ /usr/lib)
+ LIST(APPEND GFLAGS_CHECK_LIBRARY_SUFFIXES
+ gflags/lib # Windows (for C:/Program Files prefix).
+ gflags/Lib ) # Windows (for C:/Program Files prefix).
-# Catch case when caller has set GFLAGS_NAMESPACE in the cache / GUI
-# with an invalid value.
-IF (GFLAGS_NAMESPACE AND
- NOT GFLAGS_NAMESPACE STREQUAL "google" AND
- NOT GFLAGS_NAMESPACE STREQUAL "gflags")
- GFLAGS_REPORT_NOT_FOUND(
- "Caller defined GFLAGS_NAMESPACE:"
- " ${GFLAGS_NAMESPACE} is not valid, not google or gflags.")
-ENDIF ()
-# Catch case when caller has set GFLAGS_INCLUDE_DIR in the cache / GUI and
-# thus FIND_[PATH/LIBRARY] are not called, but specified locations are
-# invalid, otherwise we would report the library as found.
-IF (GFLAGS_INCLUDE_DIR AND
+ # Search supplied hint directories first if supplied.
+ FIND_PATH(GFLAGS_INCLUDE_DIR
+ NAMES gflags/gflags.h
+ PATHS ${GFLAGS_INCLUDE_DIR_HINTS}
+ ${GFLAGS_CHECK_INCLUDE_DIRS}
+ PATH_SUFFIXES ${GFLAGS_CHECK_PATH_SUFFIXES})
+ IF (NOT GFLAGS_INCLUDE_DIR OR
+ NOT EXISTS ${GFLAGS_INCLUDE_DIR})
+ GFLAGS_REPORT_NOT_FOUND(
+ "Could not find gflags include directory, set GFLAGS_INCLUDE_DIR "
+ "to directory containing gflags/gflags.h")
+ ENDIF (NOT GFLAGS_INCLUDE_DIR OR
+ NOT EXISTS ${GFLAGS_INCLUDE_DIR})
+
+ FIND_LIBRARY(GFLAGS_LIBRARY NAMES gflags
+ PATHS ${GFLAGS_LIBRARY_DIR_HINTS}
+ ${GFLAGS_CHECK_LIBRARY_DIRS}
+ PATH_SUFFIXES ${GFLAGS_CHECK_LIBRARY_SUFFIXES})
+ IF (NOT GFLAGS_LIBRARY OR
+ NOT EXISTS ${GFLAGS_LIBRARY})
+ GFLAGS_REPORT_NOT_FOUND(
+ "Could not find gflags library, set GFLAGS_LIBRARY "
+ "to full path to libgflags.")
+ ENDIF (NOT GFLAGS_LIBRARY OR
+ NOT EXISTS ${GFLAGS_LIBRARY})
+
+ # gflags typically requires a threading library (which is OS dependent), note
+ # that this defines the CMAKE_THREAD_LIBS_INIT variable. If we are able to
+ # detect threads, we assume that gflags requires it.
+ FIND_PACKAGE(Threads QUIET)
+ SET(GFLAGS_LINK_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
+ # On Windows (including MinGW), the Shlwapi library is used by gflags if
+ # available.
+ IF (WIN32)
+ INCLUDE(CheckIncludeFileCXX)
+ CHECK_INCLUDE_FILE_CXX("shlwapi.h" HAVE_SHLWAPI)
+ IF (HAVE_SHLWAPI)
+ LIST(APPEND GFLAGS_LINK_LIBRARIES shlwapi.lib)
+ ENDIF(HAVE_SHLWAPI)
+ ENDIF (WIN32)
+
+ # Mark internally as found, then verify. GFLAGS_REPORT_NOT_FOUND() unsets
+ # if called.
+ SET(GFLAGS_FOUND TRUE)
+
+ # Identify what namespace gflags was built with.
+ IF (GFLAGS_INCLUDE_DIR AND NOT GFLAGS_NAMESPACE)
+ # To handle Windows peculiarities / CMake bugs on MSVC we try two approaches
+ # to detect the gflags namespace:
+ #
+ # 1) Try to use check_cxx_source_compiles() to compile a trivial program
+ # with the two choices for the gflags namespace.
+ #
+ # 2) [In the event 1) fails] Use regex on the gflags headers to try to
+ # determine the gflags namespace. Whilst this is less robust than 1),
+ # it does avoid any interaction with msbuild.
+ GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_TRY_COMPILE()
+
+ IF (NOT GFLAGS_NAMESPACE)
+ # Failed to determine gflags namespace using check_cxx_source_compiles()
+ # method, try and obtain it using regex on the gflags headers instead.
+ MESSAGE(STATUS "Failed to find gflags namespace using using "
+ "check_cxx_source_compiles(), trying namespace regex instead, "
+ "this is expected on Windows.")
+ GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_REGEX()
+
+ IF (NOT GFLAGS_NAMESPACE)
+ GFLAGS_REPORT_NOT_FOUND(
+ "Failed to determine gflags namespace either by "
+ "check_cxx_source_compiles(), or namespace regex.")
+ ENDIF (NOT GFLAGS_NAMESPACE)
+ ENDIF (NOT GFLAGS_NAMESPACE)
+ ENDIF (GFLAGS_INCLUDE_DIR AND NOT GFLAGS_NAMESPACE)
+
+ # Make the GFLAGS_NAMESPACE a cache variable s/t the user can view it, and could
+ # overwrite it in the CMake GUI.
+ SET(GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}" CACHE STRING
+ "gflags namespace (google or gflags)" FORCE)
+
+ # gflags does not seem to provide any record of the version in its
+ # source tree, thus cannot extract version.
+
+ # Catch case when caller has set GFLAGS_NAMESPACE in the cache / GUI
+ # with an invalid value.
+ IF (GFLAGS_NAMESPACE AND
+ NOT GFLAGS_NAMESPACE STREQUAL "google" AND
+ NOT GFLAGS_NAMESPACE STREQUAL "gflags")
+ GFLAGS_REPORT_NOT_FOUND(
+ "Caller defined GFLAGS_NAMESPACE:"
+ " ${GFLAGS_NAMESPACE} is not valid, not google or gflags.")
+ ENDIF ()
+ # Catch case when caller has set GFLAGS_INCLUDE_DIR in the cache / GUI and
+ # thus FIND_[PATH/LIBRARY] are not called, but specified locations are
+ # invalid, otherwise we would report the library as found.
+ IF (GFLAGS_INCLUDE_DIR AND
+ NOT EXISTS ${GFLAGS_INCLUDE_DIR}/gflags/gflags.h)
+ GFLAGS_REPORT_NOT_FOUND(
+ "Caller defined GFLAGS_INCLUDE_DIR:"
+ " ${GFLAGS_INCLUDE_DIR} does not contain gflags/gflags.h header.")
+ ENDIF (GFLAGS_INCLUDE_DIR AND
NOT EXISTS ${GFLAGS_INCLUDE_DIR}/gflags/gflags.h)
- GFLAGS_REPORT_NOT_FOUND(
- "Caller defined GFLAGS_INCLUDE_DIR:"
- " ${GFLAGS_INCLUDE_DIR} does not contain gflags/gflags.h header.")
-ENDIF (GFLAGS_INCLUDE_DIR AND
- NOT EXISTS ${GFLAGS_INCLUDE_DIR}/gflags/gflags.h)
-# TODO: This regex for gflags library is pretty primitive, we use lowercase
-# for comparison to handle Windows using CamelCase library names, could
-# this check be better?
-STRING(TOLOWER "${GFLAGS_LIBRARY}" LOWERCASE_GFLAGS_LIBRARY)
-IF (GFLAGS_LIBRARY AND
+ # TODO: This regex for gflags library is pretty primitive, we use lowercase
+ # for comparison to handle Windows using CamelCase library names, could
+ # this check be better?
+ STRING(TOLOWER "${GFLAGS_LIBRARY}" LOWERCASE_GFLAGS_LIBRARY)
+ IF (GFLAGS_LIBRARY AND
+ NOT "${LOWERCASE_GFLAGS_LIBRARY}" MATCHES ".*gflags[^/]*")
+ GFLAGS_REPORT_NOT_FOUND(
+ "Caller defined GFLAGS_LIBRARY: "
+ "${GFLAGS_LIBRARY} does not match gflags.")
+ ENDIF (GFLAGS_LIBRARY AND
NOT "${LOWERCASE_GFLAGS_LIBRARY}" MATCHES ".*gflags[^/]*")
- GFLAGS_REPORT_NOT_FOUND(
- "Caller defined GFLAGS_LIBRARY: "
- "${GFLAGS_LIBRARY} does not match gflags.")
-ENDIF (GFLAGS_LIBRARY AND
- NOT "${LOWERCASE_GFLAGS_LIBRARY}" MATCHES ".*gflags[^/]*")
+
+ GFLAGS_RESET_FIND_LIBRARY_PREFIX()
+
+ENDIF(NOT GFLAGS_FOUND)
# Set standard CMake FindPackage variables if found.
IF (GFLAGS_FOUND)
@@ -418,8 +521,6 @@
SET(GFLAGS_LIBRARIES ${GFLAGS_LIBRARY} ${GFLAGS_LINK_LIBRARIES})
ENDIF (GFLAGS_FOUND)
-GFLAGS_RESET_FIND_LIBRARY_PREFIX()
-
# Handle REQUIRED / QUIET optional arguments.
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Gflags DEFAULT_MSG
@@ -429,6 +530,7 @@
# leave them visible in the standard GUI for the user to set manually.
IF (GFLAGS_FOUND)
MARK_AS_ADVANCED(FORCE GFLAGS_INCLUDE_DIR
- GFLAGS_LIBRARY
- GFLAGS_NAMESPACE)
+ GFLAGS_LIBRARY
+ GFLAGS_NAMESPACE
+ gflags_DIR) # Autogenerated by find_package(gflags)
ENDIF (GFLAGS_FOUND)