-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
106 lines (86 loc) · 2.83 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
# Compilation setup
# i386-elf cross compiler
CC = /home/damian/opt/cross/bin/i386-elf-gcc
# SUPPRESS THIS WARNINGS:
# pointer-to-int-cast
# int-conversion
CFLAGS = -ffreestanding
LD = /home/damian/opt/cross/bin/i386-elf-ld
# Automatically generate lists of sources using wildcards
C_SOURCES = $(wildcard kernel/*.c drivers/*.c cpu/*.c)
HEADERS = $(wildcard kernel/*.h drivers/*.h cpu/*.h)
#Convert the *.c filenames to *.o to give a list of object files to build
OBJ = ${C_SOURCES:.c=.o cpu/interrupt.o}
# Default target
release:
ifneq (,$(wildcard ./DEBUG)) # if DEBUG exist clean
release: clean release/os-image
rm DEBUG
else
release: release/os-image
endif
# Run qemu emulator
run: release
qemu-system-i386 -fda release/os-image
# Build OS image
release/os-image: boot/boot_sect.bin kernel/kernel.bin
@mkdir -p $(@D)
cat $^ > release/os-image
# Build the kernel binary
# $^ is substituted with all of the target's dependancy files
kernel/kernel.bin : kernel/kernel_entry.o ${OBJ}
$(LD) -o $@ -Ttext 0x1000 $^ --oformat binary
# Generic rule for compiling C code to an object file
# For simplicity, C files depend on all header files
%.o : %.c ${HEADERS}
$(CC) $(CFLAGS) -c $< -o $@
# Assemble the kernel entry
%.o : %.asm
nasm $< -f elf -o $@
# Assemble the boot sector to raw machine code
%.bin : %.asm
nasm $< -f bin -I '16bit/' -o $@
clean:
rm -rf release/* debug/*
rm -rf kernel/*.o ${OBJ}
rm -rf kernel/kernel.bin boot/*.bin
# Disassemble our kernel - might be useful for debugging.
kernel.dis : kernel/kernel.bin
ndisasm -b 32 $< > $@
# Phony targets
#.PHONY: clean debug release run run_debug
#----------------------------- DEBUG ---------------------------------------------#
# Build OS image and
debug: CFLAGS += -g -H
debug: debug/os-image
#checking whether DEBUG flag exists
ifeq (,$(wildcard ./DEBUG)) # if it doesn't already exist create file named DEBUG
touch DEBUG # simply signaling whether last build was debug or release
endif
# Debug version of the build
debug/os-image: boot/boot_sect.bin kernel/kernel.bin
@mkdir -p $(@D)
cat $^ > debug/os-image
# run qemu and listen for gdb connection on port 1234
# -s shorthand for -gdb tcp::1234, i.e. open gdbserver on TCP port 1234
# -S do not start CPU at startup
#
# After qemu starts listening:
# open a terminal then type:
# gdb
# (gdb) target remote localhost:1234
run_debug: debug
qemu-system-x86_64 -s- -S -fda release/os-image
#-------------------------------- TEST -----------------------------------------#
#TEST_C_SOURCES = $(wildcard test/*.c drivers/*.c)
#TEST_OBJ = ${TEST_C_SOURCES:.c=.o}
#
#
#test/test-image: boot/boot_sect.bin test/test_kernel.bin
# cat $^ > $@
#
#test/test_kernel.bin: kernel/kernel_entry.o ${TEST_OBJ} kernel/util.o
# ld -m elf_i386 -o $@ -Ttext 0x1000 $^ --oformat binary
#
#run_test: test/test-image
# qemu-system-x86_64 -fda $<