Fixup previous memcpy optimization.
[seabios.git] / Makefile
1 # Legacy Bios build system
2 #
3 # Copyright (C) 2008  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
18 cc-option = $(shell if test -z "`$(1) $(2) -S -o /dev/null -xc \
19               /dev/null 2>&1`"; then echo "$(2)"; else echo "$(3)"; fi ;)
20
21 # Default compiler flags
22 COMMONCFLAGS = -Wall -Os -MD -m32 -march=i386 -mregparm=3 \
23                -mpreferred-stack-boundary=2 -mrtd -freg-struct-return \
24                -ffreestanding -fwhole-program -fomit-frame-pointer \
25                -fno-delete-null-pointer-checks -Wno-strict-aliasing \
26                -minline-all-stringops
27 COMMONCFLAGS += $(call cc-option,$(CC),-nopie,)
28 COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector,)
29 COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector-all,)
30
31 override CFLAGS = $(COMMONCFLAGS) -g -DMODE16=0
32 CFLAGS16INC = $(COMMONCFLAGS) -DMODE16=1 -fno-jump-tables -fno-defer-pop \
33               $(call cc-option,$(CC),-fno-tree-switch-conversion,) \
34               $(call cc-option,$(CC),--param large-stack-frame=4,)
35 CFLAGS16INC += -ffunction-sections -fdata-sections
36 CFLAGS16 = $(CFLAGS16INC) -g
37
38 all: $(OUT) $(OUT)bios.bin
39
40 # Run with "make V=1" to see the actual compile commands
41 ifdef V
42 Q=
43 else
44 Q=@
45 endif
46
47 OBJCOPY=objcopy
48 OBJDUMP=objdump
49 NM=nm
50 STRIP=strip
51
52 .PHONY : all FORCE
53
54 vpath %.c src
55 vpath %.S src
56
57 ################ Build rules
58
59 TESTGCC:=$(shell CC=$(CC) tools/test-gcc.sh)
60 ifeq "$(TESTGCC)" "-1"
61 $(error "Please upgrade GCC")
62 endif
63
64 ifndef AVOIDCOMBINE
65 AVOIDCOMBINE=$(TESTGCC)
66 endif
67
68 # Do a whole file compile - two methods are supported.  The first
69 # involves including all the content textually via #include
70 # directives.  The second method uses gcc's "-combine" option.
71 ifeq "$(AVOIDCOMBINE)" "1"
72 define whole-compile
73 @echo "  Compiling whole program $3"
74 $(Q)printf '$(foreach i,$2,#include "../$i"\n)' > $3.tmp.c
75 $(Q)$(CC) $1 -c $3.tmp.c -o $3
76 endef
77 else
78 define whole-compile
79 @echo "  Compiling whole program $3"
80 $(Q)$(CC) $1 -combine -c $2 -o $3
81 endef
82 endif
83
84
85 $(OUT)%.s: %.c
86         @echo "  Compiling to assembler $@"
87         $(Q)$(CC) $(CFLAGS16INC) -S -c $< -o $@
88
89 $(OUT)%.lds: %.lds.S
90         @echo "  Precompiling $@"
91         $(Q)$(CPP) -P -D__ASSEMBLY__ $< -o $@
92
93 $(OUT)asm-offsets.h: $(OUT)asm-offsets.s
94         @echo "  Generating offset file $@"
95         $(Q)./tools/gen-offsets.sh $< $@
96
97 $(OUT)ccode.16.s: ; $(call whole-compile, $(CFLAGS16) -S, $(addprefix src/, $(SRC16)),$@)
98
99 $(OUT)romlayout16.o: romlayout.S $(OUT)ccode.16.s $(OUT)asm-offsets.h
100         @echo "  Compiling (16bit) $@"
101         $(Q)$(CC) $(CFLAGS16INC) -c -D__ASSEMBLY__ $< -o $@
102
103 $(OUT)ccode32.o: ; $(call whole-compile, $(CFLAGS), $(addprefix src/, $(SRC32)),$@)
104
105 $(OUT)rom32.o: $(OUT)ccode32.o $(OUT)rombios32.lds
106         @echo "  Linking (no relocs) $@"
107         $(Q)$(LD) -r -T $(OUT)rombios32.lds $< -o $@
108
109 $(OUT)romlayout.lds: $(OUT)romlayout16.o
110         @echo "  Building layout information $@"
111         $(Q)$(OBJDUMP) -h $< | ./tools/layoutrom.py $@
112
113 $(OUT)rombios16.lds: $(OUT)romlayout.lds
114
115 $(OUT)rom16.o: $(OUT)romlayout16.o $(OUT)rom32.o $(OUT)rombios16.lds
116         @echo "  Linking (16bit) $@"
117         $(Q)$(OBJCOPY) --prefix-symbols=_code32_ $(OUT)rom32.o $(OUT)rom32.rename.o
118         $(Q)$(LD) -T $(OUT)rombios16.lds -R $(OUT)rom32.rename.o $< -o $@
119
120 $(OUT)rom.o: $(OUT)rom16.o $(OUT)rom32.o $(OUT)rombios.lds
121         @echo "  Linking $@"
122         $(Q)$(LD) -T $(OUT)rombios.lds $(OUT)rom16.o $(OUT)rom32.o -o $@
123
124 $(OUT)bios.bin.elf: $(OUT)rom.o
125         @echo "  Prepping $@"
126         $(Q)$(NM) $< | ./tools/checkrom.py
127         $(Q)$(STRIP) $< -o $@
128
129 $(OUT)bios.bin: $(OUT)bios.bin.elf
130         @echo "  Extracting binary $@"
131         $(Q)$(OBJCOPY) -O binary $< $@
132
133
134 ####### Generic rules
135 clean:
136         $(Q)rm -rf $(OUT)
137
138 $(OUT):
139         $(Q)mkdir $@
140
141 -include $(OUT)*.d