# Constantin Knizhnik
# Moscow Software Center
# Pascal to C/C++ compiler.
#

.SUFFIXES: .cxx .c .h .o .a .exe

default : all

#
# Install path
#
INSTALL_LIB_DIR = 
INSTALL_BIN_DIR = 

#
# Target.
#

BINS =	ptoc.exe cganal.exe
LIBS =  ptoc.a Xbgi.a 

#
# Chose compiler You prefer.
#

CC =	gcc -Zmt
CCFLAGS = -O2 -Wall -c

DEFINES = 
INCLUDES =	-I.

YACC =		bison -d -p zz 
LEX =		flex -i -F -8

#
# Chose linker You prefer.
#

LD =		$(CC)
LDFLAGS = -Zcrtdll


AR =            ar
ARFLAGS =       -ru

#
# File lists.
#

INCS =		main.h token.h token.dpp nmtbl.h bring.h tpexpr.h trnod.h util.h

SRCS =		trnod.cxx nmtbl.cxx bring.cxx tpexpr.cxx util.cxx main.cxx parser.cxx lex.cxx token.cxx dprintf.cxx 

LIB_SRCS =      set.c paslib.c io.c

OBJS =		$(SRCS:.cxx=.o)

LIB_OBJS =      $(LIB_SRCS:.c=.o)

#
# Default way in which source files should be compiled.
#

.cxx.o : 
		$(CC) $(CCFLAGS) $(DEFINES) $(INCLUDES) -o $@ $<

.c.o : 
		$(CC) $(CCFLAGS) $(DEFINES) $(INCLUDES) -o $@ $<

#
# Custom targets.
#


$(OBJS) : $(INCS)

parser.cxx parser.h : parser.y 
		$(YACC) parser.y 
		mv parser.tab.c parser.cxx
		mv parser.tab.h parser.h

lex.cxx : lex.l 
		$(LEX) lex.l
		mv lex.yy.c lex.cxx

lex.o: lex.cxx token.h token.dpp
set.o: set.h ptoc.h 
array.o: array.h ptoc.h 
paslib.o: paslib.h ptoc.h
io.o: io.h ptoc.h 
analyse.o: analyse.cxx main.h

ptoc.a: $(LIB_OBJS)
	rm -f $@
	$(AR) $(ARFLAGS) $@ $(LIB_OBJS)
	ar s $@

cganal.exe : analyse.o
	$(LD) $(LDFLAGS) -o $@ analyse.o

ptoc.exe : $(OBJS)
	$(LD) $(LDFLAGS) -o $@ $(OBJS)

Xbgi.a: 
	(cd Xbgi && $(MAKE) -f makefile.emx)

all : $(BINS) $(LIBS) 

examples: $(BINS) $(LIBS) 
	(cd examples && $(MAKE) -f makefile.emx)

clean:
	rm -f $(BINS) *.a a.out *.exe *.pdb *.lib *.tgz *.zip *.o lex.yy.c \
		parser.tab.* WinBGI/*.pdb yy.log core y.output parser.output *.exe *~
	(cd examples && make -f makefile.emx clean)
	(cd Xbgi && make -f makefile.emx clean)

tgz: clean
	(cd .. && tar cvzf ptoc.tgz ptoc)

copytgz: tgz
	mcopy -o ../ptoc.tgz a:

install: $(BINS) $(LIBS)
	cp $(BINS) $(INSTALL_BIN_DIR)	
	cp $(LIBS) $(INSTALL_LIB_DIR)
