.PHONY: all clean realclean dist

#PROFILE = -pg #-ax -ftest-coverage
DEBUG = -ggdb3 -O0 -fno-exceptions -DDEBUG 
FLAGS = -fno-exceptions
#OPT = -O6 -finline-functions -fexpensive-optimizations -funroll-loops \
	#-fomit-frame-pointer

USE_FE_GTK = 1

CC = gcc
CXX = gcc
CPPFLAGS = -I.
CXXFLAGS = $(DEBUG) $(PROFILE) $(OPT) -Wall -fpic -rdynamic
CFLAGS = $(DEBUG) $(PROFILE) $(OPT) -Wall -fpic -rdynamic
CPPFLAGS =

MODELS = Electron ModelB #ModelA BPlus

MODULES = WD1770 DrvCtrl SASI #AcornTUBE

MAIN_OBJECTS = Main.o Registry.o Configuration.o \
	utils.o ByteFIFO.o \
	dlc/dlc.o \
	CompEmul.o CPUEmul.o MemEmul.o CPU6502Emul.o \
	ACIA.o VIA6522.o \
#	Display.o

ifdef USE_FE_GTK
CPPFLAGS += -DINTERFACE_GTK $(shell gtk-config --cflags)
MAIN_OBJECTS += gtk_fe.o gtk_fe_dlg.o
endif

all: tbe $(MODELS:%=%.so) $(MODULES:%=%.so)
	rm -fr bin
	mkdir bin
	cp tbe $(MODELS:%=%.so) $(MODULES:%=%.so) bin
	strip bin/*
	ln -s ../files bin/files

TestBed: TestBed.o CompEmul.o CPUEmul.o MemEmul.o CPU6502Emul.o utils.o

tbe: $(MAIN_OBJECTS)
	$(CXX) $(CXXFLAGS) -o $@ $(MAIN_OBJECTS) -ldl -lstdc++ -lpopt $(shell gtk-config --libs)

dlc/dlc.o:
	$(MAKE) -C dlc dlc.o

ModelA.so: ModelA.cc ModelB.cc StdSysVIA.cc

ModelB.so: ModelB.cc StdSysVIA.cc

%.so: %.cc
	$(CXX) -shared $(CXXFLAGS) $(CPPFLAGS) -fpic -o $@ $<

clean:
	rm -f *.o *.so tbe

realclean: clean
	rm -fr bin
	rm -f hard_drive.bin
	rm -f disc.bin
	$(MAKE) -C utils clean

ifeq ($(EXPORT_VER),)
EXPORT_OPT = -Dnow
EXPORT_VER = current
EXPORT_TARVER = current-$(shell date +%Y%m%d)
else
EXPORT_OPT := -r$(shell utils/cvsver $(EXPORT_VER))
EXPORT_TARVER = $(EXPORT_VER)
endif

OUTDIR=..

dist:
	rm -fr tbe-$(EXPORT_VER)
	rcs2log -u "tom	Tom Lees	tom@lpsg.demon.co.uk" -c ChangeLog > ChangeLog
	cvs export -dtbe-$(EXPORT_VER) $(EXPORT_OPT) tbe
	utils/bld-files `pwd`/tbe-$(EXPORT_VER) `pwd`
	echo "#define VERNAME \"$(EXPORT_TARVER)\"" >> tbe-$(EXPORT_VER)/config.h
	mv ChangeLog tbe-$(EXPORT_VER)/ChangeLog
	tar czf $(OUTDIR)/tbe-$(EXPORT_TARVER).tar.gz tbe-$(EXPORT_VER)
	rm -fr tbe-$(EXPORT_VER)
