-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakefile
167 lines (131 loc) · 3.89 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
##
# By default everything is silent. If you want to change this behavior simply
# assign V=1 when calling make.
V =
ifeq ($(strip $(V)),)
E = @echo
Q = @
else
E = @\#
Q =
endif
##
# Compile options. You can use CROSS_COMPILE just like on the Linux Kernel. You
# can also determine the ISA to be used, which defaults to my VisionFive2 board.
# I did not go too much into the rabbit hole of platform-specific flags. Hence
# no `-mcpu`, no `-mtune`, no funny business.
CC = $(CROSS_COMPILE)gcc$(CC_SUFFIX)
LD = $(CROSS_COMPILE)ld
HOSTCC = gcc$(CC_SUFFIX)
QEMU ?= qemu-system-riscv64
OBJCPY = $(CROSS_COMPILE)objcopy
ISA ?= rv64imafdc_zicntr_zicsr_zifencei_zihpm_zca_zcd_zba_zbb
ASFLAGS = -march=$(ISA) -mabi=lp64d -mcmodel=medany -fno-PIE
CCFLAGS = $(ASFLAGS) -Iinclude/ -D__KERNEL__ -std=gnu17 -nostdinc -nostdlib
WARNINGS = -Werror -Wpedantic -Wall -Wextra -Wcast-align -Wcast-qual -Winit-self \
-Wmissing-include-dirs -Wredundant-decls -Wshadow -Wsign-conversion \
-Wswitch-default -Wundef -Wunreachable-code
CCFLAGS += $(WARNINGS)
LDFLAGS = -Iinclude/ -static -melf64lriscv -z noexecstack
USRFLAGS = -static -melf64lriscv
##
# Optional parameters for QEMU and gdb.
QEMU_FLAGS ?=
GDB_EXTRA_FLAGS ?=
##
# You can pass an optional `DEBUG` variable to manipulate the build type. This
# will also affect how QEMU is run.
DEBUG =
ifeq ($(strip $(DEBUG)),)
CCFLAGS += -O3
QEMU_FLAGS += -nographic
else
ASFLAGS += -g
CCFLAGS += -g -O0 -D__DEBUG__
QEMU_FLAGS += -s -S
endif
##
# Paths
SRC = $(filter-out kernel/fbos.ld.S, $(wildcard kernel/*.S kernel/*.c lib/*.c lib/*.S))
OBJ = $(patsubst %.c,%.o,$(patsubst %.S,%.o,$(SRC)))
LINKER = kernel/fbos.ld
IMAGE = fbos
KRNL = $(IMAGE).elf
USR = usr/bin/init usr/bin/fizz usr/bin/buzz usr/bin/fizzbuzz
INIT = usr/initramfs.cpio
TESTS = test/test_dt test/test_initrd
ARCHIVE = fbos.tar.gz
LDFLAGS += -T $(LINKER)
##
# Kernel
.PHONY: all
all: clean $(IMAGE) usr test
.PHONY: $(IMAGE)
$(IMAGE): $(KRNL)
$(E) " OBJCOPY " $@
$(Q) $(OBJCPY) $(KRNL) -O binary $(IMAGE)
.PHONY: $(KRNL)
$(KRNL): $(OBJ) $(LINKER).S
$(Q) $(CC) -E $(LINKER).S -Iinclude/ -o $(LINKER)
$(E) " LD " $@
$(Q) $(LD) $(LDFLAGS) $(OBJ) -o $(KRNL)
.c.o:
$(E) " CC " $(basename $@)
$(Q) $(CC) $(CCFLAGS) -c $< -o $@
.S.o:
$(E) " CC " $(basename $@)
$(Q) $(CC) $(CCFLAGS) -D__ASSEMBLY__ -c $< -o $@
##
# User space
.PHONY: usr
usr: $(USR)
$(E) " CPIO " $(INIT)
$(Q) find usr/bin/ -type f -executable | cpio -o --quiet -H newc > $(INIT)
usr/src/%.o: usr/src/%.S
$(E) " CC " $(basename $@)
$(Q) $(CC) $(ASFLAGS) -D__ASSEMBLY__ -c $< -o $@
usr/bin/%: usr/src/%.o
$(Q) mkdir -p usr/bin/
$(E) " LD " $@
$(Q) $(LD) $(USRFLAGS) $< -o $@
# HACK: do not remove object files from usr/src/.
.SECONDARY:
##
# Tests
.PHONY: test
test: host_lib usr $(TESTS)
$(Q) ./test/test_dt
$(Q) ./test/test_initrd
host_lib:
$(Q) mkdir -p test/lib
$(Q) $(HOSTCC) $(WARNINGS) -Iinclude/ -g -c lib/dt.c -o test/lib/dt.o
$(Q) $(HOSTCC) $(WARNINGS) -Iinclude/ -g -c lib/initrd.c -o test/lib/initrd.o
test/%.o: test/%.c
$(Q) $(HOSTCC) $(WARNINGS) -g -Iinclude/ -c $< -o $@
test/%: test/%.o
$(E) " TEST " $@
$(Q) $(HOSTCC) -Iinclude/ $< test/lib/*.o -o $@
##
# Produce an archive for releases.
.PHONY: archive
archive: all
$(Q) $(eval DIR := $(shell mktemp -d))
$(Q) mkdir -p $(DIR)/fbos && cp $(IMAGE) $(INIT) $(DIR)/fbos/
$(Q) cd $(DIR) && tar czf $(ARCHIVE) fbos/
$(Q) cp $(DIR)/$(ARCHIVE) .
$(E) " TAR $(ARCHIVE)"
##
# Hacking
CPUS ?= 4
.PHONY: qemu
qemu: clean $(IMAGE) usr
$(Q) $(QEMU) $(QEMU_FLAGS) -machine virt -smp $(CPUS) -kernel $(IMAGE) -initrd $(INIT)
.PHONY: gdb
gdb:
$(Q) gdb --command utils/init.gdb $(GDB_EXTRA_FLAGS) $(KRNL)
.PHONY: clean
clean:
$(Q) rm -rf $(OBJ) $(KRNL) $(IMAGE) $(LINKER) $(USR) usr/src/*.o $(INIT) test/*.o test/lib/*.o $(TESTS) $(ARCHIVE)
.PHONY: lint
lint:
$(Q) git ls-files *.c *.h | xargs clang-format --dry-run --Werror