-
Notifications
You must be signed in to change notification settings - Fork 4
/
Makefile
98 lines (73 loc) · 2.28 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
csrc = $(wildcard src/*.c) $(wildcard src/libc/*.c) $(wildcard src/test/*.c)
ssrc = $(wildcard src/*.s) $(wildcard src/libc/*.s) $(wildcard src/boot/*.s) $(wildcard src/test/*.s)
Ssrc = $(wildcard src/*.S)
obj = $(csrc:.c=.o) $(ssrc:.s=.o) $(Ssrc:.S=.o)
dep = $(obj:.o=.d)
elf = test
bin = test.bin
warn = -pedantic -Wall
#opt = -O2
dbg = -g
inc = -Isrc -Isrc/libc -Isrc/test
gccopt = -fno-pic -ffreestanding -nostdinc -fno-builtin -ffast-math -fcommon
CFLAGS = $(ccarch) -march=i386 $(warn) $(opt) $(dbg) $(gccopt) $(inc) $(def)
ASFLAGS = $(asarch) -march=i386 $(dbg) -nostdinc -fno-builtin $(inc)
LDFLAGS = $(ldarch) -nostdlib -T pcboot.ld -print-gc-sections
QEMU_FLAGS = -drive file=floppy.img,format=raw,if=floppy -serial file:serial.log -device sb16
ifneq ($(shell uname -m), i386)
ccarch = -m32
asarch = --32
ldarch = -m elf_i386
endif
# uncomment to use a specific toolchain
#TOOLPREFIX = x86_64-elf-
CC = $(TOOLPREFIX)gcc
AS = $(TOOLPREFIX)as
LD = $(TOOLPREFIX)ld
OBJCOPY = $(TOOLPREFIX)objcopy
OBJDUMP = $(TOOLPREFIX)objdump
floppy.img: boot.img
dd if=/dev/zero of=$@ bs=512 count=2880
dd if=$< of=$@ conv=notrunc
pcboot.iso: floppy.img
rm -rf cdrom
git archive --format=tar --prefix=cdrom/ HEAD | tar xf -
cp $< cdrom
mkisofs -o $@ -R -J -V pcboot -b $< cdrom
boot.img: bootldr.bin $(bin)
cat bootldr.bin $(bin) >$@
# bootldr.bin will contain .boot, .boot2, .bootend, and .lowtext
bootldr.bin: $(elf)
$(OBJCOPY) -O binary -j '.boot*' -j .lowtext $< $@
# the main binary will contain every section *except* those
$(bin): $(elf)
$(OBJCOPY) -O binary -R '.boot*' -R .lowtext $< $@
$(elf): $(obj)
$(LD) -o $@ $(obj) -Map link.map $(LDFLAGS)
%.o: %.S
$(CC) -o $@ $(CFLAGS) -c $<
-include $(dep)
%.d: %.c
@$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@
.PHONY: clean
clean:
rm -f $(obj) $(bin) boot.img floppy.img link.map
.PHONY: cleandep
cleandep:
rm -f $(dep)
.PHONY: disasm
disasm: bootldr.disasm $(elf).disasm
bootldr.disasm: $(elf)
$(OBJDUMP) -d $< -j .boot -j .boot2 -m i8086 >$@
$(elf).disasm: $(elf)
$(OBJDUMP) -d $< -j .startup -j .text -j .lowtext -m i386 >$@
$(elf).sym: $(elf)
$(OBJCOPY) --only-keep-debug $< $@
.PHONY: run
run: $(bin)
qemu-system-i386 $(QEMU_FLAGS)
.PHONY: debug
debug: $(bin) $(elf).sym
qemu-system-i386 $(QEMU_FLAGS) -s -S
.PHONY: sym
sym: $(elf).sym