lib: utils/irqchip: Use heap in PLIC, APLIC and IMSIC drivers
[platform/kernel/opensbi.git] / Makefile
1 #
2 # SPDX-License-Identifier: BSD-2-Clause
3 #
4 # Copyright (c) 2019 Western Digital Corporation or its affiliates.
5 #
6 # Authors:
7 #   Anup Patel <anup.patel@wdc.com>
8 #
9
10 # Select Make Options:
11 # o  Do not use make's built-in rules
12 # o  Do not print "Entering directory ...";
13 MAKEFLAGS += -r --no-print-directory
14
15 # Readlink -f requires GNU readlink
16 ifeq ($(shell uname -s),Darwin)
17 READLINK ?= greadlink
18 else
19 READLINK ?= readlink
20 endif
21
22 # Find out source, build, and install directories
23 src_dir=$(CURDIR)
24 ifdef O
25  build_dir=$(shell $(READLINK) -f $(O))
26 else
27  build_dir=$(CURDIR)/build
28 endif
29 ifeq ($(build_dir),$(CURDIR))
30 $(error Build directory is same as source directory.)
31 endif
32 install_root_dir_default=$(CURDIR)/install
33 ifdef I
34  install_root_dir=$(shell $(READLINK) -f $(I))
35 else
36  install_root_dir=$(install_root_dir_default)/usr
37 endif
38 ifeq ($(install_root_dir),$(CURDIR))
39 $(error Install root directory is same as source directory.)
40 endif
41 ifeq ($(install_root_dir),$(build_dir))
42 $(error Install root directory is same as build directory.)
43 endif
44 ifdef PLATFORM_DIR
45   platform_dir_path=$(shell $(READLINK) -f $(PLATFORM_DIR))
46   ifdef PLATFORM
47     platform_parent_dir=$(platform_dir_path)
48   else
49     PLATFORM=$(shell basename $(platform_dir_path))
50     platform_parent_dir=$(shell realpath ${platform_dir_path}/..)
51   endif
52 else
53  platform_parent_dir=$(src_dir)/platform
54 endif
55 ifndef PLATFORM_DEFCONFIG
56 PLATFORM_DEFCONFIG=defconfig
57 endif
58
59 # Check if verbosity is ON for build process
60 CMD_PREFIX_DEFAULT := @
61 ifeq ($(V), 1)
62         CMD_PREFIX :=
63 else
64         CMD_PREFIX := $(CMD_PREFIX_DEFAULT)
65 endif
66
67 # Setup path of directories
68 export platform_subdir=$(PLATFORM)
69 export platform_src_dir=$(platform_parent_dir)/$(platform_subdir)
70 export platform_build_dir=$(build_dir)/platform/$(platform_subdir)
71 export include_dir=$(CURDIR)/include
72 export libsbi_dir=$(CURDIR)/lib/sbi
73 export libsbiutils_dir=$(CURDIR)/lib/utils
74 export firmware_dir=$(CURDIR)/firmware
75
76 # Setup variables for kconfig
77 ifdef PLATFORM
78 export PYTHONDONTWRITEBYTECODE=1
79 export KCONFIG_DIR=$(platform_build_dir)/kconfig
80 export KCONFIG_AUTOLIST=$(KCONFIG_DIR)/auto.list
81 export KCONFIG_AUTOHEADER=$(KCONFIG_DIR)/autoconf.h
82 export KCONFIG_AUTOCMD=$(KCONFIG_DIR)/auto.conf.cmd
83 export KCONFIG_CONFIG=$(KCONFIG_DIR)/.config
84 # Additional exports for include paths in Kconfig files
85 export OPENSBI_SRC_DIR=$(src_dir)
86 export OPENSBI_PLATFORM=$(PLATFORM)
87 export OPENSBI_PLATFORM_SRC_DIR=$(platform_src_dir)
88 endif
89
90 # Find library version
91 OPENSBI_VERSION_MAJOR=`grep "define OPENSBI_VERSION_MAJOR" $(include_dir)/sbi/sbi_version.h | sed 's/.*MAJOR.*\([0-9][0-9]*\)/\1/'`
92 OPENSBI_VERSION_MINOR=`grep "define OPENSBI_VERSION_MINOR" $(include_dir)/sbi/sbi_version.h | sed 's/.*MINOR.*\([0-9][0-9]*\)/\1/'`
93 OPENSBI_VERSION_GIT=$(shell if [ -d $(src_dir)/.git ]; then git describe 2> /dev/null; fi)
94
95 # Setup compilation commands
96 ifneq ($(LLVM),)
97 CC              =       clang
98 AR              =       llvm-ar
99 LD              =       ld.lld
100 OBJCOPY         =       llvm-objcopy
101 else
102 ifdef CROSS_COMPILE
103 CC              =       $(CROSS_COMPILE)gcc
104 AR              =       $(CROSS_COMPILE)ar
105 LD              =       $(CROSS_COMPILE)ld
106 OBJCOPY         =       $(CROSS_COMPILE)objcopy
107 else
108 CC              ?=      gcc
109 AR              ?=      ar
110 LD              ?=      ld
111 OBJCOPY         ?=      objcopy
112 endif
113 endif
114 CPP             =       $(CC) -E
115 AS              =       $(CC)
116 DTC             =       dtc
117
118 ifneq ($(shell $(CC) --version 2>&1 | head -n 1 | grep clang),)
119 CC_IS_CLANG     =       y
120 else
121 CC_IS_CLANG     =       n
122 endif
123
124 ifneq ($(shell $(LD) --version 2>&1 | head -n 1 | grep LLD),)
125 LD_IS_LLD       =       y
126 else
127 LD_IS_LLD       =       n
128 endif
129
130 ifeq ($(CC_IS_CLANG),y)
131 ifneq ($(CROSS_COMPILE),)
132 CLANG_TARGET    =       --target=$(notdir $(CROSS_COMPILE:%-=%))
133 endif
134 endif
135
136 # Guess the compiler's XLEN
137 OPENSBI_CC_XLEN := $(shell TMP=`$(CC) $(CLANG_TARGET) -dumpmachine | sed 's/riscv\([0-9][0-9]\).*/\1/'`; echo $${TMP})
138
139 # Guess the compiler's ABI and ISA
140 ifneq ($(CC_IS_CLANG),y)
141 OPENSBI_CC_ABI := $(shell TMP=`$(CC) -v 2>&1 | sed -n 's/.*\(with\-abi=\([a-zA-Z0-9]*\)\).*/\2/p'`; echo $${TMP})
142 OPENSBI_CC_ISA := $(shell TMP=`$(CC) -v 2>&1 | sed -n 's/.*\(with\-arch=\([a-zA-Z0-9]*\)\).*/\2/p'`; echo $${TMP})
143 endif
144
145 # Setup platform XLEN
146 ifndef PLATFORM_RISCV_XLEN
147   ifeq ($(OPENSBI_CC_XLEN), 32)
148     PLATFORM_RISCV_XLEN = 32
149   else
150     PLATFORM_RISCV_XLEN = 64
151   endif
152 endif
153
154 ifeq ($(CC_IS_CLANG),y)
155 ifeq ($(CROSS_COMPILE),)
156 CLANG_TARGET    =       --target=riscv$(PLATFORM_RISCV_XLEN)-unknown-elf
157 endif
158 endif
159
160 ifeq ($(LD_IS_LLD),y)
161 RELAX_FLAG      =       -mno-relax
162 USE_LD_FLAG     =       -fuse-ld=lld
163 else
164 USE_LD_FLAG     =       -fuse-ld=bfd
165 endif
166
167 # Check whether the linker supports creating PIEs
168 OPENSBI_LD_PIE := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) $(USE_LD_FLAG) -fPIE -nostdlib -Wl,-pie -x c /dev/null -o /dev/null >/dev/null 2>&1 && echo y || echo n)
169
170 # Check whether the compiler supports -m(no-)save-restore
171 CC_SUPPORT_SAVE_RESTORE := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) -nostdlib -mno-save-restore -x c /dev/null -o /dev/null 2>&1 | grep "\-save\-restore" >/dev/null && echo n || echo y)
172
173 # Check whether the assembler and the compiler support the Zicsr and Zifencei extensions
174 CC_SUPPORT_ZICSR_ZIFENCEI := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) -nostdlib -march=rv$(OPENSBI_CC_XLEN)imafd_zicsr_zifencei -x c /dev/null -o /dev/null 2>&1 | grep "zicsr\|zifencei" > /dev/null && echo n || echo y)
175
176 # Build Info:
177 # OPENSBI_BUILD_TIME_STAMP -- the compilation time stamp
178 # OPENSBI_BUILD_COMPILER_VERSION -- the compiler version info
179 BUILD_INFO ?= n
180 ifeq ($(BUILD_INFO),y)
181 OPENSBI_BUILD_DATE_FMT = +%Y-%m-%d %H:%M:%S %z
182 ifdef SOURCE_DATE_EPOCH
183         OPENSBI_BUILD_TIME_STAMP ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" \
184                 "$(OPENSBI_BUILD_DATE_FMT)" 2>/dev/null || \
185                 date -u -r "$(SOURCE_DATE_EPOCH)" \
186                 "$(OPENSBI_BUILD_DATE_FMT)" 2>/dev/null || \
187                 date -u "$(OPENSBI_BUILD_DATE_FMT)")
188 else
189         OPENSBI_BUILD_TIME_STAMP ?= $(shell date "$(OPENSBI_BUILD_DATE_FMT)")
190 endif
191 OPENSBI_BUILD_COMPILER_VERSION=$(shell $(CC) -v 2>&1 | grep ' version ' | \
192         sed 's/[[:space:]]*$$//')
193 endif
194
195 # Setup list of objects.mk files
196 ifdef PLATFORM
197 platform-object-mks=$(shell if [ -d $(platform_src_dir)/ ]; then find $(platform_src_dir) -iname "objects.mk" | sort -r; fi)
198 endif
199 libsbi-object-mks=$(shell if [ -d $(libsbi_dir) ]; then find $(libsbi_dir) -iname "objects.mk" | sort -r; fi)
200 libsbiutils-object-mks=$(shell if [ -d $(libsbiutils_dir) ]; then find $(libsbiutils_dir) -iname "objects.mk" | sort -r; fi)
201 firmware-object-mks=$(shell if [ -d $(firmware_dir) ]; then find $(firmware_dir) -iname "objects.mk" | sort -r; fi)
202
203 # The "make all" rule should always be first rule
204 .PHONY: all
205 all:
206
207 # Include platform specific .config
208 ifdef PLATFORM
209 .PHONY: menuconfig
210 menuconfig: $(platform_src_dir)/Kconfig $(src_dir)/Kconfig
211         $(CMD_PREFIX)mkdir -p $(KCONFIG_DIR)
212         $(CMD_PREFIX)$(src_dir)/scripts/Kconfiglib/menuconfig.py $(src_dir)/Kconfig
213         $(CMD_PREFIX)$(src_dir)/scripts/Kconfiglib/genconfig.py --header-path $(KCONFIG_AUTOHEADER) --sync-deps $(KCONFIG_DIR) --file-list $(KCONFIG_AUTOLIST) $(src_dir)/Kconfig
214
215 .PHONY: savedefconfig
216 savedefconfig: $(platform_src_dir)/Kconfig $(src_dir)/Kconfig
217         $(CMD_PREFIX)mkdir -p $(KCONFIG_DIR)
218         $(CMD_PREFIX)$(src_dir)/scripts/Kconfiglib/savedefconfig.py --kconfig $(src_dir)/Kconfig --out $(KCONFIG_DIR)/defconfig
219
220 $(KCONFIG_CONFIG): $(platform_src_dir)/configs/$(PLATFORM_DEFCONFIG) $(platform_src_dir)/Kconfig $(src_dir)/Kconfig
221         $(CMD_PREFIX)mkdir -p $(KCONFIG_DIR)
222         $(CMD_PREFIX)$(src_dir)/scripts/Kconfiglib/defconfig.py --kconfig $(src_dir)/Kconfig $(platform_src_dir)/configs/$(PLATFORM_DEFCONFIG)
223         $(CMD_PREFIX)$(src_dir)/scripts/Kconfiglib/genconfig.py --header-path $(KCONFIG_AUTOHEADER) --sync-deps $(KCONFIG_DIR) --file-list $(KCONFIG_AUTOLIST) $(src_dir)/Kconfig
224
225 $(KCONFIG_AUTOCMD): $(KCONFIG_CONFIG)
226         $(CMD_PREFIX)mkdir -p $(KCONFIG_DIR)
227         $(CMD_PREFIX)printf "%s: " $(KCONFIG_CONFIG) > $(KCONFIG_AUTOCMD)
228         $(CMD_PREFIX)cat $(KCONFIG_AUTOLIST) | tr '\n' ' ' >> $(KCONFIG_AUTOCMD)
229
230 include $(KCONFIG_CONFIG)
231 include $(KCONFIG_AUTOCMD)
232 endif
233
234 # Include all objects.mk files
235 ifdef PLATFORM
236 include $(platform-object-mks)
237 endif
238 include $(libsbi-object-mks)
239 include $(libsbiutils-object-mks)
240 include $(firmware-object-mks)
241
242 # Setup list of objects
243 libsbi-objs-path-y=$(foreach obj,$(libsbi-objs-y),$(build_dir)/lib/sbi/$(obj))
244 ifdef PLATFORM
245 libsbiutils-objs-path-y=$(foreach obj,$(libsbiutils-objs-y),$(platform_build_dir)/lib/utils/$(obj))
246 platform-objs-path-y=$(foreach obj,$(platform-objs-y),$(platform_build_dir)/$(obj))
247 firmware-bins-path-y=$(foreach bin,$(firmware-bins-y),$(platform_build_dir)/firmware/$(bin))
248 endif
249 firmware-elfs-path-y=$(firmware-bins-path-y:.bin=.elf)
250 firmware-objs-path-y=$(firmware-bins-path-y:.bin=.o)
251
252 # Setup list of deps files for objects
253 deps-y=$(platform-objs-path-y:.o=.dep)
254 deps-y+=$(libsbi-objs-path-y:.o=.dep)
255 deps-y+=$(libsbiutils-objs-path-y:.o=.dep)
256 deps-y+=$(firmware-objs-path-y:.o=.dep)
257 deps-y+=$(firmware-elfs-path-y:=.dep)
258
259 # Setup platform ABI, ISA and Code Model
260 ifndef PLATFORM_RISCV_ABI
261   ifneq ($(PLATFORM_RISCV_TOOLCHAIN_DEFAULT), 1)
262     ifeq ($(PLATFORM_RISCV_XLEN), 32)
263       PLATFORM_RISCV_ABI = ilp$(PLATFORM_RISCV_XLEN)
264     else
265       PLATFORM_RISCV_ABI = lp$(PLATFORM_RISCV_XLEN)
266     endif
267   else
268     PLATFORM_RISCV_ABI = $(OPENSBI_CC_ABI)
269   endif
270 endif
271 ifndef PLATFORM_RISCV_ISA
272   ifneq ($(PLATFORM_RISCV_TOOLCHAIN_DEFAULT), 1)
273     ifeq ($(CC_SUPPORT_ZICSR_ZIFENCEI), y)
274       PLATFORM_RISCV_ISA = rv$(PLATFORM_RISCV_XLEN)imafdc_zicsr_zifencei
275     else
276       PLATFORM_RISCV_ISA = rv$(PLATFORM_RISCV_XLEN)imafdc
277     endif
278   else
279     PLATFORM_RISCV_ISA = $(OPENSBI_CC_ISA)
280   endif
281 endif
282 ifndef PLATFORM_RISCV_CODE_MODEL
283   PLATFORM_RISCV_CODE_MODEL = medany
284 endif
285
286 # Setup install directories
287 ifdef INSTALL_INCLUDE_PATH
288         install_include_path=$(INSTALL_INCLUDE_PATH)
289 else
290         install_include_path=include
291 endif
292 ifdef INSTALL_LIB_PATH
293         install_lib_path=$(INSTALL_LIB_PATH)
294 else
295         ifneq ($(origin INSTALL_LIB_SUBDIR), undefined)
296                 install_lib_subdir=$(INSTALL_LIB_SUBDIR)
297         else
298                 install_lib_subdir=$(PLATFORM_RISCV_ABI)
299         endif
300         install_lib_path=lib$(subst 32,,$(PLATFORM_RISCV_XLEN))/$(install_lib_subdir)
301 endif
302 ifdef INSTALL_FIRMWARE_PATH
303         install_firmware_path=$(INSTALL_FIRMWARE_PATH)
304 else
305         install_firmware_path=share/opensbi/$(PLATFORM_RISCV_ABI)
306 endif
307 ifdef INSTALL_DOCS_PATH
308         install_docs_path=$(INSTALL_DOCS_PATH)
309 else
310         install_docs_path=share/opensbi/docs
311 endif
312
313 # Setup compilation commands flags
314 ifeq ($(CC_IS_CLANG),y)
315 GENFLAGS        +=      $(CLANG_TARGET)
316 GENFLAGS        +=      -Wno-unused-command-line-argument
317 endif
318 GENFLAGS        +=      -I$(platform_src_dir)/include
319 GENFLAGS        +=      -I$(include_dir)
320 ifneq ($(OPENSBI_VERSION_GIT),)
321 GENFLAGS        +=      -DOPENSBI_VERSION_GIT="\"$(OPENSBI_VERSION_GIT)\""
322 endif
323 ifeq ($(BUILD_INFO),y)
324 GENFLAGS        +=      -DOPENSBI_BUILD_TIME_STAMP="\"$(OPENSBI_BUILD_TIME_STAMP)\""
325 GENFLAGS        +=      -DOPENSBI_BUILD_COMPILER_VERSION="\"$(OPENSBI_BUILD_COMPILER_VERSION)\""
326 endif
327 ifdef PLATFORM
328 GENFLAGS        +=      -include $(KCONFIG_AUTOHEADER)
329 endif
330 GENFLAGS        +=      $(libsbiutils-genflags-y)
331 GENFLAGS        +=      $(platform-genflags-y)
332 GENFLAGS        +=      $(firmware-genflags-y)
333
334 CFLAGS          =       -g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing
335 ifneq ($(DEBUG),)
336 CFLAGS          +=      -O0
337 else
338 CFLAGS          +=      -O2
339 endif
340 CFLAGS          +=      -fno-omit-frame-pointer -fno-optimize-sibling-calls -mstrict-align
341 # enable -m(no-)save-restore option by CC_SUPPORT_SAVE_RESTORE
342 ifeq ($(CC_SUPPORT_SAVE_RESTORE),y)
343 CFLAGS          +=      -mno-save-restore
344 endif
345 CFLAGS          +=      -mabi=$(PLATFORM_RISCV_ABI) -march=$(PLATFORM_RISCV_ISA)
346 CFLAGS          +=      -mcmodel=$(PLATFORM_RISCV_CODE_MODEL)
347 CFLAGS          +=      $(RELAX_FLAG)
348 CFLAGS          +=      $(GENFLAGS)
349 CFLAGS          +=      $(platform-cflags-y)
350 CFLAGS          +=      -fno-pie -no-pie
351 CFLAGS          +=      $(firmware-cflags-y)
352
353 CPPFLAGS        +=      $(GENFLAGS)
354 CPPFLAGS        +=      $(platform-cppflags-y)
355 CPPFLAGS        +=      $(firmware-cppflags-y)
356
357 ASFLAGS         =       -g -Wall -nostdlib
358 ASFLAGS         +=      -fno-omit-frame-pointer -fno-optimize-sibling-calls -mstrict-align
359 # enable -m(no-)save-restore option by CC_SUPPORT_SAVE_RESTORE
360 ifeq ($(CC_SUPPORT_SAVE_RESTORE),y)
361 ASFLAGS         +=      -mno-save-restore
362 endif
363 ASFLAGS         +=      -mabi=$(PLATFORM_RISCV_ABI) -march=$(PLATFORM_RISCV_ISA)
364 ASFLAGS         +=      -mcmodel=$(PLATFORM_RISCV_CODE_MODEL)
365 ASFLAGS         +=      $(RELAX_FLAG)
366 ifneq ($(CC_IS_CLANG),y)
367 ifneq ($(RELAX_FLAG),)
368 ASFLAGS         +=      -Wa,$(RELAX_FLAG)
369 endif
370 endif
371 ASFLAGS         +=      $(GENFLAGS)
372 ASFLAGS         +=      $(platform-asflags-y)
373 ASFLAGS         +=      $(firmware-asflags-y)
374
375 ARFLAGS         =       rcs
376
377 ELFFLAGS        +=      $(USE_LD_FLAG)
378 ELFFLAGS        +=      -Wl,--build-id=none
379 ELFFLAGS        +=      $(platform-ldflags-y)
380 ELFFLAGS        +=      $(firmware-ldflags-y)
381
382 MERGEFLAGS      +=      -r
383 ifeq ($(LD_IS_LLD),y)
384 MERGEFLAGS      +=      -b elf
385 else
386 MERGEFLAGS      +=      -b elf$(PLATFORM_RISCV_XLEN)-littleriscv
387 endif
388 MERGEFLAGS      +=      -m elf$(PLATFORM_RISCV_XLEN)lriscv
389
390 DTSCPPFLAGS     =       $(CPPFLAGS) -nostdinc -nostdlib -fno-builtin -D__DTS__ -x assembler-with-cpp
391
392 # Setup functions for compilation
393 define dynamic_flags
394 -I$(shell dirname $(2)) -D__OBJNAME__=$(subst -,_,$(shell basename $(1) .o))
395 endef
396 merge_objs = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
397              echo " MERGE     $(subst $(build_dir)/,,$(1))"; \
398              $(LD) $(MERGEFLAGS) $(2) -o $(1)
399 merge_deps = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
400              echo " MERGE-DEP $(subst $(build_dir)/,,$(1))"; \
401              cat $(2) > $(1)
402 copy_file =  $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
403              echo " COPY      $(subst $(build_dir)/,,$(1))"; \
404              cp -L -f $(2) $(1)
405 inst_file =  $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
406              echo " INSTALL   $(subst $(install_root_dir)/,,$(1))"; \
407              cp -L -f $(2) $(1)
408 inst_file_list = $(CMD_PREFIX)if [ ! -z "$(4)" ]; then \
409              mkdir -p $(1)/$(3); \
410              for file in $(4) ; do \
411              rel_file=`echo $$file | sed -e 's@$(2)/$(subst $(install_firmware_path),platform,$(3))@@'`; \
412              dest_file=$(1)"/"$(3)"/"`echo $$rel_file`; \
413              dest_dir=`dirname $$dest_file`; \
414              echo " INSTALL   "$(3)"/"`echo $$rel_file`; \
415              mkdir -p $$dest_dir; \
416              cp -L -f $$file $$dest_file; \
417              done \
418              fi
419 inst_header_dir =  $(CMD_PREFIX)mkdir -p $(1); \
420              echo " INSTALL   $(subst $(install_root_dir)/,,$(1))"; \
421              cp -L -rf $(2) $(1)
422 compile_cpp_dep = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
423              echo " CPP-DEP   $(subst $(build_dir)/,,$(1))"; \
424              printf %s `dirname $(1)`/  > $(1) && \
425              $(CC) $(CPPFLAGS) -x c -MM $(3) \
426                -MT `basename $(1:.dep=$(2))` >> $(1) || rm -f $(1)
427 compile_cpp = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
428              echo " CPP       $(subst $(build_dir)/,,$(1))"; \
429              $(CPP) $(CPPFLAGS) -x c $(2) | grep -v "\#" > $(1)
430 compile_cc_dep = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
431              echo " CC-DEP    $(subst $(build_dir)/,,$(1))"; \
432              printf %s `dirname $(1)`/  > $(1) && \
433              $(CC) $(CFLAGS) $(call dynamic_flags,$(1),$(2))   \
434                -MM $(2) >> $(1) || rm -f $(1)
435 compile_cc = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
436              echo " CC        $(subst $(build_dir)/,,$(1))"; \
437              $(CC) $(CFLAGS) $(call dynamic_flags,$(1),$(2)) -c $(2) -o $(1)
438 compile_as_dep = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
439              echo " AS-DEP    $(subst $(build_dir)/,,$(1))"; \
440              printf %s `dirname $(1)`/ > $(1) && \
441              $(AS) $(ASFLAGS) $(call dynamic_flags,$(1),$(2)) \
442                -MM $(2) >> $(1) || rm -f $(1)
443 compile_as = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
444              echo " AS        $(subst $(build_dir)/,,$(1))"; \
445              $(AS) $(ASFLAGS) $(call dynamic_flags,$(1),$(2)) -c $(2) -o $(1)
446 compile_elf = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
447              echo " ELF       $(subst $(build_dir)/,,$(1))"; \
448              $(CC) $(CFLAGS) $(3) $(ELFFLAGS) -Wl,-T$(2) -o $(1)
449 compile_ar = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
450              echo " AR        $(subst $(build_dir)/,,$(1))"; \
451              $(AR) $(ARFLAGS) $(1) $(2)
452 compile_objcopy = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
453              echo " OBJCOPY   $(subst $(build_dir)/,,$(1))"; \
454              $(OBJCOPY) -S -O binary $(2) $(1)
455 compile_dts = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
456              echo " DTC       $(subst $(build_dir)/,,$(1))"; \
457              $(CPP) $(DTSCPPFLAGS) $(2) | $(DTC) -O dtb -i `dirname $(2)` -o $(1)
458 compile_d2c = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
459              echo " D2C       $(subst $(build_dir)/,,$(1))"; \
460              $(if $($(2)-varalign-$(3)),$(eval D2C_ALIGN_BYTES := $($(2)-varalign-$(3))),$(eval D2C_ALIGN_BYTES := $(4))) \
461              $(if $($(2)-varprefix-$(3)),$(eval D2C_NAME_PREFIX := $($(2)-varprefix-$(3))),$(eval D2C_NAME_PREFIX := $(5))) \
462              $(if $($(2)-padding-$(3)),$(eval D2C_PADDING_BYTES := $($(2)-padding-$(3))),$(eval D2C_PADDING_BYTES := 0)) \
463              $(src_dir)/scripts/d2c.sh -i $(6) -a $(D2C_ALIGN_BYTES) -p $(D2C_NAME_PREFIX) -t $(D2C_PADDING_BYTES) > $(1)
464 compile_carray = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
465              echo " CARRAY    $(subst $(build_dir)/,,$(1))"; \
466              $(eval CARRAY_VAR_LIST := $(carray-$(subst .c,,$(shell basename $(1)))-y)) \
467              $(src_dir)/scripts/carray.sh -i $(2) -l "$(CARRAY_VAR_LIST)" > $(1)
468 compile_gen_dep = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
469              echo " GEN-DEP   $(subst $(build_dir)/,,$(1))"; \
470              echo "$(1:.dep=$(2)): $(3)" >> $(1)
471
472 targets-y  = $(build_dir)/lib/libsbi.a
473 ifdef PLATFORM
474 targets-y += $(platform_build_dir)/lib/libplatsbi.a
475 endif
476 targets-y += $(firmware-bins-path-y)
477
478 # The default "make all" rule
479 .PHONY: all
480 all: $(targets-y)
481
482 # Preserve all intermediate files
483 .SECONDARY:
484
485 # Rules for lib/sbi sources
486 $(build_dir)/lib/libsbi.a: $(libsbi-objs-path-y)
487         $(call compile_ar,$@,$^)
488
489 $(platform_build_dir)/lib/libplatsbi.a: $(libsbi-objs-path-y) $(libsbiutils-objs-path-y) $(platform-objs-path-y)
490         $(call compile_ar,$@,$^)
491
492 $(build_dir)/%.dep: $(src_dir)/%.carray $(KCONFIG_CONFIG)
493         $(call compile_gen_dep,$@,.c,$< $(KCONFIG_CONFIG))
494         $(call compile_gen_dep,$@,.o,$(@:.dep=.c))
495
496 $(build_dir)/%.c: $(src_dir)/%.carray
497         $(call compile_carray,$@,$<)
498
499 $(build_dir)/%.dep: $(src_dir)/%.c $(KCONFIG_CONFIG)
500         $(call compile_cc_dep,$@,$<)
501
502 $(build_dir)/%.o: $(src_dir)/%.c
503         $(call compile_cc,$@,$<)
504
505 $(build_dir)/%.o: $(build_dir)/%.c
506         $(call compile_cc,$@,$<)
507
508 ifeq ($(BUILD_INFO),y)
509 $(build_dir)/lib/sbi/sbi_init.o: $(libsbi_dir)/sbi_init.c FORCE
510         $(call compile_cc,$@,$<)
511 endif
512
513 $(build_dir)/%.dep: $(src_dir)/%.S $(KCONFIG_CONFIG)
514         $(call compile_as_dep,$@,$<)
515
516 $(build_dir)/%.o: $(src_dir)/%.S
517         $(call compile_as,$@,$<)
518
519 # Rules for platform sources
520 $(platform_build_dir)/%.dep: $(platform_src_dir)/%.carray $(KCONFIG_CONFIG)
521         $(call compile_gen_dep,$@,.c,$< $(KCONFIG_CONFIG))
522         $(call compile_gen_dep,$@,.o,$(@:.dep=.c))
523
524 $(platform_build_dir)/%.c: $(platform_src_dir)/%.carray
525         $(call compile_carray,$@,$<)
526
527 $(platform_build_dir)/%.dep: $(platform_src_dir)/%.c $(KCONFIG_CONFIG)
528         $(call compile_cc_dep,$@,$<)
529
530 $(platform_build_dir)/%.o: $(platform_src_dir)/%.c $(KCONFIG_CONFIG)
531         $(call compile_cc,$@,$<)
532
533 $(platform_build_dir)/%.dep: $(platform_src_dir)/%.S
534         $(call compile_as_dep,$@,$<)
535
536 $(platform_build_dir)/%.o: $(platform_src_dir)/%.S
537         $(call compile_as,$@,$<)
538
539 $(platform_build_dir)/%.dep: $(platform_src_dir)/%.dts $(KCONFIG_CONFIG)
540         $(call compile_gen_dep,$@,.dtb,$< $(KCONFIG_CONFIG))
541         $(call compile_gen_dep,$@,.c,$(@:.dep=.dtb))
542         $(call compile_gen_dep,$@,.o,$(@:.dep=.c))
543
544 $(platform_build_dir)/%.c: $(platform_build_dir)/%.dtb
545         $(call compile_d2c,$@,platform,$(subst .dtb,.o,$(subst /,-,$(subst $(platform_build_dir)/,,$<))),16,dt,$<)
546
547 $(platform_build_dir)/%.dtb: $(platform_src_dir)/%.dts
548         $(call compile_dts,$@,$<)
549
550 # Rules for lib/utils and firmware sources
551 $(platform_build_dir)/%.bin: $(platform_build_dir)/%.elf
552         $(call compile_objcopy,$@,$<)
553
554 $(platform_build_dir)/%.elf: $(platform_build_dir)/%.o $(platform_build_dir)/%.elf.ld $(platform_build_dir)/lib/libplatsbi.a
555         $(call compile_elf,$@,$@.ld,$< $(platform_build_dir)/lib/libplatsbi.a)
556
557 $(platform_build_dir)/%.dep: $(src_dir)/%.ldS $(KCONFIG_CONFIG)
558         $(call compile_cpp_dep,$@,.ld,$<)
559
560 $(platform_build_dir)/%.ld: $(src_dir)/%.ldS
561         $(call compile_cpp,$@,$<)
562
563 $(platform_build_dir)/%.dep: $(src_dir)/%.carray $(KCONFIG_CONFIG)
564         $(call compile_gen_dep,$@,.c,$< $(KCONFIG_CONFIG))
565         $(call compile_gen_dep,$@,.o,$(@:.dep=.c))
566
567 $(platform_build_dir)/%.c: $(src_dir)/%.carray
568         $(call compile_carray,$@,$<)
569
570 $(platform_build_dir)/%.dep: $(src_dir)/%.c $(KCONFIG_CONFIG)
571         $(call compile_cc_dep,$@,$<)
572
573 $(platform_build_dir)/%.o: $(src_dir)/%.c
574         $(call compile_cc,$@,$<)
575
576 $(platform_build_dir)/%.dep: $(src_dir)/%.S $(KCONFIG_CONFIG)
577         $(call compile_as_dep,$@,$<)
578
579 $(platform_build_dir)/%.o: $(src_dir)/%.S
580         $(call compile_as,$@,$<)
581
582 # Rule for "make docs"
583 $(build_dir)/docs/latex/refman.pdf: $(build_dir)/docs/latex/refman.tex
584         $(CMD_PREFIX)mkdir -p $(build_dir)/docs
585         $(CMD_PREFIX)$(MAKE) -C $(build_dir)/docs/latex
586 $(build_dir)/docs/latex/refman.tex: $(build_dir)/docs/doxygen.cfg
587         $(CMD_PREFIX)mkdir -p $(build_dir)/docs
588         $(CMD_PREFIX)doxygen $(build_dir)/docs/doxygen.cfg
589 $(build_dir)/docs/doxygen.cfg: $(src_dir)/docs/doxygen.cfg
590         $(CMD_PREFIX)mkdir -p $(build_dir)/docs
591         $(CMD_PREFIX)cat docs/doxygen.cfg | sed -e "s#@@SRC_DIR@@#$(src_dir)#" -e "s#@@BUILD_DIR@@#$(build_dir)#" -e "s#@@OPENSBI_MAJOR@@#$(OPENSBI_VERSION_MAJOR)#" -e "s#@@OPENSBI_MINOR@@#$(OPENSBI_VERSION_MINOR)#" > $(build_dir)/docs/doxygen.cfg
592 .PHONY: docs
593 docs: $(build_dir)/docs/latex/refman.pdf
594
595 # Dependency files should only be included after default Makefile rules
596 # They should not be included for any "xxxconfig" or "xxxclean" rule
597 all-deps-1 = $(if $(findstring config,$(MAKECMDGOALS)),,$(deps-y))
598 all-deps-2 = $(if $(findstring clean,$(MAKECMDGOALS)),,$(all-deps-1))
599 -include $(all-deps-2)
600
601 # Include external dependency of firmwares after default Makefile rules
602 include $(src_dir)/firmware/external_deps.mk
603
604 # Convenient "make run" command for emulated platforms
605 .PHONY: run
606 run: all
607 ifneq ($(platform-runcmd),)
608         $(platform-runcmd) $(RUN_ARGS)
609 else
610 ifdef PLATFORM
611         @echo "Platform $(PLATFORM) doesn't specify a run command"
612         @false
613 else
614         @echo Run command only available when targeting a platform
615         @false
616 endif
617 endif
618
619 install_targets-y  = install_libsbi
620 ifdef PLATFORM
621 install_targets-y += install_libplatsbi
622 install_targets-y += install_firmwares
623 endif
624
625 # Rule for "make install"
626 .PHONY: install
627 install: $(install_targets-y)
628
629 .PHONY: install_libsbi
630 install_libsbi: $(build_dir)/lib/libsbi.a
631         $(call inst_header_dir,$(install_root_dir)/$(install_include_path),$(include_dir)/sbi)
632         $(call inst_file,$(install_root_dir)/$(install_lib_path)/libsbi.a,$(build_dir)/lib/libsbi.a)
633
634 .PHONY: install_libplatsbi
635 install_libplatsbi: $(platform_build_dir)/lib/libplatsbi.a $(build_dir)/lib/libsbi.a
636         $(call inst_file,$(install_root_dir)/$(install_lib_path)/opensbi/$(platform_subdir)/lib/libplatsbi.a,$(platform_build_dir)/lib/libplatsbi.a)
637
638 .PHONY: install_firmwares
639 install_firmwares: $(platform_build_dir)/lib/libplatsbi.a $(build_dir)/lib/libsbi.a $(firmware-bins-path-y)
640         $(call inst_file_list,$(install_root_dir),$(build_dir),$(install_firmware_path)/$(platform_subdir)/firmware,$(firmware-elfs-path-y))
641         $(call inst_file_list,$(install_root_dir),$(build_dir),$(install_firmware_path)/$(platform_subdir)/firmware,$(firmware-bins-path-y))
642
643 .PHONY: install_docs
644 install_docs: $(build_dir)/docs/latex/refman.pdf
645         $(call inst_file,$(install_root_dir)/$(install_docs_path)/refman.pdf,$(build_dir)/docs/latex/refman.pdf)
646
647 .PHONY: cscope
648 cscope:
649         $(CMD_PREFIX)find \
650                 "$(src_dir)/firmware" \
651                 "$(src_dir)/include" \
652                 "$(src_dir)/lib" \
653                 "$(platform_src_dir)" \
654         -name "*.[chS]" -print > cscope.files
655         $(CMD_PREFIX)echo "$(KCONFIG_AUTOHEADER)" >> cscope.files
656         $(CMD_PREFIX)cscope -bkq -i cscope.files -f cscope.out
657
658 # Rule for "make clean"
659 .PHONY: clean
660 clean:
661         $(CMD_PREFIX)mkdir -p $(build_dir)
662         $(if $(V), @echo " RM        $(build_dir)/*.o")
663         $(CMD_PREFIX)find $(build_dir) -type f -name "*.o" -exec rm -rf {} +
664         $(if $(V), @echo " RM        $(build_dir)/*.a")
665         $(CMD_PREFIX)find $(build_dir) -type f -name "*.a" -exec rm -rf {} +
666         $(if $(V), @echo " RM        $(build_dir)/*.elf")
667         $(CMD_PREFIX)find $(build_dir) -type f -name "*.elf" -exec rm -rf {} +
668         $(if $(V), @echo " RM        $(build_dir)/*.bin")
669         $(CMD_PREFIX)find $(build_dir) -type f -name "*.bin" -exec rm -rf {} +
670         $(if $(V), @echo " RM        $(build_dir)/*.dtb")
671         $(CMD_PREFIX)find $(build_dir) -type f -name "*.dtb" -exec rm -rf {} +
672
673 # Rule for "make distclean"
674 .PHONY: distclean
675 distclean: clean
676         $(CMD_PREFIX)mkdir -p $(build_dir)
677         $(if $(V), @echo " RM        $(build_dir)/*.dep")
678         $(CMD_PREFIX)find $(build_dir) -type f -name "*.dep" -exec rm -rf {} +
679 ifeq ($(build_dir),$(CURDIR)/build)
680         $(if $(V), @echo " RM        $(build_dir)")
681         $(CMD_PREFIX)rm -rf $(build_dir)
682 endif
683 ifeq ($(install_root_dir),$(install_root_dir_default)/usr)
684         $(if $(V), @echo " RM        $(install_root_dir_default)")
685         $(CMD_PREFIX)rm -rf $(install_root_dir_default)
686 endif
687         $(if $(V), @echo " RM        $(src_dir)/cscope*")
688         $(CMD_PREFIX)rm -f $(src_dir)/cscope*
689
690 .PHONY: FORCE
691 FORCE: