Makefile 24 KB

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