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 TEST_SRCS := $(shell find caffeine -name "test_*.cpp") gtest/gtest-all.cpp
13 PROTO_SRCS := $(wildcard caffeine/proto/*.proto)
14 PROTO_GEN_HEADER := ${PROTO_SRCS:.proto=.pb.h}
15 PROTO_GEN_CC := ${PROTO_SRCS:.proto=.pb.cc}
16 CXX_OBJS := ${CXX_SRCS:.cpp=.o}
17 PROTO_OBJS := ${PROTO_SRCS:.proto=.pb.o}
18 OBJS := $(PROTO_OBJS) $(CXX_OBJS)
19 TEST_OBJS := ${TEST_SRCS:.cpp=.o}
21 CUDA_DIR = /usr/local/cuda
23 CUDA_INCLUDE_DIR = $(CUDA_DIR)/include
24 CUDA_LIB_DIR = $(CUDA_DIR)/lib64
26 INCLUDE_DIRS := . /usr/local/include $(CUDA_INCLUDE_DIR)
27 LIBRARY_DIRS := . /usr/local/lib $(CUDA_LIB_DIR)
28 LIBRARIES := cuda cudart cublas protobuf glog
29 WARNINGS := -Wall
31 CXXFLAGS += -fPIC $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir))
32 LDFLAGS += $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir))
33 LDFLAGS += $(foreach library,$(LIBRARIES),-l$(library))
35 LINK = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $(WARNINGS)
37 .PHONY: all test clean distclean
39 all: $(NAME)
41 test: $(TEST_NAME)
43 $(TEST_NAME): $(OBJS) $(TEST_OBJS)
44 $(CXX) $(OBJS) $(TEST_OBJS) -o $(TEST_NAME) $(LDFLAGS) $(WARNINGS)
45 ./$(TEST_NAME)
47 $(NAME): $(PROTO_GEN_CC) $(OBJS)
48 $(LINK) -shared $(OBJS) -o $(NAME)
50 $(PROTO_GEN_CC): $(PROTO_SRCS)
51 protoc $(PROTO_SRCS) --cpp_out=.
53 clean:
54 @- $(RM) $(NAME) $(TEST_NAME)
55 @- $(RM) $(OBJS) $(TEST_OBJS)
56 @- $(RM) $(PROTO_GEN_HEADER) $(PROTO_GEN_CC)
58 distclean: clean