-
Notifications
You must be signed in to change notification settings - Fork 2
/
Makefile
100 lines (75 loc) · 2.39 KB
/
Makefile
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
CROSS_COMPILE ?= riscv64-unknown-elf-
CC:=$(CROSS_COMPILE)gcc
AS:=$(CROSS_COMPILE)as
LD:=$(CROSS_COMPILE)ld
OBJCOPY:=$(CROSS_COMPILE)objcopy
OBJDUMP:=$(CROSS_COMPILE)objdump
READELF:=$(CROSS_COMPILE)readelf
ifneq ($(MAKECMDGOALS), clean)
ifeq ($(PLAT),)
$(error Undefined platform)
endif
endif
build_dir:=build/$(PLAT)
plat_dir:=platform/$(PLAT)
asm_dir:=src/asm
TARGET := $(build_dir)/rv_iopmp_test
ifeq ($(wildcard $(plat_dir)),)
$(error unsupported platform $(PLAT))
else
$(shell mkdir -p $(build_dir))
endif
prev_log_file:=$(build_dir)/prev_log.mk
-include $(prev_log_file)
LOG_LEVEL := LOG_INFO
GENERIC_FLAGS += -D LOG_LEVEL=$(LOG_LEVEL)
$(file > $(prev_log_file), PREV_LOG_LEVEL:=$(LOG_LEVEL))
ifneq ($(PREV_LOG_LEVEL), $(LOG_LEVEL))
pre_targets += clean_objs
endif
# Include all architecture-related C source files
c_srcs = $(wildcard src/*.c)
# Include all platform-related C source files
c_srcs += $(wildcard $(plat_dir)/*.c)
# Include all architecture-related assembly source files
asm_srcs := $(wildcard $(asm_dir)/*.S) $(wildcard $(plat_dir)/*.S)
# Include all platform-related assembly source files
asm_srcs += $(wildcard $(plat_dir)/*.S)
ld_file:=linker.ld
inc_dirs := ./src/inc $(plat_dir)/inc
inc_dirs := $(addprefix -I, $(inc_dirs))
objs:=
objs+=$(patsubst %.c, $(build_dir)/%.o, $(c_srcs) -lm)
objs+=$(patsubst %.S, $(build_dir)/%.o, $(asm_srcs))
ld_file_final:=$(build_dir)/$(ld_file)
deps:=$(patsubst %.o, %.d, $(objs)) $(ld_file_final).d
dirs:=$(sort $(dir $(objs) $(deps)))
GENERIC_FLAGS += -march=rv64imac -mabi=lp64 -g3 -mcmodel=medany -O3 $(inc_dirs)
ASFLAGS = $(GENERIC_FLAGS)
CFLAGS = $(GENERIC_FLAGS)
LDFLAGS = -ffreestanding -nostartfiles -static $(GENERIC_FLAGS)
all: $(pre_targets) $(TARGET).bin
$(TARGET).bin: $(TARGET).elf
$(OBJCOPY) -O binary $< $@
$(TARGET).elf: $(objs) $(ld_file_final)
$(CC) $(LDFLAGS) -T$(ld_file_final) $(objs) -o $@
$(OBJDUMP) -S $@ > $(TARGET).asm
$(READELF) -a -W $@ > $(@).txt
$(build_dir)/%.o: %.[c,S] $(build_dir)/%.d
$(CC) $(CFLAGS) -c $< -o $@
$(build_dir)/%.d: %.[c,S,ld]
$(CC) $(GENERIC_FLAGS) -MM -MT "$(patsubst %.d, %.o, $@) $@" -MF $@ $<
$(ld_file_final): $(ld_file)
$(CC) $(CFLAGS) -E -x assembler-with-cpp $< | grep "^[^#;]" > $@
.SECONDEXPANSION:
$(objs) $(deps): | $$(@D)/
$(dirs):
mkdir -p $@
ifneq ($(MAKECMDGOALS), clean)
-include $(deps)
endif
clean:
rm -rf $(build_dir)
clean_objs:
rm -rf $(build_dir)/**.o
.PHONY: all clean