-
Notifications
You must be signed in to change notification settings - Fork 0
/
makefile
113 lines (79 loc) · 2.61 KB
/
makefile
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# This makefile cannot handle these cases:
# 1. Main object files "export" functions that are linked by other object files
# compilers and constant flags
CC = c++
CFLAGS = -Wall -Werror -Wextra -Wpedantic -Wextra-semi -Wnull-dereference -Wsuggest-override -Wconversion -Wshadow -std=c++17
# a list of dirs that has src code
DIRS = src test lib
# file that we want to match
EXT = cc
# output file extension (required)
OUTPUT_EXT = out
# dependency
INC = -Iinclude -Ilib
# build dir
BASE_BUILDDIR = build
BASE_TARGETDIR = bin
# flags
CDFLAGS =
LDFLAGS =
# define where to store generated file
DEBUG = debug
RELEASE = release
# release mode
ifeq ($(release), 1)
TARGETDIR = $(BASE_TARGETDIR)/$(RELEASE)
BUILDDIR = $(BASE_BUILDDIR)/$(RELEASE)
CDFLAGS := $(CDFLAGS) -O2 -DNDEBUG
LDFLAGS := $(LDFLAGS)
else
TARGETDIR = $(BASE_TARGETDIR)/$(DEBUG)
BUILDDIR = $(BASE_BUILDDIR)/$(DEBUG)
CDFLAGS := $(CDFLAGS) -g -DDEBUG
ifneq ($(OS),Windows_NT)
CDFLAGS := $(CDFLAGS) -fsanitize=address,undefined
LDFLAGS := $(LDFLAGS) -fsanitize=address,undefined
endif
endif
# if profile=1 in OSX
ifeq ($(shell uname -s),Darwin)
ifeq ($(profile), 1)
LDFLAGS := $(LDFLAGS) -L$(shell brew --prefix gperftools)/lib -lprofiler
endif
endif
# match all source files
rwildcard=$(foreach d,$(wildcard $(1:=/*)),$(call rwildcard,$d,$2) $(filter $(subst *,%,$2),$d))
SOURCES = $(call rwildcard,.,*.$(EXT))
SOURCES := $(SOURCES:./%=%)
SOURCES := $(SOURCES:.$(EXT)=.o)
OBJECTS = $(addprefix $(BUILDDIR)/, $(SOURCES))
all: compile link
compile:
@echo "===> Compiling"
# Add header dependency
HEADER_DEPEND := $(patsubst %.o,%.d,$(OBJECTS))
-include $(HEADER_DEPEND)
$(BUILDDIR)/%.o: %.$(EXT) makefile
@mkdir -p $(dir $@)
$(CC) -o $@ -c $< $(CFLAGS) $(CDFLAGS) $(INC) -MMD -MP
link: $(OBJECTS)
@echo "===> Linking"
@mkdir -p $(TARGETDIR)
$(eval MAINOBJECTS = $(shell nm -A $(OBJECTS) | grep 'T main\|T _main' | cut -d ':' -f1))
@$(foreach MAIN, $(MAINOBJECTS), \
$(eval TARGET = $(subst $(BUILDDIR), $(TARGETDIR), $(MAIN))) \
mkdir -p $(dir $(TARGET)); \
$(eval LINK = $(filter-out $(MAINOBJECTS), $(OBJECTS))) \
echo "$(CC) -o $(TARGET:.o=.$(OUTPUT_EXT)) $(MAIN) $(LINK) $(LDFLAGS)" ; \
$(CC) -o $(TARGET:.o=.$(OUTPUT_EXT)) $(MAIN) $(LINK) $(LDFLAGS); \
)
clean:
@echo "===> Cleaning"
@$(RM) -r $(BASE_BUILDDIR) $(BASE_TARGETDIR)
run:
@$(foreach file, $(call rwildcard,$(TARGETDIR),*.$(OUTPUT_EXT)), ./$(file);)
valgrind:
@$(foreach file, $(call rwildcard,$(TARGETDIR),*.$(OUTPUT_EXT)), valgrind ./$(file);)
leaks:
@$(foreach file, $(call rwildcard,$(TARGETDIR),*.$(OUTPUT_EXT)), leaks -atExit -- ./$(file);)
.PHONY: clean run valgrind leaks