-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
executable file
·72 lines (56 loc) · 1.7 KB
/
Makefile
File metadata and controls
executable file
·72 lines (56 loc) · 1.7 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
# Tools
AS = nasm
LD = ld
CC = gcc
# Flags
ASFLAGS = -f elf
LDFLAGS = -T src/linker.ld -m elf_i386
CFLAGS = -g -m32 -ffreestanding -O2 -Wall -Wextra -nostdinc -fno-builtin -fno-stack-protector -Isrc -Isrc/include -MMD -MP
# Paths
SRC = src
BUILD = build
# Auto-discover source files
C_SOURCES = $(shell find $(SRC) -name '*.c')
ASM_SOURCES = $(shell find $(SRC) -name '*.asm')
# Generate object file paths (strip src/, add build/ prefix, change extension)
C_OBJECTS = $(patsubst $(SRC)/%.c, $(BUILD)/%.o, $(C_SOURCES))
ASM_OBJECTS = $(patsubst $(SRC)/%.asm, $(BUILD)/%.o, $(ASM_SOURCES))
OBJECTS = $(ASM_OBJECTS) $(C_OBJECTS)
# Dependency files (auto-generated by compiler)
DEPS = $(C_OBJECTS:.o=.d)
# Output
KERNEL = $(BUILD)/kernel.elf
# Default target
all: $(KERNEL)
# Link kernel
$(KERNEL): $(OBJECTS)
$(LD) $(LDFLAGS) $^ -o $@
# Pattern rule: compile C files
$(BUILD)/%.o: $(SRC)/%.c
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -c $< -o $@
# Pattern rule: assemble .asm files
$(BUILD)/%.o: $(SRC)/%.asm
@mkdir -p $(dir $@)
$(AS) $(ASFLAGS) $< -o $@
# Include auto-generated dependencies (header file changes)
-include $(DEPS)
# Run normally
run: $(KERNEL)
qemu-system-i386 -kernel $(KERNEL) -m 64M -serial stdio
# Run with GDB
run-debug: $(KERNEL)
qemu-system-i386 -kernel $(KERNEL) -m 64M -serial stdio -nographic -s -S
# Clean build artifacts
clean:
rm -rf $(BUILD)
# Show discovered files (useful for debugging Makefile)
show-files:
@echo "C Sources:"
@echo "$(C_SOURCES)" | tr ' ' '\n'
@echo "\nASM Sources:"
@echo "$(ASM_SOURCES)" | tr ' ' '\n'
@echo "\nObjects:"
@echo "$(OBJECTS)" | tr ' ' '\n'
# Phony targets (not actual files)
.PHONY: all run run-debug clean show-files