-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
102 lines (77 loc) · 2.95 KB
/
Makefile
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
ASM = nasm
ASMFLAGS = -f bin
CC = i686-elf-gcc
CFLAGS = -ffreestanding -nostdlib -m32 -mgeneral-regs-only -I./src/stage2/include -g
LD = i686-elf-ld
LDFLAGS = -T $(KERNELDIR)/link.ld $(KERNELDIR)/loader.o -Map map.map
SRC = ./src
BOOTDIR = $(SRC)/boot
KERNELDIR = $(SRC)/kernel
INCLUDE_DIR = $(KERNELDIR)/include
LIBCDIR = $(INCLUDE_DIR)/libc
K_CFILES = $(wildcard $(KERNELDIR)/*.c)
K_OFILES = $(patsubst %.c, %.o, $(K_CFILES))
K_ASMFILES = $(wildcard $(KERNELDIR)/*.asm)
LIBC_CFILES = $(wildcard $(LIBCDIR)/*.c)
LIBC_OFILES = $(patsubst %.c, %.o, $(LIBC_CFILES))
I_CFILES = $(wildcard $(INCLUDE_DIR)/*.c)
I_OFILES = $(patsubst %.c, %.o, $(I_CFILES))
M_CFILES = $(wildcard $(INCLUDE_DIR)/memory/*.c)
M_OFILES = $(patsubst %.c, %.o, $(M_CFILES))
DRIVER_DIR = $(KERNELDIR)/drivers
D_CFILES = $(wildcard $(DRIVER_DIR)/*.c)
D_OFILES = $(patsubst %.c, %.o, $(D_CFILES))
BOOT_SRC = $(wildcard $(BOOTDIR)/*.asm)
BOOT_BIN = $(patsubst %.asm, %.bin, $(BOOT_SRC))
KERNEL_BIN = $(KERNELDIR)/kernel.bin
KERNEL_ELF = $(KERNELDIR)/kernel.elf
DISK_FILE = disk.img
.PHONY: all clean
all: $(DISK_FILE)
$(DISK_FILE): $(BOOT_BIN) $(KERNEL_BIN)
@echo "Generating disk.img file..."
@dd if=/dev/zero of=$@ bs=512 count=2880
@dd if=./src/boot/boot.bin of=$@ bs=512 conv=notrunc
@dd if=./src/boot/stage2.bin of=$@ bs=512 conv=notrunc seek=1
@dd if=$(KERNEL_BIN) of=$@ bs=512 conv=notrunc seek=2
#@dd if=./test.vga of=$@ bs=512 conv=notrunc seek=69
@size=$$(wc -c < $(KERNEL_BIN));\
SECTORS=$$(((size + 511) / 512));\
echo "$@" "0x$$(printf '%X' $$SECTORS)"
$(BOOTDIR)/%.bin: $(BOOTDIR)/%.asm
@echo "ASM $<"
@$(ASM) $(ASMFLAGS) -o $@ $<
$(KERNEL_BIN): $(K_OFILES) $(KERNELDIR)/loader.o $(LIBC_OFILES) $(I_OFILES) $(D_OFILES) $(M_OFILES)
@echo "LD $<"
@$(LD) $(LDFLAGS) $(K_OFILES) $(LIBC_OFILES) $(I_OFILES) $(D_OFILES) $(M_OFILES) -o $(KERNEL_ELF)
i686-elf-objcopy -O binary $(KERNEL_ELF) $@
$(LIBCDIR)/%.o: $(LIBCDIR)/%.c
@echo "CC $<"
@$(CC) $(CFLAGS) -c $< -o $@
$(KERNELDIR)/%.o: $(KERNELDIR)/%.c
@echo "CC $<"
@$(CC) $(CFLAGS) -c $< -o $@
$(INCLUDE_DIR)/%.o: $(INCLUDE_DIR)/%.c
@echo "CC $<"
@$(CC) $(CFLAGS) -c $< -o $@
$(INCLUDE_DIR)/memory/%.o: $(INCLUDE_DIR)/memory/%.c
@echo "CC $<"
@$(CC) $(CFLAGS) -c $< -o $@
$(KERNELDIR)/loader.o: $(K_ASMFILES)
@echo "ASM $<"
@$(ASM) -f elf $< -o $@
$(DRIVER_DIR)/%.o: $(DRIVER_DIR)/%.c
@echo "CC $<"
@$(CC) $(CFLAGS) -c $< -o $@
clean:
@echo "Clean up..."
@rm -f $(BOOT_BIN) $(DISK_FILE) $(KERNEL_BIN) $(LIBC_OFILES) $(K_OFILES) $(I_OFILES) $(KERNELDIR)/loader.o serial.log
cleanup:
@echo "Clean up object files..."
rm -f $(BOOT_BIN) $(KERNEL_BIN) $(LIBC_OFILES) $(K_OFILES) $(I_OFILES) $(KERNELDIR)/loader.o
run: $(DISK_FILE)
qemu-system-i386 -drive file=disk.img,format=raw -audiodev pa,id=speaker -machine pcspk-audiodev=speaker -serial file:serial.log
runb: $(DISK_FILE)
bochs -f bochsrc
debug: $(DISK_FILE)
qemu-system-i386 -drive file=disk.img,format=raw -audiodev pa,id=speaker -machine pcspk-audiodev=speaker -S -gdb tcp::1234