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