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