update changelog
[tinycc.git] / tests / Makefile
bloba46c85243bae50df6fbafac8b789fc01312bc031
2 # Tiny C Compiler Makefile - tests
5 TOP = ..
6 include $(TOP)/Makefile
7 VPATH = $(TOPSRC)/tests $(TOPSRC) $(TOP)
8 CFLAGS := $(filter-out -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 # test4_static -- Not all relocation types are implemented yet.
27 # asmtest / asmtest2 -- minor differences with gcc
29 ifneq ($(CONFIG_bcheck),no)
30 TESTS += btest test1b
31 endif
32 ifeq ($(CONFIG_dll),no)
33 TESTS := $(filter-out dlltest, $(TESTS))
34 endif
35 ifeq (-$(CONFIG_arm_eabi)-$(CONFIG_arm_vfp)-,-yes--)
36 TESTS := $(filter-out test3 test1b,$(TESTS))
37 endif
38 ifeq (,$(filter i386 x86_64,$(ARCH)))
39 TESTS := $(filter-out asm-c-connect-test,$(TESTS))
40 endif
41 ifeq ($(OS),Windows_NT) # for libtcc_test to find libtcc.dll
42 PATH := $(CURDIR)/$(TOP)$(if $(findstring ;,$(PATH)),;,:)$(PATH)
43 endif
44 ifdef CONFIG_OSX
45 LIBS += $(LINK_LIBTCC)
46 endif
47 ifeq ($(ARCH),arm)
48 # tcctest refers to the alignment of functions, and with thumb mode
49 # the low bit of code addresses selects the mode, so the "alignment"
50 # of functions via bit masking comes out as 1. Just disable thumb.
51 test.ref: CFLAGS+=-marm
52 endif
53 ifeq ($(ARCH)$(CONFIG_WIN32),i386)
54 # tcctest.c:get_asm_string uses a construct that is checked too strictly
55 # by GCC in 32bit mode when PIC is enabled.
56 test.ref: CFLAGS+=-fno-PIC -fno-PIE -Wl,-z,notext
57 endif
58 ifeq ($(CC_NAME),msvc)
59 test.ref abitest : CC = gcc
60 endif
61 ifeq ($(TARGETOS),OpenBSD)
62 dlltest: CFLAGS+=-fno-stack-protector
63 endif
64 ifneq (,$(filter FreeBSD NetBSD,$(TARGETOS)))
65 # test3 has dlsym problems
66 TESTS := $(filter-out test3,$(TESTS))
67 TESTS += test1
68 endif
70 RUN_TCC = $(NATIVE_DEFINES) -run $(TOPSRC)/tcc.c $(TCCFLAGS)
71 DISAS = objdump -d
72 ifdef CONFIG_OSX
73 DUMPTCC = (set -x; $(TOP)/tcc -vv; otool -L $(TOP)/tcc; exit 1)
74 else
75 DUMPTCC = (set -x; $(TOP)/tcc -vv; ldd $(TOP)/tcc; exit 1)
76 endif
78 all test :
79 @$(MAKE) --no-print-directory -s clean
80 @$(MAKE) --no-print-directory -s -r $(TESTS)
82 hello-exe: ../examples/ex1.c
83 @echo ------------ $@ ------------
84 $(TCC) $< -o hello$(EXESUF) && ./hello$(EXESUF) || $(DUMPTCC)
86 hello-run: ../examples/ex1.c
87 @echo ------------ $@ ------------
88 $(TCC) -run $< || $(DUMPTCC)
90 libtes%: libtcc_tes%$(EXESUF)
91 @echo ------------ $@ ------------
92 ./libtcc_tes$*$(EXESUF) $(TOPSRC)/tcc.c $(TCCFLAGS) $(NATIVE_DEFINES)
94 libtcc_test$(EXESUF): libtcc_test.c $(LIBTCC)
95 $(CC) -o $@ $^ $(CFLAGS) $(LIBS)
97 libtcc_test_mt$(EXESUF): libtcc_test_mt.c $(LIBTCC)
98 $(CC) -o $@ $^ $(CFLAGS) $(LIBS)
100 %-dir:
101 @echo ------------ $@ ------------
102 $(MAKE) -k -C $*
104 # test.ref - generate using cc
105 test.ref: tcctest.c
106 $(CC) -o tcctest.gcc $< $(NATIVE_DEFINES) $(CFLAGS) -w -O0 -std=gnu99 -fno-omit-frame-pointer
107 ./tcctest.gcc > $@
109 # auto test
110 test1 test1b: tcctest.c test.ref
111 @echo ------------ $@ ------------
112 $(TCC) $(RUN_TCC) -w -run $< > test.out1
113 @diff -u test.ref test.out1 && echo "$(AUTO_TEST) OK"
115 # iterated test2 (compile tcc then compile tcctest.c !)
116 test2 test2b: tcctest.c test.ref
117 @echo ------------ $@ ------------
118 $(TCC) $(RUN_TCC) $(RUN_TCC) -w -run $< > test.out2
119 @diff -u test.ref test.out2 && echo "$(AUTO_TEST)2 OK"
121 # iterated test3 (compile tcc then compile tcc then compile tcctest.c !)
122 test3 test3b: tcctest.c test.ref
123 @echo ------------ $@ ------------
124 $(TCC) $(RUN_TCC) $(RUN_TCC) $(RUN_TCC) -w -run $< > test.out3
125 @diff -u test.ref test.out3 && echo "$(AUTO_TEST)3 OK"
127 AUTO_TEST = Auto Test
128 test%b : TCCFLAGS += -b -bt1
129 test%b : AUTO_TEST = Auto Bound-Test
131 # binary output test
132 test4: tcctest.c test.ref
133 @echo ------------ $@ ------------
134 # object + link output
135 $(TCC) -c -o tcctest3.o $<
136 $(TCC) -o tcctest3 tcctest3.o
137 ./tcctest3 > test3.out
138 @if diff -u test.ref test3.out ; then echo "Object $(AUTO_TEST) OK"; fi
139 # dynamic output
140 $(TCC) -o tcctest1 $<
141 ./tcctest1 > test1.out
142 @if diff -u test.ref test1.out ; then echo "Dynamic $(AUTO_TEST) OK"; fi
143 # dynamic output + bound check
144 $(TCC) -b -o tcctest4 $<
145 ./tcctest4 > test4.out
146 @if diff -u test.ref test4.out ; then echo "BCheck $(AUTO_TEST) OK"; fi
148 test4_static: tcctest.c test.ref
149 @echo ------------ $@ ------------
150 # static output.
151 $(TCC) -static -o tcctest2 $<
152 ./tcctest2 > test2.out
153 @if diff -u test.ref test2.out ; then echo "Static $(AUTO_TEST) OK"; fi
155 # use tcc to create libtcc.so/.dll and the tcc(.exe) frontend and run them
156 dlltest:
157 @echo ------------ $@ ------------
158 $(TCC) $(NATIVE_DEFINES) -DLIBTCC_AS_DLL $(TOPSRC)/libtcc.c $(LIBS) -shared -o libtcc2$(DLLSUF)
159 $(TCC) $(NATIVE_DEFINES) -DONE_SOURCE=0 $(TOPSRC)/tcc.c libtcc2$(DLLSUF) $(LIBS) -Wl,-rpath=. -o tcc2$(EXESUF)
160 ./tcc2$(EXESUF) $(TCCFLAGS) $(RUN_TCC) -run $(TOPSRC)/examples/ex1.c
161 ifeq (,$(filter Darwin WIN32,$(TARGETOS)))
162 @echo ------------ $@ with PIC ------------
163 $(CC) $(CFLAGS) -fPIC $(NATIVE_DEFINES) -DLIBTCC_AS_DLL -c $(TOPSRC)/libtcc.c
164 $(TCC) libtcc.o $(LIBS) -shared -o libtcc2$(DLLSUF)
165 $(TCC) $(NATIVE_DEFINES) -DONE_SOURCE=0 $(TOPSRC)/tcc.c libtcc2$(DLLSUF) $(LIBS) -Wl,-rpath=. -o tcc2$(EXESUF)
166 ./tcc2$(EXESUF) $(TCCFLAGS) $(RUN_TCC) -run $(TOPSRC)/examples/ex1.c
167 endif
168 @rm tcc2$(EXESUF) libtcc2$(DLLSUF)
170 memtest:
171 @echo ------------ $@ ------------
172 $(CC) $(CFLAGS) $(NATIVE_DEFINES) -DMEM_DEBUG=2 $(TOPSRC)/tcc.c $(LIBS) -o memtest-tcc$(EXESUF)
173 ./memtest-tcc$(EXESUF) $(TCCFLAGS) $(NATIVE_DEFINES) $(TOPSRC)/tcc.c $(LIBS)
174 ./memtest-tcc$(EXESUF) $(TCCFLAGS) $(NATIVE_DEFINES) -run $(TOPSRC)/tcc.c $(TCCFLAGS) -w $(TOPSRC)/tests/tcctest.c
175 @echo OK
177 # memory and bound check auto test
178 BOUNDS_OK = 1 4 8 10 14 16
179 BOUNDS_FAIL= 2 5 6 7 9 11 12 13 15 17 18
181 btest: boundtest.c
182 @echo ------------ $@ ------------
183 @for i in $(BOUNDS_OK); do \
184 if $(TCC) -b -run $< $$i >/dev/null 2>&1 ; then \
185 echo "Test $$i succeeded as expected" ; \
186 else\
187 echo "Failed positive test $$i" ; exit 1 ; \
188 fi ;\
189 done ;\
190 for i in $(BOUNDS_FAIL); do \
191 if $(TCC) -b -bt1 -run $< $$i >/dev/null 2>&1 ; then \
192 echo "Failed negative test $$i" ; exit 1 ;\
193 else\
194 echo "Test $$i failed as expected" ; \
195 fi ;\
196 done ;\
197 echo Bound test OK
199 # speed test
200 speedtest: ex2 ex3
201 @echo ------------ $@ ------------
202 time ./ex2 1238 2 3 4 10 13 4
203 time $(TCC) -run $(TOPSRC)/examples/ex2.c 1238 2 3 4 10 13 4
204 time ./ex3 35
205 time $(TCC) -run $(TOPSRC)/examples/ex3.c 35
207 weaktest: tcctest.c test.ref
208 @echo ------------ $@ ------------
209 $(TCC) -c $< -o weaktest.tcc.o
210 $(CC) -c $< -o weaktest.gcc.o $(NATIVE_DEFINES) $(CFLAGS) -w -O0 -std=gnu99 -fno-omit-frame-pointer
211 objdump -t weaktest.tcc.o | grep ' w ' | sed -e 's/.* \([a-zA-Z0-9_]*\)$$/\1/' | LC_ALL=C sort > weaktest.tcc.o.txt
212 objdump -t weaktest.gcc.o | grep ' w ' | sed -e 's/.* \([a-zA-Z0-9_]*\)$$/\1/' | LC_ALL=C sort > weaktest.gcc.o.txt
213 diff weaktest.gcc.o.txt weaktest.tcc.o.txt && echo "Weak Auto Test OK"
215 ex%: $(TOPSRC)/examples/ex%.c
216 $(CC) -o $@ $< $(CFLAGS)
218 # tiny assembler testing
219 asmtest.ref: asmtest.S
220 $(CC) -Wa,-W -Wa,-mx86-used-note=no -o asmtest.ref.o -c asmtest.S
221 objdump -D asmtest.ref.o > asmtest.ref
223 ifeq ($(ARCH),arm)
224 asmtest asmtest2:
225 TCC="${TCC}" ./arm-asm-testsuite.sh
226 else
227 asmtest asmtest2: asmtest.ref
228 @echo ------------ $@ ------------
229 $(TCC) $(MAYBE_RUN_TCC) -c asmtest.S
230 objdump -D asmtest.o > asmtest.out
231 @if diff -u --ignore-matching-lines="file format" asmtest.ref asmtest.out ; then echo "ASM Auto Test OK"; fi
232 endif
234 # test assembler with tcc compiled by itself
235 asmtest2: MAYBE_RUN_TCC = $(RUN_TCC)
237 # Check that code generated by libtcc is binary compatible with
238 # that generated by CC
239 abitest-cc.exe: abitest.c $(LIBTCC)
240 $(CC) -o $@ $^ $(CFLAGS) $(LIBS) -w
242 abitest-tcc.exe: abitest.c libtcc.c
243 $(TCC) -o $@ $^ $(NATIVE_DEFINES) $(LIBS)
245 abitest-% : abitest-%.exe
246 @echo ------------ $@ ------------
247 ./$< $(TCCFLAGS)
249 abitest: abitest-cc
250 ifneq (-$(CONFIG_arm_eabi)-$(CONFIG_arm_vfp)-,-yes--)
251 abitest: abitest-tcc
252 endif
254 vla_test$(EXESUF): vla_test.c
255 $(TCC) -o $@ $^
257 vla_test-run: vla_test$(EXESUF)
258 @echo ------------ $@ ------------
259 ./vla_test$(EXESUF)
261 .PHONY: abitest vla_test
263 asm-c-connect$(EXESUF): asm-c-connect-1.c asm-c-connect-2.c
264 $(TCC) -o $@ $^
266 asm-c-connect-%.o: asm-c-connect-%.c
267 $(TCC) -c -o $@ $<
269 asm-c-connect-sep$(EXESUF): asm-c-connect-1.o asm-c-connect-2.o
270 $(TCC) -o $@ $^
272 asm-c-connect-test: asm-c-connect$(EXESUF) asm-c-connect-sep$(EXESUF)
273 @echo ------------ $@ ------------
274 ./asm-c-connect$(EXESUF) > asm-c-connect.out1 && cat asm-c-connect.out1
275 ./asm-c-connect-sep$(EXESUF) > asm-c-connect.out2 && cat asm-c-connect.out2
276 @diff -u asm-c-connect.out1 asm-c-connect.out2 || (echo "error"; exit 1)
278 # quick sanity check for cross-compilers
279 cross-test : tcctest.c examples/ex3.c
280 @echo ------------ $@ ------------
281 $(foreach T,$(CROSS-TGTS),$(call CROSS-COMPILE,$T))
283 CROSS-TGTS = \
284 i386 \
285 i386-win32 \
286 i386-OpenBSD \
287 x86_64 \
288 x86_64-win32 \
289 x86_64-osx \
290 x86_64-FreeBSD \
291 x86_64-NetBSD \
292 x86_64-OpenBSD \
293 arm-fpa \
294 arm-eabihf \
295 arm-NetBSD \
296 arm-wince \
297 arm64 \
298 arm64-osx \
299 arm64-FreeBSD \
300 arm64-NetBSD \
301 arm64-OpenBSD \
302 riscv64 \
305 define CROSS-COMPILE
306 @echo " . $(1)"
307 $(TCC) $(DEF-$1) -DTCC_CROSS_TEST -run $(TOPSRC)/tcc.c \
308 -c $(if $(findstring c67,$1),$(filter %/ex3.c,$^),$<) -w $(TCCFLAGS)
310 endef
312 # targets for development
313 %.bin: %.c tcc
314 $(TCC) -g -o $@ $<
315 $(DISAS) $@
317 instr: instr.o
318 objdump -d instr.o
320 instr.o: instr.S
321 $(CC) -o $@ -c $< -O2 -Wall -g
323 cache: tcc_g
324 cachegrind ./tcc_g -o /tmp/linpack -lm bench/linpack.c
325 vg_annotate tcc.c > /tmp/linpack.cache.log
327 # clean
328 clean:
329 rm -f *~ *.o *.a *.bin *.i *.ref *.out *.out? *.out?b *.cc *.gcc
330 rm -f *-cc *-gcc *-tcc *.exe hello libtcc_test vla_test tcctest[1234]
331 rm -f asm-c-connect$(EXESUF) asm-c-connect-sep$(EXESUF)
332 rm -f ex? tcc_g weaktest.*.txt *.def *.pdb *.obj libtcc_test_mt
333 @$(MAKE) -C tests2 $@
334 @$(MAKE) -C pp $@