blob: 10aa96f039c3607116341d24f918921740e218bc [file] [log] [blame]
Kevin O'Connor14458432009-05-23 18:21:18 -04001# SeaBIOS build system
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05002#
Kevin O'Connor14458432009-05-23 18:21:18 -04003# Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05004#
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05005# This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05006
7# Output directory
8OUT=out/
9
10# Source files
Kevin O'Connor30853762009-01-17 18:49:20 -050011SRCBOTH=output.c util.c floppy.c ata.c misc.c mouse.c kbd.c pci.c \
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040012 serial.c clock.c pic.c cdrom.c ps2port.c smp.c resume.c \
Kevin O'Connor51358db2008-12-28 21:50:29 -050013 pnpbios.c pirtable.c
Kevin O'Connor30853762009-01-17 18:49:20 -050014SRC16=$(SRCBOTH) system.c disk.c apm.c pcibios.c vgahooks.c font.c
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050015SRC32=$(SRCBOTH) post.c shadow.c memmap.c coreboot.c boot.c \
Kevin O'Connor592323f2009-04-26 23:17:49 -040016 acpi.c smm.c mptable.c smbios.c pciinit.c optionroms.c mtrr.c \
17 lzmadecode.c
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050018
Kevin O'Connor786502d2008-02-27 10:41:41 -050019cc-option = $(shell if test -z "`$(1) $(2) -S -o /dev/null -xc \
20 /dev/null 2>&1`"; then echo "$(2)"; else echo "$(3)"; fi ;)
21
Kevin O'Connorbdce35f2008-02-26 21:33:14 -050022# Default compiler flags
Kevin O'Connorba2d0df2008-04-13 16:59:03 -040023COMMONCFLAGS = -Wall -Os -MD -m32 -march=i386 -mregparm=3 \
Kevin O'Connor03f630e2009-03-26 20:45:36 -040024 -mpreferred-stack-boundary=2 -mrtd -freg-struct-return \
Kevin O'Connor942d4952009-06-10 22:44:06 -040025 -ffreestanding -fomit-frame-pointer \
Kevin O'Connord9441142009-04-19 23:18:54 -040026 -fno-delete-null-pointer-checks -Wno-strict-aliasing \
Kevin O'Connor0942e7f2009-06-15 22:27:01 -040027 -ffunction-sections -fdata-sections -fno-common \
Kevin O'Connord9441142009-04-19 23:18:54 -040028 -minline-all-stringops
Kevin O'Connor786502d2008-02-27 10:41:41 -050029COMMONCFLAGS += $(call cc-option,$(CC),-nopie,)
30COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector,)
31COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector-all,)
32
Kevin O'Connorc43bcba2008-07-30 20:37:13 -040033override CFLAGS = $(COMMONCFLAGS) -g -DMODE16=0
Kevin O'Connor942d4952009-06-10 22:44:06 -040034CFLAGS16INC = $(COMMONCFLAGS) -DMODE16=1 -fno-defer-pop \
35 $(call cc-option,$(CC),-fno-jump-tables,-DMANUAL_NO_JUMP_TABLE) \
Kevin O'Connorb49e0582009-04-08 21:25:00 -040036 $(call cc-option,$(CC),-fno-tree-switch-conversion,) \
Kevin O'Connor96462242009-01-01 17:54:16 -050037 $(call cc-option,$(CC),--param large-stack-frame=4,)
Kevin O'Connor410680b2008-03-02 20:48:35 -050038CFLAGS16 = $(CFLAGS16INC) -g
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050039
Kevin O'Connor30853762009-01-17 18:49:20 -050040all: $(OUT) $(OUT)bios.bin
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050041
42# Run with "make V=1" to see the actual compile commands
43ifdef V
44Q=
45else
46Q=@
47endif
48
Kevin O'Connor2fc90bd2008-11-07 21:42:00 -050049OBJCOPY=objcopy
Kevin O'Connor202024a2009-01-17 10:41:28 -050050OBJDUMP=objdump
Kevin O'Connor2fc90bd2008-11-07 21:42:00 -050051NM=nm
52STRIP=strip
53
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050054.PHONY : all FORCE
55
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040056vpath %.c src vgasrc
57vpath %.S src vgasrc
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050058
59################ Build rules
Kevin O'Connor410680b2008-03-02 20:48:35 -050060
Kevin O'Connor0942e7f2009-06-15 22:27:01 -040061# Verify the gcc configuration and test if -fwhole-program works.
Kevin O'Connor03f630e2009-03-26 20:45:36 -040062TESTGCC:=$(shell CC=$(CC) tools/test-gcc.sh)
63ifeq "$(TESTGCC)" "-1"
64$(error "Please upgrade GCC")
65endif
66
Kevin O'Connor0942e7f2009-06-15 22:27:01 -040067ifndef COMPSTRAT
68COMPSTRAT=$(TESTGCC)
Kevin O'Connor231f7102009-01-25 16:11:27 -050069endif
70
Kevin O'Connor0942e7f2009-06-15 22:27:01 -040071# Do a whole file compile - three methods are supported.
72ifeq "$(COMPSTRAT)" "1"
73# First method - use -fwhole-program without -combine.
74define whole-compile
75@echo " Compiling whole program $3"
76$(Q)printf '$(foreach i,$2,#include "../$i"\n)' > $3.tmp.c
77$(Q)$(CC) $1 -fwhole-program -DWHOLE_PROGRAM -c $3.tmp.c -o $3
78endef
Kevin O'Connord82a1ca2009-06-17 20:33:41 -040079else
80ifeq "$(COMPSTRAT)" "2"
Kevin O'Connor0942e7f2009-06-15 22:27:01 -040081# Second menthod - don't use -fwhole-program at all.
Kevin O'Connor410680b2008-03-02 20:48:35 -050082define whole-compile
83@echo " Compiling whole program $3"
Kevin O'Connor86cebee2008-11-06 18:57:10 -050084$(Q)printf '$(foreach i,$2,#include "../$i"\n)' > $3.tmp.c
Kevin O'Connor410680b2008-03-02 20:48:35 -050085$(Q)$(CC) $1 -c $3.tmp.c -o $3
86endef
87else
Kevin O'Connor0942e7f2009-06-15 22:27:01 -040088# Third (and preferred) method - use -fwhole-program with -combine
Kevin O'Connor410680b2008-03-02 20:48:35 -050089define whole-compile
90@echo " Compiling whole program $3"
Kevin O'Connor0942e7f2009-06-15 22:27:01 -040091$(Q)$(CC) $1 -fwhole-program -DWHOLE_PROGRAM -combine -c $2 -o $3
Kevin O'Connor410680b2008-03-02 20:48:35 -050092endef
93endif
Kevin O'Connord82a1ca2009-06-17 20:33:41 -040094endif
Kevin O'Connor410680b2008-03-02 20:48:35 -050095
96
Kevin O'Connor30853762009-01-17 18:49:20 -050097$(OUT)%.s: %.c
Kevin O'Connor9e821222008-12-06 18:56:19 -050098 @echo " Compiling to assembler $@"
Kevin O'Connor30853762009-01-17 18:49:20 -050099 $(Q)$(CC) $(CFLAGS16INC) -S -c $< -o $@
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500100
101$(OUT)%.lds: %.lds.S
Kevin O'Connor9e821222008-12-06 18:56:19 -0500102 @echo " Precompiling $@"
Kevin O'Connord9a8b2d2008-11-16 09:17:02 -0500103 $(Q)$(CPP) -P -D__ASSEMBLY__ $< -o $@
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500104
Kevin O'Connor30853762009-01-17 18:49:20 -0500105$(OUT)asm-offsets.h: $(OUT)asm-offsets.s
Kevin O'Connor952974e2008-11-16 18:14:33 -0500106 @echo " Generating offset file $@"
107 $(Q)./tools/gen-offsets.sh $< $@
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500108
Kevin O'Connor9e821222008-12-06 18:56:19 -0500109$(OUT)ccode.16.s: ; $(call whole-compile, $(CFLAGS16) -S, $(addprefix src/, $(SRC16)),$@)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500110
Kevin O'Connor30853762009-01-17 18:49:20 -0500111$(OUT)romlayout16.o: romlayout.S $(OUT)ccode.16.s $(OUT)asm-offsets.h
Kevin O'Connor9e821222008-12-06 18:56:19 -0500112 @echo " Compiling (16bit) $@"
113 $(Q)$(CC) $(CFLAGS16INC) -c -D__ASSEMBLY__ $< -o $@
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500114
Kevin O'Connor9e821222008-12-06 18:56:19 -0500115$(OUT)ccode32.o: ; $(call whole-compile, $(CFLAGS), $(addprefix src/, $(SRC32)),$@)
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -0400116
Kevin O'Connor9e821222008-12-06 18:56:19 -0500117$(OUT)rom32.o: $(OUT)ccode32.o $(OUT)rombios32.lds
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -0400118 @echo " Linking (no relocs) $@"
Kevin O'Connor7cf17f22009-04-08 21:10:08 -0400119 $(Q)$(LD) -r -T $(OUT)rombios32.lds $< -o $@
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -0400120
Kevin O'Connorc0693942009-06-10 21:56:01 -0400121$(OUT)romlayout16.lds $(OUT)romlayout32.lds: $(OUT)ccode32.o $(OUT)romlayout16.o
Kevin O'Connor202024a2009-01-17 10:41:28 -0500122 @echo " Building layout information $@"
Kevin O'Connorc0693942009-06-10 21:56:01 -0400123 $(Q)$(OBJDUMP) -thr $(OUT)ccode32.o > $(OUT)ccode32.o.objdump
124 $(Q)$(OBJDUMP) -thr $(OUT)romlayout16.o > $(OUT)romlayout16.o.objdump
125 $(Q)./tools/layoutrom.py $(OUT)romlayout16.o.objdump $(OUT)ccode32.o.objdump $(OUT)romlayout16.lds $(OUT)romlayout32.lds
Kevin O'Connor202024a2009-01-17 10:41:28 -0500126
Kevin O'Connorc0693942009-06-10 21:56:01 -0400127$(OUT)layout16.lds: $(OUT)romlayout16.lds
128$(OUT)rombios32.lds: $(OUT)romlayout32.lds
Kevin O'Connor202024a2009-01-17 10:41:28 -0500129
Kevin O'Connor14458432009-05-23 18:21:18 -0400130$(OUT)rom16.o: $(OUT)romlayout16.o $(OUT)rom32.o $(OUT)layout16.lds
131 @echo " Linking (no relocs) $@"
132 $(Q)$(LD) -r -T $(OUT)layout16.lds $< -o $@
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500133
Kevin O'Connor14458432009-05-23 18:21:18 -0400134$(OUT)rom.o: $(OUT)rom16.o $(OUT)rom32.o $(OUT)rombios16.lds $(OUT)rombios.lds
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500135 @echo " Linking $@"
Kevin O'Connor7173f6f2009-05-27 22:23:33 -0400136 $(Q)$(LD) -T $(OUT)rombios16.lds $(OUT)rom16.o -R $(OUT)rom32.o -o $(OUT)rom16.reloc.o
137 $(Q)$(STRIP) $(OUT)rom16.reloc.o -o $(OUT)rom16.final.o
Kevin O'Connor5c3f5dd2009-06-22 20:05:56 -0400138 $(Q)$(OBJCOPY) --adjust-vma 0xf0000 $(OUT)rom16.o $(OUT)rom16.moved.o
Kevin O'Connor14458432009-05-23 18:21:18 -0400139 $(Q)$(LD) -T $(OUT)rombios.lds $(OUT)rom16.final.o $(OUT)rom32.o -R $(OUT)rom16.moved.o -o $@
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500140
Kevin O'Connord2899772008-07-06 09:56:14 -0400141$(OUT)bios.bin.elf: $(OUT)rom.o
Kevin O'Connor9bcc5272008-07-05 21:08:56 -0400142 @echo " Prepping $@"
Kevin O'Connor86cebee2008-11-06 18:57:10 -0500143 $(Q)$(NM) $< | ./tools/checkrom.py
144 $(Q)$(STRIP) $< -o $@
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -0400145
146$(OUT)bios.bin: $(OUT)bios.bin.elf
147 @echo " Extracting binary $@"
Kevin O'Connor86cebee2008-11-06 18:57:10 -0500148 $(Q)$(OBJCOPY) -O binary $< $@
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -0400149
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500150
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400151################ VGA build rules
152
153# VGA src files
Kevin O'Connor8d8e0942009-05-17 21:14:46 -0400154SRCVGA=src/output.c src/util.c vgasrc/vga.c vgasrc/vgafb.c vgasrc/vgaio.c \
155 vgasrc/vgatables.c vgasrc/vgafonts.c vgasrc/clext.c
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400156
157$(OUT)vgaccode.16.s: ; $(call whole-compile, $(CFLAGS16) -S -Isrc, $(SRCVGA),$@)
158
159$(OUT)vgalayout16.o: vgaentry.S $(OUT)vgaccode.16.s $(OUT)asm-offsets.h
160 @echo " Compiling (16bit) $@"
161 $(Q)$(CC) $(CFLAGS16INC) -c -D__ASSEMBLY__ -Isrc $< -o $@
162
163$(OUT)vgarom.o: $(OUT)vgalayout16.o $(OUT)vgalayout.lds
164 @echo " Linking $@"
Kevin O'Connorc0693942009-06-10 21:56:01 -0400165 $(Q)$(LD) --gc-sections -T $(OUT)vgalayout.lds $(OUT)vgalayout16.o -o $@
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400166
167$(OUT)vgabios.bin.raw: $(OUT)vgarom.o
168 @echo " Extracting binary $@"
169 $(Q)$(OBJCOPY) -O binary $< $@
170
171$(OUT)vgabios.bin: $(OUT)vgabios.bin.raw
172 @echo " Finalizing rom $@"
173 $(Q)./tools/buildrom.py $< $@
174
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500175####### Generic rules
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500176clean:
Kevin O'Connor86cebee2008-11-06 18:57:10 -0500177 $(Q)rm -rf $(OUT)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500178
179$(OUT):
Kevin O'Connor86cebee2008-11-06 18:57:10 -0500180 $(Q)mkdir $@
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500181
182-include $(OUT)*.d