1# Copyright (C) 1991-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#	Rules for making a subdirectory in the GNU C library.
20#	Each subdirectory Makefile defines some variables and includes this.
21#
22ifneq (,)
23This makefile requires GNU Make.
24endif
25
26all: # Don't let the default goal come from Makeconfig.
27
28include $(firstword $(..) ../)Makeconfig
29
30ifndef	subdir
31Each subdirectory makefile must define the `subdir' variable.
32endif
33# This ` unconfuses emacs.
34# This is benign and useless in GNU make before 3.63.
35export subdir := $(subdir)
36
37# This is the default target; it makes the library and auxiliary programs.
38.PHONY: all
39all: objs lib others
40
41ifeq ($(build-programs),yes)
42others: $(addprefix $(objpfx),$(extra-objs) \
43			      $(install-lib) \
44			      $(install-bin) $(install-bin-script) \
45			      $(install-rootsbin) $(install-sbin))
46else
47others: $(addprefix $(objpfx),$(extra-objs) \
48			      $(install-lib))
49endif
50
51ifneq	"$(findstring env,$(origin headers))" ""
52headers :=
53endif
54
55ifneq	"$(findstring env,$(origin generated))" ""
56generated :=
57endif
58
59ifneq	"$(findstring env,$(origin common-generated))" ""
60common-generated :=
61endif
62
63# See below.  This must be set before Makerules processes it.
64before-compile += $(common-objpfx)bits/stdio_lim.h
65
66include $(..)Makerules
67
68.PHONY: subdir_lib
69subdir_lib: lib-noranlib
70
71# Some subdirs need to install a dummy library.
72# They can use "$(objpfx)libfnord.a: $(dep-dummy-lib); $(make-dummy-lib)".
73dep-dummy-lib = $(common-objpfx)dummy.o
74define make-dummy-lib
75$(AR) cr$(verbose) $@ $<
76endef
77
78$(common-objpfx)dummy.c:
79	rm -f $@
80	(echo 'extern void __dummy__ (void);'; \
81	 echo 'void __dummy__ (void) { }') > $@
82common-generated += dummy.o dummy.c
83
84ifneq "$(headers)" ""
85# Test that all of the headers installed by this directory can be compiled
86# in isolation.
87tests-special += $(objpfx)check-installed-headers-c.out
88libof-check-installed-headers-c := testsuite
89$(objpfx)check-installed-headers-c.out: \
90    $(..)scripts/check-installed-headers.sh $(headers)
91	$(SHELL) $(..)scripts/check-installed-headers.sh c \
92	  "$(CC) $(filter-out -std=%,$(CFLAGS)) -D_ISOMAC $(+includes)" \
93	  $(headers) > $@; \
94	$(evaluate-test)
95
96ifneq "$(CXX)" ""
97# If a C++ compiler is available, also test that they can be compiled
98# in isolation as C++.
99tests-special += $(objpfx)check-installed-headers-cxx.out
100libof-check-installed-headers-cxx := testsuite
101$(objpfx)check-installed-headers-cxx.out: \
102    $(..)scripts/check-installed-headers.sh $(headers)
103	$(SHELL) $(..)scripts/check-installed-headers.sh c++ \
104	  "$(CXX) $(filter-out -std=%,$(CXXFLAGS)) -D_ISOMAC $(+includes)" \
105	  $(headers) > $@; \
106	$(evaluate-test)
107endif # $(CXX)
108
109# Test that a wrapper header exists in include/ for each non-sysdeps header.
110# This script does not need $(py-env).
111tests-special += $(objpfx)check-wrapper-headers.out
112$(objpfx)check-wrapper-headers.out: \
113  $(..)scripts/check-wrapper-headers.py $(headers)
114	$(PYTHON) $< --root=$(..) --subdir=$(subdir) $(headers) > $@; \
115	  $(evaluate-test)
116
117# Test that none of the headers installed by this directory use certain
118# obsolete constructs (e.g. legacy BSD typedefs superseded by stdint.h).
119# This script does not need $(py-env).
120tests-special += $(objpfx)check-obsolete-constructs.out
121libof-check-obsolete-constructs := testsuite
122$(objpfx)check-obsolete-constructs.out: \
123    $(..)scripts/check-obsolete-constructs.py $(headers)
124	$(PYTHON) $^ > $@ 2>&1; \
125	$(evaluate-test)
126
127endif # $(headers)
128
129# This makes all the auxiliary and test programs.
130
131.PHONY: others tests bench bench-build
132
133# Test programs for the pretty printers.
134tests-printers-programs := $(addprefix $(objpfx),$(tests-printers))
135
136# .out files with the output of running the pretty printer tests.
137tests-printers-out := $(patsubst %,$(objpfx)%.out,$(tests-printers))
138
139ifeq ($(build-programs),yes)
140others: $(addprefix $(objpfx),$(others) $(sysdep-others) $(extra-objs))
141else
142others: $(addprefix $(objpfx),$(extra-objs))
143endif
144
145# Generate constant files for Python pretty printers if required.
146others: $(py-const)
147
148ifeq ($(run-built-tests),no)
149tests: $(addprefix $(objpfx),$(filter-out $(tests-unsupported), \
150                                          $(tests) $(tests-internal) \
151					  $(tests-container)) \
152			     $(test-srcs)) $(tests-special) \
153			     $(tests-printers-programs)
154xtests: tests $(xtests-special)
155else
156tests: $(tests:%=$(objpfx)%.out) $(tests-internal:%=$(objpfx)%.out) \
157       $(tests-container:%=$(objpfx)%.out) \
158       $(tests-mcheck:%=$(objpfx)%-mcheck.out) \
159       $(tests-malloc-check:%=$(objpfx)%-malloc-check.out) \
160       $(tests-malloc-hugetlb1:%=$(objpfx)%-malloc-hugetlb1.out) \
161       $(tests-malloc-hugetlb2:%=$(objpfx)%-malloc-hugetlb2.out) \
162       $(tests-special) $(tests-printers-out)
163xtests: tests $(xtests:%=$(objpfx)%.out) $(xtests-special)
164endif
165
166tests-special-notdir = $(patsubst $(objpfx)%, %, $(tests-special))
167xtests-special-notdir = $(patsubst $(objpfx)%, %, $(xtests-special))
168ifeq ($(run-built-tests),no)
169tests-expected =
170else
171tests-expected = $(tests) $(tests-internal) $(tests-printers) \
172	$(tests-container) $(tests-malloc-check:%=%-malloc-check) \
173	$(tests-malloc-hugetlb1:%=%-malloc-hugetlb1) \
174	$(tests-malloc-hugetlb2:%=%-malloc-hugetlb2) \
175	$(tests-mcheck:%=%-mcheck)
176endif
177tests:
178	$(..)scripts/merge-test-results.sh -s $(objpfx) $(subdir) \
179	  $(sort $(tests-expected) $(tests-special-notdir:.out=)) \
180	  > $(objpfx)subdir-tests.sum
181xtests:
182	$(..)scripts/merge-test-results.sh -s $(objpfx) $(subdir) \
183	  $(sort $(xtests) $(xtests-special-notdir:.out=)) \
184	  > $(objpfx)subdir-xtests.sum
185
186ifeq ($(build-programs),yes)
187binaries-all-notests = $(others) $(sysdep-others)
188binaries-all-tests = $(tests) $(tests-internal) $(xtests) $(test-srcs) \
189		     $(tests-container)
190binaries-all = $(binaries-all-notests) $(binaries-all-tests)
191binaries-static-notests = $(others-static)
192binaries-static-tests = $(tests-static) $(xtests-static)
193binaries-static = $(binaries-static-notests) $(binaries-static-tests)
194ifeq (yesyes,$(have-fpie)$(build-shared))
195binaries-pie-tests = $(tests-pie) $(xtests-pie)
196binaries-pie-notests = $(others-pie)
197else
198binaries-pie-tests =
199binaries-pie-notests =
200endif
201binaries-mcheck-tests = $(tests-mcheck:%=%-mcheck)
202binaries-malloc-check-tests = $(tests-malloc-check:%=%-malloc-check)
203binaries-malloc-hugetlb1-tests = $(tests-malloc-hugetlb1:%=%-malloc-hugetlb1)
204binaries-malloc-hugetlb2-tests = $(tests-malloc-hugetlb2:%=%-malloc-hugetlb2)
205else
206binaries-all-notests =
207binaries-all-tests = $(tests) $(tests-internal) $(xtests) $(test-srcs)
208binaries-all = $(binaries-all-tests)
209binaries-static-notests =
210binaries-static-tests =
211binaries-static =
212binaries-pie-tests =
213binaries-pie-notests =
214binaries-mcheck-tests =
215binaries-malloc-check-tests =
216binaries-malloc-hugetlb1-tests =
217binaries-malloc-hugetlb2-tests =
218endif
219
220binaries-pie = $(binaries-pie-tests) $(binaries-pie-notests)
221binaries-shared-tests = $(filter-out $(binaries-pie) $(binaries-static), \
222				     $(binaries-all-tests))
223binaries-shared-notests = $(filter-out $(binaries-pie) $(binaries-static), \
224				       $(binaries-all-notests))
225
226ifneq "$(strip $(binaries-shared-notests))" ""
227$(addprefix $(objpfx),$(binaries-shared-notests)): %: %.o \
228  $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
229  $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
230	$(+link)
231endif
232
233ifneq "$(strip $(binaries-shared-tests))" ""
234$(addprefix $(objpfx),$(binaries-shared-tests)): %: %.o \
235  $(link-extra-libs-tests) \
236  $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
237  $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
238	$(+link-tests)
239endif
240
241ifneq "$(strip $(binaries-mcheck-tests))" ""
242$(addprefix $(objpfx),$(binaries-mcheck-tests)): %-mcheck: %.o \
243  $(link-extra-libs-tests) \
244  $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
245  $(common-objpfx)malloc/libmcheck.a \
246  $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
247	$(+link-tests)
248endif
249
250ifneq "$(strip $(binaries-malloc-check-tests))" ""
251$(addprefix $(objpfx),$(binaries-malloc-check-tests)): %-malloc-check: %.o \
252  $(link-extra-libs-tests) \
253  $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
254  $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
255	$(+link-tests)
256endif
257
258ifneq "$(strip $(binaries-malloc-hugetlb1-tests))" ""
259$(addprefix $(objpfx),$(binaries-malloc-hugetlb1-tests)): %-malloc-hugetlb1: %.o \
260  $(link-extra-libs-tests) \
261  $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
262  $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
263	$(+link-tests)
264endif
265
266ifneq "$(strip $(binaries-malloc-hugetlb2-tests))" ""
267$(addprefix $(objpfx),$(binaries-malloc-hugetlb2-tests)): %-malloc-hugetlb2: %.o \
268  $(link-extra-libs-tests) \
269  $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
270  $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
271	$(+link-tests)
272endif
273
274ifneq "$(strip $(binaries-pie-tests))" ""
275$(addprefix $(objpfx),$(binaries-pie-tests)): %: %.o \
276  $(link-extra-libs-tests) \
277  $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
278  $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
279	$(+link-pie-tests)
280endif
281
282ifneq "$(strip $(binaries-pie-notests))" ""
283$(addprefix $(objpfx),$(binaries-pie-notests)): %: %.o \
284  $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
285  $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
286	$(+link-pie)
287endif
288
289ifneq "$(strip $(binaries-static-notests))" ""
290$(addprefix $(objpfx),$(binaries-static-notests)): %: %.o \
291  $(sort $(filter $(common-objpfx)lib%,$(link-libc-static))) \
292  $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
293	$(+link-static)
294endif
295
296ifneq "$(strip $(binaries-static-tests))" ""
297$(addprefix $(objpfx),$(binaries-static-tests)): %: %.o \
298  $(link-extra-libs-tests) \
299  $(sort $(filter $(common-objpfx)lib%,$(link-libc-static-tests))) \
300  $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
301	$(+link-static-tests)
302endif
303
304# All malloc-check tests will be run with MALLOC_CHECK_=3
305define malloc-check-ENVS
306$(1)-malloc-check-ENV = MALLOC_CHECK_=3 \
307			LD_PRELOAD=$(common-objpfx)/malloc/libc_malloc_debug.so
308endef
309$(foreach t,$(tests-malloc-check),$(eval $(call malloc-check-ENVS,$(t))))
310
311# All malloc-hugetlb1 tests will be run with GLIBC_TUNABLES=glibc.malloc.hugetlb=1
312define malloc-hugetlb1-ENVS
313$(1)-malloc-hugetlb1-ENV += GLIBC_TUNABLES=glibc.malloc.hugetlb=1
314endef
315$(foreach t,$(tests-malloc-hugetlb1),$(eval $(call malloc-hugetlb1-ENVS,$(t))))
316
317# All malloc-hugetlb2 tests will be run with GLIBC_TUNABLE=glibc.malloc.hugetlb=2
318define malloc-hugetlb2-ENVS
319$(1)-malloc-hugetlb2-ENV += GLIBC_TUNABLES=glibc.malloc.hugetlb=2
320endef
321$(foreach t,$(tests-malloc-hugetlb2),$(eval $(call malloc-hugetlb2-ENVS,$(t))))
322
323# mcheck tests need the debug DSO to support -lmcheck.
324define mcheck-ENVS
325$(1)-mcheck-ENV = LD_PRELOAD=$(common-objpfx)/malloc/libc_malloc_debug.so
326endef
327$(foreach t,$(tests-mcheck),$(eval $(call mcheck-ENVS,$(t))))
328
329ifneq "$(strip $(tests) $(tests-internal) $(xtests) $(test-srcs))" ""
330# These are the implicit rules for making test outputs
331# from the test programs and whatever input files are present.
332
333define make-test-out
334$(if $($*-ENV-only),$(test-wrapper-env-only) $($*-ENV-only),\
335     $(test-wrapper-env) $(run-program-env) $($*-ENV)) \
336$(host-test-program-cmd) $($*-ARGS)
337endef
338$(objpfx)%.out: %.input $(objpfx)%
339	$(make-test-out) > $@ < $(word 1,$^); \
340	$(evaluate-test)
341$(objpfx)%.out: /dev/null $(objpfx)%	# Make it 2nd arg for canned sequence.
342	$(make-test-out) > $@; \
343	$(evaluate-test)
344
345
346# Any tests that require an isolated container (filesystem, network
347# and pid namespaces) in which to run, should be added to
348# tests-container.
349$(tests-container:%=$(objpfx)%.out): $(objpfx)%.out : $(if $(wildcard $(objpfx)%.files),$(objpfx)%.files,/dev/null) $(objpfx)%
350	$(test-wrapper-env) $(run-program-env) $(run-via-rtld-prefix) \
351	  $(common-objpfx)support/test-container env $(run-program-env) $($*-ENV) \
352	  $(host-test-program-cmd) $($*-ARGS) > $@; \
353	$(evaluate-test)
354
355
356# tests-unsupported lists tests that we will not try to build at all in
357# this configuration.  Note this runs every time because it does not
358# actually create its target.  The dependency on Makefile is meant to
359# ensure that it runs after a Makefile change to add a test to the list
360# when it previously ran and produced a .out file (probably for a failure).
361ifneq "$(strip $(tests-unsupported))" ""
362$(tests-unsupported:%=$(objpfx)%.out): $(objpfx)%.out: Makefile
363	@rm -f $@
364	$(..)scripts/evaluate-test.sh $(patsubst $(common-objpfx)%.out,%,$@) \
365				      77 false false > $(@:.out=.test-result)
366endif
367
368endif	# tests
369
370ifneq "$(strip $(tests-printers))" ""
371
372# Static pattern rule for building the test programs for the pretty printers.
373$(tests-printers-programs): %: %.o $(tests-printers-libs) \
374  $(sort $(filter $(common-objpfx)lib%,$(link-libc-static-tests))) \
375  $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
376	$(+link-printers-tests)
377
378# Add the paths to the generated constants file and test_common_printers.py
379# to PYTHONPATH so the test scripts can find them.
380py-env := PYTHONPATH=$(py-const-dir):$(..)scripts:$${PYTHONPATH}
381
382# Static pattern rule that matches the test-* targets to their .c and .py
383# prerequisites.  It'll run the corresponding test script for each test program
384# we compiled and place its output in the corresponding .out file.
385# The pretty printer files and test_common_printers.py must be present for all.
386$(tests-printers-out): $(objpfx)%.out: $(objpfx)% %.py %.c $(pretty-printers) \
387		       $(..)scripts/test_printers_common.py
388	$(test-wrapper-env) $(py-env) \
389	    $(PYTHON) $*.py $*.c $(objpfx)$* $(pretty-printers) > $@; \
390	$(evaluate-test)
391endif
392
393
394.PHONY: distclean realclean subdir_distclean subdir_realclean \
395	subdir_clean subdir_mostlyclean subdir_testclean
396subdir_mostlyclean: mostlyclean
397subdir_clean: clean
398subdir_distclean: distclean
399subdir_realclean: realclean
400subdir_testclean: do-tests-clean
401realclean: distclean
402distclean: clean
403
404# We want to install everything except the library itself, but update all
405# our portions of the library because the parent make will install it later
406# (likewise the stubs file).
407.PHONY: subdir_install
408subdir_install: install-no-libc.a lib-noranlib stubs
409
410.PHONY: subdir_objs subdir_stubs
411subdir_objs: objs
412subdir_stubs: stubs
413
414# Target required by the Hurd to ensure that all the MiG-generated
415# headers are in place before building a subdirectory.
416.PHONY: before-compile
417before-compile: $(before-compile)
418
419$(common-objpfx)dummy.o: $(common-objpfx)dummy.c $(before-compile);
420	$(compile-command.c)
421
422# Local Variables:
423# mode: makefile
424# End:
425