summaryrefslogtreecommitdiff
path: root/Makefile
blob: 5cc95f6dcf1ccec0a317e9d815f6e6d7af6dd7ee (plain)
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
SHELL:=/bin/bash

override CFLAGS += \
	-nostdlib \
	-nostdinc \
	-fno-builtin \
	-fno-stack-protector \
	-mno-red-zone \
	-nostartfiles \
	-nodefaultlibs \
	-Wall \
	-Wextra \
	-MMD

STAGE3_C = \
	stage3/init.o \
	stage3/main.o \
	stage3/gfx.o \
	stage3/halt.o \
	stage3/interrupts.o \
	stage3/pic.o \
	stage3/clock.o \
	stage3/memory.o \
	stage3/heap.o \
	stage3/font.o \
	stage3/font_classic.o \
	stage3/ata.o \
	stage3/string.o \
	stage3/pci.o \
	stage3/fs.o \
	stage3/ps2.o \
	stage3/thread.o \
	stage3/shell.o \
	stage3/version.o \
	stage3/rng.o \
	stage3/cheese3d.o \
	stage3/cheese_demo.o

STAGE3 = $(STAGE3_C) \
	stage3/isr.o \
	stage3/yield.o \
	stage3/paging.o \
	stage3/watchdog.o

PAD_BOUNDARY = pad() { truncate -s $$(echo "($$(du -b $$1 | cut -f1)+$$2-1)/$$2*$$2" | bc) $$1; }; pad
DISAS = objdump -b binary -D -M intel -m i386:x86-64 stage3.bin --adjust-vma 0x9000

cuddles.img: stage1.bin stage2.bin stage3.bin fs.tar
	cat stage{1,2,3}.bin fs.tar > cuddles.img
	$(PAD_BOUNDARY) cuddles.img 1048576

stage1.bin: stage1/main.asm stage1/print.asm stage2.bin stage3.bin
	nasm -f bin stage1/main.asm -o stage1.bin \
		-dKSIZE=$$(du -cb stage{2,3}.bin | tail -n1 | cut -f1)

stage2.bin: stage2/main.asm stage2/mmap.asm stage2/paging.asm stage2/vesa.asm stage1/print.asm
	nasm -f bin stage2/main.asm -o stage2.bin
	truncate -s 4608 stage2.bin

stage3.bin fs/dbg/kernel.map &: $(STAGE3) stage3.ld
	mkdir -p fs/dbg/
	ld $(STAGE3) -T stage3.ld -Map=fs/dbg/kernel.map
	$(PAD_BOUNDARY) stage3.bin 512

fs/dbg/kernel.dis.asm: stage3.bin
	mkdir -p fs/dbg/
	$(DISAS) > fs/dbg/kernel.dis.asm

stage3/%.o: stage3/%.asm
	nasm -f elf64 $< -o $@

stage3/interrupts.o: stage3/interrupts.c
	gcc $(CFLAGS) -mgeneral-regs-only -c $< -o $@
stage3/pic.o: stage3/pic.c
	gcc $(CFLAGS) -mgeneral-regs-only -c $< -o $@
stage3/init.o: stage3/init.c
	gcc $(CFLAGS) -mgeneral-regs-only -c $< -o $@

stage3/%.o: stage3/%.c
	gcc $(CFLAGS) -c $< -o $@

-include $(STAGE3_C:%.o=%.d)

GIT_VERSION := $(shell git describe --tags 2>/dev/null || git rev-parse --short HEAD)

.version-$(GIT_VERSION):
	rm -f .version-*
	touch $@

stage3/version.c: .version-$(GIT_VERSION)
	echo -e "#include \"def.h\"\nstr version = S(\"$(GIT_VERSION)\");" > $@

stage3/%.asm: stage3/%.lua
	lua  $< > $@

stage3/font.c: stage3/font_builtin.c

stage3/font_builtin.c: fs/fonts/ter-u16n.cuddlefont
	xxd -i $< > $@

fs.tar: $(shell find fs | sed 's/ /\\ /g') fs/dbg/kernel.map fs/dbg/kernel.dis.asm
	cd fs && tar --format=ustar -cf ../fs.tar *

.PHONY: run clean flash disas qemu bochs

bochs: cuddles.img
	rm -f cuddles.img.lock
	echo c | bochs -q

# to qemu slow: make QFLAGS="-icount shift=9,align=on,sleep=on" run
# try QFLAGS="-enable-kvm" for better performance
override QFLAGS += -drive format=raw,file=cuddles.img

qemu: cuddles.img
	qemu-system-x86_64 $(QFLAGS)

run: qemu

clean:
	rm -rf .version-* stage3/*.{o,d} *.bin *.img stage3/{isr.asm,version.c,font_builtin.c} fs.tar fs/dbg

flash: cuddles.img
	dd if=cuddles.img of=$(DEV)

disas: stage3.bin
	$(DISAS) --disassembler-color=on