]> git.ipfire.org Git - thirdparty/glibc.git/blame - Makerules
benchtests: Add random() benchmark
[thirdparty/glibc.git] / Makerules
CommitLineData
dff8da6b 1# Copyright (C) 1991-2024 Free Software Foundation, Inc.
1c68c264
RM
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
59ba27a6 15# License along with the GNU C Library; if not, see
5a82c748 16# <https://www.gnu.org/licenses/>.
1c68c264
RM
17
18#
19# Common rules for making the GNU C library. This file is included
20# by the top-level Makefile and by all subdirectory makefiles
21# (through Rules).
22#
23ifneq (,)
24This makefile requires GNU Make.
25endif
26
27REQUIRED_MAKE_VERSION = 3.74
28REAL_MAKE_VERSION = $(firstword $(MAKE_VERSION))
29
30ifneq ($(REQUIRED_MAKE_VERSION), \
31 $(firstword $(sort $(REAL_MAKE_VERSION) $(REQUIRED_MAKE_VERSION))))
32Wrong GNU Make version. See above for the version needed.
33endif
34
35
36ifdef subdir
37.. := ../
38endif # subdir
39
40# If `sources' was defined by the parent makefile, undefine it so
41# we will later get it from wildcard search in this directory.
42ifneq "$(findstring env,$(origin sources))" ""
43sources :=
44endif
45
46oPATH := $(PATH)
47PATH := this definition should take precedence over $(oPATH)
48ifeq ($(PATH),$(oPATH))
49You must not use the -e flag when building the GNU C library.
50else
51PATH := $(oPATH)
52endif
53\f
54ifndef +included-Makeconfig
55include $(..)Makeconfig
56endif
57
1c68c264
RM
58# This variable is used in ``include $(o-iterator)'' after defining
59# $(o-iterator-doit) to produce some desired rule using $o for the object
60# suffix, and setting $(object-suffixes-left) to $(object-suffixes); a copy
61# is produced for each object suffix in use.
62o-iterator = $(patsubst %,$(..)o-iterator.mk,$(object-suffixes-left))
63\f
64# Include any system-specific makefiles.
65
66# This is here so things in sysdep Makefiles can easily depend on foo.h as
67# appropriate and not worry about where foo.h comes from, which may be
68# system dependent and not known by that Makefile.
69vpath %.h $(subst $(empty) ,:,$(strip $(common-objpfx) $(objpfx) \
70 $(+sysdep_dirs) $(..)))
71
72# The same is true for RPC source files.
73vpath %.x $(subst $(empty) ,:,$(strip $(common-objpfx) $(objpfx) \
74 $(+sysdep_dirs) $(..)))
75
76# Some sysdep makefiles use this to distinguish being included here from
77# being included individually by a subdir makefile (hurd/Makefile needs this).
78in-Makerules := yes
79
e0a3ed4f 80sysdep-makefiles := $(wildcard $(sysdirs:=/Makefile))
1c68c264
RM
81ifneq (,$(sysdep-makefiles))
82include $(sysdep-makefiles)
83endif
84
85
86# Reorder before-compile so that mach things come first, and hurd things
87# second, before all else. The mach and hurd subdirectories have many
88# generated header files which the much of rest of the library depends on,
89# so it is best to build them first (and mach before hurd, at that).
90before-compile := $(filter $(common-objpfx)mach% $(common-objpfx)hurd%,\
91 $(before-compile)) \
92 $(filter-out $(common-objpfx)mach% $(common-objpfx)hurd%,\
93 $(before-compile))
94
95# Even before that, we need abi-versions.h which is generated right here.
3f2e46a4 96ifeq ($(build-shared),yes)
1c68c264
RM
97ifndef avoid-generated
98before-compile := $(common-objpfx)abi-versions.h $(before-compile)
99$(common-objpfx)abi-versions.h: $(..)scripts/abi-versions.awk \
100 $(common-objpfx)Versions.all
af296fcd 101 LC_ALL=C $(AWK) -f $^ > $@T
1c68c264
RM
102 mv -f $@T $@
103
104$(common-objpfx)%.latest: $(common-objpfx)abi-versions.h
105 sed -n '/ VERSION_$*_/{s/^.*_\([A-Z0-9_]*\).*$$/\1/;h;};$${g;p;}' \
106 $(common-objpfx)abi-versions.h > $@T
107 mv -f $@T $@
8f2e1830 108
9aa4965c
JM
109# first-versions.h and ldbl-compat-choose.h provide macros used in
110# various symbol versioning macro calls.
111before-compile := $(common-objpfx)first-versions.h \
112 $(common-objpfx)ldbl-compat-choose.h $(before-compile)
8f2e1830 113$(common-objpfx)first-versions.h: $(common-objpfx)versions.stmp
9aa4965c 114$(common-objpfx)ldbl-compat-choose.h: $(common-objpfx)versions.stmp
1c68c264 115endif # avoid-generated
3f2e46a4 116endif # $(build-shared) = yes
1c68c264 117
92ad15a8 118ifndef avoid-generated
83212868
L
119ifneq (,$(CXX))
120# If C++ headers <cstdlib> or <cmath> are used, GCC 6 will include
121# /usr/include/stdlib.h or /usr/include/math.h from "#include_next"
122# (instead of stdlib/stdlib.h or math/math.h in the glibc source
123# directory), and this turns up as a make dependency. An implicit
124# rule will kick in and make will try to install stdlib/stdlib.h or
125# math/math.h as /usr/include/stdlib.h or /usr/include/math.h because
126# the target is out of date. We make a copy of <cstdlib> and <cmath>
127# in the glibc build directory so that stdlib/stdlib.h and math/math.h
128# will be used instead of /usr/include/stdlib.h and /usr/include/math.h.
129before-compile := $(common-objpfx)cstdlib $(common-objpfx)cmath \
130 $(before-compile)
fc3e1337 131$(common-objpfx)cstdlib: $(c++-cstdlib-header)
83212868
L
132 $(INSTALL_DATA) $< $@T
133 $(move-if-change) $@T $@
fc3e1337 134$(common-objpfx)cmath: $(c++-cmath-header)
83212868
L
135 $(INSTALL_DATA) $< $@T
136 $(move-if-change) $@T $@
a65ea28d
L
137ifneq (,$(c++-bits-std_abs-h))
138# Also make a copy of <bits/std_abs.h> from GCC 7 to prevent it from
139# including /usr/include/stdlib.h.
140before-compile := $(common-objpfx)bits/std_abs.h $(before-compile)
141$(common-objpfx)bits/std_abs.h: $(c++-bits-std_abs-h)
142 $(INSTALL_DATA) $< $@T
143 $(move-if-change) $@T $@
144endif
83212868
L
145endif
146
92ad15a8 147before-compile := $(common-objpfx)libc-abis.h $(before-compile)
17ad3877
RM
148$(common-objpfx)libc-abis.h: $(common-objpfx)libc-abis.stamp; @:
149$(common-objpfx)libc-abis.stamp: $(..)scripts/gen-libc-abis \
df5efd61
RM
150 $(firstword $(wildcard $(sysdirs:=/libc-abis)) \
151 $(..)libc-abis) \
152 $(..)Makerules
153 $(SHELL) $< \
92ad15a8 154 $(base-machine)-$(config-vendor)-$(config-os) \
17ad3877
RM
155 < $(word 2,$^) > $(@:.stamp=.h)T
156 $(move-if-change) $(@:.stamp=.h)T $(@:.stamp=.h)
157 touch $@
92ad15a8
UD
158common-generated += $(common-objpfx)libc-abis.h
159endif # avoid-generated
160
ac63a078
DL
161ifeq (yes,$(build-shared))
162$(common-objpfx)runtime-linker.h: $(common-objpfx)runtime-linker.stamp; @:
163$(common-objpfx)runtime-linker.stamp: $(common-objpfx)config.make
164 $(make-target-directory)
165 echo '#define RUNTIME_LINKER "$(rtlddir)/$(rtld-installed-name)"' \
166 > ${@:stamp=T}
167 $(move-if-change) ${@:stamp=T} ${@:stamp=h}
168 touch $@
169endif
170
d73f5331 171# Make sure the subdirectory for object files gets created.
1c68c264 172ifdef objpfx
d73f5331 173ifeq (,$(wildcard $(objpfx).))
1c68c264
RM
174before-compile += $(objpfx).
175$(objpfx).:
176 $(make-target-directory)
177endif
178endif
179
180# Remove existing files from `before-compile'. Things are added there when
181# they must exist for dependency generation to work right, but once they
182# exist there is no further need for every single file to depend on them,
183# and those gratuitous dependencies result in many gratuitous
184# recompilations.
185before-compile := $(filter-out $(wildcard $(before-compile)),$(before-compile))
186
187# Don't let any before-compile file be an intermediate and get removed.
188ifdef before-compile
189$(before-compile):
190endif
191
7f3a54bd
RM
192# We don't want $(common-objpfx) files to depend on miscellaneous stuff
193# in subdirs.
194ifdef subdir
195common-before-compile := $(filter-out $(objpfx)%,$(before-compile))
196else
197common-before-compile = $(before-compile)
198endif
199
d73f5331 200ifndef subdir
1c68c264
RM
201# If a makefile needs to do something conditional on something that
202# can only be figured out from headers, write a FOO.make.c input
7f0d9e61 203# file that uses cpp constructs and contains @@@ LINE @@@ for each LINE
1c68c264 204# to emit in the generated makefile, and use -include $(common-objpfx)FOO.make.
d73f5331
RM
205#
206# We only generate these in the top-level makefile, to avoid any weirdness
207# from subdir-specific makefile tweaks creeping in on an update.
7f3a54bd 208$(common-objpfx)%.make: $(..)%.make.c $(..)Makerules $(common-before-compile)
1c68c264
RM
209 rm -f $@T $@.dT
210 (echo '# Generated from $*.make.c by Makerules.'; \
3192fd8e 211 $(CC) $(CFLAGS) $(CPPFLAGS) -E -DASSEMBLER $< \
7cd72ad3 212 -MD -MP -MT '$$(common-objpfx)$*.make' -MF $@.dT \
1c68c264
RM
213 | sed -n '/@@@/{s/@@@[ ]*\(.*\)@@@/\1/;s/[ ]*$$//p;}'; \
214 echo 'common-generated += $(@F)'; \
9ae10332
RM
215 sed $(sed-remove-objpfx) $(sed-remove-dotdot) $@.dT; \
216 rm -f $@.dT) > $@T
1c68c264 217 mv -f $@T $@
d73f5331 218endif
9ae10332 219
0e56981e 220ifdef subdir
7cd72ad3
RM
221sed-remove-dotdot := -e 's@ *\.\.\/\([^ \]*\)@ $$(..)\1@g' \
222 -e 's@^\.\.\/\([^ \]*\)@$$(..)\1@g'
0e56981e 223else
7cd72ad3
RM
224sed-remove-dotdot := -e 's@ *\([^ \/$$][^ \]*\)@ $$(..)\1@g' \
225 -e 's@^\([^ \/$$][^ \]*\)@$$(..)\1@g'
0e56981e 226endif
7f3a54bd 227
23b5cae1
MG
228ifdef gen-py-const-headers
229# We'll use a static pattern rule to match .pysym files with their
230# corresponding generated .py files.
231# The generated .py files go in the submodule's dir in the glibc build dir.
232py-const-files := $(patsubst %.pysym,%.py,$(gen-py-const-headers))
233py-const-dir := $(objpfx)
234py-const := $(addprefix $(py-const-dir),$(py-const-files))
cb7be159 235py-const-script := $(..)scripts/gen-as-const.py
23b5cae1 236
cb7be159
JM
237# This is a hack we use to generate .py files with constants for
238# Python code.
23b5cae1 239#
cb7be159
JM
240# $@.tmp is a temporary file we use to store the partial contents of
241# the target file. We do this instead of just writing on $@ because,
242# if the build process terminates prematurely, re-running Make
243# wouldn't run this rule since Make would see that the target file
244# already exists (despite it being incomplete).
23b5cae1 245#
cb7be159
JM
246# The output is redirected to a .py file; we'll import it in the main
247# Python code to read the constants generated by gen-as-const.py.
23b5cae1
MG
248$(py-const): $(py-const-dir)%.py: %.pysym $(py-const-script) \
249 $(common-before-compile)
250 $(make-target-directory)
cb7be159
JM
251 $(PYTHON) $(py-const-script) --python \
252 --cc="$(CC) $(CFLAGS) $(CPPFLAGS)" $< \
253 > $@.tmp
254 mv -f $@.tmp $@
23b5cae1
MG
255
256generated += $(py-const)
257endif # gen-py-const-headers
7f3a54bd 258
0c7c6d53 259ifdef gen-as-const-headers
7f3a54bd
RM
260# Generating headers for assembly constants.
261# We need this defined early to get into before-compile before
262# it's used in sysd-rules, below.
f5d1f629
L
263# Define GEN_AS_CONST_HEADERS to avoid circular dependency [BZ #22792].
264# NB: <tcb-offsets.h> is generated from tcb-offsets.sym to define
265# offsets and sizes of types in <tls.h> and maybe <pthread.h> which
266# may include <tcb-offsets.h>. Target header files can check if
267# GEN_AS_CONST_HEADERS is defined to avoid circular dependency which
268# may lead to build hang on a many-core machine.
7e1d4240 269$(common-objpfx)%.h $(common-objpfx)%.h.d: $(..)scripts/gen-as-const.py \
7f3a54bd 270 %.sym $(common-before-compile)
7e1d4240
JM
271 $(PYTHON) $< --cc="$(CC) $(CFLAGS) $(CPPFLAGS) -DGEN_AS_CONST_HEADERS \
272 -MD -MP -MF $(@:.h=.h.d)T \
273 -MT '$(@:.h=.h.d) $(@:.h.d=.h)'" \
274 $(filter %.sym,$^) > $(@:.h.d=.h)T
7f3a54bd
RM
275 sed $(sed-remove-objpfx) $(sed-remove-dotdot) \
276 $(@:.h=.h.d)T > $(@:.h=.h.d)T2
277 rm -f $(@:.h=.h.d)T
278 mv -f $(@:.h=.h.d)T2 $(@:.h=.h.d)
279 mv -f $(@:.h.d=.h)T $(@:.h.d=.h)
280vpath %.sym $(sysdirs)
281before-compile += $(gen-as-const-headers:%.sym=$(common-objpfx)%.h)
0c7c6d53 282
7c3018f9 283tests-internal += $(gen-as-const-headers:%.sym=test-as-const-%)
ba389ce3 284generated += $(gen-as-const-headers:%.sym=test-as-const-%.c)
7e1d4240 285$(objpfx)test-as-const-%.c: $(..)scripts/gen-as-const.py $(..)Makerules \
ba389ce3 286 %.sym $(common-objpfx)%.h
0c7c6d53 287 ($(AWK) '{ sub(/^/, "asconst_", $$2); print; }' $(filter %.h,$^); \
7e1d4240 288 $(PYTHON) $< --test $(filter %.sym,$^)) > $@T
0c7c6d53
RM
289 mv -f $@T $@
290endif
1c68c264 291\f
67c37987
RM
292ifeq (yes,$(build-shared))
293# Generate the header containing the names of all shared libraries.
294# We use a stamp file to avoid unnecessary recompilations.
295before-compile += $(common-objpfx)gnu/lib-names.h
296ifeq ($(soversions.mk-done),t)
297ifndef abi-variants
298lib-names-h-abi = gnu/lib-names.h
299lib-names-stmp-abi = gnu/lib-names.stmp
300else
301lib-names-h-abi = gnu/lib-names-$(default-abi).h
302lib-names-stmp-abi = gnu/lib-names-$(default-abi).stmp
303before-compile += $(common-objpfx)$(lib-names-h-abi)
304common-generated += gnu/lib-names.h
305install-others-nosubdir: $(inst_includedir)/$(lib-names-h-abi)
306$(common-objpfx)gnu/lib-names.h:
307 $(make-target-directory)
308 { \
309 echo '/* This file is automatically generated.';\
310 echo ' It defines macros to allow user program to find the shared'; \
311 echo ' library files which come as part of GNU libc. */'; \
312 echo '#ifndef __GNU_LIB_NAMES_H'; \
313 echo '#define __GNU_LIB_NAMES_H 1'; \
314 echo ''; \
315 $(if $(abi-includes), \
316 $(foreach h,$(abi-includes), echo '#include <$(h)>';) \
317 echo '';) \
318 $(foreach v,$(abi-variants),\
319 $(if $(abi-$(v)-condition),\
320 echo '#if $(abi-$(v)-condition)'; \
321 echo '# include <gnu/lib-names-$(v).h>'); \
322 $(if $(abi-$(v)-condition),echo '#endif';)) \
323 echo ''; \
324 echo '#endif /* gnu/lib-names.h */'; \
325 } > $@
326endif
327$(common-objpfx)$(lib-names-h-abi): $(common-objpfx)$(lib-names-stmp-abi); @:
328$(common-objpfx)$(lib-names-stmp-abi): $(..)scripts/lib-names.awk \
329 $(common-objpfx)soversions.i
330 $(make-target-directory)
331 { \
332 $(if $(abi-variants), \
333 echo '/* This file is automatically generated. */';\
334 echo '#ifndef __GNU_LIB_NAMES_H'; \
335 echo '# error "Never use <$(lib-names-h-abi)> directly; include <gnu/lib-names.h> instead."'; \
336 echo '#endif';, \
337 echo '/* This file is automatically generated.';\
338 echo ' It defines macros to allow user program to find the shared'; \
339 echo ' library files which come as part of GNU libc. */'; \
340 echo '#ifndef __GNU_LIB_NAMES_H'; \
341 echo '#define __GNU_LIB_NAMES_H 1';) \
342 echo ''; \
343 ($(foreach s,$(all-sonames), echo $(s);)) \
344 | LC_ALL=C $(AWK) -f $(firstword $^) | LC_ALL=C sort; \
345 $(if $(abi-variants),, \
346 echo ''; \
347 echo '#endif /* gnu/lib-names.h */';) \
348 } > ${@:stmp=T}
349 $(move-if-change) ${@:stmp=T} ${@:stmp=h}
350 touch $@
351endif
352common-generated += $(lib-names-h-abi) $(lib-names-stmp-abi)
353endif
354\f
355###############################################################################
356# NOTE! Everything adding to before-compile needs to come before this point! #
357###############################################################################
358
1c68c264
RM
359# Generate an ordered list of implicit rules which find the source files in
360# each sysdep directory. The old method was to use vpath to search all the
361# sysdep directories. However, that had the problem that a .S file in a
362# later directory would be chosen over a .c file in an earlier directory,
363# which does not preserve the desired sysdeps ordering behavior.
364
346d65b3
RM
365# System-dependent makefiles can put in `inhibit-sysdep-asm' regexps
366# matching sysdep directories whose assembly source files should be
367# suppressed.
1c68c264 368
e0a3ed4f 369-include $(common-objpfx)sysd-rules
1c68c264
RM
370ifneq ($(sysd-rules-sysdirs),$(config-sysdirs))
371# The value of $(+sysdep_dirs) the sysd-rules was computed for
372# differs from the one we are using now. So force a rebuild of sysd-rules.
373sysd-rules-force = FORCE
374FORCE:
375endif
346d65b3
RM
376$(common-objpfx)sysd-rules: $(..)scripts/sysd-rules.awk \
377 $(common-objpfx)config.make $(..)Makerules \
708ef8a4
RM
378 $(sysdep-makefiles) $(sysdep-makeconfigs) \
379 $(sysd-rules-force)
1c68c264 380 -@rm -f $@T
346d65b3
RM
381 LC_ALL=C $(AWK) -f $< > $@T \
382 -v all_object_suffixes='$(all-object-suffixes)' \
383 -v inhibit_sysdep_asm='$(inhibit-sysdep-asm)' \
384 -v sysd_rules_patterns='$(sysd-rules-patterns)' \
385 -v config_sysdirs='$(config-sysdirs)'
1c68c264
RM
386 mv -f $@T $@
387
388ifndef sysd-rules-done
389# Don't do deps until this exists, because it provides rules to make the deps.
390no_deps=t
391endif
392
b6ab06ce
UD
393define o-iterator-doit
394$(objpfx)%$o: %.S $(before-compile); $$(compile-command.S)
395endef
396object-suffixes-left := $(all-object-suffixes)
397include $(o-iterator)
398
b6ab06ce
UD
399define o-iterator-doit
400$(objpfx)%$o: %.c $(before-compile); $$(compile-command.c)
401endef
402object-suffixes-left := $(all-object-suffixes)
403include $(o-iterator)
404
7735afa2
UD
405define o-iterator-doit
406$(objpfx)%$o: %.cc $(before-compile); $$(compile-command.cc)
407endef
408object-suffixes-left := $(all-object-suffixes)
409include $(o-iterator)
410
b6ab06ce
UD
411# Omit the objpfx rules when building in the source tree, because
412# objpfx is empty and so these rules just override the ones above.
413ifdef objpfx
414# Define first rules to find the source files in $(objpfx).
415# Generated source files will end up there.
416define o-iterator-doit
417$(objpfx)%$o: $(objpfx)%.S $(before-compile); $$(compile-command.S)
418endef
419object-suffixes-left := $(all-object-suffixes)
420include $(o-iterator)
421
b6ab06ce
UD
422define o-iterator-doit
423$(objpfx)%$o: $(objpfx)%.c $(before-compile); $$(compile-command.c)
424endef
425object-suffixes-left := $(all-object-suffixes)
426include $(o-iterator)
db27f125
SP
427
428define o-iterator-doit
429$(objpfx)%$o: $(objpfx)%.cc $(before-compile); $$(compile-command.cc)
430endef
431object-suffixes-left := $(all-object-suffixes)
432include $(o-iterator)
b6ab06ce
UD
433endif
434
d73f5331 435# Generate .dT files as we compile.
b9608428 436compile-mkdep-flags = -MD -MP -MF $@.dt -MT $@
d73f5331 437compile-command.S = $(compile.S) $(OUTPUT_OPTION) $(compile-mkdep-flags)
d73f5331 438compile-command.c = $(compile.c) $(OUTPUT_OPTION) $(compile-mkdep-flags)
7735afa2 439compile-command.cc = $(compile.cc) $(OUTPUT_OPTION) $(compile-mkdep-flags)
1c68c264 440
656416c9
AS
441# Like compile-mkdep-flags, but for use with $(BUILD_CC). We don't want to
442# track system includes here, they may spuriously trigger an install rule,
443# and would cause the check-local-headers test to fail.
444native-compile-mkdep-flags = -MMD -MP -MF $@.dt -MT $@
445
1c68c264
RM
446# GCC can grok options after the file name, and it looks nicer that way.
447compile.c = $(CC) $< -c $(CFLAGS) $(CPPFLAGS)
7735afa2 448compile.cc = $(CXX) $< -c $(CXXFLAGS) $(CPPFLAGS)
073e82bf 449compile.S = $(CC) $< -c $(CPPFLAGS) $(S-CPPFLAGS) \
c503c260
L
450 $(ASFLAGS) $(ASFLAGS-$(suffix $@)) $(sysdep-ASFLAGS) \
451 $(ASFLAGS-$(<F)) $(ASFLAGS-$(@F))
c1487492 452COMPILE.c = $(CC) -c $(CFLAGS) $(CPPFLAGS)
073e82bf 453COMPILE.S = $(CC) -c $(CPPFLAGS) $(S-CPPFLAGS) \
c503c260
L
454 $(ASFLAGS) $(ASFLAGS-$(suffix $@)) $(sysdep-ASFLAGS) \
455 $(ASFLAGS-$(<F)) $(ASFLAGS-$(@F))
1c68c264 456
1c68c264
RM
457# We need this for the output to go in the right place. It will default to
458# empty if make was configured to work with a cc that can't grok -c and -o
459# together. You can't compile the C library with such a compiler.
460OUTPUT_OPTION = -o $@
461
c1487492
RM
462# This is the end of the pipeline for compiling generated C code.
463compile-stdin.c = $(COMPILE.c) -o $@ -x c - $(compile-mkdep-flags)
464
1c68c264
RM
465# We need the $(CFLAGS) to be in there to have the right predefines during
466# the dependency run for C sources. But having it for assembly sources can
467# get the wrong predefines.
f4281cc4 468S-CPPFLAGS = -DASSEMBLER $(asm-CPPFLAGS)
d73f5331 469
1c68c264
RM
470ifneq (,$(objpfx))
471# Continuation lines here are dangerous because they introduce spaces!
472define sed-remove-objpfx
473-e 's@ $(subst .,\.,$(subst @,\@,$(common-objpfx)))@ $$(common-objpfx)@g' \
474-e 's@^$(subst .,\.,$(subst @,\@,$(common-objpfx)))@$$(common-objpfx)@g'
475endef
476endif
477\f
478# Modify the list of routines we build for different targets
479
0269750c 480ifeq (yes,$(build-shared))
1c68c264
RM
481ifndef libc.so-version
482# Undefine this because it can't work when we libc.so is unversioned.
483static-only-routines =
484endif
485endif
486
1c68c264 487elide-routines.oS += $(filter-out $(static-only-routines),\
c2d54929
JM
488 $(routines) $(aux) $(sysdep_routines))
489elide-routines.os += $(static-only-routines)
1c68c264
RM
490
491# If we have versioned code we don't need the old versions in any of the
492# static libraries.
c2d54929
JM
493elide-routines.o += $(shared-only-routines)
494elide-routines.op += $(shared-only-routines)
1c68c264
RM
495\f
496# Shared library building.
497
498ifeq (yes,$(build-shared))
499
3f2e46a4 500# Reference map file only when shared libraries are built and a map file name
1c68c264 501# is given.
3f2e46a4 502ifeq ($(build-shared),yes)
1c68c264
RM
503map-file = $(firstword $($(@F:.so=-map)) \
504 $(addprefix $(common-objpfx), \
505 $(filter $(@F:.so=.map),$(version-maps))))
506load-map-file = $(map-file:%=-Wl,--version-script=%)
507endif
508
fab7ce3f
JM
509# Compiler arguments to use to link a shared object with libc and
510# ld.so. This is intended to be as similar as possible to a default
511# link with an installed libc.
512link-libc-args = -Wl,--start-group \
513 $(libc-for-link) \
514 $(common-objpfx)libc_nonshared.a \
8b196ac4 515 -Wl,--as-needed $(elf-objpfx)ld.so -Wl,--no-as-needed \
fab7ce3f
JM
516 -Wl,--end-group
517
518# The corresponding shared libc to use. This may be modified for a
519# particular target.
520libc-for-link = $(common-objpfx)libc.so
521
522# The corresponding dependencies. As these are used in dependencies,
523# not just commands, they cannot use target-specific variables so need
524# to name both possible libc.so objects.
525link-libc-deps = $(common-objpfx)libc.so $(common-objpfx)linkobj/libc.so \
4134b50d 526 $(common-objpfx)libc_nonshared.a $(elf-objpfx)ld.so
fab7ce3f 527
1c68c264
RM
528# Pattern rule to build a shared object from an archive of PIC objects.
529# This must come after the installation rules so Make doesn't try to
530# build shared libraries in place from the installed *_pic.a files.
531# $(LDLIBS-%.so) may contain -l switches to generate run-time dependencies
fab7ce3f
JM
532# on other shared objects. The linking with libc and ld.so is intended
533# to be as similar as possible to a default link with an installed libc.
02657da2 534lib%.so: lib%_pic.a $(+preinit) $(+postinit) $(link-libc-deps)
fab7ce3f 535 $(build-shlib) $(link-libc-args)
04f5a636 536 $(call after-link,$@)
1c68c264 537
1c68c264 538define build-shlib-helper
30f58bb1 539$(LINK.o) -shared -static-libgcc -Wl,-O1 $(sysdep-LDFLAGS) \
740b3dbe 540 $(if $($(@F)-no-z-defs)$(no-z-defs),,-Wl,-z,defs) $(rtld-LDFLAGS) \
2d5ec669 541 $(if $($(@F)-no-dt-relr),$(no-dt-relr-ldflag),$(dt-relr-ldflag)) \
1c68c264
RM
542 $(extra-B-$(@F:lib%.so=%).so) -B$(csu-objpfx) \
543 $(extra-B-$(@F:lib%.so=%).so) $(load-map-file) \
544 -Wl,-soname=lib$(libprefix)$(@F:lib%.so=%).so$($(@F)-version) \
2d6ab5df 545 $(LDFLAGS.so) $(LDFLAGS-lib.so) $(LDFLAGS-$(@F:lib%.so=%).so) \
1c68c264
RM
546 -L$(subst :, -L,$(rpath-link)) -Wl,-rpath-link=$(rpath-link)
547endef
1c68c264 548
358cad7c 549define build-shlib
91fc5b99 550$(build-shlib-helper) -o $@ \
1c68c264 551 $(csu-objpfx)abi-note.o $(build-shlib-objlist)
1c68c264 552endef
1c68c264 553
1c68c264 554define build-module-helper
30f58bb1 555$(LINK.o) -shared -static-libgcc $(sysdep-LDFLAGS) $(rtld-LDFLAGS) \
1a379ea0 556 $(if $($(@F)-no-z-defs)$(no-z-defs),,-Wl,-z,defs) \
2d5ec669 557 $(if $($(@F)-no-dt-relr),$(no-dt-relr-ldflag),$(dt-relr-ldflag)) \
1c68c264
RM
558 -B$(csu-objpfx) $(load-map-file) \
559 $(LDFLAGS.so) $(LDFLAGS-$(@F:%.so=%).so) \
114d07fd 560 $(link-test-modules-rpath-link) \
1c68c264
RM
561 -L$(subst :, -L,$(rpath-link)) -Wl,-rpath-link=$(rpath-link)
562endef
1c68c264
RM
563
564# This macro is similar to build-shlib but it does not define a soname
565# and it does not depend on the destination name to start with `lib'.
1c68c264
RM
566# binutils only position loadable notes into the first page for binaries,
567# not for shared objects
568define build-module
91fc5b99 569$(build-module-helper) -o $@ \
fab7ce3f 570 $(csu-objpfx)abi-note.o $(build-module-objlist) $(link-libc-args)
04f5a636 571$(call after-link,$@)
1c68c264 572endef
d9266ea0 573define build-module-asneeded
91fc5b99 574$(build-module-helper) -o $@ \
d9266ea0 575 $(csu-objpfx)abi-note.o \
fab7ce3f
JM
576 -Wl,--as-needed $(build-module-objlist) -Wl,--no-as-needed \
577 $(link-libc-args)
04f5a636 578$(call after-link,$@)
d9266ea0 579endef
1c68c264 580
ecd0747d 581# sofini.os must be placed last since it terminates .eh_frame section.
1c68c264
RM
582build-module-helper-objlist = \
583 $(patsubst %_pic.a,$(whole-archive) %_pic.a $(no-whole-archive),\
fab7ce3f 584 $(filter-out %.lds $(map-file) $(+preinit) $(+postinit) \
ecd0747d 585 $(elf-objpfx)sofini.os \
fab7ce3f 586 $(link-libc-deps),$^))
1c68c264
RM
587
588build-module-objlist = $(build-module-helper-objlist) $(LDLIBS-$(@F:%.so=%).so)
589build-shlib-objlist = $(build-module-helper-objlist) \
ecd0747d
L
590 $(LDLIBS-$(@F:lib%.so=%).so) \
591 $(filter $(elf-objpfx)sofini.os,$^)
1c68c264
RM
592
593# Don't try to use -lc when making libc.so itself.
594# Also omits crti.o and crtn.o, which we do not want
595# since we define our own `.init' section specially.
596LDFLAGS-c.so = -nostdlib -nostartfiles
cbdb12de
UD
597# But we still want to link libc.so against $(libc.so-gnulib).
598LDLIBS-c.so += $(libc.so-gnulib)
1c68c264
RM
599# Give libc.so an entry point and make it directly runnable itself.
600LDFLAGS-c.so += -e __libc_main
dbb0f06c 601# Pre-link the objects of libc_pic.a for .gnu.glibc-stub.* processing.
1c68c264 602$(common-objpfx)libc_pic.os: $(common-objpfx)libc_pic.a
b350afab 603 $(LINK.o) -nostdlib -nostartfiles -r -o $@ \
dbb0f06c 604 $(LDFLAGS-c_pic.os) $(whole-archive) $^ -o $@
1b74661a 605
1b74661a
RM
606# Generate a list of -R options to excise .gnu.glibc-stub.* sections.
607$(common-objpfx)libc_pic.opts: $(common-objpfx)libc_pic.os
608 $(OBJDUMP) -h $< | \
609 $(AWK) '$$2 ~ /\.gnu\.glibc-stub\./ { print "-R", $$2 }' \
610 > $@T
611 mv -f $@T $@
612# Apply those -R options.
613$(common-objpfx)libc_pic.os.clean: $(common-objpfx)libc_pic.opts \
614 $(common-objpfx)libc_pic.os
615 $(OBJCOPY) @$^ $@
616generated += libc_pic.opts libc_pic.os.clean
617
618libc_pic_clean := .clean
1b74661a 619
5f855e35
BM
620# Build a possibly-modified version of libc_pic.a for use in building
621# linkobj/libc.so.
622ifeq (,$(filter sunrpc,$(subdirs)))
623$(common-objpfx)linkobj/libc_pic.a: $(common-objpfx)libc_pic.a
624 $(make-target-directory)
625 ln -f $< $@
626else
627$(common-objpfx)linkobj/libc_pic.a: $(common-objpfx)libc_pic.a \
628 $(common-objpfx)sunrpc/librpc_compat_pic.a
629 $(make-target-directory)
630 (cd $(common-objpfx)linkobj; \
631 $(AR) x ../libc_pic.a; \
632 rm $$($(AR) t ../sunrpc/librpc_compat_pic.a | sed 's/^compat-//'); \
633 $(AR) x ../sunrpc/librpc_compat_pic.a; \
634 $(AR) cr libc_pic.a *.os; \
635 rm *.os)
636endif # $(subdirs) contains sunrpc
637
638# Clear link-libc-deps for the libc.so libraries so build-shlibs does not
639# filter ld.so out of the list of linked objects.
fab7ce3f 640$(common-objpfx)libc.so: link-libc-deps = # empty
5f855e35 641$(common-objpfx)linkobj/libc.so: link-libc-deps = # empty
fab7ce3f 642
5f855e35
BM
643# Use our own special initializer and finalizer files for the libc.so
644# libraries.
ce12fc71 645$(common-objpfx)libc.so: $(common-objpfx)libc_pic.os$(libc_pic_clean) \
4134b50d
JM
646 $(elf-objpfx)sofini.os \
647 $(elf-objpfx)interp.os \
91fc5b99 648 $(elf-objpfx)ld.so
5f855e35 649 $(build-shlib)
04f5a636 650 $(call after-link,$@)
5f855e35 651
ce12fc71 652$(common-objpfx)linkobj/libc.so: $(common-objpfx)linkobj/libc_pic.a \
4134b50d
JM
653 $(elf-objpfx)sofini.os \
654 $(elf-objpfx)interp.os \
91fc5b99 655 $(elf-objpfx)ld.so
1c68c264 656 $(build-shlib)
04f5a636 657 $(call after-link,$@)
5f855e35 658
3f2e46a4 659ifeq ($(build-shared),yes)
1c68c264
RM
660$(common-objpfx)libc.so: $(common-objpfx)libc.map
661endif
662common-generated += libc.so libc_pic.os
663ifdef libc.so-version
664$(common-objpfx)libc.so$(libc.so-version): $(common-objpfx)libc.so
665 $(make-link)
666common-generated += libc.so$(libc.so-version)
667endif
668endif
1c68c264
RM
669\f
670# Figure out the source filenames in this directory.
671
672override sources := $(addsuffix .c,\
673 $(filter-out $(elided-routines),\
674 $(routines) $(aux) \
675 $(sysdep_routines)))
676sysdep_routines := $(sysdep_routines)
677
678headers := $(headers) $(sysdep_headers)
679
680# This is the list of all object files, gotten by
681# replacing every ".c" in `sources' with a ".o".
c2d54929 682override objects := $(addprefix $(objpfx),$(sources:.c=.o))
1c68c264
RM
683
684
685# The makefile may define $(extra-libs) with `libfoo libbar'
686# to build libfoo.a et al from the modules listed in $(libfoo-routines).
687ifdef extra-libs
688# extra-lib.mk is included once for each extra lib to define rules
689# to build it, and to add its objects to the various variables.
690# During its evaluation, $(lib) is set to the name of the library.
691extra-libs-left := $(extra-libs)
692include $(patsubst %,$(..)extra-lib.mk,$(extra-libs))
693endif
b30542fb
UD
694
695
aed48a2a
RM
696# The makefile may define $(modules-names) to build additional modules.
697# These are built with $(build-module), except any in $(modules-names-nobuild).
7c3018f9 698# MODULE_NAME=extramodules, except any in $(modules-names-tests).
b30542fb 699ifdef modules-names
7c3018f9
ZW
700cpp-srcs-left := $(filter-out $(modules-names-tests),$(modules-names))
701ifneq (,$(cpp-srcs-left))
702lib := extramodules
703include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
704endif
705
706ifdef modules-names-tests
707cpp-srcs-left := $(filter $(modules-names-tests),$(modules-names))
708ifneq (,$(cpp-srcs-left))
709lib := testsuite
710include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
711endif
712endif
aed48a2a
RM
713
714extra-modules-build := $(filter-out $(modules-names-nobuild),$(modules-names))
715$(extra-modules-build:%=$(objpfx)%.so): $(objpfx)%.so: \
91fc5b99 716 $(objpfx)%.os $(link-libs-deps)
aed48a2a 717 $(build-module)
b30542fb 718endif
1c68c264
RM
719\f
720+depfiles := $(sources:.c=.d) \
721 $(patsubst %.o,%.d,$(filter %.o,$(extra-objs:.os=.o))) \
5bd66283 722 $(patsubst %.oS,%.d,$(filter %.oS,$(extra-objs))) \
376e973a 723 $(patsubst %.o,%.d,$(filter %.o,$(extra-test-objs:.os=.o))) \
088d3291 724 $(addsuffix .d,$(tests) $(tests-internal) $(xtests) \
1fe00d3e 725 $(tests-container) $(tests-printers) \
088d3291 726 $(test-srcs) $(tests-time64) $(xtests-time64))
1c68c264
RM
727ifeq ($(build-programs),yes)
728+depfiles += $(addsuffix .d,$(others) $(sysdep-others))
729endif
730+depfiles := $(addprefix $(objpfx),\
731 $(filter-out $(addsuffix .d,$(omit-deps)),\
9ae10332 732 $(+depfiles)))
d73f5331
RM
733all-dt-files := $(foreach o,$(object-suffixes-for-libc),$(+depfiles:.d=$o.dt))
734+depfiles := $(patsubst %.dt,%.d,$(wildcard $(all-dt-files))) \
735 $(wildcard $(all-dt-files:.dt=.d))
736
737# This is a funny rule in that it removes its input file.
738%.d: %.dt
739 @sed $(sed-remove-objpfx) $< > $(@:.d=.T) && \
740 mv -f $(@:.d=.T) $@ && \
741 rm -f $<
9ae10332
RM
742
743# Avoid the .h.d files for any .sym files whose .h files don't exist yet.
744# They will be generated when they're needed, and trying too early won't work.
745+gen-as-const := $(gen-as-const-headers:%.sym=$(common-objpfx)%.h)
746+depfiles += $(addsuffix .d,$(filter $(wildcard $(+gen-as-const)),\
747 $(+gen-as-const)))
1c68c264
RM
748
749ifdef +depfiles
750ifneq ($(no_deps),t)
751-include $(+depfiles)
752endif
753endif
754\f\f
755# Maximize efficiency by minimizing the number of rules.
756.SUFFIXES: # Clear the suffix list. We don't use suffix rules.
757# Don't define any builtin rules.
2d7ed98a 758MAKEFLAGS := $(MAKEFLAGS) -r
1c68c264
RM
759
760# Generic rule for making directories.
761%/:
762# mkdir isn't smart enough to strip a trailing /.
5dfeae0e
UD
763# We always require a mkdir which supports the -p option to avoid error
764# messages in case of races.
765 mkdir -p $(@:%/=%)
1c68c264
RM
766\f
767# Make sure that object files are not removed
768# when they are intermediates between sources and library members.
769.PRECIOUS: $(addprefix $(objpfx)%,$(all-object-suffixes))
770
771# Make sure that the parent library archive is never removed.
772.PRECIOUS: $(foreach l,$(libtypes),$(patsubst %,$(common-objpfx)$l,c))
773\f
774# Use the verbose option of ar and tar when not running silently.
2d7ed98a 775ifeq ($(silent-make),no) # if not -s
1c68c264
RM
776verbose := v
777else # -s
778verbose :=
779endif # not -s
780
781ARFLAGS := r$(verbose)
782CREATE_ARFLAGS := cru$(verbose)
783\f
784# This makes all the object files in the parent library archive.
785
786.PHONY: lib lib-noranlib
787lib: lib-noranlib $(foreach l,$(libtypes),$(patsubst %,$(common-objpfx)$l,c))
788lib-noranlib: libobjs
789
790# For object-suffix $o, the list of objects with that suffix.
791# Makefiles can define `elide-routines.so = foo' to leave foo.so out.
792o-objects = $(patsubst %.o,%$o,$(filter-out $(patsubst %,$(objpfx)%.o,\
793 $(elide-routines$o)),\
794 $(objects))) \
795 $(addprefix $(objpfx),$(o-objects$o))
796
797others: $(addprefix $(objpfx),$(install-lib))
798
799ifndef objects
800
801# Create the stamp$o files to keep the parent makefile happy.
802subdir_lib: $(foreach o,$(object-suffixes-for-libc),$(objpfx)stamp$o)
803$(foreach o,$(object-suffixes-for-libc),$(objpfx)stamp$o):
804 $(make-target-directory)
805 rm -f $@; > $@
806else
807
808# Define explicit rules to update each $(objpfx)stamp.SUFFIX
809# timestamp file; these rules (one explicit rule is generated for each
810# object suffix) write a list of objects to update in the stamp file.
811# The parent will then actually add them all to the archive in the
812# archive rule, below.
813define o-iterator-doit
814$(objpfx)stamp$o: $(o-objects); $$(do-stamp)
815endef
816define do-stamp
d8f6e441 817$(make-target-directory)
1c68c264
RM
818echo '$(patsubst $(objpfx)%,$(addsuffix /,$(subdir))%,$^)' > $@T
819mv -f $@T $@
820endef
821object-suffixes-left := $(object-suffixes-for-libc)
822include $(o-iterator)
823
824endif
825
826# Now define explicit rules to build the library archives; these depend
827# on the stamp files built above.
828define o-iterator-doit
829$(common-objpfx)$(patsubst %,$(libtype$o),c): \
830 $(subdirs-stamp-o) $(common-objpfx)stamp$o; $$(do-makelib)
831endef
832define do-makelib
833cd $(common-objdir) && \
834$(AR) $(CREATE_ARFLAGS) $(@F) `cat $(patsubst $(common-objpfx)%,%,$^)`
1c68c264
RM
835endef
836subdirs-stamps := $(foreach d,$(subdirs),$(common-objpfx)$d/stamp%)
837subdirs-stamp-o = $(subst %,$o,$(subdirs-stamps))
838ifndef subdir
839$(subdirs-stamps): subdir_lib;
840endif
841object-suffixes-left = $(object-suffixes-for-libc)
842include $(o-iterator)
843
844
845# This makes all the object files.
846.PHONY: objects objs libobjs extra-objs
847objects objs: libobjs extra-objs
848libobjs: $(foreach o,$(object-suffixes-for-libc),$(objpfx)stamp$o)
849extra-objs: $(addprefix $(objpfx),$(extra-objs))
850
851# Canned sequence for building an extra library archive.
852define build-extra-lib
853$(patsubst %/,cd % &&,$(objpfx)) \
854$(AR) $(CREATE_ARFLAGS) $(@:$(objpfx)%=%) \
855 $(patsubst $(objpfx)%,%,$^)
1c68c264
RM
856endef
857\f
858# Installation.
859
860.PHONY: force-install
861force-install:
862
863# $(install-lib) are installed from the object directory into $(libdir);
864# files in $(install-lib) matching `lib%.a' are ranlib'd after installation
80d4c3b0
RM
865# unless they also appear in $(non-lib.a). $(install-data) are installed as
866# they are into $(datadir). $(headers) are installed as they are in
1c68c264
RM
867# $(includedir). $(install-bin), $(install-bin-script) and $(install-sbin)
868# are installed from the object directory into $(bindir), $(bindir) and
80d4c3b0
RM
869# $(sbindir), respectively. $(install-others) and $(install-others-programs)
870# are absolute path names of files to install; rules to install them are
871# defined elsewhere.
1c68c264
RM
872
873# The simple library name to install libc.a under.
874# This could be defined by a sysdep Makefile.
875ifndef libc-name
876libc-name := c
877endif
878
879define do-install
880$(make-target-directory)
881$(INSTALL_DATA) $< $@
882endef
883
884# Make the target directory if it doesn't exist, using the `mkinstalldirs'
885# script that does `mkdir -p' even if `mkdir' doesn't support that flag.
886define make-target-directory
887$(addprefix $(..)./scripts/mkinstalldirs ,\
888 $(filter-out $(wildcard $(@D:%/=%)),$(@D:%/=%)))
889endef
890
891# Any directory (parent or subdir) should install libc.a; this way
892# "make install" in a subdir is guaranteed to install everything it changes.
893installed-libcs := $(foreach o,$(filter-out .os,$(object-suffixes-for-libc)),\
894 $(inst_libdir)/$(patsubst %,$(libtype$o),\
895 $(libprefix)$(libc-name)))
a82a3db9
MS
896
897.PHONY: check-install-supported
898check-install-supported:
899
900# Check to see if the prefix or exec_prefix GNU standard variable
901# has been overridden on the command line and, if so, fail with
902# an error message since doing so is not supported (set DESTDIR
903# instead).
904ifeq ($(origin prefix),command line)
905check-install-supported:
906 $(error Overriding prefix is not supported. Set DESTDIR instead.)
907endif
908
909ifeq ($(origin exec_prefix),command line)
910check-install-supported:
911 $(error Overriding exec_prefix is not supported. Set DESTDIR instead.)
912endif
913
914install: check-install-supported
915
1c68c264
RM
916install: $(installed-libcs)
917$(installed-libcs): $(inst_libdir)/lib$(libprefix)%: lib $(+force)
918 $(make-target-directory)
919 $(INSTALL_DATA) $(common-objpfx)lib$(*:$(libc-name)%=c%) $@
1c68c264
RM
920
921define do-install-program
922$(make-target-directory)
923$(INSTALL_PROGRAM) $< $@.new
924mv -f $@.new $@
925endef
926
927define do-install-script
928$(make-target-directory)
929$(INSTALL_SCRIPT) $< $@.new
930mv -f $@.new $@
931endef
932
933install-lib.so := $(filter %.so,$(install-lib:%_pic.a=%.so))
934install-lib := $(filter-out %.so %_pic.a,$(install-lib))
935
936ifeq (yes,$(build-shared))
b89d5de2 937# Find which .so's have a version number in their soname.
1c68c264
RM
938versioned := $(strip $(foreach so,$(install-lib.so),\
939 $(patsubst %,$(so),$($(so)-version))))
940
941install-lib.so-versioned := $(filter $(versioned), $(install-lib.so))
942install-lib.so-unversioned := $(filter-out $(versioned), $(install-lib.so))
943
8208be38 944# For libraries whose soname have version numbers, we install two files:
cc8bfce9 945# $(inst_libdir)/libfoo.so -- for linking, symlink or ld script
8208be38 946# $(inst_slibdir)/libfoo.so.NN -- for loading by SONAME
cc8bfce9
RM
947install-lib-nosubdir: $(install-lib.so-unversioned:%=$(inst_slibdir)/%) \
948 $(foreach L,$(install-lib.so-versioned),\
949 $(inst_libdir)/$L \
cc8bfce9 950 $(inst_slibdir)/$L$($L-version))
1c68c264
RM
951
952# Install all the unversioned shared libraries.
953$(install-lib.so-unversioned:%=$(inst_slibdir)/%): $(inst_slibdir)/%.so: \
954 $(objpfx)%.so $(+force)
955 $(do-install-program)
956
957ifneq ($(findstring -s,$(LN_S)),)
958define make-link
959rm -f $@.new
960$(SHELL) $(..)scripts/rellns-sh $< $@.new
961mv -f $@.new $@
962endef
abcb584d
L
963define make-link-multidir
964$(patsubst %/,cd %,$(objpfx)); \
00505532 965 $(addprefix $(abspath $(..)scripts/mkinstalldirs) ,$(dir $(multidir))); \
abcb584d
L
966 $(LN_S) . $(multidir) 2> /dev/null; \
967 test -L $(multidir)
968endef
1c68c264
RM
969else
970# If we have no symbolic links don't bother with rellns-sh.
971define make-link
972rm -f $@.new
973$(LN_S) $< $@.new
974mv -f $@.new $@
975endef
abcb584d
L
976define make-link-multidir
977$(make-target-directory)
978ln -f $(objpfx)/$(@F) $@
979endef
1c68c264
RM
980endif
981
1c68c264 982ifdef libc.so-version
8208be38 983$(inst_slibdir)/libc.so$(libc.so-version): $(common-objpfx)libc.so $(+force)
1c68c264 984 $(do-install-program)
8208be38 985
1c68c264
RM
986install: $(inst_slibdir)/libc.so$(libc.so-version)
987
988# This fragment of linker script gives the OUTPUT_FORMAT statement
989# for the configuration we are building. We put this statement into
990# the linker scripts we install for -lc et al so that they will not be
991# used by a link for a different format on a multi-architecture system.
87d583c6 992$(common-objpfx)format.lds: $(common-objpfx)config.make \
1c68c264 993 $(common-objpfx)config.h $(..)Makerules
0400f928
FW
994 $(LINK.o) -shared -nostdlib -nostartfiles \
995 -x assembler /dev/null -o $@.so
87d583c6 996 $(OBJDUMP) -f $@.so | sed -n 's/.*file format \(.*\)/OUTPUT_FORMAT(\1)/;T;p' > $@
1c68c264 997 rm -f $@.so
1c68c264
RM
998common-generated += format.lds
999
1000ifndef subdir
1001# What we install as libc.so for programs to link against is in fact a
1002# link script. It contains references for the various libraries we need.
1003# The libc.so object is not complete since some functions are only defined
1004# in libc_nonshared.a.
1005# We need to use absolute paths since otherwise local copies (if they exist)
1006# of the files are taken by the linker.
1007install: $(inst_libdir)/libc.so
1008$(inst_libdir)/libc.so: $(common-objpfx)format.lds \
1009 $(common-objpfx)libc.so$(libc.so-version) \
1010 $(inst_libdir)/$(patsubst %,$(libtype.oS),\
1011 $(libprefix)$(libc-name)) \
1012 $(+force)
1013 (echo '/* GNU ld script';\
1014 echo ' Use the shared library, but some functions are only in';\
1015 echo ' the static library, so try that secondarily. */';\
1016 cat $<; \
1017 echo 'GROUP ( $(slibdir)/libc.so$(libc.so-version)' \
1018 '$(libdir)/$(patsubst %,$(libtype.oS),$(libprefix)$(libc-name))'\
04eed2b0 1019 ' AS_NEEDED (' $(rtlddir)/$(rtld-installed-name) ') )' \
1c68c264
RM
1020 ) > $@.new
1021 mv -f $@.new $@
1022
1023endif
1024
1025else
1026install: $(inst_slibdir)/libc.so
1027$(inst_slibdir)/libc.so: $(common-objpfx)libc.so $(+force)
1028 $(do-install-program)
1029endif
1030
1031ifneq (,$(versioned))
1032# Produce three sets of rules as above for all the smaller versioned libraries.
1033
1034define o-iterator-doit
1035$(inst_libdir)/$o: $(inst_slibdir)/$o$($o-version) $(+force); $$(make-link)
1036endef
2afbfec4
RM
1037object-suffixes-left := $(filter-out $(install-lib-ldscripts),$(versioned))
1038ifneq (,$(object-suffixes-left))
1c68c264 1039include $(o-iterator)
2afbfec4 1040endif
1c68c264
RM
1041
1042# Make symlinks in the build directory, because the versioned names might
1043# be referenced by a DT_NEEDED in another library.
1044define o-iterator-doit
1045$(objpfx)$o$($o-version): $(objpfx)$o; $$(make-link)
1046endef
1047object-suffixes-left := $(versioned)
1048include $(o-iterator)
1049
1050generated += $(foreach o,$(versioned),$o$($o-version))
1051
1c68c264 1052define o-iterator-doit
8208be38 1053$(inst_slibdir)/$o$($o-version): $(objpfx)$o $(+force);
1c68c264
RM
1054 $$(do-install-program)
1055endef
1056object-suffixes-left := $(versioned)
1057include $(o-iterator)
b89d5de2 1058endif # ifneq (,$(versioned))
1c68c264
RM
1059
1060define do-install-so
1061$(do-install-program)
85ee9a0d
RM
1062$(patsubst %,$(LN_S) -f $(@F) \
1063 $(@D)/$(patsubst %$*.so,%,$(<F))$(libprefix)$*.so,\
1c68c264
RM
1064 $(filter-out %.so,$@))
1065endef
1066
1067so-versions := $(sort $(foreach so,$(install-lib.so),.so$($(so)-version)))
1068$(foreach v,$(so-versions),\
1069 $(inst_slibdir)/lib$(libprefix)%$v): $(common-objpfx)lib%.so \
1070 $(+force)
1071 $(do-install-so)
1072$(foreach v,$(so-versions),\
1073 $(inst_slibdir)/$(libprefix)%$v): $(common-objpfx)%.so $(+force)
1074 $(do-install-so)
1075endif
1076
1077ifdef install-bin
1078$(addprefix $(inst_bindir)/,$(install-bin)): \
1079 $(inst_bindir)/%: $(objpfx)% $(+force)
1080 $(do-install-program)
1081endif
1082ifdef install-bin-script
1083$(addprefix $(inst_bindir)/,$(install-bin-script)): \
1084 $(inst_bindir)/%: $(objpfx)% $(+force)
1085 $(do-install-script)
1086endif
1087ifdef install-rootsbin
1088$(addprefix $(inst_rootsbindir)/,$(install-rootsbin)): \
1089 $(inst_rootsbindir)/%: $(objpfx)% $(+force)
1090 $(do-install-program)
1091endif
1092ifdef install-sbin
1093$(addprefix $(inst_sbindir)/,$(install-sbin)): \
1094 $(inst_sbindir)/%: $(objpfx)% $(+force)
1095 $(do-install-program)
1096endif
1097ifdef install-lib
1098install-lib.a := $(filter lib%.a,$(install-lib))
cbd8f930 1099install-lib.a := $(filter-out $(install-lib-ldscripts),$(install-lib.a))
1c68c264
RM
1100install-lib-non.a := $(filter-out lib%.a,$(install-lib))
1101ifdef install-lib-non.a
1102$(addprefix $(inst_libdir)/$(libprefix),$(install-lib-non.a)): \
1103 $(inst_libdir)/$(libprefix)%: $(objpfx)% $(+force)
1104 $(do-install)
1105endif
1106ifdef install-lib.a
1107$(install-lib.a:lib%.a=$(inst_libdir)/lib$(libprefix)%.a): \
1108 $(inst_libdir)/lib$(libprefix)%.a: $(objpfx)lib%.a $(+force)
1109 $(do-install)
1c68c264
RM
1110endif
1111endif
1112ifdef install-data
1113$(addprefix $(inst_datadir)/,$(install-data)): $(inst_datadir)/%: % $(+force)
1114 $(do-install)
1115endif
1116headers := $(strip $(headers))
1117ifdef headers
f0cf0902
RM
1118# This implicit rule installs headers from the source directory.
1119# It may be ignored in preference to rules from sysd-rules to find
1120# headers in the sysdeps tree.
1121$(inst_includedir)/%.h: $(objpfx)%.h $(+force)
1c68c264 1122 $(do-install)
4962050e
JM
1123$(inst_includedir)/%.h: $(common-objpfx)%.h $(+force)
1124 $(do-install)
f0cf0902
RM
1125$(inst_includedir)/%.h: %.h $(+force)
1126 $(do-install)
f0cf0902
RM
1127headers-nonh := $(filter-out %.h,$(headers))
1128ifdef headers-nonh
1129$(addprefix $(inst_includedir)/,$(headers-nonh)): $(inst_includedir)/%: \
1130 % $(+force)
1131 $(do-install)
1132endif # headers-nonh
1c68c264
RM
1133endif # headers
1134
1135.PHONY: install-bin-nosubdir install-bin-script-nosubdir \
1136 install-rootsbin-nosubdir install-sbin-nosubdir install-lib-nosubdir \
1137 install-data-nosubdir install-headers-nosubdir
1138install-bin-nosubdir: $(addprefix $(inst_bindir)/,$(install-bin))
1139install-bin-script-nosubdir: $(addprefix $(inst_bindir)/,$(install-bin-script))
1140install-rootsbin-nosubdir: \
1141 $(addprefix $(inst_rootsbindir)/,$(install-rootsbin))
1142install-sbin-nosubdir: $(addprefix $(inst_sbindir)/,$(install-sbin))
1143install-lib-nosubdir: $(addprefix $(inst_libdir)/,\
1144 $(patsubst lib%.a,lib$(libprefix)%.a,$(install-lib.a)) \
1145 $(addprefix $(libprefix),$(install-lib-non.a)))
1146install-data-nosubdir: $(addprefix $(inst_datadir)/,$(install-data))
1147install-headers-nosubdir: $(addprefix $(inst_includedir)/,$(headers))
1148install-others-nosubdir: $(install-others)
80d4c3b0 1149install-others-programs-nosubdir: $(install-others-programs)
1c68c264
RM
1150
1151# We need all the `-nosubdir' targets so that `install' in the parent
1152# doesn't depend on several things which each iterate over the subdirs.
1153# This rule makes `install-FOO' always use `install-FOO-nosubdir' as a
1154# subroutine. Then in the parent `install-FOO' also causes subdir makes.
1155install-%:: install-%-nosubdir ;
1156
1157.PHONY: install install-no-libc.a-nosubdir
80d4c3b0 1158install-no-libc.a-nosubdir: install-headers-nosubdir install-data-nosubdir \
1c68c264 1159 install-lib-nosubdir install-others-nosubdir
80d4c3b0
RM
1160ifeq ($(build-programs),yes)
1161install-no-libc.a-nosubdir: install-bin-nosubdir install-bin-script-nosubdir \
1162 install-rootsbin-nosubdir install-sbin-nosubdir \
1163 install-others-programs-nosubdir
1c68c264
RM
1164endif
1165install: install-no-libc.a-nosubdir
1166\f
c1820385 1167# Command to compile $< using the native libraries.
1c68c264
RM
1168define native-compile
1169$(make-target-directory)
1c68c264 1170$(BUILD_CC) $($(basename $(<F))-CFLAGS) $(ALL_BUILD_CFLAGS) \
2ed8cda2 1171 $< $(OUTPUT_OPTION) $(BUILD_LDFLAGS)
1c68c264
RM
1172endef
1173
1c68c264 1174# We always want to use configuration definitions.
2ed8cda2
JM
1175ALL_BUILD_CFLAGS = $(BUILD_CFLAGS) $(BUILD_CPPFLAGS) -D_GNU_SOURCE \
1176 -DIS_IN_build -include $(common-objpfx)config.h
1c68c264
RM
1177
1178# Support the GNU standard name for this target.
1179.PHONY: check
1180check: tests
1181# Special target to run tests which cannot be run unconditionally.
1182# Maintainers should use this target.
1183.PHONY: xcheck
1184xcheck: xtests
1185
088d3291
AZ
1186# Handle tests-time64 and xtests-time64 that should built with LFS
1187# and 64-bit time support.
1188include $(o-iterator)
1189define o-iterator-doit
1190$(foreach f,$(tests-time64) $(xtests-time64),\
1191 $(objpfx)$(f)$(o)): CFLAGS += -D_TIME_BITS=64 -D_FILE_OFFSET_BITS=64
1192endef
1193object-suffixes-left := $(all-object-suffixes)
1194include $(o-iterator)
1195
1196ifeq ($(have-time64-compat),yes)
1197tests += $(foreach t,$(tests-time64),$(t))
1198xtests += $(foreach t,$(xtests-time64),$(t))
1199endif
1200
7c3018f9
ZW
1201# The only difference between MODULE_NAME=testsuite and MODULE_NAME=nonlib is
1202# that almost all internal declarations from config.h, libc-symbols.h, and
1203# include/*.h are not available to 'testsuite' code, but are to 'nonlib' code.
561b0bec
DD
1204all-testsuite := $(strip $(tests) $(xtests) $(test-srcs) $(test-extras) \
1205 $(tests-container))
7c3018f9
ZW
1206ifneq (,$(all-testsuite))
1207cpp-srcs-left = $(all-testsuite)
1208lib := testsuite
1209include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
1210endif
1211
a6d2f948 1212all-nonlib := $(strip $(others) $(others-extras))
1c68c264 1213ifneq (,$(all-nonlib))
9cd47470 1214cpp-srcs-left = $(all-nonlib)
1c68c264 1215lib := nonlib
2bfdaedd 1216include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
1c68c264
RM
1217endif
1218
a6d2f948
AZ
1219# All internal tests use testsuite-internal module since for 64 bit time
1220# support is set as default for MODULE_NAME=nonlib (which include some
1221# installed programs).
1222all-testsuite-internal := $(strip $(tests-internal) $(test-internal-extras))
1223ifneq (,$(all-testsuite-internal))
1224cpp-srcs-left = $(all-testsuite-internal)
1225lib := testsuite-internal
1226include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
1227endif
1c68c264 1228
3f2e46a4 1229ifeq ($(build-shared),yes)
1c68c264
RM
1230# Generate normalized lists of symbols, versions, and data sizes.
1231# This is handy for checking against existing library binaries.
1232
1233%.symlist: $(..)scripts/abilist.awk %.dynsym
1234 LC_ALL=C $(AWK) -f $^ > $@T
1235 mv -f $@T $@
1236
1237%.dynsym: %.so
55f11534 1238 LC_ALL=C $(OBJDUMP) --dynamic-syms $< > $@T
1c68c264
RM
1239 mv -f $@T $@
1240
8dbd5d7b
DM
1241vpath %.abilist $(+sysdep_dirs)
1242
a65ef2ae
AJ
1243# The .PRECIOUS rule prevents the files built by an implicit rule whose
1244# target pattern is %.symlist from being considered "intermediate files"
1245# and automatically removed. We only want these files to be removed by
1246# 'make clean', which is handled by the 'generated' variable.
1247.PRECIOUS: %.symlist
1248generated += $(extra-libs:=.symlist)
1249
1b6dd3f1
JM
1250$(objpfx)check-abi-%.out: $(common-objpfx)config.make %.abilist \
1251 $(objpfx)%.symlist
f0881698
JM
1252 $(check-abi); \
1253 $(evaluate-test)
1b6dd3f1
JM
1254$(objpfx)check-abi-%.out: $(common-objpfx)config.make %.abilist \
1255 $(common-objpfx)%.symlist
f0881698
JM
1256 $(check-abi); \
1257 $(evaluate-test)
9ae10332 1258define check-abi
1b6dd3f1 1259 diff -p -U 0 $(filter %.abilist,$^) $(filter %.symlist,$^) > $@
9ae10332 1260endef
1c68c264 1261
8dbd5d7b 1262update-abi-%: $(objpfx)%.symlist %.abilist
9ae10332 1263 $(update-abi)
8dbd5d7b 1264update-abi-%: $(common-objpfx)%.symlist %.abilist
9ae10332 1265 $(update-abi)
9ae10332 1266define update-abi
8dbd5d7b
DM
1267@if cmp -s $^ 2> /dev/null; \
1268 then \
1269 echo '+++ $(filter %.abilist,$^) is unchanged'; \
1270 else cp -f $^; \
1271 echo '*** Now check $(filter %.abilist,$^) changes for correctness ***'; \
9ae10332
RM
1272 fi
1273endef
1c68c264 1274
c2c082c1
AS
1275# Patch all .abilist files for one DSO. The find command locates abilist
1276# files for all architectures. The abilist files in /generic/ are
1277# filtered out because these are expected to remain empty.
c100dca3
FW
1278define update-all-abi
1279$(SHELL) $(..)scripts/update-abilist.sh $^ \
c2c082c1 1280 $$(find $(..)sysdeps -name '$*.abilist' \! -path '*/generic/*')
c100dca3 1281endef
c100dca3
FW
1282update-all-abi-%: %.abilist $(objpfx)%.symlist
1283 $(update-all-abi)
1284update-all-abi-%: %.abilist $(common-objpfx)%.symlist
1285 $(update-all-abi)
1286
1287.PHONY: update-abi update-all-abi check-abi
1c68c264 1288update-abi: $(patsubst %.so,update-abi-%,$(install-lib.so-versioned))
c100dca3 1289update-all-abi: $(patsubst %.so,update-all-abi-%,$(install-lib.so-versioned))
f214606a
JM
1290check-abi-list = $(patsubst %.so,$(objpfx)check-abi-%.out, \
1291 $(install-lib.so-versioned))
1292check-abi: $(check-abi-list)
1c68c264
RM
1293ifdef subdir
1294subdir_check-abi: check-abi
1295subdir_update-abi: update-abi
c100dca3 1296subdir_update-all-abi: update-all-abi
1c68c264
RM
1297else
1298check-abi: subdir_check-abi
0813022c
SP
1299 if grep -q '^FAIL:' $(objpfx)*/check-abi*.test-result; then \
1300 cat $(objpfx)*/check-abi*.out && exit 1; fi
1c68c264 1301update-abi: subdir_update-abi
c100dca3 1302update-all-abi: subdir_update-all-abi
1c68c264
RM
1303endif
1304
1c68c264 1305ifeq ($(subdir),elf)
1b6dd3f1 1306check-abi: $(objpfx)check-abi-libc.out
f214606a 1307tests-special += $(objpfx)check-abi-libc.out
1c68c264 1308update-abi: update-abi-libc
c100dca3 1309update-all-abi: update-all-abi-libc
1c68c264
RM
1310common-generated += libc.symlist
1311endif
1312
fd54683c 1313ifeq ($(build-shared),yes)
94659495 1314ifdef subdir
f214606a 1315tests-special += $(check-abi-list)
94659495
RM
1316endif
1317endif
1318
1c68c264 1319endif
67c37987 1320\f
1c68c264
RM
1321FORCE:
1322
1c68c264
RM
1323.PHONY: echo-headers
1324echo-headers:
1325 @echo $(headers)
1326
220addf7
RM
1327%.bz2: %; bzip2 -9vk $<
1328%.gz: %; gzip -9vnc $< > $@.new && mv -f $@.new $@
6cfe8609 1329%.xz: %; xz -9evk $<
1c68c264
RM
1330\f
1331# Common cleaning targets.
1332
1333.PHONY: common-mostlyclean common-clean mostlyclean clean do-tests-clean
1334clean: common-clean
1335mostlyclean: common-mostlyclean
1336
7c507f44 1337# Remove test artifacts from a given directory
1c68c264 1338do-tests-clean:
7c507f44
MK
1339 -find $(objpfx) -name '*.out' -delete
1340 -find $(objpfx) -name '*.test-result' -delete
1c68c264
RM
1341
1342# Remove the object files.
7c507f44 1343common-mostlyclean: do-tests-clean
7c3018f9
ZW
1344 -rm -f $(addprefix $(objpfx),$(tests) $(tests-internal) $(xtests) \
1345 $(test-srcs) \
1c68c264 1346 $(others) $(sysdep-others) stubs \
7c3018f9
ZW
1347 $(addsuffix .o,$(tests) \
1348 $(tests-internal) \
1349 $(xtests) \
1350 $(test-srcs) \
1351 $(others) \
7c507f44 1352 $(sysdep-others)))
376e973a
UD
1353 -rm -f $(addprefix $(objpfx),$(extra-objs) $(extra-test-objs) \
1354 $(install-lib) $(install-lib.so) \
1c68c264
RM
1355 $(install-lib.so:%.so=%_pic.a))
1356 -rm -f core
1357 -rm -f $(objpfx)rtld-*.os
1358 $(rmobjs)
1359define rmobjs
1360$(foreach o,$(object-suffixes-for-libc),
1361-rm -f $(objpfx)stamp$o $(o-objects))
1362endef
1363
1364# Also remove the dependencies and generated source files.
1365common-clean: common-mostlyclean
d73f5331
RM
1366 -rm -f $(addprefix $(objpfx),$(generated))
1367 -rm -f $(objpfx)*.d $(objpfx)*.dt
1c68c264
RM
1368 -rm -fr $(addprefix $(objpfx),$(generated-dirs))
1369 -rm -f $(addprefix $(common-objpfx),$(common-generated))
e5a6e567 1370 -rm -f $(gen-as-const-headers:%.sym=$(common-objpfx)%.h)
1c68c264
RM
1371\f
1372# Produce a file `stubs' which contains `#define __stub_FUNCTION'
b830319d 1373# for each function which is a stub.
1c68c264
RM
1374
1375ifdef objpfx
1376.PHONY: stubs # The parent Makefile calls this target.
1377stubs: $(objpfx)stubs
1378endif
561bcb80
RM
1379objs-for-stubs := $(foreach o,$(object-suffixes-for-libc),$(o-objects)) \
1380 $(addprefix $(objpfx),$(extra-objs))
1381$(objpfx)stubs: $(objs-for-stubs)
1382ifneq (,$(strip $(objs-for-stubs)))
ae46b677 1383 (cd $(objpfx).; $(OBJDUMP) -h $(patsubst $(objpfx)%,%,$^)) | \
561bcb80 1384 $(AWK) '/\.gnu\.glibc-stub\./ { \
92ad15a8 1385 sub(/\.gnu\.glibc-stub\./, "", $$2); \
561bcb80
RM
1386 stubs[$$2] = 1; } \
1387 END { for (s in stubs) print "#define __stub_" s }' > $@T
1c68c264 1388 mv -f $@T $@
f291538f
RM
1389else
1390 > $@
1391endif
1c68c264 1392\f
1c68c264
RM
1393ifneq (,$(strip $(gpl2lgpl)))
1394ifneq (,$(wildcard $(..)gpl2lgpl.sed))
1395# Snarf from the master source and frob the copying notice.
1396$(gpl2lgpl): %: $(..)gpl2lgpl.sed /home/gd/gnu/lib/%
1397 sed -f $^ > $@-tmp
1398# So I don't edit them by mistake.
1399 chmod a-w $@-tmp
1400 mv -f $@-tmp $@
1c68c264
RM
1401endif
1402endif
1403
1404# Local Variables:
1405# mode: makefile
1406# End: