-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
277 lines (233 loc) · 6.18 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# vim: set ft=make:
#
# Modular Makefile
#
# Author: Felix Viernickel
# Date: 2019
#
# This makefile is supposed to compile executables and libraries with
# sane defaults and serve as a basic "just-compile-it" configuration for new
# projects. Keep all code you want to compile under $(SRCDIR),
# includes in $(INCDIR) and libs in $(LIBDIR) and this works like a
# charm.
#
# OOTB you can:
# make [compile/release] # with or without optimization
# make [debug/asan/tsan] # different debug builds
# make [clean/skel] # some miscellaneous stuff
# make [binary/libstatic/libshared] # NOTE: set a default one in the "all" target!
# Grab a copy of ctags:
# make tags
#
# Grab a copy of cheat.h: # from https://github.com/Tuplanolla/cheat
# make test
#
# ..or don't.
#
# {{{ Project
#
# These are the most important settings to get started.
#
# The name of your main file will also be the name of
# the output file. Libraries don't *really* have a main
# file, but you know what I mean.
MAIN=season
# The default target. It's important to adjust this to
# your needs. Predefined values are:
# binary / libstatic / libshared
all:
@$(MAKE) binary
# Adjust the projects directories as needed.
SRCDIR=src
LIBDIR=lib
INCDIR=include
TESTDIR=test
# Output directories.
# WARNING: "make clean" WILL delete these directories.
# Be careful when changing these values. Don't point
# them at important stuff.
BINDIR=bin
OBJDIR=obj
# }}}
# {{{ Compiler Settings
#
# The most important project settings. Adjust this to your needs.
#
LIBS=-lm
INCDIRS=-I. -I$(INCDIR) -I$(SRCDIR)
LIBDIRS=-L. -L$(LIBDIR)
CC=gcc
STD=gnu11
WARN=-Wall -Wextra -Wcoverage-mismatch
CFLAGS=-std=$(STD) $(WARN) $(LIBS) -D_GNU_SOURCE
# {{{ Presets
#
# These are the presets used in the mix-and-match
# settings below.
#
MAINOUT=$(MAIN)
OUTDIR=$(BINDIR)
OUTSTR=bin
OBJFLAGS=
LINKFLAGS=
TESTFLAGS=-Wno-unused-parameter -Wno-unused-function -Wno-implicit-fallthrough
# Who cares about "determinism" anyway?
OPTIMIZEFLAGS=-O2 -flto -ffast-math
DEBUGFLAGS=-g
ASANFLAGS=-fsanitize=address \
-fsanitize=undefined \
-fsanitize=signed-integer-overflow \
-fsanitize=shift \
-fsanitize=integer-divide-by-zero \
-fsanitize=bounds \
-fsanitize=vla-bound \
-fsanitize=alignment \
-fsanitize=object-size \
-fsanitize=null \
-fsanitize=float-divide-by-zero \
-fsanitize=float-cast-overflow \
TSANFLAGS=-fsanitize=thread
PROFILE_GEN=-fprofile-generate
PROFILE_USE=-fprofile-correction -fprofile-use -freorder-blocks-and-partition -freorder-functions -freorder-blocks
# }}}
# {{{ File & Directory Structure
#
# Below, we find the file and directory structure of
# the project. It's kind of a shotgun approach, but
# will work pretty reliably as long as dependencies
# are not overly complex.
#
# List of *.c files
SRCDEPS=$(shell find $(SRCDIR) -type f -name "*.c")
# List if *.o files (including main)
ALLDEPS=$(SRCDEPS:$(SRCDIR)%.c=$(OBJDIR)%.o)
# List if *.o files (without main)
LIBDEPS=$(ALLDEPS:$(OBJDIR)/$(MAIN)%=)
# Source directory structure
SRCSKEL=$(shell find $(SRCDIR) -type d)
# Object directory structure
OBJSKEL=$(SRCSKEL:$(SRCDIR)%=$(OBJDIR)%)
# List of test *.c files
TESTSRC=$(shell find $(TESTDIR) -type f -name "*.c")
# List of test-suites
TESTBIN=$(TESTSRC:%.c=$(BINDIR)/%)
# Test source directory structure
TESTSRCSKEL=$(shell find $(TESTDIR) -type d)
# Test object directory structure
TESTBINSKEL=$(TESTSRCSKEL:%=$(BINDIR)/%)
# }}}
# {{{ Mix & Match
#
# Apply Settings depending on environment.
#
# In order to "conveniently" mix and match our
# settings, we call make recursively from our
# targets, passing in the appropriate environment
# variables defined below.
#
BINDEPS=$(ALLDEPS)
ifeq ($(DEBUG), 1)
CFLAGS+=$(DEBUGFLAGS)
endif
ifeq ($(RELEASE), 1)
CFLAGS+=$(OPTIMIZEFLAGS)
endif
ifeq ($(ASAN), 1)
CFLAGS+=$(ASANFLAGS)
endif
ifeq ($(TSAN), 1)
CFLAGS+=$(TSANFLAGS)
endif
ifeq ($(LIBSHARED), 1)
MAINOUT=$(MAIN).so
BINDEPS=$(LIBDEPS)
OUTDIR=$(LIBDIR)
OUTSTR=lib
OBJFLAGS=-fPIC
LINKFLAGS=-shared
endif
ifeq ($(LIBSTATIC), 1)
MAINOUT=$(MAIN).a
BINDEPS=$(LIBDEPS)
OUTDIR=$(LIBDIR)
OUTSTR=lib
endif
# }}}
# }}}
# {{{ Targets
#
# There should be no need to modify these, but you might
# want to add some of your own.
# {{{ Utility Targets
#
#
# These are catch-all targets for compiling objects, linking executables/libraries, and
# test-suites. Object files are written to the $(OBKDIR) subtree, usually obj/, and executables
# and libraries are output to $(BINDIR), usually bin/.
#
$(OBJDIR)/%.o: $(SRCDIR)/%.c
@printf "compiling [obj]: $(@:$(OBJDIR)/%=%)\n"
@$(CC) $(INCDIRS) $(LIBDIRS) -o $@ $< -c $(CFLAGS) $(OBJFLAGS)
$(BINDIR)/$(MAINOUT): $(BINDEPS)
@printf "compiling [$(OUTSTR)]: $(MAINOUT)\n"
@$(CC) $(INCDIRS) $(LIBDIRS) -o $(BINDIR)/$(MAINOUT) $(BINDEPS) $(CFLAGS) $(LINKFLAGS)
$(BINDIR)/$(TESTDIR)/%: $(TESTDIR)/%.c $(LIBDEPS)
@printf "compiling [bin]: $(@:$(TESTDIR)/%=%)\n"
@$(CC) $(INCDIRS) $(LIBDIRS) -o $@ $< $(LIBDEPS) $(CFLAGS) $(TESTFLAGS)
.PHONY: compile
compile: skel $(BINDIR)/$(MAINOUT)
@printf "done\n"
.PHONY: skel
skel:
@printf "creating directory layouts\n"
@mkdir -p $(OBJSKEL) $(TESTBINSKEL) $(BINDIR) $(LIBDIR)
#
# }}}
# {{{ Convenience Targets
#
# Actually define the advertised targets.
#
# Most of these are just shorthand for FOO=1 BAR=1 make.
#
.PHONY: binary
binary: compile
@printf "making [binary]\n"
.PHONY: libshared
libshared:
@printf "making [libshared]\n"
@LIBSHARED=1 $(MAKE) compile
.PHONY: libstatic
libstatic:
@printf "making [libstatic]\n"
@LIBSTATIC=1 $(MAKE) compile
.PHONY: debug
debug:
@printf "setting [debug]\n"
@DEBUG=1 $(MAKE) all
.PHONY: release
release:
@printf "setting [release]\n"
@RELEASE=1 $(MAKE) all
.PHONY: asan
asan:
@printf "setting [asan]\n"
@DEBUG=1 ASAN=1 $(MAKE) all
.PHONY: tsan
tsan:
@printf "setting [tsan]\n"
@DEBUG=1 TSAN=1 $(MAKE) all
.PHONY: test
test: skel $(TESTBIN)
@printf "running [all] tests\n"
@for t in $(TESTBIN); do echo "testing [bin]: $$t"; "$$t"; done
.PHONY: tags
tags:
@printf "creating [tags]\n"
@ctags -R
.PHONY: clean
clean:
@printf "cleaning\n"
rm -fr $(OBJDIR)/*
rm -fr $(BINDIR)/*
# }}}
# }}}