Implement native 32bit APM support.
[seabios.git] / Makefile
1 # SeaBIOS build system
2 #
3 # Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
4 #
5 # This file may be distributed under the terms of the GNU LGPLv3 license.
6
7 # Program version
8 VERSION=pre-0.5.1-$(shell date +"%Y%m%d_%H%M%S")-$(shell hostname)
9
10 # Output directory
11 OUT=out/
12
13 # Source files
14 SRCBOTH=misc.c pmm.c stacks.c output.c util.c block.c floppy.c ata.c mouse.c \
15         kbd.c pci.c serial.c clock.c pic.c cdrom.c ps2port.c smp.c resume.c \
16         pnpbios.c pirtable.c vgahooks.c ramdisk.c pcibios.c \
17         usb.c usb-uhci.c usb-ohci.c usb-hid.c paravirt.c
18 SRC16=$(SRCBOTH) system.c disk.c apm.c font.c
19 SRC32FLAT=$(SRCBOTH) post.c shadow.c memmap.c coreboot.c boot.c \
20       acpi.c smm.c mptable.c smbios.c pciinit.c optionroms.c mtrr.c \
21       lzmadecode.c
22 SRC32SEG=util.c output.c pci.c pcibios.c apm.c
23
24 cc-option = $(shell if test -z "`$(1) $(2) -S -o /dev/null -xc \
25               /dev/null 2>&1`"; then echo "$(2)"; else echo "$(3)"; fi ;)
26
27 # Default compiler flags
28 COMMONCFLAGS = -Wall -Os -MD -m32 -march=i386 -mregparm=3 \
29                -mpreferred-stack-boundary=2 -mrtd -freg-struct-return \
30                -ffreestanding -fomit-frame-pointer \
31                -fno-delete-null-pointer-checks -Wno-strict-aliasing \
32                -ffunction-sections -fdata-sections -fno-common \
33                -minline-all-stringops
34 COMMONCFLAGS += $(call cc-option,$(CC),-nopie,)
35 COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector,)
36 COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector-all,)
37
38 CFLAGS32FLAT = $(COMMONCFLAGS) -g -DMODE16=0 -DMODESEGMENT=0
39 CFLAGSSEG = $(COMMONCFLAGS) -DMODESEGMENT=1 -fno-defer-pop \
40             $(call cc-option,$(CC),-fno-jump-tables,-DMANUAL_NO_JUMP_TABLE) \
41             $(call cc-option,$(CC),-fno-tree-switch-conversion,)
42 CFLAGS32SEG = $(CFLAGSSEG) -DMODE16=0 -g
43 CFLAGS16INC = $(CFLAGSSEG) -DMODE16=1 \
44               $(call cc-option,$(CC),--param large-stack-frame=4,)
45 CFLAGS16 = $(CFLAGS16INC) -g
46
47 all: $(OUT) $(OUT)bios.bin
48
49 # Run with "make V=1" to see the actual compile commands
50 ifdef V
51 Q=
52 else
53 Q=@
54 endif
55
56 OBJCOPY=objcopy
57 OBJDUMP=objdump
58 STRIP=strip
59
60 .PHONY : all FORCE
61
62 vpath %.c src vgasrc
63 vpath %.S src vgasrc
64
65 ################ Build rules
66
67 # Verify the gcc configuration and test if -fwhole-program works.
68 TESTGCC:=$(shell CC=$(CC) tools/test-gcc.sh)
69 ifeq "$(TESTGCC)" "-1"
70 $(error "Please upgrade GCC")
71 endif
72
73 ifndef COMPSTRAT
74 COMPSTRAT=$(TESTGCC)
75 endif
76
77 # Do a whole file compile - three methods are supported.
78 ifeq "$(COMPSTRAT)" "1"
79 # First method - use -fwhole-program without -combine.
80 define whole-compile
81 @echo "  Compiling whole program $3"
82 $(Q)printf '$(foreach i,$2,#include "../$i"\n)' > $3.tmp.c
83 $(Q)$(CC) $1 -fwhole-program -DWHOLE_PROGRAM -c $3.tmp.c -o $3
84 endef
85 else
86 ifeq "$(COMPSTRAT)" "2"
87 # Second menthod - don't use -fwhole-program at all.
88 define whole-compile
89 @echo "  Compiling whole program $3"
90 $(Q)printf '$(foreach i,$2,#include "../$i"\n)' > $3.tmp.c
91 $(Q)$(CC) $1 -c $3.tmp.c -o $3
92 endef
93 else
94 # Third (and preferred) method - use -fwhole-program with -combine
95 define whole-compile
96 @echo "  Compiling whole program $3"
97 $(Q)$(CC) $1 -fwhole-program -DWHOLE_PROGRAM -combine -c $2 -o $3
98 endef
99 endif
100 endif
101
102
103 $(OUT)%.s: %.c
104         @echo "  Compiling to assembler $@"
105         $(Q)$(CC) $(CFLAGS16INC) -S -c $< -o $@
106
107 $(OUT)%.lds: %.lds.S
108         @echo "  Precompiling $@"
109         $(Q)$(CPP) -P -D__ASSEMBLY__ $< -o $@
110
111 $(OUT)asm-offsets.h: $(OUT)asm-offsets.s
112         @echo "  Generating offset file $@"
113         $(Q)./tools/gen-offsets.sh $< $@
114
115
116 $(OUT)ccode.16.s: ; $(call whole-compile, $(CFLAGS16) -S, $(addprefix src/, $(SRC16)),$@)
117
118 $(OUT)code32seg.o: ; $(call whole-compile, $(CFLAGS32SEG), $(addprefix src/, $(SRC32SEG)),$@)
119
120 $(OUT)ccode32flat.o: ; $(call whole-compile, $(CFLAGS32FLAT), $(addprefix src/, $(SRC32FLAT)),$@)
121
122 $(OUT)code16.o: romlayout.S $(OUT)ccode.16.s $(OUT)asm-offsets.h
123         @echo "  Compiling (16bit) $@"
124         $(Q)$(CC) $(CFLAGS16INC) -c -D__ASSEMBLY__ $< -o $@
125
126 $(OUT)romlayout16.lds $(OUT)romlayout32seg.lds $(OUT)romlayout32flat.lds $(OUT)code32flat.o: $(OUT)ccode32flat.o $(OUT)code32seg.o $(OUT)code16.o tools/layoutrom.py
127         @echo "  Building ld scripts (version \"$(VERSION)\")"
128         $(Q)echo 'const char VERSION[] = "$(VERSION)";' > $(OUT)version.c
129         $(Q)$(CC) $(CFLAGS32FLAT) -c $(OUT)version.c -o $(OUT)version.o
130         $(Q)$(LD) -melf_i386 -r $(OUT)ccode32flat.o $(OUT)version.o -o $(OUT)code32flat.o
131         $(Q)$(OBJDUMP) -thr $(OUT)code32flat.o > $(OUT)code32flat.o.objdump
132         $(Q)$(OBJDUMP) -thr $(OUT)code32seg.o > $(OUT)code32seg.o.objdump
133         $(Q)$(OBJDUMP) -thr $(OUT)code16.o > $(OUT)code16.o.objdump
134         $(Q)./tools/layoutrom.py $(OUT)code16.o.objdump $(OUT)code32seg.o.objdump $(OUT)code32flat.o.objdump $(OUT)romlayout16.lds $(OUT)romlayout32seg.lds $(OUT)romlayout32flat.lds
135
136
137 $(OUT)rom16.o: $(OUT)code16.o $(OUT)rom32flat.o $(OUT)romlayout16.lds
138         @echo "  Linking (no relocs) $@"
139         $(Q)$(LD) -r -T $(OUT)romlayout16.lds $< -o $@
140
141 $(OUT)rom32seg.o: $(OUT)code32seg.o $(OUT)romlayout32seg.lds
142         @echo "  Linking (no relocs) $@"
143         $(Q)$(LD) -r -T $(OUT)romlayout32seg.lds $< -o $@
144
145 $(OUT)rom32flat.o: $(OUT)code32flat.o $(OUT)romlayout32flat.lds
146         @echo "  Linking (no relocs) $@"
147         $(Q)$(LD) -r -T $(OUT)romlayout32flat.lds $< -o $@
148
149 $(OUT)rom.o: $(OUT)rom16.o $(OUT)rom32seg.o $(OUT)rom32flat.o $(OUT)rombios16.lds  $(OUT)rombios32seg.lds $(OUT)rombios.lds
150         @echo "  Linking $@"
151         $(Q)$(LD) -T $(OUT)rombios16.lds $(OUT)rom16.o -R $(OUT)rom32seg.o -R $(OUT)rom32flat.o -o $(OUT)rom16.reloc.o
152         $(Q)$(STRIP) $(OUT)rom16.reloc.o -o $(OUT)rom16.final.o
153         $(Q)$(OBJCOPY) --adjust-vma 0xf0000 $(OUT)rom16.o $(OUT)rom16.moved.o
154         $(Q)$(LD) -T $(OUT)rombios32seg.lds $(OUT)rom32seg.o -R $(OUT)rom16.o -R $(OUT)rom32flat.o -o $(OUT)rom32seg.reloc.o
155         $(Q)$(STRIP) $(OUT)rom32seg.reloc.o -o $(OUT)rom32seg.final.o
156         $(Q)$(OBJCOPY) --adjust-vma 0xf0000 $(OUT)rom32seg.o $(OUT)rom32seg.moved.o
157         $(Q)$(LD) -T $(OUT)rombios.lds $(OUT)rom16.final.o $(OUT)rom32seg.final.o $(OUT)rom32flat.o -R $(OUT)rom16.moved.o -R $(OUT)rom32seg.moved.o -o $@
158
159 $(OUT)bios.bin.elf $(OUT)bios.bin: $(OUT)rom.o tools/checkrom.py
160         @echo "  Prepping $@"
161         $(Q)$(OBJDUMP) -thr $< > $<.objdump
162         $(Q)$(OBJCOPY) -O binary $< $(OUT)bios.bin.raw
163         $(Q)./tools/checkrom.py $<.objdump $(OUT)bios.bin.raw $(OUT)bios.bin
164         $(Q)$(STRIP) -R .comment $< -o $(OUT)bios.bin.elf
165
166
167 ################ VGA build rules
168
169 # VGA src files
170 SRCVGA=src/output.c src/util.c vgasrc/vga.c vgasrc/vgafb.c vgasrc/vgaio.c \
171        vgasrc/vgatables.c vgasrc/vgafonts.c vgasrc/clext.c
172
173 $(OUT)vgaccode.16.s: ; $(call whole-compile, $(CFLAGS16) -S -Isrc, $(SRCVGA),$@)
174
175 $(OUT)vgalayout16.o: vgaentry.S $(OUT)vgaccode.16.s $(OUT)asm-offsets.h
176         @echo "  Compiling (16bit) $@"
177         $(Q)$(CC) $(CFLAGS16INC) -c -D__ASSEMBLY__ -Isrc $< -o $@
178
179 $(OUT)vgarom.o: $(OUT)vgalayout16.o $(OUT)vgalayout.lds
180         @echo "  Linking $@"
181         $(Q)$(LD) --gc-sections -T $(OUT)vgalayout.lds $(OUT)vgalayout16.o -o $@
182
183 $(OUT)vgabios.bin.raw: $(OUT)vgarom.o
184         @echo "  Extracting binary $@"
185         $(Q)$(OBJCOPY) -O binary $< $@
186
187 $(OUT)vgabios.bin: $(OUT)vgabios.bin.raw tools/buildrom.py
188         @echo "  Finalizing rom $@"
189         $(Q)./tools/buildrom.py $< $@
190
191 ####### dsdt build rules
192 src/acpi-dsdt.hex: src/acpi-dsdt.dsl
193         @echo "Compiling DSDT"
194         $(Q)cpp -P $< > $(OUT)acpi-dsdt.dsl.i
195         $(Q)iasl -tc -p $@ $(OUT)acpi-dsdt.dsl.i
196         $(Q)rm $(OUT)acpi-dsdt.dsl.i
197
198 ####### Generic rules
199 clean:
200         $(Q)rm -rf $(OUT)
201
202 $(OUT):
203         $(Q)mkdir $@
204
205 -include $(OUT)*.d