Makefile.lib 15 KB

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