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