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.)
10 BUILD_DIR_LINK := $(BUILD_DIR)
11 ifeq ($(RELEASE_BUILD_DIR),)
12 RELEASE_BUILD_DIR := .$(BUILD_DIR)_release
14 ifeq ($(DEBUG_BUILD_DIR),)
15 DEBUG_BUILD_DIR := .$(BUILD_DIR)_debug
20 BUILD_DIR := $(DEBUG_BUILD_DIR)
21 OTHER_BUILD_DIR := $(RELEASE_BUILD_DIR)
23 BUILD_DIR := $(RELEASE_BUILD_DIR)
24 OTHER_BUILD_DIR := $(DEBUG_BUILD_DIR)
27 # All of the directories containing code.
28 SRC_DIRS := $(shell find * -type d -exec bash -c "find {} -maxdepth 1 \
29 \( -name '*.cpp' -o -name '*.proto' \) | grep -q ." \; -print)
31 # The target shared library name
32 LIB_BUILD_DIR := $(BUILD_DIR)/lib
33 STATIC_NAME := $(LIB_BUILD_DIR)/lib$(PROJECT).a
34 DYNAMIC_NAME := $(LIB_BUILD_DIR)/lib$(PROJECT).so
36 ##############################
37 # Get all source files
38 ##############################
39 # CXX_SRCS are the source files excluding the test ones.
40 CXX_SRCS := $(shell find src/$(PROJECT) ! -name "test_*.cpp" -name "*.cpp")
41 # CU_SRCS are the cuda source files
42 CU_SRCS := $(shell find src/$(PROJECT) ! -name "test_*.cu" -name "*.cu")
43 # TEST_SRCS are the test source files
44 TEST_MAIN_SRC := src/$(PROJECT)/test/test_caffe_main.cpp
45 TEST_SRCS := $(shell find src/$(PROJECT) -name "test_*.cpp")
46 TEST_SRCS := $(filter-out $(TEST_MAIN_SRC), $(TEST_SRCS))
47 TEST_CU_SRCS := $(shell find src/$(PROJECT) -name "test_*.cu")
48 GTEST_SRC := src/gtest/gtest-all.cpp
49 # TOOL_SRCS are the source files for the tool binaries
50 TOOL_SRCS := $(shell find tools -name "*.cpp")
51 # EXAMPLE_SRCS are the source files for the example binaries
52 EXAMPLE_SRCS := $(shell find examples -name "*.cpp")
53 # BUILD_INCLUDE_DIR contains any generated header files we want to include.
54 BUILD_INCLUDE_DIR := $(BUILD_DIR)/src
55 # PROTO_SRCS are the protocol buffer definitions
56 PROTO_SRC_DIR := src/$(PROJECT)/proto
57 PROTO_SRCS := $(wildcard $(PROTO_SRC_DIR)/*.proto)
58 # PROTO_BUILD_DIR will contain the .cc and obj files generated from
59 # PROTO_SRCS; PROTO_BUILD_INCLUDE_DIR will contain the .h header files
60 PROTO_BUILD_DIR := $(BUILD_DIR)/$(PROTO_SRC_DIR)
61 PROTO_BUILD_INCLUDE_DIR := $(BUILD_INCLUDE_DIR)/$(PROJECT)/proto
62 # NONGEN_CXX_SRCS includes all source/header files except those generated
63 # automatically (e.g., by proto).
64 NONGEN_CXX_SRCS := $(shell find \
68 matlab/+$(PROJECT)/private \
71 -name "*.cpp" -or -name "*.hpp" -or -name "*.cu" -or -name "*.cuh")
72 LINT_SCRIPT := scripts/cpp_lint.py
73 LINT_OUTPUT_DIR := $(BUILD_DIR)/.lint
75 LINT_OUTPUTS := $(addsuffix .$(LINT_EXT), $(addprefix $(LINT_OUTPUT_DIR)/, $(NONGEN_CXX_SRCS)))
76 EMPTY_LINT_REPORT := $(BUILD_DIR)/.$(LINT_EXT)
77 NONEMPTY_LINT_REPORT := $(BUILD_DIR)/$(LINT_EXT)
78 # PY$(PROJECT)_SRC is the python wrapper for $(PROJECT)
79 PY$(PROJECT)_SRC := python/$(PROJECT)/_$(PROJECT).cpp
80 PY$(PROJECT)_SO := python/$(PROJECT)/_$(PROJECT).so
81 PY$(PROJECT)_HXX := include/$(PROJECT)/python_layer.hpp
82 # MAT$(PROJECT)_SRC is the mex entrance point of matlab package for $(PROJECT)
83 MAT$(PROJECT)_SRC := matlab/+$(PROJECT)/private/$(PROJECT)_.cpp
84 ifneq ($(MATLAB_DIR),)
85 MAT_SO_EXT := $(shell $(MATLAB_DIR)/bin/mexext)
87 MAT$(PROJECT)_SO := matlab/+$(PROJECT)/private/$(PROJECT)_.$(MAT_SO_EXT)
89 ##############################
90 # Derive generated files
91 ##############################
92 # The generated files for protocol buffers
93 PROTO_GEN_HEADER_SRCS := $(addprefix $(PROTO_BUILD_DIR)/, \
94 $(notdir ${PROTO_SRCS:.proto=.pb.h}))
95 PROTO_GEN_HEADER := $(addprefix $(PROTO_BUILD_INCLUDE_DIR)/, \
96 $(notdir ${PROTO_SRCS:.proto=.pb.h}))
97 PROTO_GEN_CC := $(addprefix $(BUILD_DIR)/, ${PROTO_SRCS:.proto=.pb.cc})
98 PY_PROTO_BUILD_DIR := python/$(PROJECT)/proto
99 PY_PROTO_INIT := python/$(PROJECT)/proto/__init__.py
100 PROTO_GEN_PY := $(foreach file,${PROTO_SRCS:.proto=_pb2.py}, \
101 $(PY_PROTO_BUILD_DIR)/$(notdir $(file)))
102 # The objects corresponding to the source files
103 # These objects will be linked into the final shared library, so we
104 # exclude the tool, example, and test objects.
105 CXX_OBJS := $(addprefix $(BUILD_DIR)/, ${CXX_SRCS:.cpp=.o})
106 CU_OBJS := $(addprefix $(BUILD_DIR)/cuda/, ${CU_SRCS:.cu=.o})
107 PROTO_OBJS := ${PROTO_GEN_CC:.cc=.o}
108 OBJS := $(PROTO_OBJS) $(CXX_OBJS) $(CU_OBJS)
109 # tool, example, and test objects
110 TOOL_OBJS := $(addprefix $(BUILD_DIR)/, ${TOOL_SRCS:.cpp=.o})
111 TOOL_BUILD_DIR := $(BUILD_DIR)/tools
112 TEST_CXX_BUILD_DIR := $(BUILD_DIR)/src/$(PROJECT)/test
113 TEST_CU_BUILD_DIR := $(BUILD_DIR)/cuda/src/$(PROJECT)/test
114 TEST_CXX_OBJS := $(addprefix $(BUILD_DIR)/, ${TEST_SRCS:.cpp=.o})
115 TEST_CU_OBJS := $(addprefix $(BUILD_DIR)/cuda/, ${TEST_CU_SRCS:.cu=.o})
116 TEST_OBJS := $(TEST_CXX_OBJS) $(TEST_CU_OBJS)
117 GTEST_OBJ := $(addprefix $(BUILD_DIR)/, ${GTEST_SRC:.cpp=.o})
118 EXAMPLE_OBJS := $(addprefix $(BUILD_DIR)/, ${EXAMPLE_SRCS:.cpp=.o})
119 # Output files for automatic dependency generation
120 DEPS := ${CXX_OBJS:.o=.d} ${CU_OBJS:.o=.d} ${TEST_CXX_OBJS:.o=.d} \
121 ${TEST_CU_OBJS:.o=.d} $(BUILD_DIR)/${MAT$(PROJECT)_SO:.$(MAT_SO_EXT)=.d}
122 # tool, example, and test bins
123 TOOL_BINS := ${TOOL_OBJS:.o=.bin}
124 EXAMPLE_BINS := ${EXAMPLE_OBJS:.o=.bin}
125 # symlinks to tool bins without the ".bin" extension
126 TOOL_BIN_LINKS := ${TOOL_BINS:.bin=}
127 # Put the test binaries in build/test for convenience.
128 TEST_BIN_DIR := $(BUILD_DIR)/test
129 TEST_CU_BINS := $(addsuffix .testbin,$(addprefix $(TEST_BIN_DIR)/, \
130 $(foreach obj,$(TEST_CU_OBJS),$(basename $(notdir $(obj))))))
131 TEST_CXX_BINS := $(addsuffix .testbin,$(addprefix $(TEST_BIN_DIR)/, \
132 $(foreach obj,$(TEST_CXX_OBJS),$(basename $(notdir $(obj))))))
133 TEST_BINS := $(TEST_CXX_BINS) $(TEST_CU_BINS)
134 # TEST_ALL_BIN is the test binary that links caffe dynamically.
135 TEST_ALL_BIN := $(TEST_BIN_DIR)/test_all.testbin
137 ##############################
138 # Derive compiler warning dump locations
139 ##############################
140 WARNS_EXT := warnings.txt
141 CXX_WARNS := $(addprefix $(BUILD_DIR)/, ${CXX_SRCS:.cpp=.o.$(WARNS_EXT)})
142 CU_WARNS := $(addprefix $(BUILD_DIR)/cuda/, ${CU_SRCS:.cu=.o.$(WARNS_EXT)})
143 TOOL_WARNS := $(addprefix $(BUILD_DIR)/, ${TOOL_SRCS:.cpp=.o.$(WARNS_EXT)})
144 EXAMPLE_WARNS := $(addprefix $(BUILD_DIR)/, ${EXAMPLE_SRCS:.cpp=.o.$(WARNS_EXT)})
145 TEST_WARNS := $(addprefix $(BUILD_DIR)/, ${TEST_SRCS:.cpp=.o.$(WARNS_EXT)})
146 TEST_CU_WARNS := $(addprefix $(BUILD_DIR)/cuda/, ${TEST_CU_SRCS:.cu=.o.$(WARNS_EXT)})
147 ALL_CXX_WARNS := $(CXX_WARNS) $(TOOL_WARNS) $(EXAMPLE_WARNS) $(TEST_WARNS)
148 ALL_CU_WARNS := $(CU_WARNS) $(TEST_CU_WARNS)
149 ALL_WARNS := $(ALL_CXX_WARNS) $(ALL_CU_WARNS)
151 EMPTY_WARN_REPORT := $(BUILD_DIR)/.$(WARNS_EXT)
152 NONEMPTY_WARN_REPORT := $(BUILD_DIR)/$(WARNS_EXT)
154 ##############################
155 # Derive include and lib directories
156 ##############################
157 CUDA_INCLUDE_DIR := $(CUDA_DIR)/include
160 # add <cuda>/lib64 only if it exists
161 ifneq ("$(wildcard $(CUDA_DIR)/lib64)","")
162 CUDA_LIB_DIR += $(CUDA_DIR)/lib64
164 CUDA_LIB_DIR += $(CUDA_DIR)/lib
166 INCLUDE_DIRS += $(BUILD_INCLUDE_DIR) ./src ./include
167 ifneq ($(CPU_ONLY), 1)
168 INCLUDE_DIRS += $(CUDA_INCLUDE_DIR)
169 LIBRARY_DIRS += $(CUDA_LIB_DIR)
170 LIBRARIES := cudart cublas curand
173 LIBRARIES += glog gflags protobuf boost_system m hdf5_hl hdf5
175 # handle IO dependencies
180 ifeq ($(USE_LEVELDB), 1)
181 LIBRARIES += leveldb snappy
183 ifeq ($(USE_LMDB), 1)
186 ifeq ($(USE_OPENCV), 1)
187 LIBRARIES += opencv_core opencv_highgui opencv_imgproc
189 ifeq ($(OPENCV_VERSION), 3)
190 LIBRARIES += opencv_imgcodecs
194 PYTHON_LIBRARIES := boost_python python2.7
195 WARNINGS := -Wall -Wno-sign-compare
197 ##############################
198 # Set build directories
199 ##############################
201 DISTRIBUTE_DIR ?= distribute
202 DISTRIBUTE_SUBDIRS := $(DISTRIBUTE_DIR)/bin $(DISTRIBUTE_DIR)/lib
204 ifneq ($(strip $(DISTRIBUTE_DIR)),distribute)
205 DIST_ALIASES += distribute
208 ALL_BUILD_DIRS := $(sort $(BUILD_DIR) $(addprefix $(BUILD_DIR)/, $(SRC_DIRS)) \
209 $(addprefix $(BUILD_DIR)/cuda/, $(SRC_DIRS)) \
210 $(LIB_BUILD_DIR) $(TEST_BIN_DIR) $(PY_PROTO_BUILD_DIR) $(LINT_OUTPUT_DIR) \
211 $(DISTRIBUTE_SUBDIRS) $(PROTO_BUILD_INCLUDE_DIR))
213 ##############################
214 # Set directory for Doxygen-generated documentation
215 ##############################
216 DOXYGEN_CONFIG_FILE ?= ./.Doxyfile
217 # should be the same as OUTPUT_DIRECTORY in the .Doxyfile
218 DOXYGEN_OUTPUT_DIR ?= ./doxygen
219 DOXYGEN_COMMAND ?= doxygen
220 # All the files that might have Doxygen documentation.
221 DOXYGEN_SOURCES := $(shell find \
228 -name "*.cpp" -or -name "*.hpp" -or -name "*.cu" -or -name "*.cuh" -or \
229 -name "*.py" -or -name "*.m")
230 DOXYGEN_SOURCES += $(DOXYGEN_CONFIG_FILE)
233 ##############################
235 ##############################
238 UNAME := $(shell uname -s)
239 ifeq ($(UNAME), Linux)
241 else ifeq ($(UNAME), Darwin)
248 GCCVERSION := $(shell $(CXX) -dumpversion | cut -f1,2 -d.)
249 # older versions of gcc are too dumb to build boost with -Wuninitalized
250 ifeq ($(shell echo | awk '{exit $(GCCVERSION) < 4.6;}'), 1)
251 WARNINGS += -Wno-uninitialized
253 # boost::thread is reasonably called boost_thread (compare OS X)
254 # We will also explicitly add stdc++ to the link target.
255 LIBRARIES += boost_thread stdc++
259 # clang++ instead of g++
260 # libstdc++ for NVCC compatibility on OS X >= 10.9 with CUDA < 7.0
262 CXX := /usr/bin/clang++
263 ifneq ($(CPU_ONLY), 1)
264 CUDA_VERSION := $(shell $(CUDA_DIR)/bin/nvcc -V | grep -o 'release \d' | grep -o '\d')
265 ifeq ($(shell echo | awk '{exit $(CUDA_VERSION) < 7.0;}'), 1)
266 CXXFLAGS += -stdlib=libstdc++
267 LINKFLAGS += -stdlib=libstdc++
269 # clang throws this warning for cuda headers
270 WARNINGS += -Wno-unneeded-internal-declaration
272 # gtest needs to use its own tuple to not conflict with clang
273 COMMON_FLAGS += -DGTEST_USE_OWN_TR1_TUPLE=1
274 # boost::thread is called boost_thread-mt to mark multithreading on OS X
275 LIBRARIES += boost_thread-mt
276 # we need to explicitly ask for the rpath to be obeyed
277 DYNAMIC_FLAGS := -install_name @rpath/libcaffe.so
278 ORIGIN := @loader_path
289 ifneq (,$(findstring clang++,$(CXX)))
290 STATIC_LINK_COMMAND := -Wl,-force_load $(STATIC_NAME)
291 else ifneq (,$(findstring g++,$(CXX)))
292 STATIC_LINK_COMMAND := -Wl,--whole-archive $(STATIC_NAME) -Wl,--no-whole-archive
294 # The following line must not be indented with a tab, since we are not inside a target
295 $(error Cannot static link with the $(CXX) compiler)
300 COMMON_FLAGS += -DDEBUG -g -O0
303 COMMON_FLAGS += -DNDEBUG -O2
306 # cuDNN acceleration configuration.
307 ifeq ($(USE_CUDNN), 1)
309 COMMON_FLAGS += -DUSE_CUDNN
312 # configure IO libraries
313 ifeq ($(USE_OPENCV), 1)
314 COMMON_FLAGS += -DUSE_OPENCV
316 ifeq ($(USE_LEVELDB), 1)
317 COMMON_FLAGS += -DUSE_LEVELDB
319 ifeq ($(USE_LMDB), 1)
320 COMMON_FLAGS += -DUSE_LMDB
321 ifeq ($(ALLOW_LMDB_NOLOCK), 1)
322 COMMON_FLAGS += -DALLOW_LMDB_NOLOCK
326 # CPU-only configuration
327 ifeq ($(CPU_ONLY), 1)
328 OBJS := $(PROTO_OBJS) $(CXX_OBJS)
329 TEST_OBJS := $(TEST_CXX_OBJS)
330 TEST_BINS := $(TEST_CXX_BINS)
331 ALL_WARNS := $(ALL_CXX_WARNS)
332 TEST_FILTER := --gtest_filter="-*GPU*"
333 COMMON_FLAGS += -DCPU_ONLY
336 # Python layer support
337 ifeq ($(WITH_PYTHON_LAYER), 1)
338 COMMON_FLAGS += -DWITH_PYTHON_LAYER
339 LIBRARIES += $(PYTHON_LIBRARIES)
342 # BLAS configuration (default = ATLAS)
347 COMMON_FLAGS += -DUSE_MKL
348 MKL_DIR ?= /opt/intel/mkl
349 BLAS_INCLUDE ?= $(MKL_DIR)/include
350 BLAS_LIB ?= $(MKL_DIR)/lib $(MKL_DIR)/lib/intel64
351 else ifeq ($(BLAS), open)
353 LIBRARIES += openblas
357 ifeq ($(BLAS), atlas)
358 # Linux simply has cblas and atlas
359 LIBRARIES += cblas atlas
361 else ifeq ($(OSX), 1)
362 # OS X packages atlas as the vecLib framework
364 # 10.10 has accelerate while 10.9 has veclib
365 XCODE_CLT_VER := $(shell pkgutil --pkg-info=com.apple.pkg.CLTools_Executables | grep 'version' | sed 's/[^0-9]*\([0-9]\).*/\1/')
366 XCODE_CLT_GEQ_6 := $(shell [ $(XCODE_CLT_VER) -gt 5 ] && echo 1)
367 ifeq ($(XCODE_CLT_GEQ_6), 1)
368 BLAS_INCLUDE ?= /System/Library/Frameworks/Accelerate.framework/Versions/Current/Frameworks/vecLib.framework/Headers/
369 LDFLAGS += -framework Accelerate
371 BLAS_INCLUDE ?= /System/Library/Frameworks/vecLib.framework/Versions/Current/Headers/
372 LDFLAGS += -framework vecLib
376 INCLUDE_DIRS += $(BLAS_INCLUDE)
377 LIBRARY_DIRS += $(BLAS_LIB)
379 LIBRARY_DIRS += $(LIB_BUILD_DIR)
381 # Automatic dependency generation (nvcc is handled separately)
384 # Complete build flags.
385 COMMON_FLAGS += $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir))
386 CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
387 NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
388 # mex may invoke an older gcc that is too liberal with -Wuninitalized
389 MATLAB_CXXFLAGS := $(CXXFLAGS) -Wno-uninitialized
390 LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
393 ifeq ($(USE_PKG_CONFIG), 1)
394 PKG_CONFIG := $(shell pkg-config opencv --libs)
398 LDFLAGS += $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir)) $(PKG_CONFIG) \
399 $(foreach library,$(LIBRARIES),-l$(library))
400 PYTHON_LDFLAGS := $(LDFLAGS) $(foreach library,$(PYTHON_LIBRARIES),-l$(library))
402 # 'superclean' target recursively* deletes all files ending with an extension
403 # in $(SUPERCLEAN_EXTS) below. This may be useful if you've built older
404 # versions of Caffe that do not place all generated files in a location known
405 # to the 'clean' target.
407 # 'supercleanlist' will list the files to be deleted by make superclean.
409 # * Recursive with the exception that symbolic links are never followed, per the
410 # default behavior of 'find'.
411 SUPERCLEAN_EXTS := .so .a .o .bin .testbin .pb.cc .pb.h _pb2.py .cuo
413 # Set the sub-targets of the 'everything' target.
414 EVERYTHING_TARGETS := all py$(PROJECT) test warn lint
415 # Only build matcaffe as part of "everything" if MATLAB_DIR is specified.
416 ifneq ($(MATLAB_DIR),)
417 EVERYTHING_TARGETS += mat$(PROJECT)
420 ##############################
421 # Define build targets
422 ##############################
423 .PHONY: all lib test clean docs linecount lint lintclean tools examples $(DIST_ALIASES) \
424 py mat py$(PROJECT) mat$(PROJECT) proto runtest \
425 superclean supercleanlist supercleanfiles warn everything
427 all: lib tools examples
429 lib: $(STATIC_NAME) $(DYNAMIC_NAME)
431 everything: $(EVERYTHING_TARGETS)
434 cloc --read-lang-def=$(PROJECT).cloc \
435 src/$(PROJECT) include/$(PROJECT) tools examples \
438 lint: $(EMPTY_LINT_REPORT)
441 @ $(RM) -r $(LINT_OUTPUT_DIR) $(EMPTY_LINT_REPORT) $(NONEMPTY_LINT_REPORT)
443 docs: $(DOXYGEN_OUTPUT_DIR)
444 @ cd ./docs ; ln -sfn ../$(DOXYGEN_OUTPUT_DIR)/html doxygen
446 $(DOXYGEN_OUTPUT_DIR): $(DOXYGEN_CONFIG_FILE) $(DOXYGEN_SOURCES)
447 $(DOXYGEN_COMMAND) $(DOXYGEN_CONFIG_FILE)
449 $(EMPTY_LINT_REPORT): $(LINT_OUTPUTS) | $(BUILD_DIR)
450 @ cat $(LINT_OUTPUTS) > $@
451 @ if [ -s "$@" ]; then \
453 mv $@ $(NONEMPTY_LINT_REPORT); \
454 echo "Found one or more lint errors."; \
457 $(RM) $(NONEMPTY_LINT_REPORT); \
458 echo "No lint errors!";
460 $(LINT_OUTPUTS): $(LINT_OUTPUT_DIR)/%.lint.txt : % $(LINT_SCRIPT) | $(LINT_OUTPUT_DIR)
462 @ python $(LINT_SCRIPT) $< 2>&1 \
463 | grep -v "^Done processing " \
464 | grep -v "^Total errors found: 0" \
468 test: $(TEST_ALL_BIN) $(TEST_ALL_DYNLINK_BIN) $(TEST_BINS)
470 tools: $(TOOL_BINS) $(TOOL_BIN_LINKS)
472 examples: $(EXAMPLE_BINS)
476 py: $(PY$(PROJECT)_SO) $(PROTO_GEN_PY)
478 $(PY$(PROJECT)_SO): $(PY$(PROJECT)_SRC) $(PY$(PROJECT)_HXX) | $(DYNAMIC_NAME)
479 @ echo CXX/LD -o $@ $<
480 $(Q)$(CXX) -shared -o $@ $(PY$(PROJECT)_SRC) \
481 -o $@ $(LINKFLAGS) -l$(PROJECT) $(PYTHON_LDFLAGS) \
482 -Wl,-rpath,$(ORIGIN)/../../build/lib
486 mat: $(MAT$(PROJECT)_SO)
488 $(MAT$(PROJECT)_SO): $(MAT$(PROJECT)_SRC) $(STATIC_NAME)
489 @ if [ -z "$(MATLAB_DIR)" ]; then \
490 echo "MATLAB_DIR must be specified in $(CONFIG_FILE)" \
491 "to build mat$(PROJECT)."; \
495 $(Q)$(MATLAB_DIR)/bin/mex $(MAT$(PROJECT)_SRC) \
497 CXXFLAGS="\$$CXXFLAGS $(MATLAB_CXXFLAGS)" \
498 CXXLIBS="\$$CXXLIBS $(STATIC_LINK_COMMAND) $(LDFLAGS)" -output $@
499 @ if [ -f "$(PROJECT)_.d" ]; then \
500 mv -f $(PROJECT)_.d $(BUILD_DIR)/${MAT$(PROJECT)_SO:.$(MAT_SO_EXT)=.d}; \
503 runtest: $(TEST_ALL_BIN)
504 $(TOOL_BUILD_DIR)/caffe
505 $(TEST_ALL_BIN) $(TEST_GPUID) --gtest_shuffle $(TEST_FILTER)
508 cd python; python -m unittest discover -s caffe/test
511 cd matlab; $(MATLAB_DIR)/bin/matlab -nodisplay -r 'caffe.run_tests(), exit()'
513 warn: $(EMPTY_WARN_REPORT)
515 $(EMPTY_WARN_REPORT): $(ALL_WARNS) | $(BUILD_DIR)
516 @ cat $(ALL_WARNS) > $@
517 @ if [ -s "$@" ]; then \
519 mv $@ $(NONEMPTY_WARN_REPORT); \
520 echo "Compiler produced one or more warnings."; \
523 $(RM) $(NONEMPTY_WARN_REPORT); \
524 echo "No compiler warnings!";
526 $(ALL_WARNS): %.o.$(WARNS_EXT) : %.o
528 $(BUILD_DIR_LINK): $(BUILD_DIR)/.linked
530 # Create a target ".linked" in this BUILD_DIR to tell Make that the "build" link
531 # is currently correct, then delete the one in the OTHER_BUILD_DIR in case it
532 # exists and $(DEBUG) is toggled later.
533 $(BUILD_DIR)/.linked:
534 @ mkdir -p $(BUILD_DIR)
535 @ $(RM) $(OTHER_BUILD_DIR)/.linked
536 @ $(RM) -r $(BUILD_DIR_LINK)
537 @ ln -s $(BUILD_DIR) $(BUILD_DIR_LINK)
540 $(ALL_BUILD_DIRS): | $(BUILD_DIR_LINK)
543 $(DYNAMIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
545 $(Q)$(CXX) -shared -o $@ $(OBJS) $(LINKFLAGS) $(LDFLAGS) $(DYNAMIC_FLAGS)
547 $(STATIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
549 $(Q)ar rcs $@ $(OBJS)
551 $(BUILD_DIR)/%.o: %.cpp | $(ALL_BUILD_DIRS)
553 $(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
554 || (cat $@.$(WARNS_EXT); exit 1)
555 @ cat $@.$(WARNS_EXT)
557 $(PROTO_BUILD_DIR)/%.pb.o: $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_GEN_HEADER) \
560 $(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
561 || (cat $@.$(WARNS_EXT); exit 1)
562 @ cat $@.$(WARNS_EXT)
564 $(BUILD_DIR)/cuda/%.o: %.cu | $(ALL_BUILD_DIRS)
566 $(Q)$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -M $< -o ${@:.o=.d} \
568 $(Q)$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -c $< -o $@ 2> $@.$(WARNS_EXT) \
569 || (cat $@.$(WARNS_EXT); exit 1)
570 @ cat $@.$(WARNS_EXT)
572 $(TEST_ALL_BIN): $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) \
573 | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
574 @ echo CXX/LD -o $@ $<
575 $(Q)$(CXX) $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) \
576 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(PROJECT) -Wl,-rpath,$(ORIGIN)/../lib
578 $(TEST_CU_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CU_BUILD_DIR)/%.o \
579 $(GTEST_OBJ) | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
581 $(Q)$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) \
582 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(PROJECT) -Wl,-rpath,$(ORIGIN)/../lib
584 $(TEST_CXX_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CXX_BUILD_DIR)/%.o \
585 $(GTEST_OBJ) | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
587 $(Q)$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) \
588 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(PROJECT) -Wl,-rpath,$(ORIGIN)/../lib
590 # Target for extension-less symlinks to tool binaries with extension '*.bin'.
591 $(TOOL_BUILD_DIR)/%: $(TOOL_BUILD_DIR)/%.bin | $(TOOL_BUILD_DIR)
593 @ ln -s $(abspath $<) $@
595 $(TOOL_BINS): %.bin : %.o | $(DYNAMIC_NAME)
597 $(Q)$(CXX) $< -o $@ $(LINKFLAGS) -l$(PROJECT) $(LDFLAGS) \
598 -Wl,-rpath,$(ORIGIN)/../lib
600 $(EXAMPLE_BINS): %.bin : %.o | $(DYNAMIC_NAME)
602 $(Q)$(CXX) $< -o $@ $(LINKFLAGS) -l$(PROJECT) $(LDFLAGS) \
603 -Wl,-rpath,$(ORIGIN)/../../lib
605 proto: $(PROTO_GEN_CC) $(PROTO_GEN_HEADER)
607 $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_BUILD_DIR)/%.pb.h : \
608 $(PROTO_SRC_DIR)/%.proto | $(PROTO_BUILD_DIR)
610 $(Q)protoc --proto_path=$(PROTO_SRC_DIR) --cpp_out=$(PROTO_BUILD_DIR) $<
612 $(PY_PROTO_BUILD_DIR)/%_pb2.py : $(PROTO_SRC_DIR)/%.proto \
613 $(PY_PROTO_INIT) | $(PY_PROTO_BUILD_DIR)
614 @ echo PROTOC \(python\) $<
615 $(Q)protoc --proto_path=$(PROTO_SRC_DIR) --python_out=$(PY_PROTO_BUILD_DIR) $<
617 $(PY_PROTO_INIT): | $(PY_PROTO_BUILD_DIR)
618 touch $(PY_PROTO_INIT)
621 @- $(RM) -rf $(ALL_BUILD_DIRS)
622 @- $(RM) -rf $(OTHER_BUILD_DIR)
623 @- $(RM) -rf $(BUILD_DIR_LINK)
624 @- $(RM) -rf $(DISTRIBUTE_DIR)
625 @- $(RM) $(PY$(PROJECT)_SO)
626 @- $(RM) $(MAT$(PROJECT)_SO)
629 $(eval SUPERCLEAN_FILES := $(strip \
630 $(foreach ext,$(SUPERCLEAN_EXTS), $(shell find . -name '*$(ext)' \
631 -not -path './data/*'))))
633 supercleanlist: supercleanfiles
635 if [ -z "$(SUPERCLEAN_FILES)" ]; then \
636 echo "No generated files found."; \
638 echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \
641 superclean: clean supercleanfiles
643 if [ -z "$(SUPERCLEAN_FILES)" ]; then \
644 echo "No generated files found."; \
646 echo "Deleting the following generated files:"; \
647 echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \
648 $(RM) $(SUPERCLEAN_FILES); \
651 $(DIST_ALIASES): $(DISTRIBUTE_DIR)
653 $(DISTRIBUTE_DIR): all py | $(DISTRIBUTE_SUBDIRS)
655 cp -r include $(DISTRIBUTE_DIR)/
656 mkdir -p $(DISTRIBUTE_DIR)/include/caffe/proto
657 cp $(PROTO_GEN_HEADER_SRCS) $(DISTRIBUTE_DIR)/include/caffe/proto
658 # add tool and example binaries
659 cp $(TOOL_BINS) $(DISTRIBUTE_DIR)/bin
660 cp $(EXAMPLE_BINS) $(DISTRIBUTE_DIR)/bin
662 cp $(STATIC_NAME) $(DISTRIBUTE_DIR)/lib
663 cp $(DYNAMIC_NAME) $(DISTRIBUTE_DIR)/lib
664 # add python - it's not the standard way, indeed...
665 cp -r python $(DISTRIBUTE_DIR)/python