# Makefile for Atari Vector game simulator
#
# Copyright 1993, 1995, 1996, 1997, 2003 Eric Smith and Hedley Rainnie
#
#    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; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# $Id: Makefile 2 2003-08-20 01:51:05Z eric $


CC		= gcc
CFLAGS		= -Wall -O3 -DUSE_READLINE -DUSE_HISTORY
#CFLAGS		= -Wall -g
LIBRARIES	= -lreadline -lhistory -ltermcap

X_CFLAGS	= -I/usr/X11/include
PR_CFLAGS	=

X_LIBRARIES	= -L/usr/X11R6/lib -lX11
PR_LIBRARIES	= -lpixrect

# DEFINES = -DCOUNT_INTERRUPTS
# DEFINES = -DMAGIC_PC

PACKAGE = vecsim
VERSION = 0.8

TARGETS = xatari noatari

SRCS 		= main.c sim6502.c display.c memory.c game.c dis6502.c \
		  debugger.c mathbox.c pokey.c translator.c \
		  x_interface.c pr_interface.c mac_interface.c no_interface.c
TEMPLATE_SRCS	= framework.c
HDRS 		= display.h memory.h game.h sim6502.h dis6502.h \
		  macro6502.h debugger.h mathbox.h pokey.h

DISTFILES	= COPYING Makefile $(HDRS) $(SRCS) $(TEMPLATE_SRCS)

DISTNAME = $(PACKAGE)-$(VERSION)


all: $(TARGETS)

dist: $(DISTFILES)
	-rm -rf $(DISTNAME)
	mkdir $(DISTNAME)
	for f in $(DISTFILES); do ln $$f $(DISTNAME)/$$f; done
	tar --gzip -chf $(DISTNAME).tar.gz $(DISTNAME)
	-rm -rf $(DISTNAME)
vecsim.tgz: Makefile $(DIST_FILES)
	tar -cvzf $@ $(DIST_FILES)

clean:
	rm -rf $(TARGETS) *.o *.t *.d


# display interfaces
DISP = x no pr mac

# games that can be used with the static translator
TR_GAMES = asteroids deluxe gravitar spaceduel


.c.o:
	$(CC) -o $@ -c $(CFLAGS) $(DEFINES) $*.c



$(patsubst %,%atari,$(DISP)) : %atari : %_interface.o \
		main.o sim6502.o display.o memory.o game.o dis6502.o \
		debugger.o mathbox.o pokey.o
	$(CC) -o $@ $(CFLAGS) $^ $(LIBRARIES)

xatari: LIBRARIES += $(X_LIBRARIES)
pratari: LIBRARIES += $(PR_LIBRARIES)


TRAN_EXEC_OBJS	= display.o memory.o game.o dis6502.o debugger.o \
		  mathbox.o pokey.o

$(patsubst %,x%,$(TR_GAMES)) : x%: main.%.o $(TRAN_EXEC_OBJS) framework.%.o x_interface.o
	$(CC) -o $@ $^ $(LIBRARIES) $(X_LIBRARIES)

$(patsubst %,no%,$(TR_GAMES)) : no%: main.%.o $(TRAN_EXEC_OBJS) framework.%.o no_interface.o
	$(CC) -o $@ $^ $(LIBRARIES)


framework.%.o: framework.c %.t memory.h game.h sim6502.h macro6502.h debugger.h
	$(CC) -o $@ -c $(CFLAGS) $(DEFINES) -DGAME_INC=\"$(patsubst framework.%.o,%.t,$@)\" framework.c


# It would be nice to have the %.t targets depend on the appropriate ROM files,
# by defining variables like gravitar_roms, and putting a dependency in the
# rule like $(%_roms), but sadly it doesn't work.
%.t: translator
	./translator $(subst .t,,$@) >$@

main.%.o: main.c memory.h game.h sim6502.h display.h
	$(CC) -o $@ -c $(CFLAGS) $(DEFINES) -DSINGLE_GAME=\"$(patsubst main.%.o,%,$@)\" main.c


translator: translator.o game.o memory.o display.o no_interface.o \
		  mathbox.o pokey.o
#	$(CC) -o $@ $^

x_interface.o: CFLAGS += $(X_CFLAGS)

pr_interface.o: CFLAGS += $(PR_CFLAGS)


DEPENDS = $(SRCS:.c=.d)

%.d: %.c
	$(CC) -M -MG $(CFLAGS) $< | sed -e 's@ /[^ ]*@@g' -e 's@^\(.*\)\.o:@\1.d \1.o:@' > $@

include $(DEPENDS)
