Makefile 9.7 KB
Newer Older
grischka's avatar
grischka 已提交
1
2
3
4
#
# Tiny C Compiler Makefile - tests
#

5
TOP = ..
6
include $(TOP)/Makefile
grischka's avatar
grischka 已提交
7
VPATH = $(TOPSRC)/tests $(TOPSRC) $(TOP)
grischka's avatar
grischka 已提交
8
CFLAGS := $(filter-out -W% -g% -O%,$(CFLAGS)) -I$(TOPSRC) $(LDFLAGS)
9

grischka's avatar
grischka 已提交
10
# what tests to run
grischka's avatar
grischka 已提交
11
TESTS = \
12
13
 hello-exe \
 hello-run \
grischka's avatar
grischka 已提交
14
 libtest \
15
 libtest_mt \
grischka's avatar
grischka 已提交
16
 test3 \
grischka's avatar
grischka 已提交
17
 memtest \
grischka's avatar
grischka 已提交
18
 dlltest \
19
 abitest \
20
 asm-c-connect-test \
21
 vla_test-run \
22
 cross-test \
23
24
 tests2-dir \
 pp-dir
25

26
# test4_static -- Not all relocation types are implemented yet.
27
# asmtest / asmtest2 -- minor differences with gcc
grischka's avatar
grischka 已提交
28

29
30
ifneq ($(CONFIG_bcheck),no)
 TESTS += btest test1b
31
endif
32
ifeq ($(CONFIG_dll),no)
Michael Matz's avatar
Michael Matz 已提交
33
 TESTS := $(filter-out dlltest, $(TESTS))
34
endif
35
ifeq (,$(filter arm64 i386 x86_64,$(ARCH)))
36
37
 TESTS := $(filter-out vla_test-run,$(TESTS))
endif
38
39
40
ifeq ($(CONFIG_arm_eabi),yes)
 TESTS := $(filter-out test3,$(TESTS))
endif
41
ifeq (,$(filter i386 x86_64,$(ARCH)))
42
 TESTS := $(filter-out asm-c-connect-test,$(TESTS))
grischka's avatar
grischka 已提交
43
endif
grischka's avatar
grischka 已提交
44
ifeq ($(OS),Windows_NT) # for libtcc_test to find libtcc.dll
grischka's avatar
grischka 已提交
45
 PATH := $(CURDIR)/$(TOP)$(if $(findstring ;,$(PATH)),;,:)$(PATH)
46
endif
47
48
49
ifdef CONFIG_OSX
LIBS += $(LINK_LIBTCC)
endif
50
51
52
53
54
55
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
56
ifeq ($(ARCH)$(CONFIG_WIN32),i386)
57
58
# tcctest.c:get_asm_string uses a construct that is checked too strictly
# by GCC in 32bit mode when PIC is enabled.
herman ten brugge's avatar
herman ten brugge 已提交
59
test.ref: CFLAGS+=-fno-PIC -fno-PIE -Wl,-z,notext
60
endif
61
62
63
ifeq ($(CC_NAME),msvc)
test.ref abitest : CC = gcc
endif
herman ten brugge's avatar
herman ten brugge 已提交
64
65
66
ifeq ($(TARGETOS),OpenBSD)
dlltest: CFLAGS+=-fno-stack-protector
endif
herman ten brugge's avatar
herman ten brugge 已提交
67
ifneq (,$(filter FreeBSD NetBSD,$(TARGETOS)))
herman ten brugge's avatar
herman ten brugge 已提交
68
69
70
71
  # test3 has dlsym problems
  TESTS := $(filter-out test3,$(TESTS))
  TESTS += test1
endif
72

73
RUN_TCC = $(NATIVE_DEFINES) -run $(TOPSRC)/tcc.c $(TCCFLAGS)
74
DISAS = objdump -d
75
76
77
ifdef CONFIG_OSX
DUMPTCC = (set -x; $(TOP)/tcc -vv; otool -L $(TOP)/tcc; exit 1)
else
grischka's avatar
grischka 已提交
78
DUMPTCC = (set -x; $(TOP)/tcc -vv; ldd $(TOP)/tcc; exit 1)
79
endif
80

Vincent Lefevre's avatar
Vincent Lefevre 已提交
81
all test :
grischka's avatar
grischka 已提交
82
83
	@$(MAKE) --no-print-directory -s clean
	@$(MAKE) --no-print-directory -s -r $(TESTS)
grischka's avatar
grischka 已提交
84

85
86
hello-exe: ../examples/ex1.c
	@echo ------------ $@ ------------
grischka's avatar
grischka 已提交
87
	$(TCC) $< -o hello$(EXESUF) && ./hello$(EXESUF) || $(DUMPTCC)
88
89

hello-run: ../examples/ex1.c
grischka's avatar
grischka 已提交
90
	@echo ------------ $@ ------------
grischka's avatar
grischka 已提交
91
	$(TCC) -run $< || $(DUMPTCC)
grischka's avatar
grischka 已提交
92

93
libtes%: libtcc_tes%$(EXESUF)
grischka's avatar
grischka 已提交
94
	@echo ------------ $@ ------------
95
	./libtcc_tes$*$(EXESUF) $(TOPSRC)/tcc.c $(TCCFLAGS) $(NATIVE_DEFINES)
grischka's avatar
grischka 已提交
96

grischka's avatar
grischka 已提交
97
98
99
100
libtcc_test$(EXESUF): libtcc_test.c $(LIBTCC)
	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)

libtcc_test_mt$(EXESUF): libtcc_test_mt.c $(LIBTCC)
101
	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
grischka's avatar
grischka 已提交
102

grischka's avatar
grischka 已提交
103
%-dir:
grischka's avatar
grischka 已提交
104
	@echo ------------ $@ ------------
grischka's avatar
grischka 已提交
105
	$(MAKE) -k -C $*
grischka's avatar
grischka 已提交
106

107
# test.ref - generate using cc
grischka's avatar
grischka 已提交
108
test.ref: tcctest.c
109
110
	$(CC) -o tcctest.gcc $< $(NATIVE_DEFINES) $(CFLAGS) -w -O0 -std=gnu99 -fno-omit-frame-pointer
	./tcctest.gcc > $@
grischka's avatar
grischka 已提交
111
112

# auto test
113
test1 test1b: tcctest.c test.ref
grischka's avatar
grischka 已提交
114
	@echo ------------ $@ ------------
grischka's avatar
grischka 已提交
115
	$(TCC) $(RUN_TCC) -w -run $< > test.out1
116
	@diff -u test.ref test.out1 && echo "$(AUTO_TEST) OK"
grischka's avatar
grischka 已提交
117
118

# iterated test2 (compile tcc then compile tcctest.c !)
119
test2 test2b: tcctest.c test.ref
grischka's avatar
grischka 已提交
120
	@echo ------------ $@ ------------
grischka's avatar
grischka 已提交
121
	$(TCC) $(RUN_TCC) $(RUN_TCC) -w -run $< > test.out2
122
	@diff -u test.ref test.out2 && echo "$(AUTO_TEST)2 OK"
grischka's avatar
grischka 已提交
123
124

# iterated test3 (compile tcc then compile tcc then compile tcctest.c !)
125
test3 test3b: tcctest.c test.ref
grischka's avatar
grischka 已提交
126
	@echo ------------ $@ ------------
grischka's avatar
grischka 已提交
127
	$(TCC) $(RUN_TCC) $(RUN_TCC) $(RUN_TCC) -w -run $< > test.out3
128
	@diff -u test.ref test.out3 && echo "$(AUTO_TEST)3 OK"
129

130
131
132
AUTO_TEST = Auto Test
test%b : TCCFLAGS += -b -bt1
test%b : AUTO_TEST = Auto Bound-Test
grischka's avatar
grischka 已提交
133
134

# binary output test
grischka's avatar
grischka 已提交
135
test4: tcctest.c test.ref
grischka's avatar
grischka 已提交
136
137
	@echo ------------ $@ ------------
# object + link output
grischka's avatar
grischka 已提交
138
	$(TCC) -c -o tcctest3.o $<
grischka's avatar
grischka 已提交
139
140
	$(TCC) -o tcctest3 tcctest3.o
	./tcctest3 > test3.out
141
	@if diff -u test.ref test3.out ; then echo "Object $(AUTO_TEST) OK"; fi
grischka's avatar
grischka 已提交
142
# dynamic output
grischka's avatar
grischka 已提交
143
	$(TCC) -o tcctest1 $<
grischka's avatar
grischka 已提交
144
	./tcctest1 > test1.out
145
	@if diff -u test.ref test1.out ; then echo "Dynamic $(AUTO_TEST) OK"; fi
grischka's avatar
grischka 已提交
146
# dynamic output + bound check
grischka's avatar
grischka 已提交
147
	$(TCC) -b -o tcctest4 $<
grischka's avatar
grischka 已提交
148
	./tcctest4 > test4.out
149
	@if diff -u test.ref test4.out ; then echo "BCheck $(AUTO_TEST) OK"; fi
150
151

test4_static: tcctest.c test.ref
grischka's avatar
grischka 已提交
152
	@echo ------------ $@ ------------
153
# static output.
grischka's avatar
grischka 已提交
154
	$(TCC) -static -o tcctest2 $<
grischka's avatar
grischka 已提交
155
	./tcctest2 > test2.out
156
	@if diff -u test.ref test2.out ; then echo "Static $(AUTO_TEST) OK"; fi
grischka's avatar
grischka 已提交
157

grischka's avatar
grischka 已提交
158
159
160
# use tcc to create libtcc.so/.dll and the tcc(.exe) frontend and run them
dlltest:
	@echo ------------ $@ ------------
161
162
	$(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)
grischka's avatar
grischka 已提交
163
	./tcc2$(EXESUF) $(TCCFLAGS) $(RUN_TCC) -run $(TOPSRC)/examples/ex1.c
grischka's avatar
grischka 已提交
164
165
ifndef CONFIG_WIN32
	@echo ------------ $@ with PIC ------------
166
	$(CC) $(CFLAGS) -fPIC $(NATIVE_DEFINES) -DLIBTCC_AS_DLL -c $(TOPSRC)/libtcc.c
grischka's avatar
grischka 已提交
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)
grischka's avatar
grischka 已提交
169
	./tcc2$(EXESUF) $(TCCFLAGS) $(RUN_TCC) -run $(TOPSRC)/examples/ex1.c
grischka's avatar
grischka 已提交
170
171
172
endif
	@rm tcc2$(EXESUF) libtcc2$(DLLSUF)

grischka's avatar
grischka 已提交
173
174
memtest:
	@echo ------------ $@ ------------
175
176
	$(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)
177
	./memtest-tcc$(EXESUF) $(TCCFLAGS) $(NATIVE_DEFINES) -run $(TOPSRC)/tcc.c $(TCCFLAGS) -w $(TOPSRC)/tests/tcctest.c
grischka's avatar
grischka 已提交
178
	@echo OK
grischka's avatar
grischka 已提交
179

grischka's avatar
grischka 已提交
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
grischka's avatar
grischka 已提交
183

grischka's avatar
grischka 已提交
184
btest: boundtest.c
grischka's avatar
grischka 已提交
185
	@echo ------------ $@ ------------
grischka's avatar
grischka 已提交
186
	@for i in $(BOUNDS_OK); do \
grischka's avatar
grischka 已提交
187
188
	   if $(TCC) -b -run $< $$i >/dev/null 2>&1 ; then \
	       echo "Test $$i succeeded as expected" ; \
grischka's avatar
grischka 已提交
189
	   else\
grischka's avatar
grischka 已提交
190
	       echo "Failed positive test $$i" ; exit 1 ; \
grischka's avatar
grischka 已提交
191
192
193
	   fi ;\
	done ;\
	for i in $(BOUNDS_FAIL); do \
grischka's avatar
grischka 已提交
194
195
	   if $(TCC) -b -bt1 -run $< $$i >/dev/null 2>&1 ; then \
	       echo "Failed negative test $$i" ; exit 1 ;\
grischka's avatar
grischka 已提交
196
	   else\
grischka's avatar
grischka 已提交
197
	       echo "Test $$i failed as expected" ; \
grischka's avatar
grischka 已提交
198
199
	   fi ;\
	done ;\
200
	echo Bound test OK
grischka's avatar
grischka 已提交
201
202

# speed test
203
speedtest: ex2 ex3
grischka's avatar
grischka 已提交
204
205
	@echo ------------ $@ ------------
	time ./ex2 1238 2 3 4 10 13 4
grischka's avatar
grischka 已提交
206
	time $(TCC) -run $(TOPSRC)/examples/ex2.c 1238 2 3 4 10 13 4
grischka's avatar
grischka 已提交
207
	time ./ex3 35
grischka's avatar
grischka 已提交
208
	time $(TCC) -run $(TOPSRC)/examples/ex3.c 35
grischka's avatar
grischka 已提交
209

grischka's avatar
grischka 已提交
210
weaktest: tcctest.c test.ref
grischka's avatar
grischka 已提交
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
216
	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"
217

grischka's avatar
grischka 已提交
218
ex%: $(TOPSRC)/examples/ex%.c
219
	$(CC) -o $@ $< $(CFLAGS)
grischka's avatar
grischka 已提交
220
221
222

# tiny assembler testing
asmtest.ref: asmtest.S
223
	$(CC) -Wa,-W -o asmtest.ref.o -c asmtest.S
224
	objdump -D asmtest.ref.o > asmtest.ref
grischka's avatar
grischka 已提交
225

226
227
ifeq ($(ARCH),arm)
asmtest asmtest2:
228
	TCC="${TCC}" ./arm-asm-testsuite.sh
229
else
230
asmtest asmtest2: asmtest.ref
grischka's avatar
grischka 已提交
231
	@echo ------------ $@ ------------
232
	$(TCC) $(MAYBE_RUN_TCC) -c asmtest.S
233
	objdump -D asmtest.o > asmtest.out
grischka's avatar
grischka 已提交
234
	@if diff -u --ignore-matching-lines="file format" asmtest.ref asmtest.out ; then echo "ASM Auto Test OK"; fi
235
endif
grischka's avatar
grischka 已提交
236

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

240
241
# Check that code generated by libtcc is binary compatible with
# that generated by CC
grischka's avatar
grischka 已提交
242
abitest-cc.exe: abitest.c $(LIBTCC)
243
	$(CC) -o $@ $^ $(CFLAGS) $(LIBS) -w
244

grischka's avatar
grischka 已提交
245
abitest-tcc.exe: abitest.c libtcc.c
246
	$(TCC) -o $@ $^ $(NATIVE_DEFINES) $(LIBS)
247

grischka's avatar
grischka 已提交
248
abitest-% : abitest-%.exe
249
	@echo ------------ $@ ------------
grischka's avatar
grischka 已提交
250
251
252
	./$< $(TCCFLAGS)

abitest: abitest-cc
253
ifneq ($(CONFIG_arm_eabi),yes) # not ARM soft-float
grischka's avatar
grischka 已提交
254
abitest: abitest-tcc
255
endif
256

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

260
261
262
263
vla_test-run: vla_test$(EXESUF)
	@echo ------------ $@ ------------
	./vla_test$(EXESUF)

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

267
268
269
270
271
272
273
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)
274
	@echo ------------ $@ ------------
grischka's avatar
grischka 已提交
275
276
	./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
grischka's avatar
grischka 已提交
277
278
	@diff -u asm-c-connect.out1 asm-c-connect.out2 || (echo "error"; exit 1)

grischka's avatar
grischka 已提交
279
280
# quick sanity check for cross-compilers
cross-test : tcctest.c examples/ex3.c
281
	@echo ------------ $@ ------------
grischka's avatar
grischka 已提交
282
283
	$(foreach T,$(CROSS-TGTS),$(call CROSS-COMPILE,$T))

284
285
CROSS-MACOS= # x86_64-osx

grischka's avatar
grischka 已提交
286
287
288
CROSS-TGTS = \
 i386 \
 i386-win32 \
289
 i386-OpenBSD \
grischka's avatar
grischka 已提交
290
291
 x86_64 \
 x86_64-win32 \
292
 $(CROSS-MACOS) \
grischka's avatar
grischka 已提交
293
294
295
296
297
 x86_64-FreeBSD \
 x86_64-NetBSD \
 x86_64-OpenBSD \
 arm-fpa \
 arm-eabihf \
298
 arm-NetBSD \
grischka's avatar
grischka 已提交
299
300
 arm-wince \
 arm64 \
301
302
303
 arm64-FreeBSD \
 arm64-NetBSD \
 arm64-OpenBSD \
grischka's avatar
grischka 已提交
304
305
306
307
308
309
310
311
312
 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
313

grischka's avatar
grischka 已提交
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# 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:
grischka's avatar
grischka 已提交
331
332
	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]
333
	rm -f asm-c-connect$(EXESUF) asm-c-connect-sep$(EXESUF)
334
	rm -f ex? tcc_g weaktest.*.txt *.def *.pdb *.obj libtcc_test_mt
grischka's avatar
grischka 已提交
335
336
	@$(MAKE) -C tests2 $@
	@$(MAKE) -C pp $@
337