3 # Copyright (C) 2008-2012 Kevin O'Connor <kevin@koconnor.net>
5 # This file may be distributed under the terms of the GNU LGPLv3 license.
10 # Common command definitions
11 export HOSTCC := $(CC)
12 export CONFIG_SHELL := sh
13 export KCONFIG_AUTOHEADER := autoconf.h
14 export KCONFIG_CONFIG := $(CURDIR)/.config
17 ifneq ($(CROSS_PREFIX),)
22 OBJCOPY=$(CROSS_PREFIX)objcopy
23 OBJDUMP=$(CROSS_PREFIX)objdump
24 STRIP=$(CROSS_PREFIX)strip
28 LD32BIT_FLAG:=-melf_i386
31 SRCBOTH=misc.c stacks.c output.c string.c block.c cdrom.c disk.c mouse.c kbd.c \
32 system.c serial.c clock.c resume.c pnpbios.c vgahooks.c pcibios.c apm.c \
33 hw/pci.c hw/timer.c hw/rtc.c hw/dma.c hw/pic.c hw/ps2port.c hw/serialio.c \
34 hw/usb.c hw/usb-uhci.c hw/usb-ohci.c hw/usb-ehci.c \
35 hw/usb-hid.c hw/usb-msc.c hw/usb-uas.c \
36 hw/blockcmd.c hw/floppy.c hw/ata.c hw/ramdisk.c \
37 hw/lsi-scsi.c hw/esp-scsi.c hw/megasas.c
39 SRC32FLAT=$(SRCBOTH) post.c e820map.c malloc.c romfile.c x86.c optionroms.c \
40 pmm.c font.c boot.c bootsplash.c jpeg.c bmp.c tcgbios.c sha1.c \
41 hw/ahci.c hw/pvscsi.c hw/usb-xhci.c hw/usb-hub.c hw/sdcard.c \
42 fw/coreboot.c fw/lzmadecode.c fw/multiboot.c fw/csm.c fw/biostables.c \
43 fw/paravirt.c fw/shadow.c fw/pciinit.c fw/smm.c fw/smp.c fw/mtrr.c fw/xen.c \
44 fw/acpi.c fw/mptable.c fw/pirtable.c fw/smbios.c fw/romfile_loader.c \
45 hw/virtio-ring.c hw/virtio-pci.c hw/virtio-blk.c hw/virtio-scsi.c \
47 SRC32SEG=string.c output.c pcibios.c apm.c stacks.c hw/pci.c hw/serialio.c
48 DIRS=src src/hw src/fw vgasrc
50 # Default compiler flags
51 cc-option=$(shell if test -z "`$(1) $(2) -S -o /dev/null -xc /dev/null 2>&1`" \
52 ; then echo "$(2)"; else echo "$(3)"; fi ;)
56 CPPFLAGS = -P -MD -MT $@
58 COMMONCFLAGS := -I$(OUT) -Isrc -Os -MD -g \
59 -Wall -Wno-strict-aliasing -Wold-style-definition \
60 $(call cc-option,$(CC),-Wtype-limits,) \
61 -m32 -march=i386 -mregparm=3 -mpreferred-stack-boundary=2 \
62 -minline-all-stringops -fomit-frame-pointer \
63 -freg-struct-return -ffreestanding -fno-delete-null-pointer-checks \
64 -ffunction-sections -fdata-sections -fno-common -fno-merge-constants
65 COMMONCFLAGS += $(call cc-option,$(CC),-nopie,)
66 COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector,)
67 COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector-all,)
68 COMMONCFLAGS += $(call cc-option,$(CC),-fstack-check=no,)
71 CFLAGS32FLAT := $(COMMONCFLAGS) -DMODE16=0 -DMODESEGMENT=0
72 CFLAGSSEG := $(COMMONCFLAGS) -DMODESEGMENT=1 -fno-defer-pop \
73 $(call cc-option,$(CC),-fno-jump-tables,-DMANUAL_NO_JUMP_TABLE) \
74 $(call cc-option,$(CC),-fno-tree-switch-conversion,)
75 CFLAGS32SEG := $(CFLAGSSEG) -DMODE16=0
76 CFLAGS16 := $(CFLAGSSEG) -DMODE16=1 \
77 $(call cc-option,$(CC),-m16,-Wa$(COMMA)src/code16gcc.s) \
78 $(call cc-option,$(CC),--param large-stack-frame=4,-fno-inline)
80 # Run with "make V=1" to see the actual compile commands
85 MAKEFLAGS += --no-print-directory
89 -include $(KCONFIG_CONFIG)
92 target-$(CONFIG_QEMU) += $(OUT)bios.bin
93 target-$(CONFIG_CSM) += $(OUT)Csm16.bin
94 target-$(CONFIG_COREBOOT) += $(OUT)bios.bin.elf
95 target-$(CONFIG_BUILD_VGABIOS) += $(OUT)vgabios.bin
100 .PHONY : all clean distclean FORCE
104 ################ Common build rules
106 # Verify the build environment works.
107 TESTGCC:=$(shell OUT="$(OUT)" CC="$(CC)" LD="$(LD)" IASL="$(IASL)" scripts/test-build.sh)
108 ifeq "$(TESTGCC)" "-1"
109 $(error "Please upgrade the build environment")
112 ifeq "$(TESTGCC)" "0"
113 # Use -fwhole-program
114 CFLAGSWHOLE=-fwhole-program -DWHOLE_PROGRAM
117 # Do a whole file compile by textually including all C code.
119 @echo " Compiling whole program $3"
120 $(Q)printf '$(foreach i,$2,#include "$i"\n)' > $3.tmp.c
121 $(Q)$(CC) -I. $1 $(CFLAGSWHOLE) -c $3.tmp.c -o $3
125 @echo " Stripping $@"
126 $(Q)$(STRIP) $< -o $@
129 @echo " Compiling to assembler $@"
130 $(Q)$(CC) $(CFLAGS16) -S -c $< -o $@
132 $(OUT)%.o: %.c $(OUT)autoconf.h
133 @echo " Compile checking $@"
134 $(Q)$(CC) $(CFLAGS32FLAT) -c $< -o $@
137 @echo " Precompiling $@"
138 $(Q)$(CPP) $(CPPFLAGS) -D__ASSEMBLY__ $< -o $@
141 ################ Main BIOS build rules
143 $(OUT)asm-offsets.s: $(OUT)autoconf.h
145 $(OUT)asm-offsets.h: $(OUT)src/asm-offsets.s
146 @echo " Generating offset file $@"
147 $(Q)./scripts/gen-offsets.sh $< $@
149 $(OUT)ccode16.o: $(OUT)autoconf.h $(patsubst %.c, $(OUT)src/%.o,$(SRC16)) ; $(call whole-compile, $(CFLAGS16), $(addprefix src/, $(SRC16)),$@)
151 $(OUT)code32seg.o: $(OUT)autoconf.h $(patsubst %.c, $(OUT)src/%.o,$(SRC32SEG)) ; $(call whole-compile, $(CFLAGS32SEG), $(addprefix src/, $(SRC32SEG)),$@)
153 $(OUT)ccode32flat.o: $(OUT)autoconf.h $(patsubst %.c, $(OUT)src/%.o,$(SRC32FLAT)) ; $(call whole-compile, $(CFLAGS32FLAT), $(addprefix src/, $(SRC32FLAT)),$@)
155 $(OUT)romlayout.o: src/romlayout.S $(OUT)autoconf.h $(OUT)asm-offsets.h
156 @echo " Compiling (16bit) $@"
157 $(Q)$(CC) $(CFLAGS16) -c -D__ASSEMBLY__ $< -o $@
159 $(OUT)romlayout16.lds: $(OUT)ccode32flat.o $(OUT)code32seg.o $(OUT)ccode16.o $(OUT)romlayout.o src/version.c scripts/layoutrom.py scripts/buildversion.py
160 @echo " Building ld scripts"
161 $(Q)$(PYTHON) ./scripts/buildversion.py -e "$(EXTRAVERSION)" -t "$(CC);$(AS);$(LD);$(OBJCOPY);$(OBJDUMP);$(STRIP)" $(OUT)autoversion.h
162 $(Q)$(CC) $(CFLAGS32FLAT) -c src/version.c -o $(OUT)version.o
163 $(Q)$(LD) $(LD32BIT_FLAG) -r $(OUT)ccode32flat.o $(OUT)version.o -o $(OUT)code32flat.o
164 $(Q)$(LD) $(LD32BIT_FLAG) -r $(OUT)ccode16.o $(OUT)romlayout.o -o $(OUT)code16.o
165 $(Q)$(OBJDUMP) -thr $(OUT)code32flat.o > $(OUT)code32flat.o.objdump
166 $(Q)$(OBJDUMP) -thr $(OUT)code32seg.o > $(OUT)code32seg.o.objdump
167 $(Q)$(OBJDUMP) -thr $(OUT)code16.o > $(OUT)code16.o.objdump
168 $(Q)$(PYTHON) ./scripts/layoutrom.py $(OUT)code16.o.objdump $(OUT)code32seg.o.objdump $(OUT)code32flat.o.objdump $(OUT)$(KCONFIG_AUTOHEADER) $(OUT)romlayout16.lds $(OUT)romlayout32seg.lds $(OUT)romlayout32flat.lds
170 # These are actually built by scripts/layoutrom.py above, but by pulling them
171 # into an extra rule we prevent make -j from spawning layoutrom.py 4 times.
172 $(OUT)romlayout32seg.lds $(OUT)romlayout32flat.lds $(OUT)code32flat.o $(OUT)code16.o: $(OUT)romlayout16.lds
174 $(OUT)rom16.o: $(OUT)code16.o $(OUT)romlayout16.lds
176 $(Q)$(LD) -T $(OUT)romlayout16.lds $< -o $@
178 $(OUT)rom32seg.o: $(OUT)code32seg.o $(OUT)romlayout32seg.lds
180 $(Q)$(LD) -T $(OUT)romlayout32seg.lds $< -o $@
182 $(OUT)rom.o: $(OUT)rom16.strip.o $(OUT)rom32seg.strip.o $(OUT)code32flat.o $(OUT)romlayout32flat.lds
184 $(Q)$(LD) -N -T $(OUT)romlayout32flat.lds $(OUT)rom16.strip.o $(OUT)rom32seg.strip.o $(OUT)code32flat.o -o $@
186 $(OUT)bios.bin.prep: $(OUT)rom.o scripts/checkrom.py
188 $(Q)rm -f $(OUT)bios.bin $(OUT)Csm16.bin $(OUT)bios.bin.elf
189 $(Q)$(OBJDUMP) -thr $< > $<.objdump
190 $(Q)$(OBJCOPY) -O binary $< $(OUT)bios.bin.raw
191 $(Q)$(PYTHON) ./scripts/checkrom.py $<.objdump $(CONFIG_ROM_SIZE) $(OUT)bios.bin.raw $(OUT)bios.bin.prep
193 $(OUT)bios.bin: $(OUT)bios.bin.prep
197 $(OUT)Csm16.bin: $(OUT)bios.bin.prep
201 $(OUT)bios.bin.elf: $(OUT)rom.o $(OUT)bios.bin.prep
203 $(Q)$(STRIP) -R .comment $< -o $(OUT)bios.bin.elf
206 ################ VGA build rules
209 SRCVGA=src/output.c src/string.c src/hw/pci.c src/hw/serialio.c \
210 vgasrc/vgainit.c vgasrc/vgabios.c vgasrc/vgafb.c \
211 vgasrc/vgafonts.c vgasrc/vbe.c \
212 vgasrc/stdvga.c vgasrc/stdvgamodes.c vgasrc/stdvgaio.c \
213 vgasrc/clext.c vgasrc/bochsvga.c vgasrc/geodevga.c \
214 src/fw/coreboot.c vgasrc/cbvga.c
216 ifeq "$(CONFIG_VGA_FIXUP_ASM)" "y"
217 $(OUT)vgaccode16.raw.s: $(OUT)autoconf.h $(patsubst %.c, $(OUT)%.o,$(SRCVGA)) ; $(call whole-compile, $(filter-out -fomit-frame-pointer,$(CFLAGS16)) -fno-omit-frame-pointer -S -Isrc, $(SRCVGA),$@)
219 $(OUT)vgaccode16.o: $(OUT)vgaccode16.raw.s scripts/vgafixup.py
220 @echo " Fixup VGA rom assembler"
221 $(Q)$(PYTHON) ./scripts/vgafixup.py $< $(OUT)vgaccode16.s
222 $(Q)$(AS) --32 src/code16gcc.s $(OUT)vgaccode16.s -o $@
224 $(OUT)vgaccode16.o: $(OUT)autoconf.h $(patsubst %.c, $(OUT)%.o,$(SRCVGA)) ; $(call whole-compile, $(CFLAGS16) -Isrc, $(SRCVGA),$@)
227 $(OUT)vgaentry.o: vgasrc/vgaentry.S $(OUT)autoconf.h $(OUT)asm-offsets.h
228 @echo " Compiling (16bit) $@"
229 $(Q)$(CC) $(CFLAGS16) -c -D__ASSEMBLY__ $< -o $@
231 $(OUT)vgarom.o: $(OUT)vgaccode16.o $(OUT)vgaentry.o $(OUT)vgasrc/vgalayout.lds vgasrc/vgaversion.c scripts/buildversion.py
233 $(Q)$(PYTHON) ./scripts/buildversion.py -e "$(EXTRAVERSION)" -t "$(CC);$(AS);$(LD);$(OBJCOPY);$(OBJDUMP);$(STRIP)" $(OUT)autovgaversion.h
234 $(Q)$(CC) $(CFLAGS16) -c vgasrc/vgaversion.c -o $(OUT)vgaversion.o
235 $(Q)$(LD) --gc-sections -T $(OUT)vgasrc/vgalayout.lds $(OUT)vgaccode16.o $(OUT)vgaentry.o $(OUT)vgaversion.o -o $@
237 $(OUT)vgabios.bin.raw: $(OUT)vgarom.o
238 @echo " Extracting binary $@"
239 $(Q)$(OBJCOPY) -O binary $< $@
241 $(OUT)vgabios.bin: $(OUT)vgabios.bin.raw scripts/buildrom.py
242 @echo " Finalizing rom $@"
243 $(Q)$(PYTHON) ./scripts/buildrom.py $< $@
246 ################ DSDT build rules
248 iasl-option=$(shell if test -z "`$(1) $(2) 2>&1 > /dev/null`" \
249 ; then echo "$(2)"; else echo "$(3)"; fi ;)
251 $(OUT)%.hex: %.dsl ./scripts/acpi_extract_preprocess.py ./scripts/acpi_extract.py
252 @echo " Compiling IASL $@"
253 $(Q)$(CPP) $(CPPFLAGS) $< -o $(OUT)$*.dsl.i.orig
254 $(Q)$(PYTHON) ./scripts/acpi_extract_preprocess.py $(OUT)$*.dsl.i.orig > $(OUT)$*.dsl.i
255 $(Q)$(IASL) $(call iasl-option,$(IASL),-Pn,) -vs -l -tc -p $(OUT)$* $(OUT)$*.dsl.i
256 $(Q)$(PYTHON) ./scripts/acpi_extract.py $(OUT)$*.lst > $(OUT)$*.off
257 $(Q)cat $(OUT)$*.off > $@
259 $(OUT)src/fw/acpi.o: $(OUT)src/fw/acpi-dsdt.hex $(OUT)src/fw/ssdt-proc.hex $(OUT)src/fw/ssdt-pcihp.hex $(OUT)src/fw/ssdt-misc.hex $(OUT)src/fw/q35-acpi-dsdt.hex
261 ################ Kconfig rules
264 $(Q)mkdir -p $(OUT)/scripts/kconfig/lxdialog
265 $(Q)mkdir -p $(OUT)/include/config
266 $(Q)mkdir -p $(addprefix $(OUT), $(DIRS))
267 $(Q)$(MAKE) -C $(OUT) -f $(CURDIR)/scripts/kconfig/Makefile srctree=$(CURDIR) src=scripts/kconfig obj=scripts/kconfig Q=$(Q) Kconfig=$(CURDIR)/src/Kconfig $1
270 $(OUT)autoconf.h : $(KCONFIG_CONFIG) ; $(call do-kconfig, silentoldconfig)
271 $(KCONFIG_CONFIG): src/Kconfig vgasrc/Kconfig ; $(call do-kconfig, olddefconfig)
272 %onfig: ; $(call do-kconfig, $@)
273 help: ; $(call do-kconfig, $@)
276 ################ Generic rules
282 $(Q)rm -f .config .config.old
284 -include $(patsubst %,$(OUT)%/*.d,$(DIRS))