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