SHLIB	= libc.so.5
TSLIB	= libfred.so.1
include ../Mconfig

all:	$(SHLIB)

$(SHLIB): Infiles mcc-misc.o libc/cmp.o libgcc/_eh.o
	gcc -shared -Wl,-soname,$(SHLIB) -o $(SHLIB) `cat Infiles` mcc-misc.o
	chmod 555 $(SHLIB)

test:	$(TSLIB)

mcc-misc.o: mcc-misc.c
	gcc -O6 -fomit-frame-pointer -fPIC -c $<

$(TSLIB): Infiles mcc-misc.o libc/cmp.o libgcc/_eh.o
	rm -f $(TSLIB)
	gcc -shared -Wl,-soname,$(TSLIB) -o $(TSLIB) `cat Infiles` mcc-misc.o

combine: combine.c
	gcc -s -o $@ $<

new:	F $(TSLIB)
	for i in `cat F`;do \
	    /usr/i486-linux/bin/ld -m elf_i386 -o new/$$i \
		-dynamic-linker /lib/ld-linux.so.1 /usr/lib/crt1.o \
		/usr/lib/crti.o /usr/lib/crtbegin.o old/$$i ./$(TSLIB) \
		/usr/lib/gcc-lib/i486-linux/$(GCC)/libgcc.a \
		/usr/lib/crtend.o /usr/lib/crtn.o; \
	done

libcs:	combine libc/cmp.o
	ar cru libc.a libc/*.o
	nm libc.a|./combine >$@
	rm -f libc.a

libgccs: combine libgcc/_eh.o
	ar cru libgcc.a libgcc/*.o
	nm libgcc.a|./combine >$@
	rm -f libgcc.a

F:
	(cd old;ls *.o) >F

loop:	c F mcc-misc.o libcs libgccs
	cat Infiles c|sort -u >I
	mv I Infiles
	rm -f libc.a new/*
	ar cru libc.a `cat Infiles`
	for i in `cat F`;do \
		ld -r -x old/$$i libc.a mcc-misc.o -o new/$$i;done
	for i in new/*;do nm $$i;done|egrep ' U '|sed -e 's/.*U //'|sort -u >b
	while read i;do egrep " $$i\$$" libcs libgccs;done <b >c

startloop: F libcs libgccs
	mkdir -p new
	for i in `cat F`;do cp -p old/$$i new;done
	for i in new/*;do nm $$i;done|egrep ' U '|sed -e 's/.*U //'|sort -u >b
	while read i;do egrep " $$i\$$" libcs libgccs;done <b >c

install: $(SHLIB)
	mkdir -p ../root/Originals/disk/lib
	install -m 555 $< ../root/Originals/disk/lib/$(SHLIB)

clean:	force
	rm -rf F old new *.o combine b c

clobber: clean
	rm -rf $(TSLIB) $(SHLIB) libcs libgccs libc libgcc libc.a

.PHONY:	force new
