#COVERAGE=-fprofile-arcs -ftest-coverage
#PROFILE=-pg
DEBUG=-g
#HOME=/home/kyle
CFLAGS=-O0 -Werror -ansi -pedantic -Wall -Wshadow -Wcast-qual -Wcast-align $(DEBUG) $(PROFILE) $(COVERAGE)
LFLAGS=$(DEBUG) $(PROFILE) $(COVERAGE)
CC=gcc

export CFLAGS
export LFLAGS
export CC

PARTS = libutils libcore libpic12 jal

%.asm : %.jal
	./jalv2 -no-fuse -s chipdef $<

all : jalv2 reltest optest $(PARTS) codread

libutils:
	$(MAKE) -C libutils/libutils.a

libcore:
	$(MAKE) -C libcore/libcore.a

libpic12:
	$(MAKE) -C libpic12/libpic12.a

jal:
	$(MAKE) -C jal/jalv2

jalv2: libutils/libutils.a libcore/libcore.a libpic12/libpic12.a jal/jalv2
	-rm jalv2
	-rm jal/jalv2
	@for a in $(PARTS) ; do $(MAKE) -C $$a ; done
	mv jal/jalv2 .

README = README.txt jaldiff.txt decode.txt LICENSE.txt jalopts.txt README.asm

jalv2src:
	-rm jalv2src.zip
	zip jalv2src.zip `find . -name '*.[ch]' -o -name 'Makefile' -o -name '*.dsp' -o -name '*.dsw'` $(README) README.src tech.txt

depend : 
	@for a in $(PARTS) ; do $(MAKE) -C $$a depend ; done

clean : 
	-rm jalv2 codread *.o
	@for a in $(PARTS) ; do $(MAKE) -C $$a clean ; done

libutils/libutils.a:
	make -C libutils

libcore/libcore.a:
	make -C libcore

libpic12/libpic12.a:
	make -C libpic12

jal/jalv2: libutils/libutils.a libcore/libcore.a libpic12/libpic12.a
	make -C jal

win32: libutils/libutils.a libcore/libcore.a libpic12/libpic12.a
	wine nmake /f "jal/jal.mak" "CFG=jal - Win32 Release"
	wine nmake /f "jal/jal.mak" "CFG=jal - Win32 Debug"

jalv2.zip: jalv2
	strip jalv2
	-rm jalv2.zip
	zip jalv2 $(README) jalv2 jalv2d.exe jalv2.exe chipdef/* mplab/*

package: jalv2.zip jalv2src
	./copyit.sh

test.asm: test.jal jalv2
	./jalv2 -s chipdef\;gw test.jal
	-addcr < test.asm > mtest.asm

test01.asm: stef/test01.jal jalv2
	./jalv2 -d -p -Wno-conversion -warn-stack-overflow -s stef stef/test01.jal -const-detect -temp-reduce

mariner.asm: mariner.jal jalv2
	./jalv2 -d -p -s $(HOME)/bin/share/jal/lib\;chipdef mariner.jal

e1008.asm: e1008.jal jalv2
	./jalv2 -s $(HOME)/bin/share/jal/lib\;chipdef e1008.jal -const-detect -temp-reduce -load-reduce -d

OBJ_RELTEST = reltest.o

reltest : $(OBJ_RELTEST)
	$(CC) -o reltest $(OBJ_RELTEST)

OBJ_OPTEST = optest.o

optest : $(OBJ_OPTEST)
	$(CC) -o optest $(OBJ_OPTEST)

codread : codread.o libutils/libutils.a
	$(CC) $(LFLAGS) -o codread codread.o libutils/libutils.a

