-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
76 lines (61 loc) · 2 KB
/
Copy pathMakefile
File metadata and controls
76 lines (61 loc) · 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
AS=i686-elf-as
CC=i686-elf-gcc
BUILD_DIR=build
KERNEL_DIR=kernel
GRUB_FILE=i686-elf-grub-file
GRUB_MKRESCUE=i686-elf-grub-mkrescue
# Find all .c files in the kernel directory
C_SOURCES=$(wildcard $(KERNEL_DIR)/*.c) $(wildcard $(KERNEL_DIR)/commands/*.c)
# Convert the list of .c files into a list of .o files in the build directory
OBJS=$(patsubst $(KERNEL_DIR)/%.c, $(BUILD_DIR)/%.o, $(C_SOURCES))
OBJS += $(BUILD_DIR)/boot.o
# Output
KERNEL_BIN=oopsos.bin
ISO=OopsOs.iso
# Flags
CFLAGS=-ffreestanding -m32 -O2 -Wall -Wextra -nostdlib
LDFLAGS=-T linker.ld -ffreestanding -m32 -nostdlib
all: $(ISO)
$(BUILD_DIR):
mkdir -p $(BUILD_DIR)
# Assemble bootloader
$(BUILD_DIR)/boot.o: boot.s | $(BUILD_DIR)
$(AS) boot.s -o $(BUILD_DIR)/boot.o
# Compiles ANY .c file in kernel/ into a .o file in build/
$(BUILD_DIR)/%.o: $(KERNEL_DIR)/%.c
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -c $< -o $@
# Link kernel + bootloader
$(KERNEL_BIN): $(OBJS)
$(CC) $(LDFLAGS) -o $(KERNEL_BIN) $(OBJS)
@echo "Checking multiboot..."
@if $(GRUB_FILE) --is-x86-multiboot $(KERNEL_BIN) || $(GRUB_MKRESCUE) --is-x86-multiboot $(KERNEL_BIN); then \
echo "Multiboot confirmed"; \
else \
echo "ERROR: The file is not multiboot"; exit 1; \
fi
$(ISO): $(KERNEL_BIN)
@mkdir -p iso/boot/grub
@cp $(KERNEL_BIN) iso/boot/
@echo 'menuentry "OopsOs" { multiboot /boot/oopsos.bin }' > iso/boot/grub/grub.cfg
$(GRUB_MKRESCUE) -o $(ISO) iso/ || grub2-mkrescue -o $(ISO) iso/
run: $(ISO)
qemu-system-i386 -cdrom $(ISO) -m 1024 -boot d -vga std
# Usage: make write DEVICE=/dev/sdX
write: $(ISO)
ifeq ($(DEVICE),)
$(error You must specify DEVICE, e.g., make write DEVICE=/dev/sdX)
endif
@echo "WARNING: This will erase all data on $(DEVICE)!"
@bash -c '\
read -p "Are you sure? (Y/n): " confirm; \
if [ "$$confirm" != "" ]; then \
echo "Aborted."; \
else \
echo "Writing $(ISO) to $(DEVICE)..."; \
sudo dd if=$(ISO) of=$(DEVICE) bs=4M status=progress conv=fsync; \
sync; \
echo "Done!"; \
fi'
clean:
rm -rf $(BUILD_DIR) $(KERNEL_BIN) $(ISO) iso