1 PROJECT := caffe
3 CONFIG_FILE := Makefile.config
4 # Explicitly check for the config file, otherwise make -k will proceed anyway.
5 ifeq ($(wildcard $(CONFIG_FILE)),)
6 $(error $(CONFIG_FILE) not found. See $(CONFIG_FILE).example.)
7 endif
8 include $(CONFIG_FILE)
10 BUILD_DIR_LINK := $(BUILD_DIR)
11 ifeq ($(RELEASE_BUILD_DIR),)
12 RELEASE_BUILD_DIR := .$(BUILD_DIR)_release
13 endif
14 ifeq ($(DEBUG_BUILD_DIR),)
15 DEBUG_BUILD_DIR := .$(BUILD_DIR)_debug
16 endif
18 DEBUG ?= 0
19 ifeq ($(DEBUG), 1)
20 BUILD_DIR := $(DEBUG_BUILD_DIR)
21 OTHER_BUILD_DIR := $(RELEASE_BUILD_DIR)
22 else
23 BUILD_DIR := $(RELEASE_BUILD_DIR)
24 OTHER_BUILD_DIR := $(DEBUG_BUILD_DIR)
25 endif
27 THIRDPARTY_DIR := ./3rdparty
29 # All of the directories containing code.
30 SRC_DIRS := $(shell find * -type d -exec bash -c "find {} -maxdepth 1 \
31 \( -name '*.cpp' -o -name '*.proto' \) | grep -q ." \; -print)
33 # The target shared library name
34 LIBRARY_NAME := $(PROJECT)$(LIBRARY_NAME_SUFFIX)
35 LIB_BUILD_DIR := $(BUILD_DIR)/lib
36 STATIC_NAME := $(LIB_BUILD_DIR)/lib$(LIBRARY_NAME).a
37 DYNAMIC_VERSION_MAJOR := 0
38 DYNAMIC_VERSION_MINOR := 16
39 DYNAMIC_VERSION_REVISION := 2
40 DYNAMIC_NAME_SHORT := lib$(LIBRARY_NAME).so
41 DYNAMIC_SONAME_SHORT := $(DYNAMIC_NAME_SHORT).$(DYNAMIC_VERSION_MAJOR).$(DYNAMIC_VERSION_MINOR)
42 DYNAMIC_VERSIONED_NAME_SHORT := $(DYNAMIC_SONAME_SHORT).$(DYNAMIC_VERSION_REVISION)
43 DYNAMIC_NAME := $(LIB_BUILD_DIR)/$(DYNAMIC_VERSIONED_NAME_SHORT)
44 COMMON_FLAGS += -DCAFFE_VERSION=$(DYNAMIC_VERSION_MAJOR).$(DYNAMIC_VERSION_MINOR).$(DYNAMIC_VERSION_REVISION)
45 # NVCaffe requires C++ 11
46 COMMON_FLAGS += -std=c++11
47 COMMON_FLAGS += -DCUDA_NO_HALF
49 ##############################
50 # Get all source files
51 ##############################
52 # CXX_SRCS are the source files excluding the test ones.
53 CXX_SRCS := $(shell find src/$(PROJECT) ! -name "test_*.cpp" -name "*.cpp")
54 # CU_SRCS are the cuda source files
55 CU_SRCS := $(shell find src/$(PROJECT) ! -name "test_*.cu" -name "*.cu")
56 # TEST_SRCS are the test source files
57 TEST_MAIN_SRC := src/$(PROJECT)/test/test_caffe_main.cpp
58 TEST_SRCS := $(shell find src/$(PROJECT) -name "test_*.cpp")
59 TEST_SRCS := $(filter-out $(TEST_MAIN_SRC), $(TEST_SRCS))
60 TEST_CU_SRCS := $(shell find src/$(PROJECT) -name "test_*.cu")
61 GTEST_SRC := src/gtest/gtest-all.cpp
62 # TOOL_SRCS are the source files for the tool binaries
63 TOOL_SRCS := $(shell find tools -name "*.cpp")
64 # EXAMPLE_SRCS are the source files for the example binaries
65 EXAMPLE_SRCS := $(shell find examples -name "*.cpp")
66 # BUILD_INCLUDE_DIR contains any generated header files we want to include.
67 BUILD_INCLUDE_DIR := $(BUILD_DIR)/src
68 # PROTO_SRCS are the protocol buffer definitions
69 PROTO_SRC_DIR := src/$(PROJECT)/proto
70 PROTO_SRCS := $(wildcard $(PROTO_SRC_DIR)/*.proto)
71 # PROTO_BUILD_DIR will contain the .cc and obj files generated from
72 # PROTO_SRCS; PROTO_BUILD_INCLUDE_DIR will contain the .h header files
73 PROTO_BUILD_DIR := $(BUILD_DIR)/$(PROTO_SRC_DIR)
74 PROTO_BUILD_INCLUDE_DIR := $(BUILD_INCLUDE_DIR)/$(PROJECT)/proto
75 # NONGEN_CXX_SRCS includes all source/header files except those generated
76 # automatically (e.g., by proto).
77 NONGEN_CXX_SRCS := $(shell find \
78 src/$(PROJECT) \
79 include/$(PROJECT) \
80 python/$(PROJECT) \
81 matlab/+$(PROJECT)/private \
82 examples \
83 tools \
84 -name "*.cpp" -or -name "*.hpp" -or -name "*.cu" -or -name "*.cuh")
85 LINT_SCRIPT := scripts/cpp_lint.py
86 LINT_OUTPUT_DIR := $(BUILD_DIR)/.lint
87 LINT_EXT := lint.txt
88 LINT_OUTPUTS := $(addsuffix .$(LINT_EXT), $(addprefix $(LINT_OUTPUT_DIR)/, $(NONGEN_CXX_SRCS)))
89 EMPTY_LINT_REPORT := $(BUILD_DIR)/.$(LINT_EXT)
90 NONEMPTY_LINT_REPORT := $(BUILD_DIR)/$(LINT_EXT)
91 # PY$(PROJECT)_SRC is the python wrapper for $(PROJECT)
92 PY$(PROJECT)_SRC := python/$(PROJECT)/_$(PROJECT).cpp
93 PY$(PROJECT)_SO := python/$(PROJECT)/_$(PROJECT).so
94 PY$(PROJECT)_HXX := include/$(PROJECT)/layers/python_layer.hpp
95 # MAT$(PROJECT)_SRC is the mex entrance point of matlab package for $(PROJECT)
96 MAT$(PROJECT)_SRC := matlab/+$(PROJECT)/private/$(PROJECT)_.cpp
97 ifneq ($(MATLAB_DIR),)
98 MAT_SO_EXT := $(shell $(MATLAB_DIR)/bin/mexext)
99 endif
100 MAT$(PROJECT)_SO := matlab/+$(PROJECT)/private/$(PROJECT)_.$(MAT_SO_EXT)
102 ##############################
103 # Derive generated files
104 ##############################
105 # The generated files for protocol buffers
106 PROTO_GEN_HEADER_SRCS := $(addprefix $(PROTO_BUILD_DIR)/, \
107 $(notdir ${PROTO_SRCS:.proto=.pb.h}))
108 PROTO_GEN_HEADER := $(addprefix $(PROTO_BUILD_INCLUDE_DIR)/, \
109 $(notdir ${PROTO_SRCS:.proto=.pb.h}))
110 PROTO_GEN_CC := $(addprefix $(BUILD_DIR)/, ${PROTO_SRCS:.proto=.pb.cc})
111 PY_PROTO_BUILD_DIR := python/$(PROJECT)/proto
112 PY_PROTO_INIT := python/$(PROJECT)/proto/__init__.py
113 PROTO_GEN_PY := $(foreach file,${PROTO_SRCS:.proto=_pb2.py}, \
114 $(PY_PROTO_BUILD_DIR)/$(notdir $(file)))
115 # The objects corresponding to the source files
116 # These objects will be linked into the final shared library, so we
117 # exclude the tool, example, and test objects.
118 CXX_OBJS := $(addprefix $(BUILD_DIR)/, ${CXX_SRCS:.cpp=.o})
119 CU_OBJS := $(addprefix $(BUILD_DIR)/cuda/, ${CU_SRCS:.cu=.o})
120 PROTO_OBJS := ${PROTO_GEN_CC:.cc=.o}
121 OBJS := $(PROTO_OBJS) $(CXX_OBJS) $(CU_OBJS)
122 # tool, example, and test objects
123 TOOL_OBJS := $(addprefix $(BUILD_DIR)/, ${TOOL_SRCS:.cpp=.o})
124 TOOL_BUILD_DIR := $(BUILD_DIR)/tools
125 TEST_CXX_BUILD_DIR := $(BUILD_DIR)/src/$(PROJECT)/test
126 TEST_CU_BUILD_DIR := $(BUILD_DIR)/cuda/src/$(PROJECT)/test
127 TEST_CXX_OBJS := $(addprefix $(BUILD_DIR)/, ${TEST_SRCS:.cpp=.o})
128 TEST_CU_OBJS := $(addprefix $(BUILD_DIR)/cuda/, ${TEST_CU_SRCS:.cu=.o})
129 TEST_OBJS := $(TEST_CXX_OBJS) $(TEST_CU_OBJS)
130 GTEST_OBJ := $(addprefix $(BUILD_DIR)/, ${GTEST_SRC:.cpp=.o})
131 EXAMPLE_OBJS := $(addprefix $(BUILD_DIR)/, ${EXAMPLE_SRCS:.cpp=.o})
132 # Output files for automatic dependency generation
133 DEPS := ${CXX_OBJS:.o=.d} ${CU_OBJS:.o=.d} ${TEST_CXX_OBJS:.o=.d} \
134 ${TEST_CU_OBJS:.o=.d} $(BUILD_DIR)/${MAT$(PROJECT)_SO:.$(MAT_SO_EXT)=.d}
135 # tool, example, and test bins
136 TOOL_BINS := ${TOOL_OBJS:.o=.bin}
137 EXAMPLE_BINS := ${EXAMPLE_OBJS:.o=.bin}
138 # symlinks to tool bins without the ".bin" extension
139 TOOL_BIN_LINKS := ${TOOL_BINS:.bin=}
140 # Put the test binaries in build/test for convenience.
141 TEST_BIN_DIR := $(BUILD_DIR)/test
142 TEST_CU_BINS := $(addsuffix .testbin,$(addprefix $(TEST_BIN_DIR)/, \
143 $(foreach obj,$(TEST_CU_OBJS),$(basename $(notdir $(obj))))))
144 TEST_CXX_BINS := $(addsuffix .testbin,$(addprefix $(TEST_BIN_DIR)/, \
145 $(foreach obj,$(TEST_CXX_OBJS),$(basename $(notdir $(obj))))))
146 TEST_BINS := $(TEST_CXX_BINS) $(TEST_CU_BINS)
147 # TEST_ALL_BIN is the test binary that links caffe dynamically.
148 TEST_ALL_BIN := $(TEST_BIN_DIR)/test_all.testbin
150 ##############################
151 # Derive compiler warning dump locations
152 ##############################
153 WARNS_EXT := warnings.txt
154 CXX_WARNS := $(addprefix $(BUILD_DIR)/, ${CXX_SRCS:.cpp=.o.$(WARNS_EXT)})
155 CU_WARNS := $(addprefix $(BUILD_DIR)/cuda/, ${CU_SRCS:.cu=.o.$(WARNS_EXT)})
156 TOOL_WARNS := $(addprefix $(BUILD_DIR)/, ${TOOL_SRCS:.cpp=.o.$(WARNS_EXT)})
157 EXAMPLE_WARNS := $(addprefix $(BUILD_DIR)/, ${EXAMPLE_SRCS:.cpp=.o.$(WARNS_EXT)})
158 TEST_WARNS := $(addprefix $(BUILD_DIR)/, ${TEST_SRCS:.cpp=.o.$(WARNS_EXT)})
159 TEST_CU_WARNS := $(addprefix $(BUILD_DIR)/cuda/, ${TEST_CU_SRCS:.cu=.o.$(WARNS_EXT)})
160 ALL_CXX_WARNS := $(CXX_WARNS) $(TOOL_WARNS) $(EXAMPLE_WARNS) $(TEST_WARNS)
161 ALL_CU_WARNS := $(CU_WARNS) $(TEST_CU_WARNS)
162 ALL_WARNS := $(ALL_CXX_WARNS) $(ALL_CU_WARNS)
164 EMPTY_WARN_REPORT := $(BUILD_DIR)/.$(WARNS_EXT)
165 NONEMPTY_WARN_REPORT := $(BUILD_DIR)/$(WARNS_EXT)
167 ##############################
168 # Derive include and lib directories
169 ##############################
170 CUDA_INCLUDE_DIR := $(CUDA_DIR)/include
172 CUDA_LIB_DIR :=
173 # add <cuda>/lib64 only if it exists
174 ifneq ("$(wildcard $(CUDA_DIR)/lib64)","")
175 CUDA_LIB_DIR += $(CUDA_DIR)/lib64
176 CUDA_LIB_DIR += /usr/lib/nvidia-384 /usr/lib/nvidia-381 /usr/lib/nvidia-375 /usr/lib/nvidia-367 /usr/lib/nvidia-361 /usr/lib/nvidia-352
177 endif
178 CUDA_LIB_DIR += $(CUDA_DIR)/lib
180 INCLUDE_DIRS += $(BUILD_INCLUDE_DIR) ./src ./include $(THIRDPARTY_DIR)
181 ifneq ($(CPU_ONLY), 1)
182 INCLUDE_DIRS += $(CUDA_INCLUDE_DIR)
183 LIBRARY_DIRS += $(CUDA_LIB_DIR)
184 LIBRARIES := cudart cublas curand
185 ifneq ($(NO_NVML), 1)
186 LIBRARIES += nvidia-ml
187 endif
188 endif
190 LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
192 # handle IO dependencies
193 USE_LEVELDB ?= 1
194 USE_LMDB ?= 1
195 USE_OPENCV ?= 1
197 ifeq ($(USE_LEVELDB), 1)
198 LIBRARIES += leveldb snappy
199 endif
200 ifeq ($(USE_LMDB), 1)
201 LIBRARIES += lmdb
202 endif
203 ifeq ($(USE_OPENCV), 1)
204 LIBRARIES += opencv_core opencv_highgui opencv_imgproc
206 ifeq ($(OPENCV_VERSION), 3)
207 LIBRARIES += opencv_imgcodecs
208 endif
210 endif
211 PYTHON_LIBRARIES ?= boost_python python2.7
212 WARNINGS := -Wall -Wno-sign-compare
214 ##############################
215 # Set build directories
216 ##############################
218 DISTRIBUTE_DIR ?= distribute
219 DISTRIBUTE_SUBDIRS := $(DISTRIBUTE_DIR)/bin $(DISTRIBUTE_DIR)/lib
220 DIST_ALIASES := dist
221 ifneq ($(strip $(DISTRIBUTE_DIR)),distribute)
222 DIST_ALIASES += distribute
223 endif
225 ALL_BUILD_DIRS := $(sort $(BUILD_DIR) $(addprefix $(BUILD_DIR)/, $(SRC_DIRS)) \
226 $(addprefix $(BUILD_DIR)/cuda/, $(SRC_DIRS)) \
227 $(LIB_BUILD_DIR) $(TEST_BIN_DIR) $(PY_PROTO_BUILD_DIR) $(LINT_OUTPUT_DIR) \
228 $(DISTRIBUTE_SUBDIRS) $(PROTO_BUILD_INCLUDE_DIR))
230 ##############################
231 # Set directory for Doxygen-generated documentation
232 ##############################
233 DOXYGEN_CONFIG_FILE ?= ./.Doxyfile
234 # should be the same as OUTPUT_DIRECTORY in the .Doxyfile
235 DOXYGEN_OUTPUT_DIR ?= ./doxygen
236 DOXYGEN_COMMAND ?= doxygen
237 # All the files that might have Doxygen documentation.
238 DOXYGEN_SOURCES := $(shell find \
239 src/$(PROJECT) \
240 include/$(PROJECT) \
241 python/ \
242 matlab/ \
243 examples \
244 tools \
245 -name "*.cpp" -or -name "*.hpp" -or -name "*.cu" -or -name "*.cuh" -or \
246 -name "*.py" -or -name "*.m")
247 DOXYGEN_SOURCES += $(DOXYGEN_CONFIG_FILE)
250 ##############################
251 # Configure build
252 ##############################
254 # Determine platform
255 UNAME := $(shell uname -s)
256 ifeq ($(UNAME), Linux)
257 LINUX := 1
258 else ifeq ($(UNAME), Darwin)
259 OSX := 1
260 OSX_MAJOR_VERSION := $(shell sw_vers -productVersion | cut -f 1 -d .)
261 OSX_MINOR_VERSION := $(shell sw_vers -productVersion | cut -f 2 -d .)
262 endif
264 # Linux
265 ifeq ($(LINUX), 1)
266 CXX ?= /usr/bin/g++
267 GCCVERSION := $(shell $(CXX) -dumpversion | cut -f1,2 -d.)
268 # older versions of gcc are too dumb to build boost with -Wuninitalized
269 ifeq ($(shell echo | awk '{exit $(GCCVERSION) < 4.6;}'), 1)
270 WARNINGS += -Wno-uninitialized
271 endif
272 # boost::thread is reasonably called boost_thread (compare OS X)
273 # We will also explicitly add stdc++ to the link target.
274 LIBRARIES += boost_thread stdc++
275 VERSIONFLAGS += -Wl,-soname,$(DYNAMIC_SONAME_SHORT) -Wl,-rpath,$(ORIGIN)/../lib
276 endif
278 # OS X:
279 # clang++ instead of g++
280 # libstdc++ for NVCC compatibility on OS X >= 10.9 with CUDA < 7.0
281 ifeq ($(OSX), 1)
282 CXX := /usr/bin/clang++
283 ifneq ($(CPU_ONLY), 1)
284 CUDA_VERSION := $(shell $(CUDA_DIR)/bin/nvcc -V | grep -o 'release [0-9.]*' | grep -o '[0-9.]*')
285 ifeq ($(shell echo | awk '{exit $(CUDA_VERSION) < 7.0;}'), 1)
286 CXXFLAGS += -stdlib=libstdc++
287 LINKFLAGS += -stdlib=libstdc++
288 endif
289 # clang throws this warning for cuda headers
290 WARNINGS += -Wno-unneeded-internal-declaration
291 # 10.11 strips DYLD_* env vars so link CUDA (rpath is available on 10.5+)
292 OSX_10_OR_LATER := $(shell [ $(OSX_MAJOR_VERSION) -ge 10 ] && echo true)
293 OSX_10_5_OR_LATER := $(shell [ $(OSX_MINOR_VERSION) -ge 5 ] && echo true)
294 ifeq ($(OSX_10_OR_LATER),true)
295 ifeq ($(OSX_10_5_OR_LATER),true)
296 LDFLAGS += -Wl,-rpath,$(CUDA_LIB_DIR)
297 endif
298 endif
299 endif
300 # gtest needs to use its own tuple to not conflict with clang
301 COMMON_FLAGS += -DGTEST_USE_OWN_TR1_TUPLE=1
302 # boost::thread is called boost_thread-mt to mark multithreading on OS X
303 LIBRARIES += boost_thread-mt
304 # we need to explicitly ask for the rpath to be obeyed
305 ORIGIN := @loader_path
306 VERSIONFLAGS += -Wl,-install_name,$(DYNAMIC_SONAME_SHORT) -Wl,-rpath,$(ORIGIN)/../../build/lib
307 else
308 ORIGIN := \$$ORIGIN
309 endif
311 # Custom compiler
312 ifdef CUSTOM_CXX
313 CXX := $(CUSTOM_CXX)
314 endif
316 # Static linking
317 ifneq (,$(findstring clang++,$(CXX)))
318 STATIC_LINK_COMMAND := -Wl,-force_load $(STATIC_NAME)
319 else ifneq (,$(findstring g++,$(CXX)))
320 STATIC_LINK_COMMAND := -Wl,--whole-archive $(STATIC_NAME) -Wl,--no-whole-archive
321 else
322 # The following line must not be indented with a tab, since we are not inside a target
323 $(error Cannot static link with the $(CXX) compiler)
324 endif
326 # Debugging
327 ifeq ($(DEBUG), 1)
328 COMMON_FLAGS += -DDEBUG -g -O0
329 NVCCFLAGS += -G
330 else
331 COMMON_FLAGS += -DNDEBUG -O2
332 endif
334 # cuDNN acceleration configuration.
335 ifeq ($(USE_CUDNN), 1)
336 ifdef CUDNN_ROOT
337 CUDNN_DIR := $(CUDNN_ROOT)
338 endif
339 LIBRARIES += cudnn
340 INCLUDE_DIRS += $(CUDNN_DIR)/cuda/include $(CUDNN_DIR)/include $(CUDNN_DIR)
341 LIBRARY_DIRS += $(CUDNN_DIR)/cuda/lib64 $(CUDNN_DIR)/lib64 $(CUDNN_DIR)
342 COMMON_FLAGS += -DUSE_CUDNN
343 endif
345 # NCCL acceleration configuration
346 ifeq ($(USE_NCCL), 1)
347 LIBRARIES += nccl
348 COMMON_FLAGS += -DUSE_NCCL
349 endif
351 # configure IO libraries
352 ifeq ($(USE_OPENCV), 1)
353 COMMON_FLAGS += -DUSE_OPENCV
354 endif
355 ifeq ($(USE_LEVELDB), 1)
356 COMMON_FLAGS += -DUSE_LEVELDB
357 endif
358 ifeq ($(USE_LMDB), 1)
359 COMMON_FLAGS += -DUSE_LMDB
360 ifeq ($(ALLOW_LMDB_NOLOCK), 1)
361 COMMON_FLAGS += -DALLOW_LMDB_NOLOCK
362 endif
363 endif
365 # CPU-only configuration
366 ifeq ($(CPU_ONLY), 1)
367 OBJS := $(PROTO_OBJS) $(CXX_OBJS)
368 TEST_OBJS := $(TEST_CXX_OBJS)
369 TEST_BINS := $(TEST_CXX_BINS)
370 ALL_WARNS := $(ALL_CXX_WARNS)
371 TEST_FILTER := --gtest_filter="-*GPU*"
372 COMMON_FLAGS += -DCPU_ONLY
373 endif
375 ifeq ($(NO_NVML), 1)
376 COMMON_FLAGS += -DNO_NVML=1
377 endif
379 ifeq ($(TEST_FP16), 1)
380 COMMON_FLAGS += -DTEST_FP16=1
381 endif
383 # Python layer support
384 ifeq ($(WITH_PYTHON_LAYER), 1)
385 COMMON_FLAGS += -DWITH_PYTHON_LAYER
386 LIBRARIES += $(PYTHON_LIBRARIES)
387 endif
389 # BLAS configuration (default = ATLAS)
390 BLAS ?= atlas
391 ifeq ($(BLAS), mkl)
392 # MKL
393 LIBRARIES += mkl_rt
394 COMMON_FLAGS += -DUSE_MKL
395 MKLROOT ?= /opt/intel/mkl
396 BLAS_INCLUDE ?= $(MKLROOT)/include
397 BLAS_LIB ?= $(MKLROOT)/lib $(MKLROOT)/lib/intel64
398 else ifeq ($(BLAS), open)
399 # OpenBLAS
400 LIBRARIES += openblas
401 else
402 # ATLAS
403 ifeq ($(LINUX), 1)
404 ifeq ($(BLAS), atlas)
405 # Linux simply has cblas and atlas
406 LIBRARIES += cblas atlas
407 endif
408 else ifeq ($(OSX), 1)
409 # OS X packages atlas as the vecLib framework
410 LIBRARIES += cblas
411 # 10.10 has accelerate while 10.9 has veclib
412 XCODE_CLT_VER := $(shell pkgutil --pkg-info=com.apple.pkg.CLTools_Executables | grep 'version' | sed 's/[^0-9]*\([0-9]\).*/\1/')
413 XCODE_CLT_GEQ_6 := $(shell [ $(XCODE_CLT_VER) -gt 5 ] && echo 1)
414 ifeq ($(XCODE_CLT_GEQ_6), 1)
415 BLAS_INCLUDE ?= /System/Library/Frameworks/Accelerate.framework/Versions/Current/Frameworks/vecLib.framework/Headers/
416 LDFLAGS += -framework Accelerate
417 else
418 BLAS_INCLUDE ?= /System/Library/Frameworks/vecLib.framework/Versions/Current/Headers/
419 LDFLAGS += -framework vecLib
420 endif
421 endif
422 endif
423 INCLUDE_DIRS += $(BLAS_INCLUDE)
424 LIBRARY_DIRS += $(BLAS_LIB)
426 LIBRARY_DIRS += $(LIB_BUILD_DIR)
428 # Automatic dependency generation (nvcc is handled separately)
429 CXXFLAGS += -MMD -MP
431 # Complete build flags.
432 COMMON_FLAGS += $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir))
433 CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
434 NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
435 # mex may invoke an older gcc that is too liberal with -Wuninitalized
436 MATLAB_CXXFLAGS := $(CXXFLAGS) -Wno-uninitialized
437 LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
439 USE_PKG_CONFIG ?= 0
440 ifeq ($(USE_PKG_CONFIG), 1)
441 PKG_CONFIG := $(shell pkg-config opencv --libs)
442 else
443 PKG_CONFIG :=
444 endif
445 LDFLAGS += $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir)) $(PKG_CONFIG) \
446 $(foreach library,$(LIBRARIES),-l$(library))
447 PYTHON_LDFLAGS := $(LDFLAGS) $(foreach library,$(PYTHON_LIBRARIES),-l$(library))
449 # 'superclean' target recursively* deletes all files ending with an extension
450 # in $(SUPERCLEAN_EXTS) below. This may be useful if you've built older
451 # versions of Caffe that do not place all generated files in a location known
452 # to the 'clean' target.
453 #
454 # 'supercleanlist' will list the files to be deleted by make superclean.
455 #
456 # * Recursive with the exception that symbolic links are never followed, per the
457 # default behavior of 'find'.
458 SUPERCLEAN_EXTS := .so .a .o .bin .testbin .pb.cc .pb.h _pb2.py .cuo
460 # Set the sub-targets of the 'everything' target.
461 EVERYTHING_TARGETS := all py$(PROJECT) test warn lint
462 # Only build matcaffe as part of "everything" if MATLAB_DIR is specified.
463 ifneq ($(MATLAB_DIR),)
464 EVERYTHING_TARGETS += mat$(PROJECT)
465 endif
467 ##############################
468 # Define build targets
469 ##############################
470 .PHONY: all lib test clean docs linecount lint lintclean tools examples $(DIST_ALIASES) \
471 py mat py$(PROJECT) mat$(PROJECT) proto runtest \
472 superclean supercleanlist supercleanfiles warn everything
474 all: lib tools examples
476 lib: $(STATIC_NAME) $(DYNAMIC_NAME)
478 everything: $(EVERYTHING_TARGETS)
480 linecount:
481 cloc --read-lang-def=$(PROJECT).cloc \
482 src/$(PROJECT) include/$(PROJECT) tools examples \
483 python matlab
485 lint: $(EMPTY_LINT_REPORT)
487 lintclean:
488 @ $(RM) -r $(LINT_OUTPUT_DIR) $(EMPTY_LINT_REPORT) $(NONEMPTY_LINT_REPORT)
490 docs: $(DOXYGEN_OUTPUT_DIR)
491 @ cd ./docs ; ln -sfn ../$(DOXYGEN_OUTPUT_DIR)/html doxygen
493 $(DOXYGEN_OUTPUT_DIR): $(DOXYGEN_CONFIG_FILE) $(DOXYGEN_SOURCES)
494 $(DOXYGEN_COMMAND) $(DOXYGEN_CONFIG_FILE)
496 $(EMPTY_LINT_REPORT): $(LINT_OUTPUTS) | $(BUILD_DIR)
497 @ cat $(LINT_OUTPUTS) > $@
498 @ if [ -s "$@" ]; then \
499 cat $@; \
500 mv $@ $(NONEMPTY_LINT_REPORT); \
501 echo "Found one or more lint errors."; \
502 exit 1; \
503 fi; \
504 $(RM) $(NONEMPTY_LINT_REPORT); \
505 echo "No lint errors!";
507 $(LINT_OUTPUTS): $(LINT_OUTPUT_DIR)/%.lint.txt : % $(LINT_SCRIPT) | $(LINT_OUTPUT_DIR)
508 @ mkdir -p $(dir $@)
509 @ python $(LINT_SCRIPT) --filter=-legal,-build/include,-runtime/references,-readability,-whitespace/comments $< 2>&1 \
510 | grep -v "^Done processing " \
511 | grep -v "^Total errors found: 0" \
512 > $@ \
513 || true
515 test: $(TEST_ALL_BIN) $(TEST_ALL_DYNLINK_BIN) $(TEST_BINS)
517 tools: $(TOOL_BINS) $(TOOL_BIN_LINKS)
519 examples: $(EXAMPLE_BINS)
521 py$(PROJECT): py
523 py: $(PY$(PROJECT)_SO) $(PROTO_GEN_PY)
525 $(PY$(PROJECT)_SO): $(PY$(PROJECT)_SRC) $(PY$(PROJECT)_HXX) | $(DYNAMIC_NAME)
526 @ echo CXX/LD -o $@ $<
527 $(Q)$(CXX) -shared -o $@ $(PY$(PROJECT)_SRC) \
528 -o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(PYTHON_LDFLAGS) \
529 -Wl,-rpath,$(ORIGIN)/../../build/lib
531 mat$(PROJECT): mat
533 mat: $(MAT$(PROJECT)_SO)
535 $(MAT$(PROJECT)_SO): $(MAT$(PROJECT)_SRC) $(STATIC_NAME)
536 @ if [ -z "$(MATLAB_DIR)" ]; then \
537 echo "MATLAB_DIR must be specified in $(CONFIG_FILE)" \
538 "to build mat$(PROJECT)."; \
539 exit 1; \
540 fi
541 @ echo MEX $<
542 $(Q)$(MATLAB_DIR)/bin/mex $(MAT$(PROJECT)_SRC) \
543 CXX="$(CXX)" \
544 CXXFLAGS="\$$CXXFLAGS $(MATLAB_CXXFLAGS)" \
545 CXXLIBS="\$$CXXLIBS $(STATIC_LINK_COMMAND) $(LDFLAGS)" -output $@
546 @ if [ -f "$(PROJECT)_.d" ]; then \
547 mv -f $(PROJECT)_.d $(BUILD_DIR)/${MAT$(PROJECT)_SO:.$(MAT_SO_EXT)=.d}; \
548 fi
550 runtest: $(TEST_ALL_BIN)
551 $(TOOL_BUILD_DIR)/caffe
552 $(TEST_ALL_BIN) $(TEST_GPUID) --gtest_shuffle $(TEST_FILTER)
554 pytest: py
555 cd python; python -m unittest discover -s caffe/test
557 mattest: mat
558 cd matlab; $(MATLAB_DIR)/bin/matlab -nodisplay -r 'caffe.run_tests(), exit()'
560 warn: $(EMPTY_WARN_REPORT)
562 $(EMPTY_WARN_REPORT): $(ALL_WARNS) | $(BUILD_DIR)
563 @ cat $(ALL_WARNS) > $@
564 @ if [ -s "$@" ]; then \
565 cat $@; \
566 mv $@ $(NONEMPTY_WARN_REPORT); \
567 echo "Compiler produced one or more warnings."; \
568 exit 1; \
569 fi; \
570 $(RM) $(NONEMPTY_WARN_REPORT); \
571 echo "No compiler warnings!";
573 $(ALL_WARNS): %.o.$(WARNS_EXT) : %.o
575 $(BUILD_DIR_LINK): $(BUILD_DIR)/.linked
577 # Create a target ".linked" in this BUILD_DIR to tell Make that the "build" link
578 # is currently correct, then delete the one in the OTHER_BUILD_DIR in case it
579 # exists and $(DEBUG) is toggled later.
580 $(BUILD_DIR)/.linked:
581 @ mkdir -p $(BUILD_DIR)
582 @ $(RM) $(OTHER_BUILD_DIR)/.linked
583 @ $(RM) -r $(BUILD_DIR_LINK)
584 @ ln -s $(BUILD_DIR) $(BUILD_DIR_LINK)
585 @ touch $@
587 $(ALL_BUILD_DIRS): | $(BUILD_DIR_LINK)
588 @ mkdir -p $@
590 $(DYNAMIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
591 @ echo LD -o $@
592 $(Q)$(CXX) -shared -o $@ $(OBJS) $(VERSIONFLAGS) $(LINKFLAGS) $(LDFLAGS) $(DYNAMIC_FLAGS)
593 @ cd $(BUILD_DIR)/lib; rm -f $(DYNAMIC_SONAME_SHORT); ln -s $(DYNAMIC_VERSIONED_NAME_SHORT) $(DYNAMIC_SONAME_SHORT)
594 @ cd $(BUILD_DIR)/lib; rm -f $(DYNAMIC_NAME_SHORT); ln -s $(DYNAMIC_SONAME_SHORT) $(DYNAMIC_NAME_SHORT)
596 $(STATIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
597 @ echo AR -o $@
598 $(Q)ar rcs $@ $(OBJS)
600 $(BUILD_DIR)/%.o: %.cpp | $(ALL_BUILD_DIRS)
601 @ echo CXX $<
602 $(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
603 || (cat $@.$(WARNS_EXT); exit 1)
604 @ cat $@.$(WARNS_EXT)
606 $(PROTO_BUILD_DIR)/%.pb.o: $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_GEN_HEADER) \
607 | $(PROTO_BUILD_DIR)
608 @ echo CXX $<
609 $(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
610 || (cat $@.$(WARNS_EXT); exit 1)
611 @ cat $@.$(WARNS_EXT)
613 $(BUILD_DIR)/cuda/%.o: %.cu | $(ALL_BUILD_DIRS)
614 @ echo NVCC $<
615 $(Q)$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -M $< -o ${@:.o=.d} \
616 -odir $(@D)
617 $(Q)$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -c $< -o $@ 2> $@.$(WARNS_EXT) \
618 || (cat $@.$(WARNS_EXT); exit 1)
619 @ cat $@.$(WARNS_EXT)
621 $(TEST_ALL_BIN): $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) \
622 | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
623 @ echo CXX/LD -o $@ $<
624 $(Q)$(CXX) $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) \
625 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib
627 $(TEST_CU_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CU_BUILD_DIR)/%.o \
628 $(GTEST_OBJ) | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
629 @ echo LD $<
630 $(Q)$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) \
631 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib
633 $(TEST_CXX_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CXX_BUILD_DIR)/%.o \
634 $(GTEST_OBJ) | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
635 @ echo LD $<
636 $(Q)$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) \
637 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib
639 # Target for extension-less symlinks to tool binaries with extension '*.bin'.
640 $(TOOL_BUILD_DIR)/%: $(TOOL_BUILD_DIR)/%.bin | $(TOOL_BUILD_DIR)
641 @ $(RM) $@
642 @ ln -s $(notdir $<) $@
644 $(TOOL_BINS): %.bin : %.o | $(DYNAMIC_NAME)
645 @ echo CXX/LD -o $@
646 $(Q)$(CXX) $< -o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(LDFLAGS) \
647 -Wl,-rpath,$(ORIGIN)/../lib
649 $(EXAMPLE_BINS): %.bin : %.o | $(DYNAMIC_NAME)
650 @ echo CXX/LD -o $@
651 $(Q)$(CXX) $< -o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(LDFLAGS) \
652 -Wl,-rpath,$(ORIGIN)/../../lib
654 proto: $(PROTO_GEN_CC) $(PROTO_GEN_HEADER)
656 $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_BUILD_DIR)/%.pb.h : \
657 $(PROTO_SRC_DIR)/%.proto | $(PROTO_BUILD_DIR)
658 @ echo PROTOC $<
659 $(Q)protoc --proto_path=$(PROTO_SRC_DIR) --cpp_out=$(PROTO_BUILD_DIR) $<
661 $(PY_PROTO_BUILD_DIR)/%_pb2.py : $(PROTO_SRC_DIR)/%.proto \
662 $(PY_PROTO_INIT) | $(PY_PROTO_BUILD_DIR)
663 @ echo PROTOC \(python\) $<
664 $(Q)protoc --proto_path=$(PROTO_SRC_DIR) --python_out=$(PY_PROTO_BUILD_DIR) $<
666 $(PY_PROTO_INIT): | $(PY_PROTO_BUILD_DIR)
667 touch $(PY_PROTO_INIT)
669 clean:
670 @- $(RM) -rf $(ALL_BUILD_DIRS)
671 @- $(RM) -rf $(OTHER_BUILD_DIR)
672 @- $(RM) -rf $(BUILD_DIR_LINK)
673 @- $(RM) -rf $(DISTRIBUTE_DIR)
674 @- $(RM) $(PY$(PROJECT)_SO)
675 @- $(RM) $(MAT$(PROJECT)_SO)
677 supercleanfiles:
678 $(eval SUPERCLEAN_FILES := $(strip \
679 $(foreach ext,$(SUPERCLEAN_EXTS), $(shell find . -name '*$(ext)' \
680 -not -path './data/*'))))
682 supercleanlist: supercleanfiles
683 @ \
684 if [ -z "$(SUPERCLEAN_FILES)" ]; then \
685 echo "No generated files found."; \
686 else \
687 echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \
688 fi
690 superclean: clean supercleanfiles
691 @ \
692 if [ -z "$(SUPERCLEAN_FILES)" ]; then \
693 echo "No generated files found."; \
694 else \
695 echo "Deleting the following generated files:"; \
696 echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \
697 $(RM) $(SUPERCLEAN_FILES); \
698 fi
700 $(DIST_ALIASES): $(DISTRIBUTE_DIR)
702 $(DISTRIBUTE_DIR): all py | $(DISTRIBUTE_SUBDIRS)
703 # add proto
704 cp -r src/caffe/proto $(DISTRIBUTE_DIR)/
705 # add include
706 cp -r include $(DISTRIBUTE_DIR)/
707 mkdir -p $(DISTRIBUTE_DIR)/include/caffe/proto
708 cp $(PROTO_GEN_HEADER_SRCS) $(DISTRIBUTE_DIR)/include/caffe/proto
709 # add tool and example binaries
710 cp $(TOOL_BINS) $(DISTRIBUTE_DIR)/bin
711 cp $(EXAMPLE_BINS) $(DISTRIBUTE_DIR)/bin
712 # add libraries
713 cp $(STATIC_NAME) $(DISTRIBUTE_DIR)/lib
714 install -m 644 $(DYNAMIC_NAME) $(DISTRIBUTE_DIR)/lib
715 cd $(DISTRIBUTE_DIR)/lib; rm -f $(DYNAMIC_SONAME_SHORT); ln -s $(DYNAMIC_VERSIONED_NAME_SHORT) $(DYNAMIC_SONAME_SHORT)
716 cd $(DISTRIBUTE_DIR)/lib; rm -f $(DYNAMIC_NAME_SHORT); ln -s $(DYNAMIC_SONAME_SHORT) $(DYNAMIC_NAME_SHORT)
717 # add python - it's not the standard way, indeed...
718 cp -r python $(DISTRIBUTE_DIR)/python
720 -include $(DEPS)