]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - jacinto-ai/caffe-jacinto.git/blob - Makefile
0.17
[jacinto-ai/caffe-jacinto.git] / Makefile
1 PROJECT := caffe
3 CONFIG_FILE := Makefile.config
4 # Explicitly check for the config file, otherwise make -k will proceed anyway.
5 ifeq ($(wildcard $(CONFIG_FILE)),)
6 $(error $(CONFIG_FILE) not found. See $(CONFIG_FILE).example.)
7 endif
8 include $(CONFIG_FILE)
10 BUILD_DIR_LINK := $(BUILD_DIR)
11 ifeq ($(RELEASE_BUILD_DIR),)
12         RELEASE_BUILD_DIR := .$(BUILD_DIR)_release
13 endif
14 ifeq ($(DEBUG_BUILD_DIR),)
15         DEBUG_BUILD_DIR := .$(BUILD_DIR)_debug
16 endif
18 DEBUG ?= 0
19 ifeq ($(DEBUG), 1)
20         BUILD_DIR := $(DEBUG_BUILD_DIR)
21         OTHER_BUILD_DIR := $(RELEASE_BUILD_DIR)
22 else
23         BUILD_DIR := $(RELEASE_BUILD_DIR)
24         OTHER_BUILD_DIR := $(DEBUG_BUILD_DIR)
25 endif
27 THIRDPARTY_DIR := ./3rdparty
29 # All of the directories containing code.
30 SRC_DIRS := $(shell find * -type d -exec bash -c "find {} -maxdepth 1 \
31         \( -name '*.cpp' -o -name '*.proto' \) | grep -q ." \; -print 2>/dev/null)
33 # The target shared library name
34 LIBRARY_NAME := $(PROJECT)$(LIBRARY_NAME_SUFFIX)
35 LIB_BUILD_DIR := $(BUILD_DIR)/lib
36 STATIC_NAME := $(LIB_BUILD_DIR)/lib$(LIBRARY_NAME).a
37 DYNAMIC_VERSION_MAJOR           := 0
38 DYNAMIC_VERSION_MINOR           := 17
39 DYNAMIC_VERSION_REVISION        := 0
40 DYNAMIC_NAME_SHORT := lib$(LIBRARY_NAME).so
41 DYNAMIC_SONAME_SHORT := $(DYNAMIC_NAME_SHORT).$(DYNAMIC_VERSION_MAJOR).$(DYNAMIC_VERSION_MINOR)
42 DYNAMIC_VERSIONED_NAME_SHORT := $(DYNAMIC_SONAME_SHORT).$(DYNAMIC_VERSION_REVISION)
43 DYNAMIC_NAME := $(LIB_BUILD_DIR)/$(DYNAMIC_VERSIONED_NAME_SHORT)
44 COMMON_FLAGS += -DCAFFE_VERSION=$(DYNAMIC_VERSION_MAJOR).$(DYNAMIC_VERSION_MINOR).$(DYNAMIC_VERSION_REVISION)
45 # NVCaffe requires C++ 11
46 COMMON_FLAGS += -std=c++11
47 COMMON_FLAGS += -DCUDA_NO_HALF
49 ##############################
50 # Get all source files
51 ##############################
52 # CXX_SRCS are the source files excluding the test ones.
53 CXX_SRCS := $(shell find src/$(PROJECT) ! -name "test_*.cpp" -name "*.cpp")
54 # CU_SRCS are the cuda source files
55 CU_SRCS := $(shell find src/$(PROJECT) ! -name "test_*.cu" -name "*.cu")
56 # TEST_SRCS are the test source files
57 TEST_MAIN_SRC := src/$(PROJECT)/test/test_caffe_main.cpp
58 TEST_SRCS := $(shell find src/$(PROJECT) -name "test_*.cpp")
59 TEST_SRCS := $(filter-out $(TEST_MAIN_SRC), $(TEST_SRCS))
60 TEST_CU_SRCS := $(shell find src/$(PROJECT) -name "test_*.cu")
61 GTEST_SRC := src/gtest/gtest-all.cpp
62 # TOOL_SRCS are the source files for the tool binaries
63 TOOL_SRCS := $(shell find tools -name "*.cpp")
64 # EXAMPLE_SRCS are the source files for the example binaries
65 EXAMPLE_SRCS := $(shell find examples -name "*.cpp")
66 # BUILD_INCLUDE_DIR contains any generated header files we want to include.
67 BUILD_INCLUDE_DIR := $(BUILD_DIR)/src
68 # PROTO_SRCS are the protocol buffer definitions
69 PROTO_SRC_DIR := src/$(PROJECT)/proto
70 PROTO_SRCS := $(wildcard $(PROTO_SRC_DIR)/*.proto)
71 # PROTO_BUILD_DIR will contain the .cc and obj files generated from
72 # PROTO_SRCS; PROTO_BUILD_INCLUDE_DIR will contain the .h header files
73 PROTO_BUILD_DIR := $(BUILD_DIR)/$(PROTO_SRC_DIR)
74 PROTO_BUILD_INCLUDE_DIR := $(BUILD_INCLUDE_DIR)/$(PROJECT)/proto
75 # NONGEN_CXX_SRCS includes all source/header files except those generated
76 # automatically (e.g., by proto).
77 NONGEN_CXX_SRCS := $(shell find \
78         src/$(PROJECT) \
79         include/$(PROJECT) \
80         python/$(PROJECT) \
81         matlab/+$(PROJECT)/private \
82         examples \
83         tools \
84         -name "*.cpp" -or -name "*.hpp" -or -name "*.cu" -or -name "*.cuh")
85 LINT_SCRIPT := scripts/cpp_lint.py
86 LINT_OUTPUT_DIR := $(BUILD_DIR)/.lint
87 LINT_EXT := lint.txt
88 LINT_OUTPUTS := $(addsuffix .$(LINT_EXT), $(addprefix $(LINT_OUTPUT_DIR)/, $(NONGEN_CXX_SRCS)))
89 EMPTY_LINT_REPORT := $(BUILD_DIR)/.$(LINT_EXT)
90 NONEMPTY_LINT_REPORT := $(BUILD_DIR)/$(LINT_EXT)
91 # PY$(PROJECT)_SRC is the python wrapper for $(PROJECT)
92 PY$(PROJECT)_SRC := python/$(PROJECT)/_$(PROJECT).cpp
93 PY$(PROJECT)_OBJ := $(BUILD_DIR)/src/$(PROJECT)/layers/python_layer.o
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)
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
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)
165 EMPTY_WARN_REPORT := $(BUILD_DIR)/.$(WARNS_EXT)
166 NONEMPTY_WARN_REPORT := $(BUILD_DIR)/$(WARNS_EXT)
168 ##############################
169 # Derive include and lib directories
170 ##############################
171 ifeq ($(shell uname -m),aarch64)
172     TEGRA=1
173     NO_NVML=1
174 endif
176 CUDA_INCLUDE_DIR := $(CUDA_DIR)/include
178 CUDA_LIB_DIR :=
179 # add <cuda>/lib64 only if it exists
180 ifneq ("$(wildcard $(CUDA_DIR)/lib64)","")
181         CUDA_LIB_DIR += $(CUDA_DIR)/lib64
182         CUDA_LIB_DIR += /usr/lib/nvidia-396 /usr/lib/nvidia-390 /usr/lib/nvidia-387 /usr/lib/nvidia-384 /usr/lib/nvidia-381 /usr/lib/nvidia-375 /usr/lib/nvidia-367 /usr/lib/nvidia-361 /usr/lib/nvidia-352
183 endif
184 CUDA_LIB_DIR += $(CUDA_DIR)/lib
186 INCLUDE_DIRS += $(BUILD_INCLUDE_DIR) ./src ./include $(THIRDPARTY_DIR) /usr/include/hdf5/serial
187 INCLUDE_DIRS += $(CUDA_INCLUDE_DIR)
188 LIBRARY_DIRS += $(CUDA_LIB_DIR)
189 LIBRARIES := cudart cublas curand
190 ifneq ($(NO_NVML), 1)
191         LIBRARIES += nvidia-ml
192 endif
194 # Note: libturbojpeg has a packaging bug. Workaround:
195 # $ sudo ln -s /usr/lib/x86_64-linux-gnu/libturbojpeg.so.0 /usr/lib/x86_64-linux-gnu/libturbojpeg.so
197 LIBRARIES += boost_system glog gflags protobuf boost_filesystem m turbojpeg
198 ifeq ($(TEGRA), 1)
199     LIBRARIES += hdf5_serial_hl hdf5_serial
200 else
201     LIBRARIES += hdf5_hl hdf5
202 endif
204 # handle IO dependencies
205 USE_LEVELDB ?= 1
206 USE_LMDB ?= 1
207 USE_OPENCV ?= 1
209 ifeq ($(USE_LEVELDB), 1)
210         LIBRARIES += leveldb snappy
211 endif
212 ifeq ($(USE_LMDB), 1)
213         LIBRARIES += lmdb
214 endif
215 ifeq ($(USE_OPENCV), 1)
216         LIBRARIES += opencv_core opencv_highgui opencv_imgproc
218         ifeq ($(OPENCV_VERSION), 3)
219                 LIBRARIES += opencv_imgcodecs opencv_videoio
220         endif
221                 
222 endif
224 python_version_full := $(wordlist 2,4,$(subst ., ,$(shell python --version 2>&1)))
225 python_version_major := $(word 1,${python_version_full})
226 python_version_minor := $(word 2,${python_version_full})
227 python_version_patch := $(word 3,${python_version_full})
228 ifeq ($(python_version_major), 3)
229         python_lib_suffix := m
230 endif
232 PYTHON_LIBRARIES ?= boost_python-py${python_version_major}${python_version_minor} python${python_version_major}.${python_version_minor}${python_lib_suffix} boost_regex
233 WARNINGS := -Wall -Wno-sign-compare
235 ##############################
236 # Set build directories
237 ##############################
239 DISTRIBUTE_DIR ?= distribute
240 DISTRIBUTE_SUBDIRS := $(DISTRIBUTE_DIR)/bin $(DISTRIBUTE_DIR)/lib
241 DIST_ALIASES := dist
242 ifneq ($(strip $(DISTRIBUTE_DIR)),distribute)
243                 DIST_ALIASES += distribute
244 endif
246 ALL_BUILD_DIRS := $(sort $(BUILD_DIR) $(addprefix $(BUILD_DIR)/, $(SRC_DIRS)) \
247         $(addprefix $(BUILD_DIR)/cuda/, $(SRC_DIRS)) \
248         $(LIB_BUILD_DIR) $(TEST_BIN_DIR) $(PY_PROTO_BUILD_DIR) $(LINT_OUTPUT_DIR) \
249         $(DISTRIBUTE_SUBDIRS) $(PROTO_BUILD_INCLUDE_DIR))
251 ##############################
252 # Set directory for Doxygen-generated documentation
253 ##############################
254 DOXYGEN_CONFIG_FILE ?= ./.Doxyfile
255 # should be the same as OUTPUT_DIRECTORY in the .Doxyfile
256 DOXYGEN_OUTPUT_DIR ?= ./doxygen
257 DOXYGEN_COMMAND ?= doxygen
258 # All the files that might have Doxygen documentation.
259 DOXYGEN_SOURCES := $(shell find \
260         src/$(PROJECT) \
261         include/$(PROJECT) \
262         python/ \
263         matlab/ \
264         examples \
265         tools \
266         -name "*.cpp" -or -name "*.hpp" -or -name "*.cu" -or -name "*.cuh" -or \
267         -name "*.py" -or -name "*.m")
268 DOXYGEN_SOURCES += $(DOXYGEN_CONFIG_FILE)
271 ##############################
272 # Configure build
273 ##############################
275 # Determine platform
276 UNAME := $(shell uname -s)
277 ifeq ($(UNAME), Linux)
278         LINUX := 1
279 else ifeq ($(UNAME), Darwin)
280         OSX := 1
281         OSX_MAJOR_VERSION := $(shell sw_vers -productVersion | cut -f 1 -d .)
282         OSX_MINOR_VERSION := $(shell sw_vers -productVersion | cut -f 2 -d .)
283 endif
285 # Linux
286 ifeq ($(LINUX), 1)
287         CXX ?= /usr/bin/g++
288         GCCVERSION := $(shell $(CXX) -dumpversion | cut -f1,2 -d.)
289         # older versions of gcc are too dumb to build boost with -Wuninitalized
290         ifeq ($(shell echo | awk '{exit $(GCCVERSION) < 4.6;}'), 1)
291                 WARNINGS += -Wno-uninitialized
292         endif
293         # boost::thread is reasonably called boost_thread (compare OS X)
294         # We will also explicitly add stdc++ to the link target.
295         LIBRARIES += boost_thread boost_regex stdc++
296         VERSIONFLAGS += -Wl,-soname,$(DYNAMIC_SONAME_SHORT) -Wl,-rpath,$(ORIGIN)/../lib
297 endif
299 # OS X:
300 # clang++ instead of g++
301 # libstdc++ for NVCC compatibility on OS X >= 10.9 with CUDA < 7.0
302 ifeq ($(OSX), 1)
303         CXX := /usr/bin/clang++
304     CUDA_VERSION := $(shell $(CUDA_DIR)/bin/nvcc -V | grep -o 'release [0-9.]*' | grep -o '[0-9.]*')
305     ifeq ($(shell echo | awk '{exit $(CUDA_VERSION) < 7.0;}'), 1)
306         CXXFLAGS += -stdlib=libstdc++
307         LINKFLAGS += -stdlib=libstdc++
308     endif
309     # clang throws this warning for cuda headers
310     WARNINGS += -Wno-unneeded-internal-declaration
311     # 10.11 strips DYLD_* env vars so link CUDA (rpath is available on 10.5+)
312     OSX_10_OR_LATER   := $(shell [ $(OSX_MAJOR_VERSION) -ge 10 ] && echo true)
313     OSX_10_5_OR_LATER := $(shell [ $(OSX_MINOR_VERSION) -ge 5 ] && echo true)
314     ifeq ($(OSX_10_OR_LATER),true)
315         ifeq ($(OSX_10_5_OR_LATER),true)
316             LDFLAGS += -Wl,-rpath,$(CUDA_LIB_DIR)
317         endif
318     endif
319         # gtest needs to use its own tuple to not conflict with clang
320         COMMON_FLAGS += -DGTEST_USE_OWN_TR1_TUPLE=1
321         # boost::thread is called boost_thread-mt to mark multithreading on OS X
322         LIBRARIES += boost_thread-mt
323         # we need to explicitly ask for the rpath to be obeyed
324         ORIGIN := @loader_path
325         VERSIONFLAGS += -Wl,-install_name,$(DYNAMIC_SONAME_SHORT) -Wl,-rpath,$(ORIGIN)/../../build/lib
326 else
327         ORIGIN := \$$ORIGIN
328 endif
330 # Custom compiler
331 ifdef CUSTOM_CXX
332         CXX := $(CUSTOM_CXX)
333 endif
335 # Static linking
336 ifneq (,$(findstring clang++,$(CXX)))
337         STATIC_LINK_COMMAND := -Wl,-force_load $(STATIC_NAME)
338 else ifneq (,$(findstring g++,$(CXX)))
339         STATIC_LINK_COMMAND := -Wl,--whole-archive $(STATIC_NAME) -Wl,--no-whole-archive
340 else
341   # The following line must not be indented with a tab, since we are not inside a target
342   $(error Cannot static link with the $(CXX) compiler)
343 endif
345 # Debugging
346 ifeq ($(DEBUG), 1)
347         COMMON_FLAGS += -DDEBUG -g -O0
348         NVCCFLAGS += -G
349 else
350         COMMON_FLAGS += -DNDEBUG -O2
351 endif
353 # cuDNN acceleration configuration.
354 ifeq ($(USE_CUDNN), 1)
355         ifdef CUDNN_ROOT
356                 CUDNN_DIR := $(CUDNN_ROOT)
357         endif
358         LIBRARIES += cudnn
359         INCLUDE_DIRS += $(CUDNN_DIR)/cuda/include $(CUDNN_DIR)/include $(CUDNN_DIR)
360         LIBRARY_DIRS += $(CUDNN_DIR)/cuda/lib64 $(CUDNN_DIR)/lib64 $(CUDNN_DIR)
361         COMMON_FLAGS += -DUSE_CUDNN
362 endif
364 # NCCL acceleration configuration
365 ifeq ($(USE_NCCL), 1)
366         LIBRARIES += nccl
367         COMMON_FLAGS += -DUSE_NCCL
368 endif
370 # configure IO libraries
371 ifeq ($(USE_OPENCV), 1)
372         COMMON_FLAGS += -DUSE_OPENCV
373 endif
374 ifeq ($(USE_LEVELDB), 1)
375         COMMON_FLAGS += -DUSE_LEVELDB
376 endif
377 ifeq ($(USE_LMDB), 1)
378         COMMON_FLAGS += -DUSE_LMDB
379 ifeq ($(ALLOW_LMDB_NOLOCK), 1)
380         COMMON_FLAGS += -DALLOW_LMDB_NOLOCK
381 endif
382 endif
384 # New place for HDF5
385 LIBRARY_DIRS += /usr/lib/x86_64-linux-gnu/hdf5/serial
387 ifeq ($(NO_NVML), 1)
388         COMMON_FLAGS += -DNO_NVML=1
389 endif
391 ifeq ($(TEST_FP16), 1)
392         COMMON_FLAGS += -DTEST_FP16=1
393 endif
395 # Python layer support
396 ifeq ($(WITH_PYTHON_LAYER), 1)
397         COMMON_FLAGS += -DWITH_PYTHON_LAYER
398         LIBRARIES += $(PYTHON_LIBRARIES)
399 endif
401 # BLAS configuration (default = ATLAS)
402 BLAS ?= atlas
403 ifeq ($(BLAS), mkl)
404         # MKL
405         LIBRARIES += mkl_rt
406         COMMON_FLAGS += -DUSE_MKL
407         MKLROOT ?= /opt/intel/mkl
408         BLAS_INCLUDE ?= $(MKLROOT)/include
409         BLAS_LIB ?= $(MKLROOT)/lib $(MKLROOT)/lib/intel64
410 else ifeq ($(BLAS), open)
411         # OpenBLAS
412         LIBRARIES += openblas
413 else
414         # ATLAS
415         ifeq ($(LINUX), 1)
416                 ifeq ($(BLAS), atlas)
417                         # Linux simply has cblas and atlas
418                         LIBRARIES += cblas atlas
419                 endif
420         else ifeq ($(OSX), 1)
421                 # OS X packages atlas as the vecLib framework
422                 LIBRARIES += cblas
423                 # 10.10 has accelerate while 10.9 has veclib
424                 XCODE_CLT_VER := $(shell pkgutil --pkg-info=com.apple.pkg.CLTools_Executables | grep 'version' | sed 's/[^0-9]*\([0-9]\).*/\1/')
425                 XCODE_CLT_GEQ_6 := $(shell [ $(XCODE_CLT_VER) -gt 5 ] && echo 1)
426                 ifeq ($(XCODE_CLT_GEQ_6), 1)
427                         BLAS_INCLUDE ?= /System/Library/Frameworks/Accelerate.framework/Versions/Current/Frameworks/vecLib.framework/Headers/
428                         LDFLAGS += -framework Accelerate
429                 else
430                         BLAS_INCLUDE ?= /System/Library/Frameworks/vecLib.framework/Versions/Current/Headers/
431                         LDFLAGS += -framework vecLib
432                 endif
433         endif
434 endif
435 INCLUDE_DIRS += $(BLAS_INCLUDE)
436 LIBRARY_DIRS += $(BLAS_LIB)
438 LIBRARY_DIRS += $(LIB_BUILD_DIR)
440 # Automatic dependency generation (nvcc is handled separately)
441 CXXFLAGS += -MMD -MP
443 # Complete build flags.
444 COMMON_FLAGS += $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir))
445 CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
446 NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
447 # mex may invoke an older gcc that is too liberal with -Wuninitalized
448 MATLAB_CXXFLAGS := $(CXXFLAGS) -Wno-uninitialized -std=c++11
449 LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
451 USE_PKG_CONFIG ?= 0
452 ifeq ($(USE_PKG_CONFIG), 1)
453         PKG_CONFIG := $(shell pkg-config opencv --libs)
454 else
455         PKG_CONFIG :=
456 endif
457 LDFLAGS += $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir)) $(PKG_CONFIG) \
458                 $(foreach library,$(LIBRARIES),-l$(library))
459 PYTHON_LDFLAGS := $(LDFLAGS) $(foreach library,$(PYTHON_LIBRARIES),-l$(library))
461 # 'superclean' target recursively* deletes all files ending with an extension
462 # in $(SUPERCLEAN_EXTS) below.  This may be useful if you've built older
463 # versions of Caffe that do not place all generated files in a location known
464 # to the 'clean' target.
466 # 'supercleanlist' will list the files to be deleted by make superclean.
468 # * Recursive with the exception that symbolic links are never followed, per the
469 # default behavior of 'find'.
470 SUPERCLEAN_EXTS := .so .a .o .bin .testbin .pb.cc .pb.h _pb2.py .cuo
472 # Set the sub-targets of the 'everything' target.
473 EVERYTHING_TARGETS := all py$(PROJECT) test warn lint
474 # Only build matcaffe as part of "everything" if MATLAB_DIR is specified.
475 ifneq ($(MATLAB_DIR),)
476         EVERYTHING_TARGETS += mat$(PROJECT)
477 endif
479 ##############################
480 # Define build targets
481 ##############################
482 .PHONY: all lib test clean docs linecount lint lintclean tools examples $(DIST_ALIASES) \
483         py mat py$(PROJECT) mat$(PROJECT) proto runtest \
484         superclean supercleanlist supercleanfiles warn everything
486 all: lib tools examples
488 lib: $(STATIC_NAME) $(DYNAMIC_NAME)
490 everything: $(EVERYTHING_TARGETS)
492 linecount:
493         cloc --read-lang-def=$(PROJECT).cloc \
494                 src/$(PROJECT) include/$(PROJECT) tools examples \
495                 python matlab
497 lint: $(EMPTY_LINT_REPORT)
499 lintclean:
500         @ $(RM) -r $(LINT_OUTPUT_DIR) $(EMPTY_LINT_REPORT) $(NONEMPTY_LINT_REPORT)
502 docs: $(DOXYGEN_OUTPUT_DIR)
503         @ cd ./docs ; ln -sfn ../$(DOXYGEN_OUTPUT_DIR)/html doxygen
505 $(DOXYGEN_OUTPUT_DIR): $(DOXYGEN_CONFIG_FILE) $(DOXYGEN_SOURCES)
506         $(DOXYGEN_COMMAND) $(DOXYGEN_CONFIG_FILE)
508 $(EMPTY_LINT_REPORT): $(LINT_OUTPUTS) | $(BUILD_DIR)
509         @ cat $(LINT_OUTPUTS) > $@
510         @ if [ -s "$@" ]; then \
511                 cat $@; \
512                 mv $@ $(NONEMPTY_LINT_REPORT); \
513                 echo "Found one or more lint errors."; \
514                 exit 1; \
515           fi; \
516           $(RM) $(NONEMPTY_LINT_REPORT); \
517           echo "No lint errors!";
519 $(LINT_OUTPUTS): $(LINT_OUTPUT_DIR)/%.lint.txt : % $(LINT_SCRIPT) | $(LINT_OUTPUT_DIR)
520         @ mkdir -p $(dir $@)
521         @ python $(LINT_SCRIPT) --filter=-legal,-build/include,-runtime/references,-readability,-whitespace/comments $< 2>&1 \
522                 | grep -v "^Done processing " \
523                 | grep -v "^Total errors found: 0" \
524                 > $@ \
525                 || true
527 test: $(TEST_ALL_BIN) $(TEST_ALL_DYNLINK_BIN) $(TEST_BINS)
529 tools: $(TOOL_BINS) $(TOOL_BIN_LINKS)
531 examples: $(EXAMPLE_BINS)
533 py$(PROJECT): py
535 py: $(PY$(PROJECT)_SO) $(PROTO_GEN_PY)
537 $(PY$(PROJECT)_SO): $(PY$(PROJECT)_OBJ) $(PY$(PROJECT)_SRC) $(PY$(PROJECT)_HXX) | $(DYNAMIC_NAME)
538         @ echo CXX/LD -o $@ $<
539         $(Q)$(CXX) -shared -o $@ $(PY$(PROJECT)_SRC) $(PY$(PROJECT)_OBJ) \
540                 $(LINKFLAGS) -l$(LIBRARY_NAME) $(PYTHON_LDFLAGS) \
541                 -Wl,-rpath,$(ORIGIN)/../../build/lib
543 mat$(PROJECT): mat
545 mat: $(MAT$(PROJECT)_SO)
547 $(MAT$(PROJECT)_SO): $(MAT$(PROJECT)_SRC) $(STATIC_NAME)
548         @ if [ -z "$(MATLAB_DIR)" ]; then \
549                 echo "MATLAB_DIR must be specified in $(CONFIG_FILE)" \
550                         "to build mat$(PROJECT)."; \
551                 exit 1; \
552         fi
553         @ echo MEX $<
554         $(Q)$(MATLAB_DIR)/bin/mex $(MAT$(PROJECT)_SRC) \
555                         CXX="$(CXX)" \
556                         CXXFLAGS="\$$CXXFLAGS $(MATLAB_CXXFLAGS)" \
557                         CXXLIBS="\$$CXXLIBS $(STATIC_LINK_COMMAND) $(LDFLAGS)" -output $@
558         @ if [ -f "$(PROJECT)_.d" ]; then \
559                 mv -f $(PROJECT)_.d $(BUILD_DIR)/${MAT$(PROJECT)_SO:.$(MAT_SO_EXT)=.d}; \
560         fi
562 runtest: $(TEST_ALL_BIN)
563         $(TOOL_BUILD_DIR)/caffe
564         $(TEST_ALL_BIN) $(TEST_GPUID) --gtest_shuffle $(TEST_FILTER)
566 pytest: py
567         cd python; python -u -m unittest discover -s caffe/test
569 mattest: mat
570         cd matlab; $(MATLAB_DIR)/bin/matlab -nodisplay -r 'caffe.run_tests(), exit()'
572 warn: $(EMPTY_WARN_REPORT)
574 $(EMPTY_WARN_REPORT): $(ALL_WARNS) | $(BUILD_DIR)
575         @ cat $(ALL_WARNS) > $@
576         @ if [ -s "$@" ]; then \
577                 cat $@; \
578                 mv $@ $(NONEMPTY_WARN_REPORT); \
579                 echo "Compiler produced one or more warnings."; \
580                 exit 1; \
581           fi; \
582           $(RM) $(NONEMPTY_WARN_REPORT); \
583           echo "No compiler warnings!";
585 $(ALL_WARNS): %.o.$(WARNS_EXT) : %.o
587 $(BUILD_DIR_LINK): $(BUILD_DIR)/.linked
589 # Create a target ".linked" in this BUILD_DIR to tell Make that the "build" link
590 # is currently correct, then delete the one in the OTHER_BUILD_DIR in case it
591 # exists and $(DEBUG) is toggled later.
592 $(BUILD_DIR)/.linked:
593         @ mkdir -p $(BUILD_DIR)
594         @ $(RM) $(OTHER_BUILD_DIR)/.linked
595         @ $(RM) -r $(BUILD_DIR_LINK)
596         @ ln -s $(BUILD_DIR) $(BUILD_DIR_LINK)
597         @ touch $@
599 $(ALL_BUILD_DIRS): | $(BUILD_DIR_LINK)
600         @ mkdir -p $@
602 $(DYNAMIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
603         @ echo LD -o $@
604         $(Q)$(CXX) -shared -o $@ $(OBJS) $(VERSIONFLAGS) $(LINKFLAGS) $(LDFLAGS) $(DYNAMIC_FLAGS)
605         @ cd $(BUILD_DIR)/lib; rm -f $(DYNAMIC_SONAME_SHORT); ln -s $(DYNAMIC_VERSIONED_NAME_SHORT) $(DYNAMIC_SONAME_SHORT)
606         @ cd $(BUILD_DIR)/lib; rm -f $(DYNAMIC_NAME_SHORT);   ln -s $(DYNAMIC_SONAME_SHORT) $(DYNAMIC_NAME_SHORT)
608 $(STATIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
609         @ echo AR -o $@
610         $(Q)ar rcs $@ $(OBJS)
612 $(BUILD_DIR)/%.o: %.cpp | $(ALL_BUILD_DIRS)
613         @ echo CXX $<
614         $(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
615                 || (cat $@.$(WARNS_EXT); exit 1)
616         @ cat $@.$(WARNS_EXT)
618 $(PROTO_BUILD_DIR)/%.pb.o: $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_GEN_HEADER) \
619                 | $(PROTO_BUILD_DIR)
620         @ echo CXX $<
621         $(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
622                 || (cat $@.$(WARNS_EXT); exit 1)
623         @ cat $@.$(WARNS_EXT)
625 $(BUILD_DIR)/cuda/%.o: %.cu | $(ALL_BUILD_DIRS)
626         @ echo NVCC $<
627         $(Q)$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -M $< -o ${@:.o=.d} \
628                 -odir $(@D)
629         $(Q)$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -c $< -o $@ 2> $@.$(WARNS_EXT) \
630                 || (cat $@.$(WARNS_EXT); exit 1)
631         @ cat $@.$(WARNS_EXT)
633 $(TEST_ALL_BIN): $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) \
634                 | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
635         @ echo CXX/LD -o $@ $<
636         $(Q)$(CXX) $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) \
637                 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib
639 $(TEST_CU_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CU_BUILD_DIR)/%.o \
640         $(GTEST_OBJ) | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
641         @ echo LD $<
642         $(Q)$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) \
643                 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib
645 $(TEST_CXX_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CXX_BUILD_DIR)/%.o \
646         $(GTEST_OBJ) | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
647         @ echo LD $<
648         $(Q)$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) \
649                 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib
651 # Target for extension-less symlinks to tool binaries with extension '*.bin'.
652 $(TOOL_BUILD_DIR)/%: $(TOOL_BUILD_DIR)/%.bin | $(TOOL_BUILD_DIR)
653         @ $(RM) $@
654         @ ln -s $(notdir $<) $@
656 $(TOOL_BINS): %.bin : %.o | $(DYNAMIC_NAME)
657         @ echo CXX/LD -o $@
658         $(Q)$(CXX) $< -o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(LDFLAGS) \
659                 -Wl,-rpath,$(ORIGIN)/../lib
661 $(EXAMPLE_BINS): %.bin : %.o | $(DYNAMIC_NAME)
662         @ echo CXX/LD -o $@
663         $(Q)$(CXX) $< -o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(LDFLAGS) \
664                 -Wl,-rpath,$(ORIGIN)/../../lib
666 proto: $(PROTO_GEN_CC) $(PROTO_GEN_HEADER)
668 $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_BUILD_DIR)/%.pb.h : \
669                 $(PROTO_SRC_DIR)/%.proto | $(PROTO_BUILD_DIR)
670         @ echo PROTOC $<
671         $(Q)protoc --proto_path=$(PROTO_SRC_DIR) --cpp_out=$(PROTO_BUILD_DIR) $<
673 $(PY_PROTO_BUILD_DIR)/%_pb2.py : $(PROTO_SRC_DIR)/%.proto \
674                 $(PY_PROTO_INIT) | $(PY_PROTO_BUILD_DIR)
675         @ echo PROTOC \(python\) $<
676         $(Q)protoc --proto_path=$(PROTO_SRC_DIR) --python_out=$(PY_PROTO_BUILD_DIR) $<
678 $(PY_PROTO_INIT): | $(PY_PROTO_BUILD_DIR)
679         touch $(PY_PROTO_INIT)
681 clean:
682         @- $(RM) -rf $(ALL_BUILD_DIRS)
683         @- $(RM) -rf $(OTHER_BUILD_DIR)
684         @- $(RM) -rf $(BUILD_DIR_LINK)
685         @- $(RM) -rf $(DISTRIBUTE_DIR)
686         @- $(RM) $(PY$(PROJECT)_SO)
687         @- $(RM) $(MAT$(PROJECT)_SO)
689 supercleanfiles:
690         $(eval SUPERCLEAN_FILES := $(strip \
691                         $(foreach ext,$(SUPERCLEAN_EXTS), $(shell find . -name '*$(ext)' \
692                         -not -path './data/*'))))
694 supercleanlist: supercleanfiles
695         @ \
696         if [ -z "$(SUPERCLEAN_FILES)" ]; then \
697                 echo "No generated files found."; \
698         else \
699                 echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \
700         fi
702 superclean: clean supercleanfiles
703         @ \
704         if [ -z "$(SUPERCLEAN_FILES)" ]; then \
705                 echo "No generated files found."; \
706         else \
707                 echo "Deleting the following generated files:"; \
708                 echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \
709                 $(RM) $(SUPERCLEAN_FILES); \
710         fi
712 $(DIST_ALIASES): $(DISTRIBUTE_DIR)
714 $(DISTRIBUTE_DIR): all py | $(DISTRIBUTE_SUBDIRS)
715         # add proto
716         cp -r src/caffe/proto $(DISTRIBUTE_DIR)/
717         # add include
718         cp -r include $(DISTRIBUTE_DIR)/
719         cp -r 3rdparty/half_float $(DISTRIBUTE_DIR)/include
720         mkdir -p $(DISTRIBUTE_DIR)/include/caffe/proto
721         cp $(PROTO_GEN_HEADER_SRCS) $(DISTRIBUTE_DIR)/include/caffe/proto
722         # add tool and example binaries
723         cp $(TOOL_BINS) $(DISTRIBUTE_DIR)/bin
724         cp $(EXAMPLE_BINS) $(DISTRIBUTE_DIR)/bin
725         # add libraries
726         cp $(STATIC_NAME) $(DISTRIBUTE_DIR)/lib
727         install -m 644 $(DYNAMIC_NAME) $(DISTRIBUTE_DIR)/lib
728         cd $(DISTRIBUTE_DIR)/lib; rm -f $(DYNAMIC_SONAME_SHORT); ln -s $(DYNAMIC_VERSIONED_NAME_SHORT) $(DYNAMIC_SONAME_SHORT)
729         cd $(DISTRIBUTE_DIR)/lib; rm -f $(DYNAMIC_NAME_SHORT);   ln -s $(DYNAMIC_SONAME_SHORT) $(DYNAMIC_NAME_SHORT)
730         # add python - it's not the standard way, indeed...
731         cp -r python $(DISTRIBUTE_DIR)/python
733 -include $(DEPS)