#sccs	"@(#)shlib:Makefile	1.14"


include $(MAKEINC)/Makepre.h

ERRLST = $(SRC)/LIB/libc/port/gen/errlst.c
CTYPE = $(SRC)/LIB/libc/port/gen/ctype.c
LIBC= $(LIB)/libc.a
LIBTAM= /$(USRLIB)/libtam.a
DEPENDLIST = \
	align.c \
	$(ERRLST) \
	getopt.c \
	malloc.c \
	shlbat.c

GREPS= ifile.0413s shlib.t shlib.d _iob _iob2 _ctype _sobuf _timezone _tzname \
	_patblack _patwhite _patgray _patltgray _errlst _bufendt _buf2end \
	_tbase _dbase _sibuf

all: Makeincludes shlib.ifile  crt0s.o

Makeincludes: $(DEPENDLIST)
	$(INCCMD) $(DEPENDLIST) | $(SORT) | $(UNIQ) | $(FILTER) > Makeincludes
	$(MAKE)

includes: $(DEPENDLIST)
	$(INCCMD) $(DEPENDLIST) | $(SORT) | $(UNIQ) | $(FILTER) > Makeincludes

install:
	# Added a file shlib.sym which is shlib with the symbol table
	# The install step now creates a stripped shlib called shlib
	# The reason for all this is to preserve minimal work - the 
	# strip step was causing unnecessary work in subsequent builds.
	cp shlib.sym shlib; strip shlib
	$(INSTALL) -i$(EXITFLAG) shlib $(INSDIR) $(PROTOFILE)
	$(INSTALL) -i$(EXITFLAG) shlib.ifile $(INSDIR) $(PROTOFILE)
	$(INSTALL) -i$(EXITFLAG) crt0s.o $(INSDIR) $(PROTOFILE)
	ls crt0s.o shlib shlib.ifile | cpio -pumv /lib

clean: clobber

clobber:
	rm -f *.o shlib shlib.sym shlib.ifile shlib.s shlib.nm \
	_* *errlst* *ctype* align ..[ex]*
	>align.h

LINK_SHLIB = $(LD)  shlib.i balign.o ctdata.o wpattern.o data.o ctype_errlst.o \
dalign.o highbuf.o shlib.o shlbat.o shlbsbrk.o -ltam -ltermlib \
getopt.o malloc.o -lc$(CLIB) -o $@

ARCHIVE_DEBUGGABLE = ar q $@ balign.o ctdata.o wpattern.o data.o \
ctype_errlst.o dalign.o highbuf.o shlib.o shlbat.o shlbsbrk.o \
getopt.o malloc.o

shlib.ali: shlib.i shlbat.o shlbsbrk.o shlib.o data.o ctype_errlst.o getopt.o \
malloc.o wpattern.o ctdata.o highbuf.o
	>align.h
	$(CC) -c balign.c dalign.c
	$(LINK_SHLIB)

# If you wish to produce a debuggable product, replace LINK_SHLIB with
# ARCHIVE_DEBUGGABLE and run only the shlib.sym rule

shlib.sym: shlib.i shlbat.o shlbsbrk.o shlib.o data.o ctype_errlst.o getopt.o \
malloc.o balign.o dalign.o wpattern.o ctdata.o highbuf.o
	$(LINK_SHLIB)

shlib.o: shlib.s

ctype_errlst.o:	ctype_errlst.s

ctype_errlst.s: $(CTYPE) $(ERRLST)
	sed 's/sys_nerr/_sys_nerr/' $(ERRLST) > ..errlst.c
	$(CC) -S ..errlst.c  $(CTYPE)
	fgrep -v even ctype.s >  $@
	fgrep -v even ..errlst.s  > ..x
	fgrep -v file ..x >> $@

ctdata.o: $(LIBC)
	ar xv $(LIBC) ctdata.o

data.o: $(LIBC)
	ar xv $(LIBC) data.o

highbuf.o: $(LIBC)
	ar xv $(LIBC) highbuf.o

wpattern.o: $(LIBTAM)
	ar xv $(LIBTAM) wpattern.o

shlib.s: shlib.t shlib.d
	echo '	text' >$@
	echo '	global _tbase' >> $@
	echo '_tbase:' >> $@
	awk '{ printf "\tglobal %s\n\tjmp %s\n",$$1,$$1 }' shlib.t >> $@
	echo '	data' >> $@
	echo '	global _dbase' >> $@
	echo '_dbase:' >> $@
	awk '{ printf "\tglobal %s\n%s:\tspace %s\n",$$1,$$1,$$2 }' shlib.d >>$@

# After satisfying align_sections dependency, you should have a proper shlib.sym
# which satisfies three tests of alignment

shlib.ifile: align_sections shlib.sym $(GREPS)
	if [ `cat _tbase` != "0x00310000" ] ; then exit 1; fi
	if [ `cat _dbase` != "0x00301000" ] ; then exit 1; fi
	if [ `cat _sibuf` != "0x00306000" ] ; then exit 1; fi
	cat ifile.0413s > $@
	echo "timezone = `cat _timezone`;" >> $@
	echo "tzname = `cat _tzname`;" >> $@
	echo "patblack = `cat _patblack`;" >> $@
	echo "patwhite = `cat _patwhite`;" >> $@
	echo "patgray = `cat _patgray`;" >> $@
	echo "patltgray = `cat _patltgray`;" >> $@
	echo "_iob = `cat _iob`;" >> $@
	echo "_bufendtab = `cat _bufendt`;" >> $@
	echo "_ctype = `cat _ctype`;" >> $@
	echo "sys_errlist = `cat _errlst`;" >> $@
	echo "_iob2 = `cat _iob2`;" >> $@
	echo "_buf2endtab = `cat _buf2end`;" >> $@
	echo "_dbase = `cat _dbase`;" >>$@
	awk '{ printf "%s = _dbase + 0x%x;\n",$$1,base;base += $$2 }' shlib.d >>$@ 
	echo "_sibuf = `cat _sibuf`;" >> $@
	echo "_sobuf = `cat _sobuf`;" >> $@
	echo "_tbase = `cat _tbase`;" >> $@
	awk '{ printf "%s = _tbase + 0x%x;\n",$$1,base;base += 6 }' \
	shlib.t >>$@ 
		
# 5-29-86 SKK
# align.h is created empty by shlib.ali rule and made non-empty
# by align Its contents are cat'ed into the make log as the only 
# indicator of its value.
# SQA and DEV people should pay attention to its progress!!
# {
#	The dependencies build a non aligned version shlib.ali
#		and other *.ali* things
#	align produces align.h;
#	The non aligned *.ali* things are removed. Also balign.o, dalign.o
#	shlib is built with correct alignment;
#					/* checked by shlib.ifile rule */
#	Other nice things $(GREPS) are composed;
# }
# 

align_sections:	_dbase.ali _sibuf.ali align
	align
	rm *.ali* balign.o dalign.o
	echo + + + align.h + + +
	cat align.h

_tbase: shlib.nm
	-fgrep _tbase shlib.nm|cut -d\| -f2 > _tbase; exit 0

_timezone: shlib.nm
	-fgrep timezone shlib.nm|cut -d\| -f2 > _timezone; exit 0

_tzname: shlib.nm
	-fgrep tzname shlib.nm|cut -d\| -f2 > _tzname; exit 0

_patwhite: shlib.nm
	-fgrep patwhite shlib.nm|cut -d\| -f2 > _patwhite; exit 0

_patblack: shlib.nm
	-fgrep patblack shlib.nm|cut -d\| -f2 > _patblack; exit 0

_patgray: shlib.nm
	-fgrep patgray shlib.nm|cut -d\| -f2 > _patgray; exit 0

_patltgray: shlib.nm
	-fgrep patltgray shlib.nm|cut -d\| -f2 > _patltgray; exit 0

_errlst: shlib.nm
	-fgrep sys_errlist shlib.nm|cut -d\| -f2 > _errlst; exit 0

_dbase: shlib.nm
	-fgrep _dbase shlib.nm|cut -d\| -f2 > _dbase; exit 0

_dbase.ali: shlib.ali.nm
	-fgrep _dbase shlib.ali.nm|cut -d\| -f2 > _dbase.ali; exit 0

_iob: shlib.nm
	-grep "_iob[^2]" shlib.nm|cut -d\| -f2 > _iob; exit 0

_iob2: shlib.nm
	-fgrep _iob2 shlib.nm|cut -d\| -f2 > _iob2; exit 0

_bufendt: shlib.nm
	-fgrep _bufendtab shlib.nm|cut -d\| -f2 > _bufendt; exit 0

_buf2end: shlib.nm
	-fgrep _buf2endtab shlib.nm|cut -d\| -f2 > _buf2end; exit 0

_ctype: shlib.nm
	-fgrep _ctype shlib.nm|cut -d\| -f2 > _ctype; exit 0

_sibuf: shlib.nm
	-fgrep _sibuf shlib.nm|cut -d\| -f2 > _sibuf; exit 0

_sibuf.ali: shlib.ali.nm
	-fgrep _sibuf shlib.ali.nm|cut -d\| -f2 > _sibuf.ali; exit 0

_sobuf: shlib.nm
	-fgrep _sobuf shlib.nm|cut -d\| -f2 > _sobuf; exit 0

shlib.nm: shlib.sym
	nm -ex shlib.sym | fgrep extern > $@

shlib.ali.nm: shlib.ali
	nm -ex shlib.ali | fgrep extern > $@

align:	align.c
	$(CC) align.c -lld -o $@

getopt.o: getopt.c

malloc.o: malloc.c

shlbat.o: shlbat.c

shlbsbrk.o: shlbsbrk.s

populate:
	getunit

include Makeincludes
include $(MAKEINC)/Makepost.h
