# Copyright (C) 1991, 1992 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

# In make v4, we'll be able to really do this right (or something like that).
ifeq (,)
up-one = $(patsubst %/,%,$(dir $0))
parents-of = $(foreach x,$0,$(notdir $x) \
			      $(ifeq $(dir $x),./,$(parents-of $(up-one $x))))
# Finish this someday....
endif

ifdef  objdir
+sysdir_pfx = $(objpfx)
else
+sysdir_pfx = $(..)
endif

# Right now, however, we do it with shell scripts and lots of strangeness.
sysdep_dir := $(..)sysdeps
ifndef sysdirs
include $(+sysdir_pfx)sysdirs
define \n


endef
sysdirs := $(subst $(\n), ,$(sysdirs))
endif
$(+sysdir_pfx)sysdirs: $(+sysdir_pfx)Sysnames $(..)find-sysdirs
	(echo define sysdirs; \
	 $(dir $(word 2,$^))$(notdir $(word 2,$^)) < $<; \
	 echo endef) > $@
$(+sysdir_pfx)Sysnames:
	@(echo The GNU C Library has not been configured!	;\
	  echo Run \`configure\' to configure it before building.\
	 ) >&2; exit 1
export sysdirs := $(sysdirs)
export sysdep_dir := $(sysdep_dir)

# Turn the list of sysdep names into a colon-separated list of directories
# under SYSDEP_DIR (the base directory for sysdep files), and tell Make to
# search for C and header files in those directories.
+sysdep_dirs := $(addprefix $(sysdep_dir)/,$(sysdirs))
ifdef objdir
+sysdep_dirs := $(objdir) $(+sysdep_dirs)
endif
sysdep_path := $(subst $+ ,:,$(+sysdep_dirs))
ifeq (,)
override VPATH := $(sysdep_path)
else
vpath %.c $(sysdep_path)
vpath %.S $(sysdep_path)
vpath %.s $(sysdep_path)
vpath %.h $(sysdep_path)
endif

# Add -I switches to get the right sysdep directories.
+sysdep-includes := $(addprefix -I,$(+sysdep_dirs))
override CPPFLAGS := $(strip $(filter-out $(+sysdep-includes),$(CPPFLAGS)) \
			     $(+sysdep-includes))

# Include any system-specific makefiles.

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

include $(+sysdir_pfx)sysd-Makefile
$(+sysdir_pfx)sysd-Makefile: $(+sysdir_pfx)sysdirs
	(for dir in `cat $<`; do					      \
	   file=sysdeps/$$dir/Makefile;					      \
	   if [ -r $(..)$$file ]; then					      \
	     echo include "\$$(..)$$file";				      \
	   else true;							      \
	   fi;								      \
	 done) > $@

ifneq ($(no_deps),t)
# Include the generated dependencies of the sources in this directory.
include $(objpfx)depend-$(subdir)
endif

# Maximize efficiency by minimizing the number of rules.
.SUFFIXES:	# Clear the suffix list.
# Remove the RCS and SCCS rules.
%: %,v
%: RCS/%,v
%: s.%
%: SCCS/s.%

# Add the suffixes we use.
.SUFFIXES: .a .o .S .s .c .h .dep .m4

# 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.

ifndef .S-rule # Bletch.
$(objpfx)%.o: %.S $(before-compile)
	$(compile.S) $(OUTPUT_OPTION)
endif
$(objpfx)%.o: %.s $(before-compile)
	$(COMPILE.s) $< $(OUTPUT_OPTION)
$(objpfx)%.o: %.c $(before-compile)
	$(compile.c) $(OUTPUT_OPTION)

ifeq ($(notdir $(firstword $(CC))),gcc)
# GCC can grok options after the file name.
compile.S = $(CC) $< -c $(CPPFLAGS)
compile.c = $(CC) $< -c $(CFLAGS) $(CPPFLAGS)
else
compile.S = $(COMPILE.S) $<
compile.c = $(COMPILE.c) $<
endif

# This makes all the object files in the parent library archive.

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

+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)
#	$(+libc_lock_open)
ifdef objdir
	cd $(objdir); $(AR) ru$(verbose) libc.a $(patsubst $(objpfx)%,%,$^)
else
	$(AR) ru$(verbose) $(..)libc.a $^
endif
#	$(+libc_lock_close)



# 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 from the object directory header directories into includedir.
# install-others are absolute path names of files to install; rules to
# install them are defined elsewhere.

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

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
install: $(dir $(+install)) $(+install)

define +make-deps
$(+mkdep) $(CPPFLAGS) $< | sed 's,$*\.o,$(@:.dep=.o) $@,' > $@
endef

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

+depfiles := $(strip $(sources:.c=.dep) $(addsuffix .dep,$(others) $(tests)))
+depfiles := $(addprefix $(objpfx),$(+depfiles))
$(objpfx)depend-$(subdir): Makefile
ifdef +depfiles
	(for file in $(patsubst $(objpfx)%,%,$(+depfiles)); do	\
	 echo "include $(objpfx)$$file";	\
	 done) > $@
else
	cp /dev/null $@
endif

# Command to compile $< in $(objdir) using the native libraries.
native-compile = cwd=`pwd`; cd $(@D); $(CC) $$cwd/$< -o $(@F)

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

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

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


ifdef gpl2lgpl
ifneq (,$(wildcard /home/gd/gnu/lib/ChangeLog)
# Snarf from the master source and frob the copying notice.
$(gpl2lgpl): %: $(..)gpl2lgpl.sed /home/gd/gnu/lib/%
	sed -f $^ > $@-tmp
	mv $@-tmp $@
endif
endif
