-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
182 lines (162 loc) · 6.72 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
###############################################################################
####################### <github.com/aszdrick/magefile> ########################
############################ <aszdrick@gmail.com> #############################
###############################################################################
# Copyright (c) 2017-2018 Marleson Graf
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
############################## PROJECT VARIABLES ##############################
### SUPPORTED SUFFIXES
HEADERSUFFIXES :=h hpp
### DIRECTORIES
SRCDIR :=src
INCDIR :=include
OBJDIR :=build
BINDIR :=bin
TSTDIR :=tests
DEPDIR :=.deps
### PROGRAM-RELATED VARIABLES
# Files containing the main() function
MAINFILES :=$(SRCDIR)/pc_test.cpp $(SRCDIR)/pc_bench.cpp \
$(SRCDIR)/pc_mixed_bench.cpp
# Binaries corresponding to each file with a main() function
BINARIES :=$(BINDIR)/pc-test $(BINDIR)/pc-bench \
$(BINDIR)/pc-mixed-bench
# Compiler & linker flags
CXXFLAGS :=-std=c++14 -Wall -pthread -O3
LDFLAGS :=
LDLIBS :=
INCLUDE :=-I$(INCDIR)
### TESTS-RELATED VARIABLES
# Files containing the main() function
TMAINFILES :=$(wildcard $(TSTDIR)/*.cpp)
# Binaries corresponding to each file with a main() function
TBINARIES :=$(patsubst $(TSTDIR)/%.cpp,$(BINDIR)/%,$(TMAINFILES))
# Compiler & linker flags
TCXXFLAGS :=
TLDFLAGS :=
TLDLIBS :=
TINCLUDE :=
### MAKEFILE CONTROL VARIABLES
# Debug flag, if != 0 deactivates all supressed echoing
DEBUG :=0
############################# AUTOMATIC VARIABLES #############################
# Dependency files
MAINDEPS :=$(patsubst %.cpp,$(DEPDIR)/%.d,$(MAINFILES))
TMAINDEPS :=$(patsubst %.cpp,$(DEPDIR)/%.d,$(TMAINFILES))
MDEPS :=$(MAINDEPS) $(TMAINDEPS)
NDEPS :=$(filter-out $(MDEPS),$(shell find $(DEPDIR) -name *.d 2> /dev/null))
# Phony calls to build specific binaries
CALLS :=$(patsubst $(BINDIR)/%,%,$(BINARIES))
TCALLS :=$(patsubst $(BINDIR)/%,%,$(TBINARIES))
ALLCALLS :=$(CALLS) $(TCALLS)
# Autogenerated directories
AUTODIR :=$(BINDIR) $(SRCDIR) $(INCDIR)
# Auxiliar variables
sh_nop :=:
empty :=
space :=$(empty) $(empty)
# No operation of shell script
# Command to print status messages
INFO :=@echo
# Patterns to filter all header files
HEADERPATTERNS :=$(addprefix %.,$(HEADERSUFFIXES))
# Creation of variables associating each main file with a program
MAINEXECVARS :=$(addsuffix _EXEC:=,$(basename $(MAINFILES) $(TMAINFILES)))
MAINEXECVARS :=$(join $(MAINEXECVARS), $(BINARIES) $(TBINARIES))
$(foreach var,$(MAINEXECVARS),$(eval $(var)))
SOURCES =$(shell find $(SRCDIR) -name '*.cpp' 2> /dev/null)
SOURCES +=$(shell find $(TSTDIR) -name '*.cpp' 2> /dev/null)
OBJECTS :=$(eval OBJECTS:=$(patsubst %.cpp,$(OBJDIR)/%.o,$(SOURCES)))$(OBJECTS)
# Arguments: target string, output name, input name
makedep = $(CXX) $(CXXFLAGS) $(INCLUDE) -MM -MG -MT $1 -MF "$2" $3
# Arguments: file to be written, target file, objects variable name
define makedeps
$(info [makedep] $2 -> .d)
$(shell mkdir -p $(dir $1))
$(shell $(call makedep,"$(OBJDIR)/$3.o $1",$1,$2))
$(eval TARGET_OBJ:=$(patsubst %.cpp,$(OBJDIR)/%.o,$2))
$(eval $3_EDGES:=$(filter $(HEADERPATTERNS),$(shell cat $1)))
$(eval $3_EDGES:=$(patsubst $(INCDIR)/%,$(SRCDIR)/%,$($3_EDGES)))
$(eval $3_EDGES:=$(patsubst %,$(OBJDIR)/%.o,$(basename $($3_EDGES))))
$(eval $3_EDGES:=$(filter $($3_EDGES),$(OBJECTS)))
$(eval $3_EDGES:=$(filter-out $(TARGET_OBJ),$($3_EDGES)))
$(eval STEMLIST:=$(patsubst $(OBJDIR)/%.o,%,$($3_EDGES)))
$(eval $3_EDGES:=$(patsubst $(OBJDIR)/%.o,%_EDGES,$($3_EDGES)))
$(eval NDEPS+=$1)
$(file >> $1,-$1: | $(patsubst %,$(DEPDIR)/%.d,$(STEMLIST)))
$(file >> $1,$3_EDGES =$$(eval $3_EDGES:=$(TARGET_OBJ)\
$($3_EDGES))$$($3_EDGES))
$(file >> $1,$3_OBJS =$$(eval RET:=)$$(eval $3_OBJS:=$$(call\
expand_deps,$3_EDGES)$$(RET))$$($3_OBJS))
$(if $($3_EXEC),$(file >> $1,$($3_EXEC): $$($3_OBJS)))
$(foreach s,$(STEMLIST),$(if $(filter-out $(NDEPS),$(DEPDIR)/$(s).d),
$(call makedeps,$(DEPDIR)/$(s).d,$(s).cpp,$(s))))
endef
# Receives a *_EDGES list to expand
# *_EDGES list value: *.o [other *_EDGES lists]
define expand_deps
$(foreach list,$1,\
$(eval RG1:=$(firstword $($(list))))\
$(if $(filter-out $(RET),$(RG1)),\
$(eval RET+=$(RG1))\
$(call expand_deps,$(filter-out $(RG1),$($(list))))))
endef
# If DEBUG is not active (i.e, equals 1), set supressing character (@)
ifeq ($(DEBUG),0)
SILENT :=@
endif
.PHONY: all makedir clean distclean tests $(ALLCALLS)
################################# MAIN RULES ##################################
all: makedir $(BINARIES)
$(ALLCALLS): %: $(BINDIR)/%
$(BINARIES) $(TBINARIES):
$(INFO) "[linking] $@"
$(SILENT) mkdir -p $(dir $@)
$(SILENT) $(CXX) $(CXXFLAGS) $(LDFLAGS) $(INCLUDE) $^ $(LDLIBS) -o $@
$(OBJDIR)/%.o: %.cpp
$(INFO) "[ $(CXX) ] $< -> .o"
$(SILENT) mkdir -p $(OBJDIR)/$(*D)
$(SILENT) $(CXX) $(CXXFLAGS) $(INCLUDE) -c $< -o $@
$(DEPDIR)/%.d: %.cpp
$(SILENT) $(sh_nop) $(call makedeps,$@,$<,$*)
makedir: | $(AUTODIR)
$(AUTODIR):
$(INFO) "[ mkdir ] Creating directory '$@'"
$(SILENT) mkdir -p $@
################################ TESTS RULES ##################################
tests: makedir $(TBINARIES)
$(TBINARIES): LDLIBS +=$(TLDLIBS)
$(TBINARIES): LDFLAGS +=$(TLDFLAGS)
$(OBJDIR)/$(TSTDIR)/%.o: CXXFLAGS +=$(TCXXFLAGS)
$(OBJDIR)/$(TSTDIR)/%.o: INCLUDE +=$(TINCLUDE)
################################ CLEAN RULES ##################################
clean:
$(SILENT) $(RM) -r $(OBJDIR)
$(SILENT) $(RM) -r $(BINDIR)
distclean: clean
$(SILENT) $(RM) -r $(DEPDIR)
################################ PREREQUISITES ################################
# Do not include list of dependencies with clean rules
ifeq ($(filter-out all $(CALLS) $(BINARIES),$(MAKECMDGOALS)),)
-include $(NDEPS)
-include $(MAINDEPS)
else
ifeq ($(filter-out tests $(TCALLS) $(TBINARIES),$(MAKECMDGOALS)),)
-include $(NDEPS)
-include $(TMAINDEPS)
endif
endif