-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.mk
88 lines (68 loc) · 2.43 KB
/
common.mk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Build with clang
CC := clang
CXX := clang++
# Use pthreads
LIBS ?= pthread
# Default flags
CFLAGS ?= -g --std=c11 -fPIC -I $(ROOT)/graph
CXXFLAGS ?= -g --std=c++11 -fPIC -I $(ROOT)/graph
LDFLAGS += $(addprefix -l,$(LIBS))
# Default source and object files
SRCS ?= $(wildcard *.cc) $(wildcard *.cpp) $(wildcard *.c)
OBJS ?= $(addprefix obj/,$(patsubst %.cpp,%.o,$(patsubst %.cc,%.o,$(patsubst %.c,%.o,$(SRCS)))))
# Targets to build recirsively into $(DIRS)
RECURSIVE_TARGETS ?= all clean test
# Targets separated by type
SHARED_LIB_TARGETS := $(filter %.so, $(TARGETS))
STATIC_LIB_TARGETS := $(filter %.a, $(TARGETS))
OTHER_TARGETS := $(filter-out %.so, $(filter-out %.a, $(TARGETS)))
# If not set, the build path is just the current directory name
MAKEPATH ?= `basename $(PWD)`
# Log the build path in gray, following by a log message in bold green
LOG_PREFIX := "$(shell tput setaf 7)[$(MAKEPATH)]$(shell tput sgr0)$(shell tput setaf 2)"
LOG_SUFFIX := "$(shell tput sgr0)"
# Build all targets by default
all:: $(TARGETS)
# Clean up after a build
clean::
@for t in $(TARGETS); do \
echo $(LOG_PREFIX) Cleaning $$t $(LOG_SUFFIX); \
done
@rm -rf $(TARGETS) obj
test::
# Prevent errors if files named all, clean, bench, or test exist
.PHONY: all clean test
# Compile a C++ source file (and generate its dependency rules)
obj/%.o: %.cc $(PREREQS)
@echo $(LOG_PREFIX) Compiling $< $(LOG_SUFFIX)
@mkdir -p obj
@$(CXX) $(CXXFLAGS) -MMD -MP -o $@ -c $<
# Compile a C++ source file (and generate its dependency rules)
obj/%.o: %.cpp $(PREREQS)
@echo $(LOG_PREFIX) Compiling $< $(LOG_SUFFIX)
@mkdir -p obj
@$(CXX) $(CXXFLAGS) -MMD -MP -o $@ -c $<
# Compile a C source file (and generate its dependency rules)
obj/%.o: %.c $(PREREQS)
@echo $(LOG_PREFIX) Compiling $< $(LOG_SUFFIX)
@mkdir -p obj
@$(CC) $(CFLAGS) -MMD -MP -o $@ -c $<
# Link a shared library
$(SHARED_LIB_TARGETS): $(OBJS)
@echo $(LOG_PREFIX) Linking $@ $(LOG_SUFFIX)
@$(CXX) -shared $(LDFLAGS) -o $@ $^
$(STATIC_LIB_TARGETS): $(OBJS)
@echo $(LOG_PREFIX) Linking $@ $(LOG_SUFFIX)
@ar rs $@ $^
# Link binary targets
$(OTHER_TARGETS): $(OBJS)
@echo $(LOG_PREFIX) Linking $@ $(LOG_SUFFIX)
@$(CXX) -o $@ $^ $(LDFLAGS)
# Include dependency rules for all objects
-include $(OBJS:.o=.d)
# Build any recursive targets in subdirectories
$(RECURSIVE_TARGETS)::
@for dir in $(DIRS); do \
$(MAKE) -C $$dir --no-print-directory $@ MAKEPATH="$(MAKEPATH)/$$dir"; \
done
include $(ROOT)/deps.mk