Makefile.build 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. # SPDX-License-Identifier: GPL-2.0
  2. # ==========================================================================
  3. # Building
  4. # ==========================================================================
  5. # Modified for U-Boot
  6. prefix := tpl
  7. src := $(patsubst $(prefix)/%,%,$(obj))
  8. ifeq ($(obj),$(src))
  9. prefix := spl
  10. src := $(patsubst $(prefix)/%,%,$(obj))
  11. ifeq ($(obj),$(src))
  12. prefix := .
  13. endif
  14. endif
  15. PHONY := __build
  16. __build:
  17. # Init all relevant variables used in kbuild files so
  18. # 1) they have correct type
  19. # 2) they do not inherit any value from the environment
  20. obj-y :=
  21. obj-m :=
  22. lib-y :=
  23. lib-m :=
  24. always :=
  25. targets :=
  26. subdir-y :=
  27. subdir-m :=
  28. EXTRA_AFLAGS :=
  29. EXTRA_CFLAGS :=
  30. EXTRA_CPPFLAGS :=
  31. EXTRA_LDFLAGS :=
  32. asflags-y :=
  33. ccflags-y :=
  34. cppflags-y :=
  35. ldflags-y :=
  36. subdir-asflags-y :=
  37. subdir-ccflags-y :=
  38. # Read auto.conf if it exists, otherwise ignore
  39. # Modified for U-Boot
  40. -include include/config/auto.conf
  41. -include $(prefix)/include/autoconf.mk
  42. include scripts/Makefile.uncmd_spl
  43. include scripts/Kbuild.include
  44. # For backward compatibility check that these variables do not change
  45. save-cflags := $(CFLAGS)
  46. # The filename Kbuild has precedence over Makefile
  47. kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
  48. kbuild-file := $(if $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Kbuild,$(kbuild-dir)/Makefile)
  49. include $(kbuild-file)
  50. # Added for U-Boot
  51. asflags-y += $(PLATFORM_CPPFLAGS)
  52. ccflags-y += $(PLATFORM_CPPFLAGS)
  53. cppflags-y += $(PLATFORM_CPPFLAGS)
  54. # If the save-* variables changed error out
  55. ifeq ($(KBUILD_NOPEDANTIC),)
  56. ifneq ("$(save-cflags)","$(CFLAGS)")
  57. $(error CFLAGS was changed in "$(kbuild-file)". Fix it to use ccflags-y)
  58. endif
  59. endif
  60. include scripts/Makefile.lib
  61. ifdef host-progs
  62. ifneq ($(hostprogs-y),$(host-progs))
  63. $(warning kbuild: $(obj)/Makefile - Usage of host-progs is deprecated. Please replace with hostprogs-y!)
  64. hostprogs-y += $(host-progs)
  65. endif
  66. endif
  67. # Do not include host rules unless needed
  68. ifneq ($(hostprogs-y)$(hostprogs-m),)
  69. include scripts/Makefile.host
  70. endif
  71. # Uncommented for U-Boot
  72. # We need to create output dicrectory for SPL and TPL even for in-tree build
  73. #ifneq ($(KBUILD_SRC),)
  74. # Create output directory if not already present
  75. _dummy := $(shell [ -d $(obj) ] || mkdir -p $(obj))
  76. # Create directories for object files if directory does not exist
  77. # Needed when obj-y := dir/file.o syntax is used
  78. _dummy := $(foreach d,$(obj-dirs), $(shell [ -d $(d) ] || mkdir -p $(d)))
  79. #endif
  80. ifndef obj
  81. $(warning kbuild: Makefile.build is included improperly)
  82. endif
  83. # ===========================================================================
  84. ifneq ($(strip $(lib-y) $(lib-m) $(lib-)),)
  85. lib-target := $(obj)/lib.a
  86. endif
  87. ifneq ($(strip $(obj-y) $(obj-m) $(obj-) $(subdir-m) $(lib-target)),)
  88. builtin-target := $(obj)/built-in.o
  89. endif
  90. modorder-target := $(obj)/modules.order
  91. # We keep a list of all modules in $(MODVERDIR)
  92. __build: $(if $(KBUILD_BUILTIN),$(builtin-target) $(lib-target) $(extra-y)) \
  93. $(if $(KBUILD_MODULES),$(obj-m) $(modorder-target)) \
  94. $(subdir-ym) $(always)
  95. @:
  96. # Linus' kernel sanity checking tool
  97. ifneq ($(KBUILD_CHECKSRC),0)
  98. ifeq ($(KBUILD_CHECKSRC),2)
  99. quiet_cmd_force_checksrc = CHECK $<
  100. cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< ;
  101. else
  102. quiet_cmd_checksrc = CHECK $<
  103. cmd_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< ;
  104. endif
  105. endif
  106. # Do section mismatch analysis for each module/built-in.o
  107. ifdef CONFIG_DEBUG_SECTION_MISMATCH
  108. cmd_secanalysis = ; scripts/mod/modpost $@
  109. endif
  110. # Compile C sources (.c)
  111. # ---------------------------------------------------------------------------
  112. # Default is built-in, unless we know otherwise
  113. modkern_cflags = \
  114. $(if $(part-of-module), \
  115. $(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE), \
  116. $(KBUILD_CFLAGS_KERNEL) $(CFLAGS_KERNEL))
  117. quiet_modtag := $(empty) $(empty)
  118. $(real-objs-m) : part-of-module := y
  119. $(real-objs-m:.o=.i) : part-of-module := y
  120. $(real-objs-m:.o=.s) : part-of-module := y
  121. $(real-objs-m:.o=.lst): part-of-module := y
  122. $(real-objs-m) : quiet_modtag := [M]
  123. $(real-objs-m:.o=.i) : quiet_modtag := [M]
  124. $(real-objs-m:.o=.s) : quiet_modtag := [M]
  125. $(real-objs-m:.o=.lst): quiet_modtag := [M]
  126. $(obj-m) : quiet_modtag := [M]
  127. # Default for not multi-part modules
  128. modname = $(basetarget)
  129. $(multi-objs-m) : modname = $(modname-multi)
  130. $(multi-objs-m:.o=.i) : modname = $(modname-multi)
  131. $(multi-objs-m:.o=.s) : modname = $(modname-multi)
  132. $(multi-objs-m:.o=.lst) : modname = $(modname-multi)
  133. $(multi-objs-y) : modname = $(modname-multi)
  134. $(multi-objs-y:.o=.i) : modname = $(modname-multi)
  135. $(multi-objs-y:.o=.s) : modname = $(modname-multi)
  136. $(multi-objs-y:.o=.lst) : modname = $(modname-multi)
  137. quiet_cmd_cc_s_c = CC $(quiet_modtag) $@
  138. cmd_cc_s_c = $(CC) $(c_flags) $(DISABLE_LTO) -fverbose-asm -S -o $@ $<
  139. $(obj)/%.s: $(src)/%.c FORCE
  140. $(call if_changed_dep,cc_s_c)
  141. quiet_cmd_cc_i_c = CPP $(quiet_modtag) $@
  142. cmd_cc_i_c = $(CPP) $(c_flags) -o $@ $<
  143. $(obj)/%.i: $(src)/%.c FORCE
  144. $(call if_changed_dep,cc_i_c)
  145. cmd_gensymtypes = \
  146. $(CPP) -D__GENKSYMS__ $(c_flags) $< | \
  147. $(GENKSYMS) $(if $(1), -T $(2)) \
  148. $(patsubst y,-s _,$(CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX)) \
  149. $(if $(KBUILD_PRESERVE),-p) \
  150. -r $(firstword $(wildcard $(2:.symtypes=.symref) /dev/null))
  151. quiet_cmd_cc_symtypes_c = SYM $(quiet_modtag) $@
  152. cmd_cc_symtypes_c = \
  153. set -e; \
  154. $(call cmd_gensymtypes,true,$@) >/dev/null; \
  155. test -s $@ || rm -f $@
  156. $(obj)/%.symtypes : $(src)/%.c FORCE
  157. $(call cmd,cc_symtypes_c)
  158. # C (.c) files
  159. # The C file is compiled and updated dependency information is generated.
  160. # (See cmd_cc_o_c + relevant part of rule_cc_o_c)
  161. quiet_cmd_cc_o_c = CC $(quiet_modtag) $@
  162. ifndef CONFIG_MODVERSIONS
  163. cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
  164. else
  165. # When module versioning is enabled the following steps are executed:
  166. # o compile a .tmp_<file>.o from <file>.c
  167. # o if .tmp_<file>.o doesn't contain a __ksymtab version, i.e. does
  168. # not export symbols, we just rename .tmp_<file>.o to <file>.o and
  169. # are done.
  170. # o otherwise, we calculate symbol versions using the good old
  171. # genksyms on the preprocessed source and postprocess them in a way
  172. # that they are usable as a linker script
  173. # o generate <file>.o from .tmp_<file>.o using the linker to
  174. # replace the unresolved symbols __crc_exported_symbol with
  175. # the actual value of the checksum generated by genksyms
  176. cmd_cc_o_c = $(CC) $(c_flags) -c -o $(@D)/.tmp_$(@F) $<
  177. cmd_modversions = \
  178. if $(OBJDUMP) -h $(@D)/.tmp_$(@F) | grep -q __ksymtab; then \
  179. $(call cmd_gensymtypes,$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \
  180. > $(@D)/.tmp_$(@F:.o=.ver); \
  181. \
  182. $(LD) $(LDFLAGS) -r -o $@ $(@D)/.tmp_$(@F) \
  183. -T $(@D)/.tmp_$(@F:.o=.ver); \
  184. rm -f $(@D)/.tmp_$(@F) $(@D)/.tmp_$(@F:.o=.ver); \
  185. else \
  186. mv -f $(@D)/.tmp_$(@F) $@; \
  187. fi;
  188. endif
  189. ifdef CONFIG_FTRACE_MCOUNT_RECORD
  190. ifdef BUILD_C_RECORDMCOUNT
  191. ifeq ("$(origin RECORDMCOUNT_WARN)", "command line")
  192. RECORDMCOUNT_FLAGS = -w
  193. endif
  194. # Due to recursion, we must skip empty.o.
  195. # The empty.o file is created in the make process in order to determine
  196. # the target endianness and word size. It is made before all other C
  197. # files, including recordmcount.
  198. sub_cmd_record_mcount = \
  199. if [ $(@) != "scripts/mod/empty.o" ]; then \
  200. $(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)"; \
  201. fi;
  202. recordmcount_source := $(srctree)/scripts/recordmcount.c \
  203. $(srctree)/scripts/recordmcount.h
  204. else
  205. sub_cmd_record_mcount = set -e ; perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \
  206. "$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)" \
  207. "$(if $(CONFIG_64BIT),64,32)" \
  208. "$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CFLAGS)" \
  209. "$(LD)" "$(NM)" "$(RM)" "$(MV)" \
  210. "$(if $(part-of-module),1,0)" "$(@)";
  211. recordmcount_source := $(srctree)/scripts/recordmcount.pl
  212. endif
  213. cmd_record_mcount = \
  214. if [ "$(findstring $(CC_FLAGS_FTRACE),$(_c_flags))" = \
  215. "$(CC_FLAGS_FTRACE)" ]; then \
  216. $(sub_cmd_record_mcount) \
  217. fi;
  218. endif
  219. define rule_cc_o_c
  220. $(call echo-cmd,checksrc) $(cmd_checksrc) \
  221. $(call echo-cmd,cc_o_c) $(cmd_cc_o_c); \
  222. $(cmd_modversions) \
  223. $(call echo-cmd,record_mcount) \
  224. $(cmd_record_mcount) \
  225. scripts/basic/fixdep $(depfile) $@ '$(call make-cmd,cc_o_c)' > \
  226. $(dot-target).tmp; \
  227. rm -f $(depfile); \
  228. mv -f $(dot-target).tmp $(dot-target).cmd
  229. endef
  230. # Built-in and composite module parts
  231. $(obj)/%.o: $(src)/%.c $(recordmcount_source) FORCE
  232. $(call cmd,force_checksrc)
  233. $(call if_changed_rule,cc_o_c)
  234. # Single-part modules are special since we need to mark them in $(MODVERDIR)
  235. $(single-used-m): $(obj)/%.o: $(src)/%.c $(recordmcount_source) FORCE
  236. $(call cmd,force_checksrc)
  237. $(call if_changed_rule,cc_o_c)
  238. @{ echo $(@:.o=.ko); echo $@; } > $(MODVERDIR)/$(@F:.o=.mod)
  239. quiet_cmd_cc_lst_c = MKLST $@
  240. cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
  241. $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \
  242. System.map $(OBJDUMP) > $@
  243. $(obj)/%.lst: $(src)/%.c FORCE
  244. $(call if_changed_dep,cc_lst_c)
  245. # Compile assembler sources (.S)
  246. # ---------------------------------------------------------------------------
  247. modkern_aflags := $(KBUILD_AFLAGS_KERNEL) $(AFLAGS_KERNEL)
  248. $(real-objs-m) : modkern_aflags := $(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE)
  249. $(real-objs-m:.o=.s): modkern_aflags := $(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE)
  250. quiet_cmd_as_s_S = CPP $(quiet_modtag) $@
  251. cmd_as_s_S = $(CPP) $(a_flags) -o $@ $<
  252. $(obj)/%.s: $(src)/%.S FORCE
  253. $(call if_changed_dep,as_s_S)
  254. quiet_cmd_as_o_S = AS $(quiet_modtag) $@
  255. cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $<
  256. $(obj)/%.o: $(src)/%.S FORCE
  257. $(call if_changed_dep,as_o_S)
  258. targets += $(real-objs-y) $(real-objs-m) $(lib-y)
  259. targets += $(extra-y) $(MAKECMDGOALS) $(always)
  260. # Linker scripts preprocessor (.lds.S -> .lds)
  261. # ---------------------------------------------------------------------------
  262. quiet_cmd_cpp_lds_S = LDS $@
  263. cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -C -U$(ARCH) \
  264. -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $<
  265. $(obj)/%.lds: $(src)/%.lds.S FORCE
  266. $(call if_changed_dep,cpp_lds_S)
  267. # ASN.1 grammar
  268. # ---------------------------------------------------------------------------
  269. quiet_cmd_asn1_compiler = ASN.1 $@
  270. cmd_asn1_compiler = $(objtree)/tools/asn1_compiler $< \
  271. $(subst .h,.c,$@) $(subst .c,.h,$@)
  272. $(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/tools/asn1_compiler
  273. $(call cmd,asn1_compiler)
  274. # Build the compiled-in targets
  275. # ---------------------------------------------------------------------------
  276. # To build objects in subdirs, we need to descend into the directories
  277. $(sort $(subdir-obj-y)): $(subdir-ym) ;
  278. #
  279. # Rule to compile a set of .o files into one .o file
  280. #
  281. ifdef builtin-target
  282. quiet_cmd_link_o_target = LD $@
  283. # If the list of objects to link is empty, just create an empty built-in.o
  284. cmd_link_o_target = $(if $(strip $(obj-y)),\
  285. $(LD) $(ld_flags) -r -o $@ $(filter $(obj-y), $^) \
  286. $(cmd_secanalysis),\
  287. rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@)
  288. $(builtin-target): $(obj-y) FORCE
  289. $(call if_changed,link_o_target)
  290. targets += $(builtin-target)
  291. endif # builtin-target
  292. #
  293. # Rule to create modules.order file
  294. #
  295. # Create commands to either record .ko file or cat modules.order from
  296. # a subdirectory
  297. modorder-cmds = \
  298. $(foreach m, $(modorder), \
  299. $(if $(filter %/modules.order, $m), \
  300. cat $m;, echo kernel/$m;))
  301. $(modorder-target): $(subdir-ym) FORCE
  302. $(Q)(cat /dev/null; $(modorder-cmds)) > $@
  303. #
  304. # Rule to compile a set of .o files into one .a file
  305. #
  306. ifdef lib-target
  307. quiet_cmd_link_l_target = AR $@
  308. cmd_link_l_target = rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@ $(lib-y)
  309. $(lib-target): $(lib-y) FORCE
  310. $(call if_changed,link_l_target)
  311. targets += $(lib-target)
  312. endif
  313. #
  314. # Rule to link composite objects
  315. #
  316. # Composite objects are specified in kbuild makefile as follows:
  317. # <composite-object>-objs := <list of .o files>
  318. # or
  319. # <composite-object>-y := <list of .o files>
  320. link_multi_deps = \
  321. $(filter $(addprefix $(obj)/, \
  322. $($(subst $(obj)/,,$(@:.o=-objs))) \
  323. $($(subst $(obj)/,,$(@:.o=-y)))), $^)
  324. quiet_cmd_link_multi-y = LD $@
  325. cmd_link_multi-y = $(LD) $(ld_flags) -r -o $@ $(link_multi_deps) $(cmd_secanalysis)
  326. quiet_cmd_link_multi-m = LD [M] $@
  327. cmd_link_multi-m = $(cmd_link_multi-y)
  328. $(multi-used-y): FORCE
  329. $(call if_changed,link_multi-y)
  330. $(call multi_depend, $(multi-used-y), .o, -objs -y)
  331. $(multi-used-m): FORCE
  332. $(call if_changed,link_multi-m)
  333. @{ echo $(@:.o=.ko); echo $(link_multi_deps); } > $(MODVERDIR)/$(@F:.o=.mod)
  334. $(call multi_depend, $(multi-used-m), .o, -objs -y)
  335. targets += $(multi-used-y) $(multi-used-m)
  336. # Add intermediate targets:
  337. # When building objects with specific suffix patterns, add intermediate
  338. # targets that the final targets are derived from.
  339. intermediate_targets = $(foreach sfx, $(2), \
  340. $(patsubst %$(strip $(1)),%$(sfx), \
  341. $(filter %$(strip $(1)), $(targets))))
  342. # %.lex.o <- %.lex.c <- %.l
  343. # %.tab.o <- %.tab.[ch] <- %.y
  344. targets += $(call intermediate_targets, .lex.o, .lex.c) \
  345. $(call intermediate_targets, .tab.o, .tab.c .tab.h)
  346. # Descending
  347. # ---------------------------------------------------------------------------
  348. PHONY += $(subdir-ym)
  349. $(subdir-ym):
  350. $(Q)$(MAKE) $(build)=$@
  351. # Add FORCE to the prequisites of a target to force it to be always rebuilt.
  352. # ---------------------------------------------------------------------------
  353. PHONY += FORCE
  354. FORCE:
  355. # Read all saved command lines and dependencies for the $(targets) we
  356. # may be building above, using $(if_changed{,_dep}). As an
  357. # optimization, we don't need to read them if the target does not
  358. # exist, we will rebuild anyway in that case.
  359. targets := $(wildcard $(sort $(targets)))
  360. cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
  361. ifneq ($(cmd_files),)
  362. include $(cmd_files)
  363. endif
  364. # Declare the contents of the .PHONY variable as phony. We keep that
  365. # information in a variable se we can use it in if_changed and friends.
  366. .PHONY: $(PHONY)