]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - jacinto-ai/caffe-jacinto.git/blob - Makefile
Makefile py 3
[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)
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           := 16
39 DYNAMIC_VERSION_REVISION        := 5
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-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 ifneq ($(CPU_ONLY), 1)
188         INCLUDE_DIRS += $(CUDA_INCLUDE_DIR)
189         LIBRARY_DIRS += $(CUDA_LIB_DIR)
190         LIBRARIES := cudart cublas curand
191 ifneq ($(NO_NVML), 1)
192         LIBRARIES += nvidia-ml
193 endif
194 endif
196 # Note: libturbojpeg has a packaging bug. Workaround:
197 # $ sudo ln -s /usr/lib/x86_64-linux-gnu/libturbojpeg.so.0 /usr/lib/x86_64-linux-gnu/libturbojpeg.so
199 LIBRARIES += boost_system glog gflags protobuf boost_filesystem m turbojpeg
200 ifeq ($(TEGRA), 1)
201     LIBRARIES += hdf5_serial_hl hdf5_serial
202 else
203     LIBRARIES += hdf5_hl hdf5
204 endif
206 # handle IO dependencies
207 USE_LEVELDB ?= 1
208 USE_LMDB ?= 1
209 USE_OPENCV ?= 1
211 ifeq ($(USE_LEVELDB), 1)
212         LIBRARIES += leveldb snappy
213 endif
214 ifeq ($(USE_LMDB), 1)
215         LIBRARIES += lmdb
216 endif
217 ifeq ($(USE_OPENCV), 1)
218         LIBRARIES += opencv_core opencv_highgui opencv_imgproc
220         ifeq ($(OPENCV_VERSION), 3)
221                 LIBRARIES += opencv_imgcodecs
222         endif
223                 
224 endif
226 python_version_full := $(wordlist 2,4,$(subst ., ,$(shell python --version 2>&1)))
227 python_version_major := $(word 1,${python_version_full})
228 python_version_minor := $(word 2,${python_version_full})
229 python_version_patch := $(word 3,${python_version_full})
230 ifeq ($(python_version_major), 3)
231         python_lib_suffix := m
232 endif
234 PYTHON_LIBRARIES ?= boost_python python${python_version_major}.${python_version_minor}${python_lib_suffix} boost_regex
235 WARNINGS := -Wall -Wno-sign-compare
237 ##############################
238 # Set build directories
239 ##############################
241 DISTRIBUTE_DIR ?= distribute
242 DISTRIBUTE_SUBDIRS := $(DISTRIBUTE_DIR)/bin $(DISTRIBUTE_DIR)/lib
243 DIST_ALIASES := dist
244 ifneq ($(strip $(DISTRIBUTE_DIR)),distribute)
245                 DIST_ALIASES += distribute
246 endif
248 ALL_BUILD_DIRS := $(sort $(BUILD_DIR) $(addprefix $(BUILD_DIR)/, $(SRC_DIRS)) \
249         $(addprefix $(BUILD_DIR)/cuda/, $(SRC_DIRS)) \
250         $(LIB_BUILD_DIR) $(TEST_BIN_DIR) $(PY_PROTO_BUILD_DIR) $(LINT_OUTPUT_DIR) \
251         $(DISTRIBUTE_SUBDIRS) $(PROTO_BUILD_INCLUDE_DIR))
253 ##############################
254 # Set directory for Doxygen-generated documentation
255 ##############################
256 DOXYGEN_CONFIG_FILE ?= ./.Doxyfile
257 # should be the same as OUTPUT_DIRECTORY in the .Doxyfile
258 DOXYGEN_OUTPUT_DIR ?= ./doxygen
259 DOXYGEN_COMMAND ?= doxygen
260 # All the files that might have Doxygen documentation.
261 DOXYGEN_SOURCES := $(shell find \
262         src/$(PROJECT) \
263         include/$(PROJECT) \
264         python/ \
265         matlab/ \
266         examples \
267         tools \
268         -name "*.cpp" -or -name "*.hpp" -or -name "*.cu" -or -name "*.cuh" -or \
269         -name "*.py" -or -name "*.m")
270 DOXYGEN_SOURCES += $(DOXYGEN_CONFIG_FILE)
273 ##############################
274 # Configure build
275 ##############################
277 # Determine platform
278 UNAME := $(shell uname -s)
279 ifeq ($(UNAME), Linux)
280         LINUX := 1
281 else ifeq ($(UNAME), Darwin)
282         OSX := 1
283         OSX_MAJOR_VERSION := $(shell sw_vers -productVersion | cut -f 1 -d .)
284         OSX_MINOR_VERSION := $(shell sw_vers -productVersion | cut -f 2 -d .)
285 endif
287 # Linux
288 ifeq ($(LINUX), 1)
289         CXX ?= /usr/bin/g++
290         GCCVERSION := $(shell $(CXX) -dumpversion | cut -f1,2 -d.)
291         # older versions of gcc are too dumb to build boost with -Wuninitalized
292         ifeq ($(shell echo | awk '{exit $(GCCVERSION) < 4.6;}'), 1)
293                 WARNINGS += -Wno-uninitialized
294         endif
295         # boost::thread is reasonably called boost_thread (compare OS X)
296         # We will also explicitly add stdc++ to the link target.
297         LIBRARIES += boost_thread stdc++
298         VERSIONFLAGS += -Wl,-soname,$(DYNAMIC_SONAME_SHORT) -Wl,-rpath,$(ORIGIN)/../lib
299 endif
301 # OS X:
302 # clang++ instead of g++
303 # libstdc++ for NVCC compatibility on OS X >= 10.9 with CUDA < 7.0
304 ifeq ($(OSX), 1)
305         CXX := /usr/bin/clang++
306         ifneq ($(CPU_ONLY), 1)
307                 CUDA_VERSION := $(shell $(CUDA_DIR)/bin/nvcc -V | grep -o 'release [0-9.]*' | grep -o '[0-9.]*')
308                 ifeq ($(shell echo | awk '{exit $(CUDA_VERSION) < 7.0;}'), 1)
309                         CXXFLAGS += -stdlib=libstdc++
310                         LINKFLAGS += -stdlib=libstdc++
311                 endif
312                 # clang throws this warning for cuda headers
313                 WARNINGS += -Wno-unneeded-internal-declaration
314                 # 10.11 strips DYLD_* env vars so link CUDA (rpath is available on 10.5+)
315                 OSX_10_OR_LATER   := $(shell [ $(OSX_MAJOR_VERSION) -ge 10 ] && echo true)
316                 OSX_10_5_OR_LATER := $(shell [ $(OSX_MINOR_VERSION) -ge 5 ] && echo true)
317                 ifeq ($(OSX_10_OR_LATER),true)
318                         ifeq ($(OSX_10_5_OR_LATER),true)
319                                 LDFLAGS += -Wl,-rpath,$(CUDA_LIB_DIR)
320                         endif
321                 endif
322         endif
323         # gtest needs to use its own tuple to not conflict with clang
324         COMMON_FLAGS += -DGTEST_USE_OWN_TR1_TUPLE=1
325         # boost::thread is called boost_thread-mt to mark multithreading on OS X
326         LIBRARIES += boost_thread-mt
327         # we need to explicitly ask for the rpath to be obeyed
328         ORIGIN := @loader_path
329         VERSIONFLAGS += -Wl,-install_name,$(DYNAMIC_SONAME_SHORT) -Wl,-rpath,$(ORIGIN)/../../build/lib
330 else
331         ORIGIN := \$$ORIGIN
332 endif
334 # Custom compiler
335 ifdef CUSTOM_CXX
336         CXX := $(CUSTOM_CXX)
337 endif
339 # Static linking
340 ifneq (,$(findstring clang++,$(CXX)))
341         STATIC_LINK_COMMAND := -Wl,-force_load $(STATIC_NAME)
342 else ifneq (,$(findstring g++,$(CXX)))
343         STATIC_LINK_COMMAND := -Wl,--whole-archive $(STATIC_NAME) -Wl,--no-whole-archive
344 else
345   # The following line must not be indented with a tab, since we are not inside a target
346   $(error Cannot static link with the $(CXX) compiler)
347 endif
349 # Debugging
350 ifeq ($(DEBUG), 1)
351         COMMON_FLAGS += -DDEBUG -g -O0
352         NVCCFLAGS += -G
353 else
354         COMMON_FLAGS += -DNDEBUG -O2
355 endif
357 # cuDNN acceleration configuration.
358 ifeq ($(USE_CUDNN), 1)
359         ifdef CUDNN_ROOT
360                 CUDNN_DIR := $(CUDNN_ROOT)
361         endif
362         LIBRARIES += cudnn
363         INCLUDE_DIRS += $(CUDNN_DIR)/cuda/include $(CUDNN_DIR)/include $(CUDNN_DIR)
364         LIBRARY_DIRS += $(CUDNN_DIR)/cuda/lib64 $(CUDNN_DIR)/lib64 $(CUDNN_DIR)
365         COMMON_FLAGS += -DUSE_CUDNN
366 endif
368 # NCCL acceleration configuration
369 ifeq ($(USE_NCCL), 1)
370         LIBRARIES += nccl
371         COMMON_FLAGS += -DUSE_NCCL
372 endif
374 # configure IO libraries
375 ifeq ($(USE_OPENCV), 1)
376         COMMON_FLAGS += -DUSE_OPENCV
377 endif
378 ifeq ($(USE_LEVELDB), 1)
379         COMMON_FLAGS += -DUSE_LEVELDB
380 endif
381 ifeq ($(USE_LMDB), 1)
382         COMMON_FLAGS += -DUSE_LMDB
383 ifeq ($(ALLOW_LMDB_NOLOCK), 1)
384         COMMON_FLAGS += -DALLOW_LMDB_NOLOCK
385 endif
386 endif
388 # New place for HDF5
389 LIBRARY_DIRS += /usr/lib/x86_64-linux-gnu/hdf5/serial
391 # CPU-only configuration
392 ifeq ($(CPU_ONLY), 1)
393         OBJS := $(PROTO_OBJS) $(CXX_OBJS)
394         TEST_OBJS := $(TEST_CXX_OBJS)
395         TEST_BINS := $(TEST_CXX_BINS)
396         ALL_WARNS := $(ALL_CXX_WARNS)
397         TEST_FILTER := --gtest_filter="-*GPU*"
398         COMMON_FLAGS += -DCPU_ONLY
399 endif
401 ifeq ($(NO_NVML), 1)
402         COMMON_FLAGS += -DNO_NVML=1
403 endif
405 ifeq ($(TEST_FP16), 1)
406         COMMON_FLAGS += -DTEST_FP16=1
407 endif
409 # Python layer support
410 ifeq ($(WITH_PYTHON_LAYER), 1)
411         COMMON_FLAGS += -DWITH_PYTHON_LAYER
412         LIBRARIES += $(PYTHON_LIBRARIES)
413 endif
415 # BLAS configuration (default = ATLAS)
416 BLAS ?= atlas
417 ifeq ($(BLAS), mkl)
418         # MKL
419         LIBRARIES += mkl_rt
420         COMMON_FLAGS += -DUSE_MKL
421         MKLROOT ?= /opt/intel/mkl
422         BLAS_INCLUDE ?= $(MKLROOT)/include
423         BLAS_LIB ?= $(MKLROOT)/lib $(MKLROOT)/lib/intel64
424 else ifeq ($(BLAS), open)
425         # OpenBLAS
426         LIBRARIES += openblas
427 else
428         # ATLAS
429         ifeq ($(LINUX), 1)
430                 ifeq ($(BLAS), atlas)
431                         # Linux simply has cblas and atlas
432                         LIBRARIES += cblas atlas
433                 endif
434         else ifeq ($(OSX), 1)
435                 # OS X packages atlas as the vecLib framework
436                 LIBRARIES += cblas
437                 # 10.10 has accelerate while 10.9 has veclib
438                 XCODE_CLT_VER := $(shell pkgutil --pkg-info=com.apple.pkg.CLTools_Executables | grep 'version' | sed 's/[^0-9]*\([0-9]\).*/\1/')
439                 XCODE_CLT_GEQ_6 := $(shell [ $(XCODE_CLT_VER) -gt 5 ] && echo 1)
440                 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)
452 LIBRARY_DIRS += $(LIB_BUILD_DIR)
454 # Automatic dependency generation (nvcc is handled separately)
455 CXXFLAGS += -MMD -MP
457 # Complete build flags.
458 COMMON_FLAGS += $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir))
459 CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
460 NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
461 # mex may invoke an older gcc that is too liberal with -Wuninitalized
462 MATLAB_CXXFLAGS := $(CXXFLAGS) -Wno-uninitialized -std=c++11
463 LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
465 USE_PKG_CONFIG ?= 0
466 ifeq ($(USE_PKG_CONFIG), 1)
467         PKG_CONFIG := $(shell pkg-config opencv --libs)
468 else
469         PKG_CONFIG :=
470 endif
471 LDFLAGS += $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir)) $(PKG_CONFIG) \
472                 $(foreach library,$(LIBRARIES),-l$(library))
473 PYTHON_LDFLAGS := $(LDFLAGS) $(foreach library,$(PYTHON_LIBRARIES),-l$(library))
475 # 'superclean' target recursively* deletes all files ending with an extension
476 # in $(SUPERCLEAN_EXTS) below.  This may be useful if you've built older
477 # versions of Caffe that do not place all generated files in a location known
478 # to the 'clean' target.
480 # 'supercleanlist' will list the files to be deleted by make superclean.
482 # * Recursive with the exception that symbolic links are never followed, per the
483 # default behavior of 'find'.
484 SUPERCLEAN_EXTS := .so .a .o .bin .testbin .pb.cc .pb.h _pb2.py .cuo
486 # Set the sub-targets of the 'everything' target.
487 EVERYTHING_TARGETS := all py$(PROJECT) test warn lint
488 # Only build matcaffe as part of "everything" if MATLAB_DIR is specified.
489 ifneq ($(MATLAB_DIR),)
490         EVERYTHING_TARGETS += mat$(PROJECT)
491 endif
493 ##############################
494 # Define build targets
495 ##############################
496 .PHONY: all lib test clean docs linecount lint lintclean tools examples $(DIST_ALIASES) \
497         py mat py$(PROJECT) mat$(PROJECT) proto runtest \
498         superclean supercleanlist supercleanfiles warn everything
500 all: lib tools examples
502 lib: $(STATIC_NAME) $(DYNAMIC_NAME)
504 everything: $(EVERYTHING_TARGETS)
506 linecount:
507         cloc --read-lang-def=$(PROJECT).cloc \
508                 src/$(PROJECT) include/$(PROJECT) tools examples \
509                 python matlab
511 lint: $(EMPTY_LINT_REPORT)
513 lintclean:
514         @ $(RM) -r $(LINT_OUTPUT_DIR) $(EMPTY_LINT_REPORT) $(NONEMPTY_LINT_REPORT)
516 docs: $(DOXYGEN_OUTPUT_DIR)
517         @ cd ./docs ; ln -sfn ../$(DOXYGEN_OUTPUT_DIR)/html doxygen
519 $(DOXYGEN_OUTPUT_DIR): $(DOXYGEN_CONFIG_FILE) $(DOXYGEN_SOURCES)
520         $(DOXYGEN_COMMAND) $(DOXYGEN_CONFIG_FILE)
522 $(EMPTY_LINT_REPORT): $(LINT_OUTPUTS) | $(BUILD_DIR)
523         @ cat $(LINT_OUTPUTS) > $@
524         @ if [ -s "$@" ]; then \
525                 cat $@; \
526                 mv $@ $(NONEMPTY_LINT_REPORT); \
527                 echo "Found one or more lint errors."; \
528                 exit 1; \
529           fi; \
530           $(RM) $(NONEMPTY_LINT_REPORT); \
531           echo "No lint errors!";
533 $(LINT_OUTPUTS): $(LINT_OUTPUT_DIR)/%.lint.txt : % $(LINT_SCRIPT) | $(LINT_OUTPUT_DIR)
534         @ mkdir -p $(dir $@)
535         @ python $(LINT_SCRIPT) --filter=-legal,-build/include,-runtime/references,-readability,-whitespace/comments $< 2>&1 \
536                 | grep -v "^Done processing " \
537                 | grep -v "^Total errors found: 0" \
538                 > $@ \
539                 || true
541 test: $(TEST_ALL_BIN) $(TEST_ALL_DYNLINK_BIN) $(TEST_BINS)
543 tools: $(TOOL_BINS) $(TOOL_BIN_LINKS)
545 examples: $(EXAMPLE_BINS)
547 py$(PROJECT): py
549 py: $(PY$(PROJECT)_SO) $(PROTO_GEN_PY)
551 $(PY$(PROJECT)_SO): $(PY$(PROJECT)_OBJ) $(PY$(PROJECT)_SRC) $(PY$(PROJECT)_HXX) | $(DYNAMIC_NAME)
552         @ echo CXX/LD -o $@ $<
553         $(Q)$(CXX) -shared -o $@ $(PY$(PROJECT)_SRC) $(PY$(PROJECT)_OBJ) \
554                 $(LINKFLAGS) -l$(LIBRARY_NAME) $(PYTHON_LDFLAGS) \
555                 -Wl,-rpath,$(ORIGIN)/../../build/lib
557 mat$(PROJECT): mat
559 mat: $(MAT$(PROJECT)_SO)
561 $(MAT$(PROJECT)_SO): $(MAT$(PROJECT)_SRC) $(STATIC_NAME)
562         @ if [ -z "$(MATLAB_DIR)" ]; then \
563                 echo "MATLAB_DIR must be specified in $(CONFIG_FILE)" \
564                         "to build mat$(PROJECT)."; \
565                 exit 1; \
566         fi
567         @ echo MEX $<
568         $(Q)$(MATLAB_DIR)/bin/mex $(MAT$(PROJECT)_SRC) \
569                         CXX="$(CXX)" \
570                         CXXFLAGS="\$$CXXFLAGS $(MATLAB_CXXFLAGS)" \
571                         CXXLIBS="\$$CXXLIBS $(STATIC_LINK_COMMAND) $(LDFLAGS)" -output $@
572         @ if [ -f "$(PROJECT)_.d" ]; then \
573                 mv -f $(PROJECT)_.d $(BUILD_DIR)/${MAT$(PROJECT)_SO:.$(MAT_SO_EXT)=.d}; \
574         fi
576 runtest: $(TEST_ALL_BIN)
577         $(TOOL_BUILD_DIR)/caffe
578         $(TEST_ALL_BIN) $(TEST_GPUID) --gtest_shuffle $(TEST_FILTER)
580 pytest: py
581         cd python; python -u -m unittest discover -s caffe/test
583 mattest: mat
584         cd matlab; $(MATLAB_DIR)/bin/matlab -nodisplay -r 'caffe.run_tests(), exit()'
586 warn: $(EMPTY_WARN_REPORT)
588 $(EMPTY_WARN_REPORT): $(ALL_WARNS) | $(BUILD_DIR)
589         @ cat $(ALL_WARNS) > $@
590         @ if [ -s "$@" ]; then \
591                 cat $@; \
592                 mv $@ $(NONEMPTY_WARN_REPORT); \
593                 echo "Compiler produced one or more warnings."; \
594                 exit 1; \
595           fi; \
596           $(RM) $(NONEMPTY_WARN_REPORT); \
597           echo "No compiler warnings!";
599 $(ALL_WARNS): %.o.$(WARNS_EXT) : %.o
601 $(BUILD_DIR_LINK): $(BUILD_DIR)/.linked
603 # Create a target ".linked" in this BUILD_DIR to tell Make that the "build" link
604 # is currently correct, then delete the one in the OTHER_BUILD_DIR in case it
605 # exists and $(DEBUG) is toggled later.
606 $(BUILD_DIR)/.linked:
607         @ mkdir -p $(BUILD_DIR)
608         @ $(RM) $(OTHER_BUILD_DIR)/.linked
609         @ $(RM) -r $(BUILD_DIR_LINK)
610         @ ln -s $(BUILD_DIR) $(BUILD_DIR_LINK)
611         @ touch $@
613 $(ALL_BUILD_DIRS): | $(BUILD_DIR_LINK)
614         @ mkdir -p $@
616 $(DYNAMIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
617         @ echo LD -o $@
618         $(Q)$(CXX) -shared -o $@ $(OBJS) $(VERSIONFLAGS) $(LINKFLAGS) $(LDFLAGS) $(DYNAMIC_FLAGS)
619         @ cd $(BUILD_DIR)/lib; rm -f $(DYNAMIC_SONAME_SHORT); ln -s $(DYNAMIC_VERSIONED_NAME_SHORT) $(DYNAMIC_SONAME_SHORT)
620         @ cd $(BUILD_DIR)/lib; rm -f $(DYNAMIC_NAME_SHORT);   ln -s $(DYNAMIC_SONAME_SHORT) $(DYNAMIC_NAME_SHORT)
622 $(STATIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
623         @ echo AR -o $@
624         $(Q)ar rcs $@ $(OBJS)
626 $(BUILD_DIR)/%.o: %.cpp | $(ALL_BUILD_DIRS)
627         @ echo CXX $<
628         $(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
629                 || (cat $@.$(WARNS_EXT); exit 1)
630         @ cat $@.$(WARNS_EXT)
632 $(PROTO_BUILD_DIR)/%.pb.o: $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_GEN_HEADER) \
633                 | $(PROTO_BUILD_DIR)
634         @ echo CXX $<
635         $(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
636                 || (cat $@.$(WARNS_EXT); exit 1)
637         @ cat $@.$(WARNS_EXT)
639 $(BUILD_DIR)/cuda/%.o: %.cu | $(ALL_BUILD_DIRS)
640         @ echo NVCC $<
641         $(Q)$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -M $< -o ${@:.o=.d} \
642                 -odir $(@D)
643         $(Q)$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -c $< -o $@ 2> $@.$(WARNS_EXT) \
644                 || (cat $@.$(WARNS_EXT); exit 1)
645         @ cat $@.$(WARNS_EXT)
647 $(TEST_ALL_BIN): $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) \
648                 | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
649         @ echo CXX/LD -o $@ $<
650         $(Q)$(CXX) $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) \
651                 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib
653 $(TEST_CU_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CU_BUILD_DIR)/%.o \
654         $(GTEST_OBJ) | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
655         @ echo LD $<
656         $(Q)$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) \
657                 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib
659 $(TEST_CXX_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CXX_BUILD_DIR)/%.o \
660         $(GTEST_OBJ) | $(DYNAMIC_NAME) $(TEST_BIN_DIR)
661         @ echo LD $<
662         $(Q)$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) \
663                 -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib
665 # Target for extension-less symlinks to tool binaries with extension '*.bin'.
666 $(TOOL_BUILD_DIR)/%: $(TOOL_BUILD_DIR)/%.bin | $(TOOL_BUILD_DIR)
667         @ $(RM) $@
668         @ ln -s $(notdir $<) $@
670 $(TOOL_BINS): %.bin : %.o | $(DYNAMIC_NAME)
671         @ echo CXX/LD -o $@
672         $(Q)$(CXX) $< -o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(LDFLAGS) \
673                 -Wl,-rpath,$(ORIGIN)/../lib
675 $(EXAMPLE_BINS): %.bin : %.o | $(DYNAMIC_NAME)
676         @ echo CXX/LD -o $@
677         $(Q)$(CXX) $< -o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(LDFLAGS) \
678                 -Wl,-rpath,$(ORIGIN)/../../lib
680 proto: $(PROTO_GEN_CC) $(PROTO_GEN_HEADER)
682 $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_BUILD_DIR)/%.pb.h : \
683                 $(PROTO_SRC_DIR)/%.proto | $(PROTO_BUILD_DIR)
684         @ echo PROTOC $<
685         $(Q)protoc --proto_path=$(PROTO_SRC_DIR) --cpp_out=$(PROTO_BUILD_DIR) $<
687 $(PY_PROTO_BUILD_DIR)/%_pb2.py : $(PROTO_SRC_DIR)/%.proto \
688                 $(PY_PROTO_INIT) | $(PY_PROTO_BUILD_DIR)
689         @ echo PROTOC \(python\) $<
690         $(Q)protoc --proto_path=$(PROTO_SRC_DIR) --python_out=$(PY_PROTO_BUILD_DIR) $<
692 $(PY_PROTO_INIT): | $(PY_PROTO_BUILD_DIR)
693         touch $(PY_PROTO_INIT)
695 clean:
696         @- $(RM) -rf $(ALL_BUILD_DIRS)
697         @- $(RM) -rf $(OTHER_BUILD_DIR)
698         @- $(RM) -rf $(BUILD_DIR_LINK)
699         @- $(RM) -rf $(DISTRIBUTE_DIR)
700         @- $(RM) $(PY$(PROJECT)_SO)
701         @- $(RM) $(MAT$(PROJECT)_SO)
703 supercleanfiles:
704         $(eval SUPERCLEAN_FILES := $(strip \
705                         $(foreach ext,$(SUPERCLEAN_EXTS), $(shell find . -name '*$(ext)' \
706                         -not -path './data/*'))))
708 supercleanlist: supercleanfiles
709         @ \
710         if [ -z "$(SUPERCLEAN_FILES)" ]; then \
711                 echo "No generated files found."; \
712         else \
713                 echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \
714         fi
716 superclean: clean supercleanfiles
717         @ \
718         if [ -z "$(SUPERCLEAN_FILES)" ]; then \
719                 echo "No generated files found."; \
720         else \
721                 echo "Deleting the following generated files:"; \
722                 echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \
723                 $(RM) $(SUPERCLEAN_FILES); \
724         fi
726 $(DIST_ALIASES): $(DISTRIBUTE_DIR)
728 $(DISTRIBUTE_DIR): all py | $(DISTRIBUTE_SUBDIRS)
729         # add proto
730         cp -r src/caffe/proto $(DISTRIBUTE_DIR)/
731         # add include
732         cp -r include $(DISTRIBUTE_DIR)/
733         cp -r 3rdparty/half_float $(DISTRIBUTE_DIR)/include
734         mkdir -p $(DISTRIBUTE_DIR)/include/caffe/proto
735         cp $(PROTO_GEN_HEADER_SRCS) $(DISTRIBUTE_DIR)/include/caffe/proto
736         # add tool and example binaries
737         cp $(TOOL_BINS) $(DISTRIBUTE_DIR)/bin
738         cp $(EXAMPLE_BINS) $(DISTRIBUTE_DIR)/bin
739         # add libraries
740         cp $(STATIC_NAME) $(DISTRIBUTE_DIR)/lib
741         install -m 644 $(DYNAMIC_NAME) $(DISTRIBUTE_DIR)/lib
742         cd $(DISTRIBUTE_DIR)/lib; rm -f $(DYNAMIC_SONAME_SHORT); ln -s $(DYNAMIC_VERSIONED_NAME_SHORT) $(DYNAMIC_SONAME_SHORT)
743         cd $(DISTRIBUTE_DIR)/lib; rm -f $(DYNAMIC_NAME_SHORT);   ln -s $(DYNAMIC_SONAME_SHORT) $(DYNAMIC_NAME_SHORT)
744         # add python - it's not the standard way, indeed...
745         cp -r python $(DISTRIBUTE_DIR)/python
747 -include $(DEPS)