forked from nanovms/nanos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.mk
334 lines (261 loc) · 7.71 KB
/
rules.mk
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
IMAGE= $(OUTDIR)/image/disk.raw
DEFAULT_KERNEL_TARGET= kernel
include $(SRCDIR)/drivers/acpica.mk
include $(SRCDIR)/runtime/files.mk
# To reveal verbose build messages, override Q= in command line.
Q= @
ECHO= echo
CAT= cat
# XXX llvm for darwin
CP= cp
MV= mv -f
DD= dd
ifeq ($(UNAME_s),Darwin)
GNUTAR= gnutar
else
GNUTAR= tar
endif
GIT= git
GO= go
MKDIR= mkdir -p
ifeq ($(ARCH),x86_64)
AS= nasm
ASDEPFLAGS= -MD $(patsubst %.o,%.d,$@) -MP -MT $@
else
AS= $(CROSS_COMPILE)as
ASDEPFLAGS=
endif
ifeq ($(UNAME_s),Darwin)
CC= cc
else
CC= $(CROSS_COMPILE)gcc
endif
COMPILER_VERSION := $(shell $(CC) --version)
LD= $(CC)
LN= ln
AWK= awk
SED= sed
TR= tr
SORT= sort
STRIP= $(CROSS_COMPILE)strip
SYNC= sync
TAR= tar
OBJCOPY= $(CROSS_COMPILE)objcopy
OBJDUMP= $(CROSS_COMPILE)objdump
RM= rm -f
RMDIR= rmdir
TOUCH= touch
UMOUNT= umount
XXD= xxd
# gcov
GCOV= gcov
LCOV= lcov
GENHTML= genhtml
CFLAGS+= -std=gnu11 -g
CFLAGS+= -Wall -Werror -Wno-char-subscripts -Wno-format-truncation -Wno-unknown-warning-option -Wno-stringop-overflow
CFLAGS+= -I$(OBJDIR)
DEPFLAGS= -MD -MP -MT $@
KERNCFLAGS= -nostdinc \
-fno-builtin \
-fpie \
-fdata-sections \
-ffunction-sections
ifeq ($(ARCH),x86_64)
KERNCFLAGS+= -mno-mmx -mno-sse -mno-sse2
endif
ifeq ($(ARCH),aarch64)
KERNCFLAGS+= -march=armv8-a+nofp+nosimd -mcpu=cortex-a72 -ffixed-x18
# Workaround for GCC 12/13 bug to avoid incorrect pointer analysis https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105523
CCMAJ=$(shell $(CC) -dumpversion | awk -F. '{print $$1}')
ifeq ($(CCMAJ),$(filter $(CCMAJ),12 13))
KERNCFLAGS+= --param=min-pagesize=0
endif
# Don't ask clang since it doesn't know --help=target
ifeq ($(findstring clang,$(COMPILER_VERSION)),clang)
OUTLINE_ATOMICS=0
else
OUTLINE_ATOMICS= $(shell $(CC) --help=target | grep -c outline-atomics)
endif
ifneq ($(OUTLINE_ATOMICS),0)
KERNCFLAGS+= -mno-outline-atomics
endif
endif
ifeq ($(ARCH),riscv64)
KERNCFLAGS+= -march=rv64gc -mabi=lp64d
endif
ifeq ($(findstring clang,$(COMPILER_VERSION)),clang)
TARGET_CFLAGS= -target $(ARCH)-elf
endif
KERNCFLAGS+= $(TARGET_CFLAGS) -fno-omit-frame-pointer
KERNLDFLAGS= --gc-sections -z notext -z noexecstack -z max-page-size=4096 -L $(OBJDIR) -pie --no-dynamic-linker
ifneq ($(UBSAN),)
KERNCFLAGS+= -fsanitize=undefined -fno-sanitize=alignment,null -fsanitize-undefined-trap-on-error
endif
ifeq ($(MEMDEBUG),mcache)
CFLAGS+= -DMEMDEBUG_MCACHE
else ifeq ($(MEMDEBUG),backed)
CFLAGS+= -DMEMDEBUG_BACKED
else ifeq ($(MEMDEBUG),all)
CFLAGS+= -DMEMDEBUG_ALL
endif
.PHONY: $(OUTDIR)/debug.h.tmp
$(OUTDIR)/debug.h.tmp:
@$(MKDIR) $(dir $@)
ifeq ($(DEBUG),all)
@$(CP) $(SRCDIR)/debug_all.h $@
else
ifneq ($(DEBUG),)
@echo $(DEBUG) | $(TR) '[:lower:]' '[:upper:]' | $(SORT) | $(SED) -e 's/\s*//g' -e 's/,/\n/g' | $(SED) -e 's/^\(.*\)$$/\#define \1_DEBUG/g' > $@
else
@$(CAT) /dev/null > $@
endif
endif
$(OUTDIR)/debug.h: $(OUTDIR)/debug.h.tmp
@if ! (cmp -s $< $@); then $(CP) $< $@; fi
GENHEADERS+= $(OUTDIR)/debug.h
##############################################################################
# functions
# reverse list
reverse= $(if $(1),$(call reverse,$(wordlist 2,$(words $(1)),$(1)))) $(firstword $(1))
# execute command (additional empty line is necessary!)
define execute_command
$1
endef
# get object file from source file
# $1 - object file suffix (.o)
# $2 - source file
objfile= $(patsubst $(ROOTDIR)/%$(suffix $2),$(OBJDIR)/%$1,$2)
cc-option= $(shell if $(CC) -Werror $(1) -S -o /dev/null -xc /dev/null \
> /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
cmd= $(if $(Q),@ echo "$(msg_$(1))";) $(cmd_$(1))
##############################################################################
# commands
msg_ld= LD $@
cmd_ld= $(LD) $(LDFLAGS) $(LDFLAGS-$(@F)) $(OBJS_BEGIN) $(filter %.o,$^) $(LIBS-$(@F)) $(OBJS_END) -o $@
msg_cc= CC $@
cmd_cc= $(CC) $(DEPFLAGS) $(CFLAGS) $(CFLAGS-$(<F)) -c $< -o $@
msg_as= AS $@
cmd_as= $(AS) $(ASDEPFLAGS) $(AFLAGS) $(AFLAGS-$(<F)) $< -o $@
msg_go= GO $@
cmd_go= $(GO_ENV) $(GO) build $(GOFLAGS) -o $@ $^
msg_strip= STRIP $@
cmd_strip= $(STRIP) $(STRIPFLAGS) $(STRIPFLAGS-$(<F)) $< -o $@
msg_mvdis= MV kernel.dis kernel.dis.old
cmd_mvdis= if [ -f $(OBJDIR)/kernel.dis ]; then $(MV) $(OBJDIR)/kernel.dis $(OBJDIR)/kernel.dis.old; fi
msg_contgen= CONTGEN $@
cmd_contgen= $(CONTGEN) 10 10 >$@
msg_vendor= VENDOR $@
cmd_vendor= $(RM) -r $(@D) && $(GIT) clone $(GITFLAGS) $(@D) && $(TOUCH) $@ && \
([ ! -f $(PATCHDIR)/$(notdir $(@D)).patch ] || (patch -p1 -d$(@D) < $(PATCHDIR)/$(notdir $(@D)).patch))
##############################################################################
# build a program
define build_program
PROG-$1= $(OBJDIR)/bin/$1
OBJS-$1= $$(foreach s,$$(filter %.c %.s %.S,$$(SRCS-$1)),$$(call objfile,.o,$$s))
OBJDIRS-$1= $$(sort $$(dir $$(OBJS-$1)))
GENHEADERS-$1= $(OBJDIR)/closure_templates.h
DEPS-$1= $$(patsubst %.o,%.d,$$(OBJS-$1))
.PHONY: $1
$1: $$(PROG-$1)
ifneq ($$(OBJS-$1),)
ifeq ($(DEBUG_STRIP),)
$$(PROG-$1): $$(OBJS-$1)
@$(MKDIR) $$(dir $$@)
$$(call cmd,ld)
ifeq ($1,kernel.elf)
$(call cmd,mvdis)
endif
else
LDFLAGS-$1.dbg= $$(LDFLAGS-$1)
LIBS-$1.dbg= $$(LIBS-$1)
$$(PROG-$1).dbg: $$(OBJS-$1)
@$(MKDIR) $$(dir $$@)
$$(call cmd,ld)
$$(PROG-$1): $$(PROG-$1).dbg
$$(call cmd,strip)
endif
endif
DEPFILES+= $$(DEPS-$1)
GENHEADERS+= $$(GENHEADERS-$1)
CLEANFILES+= $$(PROG-$1) $$(OBJS-$1) $$(DEPS-$1) $$(GENHEADERS-$1)
ifneq ($(DEBUG_STRIP),)
CLEANFILES+= $$(PROG-$1).dbg
endif
CLEANDIRS+= $(OBJDIR)/bin $(OBJDIR)/src $$(OBJDIRS-$1)
endef
$(foreach prog, $(PROGRAMS) $(ADDITIONAL_PROGRAMS), $(eval $(call build_program,$(prog))))
ifeq ($(filter print-% clean cleandepend,$(MAKECMDGOALS)),)
-include $(sort $(DEPFILES))
endif
PROGRAM_BINARIES= $(foreach prog, $(PROGRAMS), $(OBJDIR)/bin/$(prog))
##############################################################################
# closure_templates
ifeq ($(CONTGEN),)
CONTGEN= $(OUTDIR)/tools/bin/contgen
$(CONTGEN):
@$(MAKE) -C $(ROOTDIR)/tools contgen
endif
$(OBJDIR)/closure_templates.h: $(CONTGEN)
@$(MKDIR) $(dir $@)
$(call cmd,contgen)
##############################################################################
# clean
.PHONY: clean pre-clean do-clean post-clean
.PHONY: cleandepend
pre-clean:
do-clean: pre-clean
$(foreach d,$(SUBDIR),$(call execute_command,$(Q) $(MAKE) -C $d clean))
$(Q) $(RM) $(CLEANFILES)
$(Q) $(RM) -d $(call reverse,$(sort $(CLEANDIRS)))
post-clean: do-clean
clean: post-clean
cleandepend:
$(foreach d,$(SUBDIR),$(call execute_command,$(Q) $(MAKE) -C $d cleandepend))
$(Q) $(RM) $(DEPFILES)
##############################################################################
# implicit rules
.SUFFIXES:
$(OBJDIR)/%.o: $(ROOTDIR)/%.s $(OBJDIR)/%.d
@$(MKDIR) $(dir $@)
$(call cmd,as)
# run .S files through gcc for preprocessing
$(OBJDIR)/%.o: $(ROOTDIR)/%.S $(OBJDIR)/%.d | $(sort $(GENHEADERS))
@$(MKDIR) $(dir $@)
$(call cmd,cc)
$(OBJDIR)/%.o: $(ROOTDIR)/%.c $(OBJDIR)/%.d | $(sort $(GENHEADERS))
@$(MKDIR) $(dir $@)
$(call cmd,cc)
$(DEPFILES):
$(OBJDIR)/bin/%: %.go
@$(MKDIR) $(dir $@)
$(call cmd,go)
%/.vendored:
$(call cmd,vendor)
##############################################################################
# other rules
.PHONY: all
print-%:
@echo "$* = [$($*)]"
ifneq ($(CURDIR),$(ROOTDIR))
-include $(ROOTDIR)/Makefile.local
endif
-include Makefile.local
ifdef BUILD_KERNEL_DIS
DEFAULT_KERNEL_TARGET= kernel.dis
BUILD_KERNEL_DIS= $(OBJDIR)/kernel.dis
endif
CLEANFILES+= $(OBJDIR)/kernel.dis.old $(OUTDIR)/debug.h $(OUTDIR)/debug.h.tmp
# Stack Smashing Protection
ifeq ($(WITHOUT_SSP),)
CFLAGS+= -fstack-protector-strong
ifneq ($(UNAME_s),Darwin)
ifeq ($(ARCH),aarch64)
# XXX SSP on arm not working yet; check flags
KERNCFLAGS+= -fstack-protector-all
else ifeq ($(ARCH),riscv64)
KERNCFLAGS+= -fstack-protector-all
else
KERNCFLAGS+= -mstack-protector-guard=global \
endif
endif
endif