1 # The makefile for caffe. Extremely hacky.
5 include Makefile.config
7 ##############################################################################
8 # After this line, things should happen automatically.
9 ##############################################################################
11 # The target static library and shared library name
12 NAME := lib$(PROJECT).so
13 STATIC_NAME := lib$(PROJECT).a
15 ##############################
16 # Get all source files
17 ##############################
18 # CXX_SRCS are the source files excluding the test ones.
19 CXX_SRCS := $(shell find src/$(PROJECT) ! -name "test_*.cpp" -name "*.cpp")
20 # HXX_SRCS are the header files
21 HXX_SRCS := $(shell find include/$(PROJECT) ! -name "*.hpp")
22 # CU_SRCS are the cuda source files
23 CU_SRCS := $(shell find src/$(PROJECT) -name "*.cu")
24 # TEST_SRCS are the test source files
25 TEST_MAIN_SRC := src/$(PROJECT)/test/test_caffe_main.cpp
26 TEST_SRCS := $(shell find src/$(PROJECT) -name "test_*.cpp")
27 TEST_SRCS := $(filter-out $(TEST_MAIN_SRC), $(TEST_SRCS))
28 GTEST_SRC := src/gtest/gtest-all.cpp
29 # TEST_HDRS are the test header files
30 TEST_HDRS := $(shell find src/$(PROJECT) -name "test_*.hpp")
31 # TOOL_SRCS are the source files for the tool binaries
32 TOOL_SRCS := $(shell find tools -name "*.cpp")
33 # EXAMPLE_SRCS are the source files for the example binaries
34 EXAMPLE_SRCS := $(shell find examples -name "*.cpp")
35 # PROTO_SRCS are the protocol buffer definitions
36 PROTO_SRCS := $(wildcard src/$(PROJECT)/proto/*.proto)
37 # NONGEN_CXX_SRCS includes all source/header files except those generated
38 # automatically (e.g., by proto).
39 NONGEN_CXX_SRCS := $(shell find \
46 -name "*.cpp" -or -name "*.hpp" -or -name "*.cu" -or -name "*.cuh")
47 LINT_REPORT := $(BUILD_DIR)/cpp_lint.log
48 FAILED_LINT_REPORT := $(BUILD_DIR)/cpp_lint.error_log
49 # PY$(PROJECT)_SRC is the python wrapper for $(PROJECT)
50 PY$(PROJECT)_SRC := python/$(PROJECT)/_$(PROJECT).cpp
51 PY$(PROJECT)_SO := python/$(PROJECT)/_$(PROJECT).so
52 # MAT$(PROJECT)_SRC is the matlab wrapper for $(PROJECT)
53 MAT$(PROJECT)_SRC := matlab/$(PROJECT)/mat$(PROJECT).cpp
54 MAT$(PROJECT)_SO := matlab/$(PROJECT)/$(PROJECT)
56 ##############################
57 # Derive generated files
58 ##############################
59 # The generated files for protocol buffers
60 PROTO_GEN_HEADER := ${PROTO_SRCS:.proto=.pb.h}
61 PROTO_GEN_CC := ${PROTO_SRCS:.proto=.pb.cc}
62 PROTO_GEN_PY := ${PROTO_SRCS:.proto=_pb2.py}
63 # The objects corresponding to the source files
64 # These objects will be linked into the final shared library, so we
65 # exclude the tool, example, and test objects.
66 CXX_OBJS := $(addprefix $(BUILD_DIR)/, ${CXX_SRCS:.cpp=.o})
67 CU_OBJS := $(addprefix $(BUILD_DIR)/, ${CU_SRCS:.cu=.cuo})
68 PROTO_OBJS := $(addprefix $(BUILD_DIR)/, ${PROTO_GEN_CC:.cc=.o})
69 OBJS := $(PROTO_OBJS) $(CXX_OBJS) $(CU_OBJS)
70 # tool, example, and test objects
71 TOOL_OBJS := $(addprefix $(BUILD_DIR)/, ${TOOL_SRCS:.cpp=.o})
72 EXAMPLE_OBJS := $(addprefix $(BUILD_DIR)/, ${EXAMPLE_SRCS:.cpp=.o})
73 TEST_OBJS := $(addprefix $(BUILD_DIR)/, ${TEST_SRCS:.cpp=.o})
74 GTEST_OBJ := $(addprefix $(BUILD_DIR)/, ${GTEST_SRC:.cpp=.o})
75 # tool, example, and test bins
76 TOOL_BINS := ${TOOL_OBJS:.o=.bin}
77 EXAMPLE_BINS := ${EXAMPLE_OBJS:.o=.bin}
78 TEST_BINS := ${TEST_OBJS:.o=.testbin}
79 TEST_ALL_BIN := $(BUILD_DIR)/src/$(PROJECT)/test/test_all.testbin
81 ##############################
82 # Derive include and lib directories
83 ##############################
84 CUDA_INCLUDE_DIR := $(CUDA_DIR)/include
85 CUDA_LIB_DIR := $(CUDA_DIR)/lib64 $(CUDA_DIR)/lib
86 MKL_INCLUDE_DIR := $(MKL_DIR)/include
87 MKL_LIB_DIR := $(MKL_DIR)/lib $(MKL_DIR)/lib/intel64
89 INCLUDE_DIRS += ./src ./include $(CUDA_INCLUDE_DIR)
90 LIBRARY_DIRS += $(CUDA_LIB_DIR)
91 LIBRARIES := cudart cublas curand \
93 glog protobuf leveldb snappy \
96 opencv_core opencv_highgui opencv_imgproc
97 PYTHON_LIBRARIES := boost_python python2.7
100 COMMON_FLAGS := -DNDEBUG -O2
102 # MKL switch (default = non-MKL)
106 COMMON_FLAGS += -DUSE_MKL
107 INCLUDE_DIRS += $(MKL_INCLUDE_DIR)
108 LIBRARY_DIRS += $(MKL_LIB_DIR)
110 LIBRARIES += atlas cblas
113 COMMON_FLAGS += $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir))
114 CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS)
115 NVCCFLAGS := -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
116 LDFLAGS += $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir)) \
117 $(foreach library,$(LIBRARIES),-l$(library))
118 PYTHON_LDFLAGS := $(LDFLAGS) $(foreach library,$(PYTHON_LIBRARIES),-l$(library))
120 ##############################
121 # Define build targets
122 ##############################
123 .PHONY: all init test clean linecount lint tools examples py mat distribute \
124 py$(PROJECT) mat$(PROJECT) proto runtest
126 all: init $(NAME) $(STATIC_NAME) tools examples
130 @ mkdir -p $(foreach obj,$(OBJS),$(dir $(obj)))
131 @ mkdir -p $(foreach obj,$(TOOL_OBJS),$(dir $(obj)))
132 @ mkdir -p $(foreach obj,$(EXAMPLE_OBJS),$(dir $(obj)))
133 @ mkdir -p $(foreach obj,$(TEST_OBJS),$(dir $(obj)))
134 @ mkdir -p $(foreach obj,$(GTEST_OBJ),$(dir $(obj)))
137 cloc --read-lang-def=$(PROJECT).cloc src/$(PROJECT)/
141 $(LINT_REPORT): $(NONGEN_CXX_SRCS)
142 @ mkdir -p $(BUILD_DIR)
143 @ (python ./scripts/cpp_lint.py $(NONGEN_CXX_SRCS) > $(LINT_REPORT) 2>&1 \
144 && (rm -f $(FAILED_LINT_REPORT); echo "No lint errors!")) || ( \
145 mv $(LINT_REPORT) $(FAILED_LINT_REPORT); \
146 grep -v "^Done processing " $(FAILED_LINT_REPORT); \
147 echo "Found 1 or more lint errors; see log at $(FAILED_LINT_REPORT)"; \
150 test: init $(TEST_BINS) $(TEST_ALL_BIN)
152 tools: init $(TOOL_BINS)
154 examples: init $(EXAMPLE_BINS)
158 py: init $(STATIC_NAME) $(PY$(PROJECT)_SRC) $(PROTO_GEN_PY)
159 $(CXX) -shared -o $(PY$(PROJECT)_SO) $(PY$(PROJECT)_SRC) \
160 $(STATIC_NAME) $(CXXFLAGS) $(PYTHON_LDFLAGS)
165 mat: init $(STATIC_NAME) $(MAT$(PROJECT)_SRC)
166 $(MATLAB_DIR)/bin/mex $(MAT$(PROJECT)_SRC) $(STATIC_NAME) \
167 CXXFLAGS="\$$CXXFLAGS $(CXXFLAGS) $(WARNINGS)" \
168 CXXLIBS="\$$CXXLIBS $(LDFLAGS)" \
169 -o $(MAT$(PROJECT)_SO)
172 $(NAME): init $(PROTO_OBJS) $(OBJS)
173 $(CXX) -shared -o $(NAME) $(OBJS) $(CXXFLAGS) $(LDFLAGS) $(WARNINGS)
176 $(STATIC_NAME): init $(PROTO_OBJS) $(OBJS)
177 ar rcs $(STATIC_NAME) $(PROTO_OBJS) $(OBJS)
180 runtest: $(TEST_ALL_BIN)
181 $(TEST_ALL_BIN) $(TEST_GPUID)
183 $(TEST_BINS): %.testbin : %.o $(GTEST_OBJ) $(STATIC_NAME) $(TEST_HDRS)
184 $(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) $(STATIC_NAME) -o $@ $(CXXFLAGS) $(LDFLAGS) $(WARNINGS)
186 $(TEST_ALL_BIN): $(GTEST_OBJ) $(STATIC_NAME) $(TEST_OBJS)
187 $(CXX) $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) $(STATIC_NAME) -o $(TEST_ALL_BIN) $(CXXFLAGS) $(LDFLAGS) $(WARNINGS)
189 $(TOOL_BINS): %.bin : %.o $(STATIC_NAME)
190 $(CXX) $< $(STATIC_NAME) -o $@ $(CXXFLAGS) $(LDFLAGS) $(WARNINGS)
193 $(EXAMPLE_BINS): %.bin : %.o $(STATIC_NAME)
194 $(CXX) $< $(STATIC_NAME) -o $@ $(CXXFLAGS) $(LDFLAGS) $(WARNINGS)
197 $(OBJS): $(PROTO_GEN_CC) $(HXX_SRCS)
199 $(BUILD_DIR)/src/$(PROJECT)/%.o: src/$(PROJECT)/%.cpp
200 $(CXX) $< $(CXXFLAGS) -c -o $@
203 $(BUILD_DIR)/src/$(PROJECT)/layers/%.o: src/$(PROJECT)/layers/%.cpp
204 $(CXX) $< $(CXXFLAGS) -c -o $@
207 $(BUILD_DIR)/src/$(PROJECT)/proto/%.o: src/$(PROJECT)/proto/%.cc
208 $(CXX) $< $(CXXFLAGS) -c -o $@
211 $(BUILD_DIR)/src/$(PROJECT)/test/%.o: src/test/%.cpp
212 $(CXX) $< $(CXXFLAGS) -c -o $@
215 $(BUILD_DIR)/src/$(PROJECT)/util/%.o: src/$(PROJECT)/util/%.cpp
216 $(CXX) $< $(CXXFLAGS) -c -o $@
219 $(BUILD_DIR)/src/gtest/%.o: src/gtest/%.cpp
220 $(CXX) $< $(CXXFLAGS) -c -o $@
223 $(BUILD_DIR)/src/$(PROJECT)/layers/%.cuo: src/$(PROJECT)/layers/%.cu
224 $(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -c $< -o $@
227 $(BUILD_DIR)/src/$(PROJECT)/util/%.cuo: src/$(PROJECT)/util/%.cu
228 $(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -c $< -o $@
231 $(BUILD_DIR)/tools/%.o: tools/%.cpp
232 $(CXX) $< $(CXXFLAGS) -c -o $@ $(LDFLAGS)
235 $(BUILD_DIR)/examples/%.o: examples/%.cpp
236 $(CXX) $< $(CXXFLAGS) -c -o $@ $(LDFLAGS)
239 $(PROTO_GEN_PY): $(PROTO_SRCS)
240 protoc --proto_path=src --python_out=python $(PROTO_SRCS)
243 proto: $(PROTO_GEN_CC)
245 $(PROTO_GEN_CC): $(PROTO_SRCS)
246 protoc --proto_path=src --cpp_out=src $(PROTO_SRCS)
247 mkdir -p include/$(PROJECT)/proto
248 cp $(PROTO_GEN_HEADER) include/$(PROJECT)/proto/
252 @- $(RM) $(NAME) $(STATIC_NAME)
253 @- $(RM) $(PROTO_GEN_HEADER) $(PROTO_GEN_CC) $(PROTO_GEN_PY)
254 @- $(RM) include/$(PROJECT)/proto/$(PROJECT).pb.h
255 @- $(RM) python/$(PROJECT)/proto/$(PROJECT)_pb2.py
256 @- $(RM) python/$(PROJECT)/*.so
257 @- $(RM) -rf $(BUILD_DIR)
258 @- $(RM) -rf $(DISTRIBUTE_DIR)
261 mkdir $(DISTRIBUTE_DIR)
263 cp -r include $(DISTRIBUTE_DIR)/
264 # add tool and example binaries
265 mkdir $(DISTRIBUTE_DIR)/bin
266 cp $(TOOL_BINS) $(DISTRIBUTE_DIR)/bin
267 cp $(EXAMPLE_BINS) $(DISTRIBUTE_DIR)/bin
269 mkdir $(DISTRIBUTE_DIR)/lib
270 cp $(NAME) $(DISTRIBUTE_DIR)/lib
271 cp $(STATIC_NAME) $(DISTRIBUTE_DIR)/lib
272 # add python - it's not the standard way, indeed...
273 cp -r python $(DISTRIBUTE_DIR)/python