# Makefile to hang the C-library together.
#  "make" with no argument updates all out-of-date modules.
#  "make order" explodes the library and rearchives everything after working
#    out what order the modules should be in.
#
C = /lib/libc.a

all:	startup runtime general system password standardio
	chown bin $C
	chmod 644 $C

startup:
	cd csu ; $(MAKE)

runtime:
	cd crt ; $(MAKE)

general:
	cd gen ; $(MAKE)

system:
	cd sys ; $(MAKE)

password:
	cd pwr ; $(MAKE)

standardio:
	cd stdio ; $(MAKE)

order:
	mkdir x ; cd x ; make -f ../Makefile orderX
	rmdir x

#	Unfortunately, some hand-crafting of the C library order is necessary.
#	"cuexit" calls "_cleanup" prior to exitting. Programs that use stdio
#	will have included "flsbuf", which will have defined "_cleanup".
#	A dummy "cleanup" is supplied in "fakcu" for those programs that
#	do not use stdio. Thus the final library order of these routines must
#	be "flsbuf", "cuexit", and "fakcu". Similarly, "doprnt" calls routines
#	that live in "fltpr" to print out floating-point numbers. To reduce
#	the size of programs that do not use floating-point numbers, dummy
#	routines are provided in "ffltpr" - hence the order must be "fltpr",
#	"doprnt", and "ffltpr". As "fltpr" declares the dummy symbol "fltused",
#	it is also necessary to remove the dependence of "ecvt" and "gcvt"
#	on "fltpr".
#
orderX:
	ar x $C
	echo "fakcu.o cuexit.o" > OUT
	echo "cuexit.o flsbuf.o" >> OUT
	echo "ffltpr.o doprnt.o" >> OUT
	echo "doprnt.o fltpr.o" >> OUT
	echo "ecvt.o fltpr.o" >> OUT
	echo "gcvt.o fltpr.o" >> OUT
	sort OUT -o OUT
	echo "cuexit.o fakcu.o"  > IN
	echo "flsbuf.o cuexit.o" >> IN
	echo "doprnt.o ffltpr.o" >> IN
	echo "fltpr.o doprnt.o" >> IN
	ar r LIB `lorder *.o | sort -u | comm -23 - OUT | cat IN - | tsort`
	install LIB bin 644 $C
	rm *
