Makefile.build 21 KB

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