forked from bao-project/bao-hypervisor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
207 lines (163 loc) · 5.61 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
##
# Bao, a Lightweight Static Partitioning Hypervisor
#
# Copyright (c) Bao Project (www.bao-project.org), 2019-
#
# Authors:
# Jose Martins <[email protected]>
#
# Bao is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License version 2 as published by the Free
# Software Foundation, with a special exception exempting guest code from such
# license. See the COPYING file in the top-level directory for details.
#
##
SHELL:=bash
PROJECT_NAME:=bao
# 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
#Makefile arguments and default values
DEBUG:=y
OPTIMIZATIONS:=0
CONFIG=
PLATFORM=
# List existing submakes
submakes:=config
# Directories
cur_dir:=$(abspath .)
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
#Plataform must be defined excpet for clean target
ifeq ($(PLATFORM),)
ifneq ($(MAKECMDGOALS), clean)
$(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)
cpu_impl_dir=$(cpu_arch_dir)/impl/$(CPU)
-include $(cpu_arch_dir)/arch.mk
build_dir:=$(cur_dir)/build/$(PLATFORM)
bin_dir:=$(cur_dir)/bin/$(PLATFORM)
directories:=$(build_dir) $(bin_dir)
SRC_DIRS:= $(cpu_arch_dir) $(cpu_impl_dir) $(lib_dir) $(core_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:= $(cur_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)
# Toolchain flags
cppflags+=$(addprefix -I, $(INC_DIRS)) $(arch-cppflags) $(platform-cppflags)
vpath:.=cppflags
cflags= -O$(OPTIMIZATIONS) -Wall -ffreestanding -std=gnu11 $(arch-cflags)\
$(platform-cflags) $(cppflags)
ifeq ($(DEBUG), y)
cflags += -g
endif
asflags:= $(cflags) $(arch-asflags) $(platform-asflags)
ldflags:= -build-id=none -nostdlib $(arch-ldflags) $(platform-ldflags)
# Default rule "make"
.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 $@
# @$(size) $@
@$(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 $(LD_SCRIPT) \
| grep -v '^\#' > $(LD_SCRIPT_TEMP)
ifeq (, $(findstring $(MAKECMDGOALS), clean $(submakes)))
-include $(deps)
endif
$(LD_SCRIPT_TEMP).d: $(LD_SCRIPT)
@echo "Creating dependecy $(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 dependecy $(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 $< $@
#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) $< -o - \
| awk '($$1 == "->") { print "#define " $$2 " " $$3 }' > $@
$(ASM_DEFS_HDR).d: $(ASM_DEFS_SRC)
@echo "Creating dependecy $(patsubst $(cur_dir)/%, %,\
$(patsubst %.d,%, $@))"
@$(cc) -MM -MT "$(patsubst %.d,%, $@)" $(addprefix -I, $(INC_DIRS)) $< > $@
endif
#build configuration binary if CONFIG target is defined
ifdef CONFIG
export
all: config
clean: config
endif
config:
@$(MAKE) -C $(configs_dir) $(MAKECMDGOALS)
#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 $@
#Clean all object, dependency and generated files
.PHONY: clean
clean:
@echo "Erasing directories..."
-rm -rf $(build_dir)
-rm -rf $(bin_dir)