Makefile 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. # Find out source, build, and install directories
  14. src_dir=$(CURDIR)
  15. ifdef O
  16. build_dir=$(shell readlink -f $(O))
  17. else
  18. build_dir=$(CURDIR)/build
  19. endif
  20. ifeq ($(build_dir),$(CURDIR))
  21. $(error Build directory is same as source directory.)
  22. endif
  23. ifdef I
  24. install_dir=$(shell readlink -f $(I))
  25. else
  26. install_dir=$(CURDIR)/install
  27. endif
  28. ifeq ($(install_dir),$(CURDIR))
  29. $(error Install directory is same as source directory.)
  30. endif
  31. ifeq ($(install_dir),$(build_dir))
  32. $(error Install directory is same as build directory.)
  33. endif
  34. ifdef PLATFORM_DIR
  35. platform_dir_path=$(shell readlink -f $(PLATFORM_DIR))
  36. ifdef PLATFORM
  37. platform_parent_dir=$(platform_dir_path)
  38. else
  39. PLATFORM=$(shell basename $(platform_dir_path))
  40. platform_parent_dir=$(subst $(PLATFORM),,$(platform_dir_path))
  41. endif
  42. else
  43. platform_parent_dir=$(src_dir)/platform
  44. endif
  45. # Check if verbosity is ON for build process
  46. CMD_PREFIX_DEFAULT := @
  47. ifeq ($(V), 1)
  48. CMD_PREFIX :=
  49. else
  50. CMD_PREFIX := $(CMD_PREFIX_DEFAULT)
  51. endif
  52. # Setup path of directories
  53. export platform_subdir=$(PLATFORM)
  54. export platform_src_dir=$(platform_parent_dir)/$(platform_subdir)
  55. export platform_build_dir=$(build_dir)/platform/$(platform_subdir)
  56. export platform_common_src_dir=$(src_dir)/platform/common
  57. export include_dir=$(CURDIR)/include
  58. export lib_dir=$(CURDIR)/lib
  59. export firmware_dir=$(CURDIR)/firmware
  60. # Find library version
  61. OPENSBI_VERSION_MAJOR=`grep MAJOR $(include_dir)/sbi/sbi_version.h | sed 's/.*MAJOR.*\([0-9][0-9]*\)/\1/'`
  62. OPENSBI_VERSION_MINOR=`grep MINOR $(include_dir)/sbi/sbi_version.h | sed 's/.*MINOR.*\([0-9][0-9]*\)/\1/'`
  63. # Setup compilation commands
  64. ifdef CROSS_COMPILE
  65. CC = $(CROSS_COMPILE)gcc
  66. CPP = $(CROSS_COMPILE)cpp
  67. AR = $(CROSS_COMPILE)ar
  68. LD = $(CROSS_COMPILE)ld
  69. OBJCOPY = $(CROSS_COMPILE)objcopy
  70. else
  71. CC ?= gcc
  72. CPP ?= cpp
  73. AR ?= ar
  74. LD ?= ld
  75. OBJCOPY ?= objcopy
  76. endif
  77. AS = $(CC)
  78. DTC = dtc
  79. # Guess the compillers xlen
  80. OPENSBI_CC_XLEN := $(shell TMP=`$(CC) -dumpmachine | sed 's/riscv\([0-9][0-9]\).*/\1/'`; echo $${TMP})
  81. # Setup platform XLEN
  82. ifndef PLATFORM_RISCV_XLEN
  83. ifeq ($(OPENSBI_CC_XLEN), 32)
  84. PLATFORM_RISCV_XLEN = 32
  85. else
  86. PLATFORM_RISCV_XLEN = 64
  87. endif
  88. endif
  89. # Setup list of objects.mk files
  90. ifdef PLATFORM
  91. platform-object-mks=$(shell if [ -d $(platform_src_dir)/ ]; then find $(platform_src_dir) -iname "objects.mk" | sort -r; fi)
  92. platform-common-object-mks=$(shell if [ -d $(platform_common_src_dir) ]; then find $(platform_common_src_dir) -iname "objects.mk" | sort -r; fi)
  93. endif
  94. lib-object-mks=$(shell if [ -d $(lib_dir) ]; then find $(lib_dir) -iname "objects.mk" | sort -r; fi)
  95. firmware-object-mks=$(shell if [ -d $(firmware_dir) ]; then find $(firmware_dir) -iname "objects.mk" | sort -r; fi)
  96. # Include platform specifig config.mk
  97. ifdef PLATFORM
  98. include $(platform_src_dir)/config.mk
  99. endif
  100. # Include all object.mk files
  101. ifdef PLATFORM
  102. include $(platform-object-mks)
  103. include $(platform-common-object-mks)
  104. endif
  105. include $(lib-object-mks)
  106. include $(firmware-object-mks)
  107. # Setup list of objects
  108. lib-objs-path-y=$(foreach obj,$(lib-objs-y),$(build_dir)/lib/$(obj))
  109. ifdef PLATFORM
  110. platform-objs-path-y=$(foreach obj,$(platform-objs-y),$(platform_build_dir)/$(obj))
  111. platform-dtb-path-y=$(foreach obj,$(platform-dtb-y),$(platform_build_dir)/$(obj))
  112. platform-common-objs-path-y=$(foreach obj,$(platform-common-objs-y),$(build_dir)/platform/common/$(obj))
  113. firmware-bins-path-y=$(foreach bin,$(firmware-bins-y),$(platform_build_dir)/firmware/$(bin))
  114. endif
  115. firmware-elfs-path-y=$(firmware-bins-path-y:.bin=.elf)
  116. firmware-objs-path-y=$(firmware-bins-path-y:.bin=.o)
  117. # Setup list of deps files for objects
  118. deps-y=$(platform-objs-path-y:.o=.dep)
  119. deps-y+=$(platform-common-objs-path-y:.o=.dep)
  120. deps-y+=$(lib-objs-path-y:.o=.dep)
  121. deps-y+=$(firmware-objs-path-y:.o=.dep)
  122. # Setup platform ABI, ISA and Code Model
  123. ifndef PLATFORM_RISCV_ABI
  124. ifeq ($(PLATFORM_RISCV_XLEN), 32)
  125. PLATFORM_RISCV_ABI = ilp$(PLATFORM_RISCV_XLEN)
  126. else
  127. PLATFORM_RISCV_ABI = lp$(PLATFORM_RISCV_XLEN)
  128. endif
  129. endif
  130. ifndef PLATFORM_RISCV_ISA
  131. PLATFORM_RISCV_ISA = rv$(PLATFORM_RISCV_XLEN)imafdc
  132. endif
  133. ifndef PLATFORM_RISCV_CODE_MODEL
  134. PLATFORM_RISCV_CODE_MODEL = medany
  135. endif
  136. # Setup compilation commands flags
  137. GENFLAGS = -I$(platform_src_dir)/include
  138. GENFLAGS += -I$(platform_common_src_dir)/include
  139. GENFLAGS += -I$(include_dir)
  140. GENFLAGS += $(platform-common-genflags-y)
  141. GENFLAGS += $(platform-genflags-y)
  142. GENFLAGS += $(firmware-genflags-y)
  143. CFLAGS = -g -Wall -Werror -nostdlib -fno-strict-aliasing -O2
  144. CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls
  145. CFLAGS += -mno-save-restore -mstrict-align
  146. CFLAGS += -mabi=$(PLATFORM_RISCV_ABI) -march=$(PLATFORM_RISCV_ISA)
  147. CFLAGS += -mcmodel=$(PLATFORM_RISCV_CODE_MODEL)
  148. CFLAGS += $(GENFLAGS)
  149. CFLAGS += $(platform-cflags-y)
  150. CFLAGS += $(firmware-cflags-y)
  151. CPPFLAGS += $(GENFLAGS)
  152. CPPFLAGS += $(platform-cppflags-y)
  153. CPPFLAGS += $(firmware-cppflags-y)
  154. ASFLAGS = -g -Wall -nostdlib -D__ASSEMBLY__
  155. ASFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls
  156. ASFLAGS += -mno-save-restore -mstrict-align
  157. ASFLAGS += -mabi=$(PLATFORM_RISCV_ABI) -march=$(PLATFORM_RISCV_ISA)
  158. ASFLAGS += -mcmodel=$(PLATFORM_RISCV_CODE_MODEL)
  159. ASFLAGS += $(GENFLAGS)
  160. ASFLAGS += $(platform-asflags-y)
  161. ASFLAGS += $(firmware-asflags-y)
  162. ARFLAGS = rcs
  163. ELFFLAGS += -Wl,--build-id=none -N -static-libgcc -lgcc
  164. ELFFLAGS += $(platform-ldflags-y)
  165. ELFFLAGS += $(firmware-ldflags-y)
  166. MERGEFLAGS += -r
  167. MERGEFLAGS += -b elf$(PLATFORM_RISCV_XLEN)-littleriscv
  168. MERGEFLAGS += -m elf$(PLATFORM_RISCV_XLEN)lriscv
  169. DTCFLAGS = -O dtb
  170. # Setup functions for compilation
  171. define dynamic_flags
  172. -I$(shell dirname $(2)) -D__OBJNAME__=$(subst -,_,$(shell basename $(1) .o))
  173. endef
  174. merge_objs = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
  175. echo " MERGE $(subst $(build_dir)/,,$(1))"; \
  176. $(LD) $(MERGEFLAGS) $(2) -o $(1)
  177. merge_deps = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
  178. echo " MERGE-DEP $(subst $(build_dir)/,,$(1))"; \
  179. cat $(2) > $(1)
  180. copy_file = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
  181. echo " COPY $(subst $(build_dir)/,,$(1))"; \
  182. cp -f $(2) $(1)
  183. inst_file = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
  184. echo " INSTALL $(subst $(install_dir)/,,$(1))"; \
  185. cp -f $(2) $(1)
  186. inst_file_list = $(CMD_PREFIX)if [ ! -z "$(4)" ]; then \
  187. mkdir -p $(1)/$(3); \
  188. for file in $(4) ; do \
  189. rel_file=`echo $$file | sed -e 's@$(2)/$(3)/@@'`; \
  190. dest_file=$(1)"/"$(3)"/"`echo $$rel_file`; \
  191. dest_dir=`dirname $$dest_file`; \
  192. echo " INSTALL "$(3)"/"`echo $$rel_file`; \
  193. mkdir -p $$dest_dir; \
  194. cp -f $$file $$dest_file; \
  195. done \
  196. fi
  197. inst_header_dir = $(CMD_PREFIX)mkdir -p $(1); \
  198. echo " INSTALL $(subst $(install_dir)/,,$(1))"; \
  199. cp -rf $(2) $(1)
  200. compile_cpp = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
  201. echo " CPP $(subst $(build_dir)/,,$(1))"; \
  202. $(CPP) $(CPPFLAGS) -x c $(2) | grep -v "\#" > $(1)
  203. compile_cc_dep = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
  204. echo " CC-DEP $(subst $(build_dir)/,,$(1))"; \
  205. printf %s `dirname $(1)`/ > $(1) && \
  206. $(CC) $(CFLAGS) $(call dynamic_flags,$(1),$(2)) \
  207. -MM $(2) >> $(1) || rm -f $(1)
  208. compile_cc = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
  209. echo " CC $(subst $(build_dir)/,,$(1))"; \
  210. $(CC) $(CFLAGS) $(call dynamic_flags,$(1),$(2)) -c $(2) -o $(1)
  211. compile_as_dep = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
  212. echo " AS-DEP $(subst $(build_dir)/,,$(1))"; \
  213. printf %s `dirname $(1)`/ > $(1) && \
  214. $(AS) $(ASFLAGS) $(call dynamic_flags,$(1),$(2)) \
  215. -MM $(2) >> $(1) || rm -f $(1)
  216. compile_as = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
  217. echo " AS $(subst $(build_dir)/,,$(1))"; \
  218. $(AS) $(ASFLAGS) $(call dynamic_flags,$(1),$(2)) -c $(2) -o $(1)
  219. compile_elf = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
  220. echo " ELF $(subst $(build_dir)/,,$(1))"; \
  221. $(CC) $(CFLAGS) $(3) $(ELFFLAGS) -Wl,-T$(2) -o $(1)
  222. compile_ar = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
  223. echo " AR $(subst $(build_dir)/,,$(1))"; \
  224. $(AR) $(ARFLAGS) $(1) $(2)
  225. compile_objcopy = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
  226. echo " OBJCOPY $(subst $(build_dir)/,,$(1))"; \
  227. $(OBJCOPY) -S -O binary $(2) $(1)
  228. compile_dts = $(CMD_PREFIX)mkdir -p `dirname $(1)`; \
  229. echo " DTC $(subst $(build_dir)/,,$(1))"; \
  230. $(DTC) $(DTCFLAGS) -o $(1) $(2)
  231. targets-y = $(build_dir)/lib/libsbi.a
  232. ifdef PLATFORM
  233. targets-y += $(platform_build_dir)/lib/libplatsbi.a
  234. targets-y += $(platform-dtb-path-y)
  235. endif
  236. targets-y += $(firmware-bins-path-y)
  237. # Default rule "make" should always be first rule
  238. .PHONY: all
  239. all: $(targets-y)
  240. # Preserve all intermediate files
  241. .SECONDARY:
  242. $(build_dir)/%.bin: $(build_dir)/%.elf
  243. $(call compile_objcopy,$@,$<)
  244. $(build_dir)/%.elf: $(build_dir)/%.o $(build_dir)/%.elf.ld $(platform_build_dir)/lib/libplatsbi.a
  245. $(call compile_elf,$@,$@.ld,$< $(platform_build_dir)/lib/libplatsbi.a)
  246. $(platform_build_dir)/%.ld: $(src_dir)/%.ldS
  247. $(call compile_cpp,$@,$<)
  248. $(build_dir)/lib/libsbi.a: $(lib-objs-path-y)
  249. $(call compile_ar,$@,$^)
  250. $(platform_build_dir)/lib/libplatsbi.a: $(lib-objs-path-y) $(platform-common-objs-path-y) $(platform-objs-path-y)
  251. $(call compile_ar,$@,$^)
  252. $(build_dir)/%.dep: $(src_dir)/%.c
  253. $(call compile_cc_dep,$@,$<)
  254. $(build_dir)/%.o: $(src_dir)/%.c
  255. $(call compile_cc,$@,$<)
  256. $(build_dir)/%.dep: $(src_dir)/%.S
  257. $(call compile_as_dep,$@,$<)
  258. $(build_dir)/%.o: $(src_dir)/%.S
  259. $(call compile_as,$@,$<)
  260. $(platform_build_dir)/%.dep: $(platform_src_dir)/%.c
  261. $(call compile_cc_dep,$@,$<)
  262. $(platform_build_dir)/%.o: $(platform_src_dir)/%.c
  263. $(call compile_cc,$@,$<)
  264. $(platform_build_dir)/%.dep: $(platform_src_dir)/%.S
  265. $(call compile_as_dep,$@,$<)
  266. $(platform_build_dir)/%.o: $(platform_src_dir)/%.S
  267. $(call compile_as,$@,$<)
  268. $(platform_build_dir)/%.dep: $(src_dir)/%.c
  269. $(call compile_cc_dep,$@,$<)
  270. $(platform_build_dir)/%.o: $(src_dir)/%.c
  271. $(call compile_cc,$@,$<)
  272. $(platform_build_dir)/%.dep: $(src_dir)/%.S
  273. $(call compile_as_dep,$@,$<)
  274. $(platform_build_dir)/%.o: $(src_dir)/%.S
  275. $(call compile_as,$@,$<)
  276. $(build_dir)/%.dtb: $(src_dir)/%.dts
  277. $(call compile_dts,$@,$<)
  278. # Rule for "make docs"
  279. $(build_dir)/docs/latex/refman.pdf: $(build_dir)/docs/latex/refman.tex
  280. $(CMD_PREFIX)mkdir -p $(build_dir)/docs
  281. $(CMD_PREFIX)$(MAKE) -C $(build_dir)/docs/latex
  282. $(build_dir)/docs/latex/refman.tex: $(build_dir)/docs/doxygen.cfg
  283. $(CMD_PREFIX)mkdir -p $(build_dir)/docs
  284. $(CMD_PREFIX)doxygen $(build_dir)/docs/doxygen.cfg
  285. $(build_dir)/docs/doxygen.cfg: $(src_dir)/docs/doxygen.cfg
  286. $(CMD_PREFIX)mkdir -p $(build_dir)/docs
  287. $(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
  288. .PHONY: docs
  289. docs: $(build_dir)/docs/latex/refman.pdf
  290. # Dependency files should only be included after default Makefile rules
  291. # They should not be included for any "xxxconfig" or "xxxclean" rule
  292. all-deps-1 = $(if $(findstring config,$(MAKECMDGOALS)),,$(deps-y))
  293. all-deps-2 = $(if $(findstring clean,$(MAKECMDGOALS)),,$(all-deps-1))
  294. -include $(all-deps-2)
  295. # Include external dependency of firmwares after default Makefile rules
  296. include $(src_dir)/firmware/external_deps.mk
  297. # Convenient "make run" command for emulated platforms
  298. .PHONY: run
  299. run: all
  300. ifneq ($(platform-runcmd),)
  301. $(platform-runcmd) $(RUN_ARGS)
  302. else
  303. ifdef PLATFORM
  304. @echo "Platform $(PLATFORM) doesn't specify a run command"
  305. @false
  306. else
  307. @echo Run command only available when targeting a platform
  308. @false
  309. endif
  310. endif
  311. install_targets-y = install_libsbi
  312. ifdef PLATFORM
  313. install_targets-y += install_libplatsbi
  314. install_targets-y += install_firmwares
  315. endif
  316. # Rule for "make install"
  317. .PHONY: install
  318. install: $(install_targets-y)
  319. .PHONY: install_libsbi
  320. install_libsbi: $(build_dir)/lib/libsbi.a
  321. $(call inst_header_dir,$(install_dir)/include,$(include_dir)/sbi)
  322. $(call inst_file,$(install_dir)/lib/libsbi.a,$(build_dir)/lib/libsbi.a)
  323. .PHONY: install_libplatsbi
  324. install_libplatsbi: $(platform_build_dir)/lib/libplatsbi.a $(build_dir)/lib/libsbi.a
  325. $(call inst_file,$(install_dir)/platform/$(platform_subdir)/lib/libplatsbi.a,$(platform_build_dir)/lib/libplatsbi.a)
  326. .PHONY: install_firmwares
  327. install_firmwares: $(platform_build_dir)/lib/libplatsbi.a $(build_dir)/lib/libsbi.a $(firmware-bins-path-y)
  328. $(call inst_file_list,$(install_dir),$(build_dir),platform/$(platform_subdir)/firmware,$(firmware-elfs-path-y))
  329. $(call inst_file_list,$(install_dir),$(build_dir),platform/$(platform_subdir)/firmware,$(firmware-bins-path-y))
  330. .PHONY: install_docs
  331. install_docs: $(build_dir)/docs/latex/refman.pdf
  332. $(call inst_file,$(install_dir)/docs/refman.pdf,$(build_dir)/docs/latex/refman.pdf)
  333. # Rule for "make clean"
  334. .PHONY: clean
  335. clean:
  336. $(CMD_PREFIX)mkdir -p $(build_dir)
  337. $(if $(V), @echo " RM $(build_dir)/*.o")
  338. $(CMD_PREFIX)find $(build_dir) -type f -name "*.o" -exec rm -rf {} +
  339. $(if $(V), @echo " RM $(build_dir)/*.a")
  340. $(CMD_PREFIX)find $(build_dir) -type f -name "*.a" -exec rm -rf {} +
  341. $(if $(V), @echo " RM $(build_dir)/*.elf")
  342. $(CMD_PREFIX)find $(build_dir) -type f -name "*.elf" -exec rm -rf {} +
  343. $(if $(V), @echo " RM $(build_dir)/*.bin")
  344. $(CMD_PREFIX)find $(build_dir) -type f -name "*.bin" -exec rm -rf {} +
  345. # Rule for "make distclean"
  346. .PHONY: distclean
  347. distclean: clean
  348. $(CMD_PREFIX)mkdir -p $(build_dir)
  349. $(if $(V), @echo " RM $(build_dir)/*.dep")
  350. $(CMD_PREFIX)find $(build_dir) -type f -name "*.dep" -exec rm -rf {} +
  351. ifeq ($(build_dir),$(CURDIR)/build)
  352. $(if $(V), @echo " RM $(build_dir)")
  353. $(CMD_PREFIX)rm -rf $(build_dir)
  354. endif
  355. ifeq ($(install_dir),$(CURDIR)/install)
  356. $(if $(V), @echo " RM $(install_dir)")
  357. $(CMD_PREFIX)rm -rf $(install_dir)
  358. endif