tcc -MD: drop system includes and duplicates
[tinycc.git] / tests / Makefile
blobdfda50c12f7b171e68e5c3f0799c72b0fbd3dc69
2 # Tiny C Compiler Makefile - tests
5 TOP = ..
6 include $(TOP)/Makefile
7 VPATH = $(TOPSRC)/tests $(TOPSRC) $(TOP)
8 CFLAGS := $(filter-out -W% -g% -O%,$(CFLAGS)) -I$(TOPSRC) $(LDFLAGS)
10 # what tests to run
11 TESTS = \
12 hello-exe \
13 hello-run \
14 libtest \
15 libtest_mt \
16 test3 \
17 memtest \
18 dlltest \
19 abitest \
20 asm-c-connect-test \
21 vla_test-run \
22 cross-test \
23 tests2-dir \
24 pp-dir
26 BTESTS = btest test1b
27 # test4_static -- Not all relocation types are implemented yet.
28 # asmtest / asmtest2 -- minor differences with gcc
30 # bounds-checking is supported on i386 and x86_64 on linux and windows
31 ifeq (-$(CONFIG_musl)-, --)
32 ifeq ($(ARCH),i386)
33 TESTS += $(BTESTS)
34 endif
35 ifeq ($(ARCH),x86_64)
36 TESTS += $(BTESTS)
37 endif
38 ifeq ($(ARCH),arm)
39 TESTS += $(BTESTS)
40 endif
41 ifeq ($(ARCH),arm64)
42 TESTS += $(BTESTS)
43 endif
44 ifeq ($(ARCH),riscv64)
45 TESTS += $(BTESTS)
46 endif
47 endif
48 ifdef CONFIG_OSX # -run only
49 TESTS := hello-run libtest tests2-dir pp-dir
50 endif
51 ifeq (,$(filter arm64 i386 x86_64,$(ARCH)))
52 TESTS := $(filter-out vla_test-run,$(TESTS))
53 endif
54 ifeq ($(CONFIG_arm_eabi),yes)
55 TESTS := $(filter-out test3,$(TESTS))
56 endif
57 ifeq (,$(filter i386 x86_64,$(ARCH)))
58 TESTS := $(filter-out dlltest asm-c-connect-test,$(TESTS))
59 endif
61 ifeq ($(OS),Windows_NT) # for libtcc_test to find libtcc.dll
62 PATH := $(CURDIR)/$(TOP)$(if $(findstring ;,$(PATH)),;,:)$(PATH)
63 endif
65 ifeq ($(ARCH),arm)
66 # tcctest refers to the alignment of functions, and with thumb mode
67 # the low bit of code addresses selects the mode, so the "alignment"
68 # of functions via bit masking comes out as 1. Just disable thumb.
69 test.ref: CFLAGS+=-marm
70 endif
71 ifeq ($(ARCH),i386)
72 # tcctest.c:get_asm_string uses a construct that is checked too strictly
73 # by GCC in 32bit mode when PIC is enabled.
74 test.ref: CFLAGS+=-fno-PIC -fno-PIE
75 endif
77 RUN_TCC = $(NATIVE_DEFINES) -run $(TOPSRC)/tcc.c $(TCCFLAGS)
78 DISAS = objdump -d
79 DUMPTCC = (set -x; $(TOP)/tcc -vv; ldd $(TOP)/tcc; exit 1)
81 all test :
82 @$(MAKE) --no-print-directory -s clean
83 @$(MAKE) --no-print-directory -s -r $(TESTS)
85 hello-exe: ../examples/ex1.c
86 @echo ------------ $@ ------------
87 $(TCC) $< -o hello$(EXESUF) && ./hello$(EXESUF) || $(DUMPTCC)
89 hello-run: ../examples/ex1.c
90 @echo ------------ $@ ------------
91 $(TCC) -run $< || $(DUMPTCC)
93 libtes%: libtcc_tes%$(EXESUF)
94 @echo ------------ $@ ------------
95 ./libtcc_tes$*$(EXESUF) $(TOPSRC)/tcc.c $(TCCFLAGS) $(NATIVE_DEFINES)
97 libtcc_test$(EXESUF): libtcc_test.c $(LIBTCC)
98 $(CC) -o $@ $^ $(CFLAGS) $(LIBS)
100 libtcc_test_mt$(EXESUF): libtcc_test_mt.c $(LIBTCC)
101 $(CC) -o $@ $^ $(CFLAGS) $(LIBS)
103 %-dir:
104 @echo ------------ $@ ------------
105 $(MAKE) -k -C $*
107 # test.ref - generate using cc
108 test.ref: tcctest.c
109 $(CC) -o tcctest.gcc $< $(NATIVE_DEFINES) $(CFLAGS) -w -O0 -std=gnu99 -fno-omit-frame-pointer
110 ./tcctest.gcc > $@
112 # auto test
113 test1 test1b: tcctest.c test.ref
114 @echo ------------ $@ ------------
115 $(TCC) $(RUN_TCC) -w -run $< > test.out1
116 @diff -u test.ref test.out1 && echo "$(AUTO_TEST) OK"
118 # iterated test2 (compile tcc then compile tcctest.c !)
119 test2 test2b: tcctest.c test.ref
120 @echo ------------ $@ ------------
121 $(TCC) $(RUN_TCC) $(RUN_TCC) -w -run $< > test.out2
122 @diff -u test.ref test.out2 && echo "$(AUTO_TEST)2 OK"
124 # iterated test3 (compile tcc then compile tcc then compile tcctest.c !)
125 test3 test3b: tcctest.c test.ref
126 @echo ------------ $@ ------------
127 $(TCC) $(RUN_TCC) $(RUN_TCC) $(RUN_TCC) -w -run $< > test.out3
128 @diff -u test.ref test.out3 && echo "$(AUTO_TEST)3 OK"
130 AUTO_TEST = Auto Test
131 test%b : TCCFLAGS += -b -bt1
132 test%b : AUTO_TEST = Auto Bound-Test
134 # binary output test
135 test4: tcctest.c test.ref
136 @echo ------------ $@ ------------
137 # object + link output
138 $(TCC) -c -o tcctest3.o $<
139 $(TCC) -o tcctest3 tcctest3.o
140 ./tcctest3 > test3.out
141 @if diff -u test.ref test3.out ; then echo "Object $(AUTO_TEST) OK"; fi
142 # dynamic output
143 $(TCC) -o tcctest1 $<
144 ./tcctest1 > test1.out
145 @if diff -u test.ref test1.out ; then echo "Dynamic $(AUTO_TEST) OK"; fi
146 # dynamic output + bound check
147 $(TCC) -b -o tcctest4 $<
148 ./tcctest4 > test4.out
149 @if diff -u test.ref test4.out ; then echo "BCheck $(AUTO_TEST) OK"; fi
151 test4_static: tcctest.c test.ref
152 @echo ------------ $@ ------------
153 # static output.
154 $(TCC) -static -o tcctest2 $<
155 ./tcctest2 > test2.out
156 @if diff -u test.ref test2.out ; then echo "Static $(AUTO_TEST) OK"; fi
158 # use tcc to create libtcc.so/.dll and the tcc(.exe) frontend and run them
159 dlltest:
160 @echo ------------ $@ ------------
161 $(TCC) $(NATIVE_DEFINES) -DLIBTCC_AS_DLL $(TOPSRC)/libtcc.c $(LIBS) -shared -o libtcc2$(DLLSUF)
162 $(TCC) $(NATIVE_DEFINES) -DONE_SOURCE=0 $(TOPSRC)/tcc.c libtcc2$(DLLSUF) $(LIBS) -Wl,-rpath=. -o tcc2$(EXESUF)
163 ./tcc2$(EXESUF) $(TCCFLAGS) $(RUN_TCC) -run $(TOPSRC)/examples/ex1.c
164 ifndef CONFIG_WIN32
165 @echo ------------ $@ with PIC ------------
166 $(CC) $(CFLAGS) -fPIC $(NATIVE_DEFINES) -DLIBTCC_AS_DLL -c $(TOPSRC)/libtcc.c
167 $(TCC) libtcc.o $(LIBS) -shared -o libtcc2$(DLLSUF)
168 $(TCC) $(NATIVE_DEFINES) -DONE_SOURCE=0 $(TOPSRC)/tcc.c libtcc2$(DLLSUF) $(LIBS) -Wl,-rpath=. -o tcc2$(EXESUF)
169 ./tcc2$(EXESUF) $(TCCFLAGS) $(RUN_TCC) -run $(TOPSRC)/examples/ex1.c
170 endif
171 @rm tcc2$(EXESUF) libtcc2$(DLLSUF)
173 memtest:
174 @echo ------------ $@ ------------
175 $(CC) $(CFLAGS) $(NATIVE_DEFINES) -DMEM_DEBUG=2 $(TOPSRC)/tcc.c $(LIBS) -o memtest-tcc$(EXESUF)
176 ./memtest-tcc$(EXESUF) $(TCCFLAGS) $(NATIVE_DEFINES) $(TOPSRC)/tcc.c $(LIBS)
177 ./memtest-tcc$(EXESUF) $(TCCFLAGS) $(NATIVE_DEFINES) -run $(TOPSRC)/tcc.c $(TCCFLAGS) -w $(TOPSRC)/tests/tcctest.c
178 @echo OK
180 # memory and bound check auto test
181 BOUNDS_OK = 1 4 8 10 14 16
182 BOUNDS_FAIL= 2 5 6 7 9 11 12 13 15 17 18
184 btest: boundtest.c
185 @echo ------------ $@ ------------
186 @for i in $(BOUNDS_OK); do \
187 if $(TCC) -b -run $< $$i >/dev/null 2>&1 ; then \
188 echo "Test $$i succeeded as expected" ; \
189 else\
190 echo "Failed positive test $$i" ; exit 1 ; \
191 fi ;\
192 done ;\
193 for i in $(BOUNDS_FAIL); do \
194 if $(TCC) -b -bt1 -run $< $$i >/dev/null 2>&1 ; then \
195 echo "Failed negative test $$i" ; exit 1 ;\
196 else\
197 echo "Test $$i failed as expected" ; \
198 fi ;\
199 done ;\
200 echo Bound test OK
202 # speed test
203 speedtest: ex2 ex3
204 @echo ------------ $@ ------------
205 time ./ex2 1238 2 3 4 10 13 4
206 time $(TCC) -run $(TOPSRC)/examples/ex2.c 1238 2 3 4 10 13 4
207 time ./ex3 35
208 time $(TCC) -run $(TOPSRC)/examples/ex3.c 35
210 weaktest: tcctest.c test.ref
211 @echo ------------ $@ ------------
212 $(TCC) -c $< -o weaktest.tcc.o
213 $(CC) -c $< -o weaktest.gcc.o $(NATIVE_DEFINES) $(CFLAGS) -w -O0 -std=gnu99 -fno-omit-frame-pointer
214 objdump -t weaktest.tcc.o | grep ' w ' | sed -e 's/.* \([a-zA-Z0-9_]*\)$$/\1/' | LC_ALL=C sort > weaktest.tcc.o.txt
215 objdump -t weaktest.gcc.o | grep ' w ' | sed -e 's/.* \([a-zA-Z0-9_]*\)$$/\1/' | LC_ALL=C sort > weaktest.gcc.o.txt
216 diff weaktest.gcc.o.txt weaktest.tcc.o.txt && echo "Weak Auto Test OK"
218 ex%: $(TOPSRC)/examples/ex%.c
219 $(CC) -o $@ $< $(CFLAGS)
221 # tiny assembler testing
222 asmtest.ref: asmtest.S
223 $(CC) -Wa,-W -o asmtest.ref.o -c asmtest.S
224 objdump -D asmtest.ref.o > asmtest.ref
226 asmtest asmtest2: asmtest.ref
227 @echo ------------ $@ ------------
228 $(TCC) $(MAYBE_RUN_TCC) -c asmtest.S
229 objdump -D asmtest.o > asmtest.out
230 @if diff -u --ignore-matching-lines="file format" asmtest.ref asmtest.out ; then echo "ASM Auto Test OK"; fi
232 # test assembler with tcc compiled by itself
233 asmtest2: MAYBE_RUN_TCC = $(RUN_TCC)
235 # Check that code generated by libtcc is binary compatible with
236 # that generated by CC
237 abitest-cc.exe: abitest.c $(LIBTCC)
238 $(CC) -o $@ $^ $(CFLAGS) $(LIBS) -w
240 abitest-tcc.exe: abitest.c libtcc.c
241 $(TCC) -o $@ $^ $(NATIVE_DEFINES) $(LIBS)
243 abitest-% : abitest-%.exe
244 @echo ------------ $@ ------------
245 ./$< $(TCCFLAGS)
247 abitest: abitest-cc
248 ifneq ($(CONFIG_arm_eabi),yes) # not ARM soft-float
249 abitest: abitest-tcc
250 endif
252 vla_test$(EXESUF): vla_test.c
253 $(TCC) -o $@ $^
255 vla_test-run: vla_test$(EXESUF)
256 @echo ------------ $@ ------------
257 ./vla_test$(EXESUF)
259 asm-c-connect$(EXESUF): asm-c-connect-1.c asm-c-connect-2.c
260 $(TCC) -o $@ $^
262 asm-c-connect-%.o: asm-c-connect-%.c
263 $(TCC) -c -o $@ $<
265 asm-c-connect-sep$(EXESUF): asm-c-connect-1.o asm-c-connect-2.o
266 $(TCC) -o $@ $^
268 asm-c-connect-test: asm-c-connect$(EXESUF) asm-c-connect-sep$(EXESUF)
269 @echo ------------ $@ ------------
270 ./asm-c-connect$(EXESUF) > asm-c-connect.out1 && cat asm-c-connect.out1
271 ./asm-c-connect-sep$(EXESUF) > asm-c-connect.out2 && cat asm-c-connect.out2
272 @diff -u asm-c-connect.out1 asm-c-connect.out2 || (echo "error"; exit 1)
274 TCC_YY = $(foreach T,$(TCC_X),$(if $(wildcard $(TOP)/$T-tcc$(EXESUF)),$T))
276 cross-test :
277 $(if $(strip $(TCC_YY)),\
278 $(MAKE) $(foreach T,$(TCC_YY),cross-$T.test) --no-print-directory,:)
280 cross-%.test :
281 @echo ------------ $@ ------------
282 $(TOP)/$*-tcc$(EXESUF) -v $(TCCFLAGS-$(if $(findstring win,$*),win,unx))\
283 -c $(TOPSRC)/examples/ex3.c
285 # targets for development
286 %.bin: %.c tcc
287 $(TCC) -g -o $@ $<
288 $(DISAS) $@
290 instr: instr.o
291 objdump -d instr.o
293 instr.o: instr.S
294 $(CC) -o $@ -c $< -O2 -Wall -g
296 cache: tcc_g
297 cachegrind ./tcc_g -o /tmp/linpack -lm bench/linpack.c
298 vg_annotate tcc.c > /tmp/linpack.cache.log
300 # clean
301 clean:
302 rm -f *~ *.o *.a *.bin *.i *.ref *.out *.out? *.out?b *.cc *.gcc
303 rm -f *-cc *-gcc *-tcc *.exe hello libtcc_test vla_test tcctest[1234]
304 rm -f asm-c-connect$(EXESUF) asm-c-connect-sep$(EXESUF)
305 rm -f ex? tcc_g weaktest.*.txt *.def libtcc_test_mt
306 @$(MAKE) -C tests2 $@
307 @$(MAKE) -C pp $@