(Dynamic Allocation and C): Say global variables are like static.
[glibc.git] / Makerules
blob49dd4cbb26675a76e3a3682d596c1036ea64664a
1 # Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
2 # This file is part of the GNU C Library.
4 # The GNU C Library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Library General Public License as
6 # published by the Free Software Foundation; either version 2 of the
7 # License, or (at your option) any later version.
9 # The GNU C Library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Library General Public License for more details.
14 # You should have received a copy of the GNU Library General Public
15 # License along with the GNU C Library; see the file COPYING.LIB.  If
16 # not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 # Cambridge, MA 02139, USA.
20 #       Common rules for making the GNU C library.  This file is included
21 #       by the top-level Makefile and by all subdirectory makefiles
22 #       (through Rules).
24 ifneq (,)
25 This makefile requires GNU Make.
26 endif
29 ifdef   subdir
30 ..      := ../
31 endif   # subdir
33 # If `sources' was defined by the parent makefile, undefine it so
34 # we will later get it from wildcard search in this directory.
35 ifneq   "$(findstring env,$(origin sources))" ""
36 sources :=
37 endif
39 headers := $(headers) $(sysdep_headers)
41 oPATH := $(PATH)
42 PATH := this definition should take precedence over $(oPATH)
43 ifeq ($(PATH),$(oPATH))
44 You must not use the -e flag when building the GNU C library.
45 else
46 PATH := $(oPATH)
47 endif
49 ifndef +included-Makeconfig
50 include $(..)Makeconfig
51 endif
53 # `configure' writes a definition of `config-sysdirs' in `config.make'.
54 sysdirs = $(config-sysdirs)
56 +sysdir_pfx = $(common-objpfx)
58 export sysdirs := $(sysdirs)
60 +sysdep_dirs := $(addprefix $(sysdep_dir)/,$(sysdirs))
61 ifdef objdir
62 +sysdep_dirs := $(objdir) $(+sysdep_dirs)
63 endif
65 # Add -I switches to get the right sysdep directories.
66 # `+includes' in Makeconfig references $(+sysdep-includes).
67 +sysdep-includes := $(addprefix -I,$(+sysdep_dirs))
69 # Include any system-specific makefiles.
71 # This is here so things in sysdep Makefiles can easily depend on foo.h as
72 # appropriate and not worry about where foo.h comes from, which may be
73 # system dependent and not known by that Makefile.
74 vpath %.h $(subst $(empty) ,:,$(strip $(common-objpfx) \
75                                       $(addprefix $(sysdep_dir)/,$(sysdirs)) \
76                                       $(..)))
78 ifeq ($(wildcard $(+sysdir_pfx)sysd-Makefile),)
79 # Don't do deps until this exists, because it might change the sources list.
80 no_deps=t
81 endif
83 # Some sysdep makefiles use this to distinguish being included here from
84 # being included individually by a subdir makefile (hurd/Makefile needs this).
85 in-Makerules := yes
87 include $(+sysdir_pfx)sysd-Makefile
88 $(+sysdir_pfx)sysd-Makefile: $(+sysdir_pfx)config.make $(..)Makerules
89         -@rm -f $@T
90         (for dir in $(sysdirs); do                                            \
91            file=sysdeps/$$dir/Makefile;                                       \
92            if [ -f $(..)$$file ]; then                                        \
93              echo include "\$$(..)$$file";                                    \
94            else true; fi;                                                     \
95          done; \
96          echo 'sysd-Makefile-done=t') > $@T
97         mv -f $@T $@
100 # Reorder before-compile so that mach things come first, and hurd things
101 # second, before all else.  The mach and hurd subdirectories have many
102 # generated header files which the much of rest of the library depends on,
103 # so it is best to build them first (and mach before hurd, at that).
104 before-compile := $(filter $(common-objpfx)mach% $(common-objpfx)hurd%,\
105                            $(before-compile)) \
106                   $(filter-out $(common-objpfx)mach% $(common-objpfx)hurd%,\
107                                $(before-compile))
109 # Remove existing files from `before-compile'.  Things are added there when
110 # they must exist for dependency generation to work right, but once they
111 # exist there is no further need for every single file to depend on them,
112 # and those gratuitous dependencies result in many gratuitous
113 # recompilations.
114 before-compile := $(filter-out $(wildcard $(before-compile)),$(before-compile))
116 # Don't let any before-compile file be an intermediate and get removed.
117 ifdef before-compile
118 $(before-compile):
119 endif
121 # Generate an ordered list of implicit rules which find the source files in
122 # each sysdep directory.  The old method was to use vpath to search all the
123 # sysdep directories.  However, that had the problem that a .S file in a
124 # later directory would be chosen over a .c file in an earlier directory,
125 # which does not preserve the desired sysdeps ordering behavior.
127 # When making the list of .d files to include, we can't know which ones
128 # have source in .s files, and thus do not in fact need a .d file.
129 # So we must define rules to make .d files for .s files.
130 define make-dummy-dep
131 $(addprefix ln $(objpfx)dummy.d ,$(filter-out $(wildcard $@),$@))
132 endef
133 $(objpfx)dummy.d:
134         echo '# .s files cannot contain includes, so they need no deps.' > $@
136 # It matters that this set of rules, for compiling from sources in
137 # the current directory (the $srcdir/$subdir) come before the
138 # generated sysdep rules in included from sysd-rules below.  When
139 # compiling in the source tree, generated sources go into the current
140 # directory, and those should be chosen before any sources in sysdeps.
141 $(objpfx)%.o: %.S $(before-compile); $(compile-command.S)
142 $(objpfx)%.d: %.S $(before-compile); $(+make-deps)
143 $(objpfx)%.o: %.s $(before-compile); $(compile-command.s)
144 $(objpfx)%.d: %.s $(objpfx)dummy.d; $(make-dummy-dep)
145 $(objpfx)%.o: %.c $(before-compile); $(compile-command.c)
146 $(objpfx)%.d: %.c $(before-compile); $(+make-deps)
148 # Omit the objpfx rules when building in the source tree, because
149 # objpfx is empty and so these rules just override the ones above.
150 ifdef objpfx
151 # Define first rules to find the source files in $(objpfx).
152 # Generated source files will end up there.
153 $(objpfx)%.o: $(objpfx)%.S $(before-compile); $(compile-command.S)
154 $(objpfx)%.d: $(objpfx)%.S $(before-compile); $(+make-deps)
155 $(objpfx)%.o: $(objpfx)%.s $(before-compile); $(compile-command.s)
156 $(objpfx)%.d: $(objpfx)%.s $(objpfx)dummy.d; $(make-dummy-dep)
157 $(objpfx)%.o: $(objpfx)%.c $(before-compile); $(compile-command.c)
158 $(objpfx)%.d: $(objpfx)%.c $(before-compile); $(+make-deps)
159 endif
161 # System-dependent makefiles can put in `inhibit-sysdep-asm' wildcard
162 # patterns matching sysdep directories whose assembly source files should
163 # be suppressed.
164 ifdef inhibit-sysdep-asm
165 define open-check-inhibit-asm
166 case $$sysdir in $(subst $(empty) ,|,$(inhibit-sysdep-asm))) : ;; *)
167 endef
168 close-check-inhibit-asm = ;; esac
169 endif
171 # Don't include sysd-rules until sysd-Makefile is already there and has been
172 # included.  It might define inhibit-sysdep-asm, which would affect the
173 # contents of sysd-rules.
174 ifdef sysd-Makefile-done
175 include $(+sysdir_pfx)sysd-rules
176 endif
177 $(+sysdir_pfx)sysd-rules: $(+sysdir_pfx)config.make $(..)Makerules \
178                           $(wildcard $(foreach dir,$(sysdirs),\
179                                                $(sysdep_dir)/$(dir)/Makefile))
180         -@rm -f $@T
181         (for sysdir in $(sysdirs); do                                         \
182            dir="\$$(sysdep_dir)/$$sysdir";                                    \
183            $(open-check-inhibit-asm) \
184            echo "\$$(objpfx)%.o: $$dir/%.S \$$(before-compile); \
185                 \$$(compile-command.S)";                                      \
186            echo "\$$(objpfx)%.d: $$dir/%.S \$$(before-compile); \
187                 \$$(+make-deps)";                                             \
188            echo "\$$(objpfx)%.o: $$dir/%.s \$$(before-compile); \
189                 \$$(compile-command.s)";                                      \
190            echo "\$$(objpfx)%.d: $$dir/%.s \$$(objpfx)dummy.d; \
191                 \$$(make-dummy-dep)" $(close-check-inhibit-asm);        \
192            echo "\$$(objpfx)%.o: $$dir/%.c \$$(before-compile); \
193                 \$$(compile-command.c)";                                      \
194            echo "\$$(objpfx)%.d: $$dir/%.c \$$(before-compile); \
195                 \$$(+make-deps)";                                             \
196          done) > $@T
197         mv -f $@T $@
199 ifndef compile-command.S
200 compile-command.S = $(compile.S) $(OUTPUT_OPTION)
201 endif
202 ifndef compile-command.s
203 compile-command.s = $(COMPILE.s) $< $(OUTPUT_OPTION)
204 endif
205 ifndef compile-command.c
206 compile-command.c = $(compile.c) $(OUTPUT_OPTION)
207 endif
209 ifeq ($(notdir $(firstword $(CC))),gcc)
210 # GCC can grok options after the file name, and it looks nicer that way.
211 compile.S = $(CC) $< -c $(CPPFLAGS) $(asm-CPPFLAGS)
212 compile.c = $(CC) $< -c $(CFLAGS) $(CPPFLAGS)
213 else
214 compile.S = $(COMPILE.S) $(asm-CPPFLAGS) $<
215 compile.c = $(COMPILE.c) $<
216 endif
218 ifndef OUTPUT_OPTION
219 ifdef objpfx
220 # We need this for the output to go in the right place.  It will default to
221 # empty if make was configured to work with a cc that can't grok -c and -o
222 # together.  You can't compile the C library with such a compiler.
223 OUTPUT_OPTION = -o $@
224 endif
225 endif
227 S-CPPFLAGS = $(asm-CPPFLAGS)
228 define +make-deps
229 -@rm -f $@
230 $(+mkdep) $< $(CPPFLAGS) $($(<:$*.%=%)-CPPFLAGS) | \
231 sed -e 's,$*\.o,$(@:.d=.o) $@,' $(sed-remove-objpfx) > $(@:.d=.T)
232 mv -f $(@:.d=.T) $@
233 endef
234 ifneq (,$(objpfx))
235 sed-remove-objpfx = -e 's@ $(subst @,\@,$(objpfx))@ $$(objpfx)@g' \
236                     -e 's@^$(subst @,\@,$(objpfx))@$$(objpfx)@g'
237 endif
239 # Figure out the source filenames in this directory.
241 override sources        := $(addsuffix .c,$(routines) $(aux) \
242                                           $(sysdep_routines))
243 sysdep_routines := $(sysdep_routines)
245 # This is the list of all object files, gotten by
246 # replacing every ".c" in `sources' with a ".o".
247 override objects        := $(addprefix $(objpfx),$(sources:.c=.o))
249 +depfiles := $(strip $(sources:.c=.d) \
250                      $(patsubst %.o,%.d,$(filter %.o,$(extra-objs))) \
251                      $(addsuffix .d,$(others) $(tests)))
252 +depfiles := $(addprefix $(objpfx),\
253                          $(filter-out $(addsuffix .d,$(omit-deps)),\
254                                       $(+depfiles)))
256 $(objpfx)depend-$(subdir): Makefile
257 ifdef +depfiles
258         for file in $(+depfiles:$(objpfx)%=%); do \
259           echo "include \$$(objpfx)$$file";     \
260         done > $@-tmp
261         mv -f $@-tmp $@
262 else
263         cp /dev/null $@
264 endif
266 ifneq ($(no_deps),t)
267 # Include the generated dependencies of the sources in this directory.
268 include $(objpfx)depend-$(subdir)
269 endif
270 \f\f
271 # Maximize efficiency by minimizing the number of rules.
272 .SUFFIXES:      # Clear the suffix list.
273 # Add the suffixes we use.
274 .SUFFIXES: .a .o .S .s .c .h .d
276 # Generic rule for making directories.
278 # mkdir isn't smart enough to strip a trailing /.
279         mkdir $(@:%/=%)
281 # Make sure that object files are not removed
282 # when they are intermediates between sources and library members.
283 # This can go away with make v4.
284 .PRECIOUS: %.o
286 # Make sure that the parent library archive is never removed.
287 .PRECIOUS: $(libc.a)
289 # Use the verbose option of ar and tar when not running silently.
290 ifeq    "$(findstring s,$(MAKEFLAGS))" ""       # if not -s
291 verbose := v
292 else                                            # -s
293 verbose :=
294 endif                                           # not -s
296 ARFLAGS := r$(verbose)
298 # This makes all the object files in the parent library archive.
300 .PHONY: lib libobjs lib-noranlib
301 $(libc.a): $(libc.a)(__.SYMDEF)
302 lib: $(libc.a)
304 +libobjs := $(patsubst %,$(libc.a)(%),$(notdir $(objects)))
305 libobjs: $(+libobjs)
306 lib-noranlib: libobjs
308 # Define a pattern rule that will match many targets libc.%(foo.o), for
309 # each foo in $(objects) (% will just happen always to match `a').  This is
310 # the only way to define a rule that updates many targets at once with one
311 # sequence of commands.
312 ifdef objects
313 $(patsubst %,$(libc.a:a=)\%(%),$(notdir $(objects))): $(objpfx)stamp-$(subdir);
314 $(objpfx)stamp-$(subdir): $(objects)
315 #       $(+libc_lock_open)
316 ifdef objdir
317         cd $(objdir); $(AR) cru$(verbose) libc.a $(patsubst $(objpfx)%,%,$^)
318 else
319         $(AR) cru$(verbose) $(..)libc.a $^
320 endif
321 #       $(+libc_lock_close)
322         touch $@
323 endif
325 $(libc.a)(__.SYMDEF): $(+libobjs)
326         $(RANLIB) $@
328 define +libc_lock_open
329 @. $(..)libc-lock-open
330 endef
331 define +libc_lock_close
332 @rm -f $(..)LOCK-libc.a
333 endef
335 # This makes all the object files.
336 .PHONY: objects objs
337 objects objs: $(objects)
339 # Installation.
341 # $(install-lib) are installed from the object directory into $(libdir);
342 # files in $(install-lib) matching `lib%.a' are ranlib'd after installation
343 # unless they also appear in $(non-lib.a).  $(install-data) are installed
344 # as they are into $(datadir).  $(headers) are installed as they are in
345 # $(includedir).  $(install-bin) and $(install-sbin) are installed from the
346 # object directory into $(bindir) and $(sbindir), respectively.
347 # $(install-others) are absolute path names of files to install; rules to
348 # install them are defined elsewhere.
350 # The simple library name to install libc.a under.
351 # This could be defined by a sysdep Makefile.
352 ifndef libc-name
353 libc-name := c
354 endif
356 define do-install
357 $(make-target-directory)
358 $(INSTALL_DATA) $< $@
359 endef
361 # Make the target directory if it doesn't exist.  Because of make's
362 # directory cache, this will produce the `mkdir' command when the directory
363 # already exists, if it didn't exist at the start of the `make' run.  The
364 # `-' prefix ignores errors from mkdir, so there will just be some
365 # gratuitous but harmless `File exists' messages.
366 define make-target-directory
367 $(addprefix -mkdir ,$(filter-out $(wildcard $(@D:%/=%)),$(@D:%/=%)))
368 endef
370 # Any directory (parent or subdir) that has any object files to build
371 # should install libc.a; this way "make install" in a subdir is guaranteed
372 # to install everything it changes.
373 ifdef objects
374 install: $(libdir)/lib$(libprefix)$(libc-name).a
375 # We avoid depending on lib-noranlib because that makes the parent make
376 # subdir_lib in all the subdirs, when the make install run they do will
377 # update the library anyway.  Running ranlib after installing makes the
378 # __.SYMDEF time stamp up to date, which avoids messages from some linkers.
379 # Depending on subdir_install gets all the subdirs to update the library,
380 # and is optimal for `make install' at top level.
381 $(libdir)/lib$(libprefix)$(libc-name).a: libobjs subdir_install
382         $(make-target-directory)
383         $(INSTALL_DATA) $(libc.a) $@
384         $(RANLIB) $@
385 endif
387 ifdef install-bin
388 $(addprefix $(bindir)/,$(install-bin)): $(bindir)/%: $(objpfx)%
389         $(make-target-directory)
390         $(INSTALL_PROGRAM) $< $@
391 endif
392 ifdef install-sbin
393 $(addprefix $(sbindir)/,$(install-sbin)): $(sbindir)/%: $(objpfx)%
394         $(make-target-directory)
395         $(INSTALL_PROGRAM) $< $@
396 endif
397 ifdef install-lib
398 install-lib.a := $(filter-out $(non-lib.a),$(filter lib%.a,$(install-lib)))
399 install-lib-non.a := $(filter-out lib%.a,$(install-lib)) $(non-lib.a)
400 ifdef install-lib-non.a
401 $(addprefix $(libdir)/$(libprefix),$(install-lib-non.a)): \
402   $(libdir)/$(libprefix)%: $(objpfx)%
403         $(do-install)
404 endif
405 ifdef install-lib.a
406 $(install-lib.a:lib%.a=$(libdir)/lib$(libprefix)%.a): \
407   $(libdir)/lib$(libprefix)%.a: $(objpfx)lib%.a
408         $(do-install)
409         $(RANLIB) $@
410 endif
411 endif
412 ifdef install-data
413 $(addprefix $(datadir)/,$(install-data)): $(datadir)/%: %;$(do-install)
414 endif
415 headers := $(strip $(headers))
416 ifdef headers
417 $(addprefix $(includedir)/,$(headers)): \
418         $(includedir)/%: %;$(do-install)
419 endif   # headers
421 .PHONY: install-bin-nosubdir install-sbin-nosubdir install-lib-nosubdir \
422         install-data-nosubdir install-headers-nosubdir
423 install-bin-nosubdir: $(addprefix $(bindir)/,$(install-bin))
424 install-sbin-nosubdir: $(addprefix $(sbindir)/,$(install-sbin))
425 install-lib-nosubdir: $(addprefix $(libdir)/,\
426                        $(patsubst lib%.a,lib$(libprefix)%.a,$(install-lib.a)) \
427                        $(addprefix $(libprefix),$(install-lib-non.a)))
428 install-data-nosubdir: $(addprefix $(datadir)/,$(install-data))
429 install-headers-nosubdir: $(addprefix $(includedir)/,$(headers))
430 install-others-nosubdir: $(install-others)
432 # We need all the `-nosubdir' targets so that `install' in the parent
433 # doesn't depend on several things which each iterate over the subdirs.
434 # This rule makes `install-FOO' always use `install-FOO-nosubdir' as a
435 # subroutine.  Then in the parent `install-FOO' also causes subdir makes.
436 install-%:: install-%-nosubdir ;
438 .PHONY: install install-no-libc.a-nosubdir
439 install-no-libc.a-nosubdir: install-headers-nosubdir install-data-nosubdir\
440                             install-bin-nosubdir install-lib-nosubdir   \
441                             install-others-nosubdir install-sbin-nosubdir
442 install: install-no-libc.a-nosubdir
444 # Command to compile $< in $(objdir) using the native libraries.
445 define native-compile
446 cwd=`pwd`; cd $(@D); $(BUILD_CC) $(BUILD_CFLAGS) \
447            $(addprefix $$cwd/,$^) -o $(@F)
448 endef
450 # Command to compile $< in $(common-objdir) using the native libraries.
451 define common-objdir-compile
452 cd $(@D); $(BUILD_CC) $(BUILD_CFLAGS) $(<:$(common-objpfx)%=%) -o $(@F)
453 endef
455 # We always want to use configuration -D switches.
456 BUILD_CFLAGS = $(config-defines)
458 # Support the GNU standard name for this target.
459 .PHONY: check
460 check: tests
462 .PHONY: TAGS
463 TAGS: distinfo $(..)MakeTAGS
464         $(MAKE) $(addprefix -f ,$^)
466 .PHONY: echo-headers
467 echo-headers:
468         @echo $(headers)
471 # Common cleaning targets.
473 .PHONY: common-mostlyclean common-clean mostlyclean clean
474 clean: common-clean
475 mostlyclean: common-mostlyclean
477 # Remove the object files.
478 common-mostlyclean:
479         -rm -f $(addprefix $(objpfx),$(tests) $(others) \
480                                      $(addsuffix .o,$(tests) $(others)))
481         -rm -f $(objects) $(addprefix $(objpfx),$(extra-objs) stamp-$(subdir))
482         -rm -f core TAGS
484 # Also remove the dependencies and generated source files.
485 common-clean: common-mostlyclean
486         -rm -f $(objpfx)depend-$(subdir) $(+depfiles)
487         -rm -f $(addprefix $(objpfx),$(generated))
489 # Produce a file `stub-$(subdir)' which contains `#define __stub_FUNCTION'
490 # for each function which is a stub.  We grovel over all the .d files
491 # looking for references to source files in sysdeps/stub.  Then we grovel
492 # over each referenced source file to see what stub function it defines.
494 .PHONY: stubs # The parent Makefile calls this target.
495 stubs: $(common-objpfx)stub-$(subdir)
496 $(common-objpfx)stub-$(subdir): $(+depfiles)
497 # Use /dev/null since `...` might expand to empty.
498         sed -n 's/^stub_warning *(\([^)]*\).*$$/#define __stub_\1/p' \
499             `sed -n 's@^.*\($(sysdep_dir)/stub/[a-z0-9_]*\.c\).*$$@\1@p' \
500                  $^ /dev/null` \
501             /dev/null > $@T
502         mv -f $@T $@
504 # Make the distribution tar file.
506 .PHONY: dist
507 dist: distinfo $(..)Make-dist 
508         $(MAKE) -f $< -f $(word 2,$^) $(Make-dist-args)
510 dist: $(distribute)
512 # We used to simply export all these variables, but that frequently made the
513 # environment get too large.  Instead, we write all the information into
514 # a generated makefile fragment `distinfo', and then include it with -f in
515 # the sub-make that makes the distribution (above).
516 distinfo: Makefile $(..)Makerules
517         $(distinfo-vars)
518         mv -f $@.new $@
520 define distinfo-vars
521 rm -f $@.new
522 $(foreach var,subdir sources elided-routines headers distribute \
523               dont_distribute generated others tests,
524 echo >> $@.new '$(var)  := $($(var))')
525 echo >> $@.new 'sources := $$(sources) $$(addsuffix .c,$$(elided-routines))'
526 endef
528 ifneq (,$(strip $(gpl2lgpl)))
529 ifneq (,$(wildcard $(..)gpl2lgpl.sed))
530 # Snarf from the master source and frob the copying notice.
531 $(gpl2lgpl): %: $(..)gpl2lgpl.sed /home/gd/gnu/lib/%
532         sed -f $^ > $@-tmp
533 # So I don't edit them by mistake.
534         chmod a-w $@-tmp
535         mv -f $@-tmp $@
536 endif
537 endif