CC	= ppc-morphos-gcc
ifeq ($(shell ppc-morphos-gcc -dumpmachine), ppc-morphos)
LD      = ppc-morphos-ld
else
LD      = ppc-morphos-ld -r
endif
STRIP	= ppc-morphos-strip
LIBPATH	= /gg/morphos/lib/

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

TARGET      =   PPC
CODETYPE    =   PPC
VERSION     =   50
DESCDEST    =   descriptor/FTXT

##############################################################################
.PHONY: all install clean

all: text.datatype.elf $(DESCDEST)

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

CFLAGS = -noixemul -O2 -mcpu=604e -DMORPHOS_AG_EXTENSION -D__MORPHOS__ -DUSE_INLINE_STDARG -DCOMPILE_DATATYPE -I./ -I/gg/morphos/aboxinclude/os-include -I/gg/morphos/aboxinclude/include -I/gg/morphos/include
LDFLAGS	   = -fl libnix
LDLIBS	= -labox -lmath -lc -lm -lsyscall

OBJS	= text_init.o libfunc.o functable.o support.o obtainengine.o textclass.o agextension.o

text.datatype.elf: $(OBJS)
	$(LD) -L$(LIBPATH) -o $@.db $(LDFLAGS) $(OBJS) $(LDLIBS)
	$(STRIP) --strip-unneeded --remove-section .comment $@.db -o $@

DUMP:	text.datatype.elf
	ppc-morphos-objdump --section-headers --all-headers --reloc --disassemble-all text.datatype.elf.db >text.datatype.elf.dump

#	$(LD) -o $@ $(LDFLAGS) -r libstart.o $(OBJS) rint.o $(LDLIBS)

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

text_init.o: text_init.c text_intern.h libdefs.h text.datatype_VERSION.h
	$(CC) $(CFLAGS) -c $*.c -o $@

libfunc.o: libfunc.c text_intern.h libdefs.h
	$(CC) $(CFLAGS) -c $*.c -o $@

functable.o: functable.c text_intern.h libdefs.h
	$(CC) $(CFLAGS) -c $*.c -o $@

support.o: support.c text_intern.h
	$(CC) $(CFLAGS) -c $*.c -o $@

obtainengine.o: obtainengine.c text_intern.h libdefs.h
	$(CC) $(CFLAGS) -c $*.c -o $@

textclass.o: textclass.c text_intern.h support.h textclass.h libdefs.h
	$(CC) $(CFLAGS) -c $*.c -o $@

agextension.o: agextension.c support.h textclass.h libdefs.h
	$(CC) $(CFLAGS) -c $*.c -o $@

$(DESCDEST): ftxt_descriptor.asm ftxt_descriptor_VERSION.h
	mkdir -p descriptor
	basm -ua+ -o$(DESCDEST) ftxt_descriptor.asm

install: all
	-mkdir -p /sys/MorphOS/classes/datatypes
	-mkdir -p /sys/MorphOS/devs/datatypes
	cp text.datatype.elf /sys/morphos/classes/datatypes/text.datatype
	cp $(DESCDEST) /sys/morphos/devs/datatypes/FTXT
	-flushlib text.datatype

install-iso: all
	mkdir -p $(ISOPATH)MorphOS/Classes/Datatypes
	mkdir -p $(ISOPATH)MorphOS/Devs/Datatypes
	cp text.datatype.elf $(ISOPATH)MorphOS/Classes/Datatypes/text.datatype
	cp $(DESCDEST) $(ISOPATH)MorphOS/Devs/Datatypes/FTXT

source:
	(cd .. && tar --transform "s,^text,&.datatype," -cf $(SOURCEPATH)text.datatype.tar text)

bump:
	bumprev2 VERSION $(VERSION) FILE text.datatype_VERSION TAG text.datatype ADD " AROS"

bumpdesc:
	bumprev2 VERSION $(VERSION) FILE ftxt_descriptor_VERSION TAG FTXT

dump:
	ppc-morphos-objdump --section-headers --all-headers --reloc --disassemble-all ftxt.datatype.elf.db >ftxt.datatype.elf.dump

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

clean:
	-rm -rf *.bak *.o *.elf

