Try to automatically fit sections into open spaces in the fixed area.
[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 system.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) disk.c apm.c pcibios.c vgahooks.c
15 SRC32=$(SRCBOTH) post.c shadow.c post_menu.c memmap.c coreboot.c boot.c \
16       acpi.c smm.c mptable.c smbios.c pciinit.c optionroms.c mtrr.c
17 TABLESRC=font.c cbt.c floppy_dbt.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 \
25                -ffreestanding -fwhole-program -fomit-frame-pointer \
26                -fno-delete-null-pointer-checks -Wno-strict-aliasing
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),--param large-stack-frame=4,)
34 CFLAGS16INC += -ffunction-sections -fdata-sections
35 CFLAGS16 = $(CFLAGS16INC) -g
36
37 TABLETMP=$(addprefix $(OUT), $(patsubst %.c,%.16.s,$(TABLESRC)))
38 all: $(OUT) $(OUT)bios.bin $(TABLETMP)
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 # Do a whole file compile - two methods are supported.  The first
60 # involves including all the content textually via #include
61 # directives.  The second method uses gcc's "-combine" option.
62 ifdef AVOIDCOMBINE
63 DEPHACK=
64 define whole-compile
65 @echo "  Compiling whole program $3"
66 $(Q)printf '$(foreach i,$2,#include "../$i"\n)' > $3.tmp.c
67 $(Q)$(CC) $1 -c $3.tmp.c -o $3
68 endef
69 else
70 # Ugly way to get gcc to generate .d files correctly.
71 DEPHACK=-combine src/null.c
72 define whole-compile
73 @echo "  Compiling whole program $3"
74 $(Q)$(CC) $1 -combine -c $2 -o $3
75 endef
76 endif
77
78
79 $(OUT)%.proc.16.s: $(OUT)%.16.s
80         @echo "  Moving data sections to text in $@"
81         $(Q)sed 's/^\t\.section\t\.\(ro\)\?data.*// ; s/^\t\.data$$//' < $< > $@
82
83 $(OUT)%.16.s: %.c
84         @echo "  Compiling to assembler $@"
85         $(Q)$(CC) $(CFLAGS16INC) $(DEPHACK) -S -c $< -o $@
86
87 $(OUT)%.lds: %.lds.S
88         @echo "  Precompiling $@"
89         $(Q)$(CPP) -P -D__ASSEMBLY__ $< -o $@
90
91 $(OUT)asm-offsets.h: $(OUT)asm-offsets.16.s
92         @echo "  Generating offset file $@"
93         $(Q)./tools/gen-offsets.sh $< $@
94
95 $(OUT)ccode.16.s: ; $(call whole-compile, $(CFLAGS16) -S, $(addprefix src/, $(SRC16)),$@)
96
97 TABLEASM=$(addprefix $(OUT), $(patsubst %.c,%.proc.16.s,$(TABLESRC)))
98 $(OUT)romlayout16.o: romlayout.S $(OUT)ccode.16.s $(OUT)asm-offsets.h $(TABLEASM)
99         @echo "  Compiling (16bit) $@"
100         $(Q)$(CC) $(CFLAGS16INC) -c -D__ASSEMBLY__ $< -o $@
101
102 $(OUT)ccode32.o: ; $(call whole-compile, $(CFLAGS), $(addprefix src/, $(SRC32)),$@)
103
104 $(OUT)rom32.o: $(OUT)ccode32.o $(OUT)rombios32.lds
105         @echo "  Linking (no relocs) $@"
106         $(Q)$(LD) -r -d -T $(OUT)rombios32.lds $< -o $@
107
108 $(OUT)romlayout.lds: $(OUT)romlayout16.o
109         @echo "  Building layout information $@"
110         $(Q)$(OBJDUMP) -h $< | ./tools/layoutrom.py $@
111
112 $(OUT)rombios16.lds: $(OUT)romlayout.lds
113
114 $(OUT)rom16.o: $(OUT)romlayout16.o $(OUT)rom32.o $(OUT)rombios16.lds
115         @echo "  Linking (16bit) $@"
116         $(Q)$(OBJCOPY) --prefix-symbols=_code32_ $(OUT)rom32.o $(OUT)rom32.rename.o
117         $(Q)$(LD) -T $(OUT)rombios16.lds -R $(OUT)rom32.rename.o $< -o $@
118
119 $(OUT)rom.o: $(OUT)rom16.o $(OUT)rom32.o $(OUT)rombios.lds
120         @echo "  Linking $@"
121         $(Q)$(LD) -T $(OUT)rombios.lds $(OUT)rom16.o $(OUT)rom32.o -o $@
122
123 $(OUT)bios.bin.elf: $(OUT)rom.o
124         @echo "  Prepping $@"
125         $(Q)$(NM) $< | ./tools/checkrom.py
126         $(Q)$(STRIP) $< -o $@
127
128 $(OUT)bios.bin: $(OUT)bios.bin.elf
129         @echo "  Extracting binary $@"
130         $(Q)$(OBJCOPY) -O binary $< $@
131
132
133 ####### Generic rules
134 clean:
135         $(Q)rm -rf $(OUT)
136
137 $(OUT):
138         $(Q)mkdir $@
139
140 -include $(OUT)*.d