Makefile.lib 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Backward compatibility
  5. asflags-y += $(EXTRA_AFLAGS)
  6. ccflags-y += $(EXTRA_CFLAGS)
  7. cppflags-y += $(EXTRA_CPPFLAGS)
  8. ldflags-y += $(EXTRA_LDFLAGS)
  9. #
  10. # flags that take effect in sub directories
  11. export KBUILD_SUBDIR_ASFLAGS := $(KBUILD_SUBDIR_ASFLAGS) $(subdir-asflags-y)
  12. export KBUILD_SUBDIR_CCFLAGS := $(KBUILD_SUBDIR_CCFLAGS) $(subdir-ccflags-y)
  13. # Figure out what we need to build from the various variables
  14. # ===========================================================================
  15. # When an object is listed to be built compiled-in and modular,
  16. # only build the compiled-in version
  17. obj-m := $(filter-out $(obj-y),$(obj-m))
  18. # Libraries are always collected in one lib file.
  19. # Filter out objects already built-in
  20. lib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m)))
  21. # Handle objects in subdirs
  22. # ---------------------------------------------------------------------------
  23. # o if we encounter foo/ in $(obj-y), replace it by foo/built-in.o
  24. # and add the directory to the list of dirs to descend into: $(subdir-y)
  25. # o if we encounter foo/ in $(obj-m), remove it from $(obj-m)
  26. # and add the directory to the list of dirs to descend into: $(subdir-m)
  27. # Determine modorder.
  28. # Unfortunately, we don't have information about ordering between -y
  29. # and -m subdirs. Just put -y's first.
  30. modorder := $(patsubst %/,%/modules.order, $(filter %/, $(obj-y)) $(obj-m:.o=.ko))
  31. __subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
  32. subdir-y += $(__subdir-y)
  33. __subdir-m := $(patsubst %/,%,$(filter %/, $(obj-m)))
  34. subdir-m += $(__subdir-m)
  35. obj-y := $(patsubst %/, %/built-in.o, $(obj-y))
  36. obj-m := $(filter-out %/, $(obj-m))
  37. # Subdirectories we need to descend into
  38. subdir-ym := $(sort $(subdir-y) $(subdir-m))
  39. # if $(foo-objs) exists, foo.o is a composite object
  40. multi-used-y := $(sort $(foreach m,$(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
  41. multi-used-m := $(sort $(foreach m,$(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
  42. multi-used := $(multi-used-y) $(multi-used-m)
  43. single-used-m := $(sort $(filter-out $(multi-used-m),$(obj-m)))
  44. # Build list of the parts of our composite objects, our composite
  45. # objects depend on those (obviously)
  46. multi-objs-y := $(foreach m, $(multi-used-y), $($(m:.o=-objs)) $($(m:.o=-y)))
  47. multi-objs-m := $(foreach m, $(multi-used-m), $($(m:.o=-objs)) $($(m:.o=-y)))
  48. multi-objs := $(multi-objs-y) $(multi-objs-m)
  49. # $(subdir-obj-y) is the list of objects in $(obj-y) which uses dir/ to
  50. # tell kbuild to descend
  51. subdir-obj-y := $(filter %/built-in.o, $(obj-y))
  52. # $(obj-dirs) is a list of directories that contain object files
  53. obj-dirs := $(dir $(multi-objs) $(obj-y))
  54. # Replace multi-part objects by their individual parts, look at local dir only
  55. real-objs-y := $(foreach m, $(filter-out $(subdir-obj-y), $(obj-y)), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m))) $(extra-y)
  56. real-objs-m := $(foreach m, $(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m)))
  57. # Add subdir path
  58. extra-y := $(addprefix $(obj)/,$(extra-y))
  59. always := $(addprefix $(obj)/,$(always))
  60. targets := $(addprefix $(obj)/,$(targets))
  61. modorder := $(addprefix $(obj)/,$(modorder))
  62. obj-y := $(addprefix $(obj)/,$(obj-y))
  63. obj-m := $(addprefix $(obj)/,$(obj-m))
  64. lib-y := $(addprefix $(obj)/,$(lib-y))
  65. subdir-obj-y := $(addprefix $(obj)/,$(subdir-obj-y))
  66. real-objs-y := $(addprefix $(obj)/,$(real-objs-y))
  67. real-objs-m := $(addprefix $(obj)/,$(real-objs-m))
  68. single-used-m := $(addprefix $(obj)/,$(single-used-m))
  69. multi-used-y := $(addprefix $(obj)/,$(multi-used-y))
  70. multi-used-m := $(addprefix $(obj)/,$(multi-used-m))
  71. multi-objs-y := $(addprefix $(obj)/,$(multi-objs-y))
  72. multi-objs-m := $(addprefix $(obj)/,$(multi-objs-m))
  73. subdir-ym := $(addprefix $(obj)/,$(subdir-ym))
  74. obj-dirs := $(addprefix $(obj)/,$(obj-dirs))
  75. # These flags are needed for modversions and compiling, so we define them here
  76. # already
  77. # $(modname_flags) #defines KBUILD_MODNAME as the name of the module it will
  78. # end up in (or would, if it gets compiled in)
  79. # Note: Files that end up in two or more modules are compiled without the
  80. # KBUILD_MODNAME definition. The reason is that any made-up name would
  81. # differ in different configs.
  82. name-fix = $(subst $(comma),_,$(subst -,_,$1))
  83. basename_flags = -D"KBUILD_BASENAME=KBUILD_STR($(call name-fix,$(basetarget)))"
  84. modname_flags = $(if $(filter 1,$(words $(modname))),\
  85. -D"KBUILD_MODNAME=KBUILD_STR($(call name-fix,$(modname)))")
  86. orig_c_flags = $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(KBUILD_SUBDIR_CCFLAGS) \
  87. $(ccflags-y) $(CFLAGS_$(basetarget).o)
  88. _c_flags = $(filter-out $(CFLAGS_REMOVE_$(basetarget).o), $(orig_c_flags))
  89. orig_a_flags = $(KBUILD_CPPFLAGS) $(KBUILD_AFLAGS) $(KBUILD_SUBDIR_ASFLAGS) \
  90. $(asflags-y) $(AFLAGS_$(basetarget).o)
  91. _a_flags = $(filter-out $(AFLAGS_REMOVE_$(basetarget).o), $(orig_a_flags))
  92. _cpp_flags = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(@F))
  93. #
  94. # Enable gcov profiling flags for a file, directory or for all files depending
  95. # on variables GCOV_PROFILE_obj.o, GCOV_PROFILE and CONFIG_GCOV_PROFILE_ALL
  96. # (in this order)
  97. #
  98. ifeq ($(CONFIG_GCOV_KERNEL),y)
  99. _c_flags += $(if $(patsubst n%,, \
  100. $(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)), \
  101. $(CFLAGS_GCOV))
  102. endif
  103. #
  104. # Enable address sanitizer flags for kernel except some files or directories
  105. # we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE)
  106. #
  107. ifeq ($(CONFIG_KASAN),y)
  108. _c_flags += $(if $(patsubst n%,, \
  109. $(KASAN_SANITIZE_$(basetarget).o)$(KASAN_SANITIZE)y), \
  110. $(CFLAGS_KASAN))
  111. endif
  112. # If building the kernel in a separate objtree expand all occurrences
  113. # of -Idir to -I$(srctree)/dir except for absolute paths (starting with '/').
  114. ifeq ($(KBUILD_SRC),)
  115. __c_flags = $(_c_flags)
  116. __a_flags = $(_a_flags)
  117. __cpp_flags = $(_cpp_flags)
  118. else
  119. # -I$(obj) locates generated .h files
  120. # $(call addtree,-I$(obj)) locates .h files in srctree, from generated .c files
  121. # and locates generated .h files
  122. # FIXME: Replace both with specific CFLAGS* statements in the makefiles
  123. __c_flags = $(call addtree,-I$(obj)) $(call flags,_c_flags)
  124. __a_flags = $(call flags,_a_flags)
  125. __cpp_flags = $(call flags,_cpp_flags)
  126. endif
  127. # Modified for U-Boot: LINUXINCLUDE -> UBOOTINCLUDE
  128. c_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(UBOOTINCLUDE) \
  129. $(__c_flags) $(modkern_cflags) \
  130. -D"KBUILD_STR(s)=\#s" $(basename_flags) $(modname_flags)
  131. a_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(UBOOTINCLUDE) \
  132. $(__a_flags) $(modkern_aflags)
  133. cpp_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(UBOOTINCLUDE) \
  134. $(__cpp_flags)
  135. ld_flags = $(LDFLAGS) $(ldflags-y)
  136. # Modified for U-Boot
  137. dtc_cpp_flags = -Wp,-MD,$(depfile).pre.tmp -nostdinc \
  138. -I$(srctree)/arch/$(ARCH)/dts \
  139. -I$(srctree)/arch/$(ARCH)/dts/include \
  140. -undef -D__DTS__
  141. # Finds the multi-part object the current object will be linked into
  142. modname-multi = $(sort $(foreach m,$(multi-used),\
  143. $(if $(filter $(subst $(obj)/,,$*.o), $($(m:.o=-objs)) $($(m:.o=-y))),$(m:.o=))))
  144. # Useful for describing the dependency of composite objects
  145. # Usage:
  146. # $(call multi_depend, multi_used_targets, suffix_to_remove, suffix_to_add)
  147. define multi_depend
  148. $(foreach m, $(notdir $1), \
  149. $(eval $(obj)/$m: \
  150. $(addprefix $(obj)/, $(foreach s, $3, $($(m:%$(strip $2)=%$(s)))))))
  151. endef
  152. ifdef REGENERATE_PARSERS
  153. # GPERF
  154. # ---------------------------------------------------------------------------
  155. quiet_cmd_gperf = GPERF $@
  156. cmd_gperf = gperf -t --output-file $@ -a -C -E -g -k 1,3,$$ -p -t $<
  157. .PRECIOUS: $(src)/%.hash.c_shipped
  158. $(src)/%.hash.c_shipped: $(src)/%.gperf
  159. $(call cmd,gperf)
  160. # LEX
  161. # ---------------------------------------------------------------------------
  162. LEX_PREFIX = $(if $(LEX_PREFIX_${baseprereq}),$(LEX_PREFIX_${baseprereq}),yy)
  163. quiet_cmd_flex = LEX $@
  164. cmd_flex = flex -o$@ -L -P $(LEX_PREFIX) $<
  165. .PRECIOUS: $(src)/%.lex.c_shipped
  166. $(src)/%.lex.c_shipped: $(src)/%.l
  167. $(call cmd,flex)
  168. # YACC
  169. # ---------------------------------------------------------------------------
  170. YACC_PREFIX = $(if $(YACC_PREFIX_${baseprereq}),$(YACC_PREFIX_${baseprereq}),yy)
  171. quiet_cmd_bison = YACC $@
  172. cmd_bison = bison -o$@ -t -l -p $(YACC_PREFIX) $<
  173. .PRECIOUS: $(src)/%.tab.c_shipped
  174. $(src)/%.tab.c_shipped: $(src)/%.y
  175. $(call cmd,bison)
  176. quiet_cmd_bison_h = YACC $@
  177. cmd_bison_h = bison -o/dev/null --defines=$@ -t -l -p $(YACC_PREFIX) $<
  178. .PRECIOUS: $(src)/%.tab.h_shipped
  179. $(src)/%.tab.h_shipped: $(src)/%.y
  180. $(call cmd,bison_h)
  181. endif
  182. # Shipped files
  183. # ===========================================================================
  184. quiet_cmd_shipped = SHIPPED $@
  185. cmd_shipped = cat $< > $@
  186. $(obj)/%: $(src)/%_shipped
  187. $(call cmd,shipped)
  188. # Commands useful for building a boot image
  189. # ===========================================================================
  190. #
  191. # Use as following:
  192. #
  193. # target: source(s) FORCE
  194. # $(if_changed,ld/objcopy/gzip)
  195. #
  196. # and add target to extra-y so that we know we have to
  197. # read in the saved command line
  198. # Linking
  199. # ---------------------------------------------------------------------------
  200. quiet_cmd_ld = LD $@
  201. cmd_ld = $(LD) $(LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F)) \
  202. $(filter-out FORCE,$^) -o $@
  203. # Objcopy
  204. # ---------------------------------------------------------------------------
  205. quiet_cmd_objcopy = OBJCOPY $@
  206. cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
  207. # Gzip
  208. # ---------------------------------------------------------------------------
  209. quiet_cmd_gzip = GZIP $@
  210. cmd_gzip = (cat $(filter-out FORCE,$^) | gzip -n -f -9 > $@) || \
  211. (rm -f $@ ; false)
  212. # DTC
  213. # ---------------------------------------------------------------------------
  214. # Generate an assembly file to wrap the output of the device tree compiler
  215. quiet_cmd_dt_S_dtb= DTB $@
  216. # Modified for U-Boot
  217. cmd_dt_S_dtb= \
  218. ( \
  219. echo '.section .dtb.init.rodata,"a"'; \
  220. echo '.balign 16'; \
  221. echo '.global __dtb_$(*F)_begin'; \
  222. echo '__dtb_$(*F)_begin:'; \
  223. echo '.incbin "$<" '; \
  224. echo '__dtb_$(*F)_end:'; \
  225. echo '.global __dtb_$(*F)_end'; \
  226. echo '.balign 16'; \
  227. ) > $@
  228. $(obj)/%.dtb.S: $(obj)/%.dtb
  229. $(call cmd,dt_S_dtb)
  230. quiet_cmd_dtc = DTC $@
  231. # Modified for U-Boot
  232. cmd_dtc = mkdir -p $(dir ${dtc-tmp}) ; \
  233. $(CPP) $(dtc_cpp_flags) -x assembler-with-cpp -o $(dtc-tmp) $< ; \
  234. $(DTC) -O dtb -o $@ -b 0 \
  235. -i $(dir $<) $(DTC_FLAGS) \
  236. -d $(depfile).dtc.tmp $(dtc-tmp) ; \
  237. cat $(depfile).pre.tmp $(depfile).dtc.tmp > $(depfile)
  238. $(obj)/%.dtb: $(src)/%.dts FORCE
  239. $(call if_changed_dep,dtc)
  240. dtc-tmp = $(subst $(comma),_,$(dot-target).dts.tmp)
  241. # Fonts
  242. # ---------------------------------------------------------------------------
  243. # Generate an assembly file to wrap the font data
  244. quiet_cmd_S_ttf= TTF $@
  245. # Modified for U-Boot
  246. cmd_S_ttf= \
  247. ( \
  248. echo '.section .rodata.ttf.init,"a"'; \
  249. echo '.balign 16'; \
  250. echo '.global __ttf_$(*F)_begin'; \
  251. echo '__ttf_$(*F)_begin:'; \
  252. echo '.incbin "$<" '; \
  253. echo '__ttf_$(*F)_end:'; \
  254. echo '.global __ttf_$(*F)_end'; \
  255. echo '.balign 16'; \
  256. ) > $@
  257. $(obj)/%.S: $(src)/%.ttf
  258. $(call cmd,S_ttf)
  259. # ACPI
  260. # ---------------------------------------------------------------------------
  261. quiet_cmd_acpi_c_asl= ASL $<
  262. cmd_acpi_c_asl= \
  263. $(CPP) -x assembler-with-cpp -D__ASSEMBLY__ -P $(UBOOTINCLUDE) -o $<.tmp $<; \
  264. iasl -p $< -tc $<.tmp $(if $(KBUILD_VERBOSE:1=), >/dev/null); \
  265. mv $(patsubst %.asl,%.hex,$<) $@
  266. $(obj)/dsdt.c: $(src)/dsdt.asl
  267. $(call cmd,acpi_c_asl)
  268. # Bzip2
  269. # ---------------------------------------------------------------------------
  270. # Bzip2 and LZMA do not include size in file... so we have to fake that;
  271. # append the size as a 32-bit littleendian number as gzip does.
  272. size_append = printf $(shell \
  273. dec_size=0; \
  274. for F in $1; do \
  275. fsize=$$(stat -c "%s" $$F); \
  276. dec_size=$$(expr $$dec_size + $$fsize); \
  277. done; \
  278. printf "%08x\n" $$dec_size | \
  279. sed 's/\(..\)/\1 /g' | { \
  280. read ch0 ch1 ch2 ch3; \
  281. for ch in $$ch3 $$ch2 $$ch1 $$ch0; do \
  282. printf '%s%03o' '\\' $$((0x$$ch)); \
  283. done; \
  284. } \
  285. )
  286. quiet_cmd_bzip2 = BZIP2 $@
  287. cmd_bzip2 = (cat $(filter-out FORCE,$^) | \
  288. bzip2 -9 && $(call size_append, $(filter-out FORCE,$^))) > $@ || \
  289. (rm -f $@ ; false)
  290. # Lzma
  291. # ---------------------------------------------------------------------------
  292. quiet_cmd_lzma = LZMA $@
  293. cmd_lzma = (cat $(filter-out FORCE,$^) | \
  294. lzma -9 && $(call size_append, $(filter-out FORCE,$^))) > $@ || \
  295. (rm -f $@ ; false)
  296. quiet_cmd_lzo = LZO $@
  297. cmd_lzo = (cat $(filter-out FORCE,$^) | \
  298. lzop -9 && $(call size_append, $(filter-out FORCE,$^))) > $@ || \
  299. (rm -f $@ ; false)
  300. quiet_cmd_lz4 = LZ4 $@
  301. cmd_lz4 = (cat $(filter-out FORCE,$^) | \
  302. lz4c -l -c1 stdin stdout && $(call size_append, $(filter-out FORCE,$^))) > $@ || \
  303. (rm -f $@ ; false)
  304. # U-Boot mkimage
  305. # ---------------------------------------------------------------------------
  306. MKIMAGE := $(srctree)/scripts/mkuboot.sh
  307. # SRCARCH just happens to match slightly more than ARCH (on sparc), so reduces
  308. # the number of overrides in arch makefiles
  309. UIMAGE_ARCH ?= $(SRCARCH)
  310. UIMAGE_COMPRESSION ?= $(if $(2),$(2),none)
  311. UIMAGE_OPTS-y ?=
  312. UIMAGE_TYPE ?= kernel
  313. UIMAGE_LOADADDR ?= arch_must_set_this
  314. UIMAGE_ENTRYADDR ?= $(UIMAGE_LOADADDR)
  315. UIMAGE_NAME ?= 'Linux-$(KERNELRELEASE)'
  316. UIMAGE_IN ?= $<
  317. UIMAGE_OUT ?= $@
  318. quiet_cmd_uimage = UIMAGE $(UIMAGE_OUT)
  319. cmd_uimage = $(CONFIG_SHELL) $(MKIMAGE) -A $(UIMAGE_ARCH) -O linux \
  320. -C $(UIMAGE_COMPRESSION) $(UIMAGE_OPTS-y) \
  321. -T $(UIMAGE_TYPE) \
  322. -a $(UIMAGE_LOADADDR) -e $(UIMAGE_ENTRYADDR) \
  323. -n $(UIMAGE_NAME) -d $(UIMAGE_IN) $(UIMAGE_OUT)
  324. # XZ
  325. # ---------------------------------------------------------------------------
  326. # Use xzkern to compress the kernel image and xzmisc to compress other things.
  327. #
  328. # xzkern uses a big LZMA2 dictionary since it doesn't increase memory usage
  329. # of the kernel decompressor. A BCJ filter is used if it is available for
  330. # the target architecture. xzkern also appends uncompressed size of the data
  331. # using size_append. The .xz format has the size information available at
  332. # the end of the file too, but it's in more complex format and it's good to
  333. # avoid changing the part of the boot code that reads the uncompressed size.
  334. # Note that the bytes added by size_append will make the xz tool think that
  335. # the file is corrupt. This is expected.
  336. #
  337. # xzmisc doesn't use size_append, so it can be used to create normal .xz
  338. # files. xzmisc uses smaller LZMA2 dictionary than xzkern, because a very
  339. # big dictionary would increase the memory usage too much in the multi-call
  340. # decompression mode. A BCJ filter isn't used either.
  341. quiet_cmd_xzkern = XZKERN $@
  342. cmd_xzkern = (cat $(filter-out FORCE,$^) | \
  343. sh $(srctree)/scripts/xz_wrap.sh && \
  344. $(call size_append, $(filter-out FORCE,$^))) > $@ || \
  345. (rm -f $@ ; false)
  346. quiet_cmd_xzmisc = XZMISC $@
  347. cmd_xzmisc = (cat $(filter-out FORCE,$^) | \
  348. xz --check=crc32 --lzma2=dict=1MiB) > $@ || \
  349. (rm -f $@ ; false)
  350. # Additional commands for U-Boot
  351. #
  352. # mkimage
  353. # ---------------------------------------------------------------------------
  354. quiet_cmd_mkimage = MKIMAGE $@
  355. cmd_mkimage = $(objtree)/tools/mkimage $(MKIMAGEFLAGS_$(@F)) -d $< $@ \
  356. $(if $(KBUILD_VERBOSE:1=), >/dev/null)