-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmakefile
131 lines (102 loc) · 4.52 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
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
# build os
# vars
ASM = nasm
ASMFLAGS = -I $(BOOTLOADER_HEADER_PATH)
ASMFLAGS_BUILD_COM = -I $(BOOTLOADER_HEADER_PATH) -D _BUILD_COM_
ASM_KERNEL_FLAGS = -f elf -I $(KERNEL_HEADER_ASM_PATH)
CC = gcc
CC_KERNEL_FLAGS = -c -I $(KERNEL_HEADER_PATH) -m32 -fno-stack-protector
CC_DEPENDENCY_FLAGS = -M -I $(KERNEL_HEADER_PATH)
LINK = ld
LINK_FLAGS = -m elf_i386 -Ttext 0x30400
BOOTLOADER_HEADER_PATH = ./boot/include/
KERNEL_PATH = ./kernel
KERNEL_HEADER_PATH = ./include
KERNEL_HEADER_ASM_PATH = ./include/
KERNEL_LIB_PATH = ./lib
OUTPUT_PATH = ./build
MOUNT_POINT = $(OUTPUT_PATH)/mount_point/
BOCHS_CFG = ./boot.bxrc
BOCHS = bochs
TARGET = os.img
DEPENDENCY_SRC = $(KERNEL_PATH)/start.c $(KERNEL_PATH)/init8259a.c $(KERNEL_PATH)/global.c $(KERNEL_PATH)/kernel.c \
$(KERNEL_PATH)/protect_mode.c $(KERNEL_LIB_PATH)/io.c $(KERNEL_LIB_PATH)/proc.c $(KERNEL_LIB_PATH)/utils.c
KERNEL_MODS_OUTPUT = $(OUTPUT_PATH)/kernel.o $(OUTPUT_PATH)/start.o $(OUTPUT_PATH)/init8259a.o $(OUTPUT_PATH)/global.o \
$(OUTPUT_PATH)/protect_mode.o $(OUTPUT_PATH)/proc.o $(OUTPUT_PATH)/kernel_main.o
LIBS_OUTPUT = $(OUTPUT_PATH)/string.o $(OUTPUT_PATH)/ioa.o $(OUTPUT_PATH)/io.o $(OUTPUT_PATH)/utils.o
IMGS_MODS_OUTPUT = $(OUTPUT_PATH)/boot.bin $(OUTPUT_PATH)/loader.bin $(OUTPUT_PATH)/kernel.bin
everything : rm_img $(OUTPUT_PATH) $(TARGET)
boot.com : ./boot/boot.asm
rm -rf $(OUTPUT_PATH)/$@
$(ASM) $(ASMFLAGS_BUILD_COM) -o $(OUTPUT_PATH)/$@ $<
rm_img :
rm -f $(OUTPUT_PATH)/os.img
clean :
rm -rf $(OUTPUT_PATH)
all : clean everything
run : boot_bochs
boot_bochs : everything
type $(BOCHS) >/dev/null 2>&1 || { echo "bochs not found"; exit 1; }
$(BOCHS) -f $(BOCHS_CFG)
dependency : $(DEPENDENCY_SRC)
$(CC) $(CC_DEPENDENCY_FLAGS) $^
$(OUTPUT_PATH) :
mkdir -p $(OUTPUT_PATH)
# img mods
$(OUTPUT_PATH)/boot.bin : ./boot/boot.asm
$(ASM) $(ASMFLAGS) -o $@ $<
$(OUTPUT_PATH)/loader.bin : ./boot/loader.asm
$(ASM) $(ASMFLAGS) -o $@ $<
$(OUTPUT_PATH)/kernel.bin : $(KERNEL_MODS_OUTPUT) $(LIBS_OUTPUT)
$(LINK) $(LINK_FLAGS) -o $@ $^
# kernel mods
$(OUTPUT_PATH)/kernel.o : $(KERNEL_PATH)/kernel.asm $(KERNEL_HEADER_ASM_PATH)/utils.inc.asm $(KERNEL_HEADER_ASM_PATH)/sconst.inc.asm
$(ASM) $(ASM_KERNEL_FLAGS) -o $@ $<
$(OUTPUT_PATH)/start.o : kernel/start.c include/type.h \
include/const.h include/protect_mode.h include/string.h include/type.h \
include/const.h include/io.h include/global.h include/protect_mode.h
$(CC) $(CC_KERNEL_FLAGS) -o $@ $<
$(OUTPUT_PATH)/init8259a.o : kernel/init8259a.c include/io.h \
include/const.h include/type.h include/const.h include/protect_mode.h \
include/type.h
$(CC) $(CC_KERNEL_FLAGS) -o $@ $<
$(OUTPUT_PATH)/global.o : kernel/global.c include/global.h \
include/const.h include/type.h include/protect_mode.h include/type.h
$(CC) $(CC_KERNEL_FLAGS) -o $@ $<
$(OUTPUT_PATH)/protect_mode.o : kernel/protect_mode.c \
include/global.h include/const.h include/type.h include/protect_mode.h \
include/type.h
$(CC) $(CC_KERNEL_FLAGS) -o $@ $<
$(OUTPUT_PATH)/proc.o : kernel/proc.c \
include/global.h include/type.h include/proc.h
$(CC) $(CC_KERNEL_FLAGS) -o $@ $<
$(OUTPUT_PATH)/kernel_main.o : kernel/kernel.c \
include/kernel.h include/global.h include/io.h include/proc.h
$(CC) $(CC_KERNEL_FLAGS) -o $@ $<
# libs
$(OUTPUT_PATH)/string.o : $(KERNEL_LIB_PATH)/string.asm
$(ASM) $(ASM_KERNEL_FLAGS) -o $@ $<
$(OUTPUT_PATH)/ioa.o : $(KERNEL_LIB_PATH)/io.asm
$(ASM) $(ASM_KERNEL_FLAGS) -o $@ $<
$(OUTPUT_PATH)/io.o : lib/io.c /usr/include/stdc-predef.h include/global.h \
include/const.h include/type.h include/protect_mode.h include/type.h \
include/io.h include/utils.h
$(CC) $(CC_KERNEL_FLAGS) -o $@ $<
$(OUTPUT_PATH)/utils.o : lib/utils.c /usr/include/stdc-predef.h include/global.h \
include/const.h include/type.h include/protect_mode.h include/type.h \
include/io.h
$(CC) $(CC_KERNEL_FLAGS) -o $@ $<
# image
# 1. create a floppy disk image.
# 2. write boot.bin as MBR.
# 3. mount the floppy disk image to MOUNT_POINT
# 4. copy loader.bin and kernel.bin into image
# 5. unmount
os.img : $(IMGS_MODS_OUTPUT)
bximage -func=create -fd=1.44M -q $(OUTPUT_PATH)/$@
dd if=$(OUTPUT_PATH)/boot.bin of=$(OUTPUT_PATH)/$@ bs=512 count=1 conv=notrunc
[ -f $(MOUNT_POINT) ] || mkdir -p $(MOUNT_POINT)
sudo mount $(OUTPUT_PATH)/$@ $(MOUNT_POINT)
sudo cp $(OUTPUT_PATH)/loader.bin $(MOUNT_POINT)
sudo cp $(OUTPUT_PATH)/kernel.bin $(MOUNT_POINT)
sudo umount $(MOUNT_POINT)