main kernel startup, libc, console tty, asmops, build flags fixes
[bootcensus] / Makefile
1 csrc = $(wildcard src/*.c) $(wildcard src/libc/*.c)
2 ssrc = $(wildcard src/*.s) $(wildcard src/libc/*.s) $(wildcard src/boot/*.s)
3 obj = $(csrc:.c=.o) $(ssrc:.s=.o)
4 dep = $(obj:.o=.d)
5 elf = test
6 bin = test.bin
7
8 warn = -pedantic -Wall
9 #opt = -O2
10 dbg = -g
11 inc = -Isrc -Isrc/libc
12 gccopt = -fno-pic -ffreestanding -nostdinc -fno-builtin
13
14 CFLAGS = $(ccarch) -march=i386 $(warn) $(opt) $(dbg) $(gccopt) $(inc) $(def)
15 ASFLAGS = $(asarch) -march=i386 $(dbg) -nostdinc -fno-builtin $(inc)
16 LDFLAGS = $(ldarch) -nostdlib -T pcboot.ld -print-gc-sections
17
18
19 ifneq ($(shell uname -m), i386)
20         ccarch = -m32
21         asarch = --32
22         ldarch = -m elf_i386
23 endif
24
25 floppy.img: boot.img
26         dd if=/dev/zero of=$@ bs=512 count=2880
27         dd if=$< of=$@ conv=notrunc
28
29 boot.img: bootldr.bin $(bin)
30         cat bootldr.bin $(bin) >$@
31
32 # bootldr.bin will contain only .boot and .boot2
33 bootldr.bin: $(elf)
34         objcopy -O binary -j '.boot*' $< $@
35
36 # the main binary will contain every section *except* .boot and .boot2
37 $(bin): $(elf)
38         objcopy -O binary -R '.boot*' $< $@
39
40 $(elf): $(obj)
41         $(LD) -o $@ $(obj) -Map link.map $(LDFLAGS)
42
43 -include $(dep)
44
45 %.d: %.c
46         @$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@
47
48 .PHONY: clean
49 clean:
50         rm -f $(obj) $(bin) boot.img floppy.img link.map
51
52 .PHONY: cleandep
53 cleandep:
54         rm -f $(dep)
55
56 .PHONY: disasm
57 disasm: bootldr.disasm $(elf).disasm
58
59 bootldr.disasm: $(elf)
60         objdump -d $< -j .boot -j .boot2 -m i8086 >$@
61
62 $(elf).disasm: $(elf)
63         objdump -d $< -j .startup -j .text -m i386 >$@
64
65 $(elf).sym: $(elf)
66         objcopy --only-keep-debug $< $@
67
68 .PHONY: run
69 run: $(bin)
70         qemu-system-i386 -fda floppy.img -serial file:serial.log
71
72 .PHONY: debug
73 debug: $(bin) $(elf).sym
74         qemu-system-i386 -fda floppy.img -serial file:serial.log -s -S
75
76 .PHONY: sym
77 sym: $(elf).sym