-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakefile
More file actions
346 lines (232 loc) · 9.65 KB
/
makefile
File metadata and controls
346 lines (232 loc) · 9.65 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
.SUFFIXES:
MAKEFLAGS += -r -j$(shell nproc)
.SECONDARY:
.SUFFIXES:
SHELL = sh -xv
SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
ifeq ($(origin CC),default)
CC = clang
endif
ifeq ($(origin CXX),default)
CXX = clang++
endif
CFLAGS ?= -std=c99 -Wall -O1 -g -gdwarf-4 -Wno-unknown-attributes
LDFLAGS ?=
# make help looks through this makefile for ## annotations
.PHONY: help
.PHONY: all
all:: ## Fetch all code and build everything
.PHONY: fetch
fetch:: ## Download code from the web
.PHONY: build
build:: ## Compile everything
.PHONY: clean
clean:: ## Delete non-downloaded files
.PHONY: deepclean
deepclean:: ## Remove everything, restore to clean checkout
.PHONY: vendor
vendor:: ## Derive vendored source files from fetched files
# Top level dependencies
build:: vendor
all:: build
deepclean:: clean
# Downloading extra source from the internet during the build is
# optional, can make fetch, copy the files somewhere else, make build
# All downloaded files go into FETCH_DIR
FETCH_DIR := fetch
include fetch.mk
deepclean::
rm -rf $(FETCH_DIR)
# Copy / edit files from fetch to create the ones under vendor
VENDOR_DIR := vendor
include vendor.mk
VENDOR_DIR_OBJ := .$(VENDOR_DIR).O
deepclean:: vendorclean
clean::
@rm -rf $(VENDOR_DIR_OBJ)
vendorclean::
@rm -rf $(VENDOR_DIR)
# Build each C file under vendor into a corresponding object file, depending on all headers
# under vendor (i.e. quite coarse grained dependency tracking)
VENDOR_OBJ := $(VENDOR_SRC:$(VENDOR_DIR)/%.c=$(VENDOR_DIR_OBJ)/%.o)
# The tree structure mirrors the source, make whatever directories are required
# If moving the mkdir under a dedicated target, check make make still reports nothing to do
VENDOR_OBJ_DIR_LIST := $(sort $(VENDOR_DIR_OBJ) $(dir $(VENDOR_OBJ)))
$(VENDOR_OBJ): $(VENDOR_DIR_OBJ)/%.o: $(VENDOR_DIR)/%.c $(VENDOR_HDR)
@mkdir -p $(VENDOR_OBJ_DIR_LIST)
@$(CC) $(CFLAGS) $< -c -o $@
VENDOR_LUA_OBJ := $(filter-out $(VENDOR_DIR_OBJ)/lua/luac.o,$(VENDOR_LUA_SRC:$(VENDOR_DIR)/%.c=$(VENDOR_DIR_OBJ)/%.o))
lua: $(VENDOR_LUA_OBJ)
@$(CC) $(CFLAGS) $^ -o $@ -lm # no ldflags, don't want to link in a fuzzer
clean::
@rm -f lua
VENDOR_LIBTOMMATH_OBJ := $(VENDOR_LIBTOMMATH_SRC:$(VENDOR_DIR)/%.c=$(VENDOR_DIR_OBJ)/%.o)
.vendor.O/libtommath.o: $(VENDOR_LIBTOMMATH_OBJ)
ld -r $^ -o $@
VENDOR_LIBTOMMATH_CXX_OBJ := $(VENDOR_LIBTOMMATH_SRC:$(VENDOR_DIR)/%.c=$(VENDOR_DIR_OBJ)/%.cpp.o)
$(VENDOR_LIBTOMMATH_CXX_OBJ): $(VENDOR_DIR_OBJ)/%.cpp.o: $(VENDOR_DIR)/%.c $(VENDOR_HDR)
@mkdir -p $(VENDOR_OBJ_DIR_LIST)
@$(CXX) -xc++ -Wall $< -c -o $@
build:: $(VENDOR_OBJ) .vendor.O/libtommath.o $(VENDOR_LIBTOMMATH_CXX_OBJ)
PROTO_SRC := proto_impl.c proto.c proto_derived.c proto_memory_check.c
PROTO_OBJ := $(PROTO_SRC:%.c=$(VENDOR_DIR_OBJ)/%.o)
$(PROTO_OBJ): $(VENDOR_DIR_OBJ)/%.o: %.c proto.h proto_memory_check.h $(VENDOR_HDR)
@mkdir -p $(VENDOR_DIR_OBJ)
@$(CC) $(CFLAGS) $< -c -o $@
proto: $(PROTO_OBJ) $(VENDOR_LIBTOMMATH_OBJ)
@$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@
clean::
@rm -f proto
build:: proto
proto_memory_check.h: lua api.lua
./lua api.lua memory_header proto proto proto_context > $@
proto_memory_check.c: lua api.lua
./lua api.lua memory_source proto proto proto_context > $@
clean::
rm -f proto_memory_check.?
DEMOLANG_DIR := demolang
DEMOLANG_DIR_OBJ := .$(DEMOLANG_DIR).O
include demolang/files.mk
DEMOLANG_SRC := $(addprefix demolang/,$(filter %.c,$(DEMOLANG_FILES)))
DEMOLANG_HDR := $(addprefix demolang/,$(filter %.h,$(DEMOLANG_FILES)))
DEMOLANG_OBJ := $(DEMOLANG_SRC:$(DEMOLANG_DIR)/%.c=$(DEMOLANG_DIR_OBJ)/%.o)
$(DEMOLANG_DIR_OBJ)/%.o: $(DEMOLANG_DIR)/%.c $(DEMOLANG_HDR) proto.h $(VENDOR_HDR) proto_memory_check.h proto.h
@mkdir -p $(DEMOLANG_DIR_OBJ)
@$(CC) $(CFLAGS) $< -c -o $@
demolang:: $(DEMOLANG_OBJ)
SEEDFILES := # $(wildcard seedfiles/*)
MORE_FUZZFILES := $(wildcard /scratch/jon/fuzz/corpus/*)
# This file is working around makefile passing many files to a single
# shell invocation which then falls over
$(shell mkdir -p $(VENDOR_DIR_OBJ))
CAT_FUZZFILES := $(VENDOR_DIR_OBJ)/all_fuzzfiles
$(file >$(CAT_FUZZFILES))
$(foreach V,$(MORE_FUZZFILES),$(file >>$(CAT_FUZZFILES),$(file <$V)))
FUZZ_FILES_DIR := $(SELF_DIR)/fuzzfiles/
clean::
@rm -rf $(CAT_FUZZFILES) fuzzfiles
EXTRACT_FILES_DIR := $(SELF_DIR)/extractfiles/
include extractfiles/files.mk
clean::
@rm -rf $(CAT_EXTRACTFILES) extractfiles
ifeq (,$(wildcard extractfiles/files.mk))
extractfiles/files.mk: extract.awk $(CAT_EXTRACTFILES)
@mkdir -p extractfiles
@gawk --lint -f extract.awk extractfiles/ $(SEEDFILES) $(CAT_FUZZFILES)
endif
EXTRACT_SOURCE := $(addprefix extractfiles/,$(extract_files))
TESTCASE_SOURCE := $(addprefix testcases/,$(extract_files))
# Calculates the answer python expects for the given arithmetic
$(TESTCASE_SOURCE): testcases/%: extractfiles/% extractfiles/files.mk testcases.py
@mkdir -p testcases
python3 testcases.py $< > $@
clean::
@rm -f $(TESTCASE_SOURCE)
SIMPLE_PYTHON := $(addprefix python/test_,$(addsuffix .py,$(extract_files)))
clean::
@rm -rf $(SIMPLE_PYTHON) python/__pycache__ __pycache__
$(SIMPLE_PYTHON): python/test_%.py: testcases/% | python/gen.awk extractfiles/files.mk
gawk -f python/gen.awk $< > $@
.PHONY: test_python
test_python: ## Run test cases through python interpreter
test_python: $(SIMPLE_PYTHON) python/gen.awk
python3 -m unittest discover python -v
tests/main.c:
@echo "#include \"../vendor/EvilUnit/EvilUnit.h\"" > $@
@echo "MAIN_MODULE()" >> $@
@echo "{" >> $@
@echo " DEPENDS(tests_runner);" >> $@
@echo "}" >> $@
SIMPLE_TESTS := $(addprefix tests/test_,$(addsuffix .c,$(extract_files)))
$(SIMPLE_TESTS): tests/test_%.c: testcases/% tests/gen.awk extractfiles/files.mk
gawk -f tests/gen.awk $< > $@
SIMPLE_TESTS_SRC := $(SIMPLE_TESTS) tests/runner.c tests/main.c
clean::
rm -f $(SIMPLE_TESTS) tests/main.c
SIMPLE_TESTS_OBJ := $(SIMPLE_TESTS_SRC:tests/%.c=$(VENDOR_DIR_OBJ)/tests/%.o)
$(SIMPLE_TESTS_OBJ): $(VENDOR_DIR_OBJ)/tests/%.o: tests/%.c
@mkdir -p $(VENDOR_DIR_OBJ)/tests
@$(CC) $(CFLAGS) -O0 $< -c -o $@
simple: ## name tbd, Run test cases through C
simple: $(VENDOR_LIBTOMMATH_OBJ)
simple: $(VENDOR_DIR_OBJ)/proto_impl.o $(VENDOR_DIR_OBJ)/proto_derived.o $(VENDOR_DIR_OBJ)/proto_memory_check.o
simple: $(SIMPLE_TESTS_OBJ) $(DEMOLANG_OBJ)
@$(CC) $(CFLAGS) $^ $(LDFLAGS) -o $@
clean::
@rm -f simple
MEMORY_TESTS := $(addprefix memory/test_,$(addsuffix .c,$(extract_files)))
memory/main.c:
@echo "#include \"../vendor/EvilUnit/EvilUnit.h\"" > $@
@echo "MAIN_MODULE()" >> $@
@echo "{" >> $@
@echo " DEPENDS(tests_memchk_runner);" >> $@
@echo "}" >> $@
$(MEMORY_TESTS): memory/test_%.c: testcases/% memory/gen.awk extractfiles/files.mk
gawk -f memory/gen.awk $< > $@
MEMORY_TESTS_SRC := $(MEMORY_TESTS) memory/runner.c memory/main.c
clean::
rm -f $(MEMORY_TESTS) memory/main.c
MEMORY_TESTS_OBJ := $(MEMORY_TESTS_SRC:memory/%.c=$(VENDOR_DIR_OBJ)/memory/%.o)
$(MEMORY_TESTS_OBJ): $(VENDOR_DIR_OBJ)/memory/%.o: memory/%.c proto_memory_check.h
@mkdir -p $(VENDOR_DIR_OBJ)/memory
@$(CC) $(CFLAGS) -O0 $< -c -o $@
memory_test: ## name tbd, Run test cases through C
memory_test: $(VENDOR_LIBTOMMATH_OBJ)
memory_test: $(VENDOR_DIR_OBJ)/proto_impl.o $(VENDOR_DIR_OBJ)/proto_derived.o
memory_test: $(VENDOR_DIR_OBJ)/proto_memory_check.o
memory_test: $(MEMORY_TESTS_OBJ) $(DEMOLANG_OBJ)
@$(CC) $(CFLAGS) $^ $(LDFLAGS) -o $@
clean::
@rm -f memory_test
run_tests: $(SIMPLE_PYTHON) simple memory_test
python3 -m unittest discover python -v
./simple
./memory_test
calc:
calc: $(VENDOR_LIBTOMMATH_OBJ)
calc: $(VENDOR_DIR_OBJ)/proto_impl.o $(VENDOR_DIR_OBJ)/proto_derived.o $(VENDOR_DIR_OBJ)/proto_memory_check.o
# libfuzzer is a c++ thing
calc: $(DEMOLANG_OBJ)
@$(CXX) -std=c++11 $^ $(LDFLAGS) -o $@
clean::
@rm -rf $(DEMOLANG_DIR_OBJ)
@rm -f calc
fuzz_bigint/lexer.h: fuzz_bigint/lexer.h.re2c
re2c --no-debug-info -W -Wno-useless-escape --no-generation-date $< > $@
fuzz_bigint_source := $(addprefix fuzz_bigint/,bigint.cpp lexer.h base_operations.hpp bigint.hpp bigint_tommath.hpp interpreter.hpp fixnum.hpp vtable.hpp)
fuzz_bigint/bigint_tommath: $(fuzz_bigint_source)
$(CXX) -DBIGINT_IMPL=tommath -O2 -fsanitize=fuzzer $< -o $@
fuzz_bigint/bigint_fixnum: $(fuzz_bigint_source)
$(CXX) -DBIGINT_IMPL=fixnum -O2 -fsanitize=fuzzer $< -o $@
FUZZTESTS_TESTS := $(addprefix fuzz_bigint/test_,$(addsuffix .cpp,$(extract_files)))
$(FUZZTESTS_TESTS): fuzz_bigint/test_%.cpp: testcases/% fuzz_bigint/gen.awk extractfiles/files.mk
gawk -f fuzz_bigint/gen.awk $< > $@
fuzz_bigint/runner.cpp: tests/runner.c
cp $< $@
fuzz_bigint/main.cpp:
@echo "#include \"../vendor/EvilUnit/EvilUnit.h\"" > $@
@echo "MAIN_MODULE()" >> $@
@echo "{" >> $@
@echo " DEPENDS(tests_runner);" >> $@
@echo "}" >> $@
FUZZTESTS_SRC := $(FUZZTESTS_TESTS) fuzz_bigint/runner.cpp fuzz_bigint/main.cpp
FUZZTESTS_OBJ := $(FUZZTESTS_SRC:fuzz_bigint/%.cpp=$(VENDOR_DIR_OBJ)/fuzz_bigint/%.o)
$(FUZZTESTS_OBJ): $(VENDOR_DIR_OBJ)/fuzz_bigint/%.o: fuzz_bigint/%.cpp
@mkdir -p $(VENDOR_DIR_OBJ)/fuzz_bigint
@$(CXX) $(CXXFLAGS) -O0 $< -c -o $@
fuzz_bigint/fuzz_bigint_test: $(FUZZTESTS_OBJ)
@$(CXX) $(CXXFLAGS) -O0 $^ -o $@
clean::
@rm -f fuzz_bigint/lexer.h fuzz_bigint/bigint_tommath fuzz_bigint/bigint_fixnum fuzz_bigint/fuzz_bigint_test $(FUZZTESTS_SRC)
fuzztests: ## various fuzz test things
fuzztests: fuzz_bigint/bigint_tommath fuzz_bigint/bigint_fixnum $(FUZZTESTS_TESTS)
HELP_PADDING := 30
help:
@sed -E \
-e '/^([a-zA-Z_*.-]+::?[ ]*)##[ ]*([^#]*)$$/ !d # grep' \
-e 's/([a-zA-Z_*.-]+:):?(.*)/ \1\2/ # drop :: and prefix pad' \
-e ':again s/^([^#]{1,'$(HELP_PADDING)'})##[ ]*([^#]*)$$/\1 ##\2/ # insert a space' \
-e 't again # do it again (termination is via {1, HELP_PADDING})' \
-e 's/^([^#]*)##([^#]*)$$/\1\2/ # remove the ##' \
$(MAKEFILE_LIST)
.EXTRA_PREREQS+=$(foreach mk, ${MAKEFILE_LIST},$(abspath ${mk}))