#Copyright (C) 1997-2007 ZSNES Team ( zsKnight, _Demo_, pagefault, Nach )
#
#http://www.zsnes.com
#http://sourceforge.net/projects/zsnes
#https://zsnes.bountysource.com
#
#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#version 2 as published by the Free Software Foundation.
#
#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; if not, write to the Free Software
#Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

CHIP_D=chips
CPU_D=cpu
DOS_D=dos
EFF_D=effects
GUI_D=gui
JMA_D=jma
MMLIB_D=mmlib
NET_D=net
PORT_D=linux
TOOL_D=tools
VID_D=video
ZIP_D=zip

PSR=parsegen
PSR_H=cfg.h md.h input.h

CHIP_O=$(CHIP_D)/c4emu.o $(CHIP_D)/c4proc.o $(CHIP_D)/dsp1emu.o\
	$(CHIP_D)/dsp1proc.o $(CHIP_D)/dsp2proc.o $(CHIP_D)/dsp3emu.o\
	$(CHIP_D)/dsp3proc.o $(CHIP_D)/dsp4emu.o $(CHIP_D)/dsp4proc.o\
	$(CHIP_D)/fxemu2.o $(CHIP_D)/fxemu2b.o $(CHIP_D)/fxemu2c.o\
	$(CHIP_D)/fxtable.o $(CHIP_D)/obc1emu.o $(CHIP_D)/obc1proc.o\
	$(CHIP_D)/sa1proc.o $(CHIP_D)/sa1regs.o $(CHIP_D)/sdd1emu.o\
	$(CHIP_D)/seta10.o $(CHIP_D)/sfxproc.o $(CHIP_D)/st10proc.o\
	$(CHIP_D)/7110proc.o $(CHIP_D)/seta11.o $(CHIP_D)/st11proc.o

CPU_O=$(CPU_D)/dma.o $(CPU_D)/dsp.o $(CPU_D)/dspproc.o $(CPU_D)/execute.o\
	$(CPU_D)/executec.o $(CPU_D)/irq.o $(CPU_D)/memory.o $(CPU_D)/memtable.o\
	$(CPU_D)/spc700.o $(CPU_D)/stable.o $(CPU_D)/table.o $(CPU_D)/tablec.o

DEBUG_O=@DEBUGGER_FILES@

EFF_O=$(EFF_D)/burn.o $(EFF_D)/smoke.o $(EFF_D)/water.o

GUI_O=$(GUI_D)/gui.o $(GUI_D)/guifuncs.o $(GUI_D)/menu.o

JMA_O=@JMA_FILES@

MAIN_O=cfg.o endmem.o init.o initc.o input.o md.o patch.o ui.o vcache.o\
	version.o zloader.o zmovie.o zpath.o zstate.o ztime.o ztimec.o

MMLIB_O=@MMLIB_FILES@

NET_O=
#$(NET_D)/ztcp.o

PORT_O=$(PORT_D)/audio.o $(PORT_D)/battery.o\
	$(PORT_D)/sdlintrf.o $(PORT_D)/sdllink.o @GL_DRAW@ $(PORT_D)/sw_draw.o\
	$(PORT_D)/safelib.o

TOOL_O=$(TOOL_D)/fileutil.o $(TOOL_D)/strutil.o

VID_O=$(VID_D)/makev16b.o $(VID_D)/makev16t.o $(VID_D)/makevid.o\
	$(VID_D)/mode716.o $(VID_D)/mode716b.o $(VID_D)/mode716d.o\
	$(VID_D)/mode716e.o $(VID_D)/mode716t.o $(VID_D)/mode7.o\
	$(VID_D)/mode7ext.o $(VID_D)/mv16tms.o $(VID_D)/m716text.o\
	$(VID_D)/newg162.o $(VID_D)/newgfx.o $(VID_D)/newgfx16.o\
	$(VID_D)/newgfx2.o $(VID_D)/procvid.o $(VID_D)/procvidc.o\
	$(VID_D)/sw_draw.o $(VID_D)/2xsaiw.o $(VID_D)/hq2x16.o $(VID_D)/hq2x32.o\
	$(VID_D)/hq3x16.o $(VID_D)/hq3x32.o $(VID_D)/hq4x16.o $(VID_D)/hq4x32.o\
	$(VID_D)/ntsc.o $(VID_D)/copyvwin.o

ZIP_O=$(ZIP_D)/unzip.o $(ZIP_D)/zpng.o

Z_OBJS=$(MAIN_O) $(CHIP_O) $(CPU_O) $(DEBUG_O) $(GUI_O) $(EFF_O) $(JMA_O)\
	$(MMLIB_O) $(NET_O) $(VID_O) $(PORT_O) $(ZIP_O)

.PHONY: default main tools all install clean tclean distclean
.SUFFIXES: .asm .c .cpp .psr

%: %.cpp
	@CXX@ @CXXFLAGS@ -o $@ $<
%.o: %.asm
	@NASMPATH@ @NFLAGS@ -o $@ $<
%.o: %.c
	@CC@ @CFLAGS@ -o $@ -c $<
%.o: %.cpp
	@CXX@ @CXXFLAGS@ -o $@ -c $<
%.o %.h: %.psr $(PSR)
	./$(PSR) @PSRFLAGS@ -gcc @CC@ -compile -flags "@CFLAGS@ -O1" -cheader $*.h -fname $* $*.o $<

default: main
all: main tools
main: makefile.dep $(Z_OBJS)
	@ZC@ -o @ZSNESEXE@ $(Z_OBJS) @ZCFLAGS@ @LDFLAGS@
	rm -f version.o

$(PSR): parsegen.cpp
	@CXX@ @CXXFLAGS@ -o $@ $< -lz

TOOLSEXE=$(TOOL_D)/archopt $(TOOL_D)/cutrtype $(TOOL_D)/extraext\
	$(TOOL_D)/macroll $(TOOL_D)/minwhite $(TOOL_D)/nreplace\
	$(TOOL_D)/sec-test $(TOOL_D)/srccount $(TOOL_D)/varrep
tools: $(TOOLSEXE) $(TOOL_D)/depbuild
$(TOOL_D)/archopt: $(TOOL_D)/archopt.c
	@CC@ @CFLAGS@ -m32 -o $@ $@.c
$(TOOL_D)/cutrtype: $(TOOL_D)/cutrtype.cpp $(TOOL_O)
	@CXX@ @CXXFLAGS@ -o $@ $@.cpp $(TOOL_O)
$(TOOL_D)/depbuild: $(TOOL_D)/depbuild.cpp $(TOOL_O)
	@CXX@ @CXXFLAGS@ -o $@ $@.cpp $(TOOL_O)
$(TOOL_D)/extraext: $(TOOL_D)/extraext.cpp $(TOOL_O)
	@CXX@ @CXXFLAGS@ -o $@ $@.cpp $(TOOL_O)
$(TOOL_D)/macroll: $(TOOL_D)/macroll.cpp $(TOOL_O)
	@CXX@ @CXXFLAGS@ -o $@ $@.cpp $(TOOL_O)
$(TOOL_D)/minwhite: $(TOOL_D)/minwhite.cpp $(TOOL_O)
	@CXX@ @CXXFLAGS@ -o $@ $@.cpp $(TOOL_D)/fileutil.o
$(TOOL_D)/nreplace: $(TOOL_D)/nreplace.cpp $(TOOL_O)
	@CXX@ @CXXFLAGS@ -o $@ $@.cpp $(TOOL_D)/fileutil.o
$(TOOL_D)/sec-test: $(TOOL_D)/sec-test.cpp $(TOOL_O)
	@CXX@ @CXXFLAGS@ -o $@ $@.cpp $(TOOL_O)
$(TOOL_D)/srccount: $(TOOL_D)/srccount.cpp $(TOOL_O)
	@CXX@ @CXXFLAGS@ -o $@ $@.cpp $(TOOL_D)/fileutil.o
$(TOOL_D)/varrep: $(TOOL_D)/varrep.cpp $(TOOL_O)
	@CXX@ @CXXFLAGS@ -o $@ $@.cpp $(TOOL_O)

include makefile.dep
makefile.dep: $(TOOL_D)/depbuild Makefile
	$(TOOL_D)/depbuild @CC@ "@CFLAGS@" @NASMPATH@ "@NFLAGS@" $(Z_OBJS) > makefile.dep

Makefile: Makefile.in config.status
	./config.status

config.status: configure.in
	./autogen.sh --recheck

install:
	@INSTALL@ -d -m 0755 $(DESTDIR)/@prefix@/bin
	@INSTALL@ -m 0755 @ZSNESEXE@ $(DESTDIR)/@prefix@/bin
	@INSTALL@ -d -m 0755 $(DESTDIR)/@prefix@/man/man1
	@INSTALL@ -m 0644 linux/zsnes.1 $(DESTDIR)/@prefix@/man/man1
uninstall:
	rm -f @prefix@/bin/$(notdir @ZSNESEXE@) @prefix@/man/man1/zsnes.1

clean:
	rm -f $(Z_OBJS) $(PSR) $(PSR_H) @ZSNESEXE@
tclean:
	rm -f $(TOOL_O) $(TOOLSEXE)
distclean: clean tclean
	rm -f Makefile makefile.dep aclocal.m4 configure config.log \
	config.status config.h $(TOOL_D)/depbuild
