THIS_SRCDIR = src/IO_generator
TOP_SRCDIR = ../..

GEN_TARGETS = in2D_gen out2D_gen in3D_gen out3D_gen inSurf_gen outSurf_gen
DERIV_TARGETS =	userIn2D.cxx userOut2D.cxx userIn3D.cxx userOut3D.cxx userInSurf.cxx userOutSurf.cxx
EXEC_TARGETS = t2d t3d tsurf
 
default: 
	$(MAKE) $(GEN_TARGETS)
	$(MAKE) $(DERIV_TARGETS) $(EXEC_TARGETS)

pre-distrib:
	$(MAKE) $(DERIV_TARGETS)

in2D_gen: input_gen.lex
	$(MAKE) "TYPE=input" "DIM=dim2" "TARGET=in2D_gen" gen

out2D_gen: output_gen.lex
	$(MAKE) "TYPE=output" "DIM=dim2" "TARGET=out2D_gen" gen

inSurf_gen: input_gen.lex
	$(MAKE) "TYPE=input" "DIM=surf" "TARGET=inSurf_gen" gen

outSurf_gen: output_gen.lex
	$(MAKE) "TYPE=output" "DIM=surf" "TARGET=outSurf_gen" gen

in3D_gen: input_gen.lex
	$(MAKE) "TYPE=input" "DIM=dim3" "TARGET=in3D_gen" gen

out3D_gen: output_gen.lex
	$(MAKE) "TYPE=output" "DIM=dim3" "TARGET=out3D_gen" gen

gen:
	echo "Creating lexical analyzer from file $(TYPE)_gen.lex..."
	flex -i $(TYPE)_gen.lex
	echo "Compiling and linking..."
	i686-pc-mingw32-gcc -L/usr/local/lib   $(INCLUDES) -fPIC -O2  -g -D$(DIM) -o $(TARGET) lex.yy.c -lfl
	-rm -f lex.yy.c
	echo "Done"

distclean: localdistclean

localdistclean:
	-rm -f *.c in2D_gen out2D_gen in3D_gen out3D_gen inSurf_gen outSurf_gen t2d t3d tsurf

# The user*.cxx targets should really depend on *_gen files, but this 
# causes problems on machines where flex/lex isn't working adequately.
# So instead the default source files are included, and everything else
# is built only on the specific request of the user.

userIn2D.cxx: user2D.template input_gen.lex
	$(MAKE) in2D_gen
	-./in2D_gen < user2D.template > userIn2D.cxx

userOut2D.cxx: user2D.template output_gen.lex
	$(MAKE) out2D_gen
	-./out2D_gen < user2D.template > userOut2D.cxx

userIn3D.cxx: user3D.template input_gen.lex
	$(MAKE) in3D_gen
	-./in3D_gen < user3D.template > userIn3D.cxx

userOut3D.cxx: user3D.template output_gen.lex
	$(MAKE) out3D_gen
	-./out3D_gen < user3D.template > userOut3D.cxx

userInSurf.cxx: userSurf.template input_gen.lex
	$(MAKE) inSurf_gen
	-./inSurf_gen < userSurf.template > userInSurf.cxx

userOutSurf.cxx: userSurf.template output_gen.lex
	$(MAKE) outSurf_gen
	-./outSurf_gen < userSurf.template > userOutSurf.cxx

user2D.template:
	cp default2D.template user2D.template

user3D.template:
	cp default3D.template user3D.template

userSurf.template:
	cp defaultSurf.template userSurf.template

t2d_in.cxx: in2D_gen t2d_in.template
	-./in2D_gen < t2d_in.template > t2d_in.cxx

t2d_out.cxx: out2D_gen t2d_out.template
	-./out2D_gen < t2d_out.template > t2d_out.cxx

tsurf_in.cxx: inSurf_gen tsurf_in.template
	-./inSurf_gen < tsurf_in.template > tsurf_in.cxx

tsurf_out.cxx: outSurf_gen tsurf_out.template
	-./outSurf_gen < tsurf_out.template > tsurf_out.cxx

t3d_in.cxx: in3D_gen t3d_in.template
	-./in3D_gen < t3d_in.template > t3d_in.cxx

t3d_out.cxx: out3D_gen t3d_out.template
	-./out3D_gen < t3d_out.template > t3d_out.cxx

include ../conf/default-rules

t2d_DEP = t2d.o t2d_in.o t2d_out.o $(LIBDIR)/libGR_2D.a \
	$(LIBDIR)/libGR_base.a \
	$(LIBDIR)/libOptMS.a \
	$(LIBDIR)/libSUMAAlog_lite.a

tsurf_DEP = tsurf.o tsurf_in.o tsurf_out.o $(LIBDIR)/libGR_surf.a \
	$(LIBDIR)/libGR_2D.a \
	$(LIBDIR)/libGR_base.a \
	$(LIBDIR)/libOptMS.a \
	$(LIBDIR)/libSUMAAlog_lite.a

LIBS=-lGR_3D -lGR_surf -lGR_2D -lGR_base -lGR_3D -lOptMS -lSUMAAlog_lite

t2d: $(t2d_DEP)
	echo "Linking t2d..."
	i686-pc-mingw32-g++ -L/usr/local/lib  -g -o t2d t2d.o -L$(LIBDIR) t2d_in.o t2d_out.o $(LIBS)

tsurf: $(tsurf_DEP)
	echo "Linking tsurf..."
	i686-pc-mingw32-g++ -L/usr/local/lib  -g -o tsurf tsurf.o -L$(LIBDIR) tsurf_in.o tsurf_out.o $(LIBS)

t3d_DEP = t3d.o t3d_in.o t3d_out.o \
	$(LIBDIR)/libGR_3D.a \
	$(LIBDIR)/libGR_surf.a \
	$(LIBDIR)/libGR_2D.a \
	$(LIBDIR)/libGR_base.a \
	$(LIBDIR)/libOptMS.a \
	$(LIBDIR)/libSUMAAlog_lite.a

t3d: $(t3d_DEP)
	echo "Linking t3d..."
	i686-pc-mingw32-g++ -L/usr/local/lib  -g -o t3d t3d.o -L$(LIBDIR) t3d_in.o t3d_out.o $(LIBS)

pre-distrib: $(SOURCE_TARGETS)

