Rework linker scripts so they work on new version of ld.
[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 COMMONCFLAGS += $(call cc-option,$(CC),-nopie,)
27 COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector,)
28 COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector-all,)
29
30 override CFLAGS = $(COMMONCFLAGS) -g -DMODE16=0
31 CFLAGS16INC = $(COMMONCFLAGS) -DMODE16=1 -fno-jump-tables -fno-defer-pop \
32               $(call cc-option,$(CC),--param large-stack-frame=4,)
33 CFLAGS16INC += -ffunction-sections -fdata-sections
34 CFLAGS16 = $(CFLAGS16INC) -g
35
36 all: $(OUT) $(OUT)bios.bin
37
38 # Run with "make V=1" to see the actual compile commands
39 ifdef V
40 Q=
41 else
42 Q=@
43 endif
44
45 OBJCOPY=objcopy
46 OBJDUMP=objdump
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 TESTGCC:=$(shell CC=$(CC) tools/test-gcc.sh)
58 ifeq "$(TESTGCC)" "-1"
59 $(error "Please upgrade GCC")
60 endif
61
62 ifndef AVOIDCOMBINE
63 AVOIDCOMBINE=$(TESTGCC)
64 endif
65
66 # Do a whole file compile - two methods are supported.  The first
67 # involves including all the content textually via #include
68 # directives.  The second method uses gcc's "-combine" option.
69 ifeq "$(AVOIDCOMBINE)" "1"
70 define whole-compile
71 @echo "  Compiling whole program $3"
72 $(Q)printf '$(foreach i,$2,#include "../$i"\n)' > $3.tmp.c
73 $(Q)$(CC) $1 -c $3.tmp.c -o $3
74 endef
75 else
76 define whole-compile
77 @echo "  Compiling whole program $3"
78 $(Q)$(CC) $1 -combine -c $2 -o $3
79 endef
80 endif
81
82
83 $(OUT)%.s: %.c
84         @echo "  Compiling to assembler $@"
85         $(Q)$(CC) $(CFLAGS16INC) -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.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 $(OUT)romlayout16.o: romlayout.S $(OUT)ccode.16.s $(OUT)asm-offsets.h
98         @echo "  Compiling (16bit) $@"
99         $(Q)$(CC) $(CFLAGS16INC) -c -D__ASSEMBLY__ $< -o $@
100
101 $(OUT)ccode32.o: ; $(call whole-compile, $(CFLAGS), $(addprefix src/, $(SRC32)),$@)
102
103 $(OUT)rom32.o: $(OUT)ccode32.o $(OUT)rombios32.lds
104         @echo "  Linking (no relocs) $@"
105         $(Q)$(LD) -r -T $(OUT)rombios32.lds $< -o $@
106
107 $(OUT)romlayout.lds: $(OUT)romlayout16.o
108         @echo "  Building layout information $@"
109         $(Q)$(OBJDUMP) -h $< | ./tools/layoutrom.py $@
110
111 $(OUT)rombios16.lds: $(OUT)romlayout.lds
112
113 $(OUT)rom16.o: $(OUT)romlayout16.o $(OUT)rom32.o $(OUT)rombios16.lds
114         @echo "  Linking (16bit) $@"
115         $(Q)$(OBJCOPY) --prefix-symbols=_code32_ $(OUT)rom32.o $(OUT)rom32.rename.o
116         $(Q)$(LD) -T $(OUT)rombios16.lds -R $(OUT)rom32.rename.o $< -o $@
117
118 $(OUT)rom.o: $(OUT)rom16.o $(OUT)rom32.o $(OUT)rombios.lds
119         @echo "  Linking $@"
120         $(Q)$(LD) -T $(OUT)rombios.lds $(OUT)rom16.o $(OUT)rom32.o -o $@
121
122 $(OUT)bios.bin.elf: $(OUT)rom.o
123         @echo "  Prepping $@"
124         $(Q)$(NM) $< | ./tools/checkrom.py
125         $(Q)$(STRIP) $< -o $@
126
127 $(OUT)bios.bin: $(OUT)bios.bin.elf
128         @echo "  Extracting binary $@"
129         $(Q)$(OBJCOPY) -O binary $< $@
130
131
132 ####### Generic rules
133 clean:
134         $(Q)rm -rf $(OUT)
135
136 $(OUT):
137         $(Q)mkdir $@
138
139 -include $(OUT)*.d