1 # Copyright (C) 1991-2025 Free Software Foundation, Inc.
2 # This file is part of the GNU C Library.
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.
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.
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/>.
19 # Rules for making a subdirectory in the GNU C library.
20 # Each subdirectory Makefile defines some variables and includes this.
23 This makefile requires GNU Make.
26 all: # Don't let the default goal come from Makeconfig.
28 include $(firstword $(..) ../)Makeconfig
31 Each subdirectory makefile must define the `subdir' variable.
33 # This ` unconfuses emacs.
34 # This is benign and useless in GNU make before 3.63.
35 export subdir := $(subdir)
37 # This is the default target; it makes the library and auxiliary programs.
41 ifeq ($(build-programs),yes)
42 others: $(addprefix $(objpfx),$(extra-objs) \
44 $(install-bin) $(install-bin-script) \
45 $(install-rootsbin) $(install-sbin))
47 others: $(addprefix $(objpfx),$(extra-objs) \
51 ifneq "$(findstring env,$(origin headers))" ""
55 ifneq "$(findstring env,$(origin generated))" ""
59 ifneq "$(findstring env,$(origin common-generated))" ""
63 include $(..)Makerules
66 subdir_lib: lib-noranlib
68 # Some subdirs need to install a dummy library.
69 # They can use "$(objpfx)libfnord.a: $(dep-dummy-lib); $(make-dummy-lib)".
70 dep-dummy-lib = $(common-objpfx)dummy.o
72 $(AR) cr$(verbose) $@ $<
75 $(common-objpfx)dummy.c:
77 (echo 'extern void __dummy__ (void);'; \
78 echo 'void __dummy__ (void) { }') > $@
79 common-generated += dummy.o dummy.c
82 # Test that all of the headers installed by this directory can be compiled
84 tests-special += $(objpfx)check-installed-headers-c.out
85 libof-check-installed-headers-c := testsuite
86 $(objpfx)check-installed-headers-c.out: \
87 $(..)scripts/check-installed-headers.sh $(headers)
88 $(SHELL) $(..)scripts/check-installed-headers.sh c $(supported-fortify) \
89 "$(CC) $(test-config-cflags-finput-charset-ascii) \
90 $(filter-out -std=%,$(CFLAGS)) -D_ISOMAC $(+includes)" \
95 # If a C++ compiler is available, also test that they can be compiled
96 # in isolation as C++.
97 tests-special += $(objpfx)check-installed-headers-cxx.out
98 libof-check-installed-headers-cxx := testsuite
99 $(objpfx)check-installed-headers-cxx.out: \
100 $(..)scripts/check-installed-headers.sh $(headers)
101 $(SHELL) $(..)scripts/check-installed-headers.sh c++ $(supported-fortify) \
102 "$(CXX) $(test-config-cxxflags-finput-charset-ascii) \
103 $(filter-out -std=%,$(CXXFLAGS)) -D_ISOMAC $(+includes)" \
108 # Test that a wrapper header exists in include/ for each non-sysdeps header.
109 # This script does not need $(py-env).
110 tests-special += $(objpfx)check-wrapper-headers.out
111 $(objpfx)check-wrapper-headers.out: \
112 $(..)scripts/check-wrapper-headers.py $(headers)
113 $(PYTHON) $< --root=$(..) --subdir=$(subdir) $(headers) > $@; \
116 # Test that none of the headers installed by this directory use certain
117 # obsolete constructs (e.g. legacy BSD typedefs superseded by stdint.h).
118 # This script does not need $(py-env).
119 tests-special += $(objpfx)check-obsolete-constructs.out
120 libof-check-obsolete-constructs := testsuite
121 $(objpfx)check-obsolete-constructs.out: \
122 $(..)scripts/check-obsolete-constructs.py $(headers)
123 $(PYTHON) $^ > $@ 2>&1; \
128 # This makes all the auxiliary and test programs.
130 .PHONY: others tests bench bench-build
132 # Test programs for the pretty printers.
133 tests-printers-programs := $(addprefix $(objpfx),$(tests-printers))
135 # .out files with the output of running the pretty printer tests.
136 tests-printers-out := $(patsubst %,$(objpfx)%.out,$(tests-printers))
138 ifeq ($(build-programs),yes)
139 others: $(addprefix $(objpfx),$(others) $(sysdep-others) $(extra-objs))
141 others: $(addprefix $(objpfx),$(extra-objs))
144 # Generate constant files for Python pretty printers if required.
147 ifeq ($(run-built-tests),no)
148 # The $(xtests) dependency ensures that xtests are always built.
149 tests: $(addprefix $(objpfx),$(filter-out $(tests-unsupported), \
150 $(tests) $(tests-internal) $(xtests) \
152 $(tests-mcheck:%=%-mcheck) \
153 $(tests-malloc-check:%=%-malloc-check) \
154 $(tests-malloc-hugetlb1:%=%-malloc-hugetlb1) \
155 $(tests-malloc-hugetlb2:%=%-malloc-hugetlb2)) \
156 $(test-srcs)) $(tests-special) \
157 $(tests-printers-programs)
158 xtests: tests $(xtests-special)
159 else # $(run-built-tests) != no
160 # The $(xtests) dependency ensures that xtests are always built.
161 tests: $(tests:%=$(objpfx)%.out) $(tests-internal:%=$(objpfx)%.out) \
162 $(addprefix $(objpfx),$(filter-out $(tests-unsupported), $(xtests))) \
163 $(tests-container:%=$(objpfx)%.out) \
164 $(tests-mcheck:%=$(objpfx)%-mcheck.out) \
165 $(tests-malloc-check:%=$(objpfx)%-malloc-check.out) \
166 $(tests-malloc-hugetlb1:%=$(objpfx)%-malloc-hugetlb1.out) \
167 $(tests-malloc-hugetlb2:%=$(objpfx)%-malloc-hugetlb2.out) \
168 $(tests-special) $(tests-printers-out)
169 xtests: tests $(xtests:%=$(objpfx)%.out) $(xtests-special)
170 endif # $(run-built-tests) != no
172 tests-special-notdir = $(patsubst $(objpfx)%, %, $(tests-special))
173 xtests-special-notdir = $(patsubst $(objpfx)%, %, $(xtests-special))
174 ifeq ($(run-built-tests),no)
177 else # $(run-built-tests) != no
178 tests-expected = $(tests) $(tests-internal) $(tests-printers) \
179 $(tests-container) $(tests-malloc-check:%=%-malloc-check) \
180 $(tests-malloc-hugetlb1:%=%-malloc-hugetlb1) \
181 $(tests-malloc-hugetlb2:%=%-malloc-hugetlb2) \
182 $(tests-mcheck:%=%-mcheck)
183 xtests-expected = $(xtests)
184 endif # $(run-built-tests) != no
186 $(..)scripts/merge-test-results.sh -s $(objpfx) $(subdir) \
187 $(sort $(tests-expected) $(tests-special-notdir:.out=)) \
188 > $(objpfx)subdir-tests.sum
190 $(..)scripts/merge-test-results.sh -s $(objpfx) $(subdir) \
191 $(sort $(xtests-expected) $(xtests-special-notdir:.out=)) \
192 > $(objpfx)subdir-xtests.sum
194 ifeq ($(build-programs),yes)
195 binaries-all-notests = $(others) $(sysdep-others)
196 binaries-all-tests = $(tests) $(tests-internal) $(xtests) $(test-srcs) \
198 binaries-all = $(binaries-all-notests) $(binaries-all-tests)
199 binaries-static-notests = $(others-static)
200 binaries-static-tests = $(tests-static) $(xtests-static)
201 binaries-static = $(binaries-static-notests) $(binaries-static-tests)
202 binaries-shared-2.0-tests = $(tests-2.0)
203 ifeq (yesyes,$(have-fpie)$(build-shared))
204 binaries-pie-tests = $(tests-pie) $(xtests-pie)
205 binaries-pie-notests = $(others-pie)
208 binaries-pie-notests =
210 binaries-mcheck-tests = $(tests-mcheck:%=%-mcheck)
211 binaries-malloc-check-tests = $(tests-malloc-check:%=%-malloc-check)
212 binaries-malloc-hugetlb1-tests = $(tests-malloc-hugetlb1:%=%-malloc-hugetlb1)
213 binaries-malloc-hugetlb2-tests = $(tests-malloc-hugetlb2:%=%-malloc-hugetlb2)
215 binaries-all-notests =
216 binaries-all-tests = $(tests) $(tests-internal) $(xtests) $(test-srcs)
217 binaries-all = $(binaries-all-tests)
218 binaries-static-notests =
219 binaries-static-tests =
222 binaries-pie-notests =
223 binaries-mcheck-tests =
224 binaries-malloc-check-tests =
225 binaries-malloc-hugetlb1-tests =
226 binaries-malloc-hugetlb2-tests =
229 binaries-pie = $(binaries-pie-tests) $(binaries-pie-notests)
230 binaries-shared-tests = $(filter-out $(binaries-pie) $(binaries-static) \
231 $(binaries-shared-2.0-tests), \
232 $(binaries-all-tests))
233 binaries-shared-notests = $(filter-out $(binaries-pie) $(binaries-static), \
234 $(binaries-all-notests))
236 ifneq "$(strip $(binaries-shared-notests))" ""
237 $(addprefix $(objpfx),$(binaries-shared-notests)): %: %.o \
238 $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
239 $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
243 ifneq "$(strip $(binaries-shared-tests))" ""
244 $(addprefix $(objpfx),$(binaries-shared-tests)): %: %.o \
245 $(link-extra-libs-tests) \
246 $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
247 $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
251 # Linking test programs with crt1.o from glibc 2.0.
252 ifneq "$(strip $(binaries-shared-2.0-tests))" ""
253 $(addprefix $(objpfx),$(binaries-shared-2.0-tests)): %: %.o \
254 $(link-extra-libs-tests) \
255 $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
256 $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
260 ifneq "$(strip $(binaries-mcheck-tests))" ""
261 $(addprefix $(objpfx),$(binaries-mcheck-tests)): %-mcheck: %.o \
262 $(link-extra-libs-tests) \
263 $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
264 $(common-objpfx)malloc/libmcheck.a \
265 $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
269 ifneq "$(strip $(binaries-malloc-check-tests))" ""
270 $(addprefix $(objpfx),$(binaries-malloc-check-tests)): %-malloc-check: %.o \
271 $(link-extra-libs-tests) \
272 $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
273 $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
277 ifneq "$(strip $(binaries-malloc-hugetlb1-tests))" ""
278 $(addprefix $(objpfx),$(binaries-malloc-hugetlb1-tests)): %-malloc-hugetlb1: %.o \
279 $(link-extra-libs-tests) \
280 $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
281 $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
285 ifneq "$(strip $(binaries-malloc-hugetlb2-tests))" ""
286 $(addprefix $(objpfx),$(binaries-malloc-hugetlb2-tests)): %-malloc-hugetlb2: %.o \
287 $(link-extra-libs-tests) \
288 $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
289 $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
293 ifneq "$(strip $(binaries-pie-tests))" ""
294 $(addprefix $(objpfx),$(binaries-pie-tests)): %: %.o \
295 $(link-extra-libs-tests) \
296 $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
297 $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
301 ifneq "$(strip $(binaries-pie-notests))" ""
302 $(addprefix $(objpfx),$(binaries-pie-notests)): %: %.o \
303 $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
304 $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
308 ifneq "$(strip $(binaries-static-notests))" ""
309 $(addprefix $(objpfx),$(binaries-static-notests)): %: %.o \
310 $(sort $(filter $(common-objpfx)lib%,$(link-libc-static))) \
311 $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
315 ifneq "$(strip $(binaries-static-tests))" ""
316 $(addprefix $(objpfx),$(binaries-static-tests)): %: %.o \
317 $(link-extra-libs-tests) \
318 $(sort $(filter $(common-objpfx)lib%,$(link-libc-static-tests))) \
319 $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
320 $(+link-static-tests)
323 # All malloc-check tests will be run with MALLOC_CHECK_=3
324 define malloc-check-ENVS
325 $(1)-malloc-check-ENV = MALLOC_CHECK_=3 \
326 LD_PRELOAD=$(common-objpfx)/malloc/libc_malloc_debug.so
328 $(foreach t,$(tests-malloc-check),$(eval $(call malloc-check-ENVS,$(t))))
330 # All malloc-hugetlb1 tests will be run with GLIBC_TUNABLES=glibc.malloc.hugetlb=1
331 define malloc-hugetlb1-ENVS
332 $(1)-malloc-hugetlb1-ENV += GLIBC_TUNABLES=glibc.malloc.hugetlb=1
334 $(foreach t,$(tests-malloc-hugetlb1),$(eval $(call malloc-hugetlb1-ENVS,$(t))))
336 # All malloc-hugetlb2 tests will be run with GLIBC_TUNABLE=glibc.malloc.hugetlb=2
337 define malloc-hugetlb2-ENVS
338 $(1)-malloc-hugetlb2-ENV += GLIBC_TUNABLES=glibc.malloc.hugetlb=2
340 $(foreach t,$(tests-malloc-hugetlb2),$(eval $(call malloc-hugetlb2-ENVS,$(t))))
342 # mcheck tests need the debug DSO to support -lmcheck.
344 $(1)-mcheck-ENV = LD_PRELOAD=$(common-objpfx)/malloc/libc_malloc_debug.so
346 $(foreach t,$(tests-mcheck),$(eval $(call mcheck-ENVS,$(t))))
348 ifneq "$(strip $(tests) $(tests-internal) $(xtests) $(test-srcs))" ""
349 # These are the implicit rules for making test outputs
350 # from the test programs and whatever input files are present.
353 $(if $($*-ENV-only),$(test-wrapper-env-only) $($*-ENV-only),\
354 $(test-wrapper-env) $(run-program-env) $($*-ENV)) \
355 $(host-test-program-cmd) $($*-ARGS)
357 $(objpfx)%.out: %.input $(objpfx)%
358 $(make-test-out) > $@ < $(word 1,$^); \
360 $(objpfx)%.out: /dev/null $(objpfx)% # Make it 2nd arg for canned sequence.
361 $(make-test-out) > $@; \
365 # Any tests that require an isolated container (filesystem, network
366 # and pid namespaces) in which to run, should be added to
368 $(tests-container:%=$(objpfx)%.out): $(objpfx)%.out : $(if $(wildcard $(objpfx)%.files),$(objpfx)%.files,/dev/null) $(objpfx)%
369 $(test-wrapper-env) $(run-program-env) $(run-via-rtld-prefix) \
370 $(common-objpfx)support/test-container env $(run-program-env) $($*-ENV) \
371 $(host-test-program-cmd) $($*-ARGS) > $@; \
375 # tests-unsupported lists tests that we will not try to build at all in
376 # this configuration. Note this runs every time because it does not
377 # actually create its target. The dependency on Makefile is meant to
378 # ensure that it runs after a Makefile change to add a test to the list
379 # when it previously ran and produced a .out file (probably for a failure).
380 ifneq "$(strip $(tests-unsupported))" ""
381 $(tests-unsupported:%=$(objpfx)%.out): $(objpfx)%.out: Makefile
383 $(..)scripts/evaluate-test.sh $(patsubst $(common-objpfx)%.out,%,$@) \
384 77 false false > $(@:.out=.test-result)
389 ifneq "$(strip $(tests-printers))" ""
391 # Static pattern rule for building the test programs for the pretty printers.
392 $(tests-printers-programs): %: %.o $(tests-printers-libs) \
393 $(sort $(filter $(common-objpfx)lib%,$(link-libc-static-tests))) \
394 $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
395 $(+link-printers-tests)
397 # Add the paths to the generated constants file and test_common_printers.py
398 # to PYTHONPATH so the test scripts can find them.
399 py-env := PYTHONPATH=$(py-const-dir):$(..)scripts:$${PYTHONPATH}
401 # Static pattern rule that matches the test-* targets to their .c and .py
402 # prerequisites. It'll run the corresponding test script for each test program
403 # we compiled and place its output in the corresponding .out file.
404 # The pretty printer files and test_common_printers.py must be present for all.
405 $(tests-printers-out): $(objpfx)%.out: $(objpfx)% %.py %.c $(pretty-printers) \
406 $(..)scripts/test_printers_common.py
407 $(test-wrapper-env) $(py-env) \
408 $(PYTHON) $*.py $*.c $(objpfx)$* $(pretty-printers) > $@; \
413 .PHONY: distclean realclean subdir_distclean subdir_realclean \
414 subdir_clean subdir_mostlyclean subdir_testclean
415 subdir_mostlyclean: mostlyclean
417 subdir_distclean: distclean
418 subdir_realclean: realclean
419 subdir_testclean: do-tests-clean
423 # We want to install everything except the library itself, but update all
424 # our portions of the library because the parent make will install it later
425 # (likewise the stubs file).
426 .PHONY: subdir_install
427 subdir_install: install-no-libc.a lib-noranlib stubs
429 .PHONY: subdir_objs subdir_stubs
433 # Target required by the Hurd to ensure that all the MiG-generated
434 # headers are in place before building a subdirectory.
435 .PHONY: before-compile
436 before-compile: $(before-compile)
438 $(common-objpfx)dummy.o: $(common-objpfx)dummy.c $(before-compile);