Skip to content

Commit

Permalink
added export config generation for build and install code trees
Browse files Browse the repository at this point in the history
  • Loading branch information
Anatoly Baksheev committed Jan 2, 2015
1 parent f7d774e commit 31fcb38
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ include(cmake/Utils.cmake)
include(cmake/Targets.cmake)
include(cmake/Misc.cmake)
include(cmake/Summary.cmake)
include(cmake/ConfigGen.cmake)

# ---[ Options
caffe_option(CPU_ONLY "Build Caffe wihtout CUDA support" OFF) # TODO: rename to USE_CUDA
Expand Down Expand Up @@ -51,3 +52,6 @@ add_custom_target(lint COMMAND ${CMAKE_COMMAND} -P ${CMAKE_SOURCE_DIR}/cmake/lin

# ---[ Configuration summary
caffe_print_configuration_summary()

# ---[ Export configs generation
caffe_generate_export_configs()
86 changes: 86 additions & 0 deletions cmake/ConfigGen.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@

################################################################################################
# Helper function to fetch caffe includes which will be passed to dependent projects
# Usage:
# caffe_get_current_includes(<includes_list_variable>)
function(caffe_get_current_includes includes_variable)
get_property(current_includes DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
caffe_convert_absolute_paths(current_includes)

# remove at most one ${CMAKE_BINARY_DIR} include added for caffe_config.h
list(FIND current_includes ${CMAKE_BINARY_DIR} __index)
list(REMOVE_AT current_includes ${__index})

caffe_list_unique(current_includes)
set(${includes_variable} ${current_includes} PARENT_SCOPE)
endfunction()

################################################################################################
# Helper function to get all list items that begin with given prefix
# Usage:
# caffe_get_items_with_prefix(<prefix> <list_variable> <output_variable>)
function(caffe_get_items_with_prefix prefix list_variable output_variable)
set(__result "")
foreach(__e ${${list_variable}})
if(__e MATCHES "^${prefix}.*")
list(APPEND __result ${__e})
endif()
endforeach()
set(${output_variable} ${__result} PARENT_SCOPE)
endfunction()

################################################################################################
# Function for generation Caffe build- and install- tree export config files
# Usage:
# caffe_generate_export_configs()
function(caffe_generate_export_configs)
set(install_cmake_suffix "share/caffe")

# ---[ Configure build-tree CaffeConfig.cmake file ]---
caffe_get_current_includes(Caffe_INCLUDE_DIRS)
if(NOT HAVE_CUDA)
set(HAVE_CUDA FALSE)
set(Caffe_DEFINITIONS -DCPU_ONLY)
endif()
if(NOT HAVE_CUDNN)
set(HAVE_CUDNN FALSE)
else()
set(Caffe_DEFINITIONS -DUSE_CUDNN)
endif()

configure_file("cmake/Templates/CaffeConfig.cmake.in" "${CMAKE_BINARY_DIR}/CaffeConfig.cmake" @ONLY)

# Add targets to the build-tree export set
export(TARGETS caffe proto FILE "${CMAKE_BINARY_DIR}/CaffeTargets.cmake")
export(PACKAGE Caffe)

# ---[ Configure install-tree CaffeConfig.cmake file ]---

# remove source and build dir includes
caffe_get_items_with_prefix(${CMAKE_SOURCE_DIR} Caffe_INCLUDE_DIRS __insource)
caffe_get_items_with_prefix(${CMAKE_BINARY_DIR} Caffe_INCLUDE_DIRS __inbinary)
list(REMOVE_ITEM Caffe_INCLUDE_DIRS ${__insource} ${__inbinary})

# add `install` include folder
set(lines "")
list(APPEND lines "get_filename_component(__caffe_include \"\${Caffe_CMAKE_DIR}/../../include\" ABSOLUTE)\n")
list(APPEND lines "list(APPEND Caffe_INCLUDE_DIRS \${__caffe_include})\n")
list(APPEND lines "unset(__caffe_include)\n")
string(REPLACE ";" "" Caffe_INSTALL_INCLUDE_DIR_APPEND_COMMAND ${lines})

configure_file("cmake/Templates/CaffeConfig.cmake.in" "${CMAKE_BINARY_DIR}/cmake/CaffeConfig.cmake" @ONLY)

# Install the CaffeConfig.cmake and export set to use wuth install-tree
install(FILES "${CMAKE_BINARY_DIR}/cmake/CaffeConfig.cmake" DESTINATION ${install_cmake_suffix})
install(EXPORT CaffeTargets DESTINATION ${install_cmake_suffix})

# ---[ Configure and install version file ]---

# TODO: Lines below are commented because Caffe does't declare its version in headers.
# When the declarations are added, modify `caffe_extract_caffe_version()` macro and uncomment

# configure_file(cmake/Templates/CaffeConfigVersion.cmake.in "${CMAKE_BINARY_DIR}/CaffeConfigVersion.cmake" @ONLY)
# install(FILES "${CMAKE_BINARY_DIR}/CaffeConfigVersion.cmake" DESTINATION ${install_cmake_suffix})
endfunction()


2 changes: 0 additions & 2 deletions cmake/Cuda.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,6 @@ list(APPEND Caffe_LINKER_LIBS ${CUDA_CUDART_LIBRARY}
${CUDA_curand_LIBRARY} ${CUDA_CUBLAS_LIBRARIES})

# cudnn detection
set(HAVE_CUDNN FALSE)

if(USE_CUDNN)
detect_cuDNN()
if(HAVE_CUDNN)
Expand Down
58 changes: 58 additions & 0 deletions cmake/Templates/CaffeConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Config file for the Caffe package.
#
# Note:
# Caffe and this config file depends on opencv,
# so put `find_package(OpenCV)` before searching Caffe
# via `find_package(Caffe)`. All other lib/includes
# dependencies are hard coded int the file
#
# After successful configuration the following variables
# will be defined:
#
# Caffe_INCLUDE_DIRS - Caffe include directories
# Caffe_LIBRARIES - libraries to link against
# Caffe_DEFINITIONS - a list of definitions to pass to compiler
#
# Caffe_HAVE_CUDA - signals about CUDA support
# Caffe_HAVE_CUDNN - signals about cuDNN support


# OpenCV dependency

if(NOT OpenCV_FOUND)
set(Caffe_OpenCV_CONFIG_PATH "@OpenCV_CONFIG_PATH@")
if(Caffe_OpenCV_CONFIG_PATH)
get_filename_component(Caffe_OpenCV_CONFIG_PATH ${Caffe_OpenCV_CONFIG_PATH} ABSOLUTE)

if(EXISTS ${Caffe_OpenCV_CONFIG_PATH} AND NOT TARGET opencv_core)
message(STATUS "Caffe: using OpenCV config from ${Caffe_OpenCV_CONFIG_PATH}")
include(${Caffe_OpenCV_CONFIG_PATH}/OpenCVModules.cmake)
endif()

else()
find_package(OpenCV REQUIRED)
endif()
unset(Caffe_OpenCV_CONFIG_PATH)
endif()

# Compute paths
get_filename_component(Caffe_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
set(Caffe_INCLUDE_DIRS "@Caffe_INCLUDE_DIRS@")

@Caffe_INSTALL_INCLUDE_DIR_APPEND_COMMAND@

# Our library dependencies
if(NOT TARGET caffe AND NOT caffe_BINARY_DIR)
include("${Caffe_CMAKE_DIR}/CaffeTargets.cmake")
endif()

# List of IMPORTED libs created by CaffeTargets.cmake
set(Caffe_LIBRARIES caffe)

# Definitions
set(Caffe_DEFINITIONS "@Caffe_DEFINITIONS@")

# Cuda support variables
set(Caffe_CPU_ONLY @CPU_ONLY@)
set(Caffe_HAVE_CUDA @HAVE_CUDA@)
set(Caffe_HAVE_CUDNN @HAVE_CUDNN@)
11 changes: 11 additions & 0 deletions cmake/Templates/CaffeConfigVersion.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
set(PACKAGE_VERSION "@Caffe_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()

0 comments on commit 31fcb38

Please sign in to comment.