###############################################################################
# Makefile for a GP32 application.
###############################################################################

# Change the line below to the name of your program
PROGRAM =    rflink

# The folder where you extracted gp32libs.tar.bz2
GPSDK_HOME =	/home/slubman/gp32gcc/sdk

# Don't change this unless you changed the compiler location when you
# installed it.
GCC_HOME =	/home/slubman/gp32gcc
#GCC_HOME =   /mnt/stock/gp32/pc/linux/gp32_gcc

# Author name for the program
AUTHOR =	"Slubman DevSoft"

# Icon for the program in the Free-Launcher
ICON=       ../slubman.bmp

# Libraries to be included... this is all of the gpsdk libraries
LIBS = -lgpos -lgpsound -lgpmem -lgpgraphic -lgpfont -lgpstdlib -lgpstdio -lfxe -lfileop

# If you have any directories with include files, add them here with -I on
# the front of each
# e.g. INCLUDES = -I/home/rfbrown/gp32/prog/include -I/usr/src/include
INCLUDES = -I.. -I../common -I../libfxe -I../libfileop

# list all .c files here
CFILES	=	GpMain.c rflink.c\
			../common/version.c\
			../main/pictures.c ../main/modules.c\
			../options/options.c\
			../common/graphics.c ../common/filelist.c ../common/intro.c ../common/langue.c ../common/window.c ../common/speed.c ../common/sound.c ../common/pictures.c

# list all assembler (.s) files here
SFILES	=

##############################################################################
# You shouldn't need to change anything below here, really
##############################################################################

BASEFILES = $(GPSDK_HOME)/basefiles

ALL_INCLUDES = -I$(GPSDK_HOME)/include $(INCLUDES)
ALL_LIBS = $(LIBS) -lm -lc -lgcc

LIBDIRS = -L $(GCC_HOME)/arm-elf/lib \
			 -L $(GCC_HOME)/lib/gcc/arm-elf/3.4.2\
			 -L $(GPSDK_HOME)/lib\
			 -L ../libfxe\
			 -L ../libfileop\
			 -L ../libplg

# LIBDIRS = -L $(GCC_HOME)/arm-elf/lib \
# 			 -L $(GCC_HOME)/lib/gcc-lib/arm-elf/3.3.3\
# 			 -L $(GPSDK_HOME)/lib\
# 			 -L ../libfxe\
# 			 -L ../libfileop\
# 			 -L ../libplg
#
LDSCRIPT = $(BASEFILES)/lnkscript

OFILES	=	crt0.o gpstart.o ../common/gpsys.o $(SFILES:.s=.o) $(CFILES:.c=.o)

CFLAGS  =		-c -march=armv4t -marm -mthumb-interwork -msoft-float \
					-ffast-math -fshort-enums -nostdlib -fno-common -ffreestanding \
					-fno-builtin -fno-exceptions -mstructure-size-boundary=8 -O2 \
					-fomit-frame-pointer -Wall
#CFLAGS	=		-DTAG_CFLAGS_FOR_TARGET -s -march=armv4t -mtune=arm920t -finline-functions -ffast-math -fshort-enums -mstructure-size-boundary=8 -O2

LDFLAGS	=	--script $(LDSCRIPT) -nostartfiles -Map $(PROGRAM).map

CC = arm-elf-gcc
AS = arm-elf-as
LD = arm-elf-ld
OBJCOPY = arm-elf-objcopy

MAPFILE	=		$(PROGRAM).map
TARGET_ELF	=	$(PROGRAM).elf
TARGET_BIN	=	$(PROGRAM).gxb
TARGET_FXE	=	$(PROGRAM).fxe

$(TARGET_FXE): $(TARGET_BIN)
	$(GPSDK_HOME)/bin/b2fxec -t "$(PROGRAM) ("`date +%d.%m.%G`")" -a $(AUTHOR) -b $(ICON) -f $< $@


$(TARGET_BIN): $(TARGET_ELF)
	$(OBJCOPY) -O binary $< $@

$(TARGET_ELF): $(OFILES) Makefile
	@touch ../common/version.c && make -C ../common version.o
	$(LD) $(OFILES) $(LDFLAGS) $(LIBDIRS) $(ALL_LIBS) -o $@

.PHONY: all clean

all:		distrib

distrib: $(TARGET_FXE)
	@( [ -e makezip.sh ] && makezip.sh && echo "cration zip russie") || echo "erreur de cration du zip"

crt0.o:	$(BASEFILES)/crt0.s
	$(CC) $(ALL_INCLUDES) $(CFLAGS) -c $< -o $@

gpstart.o:	../common/gpstart.c
	$(CC) $(ALL_INCLUDES) $(CFLAGS) -c $< -o $@

../%.o:	../%.c
	@$(MAKE) -C $(@D)

%.o:	%.c ../common/langue.h
	$(CC) $(ALL_INCLUDES) $(CFLAGS) -c $< -o $@

clean:
	-rm -f $(OFILES) $(MAPFILE) $(TARGET_ELF) $(TARGET_BIN)

clean-all:
	-rm -f $(OFILES) $(MAPFILE) $(TARGET_ELF) $(TARGET_BIN) $(TARGET_FXE)

test : $(TARGET_FXE)
	$(GPSDK_HOME)/bin/geepee32 /IIC=../common/iic.bin /FXE=$< /SMC=../common/test.smc /RUN

testno : $(TARGET_FXE)
	$(GPSDK_HOME)/bin/geepee32 /FXE=$< /IIC=../common/iic.bin /RUN

testu : $(TARGET_FXE)
	$(GPSDK_HOME)/bin/geepee32 /FXE=$< /IIC=../common/iic.bin /SMC=../common/testu.smc /RUN

run: $(TARGET_BIN)
	gplink run $(TARGET_BIN)

send: $(TARGET_FXE)
	-gplink rm /gpmm/$<
	gplink put $< /gpmm -x
