ba60ae97934536ff80958f2731fe8b95bae9a851
[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 # BLAS configuration (default = ATLAS)
292 BLAS ?= atlas
293 ifeq ($(BLAS), mkl)
294         # MKL
295         LIBRARIES += mkl_rt
296         COMMON_FLAGS += -DUSE_MKL
297         MKL_DIR ?= /opt/intel/mkl
298         BLAS_INCLUDE ?= $(MKL_DIR)/include
299         BLAS_LIB ?= $(MKL_DIR)/lib $(MKL_DIR)/lib/intel64
300 else ifeq ($(BLAS), open)
301         # OpenBLAS
302         LIBRARIES += openblas
303 else
304         # ATLAS
305         ifeq ($(LINUX), 1)
306                 ifeq ($(BLAS), atlas)
307                         # Linux simply has cblas and atlas
308                         LIBRARIES += cblas atlas
309                 endif
310         else ifeq ($(OSX), 1)
311                 # OS X packages atlas as the vecLib framework
312                 LIBRARIES += cblas
313                 # 10.10 has accelerate while 10.9 has veclib
314                 XCODE_CLT_VER := $(shell pkgutil --pkg-info=com.apple.pkg.CLTools_Executables | grep -o 'version: 6')
315                 ifneq (,$(findstring version: 6,$(XCODE_CLT_VER)))
316                         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/
317                         LDFLAGS += -framework Accelerate
318                 else
319                         BLAS_INCLUDE ?= /System/Library/Frameworks/vecLib.framework/Versions/Current/Headers/
320                         LDFLAGS += -framework vecLib
321                 endif
322         endif
323 endif
324 INCLUDE_DIRS += $(BLAS_INCLUDE)
325 LIBRARY_DIRS += $(BLAS_LIB)
326
327 LIBRARY_DIRS += $(LIB_BUILD_DIR)
328
329 # Automatic dependency generation (nvcc is handled separately)
330 CXXFLAGS += -MMD -MP
331
332 # Complete build flags.
333 COMMON_FLAGS += $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir))
334 CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
335 NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
336 # mex may invoke an older gcc that is too liberal with -Wuninitalized
337 MATLAB_CXXFLAGS := $(CXXFLAGS) -Wno-uninitialized
338 LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
339
340 USE_PKG_CONFIG ?= 0
341 ifeq ($(USE_PKG_CONFIG), 1)
342         PKG_CONFIG := $(shell pkg-config opencv --libs)
343 else
344         PKG_CONFIG :=
345 endif
346 LDFLAGS += $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir)) $(PKG_CONFIG) \
347                 $(foreach library,$(LIBRARIES),-l$(library))
348 PYTHON_LDFLAGS := $(LDFLAGS) $(foreach library,$(PYTHON_LIBRARIES),-l$(library))
349
350 # 'superclean' target recursively* deletes all files ending with an extension
351 # in $(SUPERCLEAN_EXTS) below.  This may be useful if you've built older
352 # versions of Caffe that do not place all generated files in a location known
353 # to the 'clean' target.
354 #
355 # 'supercleanlist' will list the files to be deleted by make superclean.
356 #
357 # * Recursive with the exception that symbolic links are never followed, per the
358 # default behavior of 'find'.
359 SUPERCLEAN_EXTS := .so .a .o .bin .testbin .pb.cc .pb.h _pb2.py .cuo
360
361 # Set the sub-targets of the 'everything' target.
362 EVERYTHING_TARGETS := all py$(PROJECT) test warn lint
363 # Only build matcaffe as part of "everything" if MATLAB_DIR is specified.
364 ifneq ($(MATLAB_DIR),)
365         EVERYTHING_TARGETS += mat$(PROJECT)
366 endif
367
368 ##############################
369 # Define build targets
370 ##############################
371 .PHONY: all test clean docs linecount lint lintclean tools examples $(DIST_ALIASES) \
372         py mat py$(PROJECT) mat$(PROJECT) proto runtest \
373         superclean supercleanlist supercleanfiles warn everything
374
375 all: $(STATIC_NAME) $(DYNAMIC_NAME) tools examples
376
377 everything: $(EVERYTHING_TARGETS)
378
379 linecount:
380         cloc --read-lang-def=$(PROJECT).cloc \
381                 src/$(PROJECT) include/$(PROJECT) tools examples \
382                 python matlab
383
384 lint: $(EMPTY_LINT_REPORT)
385
386 lintclean:
387         @ $(RM) -r $(LINT_OUTPUT_DIR) $(EMPTY_LINT_REPORT) $(NONEMPTY_LINT_REPORT)
388
389 docs: $(DOXYGEN_OUTPUT_DIR)
390         @ cd ./docs ; ln -sfn ../$(DOXYGEN_OUTPUT_DIR)/html doxygen
391
392 $(DOXYGEN_OUTPUT_DIR): $(DOXYGEN_CONFIG_FILE) $(DOXYGEN_SOURCES)
393         $(DOXYGEN_COMMAND) $(DOXYGEN_CONFIG_FILE)
394
395 $(EMPTY_LINT_REPORT): $(LINT_OUTPUTS) | $(BUILD_DIR)
396         @ cat $(LINT_OUTPUTS) > $@
397         @ if [ -s "$@" ]; then \
398                 cat $@; \
399                 mv $@ $(NONEMPTY_LINT_REPORT); \
400                 echo "Found one or more lint errors."; \
401                 exit 1; \
402           fi; \
403           $(RM) $(NONEMPTY_LINT_REPORT); \
404           echo "No lint errors!";
405
406 $(LINT_OUTPUTS): $(LINT_OUTPUT_DIR)/%.lint.txt : % $(LINT_SCRIPT) | $(LINT_OUTPUT_DIR)
407         @ mkdir -p $(dir $@)
408         @ python $(LINT_SCRIPT) $< 2>&1 \
409                 | grep -v "^Done processing " \
410                 | grep -v "^Total errors found: 0" \
411                 > $@ \
412                 || true
413
414 test: $(TEST_ALL_BIN) $(TEST_ALL_DYNLINK_BIN) $(TEST_BINS)
415
416 tools: $(TOOL_BINS) $(TOOL_BIN_LINKS)
417
418 examples: $(EXAMPLE_BINS)
419
420 py$(PROJECT): py
421
422 py: $(PY$(PROJECT)_SO) $(PROTO_GEN_PY)
423
424 $(PY$(PROJECT)_SO): $(PY$(PROJECT)_SRC) $(PY$(PROJECT)_HXX) | $(DYNAMIC_NAME)
425         @ echo CXX/LD -o $@ $<
426         $(Q)$(CXX) -shared -o $@ $(PY$(PROJECT)_SRC) \
427                 -o $@ $(LINKFLAGS) $(PYTHON_LDFLAGS) -l$(PROJECT) \
428                 -Wl,-rpath,$(ORIGIN)/../../build/lib
429
430 mat$(PROJECT): mat
431
432 mat: $(MAT$(PROJECT)_SO)
433
434 $(MAT$(PROJECT)_SO): $(MAT$(PROJECT)_SRC) $(STATIC_NAME)
435         @ if [ -z "$(MATLAB_DIR)" ]; then \
436                 echo "MATLAB_DIR must be specified in $(CONFIG_FILE)" \
437                         "to build mat$(PROJECT)."; \
438                 exit 1; \
439         fi
440         @ echo MEX $<
441         $(Q)$(MATLAB_DIR)/bin/mex $(MAT$(PROJECT)_SRC) \
442                         CXX="$(CXX)" \
443                         CXXFLAGS="\$$CXXFLAGS $(MATLAB_CXXFLAGS)" \
444                         CXXLIBS="\$$CXXLIBS $(STATIC_LINK_COMMAND) $(LDFLAGS)" -output $@
445
446 runtest: $(TEST_ALL_BIN)
447         $(TEST_ALL_BIN) $(TEST_GPUID) --gtest_shuffle $(TEST_FILTER)
448
449 pytest: py
450         cd python; python -m unittest discover -s caffe/test
451
452 warn: $(EMPTY_WARN_REPORT)
453
454 $(EMPTY_WARN_REPORT): $(ALL_WARNS) | $(BUILD_DIR)
455         @ cat $(ALL_WARNS) > $@
456         @ if [ -s "$@" ]; then \
457                 cat $@; \
458                 mv $@ $(NONEMPTY_WARN_REPORT); \
459                 echo "Compiler produced one or more warnings."; \
460                 exit 1; \
461           fi; \
462           $(RM) $(NONEMPTY_WARN_REPORT); \
463           echo "No compiler warnings!";
464
465 $(ALL_WARNS): %.o.$(WARNS_EXT) : %.o
466
467 $(BUILD_DIR_LINK): $(BUILD_DIR)/.linked
468
469 # Create a target ".linked" in this BUILD_DIR to tell Make that the "build" link
470 # is currently correct, then delete the one in the OTHER_BUILD_DIR in case it
471 # exists and $(DEBUG) is toggled later.
472 $(BUILD_DIR)/.linked:
473         @ mkdir -p $(BUILD_DIR)
474         @ $(RM) $(OTHER_BUILD_DIR)/.linked
475         @ $(RM) -r $(BUILD_DIR_LINK)
476         @ ln -s $(BUILD_DIR) $(BUILD_DIR_LINK)
477         @ touch $@
478
479 $(ALL_BUILD_DIRS): | $(BUILD_DIR_LINK)
480         @ mkdir -p $@
481
482 $(DYNAMIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
483         @ echo LD -o $@
484         $(Q)$(CXX) -shared -o $@ $(OBJS) $(LINKFLAGS) $(LDFLAGS) $(DYNAMIC_FLAGS)
485
486 $(STATIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
487         @ echo AR -o $@
488         $(Q)ar rcs $@ $(OBJS)
489
490 $(BUILD_DIR)/%.o: %.cpp | $(ALL_BUILD_DIRS)
491         @ echo CXX $<
492         $(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
493                 || (cat $@.$(WARNS_EXT); exit 1)
494         @ cat $@.$(WARNS_EXT)
495
496 $(PROTO_BUILD_DIR)/%.pb.o: $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_GEN_HEADER) \
497                 | $(PROTO_BUILD_DIR)
498         @ echo CXX $<
499         $(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
500                 || (cat $@.$(WARNS_EXT); exit 1)
501         @ cat $@.$(WARNS_EXT)
502
503 $(BUILD_DIR)/cuda/%.o: %.cu | $(ALL_BUILD_DIRS)
504         @ echo NVCC $<
505         $(Q)$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -M $< -o ${@:.o=.d} \
506                 -odir $(@D)
507         $(Q)$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -c $< -o $@ 2> $@.$(WARNS_EXT) \
508                 || (cat $@.$(WARNS_EXT); exit 1)
509         @ cat $@.$(WARNS_EXT)
510
511 $(TEST_ALL_BIN): $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) \
512                 | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
513         @ echo CXX/LD -o $@ $<
514         $(Q)$(CXX) $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) \
515                 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(PROJECT) -Wl,-rpath,$(ORIGIN)/../lib
516
517 $(TEST_CU_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CU_BUILD_DIR)/%.o \
518         $(GTEST_OBJ) | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
519         @ echo LD $<
520         $(Q)$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) \
521                 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(PROJECT) -Wl,-rpath,$(ORIGIN)/../lib
522
523 $(TEST_CXX_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CXX_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 # Target for extension-less symlinks to tool binaries with extension '*.bin'.
530 $(TOOL_BUILD_DIR)/%: $(TOOL_BUILD_DIR)/%.bin | $(TOOL_BUILD_DIR)
531         @ $(RM) $@
532         @ ln -s $(abspath $<) $@
533
534 $(TOOL_BINS) $(EXAMPLE_BINS): %.bin : %.o | $(DYNAMIC_NAME)
535         @ echo CXX/LD -o $@
536         $(Q)$(CXX) $< -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(PROJECT) \
537                 -Wl,-rpath,$(ORIGIN)/../lib
538
539 proto: $(PROTO_GEN_CC) $(PROTO_GEN_HEADER)
540
541 $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_BUILD_DIR)/%.pb.h : \
542                 $(PROTO_SRC_DIR)/%.proto | $(PROTO_BUILD_DIR)
543         @ echo PROTOC $<
544         $(Q)protoc --proto_path=$(PROTO_SRC_DIR) --cpp_out=$(PROTO_BUILD_DIR) $<
545
546 $(PY_PROTO_BUILD_DIR)/%_pb2.py : $(PROTO_SRC_DIR)/%.proto \
547                 $(PY_PROTO_INIT) | $(PY_PROTO_BUILD_DIR)
548         @ echo PROTOC \(python\) $<
549         $(Q)protoc --proto_path=$(PROTO_SRC_DIR) --python_out=$(PY_PROTO_BUILD_DIR) $<
550
551 $(PY_PROTO_INIT): | $(PY_PROTO_BUILD_DIR)
552         touch $(PY_PROTO_INIT)
553
554 clean:
555         @- $(RM) -rf $(ALL_BUILD_DIRS)
556         @- $(RM) -rf $(OTHER_BUILD_DIR)
557         @- $(RM) -rf $(BUILD_DIR_LINK)
558         @- $(RM) -rf $(DISTRIBUTE_DIR)
559         @- $(RM) $(PY$(PROJECT)_SO)
560         @- $(RM) $(MAT$(PROJECT)_SO)
561
562 supercleanfiles:
563         $(eval SUPERCLEAN_FILES := $(strip \
564                         $(foreach ext,$(SUPERCLEAN_EXTS), $(shell find . -name '*$(ext)' \
565                         -not -path './data/*'))))
566
567 supercleanlist: supercleanfiles
568         @ \
569         if [ -z "$(SUPERCLEAN_FILES)" ]; then \
570                 echo "No generated files found."; \
571         else \
572                 echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \
573         fi
574
575 superclean: clean supercleanfiles
576         @ \
577         if [ -z "$(SUPERCLEAN_FILES)" ]; then \
578                 echo "No generated files found."; \
579         else \
580                 echo "Deleting the following generated files:"; \
581                 echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \
582                 $(RM) $(SUPERCLEAN_FILES); \
583         fi
584
585 $(DIST_ALIASES): $(DISTRIBUTE_DIR)
586
587 $(DISTRIBUTE_DIR): all py | $(DISTRIBUTE_SUBDIRS)
588         # add include
589         cp -r include $(DISTRIBUTE_DIR)/
590         mkdir -p $(DISTRIBUTE_DIR)/include/caffe/proto
591         cp $(PROTO_GEN_HEADER_SRCS) $(DISTRIBUTE_DIR)/include/caffe/proto
592         # add tool and example binaries
593         cp $(TOOL_BINS) $(DISTRIBUTE_DIR)/bin
594         cp $(EXAMPLE_BINS) $(DISTRIBUTE_DIR)/bin
595         # add libraries
596         cp $(STATIC_NAME) $(DISTRIBUTE_DIR)/lib
597         cp $(DYNAMIC_NAME) $(DISTRIBUTE_DIR)/lib
598         # add python - it's not the standard way, indeed...
599         cp -r python $(DISTRIBUTE_DIR)/python
600
601 -include $(DEPS)