-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
213 lines (166 loc) · 5.49 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
.PHONY : all clean fclean re run docker
HOSTAC = nasm
# -f: format
HOSTACFLAGS = -f elf32
HOSTCC = gcc
# -m32: for 32 bits kernel
# -ffreestanding: no _start symbol
# -fno-pic: remove __x86.get_pc_thunk.bx function automatically generated by gcc
# -c: compile source files without linking
HOSTCFLAGS = -fno-builtin \
-fno-exceptions \
-fno-stack-protector \
-nostdlib \
-nodefaultlibs \
-fno-pic \
-m32 \
-ffreestanding \
-static \
-c \
-I$(HEADDIR) -I$(KERNLIBDIR)/headers -I$(KERNLIBDIR)/printk/headers
HOSTLINKER = ld
# -m elf_i386: emulate the elf_i386 linker
# -n: Turn off page alignment of sections, disable linking against shared libraries
HOSTLINKERFLAG = -m elf_i386 \
-n
# FOLDERS
SRCDIR = sources
OBJDIR = objects
HEADDIR = headers
TARGETDIR = targets
X86TARGETDIR = $(TARGETDIR)/x86
BOOTDIR = boot
ISODIR = iso
###############
# DOCKER
NAME_DOCKER = kfs-build
# LINKER
LINKER = linker.ld
# KERNEL
NAME = kernel.bin
NAME_ISO = kernel.iso
KERNELDIR = kernel
# LIB
KERNLIBDIR = $(SRCDIR)/$(KERNELDIR)/kernlib
KERNLIB = $(KERNLIBDIR)/kernlib.a
KERNEL_SRCS = kernel.c \
keyboard.c \
gdt.c \
shell.c
KERNEL_HEADS = kernel.h \
keyboard.h \
shell.h
KERNEL_SOURCES = $(addprefix $(SRCDIR)/$(KERNELDIR)/, $(KERNEL_SRCS))
KERNEL_OBJECTS = $(addprefix $(OBJDIR)/$(KERNELDIR)/, $(KERNEL_SRCS:.c=.o))
KERNEL_HEADERS = $(addprefix $(HEADDIR)/, $(KERNEL_HEADS))
# BOOT
GRUBDIR = grub
GRUBCFG = grub.cfg
BOOT_SRCS = boot.s \
gdt_flush.s \
BOOT_SOURCES = $(addprefix $(SRCDIR)/$(BOOTDIR)/, $(BOOT_SRCS))
BOOT_OBJECTS = $(addprefix $(OBJDIR)/$(BOOTDIR)/, $(BOOT_SRCS:.s=.o))
TODOS=$(shell grep -nr "TODO" $(SRCDIR) $(HEADDIR) | wc -l)
###############
# Count files to compile
SHOULD_COUNT = 1
FILES_TO_COMPILE = 0
ifeq ($(SHOULD_COUNT), 1)
FILES_TO_COMPILE:=$(shell make -n SHOULD_COUNT=0 | grep "gcc" | wc -l)
endif
###############
# QEMU
QEMU = qemu-system-i386
QEMUFLAGS = -s -S
DEBUG_FILE = debug.tmp
##############
# COLOR CODES
GREEN = '\033[4;32m'
RED = '\033[4;31m'
BLANK = '\033[0m'
YELLOW = '\033[4;33m'
CYAN = '\033[4;38;5;51m'
WARNING = '\033[1;33m'
RESET = '\033[0m'
COMPILE_COLOR = '\033[0;33m'
TICK = '\033[1;32m~\033[0m'
CROSS = '\033[1;31mx\033[0m'
###############
all:
@ $(MAKE) -s -C $(KERNLIBDIR)
@ env DOCKER_COMPILE=1 $(MAKE) --no-print-directory $(X86TARGETDIR)/$(NAME_ISO)
$(X86TARGETDIR)/$(NAME_ISO): $(KERNLIB) $(KERNEL_OBJECTS) $(BOOT_OBJECTS)
@ mkdir -p $(X86TARGETDIR)/$(ISODIR)/$(BOOTDIR)/$(GRUBDIR)
@ cp $(SRCDIR)/$(BOOTDIR)/$(GRUBCFG) $(X86TARGETDIR)/$(ISODIR)/$(BOOTDIR)/$(GRUBDIR)/
@ cp $(SRCDIR)/$(LINKER) $(X86TARGETDIR)/$(ISODIR)/
@ $(HOSTLINKER) $(HOSTLINKERFLAG) -o $(X86TARGETDIR)/$(ISODIR)/$(BOOTDIR)/$(NAME) \
-T $(X86TARGETDIR)/$(ISODIR)/$(LINKER) $(KERNEL_OBJECTS) \
$(KERNLIB) $(BOOT_OBJECTS)
ifeq ($(DOCKER_COMPILE), 0)
grub-mkrescue -o $(X86TARGETDIR)/$(NAME_ISO) $(X86TARGETDIR)/$(ISODIR)
else
@ docker inspect --type=image kfs-build 1>/dev/null || docker build -t $(NAME_DOCKER) .
@ docker start $(NAME_DOCKER) || docker run --name $(NAME_DOCKER) -d $(NAME_DOCKER)
@ docker cp $(X86TARGETDIR)/$(ISODIR) $(NAME_DOCKER):/$(ISODIR)
docker exec kfs-build grub-mkrescue -o $(NAME_ISO) $(ISODIR)
@ docker cp $(NAME_DOCKER):/$(NAME_ISO) $(X86TARGETDIR)/$(NAME_ISO)
@ docker exec kfs-build rm -rf $(NAME_ISO) $(ISODIR)
@ touch $(X86TARGETDIR)/$(NAME_ISO)
@ docker kill $(NAME_DOCKER) 1>/dev/null
endif
@ printf " %b | Compiled %b%b%b\n" $(TICK) $(GREEN) $(NAME) $(BLANK)
##### KERNEL LIB #####
$(KERNLIB):
@ $(MAKE) -s -C $(KERNLIBDIR)
##### KERNEL COMPILATION #####
I = 1
$(KERNEL_OBJECTS): $(OBJDIR)/$(KERNELDIR)/%.o: $(SRCDIR)/$(KERNELDIR)/%.c $(KERNEL_HEADERS)
@ printf "[$(I)/$(FILES_TO_COMPILE)] "
@ mkdir -p $(OBJDIR)/$(KERNELDIR)
$(HOSTCC) $(HOSTCFLAGS) -o $@ $<
$(eval I=$(shell echo $$(($(I) + 1))))
##############################
##### BOOT COMPILATION #####
$(BOOT_OBJECTS): $(OBJDIR)/$(BOOTDIR)/%.o: $(SRCDIR)/$(BOOTDIR)/%.s
@ mkdir -p $(OBJDIR)/$(BOOTDIR)
@ $(HOSTAC) $(HOSTACFLAGS) -o $@ $<
############################
clean:
@ $(MAKE) -s -C $(KERNLIBDIR) clean
@ test -d $(OBJDIR) && \
rm -rf $(OBJDIR) && \
printf " %b | " $(TICK) && \
printf "Removed %b%b%b folder\n" $(YELLOW) $(OBJDIR) $(BLANK) \
|| (printf " %b | " $(CROSS) && \
printf "No %b%b%b folder\n" $(YELLOW) $(OBJDIR) $(BLANK))
fclean: clean
@ test -f $(KERNLIB) && rm -rf $(KERNLIB) && \
test -f $(DEBUG_FILE) && \
rm -rf $(DEBUG_FILE) && \
printf " %b | " $(TICK) && \
printf "Removed %b%b%b file\n" $(RED) $(DEBUG_FILE) $(BLANK) \
|| (printf " %b | " $(CROSS) && \
printf "No %b%b%b file\n" $(RED) $(DEBUG_FILE) $(BLANK))
@ test -d $(TARGETDIR) && \
rm -rf $(TARGETDIR) && \
printf " %b | " $(TICK) && \
printf "Removed %b%b%b folder\n" $(RED) $(TARGETDIR) $(BLANK) \
|| (printf " %b | " $(CROSS) && \
printf "No %b%b%b folder\n" $(RED) $(TARGETDIR) $(BLANK))
re: fclean all
debug: all
@ printf "@ Creating %b%b%b file\n" $(YELLOW) $(DEBUG_FILE) $(BLANK)
@ echo 'set disassembly-flavor intel' > debug.tmp
@ echo 'target remote localhost:1234' >> debug.tmp
@ echo 'continue' >> debug.tmp
@ printf "@ Run %bexec gdb -x %b%b in another window to start debugging\n" $(CYAN) $(DEBUG_FILE) $(BLANK)
$(QEMU) $(QEMUFLAGS) -cdrom $(X86TARGETDIR)/$(NAME_ISO)
todo:
@ printf "%b" $(WARNING)
@ grep -nr "TODO" $(SRCDIR) $(HEADDIR) || true
@ printf "%b" $(BLANK)
run: all
$(QEMU) -cdrom $(X86TARGETDIR)/$(NAME_ISO)
docker:
@ $(MAKE) -s -C $(KERNLIBDIR)
@ env DOCKER_COMPILE=1 $(MAKE) --no-print-directory $(X86TARGETDIR)/$(NAME_ISO)