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