#### Start of system configuration section. ####

srcdir =	@srcdir@
VPATH =		@srcdir@

# Common prefix for machine-independent installed files.
prefix =	@prefix@

# Common prefix for machine-dependent installed files.
exec_prefix =	@exec_prefix@

bindir =	$(exec_prefix)/bin
libdir =	$(exec_prefix)/lib
syslibdir =	$(exec_prefix)/Sys/libs
incdir =	$(exec_prefix)/include

INSTALL =	@INSTALL@
INSTALL_DATA =	@INSTALL_DATA@

# Force the use of gcc 2.6.3 for now.  The beta version of 2.7 currentl
# produces an ixemul.library that has problems when doing a full
# configure of the GNU tree.
#CC =		@CC@
CC = gcc-2.6.3

CFLAGS =	@CFLAGS@
LDFLAGS =	@LDFLAGS@

RANLIB =	@RANLIB@

#### End system configuration section ####

# Default is generic m68k version with no FPU support.

CPU =		68000
FPU =		soft-float
BASE =		no-baserel

# Some additional options for OTHER_CFLAGS that may or may not be useful are:
#
# -funroll-loops
# -fforce-mem
# -DDEBUG_VERSION	(build a debugging version)

#Use the flag to suppress stack extension when 2.6.4/2.7 is installed.
#STACKEXTEND =	-mnostackextend
OTHER_CFLAGS =	-fomit-frame-pointer $(STACKEXTEND)

BASE_FLAGS_TO_PASS = \
		"BASE=$(BASE)" \
		"BUILDDIR=$(BUILDDIR)" \
		"CC=$(CC)" \
		"CFLAGS=$(CFLAGS)" \
		"CPU=$(CPU)" \
		"FPU=$(FPU)" \
		"LDFLAGS=$(LDFLAGS)" \
		"OTHER_CFLAGS=$(OTHER_CFLAGS)" \
		"RANLIB=$(RANLIB)"

FLAGS_TO_PASS =	$(BASE_FLAGS_TO_PASS) $(EXTRA_FLAGS_TO_PASS)

###  The default thing to do is to make all reasonable combinations of the
###  library.
###
###  Note that libsrc, which builds the runtime startup files and both
###  versions of libc.a, completely ignores the FPU/CPU/BASE variables,
###  so we just call it directly.  Also note that the 68000 versions
###  of the files MUST be built before libsrc.
###
###  Other than the restriction noted above, you can comment out specific
###  CPU/FPU combinations that you are not interested in.  However you
###  do have to build both the baserel and no-baserel versions for the
###  combinations you want to build.

all:
		$(MAKE) build CPU=68000 FPU=soft-float BASE=no-baserel
		$(MAKE) build CPU=68000 FPU=soft-float BASE=baserel
		$(MAKE) build CPU=68020 FPU=68881      BASE=no-baserel
		$(MAKE) build CPU=68020 FPU=68881      BASE=baserel
		$(MAKE) build CPU=68020 FPU=soft-float BASE=no-baserel
		$(MAKE) build CPU=68020 FPU=soft-float BASE=baserel
		$(MAKE) build CPU=68030 FPU=68881      BASE=no-baserel
		$(MAKE) build CPU=68030 FPU=68881      BASE=baserel
		$(MAKE) build CPU=68030 FPU=soft-float BASE=no-baserel
		$(MAKE) build CPU=68030 FPU=soft-float BASE=baserel
		$(MAKE) build CPU=68040 FPU=68881      BASE=no-baserel
		$(MAKE) build CPU=68040 FPU=68881      BASE=baserel
		@(cd libsrc && $(MAKE) $(FLAGS_TO_PASS))

###  The "build" target makes a single flavor of the library.  I.E.
###  the 68040+FPU version.

build:
		@$(MAKE) gen_library-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)
		@$(MAKE) glue-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)
		@$(MAKE) gnulib-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)
		@$(MAKE) gnulib-$(FPU)-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)
		@$(MAKE) network-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)
		@$(MAKE) stack-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)
		@$(MAKE) static-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)
		@$(MAKE) stdio_2-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)
		@$(MAKE) stdlib-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)
		@$(MAKE) string-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)
		@$(MAKE) library-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)

###  These targets are used to map a generic target <builddir>-<BASE>-<CPU>-<FPU>
###  to one of the less complicated targets.  I.E. if something only needs to
###  be built for a particular CPU and FPU, and the value of BASE is irrelevant,
###  then it would be mapped to <CPU>-<FPU>.

gen_library-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(CPU)-$(FPU) $(FLAGS_TO_PASS) BUILDDIR=gen_library

glue-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(BASE) $(FLAGS_TO_PASS) BUILDDIR=glue

gnulib-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(CPU)-$(FPU) $(FLAGS_TO_PASS) BUILDDIR=gnulib

gnulib-$(FPU)-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(CPU)-$(FPU) $(FLAGS_TO_PASS) BUILDDIR=gnulib-$(FPU)

library-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(CPU)-$(FPU) $(FLAGS_TO_PASS) BUILDDIR=library

network-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS) BUILDDIR=network

stack-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS) BUILDDIR=stack

static-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS) BUILDDIR=static

stdio_2-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(CPU)-$(FPU) $(FLAGS_TO_PASS) BUILDDIR=stdio_2

stdlib-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(CPU)-$(FPU) $(FLAGS_TO_PASS) BUILDDIR=stdlib

string-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(CPU)-$(FPU) $(FLAGS_TO_PASS) BUILDDIR=string

###  These targets are the ones that are reduced from the full generic
###  targets, on a per directory basis.  I.E. if it is only necessary
###  to build versions for a specific BASE and the CPU/FPU combination
###  is irrelevant, then the generic target is mapped to just BASE.

# This target is for things that are only dependent on BASE.

$(BASE):
		@if [ -d $(BUILDDIR)/$(BASE) ]; then \
			true; \
		else \
			echo mkdir -p $(BUILDDIR)/$(BASE); \
			mkdir -p $(BUILDDIR)/$(BASE); \
		fi;
		@(cd $(BUILDDIR)/$(BASE) ; $(MAKE) -f ../Makefile $(FLAGS_TO_PASS));

# This target is for things that are only dependent on CPU and FPU.

$(CPU)-$(FPU):
		@if [ -d $(BUILDDIR)/$(CPU)/$(FPU) ]; then \
			true; \
		else \
			echo mkdir -p $(BUILDDIR)/$(CPU)/$(FPU); \
			mkdir -p $(BUILDDIR)/$(CPU)/$(FPU); \
		fi;
		@(cd $(BUILDDIR)/$(CPU)/$(FPU) ; $(MAKE) -f ../../Makefile $(FLAGS_TO_PASS))

# This target is for things that are dependent on CPU, FPU, and BASE.

$(BASE)-$(CPU)-$(FPU):
		@if [ -d $(BUILDDIR)/$(BASE)/$(CPU)/$(FPU) ]; then \
			true; \
		else \
			echo mkdir -p $(BUILDDIR)/$(BASE)/$(CPU)/$(FPU); \
			mkdir -p $(BUILDDIR)/$(BASE)/$(CPU)/$(FPU); \
		fi;
		@(cd $(BUILDDIR)/$(BASE)/$(CPU)/$(FPU) ; $(MAKE) -f ../../../Makefile $(FLAGS_TO_PASS));

###  Install all the libraries, include files, runtime files, etc.

install:
		$(INSTALL) library/68000/soft-float/ixconfig $(bindir)/ixconfig
		$(INSTALL) library/68000/soft-float/ixemul.library $(syslibdir)/ixemul.library
		$(INSTALL) library/68000/soft-float/ixemul.trace $(syslibdir)/ixemul.trace
		$(INSTALL) library/68020/soft-float/ixemul.library $(syslibdir)/ixemul020.library
		$(INSTALL) library/68020/soft-float/ixemul.trace $(syslibdir)/ixemul020.trace
		$(INSTALL) library/68030/soft-float/ixemul.library $(syslibdir)/ixemul030.library
		$(INSTALL) library/68030/soft-float/ixemul.trace $(syslibdir)/ixemul030.trace
		$(INSTALL) library/68020/68881/ixemul.library $(syslibdir)/ixemul020fpu.library
		$(INSTALL) library/68020/68881/ixemul.trace $(syslibdir)/ixemul020fpu.trace
		$(INSTALL) library/68030/68881/ixemul.library $(syslibdir)/ixemul030fpu.library
		$(INSTALL) library/68030/68881/ixemul.trace $(syslibdir)/ixemul030fpu.trace
		$(INSTALL) library/68040/68881/ixemul.library $(syslibdir)/ixemul040fpu.library
		$(INSTALL) library/68040/68881/ixemul.trace $(syslibdir)/ixemul040fpu.trace
		$(INSTALL) libsrc/bcrt0.o $(libdir)/bcrt0.o
		$(INSTALL) libsrc/crt0.o $(libdir)/crt0.o
		$(INSTALL) libsrc/rcrt0.o $(libdir)/rcrt0.o
		$(INSTALL) libsrc/libc.a $(libdir)/libc.a
		$(INSTALL) libsrc/libbc.a $(libdir)/libb/libc.a
		(cd $(srcdir)/include && cp -r . $(incdir))
		
clean:
		(cd gen_library ; $(MAKE) $(FLAGS_TO_PASS) clean)
		(cd gnulib ; $(MAKE) $(FLAGS_TO_PASS) clean)
		(cd gnulib-soft-float ; $(MAKE) $(FLAGS_TO_PASS) clean)
		(cd gnulib-68881 ; $(MAKE) $(FLAGS_TO_PASS) clean)
		(cd library ; $(MAKE) $(FLAGS_TO_PASS) clean)
		(cd stdio_2 ; $(MAKE) $(FLAGS_TO_PASS) clean)
		(cd stdlib ; $(MAKE) $(FLAGS_TO_PASS) clean)
		(cd string ; $(MAKE) $(FLAGS_TO_PASS) clean)
		(cd network ; $(MAKE) $(FLAGS_TO_PASS) clean)
