Makefile.build 16 KB

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