Makefile.build 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. # SPDX-License-Identifier: GPL-2.0
  2. # ==========================================================================
  3. # Building
  4. # ==========================================================================
  5. src := $(obj)
  6. PHONY := __build
  7. __build:
  8. # Init all relevant variables used in kbuild files so
  9. # 1) they have correct type
  10. # 2) they do not inherit any value from the environment
  11. obj-y :=
  12. obj-m :=
  13. lib-y :=
  14. lib-m :=
  15. always :=
  16. targets :=
  17. subdir-y :=
  18. subdir-m :=
  19. EXTRA_AFLAGS :=
  20. EXTRA_CFLAGS :=
  21. EXTRA_CPPFLAGS :=
  22. EXTRA_LDFLAGS :=
  23. asflags-y :=
  24. ccflags-y :=
  25. cppflags-y :=
  26. ldflags-y :=
  27. subdir-asflags-y :=
  28. subdir-ccflags-y :=
  29. # Read auto.conf if it exists, otherwise ignore
  30. -include include/config/auto.conf
  31. include scripts/Kbuild.include
  32. # For backward compatibility check that these variables do not change
  33. save-cflags := $(CFLAGS)
  34. # The filename Kbuild has precedence over Makefile
  35. kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
  36. kbuild-file := $(if $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Kbuild,$(kbuild-dir)/Makefile)
  37. include $(kbuild-file)
  38. # If the save-* variables changed error out
  39. ifeq ($(KBUILD_NOPEDANTIC),)
  40. ifneq ("$(save-cflags)","$(CFLAGS)")
  41. $(error CFLAGS was changed in "$(kbuild-file)". Fix it to use ccflags-y)
  42. endif
  43. endif
  44. include scripts/Makefile.lib
  45. ifdef host-progs
  46. ifneq ($(hostprogs-y),$(host-progs))
  47. $(warning kbuild: $(obj)/Makefile - Usage of host-progs is deprecated. Please replace with hostprogs-y!)
  48. hostprogs-y += $(host-progs)
  49. endif
  50. endif
  51. # Do not include host rules unless needed
  52. ifneq ($(hostprogs-y)$(hostprogs-m)$(hostlibs-y)$(hostlibs-m)$(hostcxxlibs-y)$(hostcxxlibs-m),)
  53. include scripts/Makefile.host
  54. endif
  55. ifneq ($(KBUILD_SRC),)
  56. # Create output directory if not already present
  57. _dummy := $(shell [ -d $(obj) ] || mkdir -p $(obj))
  58. # Create directories for object files if directory does not exist
  59. # Needed when obj-y := dir/file.o syntax is used
  60. _dummy := $(foreach d,$(obj-dirs), $(shell [ -d $(d) ] || mkdir -p $(d)))
  61. endif
  62. ifndef obj
  63. $(warning kbuild: Makefile.build is included improperly)
  64. endif
  65. # ===========================================================================
  66. ifneq ($(strip $(lib-y) $(lib-m) $(lib-)),)
  67. lib-target := $(obj)/lib.a
  68. obj-y += $(obj)/lib-ksyms.o
  69. endif
  70. ifneq ($(strip $(obj-y) $(obj-m) $(obj-) $(subdir-m) $(lib-target)),)
  71. builtin-target := $(obj)/built-in.o
  72. endif
  73. modorder-target := $(obj)/modules.order
  74. # We keep a list of all modules in $(MODVERDIR)
  75. __build: $(if $(KBUILD_BUILTIN),$(builtin-target) $(lib-target) $(extra-y)) \
  76. $(if $(KBUILD_MODULES),$(obj-m) $(modorder-target)) \
  77. $(subdir-ym) $(always)
  78. @:
  79. # Linus' kernel sanity checking tool
  80. ifneq ($(KBUILD_CHECKSRC),0)
  81. ifeq ($(KBUILD_CHECKSRC),2)
  82. quiet_cmd_force_checksrc = CHECK $<
  83. cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< ;
  84. else
  85. quiet_cmd_checksrc = CHECK $<
  86. cmd_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< ;
  87. endif
  88. endif
  89. # Do section mismatch analysis for each module/built-in.o
  90. ifdef CONFIG_DEBUG_SECTION_MISMATCH
  91. cmd_secanalysis = ; scripts/mod/modpost $@
  92. endif
  93. # Compile C sources (.c)
  94. # ---------------------------------------------------------------------------
  95. # Default is built-in, unless we know otherwise
  96. modkern_cflags = \
  97. $(if $(part-of-module), \
  98. $(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE), \
  99. $(KBUILD_CFLAGS_KERNEL) $(CFLAGS_KERNEL))
  100. quiet_modtag := $(empty) $(empty)
  101. $(real-objs-m) : part-of-module := y
  102. $(real-objs-m:.o=.i) : part-of-module := y
  103. $(real-objs-m:.o=.s) : part-of-module := y
  104. $(real-objs-m:.o=.lst): part-of-module := y
  105. $(real-objs-m) : quiet_modtag := [M]
  106. $(real-objs-m:.o=.i) : quiet_modtag := [M]
  107. $(real-objs-m:.o=.s) : quiet_modtag := [M]
  108. $(real-objs-m:.o=.lst): quiet_modtag := [M]
  109. $(obj-m) : quiet_modtag := [M]
  110. # Default for not multi-part modules
  111. modname = $(basetarget)
  112. $(multi-objs-m) : modname = $(modname-multi)
  113. $(multi-objs-m:.o=.i) : modname = $(modname-multi)
  114. $(multi-objs-m:.o=.s) : modname = $(modname-multi)
  115. $(multi-objs-m:.o=.lst) : modname = $(modname-multi)
  116. $(multi-objs-y) : modname = $(modname-multi)
  117. $(multi-objs-y:.o=.i) : modname = $(modname-multi)
  118. $(multi-objs-y:.o=.s) : modname = $(modname-multi)
  119. $(multi-objs-y:.o=.lst) : modname = $(modname-multi)
  120. quiet_cmd_cc_s_c = CC $(quiet_modtag) $@
  121. cmd_cc_s_c = $(CC) $(c_flags) $(DISABLE_LTO) -fverbose-asm -S -o $@ $<
  122. $(obj)/%.s: $(src)/%.c FORCE
  123. $(call if_changed_dep,cc_s_c)
  124. quiet_cmd_cpp_i_c = CPP $(quiet_modtag) $@
  125. cmd_cpp_i_c = $(CPP) $(c_flags) -o $@ $<
  126. $(obj)/%.i: $(src)/%.c FORCE
  127. $(call if_changed_dep,cpp_i_c)
  128. # These mirror gensymtypes_S and co below, keep them in synch.
  129. cmd_gensymtypes_c = \
  130. $(CPP) -D__GENKSYMS__ $(c_flags) $< | \
  131. $(GENKSYMS) $(if $(1), -T $(2)) \
  132. $(patsubst y,-s _,$(CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX)) \
  133. $(patsubst y,-R,$(CONFIG_MODULE_REL_CRCS)) \
  134. $(if $(KBUILD_PRESERVE),-p) \
  135. -r $(firstword $(wildcard $(2:.symtypes=.symref) /dev/null))
  136. quiet_cmd_cc_symtypes_c = SYM $(quiet_modtag) $@
  137. cmd_cc_symtypes_c = \
  138. set -e; \
  139. $(call cmd_gensymtypes_c,true,$@) >/dev/null; \
  140. test -s $@ || rm -f $@
  141. $(obj)/%.symtypes : $(src)/%.c FORCE
  142. $(call cmd,cc_symtypes_c)
  143. # LLVM assembly
  144. # Generate .ll files from .c
  145. quiet_cmd_cc_ll_c = CC $(quiet_modtag) $@
  146. cmd_cc_ll_c = $(CC) $(c_flags) -emit-llvm -S -o $@ $<
  147. $(obj)/%.ll: $(src)/%.c FORCE
  148. $(call if_changed_dep,cc_ll_c)
  149. # C (.c) files
  150. # The C file is compiled and updated dependency information is generated.
  151. # (See cmd_cc_o_c + relevant part of rule_cc_o_c)
  152. quiet_cmd_cc_o_c = CC $(quiet_modtag) $@
  153. ifndef CONFIG_MODVERSIONS
  154. cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
  155. else
  156. # When module versioning is enabled the following steps are executed:
  157. # o compile a .tmp_<file>.o from <file>.c
  158. # o if .tmp_<file>.o doesn't contain a __ksymtab version, i.e. does
  159. # not export symbols, we just rename .tmp_<file>.o to <file>.o and
  160. # are done.
  161. # o otherwise, we calculate symbol versions using the good old
  162. # genksyms on the preprocessed source and postprocess them in a way
  163. # that they are usable as a linker script
  164. # o generate <file>.o from .tmp_<file>.o using the linker to
  165. # replace the unresolved symbols __crc_exported_symbol with
  166. # the actual value of the checksum generated by genksyms
  167. cmd_cc_o_c = $(CC) $(c_flags) -c -o $(@D)/.tmp_$(@F) $<
  168. ifdef CONFIG_LTO_CLANG
  169. # Generate .o.symversions files for each .o with exported symbols, and link these
  170. # to the kernel and/or modules at the end.
  171. cmd_modversions_c = \
  172. if $(OBJDUMP) -h $(@D)/.tmp_$(@F) >/dev/null 2>/dev/null; then \
  173. if $(OBJDUMP) -h $(@D)/.tmp_$(@F) | grep -q __ksymtab; then \
  174. $(call cmd_gensymtypes_c,$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \
  175. > $(@D)/$(@F).symversions; \
  176. fi; \
  177. else \
  178. if $(LLVM_NM) $(@D)/.tmp_$(@F) | grep -q __ksymtab; then \
  179. $(call cmd_gensymtypes_c,$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \
  180. > $(@D)/$(@F).symversions; \
  181. fi; \
  182. fi; \
  183. mv -f $(@D)/.tmp_$(@F) $@;
  184. else
  185. cmd_modversions_c = \
  186. if $(OBJDUMP) -h $(@D)/.tmp_$(@F) | grep -q __ksymtab; then \
  187. $(call cmd_gensymtypes_c,$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \
  188. > $(@D)/.tmp_$(@F:.o=.ver); \
  189. \
  190. $(LD) $(LDFLAGS) -r -o $@ $(@D)/.tmp_$(@F) \
  191. -T $(@D)/.tmp_$(@F:.o=.ver); \
  192. rm -f $(@D)/.tmp_$(@F) $(@D)/.tmp_$(@F:.o=.ver); \
  193. else \
  194. mv -f $(@D)/.tmp_$(@F) $@; \
  195. fi;
  196. endif
  197. endif
  198. ifdef CONFIG_FTRACE_MCOUNT_RECORD
  199. ifndef CC_USING_RECORD_MCOUNT
  200. # compiler will not generate __mcount_loc use recordmcount or recordmcount.pl
  201. ifdef BUILD_C_RECORDMCOUNT
  202. ifeq ("$(origin RECORDMCOUNT_WARN)", "command line")
  203. RECORDMCOUNT_FLAGS = -w
  204. endif
  205. ifdef CONFIG_LTO_CLANG
  206. # With LTO, we postpone running recordmcount until after the LTO link step, so
  207. # let's export the parameters for the link script.
  208. export RECORDMCOUNT_FLAGS
  209. else
  210. # Due to recursion, we must skip empty.o.
  211. # The empty.o file is created in the make process in order to determine
  212. # the target endianness and word size. It is made before all other C
  213. # files, including recordmcount.
  214. sub_cmd_record_mcount = \
  215. if [ $(@) != "scripts/mod/empty.o" ]; then \
  216. $(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)"; \
  217. fi;
  218. endif
  219. recordmcount_source := $(srctree)/scripts/recordmcount.c \
  220. $(srctree)/scripts/recordmcount.h
  221. else # !BUILD_C_RECORDMCOUNT
  222. sub_cmd_record_mcount = set -e ; perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \
  223. "$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)" \
  224. "$(if $(CONFIG_64BIT),64,32)" \
  225. "$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS)" \
  226. "$(LD)" "$(NM)" "$(RM)" "$(MV)" \
  227. "$(if $(part-of-module),1,0)" "$(@)";
  228. recordmcount_source := $(srctree)/scripts/recordmcount.pl
  229. endif # BUILD_C_RECORDMCOUNT
  230. ifndef CONFIG_LTO_CLANG
  231. cmd_record_mcount = \
  232. if [ "$(findstring $(CC_FLAGS_FTRACE),$(_c_flags))" = \
  233. "$(CC_FLAGS_FTRACE)" ]; then \
  234. $(sub_cmd_record_mcount) \
  235. fi;
  236. endif
  237. endif # CC_USING_RECORD_MCOUNT
  238. endif # CONFIG_FTRACE_MCOUNT_RECORD
  239. ifdef CONFIG_STACK_VALIDATION
  240. ifneq ($(SKIP_STACK_VALIDATION),1)
  241. __objtool_obj := $(objtree)/tools/objtool/objtool
  242. objtool_args = $(if $(CONFIG_UNWINDER_ORC),orc generate,check)
  243. objtool_args += $(if $(part-of-module), --module,)
  244. ifndef CONFIG_FRAME_POINTER
  245. objtool_args += --no-fp
  246. endif
  247. ifdef CONFIG_GCOV_KERNEL
  248. objtool_args += --no-unreachable
  249. else
  250. objtool_args += $(call cc-ifversion, -lt, 0405, --no-unreachable)
  251. endif
  252. ifdef CONFIG_RETPOLINE
  253. objtool_args += --retpoline
  254. endif
  255. ifdef CONFIG_MODVERSIONS
  256. objtool_o = $(@D)/.tmp_$(@F)
  257. else
  258. objtool_o = $(@)
  259. endif
  260. # 'OBJECT_FILES_NON_STANDARD := y': skip objtool checking for a directory
  261. # 'OBJECT_FILES_NON_STANDARD_foo.o := 'y': skip objtool checking for a file
  262. # 'OBJECT_FILES_NON_STANDARD_foo.o := 'n': override directory skip for a file
  263. cmd_objtool = $(if $(patsubst y%,, \
  264. $(OBJECT_FILES_NON_STANDARD_$(basetarget).o)$(OBJECT_FILES_NON_STANDARD)n), \
  265. $(__objtool_obj) $(objtool_args) "$(objtool_o)";)
  266. objtool_obj = $(if $(patsubst y%,, \
  267. $(OBJECT_FILES_NON_STANDARD_$(basetarget).o)$(OBJECT_FILES_NON_STANDARD)n), \
  268. $(__objtool_obj))
  269. endif # SKIP_STACK_VALIDATION
  270. endif # CONFIG_STACK_VALIDATION
  271. # Rebuild all objects when objtool changes, or is enabled/disabled.
  272. objtool_dep = $(objtool_obj) \
  273. $(wildcard include/config/orc/unwinder.h \
  274. include/config/stack/validation.h)
  275. define rule_cc_o_c
  276. $(call echo-cmd,checksrc) $(cmd_checksrc) \
  277. $(call cmd_and_fixdep,cc_o_c) \
  278. $(call echo-cmd,objtool) $(cmd_objtool) \
  279. $(cmd_modversions_c) \
  280. $(call echo-cmd,record_mcount) $(cmd_record_mcount)
  281. endef
  282. define rule_as_o_S
  283. $(call cmd_and_fixdep,as_o_S) \
  284. $(call echo-cmd,objtool) $(cmd_objtool) \
  285. $(cmd_modversions_S)
  286. endef
  287. # List module undefined symbols (or empty line if not enabled)
  288. ifdef CONFIG_TRIM_UNUSED_KSYMS
  289. cmd_undef_syms = $(NM) $@ | sed -n 's/^ \+U //p' | xargs echo
  290. else
  291. cmd_undef_syms = echo
  292. endif
  293. # Built-in and composite module parts
  294. $(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_dep) FORCE
  295. $(call cmd,force_checksrc)
  296. $(call if_changed_rule,cc_o_c)
  297. # Single-part modules are special since we need to mark them in $(MODVERDIR)
  298. $(single-used-m): $(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_dep) FORCE
  299. $(call cmd,force_checksrc)
  300. $(call if_changed_rule,cc_o_c)
  301. @{ echo $(@:.o=.ko); echo $@; \
  302. $(cmd_undef_syms); } > $(MODVERDIR)/$(@F:.o=.mod)
  303. quiet_cmd_cc_lst_c = MKLST $@
  304. cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
  305. $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \
  306. System.map $(OBJDUMP) > $@
  307. $(obj)/%.lst: $(src)/%.c FORCE
  308. $(call if_changed_dep,cc_lst_c)
  309. # Compile assembler sources (.S)
  310. # ---------------------------------------------------------------------------
  311. modkern_aflags := $(KBUILD_AFLAGS_KERNEL) $(AFLAGS_KERNEL)
  312. $(real-objs-m) : modkern_aflags := $(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE)
  313. $(real-objs-m:.o=.s): modkern_aflags := $(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE)
  314. # .S file exports must have their C prototypes defined in asm/asm-prototypes.h
  315. # or a file that it includes, in order to get versioned symbols. We build a
  316. # dummy C file that includes asm-prototypes and the EXPORT_SYMBOL lines from
  317. # the .S file (with trailing ';'), and run genksyms on that, to extract vers.
  318. #
  319. # This is convoluted. The .S file must first be preprocessed to run guards and
  320. # expand names, then the resulting exports must be constructed into plain
  321. # EXPORT_SYMBOL(symbol); to build our dummy C file, and that gets preprocessed
  322. # to make the genksyms input.
  323. #
  324. # These mirror gensymtypes_c and co above, keep them in synch.
  325. cmd_gensymtypes_S = \
  326. (echo "\#include <linux/kernel.h>" ; \
  327. echo "\#include <asm/asm-prototypes.h>" ; \
  328. $(CPP) $(a_flags) $< | \
  329. grep "\<___EXPORT_SYMBOL\>" | \
  330. sed 's/.*___EXPORT_SYMBOL[[:space:]]*\([a-zA-Z0-9_]*\)[[:space:]]*,.*/EXPORT_SYMBOL(\1);/' ) | \
  331. $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | \
  332. $(GENKSYMS) $(if $(1), -T $(2)) \
  333. $(patsubst y,-s _,$(CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX)) \
  334. $(patsubst y,-R,$(CONFIG_MODULE_REL_CRCS)) \
  335. $(if $(KBUILD_PRESERVE),-p) \
  336. -r $(firstword $(wildcard $(2:.symtypes=.symref) /dev/null))
  337. quiet_cmd_cc_symtypes_S = SYM $(quiet_modtag) $@
  338. cmd_cc_symtypes_S = \
  339. set -e; \
  340. $(call cmd_gensymtypes_S,true,$@) >/dev/null; \
  341. test -s $@ || rm -f $@
  342. $(obj)/%.symtypes : $(src)/%.S FORCE
  343. $(call cmd,cc_symtypes_S)
  344. quiet_cmd_cpp_s_S = CPP $(quiet_modtag) $@
  345. cmd_cpp_s_S = $(CPP) $(a_flags) -o $@ $<
  346. $(obj)/%.s: $(src)/%.S FORCE
  347. $(call if_changed_dep,cpp_s_S)
  348. quiet_cmd_as_o_S = AS $(quiet_modtag) $@
  349. ifndef CONFIG_MODVERSIONS
  350. cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $<
  351. else
  352. ASM_PROTOTYPES := $(wildcard $(srctree)/arch/$(SRCARCH)/include/asm/asm-prototypes.h)
  353. ifeq ($(ASM_PROTOTYPES),)
  354. cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $<
  355. else
  356. # versioning matches the C process described above, with difference that
  357. # we parse asm-prototypes.h C header to get function definitions.
  358. cmd_as_o_S = $(CC) $(a_flags) -c -o $(@D)/.tmp_$(@F) $<
  359. cmd_modversions_S = \
  360. if $(OBJDUMP) -h $(@D)/.tmp_$(@F) | grep -q __ksymtab; then \
  361. $(call cmd_gensymtypes_S,$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \
  362. > $(@D)/.tmp_$(@F:.o=.ver); \
  363. \
  364. $(LD) $(LDFLAGS) -r -o $@ $(@D)/.tmp_$(@F) \
  365. -T $(@D)/.tmp_$(@F:.o=.ver); \
  366. rm -f $(@D)/.tmp_$(@F) $(@D)/.tmp_$(@F:.o=.ver); \
  367. else \
  368. mv -f $(@D)/.tmp_$(@F) $@; \
  369. fi;
  370. endif
  371. endif
  372. $(obj)/%.o: $(src)/%.S $(objtool_dep) FORCE
  373. $(call if_changed_rule,as_o_S)
  374. targets += $(real-objs-y) $(real-objs-m) $(lib-y)
  375. targets += $(extra-y) $(MAKECMDGOALS) $(always)
  376. # Linker scripts preprocessor (.lds.S -> .lds)
  377. # ---------------------------------------------------------------------------
  378. quiet_cmd_cpp_lds_S = LDS $@
  379. cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH) \
  380. -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $<
  381. $(obj)/%.lds: $(src)/%.lds.S FORCE
  382. $(call if_changed_dep,cpp_lds_S)
  383. # ASN.1 grammar
  384. # ---------------------------------------------------------------------------
  385. quiet_cmd_asn1_compiler = ASN.1 $@
  386. cmd_asn1_compiler = $(objtree)/scripts/asn1_compiler $< \
  387. $(subst .h,.c,$@) $(subst .c,.h,$@)
  388. .PRECIOUS: $(objtree)/$(obj)/%-asn1.c $(objtree)/$(obj)/%-asn1.h
  389. $(obj)/%-asn1.c $(obj)/%-asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler
  390. $(call cmd,asn1_compiler)
  391. # Build the compiled-in targets
  392. # ---------------------------------------------------------------------------
  393. # To build objects in subdirs, we need to descend into the directories
  394. $(sort $(subdir-obj-y)): $(subdir-ym) ;
  395. #
  396. # Rule to compile a set of .o files into one .o file
  397. #
  398. ifdef builtin-target
  399. ifdef CONFIG_LTO_CLANG
  400. ifdef CONFIG_MODVERSIONS
  401. # combine symversions for later processing
  402. update_lto_symversions = \
  403. rm -f $@.symversions; \
  404. for i in $(filter-out FORCE,$^); do \
  405. if [ -f $$i.symversions ]; then \
  406. cat $$i.symversions \
  407. >> $@.symversions; \
  408. fi; \
  409. done;
  410. endif
  411. # rebuild the symbol table with llvm-ar to include IR files
  412. update_lto_symtable = ; \
  413. mv -f $@ $@.tmp; \
  414. $(LLVM_AR) rcsT$(KBUILD_ARFLAGS) $@ \
  415. $$($(AR) t $@.tmp); \
  416. rm -f $@.tmp
  417. endif
  418. ifdef CONFIG_THIN_ARCHIVES
  419. cmd_make_builtin = $(update_lto_symversions) \
  420. rm -f $@; $(AR) rcSTP$(KBUILD_ARFLAGS)
  421. cmd_make_empty_builtin = rm -f $@; $(AR) rcSTP$(KBUILD_ARFLAGS)
  422. quiet_cmd_link_o_target = AR $@
  423. else
  424. cmd_make_builtin = $(LD) $(ld_flags) -r -o
  425. cmd_make_empty_builtin = rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS)
  426. quiet_cmd_link_o_target = LD $@
  427. endif
  428. # If the list of objects to link is empty, just create an empty built-in.o
  429. cmd_link_o_target = $(if $(strip $(obj-y)),\
  430. $(cmd_make_builtin) $@ $(filter $(obj-y), $^) \
  431. $(cmd_secanalysis),\
  432. $(cmd_make_empty_builtin) $@)
  433. $(builtin-target): $(obj-y) FORCE
  434. $(call if_changed,link_o_target)
  435. targets += $(builtin-target)
  436. endif # builtin-target
  437. #
  438. # Rule to create modules.order file
  439. #
  440. # Create commands to either record .ko file or cat modules.order from
  441. # a subdirectory
  442. modorder-cmds = \
  443. $(foreach m, $(modorder), \
  444. $(if $(filter %/modules.order, $m), \
  445. cat $m;, echo kernel/$m;))
  446. $(modorder-target): $(subdir-ym) FORCE
  447. $(Q)(cat /dev/null; $(modorder-cmds)) > $@
  448. #
  449. # Rule to compile a set of .o files into one .a file
  450. #
  451. ifdef lib-target
  452. quiet_cmd_link_l_target = AR $@
  453. ifdef CONFIG_THIN_ARCHIVES
  454. cmd_link_l_target = \
  455. $(update_lto_symversions) \
  456. rm -f $@; \
  457. $(AR) rcsTP$(KBUILD_ARFLAGS) $@ $(lib-y) \
  458. $(update_lto_symtable)
  459. else
  460. cmd_link_l_target = rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@ $(lib-y)
  461. endif
  462. $(lib-target): $(lib-y) FORCE
  463. $(call if_changed,link_l_target)
  464. targets += $(lib-target)
  465. dummy-object = $(obj)/.lib_exports.o
  466. ksyms-lds = $(dot-target).lds
  467. ifdef CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX
  468. ref_prefix = EXTERN(_
  469. else
  470. ref_prefix = EXTERN(
  471. endif
  472. filter_export_list = sed -ne '/___ksymtab/s/.*+\([^ "]*\).*/$(ref_prefix)\1)/p'
  473. link_export_list = rm -f $(dummy-object);\
  474. echo | $(CC) $(a_flags) -c -o $(dummy-object) -x assembler -;\
  475. $(LD) $(ld_flags) -r -o $@ -T $(ksyms-lds) $(dummy-object);\
  476. rm $(dummy-object) $(ksyms-lds)
  477. quiet_cmd_export_list = EXPORTS $@
  478. ifdef CONFIG_LTO_CLANG
  479. # objdump doesn't understand IR files and llvm-dis doesn't support archives,
  480. # so we'll walk through each file in the archive separately
  481. cmd_export_list = \
  482. rm -f $(ksyms-lds); \
  483. for o in $$($(AR) t $<); do \
  484. if $(OBJDUMP) -h $$o >/dev/null 2>/dev/null; then \
  485. $(OBJDUMP) -h $$o | \
  486. $(filter_export_list) \
  487. >>$(ksyms-lds); \
  488. else \
  489. $(LLVM_NM) $$o | \
  490. $(filter_export_list) \
  491. >>$(ksyms-lds); \
  492. fi; \
  493. done; \
  494. $(link_export_list)
  495. else
  496. cmd_export_list = $(OBJDUMP) -h $< | $(filter_export_list) >$(ksyms-lds); \
  497. $(link_export_list)
  498. endif
  499. $(obj)/lib-ksyms.o: $(lib-target) FORCE
  500. $(call if_changed,export_list)
  501. targets += $(obj)/lib-ksyms.o
  502. endif
  503. #
  504. # Rule to link composite objects
  505. #
  506. # Composite objects are specified in kbuild makefile as follows:
  507. # <composite-object>-objs := <list of .o files>
  508. # or
  509. # <composite-object>-y := <list of .o files>
  510. # or
  511. # <composite-object>-m := <list of .o files>
  512. # The -m syntax only works if <composite object> is a module
  513. link_multi_deps = \
  514. $(filter $(addprefix $(obj)/, \
  515. $($(subst $(obj)/,,$(@:.o=-objs))) \
  516. $($(subst $(obj)/,,$(@:.o=-y))) \
  517. $($(subst $(obj)/,,$(@:.o=-m)))), $^)
  518. cmd_link_multi-link = $(LD) $(ld_flags) -r -o $@ $(link_multi_deps) $(cmd_secanalysis)
  519. ifdef CONFIG_THIN_ARCHIVES
  520. quiet_cmd_link_multi-y = AR $@
  521. cmd_link_multi-y = $(update_lto_symversions) \
  522. rm -f $@; $(AR) rcSTP$(KBUILD_ARFLAGS) $@ $(link_multi_deps) \
  523. $(update_lto_symtable)
  524. else
  525. quiet_cmd_link_multi-y = LD $@
  526. cmd_link_multi-y = $(cmd_link_multi-link)
  527. endif
  528. quiet_cmd_link_multi-m = LD [M] $@
  529. ifdef CONFIG_LTO_CLANG
  530. # don't compile IR until needed
  531. cmd_link_multi-m = $(cmd_link_multi-y)
  532. else
  533. cmd_link_multi-m = $(cmd_link_multi-link)
  534. endif
  535. $(multi-used-y): FORCE
  536. $(call if_changed,link_multi-y)
  537. $(multi-used-m): FORCE
  538. $(call if_changed,link_multi-m)
  539. @{ echo $(@:.o=.ko); echo $(link_multi_deps); \
  540. $(cmd_undef_syms); } > $(MODVERDIR)/$(@F:.o=.mod)
  541. $(call multi_depend, $(multi-used-y), .o, -objs -y)
  542. $(call multi_depend, $(multi-used-m), .o, -objs -y -m)
  543. targets += $(multi-used-y) $(multi-used-m)
  544. # Descending
  545. # ---------------------------------------------------------------------------
  546. PHONY += $(subdir-ym)
  547. $(subdir-ym):
  548. $(Q)$(MAKE) $(build)=$@
  549. # Add FORCE to the prequisites of a target to force it to be always rebuilt.
  550. # ---------------------------------------------------------------------------
  551. PHONY += FORCE
  552. FORCE:
  553. # Read all saved command lines and dependencies for the $(targets) we
  554. # may be building above, using $(if_changed{,_dep}). As an
  555. # optimization, we don't need to read them if the target does not
  556. # exist, we will rebuild anyway in that case.
  557. targets := $(wildcard $(sort $(targets)))
  558. cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
  559. ifneq ($(cmd_files),)
  560. include $(cmd_files)
  561. endif
  562. # Declare the contents of the .PHONY variable as phony. We keep that
  563. # information in a variable se we can use it in if_changed and friends.
  564. .PHONY: $(PHONY)