forked from bao-project/bao-hypervisor
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Makefile
318 lines (242 loc) · 8.91 KB
/
Makefile
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
## SPDX-License-Identifier: Apache-2.0
## Copyright (c) Bao Project and Contributors. All rights reserved.
SHELL:=bash
PROJECT_NAME:=bao
# Helper functions
define current_directory
$(realpath $(dir $(lastword $(MAKEFILE_LIST))))
endef
# Setup toolchain macros
cpp= $(CROSS_COMPILE)cpp
sstrip= $(CROSS_COMPILE)strip
cc= $(CROSS_COMPILE)gcc
ld = $(CROSS_COMPILE)ld
as= $(CROSS_COMPILE)as
objcopy= $(CROSS_COMPILE)objcopy
objdump= $(CROSS_COMPILE)objdump
readelf= $(CROSS_COMPILE)readelf
size= $(CROSS_COMPILE)size
HOST_CC:=gcc
#Makefile arguments and default values
DEBUG:=y
OPTIMIZATIONS:=2
CONFIG=
PLATFORM=
# Setup version
version:= baoversion_$(subst -,_,$(shell git describe --always --dirty --tag --match "v*\.*\.*"))
# Directories
cur_dir:=$(current_directory)
src_dir:=$(cur_dir)/src
cpu_arch_dir=$(src_dir)/arch
lib_dir=$(src_dir)/lib
core_dir=$(src_dir)/core
platforms_dir=$(src_dir)/platform
configs_dir=$(cur_dir)/configs
CONFIG_REPO?=$(configs_dir)
scripts_dir:=$(cur_dir)/scripts
ci_dir:=$(cur_dir)/ci
src_dirs:=
include $(ci_dir)/ci.mk
targets:=$(MAKECMDGOALS)
ifeq ($(targets),)
targets:=all
endif
non_build_targets+=ci clean
build_targets:=$(strip $(foreach target, $(targets), \
$(if $(findstring $(target),$(non_build_targets)),,$(target))))
#Plataform must be defined excpet for non-build-targets
ifeq ($(PLATFORM),)
ifneq ($(build_targets),)
$(error Target platform argument (PLATFORM) not specified)
endif
endif
platform_dir=$(platforms_dir)/$(PLATFORM)
drivers_dir=$(platforms_dir)/drivers
ifeq ($(wildcard $(platform_dir)),)
$(error Target platform $(PLATFORM) is not supported)
endif
-include $(platform_dir)/platform.mk # must define ARCH and CPU variables
cpu_arch_dir=$(src_dir)/arch/$(ARCH)
-include $(cpu_arch_dir)/arch.mk
ifneq ($(MAKECMDGOALS), clean)
core_mem_prot_dir:=$(core_dir)/$(arch_mem_prot)
endif
build_dir:=$(cur_dir)/build/$(PLATFORM)/$(CONFIG)
bin_dir:=$(cur_dir)/bin/$(PLATFORM)/$(CONFIG)
directories:=$(build_dir) $(bin_dir)
src_dirs+=$(cpu_arch_dir) $(lib_dir) $(core_dir) $(core_mem_prot_dir) \
$(platform_dir) $(addprefix $(drivers_dir)/, $(drivers))
inc_dirs:=$(addsuffix /inc, $(src_dirs))
# Setup list of objects for compilation
-include $(addsuffix /objects.mk, $(src_dirs))
objs-y:=
objs-y+=$(addprefix $(cpu_arch_dir)/, $(cpu-objs-y))
objs-y+=$(addprefix $(lib_dir)/, $(lib-objs-y))
objs-y+=$(addprefix $(core_dir)/, $(core-objs-y))
objs-y+=$(addprefix $(platform_dir)/, $(boards-objs-y))
objs-y+=$(addprefix $(drivers_dir)/, $(drivers-objs-y))
deps+=$(patsubst %.o,%.d,$(objs-y))
objs-y:=$(patsubst $(src_dir)%, $(build_dir)%, $(objs-y))
build_dirs:=$(patsubst $(src_dir)%, $(build_dir)%, $(src_dirs) $(inc_dirs))
directories+=$(build_dirs)
# Setup list of targets for compilation
targets-y+=$(bin_dir)/$(PROJECT_NAME).elf
targets-y+=$(bin_dir)/$(PROJECT_NAME).bin
# Generated files variables
ld_script:= $(src_dir)/linker.ld
ld_script_temp:= $(build_dir)/linker_temp.ld
deps+=$(ld_script_temp).d
asm_defs_src:=$(cpu_arch_dir)/asm_defs.c
asm_defs_hdr:=$(patsubst $(src_dir)%, $(build_dir)%, \
$(cpu_arch_dir))/inc/asm_defs.h
inc_dirs+=$(patsubst $(src_dir)%, $(build_dir)%, $(cpu_arch_dir))/inc
deps+=$(asm_defs_hdr).d
gens:=
gens+=$(asm_defs_hdr)
config_dir:=$(CONFIG_REPO)
config_src:=$(wildcard $(config_dir)/$(CONFIG).c)
ifeq ($(config_src),)
config_dir:=$(CONFIG_REPO)/$(CONFIG)
config_src:=$(wildcard $(config_dir)/config.c)
endif
config_build_dir:=$(build_dir)/config
platform_build_dir:=$(build_dir)/platform
scripts_build_dir:=$(build_dir)/scripts
directories+=$(config_build_dir) $(platform_build_dir) $(scripts_build_dir)
config_def_generator_src:=$(scripts_dir)/config_defs_gen.c
config_def_generator:=$(scripts_build_dir)/config_defs_gen
config_defs:=$(config_build_dir)/config_defs_gen.h
gens+=$(config_def_generator) $(config_defs)
inc_dirs+=$(config_build_dir)
platform_def_generator_src:=$(scripts_dir)/platform_defs_gen.c
platform_def_generator:=$(scripts_build_dir)/platform_defs_gen
platform_defs:=$(platform_build_dir)/platform_defs_gen.h
platform_description:=$(platform_dir)/$(platform_description)
gens+=$(platform_defs) $(platform_def_generator)
inc_dirs+=$(platform_build_dir)
ifneq ($(build_targets),)
ifeq ($(CONFIG),)
$(error Configuration (CONFIG) not defined.)
endif
ifeq ($(config_src),)
$(error Cant find file for $(CONFIG) config!)
endif
endif
config_obj:=$(config_src:$(config_dir)/%.c=$(config_build_dir)/%.o)
config_dep:=$(config_src:$(config_dir)/%.c=$(config_build_dir)/%.d)
deps+=$(config_dep)
objs-y+=$(config_obj)
# Toolchain flags
build_macros:=
ifeq ($(arch_mem_prot),mmu)
build_macros+=-DMEM_PROT_MMU
endif
ifeq ($(arch_mem_prot),mpu)
build_macros+=-DMEM_PROT_MPU
endif
override CPPFLAGS+=$(addprefix -I, $(inc_dirs)) $(arch-cppflags) \
$(platform-cppflags) $(build_macros)
vpath:.=CPPFLAGS
ifeq ($(DEBUG), y)
debug_flags:=-g
endif
override CFLAGS+=-O$(OPTIMIZATIONS) -Wall -Werror -ffreestanding -std=gnu11 \
-fno-pic $(arch-cflags) $(platform-cflags) $(CPPFLAGS) $(debug_flags)
override ASFLAGS+=$(CFLAGS) $(arch-asflags) $(platform-asflags)
override LDFLAGS+=-build-id=none -nostdlib --fatal-warnings \
--defsym=$(version)=0 \
-z common-page-size=$(PAGE_SIZE) -z max-page-size=$(PAGE_SIZE) \
$(arch-ldflags) $(platform-ldflags)
ifneq ($(build_targets),)
.PHONY: all
all: $(targets-y)
$(bin_dir)/$(PROJECT_NAME).elf: $(gens) $(objs-y) $(ld_script_temp)
@echo "Linking $(patsubst $(cur_dir)/%, %, $@)"
@$(ld) $(LDFLAGS) -T$(ld_script_temp) $(objs-y) -o $@
@$(objdump) -S --wide $@ > $(basename $@).asm
@$(readelf) -a --wide $@ > [email protected]
ifneq ($(DEBUG), y)
@echo "Striping $@"
@$(sstrip) -s $@
endif
$(ld_script_temp):
@echo "Pre-processing $(patsubst $(cur_dir)/%, %, $(ld_script))"
@$(cc) -E $(addprefix -I, $(inc_dirs)) -x assembler-with-cpp $(CPPFLAGS) \
$(ld_script) | grep -v '^\#' > $(ld_script_temp)
ifneq ($(build_targets),)
-include $(deps)
endif
$(ld_script_temp).d: $(ld_script)
@echo "Creating dependency $(patsubst $(cur_dir)/%, %, $<)"
@$(cc) -x assembler-with-cpp -MM -MT "$(ld_script_temp) $@" \
$(addprefix -I, $(inc_dirs)) $< > $@
$(build_dir)/%.d : $(src_dir)/%.[c,S]
@echo "Creating dependency $(patsubst $(cur_dir)/%, %, $<)"
@$(cc) -MM -MG -MT "$(patsubst %.d, %.o, $@) $@" $(CPPFLAGS) $< > $@
$(objs-y):
@echo "Compiling source $(patsubst $(cur_dir)/%, %, $<)"
@$(cc) $(CFLAGS) -c $< -o $@
%.bin: %.elf
@echo "Generating binary $(patsubst $(cur_dir)/%, %, $@)"
@$(objcopy) -S -O binary $< $@
$(deps): | $(gens)
#Generate assembly macro definitions from arch/$(ARCH)/$(asm_defs_src) if such
# file exists
ifneq ($(wildcard $(asm_defs_src)),)
$(asm_defs_hdr): $(asm_defs_src)
@echo "Generating header $(patsubst $(cur_dir)/%, %, $@)"
@$(cc) -S $(CFLAGS) -DGENERATING_DEFS $< -o - \
| awk '($$1 == "->") \
{ gsub("#", "", $$3); print "#define " $$2 " " $$3 }' > $@
$(asm_defs_hdr).d: $(asm_defs_src)
@echo "Creating dependency $(patsubst $(cur_dir)/%, %,\
$(patsubst %.d,%, $@))"
@$(cc) -MM -MT $(CFLAGS) "$(patsubst %.d,%, $@)" $(addprefix -I, $(inc_dirs)) $< > $@
endif
$(config_dep): $(config_src)
@echo "Creating dependency $(patsubst $(cur_dir)/%, %,\
$(patsubst %.d,%, $@))"
@$(cc) -MM -MG -MT "$(config_obj) $@" $(CPPFLAGS) $(filter %.c, $^) > $@
@$(cc) $(CPPFLAGS) -S $(config_src) -o - | grep ".incbin" | \
awk '{ gsub("\"", "", $$2); print "$(config_obj): " $$2 }' >> $@
$(config_def_generator): $(config_def_generator_src) $(config_src)
@echo "Compiling generator $(patsubst $(cur_dir)/%, %, $@)"
@$(HOST_CC) $^ $(build_macros) $(CPPFLAGS) -DGENERATING_DEFS \
$(addprefix -I, $(inc_dirs)) -o $@
$(config_defs): $(config_def_generator)
@echo "Generating header $(patsubst $(cur_dir)/%, %, $@)"
@$(config_def_generator) > $(config_defs)
$(platform_def_generator): $(platform_def_generator_src) $(platform_description)
@echo "Compiling generator $(patsubst $(cur_dir)/%, %, $@)"
@$(HOST_CC) $^ $(build_macros) $(CPPFLAGS) -DGENERATING_DEFS -D$(ARCH) \
$(addprefix -I, $(inc_dirs)) -o $@
$(platform_defs): $(platform_def_generator)
@echo "Generating header $(patsubst $(cur_dir)/%, %, $@)"
@$(platform_def_generator) > $(platform_defs)
#Generate directories for object, dependency and generated files
.SECONDEXPANSION:
$(objs-y) $(deps) $(targets-y) $(gens): | $$(@D)
$(directories):
@echo "Creating directory $(patsubst $(cur_dir)/%, %, $@)"
@mkdir -p $@
endif
#Clean all object, dependency and generated files
.PHONY: clean
clean:
@echo "Erasing directories..."
-rm -rf $(build_dir)
-rm -rf $(bin_dir)
# Instantiate CI rules
all_files= $(realpath \
$(cur_dir)/Makefile \
$(call list_dir_files_recursive, $(src_dir), *) \
$(call list_dir_files_recursive, $(scripts_dir), *) \
$(call list_dir_files_recursive, $(config_dir)/example, *) \
)
c_src_files=$(realpath $(call list_dir_files_recursive, src, *.c))
c_hdr_files=$(realpath $(call list_dir_files_recursive, src, *.h))
c_files=$(c_src_files) $(c_hdr_files)
$(call ci, license, "Apache-2.0", $(all_files))
$(call ci, format, $(c_files))
.PHONY: ci
ci: license-check format-check