-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathMakefile
More file actions
278 lines (224 loc) · 7.04 KB
/
Makefile
File metadata and controls
278 lines (224 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# Versioning information
#
VERSION := v3.0.7
WIMBOOT_VERSION := v2.8.0
SBAT_GENERATION := 1
# Abstract target-independent objects
#
OBJECTS := prefix.o
# Target-dependent objects
#
OBJECTS_PCBIOS :=
OBJECTS_i386 :=
OBJECTS_x86_64 :=
OBJECTS_i386_x86_64 :=
OBJECTS_arm64 :=
include posix/build.mk
include libnt/build.mk
include disk/build.mk
include kern/build.mk
OBJECTS_i386 += $(patsubst %.o,%.i386.o,$(OBJECTS))
OBJECTS_x86_64 += $(patsubst %.o,%.x86_64.o,$(OBJECTS))
OBJECTS_i386_x86_64 += $(patsubst %.o,%.i386.x86_64.o,$(OBJECTS))
OBJECTS_arm64 += $(patsubst %.o,%.arm64.o,$(OBJECTS))
# Header files
#
HEADERS := $(wildcard include/*.h)
# Common build tools
#
ECHO ?= echo
CP ?= cp
CD ?= cd
RM ?= rm
CUT ?= cut
CC ?= cc
AS ?= as
LD ?= ld
AR ?= ar
OBJCOPY ?= objcopy
# Get list of default compiler definitions
#
CCDEFS := $(shell $(CC) -E -x c -c /dev/null -dM | $(CUT) -d" " -f2)
# Detect compiler type
#
ifeq ($(filter __clang__,$(CCDEFS)),__clang__)
CC_i386 ?= $(CC) -target i386
CC_x86_64 ?= $(CC) -target x86_64
CC_arm64 ?= $(CC) -target aarch64
CFLAGS += -Wno-unused-command-line-argument
else ifeq ($(filter __GNUC__,$(CCDEFS)),__GNUC__)
CC := gcc
endif
# Guess appropriate cross-compilation prefixes
#
ifneq ($(filter __x86_64__,$(CCDEFS)),__x86_64__)
CROSS_x86_64 ?= x86_64-linux-gnu-
endif
ifneq ($(filter __aarch64__,$(CCDEFS)),__aarch64__)
CROSS_arm64 ?= aarch64-linux-gnu-
endif
CROSS_i386 ?= $(CROSS_x86_64)
# Build tools for i386 target
#
CC_i386 ?= $(CROSS_i386)$(CC)
AS_i386 ?= $(CROSS_i386)$(AS)
LD_i386 ?= $(CROSS_i386)$(LD)
AR_i386 ?= $(CROSS_i386)$(AR)
OBJCOPY_i386 ?= $(CROSS_i386)$(OBJCOPY)
# Build tools for x86_64 target
#
CC_x86_64 ?= $(CROSS_x86_64)$(CC)
AS_x86_64 ?= $(CROSS_x86_64)$(AS)
LD_x86_64 ?= $(CROSS_x86_64)$(LD)
AR_x86_64 ?= $(CROSS_x86_64)$(AR)
OBJCOPY_x86_64 ?= $(CROSS_x86_64)$(OBJCOPY)
# Build tools for arm64 target
#
CC_arm64 ?= $(CROSS_arm64)$(CC)
AS_arm64 ?= $(CROSS_arm64)$(AS)
LD_arm64 ?= $(CROSS_arm64)$(LD)
AR_arm64 ?= $(CROSS_arm64)$(AR)
OBJCOPY_arm64 ?= $(CROSS_arm64)$(OBJCOPY)
# Build flags for all targets
#
CFLAGS += -Os -ffreestanding -Wall -W -Werror
CFLAGS += -nostdinc -Iinclude/ -fshort-wchar
CFLAGS += -DVERSION="\"$(VERSION)\""
CFLAGS += -DWIMBOOT_VERSION="\"$(WIMBOOT_VERSION)\""
CFLAGS += -DSBAT_GENERATION="\"$(SBAT_GENERATION)\""
CFLAGS += -include compiler.h
ifneq ($(DEBUG),)
CFLAGS += -DDEBUG=$(DEBUG)
endif
CFLAGS += $(EXTRA_CFLAGS)
LDFLAGS += --no-relax
# Build flags for i386 target
#
CFLAGS_i386 := -m32 -march=i386 -malign-double -fno-pic
ASFLAGS_i386 := --32
LDFLAGS_i386 := -m elf_i386
# Build flags for x86_64 target
#
CFLAGS_x86_64 := -m64 -mno-red-zone -fpie
ASFLAGS_x86_64 := --64
LDFLAGS_x86_64 := -m elf_x86_64
# Build flags for arm64 target
#
CFLAGS_arm64 := -mlittle-endian -mcmodel=small -fno-pic
ASFLAGS_arm64 := -mabi=lp64 -EL
# Run a test compilation and discard any output
#
CC_TEST = $(CC_$(1)) $(CFLAGS) $(CFLAGS_$(1)) \
-x c -c - -o /dev/null >/dev/null 2>&1
# Test for supported compiler flags
#
CFLAGS_TEST = $(shell $(CC_TEST) $(2) </dev/null && $(ECHO) '$(2)')
# Enable stack protection if available
#
CFLAGS_SPG = $(call CFLAGS_TEST,$(1),-fstack-protector-strong \
-mstack-protector-guard=global)
# Inhibit unwanted debugging information
#
CFLAGS_CFI = $(call CFLAGS_TEST,$(1),-fno-dwarf2-cfi-asm \
-fno-exceptions \
-fno-unwind-tables \
-fno-asynchronous-unwind-tables)
# Inhibit warnings from taking address of packed struct members
#
CFLAGS_WNAPM = $(call CFLAGS_TEST,$(1),-Wno-address-of-packed-member)
# Inhibit LTO
#
CFLAGS_NLTO = $(call CFLAGS_TEST,$(1),-fno-lto)
# Inhibit address-significance table
#
CFLAGS_NAS = $(call CFLAGS_TEST,$(1),-fno-addrsig)
# Add -maccumulate-outgoing-args if required
#
MS_ABI_TEST_CODE := extern void __attribute__ (( ms_abi )) ms_abi(); \
void sysv_abi ( void ) { ms_abi(); }
define CFLAGS_MS_ABI
$(shell $(CC_TEST) -maccumulate-outgoing-args </dev/null && \
( $(ECHO) '$(MS_ABI_TEST_CODE)' | \
$(CC_TEST) -mno-accumulate-outgoing-args || \
$(ECHO) '-maccumulate-outgoing-args' ))
endef
# Conditional build flags
#
CFLAGS_COND = $(CFLAGS_SPG) $(CFLAGS_CFI) $(CFLAGS_WNAPM) $(CFLAGS_WNAB) \
$(CFLAGS_NLTO) $(CFLAGS_NAS) $(CFLAGS_MS_ABI)
CFLAGS_i386 += $(call CFLAGS_COND,i386)
CFLAGS_x86_64 += $(call CFLAGS_COND,x86_64)
CFLAGS_arm64 += $(call CFLAGS_COND,arm64)
###############################################################################
#
# Final targets
all : ntloader ntloader.i386 ntloader.x86_64 ntloader.arm64
ntloader : ntloader.x86_64 Makefile
$(CP) $< $@
ntloader.%.elf : lib.%.a script.lds Makefile
$(LD_$*) $(LDFLAGS) $(LDFLAGS_$*) -T script.lds -o $@ -q \
-Map ntloader.$*.map prefix.$*.o lib.$*.a
ntloader.i386.efi : \
ntloader.%.efi : ntloader.%.elf elf2efi32 Makefile
./elf2efi32 $< $@
ntloader.x86_64.efi ntloader.arm64.efi : \
ntloader.%.efi : ntloader.%.elf elf2efi64 Makefile
./elf2efi64 --hybrid $< $@
ntloader.% : ntloader.%.efi Makefile
$(CP) $< $@
# Utilities
include utils/build.mk
###############################################################################
#
# i386 objects
%.i386.s : %.S $(HEADERS) Makefile
$(CC_i386) $(CFLAGS) $(CFLAGS_i386) -DASSEMBLY -Ui386 -E $< -o $@
%.i386.s : %.c $(HEADERS) Makefile
$(CC_i386) $(CFLAGS) $(CFLAGS_i386) -S $< -o $@
%.i386.o : %.i386.s i386.i Makefile
$(AS_i386) $(ASFLAGS) $(ASFLAGS_i386) i386.i $< -o $@
lib.i386.a : $(OBJECTS_i386) Makefile
$(RM) -f $@
$(AR_i386) -r -s $@ $(OBJECTS_i386)
###############################################################################
#
# i386 objects to be linked into an x86_64 binary
%.i386.x86_64.raw.o : %.i386.s i386.i Makefile
$(AS_x86_64) $(ASFLAGS) $(ASFLAGS_x86_64) i386.i $< -o $@
%.i386.x86_64.o : %.i386.x86_64.raw.o Makefile
$(OBJCOPY_x86_64) --prefix-symbols=__i386_ $< $@
###############################################################################
#
# x86_64 objects
%.x86_64.s : %.S $(HEADERS) Makefile
$(CC_x86_64) $(CFLAGS) $(CFLAGS_x86_64) -DASSEMBLY -Ui386 -E $< -o $@
%.x86_64.s : %.c $(HEADERS) Makefile
$(CC_x86_64) $(CFLAGS) $(CFLAGS_x86_64) -S $< -o $@
%.x86_64.o : %.x86_64.s x86_64.i Makefile
$(AS_x86_64) $(ASFLAGS) $(ASFLAGS_x86_64) x86_64.i $< -o $@
lib.x86_64.a : $(OBJECTS_x86_64) $(OBJECTS_i386_x86_64) Makefile
$(RM) -f $@
$(AR_x86_64) -r -s $@ $(OBJECTS_x86_64) $(OBJECTS_i386_x86_64)
###############################################################################
#
# arm64 objects
%.arm64.s : %.S $(HEADERS) Makefile
$(CC_arm64) $(CFLAGS) $(CFLAGS_arm64) -DASSEMBLY -E $< -o $@
%.arm64.s : %.c $(HEADERS) Makefile
$(CC_arm64) $(CFLAGS) $(CFLAGS_arm64) -S $< -o $@
%.arm64.o : %.arm64.s Makefile
$(AS_arm64) $(ASFLAGS) $(ASFLAGS_arm64) $< -o $@
lib.arm64.a : $(OBJECTS_arm64) Makefile
$(RM) -f $@
$(AR_arm64) -r -s $@ $(OBJECTS_arm64)
###############################################################################
#
# Cleanup
clean :
$(RM) -f $(RM_FILES)
$(RM) -f *.s *.o *.a *.elf *.map
$(RM) -f ntloader.i386 ntloader.i386.*
$(RM) -f ntloader.x86_64 ntloader.x86_64.*
$(RM) -f ntloader.arm64 ntloader.arm64.*
$(RM) -f ntloader
.DELETE_ON_ERROR :