|
| 1 | +cmake_minimum_required(VERSION 2.8.7) |
| 2 | +if(POLICY CMP0046) |
| 3 | + cmake_policy(SET CMP0046 NEW) |
| 4 | +endif() |
| 5 | +if(POLICY CMP0054) |
| 6 | + cmake_policy(SET CMP0054 NEW) |
| 7 | +endif() |
| 8 | + |
| 9 | +# ---[ Caffe project |
| 10 | +project(Caffe C CXX) |
| 11 | + |
| 12 | +# ---[ Caffe version |
| 13 | +set(CAFFE_TARGET_VERSION "1.0.0" CACHE STRING "Caffe logical version") |
| 14 | +set(CAFFE_TARGET_SOVERSION "1.0.0" CACHE STRING "Caffe soname version") |
| 15 | +add_definitions(-DCAFFE_VERSION=${CAFFE_TARGET_VERSION}) |
| 16 | + |
| 17 | +# ---[ Using cmake scripts and modules |
| 18 | +list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules) |
| 19 | + |
| 20 | +include(ExternalProject) |
| 21 | +include(GNUInstallDirs) |
| 22 | + |
| 23 | +include(cmake/Utils.cmake) |
| 24 | +include(cmake/Targets.cmake) |
| 25 | +include(cmake/Misc.cmake) |
| 26 | +include(cmake/Summary.cmake) |
| 27 | +include(cmake/ConfigGen.cmake) |
| 28 | + |
| 29 | +# ---[ Options |
| 30 | +caffe_option(CPU_ONLY "Build Caffe without CUDA support" OFF) # TODO: rename to USE_CUDA |
| 31 | +caffe_option(USE_CUDNN "Build Caffe with cuDNN library support" ON IF NOT CPU_ONLY) |
| 32 | +caffe_option(USE_NCCL "Build Caffe with NCCL library support" OFF) |
| 33 | +caffe_option(BUILD_SHARED_LIBS "Build shared libraries" ON) |
| 34 | +caffe_option(BUILD_python "Build Python wrapper" ON) |
| 35 | +set(python_version "2" CACHE STRING "Specify which Python version to use") |
| 36 | +caffe_option(BUILD_matlab "Build Matlab wrapper" OFF IF UNIX OR APPLE) |
| 37 | +caffe_option(BUILD_docs "Build documentation" ON IF UNIX OR APPLE) |
| 38 | +caffe_option(BUILD_python_layer "Build the Caffe Python layer" ON) |
| 39 | +caffe_option(USE_OPENCV "Build with OpenCV support" ON) |
| 40 | +caffe_option(USE_LEVELDB "Build with levelDB" ON) |
| 41 | +caffe_option(USE_LMDB "Build with lmdb" ON) |
| 42 | +caffe_option(ALLOW_LMDB_NOLOCK "Allow MDB_NOLOCK when reading LMDB files (only if necessary)" OFF) |
| 43 | +caffe_option(USE_OPENMP "Link with OpenMP (when your BLAS wants OpenMP and you get linker errors)" OFF) |
| 44 | + |
| 45 | +# ---[ Dependencies |
| 46 | +include(cmake/Dependencies.cmake) |
| 47 | + |
| 48 | +# ---[ Flags |
| 49 | +if(UNIX OR APPLE) |
| 50 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall") |
| 51 | +endif() |
| 52 | + |
| 53 | +caffe_set_caffe_link() |
| 54 | + |
| 55 | +if(USE_libstdcpp) |
| 56 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libstdc++") |
| 57 | + message("-- Warning: forcing libstdc++ (controlled by USE_libstdcpp option in cmake)") |
| 58 | +endif() |
| 59 | + |
| 60 | +# ---[ Warnings |
| 61 | +caffe_warnings_disable(CMAKE_CXX_FLAGS -Wno-sign-compare -Wno-uninitialized) |
| 62 | + |
| 63 | +# ---[ Config generation |
| 64 | +configure_file(cmake/Templates/caffe_config.h.in "${PROJECT_BINARY_DIR}/caffe_config.h") |
| 65 | + |
| 66 | +# ---[ Includes |
| 67 | +set(Caffe_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include) |
| 68 | +set(Caffe_SRC_DIR ${PROJECT_SOURCE_DIR}/src) |
| 69 | +include_directories(${PROJECT_BINARY_DIR}) |
| 70 | + |
| 71 | +# ---[ Includes & defines for CUDA |
| 72 | + |
| 73 | +# cuda_compile() does not have per-call dependencies or include pathes |
| 74 | +# (cuda_compile() has per-call flags, but we set them here too for clarity) |
| 75 | +# |
| 76 | +# list(REMOVE_ITEM ...) invocations remove PRIVATE and PUBLIC keywords from collected definitions and include pathes |
| 77 | +if(HAVE_CUDA) |
| 78 | + # pass include pathes to cuda_include_directories() |
| 79 | + set(Caffe_ALL_INCLUDE_DIRS ${Caffe_INCLUDE_DIRS}) |
| 80 | + list(REMOVE_ITEM Caffe_ALL_INCLUDE_DIRS PRIVATE PUBLIC) |
| 81 | + cuda_include_directories(${Caffe_INCLUDE_DIR} ${Caffe_SRC_DIR} ${Caffe_ALL_INCLUDE_DIRS}) |
| 82 | + |
| 83 | + # add definitions to nvcc flags directly |
| 84 | + set(Caffe_ALL_DEFINITIONS ${Caffe_DEFINITIONS}) |
| 85 | + list(REMOVE_ITEM Caffe_ALL_DEFINITIONS PRIVATE PUBLIC) |
| 86 | + list(APPEND CUDA_NVCC_FLAGS ${Caffe_ALL_DEFINITIONS}) |
| 87 | +endif() |
| 88 | + |
| 89 | +# ---[ Subdirectories |
| 90 | +add_subdirectory(src/gtest) |
| 91 | +add_subdirectory(src/caffe) |
| 92 | +add_subdirectory(tools) |
| 93 | +add_subdirectory(examples) |
| 94 | +add_subdirectory(python) |
| 95 | +add_subdirectory(matlab) |
| 96 | +add_subdirectory(docs) |
| 97 | + |
| 98 | +# ---[ Linter target |
| 99 | +add_custom_target(lint COMMAND ${CMAKE_COMMAND} -P ${PROJECT_SOURCE_DIR}/cmake/lint.cmake) |
| 100 | + |
| 101 | +# ---[ pytest target |
| 102 | +if(BUILD_python) |
| 103 | + add_custom_target(pytest COMMAND python${python_version} -m unittest discover -s caffe/test WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/python ) |
| 104 | + add_dependencies(pytest pycaffe) |
| 105 | +endif() |
| 106 | + |
| 107 | +# ---[ uninstall target |
| 108 | +configure_file( |
| 109 | + ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Uninstall.cmake.in |
| 110 | + ${CMAKE_CURRENT_BINARY_DIR}/cmake/Uninstall.cmake |
| 111 | + IMMEDIATE @ONLY) |
| 112 | + |
| 113 | +add_custom_target(uninstall |
| 114 | + COMMAND ${CMAKE_COMMAND} -P |
| 115 | + ${CMAKE_CURRENT_BINARY_DIR}/cmake/Uninstall.cmake) |
| 116 | + |
| 117 | +# ---[ Configuration summary |
| 118 | +caffe_print_configuration_summary() |
| 119 | + |
| 120 | +# ---[ Export configs generation |
| 121 | +caffe_generate_export_configs() |
| 122 | + |
0 commit comments