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

srcdir =	@srcdir@

ifeq ($(srcdir),.)
srcdir = ../..
endif

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)/Sys/libs

INSTALL =	@INSTALL@
INSTALL_DATA =	@INSTALL_DATA@

CC =		@CC@

CFLAGS =	@CFLAGS@
LDFLAGS =	@LDFLAGS@

RANLIB =	@RANLIB@
AR =		ar

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

# I *love* GNU make!
define catenate
/bin/echo -n creating $@...
/bin/echo "$(^:%=#include \"%\"\n)" >$@
/bin/echo done
endef

FLAVOR_CFLAGS =	-m$(CPU) -m$(FPU)
STD_CFLAGS = $(CFLAGS) $(OTHER_CFLAGS) -g $(INCS)
ALL_CFLAGS = $(FLAVOR_CFLAGS) $(STD_CFLAGS)

INCS =		-I$(srcdir) -I$(srcdir)/../library -I$(srcdir)/../include -I.
LIB =		library.a

# Strip the executable (-s), don't look for standard libraries (-nostdlib)
# don't link in crt0.o (-nostartfiles) and tell the linker explicitly to
# load the module from the library that defines the symbol "___load_seg"
# (-Xlinker -u -Xlinker ___load_seg).

LINKFLAGS =	-nostdlib -nostartfiles -Xlinker -u -Xlinker ___load_seg

.c.o:
		$(CC) $(ALL_CFLAGS) -c $< -o $@

all:		ixemul.library ixemul.trace ixemul.debug

SRC_A4 =	ix_startup.c _main.c ix_exec_entry.c exit.c ix_resident.c machdep.c
SRC_MISC =	parse_version.c create_header.c tracecntl.c
SRC_OTHERS =	hwck.c trap.c ix_timer.c debugstub.c
OBJ_OTHERS =	$(SRC_OTHERS:.c=.o)

SRC =           $(filter-out $(SRC_A4) $(SRC_OTHERS) $(SRC_MISC),$(notdir $(wildcard $(srcdir)/*.c)))

SRC___ =	$(filter _%,$(SRC))
SRC_AH =	$(filter a% b% c% d% e% f% g% h%,$(SRC))
SRC_II =	$(filter i%,$(SRC))
SRC_JO =	$(filter j% k% l% m% n% o%,$(SRC))
SRC_PR =	$(filter p% q% r%,$(SRC))
SRC_SS =	$(filter s%,$(SRC))
SRC_TZ =	$(filter t% u% v% w% x% y% z%,$(SRC))

# These are object files which explicitly get linked into the ixemul.library
# file, in the order specified

OBJ_N =		start.o tracecntl.o

OBJ_T =		start.ot tracecntl.ot

IXLIBS =	$(LIB) \
		../../../string/$(CPU)/$(FPU)/libstring.a \
		../../../general/$(CPU)/$(FPU)/libgeneral.a \
		../../../stdlib/$(CPU)/$(FPU)/libstdlib.a \
		../../../stdio/$(CPU)/$(FPU)/libstdio.a

LIBS =		-lgcc -lamiga


$(LIB) :	a4.o __.o ah.o ii.o jo.o pr.o ss.o tz.o $(OBJ_OTHERS)
		rm -f $@
		$(AR) rv $@ $^
		$(RANLIB) $@

ixemul.library:	$(OBJ_N) $(IXLIBS)
		$(CC) -s $(LINKFLAGS) $(OBJ_N) $(IXLIBS) $(LIBS) -o $@

ixemul.trace:	$(OBJ_T) $(IXLIBS)
		$(CC) -s $(LINKFLAGS) $(OBJ_T) $(IXLIBS) $(LIBS) -o $@

ixemul.debug:	$(OBJ_N) $(IXLIBS)
		$(CC) -g $(LINKFLAGS) $(OBJ_N) $(IXLIBS) $(LIBS) -o $@

clean:
		rm -rf 680?0

clobber:	clean
		rm -f Makefile

a4.o:		a4.c
		$(CC) $(ALL_CFLAGS) -ffixed-a4 -c $< -o $@

a4.c:		$(SRC_A4)
		@$(catenate)

__.c:		$(SRC___)
		@$(catenate)

ah.c:		$(SRC_AH)
		@$(catenate)

ii.c:		$(SRC_II)
		@$(catenate)

jo.c:		$(SRC_JO)
		@$(catenate)

pr.c:		$(SRC_PR)
		@$(catenate)

ss.c:		$(SRC_SS)
		@$(catenate)

tz.c:		$(SRC_TZ)
		@$(catenate)

start.o:	start.s version.h $(srcdir)/../include/sys/syscall.def ix_internals.h
		cp $< x.c
		$(CC) $(STD_CFLAGS) -traditional -E x.c -o x.s
		$(CC) $(STD_CFLAGS) -c -m68000 -msoft-float x.s -o $@
		rm -f x.s x.c

start.ot:	start.s version.h $(srcdir)/../include/sys/syscall.def ix_internals.h
		cp $< x.c
		$(CC) $(STD_CFLAGS) -DTRACE_LIBRARY -traditional -E x.c -o x.s
		$(CC) $(STD_CFLAGS) -c -m68000 -msoft-float x.s -o $@
		rm -f x.c x.s

# ix_open.c (and thus ii.o) depends on the size of struct user

ii.o:		$(srcdir)/../include/user.h

# Force -m68020 & -m68881 so the assembler won't bitch about fpu
# instructions.  This means we have to leave out FLAVOR_CFLAGS.

trap.o:		trap.s ix_internals.h
		cp $< x.c
		$(CC) $(STD_CFLAGS) -traditional -E x.c -o x.s
		$(CC) $(STD_CFLAGS) -c -m68020 -m68881 x.s -o $@
		rm -f x.s x.c

# Never omit the frame pointer.

ix_timer.o:	ix_timer.c
		$(CC) $(ALL_CFLAGS) -fno-omit-frame-pointer -c $< -o $@

# hwck.c checks for defines like mc68020 and __HAVE_68881__, but it also
# must be compiled with -m68000. So we first preprocess the source with
# standard flavor flags, and then we compile the resulting source with
# -m68000. This ensures that when we try to use a 68020 ixemul.library on
# a 68000 Amiga, we get a correct message and not a crash.

hwck.o:		hwck.c
		$(CC) $(ALL_CFLAGS) -E $< -o x.c
		$(CC) $(STD_CFLAGS) -m68000 -msoft-float -c x.c -o $@
		rm -f x.c

debugstub.o:	debugstub.c

tracecntl.o:	tracecntl.c
		$(CC) $(ALL_CFLAGS) -ffixed-a4 -c $< -o $@

tracecntl.ot:	tracecntl.c
		$(CC) $(ALL_CFLAGS) -DTRACE_LIBRARY -ffixed-a4 -c $< -o $@

ix_internals.h:	create_header
		./create_header >$@

create_header:	create_header.o

create_header.o:	create_header.c $(srcdir)/../include/user.h ixemul.h

ii.o tz.o:	version.h

parse_version:	parse_version.o

version.h:	parse_version ../version.in
		./parse_version $(srcdir) >$@
