#
#    This source code is free software; you can redistribute it
#    and/or modify it in source code form under the terms of the GNU
#    Library General Public License as published by the Free Software
#    Foundation; either version 2 of the License, or (at your option)
#    any later version.
#
#    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 Library General Public License for more details.
#
#    You should have received a copy of the GNU Library General Public
#    License along with this program; if not, write to the Free
#    Software Foundation, Inc.,
#    59 Temple Place - Suite 330
#    Boston, MA 02111-1307, USA
#
#ident "$Id: Makefile.in,v 1.62 2004/12/11 02:31:29 steve Exp $"
#
#
SHELL = /bin/sh

prefix = @prefix@
exec_prefix = @exec_prefix@
srcdir = @srcdir@

VPATH = $(srcdir)

bindir = @bindir@
libdir = @libdir@
mandir = @mandir@
includedir = @includedir@

libdir64 = @libdir64@

CC = @CC@
HOSTCC = @CC@
CXX = @CXX@
INSTALL = @INSTALL@
INSTALL_SCRIPT = @INSTALL_SCRIPT@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
STRIP = @STRIP@
RANLIB = @RANLIB@
LEX = @LEX@
YACC = @YACC@

CPPFLAGS = @ident_support@ -I. -I.. -I $(srcdir) -I$(srcdir)/.. @CPPFLAGS@ @DEFS@
CXXFLAGS = -Wall @CXXFLAGS@
LDFLAGS = @rdynamic@ @LDFLAGS@
LIBS = @LIBS@ @EXTRALIBS@

dllib=@DLLIB@

MDIR1 = -DMODULE_DIR1=\"$(libdir)/ivl/@vpidir1@\"
ifneq (x@vpidir2@,x)
MDIR2 = -DMODULE_DIR2=\"$(libdir)/ivl/@vpidir2@\"
endif

ifeq (@enable_vvp32@,yes)
ALL32 = all32
INSTALL32 = install32
UNINSTALL32 = uninstall32
endif

all: dep vvp@EXEEXT@ libvpi.a $(ALL32)

clean:
	rm -f *.o *~ parse.cc parse.cc.output parse.h lexor.cc tables.cc
	rm -rf dep vvp@EXEEXT@ libvpi.a bin32

distclean: clean
	rm -f Makefile config.status config.log config.cache config.h

check: all
	./vvp -M../vpi $(srcdir)/examples/hello.vvp | grep 'Hello, World.'

V = vpi_modules.o vpi_callback.o vpi_const.o vpi_event.o vpi_iter.o vpi_mcd.o \
vpi_priv.o vpi_scope.o vpi_real.o vpi_signal.o vpi_tasks.o vpi_time.o \
vpi_memory.o vpi_vthr_vector.o vpip_bin.o vpip_hex.o vpip_oct.o \
vpip_to_dec.o vpip_format.o vvp_vpi.o

O = main.o parse.o parse_misc.o lexor.o arith.o bufif.o compile.o \
functor.o fvectors.o npmos.o part.o resolv.o stop.o symbols.o ufunc.o \
codes.o \
vthread.o schedule.o statistics.o tables.o udp.o vvp_net.o memory.o \
force.o event.o logic.o delay.o words.o $V

ifeq (@WIN32@,yes)
# Under Windows (mingw) I need to make the ivl.exe in two steps.
# The first step makes an ivl.exe that dlltool can use to make an
# export and import library, and the last link makes a, ivl.exe
# that really exports the things that the import library imports.
vvp@EXEEXT@ libvpi.a: $O vvp.def
	$(CXX) -o vvp@EXEEXT@ $(LDFLAGS) $O $(dllib) $(LIBS)
	dlltool --dllname vvp@EXEEXT@ --def vvp.def \
		--output-lib libvpi.a --output-exp vvp.exp
	$(CXX) -o vvp@EXEEXT@ vvp.exp $(LDFLAGS) $O $(dllib) $(LIBS)
else
libvpi.a: libvpi.c
	$(CC) -c $(srcdir)/libvpi.c
	rm -f libvpi.a
	ar cqv libvpi.a libvpi.o
	$(RANLIB) libvpi.a

vvp: $O
	$(CXX) $(LDFLAGS) -o vvp $O $(LIBS) $(dllib)
endif

dep:
	mkdir dep

%.o: %.cc
	$(CXX) $(CPPFLAGS) $(MDIR1) $(MDIR2) $(CXXFLAGS) -MD -c $< -o $*.o
	mv $*.d dep/$*.d

%.o: %.c
	$(CC) $(CPPFLAGS) $(MDIR1) $(MDIR2) $(CFLAGS) -MD -c $< -o $*.o
	mv $*.d dep/$*.d

tables.cc: $(srcdir)/draw_tt.c
	$(HOSTCC) -o draw_tt $(srcdir)/draw_tt.c
	./draw_tt > tables.cc
	rm draw_tt@EXEEXT@

ifeq (@enable_vvp32@,yes)

all32: bin32 vvp32

bin32:
	mkdir bin32

bin32/%.o: %.cc
	$(CXX) -m32 -DSIZEOF_UNSIGNED_LONG=4 $(CPPFLAGS) $(MDIR2) $(CXXFLAGS) -MD -c $< -o $@

bin32/%.o: %.c
	$(CC) -m32 $(CPPFLAGS) $(MDIR2) $(CFLAGS) -MD -c $< -o $@

bin32/lexor.o: lexor.cc parse.h

vvp32: $(addprefix bin32/,$O) bin32/libvpi.a
	$(CXX) -m32 $(LDFLAGS) -o vvp32 $(addprefix bin32/,$O) $(LIBS) $(dllib)

bin32/libvpi.a: libvpi.c
	$(CC) -m32 $(CPPFLAGS) $(CFLAGS) -o bin32/libvpi.o -c $(srcdir)/libvpi.c
	rm -f bin32/libvpi.a
	ar cqv bin32/libvpi.a bin32/libvpi.o
	$(RANLIB) bin32/libvpi.a

-include $(patsubst bin32/%.o, bin32/%.d, $(addprefix bin32/,$O))
endif

lexor.o: lexor.cc parse.h

parse.o: parse.cc

tables.o: tables.cc

parse.cc: $(srcdir)/parse.y
	$(YACC) --verbose -t -d -o parse.cc $(srcdir)/parse.y

parse.h: parse.cc
	mv parse.cc.h parse.h 2>/dev/null || mv parse.hh parse.h

lexor.cc: $(srcdir)/lexor.lex
	$(LEX) -s -olexor.cc $(srcdir)/lexor.lex

vvp.pdf: $(srcdir)/vvp.man
	man -t $(srcdir)/vvp.man | ps2pdf - vvp.pdf

ifeq (@WIN32@,yes)
INSTALL_DOC = $(prefix)/vvp.pdf $(mandir)/man1/vvp.1
INSTALL_DOCDIR = $(mandir)/man1
all: vvp.pdf
else
INSTALL_DOC = $(mandir)/man1/vvp.1
INSTALL_DOCDIR = $(mandir)/man1
endif

Makefile: Makefile.in config.status
	./config.status


install: all installdirs $(bindir)/vvp@EXEEXT@ $(libdir64)/libvpi.a $(INSTALL_DOC) $(INSTALL32)

$(bindir)/vvp@EXEEXT@: ./vvp@EXEEXT@
	$(INSTALL_PROGRAM) ./vvp@EXEEXT@ $(bindir)/vvp@EXEEXT@

$(libdir64)/libvpi.a : ./libvpi.a
	$(INSTALL_DATA) libvpi.a $(libdir64)/libvpi.a

$(mandir)/man1/vvp.1: $(srcdir)/vvp.man
	$(INSTALL_DATA) $(srcdir)/vvp.man $(mandir)/man1/vvp.1

$(prefix)/vvp.pdf: vvp.pdf
	$(INSTALL_DATA) vvp.pdf $(prefix)/vvp.pdf

installdirs: $(srcdir)/mkinstalldirs
	$(srcdir)/mkinstalldirs $(bindir) $(libdir) $(libdir64) $(INSTALL_DOCDIR)


ifeq (@enable_vvp32@,yes)
install32: all32 $(libdir)/libvpi.a $(bindir)/vvp32

$(libdir)/libvpi.a: bin32/libvpi.a
	$(INSTALL_DATA) bin32/libvpi.a $(libdir)/libvpi.a

$(bindir)/vvp32: ./vvp32
	$(INSTALL_PROGRAM) ./vvp32 $(bindir)/vvp32

uninstall32:
	rm -f $(libdir)/libvpi.a
	rm -f $(bindir)/vvp32
endif

uninstall: $(UNINSTALL32)
	rm -f $(bindir)/vvp@EXEEXT@
	rm -f $(libdir64)/libvpi.a
	rm -f $(mandir)/man1/vvp.1

-include $(patsubst %.o, dep/%.d, $O)
