-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
71 lines (54 loc) · 1.89 KB
/
Makefile
File metadata and controls
71 lines (54 loc) · 1.89 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
# Custom OS Makefile
# This Makefile allows building and running the OS on Linux, Mac, or WSL
# without needing the Windows-specific build.bat script.
# Compilers and Linkers
# If you have an i686-elf cross-compiler in your PATH, it will be used.
# Otherwise, it falls back to standard gcc (with 32-bit flags) if available.
CC = i686-elf-gcc
LD = i686-elf-ld
ASM = nasm
# Emulator
QEMU = qemu-system-x86_64
# Flags
CFLAGS = -O2 -ffreestanding -m32 -fno-pie -fno-builtin
# Note: On native Linux gcc, you might need -m32 for the cross assembler/linker if not using i686-elf
# LDFLAGS = -m elf_i386 -Ttext 0x8000 --oformat binary
LDFLAGS = -Ttext 0x8000 --oformat binary
# Source Directories
SRC_DIR = src
INC_DIR = include
BUILD_DIR = build
# Output Files
OS_IMAGE = os-image.bin
KERNEL_BIN = kernel.bin
BOOT_BIN = bootsect.bin
# Source Files
C_SRCS := $(wildcard $(SRC_DIR)/*.c)
OBJ_FILES := $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(C_SRCS))
# Assembly Source Files
ASM_SRCS := $(SRC_DIR)/interrupt.asm $(SRC_DIR)/gdt_flush.asm
ASM_OBJS := $(patsubst $(SRC_DIR)/%.asm, $(BUILD_DIR)/%.o, $(ASM_SRCS))
.PHONY: all clean run
all: $(OS_IMAGE)
$(OS_IMAGE): $(BUILD_DIR)/$(BOOT_BIN) $(BUILD_DIR)/$(KERNEL_BIN)
cat $^ > $@
$(BUILD_DIR)/$(KERNEL_BIN): $(BUILD_DIR)/kernel_entry.o $(ASM_OBJS) $(OBJ_FILES)
$(LD) -m elf_i386 -o $@ $(LDFLAGS) $^
$(BUILD_DIR)/$(BOOT_BIN): $(SRC_DIR)/bootsect.asm
@mkdir -p $(BUILD_DIR)
$(ASM) -I$(SRC_DIR)/ -f bin $< -o $@
$(BUILD_DIR)/kernel_entry.o: $(SRC_DIR)/kernel_entry.asm
@mkdir -p $(BUILD_DIR)
$(ASM) -f elf $< -o $@
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.asm
@mkdir -p $(BUILD_DIR)
$(ASM) -f elf $< -o $@
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
@mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) -c $< -o $@
run: $(OS_IMAGE)
@if [ ! -f disk.img ]; then qemu-img create -f raw disk.img 10M; fi
$(QEMU) -fda $< -hda disk.img -display gtk,zoom-to-fit=on
clean:
rm -rf $(BUILD_DIR)
rm -f $(OS_IMAGE)