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 LIBRARY_NAME := $(PROJECT)
33 LIB_BUILD_DIR := $(BUILD_DIR)/lib
34 STATIC_NAME := $(LIB_BUILD_DIR)/lib$(LIBRARY_NAME).a
35 DYNAMIC_VERSION_MAJOR := 1
36 DYNAMIC_VERSION_MINOR := 0
37 DYNAMIC_VERSION_REVISION := 0-rc3
38 DYNAMIC_NAME_SHORT := lib$(LIBRARY_NAME).so
39 #DYNAMIC_SONAME_SHORT := $(DYNAMIC_NAME_SHORT).$(DYNAMIC_VERSION_MAJOR)
40 DYNAMIC_VERSIONED_NAME_SHORT := $(DYNAMIC_NAME_SHORT).$(DYNAMIC_VERSION_MAJOR).$(DYNAMIC_VERSION_MINOR).$(DYNAMIC_VERSION_REVISION)
41 DYNAMIC_NAME := $(LIB_BUILD_DIR)/$(DYNAMIC_VERSIONED_NAME_SHORT)
42 COMMON_FLAGS += -DCAFFE_VERSION=$(DYNAMIC_VERSION_MAJOR).$(DYNAMIC_VERSION_MINOR).$(DYNAMIC_VERSION_REVISION)
44 ##############################
45 # Get all source files
46 ##############################
47 # CXX_SRCS are the source files excluding the test ones.
48 CXX_SRCS := $(shell find src/$(PROJECT) ! -name "test_*.cpp" -name "*.cpp")
49 # CU_SRCS are the cuda source files
50 CU_SRCS := $(shell find src/$(PROJECT) ! -name "test_*.cu" -name "*.cu")
51 # TEST_SRCS are the test source files
52 TEST_MAIN_SRC := src/$(PROJECT)/test/test_caffe_main.cpp
53 TEST_SRCS := $(shell find src/$(PROJECT) -name "test_*.cpp")
54 TEST_SRCS := $(filter-out $(TEST_MAIN_SRC), $(TEST_SRCS))
55 TEST_CU_SRCS := $(shell find src/$(PROJECT) -name "test_*.cu")
56 GTEST_SRC := src/gtest/gtest-all.cpp
57 # TOOL_SRCS are the source files for the tool binaries
58 TOOL_SRCS := $(shell find tools -name "*.cpp")
59 # EXAMPLE_SRCS are the source files for the example binaries
60 EXAMPLE_SRCS := $(shell find examples -name "*.cpp")
61 # BUILD_INCLUDE_DIR contains any generated header files we want to include.
62 BUILD_INCLUDE_DIR := $(BUILD_DIR)/src
63 # PROTO_SRCS are the protocol buffer definitions
64 PROTO_SRC_DIR := src/$(PROJECT)/proto
65 PROTO_SRCS := $(wildcard $(PROTO_SRC_DIR)/*.proto)
66 # PROTO_BUILD_DIR will contain the .cc and obj files generated from
67 # PROTO_SRCS; PROTO_BUILD_INCLUDE_DIR will contain the .h header files
68 PROTO_BUILD_DIR := $(BUILD_DIR)/$(PROTO_SRC_DIR)
69 PROTO_BUILD_INCLUDE_DIR := $(BUILD_INCLUDE_DIR)/$(PROJECT)/proto
70 # NONGEN_CXX_SRCS includes all source/header files except those generated
71 # automatically (e.g., by proto).
72 NONGEN_CXX_SRCS := $(shell find \
76 matlab/+$(PROJECT)/private \
79 -name "*.cpp" -or -name "*.hpp" -or -name "*.cu" -or -name "*.cuh")
80 LINT_SCRIPT := scripts/cpp_lint.py
81 LINT_OUTPUT_DIR := $(BUILD_DIR)/.lint
83 LINT_OUTPUTS := $(addsuffix .$(LINT_EXT), $(addprefix $(LINT_OUTPUT_DIR)/, $(NONGEN_CXX_SRCS)))
84 EMPTY_LINT_REPORT := $(BUILD_DIR)/.$(LINT_EXT)
85 NONEMPTY_LINT_REPORT := $(BUILD_DIR)/$(LINT_EXT)
86 # PY$(PROJECT)_SRC is the python wrapper for $(PROJECT)
87 PY$(PROJECT)_SRC := python/$(PROJECT)/_$(PROJECT).cpp
88 PY$(PROJECT)_SO := python/$(PROJECT)/_$(PROJECT).so
89 PY$(PROJECT)_HXX := include/$(PROJECT)/layers/python_layer.hpp
90 # MAT$(PROJECT)_SRC is the mex entrance point of matlab package for $(PROJECT)
91 MAT$(PROJECT)_SRC := matlab/+$(PROJECT)/private/$(PROJECT)_.cpp
92 ifneq ($(MATLAB_DIR),)
93 MAT_SO_EXT := $(shell $(MATLAB_DIR)/bin/mexext)
95 MAT$(PROJECT)_SO := matlab/+$(PROJECT)/private/$(PROJECT)_.$(MAT_SO_EXT)
97 ##############################
98 # Derive generated files
99 ##############################
100 # The generated files for protocol buffers
101 PROTO_GEN_HEADER_SRCS := $(addprefix $(PROTO_BUILD_DIR)/, \
102 $(notdir ${PROTO_SRCS:.proto=.pb.h}))
103 PROTO_GEN_HEADER := $(addprefix $(PROTO_BUILD_INCLUDE_DIR)/, \
104 $(notdir ${PROTO_SRCS:.proto=.pb.h}))
105 PROTO_GEN_CC := $(addprefix $(BUILD_DIR)/, ${PROTO_SRCS:.proto=.pb.cc})
106 PY_PROTO_BUILD_DIR := python/$(PROJECT)/proto
107 PY_PROTO_INIT := python/$(PROJECT)/proto/__init__.py
108 PROTO_GEN_PY := $(foreach file,${PROTO_SRCS:.proto=_pb2.py}, \
109 $(PY_PROTO_BUILD_DIR)/$(notdir $(file)))
110 # The objects corresponding to the source files
111 # These objects will be linked into the final shared library, so we
112 # exclude the tool, example, and test objects.
113 CXX_OBJS := $(addprefix $(BUILD_DIR)/, ${CXX_SRCS:.cpp=.o})
114 CU_OBJS := $(addprefix $(BUILD_DIR)/cuda/, ${CU_SRCS:.cu=.o})
115 PROTO_OBJS := ${PROTO_GEN_CC:.cc=.o}
116 OBJS := $(PROTO_OBJS) $(CXX_OBJS) $(CU_OBJS)
117 # tool, example, and test objects
118 TOOL_OBJS := $(addprefix $(BUILD_DIR)/, ${TOOL_SRCS:.cpp=.o})
119 TOOL_BUILD_DIR := $(BUILD_DIR)/tools
120 TEST_CXX_BUILD_DIR := $(BUILD_DIR)/src/$(PROJECT)/test
121 TEST_CU_BUILD_DIR := $(BUILD_DIR)/cuda/src/$(PROJECT)/test
122 TEST_CXX_OBJS := $(addprefix $(BUILD_DIR)/, ${TEST_SRCS:.cpp=.o})
123 TEST_CU_OBJS := $(addprefix $(BUILD_DIR)/cuda/, ${TEST_CU_SRCS:.cu=.o})
124 TEST_OBJS := $(TEST_CXX_OBJS) $(TEST_CU_OBJS)
125 GTEST_OBJ := $(addprefix $(BUILD_DIR)/, ${GTEST_SRC:.cpp=.o})
126 EXAMPLE_OBJS := $(addprefix $(BUILD_DIR)/, ${EXAMPLE_SRCS:.cpp=.o})
127 # Output files for automatic dependency generation
128 DEPS := ${CXX_OBJS:.o=.d} ${CU_OBJS:.o=.d} ${TEST_CXX_OBJS:.o=.d} \
129 ${TEST_CU_OBJS:.o=.d} $(BUILD_DIR)/${MAT$(PROJECT)_SO:.$(MAT_SO_EXT)=.d}
130 # tool, example, and test bins
131 TOOL_BINS := ${TOOL_OBJS:.o=.bin}
132 EXAMPLE_BINS := ${EXAMPLE_OBJS:.o=.bin}
133 # symlinks to tool bins without the ".bin" extension
134 TOOL_BIN_LINKS := ${TOOL_BINS:.bin=}
135 # Put the test binaries in build/test for convenience.
136 TEST_BIN_DIR := $(BUILD_DIR)/test
137 TEST_CU_BINS := $(addsuffix .testbin,$(addprefix $(TEST_BIN_DIR)/, \
138 $(foreach obj,$(TEST_CU_OBJS),$(basename $(notdir $(obj))))))
139 TEST_CXX_BINS := $(addsuffix .testbin,$(addprefix $(TEST_BIN_DIR)/, \
140 $(foreach obj,$(TEST_CXX_OBJS),$(basename $(notdir $(obj))))))
141 TEST_BINS := $(TEST_CXX_BINS) $(TEST_CU_BINS)
142 # TEST_ALL_BIN is the test binary that links caffe dynamically.
143 TEST_ALL_BIN := $(TEST_BIN_DIR)/test_all.testbin
145 ##############################
146 # Derive compiler warning dump locations
147 ##############################
148 WARNS_EXT := warnings.txt
149 CXX_WARNS := $(addprefix $(BUILD_DIR)/, ${CXX_SRCS:.cpp=.o.$(WARNS_EXT)})
150 CU_WARNS := $(addprefix $(BUILD_DIR)/cuda/, ${CU_SRCS:.cu=.o.$(WARNS_EXT)})
151 TOOL_WARNS := $(addprefix $(BUILD_DIR)/, ${TOOL_SRCS:.cpp=.o.$(WARNS_EXT)})
152 EXAMPLE_WARNS := $(addprefix $(BUILD_DIR)/, ${EXAMPLE_SRCS:.cpp=.o.$(WARNS_EXT)})
153 TEST_WARNS := $(addprefix $(BUILD_DIR)/, ${TEST_SRCS:.cpp=.o.$(WARNS_EXT)})
154 TEST_CU_WARNS := $(addprefix $(BUILD_DIR)/cuda/, ${TEST_CU_SRCS:.cu=.o.$(WARNS_EXT)})
155 ALL_CXX_WARNS := $(CXX_WARNS) $(TOOL_WARNS) $(EXAMPLE_WARNS) $(TEST_WARNS)
156 ALL_CU_WARNS := $(CU_WARNS) $(TEST_CU_WARNS)
157 ALL_WARNS := $(ALL_CXX_WARNS) $(ALL_CU_WARNS)
159 EMPTY_WARN_REPORT := $(BUILD_DIR)/.$(WARNS_EXT)
160 NONEMPTY_WARN_REPORT := $(BUILD_DIR)/$(WARNS_EXT)
162 ##############################
163 # Derive include and lib directories
164 ##############################
165 CUDA_INCLUDE_DIR := $(CUDA_DIR)/include
168 # add <cuda>/lib64 only if it exists
169 ifneq ("$(wildcard $(CUDA_DIR)/lib64)","")
170 CUDA_LIB_DIR += $(CUDA_DIR)/lib64
172 CUDA_LIB_DIR += $(CUDA_DIR)/lib
174 INCLUDE_DIRS += $(BUILD_INCLUDE_DIR) ./src ./include
175 ifneq ($(CPU_ONLY), 1)
176 INCLUDE_DIRS += $(CUDA_INCLUDE_DIR)
177 LIBRARY_DIRS += $(CUDA_LIB_DIR)
178 LIBRARIES := cudart cublas curand
181 LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
183 # handle IO dependencies
188 ifeq ($(USE_LEVELDB), 1)
189 LIBRARIES += leveldb snappy
191 ifeq ($(USE_LMDB), 1)
194 ifeq ($(USE_OPENCV), 1)
195 LIBRARIES += opencv_core opencv_highgui opencv_imgproc
197 ifeq ($(OPENCV_VERSION), 3)
198 LIBRARIES += opencv_imgcodecs
202 PYTHON_LIBRARIES ?= boost_python python2.7
203 WARNINGS := -Wall -Wno-sign-compare
205 ##############################
206 # Set build directories
207 ##############################
209 DISTRIBUTE_DIR ?= distribute
210 DISTRIBUTE_SUBDIRS := $(DISTRIBUTE_DIR)/bin $(DISTRIBUTE_DIR)/lib
212 ifneq ($(strip $(DISTRIBUTE_DIR)),distribute)
213 DIST_ALIASES += distribute
216 ALL_BUILD_DIRS := $(sort $(BUILD_DIR) $(addprefix $(BUILD_DIR)/, $(SRC_DIRS)) \
217 $(addprefix $(BUILD_DIR)/cuda/, $(SRC_DIRS)) \
218 $(LIB_BUILD_DIR) $(TEST_BIN_DIR) $(PY_PROTO_BUILD_DIR) $(LINT_OUTPUT_DIR) \
219 $(DISTRIBUTE_SUBDIRS) $(PROTO_BUILD_INCLUDE_DIR))
221 ##############################
222 # Set directory for Doxygen-generated documentation
223 ##############################
224 DOXYGEN_CONFIG_FILE ?= ./.Doxyfile
225 # should be the same as OUTPUT_DIRECTORY in the .Doxyfile
226 DOXYGEN_OUTPUT_DIR ?= ./doxygen
227 DOXYGEN_COMMAND ?= doxygen
228 # All the files that might have Doxygen documentation.
229 DOXYGEN_SOURCES := $(shell find \
236 -name "*.cpp" -or -name "*.hpp" -or -name "*.cu" -or -name "*.cuh" -or \
237 -name "*.py" -or -name "*.m")
238 DOXYGEN_SOURCES += $(DOXYGEN_CONFIG_FILE)
241 ##############################
243 ##############################
246 UNAME := $(shell uname -s)
247 ifeq ($(UNAME), Linux)
249 else ifeq ($(UNAME), Darwin)
256 GCCVERSION := $(shell $(CXX) -dumpversion | cut -f1,2 -d.)
257 # older versions of gcc are too dumb to build boost with -Wuninitalized
258 ifeq ($(shell echo | awk '{exit $(GCCVERSION) < 4.6;}'), 1)
259 WARNINGS += -Wno-uninitialized
261 # boost::thread is reasonably called boost_thread (compare OS X)
262 # We will also explicitly add stdc++ to the link target.
263 LIBRARIES += boost_thread stdc++
264 VERSIONFLAGS += -Wl,-soname,$(DYNAMIC_VERSIONED_NAME_SHORT) -Wl,-rpath,$(ORIGIN)/../lib
268 # clang++ instead of g++
269 # libstdc++ for NVCC compatibility on OS X >= 10.9 with CUDA < 7.0
271 CXX := /usr/bin/clang++
272 ifneq ($(CPU_ONLY), 1)
273 CUDA_VERSION := $(shell $(CUDA_DIR)/bin/nvcc -V | grep -o 'release \d' | grep -o '\d')
274 ifeq ($(shell echo | awk '{exit $(CUDA_VERSION) < 7.0;}'), 1)
275 CXXFLAGS += -stdlib=libstdc++
276 LINKFLAGS += -stdlib=libstdc++
278 # clang throws this warning for cuda headers
279 WARNINGS += -Wno-unneeded-internal-declaration
281 # gtest needs to use its own tuple to not conflict with clang
282 COMMON_FLAGS += -DGTEST_USE_OWN_TR1_TUPLE=1
283 # boost::thread is called boost_thread-mt to mark multithreading on OS X
284 LIBRARIES += boost_thread-mt
285 # we need to explicitly ask for the rpath to be obeyed
286 DYNAMIC_FLAGS := -install_name @rpath/libcaffe.so
287 ORIGIN := @loader_path
288 VERSIONFLAGS += -Wl,-install_name,$(DYNAMIC_VERSIONED_NAME_SHORT) -Wl,-rpath,$(ORIGIN)/../../build/lib
299 ifneq (,$(findstring clang++,$(CXX)))
300 STATIC_LINK_COMMAND := -Wl,-force_load $(STATIC_NAME)
301 else ifneq (,$(findstring g++,$(CXX)))
302 STATIC_LINK_COMMAND := -Wl,--whole-archive $(STATIC_NAME) -Wl,--no-whole-archive
304 # The following line must not be indented with a tab, since we are not inside a target
305 $(error Cannot static link with the $(CXX) compiler)
310 COMMON_FLAGS += -DDEBUG -g -O0
313 COMMON_FLAGS += -DNDEBUG -O2
316 # cuDNN acceleration configuration.
317 ifeq ($(USE_CUDNN), 1)
319 COMMON_FLAGS += -DUSE_CUDNN
322 # configure IO libraries
323 ifeq ($(USE_OPENCV), 1)
324 COMMON_FLAGS += -DUSE_OPENCV
326 ifeq ($(USE_LEVELDB), 1)
327 COMMON_FLAGS += -DUSE_LEVELDB
329 ifeq ($(USE_LMDB), 1)
330 COMMON_FLAGS += -DUSE_LMDB
331 ifeq ($(ALLOW_LMDB_NOLOCK), 1)
332 COMMON_FLAGS += -DALLOW_LMDB_NOLOCK
336 # CPU-only configuration
337 ifeq ($(CPU_ONLY), 1)
338 OBJS := $(PROTO_OBJS) $(CXX_OBJS)
339 TEST_OBJS := $(TEST_CXX_OBJS)
340 TEST_BINS := $(TEST_CXX_BINS)
341 ALL_WARNS := $(ALL_CXX_WARNS)
342 TEST_FILTER := --gtest_filter="-*GPU*"
343 COMMON_FLAGS += -DCPU_ONLY
346 # Python layer support
347 ifeq ($(WITH_PYTHON_LAYER), 1)
348 COMMON_FLAGS += -DWITH_PYTHON_LAYER
349 LIBRARIES += $(PYTHON_LIBRARIES)
352 # BLAS configuration (default = ATLAS)
357 COMMON_FLAGS += -DUSE_MKL
358 MKL_DIR ?= /opt/intel/mkl
359 BLAS_INCLUDE ?= $(MKL_DIR)/include
360 BLAS_LIB ?= $(MKL_DIR)/lib $(MKL_DIR)/lib/intel64
361 else ifeq ($(BLAS), open)
363 LIBRARIES += openblas
367 ifeq ($(BLAS), atlas)
368 # Linux simply has cblas and atlas
369 LIBRARIES += cblas atlas
371 else ifeq ($(OSX), 1)
372 # OS X packages atlas as the vecLib framework
374 # 10.10 has accelerate while 10.9 has veclib
375 XCODE_CLT_VER := $(shell pkgutil --pkg-info=com.apple.pkg.CLTools_Executables | grep 'version' | sed 's/[^0-9]*\([0-9]\).*/\1/')
376 XCODE_CLT_GEQ_6 := $(shell [ $(XCODE_CLT_VER) -gt 5 ] && echo 1)
377 ifeq ($(XCODE_CLT_GEQ_6), 1)
378 BLAS_INCLUDE ?= /System/Library/Frameworks/Accelerate.framework/Versions/Current/Frameworks/vecLib.framework/Headers/
379 LDFLAGS += -framework Accelerate
381 BLAS_INCLUDE ?= /System/Library/Frameworks/vecLib.framework/Versions/Current/Headers/
382 LDFLAGS += -framework vecLib
386 INCLUDE_DIRS += $(BLAS_INCLUDE)
387 LIBRARY_DIRS += $(BLAS_LIB)
389 LIBRARY_DIRS += $(LIB_BUILD_DIR)
391 # Automatic dependency generation (nvcc is handled separately)
394 # Complete build flags.
395 COMMON_FLAGS += $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir))
396 CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
397 NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
398 # mex may invoke an older gcc that is too liberal with -Wuninitalized
399 MATLAB_CXXFLAGS := $(CXXFLAGS) -Wno-uninitialized
400 LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
403 ifeq ($(USE_PKG_CONFIG), 1)
404 PKG_CONFIG := $(shell pkg-config opencv --libs)
408 LDFLAGS += $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir)) $(PKG_CONFIG) \
409 $(foreach library,$(LIBRARIES),-l$(library))
410 PYTHON_LDFLAGS := $(LDFLAGS) $(foreach library,$(PYTHON_LIBRARIES),-l$(library))
412 # 'superclean' target recursively* deletes all files ending with an extension
413 # in $(SUPERCLEAN_EXTS) below. This may be useful if you've built older
414 # versions of Caffe that do not place all generated files in a location known
415 # to the 'clean' target.
417 # 'supercleanlist' will list the files to be deleted by make superclean.
419 # * Recursive with the exception that symbolic links are never followed, per the
420 # default behavior of 'find'.
421 SUPERCLEAN_EXTS := .so .a .o .bin .testbin .pb.cc .pb.h _pb2.py .cuo
423 # Set the sub-targets of the 'everything' target.
424 EVERYTHING_TARGETS := all py$(PROJECT) test warn lint
425 # Only build matcaffe as part of "everything" if MATLAB_DIR is specified.
426 ifneq ($(MATLAB_DIR),)
427 EVERYTHING_TARGETS += mat$(PROJECT)
430 ##############################
431 # Define build targets
432 ##############################
433 .PHONY: all lib test clean docs linecount lint lintclean tools examples $(DIST_ALIASES) \
434 py mat py$(PROJECT) mat$(PROJECT) proto runtest \
435 superclean supercleanlist supercleanfiles warn everything
437 all: lib tools examples
439 lib: $(STATIC_NAME) $(DYNAMIC_NAME)
441 everything: $(EVERYTHING_TARGETS)
444 cloc --read-lang-def=$(PROJECT).cloc \
445 src/$(PROJECT) include/$(PROJECT) tools examples \
448 lint: $(EMPTY_LINT_REPORT)
451 @ $(RM) -r $(LINT_OUTPUT_DIR) $(EMPTY_LINT_REPORT) $(NONEMPTY_LINT_REPORT)
453 docs: $(DOXYGEN_OUTPUT_DIR)
454 @ cd ./docs ; ln -sfn ../$(DOXYGEN_OUTPUT_DIR)/html doxygen
456 $(DOXYGEN_OUTPUT_DIR): $(DOXYGEN_CONFIG_FILE) $(DOXYGEN_SOURCES)
457 $(DOXYGEN_COMMAND) $(DOXYGEN_CONFIG_FILE)
459 $(EMPTY_LINT_REPORT): $(LINT_OUTPUTS) | $(BUILD_DIR)
460 @ cat $(LINT_OUTPUTS) > $@
461 @ if [ -s "$@" ]; then \
463 mv $@ $(NONEMPTY_LINT_REPORT); \
464 echo "Found one or more lint errors."; \
467 $(RM) $(NONEMPTY_LINT_REPORT); \
468 echo "No lint errors!";
470 $(LINT_OUTPUTS): $(LINT_OUTPUT_DIR)/%.lint.txt : % $(LINT_SCRIPT) | $(LINT_OUTPUT_DIR)
472 @ python $(LINT_SCRIPT) $< 2>&1 \
473 | grep -v "^Done processing " \
474 | grep -v "^Total errors found: 0" \
478 test: $(TEST_ALL_BIN) $(TEST_ALL_DYNLINK_BIN) $(TEST_BINS)
480 tools: $(TOOL_BINS) $(TOOL_BIN_LINKS)
482 examples: $(EXAMPLE_BINS)
486 py: $(PY$(PROJECT)_SO) $(PROTO_GEN_PY)
488 $(PY$(PROJECT)_SO): $(PY$(PROJECT)_SRC) $(PY$(PROJECT)_HXX) | $(DYNAMIC_NAME)
489 @ echo CXX/LD -o $@ $<
490 $(Q)$(CXX) -shared -o $@ $(PY$(PROJECT)_SRC) \
491 -o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(PYTHON_LDFLAGS) \
492 -Wl,-rpath,$(ORIGIN)/../../build/lib
496 mat: $(MAT$(PROJECT)_SO)
498 $(MAT$(PROJECT)_SO): $(MAT$(PROJECT)_SRC) $(STATIC_NAME)
499 @ if [ -z "$(MATLAB_DIR)" ]; then \
500 echo "MATLAB_DIR must be specified in $(CONFIG_FILE)" \
501 "to build mat$(PROJECT)."; \
505 $(Q)$(MATLAB_DIR)/bin/mex $(MAT$(PROJECT)_SRC) \
507 CXXFLAGS="\$$CXXFLAGS $(MATLAB_CXXFLAGS)" \
508 CXXLIBS="\$$CXXLIBS $(STATIC_LINK_COMMAND) $(LDFLAGS)" -output $@
509 @ if [ -f "$(PROJECT)_.d" ]; then \
510 mv -f $(PROJECT)_.d $(BUILD_DIR)/${MAT$(PROJECT)_SO:.$(MAT_SO_EXT)=.d}; \
513 runtest: $(TEST_ALL_BIN)
514 $(TOOL_BUILD_DIR)/caffe
515 $(TEST_ALL_BIN) $(TEST_GPUID) --gtest_shuffle $(TEST_FILTER)
518 cd python; python -m unittest discover -s caffe/test
521 cd matlab; $(MATLAB_DIR)/bin/matlab -nodisplay -r 'caffe.run_tests(), exit()'
523 warn: $(EMPTY_WARN_REPORT)
525 $(EMPTY_WARN_REPORT): $(ALL_WARNS) | $(BUILD_DIR)
526 @ cat $(ALL_WARNS) > $@
527 @ if [ -s "$@" ]; then \
529 mv $@ $(NONEMPTY_WARN_REPORT); \
530 echo "Compiler produced one or more warnings."; \
533 $(RM) $(NONEMPTY_WARN_REPORT); \
534 echo "No compiler warnings!";
536 $(ALL_WARNS): %.o.$(WARNS_EXT) : %.o
538 $(BUILD_DIR_LINK): $(BUILD_DIR)/.linked
540 # Create a target ".linked" in this BUILD_DIR to tell Make that the "build" link
541 # is currently correct, then delete the one in the OTHER_BUILD_DIR in case it
542 # exists and $(DEBUG) is toggled later.
543 $(BUILD_DIR)/.linked:
544 @ mkdir -p $(BUILD_DIR)
545 @ $(RM) $(OTHER_BUILD_DIR)/.linked
546 @ $(RM) -r $(BUILD_DIR_LINK)
547 @ ln -s $(BUILD_DIR) $(BUILD_DIR_LINK)
550 $(ALL_BUILD_DIRS): | $(BUILD_DIR_LINK)
553 $(DYNAMIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
555 $(Q)$(CXX) -shared -o $@ $(OBJS) $(VERSIONFLAGS) $(LINKFLAGS) $(LDFLAGS) $(DYNAMIC_FLAGS)
556 @ cd $(BUILD_DIR)/lib; rm -f $(DYNAMIC_NAME_SHORT); ln -s $(DYNAMIC_VERSIONED_NAME_SHORT) $(DYNAMIC_NAME_SHORT)
558 $(STATIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
560 $(Q)ar rcs $@ $(OBJS)
562 $(BUILD_DIR)/%.o: %.cpp | $(ALL_BUILD_DIRS)
564 $(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
565 || (cat $@.$(WARNS_EXT); exit 1)
566 @ cat $@.$(WARNS_EXT)
568 $(PROTO_BUILD_DIR)/%.pb.o: $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_GEN_HEADER) \
571 $(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
572 || (cat $@.$(WARNS_EXT); exit 1)
573 @ cat $@.$(WARNS_EXT)
575 $(BUILD_DIR)/cuda/%.o: %.cu | $(ALL_BUILD_DIRS)
577 $(Q)$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -M $< -o ${@:.o=.d} \
579 $(Q)$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -c $< -o $@ 2> $@.$(WARNS_EXT) \
580 || (cat $@.$(WARNS_EXT); exit 1)
581 @ cat $@.$(WARNS_EXT)
583 $(TEST_ALL_BIN): $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) \
584 | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
585 @ echo CXX/LD -o $@ $<
586 $(Q)$(CXX) $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) \
587 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib
589 $(TEST_CU_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CU_BUILD_DIR)/%.o \
590 $(GTEST_OBJ) | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
592 $(Q)$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) \
593 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib
595 $(TEST_CXX_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CXX_BUILD_DIR)/%.o \
596 $(GTEST_OBJ) | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
598 $(Q)$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) \
599 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib
601 # Target for extension-less symlinks to tool binaries with extension '*.bin'.
602 $(TOOL_BUILD_DIR)/%: $(TOOL_BUILD_DIR)/%.bin | $(TOOL_BUILD_DIR)
604 @ ln -s $(abspath $<) $@
606 $(TOOL_BINS): %.bin : %.o | $(DYNAMIC_NAME)
608 $(Q)$(CXX) $< -o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(LDFLAGS) \
609 -Wl,-rpath,$(ORIGIN)/../lib
611 $(EXAMPLE_BINS): %.bin : %.o | $(DYNAMIC_NAME)
613 $(Q)$(CXX) $< -o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(LDFLAGS) \
614 -Wl,-rpath,$(ORIGIN)/../../lib
616 proto: $(PROTO_GEN_CC) $(PROTO_GEN_HEADER)
618 $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_BUILD_DIR)/%.pb.h : \
619 $(PROTO_SRC_DIR)/%.proto | $(PROTO_BUILD_DIR)
621 $(Q)protoc --proto_path=$(PROTO_SRC_DIR) --cpp_out=$(PROTO_BUILD_DIR) $<
623 $(PY_PROTO_BUILD_DIR)/%_pb2.py : $(PROTO_SRC_DIR)/%.proto \
624 $(PY_PROTO_INIT) | $(PY_PROTO_BUILD_DIR)
625 @ echo PROTOC \(python\) $<
626 $(Q)protoc --proto_path=$(PROTO_SRC_DIR) --python_out=$(PY_PROTO_BUILD_DIR) $<
628 $(PY_PROTO_INIT): | $(PY_PROTO_BUILD_DIR)
629 touch $(PY_PROTO_INIT)
632 @- $(RM) -rf $(ALL_BUILD_DIRS)
633 @- $(RM) -rf $(OTHER_BUILD_DIR)
634 @- $(RM) -rf $(BUILD_DIR_LINK)
635 @- $(RM) -rf $(DISTRIBUTE_DIR)
636 @- $(RM) $(PY$(PROJECT)_SO)
637 @- $(RM) $(MAT$(PROJECT)_SO)
640 $(eval SUPERCLEAN_FILES := $(strip \
641 $(foreach ext,$(SUPERCLEAN_EXTS), $(shell find . -name '*$(ext)' \
642 -not -path './data/*'))))
644 supercleanlist: supercleanfiles
646 if [ -z "$(SUPERCLEAN_FILES)" ]; then \
647 echo "No generated files found."; \
649 echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \
652 superclean: clean supercleanfiles
654 if [ -z "$(SUPERCLEAN_FILES)" ]; then \
655 echo "No generated files found."; \
657 echo "Deleting the following generated files:"; \
658 echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \
659 $(RM) $(SUPERCLEAN_FILES); \
662 $(DIST_ALIASES): $(DISTRIBUTE_DIR)
664 $(DISTRIBUTE_DIR): all py | $(DISTRIBUTE_SUBDIRS)
666 cp -r include $(DISTRIBUTE_DIR)/
667 mkdir -p $(DISTRIBUTE_DIR)/include/caffe/proto
668 cp $(PROTO_GEN_HEADER_SRCS) $(DISTRIBUTE_DIR)/include/caffe/proto
669 # add tool and example binaries
670 cp $(TOOL_BINS) $(DISTRIBUTE_DIR)/bin
671 cp $(EXAMPLE_BINS) $(DISTRIBUTE_DIR)/bin
673 cp $(STATIC_NAME) $(DISTRIBUTE_DIR)/lib
674 install -m 644 $(DYNAMIC_NAME) $(DISTRIBUTE_DIR)/lib
675 cd $(DISTRIBUTE_DIR)/lib; rm -f $(DYNAMIC_NAME_SHORT); ln -s $(DYNAMIC_VERSIONED_NAME_SHORT) $(DYNAMIC_NAME_SHORT)
676 # add python - it's not the standard way, indeed...
677 cp -r python $(DISTRIBUTE_DIR)/python