1 cmake_minimum_required(VERSION 3.4.0)
2 if(POLICY CMP0046)
3 cmake_policy(SET CMP0046 NEW)
4 endif()
5 if(POLICY CMP0054)
6 cmake_policy(SET CMP0054 NEW)
7 endif()
9 # ---[ Caffe project
10 project(Caffe C CXX)
12 # ---[ Caffe version
13 set(CAFFE_TARGET_VERSION "0.17.0")
14 set(CAFFE_TARGET_SOVERSION "0.17")
15 add_definitions(-DCAFFE_VERSION=${CAFFE_TARGET_VERSION})
17 # Skip `typedef __half half;`
18 add_definitions(-DCUDA_NO_HALF=1)
20 # ---[ Using cmake scripts and modules
21 list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)
23 include(ExternalProject)
25 include(cmake/Utils.cmake)
26 include(cmake/Targets.cmake)
27 include(cmake/Misc.cmake)
28 include(cmake/Summary.cmake)
29 include(cmake/ConfigGen.cmake)
31 # ---[ Options
32 caffe_option(USE_CUDNN "Build Caffe with cuDNN library support" ON)
34 # USE_NCCL: Build Caffe with NCCL Library support
35 # Regular ON/OFF option doesn't work here because we need to recognize 3 states:
36 # 1. User didn't set USE_NCCL option =>
37 # 1.1 We *quietly* try to find it and use if found; do nothing otherwise.
38 # 2. User explicitly set USE_NCCL=ON option =>
39 # 1.1 We try to find it with *required* option, thus CMake fails if not found.
40 # 3. User explicitly set USE_NCCL=OFF option => we do nothing.
41 set(USE_NCCL "NONE" CACHE STRING "Link Caffe with NCCL Library for Multi-GPU support")
42 if(USE_NCCL STREQUAL "NONE")
43 set(USE_NCCL_SET OFF)
44 else()
45 set(USE_NCCL_SET ON)
46 endif()
48 caffe_option(BUILD_SHARED_LIBS "Build shared libraries" ON)
49 caffe_option(BUILD_python "Build Python wrapper" ON)
50 set(python_version "2" CACHE STRING "Specify which Python version to use")
51 caffe_option(BUILD_matlab "Build Matlab wrapper" OFF IF UNIX OR APPLE)
52 caffe_option(BUILD_docs "Build documentation" ON IF UNIX OR APPLE)
53 caffe_option(BUILD_python_layer "Build the Caffe Python layer" ON)
54 caffe_option(USE_LEVELDB "Build with levelDB" ON)
55 caffe_option(USE_LMDB "Build with lmdb" ON)
56 caffe_option(TEST_FP16 "Build Caffe Tests with 16 bit mode included" OFF)
57 caffe_option(NO_NVML "Build Caffe Tests without NVML (i.e. no CPU affinity)" OFF)
59 if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^aarch")
60 # Tegra
61 set(NO_NVML ON)
62 endif()
64 # ---[ Dependencies
65 include(cmake/Dependencies.cmake)
67 # ---[ Flags
68 if(UNIX OR APPLE)
69 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -std=c++11")
70 endif()
72 caffe_set_caffe_link()
74 if(USE_libstdcpp)
75 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libstdc++")
76 message("-- Warning: forcing libstdc++ (controlled by USE_libstdcpp option in cmake)")
77 endif()
79 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG")
81 add_definitions(-DGTEST_USE_OWN_TR1_TUPLE)
83 # ---[ Warnings
84 caffe_warnings_disable(CMAKE_CXX_FLAGS -Wno-sign-compare -Wno-uninitialized)
86 # ---[ Config generation
87 configure_file(cmake/Templates/caffe_config.h.in "${PROJECT_BINARY_DIR}/caffe_config.h")
89 # ---[ Includes
90 set(Caffe_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
91 set(THIRDPARTY_DIR ${PROJECT_SOURCE_DIR}/3rdparty)
92 include_directories(${Caffe_INCLUDE_DIR} ${PROJECT_BINARY_DIR} ${THIRDPARTY_DIR})
93 include_directories(BEFORE src) # This is needed for gtest.
95 # ---[ Subdirectories
96 add_subdirectory(src/gtest)
97 add_subdirectory(src/caffe)
98 add_subdirectory(tools)
99 add_subdirectory(examples)
100 add_subdirectory(python)
101 add_subdirectory(matlab)
102 add_subdirectory(docs)
104 # ---[ Linter target
105 add_custom_target(lint COMMAND ${CMAKE_COMMAND} -P ${PROJECT_SOURCE_DIR}/cmake/lint.cmake)
107 # ---[ pytest target
108 if(BUILD_python)
109 add_custom_target(pytest COMMAND python${python_version} -m unittest discover -s caffe/test WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/python )
110 add_dependencies(pytest pycaffe)
111 endif()
113 # ---[ Configuration summary
114 caffe_print_configuration_summary()
116 # ---[ Export configs generation
117 caffe_generate_export_configs()