# Builds the RISC-V emulator.

PREFIX  ?= $(HOME)/bin
CC      := clang
CFLAGS  := -fvisibility=hidden -std=c99 -O3 -g \
           -Wall -Wextra -Wpedantic \
           -Wformat=2 -Wformat-security \
           -Wnull-dereference \
           -Wno-format-nonliteral \
           -Wcast-align \
           -Wunused -Wuninitialized \
           -Wmissing-field-initializers \
           -fno-common -fstack-protector-all \
           -mcmodel=medium \
           -march=native -flto
LDFLAGS := -fuse-ld=lld -Wl,-z,stack-size=33554432
EMULATOR_SRCS  := emulator.c jit.c riscv/debug.c io.c riscv.c

default: bin/emulator

bin/emulator: $(EMULATOR_SRCS) jit.h color.h types.h io.h riscv.h riscv/debug.h
	@echo "cc    emulator => $@"
	@mkdir -p bin
	@$(CC) $(CFLAGS) $(LDFLAGS) $(EMULATOR_SRCS) -o $@

install: bin/emulator
	@echo "copy  bin/emulator => $(PREFIX)/emulator"
	@mkdir -p $(PREFIX)
	@cp bin/emulator $(PREFIX)/emulator

fmt:
	git ls-files "*.c" "*.h" | xargs clang-format -i

clean:
	@rm -f bin/emulator

.PHONY: default clean install
.SUFFIXES:
.DELETE_ON_ERROR:
.SILENT:
