# Copyright cocotb contributors
# Copyright (c) 2013 Potential Ventures Ltd
# Copyright (c) 2013 SolarFlare Communications Inc
# Licensed under the Revised BSD License, see LICENSE for details.
# SPDX-License-Identifier: BSD-3-Clause

# Common Makefile for Cadence Incisive

CMD_BIN := irun

ifdef IUS_BIN_DIR
    CMD := $(shell :; command -v $(IUS_BIN_DIR)/$(CMD_BIN) 2>/dev/null)
else
    # auto-detect bin dir from system path
    CMD := $(shell :; command -v $(CMD_BIN) 2>/dev/null)
    IUS_BIN_DIR := $(shell dirname $(CMD))
endif

ifdef VERILOG_INCLUDE_DIRS
    COMPILE_ARGS += $(addprefix +incdir+, $(VERILOG_INCLUDE_DIRS))
endif

EXTRA_ARGS += $(COMPILE_ARGS)
EXTRA_ARGS += $(SIM_ARGS)
EXTRA_ARGS += -licqueue

ifeq ($(PYTHON_ARCH),64bit)
    EXTRA_ARGS += -64
endif

EXTRA_ARGS += -nclibdirpath $(SIM_BUILD)
EXTRA_ARGS += -plinowarn

ifeq ($(GUI),1)
    EXTRA_ARGS += -gui
else
    EXTRA_ARGS +=
endif

# IUS errors out if multiple timescales are specified on the command line.
ifneq (,$(findstring timescale,$(EXTRA_ARGS)))
    $(error Please use COCOTB_HDL_TIMEUNIT and COCOTB_HDL_TIMEPRECISION to specify timescale.)
endif

# Loading the VHPI library causes an error, so we always load the VPI library and supply
# GPI_EXTRA=$(shell $(PYTHON_BIN) -m cocotb_tools.config --lib-name-path vhpi ius) if needed.

# Xcelium will use default vlog_startup_routines symbol only if vpi library name is libvpi.so
GPI_ARGS = -loadvpi $(shell $(PYTHON_BIN) -m cocotb_tools.config --lib-name-path vpi ius):vlog_startup_routines_bootstrap

ifeq ($(TOPLEVEL_LANG),verilog)
    EXTRA_ARGS += -v93
    HDL_SOURCES = $(VERILOG_SOURCES)
    ROOT_LEVEL = $(COCOTB_TOPLEVEL)
ifneq ($(VHDL_SOURCES),)
    HDL_SOURCES += $(VHDL_SOURCES)
    GPI_EXTRA = $(shell $(PYTHON_BIN) -m cocotb_tools.config --lib-name-path vhpi ius):cocotbvhpi_entry_point
endif
else ifeq ($(TOPLEVEL_LANG),vhdl)
    GPI_EXTRA = $(shell $(PYTHON_BIN) -m cocotb_tools.config --lib-name-path vhpi ius):cocotbvhpi_entry_point
    EXTRA_ARGS += -v93
    EXTRA_ARGS += -top $(COCOTB_TOPLEVEL)
ifdef RTL_LIBRARY
    $(warning Using RTL_LIBRARY is deprecated, please use TOPLEVEL_LIBRARY instead.)
    TOPLEVEL_LIBRARY ?= $(RTL_LIBRARY)
else
    TOPLEVEL_LIBRARY ?= work
endif
    MAKE_LIB = -makelib $(TOPLEVEL_LIBRARY)
    HDL_SOURCES = $(VHDL_SOURCES)
ifneq ($(VERILOG_SOURCES),)
    HDL_SOURCES += $(VERILOG_SOURCES)
endif
else
   $(error A valid value (verilog or vhdl) was not provided for TOPLEVEL_LANG=$(TOPLEVEL_LANG))
endif

# Builds a list of arguments to support VHDL libraries specified in VHDL_SOURCES_*:
LIBS := $(foreach LIB, $(VHDL_LIB_ORDER),-makelib $(LIB) $(VHDL_SOURCES_$(LIB)) -endlib)

$(COCOTB_RESULTS_FILE): $(HDL_SOURCES) $(CUSTOM_COMPILE_DEPS) $(CUSTOM_SIM_DEPS) | $(SIM_BUILD)
	$(RM) $(COCOTB_RESULTS_FILE)

	# Make sure all libs in SOURCES_VHDL_* are mentioned in VHDL_LIB_ORDER and vice versa
	$(foreach LIB, $(VHDL_LIB_ORDER), $(check_vhdl_sources))
	$(foreach SOURCES_VAR, $(filter VHDL_SOURCES_%, $(.VARIABLES)), $(check_lib_order))

	set -o pipefail; \
	COCOTB_TEST_MODULES=$(call deprecate,MODULE,COCOTB_TEST_MODULES) COCOTB_TESTCASE=$(call deprecate,TESTCASE,COCOTB_TESTCASE) COCOTB_TEST_FILTER=$(COCOTB_TEST_FILTER) COCOTB_TOPLEVEL=$(call deprecate,TOPLEVEL,COCOTB_TOPLEVEL) GPI_EXTRA=$(GPI_EXTRA) TOPLEVEL_LANG=$(TOPLEVEL_LANG) \
	$(SIM_CMD_PREFIX) $(CMD) -timescale $(COCOTB_HDL_TIMEUNIT)/$(COCOTB_HDL_TIMEPRECISION) \
	$(EXTRA_ARGS) $(GPI_ARGS) +access+rwc $(LIBS) $(MAKE_LIB) $(HDL_SOURCES) $(call deprecate,PLUSARGS,COCOTB_PLUSARGS) $(SIM_CMD_SUFFIX)

	$(call check_results)
