1# Copyright (C) 1995-2022 Free Software Foundation, Inc.
2# This file is part of the GNU C Library.
3
4# The GNU C Library is free software; you can redistribute it and/or
5# modify it under the terms of the GNU Lesser General Public
6# License as published by the Free Software Foundation; either
7# version 2.1 of the License, or (at your option) any later version.
8
9# The GNU C Library is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12# Lesser General Public License for more details.
13
14# You should have received a copy of the GNU Lesser General Public
15# License along with the GNU C Library; if not, see
16# <https://www.gnu.org/licenses/>.
17
18#
19#	Specific makefile for libio.
20#
21subdir	:= libio
22
23include ../Makeconfig
24
25headers	:= stdio.h \
26	   bits/stdio.h bits/stdio2.h bits/stdio2-decl.h bits/stdio-ldbl.h \
27	   bits/types/FILE.h bits/types/__FILE.h bits/types/struct_FILE.h \
28	   bits/types/__fpos_t.h bits/types/__fpos64_t.h \
29	   bits/types/cookie_io_functions_t.h
30
31routines	:=							      \
32	filedoalloc iofclose iofdopen iofflush iofgetpos iofgets iofopen      \
33	iofopncook iofputs iofread iofsetpos ioftell wfiledoalloc	      \
34	iofwrite iogetdelim iogetline iogets iopadn iopopen ioputs	      \
35	ioseekoff ioseekpos iosetbuffer iosetvbuf ioungetc		      \
36	iovsprintf iovsscanf						      \
37	iofgetpos64 iofopen64 iofsetpos64				      \
38	fputwc fputwc_u getwc getwc_u getwchar getwchar_u iofgetws iofgetws_u \
39	iofputws iofputws_u iogetwline iowpadn ioungetwc putwc putwc_u	      \
40	putwchar putwchar_u putchar putchar_u fwprintf swprintf vwprintf      \
41	wprintf wscanf fwscanf vwscanf vswprintf iovswscanf swscanf wgenops   \
42	wstrops wfileops iofwide fwide wmemstream			      \
43									      \
44	clearerr feof ferror fileno fputc freopen fseek getc getchar	      \
45	memstream pclose putc putchar rewind setbuf setlinebuf vasprintf      \
46	iovdprintf vscanf vsnprintf obprintf fcloseall fseeko ftello	      \
47	freopen64 fseeko64 ftello64					      \
48									      \
49	__fbufsize __freading __fwriting __freadable __fwritable __flbf	      \
50	__fpurge __fpending __fsetlocking				      \
51									      \
52	libc_fatal fmemopen oldfmemopen vtables
53
54tests = tst_swprintf tst_wprintf tst_swscanf tst_wscanf tst_getwc tst_putwc   \
55	tst_wprintf2 tst-widetext test-fmemopen tst-ext tst-ext2 \
56	tst-fgetws tst-ungetwc1 tst-ungetwc2 tst-swscanf tst-sscanf	      \
57	tst-mmap-setvbuf bug-ungetwc1 bug-ungetwc2 tst-atime tst-eof          \
58	tst-freopen bug-rewind bug-rewind2 bug-ungetc bug-fseek \
59	tst-mmap-eofsync tst-mmap-fflushsync bug-mmap-fflush \
60	tst-mmap2-eofsync tst-mmap-offend bug-fopena+ bug-wfflush \
61	bug-ungetc2 bug-ftell bug-ungetc3 bug-ungetc4 tst-fopenloc2 \
62	tst-memstream1 tst-memstream2 tst-memstream3 tst-memstream4 \
63	tst-wmemstream1 tst-wmemstream2 tst-wmemstream3 tst-wmemstream4 \
64	tst-wmemstream5 bug-memstream1 bug-wmemstream1 \
65	tst-setvbuf1 tst-popen1 tst-fgetwc bug-wsetpos tst-fseek \
66	tst-fwrite-error tst-ftell-partial-wide tst-ftell-active-handler \
67	tst-ftell-append tst-fputws tst-bz22415 tst-fgetc-after-eof \
68	tst-sprintf-ub tst-sprintf-chk-ub tst-bz24051 tst-bz24153 \
69	tst-wfile-sync tst-bz28828
70
71tests-internal = tst-vtables tst-vtables-interposed
72
73ifeq (yes,$(build-shared))
74# Add test-fopenloc only if shared library is enabled since it depends on
75# shared localedata objects.
76tests += tst-fopenloc
77# Add tst-bz24228 only if shared library is enabled since it can never meet its
78# objective with static linking because the relevant code just is not there.
79tests += tst-bz24228
80endif
81test-srcs = test-freopen
82
83ifeq ($(build-shared),yes)
84routines += oldiofopen oldiofdopen oldiofclose oldiopopen oldpclose \
85	    oldtmpfile oldiofgetpos oldiofgetpos64 oldiofsetpos	    \
86	    oldiofsetpos64
87endif
88
89ifeq (yes,$(libc-reentrant))
90routines += clearerr_u feof_u ferror_u fputc_u getc_u getchar_u		      \
91	    iofflush_u putc_u putchar_u peekc iofread_u iofwrite_u iofgets_u  \
92	    iofputs_u
93endif
94
95CPPFLAGS += $(libio-mtsafe)
96
97# Support for exception handling.
98CFLAGS-fileops.c += -fexceptions
99CFLAGS-fputc.c += -fexceptions
100CFLAGS-fputwc.c += -fexceptions
101CFLAGS-freopen64.c += -fexceptions
102CFLAGS-freopen.c += -fexceptions
103CFLAGS-fseek.c += -fexceptions
104CFLAGS-fseeko64.c += -fexceptions
105CFLAGS-fseeko.c += -fexceptions
106CFLAGS-ftello64.c += -fexceptions
107CFLAGS-ftello.c += -fexceptions
108CFLAGS-fwide.c += -fexceptions
109CFLAGS-genops.c += -fexceptions
110CFLAGS-getc.c += -fexceptions
111CFLAGS-getchar.c += -fexceptions
112CFLAGS-getwc.c += -fexceptions
113CFLAGS-getwchar.c += -fexceptions
114CFLAGS-iofclose.c += -fexceptions
115CFLAGS-iofflush.c += -fexceptions
116CFLAGS-iofgetpos64.c += -fexceptions
117CFLAGS-iofgetpos.c += -fexceptions
118CFLAGS-iofgets.c += -fexceptions
119CFLAGS-iofgetws.c += -fexceptions
120CFLAGS-iofputs.c += -fexceptions
121CFLAGS-iofputws.c += -fexceptions
122CFLAGS-iofread.c += -fexceptions
123CFLAGS-iofsetpos64.c += -fexceptions
124CFLAGS-iofsetpos.c += -fexceptions
125CFLAGS-ioftell.c += -fexceptions
126CFLAGS-iofwrite.c += -fexceptions
127CFLAGS-iogetdelim.c += -fexceptions
128CFLAGS-iogetline.c += -fexceptions
129CFLAGS-iogets.c += -fexceptions
130CFLAGS-iogetwline.c += -fexceptions
131CFLAGS-ioputs.c += -fexceptions
132CFLAGS-ioseekoff.c += -fexceptions
133CFLAGS-ioseekpos.c += -fexceptions
134CFLAGS-iosetbuffer.c += -fexceptions
135CFLAGS-iosetvbuf.c += -fexceptions
136CFLAGS-ioungetc.c += -fexceptions
137CFLAGS-ioungetwc.c += -fexceptions
138CFLAGS-oldfileops.c += -fexceptions
139CFLAGS-oldiofclose.c += -fexceptions
140CFLAGS-oldiofgetpos64.c += -fexceptions
141CFLAGS-oldiofgetpos.c += -fexceptions
142CFLAGS-oldiofsetpos64.c += -fexceptions
143CFLAGS-oldiofsetpos.c += -fexceptions
144CFLAGS-peekc.c += -fexceptions
145CFLAGS-putc.c += -fexceptions
146CFLAGS-putchar.c += -fexceptions
147CFLAGS-putwc.c += -fexceptions
148CFLAGS-putwchar.c += -fexceptions
149CFLAGS-rewind.c += -fexceptions
150CFLAGS-wfileops.c += -fexceptions
151CFLAGS-wgenops.c += -fexceptions
152CFLAGS-oldiofopen.c += -fexceptions
153CFLAGS-iofopen.c += -fexceptions
154CFLAGS-iofopen64.c += -fexceptions
155CFLAGS-oldtmpfile.c += -fexceptions
156# XXX Do we need filedoalloc and wfiledoalloc?  Others?
157
158CFLAGS-tst_putwc.c += -DOBJPFX=\"$(objpfx)\"
159
160# These test cases intentionally use overlapping arguments
161CFLAGS-tst-sprintf-ub.c += -Wno-restrict
162CFLAGS-tst-sprintf-chk-ub.c += -Wno-restrict
163
164LDFLAGS-tst-bz24228 = -Wl,--version-script=tst-bz24228.map
165
166tst_wprintf2-ARGS = "Some Text"
167
168test-fmemopen-ENV = MALLOC_TRACE=$(objpfx)test-fmemopen.mtrace \
169		    LD_PRELOAD=$(common-objpfx)/malloc/libc_malloc_debug.so
170tst-fopenloc-ENV = MALLOC_TRACE=$(objpfx)tst-fopenloc.mtrace \
171		   LD_PRELOAD=$(common-objpfx)/malloc/libc_malloc_debug.so
172tst-bz22415-ENV = MALLOC_TRACE=$(objpfx)tst-bz22415.mtrace \
173		  LD_PRELOAD=$(common-objpfx)/malloc/libc_malloc_debug.so
174tst-bz24228-ENV = MALLOC_TRACE=$(objpfx)tst-bz24228.mtrace \
175		  LD_PRELOAD=$(common-objpfx)/malloc/libc_malloc_debug.so
176
177generated += test-fmemopen.mtrace test-fmemopen.check
178generated += tst-fopenloc.mtrace tst-fopenloc.check
179generated += tst-bz22415.mtrace tst-bz22415.check
180
181aux	:= fileops genops stdfiles stdio strops
182
183ifeq ($(build-shared),yes)
184generated += tst-bz24228.mtrace tst-bz24228.check
185aux	+= oldfileops oldstdfiles
186endif
187
188shared-only-routines = oldiofopen oldiofdopen oldiofclose oldfileops	\
189		       oldstdfiles oldiopopen oldpclose oldtmpfile	\
190		       oldiofgetpos oldiofgetpos64 oldiofsetpos		\
191		       oldiofsetpos64
192
193ifeq ($(run-built-tests),yes)
194tests-special += $(objpfx)test-freopen.out $(objpfx)test-fmemopen-mem.out \
195		 $(objpfx)tst-bz22415-mem.out
196ifeq (yes,$(build-shared))
197# Run tst-fopenloc-cmp.out and tst-openloc-mem.out only if shared
198# library is enabled since they depend on tst-fopenloc.out.
199tests-special += $(objpfx)tst-fopenloc-cmp.out $(objpfx)tst-fopenloc-mem.out \
200		 $(objpfx)tst-bz24228-mem.out
201endif
202
203tests += tst-cleanup-default tst-cleanup-default-static
204tests-static += tst-cleanup-default-static
205tests-special += $(objpfx)tst-cleanup-default-cmp.out $(objpfx)tst-cleanup-default-static-cmp.out
206LDFLAGS-tst-cleanup-default = -Wl,--gc-sections
207LDFLAGS-tst-cleanup-default-static = -Wl,--gc-sections
208
209ifeq ($(have-gnu-retain)$(have-z-start-stop-gc),yesyes)
210tests += tst-cleanup-start-stop-gc tst-cleanup-start-stop-gc-static \
211		tst-cleanup-nostart-stop-gc tst-cleanup-nostart-stop-gc-static
212tests-static += tst-cleanup-start-stop-gc-static tst-cleanup-nostart-stop-gc-static
213tests-special += $(objpfx)tst-cleanup-start-stop-gc-cmp.out \
214		$(objpfx)tst-cleanup-start-stop-gc-static-cmp.out \
215		$(objpfx)tst-cleanup-nostart-stop-gc-cmp.out \
216		$(objpfx)tst-cleanup-nostart-stop-gc-static-cmp.out
217LDFLAGS-tst-cleanup-start-stop-gc := -Wl,--gc-sections,-z,start-stop-gc
218LDFLAGS-tst-cleanup-start-stop-gc-static := -Wl,--gc-sections,-z,start-stop-gc
219LDFLAGS-tst-cleanup-nostart-stop-gc := -Wl,--gc-sections,-z,nostart-stop-gc
220LDFLAGS-tst-cleanup-nostart-stop-gc-static := -Wl,--gc-sections,-z,nostart-stop-gc
221endif
222endif
223
224include ../Rules
225
226ifeq ($(run-built-tests),yes)
227LOCALES := de_DE.ISO-8859-1 de_DE.UTF-8 en_US.ISO-8859-1 en_US.UTF-8 \
228	   ja_JP.EUC-JP ja_JP.UTF-8
229include ../gen-locales.mk
230
231$(objpfx)bug-ftell.out: $(gen-locales)
232$(objpfx)bug-ungetwc1.out: $(gen-locales)
233$(objpfx)bug-ungetwc2.out: $(gen-locales)
234$(objpfx)tst-fgetwc.out: $(gen-locales)
235$(objpfx)tst-fgetws.out: $(gen-locales)
236$(objpfx)tst-fopenloc.out: $(gen-locales)
237$(objpfx)tst-fputws.out: $(gen-locales)
238$(objpfx)tst-fseek.out: $(gen-locales)
239$(objpfx)tst-ftell-active-handler.out: $(gen-locales)
240$(objpfx)tst-ftell-append.out: $(gen-locales)
241$(objpfx)tst-ftell-partial-wide.out: $(gen-locales)
242$(objpfx)tst-setvbuf1.out: $(gen-locales)
243$(objpfx)tst-swscanf.out: $(gen-locales)
244$(objpfx)tst-ungetwc1.out: $(gen-locales)
245$(objpfx)tst-ungetwc2.out: $(gen-locales)
246$(objpfx)tst-widetext.out: $(gen-locales)
247$(objpfx)tst_wprintf2.out: $(gen-locales)
248$(objpfx)tst-wfile-sync.out: $(gen-locales)
249endif
250
251define gen-tst-cleanup
252$(objpfx)tst-cleanup-$1-cmp.out: tst-cleanup.exp $(objpfx)tst-cleanup-$1.out
253	cmp $$^ > $$@; $$(evaluate-test)
254endef
255
256$(foreach t,default default-static start-stop-gc start-stop-gc-static nostart-stop-gc nostart-stop-gc-static, \
257  $(eval $(call gen-tst-cleanup,$(t))))
258
259$(objpfx)test-freopen.out: test-freopen.sh $(objpfx)test-freopen
260	$(SHELL) $< $(common-objpfx) '$(test-program-prefix)'	\
261	$(common-objpfx)libio/; \
262	$(evaluate-test)
263
264$(objpfx)tst-fopenloc-cmp.out: ../iconvdata/testdata/ISO-8859-1..UTF8 \
265			       $(objpfx)tst-fopenloc.out
266	cmp $^ > $@; \
267	$(evaluate-test)
268
269$(objpfx)test-fmemopen-mem.out: $(objpfx)test-fmemopen.out
270	$(common-objpfx)malloc/mtrace $(objpfx)test-fmemopen.mtrace > $@; \
271	$(evaluate-test)
272
273$(objpfx)tst-fopenloc-mem.out: $(objpfx)tst-fopenloc.out
274	$(common-objpfx)malloc/mtrace $(objpfx)tst-fopenloc.mtrace > $@; \
275	$(evaluate-test)
276
277$(objpfx)tst-bz22415-mem.out: $(objpfx)tst-bz22415.out
278	$(common-objpfx)malloc/mtrace $(objpfx)tst-bz22415.mtrace > $@; \
279	$(evaluate-test)
280
281$(objpfx)tst-bz24228-mem.out: $(objpfx)tst-bz24228.out
282	$(common-objpfx)malloc/mtrace $(objpfx)tst-bz24228.mtrace > $@; \
283	$(evaluate-test)
284