#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) 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 etc.
# Unfortunatly it's even _required_ for linux because some versions
# have a broken standard ar command. Ie they barf if given something 
# they think is not an a.out.
#ifdef __linux__
AR=ar86
#endif

#ifdef __GNUC__

WALL  =-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  =-Wall -Wstrict-prototypes

CC	=%CC%
CFLAGS	=$(GCCFLAG) $(WALL) -O2 -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	=-Ml
LDFLAGS	=-Ml -s
#else
CFLAGS	=-O
LDFLAGS	=-s -H10000
BCCARCH =
#endif
#endif

# Apple cpp-precomp 6.14 (devkit-213/devkit_tools-214) cannot
# handle the QUOT macro in bcc. It puts a space in /usr so that
# it becomes / usr
#ifdef __APPLE__
CFLAGS += -no-cpp-precomp
#endif

# Install files with the userid of the currently running process.
INDAT=-m 644
INEXE=-m 755 -s
INSCR=-m 755

#ifdef __CYGWIN__
EXE=.exe
#else
EXE=
#endif

#ifdef GNUMAKE
all: check_config bcc86 cpp unproto copt as86 ar86 ld86 objdump86 \
     library lib-bsd alt-libs elksemu

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

install-all: install install-other

#else
all: check_config bcc86 cpp 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.'

install install-all: check_config install-bcc install-man
	@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

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

LIBARGS= CC=ncc "CCFLAGS=-O" AR=$(AR) ARFLAGS=$(ARFLAGS)
LIB3ARGS= CC=ncc AR=$(AR) ARFLAGS=$(ARFLAGS)

# Season in the top makefile
ELKSSRC= %ELKSSRC%
PREFIX=	 %PREFIX%
BINDIR=	 %BINDIR%
LIBDIR=  %LIBDIR%
INCLDIR= %INCLDIR%
ASLDDIR= %ASLDDIR%
MANDIR=  %MANDIR%

#ifdef GNUMAKE
export ELKSSRC
#endif

DISTBIN= $(DIST)$(BINDIR)
DISTLIB= $(DIST)$(LIBDIR)
DISTASLD=$(DIST)$(ASLDDIR)
DISTINCL=$(DIST)$(INCLDIR)

# Others to install
OTHERS=  tests dis88 doselks bootblocks

CLEANLIST= bcc as ar ld cpp 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

bcc86: bindir
	echo '#define VERSION "'"$(VERSION)"'"' > bcc/version.h
	VER=$(VERSION) ; \
	echo "#define VER_MAJ $${VER%%.*}" >> bcc/version.h ; \
	VER="$${VER#*.}" ; \
	echo "#define VER_MIN $${VER%.*}" >> bcc/version.h ; \
	echo "#define VER_PAT $${VER#*.}" >> bcc/version.h 
	$(MAKEC) bcc $(MAKEARG) BCCARCH='$(BCCARCH)' bcc ncc bcc-cc1
	cp -p bcc/bcc$(EXE) bin/Bcc$(EXE)
	cp -p bcc/ncc$(EXE) bin/ncc$(EXE)
	cp -p bcc/bcc-cc1$(EXE) lib/bcc-cc1$(EXE)

cpp:  bindir
	$(MAKEC) cpp $(MAKEARG) bcc-cpp
	cp -p cpp/bcc-cpp$(EXE) lib/bcc-cpp$(EXE)

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

copt:  bindir
	$(MAKEC) copt $(MAKEARG) copt
	cp -p copt/copt$(EXE) lib/copt$(EXE)
	cp -p copt/rules.* lib/.
	cp -p copt/rules.start lib/i386/.
	cp -p copt/rules.386 lib/i386/.
	cp -p copt/rules.end lib/i386/.

as86: bindir
	echo '#define VERSION "'"$(VERSION)"'"' > as/version.h
	$(MAKEC) as $(MAKEARG) all
	cp -p as/as86$(EXE) bin/as86$(EXE)
	cp -p as/as86_encap bin/as86_encap

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

ld86: bindir
	echo '#define VERSION "'"$(VERSION)"'"' > ld/version.h
	$(MAKEC) ld $(MAKEARG) ld86
	cp -p ld/ld86$(EXE) bin/ld86$(EXE)

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

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

#ifndef __AS386_16__
#ifdef __elksemu_works__
elksemu: bindir
	$(MAKEC) elksemu elksemu
	cp -p elksemu/elksemu bin/elksemu
#endif
#endif

install-bcc: bcc86 cpp unproto copt as86 ar86 ld86 objdump86
	install -d $(DISTBIN) $(DISTLIB)
	install $(INEXE) bin/Bcc$(EXE) 		$(DISTBIN)/bcc$(EXE)
	install $(INEXE) bin/as86$(EXE) 	$(DISTASLD)/as86$(EXE)
	install $(INEXE) bin/ld86$(EXE) 	$(DISTASLD)/ld86$(EXE)
	install $(INEXE) bin/ar86$(EXE) 	$(DISTBIN)/ar86$(EXE)
	install $(INEXE) bin/objdump86$(EXE) 	$(DISTBIN)/objdump86$(EXE)
	install $(INEXE) bin/objdump86$(EXE) 	$(DISTBIN)/nm86$(EXE)
	install $(INEXE) bin/objdump86$(EXE) 	$(DISTBIN)/size86$(EXE)
	install $(INSCR) bin/as86_encap		$(DISTLIB)/as86_encap
	install $(INEXE) lib/bcc-cc1$(EXE) 	$(DISTLIB)/bcc-cc1$(EXE)
	install $(INEXE) lib/bcc-cpp$(EXE) 	$(DISTLIB)/bcc-cpp$(EXE)
	install $(INEXE) lib/unproto$(EXE) 	$(DISTLIB)/unproto$(EXE)
	install $(INEXE) lib/copt$(EXE) 	$(DISTLIB)/copt$(EXE)
	install $(INDAT) lib/rules.* 		$(DISTLIB)
	install -d $(DISTLIB)/i386
	install $(INDAT) lib/i386/rules.* 	$(DISTLIB)/i386

# NB: This doesn't install as a suid root, that's ok though.
install-emu: elksemu
#ifdef __elksemu_works__
	install -d $(DISTBIN)
	install $(INEXE) bin/elksemu 	$(DISTBIN)/elksemu
#endif

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

install-lib: lib/lib0-done lib/lib2-done lib/lib1-done
	$(MAKEC) libc $(LIBARGS) DISTINCL=$(DISTINCL) install_incl
	install $(INDAT) lib/crt0.o 	$(DISTLIB)/crt0.o
	install $(INDAT) lib/libc.a 	$(DISTLIB)/libc.a
	install $(INDAT) lib/libc_f.a	$(DISTLIB)/libc_f.a
	install $(INDAT) lib/libc_s.a	$(DISTLIB)/libc_s.a
	install $(INDAT) lib/libbsd.a	$(DISTLIB)/libbsd.a
	install $(INDAT) lib/libdos.a	$(DISTLIB)/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 
	@touch lib/lib1-done
#else
alt-libs: lib-stand lib-dos lib-386 lib-fast 
	@touch lib/lib1-done
#endif

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

lib-386: bindir
	test -f bin/ncc
	$(MAKEC) libc $(LIB3ARGS) 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

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

#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: other
	@for i in $(OTHERS) ; do \
		$(MAKEC) $$i BCC=ncc DIST=$(DIST) PREFIX=$(PREFIX) install || exit 1 ; \
	done

other:
	echo '#define VERSION "'"$(VERSION)"'"' > bootblocks/version.h
	@for i in $(OTHERS) ; do \
		$(MAKEC) $$i BCC=ncc DIST=$(DIST) PREFIX=$(PREFIX) || exit 1; \
	done

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

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`
	rm -f bcc/version.h
	rm -f as/version.h
	rm -f ld/version.h
	rm -f bootblocks/version.h

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