Makefile 21 KB

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