Merge pull request #4347 from nitnelave/python/layer_dict
[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 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-rc4
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)
43
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 \
73         src/$(PROJECT) \
74         include/$(PROJECT) \
75         python/$(PROJECT) \
76         matlab/+$(PROJECT)/private \
77         examples \
78         tools \
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
82 LINT_EXT := lint.txt
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)
94 endif
95 MAT$(PROJECT)_SO := matlab/+$(PROJECT)/private/$(PROJECT)_.$(MAT_SO_EXT)
96
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
144
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)
158
159 EMPTY_WARN_REPORT := $(BUILD_DIR)/.$(WARNS_EXT)
160 NONEMPTY_WARN_REPORT := $(BUILD_DIR)/$(WARNS_EXT)
161
162 ##############################
163 # Derive include and lib directories
164 ##############################
165 CUDA_INCLUDE_DIR := $(CUDA_DIR)/include
166
167 CUDA_LIB_DIR :=
168 # add <cuda>/lib64 only if it exists
169 ifneq ("$(wildcard $(CUDA_DIR)/lib64)","")
170         CUDA_LIB_DIR += $(CUDA_DIR)/lib64
171 endif
172 CUDA_LIB_DIR += $(CUDA_DIR)/lib
173
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
179 endif
180
181 LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
182
183 # handle IO dependencies
184 USE_LEVELDB ?= 1
185 USE_LMDB ?= 1
186 USE_OPENCV ?= 1
187
188 ifeq ($(USE_LEVELDB), 1)
189         LIBRARIES += leveldb snappy
190 endif
191 ifeq ($(USE_LMDB), 1)
192         LIBRARIES += lmdb
193 endif
194 ifeq ($(USE_OPENCV), 1)
195         LIBRARIES += opencv_core opencv_highgui opencv_imgproc
196
197         ifeq ($(OPENCV_VERSION), 3)
198                 LIBRARIES += opencv_imgcodecs
199         endif
200
201 endif
202 PYTHON_LIBRARIES ?= boost_python python2.7
203 WARNINGS := -Wall -Wno-sign-compare
204
205 ##############################
206 # Set build directories
207 ##############################
208
209 DISTRIBUTE_DIR ?= distribute
210 DISTRIBUTE_SUBDIRS := $(DISTRIBUTE_DIR)/bin $(DISTRIBUTE_DIR)/lib
211 DIST_ALIASES := dist
212 ifneq ($(strip $(DISTRIBUTE_DIR)),distribute)
213                 DIST_ALIASES += distribute
214 endif
215
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))
220
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 \
230         src/$(PROJECT) \
231         include/$(PROJECT) \
232         python/ \
233         matlab/ \
234         examples \
235         tools \
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)
239
240
241 ##############################
242 # Configure build
243 ##############################
244
245 # Determine platform
246 UNAME := $(shell uname -s)
247 ifeq ($(UNAME), Linux)
248         LINUX := 1
249 else ifeq ($(UNAME), Darwin)
250         OSX := 1
251         OSX_MAJOR_VERSION := $(shell sw_vers -productVersion | cut -f 1 -d .)
252         OSX_MINOR_VERSION := $(shell sw_vers -productVersion | cut -f 2 -d .)
253 endif
254
255 # Linux
256 ifeq ($(LINUX), 1)
257         CXX ?= /usr/bin/g++
258         GCCVERSION := $(shell $(CXX) -dumpversion | cut -f1,2 -d.)
259         # older versions of gcc are too dumb to build boost with -Wuninitalized
260         ifeq ($(shell echo | awk '{exit $(GCCVERSION) < 4.6;}'), 1)
261                 WARNINGS += -Wno-uninitialized
262         endif
263         # boost::thread is reasonably called boost_thread (compare OS X)
264         # We will also explicitly add stdc++ to the link target.
265         LIBRARIES += boost_thread stdc++
266         VERSIONFLAGS += -Wl,-soname,$(DYNAMIC_VERSIONED_NAME_SHORT) -Wl,-rpath,$(ORIGIN)/../lib
267 endif
268
269 # OS X:
270 # clang++ instead of g++
271 # libstdc++ for NVCC compatibility on OS X >= 10.9 with CUDA < 7.0
272 ifeq ($(OSX), 1)
273         CXX := /usr/bin/clang++
274         ifneq ($(CPU_ONLY), 1)
275                 CUDA_VERSION := $(shell $(CUDA_DIR)/bin/nvcc -V | grep -o 'release [0-9.]*' | tr -d '[a-z ]')
276                 ifeq ($(shell echo | awk '{exit $(CUDA_VERSION) < 7.0;}'), 1)
277                         CXXFLAGS += -stdlib=libstdc++
278                         LINKFLAGS += -stdlib=libstdc++
279                 endif
280                 # clang throws this warning for cuda headers
281                 WARNINGS += -Wno-unneeded-internal-declaration
282                 # 10.11 strips DYLD_* env vars so link CUDA (rpath is available on 10.5+)
283                 OSX_10_OR_LATER   := $(shell [ $(OSX_MAJOR_VERSION) -ge 10 ] && echo true)
284                 OSX_10_5_OR_LATER := $(shell [ $(OSX_MINOR_VERSION) -ge 5 ] && echo true)
285                 ifeq ($(OSX_10_OR_LATER),true)
286                         ifeq ($(OSX_10_5_OR_LATER),true)
287                                 LDFLAGS += -Wl,-rpath,$(CUDA_LIB_DIR)
288                         endif
289                 endif
290         endif
291         # gtest needs to use its own tuple to not conflict with clang
292         COMMON_FLAGS += -DGTEST_USE_OWN_TR1_TUPLE=1
293         # boost::thread is called boost_thread-mt to mark multithreading on OS X
294         LIBRARIES += boost_thread-mt
295         # we need to explicitly ask for the rpath to be obeyed
296         ORIGIN := @loader_path
297         VERSIONFLAGS += -Wl,-install_name,@rpath/$(DYNAMIC_VERSIONED_NAME_SHORT) -Wl,-rpath,$(ORIGIN)/../../build/lib
298 else
299         ORIGIN := \$$ORIGIN
300 endif
301
302 # Custom compiler
303 ifdef CUSTOM_CXX
304         CXX := $(CUSTOM_CXX)
305 endif
306
307 # Static linking
308 ifneq (,$(findstring clang++,$(CXX)))
309         STATIC_LINK_COMMAND := -Wl,-force_load $(STATIC_NAME)
310 else ifneq (,$(findstring g++,$(CXX)))
311         STATIC_LINK_COMMAND := -Wl,--whole-archive $(STATIC_NAME) -Wl,--no-whole-archive
312 else
313   # The following line must not be indented with a tab, since we are not inside a target
314   $(error Cannot static link with the $(CXX) compiler)
315 endif
316
317 # Debugging
318 ifeq ($(DEBUG), 1)
319         COMMON_FLAGS += -DDEBUG -g -O0
320         NVCCFLAGS += -G
321 else
322         COMMON_FLAGS += -DNDEBUG -O2
323 endif
324
325 # cuDNN acceleration configuration.
326 ifeq ($(USE_CUDNN), 1)
327         LIBRARIES += cudnn
328         COMMON_FLAGS += -DUSE_CUDNN
329 endif
330
331 # NCCL acceleration configuration
332 ifeq ($(USE_NCCL), 1)
333         LIBRARIES += nccl
334         COMMON_FLAGS += -DUSE_NCCL
335 endif
336
337 # configure IO libraries
338 ifeq ($(USE_OPENCV), 1)
339         COMMON_FLAGS += -DUSE_OPENCV
340 endif
341 ifeq ($(USE_LEVELDB), 1)
342         COMMON_FLAGS += -DUSE_LEVELDB
343 endif
344 ifeq ($(USE_LMDB), 1)
345         COMMON_FLAGS += -DUSE_LMDB
346 ifeq ($(ALLOW_LMDB_NOLOCK), 1)
347         COMMON_FLAGS += -DALLOW_LMDB_NOLOCK
348 endif
349 endif
350
351 # CPU-only configuration
352 ifeq ($(CPU_ONLY), 1)
353         OBJS := $(PROTO_OBJS) $(CXX_OBJS)
354         TEST_OBJS := $(TEST_CXX_OBJS)
355         TEST_BINS := $(TEST_CXX_BINS)
356         ALL_WARNS := $(ALL_CXX_WARNS)
357         TEST_FILTER := --gtest_filter="-*GPU*"
358         COMMON_FLAGS += -DCPU_ONLY
359 endif
360
361 # Python layer support
362 ifeq ($(WITH_PYTHON_LAYER), 1)
363         COMMON_FLAGS += -DWITH_PYTHON_LAYER
364         LIBRARIES += $(PYTHON_LIBRARIES)
365 endif
366
367 # BLAS configuration (default = ATLAS)
368 BLAS ?= atlas
369 ifeq ($(BLAS), mkl)
370         # MKL
371         LIBRARIES += mkl_rt
372         COMMON_FLAGS += -DUSE_MKL
373         MKLROOT ?= /opt/intel/mkl
374         BLAS_INCLUDE ?= $(MKLROOT)/include
375         BLAS_LIB ?= $(MKLROOT)/lib $(MKLROOT)/lib/intel64
376 else ifeq ($(BLAS), open)
377         # OpenBLAS
378         LIBRARIES += openblas
379 else
380         # ATLAS
381         ifeq ($(LINUX), 1)
382                 ifeq ($(BLAS), atlas)
383                         # Linux simply has cblas and atlas
384                         LIBRARIES += cblas atlas
385                 endif
386         else ifeq ($(OSX), 1)
387                 # OS X packages atlas as the vecLib framework
388                 LIBRARIES += cblas
389                 # 10.10 has accelerate while 10.9 has veclib
390                 XCODE_CLT_VER := $(shell pkgutil --pkg-info=com.apple.pkg.CLTools_Executables | grep 'version' | sed 's/[^0-9]*\([0-9]\).*/\1/')
391                 XCODE_CLT_GEQ_7 := $(shell [ $(XCODE_CLT_VER) -gt 6 ] && echo 1)
392                 XCODE_CLT_GEQ_6 := $(shell [ $(XCODE_CLT_VER) -gt 5 ] && echo 1)
393                 ifeq ($(XCODE_CLT_GEQ_7), 1)
394                         BLAS_INCLUDE ?= /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/$(shell ls /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/ | sort | tail -1)/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/Headers
395                 else ifeq ($(XCODE_CLT_GEQ_6), 1)
396                         BLAS_INCLUDE ?= /System/Library/Frameworks/Accelerate.framework/Versions/Current/Frameworks/vecLib.framework/Headers/
397                         LDFLAGS += -framework Accelerate
398                 else
399                         BLAS_INCLUDE ?= /System/Library/Frameworks/vecLib.framework/Versions/Current/Headers/
400                         LDFLAGS += -framework vecLib
401                 endif
402         endif
403 endif
404 INCLUDE_DIRS += $(BLAS_INCLUDE)
405 LIBRARY_DIRS += $(BLAS_LIB)
406
407 LIBRARY_DIRS += $(LIB_BUILD_DIR)
408
409 # Automatic dependency generation (nvcc is handled separately)
410 CXXFLAGS += -MMD -MP
411
412 # Complete build flags.
413 COMMON_FLAGS += $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir))
414 CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
415 NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
416 # mex may invoke an older gcc that is too liberal with -Wuninitalized
417 MATLAB_CXXFLAGS := $(CXXFLAGS) -Wno-uninitialized
418 LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
419
420 USE_PKG_CONFIG ?= 0
421 ifeq ($(USE_PKG_CONFIG), 1)
422         PKG_CONFIG := $(shell pkg-config opencv --libs)
423 else
424         PKG_CONFIG :=
425 endif
426 LDFLAGS += $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir)) $(PKG_CONFIG) \
427                 $(foreach library,$(LIBRARIES),-l$(library))
428 PYTHON_LDFLAGS := $(LDFLAGS) $(foreach library,$(PYTHON_LIBRARIES),-l$(library))
429
430 # 'superclean' target recursively* deletes all files ending with an extension
431 # in $(SUPERCLEAN_EXTS) below.  This may be useful if you've built older
432 # versions of Caffe that do not place all generated files in a location known
433 # to the 'clean' target.
434 #
435 # 'supercleanlist' will list the files to be deleted by make superclean.
436 #
437 # * Recursive with the exception that symbolic links are never followed, per the
438 # default behavior of 'find'.
439 SUPERCLEAN_EXTS := .so .a .o .bin .testbin .pb.cc .pb.h _pb2.py .cuo
440
441 # Set the sub-targets of the 'everything' target.
442 EVERYTHING_TARGETS := all py$(PROJECT) test warn lint
443 # Only build matcaffe as part of "everything" if MATLAB_DIR is specified.
444 ifneq ($(MATLAB_DIR),)
445         EVERYTHING_TARGETS += mat$(PROJECT)
446 endif
447
448 ##############################
449 # Define build targets
450 ##############################
451 .PHONY: all lib test clean docs linecount lint lintclean tools examples $(DIST_ALIASES) \
452         py mat py$(PROJECT) mat$(PROJECT) proto runtest \
453         superclean supercleanlist supercleanfiles warn everything
454
455 all: lib tools examples
456
457 lib: $(STATIC_NAME) $(DYNAMIC_NAME)
458
459 everything: $(EVERYTHING_TARGETS)
460
461 linecount:
462         cloc --read-lang-def=$(PROJECT).cloc \
463                 src/$(PROJECT) include/$(PROJECT) tools examples \
464                 python matlab
465
466 lint: $(EMPTY_LINT_REPORT)
467
468 lintclean:
469         @ $(RM) -r $(LINT_OUTPUT_DIR) $(EMPTY_LINT_REPORT) $(NONEMPTY_LINT_REPORT)
470
471 docs: $(DOXYGEN_OUTPUT_DIR)
472         @ cd ./docs ; ln -sfn ../$(DOXYGEN_OUTPUT_DIR)/html doxygen
473
474 $(DOXYGEN_OUTPUT_DIR): $(DOXYGEN_CONFIG_FILE) $(DOXYGEN_SOURCES)
475         $(DOXYGEN_COMMAND) $(DOXYGEN_CONFIG_FILE)
476
477 $(EMPTY_LINT_REPORT): $(LINT_OUTPUTS) | $(BUILD_DIR)
478         @ cat $(LINT_OUTPUTS) > $@
479         @ if [ -s "$@" ]; then \
480                 cat $@; \
481                 mv $@ $(NONEMPTY_LINT_REPORT); \
482                 echo "Found one or more lint errors."; \
483                 exit 1; \
484           fi; \
485           $(RM) $(NONEMPTY_LINT_REPORT); \
486           echo "No lint errors!";
487
488 $(LINT_OUTPUTS): $(LINT_OUTPUT_DIR)/%.lint.txt : % $(LINT_SCRIPT) | $(LINT_OUTPUT_DIR)
489         @ mkdir -p $(dir $@)
490         @ python $(LINT_SCRIPT) $< 2>&1 \
491                 | grep -v "^Done processing " \
492                 | grep -v "^Total errors found: 0" \
493                 > $@ \
494                 || true
495
496 test: $(TEST_ALL_BIN) $(TEST_ALL_DYNLINK_BIN) $(TEST_BINS)
497
498 tools: $(TOOL_BINS) $(TOOL_BIN_LINKS)
499
500 examples: $(EXAMPLE_BINS)
501
502 py$(PROJECT): py
503
504 py: $(PY$(PROJECT)_SO) $(PROTO_GEN_PY)
505
506 $(PY$(PROJECT)_SO): $(PY$(PROJECT)_SRC) $(PY$(PROJECT)_HXX) | $(DYNAMIC_NAME)
507         @ echo CXX/LD -o $@ $<
508         $(Q)$(CXX) -shared -o $@ $(PY$(PROJECT)_SRC) \
509                 -o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(PYTHON_LDFLAGS) \
510                 -Wl,-rpath,$(ORIGIN)/../../build/lib
511
512 mat$(PROJECT): mat
513
514 mat: $(MAT$(PROJECT)_SO)
515
516 $(MAT$(PROJECT)_SO): $(MAT$(PROJECT)_SRC) $(STATIC_NAME)
517         @ if [ -z "$(MATLAB_DIR)" ]; then \
518                 echo "MATLAB_DIR must be specified in $(CONFIG_FILE)" \
519                         "to build mat$(PROJECT)."; \
520                 exit 1; \
521         fi
522         @ echo MEX $<
523         $(Q)$(MATLAB_DIR)/bin/mex $(MAT$(PROJECT)_SRC) \
524                         CXX="$(CXX)" \
525                         CXXFLAGS="\$$CXXFLAGS $(MATLAB_CXXFLAGS)" \
526                         CXXLIBS="\$$CXXLIBS $(STATIC_LINK_COMMAND) $(LDFLAGS)" -output $@
527         @ if [ -f "$(PROJECT)_.d" ]; then \
528                 mv -f $(PROJECT)_.d $(BUILD_DIR)/${MAT$(PROJECT)_SO:.$(MAT_SO_EXT)=.d}; \
529         fi
530
531 runtest: $(TEST_ALL_BIN)
532         $(TOOL_BUILD_DIR)/caffe
533         $(TEST_ALL_BIN) $(TEST_GPUID) --gtest_shuffle $(TEST_FILTER)
534
535 pytest: py
536         cd python; python -m unittest discover -s caffe/test
537
538 mattest: mat
539         cd matlab; $(MATLAB_DIR)/bin/matlab -nodisplay -r 'caffe.run_tests(), exit()'
540
541 warn: $(EMPTY_WARN_REPORT)
542
543 $(EMPTY_WARN_REPORT): $(ALL_WARNS) | $(BUILD_DIR)
544         @ cat $(ALL_WARNS) > $@
545         @ if [ -s "$@" ]; then \
546                 cat $@; \
547                 mv $@ $(NONEMPTY_WARN_REPORT); \
548                 echo "Compiler produced one or more warnings."; \
549                 exit 1; \
550           fi; \
551           $(RM) $(NONEMPTY_WARN_REPORT); \
552           echo "No compiler warnings!";
553
554 $(ALL_WARNS): %.o.$(WARNS_EXT) : %.o
555
556 $(BUILD_DIR_LINK): $(BUILD_DIR)/.linked
557
558 # Create a target ".linked" in this BUILD_DIR to tell Make that the "build" link
559 # is currently correct, then delete the one in the OTHER_BUILD_DIR in case it
560 # exists and $(DEBUG) is toggled later.
561 $(BUILD_DIR)/.linked:
562         @ mkdir -p $(BUILD_DIR)
563         @ $(RM) $(OTHER_BUILD_DIR)/.linked
564         @ $(RM) -r $(BUILD_DIR_LINK)
565         @ ln -s $(BUILD_DIR) $(BUILD_DIR_LINK)
566         @ touch $@
567
568 $(ALL_BUILD_DIRS): | $(BUILD_DIR_LINK)
569         @ mkdir -p $@
570
571 $(DYNAMIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
572         @ echo LD -o $@
573         $(Q)$(CXX) -shared -o $@ $(OBJS) $(VERSIONFLAGS) $(LINKFLAGS) $(LDFLAGS)
574         @ cd $(BUILD_DIR)/lib; rm -f $(DYNAMIC_NAME_SHORT);   ln -s $(DYNAMIC_VERSIONED_NAME_SHORT) $(DYNAMIC_NAME_SHORT)
575
576 $(STATIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
577         @ echo AR -o $@
578         $(Q)ar rcs $@ $(OBJS)
579
580 $(BUILD_DIR)/%.o: %.cpp | $(ALL_BUILD_DIRS)
581         @ echo CXX $<
582         $(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
583                 || (cat $@.$(WARNS_EXT); exit 1)
584         @ cat $@.$(WARNS_EXT)
585
586 $(PROTO_BUILD_DIR)/%.pb.o: $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_GEN_HEADER) \
587                 | $(PROTO_BUILD_DIR)
588         @ echo CXX $<
589         $(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
590                 || (cat $@.$(WARNS_EXT); exit 1)
591         @ cat $@.$(WARNS_EXT)
592
593 $(BUILD_DIR)/cuda/%.o: %.cu | $(ALL_BUILD_DIRS)
594         @ echo NVCC $<
595         $(Q)$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -M $< -o ${@:.o=.d} \
596                 -odir $(@D)
597         $(Q)$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -c $< -o $@ 2> $@.$(WARNS_EXT) \
598                 || (cat $@.$(WARNS_EXT); exit 1)
599         @ cat $@.$(WARNS_EXT)
600
601 $(TEST_ALL_BIN): $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) \
602                 | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
603         @ echo CXX/LD -o $@ $<
604         $(Q)$(CXX) $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) \
605                 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib
606
607 $(TEST_CU_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CU_BUILD_DIR)/%.o \
608         $(GTEST_OBJ) | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
609         @ echo LD $<
610         $(Q)$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) \
611                 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib
612
613 $(TEST_CXX_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CXX_BUILD_DIR)/%.o \
614         $(GTEST_OBJ) | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
615         @ echo LD $<
616         $(Q)$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) \
617                 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib
618
619 # Target for extension-less symlinks to tool binaries with extension '*.bin'.
620 $(TOOL_BUILD_DIR)/%: $(TOOL_BUILD_DIR)/%.bin | $(TOOL_BUILD_DIR)
621         @ $(RM) $@
622         @ ln -s $(notdir $<) $@
623
624 $(TOOL_BINS): %.bin : %.o | $(DYNAMIC_NAME)
625         @ echo CXX/LD -o $@
626         $(Q)$(CXX) $< -o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(LDFLAGS) \
627                 -Wl,-rpath,$(ORIGIN)/../lib
628
629 $(EXAMPLE_BINS): %.bin : %.o | $(DYNAMIC_NAME)
630         @ echo CXX/LD -o $@
631         $(Q)$(CXX) $< -o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(LDFLAGS) \
632                 -Wl,-rpath,$(ORIGIN)/../../lib
633
634 proto: $(PROTO_GEN_CC) $(PROTO_GEN_HEADER)
635
636 $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_BUILD_DIR)/%.pb.h : \
637                 $(PROTO_SRC_DIR)/%.proto | $(PROTO_BUILD_DIR)
638         @ echo PROTOC $<
639         $(Q)protoc --proto_path=$(PROTO_SRC_DIR) --cpp_out=$(PROTO_BUILD_DIR) $<
640
641 $(PY_PROTO_BUILD_DIR)/%_pb2.py : $(PROTO_SRC_DIR)/%.proto \
642                 $(PY_PROTO_INIT) | $(PY_PROTO_BUILD_DIR)
643         @ echo PROTOC \(python\) $<
644         $(Q)protoc --proto_path=$(PROTO_SRC_DIR) --python_out=$(PY_PROTO_BUILD_DIR) $<
645
646 $(PY_PROTO_INIT): | $(PY_PROTO_BUILD_DIR)
647         touch $(PY_PROTO_INIT)
648
649 clean:
650         @- $(RM) -rf $(ALL_BUILD_DIRS)
651         @- $(RM) -rf $(OTHER_BUILD_DIR)
652         @- $(RM) -rf $(BUILD_DIR_LINK)
653         @- $(RM) -rf $(DISTRIBUTE_DIR)
654         @- $(RM) $(PY$(PROJECT)_SO)
655         @- $(RM) $(MAT$(PROJECT)_SO)
656
657 supercleanfiles:
658         $(eval SUPERCLEAN_FILES := $(strip \
659                         $(foreach ext,$(SUPERCLEAN_EXTS), $(shell find . -name '*$(ext)' \
660                         -not -path './data/*'))))
661
662 supercleanlist: supercleanfiles
663         @ \
664         if [ -z "$(SUPERCLEAN_FILES)" ]; then \
665                 echo "No generated files found."; \
666         else \
667                 echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \
668         fi
669
670 superclean: clean supercleanfiles
671         @ \
672         if [ -z "$(SUPERCLEAN_FILES)" ]; then \
673                 echo "No generated files found."; \
674         else \
675                 echo "Deleting the following generated files:"; \
676                 echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \
677                 $(RM) $(SUPERCLEAN_FILES); \
678         fi
679
680 $(DIST_ALIASES): $(DISTRIBUTE_DIR)
681
682 $(DISTRIBUTE_DIR): all py | $(DISTRIBUTE_SUBDIRS)
683         # add proto
684         cp -r src/caffe/proto $(DISTRIBUTE_DIR)/
685         # add include
686         cp -r include $(DISTRIBUTE_DIR)/
687         mkdir -p $(DISTRIBUTE_DIR)/include/caffe/proto
688         cp $(PROTO_GEN_HEADER_SRCS) $(DISTRIBUTE_DIR)/include/caffe/proto
689         # add tool and example binaries
690         cp $(TOOL_BINS) $(DISTRIBUTE_DIR)/bin
691         cp $(EXAMPLE_BINS) $(DISTRIBUTE_DIR)/bin
692         # add libraries
693         cp $(STATIC_NAME) $(DISTRIBUTE_DIR)/lib
694         install -m 644 $(DYNAMIC_NAME) $(DISTRIBUTE_DIR)/lib
695         cd $(DISTRIBUTE_DIR)/lib; rm -f $(DYNAMIC_NAME_SHORT);   ln -s $(DYNAMIC_VERSIONED_NAME_SHORT) $(DYNAMIC_NAME_SHORT)
696         # add python - it's not the standard way, indeed...
697         cp -r python $(DISTRIBUTE_DIR)/python
698
699 -include $(DEPS)