documents fixed
[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 -O3
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 ifeq ($(USE_OPENCL), 1)
356         COMMON_FLAGS += -DUSE_OPENCL
357 endif
358
359 #USE_PROFILING -- get profiling informations, is controled by LOGACL
360 #LAYER_PERF_STAT -- haitao's net profiling information
361 ifeq ($(USE_PROFILING), 1)
362         COMMON_FLAGS += -DUSE_PROFILING -DLAYER_PERF_STAT
363 endif
364 #HDF5
365 ifeq ($(USE_HDF5), 1)
366         LIBRARY_DIRS += $(HDF5_LIBRARY_DIRS)
367         LIBRARIES += $(HDF5_LIBRARIES)
368         INCLUDE_DIRS +=$(HDF5_INCLUDE_DIRS)
369         COMMON_FLAGS += -DUSE_HDF5
370 endif
371
372 # configure IO libraries
373 ifeq ($(USE_OPENCV), 1)
374         COMMON_FLAGS += -DUSE_OPENCV
375 endif
376 ifeq ($(USE_LEVELDB), 1)
377         COMMON_FLAGS += -DUSE_LEVELDB
378 endif
379 ifeq ($(USE_LMDB), 1)
380         COMMON_FLAGS += -DUSE_LMDB
381 ifeq ($(ALLOW_LMDB_NOLOCK), 1)
382         COMMON_FLAGS += -DALLOW_LMDB_NOLOCK
383 endif
384 endif
385
386 # CPU-only configuration
387 ifeq ($(CPU_ONLY), 1)
388         OBJS := $(PROTO_OBJS) $(CXX_OBJS)
389         TEST_OBJS := $(TEST_CXX_OBJS)
390         TEST_BINS := $(TEST_CXX_BINS)
391         ALL_WARNS := $(ALL_CXX_WARNS)
392         TEST_FILTER := --gtest_filter="-*GPU*"
393         COMMON_FLAGS += -DCPU_ONLY
394 endif
395
396 ifeq ($(USE_ACL), 1)
397         OBJS := $(PROTO_OBJS) $(CXX_OBJS)
398         TEST_OBJS := $(TEST_CXX_OBJS)
399         TEST_BINS := $(TEST_CXX_BINS)
400         ALL_WARNS := $(ALL_CXX_WARNS)
401         TEST_FILTER := --gtest_filter="-*GPU*"
402         COMMON_FLAGS += -DCPU_ONLY
403 endif
404
405 # Python layer support
406 ifeq ($(WITH_PYTHON_LAYER), 1)
407         COMMON_FLAGS += -DWITH_PYTHON_LAYER
408         LIBRARIES += $(PYTHON_LIBRARIES)
409 endif
410
411 # BLAS configuration (default = ATLAS)
412 #BLAS ?= atlas
413 BLAS ?= open
414 ifeq ($(BLAS), mkl)
415         # MKL
416         LIBRARIES += mkl_rt
417         COMMON_FLAGS += -DUSE_MKL
418         MKLROOT ?= /opt/intel/mkl
419         BLAS_INCLUDE ?= $(MKLROOT)/include
420         BLAS_LIB ?= $(MKLROOT)/lib $(MKLROOT)/lib/intel64
421 else ifeq ($(BLAS), open)
422         # OpenBLAS
423         LIBRARIES += openblas
424 else
425         # ATLAS
426         ifeq ($(LINUX), 1)
427                 ifeq ($(BLAS), atlas)
428                         # Linux simply has cblas and atlas
429                         LIBRARIES += cblas atlas
430                 endif
431         else ifeq ($(OSX), 1)
432                 # OS X packages atlas as the vecLib framework
433                 LIBRARIES += cblas
434                 # 10.10 has accelerate while 10.9 has veclib
435                 XCODE_CLT_VER := $(shell pkgutil --pkg-info=com.apple.pkg.CLTools_Executables | grep 'version' | sed 's/[^0-9]*\([0-9]\).*/\1/')
436                 XCODE_CLT_GEQ_7 := $(shell [ $(XCODE_CLT_VER) -gt 6 ] && echo 1)
437                 XCODE_CLT_GEQ_6 := $(shell [ $(XCODE_CLT_VER) -gt 5 ] && echo 1)
438                 ifeq ($(XCODE_CLT_GEQ_7), 1)
439                         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
440                 else ifeq ($(XCODE_CLT_GEQ_6), 1)
441                         BLAS_INCLUDE ?= /System/Library/Frameworks/Accelerate.framework/Versions/Current/Frameworks/vecLib.framework/Headers/
442                         LDFLAGS += -framework Accelerate
443                 else
444                         BLAS_INCLUDE ?= /System/Library/Frameworks/vecLib.framework/Versions/Current/Headers/
445                         LDFLAGS += -framework vecLib
446                 endif
447         endif
448 endif
449 INCLUDE_DIRS += $(BLAS_INCLUDE)
450 LIBRARY_DIRS += $(BLAS_LIB)
451
452 LIBRARY_DIRS += $(LIB_BUILD_DIR)
453
454 # Automatic dependency generation (nvcc is handled separately)
455 CXXFLAGS += -MMD -MP
456
457 USE_PKG_CONFIG ?= 0
458 ifeq ($(USE_PKG_CONFIG), 1)
459         PKG_INCLUDE_DIRS :=  `pkg-config opencv --cflags`
460         PKG_CONFIG := `pkg-config opencv --libs`
461 else
462         PKG_CONFIG :=
463         PKG_INCLUDE_DIRS :=
464 endif
465
466 # Complete build flags.
467 COMMON_FLAGS += $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir))
468 COMMON_FLAGS += $(PKG_INCLUDE_DIRS)
469 CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
470 NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
471 # mex may invoke an older gcc that is too liberal with -Wuninitalized
472 MATLAB_CXXFLAGS := $(CXXFLAGS) -Wno-uninitialized
473 LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
474
475 LDFLAGS += $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir)) $(PKG_CONFIG) \
476                 $(foreach library,$(LIBRARIES),-l$(library))
477 PYTHON_LDFLAGS := $(LDFLAGS) $(foreach library,$(PYTHON_LIBRARIES),-l$(library))
478
479 # 'superclean' target recursively* deletes all files ending with an extension
480 # in $(SUPERCLEAN_EXTS) below.  This may be useful if you've built older
481 # versions of Caffe that do not place all generated files in a location known
482 # to the 'clean' target.
483 #
484 # 'supercleanlist' will list the files to be deleted by make superclean.
485 #
486 # * Recursive with the exception that symbolic links are never followed, per the
487 # default behavior of 'find'.
488 SUPERCLEAN_EXTS := .so .a .o .bin .testbin .pb.cc .pb.h _pb2.py .cuo
489
490 # Set the sub-targets of the 'everything' target.
491 EVERYTHING_TARGETS := all py$(PROJECT) test warn lint
492 # Only build matcaffe as part of "everything" if MATLAB_DIR is specified.
493 ifneq ($(MATLAB_DIR),)
494         EVERYTHING_TARGETS += mat$(PROJECT)
495 endif
496
497 ##############################
498 # Define build targets
499 ##############################
500 .PHONY: all lib test clean docs linecount lint lintclean tools examples $(DIST_ALIASES) \
501         py mat py$(PROJECT) mat$(PROJECT) proto runtest \
502         superclean supercleanlist supercleanfiles warn everything install
503
504 all: lib tools examples
505
506 install:
507         install -d $(AIDDIR)/Caffe-HRT
508         cp -rfp ./distribute/* $(AIDDIR)/Caffe-HRT
509         chown -R root:root $(AIDDIR)/Caffe-HRT
510
511 lib: $(STATIC_NAME) $(DYNAMIC_NAME)
512
513 everything: $(EVERYTHING_TARGETS)
514
515 linecount:
516         cloc --read-lang-def=$(PROJECT).cloc \
517                 src/$(PROJECT) include/$(PROJECT) tools examples \
518                 python matlab
519
520 lint: $(EMPTY_LINT_REPORT)
521
522 lintclean:
523         @ $(RM) -r $(LINT_OUTPUT_DIR) $(EMPTY_LINT_REPORT) $(NONEMPTY_LINT_REPORT)
524
525 docs: $(DOXYGEN_OUTPUT_DIR)
526         @ cd ./docs ; ln -sfn ../$(DOXYGEN_OUTPUT_DIR)/html doxygen
527
528 $(DOXYGEN_OUTPUT_DIR): $(DOXYGEN_CONFIG_FILE) $(DOXYGEN_SOURCES)
529         $(DOXYGEN_COMMAND) $(DOXYGEN_CONFIG_FILE)
530
531 $(EMPTY_LINT_REPORT): $(LINT_OUTPUTS) | $(BUILD_DIR)
532         @ cat $(LINT_OUTPUTS) > $@
533         @ if [ -s "$@" ]; then \
534                 cat $@; \
535                 mv $@ $(NONEMPTY_LINT_REPORT); \
536                 echo "Found one or more lint errors."; \
537                 exit 1; \
538           fi; \
539           $(RM) $(NONEMPTY_LINT_REPORT); \
540           echo "No lint errors!";
541
542 $(LINT_OUTPUTS): $(LINT_OUTPUT_DIR)/%.lint.txt : % $(LINT_SCRIPT) | $(LINT_OUTPUT_DIR)
543         @ mkdir -p $(dir $@)
544         @ python $(LINT_SCRIPT) $< 2>&1 \
545                 | grep -v "^Done processing " \
546                 | grep -v "^Total errors found: 0" \
547                 > $@ \
548                 || true
549
550 test: $(TEST_ALL_BIN) $(TEST_ALL_DYNLINK_BIN) $(TEST_BINS)
551
552 tools: $(TOOL_BINS) $(TOOL_BIN_LINKS)
553
554 examples: $(EXAMPLE_BINS)
555
556 py$(PROJECT): py
557
558 py: $(PY$(PROJECT)_SO) $(PROTO_GEN_PY)
559
560 $(PY$(PROJECT)_SO): $(PY$(PROJECT)_SRC) $(PY$(PROJECT)_HXX) | $(DYNAMIC_NAME)
561         @ echo CXX/LD -o $@ $<
562         $(Q)$(CXX) -shared -o $@ $(PY$(PROJECT)_SRC) \
563                 -o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(PYTHON_LDFLAGS) \
564                 -Wl,-rpath,$(ORIGIN)/../../build/lib
565
566 mat$(PROJECT): mat
567
568 mat: $(MAT$(PROJECT)_SO)
569
570 $(MAT$(PROJECT)_SO): $(MAT$(PROJECT)_SRC) $(STATIC_NAME)
571         @ if [ -z "$(MATLAB_DIR)" ]; then \
572                 echo "MATLAB_DIR must be specified in $(CONFIG_FILE)" \
573                         "to build mat$(PROJECT)."; \
574                 exit 1; \
575         fi
576         @ echo MEX $<
577         $(Q)$(MATLAB_DIR)/bin/mex $(MAT$(PROJECT)_SRC) \
578                         CXX="$(CXX)" \
579                         CXXFLAGS="\$$CXXFLAGS $(MATLAB_CXXFLAGS)" \
580                         CXXLIBS="\$$CXXLIBS $(STATIC_LINK_COMMAND) $(LDFLAGS)" -output $@
581         @ if [ -f "$(PROJECT)_.d" ]; then \
582                 mv -f $(PROJECT)_.d $(BUILD_DIR)/${MAT$(PROJECT)_SO:.$(MAT_SO_EXT)=.d}; \
583         fi
584
585 runtest: $(TEST_ALL_BIN)
586         $(TOOL_BUILD_DIR)/caffe
587         $(TEST_ALL_BIN) $(TEST_GPUID) --gtest_shuffle $(TEST_FILTER)
588
589 pytest: py
590         cd python; python -m unittest discover -s caffe/test
591
592 mattest: mat
593         cd matlab; $(MATLAB_DIR)/bin/matlab -nodisplay -r 'caffe.run_tests(), exit()'
594
595 warn: $(EMPTY_WARN_REPORT)
596
597 $(EMPTY_WARN_REPORT): $(ALL_WARNS) | $(BUILD_DIR)
598         @ cat $(ALL_WARNS) > $@
599         @ if [ -s "$@" ]; then \
600                 cat $@; \
601                 mv $@ $(NONEMPTY_WARN_REPORT); \
602                 echo "Compiler produced one or more warnings."; \
603                 exit 1; \
604           fi; \
605           $(RM) $(NONEMPTY_WARN_REPORT); \
606           echo "No compiler warnings!";
607
608 $(ALL_WARNS): %.o.$(WARNS_EXT) : %.o
609
610 $(BUILD_DIR_LINK): $(BUILD_DIR)/.linked
611
612 # Create a target ".linked" in this BUILD_DIR to tell Make that the "build" link
613 # is currently correct, then delete the one in the OTHER_BUILD_DIR in case it
614 # exists and $(DEBUG) is toggled later.
615 $(BUILD_DIR)/.linked:
616         @ mkdir -p $(BUILD_DIR)
617         @ $(RM) $(OTHER_BUILD_DIR)/.linked
618         @ $(RM) -r $(BUILD_DIR_LINK)
619         @ ln -s $(BUILD_DIR) $(BUILD_DIR_LINK)
620         @ touch $@
621
622 $(ALL_BUILD_DIRS): | $(BUILD_DIR_LINK)
623         @ mkdir -p $@
624
625 $(DYNAMIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
626         @ echo LD -o $@
627         $(Q)$(CXX) -shared -o $@ $(OBJS) $(VERSIONFLAGS) $(LINKFLAGS) $(LDFLAGS)
628         @ cd $(BUILD_DIR)/lib; rm -f $(DYNAMIC_NAME_SHORT);   ln -s $(DYNAMIC_VERSIONED_NAME_SHORT) $(DYNAMIC_NAME_SHORT)
629
630 $(STATIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
631         @ echo AR -o $@
632         $(Q)ar rcs $@ $(OBJS)
633
634 $(BUILD_DIR)/%.o: %.cpp | $(ALL_BUILD_DIRS)
635         @ echo CXX $<
636         $(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
637                 || (cat $@.$(WARNS_EXT); exit 1)
638         @ cat $@.$(WARNS_EXT)
639
640 $(PROTO_BUILD_DIR)/%.pb.o: $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_GEN_HEADER) \
641                 | $(PROTO_BUILD_DIR)
642         @ echo CXX $<
643         $(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
644                 || (cat $@.$(WARNS_EXT); exit 1)
645         @ cat $@.$(WARNS_EXT)
646
647 $(BUILD_DIR)/cuda/%.o: %.cu | $(ALL_BUILD_DIRS)
648         @ echo NVCC $<
649         $(Q)$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -M $< -o ${@:.o=.d} \
650                 -odir $(@D)
651         $(Q)$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -c $< -o $@ 2> $@.$(WARNS_EXT) \
652                 || (cat $@.$(WARNS_EXT); exit 1)
653         @ cat $@.$(WARNS_EXT)
654
655 $(TEST_ALL_BIN): $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) \
656                 | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
657         @ echo CXX/LD -o $@ $<
658         $(Q)$(CXX) $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) \
659                 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib
660
661 $(TEST_CU_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CU_BUILD_DIR)/%.o \
662         $(GTEST_OBJ) | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
663         @ echo LD $<
664         $(Q)$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) \
665                 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib
666
667 $(TEST_CXX_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CXX_BUILD_DIR)/%.o \
668         $(GTEST_OBJ) | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
669         @ echo LD $<
670         $(Q)$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) \
671                 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib
672
673 # Target for extension-less symlinks to tool binaries with extension '*.bin'.
674 $(TOOL_BUILD_DIR)/%: $(TOOL_BUILD_DIR)/%.bin | $(TOOL_BUILD_DIR)
675         @ $(RM) $@
676         @ ln -s $(notdir $<) $@
677
678 $(TOOL_BINS): %.bin : %.o | $(DYNAMIC_NAME)
679         @ echo CXX/LD -o $@
680         $(Q)$(CXX) $< -o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(LDFLAGS) \
681                 -Wl,-rpath,$(ORIGIN)/../lib
682
683 $(EXAMPLE_BINS): %.bin : %.o | $(DYNAMIC_NAME)
684         @ echo CXX/LD -o $@
685         $(Q)$(CXX) $< -o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(LDFLAGS) \
686                 -Wl,-rpath,$(ORIGIN)/../../lib
687
688 proto: $(PROTO_GEN_CC) $(PROTO_GEN_HEADER)
689
690 $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_BUILD_DIR)/%.pb.h : \
691                 $(PROTO_SRC_DIR)/%.proto | $(PROTO_BUILD_DIR)
692         @ echo PROTOC $<
693         $(Q)protoc --proto_path=$(PROTO_SRC_DIR) --cpp_out=$(PROTO_BUILD_DIR) $<
694
695 $(PY_PROTO_BUILD_DIR)/%_pb2.py : $(PROTO_SRC_DIR)/%.proto \
696                 $(PY_PROTO_INIT) | $(PY_PROTO_BUILD_DIR)
697         @ echo PROTOC \(python\) $<
698         $(Q)protoc --proto_path=$(PROTO_SRC_DIR) --python_out=$(PY_PROTO_BUILD_DIR) $<
699
700 $(PY_PROTO_INIT): | $(PY_PROTO_BUILD_DIR)
701         touch $(PY_PROTO_INIT)
702
703 clean:
704         @- $(RM) -rf $(ALL_BUILD_DIRS)
705         @- $(RM) -rf $(OTHER_BUILD_DIR)
706         @- $(RM) -rf $(BUILD_DIR_LINK)
707         @- $(RM) -rf $(DISTRIBUTE_DIR)
708         @- $(RM) $(PY$(PROJECT)_SO)
709         @- $(RM) $(MAT$(PROJECT)_SO)
710
711 supercleanfiles:
712         $(eval SUPERCLEAN_FILES := $(strip \
713                         $(foreach ext,$(SUPERCLEAN_EXTS), $(shell find . -name '*$(ext)' \
714                         -not -path './data/*'))))
715
716 supercleanlist: supercleanfiles
717         @ \
718         if [ -z "$(SUPERCLEAN_FILES)" ]; then \
719                 echo "No generated files found."; \
720         else \
721                 echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \
722         fi
723
724 superclean: clean supercleanfiles
725         @ \
726         if [ -z "$(SUPERCLEAN_FILES)" ]; then \
727                 echo "No generated files found."; \
728         else \
729                 echo "Deleting the following generated files:"; \
730                 echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \
731                 $(RM) $(SUPERCLEAN_FILES); \
732         fi
733
734 $(DIST_ALIASES): $(DISTRIBUTE_DIR)
735
736 $(DISTRIBUTE_DIR): all py | $(DISTRIBUTE_SUBDIRS)
737         # add proto
738         cp -r src/caffe/proto $(DISTRIBUTE_DIR)/
739         # add include
740         cp -r include $(DISTRIBUTE_DIR)/
741         mkdir -p $(DISTRIBUTE_DIR)/include/caffe/proto
742         cp $(PROTO_GEN_HEADER_SRCS) $(DISTRIBUTE_DIR)/include/caffe/proto
743         # add tool and example binaries
744         cp $(TOOL_BINS) $(DISTRIBUTE_DIR)/bin
745         cp $(EXAMPLE_BINS) $(DISTRIBUTE_DIR)/bin
746         # add libraries
747         cp $(STATIC_NAME) $(DISTRIBUTE_DIR)/lib
748         install -m 644 $(DYNAMIC_NAME) $(DISTRIBUTE_DIR)/lib
749         cd $(DISTRIBUTE_DIR)/lib; rm -f $(DYNAMIC_NAME_SHORT);   ln -s $(DYNAMIC_VERSIONED_NAME_SHORT) $(DYNAMIC_NAME_SHORT)
750         # add python - it's not the standard way, indeed...
751         cp -r python $(DISTRIBUTE_DIR)/python
752
753 -include $(DEPS)