# set this to mame, mess or the destination you want to build
# TARGET = mame
 TARGET = mess
# TARGET = neomame
# example for a tiny compile
# TARGET = tiny
# TARGET = messtiny

# uncomment next line to include the debugger
# DEBUG = 1

# uncomment next line to include the symbols for symify
# SYMBOLS = 1

# uncomment next line to use Assembler 68k engine
# currently the Psikyo games don't work with it
# X86_ASM_68K = 1

# set this the operating system you're building for
# (actually you'll probably need your own main makefile anyways)
OS = digita
# uncomment one of these
# DIGITA_PPC = 1
# DIGITA_COLDFIRE = 1
 DIGITA_DCAM = 1

# extension for executables
EXE = .cam
EXE_HOST = .exe

# CPU core include paths
VPATH=src $(wildcard src/cpu/*)

ifdef DEBUG
NAME = $(TARGET)d
else
ifdef K6
NAME = $(TARGET)k6
ARCH = -march=k6
else
ifdef I686
NAME = $(TARGET)ppro
ARCH = -march=pentiumpro
else
NAME = $(TARGET)
ARCH = -mcpu=860 -c -s -nostdinc -fno-builtin -DDIGITA_SOUND -DDIGITA_SERIAL -DWIND_RIVER -DCPU=PPC860 -I$(WIND_BASE)/target/h -Isrc/zlib 
endif
endif
endif

# compiler, linker and utilities
#AR = @ar
ifdef DIGITA_PPC
CC = @ccppc
CC_HOST = @gcc
LD = @ldppc
NAME = $(TARGET)ppc
else
ifdef DIGITA_COLDFIRE
CC = @F:\diab\4.3b\WIN32\bin\dplus.exe
GCC = @ccppc
CC_HOST = @gcc
LD = @F:\diab\4.3b\WIN32\bin\dld.exe
NAME = $(TARGET)cold
else
ifdef DIGITA_DCAM
CC = @ccmips
CC_HOST = @gcc
LD = @ldmips
NAME = $(TARGET)dcam
endif
endif
endif
#ASM = @nasm
#ASM = @nasmw
#ASMFLAGS = -f coff

# build the targets in different object dirs, since mess changes
# some structures and thus they can't be linked against each other.
# cleantiny isn't needed anymore, because the tiny build has its
# own object directory too.
OBJ = $(NAME).obj

EMULATOR = $(NAME)$(EXE)

#DEFS = -DX86_ASM -DLSB_FIRST -DINLINE="static __inline__" -Dasm=__asm__
DEFS =

#ifdef SYMBOLS
#CFLAGS = -Isrc -Isrc/$(OS) -I$(OBJ)/cpu/m68000 -Isrc/cpu/m68000 \
#	-O0 -pedantic -Wall -Werror -Wno-unused -g
#else
ifdef DIGITA_PPC
CFLAGS = -Isrc -Isrc/$(OS) -I$(OBJ)/cpu/m68000 -Isrc/cpu/m68000 \
	-DNDEBUG \
	$(ARCH) -O3 -fomit-frame-pointer \
	-Wall -Wunused \
	-Wpointer-arith -Wbad-function-cast -Wcast-align -Waggregate-return \
	-Wshadow \
	-Wstrict-prototypes
else
ifdef DIGITA_COLDFIRE
CFLAGS = \
	-I./src \
	-I./src/$(OS)  \
	-I$(OBJ)/cpu/m68000 \
	-I./src/cpu/m68000 \
	-I./src/zlib \
	-IF:/Tornado1Coldfire/target/h/ \
	-IF:/Tornado1Coldfire/target/h/drv/multi \
	-IF:/Tornado1Coldfire/target/h/drv/intrCtl \
	-IF:/Tornado1Coldfire/target/h/arch/mc68k \
	-DCPU=MCF5200 \
	-DWIND_RIVER \
	-XO \
	-Xunsigned-char \
	-A \
	-tMCF5307FS:tornado \
	-Wa,-Xreldata-addend \
	-Xansi \
	-Xlocal-struct=0 \
	-Xenum-is-int \
	-Xfloats-as-gnu \
	-Xframe-ptr \
	-Xmismatch-warning \
	-Xrelax-alias=0 \
	-Xieee754-pedantic \
	-Xstrings-in-text \
	-Xstruct-best-align=0 \
	-Xstruct-as-arg \
	-Xconst-in-data \
	-Xlint \
	-Xsmall-data=0 \
	-Xsmall-const=0 
	
#	-XO \

CFLAGSGCC = \
	-I./src \
	-I./src/$(OS)  \
	-I$(OBJ)/cpu/m68000 \
	-I./src/cpu/m68000 \
	-I./src/zlib \
	-IF:/Tornado1Coldfire/target/h/ \
	-IF:/Tornado1Coldfire/target/h/drv/multi \
	-IF:/Tornado1Coldfire/target/h/drv/intrCtl \
	-IF:/Tornado1Coldfire/target/h/arch/mc68k \
	-DCPU=MCF5200 \
	-DWIND_RIVER
else
ifdef DIGITA_DCAM
CFLAGS = -Isrc -Isrc/$(OS) -I$(OBJ)/cpu/m68000 -Isrc/cpu/m68000 -I$(WIND_BASE)/target/h -Isrc/zlib \
	-Wall -Wunused \
	-DCPU_FAMILY=MIPS -DCPU=CW4000 \
	-nostdinc  -fno-builtin -fno-for-scope -msoft-float -mips2 -mno-mdu -G 0 \
	-Wall -EB -Wshadow -Wpointer-arith -Wbad-function-cast -Wcast-qual -Wcast-align \
	-Wwrite-strings -Wconversion -Waggregate-return -Wstrict-prototypes -Wmissing-prototypes \
	-Wmissing-declarations -Wredundant-decls -Wnested-externs -Woverloaded-virtual \
	-O3 -DWIND_RIVER
endif
endif
endif
#	-pedantic \
#	-Werror 
#	-fstrict-aliasing 
#	-Wno-sign-compare 
#	-W had to remove because of the "missing initializer" warning
#	-Wredundant-decls \
#	-Wlarger-than-27648 \
#	-Wcast-qual \
#	-Wwrite-strings \
#	-Wconversion \
#	-Wmissing-prototypes \
#	-Wmissing-declarations
# endif
CFLAGS_HOST = 

#ifdef SYMBOLS
#LDFLAGS =
#else
#LDFLAGS = -s -Wl,--warn-common
#LDFLAGS = -s
ifdef DIGITA_PPC
LDFLAGS = -r
else
ifdef DIGITA_COLDFIRE
LDFLAGS = \
-tMCF5307FS:tornado \
-X \
-N \
-r4 \
-Xsection-align=4
else
ifdef DIGITA_DCAM
LDFLAGS = -r
endif
endif
endif
#endif

#LIBS = -lalleg -laudio -lz
LIBS = 

# check that the required libraries are available
#ifeq ($(wildcard $(DJDIR)/lib/liballeg.a),)
#noallegro:
#	@echo Missing Allegro library! Get it from http://www.talula.demon.co.uk/allegro/
#endif
#ifeq ($(wildcard $(DJDIR)/lib/libaudio.a),)
#noseal:
#	@echo Missing SEAL library! Get it from http://www.egerter.com/
#endif
#ifeq ($(wildcard $(DJDIR)/lib/libz.a),)
#nozlib:
#	@echo Missing zlib library! Get it from http://www.cdrom.com/pub/infozip/zlib/
#endif

#if obj subdirectory doesn't exist, create the tree before proceeding
ifeq ($(wildcard $(OBJ)),)
noobj: maketree all
endif

all:	$(EMULATOR) extra

# include the various .mak files
include src/core.mak
include src/$(TARGET).mak
include src/rules.mak
include src/$(OS)/$(OS).mak

ifdef DEBUG
DBGDEFS = -DMAME_DEBUG
else
DBGDEFS =
DBGOBJS =
endif

#extra:	romcmp$(EXE) $(TOOLS) $(TEXTS)
extra:

# combine the various definitions to one
CDEFS = $(DEFS) $(COREDEFS) $(CPUDEFS) $(SOUNDDEFS) $(ASMDEFS) $(DBGDEFS)
CDEFS_HOST =

$(EMULATOR): $(OBJS) $(COREOBJS) $(OSOBJS) $(LIBS) $(DRVLIBS)
# always recompile the version string
ifdef DIGITA_PPC
	$(CC) $(CDEFS) $(CFLAGS) -c src/version.c -o $(OBJ)/version.o
else
ifdef DIGITA_COLDFIRE
	@echo Preprocessing using GCC for MACROs src/version.c...
	$(GCC) $(CDEFS) $(CFLAGSGCC) -E src/version.c -o $(OBJ)/versioni.c
	@echo Compiling using diab $(OBJ)/versioni.c...
	$(CC) $(CDEFS) $(CFLAGS) -c $(OBJ)/versioni.c -o $(OBJ)/version.og
	@echo Removing underscores $(OBJ)/version.og...
	$(LD) $(LDFLAGS) -Xsuppress-underscore $(OBJ)/version.og -o $(OBJ)/version.o
else
ifdef DIGITA_DCAM
	$(CC) $(CDEFS) $(CFLAGS) -c src/version.c -o $(OBJ)/version.o
endif
endif
endif
	@echo Linking $@...
	$(LD) $(LDFLAGS) $(OBJS) $(COREOBJS) $(OSOBJS) $(LIBS) $(DRVLIBS) -o $@
#ifndef DEBUG
#	upx $(EMULATOR)
#endif

romcmp$(EXE): $(OBJ)/romcmp.o $(OBJ)/unzip.o
	@echo Linking $@...
	$(LD) $(LDFLAGS) $^ -lz -o $@

ifdef DIGITA_PPC
$(OBJ)/%.o: src/%.c
	@echo Compiling $<...
	$(CC) $(CDEFS) $(CFLAGS) -c $< -o $@
else
ifdef DIGITA_COLDFIRE
$(OBJ)/%i.c: src/%.c
	@echo Preprocessing using GCC for MACROs $<...
	$(GCC) $(CDEFS) $(CFLAGSGCC) $(TINYFLAGS) -E $< -o $@ 

$(OBJ)/%_.o: $(OBJ)/%i.c
	@echo Compiling using diab $<...
	$(CC) $(CDEFS) $(CFLAGS) $(TINYFLAGS) -c $< -o $@

$(OBJ)/%.o: $(OBJ)/%_.o
	@echo Removing underscores $<...
	$(LD) $(LDFLAGS) -Xsuppress-underscore $< -o $@
else
ifdef DIGITA_DCAM
$(OBJ)/%.o: src/%.c
	@echo Compiling $<...
	$(CC) $(CDEFS) $(CFLAGS) -c $< -o $@
endif
endif
endif

# compile generated C files for the 68000 emulator
ifdef DIGITA_PPC
$(M68000_GENERATED_OBJS): $(OBJ)/cpu/m68000/m68kmake$(EXE_HOST)
	@echo Compiling $(subst .og,.c,$@)...
	$(CC) $(CDEFS) $(CFLAGS) -c $(subst .og,.c,$@) -o $@
else 
ifdef DIGITA_COLDFIRE
$(M68000_GENERATED_OBJS): $(OBJ)/cpu/m68000/m68kmake$(EXE_HOST)
	@echo Preprocessing using GCC for MACROs $(subst .og,i.c,$@)...
	$(GCC) $(CDEFS) $(CFLAGSGCC) $(TINYFLAGS) -E $(subst .og,.c,$@) -o $(subst .og,i.c,$@)
	@echo Compiling using diab $(subst .og,_.og,$@)...
	$(CC) $(CDEFS) $(CFLAGS) $(TINYFLAGS) -c $(subst .og,i.c,$@) -o $(subst .og,_.og,$@)
	@echo Removing underscores $@...
	$(LD) $(LDFLAGS) -Xsuppress-underscore $(subst .og,_.og,$@) -o $@
else 
ifdef DIGITA_DCAM
$(M68000_GENERATED_OBJS): $(OBJ)/cpu/m68000/m68kmake$(EXE_HOST)
	@echo Compiling $(subst .og,.c,$@)...
	$(CC) $(CDEFS) $(CFLAGS) -c $(subst .og,.c,$@) -o $@
endif
endif
endif

# additional rule, because m68kcpu.c includes the generated m68kops.h :-/
$(OBJ)/cpu/m68000/m68kcpu.o: $(OBJ)/cpu/m68000/m68kmake$(EXE_HOST)

# generate C source files for the 68000 emulator
$(OBJ)/cpu/m68000/m68kmake$(EXE_HOST): src/cpu/m68000/m68kmake.c
	@echo M68K make $<...
	$(CC_HOST) $(CDEFS_HOST) $(CFLAGS_HOST) -DDOS -o $(OBJ)/cpu/m68000/m68kmake$(EXE_HOST) $<
	@echo Generating M68K source files...
	@$(OBJ)/cpu/m68000/m68kmake$(EXE_HOST) $(OBJ)/cpu/m68000 src/cpu/m68000/m68k_in.c

# generate asm source files for the 68000 emulator
$(OBJ)/cpu/m68000/68kem.asm:  src/cpu/m68000/make68k.c
	@echo Compiling $<...
	$(CC) $(CDEFS) $(CFLAGS) -O0 -DDOS -o $(OBJ)/cpu/m68000/make68k$(EXE_HOST) $<
	@echo Generating $@...
	@$(OBJ)/cpu/m68000/make68k$(EXE_HOST) $@ $(OBJ)/cpu/m68000/comptab.asm

# generated asm files for the 68000 emulator
$(OBJ)/cpu/m68000/68kem.o:  $(OBJ)/cpu/m68000/68kem.asm
	@echo Assembling $<...
	$(ASM) -o $@ $(ASMFLAGS) $(subst -D,-d,$(ASMDEFS)) $<

$(OBJ)/%.a:
	@echo Archiving $@...
	@rm -f $@
#	$(AR) cr $@ $^
	$(LD) -r -o $@ $^

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

maketree:
	@echo Making MAME object tree in $(OBJ)...
	@md $(OBJ)
	@md $(OBJ)\cpu
	@md $(OBJ)\cpu\z80
	@md $(OBJ)\cpu\z80gb
	@md $(OBJ)\cpu\m6502
	@md $(OBJ)\cpu\h6280
	@md $(OBJ)\cpu\i86
	@md $(OBJ)\cpu\nec
	@md $(OBJ)\cpu\i8039
	@md $(OBJ)\cpu\i8085
	@md $(OBJ)\cpu\m6800
	@md $(OBJ)\cpu\m6805
	@md $(OBJ)\cpu\m6809
	@md $(OBJ)\cpu\hd6309
	@md $(OBJ)\cpu\konami
	@md $(OBJ)\cpu\m68000
	@md $(OBJ)\cpu\s2650
	@md $(OBJ)\cpu\t11
	@md $(OBJ)\cpu\tms34010
	@md $(OBJ)\cpu\tms9900
	@md $(OBJ)\cpu\z8000
	@md $(OBJ)\cpu\tms32010
	@md $(OBJ)\cpu\ccpu
	@md $(OBJ)\cpu\adsp2100
	@md $(OBJ)\cpu\pdp1
	@md $(OBJ)\cpu\mips
	@md $(OBJ)\cpu\sc61860
	@md $(OBJ)\cpu\arm
	@md $(OBJ)\cpu\g65816
	@md $(OBJ)\cpu\f8
	@md $(OBJ)\sound
	@md $(OBJ)\$(OS)
	@md $(OBJ)\drivers
	@md $(OBJ)\machine
	@md $(OBJ)\vidhrdw
	@md $(OBJ)\sndhrdw
	@md $(OBJ)\zlib
ifdef MESS
	@echo Making MESS object tree in $(OBJ)\mess...
	@md $(OBJ)\mess
	@md $(OBJ)\mess\systems
	@md $(OBJ)\mess\machine
	@md $(OBJ)\mess\vidhrdw
	@md $(OBJ)\mess\sndhrdw
	@md $(OBJ)\mess\tools
	@md $(OBJ)\mess\formats
	@md $(OBJ)\mess\$(OS)
endif

clean:
	@echo Deleting object tree $(OBJ)...
	@rm -fr $(OBJ)
	@echo Deleting $(EMULATOR)...
	@rm -f $(EMULATOR)
