Makefile 15 KB

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