-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
154 lines (124 loc) · 4.54 KB
/
Makefile
File metadata and controls
154 lines (124 loc) · 4.54 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
# Cross-compiler setup
CC = i686-elf-gcc
CXX = i686-elf-g++
AS = i686-elf-as
# Directories
SRC_DIR = src
INCLUDE_DIR = include
BUILD_DIR = build
BOOT_DIR = boot
KERNEL_DIR = $(SRC_DIR)/kernel
KERNEL_DEST = ./kernel
LIBC_DIR = libc
# Create kernel directory if it doesn't exist
$(shell mkdir -p $(KERNEL_DEST))
# Compiler flags
COMMON_FLAGS = -O2 -g -ffreestanding -Wall -Wextra -I$(INCLUDE_DIR) -I$(LIBC_DIR)/include -DDEBUG -DTEST
CFLAGS = $(COMMON_FLAGS) -std=gnu99
CFLAGS += $(EXTRA_CFLAGS)
CXXFLAGS = $(COMMON_FLAGS) -fno-exceptions -fno-rtti
CXXFLAGS += $(EXTRA_CFLAGS)
LDFLAGS = -ffreestanding -O2 -nostdlib
# Source files (exclude toolchain build directories)
CSOURCES = $(shell find $(SRC_DIR) -name '*.c' ! -path "*/binutils-*" ! -path "*/gcc-*" ! -path "*/build-*")
CPPSOURCES = $(shell find $(SRC_DIR) -name '*.cpp' ! -path "*/binutils-*" ! -path "*/gcc-*" ! -path "*/build-*")
ASMSOURCES = $(shell find $(BOOT_DIR) -name '*.s')
KERNEL_ASMSOURCES = $(shell find $(KERNEL_DIR) -name '*.s')
LIBC_CSOURCES = $(shell find $(LIBC_DIR) -type f -name '*.c')
LIBC_CPPSOURCES = $(shell find $(LIBC_DIR) -type f -name '*.cpp')
# Object files
KERNEL_OBJS = $(patsubst $(KERNEL_DIR)/%.s,$(BUILD_DIR)/kernel/%.o,$(KERNEL_ASMSOURCES))
BOOT_OBJS = $(patsubst $(BOOT_DIR)/%.s,$(BUILD_DIR)/boot/%.o,$(ASMSOURCES))
C_OBJS = $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/%.o,$(CSOURCES))
CPP_OBJS = $(patsubst $(SRC_DIR)/%.cpp,$(BUILD_DIR)/%.o,$(CPPSOURCES))
LIBC_C_OBJS = $(patsubst $(LIBC_DIR)/%,$(BUILD_DIR)/libc/%,$(LIBC_CSOURCES:.c=.o))
LIBC_CPP_OBJS = $(patsubst $(LIBC_DIR)/%,$(BUILD_DIR)/libc/%,$(LIBC_CPPSOURCES:.cpp=.o))
OBJECTS = $(sort $(BOOT_OBJS) $(C_OBJS) $(CPP_OBJS) $(KERNEL_OBJS) $(LIBC_C_OBJS) $(LIBC_CPP_OBJS))
# Output files
KERNEL_ELF = kernel/kernel.bin
# QEMU configuration
QEMU = qemu-system-i386
QEMU_FLAGS = -kernel $(KERNEL_ELF) -serial stdio
.PHONY: all clean clean-all resetimg run directories iso debug runiso release runrelease rundebug
all: directories $(KERNEL_ELF)
directories:
@mkdir -p $(BUILD_DIR)
@mkdir -p $(BUILD_DIR)/kernel
@mkdir -p $(BUILD_DIR)/boot
@mkdir -p $(BUILD_DIR)/libc
@mkdir -p $(BUILD_DIR)/libc/stdlib
$(KERNEL_ELF): $(OBJECTS)
$(CXX) -T linker.ld -o $(KERNEL_DEST)/kernel.bin $(LDFLAGS) $(OBJECTS) -lgcc
# grub-file --is-x86-multiboot $(KERNEL_DEST)/kernel.bin
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp
@mkdir -p $(dir $@)
$(CXX) -c $< -o $@ $(CXXFLAGS)
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
@mkdir -p $(dir $@)
$(CC) -c $< -o $@ $(CFLAGS)
$(BUILD_DIR)/boot/%.o: $(BOOT_DIR)/%.s
@mkdir -p $(dir $@)
$(AS) $< -o $@
$(BUILD_DIR)/kernel/%.o: $(KERNEL_DIR)/%.s
@mkdir -p $(dir $@)
$(AS) $< -o $@
# Update libc compilation rules
$(BUILD_DIR)/libc/%.o: $(LIBC_DIR)/%.cpp
@mkdir -p $(dir $@)
@echo "Compiling C++: $<"
$(CXX) -c $< -o $@ $(CXXFLAGS)
$(BUILD_DIR)/libc/%.o: $(LIBC_DIR)/%.c
@mkdir -p $(dir $@)
@echo "Compiling C: $<"
$(CC) -c $< -o $@ $(CFLAGS)
iso: $(KERNEL_ELF)
mkdir -p isodir/boot/grub
cp $(KERNEL_DEST)/kernel.bin isodir/boot/kernel.bin
cp grub.cfg isodir/boot/grub/grub.cfg
@if [ -f /usr/share/grub/unicode.pf2 ]; then \
mkdir -p isodir/boot/grub/fonts; \
cp /usr/share/grub/unicode.pf2 isodir/boot/grub/fonts/unicode.pf2; \
elif [ -f /usr/share/grub/fonts/unicode.pf2 ]; then \
mkdir -p isodir/boot/grub/fonts; \
cp /usr/share/grub/fonts/unicode.pf2 isodir/boot/grub/fonts/unicode.pf2; \
fi
grub-mkrescue -o kernel.iso isodir
# Build ISO with debug/test logging disabled for release workflows
release:
$(MAKE) clean
$(MAKE) iso EXTRA_CFLAGS="-UDEBUG -UTEST"
rundebug :
$(MAKE) clean
$(MAKE)
$(QEMU) $(QEMU_FLAGS) -drive file=test_fat32.img,format=raw,if=ide
run: $(KERNEL_ELF) test_fat32.img
$(QEMU) $(QEMU_FLAGS) -drive file=test_fat32.img,format=raw,if=ide
runrelease :
$(MAKE) clean
$(MAKE) EXTRA_CFLAGS="-UDEBUG -UTEST"
$(QEMU) $(QEMU_FLAGS) -drive file=test_fat32.img,format=raw,if=ide
runiso: iso
$(MAKE) iso EXTRA_CFLAGS="-UDEBUG -UTEST"
$(QEMU) $(QEMU_FLAGS) -cdrom kernel.iso
clean:
rm -rf $(BUILD_DIR)
rm -rf isodir
rm -f kernel.iso
rm -f $(KERNEL_DEST)/kernel.bin
rm -f $(KERNEL_DEST)/kernel2.bin
resetimg:
rm -f test_fat32.img
clean-all: clean resetimg
# Create test FAT32 disk image
test_fat32.img: fat32_template.img
@echo "Creating test FAT32 disk image from template..."
cp fat32_template.img test_fat32.img
@echo "Test FAT32 disk image created successfully"
fat32_template.img:
@echo "Template image missing; rebuilding..."
./scripts/build_fat32_template.sh $@
# Add debug target
debug:
$(MAKE) clean
$(MAKE) CFLAGS="$(CFLAGS) -O0 -g" ASFLAGS="--32"
qemu-system-i386 -S -s -kernel kernel/kernel.bin &