-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
73 lines (57 loc) · 1.84 KB
/
Copy pathMakefile
File metadata and controls
73 lines (57 loc) · 1.84 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
# Toolchain
ASM := nasm
LD := ld
QEMU := qemu-system-x86_64
OBJCOPY := objcopy
# Target Triple
TARGET := x86_64-unknown-none
# Directories
BOOT_DIR := boot
SRC_DIR := src
BUILD_DIR := build
# Files
BOOT_ASM := $(BOOT_DIR)/boot.asm
LINKER := linker.ld
# Object and Library files
BOOT_OBJ := $(BUILD_DIR)/boot.o
KERNEL_ELF := $(BUILD_DIR)/kernel.elf
# This is the static library produced by Cargo (crate-type = ["staticlib"])
KERNEL_LIB_CARGO := target/$(TARGET)/release/libkernel.a
OS_IMAGE := boot.bin
# Linker flags
# We link the bootloader object and the Rust static library together
LD_FLAGS := -m elf_x86_64 -T $(LINKER) -Map $(BUILD_DIR)/kernel.map
# Colors for output
GREEN := \033[0;32m
YELLOW := \033[1;33m
NC := \033[0m
.PHONY: all clean run
all: $(BUILD_DIR) $(OS_IMAGE)
@echo "$(GREEN)✓ Build complete: $(OS_IMAGE)$(NC)"
$(BUILD_DIR):
mkdir -p $@
# Rule to assemble the bootloader
$(BOOT_OBJ): $(BOOT_ASM) | $(BUILD_DIR)
@echo "$(YELLOW)→ Assembling bootloader...$(NC)"
$(ASM) -f elf64 $< -o $@
# Rule to compile the Rust kernel into a static library
$(KERNEL_LIB_CARGO):
@echo "$(YELLOW)→ Compiling Rust kernel with Cargo...$(NC)"
cargo build --release --target $(TARGET)
# Rule to link bootloader and Rust library into an ELF file
$(KERNEL_ELF): $(BOOT_OBJ) $(KERNEL_LIB_CARGO) $(LINKER)
@echo "$(YELLOW)→ Linking kernel with bootloader...$(NC)"
$(LD) $(LD_FLAGS) $(BOOT_OBJ) $(KERNEL_LIB_CARGO) -o $@
# Rule to create the final bootable binary image
$(OS_IMAGE): $(KERNEL_ELF)
@echo "$(YELLOW)→ Creating boot image...$(NC)"
$(OBJCOPY) -O binary $< $@
run: $(OS_IMAGE)
@echo "$(YELLOW)→ Starting QEMU...$(NC)"
$(QEMU) -drive format=raw,file=$(OS_IMAGE)
clean:
@echo "$(YELLOW)→ Cleaning project...$(NC)"
rm -rf $(BUILD_DIR) $(OS_IMAGE)
cargo clean
@echo "$(GREEN)✓ Clean complete$(NC)"
.DEFAULT_GOAL := all