140 lines
3.0 KiB
Makefile
140 lines
3.0 KiB
Makefile
PROJ_NAME = hello_world
|
|
DEBUG = yes
|
|
BENCH = no
|
|
MULDIV = no
|
|
|
|
SRCS = $(wildcard src/*.c) \
|
|
$(wildcard src/*.cpp) \
|
|
$(wildcard src/*.S) \
|
|
src/stub_stdlib.c
|
|
|
|
OBJDIR = build
|
|
|
|
INC =
|
|
LIBS =
|
|
LIBSINC = -L$(OBJDIR)
|
|
LDSCRIPT = ./src/linker.ld
|
|
|
|
# Set it to yes if you are using the sifive precompiled GCC pack
|
|
GCC_PACK ?= yes
|
|
|
|
ifeq ($(GCC_PACK),yes)
|
|
RISCV_NAME ?= riscv32-unknown-elf
|
|
RISCV_PATH ?= /home/user/riscv/opt/rv32im/
|
|
endif
|
|
|
|
MABI = ilp32
|
|
MARCH := rv32i_zicsr
|
|
ifeq ($(MULDIV),yes)
|
|
MARCH := $(MARCH)m
|
|
endif
|
|
|
|
ifeq ($(DEBUG),yes)
|
|
# CFLAGS += -g3 -O0
|
|
CFLAGS += -g3 -gdwarf-3 -O0 -DDEBUG
|
|
CXXFLAGS += -g3 -gdwarf-3 -O0 -DDEBUG
|
|
LDFLAGS += -g3 -gdwarf-3
|
|
endif
|
|
|
|
ifeq ($(DEBUG),no)
|
|
CFLAGS += -g -Os
|
|
endif
|
|
|
|
ifeq ($(BENCH),yes)
|
|
CFLAGS += -fno-inline
|
|
endif
|
|
|
|
ifeq ($(SIFIVE_GCC_PACK),yes)
|
|
RISCV_CLIB=$(RISCV_PATH)/$(RISCV_NAME)/lib/$(MARCH)/$(MABI)/
|
|
else
|
|
RISCV_CLIB=$(RISCV_PATH)/$(RISCV_NAME)/lib/
|
|
endif
|
|
|
|
RISCV_OBJCOPY = $(RISCV_PATH)/bin/$(RISCV_NAME)-objcopy
|
|
RISCV_OBJDUMP = $(RISCV_PATH)/bin/$(RISCV_NAME)-objdump
|
|
RISCV_CC = $(RISCV_PATH)/bin/$(RISCV_NAME)-gcc
|
|
CXX = $(RISCV_PATH)/bin/$(RISCV_NAME)-g++
|
|
|
|
CFLAGS += -march=$(MARCH) \
|
|
-mabi=$(MABI) \
|
|
-DNDEBUG \
|
|
-fno-pic \
|
|
-ffreestanding \
|
|
-MD \
|
|
-fstrict-volatile-bitfields \
|
|
-fno-strict-aliasing
|
|
|
|
CXXFLAGS += -march=$(MARCH) \
|
|
-mabi=$(MABI) \
|
|
-DNDEBUG \
|
|
-fno-pic \
|
|
-ffreestanding \
|
|
-MD \
|
|
-fstrict-volatile-bitfields \
|
|
-fno-strict-aliasing \
|
|
-std=c++11
|
|
|
|
LDFLAGS += -march=$(MARCH) \
|
|
-mabi=$(MABI) \
|
|
-lc \
|
|
-lgcc \
|
|
-nostdlib \
|
|
-nostartfiles \
|
|
-mcmodel=medany \
|
|
-ffreestanding \
|
|
-Wl,-Bstatic,-T,$(LDSCRIPT),-Map,$(OBJDIR)/$(PROJ_NAME).map
|
|
|
|
OBJS := $(SRCS)
|
|
OBJS := $(OBJS:.c=.o)
|
|
OBJS := $(OBJS:.cpp=.o)
|
|
OBJS := $(OBJS:.S=.o)
|
|
OBJS := $(addprefix $(OBJDIR)/,$(OBJS))
|
|
|
|
all: $(OBJDIR)/$(PROJ_NAME).elf $(OBJDIR)/$(PROJ_NAME).hex $(OBJDIR)/$(PROJ_NAME).asm $(OBJDIR)/$(PROJ_NAME).v
|
|
|
|
$(OBJDIR)/%.elf: $(OBJS) | $(OBJDIR)
|
|
$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) $(LIBSINC) $(LIBS)
|
|
|
|
%.hex: %.elf
|
|
$(RISCV_OBJCOPY) -O ihex $^ $@
|
|
|
|
%.bin: %.elf
|
|
$(RISCV_OBJCOPY) -O binary $^ $@
|
|
|
|
%.v: %.elf
|
|
$(RISCV_OBJCOPY) -O verilog $^ $@
|
|
|
|
%.asm: %.elf
|
|
$(RISCV_OBJDUMP) -S -d $^ > $@
|
|
|
|
$(OBJDIR)/%.o: %.c
|
|
mkdir -p $(dir $@)
|
|
$(RISCV_CC) -c $(CFLAGS) $(INC) -o $@ $^
|
|
|
|
$(OBJDIR)/%.o: %.cpp
|
|
mkdir -p $(dir $@)
|
|
$(CXX) -c $(CXXFLAGS) $(INC) -o $@ $^
|
|
|
|
$(OBJDIR)/%.o: %.S
|
|
mkdir -p $(dir $@)
|
|
$(RISCV_CC) -c $(CFLAGS) -o $@ $^ -D__ASSEMBLY__=1
|
|
|
|
$(OBJDIR):
|
|
mkdir -p $@
|
|
|
|
.PHONY: clean
|
|
clean:
|
|
rm -rf $(OBJDIR)/src
|
|
rm -f $(OBJDIR)/$(PROJ_NAME).elf
|
|
rm -f $(OBJDIR)/$(PROJ_NAME).hex
|
|
rm -f $(OBJDIR)/$(PROJ_NAME).map
|
|
rm -f $(OBJDIR)/$(PROJ_NAME).v
|
|
rm -f $(OBJDIR)/$(PROJ_NAME).asm
|
|
find $(OBJDIR) -type f -name '*.o' -print0 | xargs -0 -r rm
|
|
find $(OBJDIR) -type f -name '*.d' -print0 | xargs -0 -r rm
|
|
|
|
clean-all: clean
|
|
|
|
.SECONDARY: $(OBJS)
|
|
|