Skip to content

Commit b7cecaf

Browse files
committed
Non-phony kernel targets
Saves time on various targets that depend on the kernel ELF because calling cargo can be completely skipped if nothing changed.
1 parent cb4ae40 commit b7cecaf

File tree

29 files changed

+828
-410
lines changed

29 files changed

+828
-410
lines changed

01_wait_forever/Makefile

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
##
33
## Copyright (c) 2018-2022 Andre Richter <[email protected]>
44

5-
include ../common/color.mk
5+
include ../common/format.mk
66
include ../common/docker.mk
77

88
##--------------------------------------------------------------------------------------------------
@@ -49,10 +49,16 @@ export LD_SCRIPT_PATH
4949

5050

5151
##--------------------------------------------------------------------------------------------------
52-
## Generic configuration values
52+
## Targets and Prerequisites
5353
##--------------------------------------------------------------------------------------------------
5454
KERNEL_LINKER_SCRIPT = link.ld
55-
KERNEL_ELF = target/$(TARGET)/release/kernel
55+
56+
LAST_BUILD_CONFIG = target/$(BSP).build_config
57+
58+
KERNEL_ELF = target/$(TARGET)/release/kernel
59+
# This parses cargo's dep-info file.
60+
# https://doc.rust-lang.org/cargo/guide/build-cache.html#dep-info-files
61+
KERNEL_ELF_DEPS = $(filter-out %: ,$(file < $(KERNEL_ELF_RAW).d)) $(LAST_BUILD_CONFIG)
5662

5763

5864

@@ -97,28 +103,41 @@ DOCKER_TOOLS = $(DOCKER_CMD) $(DOCKER_IMAGE)
97103
##--------------------------------------------------------------------------------------------------
98104
## Targets
99105
##--------------------------------------------------------------------------------------------------
100-
.PHONY: all $(KERNEL_ELF) $(KERNEL_BIN) doc qemu clippy clean readelf objdump nm check
106+
.PHONY: all doc qemu clippy clean readelf objdump nm check
101107

102108
all: $(KERNEL_BIN)
103109

104110
##------------------------------------------------------------------------------
105-
## Build the kernel ELF
111+
## Save the configuration as a file, so make understands if it changed.
112+
##------------------------------------------------------------------------------
113+
$(LAST_BUILD_CONFIG):
114+
@rm -f target/*.build_config
115+
@mkdir -p target
116+
@touch $(LAST_BUILD_CONFIG)
117+
118+
##------------------------------------------------------------------------------
119+
## Compile the kernel ELF
106120
##------------------------------------------------------------------------------
107-
$(KERNEL_ELF):
108-
$(call colorecho, "\nCompiling kernel - $(BSP)")
121+
$(KERNEL_ELF): $(KERNEL_ELF_DEPS)
122+
$(call color_header, "Compiling kernel ELF - $(BSP)")
109123
@RUSTFLAGS="$(RUSTFLAGS_PEDANTIC)" $(RUSTC_CMD)
110124

111125
##------------------------------------------------------------------------------
112-
## Build the stripped kernel binary
126+
## Generate the stripped kernel binary
113127
##------------------------------------------------------------------------------
114128
$(KERNEL_BIN): $(KERNEL_ELF)
129+
$(call color_header, "Generating stripped binary")
115130
@$(OBJCOPY_CMD) $(KERNEL_ELF) $(KERNEL_BIN)
131+
$(call color_progress_prefix, "Name")
132+
@echo $(KERNEL_BIN)
133+
$(call color_progress_prefix, "Size")
134+
@printf '%s KiB\n' `du -k $(KERNEL_BIN) | cut -f1`
116135

117136
##------------------------------------------------------------------------------
118-
## Build the documentation
137+
## Generate the documentation
119138
##------------------------------------------------------------------------------
120139
doc:
121-
$(call colorecho, "\nGenerating docs")
140+
$(call color_header, "Generating docs")
122141
@$(DOC_CMD) --document-private-items --open
123142

124143
##------------------------------------------------------------------------------
@@ -127,12 +146,12 @@ doc:
127146
ifeq ($(QEMU_MACHINE_TYPE),) # QEMU is not supported for the board.
128147

129148
qemu:
130-
$(call colorecho, "\n$(QEMU_MISSING_STRING)")
149+
$(call color_header, "$(QEMU_MISSING_STRING)")
131150

132151
else # QEMU is supported.
133152

134153
qemu: $(KERNEL_BIN)
135-
$(call colorecho, "\nLaunching QEMU")
154+
$(call color_header, "Launching QEMU")
136155
@$(DOCKER_QEMU) $(EXEC_QEMU) $(QEMU_RELEASE_ARGS) -kernel $(KERNEL_BIN)
137156
endif
138157

@@ -152,14 +171,14 @@ clean:
152171
## Run readelf
153172
##------------------------------------------------------------------------------
154173
readelf: $(KERNEL_ELF)
155-
$(call colorecho, "\nLaunching readelf")
174+
$(call color_header, "Launching readelf")
156175
@$(DOCKER_TOOLS) $(READELF_BINARY) --headers $(KERNEL_ELF)
157176

158177
##------------------------------------------------------------------------------
159178
## Run objdump
160179
##------------------------------------------------------------------------------
161180
objdump: $(KERNEL_ELF)
162-
$(call colorecho, "\nLaunching objdump")
181+
$(call color_header, "Launching objdump")
163182
@$(DOCKER_TOOLS) $(OBJDUMP_BINARY) --disassemble --demangle \
164183
--section .text \
165184
$(KERNEL_ELF) | rustfilt
@@ -168,7 +187,7 @@ objdump: $(KERNEL_ELF)
168187
## Run nm
169188
##------------------------------------------------------------------------------
170189
nm: $(KERNEL_ELF)
171-
$(call colorecho, "\nLaunching nm")
190+
$(call color_header, "Launching nm")
172191
@$(DOCKER_TOOLS) $(NM_BINARY) --demangle --print-size $(KERNEL_ELF) | sort | rustfilt
173192

174193
##------------------------------------------------------------------------------

02_runtime_init/Makefile

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
##
33
## Copyright (c) 2018-2022 Andre Richter <[email protected]>
44

5-
include ../common/color.mk
5+
include ../common/format.mk
66
include ../common/docker.mk
77

88
##--------------------------------------------------------------------------------------------------
@@ -49,10 +49,16 @@ export LD_SCRIPT_PATH
4949

5050

5151
##--------------------------------------------------------------------------------------------------
52-
## Generic configuration values
52+
## Targets and Prerequisites
5353
##--------------------------------------------------------------------------------------------------
5454
KERNEL_LINKER_SCRIPT = link.ld
55-
KERNEL_ELF = target/$(TARGET)/release/kernel
55+
56+
LAST_BUILD_CONFIG = target/$(BSP).build_config
57+
58+
KERNEL_ELF = target/$(TARGET)/release/kernel
59+
# This parses cargo's dep-info file.
60+
# https://doc.rust-lang.org/cargo/guide/build-cache.html#dep-info-files
61+
KERNEL_ELF_DEPS = $(filter-out %: ,$(file < $(KERNEL_ELF_RAW).d)) $(LAST_BUILD_CONFIG)
5662

5763

5864

@@ -97,28 +103,41 @@ DOCKER_TOOLS = $(DOCKER_CMD) $(DOCKER_IMAGE)
97103
##--------------------------------------------------------------------------------------------------
98104
## Targets
99105
##--------------------------------------------------------------------------------------------------
100-
.PHONY: all $(KERNEL_ELF) $(KERNEL_BIN) doc qemu clippy clean readelf objdump nm check
106+
.PHONY: all doc qemu clippy clean readelf objdump nm check
101107

102108
all: $(KERNEL_BIN)
103109

104110
##------------------------------------------------------------------------------
105-
## Build the kernel ELF
111+
## Save the configuration as a file, so make understands if it changed.
112+
##------------------------------------------------------------------------------
113+
$(LAST_BUILD_CONFIG):
114+
@rm -f target/*.build_config
115+
@mkdir -p target
116+
@touch $(LAST_BUILD_CONFIG)
117+
118+
##------------------------------------------------------------------------------
119+
## Compile the kernel ELF
106120
##------------------------------------------------------------------------------
107-
$(KERNEL_ELF):
108-
$(call colorecho, "\nCompiling kernel - $(BSP)")
121+
$(KERNEL_ELF): $(KERNEL_ELF_DEPS)
122+
$(call color_header, "Compiling kernel ELF - $(BSP)")
109123
@RUSTFLAGS="$(RUSTFLAGS_PEDANTIC)" $(RUSTC_CMD)
110124

111125
##------------------------------------------------------------------------------
112-
## Build the stripped kernel binary
126+
## Generate the stripped kernel binary
113127
##------------------------------------------------------------------------------
114128
$(KERNEL_BIN): $(KERNEL_ELF)
129+
$(call color_header, "Generating stripped binary")
115130
@$(OBJCOPY_CMD) $(KERNEL_ELF) $(KERNEL_BIN)
131+
$(call color_progress_prefix, "Name")
132+
@echo $(KERNEL_BIN)
133+
$(call color_progress_prefix, "Size")
134+
@printf '%s KiB\n' `du -k $(KERNEL_BIN) | cut -f1`
116135

117136
##------------------------------------------------------------------------------
118-
## Build the documentation
137+
## Generate the documentation
119138
##------------------------------------------------------------------------------
120139
doc:
121-
$(call colorecho, "\nGenerating docs")
140+
$(call color_header, "Generating docs")
122141
@$(DOC_CMD) --document-private-items --open
123142

124143
##------------------------------------------------------------------------------
@@ -127,12 +146,12 @@ doc:
127146
ifeq ($(QEMU_MACHINE_TYPE),) # QEMU is not supported for the board.
128147

129148
qemu:
130-
$(call colorecho, "\n$(QEMU_MISSING_STRING)")
149+
$(call color_header, "$(QEMU_MISSING_STRING)")
131150

132151
else # QEMU is supported.
133152

134153
qemu: $(KERNEL_BIN)
135-
$(call colorecho, "\nLaunching QEMU")
154+
$(call color_header, "Launching QEMU")
136155
@$(DOCKER_QEMU) $(EXEC_QEMU) $(QEMU_RELEASE_ARGS) -kernel $(KERNEL_BIN)
137156
endif
138157

@@ -152,14 +171,14 @@ clean:
152171
## Run readelf
153172
##------------------------------------------------------------------------------
154173
readelf: $(KERNEL_ELF)
155-
$(call colorecho, "\nLaunching readelf")
174+
$(call color_header, "Launching readelf")
156175
@$(DOCKER_TOOLS) $(READELF_BINARY) --headers $(KERNEL_ELF)
157176

158177
##------------------------------------------------------------------------------
159178
## Run objdump
160179
##------------------------------------------------------------------------------
161180
objdump: $(KERNEL_ELF)
162-
$(call colorecho, "\nLaunching objdump")
181+
$(call color_header, "Launching objdump")
163182
@$(DOCKER_TOOLS) $(OBJDUMP_BINARY) --disassemble --demangle \
164183
--section .text \
165184
--section .rodata \
@@ -170,7 +189,7 @@ objdump: $(KERNEL_ELF)
170189
## Run nm
171190
##------------------------------------------------------------------------------
172191
nm: $(KERNEL_ELF)
173-
$(call colorecho, "\nLaunching nm")
192+
$(call color_header, "Launching nm")
174193
@$(DOCKER_TOOLS) $(NM_BINARY) --demangle --print-size $(KERNEL_ELF) | sort | rustfilt
175194

176195
##------------------------------------------------------------------------------

02_runtime_init/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ diff -uNr 01_wait_forever/Cargo.toml 02_runtime_init/Cargo.toml
5252
diff -uNr 01_wait_forever/Makefile 02_runtime_init/Makefile
5353
--- 01_wait_forever/Makefile
5454
+++ 02_runtime_init/Makefile
55-
@@ -162,6 +162,8 @@
56-
$(call colorecho, "\nLaunching objdump")
55+
@@ -181,6 +181,8 @@
56+
$(call color_header, "Launching objdump")
5757
@$(DOCKER_TOOLS) $(OBJDUMP_BINARY) --disassemble --demangle \
5858
--section .text \
5959
+ --section .rodata \

03_hacky_hello_world/Makefile

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
##
33
## Copyright (c) 2018-2022 Andre Richter <[email protected]>
44

5-
include ../common/color.mk
5+
include ../common/format.mk
66
include ../common/docker.mk
77

88
##--------------------------------------------------------------------------------------------------
@@ -49,10 +49,16 @@ export LD_SCRIPT_PATH
4949

5050

5151
##--------------------------------------------------------------------------------------------------
52-
## Generic configuration values
52+
## Targets and Prerequisites
5353
##--------------------------------------------------------------------------------------------------
5454
KERNEL_LINKER_SCRIPT = link.ld
55-
KERNEL_ELF = target/$(TARGET)/release/kernel
55+
56+
LAST_BUILD_CONFIG = target/$(BSP).build_config
57+
58+
KERNEL_ELF = target/$(TARGET)/release/kernel
59+
# This parses cargo's dep-info file.
60+
# https://doc.rust-lang.org/cargo/guide/build-cache.html#dep-info-files
61+
KERNEL_ELF_DEPS = $(filter-out %: ,$(file < $(KERNEL_ELF_RAW).d)) $(LAST_BUILD_CONFIG)
5662

5763

5864

@@ -100,28 +106,41 @@ DOCKER_TEST = $(DOCKER_CMD) $(DOCKER_ARG_DIR_COMMON) $(DOCKER_IMAGE)
100106
##--------------------------------------------------------------------------------------------------
101107
## Targets
102108
##--------------------------------------------------------------------------------------------------
103-
.PHONY: all $(KERNEL_ELF) $(KERNEL_BIN) doc qemu clippy clean readelf objdump nm check
109+
.PHONY: all doc qemu clippy clean readelf objdump nm check
104110

105111
all: $(KERNEL_BIN)
106112

107113
##------------------------------------------------------------------------------
108-
## Build the kernel ELF
114+
## Save the configuration as a file, so make understands if it changed.
115+
##------------------------------------------------------------------------------
116+
$(LAST_BUILD_CONFIG):
117+
@rm -f target/*.build_config
118+
@mkdir -p target
119+
@touch $(LAST_BUILD_CONFIG)
120+
121+
##------------------------------------------------------------------------------
122+
## Compile the kernel ELF
109123
##------------------------------------------------------------------------------
110-
$(KERNEL_ELF):
111-
$(call colorecho, "\nCompiling kernel - $(BSP)")
124+
$(KERNEL_ELF): $(KERNEL_ELF_DEPS)
125+
$(call color_header, "Compiling kernel ELF - $(BSP)")
112126
@RUSTFLAGS="$(RUSTFLAGS_PEDANTIC)" $(RUSTC_CMD)
113127

114128
##------------------------------------------------------------------------------
115-
## Build the stripped kernel binary
129+
## Generate the stripped kernel binary
116130
##------------------------------------------------------------------------------
117131
$(KERNEL_BIN): $(KERNEL_ELF)
132+
$(call color_header, "Generating stripped binary")
118133
@$(OBJCOPY_CMD) $(KERNEL_ELF) $(KERNEL_BIN)
134+
$(call color_progress_prefix, "Name")
135+
@echo $(KERNEL_BIN)
136+
$(call color_progress_prefix, "Size")
137+
@printf '%s KiB\n' `du -k $(KERNEL_BIN) | cut -f1`
119138

120139
##------------------------------------------------------------------------------
121-
## Build the documentation
140+
## Generate the documentation
122141
##------------------------------------------------------------------------------
123142
doc:
124-
$(call colorecho, "\nGenerating docs")
143+
$(call color_header, "Generating docs")
125144
@$(DOC_CMD) --document-private-items --open
126145

127146
##------------------------------------------------------------------------------
@@ -130,12 +149,12 @@ doc:
130149
ifeq ($(QEMU_MACHINE_TYPE),) # QEMU is not supported for the board.
131150

132151
qemu:
133-
$(call colorecho, "\n$(QEMU_MISSING_STRING)")
152+
$(call color_header, "$(QEMU_MISSING_STRING)")
134153

135154
else # QEMU is supported.
136155

137156
qemu: $(KERNEL_BIN)
138-
$(call colorecho, "\nLaunching QEMU")
157+
$(call color_header, "Launching QEMU")
139158
@$(DOCKER_QEMU) $(EXEC_QEMU) $(QEMU_RELEASE_ARGS) -kernel $(KERNEL_BIN)
140159
endif
141160

@@ -155,14 +174,14 @@ clean:
155174
## Run readelf
156175
##------------------------------------------------------------------------------
157176
readelf: $(KERNEL_ELF)
158-
$(call colorecho, "\nLaunching readelf")
177+
$(call color_header, "Launching readelf")
159178
@$(DOCKER_TOOLS) $(READELF_BINARY) --headers $(KERNEL_ELF)
160179

161180
##------------------------------------------------------------------------------
162181
## Run objdump
163182
##------------------------------------------------------------------------------
164183
objdump: $(KERNEL_ELF)
165-
$(call colorecho, "\nLaunching objdump")
184+
$(call color_header, "Launching objdump")
166185
@$(DOCKER_TOOLS) $(OBJDUMP_BINARY) --disassemble --demangle \
167186
--section .text \
168187
--section .rodata \
@@ -173,7 +192,7 @@ objdump: $(KERNEL_ELF)
173192
## Run nm
174193
##------------------------------------------------------------------------------
175194
nm: $(KERNEL_ELF)
176-
$(call colorecho, "\nLaunching nm")
195+
$(call color_header, "Launching nm")
177196
@$(DOCKER_TOOLS) $(NM_BINARY) --demangle --print-size $(KERNEL_ELF) | sort | rustfilt
178197

179198
##------------------------------------------------------------------------------
@@ -191,16 +210,16 @@ check:
191210

192211
ifeq ($(QEMU_MACHINE_TYPE),) # QEMU is not supported for the board.
193212

194-
test_boot test :
195-
$(call colorecho, "\n$(QEMU_MISSING_STRING)")
213+
test_boot test:
214+
$(call color_header, "$(QEMU_MISSING_STRING)")
196215

197216
else # QEMU is supported.
198217

199218
##------------------------------------------------------------------------------
200219
## Run boot test
201220
##------------------------------------------------------------------------------
202221
test_boot: $(KERNEL_BIN)
203-
$(call colorecho, "\nBoot test - $(BSP)")
222+
$(call color_header, "Boot test - $(BSP)")
204223
@$(DOCKER_TEST) $(EXEC_TEST_DISPATCH) $(EXEC_QEMU) $(QEMU_RELEASE_ARGS) -kernel $(KERNEL_BIN)
205224

206225
test: test_boot

0 commit comments

Comments
 (0)