summary refs log tree commit diff
path: root/Makerules
blob: 76cbe6b1be8361262166e6d7f53ab3c623b389fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
# Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98 Free Software Foundation, Inc.
# This file is part of the GNU C Library.

# The GNU C Library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.

# The GNU C Library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.

# You should have received a copy of the GNU Library General Public
# License along with the GNU C Library; see the file COPYING.LIB.  If not,
# write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

#
#	Common rules for making the GNU C library.  This file is included
#	by the top-level Makefile and by all subdirectory makefiles
#	(through Rules).
#
ifneq (,)
This makefile requires GNU Make.
endif

REQUIRED_MAKE_VERSION = 3.74
REAL_MAKE_VERSION = $(firstword $(MAKE_VERSION))

ifneq ($(REQUIRED_MAKE_VERSION), \
       $(firstword $(sort $(REAL_MAKE_VERSION) $(REQUIRED_MAKE_VERSION))))
Wrong GNU Make version.  See above for the version needed.
endif


ifdef	subdir
..	:= ../
endif	# subdir

# If `sources' was defined by the parent makefile, undefine it so
# we will later get it from wildcard search in this directory.
ifneq	"$(findstring env,$(origin sources))" ""
sources :=
endif

oPATH := $(PATH)
PATH := this definition should take precedence over $(oPATH)
ifeq ($(PATH),$(oPATH))
You must not use the -e flag when building the GNU C library.
else
PATH := $(oPATH)
endif

ifndef +included-Makeconfig
include $(..)Makeconfig
endif

# `configure' writes a definition of `config-sysdirs' in `config.make'.
sysdirs = $(strip $(full-config-sysdirs))

+sysdir_pfx = $(common-objpfx)

export sysdirs := $(sysdirs)

+sysdep_dirs := $(full-config-sysdirs)
ifdef objdir
+sysdep_dirs := $(objdir) $(+sysdep_dirs)
endif

# Add -I switches to get the right sysdep directories.
# `+includes' in Makeconfig references $(+sysdep-includes).
+sysdep-includes := $(addprefix -I,$(+sysdep_dirs))

# This variable is used in ``include $(o-iterator)'' after defining
# $(o-iterator-doit) to produce some desired rule using $o for the object
# suffix, and setting $(object-suffixes-left) to $(object-suffixes); a copy
# is produced for each object suffix in use.
o-iterator = $(patsubst %,$(..)o-iterator.mk,$(object-suffixes-left))

# Include any system-specific makefiles.

# This is here so things in sysdep Makefiles can easily depend on foo.h as
# appropriate and not worry about where foo.h comes from, which may be
# system dependent and not known by that Makefile.
vpath %.h $(subst $(empty) ,:,$(strip $(common-objpfx) $(objpfx) \
				      $(+sysdep_dirs) $(..)))

# The same is true for RPC source files.
vpath %.x $(subst $(empty) ,:,$(strip $(common-objpfx) $(objpfx) \
				      $(+sysdep_dirs) $(..)))

# Some sysdep makefiles use this to distinguish being included here from
# being included individually by a subdir makefile (hurd/Makefile needs this).
in-Makerules := yes

-include $(+sysdir_pfx)sysd-Makefile
ifndef avoid-generated
ifneq ($(sysd-Makefile-sysdirs),$(config-sysdirs))
sysd-Makefile-force = FORCE
FORCE:
endif
$(+sysdir_pfx)sysd-Makefile: $(+sysdir_pfx)config.make $(..)Makerules \
			     $(sysd-Makefile-force)
	-@rm -f $@T
	(echo 'sysd-Makefile-sysdirs := $(config-sysdirs)';		      \
	 for dir in $(config-sysdirs); do	   			      \
	   file=$$dir/Makefile;						      \
	   case $$dir in						      \
	     /*) rel= ;;						      \
	     *)  rel=$(..) ;;						      \
	   esac;							      \
	   if [ -f $$rel$$file ]; then					      \
	     case $$dir in						      \
	       /*) echo include "$$file" ;;				      \
	       *)  echo include "\$$(..)$$file" ;;			      \
	     esac;							      \
	   else true; fi;						      \
	 done;								      \
	 echo 'sysd-Makefile-done=t') > $@T
	mv -f $@T $@
endif

ifndef sysd-Makefile-done
# Don't do deps until this exists, because it might change the sources list.
no_deps=t
endif


# Reorder before-compile so that mach things come first, and hurd things
# second, before all else.  The mach and hurd subdirectories have many
# generated header files which the much of rest of the library depends on,
# so it is best to build them first (and mach before hurd, at that).
before-compile := $(filter $(common-objpfx)mach% $(common-objpfx)hurd%,\
			   $(before-compile)) \
		  $(filter-out $(common-objpfx)mach% $(common-objpfx)hurd%,\
			       $(before-compile))

# Remove existing files from `before-compile'.  Things are added there when
# they must exist for dependency generation to work right, but once they
# exist there is no further need for every single file to depend on them,
# and those gratuitous dependencies result in many gratuitous
# recompilations.
before-compile := $(filter-out $(wildcard $(before-compile)),$(before-compile))

# Don't let any before-compile file be an intermediate and get removed.
ifdef before-compile
$(before-compile):
endif

# Generate an ordered list of implicit rules which find the source files in
# each sysdep directory.  The old method was to use vpath to search all the
# sysdep directories.  However, that had the problem that a .S file in a
# later directory would be chosen over a .c file in an earlier directory,
# which does not preserve the desired sysdeps ordering behavior.

# When making the list of .d files to include, we can't know which ones
# have source in .s files, and thus do not in fact need a .d file.
# So we must define rules to make .d files for .s files.
define make-dummy-dep
$(addprefix ln $(common-objpfx)dummy.d ,$(filter-out $(wildcard $@),$@))
endef
$(common-objpfx)dummy.d:
	echo '# .s files cannot contain includes, so they need no deps.' > $@

# It matters that this set of rules, for compiling from sources in
# the current directory (the $srcdir/$subdir) come before the
# generated sysdep rules in included from sysd-rules below.  When
# compiling in the source tree, generated sources go into the current
# directory, and those should be chosen before any sources in sysdeps.
define o-iterator-doit
$(objpfx)%$o: %.S $(before-compile); $$(compile-command.S)
endef
object-suffixes-left := $(object-suffixes-for-libc)
include $(o-iterator)
$(objpfx)%.d: %.S $(before-compile); $(+make-deps)

define o-iterator-doit
$(objpfx)%$o: %.s $(before-compile); $$(compile-command.s)
endef
object-suffixes-left := $(object-suffixes-for-libc)
include $(o-iterator)
$(objpfx)%.d: %.s $(common-objpfx)dummy.d; $(make-dummy-dep)

define o-iterator-doit
$(objpfx)%$o: %.c $(before-compile); $$(compile-command.c)
endef
object-suffixes-left := $(object-suffixes-for-libc)
include $(o-iterator)
$(objpfx)%.d: %.c $(before-compile); $(+make-deps)

# Omit the objpfx rules when building in the source tree, because
# objpfx is empty and so these rules just override the ones above.
ifdef objpfx
# Define first rules to find the source files in $(objpfx).
# Generated source files will end up there.
define o-iterator-doit
$(objpfx)%$o: $(objpfx)%.S $(before-compile); $$(compile-command.S)
endef
object-suffixes-left := $(object-suffixes-for-libc)
include $(o-iterator)
$(objpfx)%.d: $(objpfx)%.S $(before-compile); $(+make-deps)

define o-iterator-doit
$(objpfx)%$o: $(objpfx)%.s $(before-compile); $$(compile-command.s)
endef
object-suffixes-left := $(object-suffixes-for-libc)
include $(o-iterator)
$(objpfx)%.d: $(objpfx)%.s $(common-objpfx)dummy.d; $(make-dummy-dep)

define o-iterator-doit
$(objpfx)%$o: $(objpfx)%.c $(before-compile); $$(compile-command.c)
endef
object-suffixes-left := $(object-suffixes-for-libc)
include $(o-iterator)
$(objpfx)%.d: $(objpfx)%.c $(before-compile); $(+make-deps)
endif

# System-dependent makefiles can put in `inhibit-sysdep-asm' wildcard
# patterns matching sysdep directories whose assembly source files should
# be suppressed.
ifdef inhibit-sysdep-asm
define open-check-inhibit-asm
case $$sysdir in $(subst $(empty) ,|,$(inhibit-sysdep-asm))) : ;; *)
endef
close-check-inhibit-asm = ;; esac ;
endif

# Don't include sysd-rules until sysd-Makefile is already there and has been
# included.  It might define inhibit-sysdep-asm, which would affect the
# contents of sysd-rules.
ifdef sysd-Makefile-done
-include $(+sysdir_pfx)sysd-rules
ifneq ($(sysd-rules-sysdirs),$(config-sysdirs))
# The value of $(+sysdep_dirs) the sysd-rules was computed for
# differs from the one we are using now.  So force a rebuild of sysd-rules.
sysd-rules-force = FORCE
FORCE:
endif
endif
$(+sysdir_pfx)sysd-rules: $(+sysdir_pfx)config.make $(..)Makerules \
			  $(wildcard $(foreach dir,$(sysdirs),\
					       $(dir)/Makefile))\
			  $(sysd-rules-force)
	-@rm -f $@T
	(echo 'sysd-rules-sysdirs := $(config-sysdirs)';		      \
	 for sysdir in $(config-sysdirs); do				      \
	   case $$sysdir in \
	     /*) dir=$$sysdir ;; \
	     *)  dir="\$$(..)$$sysdir" ;; \
	   esac; \
	   for o in $(all-object-suffixes); do \
	     $(open-check-inhibit-asm) \
	     echo "\$$(objpfx)%$$o: $$dir/%.S \$$(before-compile); \
		  \$$(compile-command.S)";				      \
	     echo "\$$(objpfx)%$$o: $$dir/%.s \$$(before-compile); \
		  \$$(compile-command.s)";				      \
	     $(close-check-inhibit-asm)	\
	     echo "\$$(objpfx)%$$o: $$dir/%.c \$$(before-compile); \
		  \$$(compile-command.c)";				      \
	   done; \
	   $(open-check-inhibit-asm) \
	   echo "\$$(objpfx)%.d: $$dir/%.s \$$(common-objpfx)dummy.d; \
		\$$(make-dummy-dep)";			       \
	   echo "\$$(objpfx)%.d: $$dir/%.S \$$(before-compile); \
		\$$(+make-deps)";					      \
	   $(close-check-inhibit-asm)	\
	   echo "\$$(objpfx)%.d: $$dir/%.c \$$(before-compile); \
		\$$(+make-deps)";					      \
	 done;								      \
	 echo 'sysd-rules-done = t') > $@T
	mv -f $@T $@

ifndef sysd-rules-done
# Don't do deps until this exists, because it provides rules to make the deps.
no_deps=t
endif


ifndef compile-command.S
compile-command.S = $(compile.S) $(OUTPUT_OPTION)
endif
ifndef compile-command.s
compile-command.s = $(COMPILE.s) $< $(OUTPUT_OPTION)
endif
ifndef compile-command.c
compile-command.c = $(compile.c) $(OUTPUT_OPTION)
endif

# GCC can grok options after the file name, and it looks nicer that way.
compile.c = $(CC) $< -c $(CFLAGS) $(CPPFLAGS)
compile.S = \
  $(CC) $< -c $(CPPFLAGS) -DASSEMBLER $(asm-CPPFLAGS) $(ASFLAGS-$(suffix $@))
COMPILE.S = \
  $(CC) -c $(CPPFLAGS) -DASSEMBLER $(asm-CPPFLAGS) $(ASFLAGS-$(suffix $@))
COMPILE.s = $(filter-out -pipe,$(CC)) -c $(ASFLAGS)

# We need this for the output to go in the right place.  It will default to
# empty if make was configured to work with a cc that can't grok -c and -o
# together.  You can't compile the C library with such a compiler.
OUTPUT_OPTION = -o $@

S-CPPFLAGS = $(asm-CPPFLAGS)
define +make-deps
$(make-target-directory)
-@rm -f $@
$(+mkdep) $< $(CFLAGS) $(CPPFLAGS) $($(<:$*.%=%)-CPPFLAGS) | \
sed \
-e 's,$(subst .,\.,$*)\.o,$(foreach o,$(all-object-suffixes),$(@:.d=$o)) $@,' \
$(sed-remove-objpfx) > $(@:.d=.T)
mv -f $(@:.d=.T) $@
endef
ifneq (,$(objpfx))
# Continuation lines here are dangerous because they introduce spaces!
define sed-remove-objpfx
-e 's@ $(subst .,\.,$(subst @,\@,$(common-objpfx)))@ $$(common-objpfx)@g' \
-e 's@^$(subst .,\.,$(subst @,\@,$(common-objpfx)))@$$(common-objpfx)@g'
endef
endif

# Shared library building.

ifeq (yes,$(build-shared))

# Reference map file only when versioning is selected and a map file name
# is given.
ifeq ($(versioning),yes)
load-map-file = $($(@F:%.so=%)-map)
else
load-map-file =
endif

# Pattern rule to build a shared object from an archive of PIC objects.
# This must come after the installation rules so Make doesn't try to
# build shared libraries in place from the installed *_pic.a files.
# $(LDLIBS-%.so) may contain -l switches to generate run-time dependencies
# on other shared objects.
lib%.so: lib%_pic.a $(+preinit) $(+postinit)
	$(build-shlib)

ifeq ($(have-no-whole-archive),yes)
no-whole-archive = -Wl,--no-whole-archive
else
no-whole-archive =
endif

interp-obj = $(common-objpfx)interp.os
$(interp-obj): $(common-objpfx)%.os: $(..)%.c
	$(compile-command.c)
common-generated += interp.os
CFLAGS-interp.c = -D'RUNTIME_LINKER="$(slibdir)/$(rtld-installed-name)"'

$(common-objpfx)libc.so: $(interp-obj)
$(patsubst %,$(objpfx)%.so,$(extra-libs)): $(interp-obj)

define build-shlib
$(LINK.o) -shared -o $@ $(sysdep-LDFLAGS) $(config-LDFLAGS)  \
	  -B$(csu-objpfx) $(load-map-file:%=-Wl,--version-script=%) \
	  -Wl,-soname=lib$(libprefix)$(@F:lib%.so=%).so$($(@F)-version) \
	  $(LDFLAGS.so) $(LDFLAGS-$(@F:lib%.so=%).so) \
	  -L$(subst :, -L,$(rpath-link)) -Wl,-rpath-link=$(rpath-link) \
	  -Wl,--whole-archive \
	  $(filter-out $($(@F:.so=)-map) $(+preinit) $(+postinit),$^) \
	  $(no-whole-archive) $(LDLIBS-$(@F:lib%.so=%).so)
endef

ifndef libc.so-version
# Undefine this because it can't work when we libc.so is unversioned.
static-only-routines =
endif

elide-routines.oS = $(filter-out $(static-only-routines),\
				 $(routines) $(aux) $(sysdep_routines))

ifdef static-only-routines
elide-routines.os += $(static-only-routines)
endif

ifdef shared-only-routines
# If we have versioned code we don't need the old versions in any of the
# static libraries.
define o-iterator-doit
elide-routines.$o := $(shared-only-routines)
endef
object-suffixes-left := $(filter-out .os,$(object-suffixes))
include $(o-iterator)
endif

# Don't try to use -lc when making libc.so itself.
# Also omits crti.o and crtn.o, which we do not want
# since we define our own `.init' section specially.
LDFLAGS-c.so = -nostdlib -nostartfiles
# But we still want to link libc.so against $(gnulib).
LDLIBS-c.so = $(gnulib)
# Give libc.so an entry point and make it directly runnable itself.
LDFLAGS-c.so += -e __libc_main
# We have a versioning file for libc.so.
libc-map = $(..)libc.map
# Pre-link the objects of libc_pic.a so that we can locally resolve
# COMMON symbols before we link against ld.so.  This is because ld.so
# contains some of libc_pic.a already, which will prevent the COMMONs
# from being allocated in libc.so, which introduces evil dependencies
# between libc.so and ld.so, which can make it impossible to upgrade.
$(common-objpfx)libc_pic.os: $(common-objpfx)libc_pic.a
	$(LINK.o) -nostdlib -nostartfiles -r -o $@ \
	-Wl,-d -Wl,--whole-archive $^
# Use our own special initializer and finalizer files for libc.so.
$(common-objpfx)libc.so: $(elfobjdir)/soinit.os \
			 $(common-objpfx)libc_pic.os \
			 $(elfobjdir)/sofini.os $(elfobjdir)/ld.so \
			 $(libc-map)
	$(build-shlib)
common-generated += libc.so libc_pic.os
ifdef libc.so-version
$(common-objpfx)libc.so$(libc.so-version): $(common-objpfx)libc.so
	$(make-link)
common-generated += libc.so$(libc.so-version)
endif
endif

# Some files must not be compiled with the exception handler mechanism
# enabled (introduced in gcc-2.8).  Use $(no-exceptions) in the
# appropriate CFLAGS definition.
ifeq ($(have-no-exceptions),yes)
no-exceptions = -fno-exceptions
else
no-exceptions =
endif

# Figure out the source filenames in this directory.

override sources	:= $(addsuffix .c,$(filter-out $(elided-routines),\
						       $(routines) $(aux) \
						       $(sysdep_routines)))
sysdep_routines := $(sysdep_routines)

headers := $(headers) $(sysdep_headers)

# This is the list of all object files, gotten by
# replacing every ".c" in `sources' with a ".o".
override objects	:= $(addprefix $(objpfx),$(sources:.c=.o))


# The makefile may define $(extra-libs) with `libfoo libbar'
# to build libfoo.a et al from the modules listed in $(libfoo-routines).
ifdef extra-libs
# extra-lib.mk is included once for each extra lib to define rules
# to build it, and to add its objects to the various variables.
# During its evaluation, $(lib) is set to the name of the library.
extra-libs-left := $(extra-libs)
include $(patsubst %,$(..)extra-lib.mk,$(extra-libs))
endif

+depfiles := $(sources:.c=.d) \
	     $(patsubst %.o,%.d,$(filter %.o,$(extra-objs:.os=.o))) \
	     $(addsuffix .d,$(tests) $(test-srcs) $(tests-static))
ifeq ($(build-programs),yes)
+depfiles += $(addsuffix .d,$(others))
endif
+depfiles := $(addprefix $(objpfx),\
			 $(filter-out $(addsuffix .d,$(omit-deps)),\
				      $(+depfiles)))

ifdef +depfiles
ifneq ($(no_deps),t)
-include $(+depfiles)
endif
endif

# Maximize efficiency by minimizing the number of rules.
.SUFFIXES:	# Clear the suffix list.  We don't use suffix rules.
# Don't define any builtin rules.
MAKEFLAGS := $(MAKEFLAGS)r

# Generic rule for making directories.
%/:
# mkdir isn't smart enough to strip a trailing /.
	mkdir $(@:%/=%)

# Make sure that object files are not removed
# when they are intermediates between sources and library members.
.PRECIOUS: $(addprefix $(objpfx)%,$(all-object-suffixes))

# Make sure that the parent library archive is never removed.
.PRECIOUS: $(foreach l,$(libtypes),$(patsubst %,$(common-objpfx)$l,c))

# Use the verbose option of ar and tar when not running silently.
ifeq	"$(findstring s,$(MAKEFLAGS))" ""	# if not -s
verbose := v
else	   					# -s
verbose	:=
endif						# not -s

ARFLAGS := r$(verbose)
CREATE_ARFLAGS := cru$(verbose)

# This makes all the object files in the parent library archive.

.PHONY: lib lib-noranlib
lib: lib-noranlib $(foreach l,$(libtypes),$(patsubst %,$(common-objpfx)$l,c))

# For object-suffix $o, the list of objects with that suffix.
# Makefiles can define `elide-routines.so = foo' to leave foo.so out.
o-objects = $(patsubst %.o,%$o,$(filter-out $(patsubst %,$(objpfx)%.o,\
						       $(elide-routines$o)),\
					    $(objects)))
lib-noranlib: libobjs
others: $(addprefix $(objpfx),$(install-lib))

ifndef objects

# Create the stamp$o files to keep the parent makefile happy.
subdir_lib: $(foreach o,$(object-suffixes-for-libc),$(objpfx)stamp$o)
$(foreach o,$(object-suffixes-for-libc),$(objpfx)stamp$o):
	$(make-target-directory)
	rm -f $@; > $@
else

# Define explicit rules to update each $(objpfx)stamp.SUFFIX
# timestamp file; these rules (one explicit rule is generated for each
# object suffix) write a list of objects to update in the stamp file.
# The parent will then actually add them all to the archive in the
# archive rule, below.
define o-iterator-doit
$(objpfx)stamp$o: $(o-objects); $$(do-stamp)
endef
define do-stamp
	echo '$(patsubst $(common-objpfx)%,%,$^)' > $@T
	mv -f $@T $@
endef
ifdef static-only-routines
object-suffixes-left := $(object-suffixes-for-libc)
else
object-suffixes-left := $(object-suffixes)
endif
include $(o-iterator)

ifndef static-only-routines
subdir_lib: $(objpfx)stamp.oS
$(objpfx)stamp.oS:
	$(make-target-directory)
	rm -f $@; > $@
endif

endif

# Now define explicit rules to build the library archives; these depend
# on the stamp files built above.  The rule always destroys and recreates
# the library from scratch; it's faster (!) that way.
define o-iterator-doit
$(common-objpfx)$(patsubst %,$(libtype$o),c): \
		$(subdirs-stamp-o) $(common-objpfx)stamp$o; $$(do-makelib)
endef
define do-makelib
	(set -e; cd $(common-objdir); 			  \
	$(AR) $(CREATE_ARFLAGS) $(@F)T			  \
		`cat $(patsubst $(common-objpfx)%,%,$^)`; \
	$(RANLIB) $(@F)T;				  \
	mv -f $(@F)T $(@F))
endef
ifndef subdir
subdirs-stamps := $(foreach d,$(subdirs),$(common-objpfx)$d/stamp%)
subdirs-stamp-o = $(subst %,$o,$(subdirs-stamps))
$(subdirs-stamps): subdir_lib;
endif
# The elf directory needs to know subdirs-stamp-o; otherwise libc_pic.a
# is mangled. FIXME: It would be cleaner to move the final construction
# of ld.so to top level.
ifeq ($(subdir),elf)
subdirs-stamps := $(foreach d,$(subdirs),$(common-objpfx)$d/stamp%)
subdirs-stamp-o = $(subst %,$o,$(subdirs-stamps))
endif

object-suffixes-left = $(object-suffixes-for-libc)
include $(o-iterator)


# This makes all the object files.
.PHONY: objects objs libobjs extra-objs
objects objs: libobjs extra-objs
libobjs: $(foreach o,$(object-suffixes-for-libc),$(objpfx)stamp$o)
extra-objs: $(addprefix $(objpfx),$(extra-objs))

# Canned sequence for building an extra library archive.
define build-extra-lib
$(patsubst %/,cd % &&,$(objpfx)) \
$(AR) $(CREATE_ARFLAGS) $(@:$(objpfx)%=%) \
      $(patsubst $(objpfx)%,%,$^)
$(RANLIB) $@
endef

# Installation.

.PHONY: force-install
force-install:

# $(install-lib) are installed from the object directory into $(libdir);
# files in $(install-lib) matching `lib%.a' are ranlib'd after installation
# unless they also appear in $(non-lib.a).  $(install-data) are installed
# as they are into $(datadir).  $(headers) are installed as they are in
# $(includedir).  $(install-bin) and $(install-sbin) are installed from the
# object directory into $(bindir) and $(sbindir), respectively.
# $(install-others) are absolute path names of files to install; rules to
# install them are defined elsewhere.

# The simple library name to install libc.a under.
# This could be defined by a sysdep Makefile.
ifndef libc-name
libc-name := c
endif

define do-install
$(make-target-directory)
$(INSTALL_DATA) $< $@
endef

# Make the target directory if it doesn't exist, using the `mkinstalldirs'
# script that does `mkdir -p' even if `mkdir' doesn't support that flag.
define make-target-directory
$(addprefix $(..)./mkinstalldirs ,\
	    $(filter-out $(wildcard $(@D:%/=%)),$(@D:%/=%)))
endef

# Any directory (parent or subdir) that has any object files to build
# should install libc.a; this way "make install" in a subdir is guaranteed
# to install everything it changes.
ifdef objects
installed-libcs := $(foreach o,$(filter-out .os,$(object-suffixes-for-libc)),\
			     $(inst_libdir)/$(patsubst %,$(libtype$o),\
						     $(libprefix)$(libc-name)))
install: $(installed-libcs)
$(installed-libcs): $(inst_libdir)/lib$(libprefix)%: lib $(+force)
	$(make-target-directory)
	$(INSTALL_DATA) $(common-objpfx)lib$(*:$(libc-name)%=c%) $@
# Running ranlib after installing makes the __.SYMDEF time stamp up to
# date, which avoids messages from some linkers.
	$(RANLIB) $@
endif

define do-install-program
$(make-target-directory)
$(INSTALL_PROGRAM) $< $@.new
mv -f $@.new $@
endef

install-lib.so := $(filter %.so,$(install-lib:%_pic.a=%.so))
install-lib := $(filter-out %.so %_pic.a,$(install-lib))
ifeq (yes,$(build-shared))

install-lib-nosubdir: $(install-lib.so:%=$(inst_libdir)/%)

# Find which .so's have versions.
versioned := $(strip $(foreach so,$(install-lib.so),\
			       $(patsubst %,$(so),$($(so)-version))))

# Install all the unversioned shared libraries.
$(addprefix $(inst_slibdir)/, $(filter-out $(versioned),$(install-lib.so))): \
	$(inst_slibdir)/%.so: $(objpfx)%.so $(+force); $(do-install-program)

ifneq ($(findstring -s,$(LN_S)),)
define make-link
rm -f $@.new
$(SHELL) $(..)rellns-sh $< $@.new
mv -f $@.new $@
endef
else
# If we have no symbolic links don't bother with rellns-sh.
define make-link
rm -f $@.new
$(LN_S) $< $@.new
mv -f $@.new $@
endef
endif

# XXX The following will have to be changed when `ldconfig' is available.
ifneq (yes,$(cross-compiling))
ifeq (yes,$(has-ldconfig))
define make-shlib-link
@:
endef
endif
endif
ifndef make-shlib-link
define make-shlib-link
$(make-link)
endef
endif

ifdef libc.so-version
# For a library specified to be version N, install three files:
# libc.so	->	libc.so.N	(e.g. libc.so.6)
# libc.so.6	->	libc-VERSION.so	(e.g. libc-1.10.so)

$(inst_slibdir)/libc.so$(libc.so-version): $(inst_slibdir)/libc-$(version).so \
					   $(+force)
	$(make-shlib-link)
$(inst_slibdir)/libc-$(version).so: $(common-objpfx)libc.so $(+force)
	$(do-install-program)
install: $(inst_slibdir)/libc.so$(libc.so-version)

ifndef subdir
# What we install as libc.so for programs to link against is in fact a
# link script.  It contains references for the various libraries we need.
# The libc.so object is not complete since some functions are only defined
# in libc_nonshared.a.
install: $(inst_libdir)/libc.so
$(inst_libdir)/libc.so: $(common-objpfx)libc.so$(libc.so-version) \
			$(inst_libdir)/$(patsubst %,$(libtype.oS),\
						  $(libprefix)$(libc-name)) \
			$(+force)
	(echo '/* GNU ld script';\
	 echo '   Use the shared library, but some functions are only in';\
	 echo '   the static library, so try that secondarily.  */';\
	 echo 'GROUP ( $(slibdir)/libc.so$(libc.so-version)' \
	      '$(libdir)/$(patsubst %,$(libtype.oS),$(libprefix)$(libc-name))'\
	      ')' \
	) > $@.new
	mv -f $@.new $@

endif

else
install: $(inst_slibdir)/libc.so
$(inst_slibdir)/libc.so: $(common-objpfx)libc.so $(+force)
	$(do-install-program)
endif

ifneq (,$(versioned))
# Produce three sets of rules as above for all the smaller versioned libraries.

define o-iterator-doit
$(inst_libdir)/$o: $(inst_slibdir)/$o$($o-version) $(+force); $$(make-link)
endef
object-suffixes-left := $(versioned)
include $(o-iterator)

# Make symlinks in the build directory, because the versioned names might
# be referenced by a DT_NEEDED in another library.
define o-iterator-doit
$(objpfx)$o$($o-version): $(objpfx)$o; $$(make-link)
endef
object-suffixes-left := $(versioned)
include $(o-iterator)

generated += $(foreach o,$(versioned),$o$($o-version))

ifeq (,$($(subdir)-version))
define o-iterator-doit
$(inst_slibdir)/$o$($o-version): $(inst_slibdir)/$(o:.so=)-$(version).so \
				 $(+force);
	$$(make-shlib-link)
endef
object-suffixes-left := $(versioned)
include $(o-iterator)

define o-iterator-doit
$(inst_slibdir)/$(o:.so=)-$(version).so: $(objpfx)$o $(+force);
	$$(do-install-program)
endef
object-suffixes-left := $(versioned)
include $(o-iterator)
else
define o-iterator-doit
$(inst_slibdir)/$o$($o-version): \
  $(inst_slibdir)/$(o:.so=)-$($(subdir)-version).so $(+force);
	$$(make-shlib-link)
endef
object-suffixes-left := $(versioned)
include $(o-iterator)

define o-iterator-doit
$(inst_slibdir)/$(o:.so=)-$($(subdir)-version).so: $(objpfx)$o $(+force);
	$$(do-install-program)
endef
object-suffixes-left := $(versioned)
include $(o-iterator)
endif
endif

define do-install-so
$(do-install-program)
$(patsubst %,ln -s -f $(@F) $(@D)/$(patsubst %$*.so,%,$(<F))$(libprefix)$*.so,\
	   $(filter-out %.so,$@))
endef

so-versions := $(sort $(foreach so,$(install-lib.so),.so$($(so)-version)))
$(foreach v,$(so-versions),\
	  $(inst_slibdir)/lib$(libprefix)%$v): $(common-objpfx)lib%.so \
					       $(+force)
	$(do-install-so)
$(foreach v,$(so-versions),\
	  $(inst_slibdir)/$(libprefix)%$v): $(common-objpfx)%.so $(+force)
	$(do-install-so)
endif

ifdef install-bin
$(addprefix $(inst_bindir)/,$(install-bin)): \
    $(inst_bindir)/%: $(objpfx)% $(+force)
	$(do-install-program)
endif
ifdef install-rootsbin
$(addprefix $(inst_rootsbindir)/,$(install-rootsbin)): \
   $(inst_rootsbindir)/%: $(objpfx)% $(+force)
	$(do-install-program)
endif
ifdef install-sbin
$(addprefix $(inst_sbindir)/,$(install-sbin)): \
    $(inst_sbindir)/%: $(objpfx)% $(+force)
	$(do-install-program)
endif
ifdef install-lib
install-lib.a := $(filter lib%.a,$(install-lib))
install-lib-non.a := $(filter-out lib%.a,$(install-lib))
ifdef install-lib-non.a
$(addprefix $(inst_libdir)/$(libprefix),$(install-lib-non.a)): \
  $(inst_libdir)/$(libprefix)%: $(objpfx)% $(+force)
	$(do-install)
endif
ifdef install-lib.a
$(install-lib.a:lib%.a=$(inst_libdir)/lib$(libprefix)%.a): \
  $(inst_libdir)/lib$(libprefix)%.a: $(objpfx)lib%.a $(+force)
	$(do-install)
	$(patsubst %,$(RANLIB) $@,$(filter-out $(non-lib.a),$(<F)))
endif
endif
ifdef install-data
$(addprefix $(inst_datadir)/,$(install-data)): $(inst_datadir)/%: % $(+force)
	$(do-install)
endif
headers := $(strip $(headers))
ifdef headers
$(addprefix $(inst_includedir)/,$(headers)): $(inst_includedir)/%: % $(+force)
	$(do-install)
endif	# headers

.PHONY: install-bin-nosubdir install-rootsbin-nosubdir install-sbin-nosubdir \
	install-lib-nosubdir install-data-nosubdir install-headers-nosubdir
install-bin-nosubdir: $(addprefix $(inst_bindir)/,$(install-bin))
install-rootsbin-nosubdir: \
	$(addprefix $(inst_rootsbindir)/,$(install-rootsbin))
install-sbin-nosubdir: $(addprefix $(inst_sbindir)/,$(install-sbin))
install-lib-nosubdir: $(addprefix $(inst_libdir)/,\
		       $(patsubst lib%.a,lib$(libprefix)%.a,$(install-lib.a)) \
		       $(addprefix $(libprefix),$(install-lib-non.a)))
install-data-nosubdir: $(addprefix $(inst_datadir)/,$(install-data))
install-headers-nosubdir: $(addprefix $(inst_includedir)/,$(headers))
install-others-nosubdir: $(install-others)

# We need all the `-nosubdir' targets so that `install' in the parent
# doesn't depend on several things which each iterate over the subdirs.
# This rule makes `install-FOO' always use `install-FOO-nosubdir' as a
# subroutine.  Then in the parent `install-FOO' also causes subdir makes.
install-%:: install-%-nosubdir ;

.PHONY: install install-no-libc.a-nosubdir
ifeq ($(build-programs),yes)
install-no-libc.a-nosubdir: install-headers-nosubdir install-data-nosubdir\
			    install-bin-nosubdir install-lib-nosubdir	\
			    install-others-nosubdir install-rootsbin-nosubdir \
			    install-sbin-nosubdir
else
install-no-libc.a-nosubdir: install-headers-nosubdir install-data-nosubdir\
			    install-lib-nosubdir install-others-nosubdir
endif
install: install-no-libc.a-nosubdir

# Command to compile $< in $(objdir) using the native libraries.
define native-compile
$(make-target-directory)
$(patsubst %/,cd % &&,$(objpfx)) \
$(BUILD_CC) $($(basename $(<F))-CFLAGS) $(ALL_BUILD_CFLAGS) \
	    $(firstword $(filter /%,$< $(shell pwd)/$<)) -o $(@F)
endef

# Command to compile $< in $(common-objdir) using the native libraries.
# We must cd to $(objdir) anyway so that $(..)config.h is valid.
define common-objdir-compile
$(patsubst %/,cd % &&,$(objpfx)) \
$(BUILD_CC) $($(basename $(<F))-CFLAGS) $(ALL_BUILD_CFLAGS) \
	    $(firstword $(filter /%,$< $(shell pwd)/$<)) -o $(..)$(@F)
endef

# We always want to use configuration definitions.
# Note that this is only used for commands running in $(objpfx).
ALL_BUILD_CFLAGS = $(BUILD_CFLAGS) -include $(..)config.h

# Support the GNU standard name for this target.
.PHONY: check
check: tests

.PHONY: TAGS
TAGS: $(objpfx)distinfo $(..)MakeTAGS
	$(MAKE) $(addprefix -f ,$^) $@

$(..)po/%.pot: $(objpfx)distinfo $(..)MakeTAGS FORCE
	$(MAKE) $(addprefix -f ,$(filter-out FORCE,$^)) $@
FORCE:


.PHONY: echo-headers
echo-headers:
	@echo $(headers)


# Common cleaning targets.

.PHONY: common-mostlyclean common-clean mostlyclean clean
clean: common-clean
mostlyclean: common-mostlyclean

# Remove the object files.
common-mostlyclean:
	-rm -f $(addprefix $(objpfx),$(tests) $(test-srcs) $(others) \
				     $(tests-static) stubs \
				     $(addsuffix .o,$(tests) $(test-srcs) \
						    $(tests-static) \
						    $(others)) \
				     $(addsuffix .out,$(tests) $(test-srcs)) \
				     $(addsuffix .sout,$(tests-static)))
	-rm -f $(addprefix $(objpfx),$(extra-objs) $(install-lib) \
				     $(install-lib.so) \
				     $(install-lib.so:%.so=%_pic.a))
	-rm -f core
	$(rmobjs)
define rmobjs
$(foreach o,$(object-suffixes-for-libc),
-rm -f $(objpfx)stamp$o $(o-objects))
endef

# Also remove the dependencies and generated source files.
common-clean: common-mostlyclean
	-rm -f $(addprefix $(objpfx),$(generated)) $(+depfiles)
	-rm -fr $(addprefix $(objpfx),$(generated-dirs))
	-rm -f $(addprefix $(common-objpfx),$(common-generated))
	-rm -f $(objpfx)distinfo

# Produce a file `stubs' which contains `#define __stub_FUNCTION'
# for each function which is a stub.  We grovel over all the .d files
# looking for references to <stub-tag.h>.  Then we grovel over each
# referenced source file to see what stub function it defines.

.PHONY: stubs # The parent Makefile calls this target.
stubs: $(objpfx)stubs
s = $(sysdep_dir)/generic
$(objpfx)stubs: $(+depfiles)
# Use /dev/null since `...` might expand to empty.
	(s=`cd $s && /bin/pwd`; \
	 $(patsubst %/,cd % &&,$(objpfx)) \
	 sed -n 's/^stub_warning *(\([^)]*\).*$$/#define __stub_\1/p' \
	  `sed -n -e '\@ $s/[^ ]*\.c@{; s@^.* $s/\([^ ]*\.c\).*$$@'"$$s"'/\1@; h; }' \
		-e '/stub-tag\.h/{; g; p; }' \
		  $(patsubst $(objpfx)%,%,$^) /dev/null` \
	     /dev/null) > $@T
	mv -f $@T $@

# Make the distribution tar file.

.PHONY: dist
dist: $(objpfx)distinfo $(..)Make-dist
	$(MAKE) -f $< -f $(word 2,$^) $(Make-dist-args)

# Avoid depending on source files found in sysdeps dirs,
# because the references affect implicit rule selection.
dist: $(filter-out %.c %.S %.s,$(distribute))

# We used to simply export all these variables, but that frequently made the
# environment get too large.  Instead, we write all the information into
# a generated makefile fragment `distinfo', and then include it with -f in
# the sub-make that makes the distribution (above).
$(objpfx)distinfo: Makefile $(..)Makerules \
		   $(wildcard $(foreach dir,$(sysdirs),$(dir)/Makefile))
	$(make-target-directory)
	$(distinfo-vars)
	mv -f $@.new $@
.PHONY: subdir_distinfo
subdir_distinfo: $(objpfx)distinfo

define distinfo-vars
rm -f $@.new
echo > $@.new 'subdir := $(subdir)'
$(foreach var,subdir-dirs sources elided-routines sysdep_routines \
	      headers sysdep_headers distribute dont_distribute generated \
	      others tests test-srcs extra-libs $(extra-libs:%=%-routines) \
	      $(extra-libs:%=%-map) versioned \
	      $(addprefix install-,lib lib.so data bin sbin others),
echo >> $@.new '$(subdir)-$(var) := $($(var))'
echo >> $@.new '$(var) = $$($(subdir)-$(var))')
endef

ifneq (,$(strip $(gpl2lgpl)))
ifneq (,$(wildcard $(..)gpl2lgpl.sed))
# Snarf from the master source and frob the copying notice.
$(gpl2lgpl): %: $(..)gpl2lgpl.sed /home/gd/gnu/lib/%
	sed -f $^ > $@-tmp
# So I don't edit them by mistake.
	chmod a-w $@-tmp
	mv -f $@-tmp $@
ifeq ($(with-cvs),yes)
	test ! -d CVS || cvs $(CVSOPTS) commit -m'Updated from $^' $@
endif
endif
endif