-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakefile
187 lines (148 loc) · 5.6 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
183
184
185
186
187
PROJECT := $(notdir $(CURDIR))
EXCECUTABLE = $(BUILDDIR)/$(PROJECT)
# Directories specification
SRCDIRS := src example
INCDIRS := example inc
BUILDDIR := build
# @note: to add another source extension, add to herer AND make sure to
# write the " $(BUILDDIR)/%.o: %.ext " rule for this extention in order to work
SRCEXTS := cpp cc c cxx c++ C
# @note: to add another header extention, just add here and it should recognize it
HDREXTS := hpp hh h hxx h++ H
# list of all recognized files found in the specified directories
SOURCES := $(foreach dir, $(SRCDIRS), $(foreach ext, $(SRCEXTS), $(wildcard $(dir)/*.$(ext))))
INCLUDES := $(foreach dir, $(INCDIRS), $(foreach ext, $(HDREXTS), $(wildcard $(dir)/*.$(ext))))
OBJECTS := $(foreach ext, $(SRCEXTS), $(patsubst %.$(ext), $(BUILDDIR)/%.o, $(filter %.$(ext), $(SOURCES))))
# Compilers and flags
CC := gcc
CXX := g++
override CFLAGS += -g -Wall -Wno-unused-variable -fprofile-arcs -ftest-coverage -fno-exceptions -fno-inline
override CXXFLAGS += -g -Wall -Wno-unused-variable
override LDFLAGS += -fprofile-arcs -ftest-coverage -fno-exceptions -fno-inline
INCFLAGS := $(INCDIRS:%=-I%)
DEPFLAGS := -MMD -MP
# Tools and flags
CPPLINT := cpplint
override CPPLINTFLAGS += --linelength=120 --filter=-whitespace/braces,-whitespace/parens,-readability/casting,-build/header_guard,-runtime/references,-runtime/indentation_namespace,-build/namespaces --extensions=$(subst $( ),$(,),$(SRCEXTS)) --headers=$(subst $( ),$(,),$(HDREXTS))
CPPCHECK := cppcheck
override CPPCHECKFLAGS +=
override MISRAFLAGS += --addon=misra.json
# This makefile name
MAKEFILE := $(lastword $(MAKEFILE_LIST))
# Function to compile using $(CC) : (files: .c)
define compilecc
@mkdir -p $(dir $1)
@$(CC) -c $2 -o $1 $(CFLAGS) $(INCFLAGS) -MT $1 -MF $(BUILDDIR)/$3.Td $(DEPFLAGS)
@mv -f $(BUILDDIR)/$3.Td $(BUILDDIR)/$3.d && touch $1
@echo CC: $1
endef
# Function to compile using $(CXX) : (files: .cpp .cc .cxx .c++ .C)
define compilecxx
@mkdir -p $(dir $1)
@$(CXX) -c $2 -o $1 $(CXXFLAGS) $(INCFLAGS) -MT $1 -MF $(BUILDDIR)/$3.Td $(DEPFLAGS)
@mv -f $(BUILDDIR)/$3.Td $(BUILDDIR)/$3.d && touch $1
@echo CXX: $1
endef
# Rules to build objects for each source file extension
$(BUILDDIR)/%.o: %.c $(BUILDDIR)/%.d $(MAKEFILE) $@
$(call compilecc,$@,$<,$*)
$(BUILDDIR)/%.o: %.cc $(BUILDDIR)/%.d $(MAKEFILE) $@
$(call compilecxx,$@,$<,$*)
$(BUILDDIR)/%.o: %.cpp $(BUILDDIR)/%.d $(MAKEFILE) $@
$(call compilecxx,$@,$<,$*)
$(BUILDDIR)/%.o: %.cxx $(BUILDDIR)/%.d $(MAKEFILE) $@
$(call compilecxx,$@,$<,$*)
$(BUILDDIR)/%.o: %.c++ $(BUILDDIR)/%.d $(MAKEFILE) $@
$(call compilecxx,$@,$<,$*)
$(BUILDDIR)/%.o: %.C $(BUILDDIR)/%.d $(MAKEFILE) $@
$(call compilecxx,$@,$<,$*)
$(BUILDDIR)/%.d: ;
.PHONY: all help run clean force cpplint cppcheck info list-headers list-sources list-objects debug
# Main target for building
all: $(EXCECUTABLE)
@echo Done.
# Print commands
help:
@echo "Some useful make targets:"
@echo " make all - Build entire project (modified sources only or dependents)"
@echo " make run - Build and launch excecutable immediately"
@echo " make force - Force rebuild of entire project (clean first)"
@echo " make clean - Remove all build output"
@echo " make info - Print out project configurations"
@echo " make cpplint - C++ style checker tool following Google's C++ style guide"
@echo " make cppcheck - Static code analysis tool for the C and C++"
@echo " make list-headers - Print out all recognized headers files"
@echo " make list-sources - Print out all recognized sources files"
@echo " make list-objects - Print out final objects"
@echo ""
# make sure Make do not delete included dependencies files
.PRECIOUS: $(BUILDDIR)/%.d
# include the dependency files here (should not be before first target)
include $(wildcard $(foreach ext, $(SRCEXTS), $(patsubst %.$(ext), $(BUILDDIR)/%.d, $(filter %.$(ext), $(SOURCES)))))
# Compile binary if necessary, checks for modified files first
# @note: uses CC if all source files are .c , otherwise uses CXX
$(EXCECUTABLE): $(OBJECTS) $(MAKEFILE) $@
ifeq ($(filter-out %.c,$(SOURCES)),$(blank))
@$(CC) -o $@ $(OBJECTS) $(CFLAGS) $(LDFLAGS)
@echo CC: $@
else
@$(CXX) -o $@ $(OBJECTS) $(CXXFLAGS) $(LDFLAGS)
@echo CXX: $@
endif
# Launch excecutable, compile if necessary
run: $(EXCECUTABLE)
@./$(EXCECUTABLE)
# Clean all build files
clean:
@rm -rf $(EXCECUTABLE)
@rm -rf $(BUILDDIR)
@echo Cleaned.
# Force build of all files
force: clean all
# C++ style checker tool (following Google's C++ style guide)
cpplint:
@$(CPPLINT) $(CPPLINTFLAGS) $(SOURCES) $(INCLUDES)
# Static code analysis tool for the C and C++
cppcheck:
@$(CPPCHECK) $(CPPCHECKFLAGS) $(SOURCES) $(INCLUDES) $(INCFLAGS)
misra:
@$(CPPCHECK) $(MISRAFLAGS) $(SOURCES) $(INCFLAGS)
# Prints out project configurations
info:
@echo Project: $(PROJECT)
@echo Excecutable: $(EXCECUTABLE)
@echo SourceDirs: $(SRCDIRS)
@echo IncludeDirs: $(INCDIRS)
@echo BuildDir: $(BUILDDIR)
@echo CC: $(CC)
@echo CXX: $(CXX)
@echo CCFlags: $(CFLAGS)
@echo CXXFlags: $(CXXFLAGS)
@echo LDFlags: $(LDFLAGS)
@echo IncFlags: $(INCFLAGS)
@echo DepFlags: $(DEPFLAGS)
@echo CppLintFlags : $(CPPLINTFLAGS)
@echo CppCheckFlags: $(CPPCHECKFLAGS)
list-sources:
$(foreach src, $(SOURCES), $(call print,$(src)))
list-headers:
$(foreach hdr, $(INCLUDES), $(call print,$(hdr)))
list-objects:
$(foreach obj, $(OBJECTS), $(call print,$(obj)))
# Debugging of this makefile, for development
debug:
@echo SourceExts: $(SRCEXTS)
@echo HeaderExts: $(HDREXTS)
#
# ### Utils ###
#
define print
@echo $1
endef
# comma -> $(,)
, = ,
# blank -> $(blank)
blank =
# space -> $( )
space = $(blank) $(blank)
$(space) = $(space)