-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
116 lines (104 loc) · 3.44 KB
/
Makefile
File metadata and controls
116 lines (104 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# DEMO Makefile RISC-V
# wykys 2019
######################################
# project variables
######################################
# target name
TARGET = app
# optimalization
OPT = -O0
# build dir
BUILD_DIR = build
# includes
INC = -Iapp/inc -Idrivers/inc
######################################
# source
######################################
# C sources
C_SOURCES = $(wildcard app/src/*.c)
C_SOURCES += $(wildcard drivers/src/*.c)
# ASM sources
ASM_SOURCES = \
startup.S
#######################################
# toolchain
#######################################
TOOLCHAIN_PATH = /opt/riscv-none-embed-gcc
#######################################
BINPATH = $(TOOLCHAIN_PATH)/bin/
PREFIX = riscv-none-embed-
CC = $(BINPATH)$(PREFIX)gcc -fdiagnostics-color=always
AS = $(BINPATH)$(PREFIX)gcc -fdiagnostics-color=always -x assembler-with-cpp
CP = $(BINPATH)$(PREFIX)objcopy
DP = $(BINPATH)$(PREFIX)objdump
LD = $(BINPATH)$(PREFIX)ld
AR = $(BINPATH)$(PREFIX)ar
SZ = $(BINPATH)$(PREFIX)size -A
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
RM = rm -rf
SIMULATOR = ../cpu-cpp-emulator/sw/build/cpu-emulator
#######################################
# build the application
#######################################
# compile gcc flags
ARCH = -march=rv32i
SPEC = -nostartfiles --specs=nosys.specs
AFLAGS = $(ARCH) $(SPEC) -Wall $(INC)
CFLAGS = $(ARCH) $(SPEC) -Wall -std=c99 $(INC) $(OPT) -fdata-sections -ffunction-sections
LDSCRIPT = linker_script.ld
LDFLAGS = $(ARCH) $(SPEC) -Wl,--gc-sections -Wl,-Map=$(BUILD_DIR)/$(TARGET).map -Wl,--cref -T$(LDSCRIPT)
# generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)"
# list of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
# add ASM to objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.S=.o)))
vpath %.S $(sort $(dir $(ASM_SOURCES) $(BUILD_DIR)))
# default action: build all
all: \
$(BUILD_DIR)/$(TARGET).elf \
$(BUILD_DIR)/$(TARGET).lss \
$(BUILD_DIR)/$(TARGET).bin \
size
# create object files from C files
$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
@$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
# create object files from ASM files
$(BUILD_DIR)/%.o: %.S Makefile | $(BUILD_DIR)
@$(AS) -c $(AFLAGS) $< -o $@
$(BUILD_DIR)/init.o: $(BUILD_DIR)/init.S Makefile | $(BUILD_DIR)
@$(AS) -c $(AFLAGS) $< -o $@
# create init asm
init: $(BUILD_DIR)/rodata.bin $(BUILD_DIR)/data.bin
@./init.py
# create aplication ELF file
$(BUILD_DIR)/tmp.elf: $(OBJECTS) Makefile
@$(CC) $(OBJECTS) $(LDFLAGS) -o $@
$(BUILD_DIR)/$(TARGET).elf: init $(OBJECTS) $(BUILD_DIR)/init.o Makefile
@$(CC) $(OBJECTS) $(BUILD_DIR)/init.o $(LDFLAGS) -o $@
# create bin program file
$(BUILD_DIR)/$(TARGET).bin: $(BUILD_DIR)/$(TARGET).elf | $(BUILD_DIR)
@$(BIN) --only-section .text $< $@
# create bin data file for RAM initialization
$(BUILD_DIR)/rodata.bin: $(BUILD_DIR)/tmp.elf | $(BUILD_DIR)
@$(BIN) --only-section .rodata $< $@
$(BUILD_DIR)/data.bin: $(BUILD_DIR)/tmp.elf | $(BUILD_DIR)
@$(BIN) --only-section .data $< $@
# disassembly EFL
$(BUILD_DIR)/$(TARGET).lss: $(BUILD_DIR)/$(TARGET).elf
@$(DP) -h -S $< > $@
# create build directory
$(BUILD_DIR):
@mkdir $@
# prints memory usage tables
size:
@$(SZ) $(BUILD_DIR)/$(TARGET).elf
# clean up
clean:
@$(RM) $(BUILD_DIR)
show_default_linker_script:
@$(LD) --verbose -arch=elf32lriscv
simulator: $(BUILD_DIR)/$(TARGET).bin
@$(SIMULATOR) $(BUILD_DIR)/$(TARGET).bin