#*******************************************************************************
#*                              uBee512 Makefile                               *
#*             An emulator for the Premium Microbee 512K FDD model             *
#*                                                                             *
#*                    Copyright (C) 2007-2008 Stewart Kay                      *
#*******************************************************************************
#
#===============================================================================
# REVISION HISTORY (Most recent at top)
#===============================================================================
# v2.4.0 - S.J.Kay 29/1/2008
# --------------------------
# - Added libdsk library (-ldsk) and -DUSE_LIBDSK
# - Added libdskrc file to distribution
#
# v2.3.0 - S.J.Kay 5/1/2008
# ---------------------------
# - Added z80debug.c and scc.c module.
# - Removed -DINFOFILE=$(INFOFILE) from Linux build
#
# v2.2.0 - S.J.Kay 13/12/2007
# ---------------------------
# - Added emulator CP/M tools.
# - Added documention: microbee_disk_to_image.txt
# - Changed APPIDSTR to =\"$(APP)\"
# - Install target added for FreeBSD.
#
# v2.1.0 - S.J.Kay 19/10/2007
# ---------------------------
# - Added clock.o module.
#
# v2.0.0 - S.J.Kay 17/10/2007
# ---------------------------
# - Added changes for Unices builds for a message box.
# - Added keystd.o, keytc.o, rtc.o, mbox.o and fallback.o modules.
# - Added rtc directory.
#
# v1.4.0 - S.J.Kay 25/9/2007
# -------------------------
# - Added a Windows InnoSetup installer "winexedist".  ZIP distribution file is
#   now built with "winzipdist",  to build both added "winalldist", to build
#   all distribution files added "alldist"
# - Added .ico file creation and insertion into win32 exe file and creation of
#   .bmp graphic for SDL from .png
# - Added strip command for all builds resulting in much smaller executables.
# - Added -mwindows to windows build to make a "windows" application.  This
#   stops the cmd prompt in the background appearing.
# - Added -O2 code optimization to all builds.
# - Changes to distribution destinations and contents.
# - Change TITLESTRING to be shorter and more generic.
#
# v1.3.0 - S.J.Kay 31/8/2007
# --------------------------
# - Added async, serial, and printer C modules.
# - Added printer sub directory to Windows ZIP file.
#
# v1.2.0 - S.J.Kay 22/8/2007
# --------------------------
# - Added tapes sub directory to Windows ZIP file
# - Added tape and function C modules.
# - Fixed -DAPPVER=$(APPVER) for Windows, and FreeBSD.  Caused the --version
#   option to crash program.
#
# v1.1.0 - S.J.Kay 17/8/2007
# --------------------------
# Added and changed some variable names,  added -DAPPVER, modified FreeBSD
# build section for v6.2.
#
# v1.0.0 - S.J.Kay 28/6/2007
# --------------------------
# Created initial multi-platform make file to produce Linux, Win32 and FreeBSD
# targets.  The Win32 target is cross compiled on a Linux host.  The FreeBSD
# target currently requires building on a FreeBSD machine. 
#
#===============================================================================
# How to use this GNU make file.
#===============================================================================
# make or gmake if on a FreeBSD machine will show the options available.
#
# !!!!! Do a make clean after changing any header files before compiling !!!!!
#
#===============================================================================
# Requirements
#===============================================================================
# Requirements for all targets:
# - NASM assembler (Netwide Assembler)
# - MZ80 package (v3.4 appears to be the latest)
# - Application sources !
# - Set the xCINC and xCLIB in this Makefile for each target to suit your own
#   development platform.
#
# Requirements for native target:
# - GCC tool chain
# - The SDL development package
# - Athena and X development packages (for message box).
#
# Requirements for cross compile target (win32):
# - For the Win32 build the i386-mingw32 cross compile tool chain is required.
# - The SDL development package for mingw32 must be installed using the cross
#   compile option.  The WCINC and WCLIB values must match the location of the
#   libs and includes directories created.

#===============================================================================
# Overrides
#===============================================================================
.SUFFIXES:

#===============================================================================
# Archiving definitions
#===============================================================================
APPROOT=../../ubee512

#===============================================================================
# Application specific definitions
#===============================================================================
APP=ubee512
VERSION=2.4.0
TITLE=uBee512

APPVER=\"$(VERSION)\"
APPIDSTR=\"$(APP)\"
TITLESTRING=\"$(TITLE)\ v$(VERSION)\ -\ Microbee\ emulator\"
ICONSTRING=\"$(TITLE)\"

OBJC=DIR/$(APP).o DIR/crtc.o DIR/fdc.o DIR/gui.o DIR/keystd.o DIR/keytc.o
OBJC+=DIR/keyb.o DIR/memmap.o DIR/vdu.o DIR/z80.o DIR/disk.o DIR/pio.o
OBJC+=DIR/sound.o DIR/paknet.o DIR/tape.o DIR/function.o DIR/async.o
OBJC+=DIR/serial.o DIR/printer.o DIR/rtc.o DIR/clock.o DIR/scc.o
OBJC+=DIR/z80debug.o DIR/mbox.o DIR/fallback.o

OBJA=DIR/mz80.o

# Platform prefixes: P1=linux, P2=win32, P3=FreeBSD
P1_OBJC=$(OBJC:DIR/%=linux/%)
P1_OBJA=$(OBJA:DIR/%=linux/%)

P2_OBJC=$(OBJC:DIR/%=win32/%)
P2_OBJA=$(OBJA:DIR/%=win32/%)

P3_OBJC=$(OBJC:DIR/%=freebsd/%)
P3_OBJA=$(OBJA:DIR/%=freebsd/%)

ICON=win32/$(APP)-logo.o

#===============================================================================
# Executables and flags required by all targets 
#===============================================================================
XCC = gcc
XCFLAGS=-Wall
NASM=nasm

#===============================================================================
# Default target
#===============================================================================
help:
	@echo "This is the $(APP) v$(VERSION) Microbee emulator GNU makefile"
	@echo "You must specify one of the following:"
	@echo
	@echo "make linux            native Linux build"
	@echo "gmake freebsd         native FreeBSD build"
	@echo "make win              cross compiled Windows build"
	@echo "make all              make Linux and Windows builds"
	@echo "make clean            removes all built platform files"
	@echo "make srcdist          make source distribution"
	@echo "make alldist          make source, win32 EXE and ZIP binary distros"
	@echo "make winexedist       make win32 executable installer distribution"
	@echo "make winzipdist       make win32 ZIP binary distribution"
	@echo "make winalldist       make win32 EXE and ZIP binary distributions"
	@echo "make help             this help information"
	@echo "make install-linux    installs the Linux binary files"
	@echo "make install-freebsd  installs the freebsd binary files"
	@echo "make uninstall        uninstalls the binary files"
	
#===============================================================================
# Linux target
#===============================================================================
CC = gcc
STRIP=strip
DEBUG=-g
CFLAGS=$(DEBUG) -O2 -Wall
CINC=-I/usr/include/SDL
CLIB=-L/usr/lib -lSDL -lpthread
CLIB+=-lXaw3d -lXt -lX11 -ldsk
CDEF=-D_GNU_SOURCE=1 -D_REENTRANT -Duse_audio
CDEF+=-DAPPVER=$(APPVER) -DTITLESTRING=$(TITLESTRING) -DICONSTRING=$(ICONSTRING)
CDEF+=-DAPPIDSTR=$(APPIDSTR) -DUSE_LIBDSK

linux: linux/$(APP)

linux/$(APP): makez80 $(P1_OBJC) $(P1_OBJA)
# bmp
	$(CC) $(P1_OBJC) $(P1_OBJA) $(CLIB) -o linux/$(APP)
	$(STRIP) linux/$(APP)

linux/%.o: %.c
	[ -d linux ] || mkdir linux
	$(CC) -c $(CFLAGS) $(CINC) $(CDEF) $(*).c -o linux/$(*).o

linux/%.o: %.asm
	[ -d linux ] || mkdir linux
	$(NASM) -f elf mz80.asm
	mv $(*).o linux/

#===============================================================================
# Windows target
# 
# png2ico is a simple command line program to convert PNG files to ICO format.
# http://www.winterdrache.de/freeware/png2ico/index.html
#===============================================================================
WCC=i386-mingw32-gcc
WSTRIP=i386-mingw32-strip
WWINDRES=i386-mingw32-windres
WDEBUG=-g
WCFLAGS=$(WDEBUG) -O2 -Wall
WCINC=-I/usr/local/i386-mingw32/include
WCLIB=-L/usr/local/i386-mingw32/lib -lmingw32 -lSDLmain -lSDL -ldsk -mwindows
WCDEF=-D_GNU_SOURCE=1 -D_REENTRANT -DMINGW -Duse_audio
WCDEF+=-DAPPVER=$(APPVER) -DTITLESTRING=$(TITLESTRING) -DICONSTRING=$(ICONSTRING)
WCDEF+=-DAPPIDSTR=$(APPIDSTR) -DUSE_LIBDSK

win: win32/$(APP).exe

win32/$(APP).exe: makez80 $(P2_OBJC) $(P2_OBJA) icon
# bmp
	$(WCC) $(P2_OBJC) $(P2_OBJA) $(ICON) $(WCLIB) -o win32/$(APP).exe
	$(WSTRIP) win32/$(APP).exe

win32/%.o: %.c
	[ -d win32 ] || mkdir win32
	$(WCC) -c $(WCFLAGS) $(WCINC) $(WCDEF) $(*).c -o win32/$(*).o

win32/%.o: %.asm
	[ -d win32 ] || mkdir win32
	$(NASM) -f win32 mz80.asm
	mv mz80.obj win32/mz80.o

icon: ../images/$(APP)-logo.rc ../images/$(APP)-logo.png
	png2ico ../images/$(APP)-logo.ico --colors 256 ../images/$(APP)-logo.png
	cd ../images && $(WWINDRES) $(APP)-logo.rc $(APP)-logo.o
	mv ../images/$(APP)-logo.o $(ICON)

#===============================================================================
# FreeBSD target (not a cross compile)
#===============================================================================
FCC = gcc
FSTRIP=strip
FDEBUG=-g
FCFLAGS=$(DEBUG) -O2 -Wall
#
# FreeBSD v6.2
FCINC=-I/usr/local/include/SDL -I/usr/local/include
FCINC+=-I/usr/X11R6/include
FCLIB=-L/usr/local/lib -lSDL -lSDLmain
FCLIB+=-L/usr/X11R6/lib -lXaw3d -lXt -lX11
FCDEF=-D_GNU_SOURCE=1 -D_REENTRANT -Duse_audio
FCDEF+=-DAPPVER=$(APPVER) -DTITLESTRING=$(TITLESTRING) -DICONSTRING=$(ICONSTRING)
FCDEF+=-DAPPIDSTR=$(APPIDSTR) -DUSE_LIBDSK

freebsd: freebsd/$(APP)

freebsd/$(APP): makez80 $(P3_OBJC) $(P3_OBJA)
# bmp
	$(FCC) -pthread $(P3_OBJC) $(P3_OBJA) $(FCLIB) -o freebsd/$(APP)
	$(FSTRIP) freebsd/$(APP)	

freebsd/%.o: %.c
	[ -d freebsd ] || mkdir freebsd
	$(FCC) -c $(FCFLAGS) $(FCINC) $(FCDEF) $(*).c -o freebsd/$(*).o

freebsd/%.o: %.asm
	[ -d freebsd ] || mkdir freebsd
	$(NASM) -f elf mz80.asm
	mv $(*).o freebsd/

#===============================================================================
# Build Linux and Windows targets
#===============================================================================
all: linux/$(APP) win32/$(APP).exe

#===============================================================================
# Create the makez80 binary
#===============================================================================
makez80: makez80.c
	$(XCC) $(XCFLAGS) -o $@ makez80.c

#===============================================================================
# Run the makez80 binary to produce the mz80.asm file
#===============================================================================
mz80.asm: makez80
	./makez80 mz80.asm -l -s -x86

#===============================================================================
# Convert the logo PNG file to BMP format.
# Convert is part of the ImageMagick(1) suite of tools.
#
# !!!!! For now just create with Gimp, need to sort out transparency issue
# later.
#===============================================================================
bmp: ../images/$(APP)-logo.png
	convert ../images/$(APP)-logo.png ../images/$(APP)-logo.bmp

#===============================================================================
# General maintenance
#===============================================================================
clean:
	rm -f $(P1_OBJC) $(P1_OBJA) linux/$(APP) \
              $(P2_OBJC) $(P2_OBJA) win32/$(APP).exe \
              $(P3_OBJC) $(P3_OBJA) freebsd/$(APP) \
              ../distributions/$(APP)-$(VERSION).tar.gz \
              ../distributions/$(APP)-$(VERSION)-win32.zip \
              ../distributions//$(APP)-$(VERSION)-win32.exe \
              makez80.o makez80 \
              $(ICON)
	rm -Rf win32/$(APP)

#===============================================================================
# Install and remove Linux installed binaries, and shared application directory
#===============================================================================
PREFIX=/usr/local
BINDIR=$(PREFIX)/bin
APPDIR=$(PREFIX)/share/$(APP)
DOCDIR=$(APPDIR)/doc
DISKDIR=$(APPDIR)/disks
TOOLSDIR=$(APPDIR)/tools
IMAGEDIR=$(APPDIR)/images
LIBDSKDIR=$(APPDIR)/libdsk

installdirs:
	/bin/sh ./mkinstalldirs $(BINDIR)
	/bin/sh ./mkinstalldirs $(DOCDIR)
	/bin/sh ./mkinstalldirs $(IMAGEDIR)
	/bin/sh ./mkinstalldirs $(DISKDIR)
	/bin/sh ./mkinstalldirs $(TOOLSDIR)/cpm/src/	
	/bin/sh ./mkinstalldirs $(DISKDIR)
	/bin/sh ./mkinstalldirs $(LIBDSKDIR)	

install-linux: installdirs
	install -m 755 linux/$(APP) $(BINDIR)
	cp ../images/$(APP)-logo.bmp $(IMAGEDIR)/
	cp ../images/info.xbm $(IMAGEDIR)/	
	cp ../images/excl.xbm $(IMAGEDIR)/
	cp -R ../doc/*[!~] $(DOCDIR)/
	cp -R ../tools/cpm/src/* $(TOOLSDIR)/cpm/src/
	cp -R ../disks/* $(DISKDIR)/
	cp ../libdsk/libdskrc $(LIBDSKDIR)/

install-freebsd: installdirs
	install -m 755 freebsd/$(APP) $(BINDIR)
	cp ../images/$(APP)-logo.bmp $(IMAGEDIR)/
	cp ../images/info.xbm $(IMAGEDIR)/	
	cp ../images/excl.xbm $(IMAGEDIR)/
	cp -R ../doc/*[!~] $(DOCDIR)/
	cp -R ../tools/cpm/src/* $(TOOLSDIR)/cpm/src/
	cp -R ../disks/* $(DISKDIR)/
	cp ../libdsk/libdskrc $(LIBDSKDIR)	

install:
	@echo "Please use \"make install-linux\" or \"gmake install-freebsd\""

uninstall:
	rm $(BINDIR)/$(APP)
	rm -Rf $(APPDIR)
	
#===============================================================================
# Create the source,  ZIP and win32 binary installer distributions.
#===============================================================================
alldist: srcdist winzipdist winexedist

#===============================================================================
# Create the ZIP and win32 binary installer distributions.
#===============================================================================
winalldist: winzipdist winexedist

#===============================================================================
# Create source distribution
#===============================================================================
# Create a source tar distribution. This should contain the following:
# SDL.dll, tools (all bins), image (tools), doc
srcdist: create_distrodir create_toolsimages
	rm -f ../distributions/$(APP)-$(VERSION).tar.gz
	ln -s $(APP) $(APPROOT)-$(VERSION)
	tar cvzf $(APP)-$(VERSION).tar.gz \
	$(APPROOT)-$(VERSION)/src/*.c \
	$(APPROOT)-$(VERSION)/src/*.h \
	$(APPROOT)-$(VERSION)/src/Makefile \
	$(APPROOT)-$(VERSION)/src/mkinstalldirs \
 	$(APPROOT)-$(VERSION)/libs/SDL.dll \
	$(APPROOT)-$(VERSION)/doc/MZ80/MZ80.TXT \
	$(APPROOT)-$(VERSION)/doc/GPL/COPYING \
	$(APPROOT)-$(VERSION)/doc/LGPL/lgpl-3.0.txt \
	$(APPROOT)-$(VERSION)/doc/ChangeLog \
	$(APPROOT)-$(VERSION)/doc/README \
	$(APPROOT)-$(VERSION)/doc/quickstart.txt \
	$(APPROOT)-$(VERSION)/doc/windows.txt \
	$(APPROOT)-$(VERSION)/doc/license.txt \
        $(APPROOT)-$(VERSION)/doc/microbee_disk_to_image.txt \
 	$(APPROOT)-$(VERSION)/images/$(APP)-logo.png \
 	$(APPROOT)-$(VERSION)/images/$(APP)-logo.bmp \
 	$(APPROOT)-$(VERSION)/images/$(APP)-logo.rc \
 	$(APPROOT)-$(VERSION)/images/info.xbm \
 	$(APPROOT)-$(VERSION)/images/excl.xbm \
 	$(APPROOT)-$(VERSION)/tools/cpm/src/FIXTURBO.PAS \
 	$(APPROOT)-$(VERSION)/tools/cpm/src/CPM2HOST.PAS \
 	$(APPROOT)-$(VERSION)/tools/cpm/src/C2H.SUB \
 	$(APPROOT)-$(VERSION)/tools/cpm/src/HOST2CPM.PAS \
 	$(APPROOT)-$(VERSION)/tools/cpm/src/H2C.SUB \
 	$(APPROOT)-$(VERSION)/tools/cpm/src/HOSTDIR.PAS \
 	$(APPROOT)-$(VERSION)/tools/cpm/src/HD.SUB \
 	$(APPROOT)-$(VERSION)/tools/cpm/src/PARMFUNC.INC \
 	$(APPROOT)-$(VERSION)/tools/cpm/src/RESET.AZM \
 	$(APPROOT)-$(VERSION)/tools/cpm/src/RESET.SUB \
 	$(APPROOT)-$(VERSION)/tools/cpm/src/EXITEMU.AZM \
 	$(APPROOT)-$(VERSION)/tools/cpm/src/EXITEMU.SUB \
 	$(APPROOT)-$(VERSION)/disks/ubee512_cpm_tools.????_ \
 	$(APPROOT)-$(VERSION)/libdsk/libdskrc
	rm $(APPROOT)-$(VERSION)
	mv $(APP)-$(VERSION).tar.gz ../distributions/

#===============================================================================
# Create a win32 binary installer distribution.
#
# The installer is compiled and built using InnoSetup.  The ubee512.exe and
# SDL.dll files are handled directly by the installer.  This is a Windows
# program and is run under wine.  Drive e: is setup to this project
# directory using wineconfig.
#===============================================================================
winexedist:  create_distrodir create_toolsimages win_build_dir
	rm -f ../InnoSetup/$(APP)-$(VERSION)-win32.exe
	rm -f ../distributions/$(APP)-$(VERSION)-win32.exe
	sed "s/_VERSION_/$(VERSION)/g" ../InnoSetup/$(APP).iss > \
	../InnoSetup/$(APP)-$(VERSION).iss
	wine c:/Programs/InnoSetup5/Compil32.exe /cc \
	e:\\ubee512\\InnoSetup\\$(APP)-$(VERSION).iss
	mv ../InnoSetup/$(APP)-$(VERSION)-win32.exe ../distributions/

#===============================================================================
# Create a win32 binary ZIP file distribution.
#===============================================================================
winzipdist: create_distrodir create_toolsimages win_build_dir
	rm -f ../distributions/$(APP)-$(VERSION)-win32.zip
	cp ../libs/SDL.dll win32/$(APP)/
	cp win32/$(APP).exe win32/$(APP)/
	cd win32 && zip -r $(APP)-$(VERSION)-win32.zip $(APP)
	mv win32/$(APP)-$(VERSION)-win32.zip ../distributions/

#===============================================================================
# Build the win32 directory for the Windows distributions.  The ubee512.exe
# and SDL.dll files are handled by each install method seperately.
#===============================================================================
win_build_dir:
	rm -Rf win32/$(APP)
	mkdir win32/$(APP)
	mkdir win32/$(APP)/disks
	mkdir win32/$(APP)/roms
	mkdir win32/$(APP)/tapes
	mkdir win32/$(APP)/printer	
	mkdir win32/$(APP)/images
	mkdir win32/$(APP)/rtc	
	mkdir win32/$(APP)/doc
	mkdir win32/$(APP)/doc/MZ80
	mkdir win32/$(APP)/doc/GPL
	mkdir win32/$(APP)/doc/LGPL
	mkdir win32/$(APP)/tools
	mkdir win32/$(APP)/share
	cp ../disks/ubee512_cpm_tools.????_ win32/$(APP)/disks/
	cp ../doc/MZ80/MZ80.TXT win32/$(APP)/doc/MZ80/
	cp ../doc/GPL/COPYING win32/$(APP)/doc/GPL/
	cp ../doc/LGPL/lgpl-3.0.txt win32/$(APP)/doc/LGPL/
	cp ../doc/ChangeLog win32/$(APP)/doc/
	cp ../doc/README win32/$(APP)/doc/
	cp ../doc/quickstart.txt win32/$(APP)/doc/
	cp ../doc/windows.txt win32/$(APP)/doc/
	cp ../doc/license.txt win32/$(APP)/doc/
	cp ../doc/microbee_disk_to_image.txt win32/$(APP)/doc/	
	cp ../images/$(APP)-logo.bmp win32/$(APP)/images/
	cp ../libdsk/libdskrc win32/$(APP)/share/
	unix2dos win32/$(APP)/share/libdskrc

#===============================================================================
# Build raw disk images for the ubee512 CP/M tools for all common Microbee
# formats. This requires cpmtools 2.6 or later with the necessary patch.  Images
# are initially created with mkfs.cpm as these are designed to grow and keep
# the distribution and installed size down.  All images will be write protected
# by appending a '_' character to the image name.
#===============================================================================
create_toolsimages:
	mkfs.cpm -f ss80 ../disks/ubee512_cpm_tools.ss80_
	cpmcp -f ss80 ../disks/ubee512_cpm_tools.ss80_ ../tools/cpm/bin/* 0:
	mkfs.cpm -f ds40 ../disks/ubee512_cpm_tools.ds40_
	cpmcp -f ds40 ../disks/ubee512_cpm_tools.ds40_ ../tools/cpm/bin/* 0:
	mkfs.cpm -f ds80 ../disks/ubee512_cpm_tools.ds80_
	cpmcp -f ds80 ../disks/ubee512_cpm_tools.ds80_ ../tools/cpm/bin/* 0:
	mkfs.cpm -f ds82 ../disks/ubee512_cpm_tools.ds82_
	cpmcp -f ds82 ../disks/ubee512_cpm_tools.ds82_ ../tools/cpm/bin/* 0:
	mkfs.cpm -f ds84 ../disks/ubee512_cpm_tools.ds84_
	cpmcp -f ds84 ../disks/ubee512_cpm_tools.ds84_ ../tools/cpm/bin/* 0:

#===============================================================================
# Create the distribution directory if it does not exist.
#===============================================================================
create_distrodir:
	[ -d ../distributions ] || mkdir ../distributions
