EECS151 / riscv-cpu / software / Makefile.gcc.in
Makefile.gcc.in
Raw
# RISC-V GCC Compilers
# riscv64-linux-gnu-gcc - Compiler from system packages (for local dev)
# riscv64-unknown-elf-gcc - From SiFive (on c125m/eda machines)

RISCV_PACKAGE := riscv64-linux-gnu
RISCV_DEFAULT := riscv64-unknown-elf
RISCV_DEFAULT_PRESENT := $(shell command -v $(RISCV_DEFAULT)-gcc 2> /dev/null)

ifdef RISCV_DEFAULT_PRESENT
    RISCV := $(RISCV_DEFAULT)
else
    RISCV := $(RISCV_PACKAGE)
endif

LIB_PATH := ../151_library
LIB_SOURCES := $(wildcard ../151_library/*.c)
CSRCS := $(wildcard *.c)
SSRCS := $(wildcard *.s)
LDSRC := $(TARGET).ld

GCC_OPTS += -mabi=ilp32 -march=rv32i -static -mcmodel=medany -nostdlib -nostartfiles -T $(LDSRC)

default: $(TARGET).elf

SOURCES = $(CSRCS) $(SSRCS)
ifeq ($(INCLUDE_LIB), true)
	SOURCES += $(LIB_SOURCES)
endif

# objdump is called before strip because it inlines functions and makes the assembly harder to read
$(TARGET).elf: $(SOURCES)
	$(RISCV)-gcc $(GCC_OPTS) -I$(LIB_PATH) $^ -o $@
	$(RISCV)-objdump -D -Mnumeric $@ > $(basename $@).dump
	$(RISCV)-strip -R .comment -R .note.gnu.build-id $@
	$(RISCV)-objcopy $(basename $@).elf -O binary $(basename $@).bin
	$(RISCV)-bin2hex -w 32 $(basename $@).bin $(basename $@).hex
	sed -i '1i @0' $(basename $@).hex

clean:
	rm -f *.elf *.dump *.hex *.bin

.PHONY: target