# $Id: Makefile,v 1.1.1.1 2001/07/13 02:54:20 bhepple Exp $
#
# Copyright 2001 The Finder Pty Ltd
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#

APPNAME = adventure_app

BETA = NO
DEBUG = NO
CFLAGS+=-DEBOOKMAN=1
# Define the list of source files
SRCS_CPP = adventure.cpp BinFile.cpp
SRCS_C = advcust.c advkern.c autop0.c autop1.c autop2.c autop3.c autop4.c autop5.c

SRCS_S =

SRCS_H = adventure.h \
advkern.h \
autod0.h \
autod1.h \
autod2.h \
autod3.h \
datacache.h \
BinFile.h 

ALL_SOURCE = $(SRCS_CPP) $(SRC_C) $(SRC_H) \
INSTALL \
Makefile \
initial.mom \
ADVENTURE4.dat \
COPYING \
README.txt \
adventure.hlp \
adventure_app.atts \
adventure_app.bmp \
adventure_app.icn \
datacache.h \
initial.mom \
libraries.link \
os.def \
os.link \
standalone.link

# Program parameters (ebo_locate needs 12K of stack)
HEAPSPACE       = 98304
MAINSTACKSPACE  = 16384
TIMERSTACKSPACE = 4096

# Environment: Use "yes" or "no"
ifeq ($(DEBUG),YES)
GUI_LOCAL_COPY = no
OS_LOCAL_COPY  = no
LANG_LOCAL_COPY = no
GUI_LANGUAGE    = GUI_Amr.pkg
else # don't change these:
GUI_LOCAL_COPY = no
OS_LOCAL_COPY  = no
LANG_LOCAL_COPY = no
GUI_LANGUAGE    = GUI_Amr.pkg
endif

# Use ${ESDK_LIBGCC_INSTALL_LIBFILE} if you want floating-point support, or
# LIBGCC = ${ESDK_LIBGCC_INSTALL_LIBFILE_FLOAT}
LIBGCC = ${ESDK_LIBGCC_INSTALL_LIBFILE_NOFLOAT}

#
# Set paths for release building or SDK use.
# Third party developers only use the "Binary release version" section.
#
ifeq (x$(EBSDK_LIBS)x,xx) # Binary release version
	include ${EBOOKMAN_SDK}/ebsdk.uses
	DB_LIB=${ELIB_DATABASES_INSTALL_LIBFILE}
	HEAPS_LIB=${ELIB_HEAPS_INSTALL_LIBFILE}
	INST_INC=-I${ESDK_TARGET_INC}
	# DB_INC and HEAPS_INC files are included in INST_INC for the
	# binary release, we don't need them here
	DB_INC=
	HEAPS_INC=
	OS_BASEDIR = ../../os
else
	# Building the SDK
	include ${EBSDK_LIBS}/ebookman_libs.uses
	DB_LIB=${_ELIB_DATABASES_LIBFILE}
	HEAPS_LIB=${_ELIB_HEAPS_LIBFILE}
	INST_INC=-I${ESDK_TARGET_INC}
	DB_INC=-I${_ELIB_DATABASES_INC}
	HEAPS_INC=-I${_ELIB_HEAPS_INC}
	OS_LOCAL_COPY = yes
	OS_BASEDIR = ../../os
endif

#Get the operating system from a local copy or the official SDK release
ifeq	(${OS_LOCAL_COPY},yes)
	OS_INC     = -I${OS_BASEDIR}/include
	OSLIBLIST  = audio ebo event lcd piezo root sigma sio sys touch timer internal
	OS_LIBS    = ${OSLIBLIST:%=${OS_BASEDIR}/lib/lib%.a} \
		     ${OS_BASEDIR}/kernel_api/kernel.a
	OS_BOOT    = ${OS_BASEDIR}/boot/boot.rom
	OS_STREAM  = ${OS_BASEDIR}/loader/loader.rom
	OS_GDBCMD  = ${OS_BASEDIR}/loader/gdb_cmd
else
	OS_BASEDIR = ${ESDK_TARGET_OS}
	OS_INC     =
	OS_LIBS    = ${ESDK_TARGET_LIBS}/ebm_os.a
	OS_BOOT    = ${OS_BASEDIR}/boot.rom
	OS_STREAM  = ${OS_BASEDIR}/os.rom
	OS_GDBCMD  = ${OS_BASEDIR}/gdb_cmd
endif

#Get the GUI from a local copy or the official SDK release
ifeq	(${GUI_LOCAL_COPY},yes)
	GUI_BASEDIR  = ../franklin/gui/source
	GUI_INC	     = -I${GUI_BASEDIR}
	GDBDIRS      = -dir ${GUI_BASEDIR}
	GUI_A        = ${GUI_BASEDIR}/lib.snk32/gui.a
	STANDALONE_O = ${GUI_BASEDIR}/lib.snk32/standalone.o
else
	GUI_BASEDIR  =
	GUI_INC      =
	GDBDIRS      = -dir ${ESDK_HOME}/samples/gui/source
	GUI_A        = ${ESDK_TARGET_LIBS}/gui.a
	STANDALONE_O = ${ESDK_TARGET_LIBS}/standalone.o
endif

#Get the fonts and languages files from a local copy or the official release
ifeq	(${LANG_LOCAL_COPY},yes)
	GUI_FILEDIR = ../gui/install
else
	GUI_FILEDIR = ${ESDK_TARGET_OS}
endif

# Add eBookMan header file paths to the standard path
INCFLAGS += ${GUI_INC} ${OS_INC} ${DB_INC} ${HEAPS_INC} ${INST_INC}

#To prevent warnings from stopping the compilation, change the following line
#to "ERROR=" or start the make using "make ERROR=".
ERROR = -Werror

# Set eBookMan complier options
CFLAGS += ${INCFLAGS} 
CPPFLAGS += -fno-defer-pop -Wall -Wmissing-declarations ${ERROR} 

ifeq ($(BETA),YES)
CFLAGS += -DISBETA=1
endif

ifeq ($(DEBUG),YES)
# CFLAGS += -g
CFLAGS += -DDEBUG=1
else
CFLAGS += -O2 -DNDEBUG
endif

# Define target and directory names
OBJDIR = ./lib.snk32

# Define the list of created objects.   
OBJS = ${SRCS_CPP:%.cpp=${OBJDIR}/%.o} ${SRCS_C:%.c=${OBJDIR}/%.o} ${SRCS_S:%.s=${OBJDIR}/%.o}

#
# Make Targets
#

all: os sa

os: ${APPNAME}.fxe GDB ${APPNAME}.icn gui.pkg system.fnt default.fnt \
	settings.dat jingles.dat alarms.dat

sa: ${APPNAME}_sa.coff sGDB gui.pkg system.fnt default.fnt

seb: ${APPNAME}.seb

ifeq ($(DEBUG),YES)
buildno.h: $(SRCS_CPP) $(SRCS_C) $(SRCS_H) adventure.hlp
	./inc_buildno
endif

CLEANABLES = XXX XXX.coff XXX.map XXX.seg XXX.sym XXX.chk XXX.mp XXX.link

CLEANLIST += GDB  $(CLEANABLES:XXX%=${APPNAME}%) ${APPNAME}.fxe os.cmd
CLEANLIST += sGDB $(CLEANABLES:XXX%=${APPNAME}_sa%)
CLEANLIST += gui.pkg system.fnt default.fnt settings.dat jingles.dat alarms.dat
CLEANLIST += ${APPNAME}.seb temp.* decompose.*

allclean: clean
	(cd ${GUI_BASEDIR}; ${MAKE} clean)

clean:
	rm -rf ${CLEANLIST} ${OBJDIR} *~

ifeq (${GUI_LOCAL_COPY},yes)
${GUI_A}::
	(cd ${GUI_BASEDIR}; make quick)
endif

gui.pkg:
	-ln -s ${GUI_FILEDIR}/${GUI_LANGUAGE} gui.pkg

system.fnt:
	-ln -s ${GUI_FILEDIR}/System.fnt system.fnt

default.fnt:
	-ln -s ${GUI_FILEDIR}/Default.fnt default.fnt

settings.dat:
	-ln -s ${ESDK_TARGET_OS}/settings.dat settings.dat

jingles.dat:
	-ln -s ${ESDK_TARGET_OS}/jingles.dat jingles.dat

alarms.dat:
	-ln -s ${ESDK_TARGET_OS}/alarms.dat alarms.dat

${APPNAME}.fxe: ${APPNAME}.coff os.def
	${ESDK_MAKEROM_EXE} -dAPPNAME=${@:%.fxe=%} -dMAIN_STACK_SIZE=${MAINSTACKSPACE} -dTIMER_STACK_SIZE=${TIMERSTACKSPACE} os.def

${APPNAME}_sa.coff ${APPNAME}_sa: ${OBJDIR} ${APPNAME}_sa.link ${OBJS} ${GUI_A} ${STANDALONE_O} 
	${ESDK_LINK32_EXE} -s -m -t -case -coff -wide -sort ${APPNAME}_sa ${APPNAME}_sa

${APPNAME}.coff ${APPNAME}: ${OBJDIR} ${APPNAME}.link ${OBJS} ${GUI_A}
	${ESDK_LINK32_EXE} -s -m -t -case -coff -wide -sort ${APPNAME} ${APPNAME}

${APPNAME}.seb: ${APPNAME}.fxe ${APPNAME}.icn ${APPNAME}.atts
	${ESDK_CREATESEB_EXE} ${APPNAME}.fxe

${APPNAME}.link: os.link ${OBJDIR}/objects.link Makefile
	@cat os.link ${OBJDIR}/objects.link > $@.tmp
	@echo "LIBOBJECT ${GUI_A} 0 os" >>$@.tmp
	@for tt in ${OS_LIBS} ; \
	do \
		echo "LIBRARY $$tt" >>$@.tmp ; \
	done
	@mv $@.tmp $@

${APPNAME}_sa.link: standalone.link ${OBJDIR}/objects.link Makefile
	@cat standalone.link ${OBJDIR}/objects.link > $@.tmp
	@echo "LIBOBJECT ${GUI_A} 1 standalone_boot" >>$@.tmp
	@echo "OBJECT ${STANDALONE_O}" >>$@.tmp
	@echo "marker ram 5 _MAIN_STACK_BOTTOM ${MAINSTACKSPACE}" >>$@.tmp
	@echo "marker ram 7 _TIMER_STACK_BOTTOM ${TIMERSTACKSPACE}" >>$@.tmp
	@mv $@.tmp $@

${OBJDIR}/objects.link: libraries.link Makefile ${EBOOKMAN_SDK}/ebsdk_tools.uses
	@echo "Processing link files..."
	@echo "marker ram 2 _HEAP_START ${HEAPSPACE}" > $@.tmp
	@echo "marker ram 2 _HEAP_END" >> $@.tmp
	@for tt in $(OBJS:%.o=%) ; \
	do \
		echo "OBJECT " $$tt >> $@.tmp ; \
	done
	@sed libraries.link \
		-e 's^%LIBGUI_LIBFILE%^${GUI_A}^g' \
		-e 's^%LIBDATABASES_LIBFILE%^${DB_LIB}^g' \
		-e 's^%LIBHEAPS_LIBFILE%^${HEAPS_LIB}^g' \
		-e 's^%HOSTIO_LIBFILE%^${ESDK_HOSTIO_INSTALL_LIBFILE}^g' \
		-e 's^%LIBGCC_LIBFILE%^${LIBGCC}^g' \
		-e 's^%LIBC_LIBFILE%^${ESDK_NEWLIB_LIBC_INSTALL_LIBFILE}^g' \
		-e 's^%LIBG_LIBFILE%^${ESDK_NEWLIB_LIBG_INSTALL_LIBFILE}^g' \
		-e 's^%LIBM_LIBFILE%^${ESDK_NEWLIB_LIBM_INSTALL_LIBFILE}^g' \
		>> $@.tmp
	@mv $@.tmp $@

# Extra dependencies
${OBJDIR}/adventure.o: Makefile adventure.h adventure.hlp buildno.h
${OBJDIR}/BinFile.o: Makefile BinFile.h

# Compiler and assembler rules
${OBJDIR}/%.s :: %.c
	${ESDK_GCC32_EXE} -b sneak32 -g $(CFLAGS) -mclist -S -o $@ -c $<

${OBJDIR}/%.i :: %.c
	${ESDK_GCC32_EXE} -b sneak32 -g $(CFLAGS) -mclist -E -o $@ -c $<

${OBJDIR}/%.i :: %.cpp
	${ESDK_GXX32_EXE} -b sneak32 -g $(CFLAGS) $(CPPFLAGS) -mclist -E -o $@ -c $<

${OBJDIR}/%.o :: %.cpp
	${ESDK_GXX32_EXE} -b sneak32 -g $(CFLAGS) $(CPPFLAGS) -o $@ -c $<


${OBJDIR}/%.o :: %.c
	${ESDK_GCC32_EXE} -b sneak32 -g $(CFLAGS) -o $@ -c $<

${OBJDIR}/%.o :: %.s
	${ESDK_ASM32_EXE} -case ${INCFLAGS} -po ${OBJDIR} -o $<

${OBJDIR}:
	mkdir -p $@

sGDB: Makefile
	@echo "Creating new \"$@\" ..."
	@echo "#! /bin/sh" > $@.tmp
	@echo "# ===== WARNING: Generated file - do not edit! =====" >> $@.tmp
	@echo "# ===== WARNING: Generated file - do not edit! =====" >> $@.tmp
	@echo "${ESDK_INSIGHT_EXE} ${APPNAME}_sa.coff ${GDBDIRS} " >> $@.tmp
	@chmod a+x $@.tmp
	@mv $@.tmp $@

GDB: os.cmd Makefile
	@echo "Creating new \"$@\" ..."
	@echo "#! /bin/sh" > $@.tmp
	@echo "# ===== WARNING: Generated file - do not edit! =====" >> $@.tmp
	@echo "# ===== WARNING: Generated file - do not edit! =====" >> $@.tmp
	@echo "${ESDK_INSIGHT_EXE} ${APPNAME}.coff -command=os.cmd ${GDBDIRS}" >> $@.tmp
	@chmod a+x $@.tmp
	@mv $@.tmp $@

os.cmd: ${EBOOKMAN_SDK}/ebsdk_tools.uses Makefile
	@echo "Creating new \"$@\" ..."
	@sed -e 's=%%BOOT_ROM_PATH%%=${OS_BOOT}=' \
	     -e 's=%%OS_ROM_PATH%%=${OS_STREAM}=' \
	     <${OS_GDBCMD} >$@.tmp
	@echo "rsim os_task /numeric 20 0" >> $@.tmp
	@mv $@.tmp $@

${APPNAME}.icn: ${APPNAME}.bmp
	${ESDK_UTIL_INSTALL_BIN}/bmpconvert -art_palette -to_bin -outfile $@ ${APPNAME}.bmp

# Don't put these two 'cp' commands on the same line to guard against an
# empty ${SYNC_OUT_DIR} variable
install_to_sync: ${SYNC_OUT_DIR} ${APPNAME}.icn ${APPNAME}.fxe
	cp -p ${APPNAME}.fxe ${SYNC_OUT_DIR}
	cp -p ${APPNAME}.icn ${SYNC_OUT_DIR}
	cp -p initial.mom.${APPNAME} ${SYNC_OUT_DIR}

tar:
	tar cvzf adventure.tgz $(ALL_SOURCE)