blob: e079f12f104feddf17c232147b682258d1ed5fe5 [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001# Legacy Bios build system
2#
3# Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4#
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'Connor0c3068d2008-12-21 17:51:36 -050012 serial.c clock.c pic.c cdrom.c ps2port.c smpdetect.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'Connorbc28a2b2008-03-11 19:32:38 -040025 -ffreestanding -fwhole-program -fomit-frame-pointer \
Kevin O'Connord9441142009-04-19 23:18:54 -040026 -fno-delete-null-pointer-checks -Wno-strict-aliasing \
27 -minline-all-stringops
Kevin O'Connor786502d2008-02-27 10:41:41 -050028COMMONCFLAGS += $(call cc-option,$(CC),-nopie,)
29COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector,)
30COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector-all,)
31
Kevin O'Connorc43bcba2008-07-30 20:37:13 -040032override CFLAGS = $(COMMONCFLAGS) -g -DMODE16=0
Kevin O'Connor7ab798f2008-07-13 11:08:36 -040033CFLAGS16INC = $(COMMONCFLAGS) -DMODE16=1 -fno-jump-tables -fno-defer-pop \
Kevin O'Connorb49e0582009-04-08 21:25:00 -040034 $(call cc-option,$(CC),-fno-tree-switch-conversion,) \
Kevin O'Connor96462242009-01-01 17:54:16 -050035 $(call cc-option,$(CC),--param large-stack-frame=4,)
Kevin O'Connor202024a2009-01-17 10:41:28 -050036CFLAGS16INC += -ffunction-sections -fdata-sections
Kevin O'Connor410680b2008-03-02 20:48:35 -050037CFLAGS16 = $(CFLAGS16INC) -g
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050038
Kevin O'Connor30853762009-01-17 18:49:20 -050039all: $(OUT) $(OUT)bios.bin
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050040
41# Run with "make V=1" to see the actual compile commands
42ifdef V
43Q=
44else
45Q=@
46endif
47
Kevin O'Connor2fc90bd2008-11-07 21:42:00 -050048OBJCOPY=objcopy
Kevin O'Connor202024a2009-01-17 10:41:28 -050049OBJDUMP=objdump
Kevin O'Connor2fc90bd2008-11-07 21:42:00 -050050NM=nm
51STRIP=strip
52
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050053.PHONY : all FORCE
54
55vpath %.c src
56vpath %.S src
57
58################ Build rules
Kevin O'Connor410680b2008-03-02 20:48:35 -050059
Kevin O'Connor03f630e2009-03-26 20:45:36 -040060TESTGCC:=$(shell CC=$(CC) tools/test-gcc.sh)
61ifeq "$(TESTGCC)" "-1"
62$(error "Please upgrade GCC")
63endif
64
Kevin O'Connor231f7102009-01-25 16:11:27 -050065ifndef AVOIDCOMBINE
Kevin O'Connor03f630e2009-03-26 20:45:36 -040066AVOIDCOMBINE=$(TESTGCC)
Kevin O'Connor231f7102009-01-25 16:11:27 -050067endif
68
Kevin O'Connor410680b2008-03-02 20:48:35 -050069# Do a whole file compile - two methods are supported. The first
70# involves including all the content textually via #include
71# directives. The second method uses gcc's "-combine" option.
Kevin O'Connor231f7102009-01-25 16:11:27 -050072ifeq "$(AVOIDCOMBINE)" "1"
Kevin O'Connor410680b2008-03-02 20:48:35 -050073define whole-compile
74@echo " Compiling whole program $3"
Kevin O'Connor86cebee2008-11-06 18:57:10 -050075$(Q)printf '$(foreach i,$2,#include "../$i"\n)' > $3.tmp.c
Kevin O'Connor410680b2008-03-02 20:48:35 -050076$(Q)$(CC) $1 -c $3.tmp.c -o $3
77endef
78else
Kevin O'Connor410680b2008-03-02 20:48:35 -050079define whole-compile
80@echo " Compiling whole program $3"
81$(Q)$(CC) $1 -combine -c $2 -o $3
82endef
83endif
84
85
Kevin O'Connor30853762009-01-17 18:49:20 -050086$(OUT)%.s: %.c
Kevin O'Connor9e821222008-12-06 18:56:19 -050087 @echo " Compiling to assembler $@"
Kevin O'Connor30853762009-01-17 18:49:20 -050088 $(Q)$(CC) $(CFLAGS16INC) -S -c $< -o $@
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050089
90$(OUT)%.lds: %.lds.S
Kevin O'Connor9e821222008-12-06 18:56:19 -050091 @echo " Precompiling $@"
Kevin O'Connord9a8b2d2008-11-16 09:17:02 -050092 $(Q)$(CPP) -P -D__ASSEMBLY__ $< -o $@
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050093
Kevin O'Connor30853762009-01-17 18:49:20 -050094$(OUT)asm-offsets.h: $(OUT)asm-offsets.s
Kevin O'Connor952974e2008-11-16 18:14:33 -050095 @echo " Generating offset file $@"
96 $(Q)./tools/gen-offsets.sh $< $@
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050097
Kevin O'Connor9e821222008-12-06 18:56:19 -050098$(OUT)ccode.16.s: ; $(call whole-compile, $(CFLAGS16) -S, $(addprefix src/, $(SRC16)),$@)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050099
Kevin O'Connor30853762009-01-17 18:49:20 -0500100$(OUT)romlayout16.o: romlayout.S $(OUT)ccode.16.s $(OUT)asm-offsets.h
Kevin O'Connor9e821222008-12-06 18:56:19 -0500101 @echo " Compiling (16bit) $@"
102 $(Q)$(CC) $(CFLAGS16INC) -c -D__ASSEMBLY__ $< -o $@
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500103
Kevin O'Connor9e821222008-12-06 18:56:19 -0500104$(OUT)ccode32.o: ; $(call whole-compile, $(CFLAGS), $(addprefix src/, $(SRC32)),$@)
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -0400105
Kevin O'Connor9e821222008-12-06 18:56:19 -0500106$(OUT)rom32.o: $(OUT)ccode32.o $(OUT)rombios32.lds
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -0400107 @echo " Linking (no relocs) $@"
Kevin O'Connor7cf17f22009-04-08 21:10:08 -0400108 $(Q)$(LD) -r -T $(OUT)rombios32.lds $< -o $@
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -0400109
Kevin O'Connor202024a2009-01-17 10:41:28 -0500110$(OUT)romlayout.lds: $(OUT)romlayout16.o
111 @echo " Building layout information $@"
Kevin O'Connor711ddc62009-01-17 15:17:34 -0500112 $(Q)$(OBJDUMP) -h $< | ./tools/layoutrom.py $@
Kevin O'Connor202024a2009-01-17 10:41:28 -0500113
114$(OUT)rombios16.lds: $(OUT)romlayout.lds
115
Kevin O'Connord2899772008-07-06 09:56:14 -0400116$(OUT)rom16.o: $(OUT)romlayout16.o $(OUT)rom32.o $(OUT)rombios16.lds
Kevin O'Connor9e821222008-12-06 18:56:19 -0500117 @echo " Linking (16bit) $@"
Kevin O'Connor86cebee2008-11-06 18:57:10 -0500118 $(Q)$(OBJCOPY) --prefix-symbols=_code32_ $(OUT)rom32.o $(OUT)rom32.rename.o
119 $(Q)$(LD) -T $(OUT)rombios16.lds -R $(OUT)rom32.rename.o $< -o $@
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500120
Kevin O'Connord2899772008-07-06 09:56:14 -0400121$(OUT)rom.o: $(OUT)rom16.o $(OUT)rom32.o $(OUT)rombios.lds
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500122 @echo " Linking $@"
Kevin O'Connor86cebee2008-11-06 18:57:10 -0500123 $(Q)$(LD) -T $(OUT)rombios.lds $(OUT)rom16.o $(OUT)rom32.o -o $@
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500124
Kevin O'Connord2899772008-07-06 09:56:14 -0400125$(OUT)bios.bin.elf: $(OUT)rom.o
Kevin O'Connor9bcc5272008-07-05 21:08:56 -0400126 @echo " Prepping $@"
Kevin O'Connor86cebee2008-11-06 18:57:10 -0500127 $(Q)$(NM) $< | ./tools/checkrom.py
128 $(Q)$(STRIP) $< -o $@
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -0400129
130$(OUT)bios.bin: $(OUT)bios.bin.elf
131 @echo " Extracting binary $@"
Kevin O'Connor86cebee2008-11-06 18:57:10 -0500132 $(Q)$(OBJCOPY) -O binary $< $@
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -0400133
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500134
135####### Generic rules
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500136clean:
Kevin O'Connor86cebee2008-11-06 18:57:10 -0500137 $(Q)rm -rf $(OUT)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500138
139$(OUT):
Kevin O'Connor86cebee2008-11-06 18:57:10 -0500140 $(Q)mkdir $@
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500141
142-include $(OUT)*.d