-
Notifications
You must be signed in to change notification settings - Fork 129
/
Makefile
391 lines (305 loc) · 11.5 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
## 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
# Check cross compiler
ifneq ($(findstring clang,$(CROSS_COMPILE)),)
CC_IS_CLANG = y
else
CC_IS_GCC = y
endif
# Setup toolchain macros
ifdef CC_IS_CLANG
clang_version:=$(strip $(patsubst clang%, %, $(notdir $(CROSS_COMPILE))))
clang_path:=$(dir $(wildcard $(abspath $(CROSS_COMPILE))))
cpp= $(clang_path)clang-cpp$(clang_version)
sstrip= $(clang_path)llvm-strip$(clang_version)
cc= $(clang_path)clang$(clang_version)
ld = $(clang_path)ld.lld$(clang_version)
as= $(clang_path)llvm-as$(clang_version)
objcopy= $(clang_path)llvm-objcopy$(clang_version)
objdump= $(clang_path)llvm-objdump$(clang_version)
readelf= $(clang_path)llvm-readelf$(clang_version)
size= $(clang_path)llvm-size$(clang_version)
else
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
endif
HOST_CC:=gcc
#Makefile arguments and default values
DEBUG:=n
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))))
# Check platform target and set platform, driver and arch dirs based on it
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
# Check configuration exists and set configurtion sources based on it
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
ifneq ($(build_targets),)
ifeq ($(CONFIG),)
$(error Configuration (CONFIG) not defined.)
endif
ifeq ($(config_src),)
$(error Cant find file for $(CONFIG) config!)
endif
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))
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_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_arch_def_generator_src:=$(wildcard $(scripts_dir)/arch/$(ARCH)/platform_defs_gen.c)
platform_def_generator_src+=$(platform_arch_def_generator_src)
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)
# 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))
c_src_files:=$(wildcard $(patsubst %.o,%.c, $(objs-y)))
asm_src_files:=$(wildcard $(patsubst %.o,%.S, $(objs-y)))
c_hdr_files=$(shell cat $(deps) | grep -o "$(src_dir)/\S*\.h" | sort | uniq)
deps+=$(patsubst %.o,%.d,$(objs-y))
objs-y:=$(patsubst $(src_dir)%, $(build_dir)%, $(objs-y))
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
ifeq ($(CC_IS_GCC),y)
build_macros+=-DCC_IS_GCC
else ifeq ($(CC_IS_CLANG),y)
build_macros+=-DCC_IS_CLANG
endif
override CPPFLAGS+=$(addprefix -I, $(inc_dirs)) $(arch-cppflags) \
$(platform-cppflags) $(build_macros)
vpath:.=CPPFLAGS
HOST_CPPFLAGS+=$(addprefix -I, $(inc_dirs)) $(arch-cppflags) \
$(platform-cppflags) $(build_macros)
ifeq ($(DEBUG), y)
debug_flags:=-g
OPTIMIZATIONS:=g
endif
ifeq ($(CC_IS_GCC),y)
cflags_warns:= \
-Warith-conversion -Wbuiltin-declaration-mismatch \
-Wcomments -Wdiscarded-qualifiers \
-Wimplicit-fallthrough \
-Wswitch-unreachable -Wreturn-local-addr \
-Wshift-count-negative -Wuninitialized \
-Wunused -Wunused-local-typedefs -Wunused-parameter \
-Wunused-result -Wvla \
-Wconversion -Wsign-conversion \
-Wmissing-prototypes -Wmissing-declarations \
-Wswitch-default -Wshadow -Wshadow=global \
-Wcast-qual -Wunused-macros \
-Wstrict-prototypes -Wunused-but-set-variable
override CFLAGS+=-Wno-unused-command-line-argument \
-pedantic -pedantic-errors
override LDFLAGS+=--no-check-sections
else ifeq ($(CC_IS_CLANG), y)
override CFLAGS+=-Wno-unused-command-line-argument --target=$(clang_arch_target)
override CPPFLAGS+=--target=$(clang_arch_target) -ffreestanding
override LDFLAGS+=--no-check-sections
endif
override CFLAGS+=-O$(OPTIMIZATIONS) -Wall -Werror -Wextra $(cflags_warns) \
-ffreestanding -std=c11 -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 $@ > $@.txt
ifneq ($(DEBUG), y)
@echo "Striping $@"
@$(sstrip) -s $@
endif
$(ld_script_temp):
@echo "Pre-processing $(patsubst $(cur_dir)/%, %, $(ld_script))"
@$(cc) $(CFLAGS) -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) $(CFLAGS) -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 == "//#" || $$1 == "##" || $$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) $(CFLAGS) -MM -MG -MT "$(config_obj) $@" $(CPPFLAGS) $(filter %.c, $^) > $@
@$(cc) $(CFLAGS) $(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) $(HOST_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) $(HOST_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
# Count lines of code for the exact target platform and configuration
.PHONY: cloc
cloc: | $(deps)
@cloc --by-file-by-lang $(c_src_files) $(asm_src_files) $(c_hdr_files)
#Clean all object, dependency and generated files
.PHONY: clean
clean:
@echo "Erasing directories..."
-rm -rf $(build_dir)
-rm -rf $(bin_dir)
# Instantiate CI rules
ifneq ($(wildcard $(ci_dir)/ci.mk),)
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, *) \
)
all_c_src_files=$(realpath $(call list_dir_files_recursive, src, *.c))
all_c_hdr_files=$(realpath $(call list_dir_files_recursive, src, *.h))
all_c_files=$(all_c_src_files) $(all_c_hdr_files)
$(call ci, license, "Apache-2.0", $(all_files))
$(call ci, format, $(all_c_files))
.PHONY: ci
ci: license-check format-check
endif