-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
160 lines (124 loc) · 4.08 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
# Currently only support x86, x86_64
ARCH := x86_64
# Arch-specific makefile needs to define (or add to) the following
CC :=
LD :=
OBJDUMP :=
OBJCOPY :=
GDB :=
QEMU :=
CFLAGS := -ffreestanding -fwrapv -fno-pic -fno-stack-protector -Wall -Werror -g -MMD -MP -I include
CFLAGS +=
LDFLAGS :=
TOOLS_CFLAGS := -Werror -Wall -I include
KERNEL_CLFAGS :=
MKDIR_P := mkdir -p
HOST_CC := gcc
QEMUOPTS := -serial mon:stdio -m 512 -no-reboot -device isa-debug-exit #-d int
QEMUOPTS_LOWMEM := -serial mon:stdio -m 4 -no-reboot -device isa-debug-exit #-d int
QEMUTESTOPTS := -serial pipe:build/osv-test -monitor none -m 512 -no-reboot -device isa-debug-exit #-d int
QEMUTESTOPTS_LOWMEM := -serial pipe:build/osv-test -monitor none -m 4 -no-reboot -device isa-debug-exit #-d int
GDBPORT := $(shell expr `id -u` % 5000 + 25000)
QEMUGDB := -gdb tcp::$(GDBPORT)
BUILD := build
OSV_IMG := $(BUILD)/osv.img
FS_IMG := $(BUILD)/fs.img
KERNEL_ELF := $(BUILD)/kernel/kernel.elf
# Some extra files for filesystem testing
LARGEFILE := $(BUILD)/largefile
README := $(BUILD)/README
SMALLFILE := $(BUILD)/smallfile
TESTFILE := $(BUILD)/testfile
### Default rule ###
all: osv tools
### Arch-specific makefiles ###
BOOTLOADER :=
ARCH_KERNEL_OBJS :=
ARCH_KERNEL_LD :=
GDBINIT :=
ifeq ($(ARCH), x86)
include arch/x86/Rules.mk
else ifeq ($(ARCH), x86_64)
include arch/x86_64/Rules.mk
else
$(error Unsupported architecture $(ARCH))
endif
### Kernel makefile ###
include kernel/Rules.mk
### Library makefile ###
include lib/Rules.mk
### User makefile ###
include user/Rules.mk
### General rules ###
osv: $(OSV_IMG) $(FS_IMG)
$(OSV_IMG): $(BOOTLOADER) $(KERNEL_ELF)
dd if=/dev/zero of=$@ count=10000
dd if=$(BOOTLOADER) of=$@ conv=notrunc
dd if=$(KERNEL_ELF) of=$@ seek=1 conv=notrunc
$(LARGEFILE):
$(MKDIR_P) $(@D)
cat /dev/zero | tr '\0' 'a' | dd of=$@ count=40
$(SMALLFILE):
$(MKDIR_P) $(@D)
echo "aaaaaaaaaabbbbbbbbbbccccc" > $@
$(TESTFILE):
$(MKDIR_P) $(@D)
echo "abcdef" > $@
$(README):
$(MKDIR_P) $(@D)
echo "*************************************************" > $@
echo "***OSV, the Ultimate Teaching Operating System***" >> $@
echo "*************************************************" >> $@
$(FS_IMG): $(BUILD)/tools/mkfs $(README) $(LARGEFILE) $(SMALLFILE) $(TESTFILE) $(USER_OBJS)
$(BUILD)/tools/mkfs $@ $(README) $(LARGEFILE) $(SMALLFILE) $(TESTFILE) $(USER_BIN)
$(KERNEL_ELF): $(ARCH_KERNEL_OBJS) $(ARCH_KERNEL_LD) $(KERNEL_OBJS) $(KLIB_OBJS) $(ENTRY_AP)
$(LD) $(LDFLAGS) -T $(ARCH_KERNEL_LD) -o $@ $(ARCH_KERNEL_OBJS) $(KERNEL_OBJS) $(KLIB_OBJS) -b binary $(ENTRY_AP)
$(OBJDUMP) -S $@ > $(BUILD)/kernel/kernel.asm
$(OBJDUMP) -t $@ > $(BUILD)/kernel/kernel.sym
$(ULIB_OBJS): $(BUILD)/ulib/%.o: lib/%.c
$(MKDIR_P) $(@D)
$(CC) $(CFLAGS) -o $@ -c $<
$(BUILD)/user/%.o: user/%.c $(ARCH_USER_OBJS) $(ULIB_OBJS)
$(MKDIR_P) $(@D)
$(CC) $(CFLAGS) -o $@ -c $<
$(LD) $(LDFLAGS) --no-omagic -e main -o $(subst .o, , $@) $@ $(ARCH_USER_OBJS) $(ULIB_OBJS)
rm $@ $(subst .o,.d, $@)
$(BUILD)/%.o: %.c
$(MKDIR_P) $(@D)
$(CC) $(CFLAGS) $(KERNEL_CFLAGS) -o $@ -c $<
$(BUILD)/%.o: %.S
$(MKDIR_P) $(@D)
$(CC) $(CFLAGS) -o $@ -c $<
tools: $(BUILD)/tools/mkfs
$(BUILD)/tools/mkfs: tools/mkfs.c
$(MKDIR_P) $(@D)
$(HOST_CC) $(TOOLS_CFLAGS) -o $@ $<
ifndef CPUS
CPUS := 2
endif
# Dependency generation
DEPS := $(shell find $(BUILD)/ -type f -name '*.d')
-include $(DEPS)
clean:
-rm -f .gdbinit $(VECTORS) $(ENTRY_AP)
-rm -rf $(BUILD)
### QEMU and GDB ###
DRIVE_OPTS := -drive file=$(OSV_IMG),index=0,media=disk,format=raw -drive file=$(FS_IMG),index=1,media=disk,format=raw -smp $(CPUS)
qemu: osv
$(QEMU) $(QEMUOPTS) $(DRIVE_OPTS) -nographic
qemu-low-mem: osv
$(QEMU) $(QEMUOPTS_LOWMEM) $(DRIVE_OPTS) -nographic
qemu-test: osv
$(QEMU) $(QEMUTESTOPTS) $(DRIVE_OPTS) -nographic
qemu-test-low-mem: osv
$(QEMU) $(QEMUTESTOPTS_LOWMEM) $(DRIVE_OPTS) -nographic
qemu-graphic: osv
$(QEMU) $(QEMUOPTS) $(DRIVE_OPTS)
qemu-gdb: osv
cp ~/.gdbinit .tmpgdb
sed "s/localhost:21795/localhost:$(GDBPORT)/" < .tmpgdb > ~/.gdbinit
rm .tmpgdb
$(QEMU) $(QEMUOPTS) $(DRIVE_OPTS) -nographic -S $(QEMUGDB)
gdb: $(GDBINIT)
ln -s -f $(GDBINIT) .gdbinit
$(GDB)