summaryrefslogtreecommitdiff
path: root/tests/Makefile
blob: c89e2988e63158bc9ce3ebb93efb72f2fa4aadc6 (plain)
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
347
348
349
#
# Tiny C Compiler Makefile - tests
#

TOP = ..
include $(TOP)/Makefile
VPATH = $(TOPSRC)/tests $(TOPSRC) $(TOP)
CFLAGS := $(filter-out -g% -O%,$(CFLAGS)) -I$(TOPSRC) $(LDFLAGS)

# what tests to run
TESTS = \
 hello-exe \
 hello-run \
 libtest \
 libtest_mt \
 test3 \
 abitest \
 asm-c-connect-test \
 vla_test-run \
 tests2-dir \
 pp-dir \
 memtest \
 dlltest \
 cross-test

# test4_static -- Not all relocation types are implemented yet.
# asmtest / asmtest2 -- minor differences with gcc

ifeq ($(CONFIG_backtrace),no)
 TESTS := $(filter-out libtest_mt, $(TESTS))
else ifneq ($(CONFIG_bcheck),no)
 TESTS += btest test1b tccb
endif
ifeq ($(CONFIG_dll),no)
 TESTS := $(filter-out dlltest, $(TESTS))
endif
ifeq (-$(CONFIG_arm_eabi)-$(CONFIG_arm_vfp)-,-yes--)
 TESTS := $(filter-out test3 test1b,$(TESTS))
endif
ifeq (,$(filter i386 x86_64,$(ARCH)))
 TESTS := $(filter-out asm-c-connect-test,$(TESTS))
endif
ifeq ($(OS),Windows_NT) # for libtcc_test to find libtcc.dll
 PATH := $(CURDIR)/$(TOP)$(if $(findstring ;,$(PATH)),;,:)$(PATH)
endif
ifdef CONFIG_OSX
LIBS += $(LINK_LIBTCC)
endif
ifeq ($(ARCH),arm)
# tcctest refers to the alignment of functions, and with thumb mode
# the low bit of code addresses selects the mode, so the "alignment"
# of functions via bit masking comes out as 1.  Just disable thumb.
test.ref: CFLAGS+=-marm
endif
ifeq ($(ARCH)$(CONFIG_WIN32),i386)
# tcctest.c:get_asm_string uses a construct that is checked too strictly
# by GCC in 32bit mode when PIC is enabled.
test.ref: CFLAGS+=-fno-PIC -fno-PIE -Wl,-z,notext
endif
ifeq ($(CC_NAME),msvc)
test.ref abitest : CC = gcc
endif
ifeq ($(TARGETOS),OpenBSD)
dlltest: CFLAGS+=-fno-stack-protector
endif
ifneq (,$(filter FreeBSD NetBSD,$(TARGETOS)))
  # test3 has dlsym problems
  TESTS := $(filter-out test3,$(TESTS))
  TESTS += test1
endif

RUN_TCC = $(NATIVE_DEFINES) -run $(TOPSRC)/tcc.c $(TCCFLAGS)
DISAS = objdump -d
ifdef CONFIG_OSX
DUMPTCC = (set -x; $(TOP)/tcc -vv; otool -L $(TOP)/tcc; exit 1)
else
DUMPTCC = (set -x; $(TOP)/tcc -vv; ldd $(TOP)/tcc; exit 1)
endif

all test :
	@echo ------------ version ------------
	@$(TCC_LOCAL) -v
	@$(MAKE) --no-print-directory -s clean
	@$(MAKE) --no-print-directory -s -r _all

_all : $(TESTS)

hello-exe: ../examples/ex1.c
	@echo ------------ $@ ------------
	$(TCC) $< -o hello$(EXESUF) && ./hello$(EXESUF) || $(DUMPTCC)

hello-run: ../examples/ex1.c
	@echo ------------ $@ ------------
	$(TCC) -run $< || $(DUMPTCC)

libtes%: libtcc_tes%$(EXESUF)
	@echo ------------ $@ ------------
	./libtcc_tes$*$(EXESUF) $(TOPSRC)/tcc.c $(TCCFLAGS) $(NATIVE_DEFINES)

libtcc_test$(EXESUF): libtcc_test.c $(LIBTCC)
	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)

libtcc_test_mt$(EXESUF): libtcc_test_mt.c $(LIBTCC)
	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)

%-dir:
	@echo ------------ $@ ------------
	$(MAKE) -k -C $*

# test.ref - generate using cc
test.ref: tcctest.c
	$(CC) -o tcctest.gcc $< $(NATIVE_DEFINES) $(CFLAGS) -w -O0 -std=gnu99 -fno-omit-frame-pointer
	./tcctest.gcc > $@

# auto test
test1 test1b: tcctest.c test.ref
	@echo ------------ $@ ------------
	$(TCC) $(RUN_TCC) -w -run $< > test.out1
	@diff -u test.ref test.out1 && echo "$(AUTO_TEST) OK"

# iterated test2 (compile tcc then compile tcctest.c !)
test2 test2b: tcctest.c test.ref
	@echo ------------ $@ ------------
	$(TCC) $(RUN_TCC) $(RUN_TCC) -w -run $< > test.out2
	@diff -u test.ref test.out2 && echo "$(AUTO_TEST)2 OK"

# iterated test3 (compile tcc then compile tcc then compile tcctest.c !)
test3 test3b: tcctest.c test.ref
	@echo ------------ $@ ------------
	$(TCC) $(RUN_TCC) $(RUN_TCC) $(RUN_TCC) -w -run $< > test.out3
	@diff -u test.ref test.out3 && echo "$(AUTO_TEST)3 OK"

AUTO_TEST = Auto Test
test%b : TCCFLAGS += -b -bt1
test%b : AUTO_TEST = Auto Bound-Test

# binary output test
test4: tcctest.c test.ref
	@echo ------------ $@ ------------
# object + link output
	$(TCC) -c -o tcctest3.o $<
	$(TCC) -o tcctest3 tcctest3.o
	./tcctest3 > test3.out
	@if diff -u test.ref test3.out ; then echo "Object $(AUTO_TEST) OK"; fi
# dynamic output
	$(TCC) -o tcctest1 $<
	./tcctest1 > test1.out
	@if diff -u test.ref test1.out ; then echo "Dynamic $(AUTO_TEST) OK"; fi
# dynamic output + bound check
	$(TCC) -b -o tcctest4 $<
	./tcctest4 > test4.out
	@if diff -u test.ref test4.out ; then echo "BCheck $(AUTO_TEST) OK"; fi

test4_static: tcctest.c test.ref
	@echo ------------ $@ ------------
# static output.
	$(TCC) -static -o tcctest2 $<
	./tcctest2 > test2.out
	@if diff -u test.ref test2.out ; then echo "Static $(AUTO_TEST) OK"; fi

# use tcc to create libtcc.so/.dll and the tcc(.exe) frontend and run them
dlltest:
	@echo ------------ $@ ------------
	$(TCC) $(NATIVE_DEFINES) -DLIBTCC_AS_DLL $(TOPSRC)/libtcc.c $(LIBS) -shared -o libtcc2$(DLLSUF)
	$(TCC) $(NATIVE_DEFINES) -DONE_SOURCE=0 $(TOPSRC)/tcc.c libtcc2$(DLLSUF) $(LIBS) -Wl,-rpath=. -o tcc2$(EXESUF)
	./tcc2$(EXESUF) $(TCCFLAGS) $(RUN_TCC) -run $(TOPSRC)/examples/ex1.c
ifeq (,$(filter Darwin WIN32,$(TARGETOS)))
	@echo ------------ $@ with PIC ------------
	$(CC) $(CFLAGS) -fPIC $(NATIVE_DEFINES) -DLIBTCC_AS_DLL -c $(TOPSRC)/libtcc.c
	$(TCC) libtcc.o $(LIBS) -shared -o libtcc2$(DLLSUF)
	$(TCC) $(NATIVE_DEFINES) -DONE_SOURCE=0 $(TOPSRC)/tcc.c libtcc2$(DLLSUF) $(LIBS) -Wl,-rpath=. -o tcc2$(EXESUF)
	./tcc2$(EXESUF) $(TCCFLAGS) $(RUN_TCC) -run $(TOPSRC)/examples/ex1.c
endif
	@rm tcc2$(EXESUF) libtcc2$(DLLSUF)

memtest:
	@echo ------------ $@ ------------
	$(CC) $(CFLAGS) $(NATIVE_DEFINES) -DMEM_DEBUG=2 $(TOPSRC)/tcc.c $(LIBS) -o memtest-tcc$(EXESUF)
	./memtest-tcc$(EXESUF) $(TCCFLAGS) $(NATIVE_DEFINES) $(TOPSRC)/tcc.c $(LIBS)
	./memtest-tcc$(EXESUF) $(TCCFLAGS) $(NATIVE_DEFINES) -run $(TOPSRC)/tcc.c $(TCCFLAGS) -w $(TOPSRC)/tests/tcctest.c
	@echo OK

# memory and bound check auto test
BOUNDS_OK  = 1 4 8 10 14 16
BOUNDS_FAIL= 2 5 6 7 9 11 12 13 15 17 18

btest: boundtest.c
	@echo ------------ $@ ------------
	@for i in $(BOUNDS_OK); do \
	   if $(TCC) -b -run $< $$i >/dev/null 2>&1 ; then \
	       echo "Test $$i succeeded as expected" ; \
	   else\
	       echo "Failed positive test $$i" ; exit 1 ; \
	   fi ;\
	done ;\
	for i in $(BOUNDS_FAIL); do \
	   if $(TCC) -b -bt1 -run $< $$i >/dev/null 2>&1 ; then \
	       echo "Failed negative test $$i" ; exit 1 ;\
	   else\
	       echo "Test $$i failed as expected" ; \
	   fi ;\
	done ;\
	echo Bound-Test OK

tccb:
	@echo ------------ $@ ------------
	$(TCC) -b $(TOPSRC)/tcc.c $(TCCFLAGS) $(NATIVE_DEFINES) -o tccb1.exe
	mv tccb1.exe tccb2.exe
	./tccb2.exe -b $(TOPSRC)/tcc.c $(TCCFLAGS) $(NATIVE_DEFINES) -o tccb1.exe
	cmp -s tccb1.exe tccb2.exe && echo "Exe Bound-Test OK"


# speed test
speedtest: ex2 ex3
	@echo ------------ $@ ------------
	time ./ex2 1238 2 3 4 10 13 4
	time $(TCC) -run $(TOPSRC)/examples/ex2.c 1238 2 3 4 10 13 4
	time ./ex3 35
	time $(TCC) -run $(TOPSRC)/examples/ex3.c 35

weaktest: tcctest.c test.ref
	@echo ------------ $@ ------------
	$(TCC) -c $< -o weaktest.tcc.o
	$(CC) -c $< -o weaktest.gcc.o $(NATIVE_DEFINES) $(CFLAGS) -w -O0 -std=gnu99 -fno-omit-frame-pointer
	objdump -t weaktest.tcc.o | grep ' w ' | sed -e 's/.* \([a-zA-Z0-9_]*\)$$/\1/' | LC_ALL=C sort > weaktest.tcc.o.txt
	objdump -t weaktest.gcc.o | grep ' w ' | sed -e 's/.* \([a-zA-Z0-9_]*\)$$/\1/' | LC_ALL=C sort > weaktest.gcc.o.txt
	diff weaktest.gcc.o.txt weaktest.tcc.o.txt && echo "Weak Auto Test OK"

ex%: $(TOPSRC)/examples/ex%.c
	$(CC) -o $@ $< $(CFLAGS)

# tiny assembler testing
asmtest.ref: asmtest.S
	$(CC) -Wa,-W -Wa,-mx86-used-note=no -o asmtest.ref.o -c asmtest.S
	objdump -D asmtest.ref.o > asmtest.ref

ifeq ($(ARCH),arm)
asmtest asmtest2:
	TCC="${TCC}" ./arm-asm-testsuite.sh
else
asmtest asmtest2: asmtest.ref
	@echo ------------ $@ ------------
	$(TCC) $(MAYBE_RUN_TCC) -c asmtest.S
	objdump -D asmtest.o > asmtest.out
	@if diff -u --ignore-matching-lines="file format" asmtest.ref asmtest.out ; then echo "ASM Auto Test OK"; fi
endif

# test assembler with tcc compiled by itself
asmtest2: MAYBE_RUN_TCC = $(RUN_TCC)

# Check that code generated by libtcc is binary compatible with
# that generated by CC
abitest-cc.exe: abitest.c $(LIBTCC)
	$(CC) -o $@ $^ $(CFLAGS) $(LIBS) -w

abitest-tcc.exe: abitest.c libtcc.c
	$(TCC) -o $@ $^ $(NATIVE_DEFINES) $(LIBS)

abitest-% : abitest-%.exe
	@echo ------------ $@ ------------
	./$< $(TCCFLAGS)

abitest: abitest-cc
ifneq (-$(CONFIG_arm_eabi)-$(CONFIG_arm_vfp)-,-yes--)
abitest: abitest-tcc
endif

vla_test$(EXESUF): vla_test.c
	$(TCC) -o $@ $^

vla_test-run: vla_test$(EXESUF)
	@echo ------------ $@ ------------
	./vla_test$(EXESUF)

.PHONY: abitest vla_test tccb

asm-c-connect$(EXESUF): asm-c-connect-1.c asm-c-connect-2.c
	$(TCC) -o $@ $^

asm-c-connect-%.o: asm-c-connect-%.c
	$(TCC) -c -o $@ $<

asm-c-connect-sep$(EXESUF): asm-c-connect-1.o asm-c-connect-2.o
	$(TCC) -o $@ $^

asm-c-connect-test: asm-c-connect$(EXESUF) asm-c-connect-sep$(EXESUF)
	@echo ------------ $@ ------------
	./asm-c-connect$(EXESUF) > asm-c-connect.out1 && cat asm-c-connect.out1
	./asm-c-connect-sep$(EXESUF) > asm-c-connect.out2 && cat asm-c-connect.out2
	@diff -u asm-c-connect.out1 asm-c-connect.out2 || (echo "error"; exit 1)

# quick sanity check for cross-compilers
cross-test : tcctest.c examples/ex3.c
	@echo ------------ $@ ------------
	$(foreach T,$(CROSS-TGTS),$(call CROSS-COMPILE,$T))

CROSS-TGTS = \
 i386 \
 i386-win32 \
 i386-OpenBSD \
 x86_64 \
 x86_64-win32 \
 x86_64-osx \
 x86_64-FreeBSD \
 x86_64-NetBSD \
 x86_64-OpenBSD \
 arm-fpa \
 arm-eabihf \
 arm-NetBSD \
 arm-wince \
 arm64 \
 arm64-osx \
 arm64-FreeBSD \
 arm64-NetBSD \
 arm64-OpenBSD \
 riscv64 \
 c67

define CROSS-COMPILE
 @echo " . $(1)"
 $(TCC) $(DEF-$1) -DTCC_CROSS_TEST -run $(TOPSRC)/tcc.c \
    -c $(if $(findstring c67,$1),$(filter %/ex3.c,$^),$<) -w $(TCCFLAGS)

endef

# targets for development
%.bin: %.c tcc
	$(TCC) -g -o $@ $<
	$(DISAS) $@

instr: instr.o
	objdump -d instr.o

instr.o: instr.S
	$(CC) -o $@ -c $< -O2 -Wall -g

cache: tcc_g
	cachegrind ./tcc_g -o /tmp/linpack -lm bench/linpack.c
	vg_annotate tcc.c > /tmp/linpack.cache.log

# clean
clean:
	rm -f *~ *.o *.a *.bin *.i *.ref *.out *.out? *.out?b *.cc *.gcc
	rm -f *-cc *-gcc *-tcc *.exe hello libtcc_test vla_test tcctest[1234]
	rm -f asm-c-connect$(EXESUF) asm-c-connect-sep$(EXESUF)
	rm -f ex? tcc_g weaktest.*.txt *.def *.pdb *.obj libtcc_test_mt
	@$(MAKE) -C tests2 $@
	@$(MAKE) -C pp $@