Modernizing the gflags dependency in cmake.

Summary:
Historically, for interface dependent libraries (glog, gflags and protobuf), exposing them in Caffe2Config.cmake is usually difficult.

New versions of glog and gflags ship with new-style cmake targets, so one does not need to use variables. New-style targets also make it easier for people to depend on them in installed config files.

This diff modernizes the gflags library, and still provides a fallback path if the installed gflags does not have cmake config files coming with it.

It does change one behavior of the build process though - when one specifies -DUSE_GFLAGS=ON but gflags cannot be found, the old script automatically turns it off but the new script crashes, forcing the user to specify USE_GFLAGS=OFF.
Closes https://github.com/caffe2/caffe2/pull/1819

Differential Revision: D6826604

Pulled By: Yangqing

fbshipit-source-id: 210f3926f291c8bfeb24eb9671e5adfcbf8cf7fe
This commit is contained in:
Yangqing Jia 2018-01-27 18:56:42 -08:00 committed by Facebook Github Bot
parent f8575f6d68
commit 73ed0d5ced
7 changed files with 92 additions and 45 deletions

View File

@ -193,7 +193,7 @@ if (USE_GLOG)
list(APPEND CAFFE2_INTERFACE_LIBS ${GLOG_LIBRARIES})
endif()
if (USE_GFLAGS)
list(APPEND CAFFE2_INTERFACE_LIBS ${GFLAGS_LIBRARIES})
list(APPEND CAFFE2_INTERFACE_LIBS gflags)
endif()
if (NOT CAFFE2_USE_CUSTOM_PROTOBUF)
list(APPEND CAFFE2_INTERFACE_LIBS ${PROTOBUF_LIBRARIES})
@ -232,6 +232,10 @@ if (BUILD_SHARED_LIBS)
${PROJECT_BINARY_DIR}/Caffe2Config.cmake
DESTINATION share/cmake/Caffe2
COMPONENT dev)
install(FILES
${PROJECT_SOURCE_DIR}/cmake/public/gflags.cmake
DESTINATION share/cmake/Caffe2/public
COMPONENT dev)
install(EXPORT Caffe2Targets DESTINATION share/cmake/Caffe2
FILE Caffe2Targets.cmake
COMPONENT dev)

View File

@ -10,6 +10,19 @@ set(CAFFE2_VERSION_MINOR @CAFFE2_VERSION_MINOR@)
set(CAFFE2_VERSION_PATCH @CAFFE2_VERSION_PATCH@)
set(CAFFE2_VERSION "@CAFFE2_VERSION@")
# Depending on whether Caffe2 uses gflags during compile time or
# not, invoke gflags.
if (@USE_GFLAGS@)
include("${CMAKE_CURRENT_LIST_DIR}/public/gflags.cmake")
if (NOT TARGET gflags)
message(FATAL_ERROR
"Your installed Caffe2 version uses gflags but the gflags library "
"cannot be found. Did you accidentally remove it, or have you set "
"the right CMAKE_PREFIX_PATH and/or GFLAGS_ROOT_DIR? If you do not "
"have gflags, you will need to install gflags and set the library "
"path accordingly.")
endif()
endif()
# import targets
include ("${CMAKE_CURRENT_LIST_DIR}/Caffe2Targets.cmake")

View File

@ -84,15 +84,14 @@ endif()
# ---[ gflags
if(USE_GFLAGS)
find_package(GFlags)
if(GFLAGS_FOUND)
include(cmake/public/gflags.cmake)
if (TARGET gflags)
set(CAFFE2_USE_GFLAGS 1)
caffe2_include_directories(${GFLAGS_INCLUDE_DIRS})
list(APPEND Caffe2_DEPENDENCY_LIBS ${GFLAGS_LIBRARIES})
list(APPEND Caffe2_DEPENDENCY_LIBS gflags)
else()
message(WARNING
"gflags is not found. Caffe2 will build without gflags support but it "
"is strongly recommended that you install gflags. Suppress this "
"gflags is not found. Caffe2 will build without gflags support but "
"it is strongly recommended that you install gflags. Suppress this "
"warning with -DUSE_GFLAGS=OFF")
set(USE_GFLAGS OFF)
endif()

View File

@ -1,38 +0,0 @@
# - Try to find GFLAGS
#
# The following variables are optionally searched for defaults
# GFLAGS_ROOT_DIR: Base directory where all GFLAGS components are found
#
# The following are set after configuration is done:
# GFLAGS_FOUND
# GFLAGS_INCLUDE_DIRS
# GFLAGS_LIBRARIES
# GFLAGS_LIBRARYRARY_DIRS
include(FindPackageHandleStandardArgs)
set(GFLAGS_ROOT_DIR "" CACHE PATH "Folder contains Gflags")
# We are testing only a couple of files in the include directories
if(NOT WIN32)
find_path(GFLAGS_INCLUDE_DIR gflags/gflags.h
PATHS ${GFLAGS_ROOT_DIR})
endif()
if(MSVC)
find_package(gflags NO_MODULE)
set(GFLAGS_LIBRARY ${gflags_LIBRARIES})
else()
find_library(GFLAGS_LIBRARY gflags)
endif()
find_package_handle_standard_args(GFlags DEFAULT_MSG GFLAGS_INCLUDE_DIR GFLAGS_LIBRARY)
if(GFLAGS_FOUND)
set(GFLAGS_INCLUDE_DIRS ${GFLAGS_INCLUDE_DIR})
set(GFLAGS_LIBRARIES ${GFLAGS_LIBRARY})
message(STATUS "Found gflags (include: ${GFLAGS_INCLUDE_DIR}, library: ${GFLAGS_LIBRARY})")
mark_as_advanced(GFLAGS_LIBRARY_DEBUG GFLAGS_LIBRARY_RELEASE
GFLAGS_LIBRARY GFLAGS_INCLUDE_DIR GFLAGS_ROOT_DIR)
endif()

67
cmake/public/gflags.cmake Normal file
View File

@ -0,0 +1,67 @@
# ---[ gflags
find_package(gflags)
if (TARGET gflags)
message(STATUS "Found gflags with new-style gflags target.")
elseif(GFLAGS_FOUND)
message(STATUS "Found gflags with old-style gflag starget.")
add_library(gflags UNKNOWN IMPORTED)
set_property(
TARGET gflags PROPERTY IMPORTED_LOCATION ${GFLAGS_LIBRARY})
set_property(
TARGET gflags PROPERTY INTERFACE_INCLUDE_DIRECTORIES
${GFLAGS_INCLUDE_DIR})
else()
message(STATUS "Cannot find gflags with config files. Using legacy find.")
# - Try to find GFLAGS in the legacy way.
#
# The following variables are optionally searched for defaults
# GFLAGS_ROOT_DIR: Base directory where all GFLAGS components are found
#
# The following are set after configuration is done:
# GFLAGS_FOUND
# GFLAGS_INCLUDE_DIRS
# GFLAGS_LIBRARIES
# GFLAGS_LIBRARYRARY_DIRS
include(FindPackageHandleStandardArgs)
set(GFLAGS_ROOT_DIR "" CACHE PATH "Folder contains Gflags")
# We are testing only a couple of files in the include directories
if(NOT WIN32)
find_path(GFLAGS_INCLUDE_DIR gflags/gflags.h
PATHS ${GFLAGS_ROOT_DIR})
endif()
if(MSVC)
find_package(gflags NO_MODULE)
set(GFLAGS_LIBRARY ${gflags_LIBRARIES})
else()
find_library(GFLAGS_LIBRARY gflags)
endif()
find_package_handle_standard_args(
gflags DEFAULT_MSG GFLAGS_INCLUDE_DIR GFLAGS_LIBRARY)
if(GFLAGS_FOUND)
message(
STATUS
"Found gflags (include: ${GFLAGS_INCLUDE_DIR}, "
"library: ${GFLAGS_LIBRARY})")
add_library(gflags UNKNOWN IMPORTED)
set_property(
TARGET gflags PROPERTY IMPORTED_LOCATION ${GFLAGS_LIBRARY})
set_property(
TARGET gflags PROPERTY INTERFACE_INCLUDE_DIRECTORIES
${GFLAGS_INCLUDE_DIR})
endif()
endif()
# After above, we should have the gflags target now.
if (NOT TARGET gflags)
message(WARNING
"gflags cannot be found. Depending on whether you are building Caffe2 "
"or a Caffe2 dependent library, the next warning / error will give you "
"more info.")
endif()

View File

@ -64,6 +64,7 @@ CMAKE_ARGS+=("-DANDROID_TOOLCHAIN=gcc")
# Disable unused dependencies
CMAKE_ARGS+=("-DUSE_CUDA=OFF")
CMAKE_ARGS+=("-DUSE_GFLAGS=OFF")
CMAKE_ARGS+=("-DUSE_OPENCV=OFF")
CMAKE_ARGS+=("-DUSE_LMDB=OFF")
CMAKE_ARGS+=("-DUSE_LEVELDB=OFF")

View File

@ -53,6 +53,7 @@ CMAKE_ARGS+=("-DBUILD_PYTHON=OFF")
# Disable unused dependencies
CMAKE_ARGS+=("-DUSE_CUDA=OFF")
CMAKE_ARGS+=("-DUSE_GFLAGS=OFF")
CMAKE_ARGS+=("-DUSE_OPENCV=OFF")
CMAKE_ARGS+=("-DUSE_LMDB=OFF")
CMAKE_ARGS+=("-DUSE_LEVELDB=OFF")