-
Notifications
You must be signed in to change notification settings - Fork 87
/
Makefile
213 lines (174 loc) · 5.62 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Default values for build system.
export V ?=
export GUI ?=
export RELEASE ?=
export ARCH ?= x64
export LOG ?=
export LOG_SERIAL ?=
export CMDLINE ?=
export QEMU_ARGS ?=
# The default build target.
.PHONY: default
default: build
# Disable builtin implicit rules and variables.
MAKEFLAGS += --no-builtin-rules --no-builtin-variables
.SUFFIXES:
# Enable verbose output if $(V) is set.
ifeq ($(V),)
.SILENT:
endif
# $(IMAGE): Use a Docker image for initramfs.
ifeq ($(IMAGE),)
INITRAMFS_PATH := build/testing.initramfs
export INIT_SCRIPT := /bin/sh
else
IMAGE_FILENAME := $(subst /,.s,$(IMAGE))
INITRAMFS_PATH := build/$(IMAGE_FILENAME).initramfs
export INIT_SCRIPT := $(shell tools/inspect-init-in-docker-image.py $(IMAGE))
endif
DUMMY_INITRAMFS_PATH := build/dummy-for-lint.initramfs
# Set the platform name for docker image cross compiling.
ifeq ($(ARCH),x64)
docker_platform = linux/amd64
endif
ifeq ($(docker_platform),)
$(error "docker_platform is not set for $(ARCH)!")
endif
topdir := $(PWD)
build_mode := $(if $(RELEASE),release,debug)
target_json := kernel/arch/$(ARCH)/$(ARCH).json
kernel_elf := kerla.$(ARCH).elf
stripped_kernel_elf := kerla.$(ARCH).stripped.elf
kernel_symbols := $(kernel_elf:.elf=.symbols)
PROGRESS := printf " \\033[1;96m%8s\\033[0m \\033[1;m%s\\033[0m\\n"
PYTHON3 ?= python3
CARGO ?= cargo
BOCHS ?= bochs
NM ?= rust-nm
READELF ?= readelf
STRIP ?= rust-strip
DRAWIO ?= /Applications/draw.io.app/Contents/MacOS/draw.io
export RUSTFLAGS = -Z emit-stack-sizes
CARGOFLAGS += -Z build-std=core,alloc -Z build-std-features=compiler-builtins-mem
CARGOFLAGS += --target $(target_json)
CARGOFLAGS += $(if $(RELEASE),--release,)
TESTCARGOFLAGS += --package kerla_kernel -Z unstable-options
TESTCARGOFLAGS += --config "target.$(ARCH).runner = './tools/run-unittests.sh'"
WATCHFLAGS += --clear
export CARGO_FROM_MAKE=1
export INITRAMFS_PATH
export ARCH
export PYTHON3
export NM
#
# Build Commands
#
.PHONY: build
build:
$(MAKE) build-crate
cp target/$(ARCH)/$(build_mode)/kerla_kernel $(kernel_elf)
$(PROGRESS) "NM" $(kernel_symbols)
$(NM) $(kernel_elf) | rustfilt | awk '{ $$2=""; print $$0 }' > $(kernel_symbols)
$(PROGRESS) "SYMBOLS" $(kernel_elf)
$(PYTHON3) tools/embed-symbol-table.py $(kernel_symbols) $(kernel_elf)
$(PROGRESS) "STRIP" $(stripped_kernel_elf)
$(STRIP) $(kernel_elf) -o $(stripped_kernel_elf)
.PHONY: build-crate
build-crate:
$(MAKE) initramfs
$(PROGRESS) "CARGO" "kernel"
$(CARGO) build $(CARGOFLAGS) --manifest-path kernel/Cargo.toml
.PHONY: initramfs
initramfs: $(INITRAMFS_PATH)
.PHONY: buildw
buildw:
$(CARGO) watch $(WATCHFLAGS) -s "$(MAKE) build-crate"
.PHONY: iso
iso: build
$(PROGRESS) MKISO kerla.iso
mkdir -p isofiles/boot/grub
cp boot/grub.cfg isofiles/boot/grub/grub.cfg
cp $(stripped_kernel_elf) isofiles/kerla.elf
grub-mkrescue -o kerla.iso isofiles
.PHONY: run
run: build
$(PYTHON3) tools/run-qemu.py \
--arch $(ARCH) \
$(if $(GUI),--gui,) \
$(if $(KVM),--kvm,) \
$(if $(GDB),--gdb,) \
$(if $(LOG),--append-cmdline "log=$(LOG)",) \
$(if $(CMDLINE),--append-cmdline "$(CMDLINE)",) \
$(if $(LOG_SERIAL),--log-serial "$(LOG_SERIAL)",) \
$(if $(QEMU),--qemu $(QEMU),) \
$(kernel_elf) -- $(QEMU_ARGS)
.PHONY: bochs
bochs: iso
$(BOCHS) -qf boot/bochsrc
.PHONY: test
test:
$(MAKE) initramfs
$(CARGO) test $(CARGOFLAGS) $(TESTCARGOFLAGS)
.PHONY: testw
testw:
$(CARGO) watch $(WATCHFLAGS) -s "$(MAKE) test"
.PHONY: check
check:
$(MAKE) $(DUMMY_INITRAMFS_PATH)
INITRAMFS_PATH=$(DUMMY_INITRAMFS_PATH) $(CARGO) check $(CARGOFLAGS)
.PHONY: checkw
checkw:
$(CARGO) watch $(WATCHFLAGS) -s "$(MAKE) check"
.PHONY: docs
docs:
$(PROGRESS) "MDBOOK" build/docs
mkdir -p build
make doc-images
mdbook build -d $(topdir)/build/docs Documentation
.PHONY: doc-images
doc-images: $(patsubst %.drawio, %.svg, $(wildcard Documentation/*.drawio))
.PHONY: docsw
docsw:
mkdir -p build
mdbook serve -d $(topdir)/build/docs Documentation
.PHONY: src-docs
src-docs:
RUSTFLAGS="-C panic=abort -Z panic_abort_tests" $(CARGO) doc
.PHONY: lint
lint:
$(MAKE) $(DUMMY_INITRAMFS_PATH)
INITRAMFS_PATH=$(DUMMY_INITRAMFS_PATH) RUSTFLAGS="-C panic=abort -Z panic_abort_tests" $(CARGO) clippy
.PHONY: strict-lint
strict-lint:
$(MAKE) $(DUMMY_INITRAMFS_PATH)
INITRAMFS_PATH=$(DUMMY_INITRAMFS_PATH) RUSTFLAGS="-C panic=abort -Z panic_abort_tests" $(CARGO) clippy -- -D warnings
.PHONY: lint-and-fix
lint-and-fix:
$(MAKE) $(DUMMY_INITRAMFS_PATH)
INITRAMFS_PATH=$(DUMMY_INITRAMFS_PATH) RUSTFLAGS="-C panic=abort -Z panic_abort_tests" $(CARGO) clippy --fix -Z unstable-options
.PHONY: print-stack-sizes
print-stack-sizes: build
$(READELF) --stack-sizes $(kernel_elf) | sort -n | rustfilt
.PHONY: clean
clean:
$(CARGO) clean
rm -rf *.elf *.iso *.bin *.symbols isofiles
#
# Build Rules
#
build/testing.initramfs: $(wildcard testing/*) $(wildcard testing/*/*) Makefile
$(PROGRESS) "BUILD" testing
cd testing && docker buildx build --platform $(docker_platform) -t kerla-testing .
$(PROGRESS) "EXPORT" testing
mkdir -p build
$(PYTHON3) tools/docker2initramfs.py $@ kerla-testing
build/$(IMAGE_FILENAME).initramfs: tools/docker2initramfs.py Makefile
$(PROGRESS) "EXPORT" $(IMAGE)
mkdir -p build
$(PYTHON3) tools/docker2initramfs.py $@ $(IMAGE)
$(DUMMY_INITRAMFS_PATH):
mkdir -p $(@D)
touch $@
%.svg: %.drawio
$(PROGRESS) "DRAWIO" $@
$(DRAWIO) -x -f svg -o $@ $<