#ifdef COMMENT
##############################################################################
#
# This Makefile has been completely re-vamped. Two reasons, first to allow
# for a full compile before installation, hence allowing us to make a binary
# distribution without installing and second to allow proper dependancy trees
# for the different versions of the library.
#
# As an aside, it would be useful if I could avoid GNU-make constucts.
#
##############################################################################
#endif

# Defaults, generic C
ARFLAGS =r
CC      =%CC%
CFLAGS	=%CFLAGS%
LDFLAGS	=
MAKEARG	=CC='$(CC)' CFLAGS='$(CFLAGS)' LDFLAGS='$(LDFLAGS)' PREFIX=$(PREFIX) \
         LIBPRE='$(LIBPRE)' LIBDIR='$(LIBDIR)' BINDIR='$(BINDIR)' ANSI='$(ANSI)'
MAKEC=$(MAKE) -C
MAKEX=

# This is only allowed on Linux because make needs to know how to look
# inside an archive to get the last modified times of the component .o
# files. This should be fine for Linux, but it won't be for AIX.
#ifdef __linux__
AR=ar86
#endif

#ifdef __GNUC__
# unproto is yukky, I've included '-w' in the local makefile.

WALL  =-Wtraditional -Wshadow -Wid-clash-14 -Wpointer-arith \
       -Wcast-qual -Wcast-align -Wconversion -Waggregate-return \
       -Wstrict-prototypes -Wmissing-prototypes -Wredundant-decls \
       -Wnested-externs -Winline 

WALL  =-Wstrict-prototypes

CC	=gcc
CFLAGS	=$(GCCFLAG) -Wall $(WALL) -O6 -g
#endif

#ifndef GNUMAKE
MAKEC=sh makec
MAKEX=makec
#endif

#ifdef __minix
# Minix make doesn't understand -C, cc runs out of memory with the headers.
MAKEC=ash makec
MAKEX=makec
CFLAGS=-O -m -w -DPOSIX_HEADERS_MISSING -DVERY_SMALL_MEMORY
#endif

#ifdef __BCC__
ANSI    =-ansi
#ifdef __AS386_32__
CFLAGS	=-3
LDFLAGS	=-3 -s -N
#else
CFLAGS	=-0
LDFLAGS	=-0 -s -H10000
BCCARCH =-Mf -O
#endif
#endif

# Alter these if for some reason you don't want this done as root.
#ifdef __BCC__
INDAT=-o root -g root -m 644
INEXE=-o root -g root -m 755
INSCR=-o root -g root -m 755
#else
INDAT=-o root -g root -m 644
INEXE=-o root -g root -m 755 -s
INSCR=-o root -g root -m 755
#endif

#ifdef GNUMAKE
all: check_config bcc unproto copt as86 ar86 ld86 objdump86 \
     library lib-bsd alt-libs elksemu
#else
all: check_config bcc unproto copt as86 ar86 ld86 objdump86
	@echo
	@echo 'NOTE: To build the libraries you need GNU-Make.'
	@echo '      They are available precompiled in the Dev86clb-X.X.X.zip file.'
#endif

install: check_config install-bcc install-man \
     install-lib install-lib2 install-emu

install-all: install install-other

##############################################################################

LIBARGS= CC=ncc CCFLAGS= AR=$(AR) ARFLAGS=$(ARFLAGS)

# Season in the top makefile
ELKSSRC= %ELKSSRC%
PREFIX=	 %PREFIX%
LIBPRE=	 %LIBPRE%
BINDIR=	 %BINDIR%
LIBDIR=  %LIBDIR%

#ifdef GNUMAKE
export ELKSSRC
#endif

DISTBIN= $(DIST)$(BINDIR)
DISTLIB= $(DIST)$(LIBDIR)
DISTPRE= $(DIST)$(LIBPRE)

# Others to install
OTHERS=  tests dis88 doselks bootblocks

CLEANLIST= bcc as ar ld unproto copt libc elksemu libbsd $(OTHERS)

##############################################################################

bindir: $(MAKEX)
	@mkdir -p bin lib lib/i386
	@rm -f include
	@ln -s libc/include include 2>/dev/null || true
#ifndef GNUMAKE
	@rm -f include/linuxmt include/arch || true
	@ln -s ../kinclude/linuxmt include/linuxmt 2>/dev/null || true
	@ln -s ../kinclude/arch    include/arch    2>/dev/null || true
#endif

bcc: bindir
	$(MAKEC) bcc $(MAKEARG) BCCARCH='$(BCCARCH)' bcc ncc bcc-cc1
	cp -p bcc/bcc bin/Bcc
	cp -p bcc/ncc bin/ncc
	cp -p bcc/bcc-cc1 lib/bcc-cc1

unproto:  bindir
	$(MAKEC) unproto $(MAKEARG) unproto
	cp -p unproto/unproto lib/unproto

copt:  bindir
	$(MAKEC) copt $(MAKEARG) copt
	cp -p copt/copt lib/copt
	cp -p copt/rules.* lib/.

as86: bindir
	$(MAKEC) as $(MAKEARG) all
	cp -p as/as86 bin/as86
	cp -p as/as86_encap bin/as86_encap

ar86: bindir
	$(MAKEC) ar $(MAKEARG) all
	cp -p ar/ar86 bin/ar86

ld86: bindir
	$(MAKEC) ld $(MAKEARG) ld86
	cp -p ld/ld86 bin/ld86

ld86r: bindir
	$(MAKEC) ld $(MAKEARG) ld86r
	cp -p ld/ld86r bin/ld86r

objdump86: bindir
	$(MAKEC) ld $(MAKEARG) objdump86
	cp -p ld/objdump86 bin/objdump86

elksemu: bindir
#ifndef __AS386_16__
#ifdef __linux_i386__
	$(MAKEC) elksemu \
	CC='$(CC)' PREFIX=$(PREFIX) LIBPRE='$(LIBPRE)' LIBDIR='$(LIBDIR)' BINDIR='$(BINDIR)' \
	elksemu
#else
	$(MAKEC) elksemu CC='ncc' elksemu
#endif
	cp -p elksemu/elksemu bin/elksemu
#endif

install-ln: bcc unproto copt as86 ar86 ld86 elksemu
	install -d $(DISTBIN)
	ln -fs `pwd`/bin/ncc		$(DISTBIN)/bcc
	ln -fs `pwd`/bin/as86_encap	$(DISTBIN)/as86_encap
	ln -fs `pwd`/bin/as86 		$(DISTBIN)/as86
	ln -fs `pwd`/bin/ar86 		$(DISTBIN)/ar86
	ln -fs `pwd`/bin/ld86 		$(DISTBIN)/ld86
#ifndef __AS386_16__
	ln -fs `pwd`/bin/elksemu 	$(DIST)/lib/elksemu
#endif
	-install -d $(DIST)/usr/lib
	-install $(INDAT) libc/error/liberror.txt $(DIST)/usr/lib/liberror.txt

install-bcc: bcc unproto copt as86 ar86 ld86 objdump86
	install -d $(DISTBIN) $(DISTLIB) $(DISTLIB)/i86
	install $(INEXE) bin/Bcc 	$(DISTBIN)/bcc
	install $(INSCR) bin/as86_encap	$(DISTBIN)/as86_encap
	install $(INEXE) bin/as86 	$(DISTBIN)/as86
	install $(INEXE) bin/ar86 	$(DISTBIN)/ar86
	install $(INEXE) bin/ld86 	$(DISTBIN)/ld86
	install $(INEXE) bin/objdump86 	$(DISTBIN)/objdump86
	install $(INEXE) bin/objdump86 	$(DISTBIN)/nm86
	install $(INEXE) bin/objdump86 	$(DISTBIN)/size86
	install $(INEXE) lib/bcc-cc1 	$(DISTLIB)/bcc-cc1
	install $(INEXE) lib/unproto 	$(DISTLIB)/unproto
	install $(INEXE) lib/copt 	$(DISTLIB)/copt
	install $(INDAT) lib/rules.* 	$(DISTLIB)/i86
	@test ! -f $(DISTLIB)/as86 || rm -f $(DISTLIB)/as86
	@test ! -f $(DISTLIB)/ld86 || rm -f $(DISTLIB)/ld86

# NB: This doesn't install as a suid root, that's ok though.
install-emu: elksemu
#ifndef __AS386_16__
	install -d $(DIST)/lib
	install -o root -g root -m 755 bin/elksemu 	$(DIST)/lib/elksemu
#endif

install-man:
	-$(MAKEC) man MANDIR=$(DIST)$(PREFIX)/man install

install-lib: lib/lib0-done lib/lib2-done
	install -d $(DISTLIB)/i86
	$(MAKEC) libc $(LIBARGS) BCCHOME=$(DISTPRE) install_incl
	install $(INDAT) lib/crt0.o 	$(DISTLIB)/i86/crt0.o
	install $(INDAT) lib/libc.a 	$(DISTLIB)/i86/libc.a
	install $(INDAT) lib/libbsd.a	$(DISTLIB)/i86/libbsd.a
	-install -d $(DIST)/usr/lib
	-install $(INDAT) libc/error/liberror.txt $(DIST)/usr/lib/liberror.txt

install-lib2: lib/lib1-done
	install -d $(DISTLIB)/i86
	install $(INDAT) lib/libc_f.a	$(DISTLIB)/i86/libc_f.a
	install $(INDAT) lib/libc_s.a	$(DISTLIB)/i86/libc_s.a
	install $(INDAT) lib/libdos.a	$(DISTLIB)/i86/libdos.a

#ifndef __AS386_16__
	install -d $(DISTLIB)/i386
	install $(INDAT) lib/i386/crt0.o 	$(DISTLIB)/i386/crt0.o
	install $(INDAT) lib/i386/libc.a 	$(DISTLIB)/i386/libc.a
#endif

lib/lib0-done:
	$(MAKE) -f make.fil library

lib/lib1-done:
	$(MAKE) -f make.fil alt-libs

lib/lib2-done:
	$(MAKE) -f make.fil lib-bsd

all-libs: library lib-bsd alt-libs
	@:

library: bindir
	test -f bin/ncc
	$(MAKEC) libc $(LIBARGS) PLATFORM=i86-ELKS
	cp -p libc/crt0.o libc/libc.a lib/.
	sh libcompat lib/libc.a
	@touch lib/lib0-done

lib-bsd: bindir
	test -f bin/ncc
	$(MAKEC) libbsd $(LIBARGS)
	cp -p libbsd/libbsd.a lib/libbsd.a
	sh libcompat lib/libbsd.a
	@touch lib/lib2-done

#ifdef __AS386_16__
alt-libs: lib-stand lib-dos lib-fast 
#else
alt-libs: lib-stand lib-dos lib-386 lib-fast 
#endif
	@touch lib/lib1-done

lib-fast: bindir
	test -f bin/ncc
	$(MAKEC) libc $(LIBARGS) PLATFORM=i86-FAST
	cp -p libc/libc_f.a lib/libc_f.a
	sh libcompat lib/libc_f.a

lib-stand: bindir
	test -f bin/ncc
	$(MAKEC) libc $(LIBARGS) PLATFORM=i86-BIOS
	cp -p libc/libc_s.a lib/libc_s.a
	sh libcompat lib/libc_s.a

lib-dos: bindir
	test -f bin/ncc
	$(MAKEC) libc $(LIBARGS) PLATFORM=i86-DOS
	cp -p libc/libdos.a lib/libdos.a
	sh libcompat lib/libdos.a

#ifndef __AS386_16__
lib-386: bindir
	test -f bin/ncc
	$(MAKEC) libc $(LIBARGS) PLATFORM=i386-BCC
	cp -p libc/crt3.o lib/i386/crt0.o
	cp -p libc/libc3.a lib/i386/libc.a
	sh libcompat lib/i386/libc.a
#endif

##############################################################################

#ifdef GNUMAKE

check_config: $(MAKEX)
	$(MAKEC) libc .config.dir

config: $(MAKEX)
	$(MAKEC) libc config

#else
check_config: ;

config: ;
#endif

makec:
	echo 'cd $$1 ; shift ; make "$$@"' > makec
	chmod +x makec

##############################################################################

install-other:
	@for i in $(OTHERS) ; do \
		$(MAKEC) $$i BCC=ncc DIST=$(DIST) PREFIX=$(PREFIX) install || exit 1 ; \
	done

other: $(OTHERS)

$(OTHERS):
	$(MAKEC) $@ BCC=ncc

clean:
	-@for i in $(CLEANLIST) ; do $(MAKEC) $$i $@ ; true ; done

realclean:
	-@for i in $(CLEANLIST) ; do $(MAKEC) $$i $@ ; true ; done
	rm -rf bin lib
	rm -f include
	rm -f makec
	rm -f `find $(CLEANLIST) -type l -print`

##############################################################################
