#     ZZZZZZZZZZZZZZZZZZZZ    8888888888888       00000000000
#   ZZZZZZZZZZZZZZZZZZZZ    88888888888888888    0000000000000
#                ZZZZZ      888           888  0000         0000
#              ZZZZZ        88888888888888888  0000         0000
#            ZZZZZ            8888888888888    0000         0000       AAAAAA         SSSSSSSSSSS   MMMM       MMMM
#          ZZZZZ            88888888888888888  0000         0000      AAAAAAAA      SSSS            MMMMMM   MMMMMM
#        ZZZZZ              8888         8888  0000         0000     AAAA  AAAA     SSSSSSSSSSS     MMMMMMMMMMMMMMM
#      ZZZZZ                8888         8888  0000         0000    AAAAAAAAAAAA      SSSSSSSSSSS   MMMM MMMMM MMMM
#    ZZZZZZZZZZZZZZZZZZZZZ  88888888888888888    0000000000000     AAAA      AAAA           SSSSS   MMMM       MMMM
#  ZZZZZZZZZZZZZZZZZZZZZ      8888888888888       00000000000     AAAA        AAAA  SSSSSSSSSSS     MMMM       MMMM
#
# Copyright (C) Gunther Strube, InterLogic 1993-99
# Copyright (C) Paulo Custodio, 2011-2013

# $Header: /cvsroot/z88dk/z88dk/src/z80asm/Makefile,v 1.65 2013/08/26 21:49:39 pauloscustodio Exp $
# $Log: Makefile,v $
# Revision 1.65  2013/08/26 21:49:39  pauloscustodio
# Bug report 2013-07-27 10:50:27 by rkd77 : compile with -Wformat -Werror=format-security
#
# Revision 1.64  2013/06/16 20:14:39  pauloscustodio
# Move deffile writing to deffile.c, remove global variable deffile
#
# Revision 1.63  2013/06/16 18:01:54  pauloscustodio
# Add testzcc target to compile and run a few examples using the assembler.
#
# Revision 1.62  2013/06/15 00:26:23  pauloscustodio
# Move mapfile writing to mapfile.c.
#
# Revision 1.61  2013/06/10 23:08:03  pauloscustodio
# depends
#
# Revision 1.60  2013/06/08 22:58:22  pauloscustodio
# Add module.c
#
# Revision 1.59  2013/06/01 01:24:21  pauloscustodio
# CH_0022 : Replace avltree by hash table for symbol table
#
# Revision 1.58  2013/05/23 22:22:23  pauloscustodio
# Move symbol to sym.c, rename to Symbol
#
# Revision 1.57  2013/05/16 23:39:48  pauloscustodio
# Move struct node to sym.c, rename to Symbol
# Move SymbolRef to symref.c
#
# Revision 1.56  2013/05/11 00:22:33  pauloscustodio
# depends
#
# Revision 1.55  2013/05/07 22:43:24  pauloscustodio
# Build gccmake.bat so that CVS Id is not always changed, causing file to be always
#  modified
#
# Revision 1.54  2013/05/02 21:20:14  pauloscustodio
# depends
#
# Revision 1.53  2013/05/01 19:03:45  pauloscustodio
# Simplified scanner and adapted to be used with a BISON generated parser.
# Removed balanced struct checking and token ring.
# Removed start condition to list assembly lines, as it was difficult to keep in sync across included
# files; inserted an RS char in the input before each line to trigger listing.
# Allow ".NAME" and "NAME:" to return a NAME token, so that ".LD" is recognized as a label and not the LD assembly statement.
# Added Integer out of range warning to number scanning routine.
# Allow input lines to be any size, as long as memory can be allocated.
# Created a skeleton BISON parser.
#
# Revision 1.52  2013/04/21 22:51:51  pauloscustodio
# Simplified flex build
#
# Revision 1.51  2013/04/14 18:16:58  pauloscustodio
# Split scanner in several modules, allow token look-ahead to simplify
# parser.
#
# Revision 1.50  2013/03/31 13:16:48  pauloscustodio
# Embryo of parse module
#
# Revision 1.49  2013/03/29 23:53:07  pauloscustodio
# Added GNU Flex-based scanner. Not yet integrated into assembler.
#
# Revision 1.48  2013/03/02 23:49:44  pauloscustodio
# depends
#
# Revision 1.47  2013/02/27 22:29:59  pauloscustodio
# New srcfile.c to handle reading lines from source files
#
# Revision 1.46  2013/02/26 02:11:32  pauloscustodio
# New model_symref.c with all symbol cross-reference list handling
#
# Revision 1.45  2013/02/25 21:37:30  pauloscustodio
# Show output difference of t_run_module() in visual-diff, to allow easy merge of changes
#
# Revision 1.44  2013/02/19 22:52:40  pauloscustodio
# BUG_0030 : List bytes patching overwrites header
# BUG_0031 : List file garbled with input lines with 255 chars
# New listfile.c with all the listing related code
#
# Revision 1.43  2013/01/30 00:39:25  pauloscustodio
# New CLASS_LIST() to create lists of objects defined with CLASS()
#
# Revision 1.42  2013/01/20 20:43:59  pauloscustodio
# make clean now deletes Microsoft Visual C++ output files
#
# Revision 1.41  2013/01/18 22:59:17  pauloscustodio
# CH_0016 : StrHash class to create maps from string to void*
# Created the StrHash to create hash tables mapping string keys kept in
# strpool to void* user pointer.
#
# Revision 1.40  2013/01/10 23:33:49  pauloscustodio
# Fix dependencies for MSVC_TARGET
#
# Revision 1.39  2013/01/10 22:54:18  pauloscustodio
# Add testmsvc to run tests with executable generated by MS Visual Studio - stops with debugger on memory allocation/usage errors
#
# Revision 1.38  2013/01/10 22:32:16  pauloscustodio
# Dont force gcc
#
# Revision 1.37  2012/11/03 17:27:51  pauloscustodio
# Remove all test* in make clean
#
# Revision 1.36  2012/08/01 21:08:02  dom
# Allow x-compilation of z80asm
#
# Revision 1.35  2012/06/14 15:04:58  pauloscustodio
# New safestr.o, dynstr.o
#
# Revision 1.34  2012/05/26 18:50:06  pauloscustodio
# Use gcc to compile instead of cc.
#
# Revision 1.33  2012/05/24 21:42:42  pauloscustodio
# CH_0011 : new string list class to hold lists of strings
#
# Revision 1.32  2012/05/24 17:50:02  pauloscustodio
# CH_0010 : new string pool to hold strings for all program duration
#
# Revision 1.31  2012/05/24 17:01:45  pauloscustodio
# CH_0009 : new CLASS to define simple classes
#
# Revision 1.30  2012/05/24 15:02:26  pauloscustodio
# depends
#
# Revision 1.29  2012/05/17 20:29:32  pauloscustodio
# depends
#
# Revision 1.28  2012/05/17 17:11:47  pauloscustodio
# depends
#
# Revision 1.27  2012/05/17 14:59:39  pauloscustodio
# New dio.o
#
# Revision 1.26  2012/05/11 19:29:49  pauloscustodio
# Format code with AStyle (http://astyle.sourceforge.net/) to unify brackets, spaces instead of tabs, indenting style, space padding in parentheses and operators. Options written in the makefile, target astyle.
#         --mode=c
#         --lineend=linux
#         --indent=spaces=4
#         --style=ansi --add-brackets
#         --indent-switches --indent-classes
#         --indent-preprocessor --convert-tabs
#         --break-blocks
#         --pad-oper --pad-paren-in --pad-header --unpad-paren
#         --align-pointer=name
#
# Revision 1.25  2011/10/14 13:56:51  pauloscustodio
# New module scrutil
#
# Revision 1.24  2011/10/07 17:23:57  pauloscustodio
# Delete all *.exe in clean
#
# Revision 1.23  2011/09/29 21:27:16  pauloscustodio
# Updated depend
#
# Revision 1.22  2011/08/21 20:18:33  pauloscustodio
# Dependencies updated
#
# Revision 1.21  2011/08/19 15:53:58  pauloscustodio
# BUG_0010 : heap corruption when reaching MAXCODESIZE
# - test for overflow of MAXCODESIZE is done before each instruction at parseline(); if only one byte is available in codearea, and a 2 byte instruction is assembled, the heap is corrupted before the exception is raised.
# - Factored all the codearea-accessing code into a new module, checking for MAXCODESIZE on every write.
#
# Revision 1.20  2011/08/18 23:16:10  pauloscustodio
# Dependencies
#
# Revision 1.19  2011/08/18 21:45:17  pauloscustodio
# new object file.o
#
# Revision 1.18  2011/08/05 19:30:57  pauloscustodio
# CH_0004 : Exception mechanism to handle fatal errors
#
# Revision 1.17  2011/07/18 00:48:25  pauloscustodio
# Initialize MS Visual Studio DEBUG build to show memory leaks on exit
#
# Revision 1.16  2011/07/14 01:32:08  pauloscustodio
#     - Unified "Integer out of range" and "Out of range" errors; they are the same error.
#     - Unified ReportIOError as ReportError(ERR_FILE_OPEN)
#     CH_0003 : Error messages should be more informative
#         - Added printf-args to error messages, added "Error:" prefix.
#     BUG_0006 : sub-expressions with unbalanced parentheses type accepted, e.g. (2+3] or [2+3)
#         - Raise ERR_UNBALANCED_PAREN instead
#
# Revision 1.15  2011/07/12 22:47:59  pauloscustodio
# - Moved all error variables and error reporting code to a separate module errors.c,
#   replaced all extern declarations of these variables by include errors.h,
#   created symbolic constants for error codes.
# - Added test scripts for error messages.
#
# Revision 1.14  2011/07/11 15:54:37  pauloscustodio
# new module options.o
#
# Revision 1.13  2011/07/09 18:25:35  pauloscustodio
# Log keyword in checkin comment was expanded inside Log expansion... recursive
# Added Z80asm banner to all source files
#
# Revision 1.12  2011/07/09 17:36:09  pauloscustodio
# Copied cvs log into Log history
#
# Revision 1.11  2011/07/09 01:46:00  pauloscustodio
# Added Log keyword
# 
# Revision 1.10  2011/07/09 01:12:31  pauloscustodio
# Added test and depend targets
# 
# Revision 1.9  2011/06/26 16:07:29  dom
# Add some support for using binutils
# 
# Revision 1.8  2009/07/19 18:54:52  dom
# introduce exesuffix so that we can use mingw to produce an automagic build
# 
# Revision 1.7  2002/12/09 18:49:46  dom
# simplified the makefiles somewhat, allow cross building for Amiga on linux
# 
# Revision 1.6  2002/10/22 19:15:27  dom
# Makefile changes to use $(RM) instead of rm
# 
# Revision 1.5  2002/07/15 17:45:46  dom
# updates to ensure that the thing can install in /usr/local/bin /usr/local/z88dk on unix systems
# 
# Revision 1.4  2002/01/20 23:21:14  dom
# bumped version number and added prototype (from Dennis)
# 
# Revision 1.3  2001/02/28 17:59:22  dom
# Added UNDEFINE for undefining symbols, bumped version to 1.0.18
# 
# Revision 1.2  2001/01/23 10:00:08  dom
# Changes by x1cygnus:
# 
# just added a harcoded macro Invoke, similar to callpkg except that the
# instruction 'Invoke' resolves to a call by default (ti83) and to a RST if
# the parameter -plus is specified on the command line.
# 
# Changes by dom:
# Added in a rudimentary default directory set up (Defined at compile time)
# a bit kludgy and not very nice!
# 
# Revision 1.1  2000/07/04 15:33:30  dom
# branches:  1.1.1;
# Initial revision
# 
# Revision 1.1.1.1  2000/07/04 15:33:30  dom
# First import of z88dk into the sourceforge system <gulp>
#

OBJS = 	\
	asmdrctv.o 		\
	class.o			\
	codearea.o		\
	deffile.o		\
	die.o			\
	dynstr.o		\
	errors.o		\
	except.o		\
	exprprsr.o		\
	file.o			\
	hist.o			\
	ldinstr.o		\
	listfile.o		\
	mapfile.o		\
	memalloc.o		\
	model.o			\
	model_symref.o	\
	modlink.o		\
	module.o		\
	objfile.o		\
	options.o		\
	parse.o			\
	prsident.o		\
	prsline.o		\
	safestr.o		\
	scan.o			\
	srcfile.o		\
	strhash.o		\
	strlist.o		\
	strpool.o		\
	strutil.o		\
	sym.o			\
	symref.o		\
	symtab.o		\
	z80asm.o		\
	z80instr.o		\
	z80pass.o		\

ASTYLE = astyle \
	--mode=c \
	--lineend=linux \
	--indent=spaces=4 \
	--style=ansi --add-brackets \
	--indent-switches --indent-classes \
	--indent-preprocessor --convert-tabs \
	--break-blocks \
	--pad-oper --pad-paren-in --pad-header --unpad-paren \
	--align-pointer=name

TARGET =		z80asm$(EXESUFFIX)
MSVC_TARGET =	../../win32/Debug/$(TARGET)

CFLAGS +=		-W -Wformat -Werror=format-security

all: $(TARGET) gccmake.bat

$(TARGET): $(OBJS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $(TARGET)  $(OBJS)

# flex
scan.c scan.h : scan.l
	flex -L scan.l
	perl -i.bak -ne "print unless /^#line/" scan.c scan.h
	$(RM) scan.c.bak scan.h.bak

# bison
parse.c parse.h : parse.y
	bison parse.y
	
install:
	install $(TARGET) $(PREFIX)/bin/

clean :
	$(RM) *.o *.exe *~ *.bak *.orig core $(TARGET) test* t/*.tmp t/developer/*.tmp 
	$(RM) microman.bin microman.tap mandel.bin mandel.tap zcc_opt.def
	$(RM) -rf Debug Release x1 x2 x3

# test
test : $(TARGET)
	perl -S prove t/*.t

testmsvc : $(MSVC_TARGET)
	make Z80ASM=$(MSVC_TARGET) test

testzcc : $(TARGET)
	zcc +zx -lndos       -create-app -omicroman.bin ../../examples/microman.c
	zcc +zx -lndos -lmzx -create-app -omandel.bin   ../../examples/graphics/mandel.c
	eightyone microman.tap
	eightyone mandel.tap

# build with msvc - need to build manualy, devenv not available in Express
$(MSVC_TARGET) : $(wildcard *.c) $(wildcard *.h)
	@echo Build z80asm inside Microsoft Visual C++ 2010 Express
	@exit 1

# formatting style
astyle:
	$(ASTYLE) *.c *.h

# gccmake.bat - work arround impossibility of writing a '$' in the perl code
gccmake.bat : Makefile
	perl -e " \
			print \
				'@echo off', qq(\n); \
			@printed = (); \
			open(F, 'gccmake.bat'); \
			while (<F>) { \
				next unless /^rem.*Id/; \
				push @printed, 1; \
				print; \
				last; \
			} \
			close(F); \
			@printed or print 'rem ','$$','Id','$$',qq(\n); \
			print \
				qq(\n), \
				'echo *******************', qq(\n), \
				'echo * Building z80asm *', qq(\n), \
				'echo *******************', qq(\n), \
				'del *.o', qq(\n); \
			for (@ARGV) { \
				s/\.o$$//; \
				print 'gcc -c -o '; \
				print; \
				print '.o ', ' ' x (20 - length); \
				print; \
				print '.c'; \
				print qq(\n); \
			} \
			print \
				'gcc -o z80asm.exe *.o', qq(\n), \
				'copy z80asm.exe ..\\..\\bin', qq(\n), \
				'del z80asm.exe', qq(\n); \
		" $(OBJS) > gccmake.bat.new
	mv gccmake.bat.new gccmake.bat
	
# Dependencies
depend:
	makedepend -Y -- $(CFLAGS) -- $(OBJS:.o=.c)
	perl -i.bak -pe "s!\.\.\/config\.h!!g" Makefile

# DO NOT DELETE

asmdrctv.o: memalloc.h except.h e4c.h codearea.h types.h config.h 
asmdrctv.o: errors.h errors_def.h file.h safestr.h strlist.h class.h queue.h
asmdrctv.o: listfile.h dynstr.h symref.h classlist.h options.h symbol.h
asmdrctv.o: model.h objfile.h symtab.h classhash.h strhash.h uthash.h die.h
asmdrctv.o: sym.h srcfile.h z80asm.h
class.o: memalloc.h except.h e4c.h class.h queue.h types.h die.h
codearea.o: memalloc.h except.h e4c.h codearea.h types.h config.h 
codearea.o: errors.h errors_def.h symbol.h model.h objfile.h class.h queue.h
codearea.o: symtab.h classhash.h strhash.h uthash.h die.h sym.h symref.h
codearea.o: classlist.h z80asm.h file.h safestr.h strlist.h listfile.h
codearea.o: dynstr.h
deffile.o: memalloc.h except.h e4c.h deffile.h file.h safestr.h types.h
deffile.o: strlist.h class.h queue.h listfile.h dynstr.h symref.h classlist.h
deffile.o: options.h symbol.h model.h objfile.h symtab.h classhash.h
deffile.o: strhash.h uthash.h die.h sym.h z80asm.h config.h 
die.o: memalloc.h except.h e4c.h die.h types.h strutil.h safestr.h
dynstr.o: memalloc.h except.h e4c.h dynstr.h class.h queue.h types.h
dynstr.o: strutil.h
errors.o: memalloc.h except.h e4c.h class.h queue.h types.h errors.h
errors.o: errors_def.h file.h safestr.h strlist.h strhash.h uthash.h die.h
errors.o: strpool.h strutil.h
except.o: memalloc.h except.h e4c.h types.h die.h e4c.c
exprprsr.o: memalloc.h except.h e4c.h config.h  symbol.h model.h
exprprsr.o: objfile.h class.h queue.h types.h symtab.h classhash.h strhash.h
exprprsr.o: uthash.h die.h sym.h symref.h classlist.h options.h errors.h
exprprsr.o: errors_def.h file.h safestr.h strlist.h codearea.h z80asm.h
file.o: memalloc.h except.h e4c.h class.h queue.h types.h config.h
file.o:  errors.h errors_def.h file.h safestr.h strlist.h
file.o: strpool.h strutil.h uthash.h die.h
hist.o: memalloc.h except.h e4c.h hist.h
ldinstr.o: memalloc.h except.h e4c.h config.h  z80asm.h types.h
ldinstr.o: sym.h class.h queue.h symref.h classlist.h symbol.h model.h
ldinstr.o: objfile.h symtab.h classhash.h strhash.h uthash.h die.h errors.h
ldinstr.o: errors_def.h codearea.h options.h
listfile.o: memalloc.h except.h e4c.h listfile.h class.h queue.h types.h
listfile.o: dynstr.h symref.h classlist.h file.h safestr.h strlist.h
listfile.o: options.h symbol.h model.h objfile.h symtab.h classhash.h
listfile.o: strhash.h uthash.h die.h sym.h z80asm.h config.h 
listfile.o: errors.h errors_def.h hist.h strpool.h strutil.h codearea.h
mapfile.o: memalloc.h except.h e4c.h file.h safestr.h types.h strlist.h
mapfile.o: class.h queue.h listfile.h dynstr.h symref.h classlist.h mapfile.h
mapfile.o: options.h symbol.h model.h objfile.h symtab.h classhash.h
mapfile.o: strhash.h uthash.h die.h sym.h strpool.h z80asm.h config.h
mapfile.o: 
memalloc.o: memalloc.h except.h e4c.h types.h die.h queue.h
modlink.o: memalloc.h except.h e4c.h codearea.h types.h config.h 
modlink.o: errors.h errors_def.h file.h safestr.h strlist.h class.h queue.h
modlink.o: listfile.h dynstr.h symref.h classlist.h options.h symbol.h
modlink.o: model.h objfile.h symtab.h classhash.h strhash.h uthash.h die.h
modlink.o: sym.h strpool.h strutil.h z80asm.h
module.o: memalloc.h except.h e4c.h module.h class.h queue.h types.h
module.o: classlist.h dynstr.h symtab.h classhash.h strhash.h uthash.h die.h
module.o: sym.h symref.h
objfile.o: memalloc.h except.h e4c.h class.h queue.h types.h errors.h
objfile.o: errors_def.h file.h safestr.h strlist.h objfile.h strpool.h
options.o: memalloc.h except.h e4c.h errors.h errors_def.h file.h safestr.h
options.o: types.h strlist.h class.h queue.h options.h symbol.h model.h
options.o: objfile.h symtab.h classhash.h strhash.h uthash.h die.h sym.h
options.o: symref.h classlist.h srcfile.h dynstr.h strpool.h z80asm.h
options.o: config.h 
parse.o: memalloc.h except.h e4c.h
prsident.o: memalloc.h except.h e4c.h codearea.h types.h config.h 
prsident.o: errors.h errors_def.h listfile.h class.h queue.h dynstr.h
prsident.o: symref.h classlist.h options.h symbol.h model.h objfile.h
prsident.o: symtab.h classhash.h strhash.h uthash.h die.h sym.h z80asm.h
prsline.o: memalloc.h except.h e4c.h config.h  z80asm.h types.h
prsline.o: sym.h class.h queue.h symref.h classlist.h symbol.h model.h
prsline.o: objfile.h symtab.h classhash.h strhash.h uthash.h die.h options.h
prsline.o: errors.h errors_def.h
safestr.o: memalloc.h except.h e4c.h safestr.h types.h strutil.h
scan.o: memalloc.h except.h e4c.h parse.h types.h codearea.h class.h queue.h
scan.o: classlist.h die.h dynstr.h errors.h errors_def.h listfile.h symref.h
scan.o: options.h symbol.h model.h objfile.h symtab.h classhash.h strhash.h
scan.o: uthash.h sym.h srcfile.h strpool.h strutil.h
srcfile.o: memalloc.h except.h e4c.h codearea.h types.h errors.h errors_def.h
srcfile.o: file.h safestr.h strlist.h class.h queue.h listfile.h dynstr.h
srcfile.o: symref.h classlist.h options.h symbol.h model.h objfile.h symtab.h
srcfile.o: classhash.h strhash.h uthash.h die.h sym.h srcfile.h strpool.h
strhash.o: memalloc.h except.h e4c.h strhash.h types.h class.h queue.h
strhash.o: uthash.h die.h strpool.h
strlist.o: memalloc.h except.h e4c.h strlist.h class.h queue.h types.h
strlist.o: strpool.h
strpool.o: memalloc.h except.h e4c.h strpool.h class.h queue.h types.h
strpool.o: uthash.h die.h
strutil.o: memalloc.h except.h e4c.h strutil.h
sym.o: memalloc.h except.h e4c.h listfile.h class.h queue.h types.h dynstr.h
sym.o: symref.h classlist.h options.h symbol.h model.h objfile.h symtab.h
sym.o: classhash.h strhash.h uthash.h die.h sym.h safestr.h strpool.h
symref.o: memalloc.h except.h e4c.h symref.h class.h queue.h types.h
symref.o: classlist.h
symtab.o: memalloc.h except.h e4c.h errors.h errors_def.h listfile.h class.h
symtab.o: queue.h types.h dynstr.h symref.h classlist.h options.h symbol.h
symtab.o: model.h objfile.h symtab.h classhash.h strhash.h uthash.h die.h
symtab.o: sym.h z80asm.h config.h 
z80asm.o: memalloc.h except.h e4c.h codearea.h types.h config.h 
z80asm.o: deffile.h errors.h errors_def.h file.h safestr.h strlist.h class.h
z80asm.o: queue.h hist.h listfile.h dynstr.h symref.h classlist.h mapfile.h
z80asm.o: objfile.h options.h symbol.h model.h symtab.h classhash.h strhash.h
z80asm.o: uthash.h die.h sym.h strpool.h strutil.h z80asm.h
z80instr.o: memalloc.h except.h e4c.h config.h  z80asm.h types.h
z80instr.o: sym.h class.h queue.h symref.h classlist.h symbol.h model.h
z80instr.o: objfile.h symtab.h classhash.h strhash.h uthash.h die.h options.h
z80instr.o: errors.h errors_def.h codearea.h
z80pass.o: memalloc.h except.h e4c.h codearea.h types.h config.h 
z80pass.o: errors.h errors_def.h file.h safestr.h strlist.h class.h queue.h
z80pass.o: hist.h listfile.h dynstr.h symref.h classlist.h model.h options.h
z80pass.o: symbol.h objfile.h symtab.h classhash.h strhash.h uthash.h die.h
z80pass.o: sym.h scan.h parse.h strutil.h z80asm.h
