-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
148 lines (108 loc) · 3.67 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# Target appearing at the very top ensures this is the default target
default: run
### C/C++
CXX := g++
LINK := $(CXX)
AUTODEPS := $(CXX)
HEADERS := $(HEADERS) -I/usr/local/include
### Libraries ###
LDSDL := $(shell sdl2-config --libs)
LDMATH := -lm
### Flags ###
# CXXFLAGS is lazy expanded to allow for modification of HEADERS
# Note: -Wmissing-braces may be safely disabled as per http://stackoverflow.com/questions/13905200/is-it-wise-to-ignore-gcc-clangs-wmissing-braces-warning
CXXFLAGS = -Wall -Wextra -Wno-missing-braces -pedantic -g -std=c++11 $(shell sdl2-config --cflags) $(HEADERS) -pthread
LDFLAGS := $(LDFLAGS) $(LDSDL) -L/usr/local/lib $(LDMATH)
AUTODEPSFLAGS = -MM $(CXXFLAGS)
### Files and Paths ###
EXE := navalngage
TARGETS += $(EXE)
SRCDIRS :=common 3rdparty core render components go systems game math
# For convenience, allow all of our source directories to be valid include paths
define ADD_INCLUDE
HEADERS += -I$(1)
endef
$(foreach dir,$(SRCDIRS),$(eval $(call ADD_INCLUDE,$(dir))))
### Environment Variables ###
# ENV := LD_LIBRARY_PATH=/usr/local/lib
### Profiler/Debugger ###
ifeq ($(CXX), g++)
DEBUG := gdb
else ifeq ($(CXX), clang++)
DEBUG := lldb
endif
MEMCHECK := valgrind
MEMCHECKFLAGS := --leak-check=full --track-origins=yes
### Templates ###
define COMPILE_TEMPLATE
# $(1) => SRCDIR
# $(2) => OBJDIR
# $(3) => DEPSDIR
SRC_$(1) := $$(wildcard $(1)/*.cpp)
OBJ_$(1) := $$(patsubst $(1)/%.cpp,$(2)/%.o,$$(SRC_$(1)))
DEPS_$(1) := $$(patsubst $(1)/%.cpp,$(3)/%.d,$$(SRC_$(1)))
TARGETS +=$$(OBJ_$(1)) $$(DEPS_$(1))
TARGETS_OBJ +=$$(OBJ_$(1))
CLEAN_DIRS +=$(2) $(3)
# Pull in deps info for existing .o files
-include $$(DEPS_$(1))
$(2)/%.o: $(1)/%.cpp
@mkdir -p $(2) $(3)
@echo "\033[36mCompiling $$<...\033[0m"
$(CXX) $$< $(CXXFLAGS) -c -o $$@
@echo "\033[36mGenerating dependencies for $$<...\033[0m"
$(AUTODEPS) $$< $(AUTODEPSFLAGS) -o $(3)/$$*.d
@# Based on http://scottmcpeak.com/autodepend/autodepend.html
@mv -f $(3)/$$*.d $(3)/$$*.d.tmp
@sed -e 's|.*:|$(2)/$$*.o:|' < $(3)/$$*.d.tmp > $(3)/$$*.d
@sed -e 's/.*://' -e 's/\\$$$$//' < $(3)/$$*.d.tmp | fmt -1 | \
sed -e 's/^ *//' -e 's/$$$$/:/' >> $(3)/$$*.d
@rm -f $(3)/$$*.d.tmp
endef
### Templatize the non-recursive make code when needed
# # Non-recursive make technique courtesy of https://evbergen.home.xs4all.nl/nonrecursive-make.html
# ### Push ###
# sp := $(sp).x
# dirstack_$(sp) := $(d)
# d := $(dir)
# ### BEGIN ###
# ### END ###
# ### Pop ###
# d := $(dirstack_$(sp))
# sp := $(basename $(sp))
### Run root directory rules ###
SRCDIR := .
OBJDIR := debug
DEPSDIR := $(OBJDIR)
$(eval $(call COMPILE_TEMPLATE,$(SRCDIR),$(OBJDIR),$(DEPSDIR)))
### Run subdirectory rules ###
$(foreach dir,$(SRCDIRS),$(eval $(call COMPILE_TEMPLATE,$(dir)/$(SRCDIR),$(dir)/$(OBJDIR),$(dir)/$(DEPSDIR))))
# define INCLUDE_MAKEFILES
# include $(1)/Makefile
# endef
# $(foreach dir,$(SRCDIRS),$(eval $(call INCLUDE_MAKEFILES,$(dir))))
### Main rules ###
.PHONY: default vg run build debug clean
ifeq ($(DEBUG),gdb)
debug: build
$(DEBUG) -tui -f $(EXE) --command=dbg_input
else
debug:
$(error Debugging is currently only supported via gdb. The selected compiler $(CXX) does not use gdb, sadly. Please change to a supported compiler.)
endif
vg: build
@echo "\033[90mRunning memcheck on $(EXE)...\033[0m"
$(MEMCHECK) $(MEMCHECKFLAGS) ./$(EXE)
run: build
@echo "\033[90mRunning $(EXE)...\033[0m"
./$(EXE)
build: $(EXE)
$(EXE): $(TARGETS_OBJ)
@echo "\033[34mLinking object files to create target $@...\033[0m"
$(CXX) $^ $(LDFLAGS) -o $@
@echo "\033[32mSuccessfully built $(EXE)!\033[0m"
clean:
@echo "\033[33mRemoving targets...\033[0m"
rm -f $(TARGETS)
@echo "\033[33mRemoving directories...\033[0m"
rm -rf $(CLEAN_DIRS)