-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
62 lines (51 loc) · 1.79 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
arch ?= x86_64
kernel := build/kernel-$(arch).bin
img := build/os.img
cc := x86_64-elf-gcc-6.3.0 -g -Wall -mno-red-zone -Wno-unused-variable -Wno-unused-but-set-variable
linker_script := src/linker.ld
grub_cfg := src/grub.cfg
c_source_files := $(wildcard src/*.c)
c_object_files := $(patsubst src/%.c, \
build/%.c.o, $(c_source_files))
assembly_source_files := $(wildcard src/*.asm)
assembly_object_files := $(patsubst src/%.asm, \
build/%.asm.o, $(assembly_source_files))
.PHONY: all clean run img
all: $(kernel)
clean:
@rm -r build
run: $(img)
@qemu-system-x86_64 -drive format=raw,file=$(img)
test: $(img)
@qemu-system-x86_64 -s -drive format=raw,file=$(img) -serial stdio
#-monitor stdio
img: $(img)
$(img): $(kernel) $(grub_cfg)
@mkdir -p build/isofiles/boot/grub
@mkdir -p build/isofiles/test
@cp $(kernel) build/isofiles/boot/kernel.bin
@cp $(grub_cfg) build/isofiles/boot/grub
cp testfile build/isofiles/test
@dd if=/dev/zero of=$(img) bs=512 count=32768
@parted $(img) mklabel msdos
@parted $(img) mkpart primary fat32 2048s 30720s
@parted $(img) set 1 boot on
@sudo losetup /dev/loop0 $(img)
@sudo losetup /dev/loop1 $(img) -o 1048576
@sudo mkdosfs -F32 -f2 /dev/loop1
@sudo mount /dev/loop1 /mnt/fatgrub
@sudo grub-install --root-directory=/mnt/fatgrub --no-floppy --modules="normal part_msdos ext2 multiboot" /dev/loop0
@sudo cp -r build/isofiles/* /mnt/fatgrub
@sudo umount /mnt/fatgrub
@sudo losetup -d /dev/loop0
@sudo losetup -d /dev/loop1
@rm -r build/isofiles
$(kernel): $(assembly_object_files) $(linker_script) $(c_object_files)
ld -n -T $(linker_script) -o $(kernel) $(assembly_object_files) $(c_object_files)
#compile c files
build/%.c.o: src/%.c
$(cc) -c -g -o $@ $<
# compile assembly files
build/%.asm.o: src/%.asm
@mkdir -p $(shell dirname $@)
@nasm -felf64 $< -o $@