TARGET_SIM   ?= <sim runtime>
TARGET_FLAGS ?= $(RISCV_TARGET_FLAGS)
ifeq ($(shell command -v $(TARGET_SIM) 2> /dev/null),)
    $(error Target simulator executable '$(TARGET_SIM)` not found)
endif

RISCV_PREFIX   ?= riscv64-unknown-elf-
RISCV_GCC      ?= $(RISCV_PREFIX)gcc
RISCV_OBJDUMP  ?= $(RISCV_PREFIX)objdump
RISCV_OBJCOPY  ?= $(RISCV_PREFIX)objcopy
RISCV_READELF  ?= $(RISCV_PREFIX)readelf
RISCV_GCC_OPTS ?= -static -mcmodel=medany -fvisibility=hidden -nostdlib -nostartfiles -DXLEN=$(XLEN) $(RVTEST_DEFINES)

COMPILE_CMD=\
	$$(RISCV_GCC) $(1) $$(RISCV_GCC_OPTS) \
		-I$(ROOTDIR)/riscv-test-suite/env/ \
		-I$(TARGETDIR)/$(RISCV_TARGET)/ \
		-T$(TARGETDIR)/$(RISCV_TARGET)/env/linkmono.ld $$< \
		-o $$@; \
	$$(RISCV_OBJCOPY) -O binary $$@ $$@.bin

OBJ_CMD = $$(RISCV_OBJDUMP) $$@ -D > $$@.objdump; \
		  $$(RISCV_OBJDUMP) $$@ --source-comment="// " > $$@.debug; \
		  $$(RISCV_READELF) -a $$@ > $$@.readelf


COMPILE_TARGET=\
		$(COMPILE_CMD); \
        if [ $$$$? -ne 0 ] ; \
            then \
            echo "\e[31m$$(RISCV_GCC) failed for target $$(@) \e[39m" ; \
            exit 1 ; \
        fi ; \
		$(OBJ_CMD); \
        if [ $$$$? -ne 0 ] ; \
            then \
            echo "\e[31m $$(RISCV_OBJDUMP) failed for target $$(@) \e[39m" ; \
            exit 1 ; \
        fi ;

RUN_CMD=\
	$(TARGET_SIM) <sim cmdline args>

RUN_TARGET=\
	$(RUN_CMD)


