-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
148 lines (120 loc) · 3.97 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
.DEFAULT_GOAL = all
NAME = t3
PREFIX ?= /usr/local
CFLAGS = -Wall -g
BINDIR = $(PREFIX)/bin
BIN = $(NAME)
INSTBIN = $(addprefix $(BINDIR)/,$(BIN))
MAN1DIR = $(PREFIX)/share/man/man1
MAN1 = $(NAME).1
INSTMAN1 = $(addprefix $(MAN1DIR)/,$(MAN1))
.SUFFIXES: .x .1
.x.1:
help2man \
--section=1 \
--include=$< \
--no-info \
./$(basename $<) -o $@
$(NAME).1: $(BIN)
$(BINDIR)/%: %
@mkdir -p $(@D)
cp $< $@
chmod 555 $@
$(MAN1DIR)/%: %
@mkdir -p $(@D)
cp $< $@
chmod 444 $@
.PHONY: all install lint clean
all: $(BIN) $(MAN1)
install: $(INSTBIN) $(INSTMAN1)
lint: $(BIN).c
clang-tidy $< -- $(CFLAGS)
clean:
-rm -f $(BIN) $(MAN1)
TESTS_DIR = tests
TESTS = $(basename $(wildcard $(TESTS_DIR)/*.args))
TESTTMPDIR := $(shell mktemp -d)
define TEST_template =
$(eval testname := $(notdir $(test)))
.INTERMEDIATE: $(test).sh
$(test).sh: $(test).args
@mkdir -p $$(TESTTMPDIR)
@echo "#!/bin/sh" > $$@
@echo -n './$$(BIN) "$$$$1" ' >> $$@
@cat $(test).args >> $$@
@chmod +x $$@
# If the test files aren't present or make is invoked with
# REGENERATE_TEST_RESULTS=1, automatically outputs from args.
$(test).rc: $(test).sh $(if $(REGENERATE_TEST_RESULTS),FORCE)
@echo; echo "--> INFO: regenerating test outputs for $(test)"
$$< $(test).log > $(test).out 2> $(test).err; echo $$$$? > $(test).rc
@echo; echo "--> done ... adding to git"
git add $(test).rc
$(test).log $(test).out $(test).err: $(test).rc
touch $(test).log $(test).out $(test).err
git add -f $(test).log $(test).out $(test).err
.PHONY: $(test)/run
$(test)/run: $(test).sh $$(BIN) $(if $(wildcard .git),$(test).rc $(test).log $(test).out $(test).err)
@expectedrc=`cat $$(@D).rc`; \
echo "+ $$< $$(TESTTMPDIR)/$(testname).log > $$(TESTTMPDIR)/$(testname).out 2> $$(TESTTMPDIR)/$(testname).err"; \
$$< $$(TESTTMPDIR)/$(testname).log > $$(TESTTMPDIR)/$(testname).out 2> $$(TESTTMPDIR)/$(testname).err; \
actualrc=$$$$?; \
if [ $$$$actualrc -ne $$$$expectedrc ]; then \
echo "Failed test $$(@D)"; \
echo "Was expecting a return code of $$$$expectedrc and instead got return code $$$$actualrc"; \
echo "See contents of $$(@D) and $$(TESTTMPDIR)/$(testname)"; \
exit 1; \
fi
.PHONY: $(test)/log
$(test)/log: $(test)/run
@if [ -e $$(@D).log ]; then \
cmp $$(TESTTMPDIR)/$(testname).log $$(@D).log || ( \
echo "Failed test $$(@D)"; \
diff -u $$(@D).log $$(TESTTMPDIR)/$(testname).log; \
exit 1; \
); \
elif [ -s $$(TESTTMPDIR)/$(testname).log ]; then \
echo "Failed test $$(@D)"; \
echo "Produced STDOUT in $$(TESTTMPDIR)/$(testname).log when we were not expecting any"; \
exit 1; \
fi
.PHONY: $(test)/out
$(test)/out: $(test)/run
@if [ -e $$(@D).out ]; then \
cmp $$(TESTTMPDIR)/$(testname).out $$(@D).out || ( \
echo "Failed test $$(@D)"; \
diff -u $$(@D).out $$(TESTTMPDIR)/$(testname).out; \
exit 1; \
); \
elif [ -s $$(TESTTMPDIR)/$(testname).out ]; then \
echo "Failed test $$(@D)"; \
echo "Produced STDOUT in $$(TESTTMPDIR)/$(testname).out when we were not expecting any"; \
exit 1; \
fi
.PHONY: $(test)/err
$(test)/err: $(test)/run
@if [ -e $$(@D).err ]; then \
cmp $$(TESTTMPDIR)/$(testname).err $$(@D).err || ( \
echo "Failed test $$(@D)"; \
diff -u $$(@D).err $$(TESTTMPDIR)/$(testname).err; \
exit 1; \
); \
elif [ -s $$(TESTTMPDIR)/$(testname).err ]; then \
echo "Failed test $$(@D)"; \
echo "Produced STDERR in $$(TESTTMPDIR)/$(testname).err when we were not expecting any"; \
exit 1; \
fi
.PHONY: $(test)/test
$(test)/test: $(test)/log $(test)/out $(test)/err
.PHONY: test
test: $(test)/test
endef
$(foreach test,$(TESTS),$(eval $(call TEST_template)))
# The partial write test depends on the compiled tests/midline-flush binary.
tests/midline-flush/run: tests/midline-flush
# Once tests are complete (and successful), remove test results.
test:
@rm -rf $(TESTTMPDIR)
# Phony target to force re-evaluation of targets.
.PHONY: FORCE
FORCE: