-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
193 lines (154 loc) · 5.43 KB
/
Makefile
File metadata and controls
193 lines (154 loc) · 5.43 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
# ==============================================================================
# PyramidOS Master Makefile (Restructured)
# ==============================================================================
# --- Toolchain Configuration ---
CC = i686-elf-gcc
LD = i686-elf-ld
OBJCOPY = i686-elf-objcopy
# Fallback to native GCC with 32-bit flags if cross-compiler is missing
ifeq ($(shell which $(CC) 2>/dev/null),)
CC = gcc
LD = ld
OBJCOPY = objcopy
CFLAGS_EXTRA = -m32
LDFLAGS_EXTRA = -melf_i386
endif
ASM = nasm
# --- Directories ---
BUILD_DIR = build
BOOT_DIR = boot/src/legacy
KERNEL_DIR = kernel
# Subdirectories for Source
ARCH_DIR = $(KERNEL_DIR)/arch/i386
CORE_DIR = $(KERNEL_DIR)/core
DRIVERS_DIR = $(KERNEL_DIR)/drivers
LIB_DIR = $(KERNEL_DIR)/lib
FS_DIR = $(KERNEL_DIR)/fs
# Define include paths for all modules to find headers
INCLUDES = -I$(KERNEL_DIR) \
-I$(ARCH_DIR) \
-I$(CORE_DIR) \
-I$(DRIVERS_DIR) \
-I$(LIB_DIR)
# --- Targets ---
KERNEL_ELF = $(BUILD_DIR)/kernel.elf
KERNEL_BIN = $(BUILD_DIR)/kernel.bin
KERNEL_HDR = $(BUILD_DIR)/kernel.hdr
KERNEL_IMG = $(BUILD_DIR)/kernel.img
STAGE1_BIN = $(BUILD_DIR)/stage1.bin
STAGE2_BIN = $(BUILD_DIR)/stage2.bin
DISK_IMG = $(BUILD_DIR)/pyramidos.img
KERNEL_MAP = $(BUILD_DIR)/kernel.map
# Stage 2 grew to include an Arabic-capable bitmap font + shaping tables.
# Keep it well below the kernel placement at LBA 60.
STAGE2_SECTORS ?= 32
# --- Build Profile ---
# BUILD=release (default) or BUILD=debug
BUILD ?= release
# STRICT=1 enables -Werror (opt-in until the codebase is fully warning-clean)
STRICT ?= 0
# --- Flags ---
CFLAGS_COMMON = $(CFLAGS_EXTRA) \
-ffreestanding \
-std=gnu11 \
-Wall -Wextra \
-fno-pie -fno-pic \
-fno-stack-protector \
-fno-builtin \
$(INCLUDES)
CFLAGS_release = -O2
CFLAGS_debug = -O0 -g3 -ggdb -DDEBUG -fno-omit-frame-pointer
CFLAGS = $(CFLAGS_COMMON) $(CFLAGS_$(BUILD))
ifeq ($(STRICT),1)
CFLAGS += -Werror
endif
NASMFLAGS = -f elf32
ifeq ($(BUILD),debug)
NASMFLAGS += -g -F dwarf
endif
LDFLAGS = $(LDFLAGS_EXTRA) -T $(ARCH_DIR)/linker.ld -Map $(KERNEL_MAP)
# ==============================================================================
# Build Rules
# ==============================================================================
.PHONY: all clean run debug release
all: $(DISK_IMG)
debug: BUILD=debug
debug: $(DISK_IMG)
release: BUILD=release
release: $(DISK_IMG)
# Ensure build directory exists
$(BUILD_DIR):
@mkdir -p $@
# --- Source Discovery (VPATH) ---
# Tell Make to look for .c and .asm files in these directories
vpath %.c $(CORE_DIR) $(DRIVERS_DIR) $(LIB_DIR) $(FS_DIR) $(ARCH_DIR)
vpath %.asm $(ARCH_DIR)
# --- Compile C Sources ---
$(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)
$(CC) $(CFLAGS) -c $< -o $@
# --- Compile ASM Sources ---
$(BUILD_DIR)/%.o: %.asm | $(BUILD_DIR)
$(ASM) $(NASMFLAGS) $< -o $@
# --- Special ASM rules for Bootloader ---
$(STAGE1_BIN): $(BOOT_DIR)/stage1.asm | $(BUILD_DIR)
$(ASM) -f bin -D STAGE2_SECTOR_COUNT=$(STAGE2_SECTORS) $< -o $@
$(STAGE2_BIN): $(BOOT_DIR)/stage2.asm | $(BUILD_DIR)
$(ASM) -f bin -I$(BOOT_DIR)/ -D STAGE2_SECTOR_COUNT=$(STAGE2_SECTORS) $< -o $@
# --- Link Kernel ---
# Note: entry.o MUST be first
OBJECTS = $(BUILD_DIR)/entry.o \
$(BUILD_DIR)/main.o \
$(BUILD_DIR)/string.o \
$(BUILD_DIR)/pmm.o \
$(BUILD_DIR)/idt.o \
$(BUILD_DIR)/idt_asm.o \
$(BUILD_DIR)/vmm.o \
$(BUILD_DIR)/pic.o \
$(BUILD_DIR)/keyboard.o \
$(BUILD_DIR)/terminal.o \
$(BUILD_DIR)/shell.o \
$(BUILD_DIR)/selftest.o \
$(BUILD_DIR)/debug.o \
$(BUILD_DIR)/timer.o \
$(BUILD_DIR)/rtc.o \
$(BUILD_DIR)/heap.o \
$(BUILD_DIR)/ata.o \
$(BUILD_DIR)/block.o \
$(BUILD_DIR)/ata_block.o \
$(BUILD_DIR)/mbr.o \
$(BUILD_DIR)/vfs.o \
$(BUILD_DIR)/nullfs.o \
$(BUILD_DIR)/devfs.o \
$(BUILD_DIR)/pyfs.o
$(KERNEL_BIN): $(OBJECTS)
$(LD) $(LDFLAGS) -o $(KERNEL_ELF) $^
$(OBJCOPY) -O binary $(KERNEL_ELF) $@
# 5. Generate Header (Calculates size dynamically)
$(KERNEL_HDR): $(KERNEL_BIN)
@SIZE=$$(stat -c%s $(KERNEL_BIN) 2>/dev/null || stat -f%z $(KERNEL_BIN)); \
CHECKSUM=$$(python3 -c "import sys; print(sum(open(sys.argv[1],'rb').read()) & 0xffffffff)" "$(KERNEL_BIN)" 2>/dev/null || \
python -c "import sys; print(sum(open(sys.argv[1],'rb').read()) & 0xffffffff)" "$(KERNEL_BIN)" 2>/dev/null || \
echo 0); \
echo "Kernel Size: $$SIZE bytes"; \
echo "Kernel Checksum: $$CHECKSUM"; \
$(ASM) -f bin $(ARCH_DIR)/header.asm -o $@ -D KERNEL_SIZE=$$SIZE -D KERNEL_CHECKSUM=$$CHECKSUM
# 6. Create Final Kernel Image (Header + Binary)
$(KERNEL_IMG): $(KERNEL_HDR) $(KERNEL_BIN)
cat $^ > $@
# --- Disk Image Construction ---
$(DISK_IMG): $(STAGE1_BIN) $(STAGE2_BIN) $(KERNEL_IMG)
@echo "--- Assembling Disk Image ---"
# 1. Create blank 1.44MB image (2880 sectors * 512 bytes)
dd if=/dev/zero of=$@ bs=512 count=2880 status=none
# 2. Write Stage 1 (Sector 0)
dd if=$(STAGE1_BIN) of=$@ bs=512 count=1 conv=notrunc status=none
# 3. Write Stage 2 (Sector 1)
dd if=$(STAGE2_BIN) of=$@ bs=512 seek=1 conv=notrunc status=none
# 4. Write Kernel (Sector 60)
dd if=$(KERNEL_IMG) of=$@ bs=512 seek=60 conv=notrunc status=none
@echo "Build Complete: $@"
# --- Utilities ---
clean:
rm -rf $(BUILD_DIR)
run: $(DISK_IMG)
qemu-system-i386 -drive format=raw,file=$(DISK_IMG)