Makefile.build 17 KB

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