############################################################################
##									  ##
##				RAINE					  ##
##									  ##
############################################################################

##
## type make RAINECPS=1 to build rainecps
## The default is to build a dynamic version. Use make STATIC=1 to build a
## static version.
##
## Requirements: read the docs !
##

# version (when the version increases, raine shows the issue dialog on
# startup
VERSION = "0.50.4"

# Comment out if you don't want the debug features
# RAINE_DEBUG = 1

# Be verbose ?
# VERBOSE = 1

# Use asm video core ? (comment to use C core)
ASM_VIDEO_CORE = 1

# Try to detect mingw... If you want to build the dos and the mingw
# version on the same system you should unset djdir before making
# the mingw version.
ifeq ("$(shell uname)","Linux")
OSTYPE=linux-gnu
endif

ifeq ("$(shell uname)","FreeBSD")
OSTYPE=linux-gnu
endif

ifeq ("$(OSTYPE)","msys")
MINGDIR=1
OSTYPE=mingw32
endif

ifeq ("$(shell nasm -r)","")
ifdef VERBOSE
ASM=nasmw
else
ASM=@nasmw
endif
else
ifdef VERBOSE
ASM=nasm
else
ASM=@nasm
endif
endif

ifdef mingdir
MINGDIR=1
endif

ifdef MINGDIR
# mingw
RAINE32 = 1
OSTYPE = mingw32
endif

RM =	@rm -f
ifeq ($(CC),)
CC=gcc
endif
ifeq ($(CXX),)
CXX=g++
endif

# This test is for stupid win32 gcc ports with bad defaults
ifeq ($(CC),cc)
CC=gcc
endif
ifdef VERBOSE
CCV=$(CC)
CXXV=$(CXX)
else
CCV=@$(CC)
CXXV=@$(CXX)
endif

MD =	@mkdir

# error logging (djgpp - old stuff)
# CC =	redir -ea errorlog.txt gcc

# profiling
# CC =	gcc -pg

INCDIR= -I. \
	-Isource \
	-Isource/68000 \
	-Isource/68020 \
	-Isource/z80 \
	-Isource/6502 \
	-Isource/m68705 \
	-Isource/sound \
	-Isource/games \
	-Isource/video \
	-Isource/mame
ifdef RAINE_DEBUG
INCDIR +=	-Isource/alleg/debug
endif

ifeq ($(OSTYPE),cygwin)

   # Cygwin


RAINE_EXE = raine32.exe
RAINECPS_EXE = rainecps32.exe

AFLAGS = -f coff

RAINE32 = 1

DEFINE = -D__RAINE__ \
	   -DRAINE_WIN32 \

   PNG_LFLAGS = "$(shell libpng-config --ldflags)"
   PNG_STATIC_LFLAGS = "$(shell libpng-config --static --ldflags)"
   LIBS = -lz -lalleg $(PNG_LFAGS)
   LIBS_STATIC = -lz -lalleg_s -lkernel32 -luser32 -lgdi32 -lcomdlg32 \
   -lole32 -ldinput -lddraw -ldxguid -lwinmm -ldsound \
   -L/lib/mingw -lmoldname -lmsvcrt $(PNG_STATIC_LFLAGS) 

LIBS_DEBUG = -lz -lalld $(PNG_LFLAGS)

  LFLAGS = -mno-cygwin -mwindows

else
ifdef RAINE32

   # MINGW32

SDL = 1
   RAINE_EXE = raine32.exe
   RAINECPS_EXE = rainecps32.exe
ifdef CROSSCOMPILE
	ASM = @nasm
	MD = @mkdir
else
ifndef ASM
   ASM = @nasmw.exe # auto-detection broken for mingw !!!
endif
   MD = @mkdir.exe # to avoid the built-in command... strange it's necessary...
endif

   AFLAGS = -f coff

   PNG_CFLAGS = "$(shell libpng-config --cflags)"
   PNG_LFLAGS = $(shell libpng-config --ldflags)
   PNG_STATIC_LFLAGS = "$(shell libpng-config --static --ldflags)"
   DEFINE = -D__RAINE__ \
	   -DRAINE_WIN32 \

   LIBS = -lz $(PNG_LFLAGS)
   LIBS_STATIC = -lz  $(PNG_STATIC_LFLAGS)
   INCDIR += $(PNG_CFLAGS)

LIBS_DEBUG = -lz $(PNG_LFLAGS)

ifndef SDL
LIBS += -lalleg
LIBS_STATIC += -lalleg_s -lkernel32 -luSer32 -lgdi32 -lcomdlg32 \
   -lole32 -ldinput -lddraw -ldxguid -lwinmm -ldsound
LIBS_DEBUG += -lalld
endif

ifndef RAINE_DEBUG
# this one hides the console in windows
   LFLAGS = -mwindows 
endif
else
ifdef DJDIR
   RAINE_EXE = Raine.exe
   RAINECPS_EXE = rainecps.exe
   RAINE_DOS = 1

ifdef CROSSCOMPILE
	ASM = @nasm
else
	ASM = @nasmw.exe # auto-detection broken for djgpp !!!
endif

   OSTYPE = dos
   AFLAGS = -f coff

   DEFINE = -D__RAINE__ \
	   -DRAINE_DOS

   LIBS = -lalleg -lpng -lz -lm
else
# linux

SDL = 1

   prefix = $(DESTDIR)/usr
   bindir = $(prefix)/games
   sharedir = $(prefix)/share/games
   mandir = $(prefix)/man/man6
   rainedata = $(sharedir)/raine
ifndef SDL
   langdir = $(rainedata)/languages
else
   bitmaps_dir = $(rainedata)/bitmaps
   fonts_dir = $(rainedata)/fonts
endif
   romdir = $(rainedata)/roms
   artdir = $(rainedata)/artwork
   emudxdir = $(rainedata)/emudx

   RAINE_EXE = raine
ifndef SDL
   RAINE_DAT = raine.dat
   RAINE_LNG = brasil.cfg dansk.cfg espanol.cfg french2.cfg german2.cfg japanese.cfg spanish.cfg turkish.cfg catala.cfg dutch.cfg euskera.cfg french.cfg german.cfg polish.cfg svenska.cfg czech.cfg english.cfg finnish.cfg galego.cfg italian.cfg portugal.cfg template.cfg
endif
   RAINECPS_EXE = rainecps
   RAINE_UNIX = 1

   INSTALL = /usr/bin/install
   INSTALL_BIN = $(INSTALL) -m 755
   INSTALL_DATA = $(INSTALL) -m 644
   RD = rmdir --ignore-fail-on-non-empty
   CD = cd

   AFLAGS = -f elf

   PNG_CFLAGS = "$(shell libpng-config --cflags)"
ifndef SDL
ALLEGRO_CFLAGS = "$(shell allegro-config --cflags)"
endif
   SDL_CFLAGS = "$(shell sdl-config --cflags)"

   INCDIR += $(PNG_CFLAGS) $(ALLEGRO_CFLAGS) $(SDL_CFLAGS) -I$(X11BASE)/include -I$(LOCALBASE)/include


   DEFINE = -D__RAINE__ \
	   -DRAINE_UNIX \

ifndef SDL
   LIBS = -lz `allegro-config --libs` `libpng-config --ldflags` 
   LIBS_DEBUG = -lz `allegro-config --libs ` `libpng-config --ldflags`
   LIBS_STATIC = -lz `allegro-config --static` `libpng-config --static --ldflags`
else
   LIBS = -lz `libpng-config --ldflags` 
   LIBS_DEBUG = -lz `libpng-config --ldflags`
   LIBS_STATIC = -lz `libpng-config --static --ldflags`
endif

ifndef SDL
ifeq ("$(shell if [ -e /usr/include/vga.h ] || [ -e /usr/local/include/vga.h ]; then echo yes; fi)","yes")
GFX_SVGALIB=1
endif
endif

ifdef GFX_SVGALIB
   LIBS += -lvga
   LIBS_DEBUG += -lvga
   LIBS_STATIC += -lvga
endif # GFX_SVGALIB

endif # linux / mingw32
endif # djgpp
endif # if OSTYPE == cygwin

ifndef VERBOSE
ASM := @$(ASM)
endif

# Uncomment if you want to use the SEAL audio library (linux, dos or win32)
# seal is dead since the end of 1998, and it's not actievely maintained anymore in raine,
# so expect problems if you enable this.
ifdef RAINE_UNIX
# SEAL = 1
else
ifdef RAINE32
# SEAL = 1
else
ifdef RAINE_DOS
# In dos seal is better to handle pci soundcards.
# SEAL = 1
endif
endif
endif

ifdef SEAL
LIBS += -laudio
LIBS_STATIC += -laudio
ifdef RAINE32
LIBS += -lmsvcrt \
	-lkernel32 -lwinmm \
	-lole32 -luser32 \
 -ldsound
endif
endif

ifdef SDL
INCDIR += -I source/sdl
else
INCDIR += -Isource/alleg/gui
endif

# To allow corss-compilation, we need one dir / target
OBJDIR = $(OSTYPE)
ifdef SDL
OBJDIR = $(OSTYPE)-sdl
endif

include cpuinfo

ifdef X86_64
OBJDIR := $(OBJDIR)64
endif

ifeq "$(RAINE32) $(STATIC)" "1 1"
# Windows need a separate object dir for the static version (for allegro)
OBJDIR := $(OBJDIR)/static
else
OBJDIR := $(OBJDIR)/object
endif
ifdef RAINECPS
OBJDIR := $(OBJDIR)/rainecps
RAINE_EXE := $(RAINECPS_EXE)
endif

ifdef RAINE_DEBUG
OBJDIR := $(OBJDIR)d
endif

ifdef ASM_VIDEO_CORE
VIDEO_CORE = $(OBJDIR)/video/i386
else
VIDEO_CORE = $(OBJDIR)/video/c
endif

OBJDIRS=$(OBJDIR) \
	$(OBJDIR)/mame \
	$(OBJDIR)/sound \
	$(OBJDIR)/68000 \
	$(OBJDIR)/68020 \
	$(OBJDIR)/z80 \
	$(OBJDIR)/6502 \
	$(OBJDIR)/m68705 \
	$(OBJDIR)/video \
	$(OBJDIR)/video/c \
	$(OBJDIR)/video/i386 \
	$(OBJDIR)/video/i386/newspr2 \
	$(OBJDIR)/video/i386/packed \
	$(VIDEO_CORE)/str \
	$(VIDEO_CORE)/blit_x2 \
	$(OBJDIR)/video/zoom \
	$(OBJDIR)/games \

ifdef SDL
OBJDIRS += \
	$(OBJDIR)/sdl \
	$(OBJDIR)/sdl/SDL_gfx \
	$(OBJDIR)/sdl/gui \
	$(OBJDIR)/sdl/dialogs

else
OBJDIRS += $(OBJDIR)/alleg \
	$(OBJDIR)/alleg/png 
endif

ifdef SEAL
OBJDIRS += $(OBJDIR)/seal
endif

ifdef RAINE_DEBUG
OBJDIRS += $(OBJDIR)/alleg/debug \
	$(OBJDIR)/alleg/debug/dz80
endif

ifeq ($(OSTYPE),cygwin)
INCDIR += -I/usr/local/include
endif

ifdef RAINE_DEBUG
CFLAGS = $(INCDIR) $(DEFINE) $(_MARCH) -Wall -g -DRAINE_DEBUG
CFLAGS_MCU = $(INCDIR) $(DEFINE) $(_MARCH) -Wall -g -DRAINE_DEBUG

else
# All the flags are optimisations except -fomit-frame-pointer necessary for
# the 68020 core in dos. -Wno-trigraphs suppress some anoying warnings with
# gcc-2.96
# These flags are for gcc-2.x

ifdef RAINE32
# when starting a game -> black screen if -O > 1 (bug in uint64 calculation)
CFLAGS = -O1
else
# Seems to work now, at least with the sdl version ? (to be tested with windows !)
CFLAGS = -O3
endif

CFLAGS += $(INCDIR) \
	$(DEFINE) \
	$(_MARCH) \
	-Wno-trigraphs \
	-fschedule-insns2 \
	-funroll-all-loops \
	-fexpensive-optimizations \
	-ffast-math \
	-w \
	-fomit-frame-pointer

# This is required for gcc-2.9x (bug in -fomit-frame-pointer)
CFLAGS_MCU = $(_MARCH) -O3 -fexpensive-optimizations # switches for the 68705 mcus

ifdef RAINE_UNIX
CFLAGS += -pipe
endif
# The accumulate-outgoing-args is for gcc3, but I am not sure it has any
# effect... And I could not notice any improvement with my duron if I change
# the cpu and arch to athlon... For now I comment the gcc3 option for the
# win32 version (no gcc3 for win32 for now).
#	 -maccumulate-outgoing-args
#	-pedantic
endif

ifdef GFX_SVGALIB
CFLAGS += -DGFX_SVGA
endif

ifdef X86_64
CFLAGS += -DX86_64
CFLAGS_MCU += -DX86_64
endif

ifdef RAINECPS
CFLAGS += -DNO020 -DRAINECPS
endif
ifdef SDL
CFLAGS += -DSDL 
CFLAGS_MCU += -DSDL
else
OBJDIRS += $(OBJDIR)/alleg \
	$(OBJDIR)/alleg/gui
	$(OBJDIR)/alleg/jpg \

endif

# assembler (gas)
SFLAGS= $(INCDIR) \
	$(DEFINE) \
	-Wall \
	$(_MARCH) \
	-xassembler-with-cpp \

ifeq ($(OSTYPE),cygwin)
CFLAGS += -mno-cygwin
CFLAGS_MCU += -mno-cygwin
SFLAGS += -mno-cygwin
endif

ifdef RAINE_DEBUG

# Debuger

# dz80 interface

DZ80=	$(OBJDIR)/alleg/debug/dz80/dissz80.o \
	$(OBJDIR)/alleg/debug/dz80/dz80_raine.o \
	$(OBJDIR)/alleg/debug/dz80/tables.o

ifndef SDL
# in sdl the debuger will very probably use sdl_console
# for now there is no debuger at all
DEBUG= $(OBJDIR)/alleg/debug/dbg_gui.o \
	$(OBJDIR)/alleg/debug/breakpt.o \
	$(DZ80)
endif

endif

# ASM 68020 core

ASM020= $(OBJDIR)/68020/newcpu.o \
	$(OBJDIR)/68020/readcpu.o \
	$(OBJDIR)/68020/cpustbl.o \
	$(OBJDIR)/68020/cpudefs.o \
	$(OBJDIR)/68020/a020core.o \

# STARSCREAM 68000 core

SC000=	$(OBJDIR)/68000/s68000.oa \
	$(OBJDIR)/68000/starhelp.o \

# MZ80 core

MZ80=	$(OBJDIR)/z80/mz80.oa \
	$(OBJDIR)/z80/mz80help.o

# network core

NET=	$(OBJDIR)/net/d_system.o \
	$(OBJDIR)/net/d_net.o \
	$(OBJDIR)/net/d_netfil.o

# M6502 core

M6502=	$(OBJDIR)/6502/m6502.oa \
	$(OBJDIR)/6502/m6502hlp.o \

# M68705 core

M68705= $(OBJDIR)/m68705/m68705.o \

# virtua i960 core

I960=	$(OBJDIR)/i960/i960cpu.o \

# Video core

VIDEO=	$(OBJDIR)/video/tilemod.o \
	$(OBJDIR)/video/palette.o \
	$(OBJDIR)/video/priorities.o \
	$(OBJDIR)/video/newspr.o \
	$(OBJDIR)/video/spr64.o \
	$(OBJDIR)/video/cache.o \
	$(OBJDIR)/video/res.o \
	$(OBJDIR)/video/scale2x.o \
	$(OBJDIR)/video/scale3x.o \
	$(OBJDIR)/video/i386/move.o \
	$(OBJDIR)/video/i386/newspr2/8.o \
	$(OBJDIR)/video/i386/newspr2/16.o \
	$(OBJDIR)/video/i386/newspr2/32.o \
	$(OBJDIR)/video/i386/packed/8.o \
	$(OBJDIR)/video/i386/packed/16.o \
	$(OBJDIR)/video/i386/packed/32.o \
	$(VIDEO_CORE)/spr8x8_8.o \
	$(VIDEO_CORE)/spr8_16.o \
	$(VIDEO_CORE)/spr8_32.o \
	$(VIDEO_CORE)/str/6x8_8.o \
	$(VIDEO_CORE)/str/6x8_16.o \
	$(VIDEO_CORE)/str/6x8_32.o \
		\
	$(VIDEO_CORE)/16x16_8.o \
	$(VIDEO_CORE)/16x16_16.o \
	$(VIDEO_CORE)/16x16_32.o \
		\
	$(VIDEO_CORE)/16x8_8.o \
	$(VIDEO_CORE)/16x8_16.o \
	$(VIDEO_CORE)/16x8_32.o \
		\
	$(OBJDIR)/video/i386/32x32_8.o \
	$(OBJDIR)/video/i386/32x32_16.o \
	$(OBJDIR)/video/i386/32x32_32.o \
		\
	$(VIDEO_CORE)/blit_x2/8.o \
	$(VIDEO_CORE)/blit_x2/16.o \
	$(VIDEO_CORE)/blit_x2/24.o \
	$(VIDEO_CORE)/blit_x2/32.o \
	$(OBJDIR)/video/zoom/16x16.o \
	$(OBJDIR)/video/zoom/16x16_16.o \
	$(OBJDIR)/video/zoom/16x16_32.o \
	$(OBJDIR)/video/zoom/16x8.o \
	$(OBJDIR)/video/c/lscroll.o \
	$(OBJDIR)/video/alpha.o \
	$(OBJDIR)/video/c/pdraw.o

ifndef SDL
VIDEO += \ 
	$(OBJDIR)/video/arcmon.o \
	$(OBJDIR)/video/arcmode.o \
	$(OBJDIR)/video/eagle.oa
endif

# Sound core

CPS1_SOUND= \
	$(OBJDIR)/sound/2151intf.o \
	$(OBJDIR)/sound/adpcm.o \
	$(OBJDIR)/sound/fm.o \
	$(OBJDIR)/sound/ym2151.o \
	$(OBJDIR)/sound/smp16bit.o \
	$(OBJDIR)/sound/qsound.o \
	$(OBJDIR)/sound/emulator.o

SOUND=	$(OBJDIR)/sound/2610intf.o \
	$(OBJDIR)/sound/ymdeltat.o \
	$(OBJDIR)/sound/2203intf.o \
	$(OBJDIR)/sound/2413intf.o \
	$(OBJDIR)/sound/ym2413.o \
	$(OBJDIR)/sound/3812intf.o \
	$(OBJDIR)/sound/ay8910.o \
	$(OBJDIR)/sound/fmopl.o \
	$(OBJDIR)/sound/ymz280b.o \
	$(OBJDIR)/sound/m6585.o \
	$(OBJDIR)/sound/msm5205.o \
	$(OBJDIR)/sound/es5506.o \
	$(OBJDIR)/sound/ymf278b.o \
	$(OBJDIR)/sound/namco.o \
	$(OBJDIR)/sound/x1_010.o \
	$(OBJDIR)/sound/dac.o \
	$(OBJDIR)/sound/dxsmp.o \
	$(CPS1_SOUND)

# Release Games

CPS1_GAMES= 	$(OBJDIR)/games/cps1.o $(OBJDIR)/games/cps1drv.o $(OBJDIR)/games/cps2drv.o

MCU_OBJ = $(OBJDIR)/games/bubl_mcu.o $(OBJDIR)/games/kiki_mcu.o $(OBJDIR)/games/kick_mcu.o

GAMES=	$(OBJDIR)/games/arabianm.o \
	$(OBJDIR)/games/arkanoid.o \
	$(OBJDIR)/games/arkretrn.o \
	$(OBJDIR)/games/armedf.o \
	$(OBJDIR)/games/ashura.o \
	$(OBJDIR)/games/asuka.o \
	$(OBJDIR)/games/biomtoy.o \
	$(OBJDIR)/games/bloodbro.o \
	$(OBJDIR)/games/bonzeadv.o \
	$(OBJDIR)/games/bshark.o \
	$(OBJDIR)/games/bubblem.o \
	$(OBJDIR)/games/bublbobl.o \
	$(OBJDIR)/games/bubsymph.o \
	$(OBJDIR)/games/cabal.o \
	$(OBJDIR)/games/cadash.o \
	$(OBJDIR)/games/cookbib.o \
	$(OBJDIR)/games/crospang.o \
	$(OBJDIR)/games/ctribebl.o \
	$(OBJDIR)/games/cleofort.o \
	$(OBJDIR)/games/crimec.o \
	$(OBJDIR)/games/darius.o \
	$(OBJDIR)/games/darius2d.o \
	$(OBJDIR)/games/dariusg.o \
	$(OBJDIR)/games/diverboy.o \
	$(OBJDIR)/games/taito_f2.o \
	$(OBJDIR)/games/doubled3.o \
	$(OBJDIR)/games/eaction2.o \
	$(OBJDIR)/games/earthjkr.o \
	$(OBJDIR)/games/esd16.o \
	$(OBJDIR)/games/fchamp.o \
	$(OBJDIR)/games/exzisus.o \
	$(OBJDIR)/games/galpanic.o \
	$(OBJDIR)/games/gcpball.o \
	$(OBJDIR)/games/gekridan.o \
	$(OBJDIR)/games/gunbird.o \
	$(OBJDIR)/games/hitice.o \
	$(OBJDIR)/games/hyperpcb.o \
	$(OBJDIR)/games/kaiserkn.o \
	$(OBJDIR)/games/macrossp.o \
	$(OBJDIR)/games/masterw.o \
	$(OBJDIR)/games/mcatadv.o \
	$(OBJDIR)/games/mitchell.o \
	$(OBJDIR)/games/mugsmash.o \
	$(OBJDIR)/games/ninjak.o \
	$(OBJDIR)/games/ninjaw.o \
	$(OBJDIR)/games/othunder.o \
	$(OBJDIR)/games/opwolf.o \
	$(OBJDIR)/games/pbobble2.o \
	$(OBJDIR)/games/pbobble3.o \
	$(OBJDIR)/games/pirates.o \
	$(OBJDIR)/games/popnpop.o \
	$(OBJDIR)/games/puchicar.o \
	$(OBJDIR)/games/pulirula.o \
	$(OBJDIR)/games/rainbow.o \
	$(OBJDIR)/games/rambo3.o \
	$(OBJDIR)/games/rastan.o \
	$(OBJDIR)/games/rastan2.o \
	$(OBJDIR)/games/robokid.o \
	$(OBJDIR)/games/silentd.o \
	$(OBJDIR)/games/silkworm.o \
	$(OBJDIR)/games/spcinv95.o \
	$(OBJDIR)/games/syvalion.o \
	$(OBJDIR)/games/terrac.o \
	$(OBJDIR)/games/terraf.o \
	$(OBJDIR)/games/tetrist.o \
	$(OBJDIR)/games/toki.o \
	$(OBJDIR)/games/twinqix.o \
	$(OBJDIR)/games/viofight.o \
	$(OBJDIR)/games/warriorb.o \
	$(OBJDIR)/games/wrestlef.o \
	$(OBJDIR)/games/wwfsstar.o \
	$(OBJDIR)/games/zerozone.o \
	$(OBJDIR)/games/ksystem.o \
	$(OBJDIR)/games/gunlock.o \
	$(OBJDIR)/games/gridseek.o \
	$(OBJDIR)/games/cave.o \
	$(OBJDIR)/games/seta.o \
	$(OBJDIR)/games/matmania.o \
	$(MCU_OBJ) \
	$(CPS1_GAMES)

DX_GAMES = \
	$(OBJDIR)/emudx.o \
	$(OBJDIR)/games/pengo.o \
	$(OBJDIR)/games/frogger.o \
	$(OBJDIR)/games/dkong.o \

# ifndef SDL
GAMES += $(DX_GAMES)
# endif

# System drivers

CPS1_SYS=	$(OBJDIR)/games/games.o \
	$(OBJDIR)/games/default.o

SYSDRV= \
	$(OBJDIR)/games/f3system.o \
	$(OBJDIR)/games/konamigx.o \
	$(OBJDIR)/games/lsystem.o \
	$(OBJDIR)/games/megasys1.o \
	$(OBJDIR)/games/nichisnd.o \
	$(OBJDIR)/games/quizgame.o \
	$(OBJDIR)/games/nmk.o \
	$(OBJDIR)/games/nmk004.o \
	$(OBJDIR)/games/taitosnd.o \
	$(OBJDIR)/games/tchnosnd.o \
	$(OBJDIR)/games/tecmosys.o \
	$(OBJDIR)/games/bsystem.o \
	$(OBJDIR)/games/xsystem1.o \
	$(OBJDIR)/games/xsystem2.o \
	$(OBJDIR)/games/toaplan1.o \
	$(OBJDIR)/games/toaplan2.o \
	$(OBJDIR)/games/upl.o \
	$(OBJDIR)/games/setax1.o \
	$(OBJDIR)/games/tc002obj.o \
	$(OBJDIR)/games/tc003vcu.o \
	$(OBJDIR)/games/tc004vcu.o \
	$(OBJDIR)/games/tc005rot.o \
	$(OBJDIR)/games/tc006vcu.o \
	$(OBJDIR)/games/tc100scn.o \
	$(OBJDIR)/games/tc101scn.o \
	$(OBJDIR)/games/tc110pcr.o \
	$(OBJDIR)/games/tc140syt.o \
	$(OBJDIR)/games/tc150rod.o \
	$(OBJDIR)/games/tc180vcu.o \
	$(OBJDIR)/games/tc200obj.o \
	$(OBJDIR)/games/tc220ioc.o \
	$(OBJDIR)/games/gun.o \
	$(CPS1_SYS)

# Beta only games

BGAMES= $(OBJDIR)/games/chasehq2.o \
	$(OBJDIR)/games/koshien.o \
	$(OBJDIR)/games/lightbr.o \
	$(OBJDIR)/games/cupfinal.o \
	$(OBJDIR)/games/intcup94.o \
	$(OBJDIR)/games/opwolf3.o \
	$(OBJDIR)/games/trstars.o \
	$(OBJDIR)/games/topland.o \
	$(OBJDIR)/games/topspeed.o \
	$(OBJDIR)/games/metalb.o \
	$(OBJDIR)/games/ainferno.o \
	$(OBJDIR)/games/aquajack.o \
	$(OBJDIR)/games/chaknpop.o \
	$(OBJDIR)/games/taito_z.o \
	$(OBJDIR)/games/dleague.o \
	$(OBJDIR)/games/recordbr.o \
	$(OBJDIR)/games/ringrage.o \
	$(OBJDIR)/games/undrfire.o \
	$(OBJDIR)/games/ridefght.o \
	$(OBJDIR)/games/deadconx.o \
	$(OBJDIR)/games/pbobble4.o \
	$(OBJDIR)/games/volfied.o \
	$(OBJDIR)/games/tetrisp2.o \
	$(OBJDIR)/games/ktiger2.o \
	$(OBJDIR)/games/f3demo.o \
	$(OBJDIR)/games/flstory.o \
	$(OBJDIR)/games/wgp.o \
	$(OBJDIR)/games/superchs.o \
	$(OBJDIR)/games/hthero95.o \
	$(OBJDIR)/games/heavyunt.o \
	$(OBJDIR)/games/landmakr.o \
	$(OBJDIR)/games/dangarb.o \
	$(OBJDIR)/games/chasehq.o \
	$(OBJDIR)/games/contcirc.o \
	$(OBJDIR)/games/spacegun.o \
	$(OBJDIR)/games/psyforce.o

# Interface

ifdef SDL
GUI=	$(OBJDIR)/sdl/gui.o \
	$(OBJDIR)/sdl/dialogs/video_info.o \
	$(OBJDIR)/sdl/dialogs/video_options.o \
	$(OBJDIR)/sdl/dialogs/sound_options.o \
	$(OBJDIR)/sdl/dialogs/gui_options.o \
	$(OBJDIR)/sdl/dialogs/about.o \
	$(OBJDIR)/sdl/dialogs/game_selection.o \
	$(OBJDIR)/sdl/dialogs/dlg_dsw.o \
	$(OBJDIR)/sdl/dialogs/messagebox.o \
	$(OBJDIR)/sdl/dialogs/controls.o \
	$(OBJDIR)/sdl/dialogs/cheats.o \
	$(OBJDIR)/sdl/dialogs/game_options.o \
	$(OBJDIR)/sdl/gui/tfont.o \
	$(OBJDIR)/sdl/gui/menu.o \
	$(OBJDIR)/sdl/gui/tlift.o \

else
GUI=	$(OBJDIR)/alleg/gui/gui.o \
	$(OBJDIR)/alleg/gui/rgui.o \
	$(OBJDIR)/alleg/gui/rguiproc.o \
	$(OBJDIR)/alleg/gui/rfsel.o \
	$(OBJDIR)/alleg/gui/about.o \
	$(OBJDIR)/alleg/gui/cheat.o \
	$(OBJDIR)/alleg/gui/dlg_dsw.o \
	$(OBJDIR)/alleg/gui/sound.o \
	$(OBJDIR)/alleg/gui/keys.o \
	$(OBJDIR)/alleg/gui/joystick.o
endif

# Core

CORE=	$(OBJDIR)/raine.o \
	$(OBJDIR)/hiscore.o \
	$(OBJDIR)/history.o \
	$(OBJDIR)/ingame.o \
	$(OBJDIR)/savegame.o \
	$(OBJDIR)/arpro.o \
	$(OBJDIR)/decode.o \
	$(OBJDIR)/debug.o \
	$(OBJDIR)/config.o \
	$(OBJDIR)/confile.o \
	$(OBJDIR)/unzip.o \
	$(OBJDIR)/files.o \
	$(OBJDIR)/newmem.o \
	$(OBJDIR)/cpuid.o \
	$(OBJDIR)/cpumain.o \
	$(OBJDIR)/emumain.o \
	$(OBJDIR)/cat93c46.o \
	$(OBJDIR)/loadroms.o \
	$(OBJDIR)/demos.o \
	$(OBJDIR)/timer.o \
	$(OBJDIR)/bezel.o \
	$(OBJDIR)/soundcfg.o \
	$(OBJDIR)/savepng.o

ifndef SDL
CORE += $(OBJDIR)/alleg/jpg/jpeg.o \
	$(OBJDIR)/alleg/dsw.o \
	$(OBJDIR)/alleg/png/loadpng.o 

else
CORE +=	$(OBJDIR)/sdl/dsw.o $(OBJDIR)/sdl/png.o
endif

ifndef RAINE_DOS
CORE += $(OBJDIR)/sdl/SDL_gfx/SDL_framerate.o \

endif

ifdef SDL
CORE += \
	$(OBJDIR)/sdl/SDL_gfx/SDL_rotozoom.o \
	$(OBJDIR)/sdl/SDL_gfx/SDL_gfxPrimitives.o \

endif

# Mame Support (eeprom and handlers for the sound interface)

MAME=	$(OBJDIR)/mame/memory.o \
	$(OBJDIR)/mame/eeprom.o \

ifdef RAINECPS
OBJS=	$(SC000) \
	$(MZ80) \
	$(CPS1_GAMES) \
	$(CPS1_SYS) \
	$(VIDEO) \
	$(CPS1_SOUND) \
	$(CORE) \
	$(MAME) \
	$(DEBUG) \
	$(GUI) 
else
OBJS=	$(SC000) \
	$(ASM020) \
	$(MZ80) \
	$(M68705) \
	$(GAMES) \
	$(BGAMES) \
	$(SYSDRV) \
	$(VIDEO) \
	$(SOUND) \
	$(CORE) \
	$(MAME) \
	$(DEBUG) \
	$(GUI) \
	$(M6502) 

endif

ifdef SDL
OBJS +=	$(OBJDIR)/sdl/blit.o \
	$(OBJDIR)/sdl/display.o \
	$(OBJDIR)/sdl/compat.o \
	$(OBJDIR)/sdl/control.o \
	$(OBJDIR)/sdl/gen_conv.o \
	$(OBJDIR)/sdl/profile.o

else
OBJS +=	$(OBJDIR)/alleg/blit.o \
	$(OBJDIR)/alleg/control.o \
	$(OBJDIR)/alleg/display.o \
	$(OBJDIR)/alleg/profile.o

endif

ifdef STATIC
LIBS = $(LIBS_STATIC)
CFLAGS += -DALLEGRO_STATICLINK
endif
ifdef RAINE_DEBUG
LIBS = $(LIBS_DEBUG)
# Uncomment only if you want to debug the cpu cores
# AFLAGS += -g
endif

ifdef X86_64
AFLAGS += -m amd64
endif

ifdef SEAL
OBJS += $(OBJDIR)/seal/sasound.o
else
ifdef RAINE_DOS
OBJS += $(OBJDIR)/alleg/sasound.o
CFLAGS += -DALLEGRO_SOUND
else
# avoid allegro when we can, sdl is much more reliable for sound
OBJS += $(OBJDIR)/sdl/sasound.o
CFLAGS += `sdl-config --cflags`
LIBS += `sdl-config --libs` -lSDL_ttf -lSDL_image 
endif
endif

all:	cpuinfo message maketree depend $(RAINE_EXE)

depend:
	@echo dependencies : if you get an error here, install the required dev package
ifndef RAINE_DOS
	@echo -n libpng:
	@libpng-config --version
	@echo -n sdl:
	@sdl-config --version
endif
ifdef RAINE_UNIX
ifndef SDL
	@echo -n allegro:
	@allegro-config --version
endif
endif

source/version.h: makefile
# I have just removed the stuff about gcc --version.
# Too much trouble with it. And I didn't know the __GNUC__ macros...
ifeq ($(OSTYPE),dos)
ifdef CROSSCOMPILE
	@echo "#define VERSION \"$(VERSION)\"" > source/version.h
else
# The dos seems too stupid to redirect stderr to a file (lame command.com).
# Usually I use 4dos for that, but since most people don't I use redir here
# which is in the standard djgpp install...
# Even with bash installed it does not work (it calls the dos shell instead
# of sh).
	@djecho '#define VERSION $(VERSION)' > source/version.h
endif
else
# Carefull for the windows version you need sh.exe and echo.exe !
	@echo "#define VERSION \"$(VERSION)\"" > source/version.h
endif

ifdef RAINE32

# Add a nice little icon...

OBJS += $(OBJDIR)/raine.res

$(OBJDIR)/raine.res:	source/raine.rc
	windres -O coff -o $(OBJDIR)/raine.res -i source/raine.rc
endif

message:
ifdef RAINE_DEBUG
	@echo -n Building Raine, debug version 
else
	@echo -n Building Raine, Fully optimized version
endif
ifdef GFX_SVGALIB
	@echo -n " with svgalib support"
endif
	@echo " with $(CC) for $(OSTYPE) CPU=$(CPU)"
ifndef ASM_VIDEO_CORE
	@echo "WARNING : move, newspr2, packed, and 32x32 sprites do not exist in the c"
	@echo "          video core. Using the asm functions for these..."
endif

OBJTEST = $(OBJDIR)/test.o $(OBJDIR)/cpuid.o $(OBJDIR)/confile.o $(OBJDIR)/control.o

$(RAINE_EXE):	$(OBJS)
	@echo Linking Raine...
	$(CCV) $(LFLAGS) -g -Wall -o $(RAINE_EXE) $(OBJS) $(LIBS) -lstdc++

converter: source/bonus/converter.c
	$(CCV) $(CFLAGS) -c $< -o $(OBJDIR)/converter.o
ifdef RAINE_UNIX
	$(CCV) $(LFLAGS) -g -Wall -o converter $(OBJDIR)/converter.o `allegro-config --libs` -lz
else
	$(CCV) $(LFLAGS) -g -Wall -o converter $(OBJDIR)/converter.o -lalleg -lz
endif

ASM020: $(ASM020)

VIDEO: $(VIDEO)

grabber: raine.dat
	   @echo Editing datafile...
	   grabber raine.dat

compress: $(RAINE_EXE)
	   @strip $(RAINE_EXE)
	   @echo Appending datafile...
	   exedat -a -c $(RAINE_EXE) raine.dat
	   upx -9 $(RAINE_EXE)

# compile object from standard c

$(OBJDIR)/%.o: source/%.c
	@echo Compiling $<...
	$(CCV) $(CFLAGS) -c $< -o $@

$(OBJDIR)/%.o: source/%.cpp
	@echo Compiling c++ $<...
	$(CXXV) $(CFLAGS) -c $< -o $@

# compile object from at&t asm

$(OBJDIR)/%.o: source/%.s
	@echo Assembling $<...
	$(CCV) $(SFLAGS) -c $< -o $@

# compile at&t asm from standard c

$(OBJDIR)/%.s: source/%.c
	@echo Compiling $<...
	$(CCV) $(CFLAGS) -S -c $< -o $@

# compile object from intel asm

$(OBJDIR)/%.oa: source/%.asm
	@echo Assembling $<...
	$(ASM) -o $@ $(AFLAGS) $<

# generate s68000.asm

$(OBJDIR)/68000/s68000.oa: $(OBJDIR)/68000/s68000.asm
	@echo Assembling $<...
	$(ASM) -o $@ $(AFLAGS) $<

ifdef CROSSCOMPILE
$(OBJDIR)/68000/s68000.asm: $(NATIVE)/object/68000/star.exe
	cp -fv $(NATIVE)/object/68000/star.exe $(OBJDIR)/68000/
else
$(OBJDIR)/68000/s68000.asm: $(OBJDIR)/68000/star.o
	$(CCV) $(LFLAGS) -s -o $(OBJDIR)/68000/star.exe $(OBJDIR)/68000/star.o
# To check : $(LIBS) seem necessary in win32 ??!!!
endif
	$(OBJDIR)/68000/star.exe -hog $@

# generate mz80.asm

$(OBJDIR)/z80/mz80.oa: $(OBJDIR)/z80/mz80.asm
	@echo Assembling $<...
	$(ASM) -o $@ $(AFLAGS) $<

ifdef CROSSCOMPILE
$(OBJDIR)/z80/mz80.asm: $(NATIVE)/object/z80/makez80.exe
	cp -fv $(NATIVE)/object/z80/makez80.exe $(OBJDIR)/z80/
else
$(OBJDIR)/z80/mz80.asm: $(OBJDIR)/z80/makez80.o
	$(CCV) $(LFLAGS) -s -o $(OBJDIR)/z80/makez80.exe $<
endif
ifndef RAINE_UNIX
	@$(OBJDIR)/z80/makez80.exe -s -cs -x86 $@
else
	$(OBJDIR)/z80/makez80.exe -l -s -cs -x86 $@
endif

# generate m6502.asm

$(OBJDIR)/6502/m6502.oa: $(OBJDIR)/6502/m6502.asm
	@echo Assembling $<...
	$(ASM) -o $@ $(AFLAGS) $<

$(OBJDIR)/6502/m6502.asm: $(OBJDIR)/6502/make6502.o
	@echo Building M6502 $(OBJDIR)...
ifdef CROSSCOMPILE
	cp -fv $(NATIVE)/object/6502/make6502.exe $(OBJDIR)/6502/
else
	$(CCV) -s -o $(OBJDIR)/6502/make6502.exe $(OBJDIR)/6502/make6502.o
endif
ifdef RAINE_UNIX
	$(OBJDIR)/6502/make6502.exe -l -s -6510 $@
else
	$(OBJDIR)/6502/make6502.exe -s -6510 $@
endif

# Notice : the following fix is specific to the frame pointer optimisation
# of gcc 2.81 and higher (< 3.00)

# kiki kai kai gcc bug

$(OBJDIR)/games/kiki_mcu.o: source/games/kiki_mcu.c
	@echo Compiling $<...
	$(CCV) $(INCDIR) $(DEFINE) $(CFLAGS_MCU) -c $< -o $@

# Same for kick and run...

$(OBJDIR)/games/kick_mcu.o: source/games/kick_mcu.c
	@echo Compiling $<...
	$(CCV) $(INCDIR) $(DEFINE) $(CFLAGS_MCU) -c $< -o $@

# This one happens for gcc < 3.2.1 (even 3.2.0)
$(OBJDIR)/sound/ymf278b.o: source/sound/ymf278b.c
	@echo Compiling WITH frame pointer $<...
	$(CCV) $(INCDIR) $(DEFINE) $(CFLAGS_MCU) -c $< -o $@

$(OBJDIR)/games/bubl_mcu.o: source/games/bubl_mcu.c
	@echo "Compiling $< (gcc-3.3 bug work around)..."
	$(CCV) $(INCDIR) $(DEFINE) $(_MARCH) -c $< -o $@

# SDL redefines the main function for windows programs, so we need to
# explicitely compile the cpu emulators without SDL (or link them with it
# which would be useless).

$(OBJDIR)/68000/star.o: source/68000/star.c
	@echo Compiling StarScream...
	$(CCV) $(DEFINE) $(CFLAGS_MCU) -c $< -o $@

$(OBJDIR)/z80/makez80.o: source/z80/makez80.c
	@echo Compiling mz80...
	$(CCV) $(INCDIR) $(DEFINE) $(CFLAGS_MCU) -c $< -o $@

$(OBJDIR)/6502/make6502.o: source/6502/make6502.c
	@echo Compiling make6502...
	$(CCV) $(INCDIR) $(DEFINE) $(CFLAGS_MCU) -c $< -o $@

# dependencies

# does perl exist for djgpp ??!
dep: make.dep
make.dep:
	./makedep $(OBJDIR) $(OBJS) > make.dep

cpuinfo:
	@sh ./detect-cpu

include make.dep

# create directories

dirs:
	@echo make dirs is no longer necessary, just type make

$(OBJDIRS):
ifndef OSTYPE
	@echo Your OSTYPE is not defined.
	@echo "If you are in Unix/Linux, make sure you are using bash/sh."
	@echo 'If this is the case, try to export OSTYPE : export OSTYPE=$OSTYPE'
	@echo "If not, you can type (for dos/windows) :"
	@echo "set OSTYPE=dos"
	@echo "before make."
	@exit 1
else
	$(MD) -p $@
endif

maketree: $(sort $(OBJDIRS))

# remove all object files

clean:
	@echo Deleting object tree ...
	$(RM) -r $(OBJDIR)
	@echo Deleting $(RAINE_EXE)...
	$(RM) $(RAINE_EXE) make.dep
#	$(RM) cpuinfo

vclean:
	@echo make vclean is no longer necessary, just type make clean

# Installation part (Only for Unix)
install: install_dirs $(RAINE_LNG) $(RAINE_EXE)
ifdef RAINE_UNIX
	echo installing $(RAINE_EXE) in $(bindir)
	$(INSTALL_BIN) $(RAINE_EXE) $(bindir)
ifndef SDL
	$(INSTALL_DATA) $(RAINE_DAT) $(rainedata)
else
	echo installing fonts in $(fonts_dir)
	$(INSTALL_DATA) fonts/Vera.ttf fonts/font6x8.bin $(fonts_dir)
	$(INSTALL_DATA) bitmaps/cursor.png bitmaps/raine_logo.png $(bitmaps_dir)
endif
	sh -c "if [ -f hiscore.dat ]; then $(INSTALL_DATA) hiscore.dat $(rainedata); fi"
	$(INSTALL_DATA) config/cheats.cfg $(rainedata)
	$(INSTALL_DATA) raine.desktop $(prefix)/share/applications
	$(INSTALL_DATA) raine.png $(prefix)/share/pixmaps

install_dirs:
	$(MD) -pv $(bindir) $(rainedata) $(langdir) $(romdir) $(artdir) $(emudxdir) $(prefix)/share/pixmaps $(prefix)/share/applications $(bitmaps_dir) $(fonts_dir)

$(RAINE_LNG):
	$(INSTALL_DATA) config/language/$@ $(langdir)

else
	@echo There is no needs to install for a win32/dos system
endif


uninstall:
ifdef RAINE_UNIX
	$(RM) $(bindir)/$(RAINE_EXE)
	$(RM) $(rainedata)/$(RAINE_DAT)
	$(CD) $(langdir); rm -f $(RAINE_LNG)
	$(RD) $(langdir) $(romdir) $(emudxdir) $(artdir) $(rainedata)

else
	@echo There is no needs to uninstall for a win32/dos system
endif

