Skip to content

Commit de3ba3e

Browse files
committed
Refactor Tests and Makefile
- Carve out common files for tests - Add boot tests starting tutorial 3 - Overhaul the Makefile for more structure
1 parent 15a1e71 commit de3ba3e

File tree

93 files changed

+3558
-2190
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+3558
-2190
lines changed

.githooks/pre-commit

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
#
66
# Copyright (c) 2018-2021 Andre Richter <[email protected]>
77

8-
require 'rubygems'
9-
require 'bundler/setup'
108
require_relative '../utils/devtool/copyright'
119

1210
def copyright_check(staged_files)
1311
source_files_exts = ['.S', '.rs', '.rb']
1412

1513
staged_files = staged_files.select do |f|
1614
next if f.include?('build.rs')
15+
next if f.include?('boot_test_string.rb')
1716

1817
f.include?('Makefile') ||
1918
f.include?('Dockerfile') ||

01_wait_forever/Makefile

+68-13
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@
22
##
33
## Copyright (c) 2018-2021 Andre Richter <[email protected]>
44

5-
include ../utils/color.mk.in
5+
include ../common/color.mk.in
66

7-
# Default to the RPi3
7+
##--------------------------------------------------------------------------------------------------
8+
## Optional, user-provided configuration values
9+
##--------------------------------------------------------------------------------------------------
10+
11+
# Default to the RPi3.
812
BSP ?= rpi3
913

10-
# BSP-specific arguments
14+
15+
16+
##--------------------------------------------------------------------------------------------------
17+
## Hardcoded configuration values
18+
##--------------------------------------------------------------------------------------------------
19+
20+
# BSP-specific arguments.
1121
ifeq ($(BSP),rpi3)
1222
TARGET = aarch64-unknown-none-softfloat
1323
KERNEL_BIN = kernel8.img
@@ -32,11 +42,18 @@ else ifeq ($(BSP),rpi4)
3242
RUSTC_MISC_ARGS = -C target-cpu=cortex-a72
3343
endif
3444

35-
# Export for build.rs
45+
QEMU_MISSING_STRING = "This board is not yet supported for QEMU."
46+
47+
# Export for build.rs.
3648
export LINKER_FILE
3749

38-
QEMU_MISSING_STRING = "This board is not yet supported for QEMU."
50+
KERNEL_ELF = target/$(TARGET)/release/kernel
51+
3952

53+
54+
##--------------------------------------------------------------------------------------------------
55+
## Command building blocks
56+
##--------------------------------------------------------------------------------------------------
4057
RUSTFLAGS = -C link-arg=-T$(LINKER_FILE) $(RUSTC_MISC_ARGS)
4158
RUSTFLAGS_PEDANTIC = $(RUSTFLAGS) -D warnings -D missing_docs
4259

@@ -53,61 +70,99 @@ OBJCOPY_CMD = rust-objcopy \
5370
--strip-all \
5471
-O binary
5572

56-
KERNEL_ELF = target/$(TARGET)/release/kernel
73+
EXEC_QEMU = $(QEMU_BINARY) -M $(QEMU_MACHINE_TYPE)
5774

58-
DOCKER_IMAGE = rustembedded/osdev-utils
59-
DOCKER_CMD = docker run --rm -v $(shell pwd):/work/tutorial -w /work/tutorial
60-
DOCKER_CMD_INTERACT = $(DOCKER_CMD) -i -t
75+
##------------------------------------------------------------------------------
76+
## Dockerization
77+
##------------------------------------------------------------------------------
78+
DOCKER_IMAGE = rustembedded/osdev-utils
79+
DOCKER_CMD = docker run -t --rm -v $(shell pwd):/work/tutorial -w /work/tutorial
80+
DOCKER_CMD_INTERACT = $(DOCKER_CMD) -i
6181

6282
DOCKER_QEMU = $(DOCKER_CMD_INTERACT) $(DOCKER_IMAGE)
6383
DOCKER_TOOLS = $(DOCKER_CMD) $(DOCKER_IMAGE)
6484

65-
EXEC_QEMU = $(QEMU_BINARY) -M $(QEMU_MACHINE_TYPE)
6685

86+
87+
##--------------------------------------------------------------------------------------------------
88+
## Targets
89+
##--------------------------------------------------------------------------------------------------
6790
.PHONY: all $(KERNEL_ELF) $(KERNEL_BIN) doc qemu clippy clean readelf objdump nm check
6891

6992
all: $(KERNEL_BIN)
7093

94+
##------------------------------------------------------------------------------
95+
## Build the kernel ELF
96+
##------------------------------------------------------------------------------
7197
$(KERNEL_ELF):
7298
$(call colorecho, "\nCompiling kernel - $(BSP)")
7399
@RUSTFLAGS="$(RUSTFLAGS_PEDANTIC)" $(RUSTC_CMD)
74100

101+
##------------------------------------------------------------------------------
102+
## Build the stripped kernel binary
103+
##------------------------------------------------------------------------------
75104
$(KERNEL_BIN): $(KERNEL_ELF)
76105
@$(OBJCOPY_CMD) $(KERNEL_ELF) $(KERNEL_BIN)
77106

107+
##------------------------------------------------------------------------------
108+
## Build the documentation
109+
##------------------------------------------------------------------------------
78110
doc:
79111
$(call colorecho, "\nGenerating docs")
80112
@$(DOC_CMD) --document-private-items --open
81113

82-
ifeq ($(QEMU_MACHINE_TYPE),)
114+
##------------------------------------------------------------------------------
115+
## Run the kernel in QEMU
116+
##------------------------------------------------------------------------------
117+
ifeq ($(QEMU_MACHINE_TYPE),) # QEMU is not supported for the board.
118+
83119
qemu:
84120
$(call colorecho, "\n$(QEMU_MISSING_STRING)")
85-
else
121+
122+
else # QEMU is supported.
123+
86124
qemu: $(KERNEL_BIN)
87125
$(call colorecho, "\nLaunching QEMU")
88126
@$(DOCKER_QEMU) $(EXEC_QEMU) $(QEMU_RELEASE_ARGS) -kernel $(KERNEL_BIN)
89127
endif
90128

129+
##------------------------------------------------------------------------------
130+
## Run clippy
131+
##------------------------------------------------------------------------------
91132
clippy:
92133
@RUSTFLAGS="$(RUSTFLAGS_PEDANTIC)" $(CLIPPY_CMD)
93134

135+
##------------------------------------------------------------------------------
136+
## Clean
137+
##------------------------------------------------------------------------------
94138
clean:
95139
rm -rf target $(KERNEL_BIN)
96140

141+
##------------------------------------------------------------------------------
142+
## Run readelf
143+
##------------------------------------------------------------------------------
97144
readelf: $(KERNEL_ELF)
98145
$(call colorecho, "\nLaunching readelf")
99146
@$(DOCKER_TOOLS) $(READELF_BINARY) --headers $(KERNEL_ELF)
100147

148+
##------------------------------------------------------------------------------
149+
## Run objdump
150+
##------------------------------------------------------------------------------
101151
objdump: $(KERNEL_ELF)
102152
$(call colorecho, "\nLaunching objdump")
103153
@$(DOCKER_TOOLS) $(OBJDUMP_BINARY) --disassemble --demangle \
104154
--section .text \
105155
$(KERNEL_ELF) | rustfilt
106156

157+
##------------------------------------------------------------------------------
158+
## Run nm
159+
##------------------------------------------------------------------------------
107160
nm: $(KERNEL_ELF)
108161
$(call colorecho, "\nLaunching nm")
109162
@$(DOCKER_TOOLS) $(NM_BINARY) --demangle --print-size $(KERNEL_ELF) | sort | rustfilt
110163

111-
# For rust-analyzer
164+
##------------------------------------------------------------------------------
165+
## Helper target for rust-analyzer
166+
##------------------------------------------------------------------------------
112167
check:
113168
@RUSTFLAGS="$(RUSTFLAGS)" $(CHECK_CMD) --message-format=json

02_runtime_init/Makefile

+68-13
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@
22
##
33
## Copyright (c) 2018-2021 Andre Richter <[email protected]>
44

5-
include ../utils/color.mk.in
5+
include ../common/color.mk.in
66

7-
# Default to the RPi3
7+
##--------------------------------------------------------------------------------------------------
8+
## Optional, user-provided configuration values
9+
##--------------------------------------------------------------------------------------------------
10+
11+
# Default to the RPi3.
812
BSP ?= rpi3
913

10-
# BSP-specific arguments
14+
15+
16+
##--------------------------------------------------------------------------------------------------
17+
## Hardcoded configuration values
18+
##--------------------------------------------------------------------------------------------------
19+
20+
# BSP-specific arguments.
1121
ifeq ($(BSP),rpi3)
1222
TARGET = aarch64-unknown-none-softfloat
1323
KERNEL_BIN = kernel8.img
@@ -32,11 +42,18 @@ else ifeq ($(BSP),rpi4)
3242
RUSTC_MISC_ARGS = -C target-cpu=cortex-a72
3343
endif
3444

35-
# Export for build.rs
45+
QEMU_MISSING_STRING = "This board is not yet supported for QEMU."
46+
47+
# Export for build.rs.
3648
export LINKER_FILE
3749

38-
QEMU_MISSING_STRING = "This board is not yet supported for QEMU."
50+
KERNEL_ELF = target/$(TARGET)/release/kernel
51+
3952

53+
54+
##--------------------------------------------------------------------------------------------------
55+
## Command building blocks
56+
##--------------------------------------------------------------------------------------------------
4057
RUSTFLAGS = -C link-arg=-T$(LINKER_FILE) $(RUSTC_MISC_ARGS)
4158
RUSTFLAGS_PEDANTIC = $(RUSTFLAGS) -D warnings -D missing_docs
4259

@@ -53,51 +70,84 @@ OBJCOPY_CMD = rust-objcopy \
5370
--strip-all \
5471
-O binary
5572

56-
KERNEL_ELF = target/$(TARGET)/release/kernel
73+
EXEC_QEMU = $(QEMU_BINARY) -M $(QEMU_MACHINE_TYPE)
5774

58-
DOCKER_IMAGE = rustembedded/osdev-utils
59-
DOCKER_CMD = docker run --rm -v $(shell pwd):/work/tutorial -w /work/tutorial
60-
DOCKER_CMD_INTERACT = $(DOCKER_CMD) -i -t
75+
##------------------------------------------------------------------------------
76+
## Dockerization
77+
##------------------------------------------------------------------------------
78+
DOCKER_IMAGE = rustembedded/osdev-utils
79+
DOCKER_CMD = docker run -t --rm -v $(shell pwd):/work/tutorial -w /work/tutorial
80+
DOCKER_CMD_INTERACT = $(DOCKER_CMD) -i
6181

6282
DOCKER_QEMU = $(DOCKER_CMD_INTERACT) $(DOCKER_IMAGE)
6383
DOCKER_TOOLS = $(DOCKER_CMD) $(DOCKER_IMAGE)
6484

65-
EXEC_QEMU = $(QEMU_BINARY) -M $(QEMU_MACHINE_TYPE)
6685

86+
87+
##--------------------------------------------------------------------------------------------------
88+
## Targets
89+
##--------------------------------------------------------------------------------------------------
6790
.PHONY: all $(KERNEL_ELF) $(KERNEL_BIN) doc qemu clippy clean readelf objdump nm check
6891

6992
all: $(KERNEL_BIN)
7093

94+
##------------------------------------------------------------------------------
95+
## Build the kernel ELF
96+
##------------------------------------------------------------------------------
7197
$(KERNEL_ELF):
7298
$(call colorecho, "\nCompiling kernel - $(BSP)")
7399
@RUSTFLAGS="$(RUSTFLAGS_PEDANTIC)" $(RUSTC_CMD)
74100

101+
##------------------------------------------------------------------------------
102+
## Build the stripped kernel binary
103+
##------------------------------------------------------------------------------
75104
$(KERNEL_BIN): $(KERNEL_ELF)
76105
@$(OBJCOPY_CMD) $(KERNEL_ELF) $(KERNEL_BIN)
77106

107+
##------------------------------------------------------------------------------
108+
## Build the documentation
109+
##------------------------------------------------------------------------------
78110
doc:
79111
$(call colorecho, "\nGenerating docs")
80112
@$(DOC_CMD) --document-private-items --open
81113

82-
ifeq ($(QEMU_MACHINE_TYPE),)
114+
##------------------------------------------------------------------------------
115+
## Run the kernel in QEMU
116+
##------------------------------------------------------------------------------
117+
ifeq ($(QEMU_MACHINE_TYPE),) # QEMU is not supported for the board.
118+
83119
qemu:
84120
$(call colorecho, "\n$(QEMU_MISSING_STRING)")
85-
else
121+
122+
else # QEMU is supported.
123+
86124
qemu: $(KERNEL_BIN)
87125
$(call colorecho, "\nLaunching QEMU")
88126
@$(DOCKER_QEMU) $(EXEC_QEMU) $(QEMU_RELEASE_ARGS) -kernel $(KERNEL_BIN)
89127
endif
90128

129+
##------------------------------------------------------------------------------
130+
## Run clippy
131+
##------------------------------------------------------------------------------
91132
clippy:
92133
@RUSTFLAGS="$(RUSTFLAGS_PEDANTIC)" $(CLIPPY_CMD)
93134

135+
##------------------------------------------------------------------------------
136+
## Clean
137+
##------------------------------------------------------------------------------
94138
clean:
95139
rm -rf target $(KERNEL_BIN)
96140

141+
##------------------------------------------------------------------------------
142+
## Run readelf
143+
##------------------------------------------------------------------------------
97144
readelf: $(KERNEL_ELF)
98145
$(call colorecho, "\nLaunching readelf")
99146
@$(DOCKER_TOOLS) $(READELF_BINARY) --headers $(KERNEL_ELF)
100147

148+
##------------------------------------------------------------------------------
149+
## Run objdump
150+
##------------------------------------------------------------------------------
101151
objdump: $(KERNEL_ELF)
102152
$(call colorecho, "\nLaunching objdump")
103153
@$(DOCKER_TOOLS) $(OBJDUMP_BINARY) --disassemble --demangle \
@@ -106,10 +156,15 @@ objdump: $(KERNEL_ELF)
106156
--section .got \
107157
$(KERNEL_ELF) | rustfilt
108158

159+
##------------------------------------------------------------------------------
160+
## Run nm
161+
##------------------------------------------------------------------------------
109162
nm: $(KERNEL_ELF)
110163
$(call colorecho, "\nLaunching nm")
111164
@$(DOCKER_TOOLS) $(NM_BINARY) --demangle --print-size $(KERNEL_ELF) | sort | rustfilt
112165

113-
# For rust-analyzer
166+
##------------------------------------------------------------------------------
167+
## Helper target for rust-analyzer
168+
##------------------------------------------------------------------------------
114169
check:
115170
@RUSTFLAGS="$(RUSTFLAGS)" $(CHECK_CMD) --message-format=json

02_runtime_init/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ diff -uNr 01_wait_forever/Cargo.toml 02_runtime_init/Cargo.toml
5353
diff -uNr 01_wait_forever/Makefile 02_runtime_init/Makefile
5454
--- 01_wait_forever/Makefile
5555
+++ 02_runtime_init/Makefile
56-
@@ -102,6 +102,8 @@
56+
@@ -152,6 +152,8 @@
5757
$(call colorecho, "\nLaunching objdump")
5858
@$(DOCKER_TOOLS) $(OBJDUMP_BINARY) --disassemble --demangle \
5959
--section .text \
6060
+ --section .rodata \
6161
+ --section .got \
6262
$(KERNEL_ELF) | rustfilt
6363

64-
nm: $(KERNEL_ELF)
64+
##------------------------------------------------------------------------------
6565

6666
diff -uNr 01_wait_forever/src/_arch/aarch64/cpu/boot.rs 02_runtime_init/src/_arch/aarch64/cpu/boot.rs
6767
--- 01_wait_forever/src/_arch/aarch64/cpu/boot.rs

0 commit comments

Comments
 (0)