caffe
/
Makefile
709 строк · 23.8 Кб
1PROJECT := caffe2
3CONFIG_FILE := Makefile.config4# Explicitly check for the config file, otherwise make -k will proceed anyway.
5ifeq ($(wildcard $(CONFIG_FILE)),)6$(error $(CONFIG_FILE) not found. See $(CONFIG_FILE).example.)7endif
8include $(CONFIG_FILE)9
10BUILD_DIR_LINK := $(BUILD_DIR)11ifeq ($(RELEASE_BUILD_DIR),)12RELEASE_BUILD_DIR := .$(BUILD_DIR)_release13endif
14ifeq ($(DEBUG_BUILD_DIR),)15DEBUG_BUILD_DIR := .$(BUILD_DIR)_debug16endif
17
18DEBUG ?= 019ifeq ($(DEBUG), 1)20BUILD_DIR := $(DEBUG_BUILD_DIR)21OTHER_BUILD_DIR := $(RELEASE_BUILD_DIR)22else
23BUILD_DIR := $(RELEASE_BUILD_DIR)24OTHER_BUILD_DIR := $(DEBUG_BUILD_DIR)25endif
26
27# All of the directories containing code.
28SRC_DIRS := $(shell find * -type d -exec bash -c "find {} -maxdepth 1 \29\( -name '*.cpp' -o -name '*.proto' \) | grep -q ." \; -print)30
31# The target shared library name
32LIBRARY_NAME := $(PROJECT)33LIB_BUILD_DIR := $(BUILD_DIR)/lib34STATIC_NAME := $(LIB_BUILD_DIR)/lib$(LIBRARY_NAME).a35DYNAMIC_VERSION_MAJOR := 136DYNAMIC_VERSION_MINOR := 037DYNAMIC_VERSION_REVISION := 038DYNAMIC_NAME_SHORT := lib$(LIBRARY_NAME).so39#DYNAMIC_SONAME_SHORT := $(DYNAMIC_NAME_SHORT).$(DYNAMIC_VERSION_MAJOR)
40DYNAMIC_VERSIONED_NAME_SHORT := $(DYNAMIC_NAME_SHORT).$(DYNAMIC_VERSION_MAJOR).$(DYNAMIC_VERSION_MINOR).$(DYNAMIC_VERSION_REVISION)41DYNAMIC_NAME := $(LIB_BUILD_DIR)/$(DYNAMIC_VERSIONED_NAME_SHORT)42COMMON_FLAGS += -DCAFFE_VERSION=$(DYNAMIC_VERSION_MAJOR).$(DYNAMIC_VERSION_MINOR).$(DYNAMIC_VERSION_REVISION)43
44##############################
45# Get all source files
46##############################
47# CXX_SRCS are the source files excluding the test ones.
48CXX_SRCS := $(shell find src/$(PROJECT) ! -name "test_*.cpp" -name "*.cpp")49# CU_SRCS are the cuda source files
50CU_SRCS := $(shell find src/$(PROJECT) ! -name "test_*.cu" -name "*.cu")51# TEST_SRCS are the test source files
52TEST_MAIN_SRC := src/$(PROJECT)/test/test_caffe_main.cpp53TEST_SRCS := $(shell find src/$(PROJECT) -name "test_*.cpp")54TEST_SRCS := $(filter-out $(TEST_MAIN_SRC), $(TEST_SRCS))55TEST_CU_SRCS := $(shell find src/$(PROJECT) -name "test_*.cu")56GTEST_SRC := src/gtest/gtest-all.cpp57# TOOL_SRCS are the source files for the tool binaries
58TOOL_SRCS := $(shell find tools -name "*.cpp")59# EXAMPLE_SRCS are the source files for the example binaries
60EXAMPLE_SRCS := $(shell find examples -name "*.cpp")61# BUILD_INCLUDE_DIR contains any generated header files we want to include.
62BUILD_INCLUDE_DIR := $(BUILD_DIR)/src63# PROTO_SRCS are the protocol buffer definitions
64PROTO_SRC_DIR := src/$(PROJECT)/proto65PROTO_SRCS := $(wildcard $(PROTO_SRC_DIR)/*.proto)66# PROTO_BUILD_DIR will contain the .cc and obj files generated from
67# PROTO_SRCS; PROTO_BUILD_INCLUDE_DIR will contain the .h header files
68PROTO_BUILD_DIR := $(BUILD_DIR)/$(PROTO_SRC_DIR)69PROTO_BUILD_INCLUDE_DIR := $(BUILD_INCLUDE_DIR)/$(PROJECT)/proto70# NONGEN_CXX_SRCS includes all source/header files except those generated
71# automatically (e.g., by proto).
72NONGEN_CXX_SRCS := $(shell find \73src/$(PROJECT) \74include/$(PROJECT) \75python/$(PROJECT) \76matlab/+$(PROJECT)/private \77examples \
78tools \
79-name "*.cpp" -or -name "*.hpp" -or -name "*.cu" -or -name "*.cuh")80LINT_SCRIPT := scripts/cpp_lint.py81LINT_OUTPUT_DIR := $(BUILD_DIR)/.lint82LINT_EXT := lint.txt83LINT_OUTPUTS := $(addsuffix .$(LINT_EXT), $(addprefix $(LINT_OUTPUT_DIR)/, $(NONGEN_CXX_SRCS)))84EMPTY_LINT_REPORT := $(BUILD_DIR)/.$(LINT_EXT)85NONEMPTY_LINT_REPORT := $(BUILD_DIR)/$(LINT_EXT)86# PY$(PROJECT)_SRC is the python wrapper for $(PROJECT)
87PY$(PROJECT)_SRC := python/$(PROJECT)/_$(PROJECT).cpp88PY$(PROJECT)_SO := python/$(PROJECT)/_$(PROJECT).so89PY$(PROJECT)_HXX := include/$(PROJECT)/layers/python_layer.hpp90# MAT$(PROJECT)_SRC is the mex entrance point of matlab package for $(PROJECT)
91MAT$(PROJECT)_SRC := matlab/+$(PROJECT)/private/$(PROJECT)_.cpp92ifneq ($(MATLAB_DIR),)93MAT_SO_EXT := $(shell $(MATLAB_DIR)/bin/mexext)94endif
95MAT$(PROJECT)_SO := matlab/+$(PROJECT)/private/$(PROJECT)_.$(MAT_SO_EXT)96
97##############################
98# Derive generated files
99##############################
100# The generated files for protocol buffers
101PROTO_GEN_HEADER_SRCS := $(addprefix $(PROTO_BUILD_DIR)/, \102$(notdir ${PROTO_SRCS:.proto=.pb.h}))103PROTO_GEN_HEADER := $(addprefix $(PROTO_BUILD_INCLUDE_DIR)/, \104$(notdir ${PROTO_SRCS:.proto=.pb.h}))105PROTO_GEN_CC := $(addprefix $(BUILD_DIR)/, ${PROTO_SRCS:.proto=.pb.cc})106PY_PROTO_BUILD_DIR := python/$(PROJECT)/proto107PY_PROTO_INIT := python/$(PROJECT)/proto/__init__.py108PROTO_GEN_PY := $(foreach file,${PROTO_SRCS:.proto=_pb2.py}, \109$(PY_PROTO_BUILD_DIR)/$(notdir $(file)))110# The objects corresponding to the source files
111# These objects will be linked into the final shared library, so we
112# exclude the tool, example, and test objects.
113CXX_OBJS := $(addprefix $(BUILD_DIR)/, ${CXX_SRCS:.cpp=.o})114CU_OBJS := $(addprefix $(BUILD_DIR)/cuda/, ${CU_SRCS:.cu=.o})115PROTO_OBJS := ${PROTO_GEN_CC:.cc=.o}116OBJS := $(PROTO_OBJS) $(CXX_OBJS) $(CU_OBJS)117# tool, example, and test objects
118TOOL_OBJS := $(addprefix $(BUILD_DIR)/, ${TOOL_SRCS:.cpp=.o})119TOOL_BUILD_DIR := $(BUILD_DIR)/tools120TEST_CXX_BUILD_DIR := $(BUILD_DIR)/src/$(PROJECT)/test121TEST_CU_BUILD_DIR := $(BUILD_DIR)/cuda/src/$(PROJECT)/test122TEST_CXX_OBJS := $(addprefix $(BUILD_DIR)/, ${TEST_SRCS:.cpp=.o})123TEST_CU_OBJS := $(addprefix $(BUILD_DIR)/cuda/, ${TEST_CU_SRCS:.cu=.o})124TEST_OBJS := $(TEST_CXX_OBJS) $(TEST_CU_OBJS)125GTEST_OBJ := $(addprefix $(BUILD_DIR)/, ${GTEST_SRC:.cpp=.o})126EXAMPLE_OBJS := $(addprefix $(BUILD_DIR)/, ${EXAMPLE_SRCS:.cpp=.o})127# Output files for automatic dependency generation
128DEPS := ${CXX_OBJS:.o=.d} ${CU_OBJS:.o=.d} ${TEST_CXX_OBJS:.o=.d} \129${TEST_CU_OBJS:.o=.d} $(BUILD_DIR)/${MAT$(PROJECT)_SO:.$(MAT_SO_EXT)=.d}130# tool, example, and test bins
131TOOL_BINS := ${TOOL_OBJS:.o=.bin}132EXAMPLE_BINS := ${EXAMPLE_OBJS:.o=.bin}133# symlinks to tool bins without the ".bin" extension
134TOOL_BIN_LINKS := ${TOOL_BINS:.bin=}135# Put the test binaries in build/test for convenience.
136TEST_BIN_DIR := $(BUILD_DIR)/test137TEST_CU_BINS := $(addsuffix .testbin,$(addprefix $(TEST_BIN_DIR)/, \138$(foreach obj,$(TEST_CU_OBJS),$(basename $(notdir $(obj))))))139TEST_CXX_BINS := $(addsuffix .testbin,$(addprefix $(TEST_BIN_DIR)/, \140$(foreach obj,$(TEST_CXX_OBJS),$(basename $(notdir $(obj))))))141TEST_BINS := $(TEST_CXX_BINS) $(TEST_CU_BINS)142# TEST_ALL_BIN is the test binary that links caffe dynamically.
143TEST_ALL_BIN := $(TEST_BIN_DIR)/test_all.testbin144
145##############################
146# Derive compiler warning dump locations
147##############################
148WARNS_EXT := warnings.txt149CXX_WARNS := $(addprefix $(BUILD_DIR)/, ${CXX_SRCS:.cpp=.o.$(WARNS_EXT)})150CU_WARNS := $(addprefix $(BUILD_DIR)/cuda/, ${CU_SRCS:.cu=.o.$(WARNS_EXT)})151TOOL_WARNS := $(addprefix $(BUILD_DIR)/, ${TOOL_SRCS:.cpp=.o.$(WARNS_EXT)})152EXAMPLE_WARNS := $(addprefix $(BUILD_DIR)/, ${EXAMPLE_SRCS:.cpp=.o.$(WARNS_EXT)})153TEST_WARNS := $(addprefix $(BUILD_DIR)/, ${TEST_SRCS:.cpp=.o.$(WARNS_EXT)})154TEST_CU_WARNS := $(addprefix $(BUILD_DIR)/cuda/, ${TEST_CU_SRCS:.cu=.o.$(WARNS_EXT)})155ALL_CXX_WARNS := $(CXX_WARNS) $(TOOL_WARNS) $(EXAMPLE_WARNS) $(TEST_WARNS)156ALL_CU_WARNS := $(CU_WARNS) $(TEST_CU_WARNS)157ALL_WARNS := $(ALL_CXX_WARNS) $(ALL_CU_WARNS)158
159EMPTY_WARN_REPORT := $(BUILD_DIR)/.$(WARNS_EXT)160NONEMPTY_WARN_REPORT := $(BUILD_DIR)/$(WARNS_EXT)161
162##############################
163# Derive include and lib directories
164##############################
165CUDA_INCLUDE_DIR := $(CUDA_DIR)/include166
167CUDA_LIB_DIR :=168# add <cuda>/lib64 only if it exists
169ifneq ("$(wildcard $(CUDA_DIR)/lib64)","")170CUDA_LIB_DIR += $(CUDA_DIR)/lib64171endif
172CUDA_LIB_DIR += $(CUDA_DIR)/lib173
174INCLUDE_DIRS += $(BUILD_INCLUDE_DIR) ./src ./include175ifneq ($(CPU_ONLY), 1)176INCLUDE_DIRS += $(CUDA_INCLUDE_DIR)177LIBRARY_DIRS += $(CUDA_LIB_DIR)178LIBRARIES := cudart cublas curand179endif
180
181LIBRARIES += glog gflags protobuf boost_system boost_filesystem m182
183# handle IO dependencies
184USE_LEVELDB ?= 1185USE_LMDB ?= 1186# This code is taken from https://github.com/sh1r0/caffe-android-lib
187USE_HDF5 ?= 1188USE_OPENCV ?= 1189
190ifeq ($(USE_LEVELDB), 1)191LIBRARIES += leveldb snappy192endif
193ifeq ($(USE_LMDB), 1)194LIBRARIES += lmdb195endif
196# This code is taken from https://github.com/sh1r0/caffe-android-lib
197ifeq ($(USE_HDF5), 1)198LIBRARIES += hdf5_hl hdf5199endif
200ifeq ($(USE_OPENCV), 1)201LIBRARIES += opencv_core opencv_highgui opencv_imgproc202
203ifeq ($(OPENCV_VERSION), 3)204LIBRARIES += opencv_imgcodecs205endif206
207endif
208PYTHON_LIBRARIES ?= boost_python python2.7209WARNINGS := -Wall -Wno-sign-compare210
211##############################
212# Set build directories
213##############################
214
215DISTRIBUTE_DIR ?= distribute216DISTRIBUTE_SUBDIRS := $(DISTRIBUTE_DIR)/bin $(DISTRIBUTE_DIR)/lib217DIST_ALIASES := dist218ifneq ($(strip $(DISTRIBUTE_DIR)),distribute)219DIST_ALIASES += distribute220endif
221
222ALL_BUILD_DIRS := $(sort $(BUILD_DIR) $(addprefix $(BUILD_DIR)/, $(SRC_DIRS)) \223$(addprefix $(BUILD_DIR)/cuda/, $(SRC_DIRS)) \224$(LIB_BUILD_DIR) $(TEST_BIN_DIR) $(PY_PROTO_BUILD_DIR) $(LINT_OUTPUT_DIR) \225$(DISTRIBUTE_SUBDIRS) $(PROTO_BUILD_INCLUDE_DIR))226
227##############################
228# Set directory for Doxygen-generated documentation
229##############################
230DOXYGEN_CONFIG_FILE ?= ./.Doxyfile231# should be the same as OUTPUT_DIRECTORY in the .Doxyfile
232DOXYGEN_OUTPUT_DIR ?= ./doxygen233DOXYGEN_COMMAND ?= doxygen234# All the files that might have Doxygen documentation.
235DOXYGEN_SOURCES := $(shell find \236src/$(PROJECT) \237include/$(PROJECT) \238python/ \
239matlab/ \
240examples \
241tools \
242-name "*.cpp" -or -name "*.hpp" -or -name "*.cu" -or -name "*.cuh" -or \243-name "*.py" -or -name "*.m")244DOXYGEN_SOURCES += $(DOXYGEN_CONFIG_FILE)245
246
247##############################
248# Configure build
249##############################
250
251# Determine platform
252UNAME := $(shell uname -s)253ifeq ($(UNAME), Linux)254LINUX := 1255else ifeq ($(UNAME), Darwin)256OSX := 1257OSX_MAJOR_VERSION := $(shell sw_vers -productVersion | cut -f 1 -d .)258OSX_MINOR_VERSION := $(shell sw_vers -productVersion | cut -f 2 -d .)259endif
260
261# Linux
262ifeq ($(LINUX), 1)263CXX ?= /usr/bin/g++264GCCVERSION := $(shell $(CXX) -dumpversion | cut -f1,2 -d.)265# older versions of gcc are too dumb to build boost with -Wuninitalized266ifeq ($(shell echo | awk '{exit $(GCCVERSION) < 4.6;}'), 1)267WARNINGS += -Wno-uninitialized268endif269# boost::thread is reasonably called boost_thread (compare OS X)270# We will also explicitly add stdc++ to the link target.271LIBRARIES += boost_thread stdc++272VERSIONFLAGS += -Wl,-soname,$(DYNAMIC_VERSIONED_NAME_SHORT) -Wl,-rpath,$(ORIGIN)/../lib273endif
274
275# OS X:
276# clang++ instead of g++
277# libstdc++ for NVCC compatibility on OS X >= 10.9 with CUDA < 7.0
278ifeq ($(OSX), 1)279CXX := /usr/bin/clang++280ifneq ($(CPU_ONLY), 1)281CUDA_VERSION := $(shell $(CUDA_DIR)/bin/nvcc -V | grep -o 'release [0-9.]*' | tr -d '[a-z ]')282ifeq ($(shell echo | awk '{exit $(CUDA_VERSION) < 7.0;}'), 1)283CXXFLAGS += -stdlib=libstdc++284LINKFLAGS += -stdlib=libstdc++285endif286# clang throws this warning for cuda headers287WARNINGS += -Wno-unneeded-internal-declaration288# 10.11 strips DYLD_* env vars so link CUDA (rpath is available on 10.5+)289OSX_10_OR_LATER := $(shell [ $(OSX_MAJOR_VERSION) -ge 10 ] && echo true)290OSX_10_5_OR_LATER := $(shell [ $(OSX_MINOR_VERSION) -ge 5 ] && echo true)291ifeq ($(OSX_10_OR_LATER),true)292ifeq ($(OSX_10_5_OR_LATER),true)293LDFLAGS += -Wl,-rpath,$(CUDA_LIB_DIR)294endif295endif296endif297# gtest needs to use its own tuple to not conflict with clang298COMMON_FLAGS += -DGTEST_USE_OWN_TR1_TUPLE=1299# boost::thread is called boost_thread-mt to mark multithreading on OS X300LIBRARIES += boost_thread-mt301# we need to explicitly ask for the rpath to be obeyed302ORIGIN := @loader_path303VERSIONFLAGS += -Wl,-install_name,@rpath/$(DYNAMIC_VERSIONED_NAME_SHORT) -Wl,-rpath,$(ORIGIN)/../../build/lib304else
305ORIGIN := \$$ORIGIN306endif
307
308# Custom compiler
309ifdef CUSTOM_CXX310CXX := $(CUSTOM_CXX)311endif
312
313# Static linking
314ifneq (,$(findstring clang++,$(CXX)))315STATIC_LINK_COMMAND := -Wl,-force_load $(STATIC_NAME)316else ifneq (,$(findstring g++,$(CXX)))317STATIC_LINK_COMMAND := -Wl,--whole-archive $(STATIC_NAME) -Wl,--no-whole-archive318else
319# The following line must not be indented with a tab, since we are not inside a target320$(error Cannot static link with the $(CXX) compiler)321endif
322
323# Debugging
324ifeq ($(DEBUG), 1)325COMMON_FLAGS += -DDEBUG -g -O0326NVCCFLAGS += -G327else
328COMMON_FLAGS += -DNDEBUG -O2329endif
330
331# cuDNN acceleration configuration.
332ifeq ($(USE_CUDNN), 1)333LIBRARIES += cudnn334COMMON_FLAGS += -DUSE_CUDNN335endif
336
337# NCCL acceleration configuration
338ifeq ($(USE_NCCL), 1)339LIBRARIES += nccl340COMMON_FLAGS += -DUSE_NCCL341endif
342
343# configure IO libraries
344ifeq ($(USE_OPENCV), 1)345COMMON_FLAGS += -DUSE_OPENCV346endif
347ifeq ($(USE_LEVELDB), 1)348COMMON_FLAGS += -DUSE_LEVELDB349endif
350ifeq ($(USE_LMDB), 1)351COMMON_FLAGS += -DUSE_LMDB352ifeq ($(ALLOW_LMDB_NOLOCK), 1)353COMMON_FLAGS += -DALLOW_LMDB_NOLOCK354endif
355endif
356# This code is taken from https://github.com/sh1r0/caffe-android-lib
357ifeq ($(USE_HDF5), 1)358COMMON_FLAGS += -DUSE_HDF5359endif
360
361# CPU-only configuration
362ifeq ($(CPU_ONLY), 1)363OBJS := $(PROTO_OBJS) $(CXX_OBJS)364TEST_OBJS := $(TEST_CXX_OBJS)365TEST_BINS := $(TEST_CXX_BINS)366ALL_WARNS := $(ALL_CXX_WARNS)367TEST_FILTER := --gtest_filter="-*GPU*"368COMMON_FLAGS += -DCPU_ONLY369endif
370
371# Python layer support
372ifeq ($(WITH_PYTHON_LAYER), 1)373COMMON_FLAGS += -DWITH_PYTHON_LAYER374LIBRARIES += $(PYTHON_LIBRARIES)375endif
376
377# BLAS configuration (default = ATLAS)
378BLAS ?= atlas379ifeq ($(BLAS), mkl)380# MKL381LIBRARIES += mkl_rt382COMMON_FLAGS += -DUSE_MKL383MKLROOT ?= /opt/intel/mkl384BLAS_INCLUDE ?= $(MKLROOT)/include385BLAS_LIB ?= $(MKLROOT)/lib $(MKLROOT)/lib/intel64386else ifeq ($(BLAS), open)387# OpenBLAS388LIBRARIES += openblas389else
390# ATLAS391ifeq ($(LINUX), 1)392ifeq ($(BLAS), atlas)393# Linux simply has cblas and atlas394LIBRARIES += cblas atlas395endif396else ifeq ($(OSX), 1)397# OS X packages atlas as the vecLib framework398LIBRARIES += cblas399# 10.10 has accelerate while 10.9 has veclib400XCODE_CLT_VER := $(shell pkgutil --pkg-info=com.apple.pkg.CLTools_Executables | grep 'version' | sed 's/[^0-9]*\([0-9]\).*/\1/')401XCODE_CLT_GEQ_7 := $(shell [ $(XCODE_CLT_VER) -gt 6 ] && echo 1)402XCODE_CLT_GEQ_6 := $(shell [ $(XCODE_CLT_VER) -gt 5 ] && echo 1)403ifeq ($(XCODE_CLT_GEQ_7), 1)404BLAS_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/Headers405else ifeq ($(XCODE_CLT_GEQ_6), 1)406BLAS_INCLUDE ?= /System/Library/Frameworks/Accelerate.framework/Versions/Current/Frameworks/vecLib.framework/Headers/407LDFLAGS += -framework Accelerate408else409BLAS_INCLUDE ?= /System/Library/Frameworks/vecLib.framework/Versions/Current/Headers/410LDFLAGS += -framework vecLib411endif412endif413endif
414INCLUDE_DIRS += $(BLAS_INCLUDE)415LIBRARY_DIRS += $(BLAS_LIB)416
417LIBRARY_DIRS += $(LIB_BUILD_DIR)418
419# Automatic dependency generation (nvcc is handled separately)
420CXXFLAGS += -MMD -MP421
422# Complete build flags.
423COMMON_FLAGS += $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir))424CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)425NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)426# mex may invoke an older gcc that is too liberal with -Wuninitalized
427MATLAB_CXXFLAGS := $(CXXFLAGS) -Wno-uninitialized428LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)429
430USE_PKG_CONFIG ?= 0431ifeq ($(USE_PKG_CONFIG), 1)432PKG_CONFIG := $(shell pkg-config opencv --libs)433else
434PKG_CONFIG :=435endif
436LDFLAGS += $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir)) $(PKG_CONFIG) \437$(foreach library,$(LIBRARIES),-l$(library))438PYTHON_LDFLAGS := $(LDFLAGS) $(foreach library,$(PYTHON_LIBRARIES),-l$(library))439
440# 'superclean' target recursively* deletes all files ending with an extension
441# in $(SUPERCLEAN_EXTS) below. This may be useful if you've built older
442# versions of Caffe that do not place all generated files in a location known
443# to the 'clean' target.
444#
445# 'supercleanlist' will list the files to be deleted by make superclean.
446#
447# * Recursive with the exception that symbolic links are never followed, per the
448# default behavior of 'find'.
449SUPERCLEAN_EXTS := .so .a .o .bin .testbin .pb.cc .pb.h _pb2.py .cuo450
451# Set the sub-targets of the 'everything' target.
452EVERYTHING_TARGETS := all py$(PROJECT) test warn lint453# Only build matcaffe as part of "everything" if MATLAB_DIR is specified.
454ifneq ($(MATLAB_DIR),)455EVERYTHING_TARGETS += mat$(PROJECT)456endif
457
458##############################
459# Define build targets
460##############################
461.PHONY: all lib test clean docs linecount lint lintclean tools examples $(DIST_ALIASES) \462py mat py$(PROJECT) mat$(PROJECT) proto runtest \463superclean supercleanlist supercleanfiles warn everything
464
465all: lib tools examples466
467lib: $(STATIC_NAME) $(DYNAMIC_NAME)468
469everything: $(EVERYTHING_TARGETS)470
471linecount:472cloc --read-lang-def=$(PROJECT).cloc \473src/$(PROJECT) include/$(PROJECT) tools examples \474python matlab
475
476lint: $(EMPTY_LINT_REPORT)477
478lintclean:479@ $(RM) -r $(LINT_OUTPUT_DIR) $(EMPTY_LINT_REPORT) $(NONEMPTY_LINT_REPORT)480
481docs: $(DOXYGEN_OUTPUT_DIR)482@ cd ./docs ; ln -sfn ../$(DOXYGEN_OUTPUT_DIR)/html doxygen483
484$(DOXYGEN_OUTPUT_DIR): $(DOXYGEN_CONFIG_FILE) $(DOXYGEN_SOURCES)485$(DOXYGEN_COMMAND) $(DOXYGEN_CONFIG_FILE)486
487$(EMPTY_LINT_REPORT): $(LINT_OUTPUTS) | $(BUILD_DIR)488@ cat $(LINT_OUTPUTS) > $@489@ if [ -s "$@" ]; then \490cat $@; \491mv $@ $(NONEMPTY_LINT_REPORT); \492echo "Found one or more lint errors."; \493exit 1; \494fi; \495$(RM) $(NONEMPTY_LINT_REPORT); \496echo "No lint errors!";497
498$(LINT_OUTPUTS): $(LINT_OUTPUT_DIR)/%.lint.txt : % $(LINT_SCRIPT) | $(LINT_OUTPUT_DIR)499@ mkdir -p $(dir $@)500@ python $(LINT_SCRIPT) $< 2>&1 \501| grep -v "^Done processing " \502| grep -v "^Total errors found: 0" \503> $@ \504|| true505
506test: $(TEST_ALL_BIN) $(TEST_ALL_DYNLINK_BIN) $(TEST_BINS)507
508tools: $(TOOL_BINS) $(TOOL_BIN_LINKS)509
510examples: $(EXAMPLE_BINS)511
512py$(PROJECT): py513
514py: $(PY$(PROJECT)_SO) $(PROTO_GEN_PY)515
516$(PY$(PROJECT)_SO): $(PY$(PROJECT)_SRC) $(PY$(PROJECT)_HXX) | $(DYNAMIC_NAME)517@ echo CXX/LD -o $@ $<518$(Q)$(CXX) -shared -o $@ $(PY$(PROJECT)_SRC) \519-o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(PYTHON_LDFLAGS) \520-Wl,-rpath,$(ORIGIN)/../../build/lib521
522mat$(PROJECT): mat523
524mat: $(MAT$(PROJECT)_SO)525
526$(MAT$(PROJECT)_SO): $(MAT$(PROJECT)_SRC) $(STATIC_NAME)527@ if [ -z "$(MATLAB_DIR)" ]; then \528echo "MATLAB_DIR must be specified in $(CONFIG_FILE)" \529"to build mat$(PROJECT)."; \530exit 1; \531fi
532@ echo MEX $<533$(Q)$(MATLAB_DIR)/bin/mex $(MAT$(PROJECT)_SRC) \534CXX="$(CXX)" \535CXXFLAGS="\$$CXXFLAGS $(MATLAB_CXXFLAGS)" \536CXXLIBS="\$$CXXLIBS $(STATIC_LINK_COMMAND) $(LDFLAGS)" -output $@537@ if [ -f "$(PROJECT)_.d" ]; then \538mv -f $(PROJECT)_.d $(BUILD_DIR)/${MAT$(PROJECT)_SO:.$(MAT_SO_EXT)=.d}; \539fi
540
541runtest: $(TEST_ALL_BIN)542$(TOOL_BUILD_DIR)/caffe543$(TEST_ALL_BIN) $(TEST_GPUID) --gtest_shuffle $(TEST_FILTER)544
545pytest: py546cd python; python -m unittest discover -s caffe/test547
548mattest: mat549cd matlab; $(MATLAB_DIR)/bin/matlab -nodisplay -r 'caffe.run_tests(), exit()'550
551warn: $(EMPTY_WARN_REPORT)552
553$(EMPTY_WARN_REPORT): $(ALL_WARNS) | $(BUILD_DIR)554@ cat $(ALL_WARNS) > $@555@ if [ -s "$@" ]; then \556cat $@; \557mv $@ $(NONEMPTY_WARN_REPORT); \558echo "Compiler produced one or more warnings."; \559exit 1; \560fi; \561$(RM) $(NONEMPTY_WARN_REPORT); \562echo "No compiler warnings!";563
564$(ALL_WARNS): %.o.$(WARNS_EXT) : %.o565
566$(BUILD_DIR_LINK): $(BUILD_DIR)/.linked567
568# Create a target ".linked" in this BUILD_DIR to tell Make that the "build" link
569# is currently correct, then delete the one in the OTHER_BUILD_DIR in case it
570# exists and $(DEBUG) is toggled later.
571$(BUILD_DIR)/.linked:572@ mkdir -p $(BUILD_DIR)573@ $(RM) $(OTHER_BUILD_DIR)/.linked574@ $(RM) -r $(BUILD_DIR_LINK)575@ ln -s $(BUILD_DIR) $(BUILD_DIR_LINK)576@ touch $@577
578$(ALL_BUILD_DIRS): | $(BUILD_DIR_LINK)579@ mkdir -p $@580
581$(DYNAMIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)582@ echo LD -o $@583$(Q)$(CXX) -shared -o $@ $(OBJS) $(VERSIONFLAGS) $(LINKFLAGS) $(LDFLAGS)584@ cd $(BUILD_DIR)/lib; rm -f $(DYNAMIC_NAME_SHORT); ln -s $(DYNAMIC_VERSIONED_NAME_SHORT) $(DYNAMIC_NAME_SHORT)585
586$(STATIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)587@ echo AR -o $@588$(Q)ar rcs $@ $(OBJS)589
590$(BUILD_DIR)/%.o: %.cpp $(PROTO_GEN_HEADER) | $(ALL_BUILD_DIRS)591@ echo CXX $<592$(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \593|| (cat $@.$(WARNS_EXT); exit 1)594@ cat $@.$(WARNS_EXT)595
596$(PROTO_BUILD_DIR)/%.pb.o: $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_GEN_HEADER) \597| $(PROTO_BUILD_DIR)598@ echo CXX $<599$(Q)$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \600|| (cat $@.$(WARNS_EXT); exit 1)601@ cat $@.$(WARNS_EXT)602
603$(BUILD_DIR)/cuda/%.o: %.cu | $(ALL_BUILD_DIRS)604@ echo NVCC $<605$(Q)$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -M $< -o ${@:.o=.d} \606-odir $(@D)607$(Q)$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -c $< -o $@ 2> $@.$(WARNS_EXT) \608|| (cat $@.$(WARNS_EXT); exit 1)609@ cat $@.$(WARNS_EXT)610
611$(TEST_ALL_BIN): $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) \612| $(DYNAMIC_NAME) $(TEST_BIN_DIR)613@ echo CXX/LD -o $@ $<614$(Q)$(CXX) $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) \615-o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib616
617$(TEST_CU_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CU_BUILD_DIR)/%.o \618$(GTEST_OBJ) | $(DYNAMIC_NAME) $(TEST_BIN_DIR)619@ echo LD $<620$(Q)$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) \621-o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib622
623$(TEST_CXX_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CXX_BUILD_DIR)/%.o \624$(GTEST_OBJ) | $(DYNAMIC_NAME) $(TEST_BIN_DIR)625@ echo LD $<626$(Q)$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) \627-o $@ $(LINKFLAGS) $(LDFLAGS) -l$(LIBRARY_NAME) -Wl,-rpath,$(ORIGIN)/../lib628
629# Target for extension-less symlinks to tool binaries with extension '*.bin'.
630$(TOOL_BUILD_DIR)/%: $(TOOL_BUILD_DIR)/%.bin | $(TOOL_BUILD_DIR)631@ $(RM) $@632@ ln -s $(notdir $<) $@633
634$(TOOL_BINS): %.bin : %.o | $(DYNAMIC_NAME)635@ echo CXX/LD -o $@636$(Q)$(CXX) $< -o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(LDFLAGS) \637-Wl,-rpath,$(ORIGIN)/../lib638
639$(EXAMPLE_BINS): %.bin : %.o | $(DYNAMIC_NAME)640@ echo CXX/LD -o $@641$(Q)$(CXX) $< -o $@ $(LINKFLAGS) -l$(LIBRARY_NAME) $(LDFLAGS) \642-Wl,-rpath,$(ORIGIN)/../../lib643
644proto: $(PROTO_GEN_CC) $(PROTO_GEN_HEADER)645
646$(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_BUILD_DIR)/%.pb.h : \647$(PROTO_SRC_DIR)/%.proto | $(PROTO_BUILD_DIR)648@ echo PROTOC $<649$(Q)protoc --proto_path=$(PROTO_SRC_DIR) --cpp_out=$(PROTO_BUILD_DIR) $<650
651$(PY_PROTO_BUILD_DIR)/%_pb2.py : $(PROTO_SRC_DIR)/%.proto \652$(PY_PROTO_INIT) | $(PY_PROTO_BUILD_DIR)653@ echo PROTOC \(python\) $<654$(Q)protoc --proto_path=src --python_out=python $<655
656$(PY_PROTO_INIT): | $(PY_PROTO_BUILD_DIR)657touch $(PY_PROTO_INIT)658
659clean:660@- $(RM) -rf $(ALL_BUILD_DIRS)661@- $(RM) -rf $(OTHER_BUILD_DIR)662@- $(RM) -rf $(BUILD_DIR_LINK)663@- $(RM) -rf $(DISTRIBUTE_DIR)664@- $(RM) $(PY$(PROJECT)_SO)665@- $(RM) $(MAT$(PROJECT)_SO)666
667supercleanfiles:668$(eval SUPERCLEAN_FILES := $(strip \669$(foreach ext,$(SUPERCLEAN_EXTS), $(shell find . -name '*$(ext)' \670-not -path './data/*'))))671
672supercleanlist: supercleanfiles673@ \674if [ -z "$(SUPERCLEAN_FILES)" ]; then \675echo "No generated files found."; \676else \677echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \678fi
679
680superclean: clean supercleanfiles681@ \682if [ -z "$(SUPERCLEAN_FILES)" ]; then \683echo "No generated files found."; \684else \685echo "Deleting the following generated files:"; \686echo $(SUPERCLEAN_FILES) | tr ' ' '\n'; \687$(RM) $(SUPERCLEAN_FILES); \688fi
689
690$(DIST_ALIASES): $(DISTRIBUTE_DIR)691
692$(DISTRIBUTE_DIR): all py | $(DISTRIBUTE_SUBDIRS)693# add proto694cp -r src/caffe/proto $(DISTRIBUTE_DIR)/695# add include696cp -r include $(DISTRIBUTE_DIR)/697mkdir -p $(DISTRIBUTE_DIR)/include/caffe/proto698cp $(PROTO_GEN_HEADER_SRCS) $(DISTRIBUTE_DIR)/include/caffe/proto699# add tool and example binaries700cp $(TOOL_BINS) $(DISTRIBUTE_DIR)/bin701cp $(EXAMPLE_BINS) $(DISTRIBUTE_DIR)/bin702# add libraries703cp $(STATIC_NAME) $(DISTRIBUTE_DIR)/lib704install -m 644 $(DYNAMIC_NAME) $(DISTRIBUTE_DIR)/lib705cd $(DISTRIBUTE_DIR)/lib; rm -f $(DYNAMIC_NAME_SHORT); ln -s $(DYNAMIC_VERSIONED_NAME_SHORT) $(DYNAMIC_NAME_SHORT)706# add python - it's not the standard way, indeed...707cp -r python $(DISTRIBUTE_DIR)/708
709-include $(DEPS)710