#
# server Makefile - build ww, wg, fmtb_parse, and other programs
#
# Copyright (C) 2006  Nathan (Acorn) Pooley
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.
# 
# 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 (gpl.txt) 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
# 
# You can contact the author, Nathan (Acorn) Pooley, by writing
# to Nathan (Acorn) Pooley, 949 Buckeye Drive, Sunnyvale, CA  94086, USA
# or through the form at http://www.rawbw.com/~acorn/wand/feedback.html
#

TARGETS	:= \
			ww \
			graph \
			wg \
			my_sqrt16 \
			calc \
			gg \
			parse \
			check \
			fmtb_parse \
			fff \
			fmtb_parse_oldA \
			wparse.inc \
			wmspells.inc \

#      serv

MAKEFLAGS	:= -r

OBJS	:=	\
			ac_assert.o \
			ac_fdio.o \
			ac_hexfile.o \
			ac_log.o \
			ac_serialio.o \
			ac_time.o \
			calc.o \
			check.o \
			fmtb_parse.o \
			gg.c \
			gg.o \
			graph.o \
			my_sqrt16.o \
			parse.o \
			wandio.o \
			wg.c \
			wg.o \
			ww.o \
			picsim.o \

OBJS_ww	:=	\
			ww.o \
			wandio.o \
			graph.o \
			ac_serialio.o \
			ac_fdio.o \
			ac_time.o \
			ac_hexfile.o \
			ac_log.o \

OBJS_serv	:=	\
			serv.o \
			ac_serialio.o \
			ac_fdio.o \
			ac_time.o \
			ac_hexfile.o \
			ac_log.o \

OBJS_wg	:=	\
			wg.o \
			graph.o \
			ac_serialio.o \
			ac_fdio.o \
			ac_time.o \
			ac_hexfile.o \
			ac_log.o \

OBJS_gg	:=	\
			gg.o \
			graph.o \

OBJS_my_sqrt16	:=	\
			my_sqrt16.o \
			ac_assert.o \

OBJS_calc	:=	\
			calc.o \

OBJS_parse	:=	\
			parse.o \

OBJS_fmtb_parse	:=	\
			fmtb_parse.o \
			picsim.o \
			ac_assert.o \

OBJS_fmtb_parse_oldA	:=	\
			fmtb_parse_oldA.o \

OBJS_check	:=	\
			check.o \
			ac_assert.o \

default: all

all: $(OBJS) $(TARGETS)

LINK_CMD	= \
		echo "======= Linking $@" ; \
		gcc -g -Wall -o $@ $+ -L/usr/X11R6/lib -lGL -lXext -lX11 -lm

#serv: $(OBJS_COMMON) serv.o
#	gcc -g -Wall -o $@ $+

serv: $(OBJS_serv)
	$(LINK_CMD)

ww: $(OBJS_ww)
	$(LINK_CMD)

wg: $(OBJS_wg)
	$(LINK_CMD)

gg: $(OBJS_wg)
	$(LINK_CMD)

calc: $(OBJS_calc)
	$(LINK_CMD)

parse: $(OBJS_parse)
	$(LINK_CMD)

fmtb_parse: $(OBJS_fmtb_parse)
	$(LINK_CMD)

fff: fmtb_parse
	cp $< $@

fmtb_parse.o: fmtb_pic.c

fmtb_parse_oldA: $(OBJS_fmtb_parse_oldA)
	$(LINK_CMD)

check: $(OBJS_check)
	$(LINK_CMD)

my_sqrt16: $(OBJS_my_sqrt16)
	$(LINK_CMD)

graph: graph.c
	gcc -g -DGRAPHTEST -Wall -o $@ $<  -L/usr/X11R6/lib -lGL -lXext -lX11 -lm

%.o: %.c
	echo "======= Compiling $@"
	gcc -g -Wall -I../sw -c -MD -o $@ $<

wparse.inc: fmtb_pic.c picsim2pic.seds Makefile spells.h
	echo "Creating $@ from $<"
	cpp -DPIC_CSIM=0 -Wundef -C $< $<.i
	echo ";" > $@
	echo "; AUTOGENERATED FILE - DO NOT EDIT" >> $@
	echo ";" >> $@
	echo "; This file is generated from $<" >> $@
	echo "; using cpp and sed (with picsim2pic.seds)." >> $@
	echo "; See Makefile $@ rule for exact commands." >> $@
	echo ";" >> $@
	echo "" >> $@
	grep '#define' spells.h | grep -v '\\' | sed -f picsim2pic.seds >> $@
	echo "" >> $@
	grep '#define' $< | grep -v '\\' | sed -f picsim2pic.seds >> $@
	echo "" >> $@
	grep 'picReg' $<.i | \
		sed -e 's|.*([ 	]*||' \
			-e 's|[ 	]*[),].*||' \
			-e 's|.*|picReg(&)|' | \
		sort -u | \
		sed -f picsim2pic.seds >> $@
	echo "" >> $@
	grep -v 'picReg' $<.i | sed -f picsim2pic.seds >> $@
	
wmspells.inc: fmtb_parse Makefile
	echo "Creating $@ from $<"
	$< -s -q -W$@
	
dist_files:
	\ls Makefile *.c *.h
	\ls mwanemes.txt notes.txt problems.txt
	\ls aaa ggg htt lll rrr th tth w2g wgg ws 
	\ls quicktest regress
	\ls try* | grep -v result


dist_data_files:
	\ls picfmtb_official/wpic_*.txt
	\ls picfmtb_spells/wpic_*.txt
	\ls picfmtb_sean/wpic_*.txt

clean:
	echo "======= Cleaning"
	\rm -f *.d *.o $(TARGETS) *.i

.PHONY: clean dist_files

-include *.d

n=.SILENT
$n dummy:


