3 # Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
5 # This file may be distributed under the terms of the GNU LGPLv3 license.
8 VERSION=pre-0.4.3-$(shell date +"%Y%m%d_%H%M%S")-$(shell hostname)
14 SRCBOTH=output.c util.c block.c floppy.c ata.c misc.c mouse.c kbd.c pci.c \
15 serial.c clock.c pic.c cdrom.c ps2port.c smp.c resume.c \
16 pnpbios.c pirtable.c vgahooks.c pmm.c ramdisk.c
17 SRC16=$(SRCBOTH) system.c disk.c apm.c pcibios.c font.c
18 SRC32=$(SRCBOTH) post.c shadow.c memmap.c coreboot.c boot.c \
19 acpi.c smm.c mptable.c smbios.c pciinit.c optionroms.c mtrr.c \
22 cc-option = $(shell if test -z "`$(1) $(2) -S -o /dev/null -xc \
23 /dev/null 2>&1`"; then echo "$(2)"; else echo "$(3)"; fi ;)
25 # Default compiler flags
26 COMMONCFLAGS = -Wall -Os -MD -m32 -march=i386 -mregparm=3 \
27 -mpreferred-stack-boundary=2 -mrtd -freg-struct-return \
28 -ffreestanding -fomit-frame-pointer \
29 -fno-delete-null-pointer-checks -Wno-strict-aliasing \
30 -ffunction-sections -fdata-sections -fno-common \
31 -minline-all-stringops
32 COMMONCFLAGS += $(call cc-option,$(CC),-nopie,)
33 COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector,)
34 COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector-all,)
36 override CFLAGS = $(COMMONCFLAGS) -g -DMODE16=0
37 CFLAGS16INC = $(COMMONCFLAGS) -DMODE16=1 -fno-defer-pop \
38 $(call cc-option,$(CC),-fno-jump-tables,-DMANUAL_NO_JUMP_TABLE) \
39 $(call cc-option,$(CC),-fno-tree-switch-conversion,) \
40 $(call cc-option,$(CC),--param large-stack-frame=4,)
41 CFLAGS16 = $(CFLAGS16INC) -g
43 all: $(OUT) $(OUT)bios.bin
45 # Run with "make V=1" to see the actual compile commands
61 ################ Build rules
63 # Verify the gcc configuration and test if -fwhole-program works.
64 TESTGCC:=$(shell CC=$(CC) tools/test-gcc.sh)
65 ifeq "$(TESTGCC)" "-1"
66 $(error "Please upgrade GCC")
73 # Do a whole file compile - three methods are supported.
74 ifeq "$(COMPSTRAT)" "1"
75 # First method - use -fwhole-program without -combine.
77 @echo " Compiling whole program $3"
78 $(Q)printf '$(foreach i,$2,#include "../$i"\n)' > $3.tmp.c
79 $(Q)$(CC) $1 -fwhole-program -DWHOLE_PROGRAM -c $3.tmp.c -o $3
82 ifeq "$(COMPSTRAT)" "2"
83 # Second menthod - don't use -fwhole-program at all.
85 @echo " Compiling whole program $3"
86 $(Q)printf '$(foreach i,$2,#include "../$i"\n)' > $3.tmp.c
87 $(Q)$(CC) $1 -c $3.tmp.c -o $3
90 # Third (and preferred) method - use -fwhole-program with -combine
92 @echo " Compiling whole program $3"
93 $(Q)$(CC) $1 -fwhole-program -DWHOLE_PROGRAM -combine -c $2 -o $3
100 @echo " Compiling to assembler $@"
101 $(Q)$(CC) $(CFLAGS16INC) -S -c $< -o $@
104 @echo " Precompiling $@"
105 $(Q)$(CPP) -P -D__ASSEMBLY__ $< -o $@
107 $(OUT)asm-offsets.h: $(OUT)asm-offsets.s
108 @echo " Generating offset file $@"
109 $(Q)./tools/gen-offsets.sh $< $@
112 $(OUT)ccode.16.s: ; $(call whole-compile, $(CFLAGS16) -S, $(addprefix src/, $(SRC16)),$@)
114 $(OUT)ccode32.o: ; $(call whole-compile, $(CFLAGS), $(addprefix src/, $(SRC32)),$@)
116 $(OUT)code16.o: romlayout.S $(OUT)ccode.16.s $(OUT)asm-offsets.h
117 @echo " Compiling (16bit) $@"
118 $(Q)$(CC) $(CFLAGS16INC) -c -D__ASSEMBLY__ $< -o $@
120 $(OUT)romlayout16.lds $(OUT)romlayout32.lds $(OUT)code32.o: $(OUT)ccode32.o $(OUT)code16.o tools/layoutrom.py
121 @echo " Building ld scripts (version \"$(VERSION)\")"
122 $(Q)echo 'const char VERSION[] = "$(VERSION)";' > $(OUT)version.c
123 $(Q)$(CC) $(CFLAGS) -c $(OUT)version.c -o $(OUT)version.o
124 $(Q)$(LD) -melf_i386 -r $(OUT)ccode32.o $(OUT)version.o -o $(OUT)code32.o
125 $(Q)$(OBJDUMP) -thr $(OUT)code32.o > $(OUT)code32.o.objdump
126 $(Q)$(OBJDUMP) -thr $(OUT)code16.o > $(OUT)code16.o.objdump
127 $(Q)./tools/layoutrom.py $(OUT)code16.o.objdump $(OUT)code32.o.objdump $(OUT)romlayout16.lds $(OUT)romlayout32.lds
130 $(OUT)rom16.o: $(OUT)code16.o $(OUT)rom32.o $(OUT)romlayout16.lds
131 @echo " Linking (no relocs) $@"
132 $(Q)$(LD) -r -T $(OUT)romlayout16.lds $< -o $@
134 $(OUT)rom32.o: $(OUT)code32.o $(OUT)romlayout32.lds
135 @echo " Linking (no relocs) $@"
136 $(Q)$(LD) -r -T $(OUT)romlayout32.lds $< -o $@
138 $(OUT)rom.o: $(OUT)rom16.o $(OUT)rom32.o $(OUT)rombios16.lds $(OUT)rombios.lds
139 @echo " Linking $@ (version \"$(VERSION)\")"
140 $(Q)$(LD) -T $(OUT)rombios16.lds $(OUT)rom16.o -R $(OUT)rom32.o -o $(OUT)rom16.reloc.o
141 $(Q)$(STRIP) $(OUT)rom16.reloc.o -o $(OUT)rom16.final.o
142 $(Q)$(OBJCOPY) --adjust-vma 0xf0000 $(OUT)rom16.o $(OUT)rom16.moved.o
143 $(Q)$(LD) -T $(OUT)rombios.lds $(OUT)rom16.final.o $(OUT)rom32.o -R $(OUT)rom16.moved.o -o $@
145 $(OUT)bios.bin.elf $(OUT)bios.bin: $(OUT)rom.o tools/checkrom.py
147 $(Q)$(OBJDUMP) -thr $< > $<.objdump
148 $(Q)$(OBJCOPY) -O binary $< $(OUT)bios.bin.raw
149 $(Q)./tools/checkrom.py $<.objdump $(OUT)bios.bin.raw $(OUT)bios.bin
150 $(Q)$(STRIP) $< -o $(OUT)bios.bin.elf
153 ################ VGA build rules
156 SRCVGA=src/output.c src/util.c vgasrc/vga.c vgasrc/vgafb.c vgasrc/vgaio.c \
157 vgasrc/vgatables.c vgasrc/vgafonts.c vgasrc/clext.c
159 $(OUT)vgaccode.16.s: ; $(call whole-compile, $(CFLAGS16) -S -Isrc, $(SRCVGA),$@)
161 $(OUT)vgalayout16.o: vgaentry.S $(OUT)vgaccode.16.s $(OUT)asm-offsets.h
162 @echo " Compiling (16bit) $@"
163 $(Q)$(CC) $(CFLAGS16INC) -c -D__ASSEMBLY__ -Isrc $< -o $@
165 $(OUT)vgarom.o: $(OUT)vgalayout16.o $(OUT)vgalayout.lds
167 $(Q)$(LD) --gc-sections -T $(OUT)vgalayout.lds $(OUT)vgalayout16.o -o $@
169 $(OUT)vgabios.bin.raw: $(OUT)vgarom.o
170 @echo " Extracting binary $@"
171 $(Q)$(OBJCOPY) -O binary $< $@
173 $(OUT)vgabios.bin: $(OUT)vgabios.bin.raw tools/buildrom.py
174 @echo " Finalizing rom $@"
175 $(Q)./tools/buildrom.py $< $@
177 ####### Generic rules