#
#    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., 51 Franklin Street, Fifth Floor,
#    Boston, MA 02110-1301, USA.
#
SHELL = /bin/sh

suffix = @install_suffix@

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

VPATH = $(srcdir)

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

# For a cross compile these defines will need to be set accordingly.
HOSTCC = @CC@
HOSTCFLAGS = @WARNING_FLAGS@ @WARNING_FLAGS_CC@ @CFLAGS@

BUILDCC = @CC_FOR_BUILD@
BUILDEXT = @BUILD_EXEEXT@
CXX = @CXX@
DLLTOOL = @DLLTOOL@
INSTALL = @INSTALL@
INSTALL_SCRIPT = @INSTALL_SCRIPT@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
LEX = @LEX@
YACC = @YACC@
MAN = @MAN@
PS2PDF = @PS2PDF@

ifeq (@srcdir@,.)
INCLUDE_PATH = -I. -I..
else
INCLUDE_PATH = -I. -I.. -I$(srcdir) -I$(srcdir)/..
endif

CPPFLAGS = $(INCLUDE_PATH) @CPPFLAGS@ @DEFS@ -DICARUS_VPI_CONST=const
CFLAGS = @WARNING_FLAGS@ @WARNING_FLAGS_CC@ @CFLAGS@
CXXFLAGS = @WARNING_FLAGS@ @WARNING_FLAGS_CXX@ @CXXFLAGS@
LDFLAGS = @rdynamic@ @LDFLAGS@
LIBS = @LIBS@ @EXTRALIBS@

dllib=@DLLIB@

MDIR1 = -DMODULE_DIR1='"$(libdir)/ivl$(suffix)"'

VPI = vpi_modules.o vpi_bit.o vpi_callback.o vpi_cobject.o vpi_const.o vpi_darray.o \
      vpi_event.o vpi_iter.o vpi_mcd.o \
      vpi_priv.o vpi_scope.o vpi_real.o vpi_signal.o vpi_string.o vpi_tasks.o vpi_time.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 array_common.o array.o bufif.o compile.o \
    concat.o dff.o class_type.o enum_type.o extend.o file_line.o latch.o npmos.o part.o \
    permaheap.o reduce.o resolv.o \
    sfunc.o stop.o \
    substitute.o \
    symbols.o ufunc.o codes.o vthread.o schedule.o \
    statistics.o tables.o udp.o vvp_island.o vvp_net.o vvp_net_sig.o \
    vvp_object.o vvp_cobject.o vvp_darray.o event.o logic.o delay.o \
    words.o island_tran.o $(VPI)

all: dep vvp@EXEEXT@ vvp.man

check: all
ifeq (@WIN32@,yes)
ifeq (@install_suffix@,)
	./vvp -M../vpi $(srcdir)/examples/hello.vvp | grep 'Hello, World.'
else
	# On Windows if we have a suffix we must run the vvp test with
	# a suffix since it was built/linked that way.
	ln vvp.exe vvp$(suffix).exe
	./vvp$(suffix) -M../vpi $(srcdir)/examples/hello.vvp | grep 'Hello, World.'
	rm -f vvp$(suffix).exe
endif
else
	./vvp -M../vpi $(srcdir)/examples/hello.vvp | grep 'Hello, World.'
endif

clean:
	rm -f *.o *~ parse.cc parse.h lexor.cc tables.cc
	rm -rf dep vvp@EXEEXT@ parse.output vvp.man vvp.ps vvp.pdf vvp.exp

distclean: clean
	rm -f Makefile config.log
	rm -f stamp-config-h config.h

cppcheck: $(O:.o=.cc) draw_tt.c
	cppcheck --enable=all --std=c99 --std=c++03 -f \
	         --suppressions-list=$(srcdir)/cppcheck.sup \
	         -UMODULE_DIR1 -UMODULE_DIR2 -UYY_USER_INIT \
	         -UYYPARSE_PARAM -UYYPRINT -Ushort -Usize_t -Uyyoverflow \
	         -UYYTYPE_INT8 -UYYTYPE_INT16 -UYYTYPE_UINT8 -UYYTYPE_UINT16 \
	         -UYYSTYPE -UINFINITY -U__SIZE_TYPE__ -Ufree \
	         --relative-paths=$(srcdir) $(INCLUDE_PATH) $^

Makefile: $(srcdir)/Makefile.in
	cd ..; ./config.status --file=vvp/$@

dep:
	mkdir dep

ifeq (@WIN32@,yes)
# To support cocotb, we export the VPI functions directly. This allows
# cocotb to build VPI modules without using our vpi_user.h and libvpi.a.
# This requires making the vvp.exe in two steps. The first step makes
# a vvp.exe that dlltool can use to make an export library, and the
# second step makes a vvp.exe that really exports those things.
vvp@EXEEXT@: $O $(srcdir)/vvp.def
	$(CXX) -o vvp$(suffix)@EXEEXT@ $(LDFLAGS) $O $(dllib) $(LIBS)
	$(DLLTOOL) --dllname vvp$(suffix)@EXEEXT@ --def $(srcdir)/vvp.def \
		--output-exp vvp.exp
	rm -f vvp$(suffix)@EXEEXT@
	$(CXX) $(LDFLAGS) -o vvp@EXEEXT@ vvp.exp $(LDFLAGS) $O $(dllib) $(LIBS)
else
vvp@EXEEXT@: $O
	$(CXX) $(LDFLAGS) -o vvp@EXEEXT@ $O $(LIBS) $(dllib)
endif

%.o: %.cc config.h
	$(CXX) $(CPPFLAGS) -DIVL_SUFFIX='"$(suffix)"' $(MDIR1) $(MDIR2) $(CXXFLAGS) @DEPENDENCY_FLAG@ -c $< -o $*.o
	mv $*.d dep/$*.d

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

tables.cc: $(srcdir)/draw_tt.c
	$(BUILDCC) $(CFLAGS) -o draw_tt$(BUILDEXT) $(srcdir)/draw_tt.c
	./draw_tt$(BUILDEXT) > tables.cc
	rm draw_tt$(BUILDEXT)

lexor.o: lexor.cc parse.h

parse.o: parse.cc

tables.o: tables.cc

# Use pattern rules to avoid parallel build issues (see pr3462585)
parse%cc parse%h: $(srcdir)/parse%y
	$(YACC) --verbose -t --defines=parse.h -o parse.cc $<

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

vvp.man: $(srcdir)/vvp.man.in ../version.exe
	../version.exe `head -1 $(srcdir)/vvp.man.in`'\n' > $@
	tail -n +2 $(srcdir)/vvp.man.in >> $@

vvp.ps: vvp.man
	$(MAN) -t ./vvp.man > vvp.ps

vvp.pdf: vvp.ps
	$(PS2PDF) vvp.ps vvp.pdf

ifeq (@MINGW32@,yes)
ifeq ($(MAN),none)
INSTALL_DOC = installman
else
ifeq ($(PS2PDF),none)
INSTALL_DOC = installman
else
INSTALL_DOC = installpdf installman
all: vvp.pdf
endif
endif
INSTALL_DOCDIR = $(mandir)/man1
else
INSTALL_DOC = installman
INSTALL_DOCDIR = $(mandir)/man1
endif

stamp-config-h: $(srcdir)/config.h.in ../config.status
	@rm -f $@
	cd ..; ./config.status --header=vvp/config.h
config.h: stamp-config-h

install: all installdirs installfiles

F = ./vvp@EXEEXT@ $(INSTALL_DOC)

installman: vvp.man installdirs
	$(INSTALL_DATA) vvp.man "$(DESTDIR)$(mandir)/man1/vvp$(suffix).1"

installpdf: vvp.pdf installdirs
	$(INSTALL_DATA) vvp.pdf "$(DESTDIR)$(prefix)/vvp$(suffix).pdf"

installfiles: $(F) | installdirs
	$(INSTALL_PROGRAM) ./vvp@EXEEXT@ "$(DESTDIR)$(bindir)/vvp$(suffix)@EXEEXT@"

installdirs: $(srcdir)/../mkinstalldirs
	$(srcdir)/../mkinstalldirs "$(DESTDIR)$(bindir)" "$(DESTDIR)$(libdir)" "$(DESTDIR)$(INSTALL_DOCDIR)"


uninstall: $(UNINSTALL32)
	rm -f "$(DESTDIR)$(bindir)/vvp$(suffix)@EXEEXT@"
	rm -f "$(DESTDIR)$(mandir)/man1/vvp$(suffix).1" "$(DESTDIR)$(prefix)/vvp$(suffix).pdf"

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