Avoid makefile "else ifeq" syntax - old versions don't support it.
[seabios.git] / Makefile
1 # SeaBIOS build system
2 #
3 # Copyright (C) 2008,2009  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
8 OUT=out/
9
10 # Source files
11 SRCBOTH=output.c util.c floppy.c ata.c misc.c mouse.c kbd.c pci.c \
12         serial.c clock.c pic.c cdrom.c ps2port.c smpdetect.c resume.c \
13         pnpbios.c pirtable.c
14 SRC16=$(SRCBOTH) system.c disk.c apm.c pcibios.c vgahooks.c font.c
15 SRC32=$(SRCBOTH) post.c shadow.c memmap.c coreboot.c boot.c \
16       acpi.c smm.c mptable.c smbios.c pciinit.c optionroms.c mtrr.c \
17       lzmadecode.c
18
19 cc-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
22 # Default compiler flags
23 COMMONCFLAGS = -Wall -Os -MD -m32 -march=i386 -mregparm=3 \
24                -mpreferred-stack-boundary=2 -mrtd -freg-struct-return \
25                -ffreestanding -fomit-frame-pointer \
26                -fno-delete-null-pointer-checks -Wno-strict-aliasing \
27                -ffunction-sections -fdata-sections -fno-common \
28                -minline-all-stringops
29 COMMONCFLAGS += $(call cc-option,$(CC),-nopie,)
30 COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector,)
31 COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector-all,)
32
33 override CFLAGS = $(COMMONCFLAGS) -g -DMODE16=0
34 CFLAGS16INC = $(COMMONCFLAGS) -DMODE16=1 -fno-defer-pop \
35               $(call cc-option,$(CC),-fno-jump-tables,-DMANUAL_NO_JUMP_TABLE) \
36               $(call cc-option,$(CC),-fno-tree-switch-conversion,) \
37               $(call cc-option,$(CC),--param large-stack-frame=4,)
38 CFLAGS16 = $(CFLAGS16INC) -g
39
40 all: $(OUT) $(OUT)bios.bin
41
42 # Run with "make V=1" to see the actual compile commands
43 ifdef V
44 Q=
45 else
46 Q=@
47 endif
48
49 OBJCOPY=objcopy
50 OBJDUMP=objdump
51 NM=nm
52 STRIP=strip
53
54 .PHONY : all FORCE
55
56 vpath %.c src vgasrc
57 vpath %.S src vgasrc
58
59 ################ Build rules
60
61 # Verify the gcc configuration and test if -fwhole-program works.
62 TESTGCC:=$(shell CC=$(CC) tools/test-gcc.sh)
63 ifeq "$(TESTGCC)" "-1"
64 $(error "Please upgrade GCC")
65 endif
66
67 ifndef COMPSTRAT
68 COMPSTRAT=$(TESTGCC)
69 endif
70
71 # Do a whole file compile - three methods are supported.
72 ifeq "$(COMPSTRAT)" "1"
73 # First method - use -fwhole-program without -combine.
74 define 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
78 endef
79 else
80 ifeq "$(COMPSTRAT)" "2"
81 # Second menthod - don't use -fwhole-program at all.
82 define whole-compile
83 @echo "  Compiling whole program $3"
84 $(Q)printf '$(foreach i,$2,#include "../$i"\n)' > $3.tmp.c
85 $(Q)$(CC) $1 -c $3.tmp.c -o $3
86 endef
87 else
88 # Third (and preferred) method - use -fwhole-program with -combine
89 define whole-compile
90 @echo "  Compiling whole program $3"
91 $(Q)$(CC) $1 -fwhole-program -DWHOLE_PROGRAM -combine -c $2 -o $3
92 endef
93 endif
94 endif
95
96
97 $(OUT)%.s: %.c
98         @echo "  Compiling to assembler $@"
99         $(Q)$(CC) $(CFLAGS16INC) -S -c $< -o $@
100
101 $(OUT)%.lds: %.lds.S
102         @echo "  Precompiling $@"
103         $(Q)$(CPP) -P -D__ASSEMBLY__ $< -o $@
104
105 $(OUT)asm-offsets.h: $(OUT)asm-offsets.s
106         @echo "  Generating offset file $@"
107         $(Q)./tools/gen-offsets.sh $< $@
108
109 $(OUT)ccode.16.s: ; $(call whole-compile, $(CFLAGS16) -S, $(addprefix src/, $(SRC16)),$@)
110
111 $(OUT)romlayout16.o: romlayout.S $(OUT)ccode.16.s $(OUT)asm-offsets.h
112         @echo "  Compiling (16bit) $@"
113         $(Q)$(CC) $(CFLAGS16INC) -c -D__ASSEMBLY__ $< -o $@
114
115 $(OUT)ccode32.o: ; $(call whole-compile, $(CFLAGS), $(addprefix src/, $(SRC32)),$@)
116
117 $(OUT)rom32.o: $(OUT)ccode32.o $(OUT)rombios32.lds
118         @echo "  Linking (no relocs) $@"
119         $(Q)$(LD) -r -T $(OUT)rombios32.lds $< -o $@
120
121 $(OUT)romlayout16.lds $(OUT)romlayout32.lds: $(OUT)ccode32.o $(OUT)romlayout16.o
122         @echo "  Building layout information $@"
123         $(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
126
127 $(OUT)layout16.lds: $(OUT)romlayout16.lds
128 $(OUT)rombios32.lds: $(OUT)romlayout32.lds
129
130 $(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 $@
133
134 $(OUT)rom.o: $(OUT)rom16.o $(OUT)rom32.o $(OUT)rombios16.lds $(OUT)rombios.lds
135         @echo "  Linking $@"
136         $(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
138         $(Q)$(OBJCOPY) --extract-symbol --adjust-vma 0xf0000 $(OUT)rom16.o $(OUT)rom16.moved.o
139         $(Q)$(LD) -T $(OUT)rombios.lds $(OUT)rom16.final.o $(OUT)rom32.o -R $(OUT)rom16.moved.o -o $@
140
141 $(OUT)bios.bin.elf: $(OUT)rom.o
142         @echo "  Prepping $@"
143         $(Q)$(NM) $< | ./tools/checkrom.py
144         $(Q)$(STRIP) $< -o $@
145
146 $(OUT)bios.bin: $(OUT)bios.bin.elf
147         @echo "  Extracting binary $@"
148         $(Q)$(OBJCOPY) -O binary $< $@
149
150
151 ################ VGA build rules
152
153 # VGA src files
154 SRCVGA=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
156
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 $@"
165         $(Q)$(LD) --gc-sections -T $(OUT)vgalayout.lds $(OUT)vgalayout16.o -o $@
166
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
175 ####### Generic rules
176 clean:
177         $(Q)rm -rf $(OUT)
178
179 $(OUT):
180         $(Q)mkdir $@
181
182 -include $(OUT)*.d