Makefile.build 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. ifneq ($(KBUILD_SRC),)
  55. # Create output directory if not already present
  56. _dummy := $(shell [ -d $(obj) ] || mkdir -p $(obj))
  57. # Create directories for object files if directory does not exist
  58. # Needed when obj-y := dir/file.o syntax is used
  59. _dummy := $(foreach d,$(obj-dirs), $(shell [ -d $(d) ] || mkdir -p $(d)))
  60. endif
  61. ifndef obj
  62. $(warning kbuild: Makefile.build is included improperly)
  63. endif
  64. # ===========================================================================
  65. ifneq ($(strip $(lib-y) $(lib-m) $(lib-)),)
  66. lib-target := $(obj)/lib.a
  67. obj-y += $(obj)/lib-ksyms.o
  68. endif
  69. ifneq ($(strip $(obj-y) $(obj-m) $(obj-) $(subdir-m) $(lib-target)),)
  70. builtin-target := $(obj)/built-in.o
  71. endif
  72. modorder-target := $(obj)/modules.order
  73. # We keep a list of all modules in $(MODVERDIR)
  74. __build: $(if $(KBUILD_BUILTIN),$(builtin-target) $(lib-target) $(extra-y)) \
  75. $(if $(KBUILD_MODULES),$(obj-m) $(modorder-target)) \
  76. $(subdir-ym) $(always)
  77. @:
  78. # Linus' kernel sanity checking tool
  79. ifneq ($(KBUILD_CHECKSRC),0)
  80. ifeq ($(KBUILD_CHECKSRC),2)
  81. quiet_cmd_force_checksrc = CHECK $<
  82. cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< ;
  83. else
  84. quiet_cmd_checksrc = CHECK $<
  85. cmd_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< ;
  86. endif
  87. endif
  88. # Do section mismatch analysis for each module/built-in.o
  89. ifdef CONFIG_DEBUG_SECTION_MISMATCH
  90. cmd_secanalysis = ; scripts/mod/modpost $@
  91. endif
  92. # Compile C sources (.c)
  93. # ---------------------------------------------------------------------------
  94. # Default is built-in, unless we know otherwise
  95. modkern_cflags = \
  96. $(if $(part-of-module), \
  97. $(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE), \
  98. $(KBUILD_CFLAGS_KERNEL) $(CFLAGS_KERNEL))
  99. quiet_modtag := $(empty) $(empty)
  100. $(real-objs-m) : part-of-module := y
  101. $(real-objs-m:.o=.i) : part-of-module := y
  102. $(real-objs-m:.o=.s) : part-of-module := y
  103. $(real-objs-m:.o=.lst): part-of-module := y
  104. $(real-objs-m) : quiet_modtag := [M]
  105. $(real-objs-m:.o=.i) : quiet_modtag := [M]
  106. $(real-objs-m:.o=.s) : quiet_modtag := [M]
  107. $(real-objs-m:.o=.lst): quiet_modtag := [M]
  108. $(obj-m) : quiet_modtag := [M]
  109. # Default for not multi-part modules
  110. modname = $(basetarget)
  111. $(multi-objs-m) : modname = $(modname-multi)
  112. $(multi-objs-m:.o=.i) : modname = $(modname-multi)
  113. $(multi-objs-m:.o=.s) : modname = $(modname-multi)
  114. $(multi-objs-m:.o=.lst) : modname = $(modname-multi)
  115. $(multi-objs-y) : modname = $(modname-multi)
  116. $(multi-objs-y:.o=.i) : modname = $(modname-multi)
  117. $(multi-objs-y:.o=.s) : modname = $(modname-multi)
  118. $(multi-objs-y:.o=.lst) : modname = $(modname-multi)
  119. quiet_cmd_cc_s_c = CC $(quiet_modtag) $@
  120. cmd_cc_s_c = $(CC) $(c_flags) $(DISABLE_LTO) -fverbose-asm -S -o $@ $<
  121. $(obj)/%.s: $(src)/%.c FORCE
  122. $(call if_changed_dep,cc_s_c)
  123. quiet_cmd_cpp_i_c = CPP $(quiet_modtag) $@
  124. cmd_cpp_i_c = $(CPP) $(c_flags) -o $@ $<
  125. $(obj)/%.i: $(src)/%.c FORCE
  126. $(call if_changed_dep,cpp_i_c)
  127. cmd_gensymtypes = \
  128. $(CPP) -D__GENKSYMS__ $(c_flags) $< | \
  129. $(GENKSYMS) $(if $(1), -T $(2)) \
  130. $(patsubst y,-s _,$(CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX)) \
  131. $(if $(KBUILD_PRESERVE),-p) \
  132. -r $(firstword $(wildcard $(2:.symtypes=.symref) /dev/null))
  133. quiet_cmd_cc_symtypes_c = SYM $(quiet_modtag) $@
  134. cmd_cc_symtypes_c = \
  135. set -e; \
  136. $(call cmd_gensymtypes,true,$@) >/dev/null; \
  137. test -s $@ || rm -f $@
  138. $(obj)/%.symtypes : $(src)/%.c FORCE
  139. $(call cmd,cc_symtypes_c)
  140. # C (.c) files
  141. # The C file is compiled and updated dependency information is generated.
  142. # (See cmd_cc_o_c + relevant part of rule_cc_o_c)
  143. quiet_cmd_cc_o_c = CC $(quiet_modtag) $@
  144. ifndef CONFIG_MODVERSIONS
  145. cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
  146. else
  147. # When module versioning is enabled the following steps are executed:
  148. # o compile a .tmp_<file>.o from <file>.c
  149. # o if .tmp_<file>.o doesn't contain a __ksymtab version, i.e. does
  150. # not export symbols, we just rename .tmp_<file>.o to <file>.o and
  151. # are done.
  152. # o otherwise, we calculate symbol versions using the good old
  153. # genksyms on the preprocessed source and postprocess them in a way
  154. # that they are usable as a linker script
  155. # o generate <file>.o from .tmp_<file>.o using the linker to
  156. # replace the unresolved symbols __crc_exported_symbol with
  157. # the actual value of the checksum generated by genksyms
  158. cmd_cc_o_c = $(CC) $(c_flags) -c -o $(@D)/.tmp_$(@F) $<
  159. cmd_modversions = \
  160. if $(OBJDUMP) -h $(@D)/.tmp_$(@F) | grep -q __ksymtab; then \
  161. $(call cmd_gensymtypes,$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \
  162. > $(@D)/.tmp_$(@F:.o=.ver); \
  163. \
  164. $(LD) $(LDFLAGS) -r -o $@ $(@D)/.tmp_$(@F) \
  165. -T $(@D)/.tmp_$(@F:.o=.ver); \
  166. rm -f $(@D)/.tmp_$(@F) $(@D)/.tmp_$(@F:.o=.ver); \
  167. else \
  168. mv -f $(@D)/.tmp_$(@F) $@; \
  169. fi;
  170. endif
  171. ifdef CONFIG_FTRACE_MCOUNT_RECORD
  172. ifdef BUILD_C_RECORDMCOUNT
  173. ifeq ("$(origin RECORDMCOUNT_WARN)", "command line")
  174. RECORDMCOUNT_FLAGS = -w
  175. endif
  176. # Due to recursion, we must skip empty.o.
  177. # The empty.o file is created in the make process in order to determine
  178. # the target endianness and word size. It is made before all other C
  179. # files, including recordmcount.
  180. sub_cmd_record_mcount = \
  181. if [ $(@) != "scripts/mod/empty.o" ]; then \
  182. $(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)"; \
  183. fi;
  184. recordmcount_source := $(srctree)/scripts/recordmcount.c \
  185. $(srctree)/scripts/recordmcount.h
  186. else
  187. sub_cmd_record_mcount = set -e ; perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \
  188. "$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)" \
  189. "$(if $(CONFIG_64BIT),64,32)" \
  190. "$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CFLAGS)" \
  191. "$(LD)" "$(NM)" "$(RM)" "$(MV)" \
  192. "$(if $(part-of-module),1,0)" "$(@)";
  193. recordmcount_source := $(srctree)/scripts/recordmcount.pl
  194. endif
  195. cmd_record_mcount = \
  196. if [ "$(findstring $(CC_FLAGS_FTRACE),$(_c_flags))" = \
  197. "$(CC_FLAGS_FTRACE)" ]; then \
  198. $(sub_cmd_record_mcount) \
  199. fi;
  200. endif
  201. ifdef CONFIG_STACK_VALIDATION
  202. ifneq ($(SKIP_STACK_VALIDATION),1)
  203. __objtool_obj := $(objtree)/tools/objtool/objtool
  204. objtool_args = check
  205. ifndef CONFIG_FRAME_POINTER
  206. objtool_args += --no-fp
  207. endif
  208. # 'OBJECT_FILES_NON_STANDARD := y': skip objtool checking for a directory
  209. # 'OBJECT_FILES_NON_STANDARD_foo.o := 'y': skip objtool checking for a file
  210. # 'OBJECT_FILES_NON_STANDARD_foo.o := 'n': override directory skip for a file
  211. cmd_objtool = $(if $(patsubst y%,, \
  212. $(OBJECT_FILES_NON_STANDARD_$(basetarget).o)$(OBJECT_FILES_NON_STANDARD)n), \
  213. $(__objtool_obj) $(objtool_args) "$(@)";)
  214. objtool_obj = $(if $(patsubst y%,, \
  215. $(OBJECT_FILES_NON_STANDARD_$(basetarget).o)$(OBJECT_FILES_NON_STANDARD)n), \
  216. $(__objtool_obj))
  217. endif # SKIP_STACK_VALIDATION
  218. endif # CONFIG_STACK_VALIDATION
  219. define rule_cc_o_c
  220. $(call echo-cmd,checksrc) $(cmd_checksrc) \
  221. $(call cmd_and_fixdep,cc_o_c) \
  222. $(cmd_modversions) \
  223. $(cmd_objtool) \
  224. $(call echo-cmd,record_mcount) $(cmd_record_mcount)
  225. endef
  226. define rule_as_o_S
  227. $(call cmd_and_fixdep,as_o_S) \
  228. $(cmd_objtool)
  229. endef
  230. # List module undefined symbols (or empty line if not enabled)
  231. ifdef CONFIG_TRIM_UNUSED_KSYMS
  232. cmd_undef_syms = $(NM) $@ | sed -n 's/^ \+U //p' | xargs echo
  233. else
  234. cmd_undef_syms = echo
  235. endif
  236. # Built-in and composite module parts
  237. $(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_obj) FORCE
  238. $(call cmd,force_checksrc)
  239. $(call if_changed_rule,cc_o_c)
  240. # Single-part modules are special since we need to mark them in $(MODVERDIR)
  241. $(single-used-m): $(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_obj) FORCE
  242. $(call cmd,force_checksrc)
  243. $(call if_changed_rule,cc_o_c)
  244. @{ echo $(@:.o=.ko); echo $@; \
  245. $(cmd_undef_syms); } > $(MODVERDIR)/$(@F:.o=.mod)
  246. quiet_cmd_cc_lst_c = MKLST $@
  247. cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
  248. $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \
  249. System.map $(OBJDUMP) > $@
  250. $(obj)/%.lst: $(src)/%.c FORCE
  251. $(call if_changed_dep,cc_lst_c)
  252. # Compile assembler sources (.S)
  253. # ---------------------------------------------------------------------------
  254. modkern_aflags := $(KBUILD_AFLAGS_KERNEL) $(AFLAGS_KERNEL)
  255. $(real-objs-m) : modkern_aflags := $(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE)
  256. $(real-objs-m:.o=.s): modkern_aflags := $(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE)
  257. quiet_cmd_cpp_s_S = CPP $(quiet_modtag) $@
  258. cmd_cpp_s_S = $(CPP) $(a_flags) -o $@ $<
  259. $(obj)/%.s: $(src)/%.S FORCE
  260. $(call if_changed_dep,cpp_s_S)
  261. quiet_cmd_as_o_S = AS $(quiet_modtag) $@
  262. cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $<
  263. $(obj)/%.o: $(src)/%.S $(objtool_obj) FORCE
  264. $(call if_changed_rule,as_o_S)
  265. targets += $(real-objs-y) $(real-objs-m) $(lib-y)
  266. targets += $(extra-y) $(MAKECMDGOALS) $(always)
  267. # Linker scripts preprocessor (.lds.S -> .lds)
  268. # ---------------------------------------------------------------------------
  269. quiet_cmd_cpp_lds_S = LDS $@
  270. cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -C -U$(ARCH) \
  271. -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $<
  272. $(obj)/%.lds: $(src)/%.lds.S FORCE
  273. $(call if_changed_dep,cpp_lds_S)
  274. # ASN.1 grammar
  275. # ---------------------------------------------------------------------------
  276. quiet_cmd_asn1_compiler = ASN.1 $@
  277. cmd_asn1_compiler = $(objtree)/scripts/asn1_compiler $< \
  278. $(subst .h,.c,$@) $(subst .c,.h,$@)
  279. .PRECIOUS: $(objtree)/$(obj)/%-asn1.c $(objtree)/$(obj)/%-asn1.h
  280. $(obj)/%-asn1.c $(obj)/%-asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler
  281. $(call cmd,asn1_compiler)
  282. # Build the compiled-in targets
  283. # ---------------------------------------------------------------------------
  284. # To build objects in subdirs, we need to descend into the directories
  285. $(sort $(subdir-obj-y)): $(subdir-ym) ;
  286. #
  287. # Rule to compile a set of .o files into one .o file
  288. #
  289. ifdef builtin-target
  290. ifdef CONFIG_THIN_ARCHIVES
  291. cmd_make_builtin = rm -f $@; $(AR) rcST$(KBUILD_ARFLAGS)
  292. cmd_make_empty_builtin = rm -f $@; $(AR) rcST$(KBUILD_ARFLAGS)
  293. quiet_cmd_link_o_target = AR $@
  294. else
  295. cmd_make_builtin = $(LD) $(ld_flags) -r -o
  296. cmd_make_empty_builtin = rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS)
  297. quiet_cmd_link_o_target = LD $@
  298. endif
  299. # If the list of objects to link is empty, just create an empty built-in.o
  300. cmd_link_o_target = $(if $(strip $(obj-y)),\
  301. $(cmd_make_builtin) $@ $(filter $(obj-y), $^) \
  302. $(cmd_secanalysis),\
  303. $(cmd_make_empty_builtin) $@)
  304. $(builtin-target): $(obj-y) FORCE
  305. $(call if_changed,link_o_target)
  306. targets += $(builtin-target)
  307. endif # builtin-target
  308. #
  309. # Rule to create modules.order file
  310. #
  311. # Create commands to either record .ko file or cat modules.order from
  312. # a subdirectory
  313. modorder-cmds = \
  314. $(foreach m, $(modorder), \
  315. $(if $(filter %/modules.order, $m), \
  316. cat $m;, echo kernel/$m;))
  317. $(modorder-target): $(subdir-ym) FORCE
  318. $(Q)(cat /dev/null; $(modorder-cmds)) > $@
  319. #
  320. # Rule to compile a set of .o files into one .a file
  321. #
  322. ifdef lib-target
  323. quiet_cmd_link_l_target = AR $@
  324. ifdef CONFIG_THIN_ARCHIVES
  325. cmd_link_l_target = rm -f $@; $(AR) rcsT$(KBUILD_ARFLAGS) $@ $(lib-y)
  326. else
  327. cmd_link_l_target = rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@ $(lib-y)
  328. endif
  329. $(lib-target): $(lib-y) FORCE
  330. $(call if_changed,link_l_target)
  331. targets += $(lib-target)
  332. dummy-object = $(obj)/.lib_exports.o
  333. ksyms-lds = $(dot-target).lds
  334. ifdef CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX
  335. ref_prefix = EXTERN(_
  336. else
  337. ref_prefix = EXTERN(
  338. endif
  339. quiet_cmd_export_list = EXPORTS $@
  340. cmd_export_list = $(OBJDUMP) -h $< | \
  341. sed -ne '/___ksymtab/{s/.*+/$(ref_prefix)/;s/ .*/)/;p}' >$(ksyms-lds);\
  342. rm -f $(dummy-object);\
  343. $(AR) rcs$(KBUILD_ARFLAGS) $(dummy-object);\
  344. $(LD) $(ld_flags) -r -o $@ -T $(ksyms-lds) $(dummy-object);\
  345. rm $(dummy-object) $(ksyms-lds)
  346. $(obj)/lib-ksyms.o: $(lib-target) FORCE
  347. $(call if_changed,export_list)
  348. endif
  349. #
  350. # Rule to link composite objects
  351. #
  352. # Composite objects are specified in kbuild makefile as follows:
  353. # <composite-object>-objs := <list of .o files>
  354. # or
  355. # <composite-object>-y := <list of .o files>
  356. # or
  357. # <composite-object>-m := <list of .o files>
  358. # The -m syntax only works if <composite object> is a module
  359. link_multi_deps = \
  360. $(filter $(addprefix $(obj)/, \
  361. $($(subst $(obj)/,,$(@:.o=-objs))) \
  362. $($(subst $(obj)/,,$(@:.o=-y))) \
  363. $($(subst $(obj)/,,$(@:.o=-m)))), $^)
  364. quiet_cmd_link_multi-y = LD $@
  365. cmd_link_multi-y = $(LD) $(ld_flags) -r -o $@ $(link_multi_deps) $(cmd_secanalysis)
  366. quiet_cmd_link_multi-m = LD [M] $@
  367. cmd_link_multi-m = $(cmd_link_multi-y)
  368. $(multi-used-y): FORCE
  369. $(call if_changed,link_multi-y)
  370. $(call multi_depend, $(multi-used-y), .o, -objs -y)
  371. $(multi-used-m): FORCE
  372. $(call if_changed,link_multi-m)
  373. @{ echo $(@:.o=.ko); echo $(link_multi_deps); \
  374. $(cmd_undef_syms); } > $(MODVERDIR)/$(@F:.o=.mod)
  375. $(call multi_depend, $(multi-used-m), .o, -objs -y -m)
  376. targets += $(multi-used-y) $(multi-used-m)
  377. # Descending
  378. # ---------------------------------------------------------------------------
  379. PHONY += $(subdir-ym)
  380. $(subdir-ym):
  381. $(Q)$(MAKE) $(build)=$@
  382. # Add FORCE to the prequisites of a target to force it to be always rebuilt.
  383. # ---------------------------------------------------------------------------
  384. PHONY += FORCE
  385. FORCE:
  386. # Read all saved command lines and dependencies for the $(targets) we
  387. # may be building above, using $(if_changed{,_dep}). As an
  388. # optimization, we don't need to read them if the target does not
  389. # exist, we will rebuild anyway in that case.
  390. targets := $(wildcard $(sort $(targets)))
  391. cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
  392. ifneq ($(cmd_files),)
  393. include $(cmd_files)
  394. endif
  395. # Declare the contents of the .PHONY variable as phony. We keep that
  396. # information in a variable se we can use it in if_changed and friends.
  397. .PHONY: $(PHONY)