#
#    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@

VPATH = $(srcdir)

bindir = @bindir@
libdir = @libdir@
includedir = $(prefix)/include

vpidir = $(libdir)/ivl$(suffix)

CC = @CC@
CXX = @CXX@
AR = @AR@
RANLIB = @RANLIB@
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
LEX = @LEX@
YACC = @YACC@

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

CPPFLAGS = $(INCLUDE_PATH) @file64_support@ @CPPFLAGS@ @DEFS@ -DICARUS_VPI_CONST=const @PICFLAG@
CFLAGS = @WARNING_FLAGS@ @WARNING_FLAGS_CC@ @CFLAGS@
CXXFLAGS = @WARNING_FLAGS@ @WARNING_FLAGS_CXX@ @CXXFLAGS@
LDFLAGS = @LDFLAGS@

# Object files for system.vpi
O = sys_table.o sys_convert.o sys_countdrivers.o sys_darray.o sys_deposit.o \
    sys_display.o \
    sys_fileio.o sys_finish.o sys_icarus.o sys_plusargs.o sys_queue.o \
    sys_random.o sys_random_mti.o sys_readmem.o sys_readmem_lex.o sys_scanf.o \
    sys_sdf.o sys_time.o sys_vcd.o sys_vcdoff.o vcd_priv.o mt19937int.o \
    sys_priv.o sdf_parse.o sdf_lexor.o stringheap.o vams_simparam.o \
    table_mod.o table_mod_parse.o table_mod_lexor.o
OPP = vcd_priv2.o

ifeq (@HAVE_LIBZ@,yes)
ifeq (@HAVE_LIBBZ2@,yes)
O += sys_lxt.o lxt_write.o
endif
O += sys_lxt2.o lxt2_write.o
O += sys_fst.o fstapi.o fastlz.o lz4.o
endif

# Object files for v2005_math.vpi
V2005 = sys_clog2.o v2005_math.o

# Object files for va_math.vpi
VA_MATH = va_math.o

V2009 = v2009_table.o v2009_array.o v2009_bitvec.o v2009_enum.o v2009_string.o \
        sys_priv.o

VHDL_SYS = vhdl_table.o sys_priv.o

VHDL_TEXTIO = vhdl_textio.o sys_priv.o

VPI_DEBUG = vpi_debug.o

all: dep libvpi.a system.vpi va_math.vpi v2005_math.vpi v2009.vpi vhdl_sys.vpi vhdl_textio.vpi vpi_debug.vpi $(ALL32)

check: all

clean:
	rm -rf *.o sys_readmem_lex.c dep libvpi.a system.vpi
	rm -f sdf_lexor.c sdf_parse.c sdf_parse.output sdf_parse.h
	rm -f table_mod_parse.c table_mod_parse.h table_mod_parse.output
	rm -f table_mod_lexor.c
	rm -f va_math.vpi v2005_math.vpi v2009.vpi vhdl_sys.vpi vhdl_textio.vpi vpi_debug.vpi

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

# The -U flag is used to skip checking paths that depend on that define having
# an explicit value (i.e. the define is expected to be real code).
cppcheck: $(O:.o=.c) $(OPP:.o=.cc) $(M:.o=.c) $(V:.o=.c)
	cppcheck --enable=all --std=c99 --std=c++03 -f \
	         --suppressions-list=$(srcdir)/cppcheck.sup \
	         -UYY_USER_INIT \
	         -UYYPARSE_PARAM -UYYPRINT -Ushort -Uyyoverflow \
	         -UYYTYPE_INT8 -UYYTYPE_INT16 -UYYTYPE_UINT8 -UYYTYPE_UINT16 \
	         -UYYLTYPE -UYYSTYPE -U__SIZE_TYPE__ -Umalloc -Usize_t -Ufree  \
	         --relative-paths=$(srcdir) $(INCLUDE_PATH) $^

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

dep:
	mkdir dep

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

%.o: %.cc vpi_config.h
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) @DEPENDENCY_FLAG@ -c $< -o $*.o
	mv $*.d dep

libvpi.a: libvpi.c ../vpi_user.h
	$(CC) $(CPPFLAGS) $(CFLAGS) -c $<
	rm -f libvpi.a
	$(AR) cqv libvpi.a libvpi.o
	$(RANLIB) libvpi.a

LIBS = @LIBS@
SYSTEM_VPI_LDFLAGS = $(LIBS)
VA_MATH_LDFLAGS =
ifeq (@MINGW32@,yes)
    SYSTEM_VPI_LDFLAGS += @EXTRALIBS@
    VA_MATH_LDFLAGS += @EXTRALIBS@
endif

system.vpi: $O $(OPP) libvpi.a
	$(CXX) @shared@ -o $@ $O $(OPP) -L. $(LDFLAGS) -lvpi $(SYSTEM_VPI_LDFLAGS)

sys_readmem_lex.c: $(srcdir)/sys_readmem_lex.lex
	$(LEX) -t $< > $@

sdf_lexor.o: sdf_lexor.c sdf_parse.h

sdf_lexor.c: $(srcdir)/sdf_lexor.lex
	$(LEX) -t $< > $@

# Use pattern rules to avoid parallel build issues (see pr3462585)
sdf_parse%c sdf_parse%h: $(srcdir)/sdf_parse%y
	$(YACC) --verbose -t -p sdf -d -o sdf_parse.c $<

table_mod_lexor.o: table_mod_lexor.c table_mod_parse.h

table_mod_lexor.c: $(srcdir)/table_mod_lexor.lex
	$(LEX) -t $< > $@

# Use pattern rules to avoid parallel build issues (see pr3462585)
table_mod_parse%c table_mod_parse%h: $(srcdir)/table_mod_parse%y
	$(YACC) --verbose -t -p tblmod -d -o table_mod_parse.c $<

v2005_math.vpi: $(V2005) libvpi.a
	$(CC) @shared@ -o $@ $(V2005) -L. $(LDFLAGS) -lvpi $(VA_MATH_VPI_LDFLAGS)

v2009.vpi: $(V2009) libvpi.a
	$(CC) @shared@ -o $@ $(V2009) -L. $(LDFLAGS) -lvpi $(SYSTEM_VPI_LDFLAGS)

va_math.vpi: $(VA_MATH) libvpi.a
	$(CC) @shared@ -o $@ $(VA_MATH) -L. $(LDFLAGS) -lvpi $(VA_MATH_VPI_LDFLAGS)

vhdl_sys.vpi: $(VHDL_SYS) libvpi.a
	$(CC) @shared@ -o $@ $(VHDL_SYS) -L. $(LDFLAGS) -lvpi $(SYSTEM_VPI_LDFLAGS)

vhdl_textio.vpi: $(VHDL_TEXTIO) libvpi.a
	$(CC) @shared@ -o $@ $(VHDL_TEXTIO) -L. $(LDFLAGS) -lvpi $(SYSTEM_VPI_LDFLAGS)

vpi_debug.vpi: $(VPI_DEBUG) libvpi.a
	$(CC) @shared@ -o $@ $(VPI_DEBUG) -L. $(LDFLAGS) -lvpi $(SYSTEM_VPI_LDFLAGS)

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

install: all installdirs installfiles

F = ./libvpi.a \
	./system.vpi \
	./va_math.vpi \
	./v2005_math.vpi \
	./v2009.vpi \
	./vhdl_sys.vpi \
	./vhdl_textio.vpi \
	./vpi_debug.vpi

installfiles: $(F) | installdirs
	$(INSTALL_DATA) ./libvpi.a "$(DESTDIR)$(libdir)/libvpi$(suffix).a"
	$(INSTALL_PROGRAM) ./system.vpi "$(DESTDIR)$(vpidir)/system.vpi"
	$(INSTALL_PROGRAM) ./va_math.vpi "$(DESTDIR)$(vpidir)/va_math.vpi"
	$(INSTALL_PROGRAM) ./v2005_math.vpi "$(DESTDIR)$(vpidir)/v2005_math.vpi"
	$(INSTALL_PROGRAM) ./v2009.vpi "$(DESTDIR)$(vpidir)/v2009.vpi"
	$(INSTALL_PROGRAM) ./vhdl_sys.vpi "$(DESTDIR)$(vpidir)/vhdl_sys.vpi"
	$(INSTALL_PROGRAM) ./vhdl_textio.vpi "$(DESTDIR)$(vpidir)/vhdl_textio.vpi"
	$(INSTALL_PROGRAM) ./vpi_debug.vpi "$(DESTDIR)$(vpidir)/vpi_debug.vpi"

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

uninstall:
	rm -f "$(DESTDIR)$(libdir)/libvpi$(suffix).a"
	rm -f "$(DESTDIR)$(vpidir)/system.vpi"
	rm -f "$(DESTDIR)$(vpidir)/va_math.vpi"
	rm -f "$(DESTDIR)$(vpidir)/v2005_math.vpi"
	rm -f "$(DESTDIR)$(vpidir)/v2009.vpi"
	rm -f "$(DESTDIR)$(vpidir)/vhdl_sys.vpi"
	rm -f "$(DESTDIR)$(vpidir)/vhdl_textio.vpi"
	rm -f "$(DESTDIR)$(vpidir)/vpi_debug.vpi"

-include $(patsubst %.o, dep/%.d, $O)
-include $(patsubst %.o, dep/%.d, $(OPP))
-include $(patsubst %.o, dep/%.d, $(V2005))
-include $(patsubst %.o, dep/%.d, $(VA_MATH))
-include $(patsubst %.o, dep/%.d, $(V2009))
-include $(patsubst %.o, dep/%.d, $(VHDL_SYS))
-include $(patsubst %.o, dep/%.d, $(VHDL_TEXTIO))
-include $(patsubst %.o, dep/%.d, $(VPI_DEBUG))
