1 #
2 # The following defines a variable named "NAME" with a value of "myprogram". By convention,
3 # a lowercase prefix (in this case "program") and an uppercased suffix (in this case "NAME"), separated
4 # by an underscore is used to name attributes for a common element. Think of this like
5 # using program.NAME, program.C_SRCS, etc. There are no structs in Make, so we use this convention
6 # to keep track of attributes that all belong to the same target or program.
7 #
8 PROJECT := caffeine
9 NAME := lib$(PROJECT).so
10 TEST_NAME := test_$(PROJECT)
11 CXX_SRCS := $(shell find caffeine ! -name "test_*.cpp" -name "*.cpp")
12 CU_SRCS := $(shell find caffeine -name "*.cu")
13 TEST_SRCS := $(shell find caffeine -name "test_*.cpp") gtest/gtest-all.cpp
14 PROTO_SRCS := $(wildcard caffeine/proto/*.proto)
15 PROTO_GEN_HEADER := ${PROTO_SRCS:.proto=.pb.h}
16 PROTO_GEN_CC := ${PROTO_SRCS:.proto=.pb.cc}
17 PROTO_GEN_PY := ${PROTO_SRCS:.proto=_pb2.py}
18 CXX_OBJS := ${CXX_SRCS:.cpp=.o}
19 CU_OBJS := ${CU_SRCS:.cu=.o}
20 PROTO_OBJS := ${PROTO_SRCS:.proto=.pb.o}
21 OBJS := $(PROTO_OBJS) $(CXX_OBJS) $(CU_OBJS)
22 TEST_OBJS := ${TEST_SRCS:.cpp=.o}
24 CUDA_DIR := /usr/local/cuda
25 CUDA_ARCH := -arch=sm_20
26 MKL_DIR := /opt/intel/mkl
28 CUDA_INCLUDE_DIR := $(CUDA_DIR)/include
29 CUDA_LIB_DIR := $(CUDA_DIR)/lib64
30 MKL_INCLUDE_DIR := $(MKL_DIR)/include
31 MKL_LIB_DIR := $(MKL_DIR)/lib $(MKL_DIR)/lib/intel64
33 INCLUDE_DIRS := . /usr/local/include $(CUDA_INCLUDE_DIR) $(MKL_INCLUDE_DIR)
34 LIBRARY_DIRS := . /usr/local/lib $(CUDA_LIB_DIR) $(MKL_LIB_DIR)
35 LIBRARIES := cuda cudart cublas protobuf glog mkl_rt mkl_intel_thread curand
36 WARNINGS := -Wall
38 CXXFLAGS += -fPIC $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir))
39 LDFLAGS += $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir))
40 LDFLAGS += $(foreach library,$(LIBRARIES),-l$(library))
42 LINK = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $(WARNINGS)
43 NVCC = nvcc ${CXXFLAGS:-fPIC=-Xcompiler -fPIC} $(CPPFLAGS) $(CUDA_ARCH)
45 .PHONY: all test clean distclean linecount
47 all: $(NAME)
49 linecount:
50 cloc --read-lang-def=caffeine.cloc caffeine/
52 test: $(TEST_NAME)
54 $(TEST_NAME): $(OBJS) $(TEST_OBJS)
55 $(CXX) $(OBJS) $(TEST_OBJS) -o $(TEST_NAME) $(LDFLAGS) $(WARNINGS)
57 $(NAME): $(PROTO_GEN_CC) $(OBJS)
58 $(LINK) -shared $(OBJS) -o $(NAME)
60 $(CU_OBJS): %.o: %.cu
61 $(NVCC) -c $< -o $@
63 $(PROTO_GEN_CC): $(PROTO_SRCS)
64 protoc $(PROTO_SRCS) --cpp_out=. --python_out=.
66 clean:
67 @- $(RM) $(NAME) $(TEST_NAME)
68 @- $(RM) $(OBJS) $(TEST_OBJS)
69 @- $(RM) $(PROTO_GEN_HEADER) $(PROTO_GEN_CC) $(PROTO_GEN_PY)
71 distclean: clean