CMake changes to make Caffe2 more friendly for dependent libraries

Summary:
This introduces a few things:

- It enables us to create Caffe2Config.cmake that can be used down the road for building dependent libraries, so they do not need to explicitly write FindCaffe2.cmake.
- The config file will automatically figure out transitive dependency of Caffe2 as well as compiler flags.
- This diff also disables the RPATH setting since it is kind of a mess right now. In principle, we should figure out a clearer rpath setting following the typical rpath setting choices (https://cmake.org/Wiki/CMake_RPATH_handling) - I can send a follow up PR to clean this up.
- Minor: removed old gflags ang glog files.
Closes https://github.com/caffe2/caffe2/pull/1354

Reviewed By: dzhulgakov

Differential Revision: D6098014

Pulled By: Yangqing

fbshipit-source-id: cb06c41a7ef60fddb78b24887b6b3e82684b7c6b
This commit is contained in:
Yangqing Jia 2017-10-19 09:55:56 -07:00 committed by Facebook Github Bot
parent 8d8cebd6be
commit a1518b7801
9 changed files with 130 additions and 54 deletions

View file

@ -7,8 +7,7 @@ set(CAFFE2_VERSION_MAJOR 0)
set(CAFFE2_VERSION_MINOR 8)
set(CAFFE2_VERSION_PATCH 1)
set(CAFFE2_VERSION
${CAFFE2_VERSION_MAJOR}.${CAFFE2_VERSION_MINOR}.${CAFFE2_VERSION_PATCH})
"${CAFFE2_VERSION_MAJOR}.${CAFFE2_VERSION_MINOR}.${CAFFE2_VERSION_PATCH}")
# ---[ Do not search cmake install prefix. See the following link for details:
# https://github.com/caffe2/caffe2/commit/251d2d0c521c781276eea3748d92fc92e5dbceab
@ -52,8 +51,6 @@ option(USE_ZMQ "Use ZMQ" OFF)
# ---[ CMake scripts + modules
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_MACOSX_RPATH 1)
enable_testing()
@ -130,6 +127,8 @@ add_subdirectory(caffe2)
caffe2_print_configuration_summary()
# ---[ CMake related files
# Uninistall option.
if(NOT TARGET uninstall)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in
@ -140,3 +139,44 @@ if(NOT TARGET uninstall)
COMMAND ${CMAKE_COMMAND} -P
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
endif()
# ---[ Make configuration files for cmake to allow dependent libraries
# easier access to Caffe2.
# Specific interface link libraries for caffe2
if ((NOT USE_GLOG) OR (NOT USE_GFLAGS) OR CAFFE2_USE_CUSTOM_PROTOBUF)
message(WARNING
"Generated cmake files are only fully available if one builds "
"with system glog, gflags and protobuf.")
else()
# Since one would use glog, gflags and protobuf functionalities, any
# dependent library will have to depend on glog, gflags and protobuf
# as well, so we mark them as INTERFACE transitive dependency.
target_link_libraries(caffe2 INTERFACE glog gflags protobuf)
if (USE_CUDA)
target_link_libraries(caffe2_gpu INTERFACE glog gflags protobuf)
endif()
endif()
# Note(jiayq): Android cmake has an unresolved issue right now
# (install(EXPORT "Caffe2Targets" ...) includes target "caffe2" which
# requires target "cpufeatures" that is not in the export set.)
# and as a result we disable it for now.
if (NOT ANDROID)
configure_file(
${PROJECT_SOURCE_DIR}/cmake/Caffe2ConfigVersion.cmake.in
${PROJECT_BINARY_DIR}/Caffe2ConfigVersion.cmake
@ONLY)
configure_file(
${PROJECT_SOURCE_DIR}/cmake/Caffe2Config.cmake.in
${PROJECT_BINARY_DIR}/Caffe2Config.cmake
@ONLY)
install(FILES
${PROJECT_BINARY_DIR}/Caffe2ConfigVersion.cmake
${PROJECT_BINARY_DIR}/Caffe2Config.cmake
DESTINATION share/cmake/Caffe2
COMPONENT dev)
install(EXPORT Caffe2Targets DESTINATION share/cmake/Caffe2
FILE Caffe2Targets.cmake
COMPONENT dev)
endif()

View file

@ -98,8 +98,10 @@ set(Caffe2_MAIN_LIBS)
# Compile exposed libraries.
add_library(caffe2 ${Caffe2_CPU_SRCS} $<TARGET_OBJECTS:Caffe_PROTO> $<TARGET_OBJECTS:Caffe2_PROTO>)
target_link_libraries(caffe2 ${Caffe2_DEPENDENCY_LIBS})
install(TARGETS caffe2 DESTINATION lib)
target_link_libraries(caffe2 PRIVATE ${Caffe2_DEPENDENCY_LIBS})
target_include_directories(caffe2 INTERFACE $<INSTALL_INTERFACE:include>)
target_compile_options(caffe2 INTERFACE "-std=c++11")
install(TARGETS caffe2 EXPORT Caffe2Targets DESTINATION lib)
link_directories(${CMAKE_INSTALL_PREFIX}/lib)
caffe_add_linker_flag(caffe2 Caffe2_CPU_LINK)
list(APPEND Caffe2_MAIN_LIBS_ORDER caffe2 Caffe2_PROTO)
@ -110,28 +112,42 @@ endif()
# ---[ CUDA library.
if(USE_CUDA)
# A hack to deal with cuda library dependencies and modern CMake: the
# CUDA_ADD_LIBRARY includes a target_link_libraries, and as a result,
# one cannot use PUBLIC/PRIVATE/INTERFACE for the target anymore. This
# hack adds the PRIVATE keywords to CUDA_LIBRARIES so we can deal with
# it.
set(__tmp ${CUDA_LIBRARIES})
set(CUDA_LIBRARIES PRIVATE ${CUDA_LIBRARIES})
CUDA_ADD_LIBRARY(caffe2_gpu ${Caffe2_GPU_SRCS})
set(CUDA_LIBRARIES ${__tmp})
target_include_directories(
caffe2_gpu INTERFACE $<INSTALL_INTERFACE:include>)
target_compile_options(caffe2 INTERFACE "-std=c++11")
list(APPEND Caffe2_MAIN_LIBS_ORDER caffe2_gpu)
add_dependencies(caffe2_gpu Caffe2_PROTO)
if (BUILD_SHARED_LIBS)
target_link_libraries(
caffe2_gpu
PRIVATE
caffe2
${Caffe2_DEPENDENCY_LIBS}
${Caffe2_CUDA_DEPENDENCY_LIBS})
else()
target_link_libraries(
caffe2_gpu
PRIVATE
${Caffe2_DEPENDENCY_LIBS}
${Caffe2_CUDA_DEPENDENCY_LIBS})
endif()
caffe_add_linker_flag(caffe2_gpu Caffe2_GPU_LINK)
list(APPEND Caffe2_MAIN_LIBS_ORDER caffe2_gpu)
list(APPEND Caffe2_MAIN_LIBS ${Caffe2_GPU_LINK})
list(APPEND Caffe2_MAIN_LIBS_ORDER caffe2_gpu)
if (Caffe2_EXTERNAL_DEPENDENCIES)
add_dependencies(caffe2_gpu ${Caffe2_EXTERNAL_DEPENDENCIES})
endif()
install(TARGETS caffe2_gpu DESTINATION lib)
install(TARGETS caffe2_gpu EXPORT Caffe2Targets DESTINATION lib)
endif()
# ---[ Test binaries.

View file

@ -11,6 +11,7 @@ caffe2_binary_target("split_db.cc")
if (USE_CUDA)
caffe2_binary_target("inspect_gpus.cc")
target_link_libraries(inspect_gpus ${CUDA_LIBRARIES})
caffe2_binary_target("print_core_object_sizes.cc")
endif()

View file

@ -0,0 +1,29 @@
# - Config file for the Caffe2 package
# It defines the following variable(s)
# CAFFE2_INCLUDE_DIRS - include directories for FooBar
# as well as Caffe2 targets for other cmake libraries to use.
# library version information
set(CAFFE2_VERSION_MAJOR @CAFFE2_VERSION_MAJOR@)
set(CAFFE2_VERSION_MINOR @CAFFE2_VERSION_MINOR@)
set(CAFFE2_VERSION_PATCH @CAFFE2_VERSION_PATCH@)
set(CAFFE2_VERSION "@CAFFE2_VERSION@")
# import targets
include ("${CMAKE_CURRENT_LIST_DIR}/Caffe2Targets.cmake")
# include directory.
#
# Newer versions of CMake set the INTERFACE_INCLUDE_DIRECTORIES property
# of the imported targets. It is hence not necessary to add this path
# manually to the include search path for targets which link to gflags.
# The following lines are here for backward compatibility, in case one
# would like to use the old-style include path.
get_filename_component(
CMAKE_CURRENT_LIST_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
# Note: the current list dir is _INSTALL_PREFIX/share/cmake/Gloo.
get_filename_component(
_INSTALL_PREFIX "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)
set(CAFFE2_INCLUDE_DIRS "${_INSTALL_PREFIX}/include")

View file

@ -0,0 +1,11 @@
set(PACKAGE_VERSION "@CAFFE2_VERSION@")
# Check whether the requested PACKAGE_FIND_VERSION is compatible
if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}")
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
set(PACKAGE_VERSION_COMPATIBLE TRUE)
if ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}")
set(PACKAGE_VERSION_EXACT TRUE)
endif()
endif()

View file

@ -80,32 +80,38 @@ if(USE_NNPACK)
endif()
endif()
# ---[ Google-glog
if(USE_GLOG)
include("cmake/External/glog.cmake")
if(GLOG_FOUND)
set(CAFFE2_USE_GOOGLE_GLOG 1)
caffe2_include_directories(${GLOG_INCLUDE_DIRS})
list(APPEND Caffe2_DEPENDENCY_LIBS ${GLOG_LIBRARIES})
else()
message(WARNING "Not compiling with glog. Suppress this warning with -DUSE_GLOG=OFF")
set(USE_GLOG OFF)
endif()
endif()
# ---[ Google-gflags
# ---[ gflags
if(USE_GFLAGS)
include("cmake/External/gflags.cmake")
find_package(GFlags)
if(GFLAGS_FOUND)
set(CAFFE2_USE_GFLAGS 1)
caffe2_include_directories(${GFLAGS_INCLUDE_DIRS})
list(APPEND Caffe2_DEPENDENCY_LIBS ${GFLAGS_LIBRARIES})
else()
message(WARNING "Not compiling with gflags. Suppress this warning with -DUSE_GFLAGS=OFF")
message(WARNING
"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()
endif()
# ---[ Google-glog
if(USE_GLOG)
find_package(Glog)
if(GLOG_FOUND)
set(CAFFE2_USE_GOOGLE_GLOG 1)
caffe2_include_directories(${GLOG_INCLUDE_DIRS})
list(APPEND Caffe2_DEPENDENCY_LIBS ${GLOG_LIBRARIES})
else()
message(WARNING
"glog is not found. Caffe2 will build without glog support but it is "
"strongly recommended that you install glog. Suppress this warning "
"with -DUSE_GLOG=OFF")
set(USE_GLOG OFF)
endif()
endif()
# ---[ Googletest and benchmark
if(BUILD_TEST)
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/googletest)
@ -219,10 +225,12 @@ endif()
# ---[ EIGEN
set(EIGEN_MPL2_ONLY 1)
find_package(Eigen3 QUIET)
find_package(Eigen3)
if(EIGEN3_FOUND)
message(STATUS "Found system Eigen at " ${EIGEN3_INCLUDE_DIRS})
caffe2_include_directories(${EIGEN3_INCLUDE_DIRS})
else()
message(STATUS "Did not find system Eigen. Using third party subdirectory.")
caffe2_include_directories(${PROJECT_SOURCE_DIR}/third_party/eigen)
endif()

View file

@ -1,13 +0,0 @@
if (NOT __GFLAGS_INCLUDED) # guard against multiple includes
set(__GFLAGS_INCLUDED TRUE)
# try the system-wide glog first
find_package(GFlags)
if (GFLAGS_FOUND)
# Great, we will use gflags.
message(STATUS "Found system gflags install.")
else()
message(WARNING "gflags is not found. Caffe2 will build without gflags support but it is strongly recommended that you install gflags.")
endif()
endif()

View file

@ -1,17 +0,0 @@
# glog depends on gflags
include("cmake/External/gflags.cmake")
if (NOT __GLOG_INCLUDED)
set(__GLOG_INCLUDED TRUE)
# try the system-wide glog first
find_package(Glog)
if (GLOG_FOUND)
# Great, we will use glog.
message(STATUS "Found system glog install.")
else()
message(WARNING "glog is not found. Caffe2 will build without glog support but it is strongly recommended that you install glog.")
endif()
endif()

View file

@ -1,6 +1,7 @@
# Finds Google Protocol Buffers library and compilers and extends
# the standard cmake script with version and python generation support
function(custom_protobuf_find)
set(CAFFE2_USE_CUSTOM_PROTOBUF ON PARENT_SCOPE)
message(STATUS "Use custom protobuf build.")
# For a custom protobuf build, we will always use static protobuf.
option(protobuf_BUILD_SHARED_LIBS "" OFF)