# Copyright (C) 1991, 1992, 1993 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., 675 Mass Ave,
# Cambridge, MA 02139, USA.

#
#	Propagated rules for making the GNU C library.
#
ifneq (,)
This makefile requires GNU Make.
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

headers := $(headers) $(sysdep_headers)

# This is benign and useless in GNU make before version 3.63.
export sources := $(sources)
export headers := $(headers)

ifneq ($(findstring e,$(MAKEFLAGS)),)
You must not use the -e flag when building the GNU C library.
endif

ifndef +included-Makeconfig
include $(..)Makeconfig
endif

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

+sysdir_pfx = $(common-objpfx)

sysdep_dir := $(..)sysdeps
export sysdirs := $(sysdirs)
export sysdep_dir := $(sysdep_dir)

+sysdep_dirs := $(addprefix $(sysdep_dir)/,$(sysdirs))
ifdef objdir
+sysdep_dirs := $(objdir) $(+sysdep_dirs)
endif

# Add -I switches to get the right sysdep directories.
+sysdep-includes := $(addprefix -I,$(+sysdep_dirs))
+includes := $(+includes) $(+sysdep-includes)

# Include any system-specific makefiles.
# Also 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.

# 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 $(objpfx) \
				      $(addprefix $(sysdep_dir)/,$(sysdirs)) \
				      $(..)))

ifeq ($(wildcard $(+sysdir_pfx)sysd-Makefile),)
# Don't do deps until this exists, because it might change the sources list.
no_deps=t
endif

# When making the list of .dep files to include, we can't know which ones
# have source in .s files, and thus do not in fact need a .dep file.
# So we must define rules to make .dep files for .s files.
make-dummy-dep = \
	echo '\# $< cannot contain includes, so it needs no deps.' > $@

# Define first rules to find the source files in $(objpfx).
# Generated source files will end up there.
$(objpfx)%.o: $(objpfx)%.S $(before-compile); $(compile-command.S)
$(objpfx)%.dep: $(objpfx)%.S $(before-compile); $(+make-deps)
$(objpfx)%.o: $(objpfx)%.s $(before-compile); $(compile-command.s)
$(objpfx)%.dep: $(objpfx)%.s; $(make-dummy-dep)
$(objpfx)%.o: $(objpfx)%.c $(before-compile); $(compile-command.c)
$(objpfx)%.dep: $(objpfx)%.c $(before-compile); $(+make-deps)

include $(+sysdir_pfx)sysd-Makefile
$(+sysdir_pfx)sysd-Makefile: $(+sysdir_pfx)config.make $(..)Makerules
	(for dir in $(sysdirs); do		   			      \
	   file=sysdeps/$$dir/Makefile;					      \
	   if [ -f $(..)$$file ]; then					      \
	     echo include "\$$(..)$$file";				      \
	   else true;							      \
	   fi;								      \
	   dir="\$$(sysdep_dir)/$$dir";					      \
	   echo "\$$(objpfx)%.o: $$dir/%.S \$$(before-compile); \
		\$$(compile-command.S)";				      \
	   echo "\$$(objpfx)%.dep: $$dir/%.S \$$(before-compile); \
		\$$(+make-deps)";					      \
	   echo "\$$(objpfx)%.o: $$dir/%.s \$$(before-compile); \
		\$$(compile-command.s)";				      \
	   echo "\$$(objpfx)%.dep: $$dir/%.s; \$$(make-dummy-dep)";	      \
	   echo "\$$(objpfx)%.o: $$dir/%.c \$$(before-compile); \
		\$$(compile-command.c)";				      \
	   echo "\$$(objpfx)%.dep: $$dir/%.c \$$(before-compile); \
		\$$(+make-deps)";					      \
	 done) > $@

# Maximize efficiency by minimizing the number of rules.
.SUFFIXES:	# Clear the suffix list.
# Add the suffixes we use.
.SUFFIXES: .a .o .S .s .c .h .dep

# 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.
# This can go away with make v4.
.PRECIOUS: %.o

# Make sure that the parent library archive is never removed.
.PRECIOUS: $(libc.a)

# 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)

# Figure out the source filenames in this directory.

override sources	:= $(addsuffix .c,$(routines) $(aux) \
				          $(sysdep_routines))
export sysdep_routines := $(sysdep_routines)

# 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 order of these rules is important.

$(objpfx)%.o: %.S $(before-compile); $(compile-command.S)
$(objpfx)%.o: %.s $(before-compile); $(compile-command.s)
$(objpfx)%.o: %.c $(before-compile); $(compile-command.c)

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

ifeq ($(notdir $(firstword $(CC))),gcc)
# GCC can grok options after the file name.
compile.S = $(CC) $< -c $(CPPFLAGS) $(asm-CPPFLAGS)
compile.c = $(CC) $< -c $(CFLAGS) $(CPPFLAGS)
else
compile.S = $(COMPILE.S) $(asm-CPPFLAGS) $<
compile.c = $(COMPILE.c) $<
endif

ifndef OUTPUT_OPTION
ifdef objpfx
# 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 $@
endif
endif

# This makes all the object files in the parent library archive.

.PHONY: lib libobjs lib-noranlib
$(libc.a): lib-noranlib
	$(RANLIB) $@
lib: $(libc.a)

+libobjs := $(patsubst %,$(libc.a)(%),$(notdir $(objects)))
ifdef +libobjs
$(+libobjs): $(libc.a)(%): $(objpfx)%;
endif
libobjs: $(+libobjs)
lib-noranlib: libobjs ar-it

#$(libc.a): %: $(addprefix %,$(patsubst $(objpfx)%,(%),$(objects)))
#	$(AR) $(ARFLAGS) $@ $(patsubst $@(%),%,$?)

define +libc_lock_open
@. $(..)libc-lock-open
endef
define +libc_lock_close
@rm -f $(..)LOCK-libc.a
endef

# Temporary until I get a better solution.
.PHONY: ar-it
ar-it: $(objects)
ifdef objects
#	$(+libc_lock_open)
ifdef objdir
	cd $(objdir); $(AR) cru$(verbose) libc.a $(patsubst $(objpfx)%,%,$^)
else
	$(AR) cru$(verbose) $(..)libc.a $^
endif
#	$(+libc_lock_close)
endif



# This makes all the object files.
.PHONY: objects objs
objects objs: $(objects)

# Installation.

# install-lib are installed from the object directory into $(libdir).
# install-data are installed as they are into $(datadir).  headers are
# installed as they are in $(includedir).  install-others are absolute path
# names of files to install; rules to install them are defined elsewhere.

do-install = $(INSTALL_DATA) $< $@

# 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
install: $(libdir)/libc.a
$(libdir)/libc.a: $(libc.a) installdirs; $(do-install)
endif

ifdef install
$(addprefix $(bindir)/,$(install)): $(bindir)/%: $(objpfx)%
	$(INSTALL) $< $@
endif
ifdef install-lib
$(addprefix $(libdir)/,$(install-lib)): $(libdir)/%: $(objpfx)%;$(do-install)
endif
ifdef install-data
$(addprefix $(datadir)/,$(install-data)): $(datadir)/%: %;$(do-install)
endif
+install := $(addprefix $(libdir)/,$(install-lib))	\
	    $(addprefix $(datadir)/,$(install-data))	\
	    $(addprefix $(bindir)/,$(install))		\
	    $(install-others)
headers := $(strip $(headers))
ifdef headers
$(addprefix $(includedir)/,$(headers)): \
	$(includedir)/%: %;$(do-install)
+install := $(+install) $(addprefix $(includedir)/,$(headers))
endif	# headers
+install := $(filter-out $(addprefix %/,$(no-install)),$(+install))

.PHONY: install installdirs install-no-libc.a
install-no-libc.a: $(+install)
install: install-no-libc.a
$(+install): installdirs
installdirs: $(..)mkinstalldirs
	$(dir $<)$(notdir $<) $(sort $(dir $(+install)))

S-CPPFLAGS = $(asm-CPPFLAGS)
+make-deps = $(+mkdep) $< $(CPPFLAGS) $($(<:$*.%=%)-CPPFLAGS) | \
	     sed $(sed-remove-objpfx) -e 's,$*\.o,$(@:.dep=.o) $@,' > $@
ifneq (,$(objpfx))
sed-remove-objpfx = -e 's,$(objpfx),$$(objpfx),g'
endif

# N.B.: The order of these two rules is important.
$(objpfx)%.dep: %.S $(before-compile)
	$(+make-deps)
$(objpfx)%.dep: %.c $(before-compile)
	$(+make-deps)
$(objpfx)%.dep: %.s
	$(make-dummy-dep)

+depfiles := $(strip $(sources:.c=.dep) $(addsuffix .dep,$(others) $(tests)))
+depfiles := $(addprefix $(objpfx),$(+depfiles))

$(objpfx)depend-$(subdir): Makefile
ifdef +depfiles
# This used to simply use $(+depfiles) on the command line, but that tended
# to overflow the arg limit on USG systems.  Since `sources' is in the
# environment anyway, we do some shell processing on it instead.
	for file in $(patsubst %,%.dep,$(others) $(tests)) \
		    `echo $${sources} | sed 's/\.c/.dep/g'`; do \
	  echo "include \$$(objpfx)$$file";	\
	done > $@
else
	cp /dev/null $@
endif

ifneq ($(no_deps),t)
# Include the generated dependencies of the sources in this directory.
ifdef have.make.that.groks.multiple.includes
# Avoid the extra file and waiting for it, since we can.
+depfiles := $(strip $(+depfiles))
ifdef +depfiles
include $(+depfiles)
endif
else
include $(objpfx)depend-$(subdir)
endif
endif

# Command to compile $< in $(objdir) using the native libraries.
native-compile = cwd=`pwd`; cd $(@D); $(CC) $(native-CFLAGS) \
					    $(addprefix $$cwd/,$^) -o $(@F)

# Command to compile $< in $(common-objdir) using the native libraries.
define common-objdir-compile
cd $(@D); $(CC) $(native-CFLAGS) $(<:$(common-objpfx)%=%) -o $(@F)
endef

# We always want to use configuration -D switches.
native-CFLAGS = $(config-defines)

# Support the GNU standard name for this target.
.PHONY: check
check: tests

.PHONY: TAGS
TAGS: $(..)MakeTAGS
	$(MAKE) -f $< $@

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


# Produce a file `stub-$(subdir)' which contains `#define __stub_FUNCTION'
# for each function which is a stub.  We grovel over all the .dep files
# looking for references to source files in sysdeps/stub.  Then we grovel
# over each referenced source file to see what stub function it defines.

.PHONY: stubs # The parent Makefile calls this target.
stubs: $(common-objpfx)stub-$(subdir)
$(common-objpfx)stub-$(subdir): $(+depfiles)
# Use /dev/null since `...` might expand to empty.
	sed -n 's/^stub_warning *(\([^)]*\).*$$/#define __stub_\1/p' \
	    `sed -n 's@^.*\($(sysdep_dir)/stub/[a-z0-9_]*\.c\).*$$@\1@p' \
		 $^ /dev/null` \
	    /dev/null > $@T
	mv $@T $@

ifdef 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 $@
endif
endif
