# $Id: Makefile,v 1.15 2003/12/16 21:47:56 rwuest Exp $
#
#    SPGMR08 68HC08GP32 Loader
#    Copyright (C) 2001  Sirius Engineering Company
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License version 2.
#    See the file COPYING that is distributed with this program.
#
#    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.


#
# get a build number
#
BUILD ?= 0.12

ifndef BUILD
# use the current date and time
BUILD = $(shell date +%m%d%Y%H%M)
endif

#
# where is your compiler?
#
# My gcc-3.0 Install
# CC = /usr/local2/bin/gcc
#
# My gcc-2.96-98 install (redhat 7.2 default compiler)
CC = /usr/bin/gcc

#
# PCCTS installation paths
#
ANTLRHDIR = /usr/local/include/pccts
ANTLR = antlr
DLG = dlg

#
# Where to put the executable
#
BINDIR=$(HOME)/bin

#
# Where to put cpu modules
#
LIBDIR=$(HOME)/lib/spgmr08

#
# Compiler flags
#

CFLAGS =  -Wall -g -O2 -funsigned-char
# -mpentiumpro

# compile in debugging code
CFLAGS += -DDEBUG -DDEBUG_MONITOR -DDEBUG_SERIAL

#
# uncomment this if you have a serial scope program
# You also need to edit the variable scopecmd in cli.g
#
CFLAGS += -DUSE_SCOPE

#
# Linker flags. Add any needed -L<libdirectory>
#
LDFLAGS =
LDLIBS =
# --------------------------------------------------------------------------
# You should not need to change anything below this line.

CFLAGS += -D_GNU_SOURCE -DBUILD=\"$(BUILD)\" -I./pccts
# -I$(ANTLRHDIR)
CFLAGS += -DLIBDIR=\"$(LIBDIR)\"
CFLAGS += -Wno-unused
# LDFLAGS += -static

LDLIBS = -lreadline -lhistory -lncurses

OBJS=main.o basename.o args.o lin_serial.o s1utils.o seccodes.o mongp32.o \
	cli.o scan.o err.o rcfile.o utils.o ops.o ops_gp32.o jb8_driver.o

SRCS=main.c basename.c args.c lin_serial.c s1utils.c seccodes.c mongp32.c \
	rcfile.c utils.c ops.c ops_gp32.c jb8_driver.c

HDRS = spgmr.h lin_serial.h s1utils.h seccodes.h mongp32.h rcfile.h cli.h \
	ops.h timec.h

XTRAS = Makefile COPYING INSTALL README doc/design.txt grammar.txt cli.g \
	tkspgmr.tcl UnderstandingTheGPL.txt CHANGES
	
ASRCS = gp32.inc erase_gp32.s program_gp32.s \
	ab32.inc erase_ab32.s program_ab32.s \
	jb8_loader.s

AS19S = erase_gp32.s19 program_gp32.s19 \
	erase_ab32.s19 program_ab32.s19 \
	jb8_loader.s19

ANTLRS = cli.c parser.dlg tokens.h scan.c err.c mode.h

TARGET=spgmr
PROJECT=spgmr08

DLGFLAGS = -C2 # -Wambiguity
ANTLRFLAGS = -gk -w2 -gh -gl -e2

all: $(TARGET) grammar.txt $(AS19S)

$(TARGET): $(OBJS)
	$(CC) $(LDFLAGS) $(OBJS) -o $@ $(LDLIBS)

$(LIBDIR)/program_ab32.s19: program_ab32.s19
	cp $< $@


# Build the grammar

cli.o: mode.h tokens.h cli.c

scan.o: scan.c tokens.h

cli.c parser.dlg tokens.h err.c: cli.g
	$(ANTLR) $(ANTLRFLAGS) cli.g

scan.c mode.h: parser.dlg
	$(DLG) $(DLGFLAGS) parser.dlg scan.c

err.o: err.c tokens.h

# grammar.txt is a listing of the grammar w/o actions
grammar.txt: cli.g
	$(ANTLR) -p cli.g > $@
	

# assemble '08 source to .s19 file
# using asl
%.p: %.s ; asl -L -U $<
%.s19: %.p ; p2hex +5 $< $@


	
link:
	rm -f $(TARGET)
	$(MAKE)


install: $(BINDIR)/$(TARGET) $(BINDIR)/tkspgmr $(LIBDIR)/program_ab32.s19
	

$(BINDIR)/$(TARGET): $(TARGET)
	cp $(TARGET) $(BINDIR)
	
$(BINDIR)/tkspgmr: tkspgmr.tcl
	sed s/Toplevel/tkspgmr/ <tkspgmr.tcl >$(BINDIR)/tkspgmr
	# cp $< $@
	chmod +x $@


clean:
	rm -f *.o core $(TARGET)

rebuild: scrub all

distclean: scrub

# this is historic from pccts makefiles
#deletes the antlr and dlg output files
scrub: clean
	rm -f $(ANTLRS)

.PHONY: tarball patch

tarball: $(PROJECT)-$(BUILD).tar.gz
patch: $(PROJECT)-$(BUILD).patch

$(PROJECT)-$(BUILD): $(AS19S)
	mkdir $(PROJECT)-$(BUILD)
	cp -a $(SRCS) $(HDRS) $(XTRAS) $(ASRCS) $(AS19S) $(ANTLRS) pccts \
		$(PROJECT)-$(BUILD)

$(PROJECT)-$(BUILD).tar.gz: $(TARGET) $(PROJECT)-$(BUILD)
	# $(RM) $@
	strip $(TARGET)
	cp -a $(TARGET) $(PROJECT)-$(BUILD)
	tar  -czf $@ $(PROJECT)-$(BUILD)
	rm -rf $(PROJECT)-$(BUILD)

$(PROJECT)-$(BUILD).patch: $(PROJECT)-$(BUILD)-orig $(PROJECT)-$(BUILD)
	diff -ruNp $^ > $@ || true
	rm -rf $(PROJECT)-$(BUILD)


# Dependencies stuff
# will run anltr to get those files :(
depend: cli.c mode.h
	$(CC) -MM $(SRCS) cli.c $(CFLAGS) > .depend

ifeq (.depend,$(wildcard .depend))
include .depend
endif

# $Log: Makefile,v $
# Revision 1.15  2003/12/16 21:47:56  rwuest
# patches from Anders Nystrom <andesr@southpole.se>
# add jb8 processor
# add patch target
# fix default port bug
# added load_module()
#
# Revision 1.14  2003/12/16 21:32:33  rwuest
# bump rev to 11
# added timec.h
#
# Revision 1.13  2001/08/17 11:56:09  rwuest
# Bump build number to 0.10
# Add Changes to XTRAS macro
# Modify license to eliminate the "any later version clause"
# If a later version comes out, and I want to use it,
# I'll change the license then.
#
# Revision 1.12  2001/08/11 23:05:58  rwuest
# rev 0.9
#
# Revision 1.11  2001/08/11 14:33:00  rwuest
# rev to 0.8
# pass LIBDIR through -D in CFLAGS
#
# Revision 1.10  2001/08/04 16:35:03  rwuest
# bump buil to 0.7
# added LIBDIR for various modules
# added ops.[ch]
# added AB32 modules
# added GUI install target (BINDIR)/tkspgmr:
#
# Revision 1.9  2001/07/29 03:15:34  rwuest
# build 0.6
# remove dlg -Wambiguity flag (too much with garbage token added)
#
# Revision 1.8  2001/07/27 16:51:52  rwuest
# Added UnderstandingTheGPL.txt to XTRAS so it goes in the tarball
# bump BUILD to 0.5
#
# Revision 1.7  2001/07/26 21:10:10  rwuest
# Put user stuff at top, stuff not lilkely to change at the bottom
# Added other compilers for testing: gcc-3.0 and egcs-2.91.66
# Added rules to make .s19 file from .s (asl assembler)
# Added stripped executable to tarball.
#
# Revision 1.6  2001/07/22 02:40:10  rwuest
# add tkspgmr.tcl to XTRAS, aieeee!
#
# Revision 1.5  2001/07/22 02:35:32  rwuest
# add USE_SCOPE
#
# Revision 1.4  2001/07/22 02:02:02  rwuest
# remove libPropList
# fix BUILD variable
#
# Revision 1.3  2001/07/22 00:19:43  rwuest
# Got libs working for static linking (end of link command)
# added libPropList, but that'll may go
# added -D_GNU_SOURCE to CFLAGS
#
# Revision 1.2  2001/07/19 21:56:16  rwuest
# depend need the pccts files
# made an ANTLRS macro for all the antlr and dlg output fils
#
# Revision 1.1  2001/07/19 21:24:53  rwuest
# Initial checkin
#

