updata Makefile
[platform/upstream/caffeonacl.git] / Makefile
1 PROJECT := caffe
2
3 CONFIG_FILE := Makefile.config.acl
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 USE_PKG_CONFIG ?= 0
454 ifeq ($(USE_PKG_CONFIG), 1)
455         PKG_INCLUDE_DIRS :=  `pkg-config opencv --cflags`
456         PKG_CONFIG := `pkg-config opencv --libs`
457 else
458         PKG_CONFIG :=
459         PKG_INCLUDE_DIRS :=
460 endif
461
462 # Complete build flags.
463 COMMON_FLAGS += $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir))
464 COMMON_FLAGS += $(PKG_INCLUDE_DIRS)
465 CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
466 NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
467 # mex may invoke an older gcc that is too liberal with -Wuninitalized
468 MATLAB_CXXFLAGS := $(CXXFLAGS) -Wno-uninitialized
469 LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
470
471 LDFLAGS += $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir)) $(PKG_CONFIG) \
472                 $(foreach library,$(LIBRARIES),-l$(library))
473 PYTHON_LDFLAGS := $(LDFLAGS) $(foreach library,$(PYTHON_LIBRARIES),-l$(library))
474
475 # 'superclean' target recursively* deletes all files ending with an extension
476 # in $(SUPERCLEAN_EXTS) below.  This may be useful if you've built older
477 # versions of Caffe that do not place all generated files in a location known
478 # to the 'clean' target.
479 #
480 # 'supercleanlist' will list the files to be deleted by make superclean.
481 #
482 # * Recursive with the exception that symbolic links are never followed, per the
483 # default behavior of 'find'.
484 SUPERCLEAN_EXTS := .so .a .o .bin .testbin .pb.cc .pb.h _pb2.py .cuo
485
486 # Set the sub-targets of the 'everything' target.
487 EVERYTHING_TARGETS := all py$(PROJECT) test warn lint
488 # Only build matcaffe as part of "everything" if MATLAB_DIR is specified.
489 ifneq ($(MATLAB_DIR),)
490         EVERYTHING_TARGETS += mat$(PROJECT)
491 endif
492
493 ##############################
494 # Define build targets
495 ##############################
496 .PHONY: all lib test clean docs linecount lint lintclean tools examples $(DIST_ALIASES) \
497         py mat py$(PROJECT) mat$(PROJECT) proto runtest \
498         superclean supercleanlist supercleanfiles warn everything install
499
500 all: lib tools examples
501
502 install:
503         install -d $(AIDDIR)/CaffeOnACL
504         cp -rfp ./distribute/* $(AIDDIR)/CaffeOnACL
505         chown -R root:root $(AIDDIR)/CaffeOnACL
506
507 lib: $(STATIC_NAME) $(DYNAMIC_NAME)
508
509 everything: $(EVERYTHING_TARGETS)
510
511 linecount:
512         cloc --read-lang-def=$(PROJECT).cloc \
513                 src/$(PROJECT) include/$(PROJECT) tools examples \
514                 python matlab
515
516 lint: $(EMPTY_LINT_REPORT)
517
518 lintclean:
519         @ $(RM) -r $(LINT_OUTPUT_DIR) $(EMPTY_LINT_REPORT) $(NONEMPTY_LINT_REPORT)
520
521 docs: $(DOXYGEN_OUTPUT_DIR)
522         @ cd ./docs ; ln -sfn ../$(DOXYGEN_OUTPUT_DIR)/html doxygen
523
524 $(DOXYGEN_OUTPUT_DIR): $(DOXYGEN_CONFIG_FILE) $(DOXYGEN_SOURCES)
525         $(DOXYGEN_COMMAND) $(DOXYGEN_CONFIG_FILE)
526
527 $(EMPTY_LINT_REPORT): $(LINT_OUTPUTS) | $(BUILD_DIR)
528         @ cat $(LINT_OUTPUTS) > $@
529         @ if [ -s "$@" ]; then \
530                 cat $@; \
531                 mv $@ $(NONEMPTY_LINT_REPORT); \
532                 echo "Found one or more lint errors."; \
533                 exit 1; \
534           fi; \
535           $(RM) $(NONEMPTY_LINT_REPORT); \
536           echo "No lint errors!";
537
538 $(LINT_OUTPUTS): $(LINT_OUTPUT_DIR)/%.lint.txt : % $(LINT_SCRIPT) | $(LINT_OUTPUT_DIR)
539         @ mkdir -p $(dir $@)
540         @ python $(LINT_SCRIPT) $< 2>&1 \
541                 | grep -v "^Done processing " \
542                 | grep -v "^Total errors found: 0" \
543                 > $@ \
544                 || true
545
546 test: $(TEST_ALL_BIN) $(TEST_ALL_DYNLINK_BIN) $(TEST_BINS)
547
548 tools: $(TOOL_BINS) $(TOOL_BIN_LINKS)
549
550 examples: $(EXAMPLE_BINS)
551
552 py$(PROJECT): py
553
554 py: $(PY$(PROJECT)_SO) $(PROTO_GEN_PY)
555
556 $(PY$(PROJECT)_SO): $(PY$(PROJECT)_SRC) $(PY$(PROJECT)_HXX) | $(DYNAMIC_NAME)
557         @ echo CXX/LD -o $@ $<
558         $(Q)$(CXX) -shared -o $@ $(PY$(PROJECT)_SRC) \
559                 -o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(PYTHON_LDFLAGS) \
560                 -Wl,-rpath,$(ORIGIN)/../../build/lib
561
562 mat$(PROJECT): mat
563
564 mat: $(MAT$(PROJECT)_SO)
565
566 $(MAT$(PROJECT)_SO): $(MAT$(PROJECT)_SRC) $(STATIC_NAME)
567         @ if [ -z "$(MATLAB_DIR)" ]; then \
568                 echo "MATLAB_DIR must be specified in $(CONFIG_FILE)" \
569                         "to build mat$(PROJECT)."; \
570                 exit 1; \
571         fi
572         @ echo MEX $<
573         $(Q)$(MATLAB_DIR)/bin/mex $(MAT$(PROJECT)_SRC) \
574                         CXX="$(CXX)" \
575                         CXXFLAGS="\$$CXXFLAGS $(MATLAB_CXXFLAGS)" \
576                         CXXLIBS="\$$CXXLIBS $(STATIC_LINK_COMMAND) $(LDFLAGS)" -output $@
577         @ if [ -f "$(PROJECT)_.d" ]; then \
578                 mv -f $(PROJECT)_.d $(BUILD_DIR)/${MAT$(PROJECT)_SO:.$(MAT_SO_EXT)=.d}; \
579         fi
580
581 runtest: $(TEST_ALL_BIN)
582         $(TOOL_BUILD_DIR)/caffe
583         $(TEST_ALL_BIN) $(TEST_GPUID) --gtest_shuffle $(TEST_FILTER)
584
585 pytest: py
586         cd python; python -m unittest discover -s caffe/test
587
588 mattest: mat
589         cd matlab; $(MATLAB_DIR)/bin/matlab -nodisplay -r 'caffe.run_tests(), exit()'
590
591 warn: $(EMPTY_WARN_REPORT)
592
593 $(EMPTY_WARN_REPORT): $(ALL_WARNS) | $(BUILD_DIR)
594         @ cat $(ALL_WARNS) > $@
595         @ if [ -s "$@" ]; then \
596                 cat $@; \
597                 mv $@ $(NONEMPTY_WARN_REPORT); \
598                 echo "Compiler produced one or more warnings."; \
599                 exit 1; \
600           fi; \
601           $(RM) $(NONEMPTY_WARN_REPORT); \
602           echo "No compiler warnings!";
603
604 $(ALL_WARNS): %.o.$(WARNS_EXT) : %.o
605
606 $(BUILD_DIR_LINK): $(BUILD_DIR)/.linked
607
608 # Create a target ".linked" in this BUILD_DIR to tell Make that the "build" link
609 # is currently correct, then delete the one in the OTHER_BUILD_DIR in case it
610 # exists and $(DEBUG) is toggled later.
611 $(BUILD_DIR)/.linked:
612         @ mkdir -p $(BUILD_DIR)
613         @ $(RM) $(OTHER_BUILD_DIR)/.linked
614         @ $(RM) -r $(BUILD_DIR_LINK)
615         @ ln -s $(BUILD_DIR) $(BUILD_DIR_LINK)
616         @ touch $@
617
618 $(ALL_BUILD_DIRS): | $(BUILD_DIR_LINK)
619         @ mkdir -p $@
620
621 $(DYNAMIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
622         @ echo LD -o $@
623         $(Q)$(CXX) -shared -o $@ $(OBJS) $(VERSIONFLAGS) $(LINKFLAGS) $(LDFLAGS)
624         @ cd $(BUILD_DIR)/lib; rm -f $(DYNAMIC_NAME_SHORT);   ln -s $(DYNAMIC_VERSIONED_NAME_SHORT) $(DYNAMIC_NAME_SHORT)
625
626 $(STATIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
627         @ echo AR -o $@
628         $(Q)ar rcs $@ $(OBJS)
629
630 $(BUILD_DIR)/%.o: %.cpp | $(ALL_BUILD_DIRS)
631         @ echo CXX $<
632         $(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
633                 || (cat $@.$(WARNS_EXT); exit 1)
634         @ cat $@.$(WARNS_EXT)
635
636 $(PROTO_BUILD_DIR)/%.pb.o: $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_GEN_HEADER) \
637                 | $(PROTO_BUILD_DIR)
638         @ echo CXX $<
639         $(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
640                 || (cat $@.$(WARNS_EXT); exit 1)
641         @ cat $@.$(WARNS_EXT)
642
643 $(BUILD_DIR)/cuda/%.o: %.cu | $(ALL_BUILD_DIRS)
644         @ echo NVCC $<
645         $(Q)$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -M $< -o ${@:.o=.d} \
646                 -odir $(@D)
647         $(Q)$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -c $< -o $@ 2> $@.$(WARNS_EXT) \
648                 || (cat $@.$(WARNS_EXT); exit 1)
649         @ cat $@.$(WARNS_EXT)
650
651 $(TEST_ALL_BIN): $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) \
652                 | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
653         @ echo CXX/LD -o $@ $<
654         $(Q)$(CXX) $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) \
655                 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib
656
657 $(TEST_CU_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CU_BUILD_DIR)/%.o \
658         $(GTEST_OBJ) | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
659         @ echo LD $<
660         $(Q)$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) \
661                 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib
662
663 $(TEST_CXX_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CXX_BUILD_DIR)/%.o \
664         $(GTEST_OBJ) | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
665         @ echo LD $<
666         $(Q)$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) \
667                 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib
668
669 # Target for extension-less symlinks to tool binaries with extension '*.bin'.
670 $(TOOL_BUILD_DIR)/%: $(TOOL_BUILD_DIR)/%.bin | $(TOOL_BUILD_DIR)
671         @ $(RM) $@
672         @ ln -s $(notdir $<) $@
673
674 $(TOOL_BINS): %.bin : %.o | $(DYNAMIC_NAME)
675         @ echo CXX/LD -o $@
676         $(Q)$(CXX) $< -o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(LDFLAGS) \
677                 -Wl,-rpath,$(ORIGIN)/../lib
678
679 $(EXAMPLE_BINS): %.bin : %.o | $(DYNAMIC_NAME)
680         @ echo CXX/LD -o $@
681         $(Q)$(CXX) $< -o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(LDFLAGS) \
682                 -Wl,-rpath,$(ORIGIN)/../../lib
683
684 proto: $(PROTO_GEN_CC) $(PROTO_GEN_HEADER)
685
686 $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_BUILD_DIR)/%.pb.h : \
687                 $(PROTO_SRC_DIR)/%.proto | $(PROTO_BUILD_DIR)
688         @ echo PROTOC $<
689         $(Q)protoc --proto_path=$(PROTO_SRC_DIR) --cpp_out=$(PROTO_BUILD_DIR) $<
690
691 $(PY_PROTO_BUILD_DIR)/%_pb2.py : $(PROTO_SRC_DIR)/%.proto \
692                 $(PY_PROTO_INIT) | $(PY_PROTO_BUILD_DIR)
693         @ echo PROTOC \(python\) $<
694         $(Q)protoc --proto_path=$(PROTO_SRC_DIR) --python_out=$(PY_PROTO_BUILD_DIR) $<
695
696 $(PY_PROTO_INIT): | $(PY_PROTO_BUILD_DIR)
697         touch $(PY_PROTO_INIT)
698
699 clean:
700         @- $(RM) -rf $(ALL_BUILD_DIRS)
701         @- $(RM) -rf $(OTHER_BUILD_DIR)
702         @- $(RM) -rf $(BUILD_DIR_LINK)
703         @- $(RM) -rf $(DISTRIBUTE_DIR)
704         @- $(RM) $(PY$(PROJECT)_SO)
705         @- $(RM) $(MAT$(PROJECT)_SO)
706
707 supercleanfiles:
708         $(eval SUPERCLEAN_FILES := $(strip \
709                         $(foreach ext,$(SUPERCLEAN_EXTS), $(shell find . -name '*$(ext)' \
710                         -not -path './data/*'))))
711
712 supercleanlist: supercleanfiles
713         @ \
714         if [ -z "$(SUPERCLEAN_FILES)" ]; then \
715                 echo "No generated files found."; \
716         else \
717                 echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \
718         fi
719
720 superclean: clean supercleanfiles
721         @ \
722         if [ -z "$(SUPERCLEAN_FILES)" ]; then \
723                 echo "No generated files found."; \
724         else \
725                 echo "Deleting the following generated files:"; \
726                 echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \
727                 $(RM) $(SUPERCLEAN_FILES); \
728         fi
729
730 $(DIST_ALIASES): $(DISTRIBUTE_DIR)
731
732 $(DISTRIBUTE_DIR): all py | $(DISTRIBUTE_SUBDIRS)
733         # add proto
734         cp -r src/caffe/proto $(DISTRIBUTE_DIR)/
735         # add include
736         cp -r include $(DISTRIBUTE_DIR)/
737         mkdir -p $(DISTRIBUTE_DIR)/include/caffe/proto
738         cp $(PROTO_GEN_HEADER_SRCS) $(DISTRIBUTE_DIR)/include/caffe/proto
739         # add tool and example binaries
740         cp $(TOOL_BINS) $(DISTRIBUTE_DIR)/bin
741         cp $(EXAMPLE_BINS) $(DISTRIBUTE_DIR)/bin
742         # add libraries
743         cp $(STATIC_NAME) $(DISTRIBUTE_DIR)/lib
744         install -m 644 $(DYNAMIC_NAME) $(DISTRIBUTE_DIR)/lib
745         cd $(DISTRIBUTE_DIR)/lib; rm -f $(DYNAMIC_NAME_SHORT);   ln -s $(DYNAMIC_VERSIONED_NAME_SHORT) $(DYNAMIC_NAME_SHORT)
746         # add python - it's not the standard way, indeed...
747         cp -r python $(DISTRIBUTE_DIR)/python
748
749 -include $(DEPS)