c97ed8f891e2f7a10bd0056e9f8dc3d65ed32246
[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-1.6.4-$(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 stacks.c pmm.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 ahci.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 \
23     biostables.c xen.c bmp.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 /dev/null 2>&1`" \
27     ; then echo "$(2)"; else echo "$(3)"; fi ;)
28
29 # Default compiler flags
30 COMMONCFLAGS = -I$(OUT) -Os -MD \
31     -Wall -Wno-strict-aliasing -Wold-style-definition \
32     $(call cc-option,$(CC),-Wtype-limits,) \
33     -m32 -march=i386 -mregparm=3 -mpreferred-stack-boundary=2 \
34     -mrtd -minline-all-stringops \
35     -freg-struct-return -ffreestanding -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) -DMODE16=0 -DMODESEGMENT=0 -g -fomit-frame-pointer
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 -fomit-frame-pointer
46 CFLAGS16INC = $(CFLAGSSEG) -DMODE16=1 \
47     $(call cc-option,$(CC),--param large-stack-frame=4,-fno-inline)
48 CFLAGS16 = $(CFLAGS16INC) -g -fomit-frame-pointer
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 MAKEFLAGS += --no-print-directory
58 endif
59
60 OBJCOPY=objcopy
61 OBJDUMP=objdump
62 STRIP=strip
63
64 .PHONY : all clean distclean FORCE
65
66 vpath %.c src vgasrc
67 vpath %.S src vgasrc
68
69 ################ Build rules
70
71 # Verify the gcc configuration and test if -fwhole-program works.
72 TESTGCC:=$(shell CC="$(CC)" LD="$(LD)" tools/test-gcc.sh)
73 ifeq "$(TESTGCC)" "-1"
74 $(error "Please upgrade GCC and/or binutils")
75 endif
76
77 ifndef COMPSTRAT
78 COMPSTRAT=$(TESTGCC)
79 endif
80
81 # Do a whole file compile - three methods are supported.
82 ifeq "$(COMPSTRAT)" "1"
83 # First method - use -fwhole-program without -combine.
84 define whole-compile
85 @echo "  Compiling whole program $3"
86 $(Q)printf '$(foreach i,$2,#include "../$i"\n)' > $3.tmp.c
87 $(Q)$(CC) $1 -fwhole-program -DWHOLE_PROGRAM -c $3.tmp.c -o $3
88 endef
89 else
90 ifeq "$(COMPSTRAT)" "2"
91 # Second menthod - don't use -fwhole-program at all.
92 define whole-compile
93 @echo "  Compiling whole program $3"
94 $(Q)printf '$(foreach i,$2,#include "../$i"\n)' > $3.tmp.c
95 $(Q)$(CC) $1 -c $3.tmp.c -o $3
96 endef
97 else
98 # Third (and preferred) method - use -fwhole-program with -combine
99 define whole-compile
100 @echo "  Compiling whole program $3"
101 $(Q)$(CC) $1 -fwhole-program -DWHOLE_PROGRAM -combine -c $2 -o $3
102 endef
103 endif
104 endif
105
106 %.strip.o: %.o
107         @echo "  Stripping $@"
108         $(Q)$(STRIP) $< -o $@
109
110 $(OUT)%.s: %.c
111         @echo "  Compiling to assembler $@"
112         $(Q)$(CC) $(CFLAGS16INC) -S -c $< -o $@
113
114 $(OUT)%.lds: %.lds.S
115         @echo "  Precompiling $@"
116         $(Q)$(CPP) -P -D__ASSEMBLY__ $< -o $@
117
118 $(OUT)asm-offsets.s: $(OUT)autoconf.h
119
120 $(OUT)asm-offsets.h: $(OUT)asm-offsets.s
121         @echo "  Generating offset file $@"
122         $(Q)./tools/gen-offsets.sh $< $@
123
124
125 $(OUT)ccode.16.s: $(OUT)autoconf.h ; $(call whole-compile, $(CFLAGS16) -S, $(addprefix src/, $(SRC16)),$@)
126
127 $(OUT)code32seg.o: $(OUT)autoconf.h ; $(call whole-compile, $(CFLAGS32SEG), $(addprefix src/, $(SRC32SEG)),$@)
128
129 $(OUT)ccode32flat.o: $(OUT)autoconf.h ; $(call whole-compile, $(CFLAGS32FLAT), $(addprefix src/, $(SRC32FLAT)),$@)
130
131 $(OUT)code16.o: romlayout.S $(OUT)ccode.16.s $(OUT)asm-offsets.h
132         @echo "  Compiling (16bit) $@"
133         $(Q)$(CC) $(CFLAGS16INC) -c -D__ASSEMBLY__ $< -o $@
134
135 $(OUT)romlayout16.lds: $(OUT)ccode32flat.o $(OUT)code32seg.o $(OUT)code16.o tools/layoutrom.py
136         @echo "  Building ld scripts (version \"$(VERSION)\")"
137         $(Q)echo 'const char VERSION[] = "$(VERSION)";' > $(OUT)version.c
138         $(Q)$(CC) $(CFLAGS32FLAT) -c $(OUT)version.c -o $(OUT)version.o
139         $(Q)$(LD) -melf_i386 -r $(OUT)ccode32flat.o $(OUT)version.o -o $(OUT)code32flat.o
140         $(Q)$(OBJDUMP) -thr $(OUT)code32flat.o > $(OUT)code32flat.o.objdump
141         $(Q)$(OBJDUMP) -thr $(OUT)code32seg.o > $(OUT)code32seg.o.objdump
142         $(Q)$(OBJDUMP) -thr $(OUT)code16.o > $(OUT)code16.o.objdump
143         $(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
144
145 # These are actually built by tools/layoutrom.py above, but by pulling them
146 # into an extra rule we prevent make -j from spawning layoutrom.py 4 times.
147 $(OUT)romlayout32seg.lds $(OUT)romlayout32flat.lds $(OUT)code32flat.o: $(OUT)romlayout16.lds
148
149 $(OUT)rom16.o: $(OUT)code16.o $(OUT)romlayout16.lds
150         @echo "  Linking $@"
151         $(Q)$(LD) -T $(OUT)romlayout16.lds $< -o $@
152
153 $(OUT)rom32seg.o: $(OUT)code32seg.o $(OUT)romlayout32seg.lds
154         @echo "  Linking $@"
155         $(Q)$(LD) -T $(OUT)romlayout32seg.lds $< -o $@
156
157 $(OUT)rom.o: $(OUT)rom16.strip.o $(OUT)rom32seg.strip.o $(OUT)code32flat.o $(OUT)romlayout32flat.lds
158         @echo "  Linking $@"
159         $(Q)$(LD) -T $(OUT)romlayout32flat.lds $(OUT)rom16.strip.o $(OUT)rom32seg.strip.o $(OUT)code32flat.o -o $@
160
161 $(OUT)bios.bin.elf $(OUT)bios.bin: $(OUT)rom.o tools/checkrom.py
162         @echo "  Prepping $@"
163         $(Q)$(OBJDUMP) -thr $< > $<.objdump
164         $(Q)$(OBJCOPY) -O binary $< $(OUT)bios.bin.raw
165         $(Q)./tools/checkrom.py $<.objdump $(OUT)bios.bin.raw $(OUT)bios.bin
166         $(Q)$(STRIP) -R .comment $< -o $(OUT)bios.bin.elf
167
168
169 ################ VGA build rules
170
171 # VGA src files
172 SRCVGA=src/output.c src/util.c src/pci.c vgasrc/vgabios.c vgasrc/vgafb.c \
173     vgasrc/vgatables.c vgasrc/vgafonts.c vgasrc/vbe.c \
174     vgasrc/stdvga.c vgasrc/clext.c vgasrc/bochsvga.c vgasrc/geodevga.c
175
176 CFLAGS16VGA = $(CFLAGS16INC) -g -Isrc
177
178 $(OUT)vgaccode.16.s: $(OUT)autoconf.h ; $(call whole-compile, $(CFLAGS16VGA) -S, $(SRCVGA),$@)
179
180 $(OUT)vgalayout16.o: vgaentry.S $(OUT)vgaccode.16.s $(OUT)asm-offsets.h
181         @echo "  Compiling (16bit) $@"
182         $(Q)$(CC) $(CFLAGS16INC) -c -D__ASSEMBLY__ -Isrc $< -o $@
183
184 $(OUT)vgarom.o: $(OUT)vgalayout16.o $(OUT)vgalayout.lds
185         @echo "  Linking $@"
186         $(Q)$(LD) --gc-sections -T $(OUT)vgalayout.lds $(OUT)vgalayout16.o -o $@
187
188 $(OUT)vgabios.bin.raw: $(OUT)vgarom.o
189         @echo "  Extracting binary $@"
190         $(Q)$(OBJCOPY) -O binary $< $@
191
192 $(OUT)vgabios.bin: $(OUT)vgabios.bin.raw tools/buildrom.py
193         @echo "  Finalizing rom $@"
194         $(Q)./tools/buildrom.py $< $@
195
196 ####### dsdt build rules
197 src/%.hex: src/%.dsl ./tools/acpi_extract_preprocess.py ./tools/acpi_extract.py
198         @echo "Compiling DSDT"
199         $(Q)cpp -P $< > $(OUT)$*.dsl.i.orig
200         $(Q)./tools/acpi_extract_preprocess.py $(OUT)$*.dsl.i.orig > $(OUT)$*.dsl.i
201         $(Q)iasl -l -tc -p $(OUT)$* $(OUT)$*.dsl.i
202         $(Q)./tools/acpi_extract.py $(OUT)$*.lst > $(OUT)$*.off
203         $(Q)cat $(OUT)$*.off > $@
204
205 $(OUT)ccode32flat.o: src/acpi-dsdt.hex src/ssdt-proc.hex src/ssdt-pcihp.hex
206
207 ####### Kconfig rules
208 export HOSTCC             := $(CC)
209 export CONFIG_SHELL       := sh
210 export KCONFIG_AUTOHEADER := autoconf.h
211 export KCONFIG_CONFIG     := $(CURDIR)/.config
212
213 $(OUT)autoconf.h : $(KCONFIG_CONFIG)
214         $(Q)$(MAKE) silentoldconfig
215
216 $(KCONFIG_CONFIG):
217         $(Q)$(MAKE) defconfig
218
219 %onfig:
220         $(Q)mkdir -p $(OUT)/tools/kconfig/lxdialog
221         $(Q)mkdir -p $(OUT)/include/config
222         $(Q)$(MAKE) -C $(OUT) -f $(CURDIR)/tools/kconfig/Makefile srctree=$(CURDIR) src=tools/kconfig obj=tools/kconfig Q=$(Q) Kconfig=$(CURDIR)/src/Kconfig $@
223
224 help:
225         $(Q)$(MAKE) -f $(CURDIR)/tools/kconfig/Makefile help
226
227 ####### Generic rules
228 clean:
229         $(Q)rm -rf $(OUT)
230
231 distclean: clean
232         $(Q)rm -f .config .config.old
233
234 $(OUT):
235         $(Q)mkdir $@
236
237 -include $(OUT)*.d