-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMakefile
More file actions
361 lines (306 loc) · 10.2 KB
/
Makefile
File metadata and controls
361 lines (306 loc) · 10.2 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# =============================================================================
# Makefile — Tutorial-OS Unified Build System
# =============================================================================
#
# Two languages, one project, one entry point.
#
# Usage:
# make LANG=c BOARD=rpi-zero2w-gpi # C build for Pi Zero 2W
# make LANG=rust BOARD=rpi-zero2w # Rust build for Pi Zero 2W
# make LANG=c BOARD=milkv-mars # C build for Milk-V Mars
# make LANG=rust BOARD=milkv-mars # Rust build for Milk-V Mars
# make LANG=c BOARD=lattepanda-mu # C build for LattePanda MU
# make info # Show build configuration
# make clean # Clean all build artifacts
#
# How this works:
# LANG=c: Traditional board.mk → soc.mk → gcc cascade.
# C sources live in src/ directories alongside Rust files.
# Assembly and linker scripts stay at crate/SoC root level.
#
# LANG=rust: Delegates to build.sh which drives Cargo.
# cargo build -p kernel --features board-xxx --target yyy
# Assembly compiled via build.rs + cc crate.
#
# Both languages share:
# - boot/ Shared assembly entry points
# - board/ Board configs, deploy docs, boot files
# - soc/*/linker.ld Linker scripts (identical for both)
# - soc/*/soc.mk C build config (ignored by Cargo)
# - soc/*/boot_soc.S SoC-specific assembly
#
# =============================================================================
LANG ?= c
BOARD ?= rpi-zero2w-gpi
# =============================================================================
# LANG=rust — Delegate to Cargo via build.sh
# =============================================================================
#
# The Rust build chain is entirely Cargo-driven. The Makefile just forwards
# the board name and gets out of the way.
ifeq ($(LANG),rust)
# Strip the GPi suffix for Rust board names (Rust uses board-rpi-zero2w, not board-rpi-zero2w-gpi)
# The -gpi variant is a C-side board.mk concern (display resolution override).
.PHONY: all clean info image
all:
@./build.sh $(BOARD) release
debug:
@./build.sh $(BOARD) debug
clean:
@echo "Cleaning Rust build artifacts..."
@cargo clean 2>/dev/null || true
@rm -rf output/
@echo "Done."
info:
@echo "Language: Rust"
@echo "Board: $(BOARD)"
@echo "Build: cargo build -p kernel --features board-$(BOARD) --target <arch>"
image: all
@echo "Image creation handled by build.sh (checks board/$(BOARD)/mkimage.sh)"
else
# =============================================================================
# LANG=c — Traditional Makefile Build
# =============================================================================
#
# Source paths point into src/ directories where C files live alongside
# Rust files. Assembly and build infrastructure stay at crate root level.
#
BUILD_DIR := build/$(BOARD)
# ──── Default toolchain (ARM64, overridden by board.mk for other arches) ────
CROSS_COMPILE ?= aarch64-none-elf-
ARCH_CFLAGS ?= -mcpu=cortex-a53 -mgeneral-regs-only
ARCH_ASFLAGS ?=
# ──── Base flags ────
#
# Include paths point into src/ directories where headers now live.
# The old layout had -Icommon -Ihal etc; now it's -Icommon/src -Ihal/src.
#
BASE_CFLAGS := -Wall -Wextra -O2 -ffreestanding -nostdlib -nostartfiles
BASE_CFLAGS += -Ihal/src
BASE_CFLAGS += -Icommon/src
BASE_CFLAGS += -Idrivers/src/framebuffer
BASE_CFLAGS += -Iui/src/core -Iui/src/themes -Iui/src/widgets
LDFLAGS := -nostdlib
# ──── Include board config ────
ifeq ($(wildcard board/$(BOARD)/board.mk),)
$(error Unknown board: $(BOARD). Check board/ directory.)
endif
include board/$(BOARD)/board.mk
ifndef SOC
$(error board/$(BOARD)/board.mk must set SOC)
endif
# ──── Include SoC config ────
ifeq ($(wildcard soc/$(SOC)/soc.mk),)
$(error Unknown SoC: $(SOC). Check soc/ directory.)
endif
include soc/$(SOC)/soc.mk
# Validate required variables from soc.mk
ifneq ($(BUILD_MODE),uefi)
ifneq ($(BUILD_MODE),pico)
ifndef LINKER_SCRIPT
$(error soc/$(SOC)/soc.mk must set LINKER_SCRIPT)
endif
endif
endif
ifndef KERNEL_NAME
$(error soc/$(SOC)/soc.mk must set KERNEL_NAME)
endif
# ──── Toolchain (derived AFTER board/soc includes) ────
CC := $(CROSS_COMPILE)gcc
LD := $(CROSS_COMPILE)ld
OBJCOPY := $(CROSS_COMPILE)objcopy
OBJDUMP := $(CROSS_COMPILE)objdump
# ──── CFLAGS assembled from base + arch + board + soc ────
CFLAGS := $(BASE_CFLAGS) $(ARCH_CFLAGS)
CFLAGS += $(BOARD_INCLUDES) $(BOARD_DEFINES)
CFLAGS += $(SOC_INCLUDES) $(SOC_DEFINES) $(SOC_CFLAGS)
ASFLAGS := $(ARCH_ASFLAGS) $(BOARD_INCLUDES) $(BOARD_DEFINES) $(SOC_INCLUDES) $(SOC_DEFINES)
# ──── Source files (paths updated for src/ layout) ────
#
# C sources now live in src/ subdirectories:
# common/string.c → common/src/string.c
# kernel/main.c → kernel/src/main.c
# drivers/.../fb.c → drivers/src/framebuffer/framebuffer.c
# memory/allocator.c → memory/src/allocator.c
# ui/widgets/ui_widgets.c → ui/src/widgets/ui_widgets.c
#
# Assembly and soc.mk paths are unchanged — they stay at crate root.
KERNEL_SOURCES := kernel/src/main.c
COMMON_SOURCES := common/src/string.c
ifneq ($(wildcard memory/src/allocator.c),)
MEMORY_SOURCES := memory/src/allocator.c
endif
DRIVER_SOURCES := drivers/src/framebuffer/framebuffer.c
ifneq ($(wildcard ui/src/widgets/ui_widgets.c),)
UI_SOURCES := ui/src/widgets/ui_widgets.c
endif
ALL_C_SOURCES := \
$(KERNEL_SOURCES) \
$(COMMON_SOURCES) \
$(MEMORY_SOURCES) \
$(DRIVER_SOURCES) \
$(UI_SOURCES)
# ──── Pico SDK: CMake handles everything ────
ifeq ($(BUILD_MODE),pico)
ALL_C_SOURCES :=
BOOT_SOURCES :=
SOC_SOURCES :=
HAL_SOURCES :=
endif
# ──── Object files ────
ASM_OBJECTS := $(patsubst %.S,$(BUILD_DIR)/%.o,$(BOOT_SOURCES))
SOC_C_OBJECTS := $(patsubst %.c,$(BUILD_DIR)/%.o,$(SOC_SOURCES))
HAL_C_OBJECTS := $(patsubst %.c,$(BUILD_DIR)/%.o,$(HAL_SOURCES))
OTHER_C_OBJECTS := $(patsubst %.c,$(BUILD_DIR)/%.o,$(ALL_C_SOURCES))
ALL_OBJECTS := $(ASM_OBJECTS) $(SOC_C_OBJECTS) $(HAL_C_OBJECTS) $(OTHER_C_OBJECTS)
# ──── Canonical output names ────
ELF := $(BUILD_DIR)/kernel.elf
BIN := $(BUILD_DIR)/$(KERNEL_NAME)
BUILD_OBJS := $(ALL_OBJECTS)
# =============================================================================
# Targets
# =============================================================================
.PHONY: all clean info disasm image print-%
all: $(BIN)
@echo ""
@echo "Build complete: $(BIN)"
@ls -lh $(BIN)
# ──── Binary output (ARM64 / RISC-V) ────
ifneq ($(BUILD_MODE),uefi)
ifneq ($(BUILD_MODE),pico)
$(BIN): $(ELF)
@echo " OBJCOPY $@"
@$(OBJCOPY) $< -O binary $@
endif
endif
# ──── PE/COFF EFI application (x86_64 UEFI) ────
ifeq ($(BUILD_MODE),uefi)
$(BIN): $(ELF)
@echo " EFI $@ (objcopy ELF → PE/COFF)"
@mkdir -p $(dir $@)
@$(OBJCOPY) \
-j .text \
-j .sdata \
-j .data \
-j .dynamic \
-j .dynsym \
-j .rel \
-j .rela \
-j .reloc \
--target=efi-app-x86_64 \
--subsystem=10 \
$< $@
@echo " Size: $$(wc -c < $@) bytes"
endif
# ──── ELF link (non-UEFI) ────
ifneq ($(BUILD_MODE),uefi)
ifneq ($(BUILD_MODE),pico)
$(ELF): $(BUILD_OBJS)
@echo " LD $@"
@mkdir -p $(dir $@)
$(LD) $(LDFLAGS) -T $(LINKER_SCRIPT) -o $@ $(BUILD_OBJS)
endif
endif
# ──── ELF link (UEFI: gnu-efi pipeline) ────
ifeq ($(BUILD_MODE),uefi)
$(ELF): $(BUILD_OBJS)
@echo " LD $@ [UEFI gnu-efi pipeline]"
@mkdir -p $(dir $@)
$(LD) \
-T $(LINKER_SCRIPT) \
-shared \
-Bsymbolic \
-znocombreloc \
$(GNUEFI_LIB)/crt0-efi-x86_64.o \
$(BUILD_OBJS) \
-o $@ \
-L$(GNUEFI_LIB) \
-lgnuefi
endif
# ──── Pico SDK (CMake substep) ────
PICO_CMAKE_BUILD := $(BUILD_DIR)/cmake-build
TARGET_PICO := tutorial-os-pico2
ifeq ($(BUILD_MODE),pico)
$(BIN): pico-build
@cp $(PICO_CMAKE_BUILD)/$(TARGET_PICO).uf2 $@
@echo ""
@echo "Build complete: $@"
@ls -lh $@
.PHONY: pico-build
pico-build:
@mkdir -p $(PICO_CMAKE_BUILD)
@cd $(PICO_CMAKE_BUILD) && cmake \
-DPICO_BOARD=$(PICO_BOARD) \
-DPICO_PLATFORM=$(PICO_PLATFORM) \
-DPICO_SDK_PATH=$(PICO_SDK_PATH) \
-DTUTORIAL_OS_ROOT=$(CURDIR) \
$(CURDIR)/soc/rp2350
@$(MAKE) -C $(PICO_CMAKE_BUILD) -j$$(nproc)
endif
# ──── Compilation rules ────
$(BUILD_DIR)/%.o: %.S
@echo " AS $<"
@mkdir -p $(dir $@)
@$(CC) $(ASFLAGS) -c $< -o $@
# Special rule for cache.S — needs Zicbom extensions (-march override)
ifdef CACHE_ASFLAGS
$(BUILD_DIR)/boot/riscv64/cache.o: boot/riscv64/cache.S
@echo " AS $< (Zicbom/Zicboz)"
@mkdir -p $(dir $@)
@$(CC) $(CACHE_ASFLAGS) -c $< -o $@
endif
$(BUILD_DIR)/%.o: %.c
@echo " CC $<"
@mkdir -p $(dir $@)
@$(CC) $(CFLAGS) -c $< -o $@
# ──── Image creation ────
image: $(BIN)
ifeq ($(BUILD_MODE),uefi)
python3 board/$(BOARD)/mkimage.py \
$(BIN) \
$(BUILD_DIR)/tutorial-os-lp-mu.img \
64
else
@if [ -x board/$(BOARD)/mkimage.sh ]; then \
BOARD=$(BOARD) SOC=$(SOC) BUILD_DIR=$(BUILD_DIR) board/$(BOARD)/mkimage.sh; \
else \
echo "No mkimage.sh for board $(BOARD)"; \
exit 1; \
fi
endif
# ──── Utility targets ────
clean:
@echo "Cleaning all build artifacts..."
@rm -rf build/
@cargo clean 2>/dev/null || true
@rm -rf output/
@echo "Done."
print-%:
@echo $($*)
info:
@echo "Language: C"
@echo "Board: $(BOARD)"
@echo "SoC: $(SOC)"
@echo "Toolchain: $(CROSS_COMPILE)"
@echo "Kernel: $(KERNEL_NAME)"
@echo ""
@echo "ARCH_CFLAGS: $(ARCH_CFLAGS)"
@echo "ARCH_ASFLAGS: $(ARCH_ASFLAGS)"
@echo ""
@echo "Boot sources:"
@for f in $(BOOT_SOURCES); do echo " $$f"; done
@echo ""
@echo "SoC sources:"
@for f in $(SOC_SOURCES); do echo " $$f"; done
@echo ""
@echo "HAL sources:"
@for f in $(HAL_SOURCES); do echo " $$f"; done
@echo ""
@echo "C sources:"
@for f in $(ALL_C_SOURCES); do echo " $$f"; done
@echo ""
@echo "Total objects: $(words $(ALL_OBJECTS))"
disasm: $(ELF)
@$(OBJDUMP) -d $< > $(BUILD_DIR)/kernel.list
@echo "Disassembly: $(BUILD_DIR)/kernel.list"
endif # LANG=c