Makefile.build 16 KB

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