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