-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
159 lines (127 loc) · 4 KB
/
Makefile
File metadata and controls
159 lines (127 loc) · 4 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
.DEFAULT_GOAL := all
NASM := nasm
GCC := i686-elf-gcc
LD := i686-elf-ld
QEMU := "qemu-system-i386"
INCLUDE_DIR=src/include
CFLAGS := -g -fno-asynchronous-unwind-tables -ffreestanding -masm=intel -MMD -MP -mgeneral-regs-only -I$(INCLUDE_DIR)
LDFLAGS := --oformat=binary
SRCDIR := src
BLDDIR := build
$(BLDDIR)/%.o: $(SRCDIR)/%.c
$(GCC) $(CFLAGS) -c $< -o $@
##
# boot sector
#
$(BLDDIR)/bootsect.bin: $(SRCDIR)/arch_x86/bootsect.asm
$(NASM) $< -o $@
##
# kernel
#
$(BLDDIR)/kernel/isr.o: $(SRCDIR)/kernel/isr.asm
$(NASM) -felf32 $< -o $@
$(BLDDIR)/arch_x86/task_switch.o: $(SRCDIR)/arch_x86/task_switch.asm
$(NASM) -felf32 $< -o $@
KERNEL_LDFLAGS := $(LDFLAGS) --entry=kmain # --print-map
KERNEL_SRCS = \
$(SRCDIR)/arch_x86/cpu.c \
$(SRCDIR)/arch_x86/gdt.c \
$(SRCDIR)/arch_x86/idt.c \
$(SRCDIR)/arch_x86/port.c \
$(SRCDIR)/device/ata.c \
$(SRCDIR)/device/bga.c \
$(SRCDIR)/device/console.c \
$(SRCDIR)/device/kbd.c \
$(SRCDIR)/device/keyboard.c \
$(SRCDIR)/device/pic.c \
$(SRCDIR)/device/pit.c \
$(SRCDIR)/kernel/event.c \
$(SRCDIR)/kernel/exceptions.c \
$(SRCDIR)/kernel/interrupt.c \
$(SRCDIR)/kernel/kernel.c \
$(SRCDIR)/kernel/loader.c \
$(SRCDIR)/kernel/scheduler.c \
$(SRCDIR)/kernel/task.c \
$(SRCDIR)/kernel/vector.c \
$(SRCDIR)/lib/blocking_queue.c \
$(SRCDIR)/lib/queue.c \
$(SRCDIR)/gui/font.c \
$(SRCDIR)/gui/gui.c \
$(SRCDIR)/lib/util.c \
$(SRCDIR)/shell/shell.c
KERNEL_OBJS = \
$(patsubst $(SRCDIR)/%.c, $(BLDDIR)/%.o, $(KERNEL_SRCS)) \
$(BLDDIR)/kernel/isr.o \
$(BLDDIR)/arch_x86/task_switch.o
KERNEL_DEPS = \
$(patsubst $(SRCDIR)/%.c, $(BLDDIR)/%.d, $(KERNEL_SRCS))
$(BLDDIR)/kernel.bin: $(KERNEL_OBJS) $(SRCDIR)/kernel/kernel.ld
$(LD) $(KERNEL_LDFLAGS) $(KERNEL_OBJS) -T $(SRCDIR)/kernel/kernel.ld -o $@
$(shell mkdir -p $(dir $(KERNEL_OBJS)) >/dev/null)
##
# tasks
#
$(BLDDIR)/task_a.bin: $(BLDDIR)/tasks/task_a.o $(SRCDIR)/tasks/task.ld
$(LD) $(LDFLAGS) $< -T $(SRCDIR)/tasks/task.ld -o $@
$(BLDDIR)/task_b.bin: $(BLDDIR)/tasks/task_b.o $(SRCDIR)/tasks/task.ld
$(LD) $(LDFLAGS) $< -T $(SRCDIR)/tasks/task.ld -o $@
$(shell mkdir -p $(BLDDIR)/tasks >/dev/null)
##
# OS image
#
# Disk layout:
# Sector 0: Boot sector (512 bytes)
# Sector 1: File table (512 bytes) - generated at build time
# Sector 2+: Kernel
# After kernel: Tasks, fonts, etc.
# Calculate sector positions dynamically
KERNEL_START_SECTOR := 2
KERNEL_SECTORS = $$(( $$(stat -f%z $(BLDDIR)/kernel.bin) / 512 ))
$(BLDDIR)/filetable.bin: $(BLDDIR)/tools/gen_filetable $(BLDDIR)/kernel.bin $(BLDDIR)/task_a.bin $(BLDDIR)/task_b.bin
@TASK_A_SECTOR=$$(( $(KERNEL_START_SECTOR) + $$(stat -f%z $(BLDDIR)/kernel.bin) / 512 )); \
TASK_B_SECTOR=$$(( $$TASK_A_SECTOR + 1 )); \
echo "Generating file table: task_a=$$TASK_A_SECTOR, task_b=$$TASK_B_SECTOR"; \
$(BLDDIR)/tools/gen_filetable $@ "task_a:$$TASK_A_SECTOR" "task_b:$$TASK_B_SECTOR"
$(BLDDIR)/os.img: \
$(BLDDIR)/bootsect.bin \
$(BLDDIR)/filetable.bin \
$(BLDDIR)/kernel.bin \
$(BLDDIR)/task_a.bin \
$(BLDDIR)/task_b.bin \
resources/fonts/screen7x14.fon
cat $^ > $@
##
# Tools
$(BLDDIR)/tools/parse_fon: tools/parse_fon.c
gcc $< -o $@
$(BLDDIR)/tools/gen_filetable: tools/gen_filetable.c
gcc $< -o $@
##
# other targets
#
.PHONY: all run clean
all: $(BLDDIR)/os.img
run: $(BLDDIR)/os.img
$(QEMU) \
-nic none \
-drive file=$<,format=raw \
-no-reboot \
-display curses
run-window: $(BLDDIR)/os.img
$(QEMU) \
-nic none \
-drive file=$<,format=raw \
-no-reboot & \
osascript -e 'tell application "System Events" to set frontmost of (first process whose name contains "qemu") to true'
clean:
$(RM) \
$(KERNEL_OBJS) \
$(KERNEL_DEPS) \
$(BLDDIR)/tasks/task_*.o \
$(BLDDIR)/tasks/task_*.d \
$(BLDDIR)/*.bin \
$(BLDDIR)/os.img \
$(BLDDIR)/tools/gen_filetable
tools: $(BLDDIR)/tools/parse_fon
$(shell mkdir -p $(BLDDIR)/tools >/dev/null)
-include $(deps)