1# SeaBIOS build system
2#
3# Copyright (C) 2008-2012  Kevin O'Connor <kevin@koconnor.net>
4#
5# This file may be distributed under the terms of the GNU LGPLv3 license.
6
7# Output directory
8OUT=out/
9
10# Common command definitions
11export HOSTCC             := $(CC)
12export CONFIG_SHELL       := sh
13export KCONFIG_AUTOHEADER := autoconf.h
14export KCONFIG_CONFIG     := $(CURDIR)/.config
15export LC_ALL             := C
16CROSS_PREFIX=
17ifneq ($(CROSS_PREFIX),)
18CC=$(CROSS_PREFIX)gcc
19endif
20AS=$(CROSS_PREFIX)as
21LD=$(CROSS_PREFIX)ld
22OBJCOPY=$(CROSS_PREFIX)objcopy
23OBJDUMP=$(CROSS_PREFIX)objdump
24STRIP=$(CROSS_PREFIX)strip
25PYTHON=python
26CPP=cpp
27IASL:=iasl
28LD32BIT_FLAG:=-melf_i386
29
30# Source files
31SRCBOTH=misc.c stacks.c output.c string.c block.c cdrom.c disk.c mouse.c kbd.c \
32    system.c serial.c sercon.c clock.c resume.c pnpbios.c vgahooks.c pcibios.c \
33    apm.c cp437.c \
34    hw/pci.c hw/timer.c hw/rtc.c hw/dma.c hw/pic.c hw/ps2port.c hw/serialio.c \
35    hw/usb.c hw/usb-uhci.c hw/usb-ohci.c hw/usb-ehci.c \
36    hw/usb-hid.c hw/usb-msc.c hw/usb-uas.c \
37    hw/blockcmd.c hw/floppy.c hw/ata.c hw/ramdisk.c \
38    hw/lsi-scsi.c hw/esp-scsi.c hw/megasas.c hw/mpt-scsi.c
39SRC16=$(SRCBOTH)
40SRC32FLAT=$(SRCBOTH) post.c e820map.c malloc.c romfile.c x86.c optionroms.c \
41    pmm.c font.c boot.c bootsplash.c jpeg.c bmp.c tcgbios.c sha1.c \
42    hw/pcidevice.c hw/ahci.c hw/pvscsi.c hw/usb-xhci.c hw/usb-hub.c hw/sdcard.c \
43    fw/coreboot.c fw/lzmadecode.c fw/multiboot.c fw/csm.c fw/biostables.c \
44    fw/paravirt.c fw/shadow.c fw/pciinit.c fw/smm.c fw/smp.c fw/mtrr.c fw/xen.c \
45    fw/acpi.c fw/mptable.c fw/pirtable.c fw/smbios.c fw/romfile_loader.c \
46    hw/virtio-ring.c hw/virtio-pci.c hw/virtio-blk.c hw/virtio-scsi.c \
47    hw/tpm_drivers.c hw/nvme.c
48SRC32SEG=string.c output.c pcibios.c apm.c stacks.c hw/pci.c hw/serialio.c
49DIRS=src src/hw src/fw vgasrc
50
51# Default compiler flags
52cc-option=$(shell if test -z "`$(1) $(2) -S -o /dev/null -xc /dev/null 2>&1`" \
53    ; then echo "$(2)"; else echo "$(3)"; fi ;)
54
55EXTRAVERSION=
56
57CPPFLAGS = -P -MD -MT $@
58
59COMMONCFLAGS := -I$(OUT) -Isrc -Os -MD -g \
60    -Wall -Wno-strict-aliasing -Wold-style-definition \
61    $(call cc-option,$(CC),-Wtype-limits,) \
62    -m32 -march=i386 -mregparm=3 -mpreferred-stack-boundary=2 \
63    -minline-all-stringops -fomit-frame-pointer \
64    -freg-struct-return -ffreestanding -fno-delete-null-pointer-checks \
65    -ffunction-sections -fdata-sections -fno-common -fno-merge-constants
66COMMONCFLAGS += $(call cc-option,$(CC),-nopie,)
67COMMONCFLAGS += $(call cc-option,$(CC),-fno-pie,)
68COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector,)
69COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector-all,)
70COMMONCFLAGS += $(call cc-option,$(CC),-fstack-check=no,)
71COMMONCFLAGS += $(call cc-option,$(CC),-Wno-address-of-packed-member,)
72COMMA := ,
73
74CFLAGS32FLAT := $(COMMONCFLAGS) -DMODE16=0 -DMODESEGMENT=0
75CFLAGSSEG := $(COMMONCFLAGS) -DMODESEGMENT=1 -fno-defer-pop \
76    $(call cc-option,$(CC),-fno-jump-tables,-DMANUAL_NO_JUMP_TABLE) \
77    $(call cc-option,$(CC),-fno-tree-switch-conversion,)
78CFLAGS32SEG := $(CFLAGSSEG) -DMODE16=0
79CFLAGS16 := $(CFLAGSSEG) -DMODE16=1 \
80    $(call cc-option,$(CC),-m16,-Wa$(COMMA)src/code16gcc.s) \
81    $(call cc-option,$(CC),--param large-stack-frame=4,-fno-inline)
82
83# Run with "make V=1" to see the actual compile commands
84ifdef V
85Q=
86else
87Q=@
88MAKEFLAGS += --no-print-directory
89endif
90
91# Default targets
92-include $(KCONFIG_CONFIG)
93
94target-y :=
95target-$(CONFIG_QEMU) += $(OUT)bios.bin
96target-$(CONFIG_CSM) += $(OUT)Csm16.bin
97target-$(CONFIG_COREBOOT) += $(OUT)bios.bin.elf
98target-$(CONFIG_BUILD_VGABIOS) += $(OUT)vgabios.bin
99
100all: $(target-y)
101
102# Make definitions
103.PHONY : all clean distclean FORCE
104.DELETE_ON_ERROR:
105
106
107################ Common build rules
108
109# Verify the build environment works.
110TESTGCC:=$(shell OUT="$(OUT)" CC="$(CC)" LD="$(LD)" IASL="$(IASL)" scripts/test-build.sh)
111ifeq "$(TESTGCC)" "-1"
112$(error "Please upgrade the build environment")
113endif
114
115ifeq "$(TESTGCC)" "0"
116# Use -fwhole-program
117CFLAGSWHOLE=-fwhole-program -DWHOLE_PROGRAM
118endif
119
120# Do a whole file compile by textually including all C code.
121define whole-compile
122@echo "  Compiling whole program $3"
123$(Q)printf '$(foreach i,$2,#include "$i"\n)' > $3.tmp.c
124$(Q)$(CC) -I. $1 $(CFLAGSWHOLE) -c $3.tmp.c -o $3
125endef
126
127%.strip.o: %.o
128	@echo "  Stripping $@"
129	$(Q)$(STRIP) $< -o $@
130
131$(OUT)%.s: %.c
132	@echo "  Compiling to assembler $@"
133	$(Q)$(CC) $(CFLAGS16) -S -c $< -o $@
134
135$(OUT)%.o: %.c $(OUT)autoconf.h
136	@echo "  Compile checking $@"
137	$(Q)$(CC) $(CFLAGS32FLAT) -c $< -o $@
138
139$(OUT)%.lds: %.lds.S
140	@echo "  Precompiling $@"
141	$(Q)$(CPP) $(CPPFLAGS) -D__ASSEMBLY__ $< -o $@
142
143
144################ Main BIOS build rules
145
146$(OUT)asm-offsets.s: $(OUT)autoconf.h
147
148$(OUT)asm-offsets.h: $(OUT)src/asm-offsets.s
149	@echo "  Generating offset file $@"
150	$(Q)./scripts/gen-offsets.sh $< $@
151
152$(OUT)ccode16.o: $(OUT)autoconf.h $(patsubst %.c, $(OUT)src/%.o,$(SRC16)) ; $(call whole-compile, $(CFLAGS16), $(addprefix src/, $(SRC16)),$@)
153
154$(OUT)code32seg.o: $(OUT)autoconf.h $(patsubst %.c, $(OUT)src/%.o,$(SRC32SEG)) ; $(call whole-compile, $(CFLAGS32SEG), $(addprefix src/, $(SRC32SEG)),$@)
155
156$(OUT)ccode32flat.o: $(OUT)autoconf.h $(patsubst %.c, $(OUT)src/%.o,$(SRC32FLAT)) ; $(call whole-compile, $(CFLAGS32FLAT), $(addprefix src/, $(SRC32FLAT)),$@)
157
158$(OUT)romlayout.o: src/romlayout.S $(OUT)autoconf.h $(OUT)asm-offsets.h
159	@echo "  Compiling (16bit) $@"
160	$(Q)$(CC) $(CFLAGS16) -c -D__ASSEMBLY__ $< -o $@
161
162$(OUT)romlayout16.lds: $(OUT)ccode32flat.o $(OUT)code32seg.o $(OUT)ccode16.o $(OUT)romlayout.o src/version.c scripts/layoutrom.py scripts/buildversion.py
163	@echo "  Building ld scripts"
164	$(Q)$(PYTHON) ./scripts/buildversion.py -e "$(EXTRAVERSION)" -t "$(CC);$(AS);$(LD);$(OBJCOPY);$(OBJDUMP);$(STRIP)" $(OUT)autoversion.h
165	$(Q)$(CC) $(CFLAGS32FLAT) -c src/version.c -o $(OUT)version.o
166	$(Q)$(LD) $(LD32BIT_FLAG) -r $(OUT)ccode32flat.o $(OUT)version.o -o $(OUT)code32flat.o
167	$(Q)$(LD) $(LD32BIT_FLAG) -r $(OUT)ccode16.o $(OUT)romlayout.o -o $(OUT)code16.o
168	$(Q)$(OBJDUMP) -thr $(OUT)code32flat.o > $(OUT)code32flat.o.objdump
169	$(Q)$(OBJDUMP) -thr $(OUT)code32seg.o > $(OUT)code32seg.o.objdump
170	$(Q)$(OBJDUMP) -thr $(OUT)code16.o > $(OUT)code16.o.objdump
171	$(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
172
173# These are actually built by scripts/layoutrom.py above, but by pulling them
174# into an extra rule we prevent make -j from spawning layoutrom.py 4 times.
175$(OUT)romlayout32seg.lds $(OUT)romlayout32flat.lds $(OUT)code32flat.o $(OUT)code16.o: $(OUT)romlayout16.lds
176
177$(OUT)rom16.o: $(OUT)code16.o $(OUT)romlayout16.lds
178	@echo "  Linking $@"
179	$(Q)$(LD) -T $(OUT)romlayout16.lds $< -o $@
180
181$(OUT)rom32seg.o: $(OUT)code32seg.o $(OUT)romlayout32seg.lds
182	@echo "  Linking $@"
183	$(Q)$(LD) -T $(OUT)romlayout32seg.lds $< -o $@
184
185$(OUT)rom.o: $(OUT)rom16.strip.o $(OUT)rom32seg.strip.o $(OUT)code32flat.o $(OUT)romlayout32flat.lds
186	@echo "  Linking $@"
187	$(Q)$(LD) -N -T $(OUT)romlayout32flat.lds $(OUT)rom16.strip.o $(OUT)rom32seg.strip.o $(OUT)code32flat.o -o $@
188
189$(OUT)bios.bin.prep: $(OUT)rom.o scripts/checkrom.py
190	@echo "  Prepping $@"
191	$(Q)rm -f $(OUT)bios.bin $(OUT)Csm16.bin $(OUT)bios.bin.elf
192	$(Q)$(OBJDUMP) -thr $< > $<.objdump
193	$(Q)$(OBJCOPY) -O binary $< $(OUT)bios.bin.raw
194	$(Q)$(PYTHON) ./scripts/checkrom.py $<.objdump $(CONFIG_ROM_SIZE) $(OUT)bios.bin.raw $(OUT)bios.bin.prep
195
196$(OUT)bios.bin: $(OUT)bios.bin.prep
197	@echo "  Creating $@"
198	$(Q)cp $< $@
199
200$(OUT)Csm16.bin: $(OUT)bios.bin.prep
201	@echo "  Creating $@"
202	$(Q)cp $< $@
203
204$(OUT)bios.bin.elf: $(OUT)rom.o $(OUT)bios.bin.prep
205	@echo "  Creating $@"
206	$(Q)$(STRIP) -R .comment $< -o $(OUT)bios.bin.elf
207
208
209################ VGA build rules
210
211# VGA src files
212SRCVGA=src/output.c src/string.c src/hw/pci.c src/hw/serialio.c \
213    vgasrc/vgainit.c vgasrc/vgabios.c vgasrc/vgafb.c vgasrc/swcursor.c \
214    vgasrc/vgafonts.c vgasrc/vbe.c \
215    vgasrc/stdvga.c vgasrc/stdvgamodes.c vgasrc/stdvgaio.c \
216    vgasrc/clext.c vgasrc/svgamodes.c vgasrc/atiext.c vgasrc/bochsvga.c vgasrc/geodevga.c \
217    src/fw/coreboot.c vgasrc/cbvga.c vgasrc/bochsdisplay.c vgasrc/ramfb.c
218
219ifeq "$(CONFIG_VGA_FIXUP_ASM)" "y"
220$(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),$@)
221
222$(OUT)vgaccode16.o: $(OUT)vgaccode16.raw.s scripts/vgafixup.py
223	@echo "  Fixup VGA rom assembler"
224	$(Q)$(PYTHON) ./scripts/vgafixup.py $< $(OUT)vgaccode16.s
225	$(Q)$(AS) --32 src/code16gcc.s $(OUT)vgaccode16.s -o $@
226else
227$(OUT)vgaccode16.o: $(OUT)autoconf.h $(patsubst %.c, $(OUT)%.o,$(SRCVGA)) ; $(call whole-compile, $(CFLAGS16) -Isrc, $(SRCVGA),$@)
228endif
229
230$(OUT)vgaentry.o: vgasrc/vgaentry.S $(OUT)autoconf.h $(OUT)asm-offsets.h
231	@echo "  Compiling (16bit) $@"
232	$(Q)$(CC) $(CFLAGS16) -c -D__ASSEMBLY__ $< -o $@
233
234$(OUT)vgarom.o: $(OUT)vgaccode16.o $(OUT)vgaentry.o $(OUT)vgasrc/vgalayout.lds vgasrc/vgaversion.c scripts/buildversion.py
235	@echo "  Linking $@"
236	$(Q)$(PYTHON) ./scripts/buildversion.py -e "$(EXTRAVERSION)" -t "$(CC);$(AS);$(LD);$(OBJCOPY);$(OBJDUMP);$(STRIP)" $(OUT)autovgaversion.h
237	$(Q)$(CC) $(CFLAGS16) -c vgasrc/vgaversion.c -o $(OUT)vgaversion.o
238	$(Q)$(LD) --gc-sections -T $(OUT)vgasrc/vgalayout.lds $(OUT)vgaccode16.o $(OUT)vgaentry.o $(OUT)vgaversion.o -o $@
239
240$(OUT)vgabios.bin.raw: $(OUT)vgarom.o
241	@echo "  Extracting binary $@"
242	$(Q)$(OBJCOPY) -O binary $< $@
243
244$(OUT)vgabios.bin: $(OUT)vgabios.bin.raw scripts/buildrom.py
245	@echo "  Finalizing rom $@"
246	$(Q)$(PYTHON) ./scripts/buildrom.py $< $@
247
248
249################ DSDT build rules
250
251iasl-option=$(shell if test -z "`$(1) $(2) 2>&1 > /dev/null`" \
252    ; then echo "$(2)"; else echo "$(3)"; fi ;)
253
254%.hex: %.dsl ./scripts/acpi_extract_preprocess.py ./scripts/acpi_extract.py
255	@echo "  Compiling IASL $@"
256	$(Q)$(CPP) $(CPPFLAGS) $< -o $(OUT)$*.dsl.i.orig
257	$(Q)$(PYTHON) ./scripts/acpi_extract_preprocess.py $(OUT)$*.dsl.i.orig > $(OUT)$*.dsl.i
258	$(Q)$(IASL) $(call iasl-option,$(IASL),-Pn,) -vs -l -tc -p $(OUT)$* $(OUT)$*.dsl.i
259	$(Q)$(PYTHON) ./scripts/acpi_extract.py $(OUT)$*.lst > $(OUT)$*.off
260	$(Q)cat $(OUT)$*.off > $@
261
262iasl: src/fw/acpi-dsdt.hex src/fw/ssdt-proc.hex src/fw/ssdt-pcihp.hex src/fw/ssdt-misc.hex
263
264################ Kconfig rules
265
266define do-kconfig
267$(Q)mkdir -p $(OUT)/scripts/kconfig/lxdialog
268$(Q)mkdir -p $(OUT)/include/config
269$(Q)mkdir -p $(addprefix $(OUT), $(DIRS))
270$(Q)$(MAKE) -C $(OUT) -f $(CURDIR)/scripts/kconfig/Makefile srctree=$(CURDIR) src=scripts/kconfig obj=scripts/kconfig Q=$(Q) Kconfig=$(CURDIR)/src/Kconfig $1
271endef
272
273$(OUT)autoconf.h : $(KCONFIG_CONFIG) ; $(call do-kconfig, silentoldconfig)
274$(KCONFIG_CONFIG): src/Kconfig vgasrc/Kconfig ; $(call do-kconfig, olddefconfig)
275%onfig: ; $(call do-kconfig, $@)
276help: ; $(call do-kconfig, $@)
277
278
279################ Generic rules
280
281clean:
282	$(Q)rm -rf $(OUT)
283
284distclean: clean
285	$(Q)rm -f .config .config.old
286
287-include $(OUT)*.d $(patsubst %,$(OUT)%/*.d,$(DIRS))
288