Cleanup a20 code.
[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 GPLv3 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
13 SRC16=$(SRCBOTH) disk.c apm.c pcibios.c vgahooks.c
14 SRC32=$(SRCBOTH) post.c shadow.c post_menu.c memmap.c coreboot.c boot.c \
15       acpi.c pirtable.c smm.c smpdetect.c mptable.c smbios.c pciinit.c \
16       optionroms.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
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=8,)
34 CFLAGS16 = $(CFLAGS16INC) -g
35
36 TABLETMP=$(addprefix $(OUT), $(patsubst %.c,%.16.s,$(TABLESRC)))
37 all: $(OUT) $(OUT)bios.bin $(TABLETMP)
38
39 # Run with "make V=1" to see the actual compile commands
40 ifdef V
41 Q=
42 else
43 Q=@
44 endif
45
46 OBJCOPY=objcopy
47 NM=nm
48 STRIP=strip
49
50 .PHONY : all FORCE
51
52 vpath %.c src
53 vpath %.S src
54
55 ################ Build rules
56
57 # Do a whole file compile - two methods are supported.  The first
58 # involves including all the content textually via #include
59 # directives.  The second method uses gcc's "-combine" option.
60 ifdef AVOIDCOMBINE
61 DEPHACK=
62 define whole-compile
63 @echo "  Compiling whole program $3"
64 $(Q)printf '$(foreach i,$2,#include "../$i"\n)' > $3.tmp.c
65 $(Q)$(CC) $1 -c $3.tmp.c -o $3
66 endef
67 else
68 # Ugly way to get gcc to generate .d files correctly.
69 DEPHACK=-combine src/null.c
70 define whole-compile
71 @echo "  Compiling whole program $3"
72 $(Q)$(CC) $1 -combine -c $2 -o $3
73 endef
74 endif
75
76
77 $(OUT)%.proc.16.s: $(OUT)%.16.s
78         @echo "  Moving data sections to text in $<"
79         $(Q)sed 's/\t\.section\t\.rodata.*// ; s/\t\.data//' < $< > $@
80
81 $(OUT)%.16.s: %.c
82         @echo "  Generating assembler for $<"
83         $(Q)$(CC) $(CFLAGS16INC) $(DEPHACK) -S -c $< -o $@
84
85 $(OUT)%.lds: %.lds.S
86         @echo "  Precompiling $<"
87         $(Q)$(CPP) -P -D__ASSEMBLY__ $< -o $@
88
89
90 $(OUT)blob.16.s: ; $(call whole-compile, $(CFLAGS16) -S, $(addprefix src/, $(SRC16)),$@)
91
92 TABLEASM=$(addprefix $(OUT), $(patsubst %.c,%.proc.16.s,$(TABLESRC)))
93 $(OUT)romlayout16.o: romlayout.S $(OUT)blob.16.s $(TABLEASM)
94         @echo "  Generating 16bit layout of $@"
95         $(Q)$(CC) $(CFLAGS16) -c -D__ASSEMBLY__ $< -o $@
96
97 $(OUT)romlayout32.o: ; $(call whole-compile, $(CFLAGS), $(addprefix src/, $(SRC32)),$@)
98
99 $(OUT)rom32.o: $(OUT)romlayout32.o $(OUT)rombios32.lds
100         @echo "  Linking (no relocs) $@"
101         $(Q)$(LD) -r -d -T $(OUT)rombios32.lds $< -o $@
102
103 $(OUT)rom16.o: $(OUT)romlayout16.o $(OUT)rom32.o $(OUT)rombios16.lds
104         @echo "  Linking $@"
105         $(Q)$(OBJCOPY) --prefix-symbols=_code32_ $(OUT)rom32.o $(OUT)rom32.rename.o
106         $(Q)$(LD) -T $(OUT)rombios16.lds -R $(OUT)rom32.rename.o $< -o $@
107
108 $(OUT)rom.o: $(OUT)rom16.o $(OUT)rom32.o $(OUT)rombios.lds
109         @echo "  Linking $@"
110         $(Q)$(LD) -T $(OUT)rombios.lds $(OUT)rom16.o $(OUT)rom32.o -o $@
111
112 $(OUT)bios.bin.elf: $(OUT)rom.o
113         @echo "  Prepping $@"
114         $(Q)$(NM) $< | ./tools/checkrom.py
115         $(Q)$(STRIP) $< -o $@
116
117 $(OUT)bios.bin: $(OUT)bios.bin.elf
118         @echo "  Extracting binary $@"
119         $(Q)$(OBJCOPY) -O binary $< $@
120
121
122 ####### Generic rules
123 clean:
124         $(Q)rm -rf $(OUT)
125
126 $(OUT):
127         $(Q)mkdir $@
128
129 -include $(OUT)*.d