Merge pull request #3295 from timmeinhardt/fix_issue_3274
[platform/upstream/caffeonacl.git] / Makefile
1 PROJECT := caffe
2
3 CONFIG_FILE := Makefile.config
4 # Explicitly check for the config file, otherwise make -k will proceed anyway.
5 ifeq ($(wildcard $(CONFIG_FILE)),)
6 $(error $(CONFIG_FILE) not found. See $(CONFIG_FILE).example.)
7 endif
8 include $(CONFIG_FILE)
9
10 BUILD_DIR_LINK := $(BUILD_DIR)
11 ifeq ($(RELEASE_BUILD_DIR),)
12         RELEASE_BUILD_DIR := .$(BUILD_DIR)_release
13 endif
14 ifeq ($(DEBUG_BUILD_DIR),)
15         DEBUG_BUILD_DIR := .$(BUILD_DIR)_debug
16 endif
17
18 DEBUG ?= 0
19 ifeq ($(DEBUG), 1)
20         BUILD_DIR := $(DEBUG_BUILD_DIR)
21         OTHER_BUILD_DIR := $(RELEASE_BUILD_DIR)
22 else
23         BUILD_DIR := $(RELEASE_BUILD_DIR)
24         OTHER_BUILD_DIR := $(DEBUG_BUILD_DIR)
25 endif
26
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)
30
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
35
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 \
65         src/$(PROJECT) \
66         include/$(PROJECT) \
67         python/$(PROJECT) \
68         matlab/+$(PROJECT)/private \
69         examples \
70         tools \
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
74 LINT_EXT := lint.txt
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)
86 endif
87 MAT$(PROJECT)_SO := matlab/+$(PROJECT)/private/$(PROJECT)_.$(MAT_SO_EXT)
88
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
136
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)
150
151 EMPTY_WARN_REPORT := $(BUILD_DIR)/.$(WARNS_EXT)
152 NONEMPTY_WARN_REPORT := $(BUILD_DIR)/$(WARNS_EXT)
153
154 ##############################
155 # Derive include and lib directories
156 ##############################
157 CUDA_INCLUDE_DIR := $(CUDA_DIR)/include
158
159 CUDA_LIB_DIR :=
160 # add <cuda>/lib64 only if it exists
161 ifneq ("$(wildcard $(CUDA_DIR)/lib64)","")
162         CUDA_LIB_DIR += $(CUDA_DIR)/lib64
163 endif
164 CUDA_LIB_DIR += $(CUDA_DIR)/lib
165
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
171 endif
172
173 LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
174
175 # handle IO dependencies
176 USE_LEVELDB ?= 1
177 USE_LMDB ?= 1
178 USE_OPENCV ?= 1
179
180 ifeq ($(USE_LEVELDB), 1)
181         LIBRARIES += leveldb snappy
182 endif
183 ifeq ($(USE_LMDB), 1)
184         LIBRARIES += lmdb
185 endif
186 ifeq ($(USE_OPENCV), 1)
187         LIBRARIES += opencv_core opencv_highgui opencv_imgproc 
188
189         ifeq ($(OPENCV_VERSION), 3)
190                 LIBRARIES += opencv_imgcodecs
191         endif
192                 
193 endif
194 PYTHON_LIBRARIES := boost_python python2.7
195 WARNINGS := -Wall -Wno-sign-compare
196
197 ##############################
198 # Set build directories
199 ##############################
200
201 DISTRIBUTE_DIR ?= distribute
202 DISTRIBUTE_SUBDIRS := $(DISTRIBUTE_DIR)/bin $(DISTRIBUTE_DIR)/lib
203 DIST_ALIASES := dist
204 ifneq ($(strip $(DISTRIBUTE_DIR)),distribute)
205                 DIST_ALIASES += distribute
206 endif
207
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))
212
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 \
222         src/$(PROJECT) \
223         include/$(PROJECT) \
224         python/ \
225         matlab/ \
226         examples \
227         tools \
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)
231
232
233 ##############################
234 # Configure build
235 ##############################
236
237 # Determine platform
238 UNAME := $(shell uname -s)
239 ifeq ($(UNAME), Linux)
240         LINUX := 1
241 else ifeq ($(UNAME), Darwin)
242         OSX := 1
243 endif
244
245 # Linux
246 ifeq ($(LINUX), 1)
247         CXX ?= /usr/bin/g++
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
252         endif
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++
256 endif
257
258 # OS X:
259 # clang++ instead of g++
260 # libstdc++ for NVCC compatibility on OS X >= 10.9 with CUDA < 7.0
261 ifeq ($(OSX), 1)
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++
268                 endif
269                 # clang throws this warning for cuda headers
270                 WARNINGS += -Wno-unneeded-internal-declaration
271         endif
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
279 else
280         ORIGIN := \$$ORIGIN
281 endif
282
283 # Custom compiler
284 ifdef CUSTOM_CXX
285         CXX := $(CUSTOM_CXX)
286 endif
287
288 # Static linking
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
293 else
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)
296 endif
297
298 # Debugging
299 ifeq ($(DEBUG), 1)
300         COMMON_FLAGS += -DDEBUG -g -O0
301         NVCCFLAGS += -G
302 else
303         COMMON_FLAGS += -DNDEBUG -O2
304 endif
305
306 # cuDNN acceleration configuration.
307 ifeq ($(USE_CUDNN), 1)
308         LIBRARIES += cudnn
309         COMMON_FLAGS += -DUSE_CUDNN
310 endif
311
312 # configure IO libraries
313 ifeq ($(USE_OPENCV), 1)
314         COMMON_FLAGS += -DUSE_OPENCV
315 endif
316 ifeq ($(USE_LEVELDB), 1)
317         COMMON_FLAGS += -DUSE_LEVELDB
318 endif
319 ifeq ($(USE_LMDB), 1)
320         COMMON_FLAGS += -DUSE_LMDB
321 ifeq ($(ALLOW_LMDB_NOLOCK), 1)
322         COMMON_FLAGS += -DALLOW_LMDB_NOLOCK
323 endif
324 endif
325
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
334 endif
335
336 # Python layer support
337 ifeq ($(WITH_PYTHON_LAYER), 1)
338         COMMON_FLAGS += -DWITH_PYTHON_LAYER
339         LIBRARIES += $(PYTHON_LIBRARIES)
340 endif
341
342 # BLAS configuration (default = ATLAS)
343 BLAS ?= atlas
344 ifeq ($(BLAS), mkl)
345         # MKL
346         LIBRARIES += mkl_rt
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)
352         # OpenBLAS
353         LIBRARIES += openblas
354 else
355         # ATLAS
356         ifeq ($(LINUX), 1)
357                 ifeq ($(BLAS), atlas)
358                         # Linux simply has cblas and atlas
359                         LIBRARIES += cblas atlas
360                 endif
361         else ifeq ($(OSX), 1)
362                 # OS X packages atlas as the vecLib framework
363                 LIBRARIES += cblas
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
370                 else
371                         BLAS_INCLUDE ?= /System/Library/Frameworks/vecLib.framework/Versions/Current/Headers/
372                         LDFLAGS += -framework vecLib
373                 endif
374         endif
375 endif
376 INCLUDE_DIRS += $(BLAS_INCLUDE)
377 LIBRARY_DIRS += $(BLAS_LIB)
378
379 LIBRARY_DIRS += $(LIB_BUILD_DIR)
380
381 # Automatic dependency generation (nvcc is handled separately)
382 CXXFLAGS += -MMD -MP
383
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)
391
392 USE_PKG_CONFIG ?= 0
393 ifeq ($(USE_PKG_CONFIG), 1)
394         PKG_CONFIG := $(shell pkg-config opencv --libs)
395 else
396         PKG_CONFIG :=
397 endif
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))
401
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.
406 #
407 # 'supercleanlist' will list the files to be deleted by make superclean.
408 #
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
412
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)
418 endif
419
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
426
427 all: lib tools examples
428
429 lib: $(STATIC_NAME) $(DYNAMIC_NAME)
430
431 everything: $(EVERYTHING_TARGETS)
432
433 linecount:
434         cloc --read-lang-def=$(PROJECT).cloc \
435                 src/$(PROJECT) include/$(PROJECT) tools examples \
436                 python matlab
437
438 lint: $(EMPTY_LINT_REPORT)
439
440 lintclean:
441         @ $(RM) -r $(LINT_OUTPUT_DIR) $(EMPTY_LINT_REPORT) $(NONEMPTY_LINT_REPORT)
442
443 docs: $(DOXYGEN_OUTPUT_DIR)
444         @ cd ./docs ; ln -sfn ../$(DOXYGEN_OUTPUT_DIR)/html doxygen
445
446 $(DOXYGEN_OUTPUT_DIR): $(DOXYGEN_CONFIG_FILE) $(DOXYGEN_SOURCES)
447         $(DOXYGEN_COMMAND) $(DOXYGEN_CONFIG_FILE)
448
449 $(EMPTY_LINT_REPORT): $(LINT_OUTPUTS) | $(BUILD_DIR)
450         @ cat $(LINT_OUTPUTS) > $@
451         @ if [ -s "$@" ]; then \
452                 cat $@; \
453                 mv $@ $(NONEMPTY_LINT_REPORT); \
454                 echo "Found one or more lint errors."; \
455                 exit 1; \
456           fi; \
457           $(RM) $(NONEMPTY_LINT_REPORT); \
458           echo "No lint errors!";
459
460 $(LINT_OUTPUTS): $(LINT_OUTPUT_DIR)/%.lint.txt : % $(LINT_SCRIPT) | $(LINT_OUTPUT_DIR)
461         @ mkdir -p $(dir $@)
462         @ python $(LINT_SCRIPT) $< 2>&1 \
463                 | grep -v "^Done processing " \
464                 | grep -v "^Total errors found: 0" \
465                 > $@ \
466                 || true
467
468 test: $(TEST_ALL_BIN) $(TEST_ALL_DYNLINK_BIN) $(TEST_BINS)
469
470 tools: $(TOOL_BINS) $(TOOL_BIN_LINKS)
471
472 examples: $(EXAMPLE_BINS)
473
474 py$(PROJECT): py
475
476 py: $(PY$(PROJECT)_SO) $(PROTO_GEN_PY)
477
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
483
484 mat$(PROJECT): mat
485
486 mat: $(MAT$(PROJECT)_SO)
487
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)."; \
492                 exit 1; \
493         fi
494         @ echo MEX $<
495         $(Q)$(MATLAB_DIR)/bin/mex $(MAT$(PROJECT)_SRC) \
496                         CXX="$(CXX)" \
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}; \
501         fi
502
503 runtest: $(TEST_ALL_BIN)
504         $(TOOL_BUILD_DIR)/caffe
505         $(TEST_ALL_BIN) $(TEST_GPUID) --gtest_shuffle $(TEST_FILTER)
506
507 pytest: py
508         cd python; python -m unittest discover -s caffe/test
509
510 mattest: mat
511         cd matlab; $(MATLAB_DIR)/bin/matlab -nodisplay -r 'caffe.run_tests(), exit()'
512
513 warn: $(EMPTY_WARN_REPORT)
514
515 $(EMPTY_WARN_REPORT): $(ALL_WARNS) | $(BUILD_DIR)
516         @ cat $(ALL_WARNS) > $@
517         @ if [ -s "$@" ]; then \
518                 cat $@; \
519                 mv $@ $(NONEMPTY_WARN_REPORT); \
520                 echo "Compiler produced one or more warnings."; \
521                 exit 1; \
522           fi; \
523           $(RM) $(NONEMPTY_WARN_REPORT); \
524           echo "No compiler warnings!";
525
526 $(ALL_WARNS): %.o.$(WARNS_EXT) : %.o
527
528 $(BUILD_DIR_LINK): $(BUILD_DIR)/.linked
529
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)
538         @ touch $@
539
540 $(ALL_BUILD_DIRS): | $(BUILD_DIR_LINK)
541         @ mkdir -p $@
542
543 $(DYNAMIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
544         @ echo LD -o $@
545         $(Q)$(CXX) -shared -o $@ $(OBJS) $(LINKFLAGS) $(LDFLAGS) $(DYNAMIC_FLAGS)
546
547 $(STATIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
548         @ echo AR -o $@
549         $(Q)ar rcs $@ $(OBJS)
550
551 $(BUILD_DIR)/%.o: %.cpp | $(ALL_BUILD_DIRS)
552         @ echo CXX $<
553         $(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
554                 || (cat $@.$(WARNS_EXT); exit 1)
555         @ cat $@.$(WARNS_EXT)
556
557 $(PROTO_BUILD_DIR)/%.pb.o: $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_GEN_HEADER) \
558                 | $(PROTO_BUILD_DIR)
559         @ echo CXX $<
560         $(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
561                 || (cat $@.$(WARNS_EXT); exit 1)
562         @ cat $@.$(WARNS_EXT)
563
564 $(BUILD_DIR)/cuda/%.o: %.cu | $(ALL_BUILD_DIRS)
565         @ echo NVCC $<
566         $(Q)$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -M $< -o ${@:.o=.d} \
567                 -odir $(@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)
571
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
577
578 $(TEST_CU_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CU_BUILD_DIR)/%.o \
579         $(GTEST_OBJ) | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
580         @ echo LD $<
581         $(Q)$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) \
582                 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(PROJECT) -Wl,-rpath,$(ORIGIN)/../lib
583
584 $(TEST_CXX_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CXX_BUILD_DIR)/%.o \
585         $(GTEST_OBJ) | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
586         @ echo LD $<
587         $(Q)$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) \
588                 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(PROJECT) -Wl,-rpath,$(ORIGIN)/../lib
589
590 # Target for extension-less symlinks to tool binaries with extension '*.bin'.
591 $(TOOL_BUILD_DIR)/%: $(TOOL_BUILD_DIR)/%.bin | $(TOOL_BUILD_DIR)
592         @ $(RM) $@
593         @ ln -s $(abspath $<) $@
594
595 $(TOOL_BINS): %.bin : %.o | $(DYNAMIC_NAME)
596         @ echo CXX/LD -o $@
597         $(Q)$(CXX) $< -o $@ $(LINKFLAGS) -l$(PROJECT) $(LDFLAGS) \
598                 -Wl,-rpath,$(ORIGIN)/../lib
599
600 $(EXAMPLE_BINS): %.bin : %.o | $(DYNAMIC_NAME)
601         @ echo CXX/LD -o $@
602         $(Q)$(CXX) $< -o $@ $(LINKFLAGS) -l$(PROJECT) $(LDFLAGS) \
603                 -Wl,-rpath,$(ORIGIN)/../../lib
604
605 proto: $(PROTO_GEN_CC) $(PROTO_GEN_HEADER)
606
607 $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_BUILD_DIR)/%.pb.h : \
608                 $(PROTO_SRC_DIR)/%.proto | $(PROTO_BUILD_DIR)
609         @ echo PROTOC $<
610         $(Q)protoc --proto_path=$(PROTO_SRC_DIR) --cpp_out=$(PROTO_BUILD_DIR) $<
611
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) $<
616
617 $(PY_PROTO_INIT): | $(PY_PROTO_BUILD_DIR)
618         touch $(PY_PROTO_INIT)
619
620 clean:
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)
627
628 supercleanfiles:
629         $(eval SUPERCLEAN_FILES := $(strip \
630                         $(foreach ext,$(SUPERCLEAN_EXTS), $(shell find . -name '*$(ext)' \
631                         -not -path './data/*'))))
632
633 supercleanlist: supercleanfiles
634         @ \
635         if [ -z "$(SUPERCLEAN_FILES)" ]; then \
636                 echo "No generated files found."; \
637         else \
638                 echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \
639         fi
640
641 superclean: clean supercleanfiles
642         @ \
643         if [ -z "$(SUPERCLEAN_FILES)" ]; then \
644                 echo "No generated files found."; \
645         else \
646                 echo "Deleting the following generated files:"; \
647                 echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \
648                 $(RM) $(SUPERCLEAN_FILES); \
649         fi
650
651 $(DIST_ALIASES): $(DISTRIBUTE_DIR)
652
653 $(DISTRIBUTE_DIR): all py | $(DISTRIBUTE_SUBDIRS)
654         # add include
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
661         # add libraries
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
666
667 -include $(DEPS)