Makefile.build 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 EXTRA_CFLAGS)
  41. endif
  42. endif
  43. #
  44. # make W=1 settings
  45. #
  46. # $(call cc-option... ) handles gcc -W.. options which
  47. # are not supported by all versions of the compiler
  48. ifdef KBUILD_ENABLE_EXTRA_GCC_CHECKS
  49. KBUILD_EXTRA_WARNINGS := -Wextra
  50. KBUILD_EXTRA_WARNINGS += -Wunused -Wno-unused-parameter
  51. KBUILD_EXTRA_WARNINGS += -Waggregate-return
  52. KBUILD_EXTRA_WARNINGS += -Wbad-function-cast
  53. KBUILD_EXTRA_WARNINGS += -Wcast-qual
  54. KBUILD_EXTRA_WARNINGS += -Wcast-align
  55. KBUILD_EXTRA_WARNINGS += -Wconversion
  56. KBUILD_EXTRA_WARNINGS += -Wdisabled-optimization
  57. KBUILD_EXTRA_WARNINGS += -Wlogical-op
  58. KBUILD_EXTRA_WARNINGS += -Wmissing-declarations
  59. KBUILD_EXTRA_WARNINGS += -Wmissing-format-attribute
  60. KBUILD_EXTRA_WARNINGS += $(call cc-option, -Wmissing-include-dirs,)
  61. KBUILD_EXTRA_WARNINGS += -Wmissing-prototypes
  62. KBUILD_EXTRA_WARNINGS += -Wnested-externs
  63. KBUILD_EXTRA_WARNINGS += -Wold-style-definition
  64. KBUILD_EXTRA_WARNINGS += $(call cc-option, -Woverlength-strings,)
  65. KBUILD_EXTRA_WARNINGS += -Wpacked
  66. KBUILD_EXTRA_WARNINGS += -Wpacked-bitfield-compat
  67. KBUILD_EXTRA_WARNINGS += -Wpadded
  68. KBUILD_EXTRA_WARNINGS += -Wpointer-arith
  69. KBUILD_EXTRA_WARNINGS += -Wredundant-decls
  70. KBUILD_EXTRA_WARNINGS += -Wshadow
  71. KBUILD_EXTRA_WARNINGS += -Wswitch-default
  72. KBUILD_EXTRA_WARNINGS += $(call cc-option, -Wvla,)
  73. KBUILD_CFLAGS += $(KBUILD_EXTRA_WARNINGS)
  74. endif
  75. include scripts/Makefile.lib
  76. ifdef host-progs
  77. ifneq ($(hostprogs-y),$(host-progs))
  78. $(warning kbuild: $(obj)/Makefile - Usage of host-progs is deprecated. Please replace with hostprogs-y!)
  79. hostprogs-y += $(host-progs)
  80. endif
  81. endif
  82. # Do not include host rules unless needed
  83. ifneq ($(hostprogs-y)$(hostprogs-m),)
  84. include scripts/Makefile.host
  85. endif
  86. ifneq ($(KBUILD_SRC),)
  87. # Create output directory if not already present
  88. _dummy := $(shell [ -d $(obj) ] || mkdir -p $(obj))
  89. # Create directories for object files if directory does not exist
  90. # Needed when obj-y := dir/file.o syntax is used
  91. _dummy := $(foreach d,$(obj-dirs), $(shell [ -d $(d) ] || mkdir -p $(d)))
  92. endif
  93. ifndef obj
  94. $(warning kbuild: Makefile.build is included improperly)
  95. endif
  96. # ===========================================================================
  97. ifneq ($(strip $(lib-y) $(lib-m) $(lib-n) $(lib-)),)
  98. lib-target := $(obj)/lib.a
  99. endif
  100. ifneq ($(strip $(obj-y) $(obj-m) $(obj-n) $(obj-) $(subdir-m) $(lib-target)),)
  101. builtin-target := $(obj)/built-in.o
  102. endif
  103. modorder-target := $(obj)/modules.order
  104. # We keep a list of all modules in $(MODVERDIR)
  105. __build: $(if $(KBUILD_BUILTIN),$(builtin-target) $(lib-target) $(extra-y)) \
  106. $(if $(KBUILD_MODULES),$(obj-m) $(modorder-target)) \
  107. $(subdir-ym) $(always)
  108. @:
  109. # Linus' kernel sanity checking tool
  110. ifneq ($(KBUILD_CHECKSRC),0)
  111. ifeq ($(KBUILD_CHECKSRC),2)
  112. quiet_cmd_force_checksrc = CHECK $<
  113. cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< ;
  114. else
  115. quiet_cmd_checksrc = CHECK $<
  116. cmd_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< ;
  117. endif
  118. endif
  119. # Do section mismatch analysis for each module/built-in.o
  120. ifdef CONFIG_DEBUG_SECTION_MISMATCH
  121. cmd_secanalysis = ; scripts/mod/modpost $@
  122. endif
  123. # Compile C sources (.c)
  124. # ---------------------------------------------------------------------------
  125. # Default is built-in, unless we know otherwise
  126. modkern_cflags = \
  127. $(if $(part-of-module), \
  128. $(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE), \
  129. $(KBUILD_CFLAGS_KERNEL) $(CFLAGS_KERNEL))
  130. quiet_modtag := $(empty) $(empty)
  131. $(real-objs-m) : part-of-module := y
  132. $(real-objs-m:.o=.i) : part-of-module := y
  133. $(real-objs-m:.o=.s) : part-of-module := y
  134. $(real-objs-m:.o=.lst): part-of-module := y
  135. $(real-objs-m) : quiet_modtag := [M]
  136. $(real-objs-m:.o=.i) : quiet_modtag := [M]
  137. $(real-objs-m:.o=.s) : quiet_modtag := [M]
  138. $(real-objs-m:.o=.lst): quiet_modtag := [M]
  139. $(obj-m) : quiet_modtag := [M]
  140. # Default for not multi-part modules
  141. modname = $(basetarget)
  142. $(multi-objs-m) : modname = $(modname-multi)
  143. $(multi-objs-m:.o=.i) : modname = $(modname-multi)
  144. $(multi-objs-m:.o=.s) : modname = $(modname-multi)
  145. $(multi-objs-m:.o=.lst) : modname = $(modname-multi)
  146. $(multi-objs-y) : modname = $(modname-multi)
  147. $(multi-objs-y:.o=.i) : modname = $(modname-multi)
  148. $(multi-objs-y:.o=.s) : modname = $(modname-multi)
  149. $(multi-objs-y:.o=.lst) : modname = $(modname-multi)
  150. quiet_cmd_cc_s_c = CC $(quiet_modtag) $@
  151. cmd_cc_s_c = $(CC) $(c_flags) -fverbose-asm -S -o $@ $<
  152. $(obj)/%.s: $(src)/%.c FORCE
  153. $(call if_changed_dep,cc_s_c)
  154. quiet_cmd_cc_i_c = CPP $(quiet_modtag) $@
  155. cmd_cc_i_c = $(CPP) $(c_flags) -o $@ $<
  156. $(obj)/%.i: $(src)/%.c FORCE
  157. $(call if_changed_dep,cc_i_c)
  158. cmd_gensymtypes = \
  159. $(CPP) -D__GENKSYMS__ $(c_flags) $< | \
  160. $(GENKSYMS) $(if $(1), -T $(2)) -a $(ARCH) \
  161. $(if $(KBUILD_PRESERVE),-p) \
  162. -r $(firstword $(wildcard $(2:.symtypes=.symref) /dev/null))
  163. quiet_cmd_cc_symtypes_c = SYM $(quiet_modtag) $@
  164. cmd_cc_symtypes_c = \
  165. set -e; \
  166. $(call cmd_gensymtypes,true,$@) >/dev/null; \
  167. test -s $@ || rm -f $@
  168. $(obj)/%.symtypes : $(src)/%.c FORCE
  169. $(call cmd,cc_symtypes_c)
  170. # C (.c) files
  171. # The C file is compiled and updated dependency information is generated.
  172. # (See cmd_cc_o_c + relevant part of rule_cc_o_c)
  173. quiet_cmd_cc_o_c = CC $(quiet_modtag) $@
  174. ifndef CONFIG_MODVERSIONS
  175. cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
  176. else
  177. # When module versioning is enabled the following steps are executed:
  178. # o compile a .tmp_<file>.o from <file>.c
  179. # o if .tmp_<file>.o doesn't contain a __ksymtab version, i.e. does
  180. # not export symbols, we just rename .tmp_<file>.o to <file>.o and
  181. # are done.
  182. # o otherwise, we calculate symbol versions using the good old
  183. # genksyms on the preprocessed source and postprocess them in a way
  184. # that they are usable as a linker script
  185. # o generate <file>.o from .tmp_<file>.o using the linker to
  186. # replace the unresolved symbols __crc_exported_symbol with
  187. # the actual value of the checksum generated by genksyms
  188. cmd_cc_o_c = $(CC) $(c_flags) -c -o $(@D)/.tmp_$(@F) $<
  189. cmd_modversions = \
  190. if $(OBJDUMP) -h $(@D)/.tmp_$(@F) | grep -q __ksymtab; then \
  191. $(call cmd_gensymtypes,$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \
  192. > $(@D)/.tmp_$(@F:.o=.ver); \
  193. \
  194. $(LD) $(LDFLAGS) -r -o $@ $(@D)/.tmp_$(@F) \
  195. -T $(@D)/.tmp_$(@F:.o=.ver); \
  196. rm -f $(@D)/.tmp_$(@F) $(@D)/.tmp_$(@F:.o=.ver); \
  197. else \
  198. mv -f $(@D)/.tmp_$(@F) $@; \
  199. fi;
  200. endif
  201. ifdef CONFIG_FTRACE_MCOUNT_RECORD
  202. ifdef BUILD_C_RECORDMCOUNT
  203. # Due to recursion, we must skip empty.o.
  204. # The empty.o file is created in the make process in order to determine
  205. # the target endianness and word size. It is made before all other C
  206. # files, including recordmcount.
  207. sub_cmd_record_mcount = \
  208. if [ $(@) != "scripts/mod/empty.o" ]; then \
  209. $(objtree)/scripts/recordmcount "$(@)"; \
  210. fi;
  211. else
  212. sub_cmd_record_mcount = set -e ; perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \
  213. "$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)" \
  214. "$(if $(CONFIG_64BIT),64,32)" \
  215. "$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CFLAGS)" \
  216. "$(LD)" "$(NM)" "$(RM)" "$(MV)" \
  217. "$(if $(part-of-module),1,0)" "$(@)";
  218. endif
  219. cmd_record_mcount = \
  220. if [ "$(findstring -pg,$(_c_flags))" = "-pg" ]; then \
  221. $(sub_cmd_record_mcount) \
  222. fi;
  223. endif
  224. define rule_cc_o_c
  225. $(call echo-cmd,checksrc) $(cmd_checksrc) \
  226. $(call echo-cmd,cc_o_c) $(cmd_cc_o_c); \
  227. $(cmd_modversions) \
  228. $(call echo-cmd,record_mcount) \
  229. $(cmd_record_mcount) \
  230. scripts/basic/fixdep $(depfile) $@ '$(call make-cmd,cc_o_c)' > \
  231. $(dot-target).tmp; \
  232. rm -f $(depfile); \
  233. mv -f $(dot-target).tmp $(dot-target).cmd
  234. endef
  235. # Built-in and composite module parts
  236. $(obj)/%.o: $(src)/%.c FORCE
  237. $(call cmd,force_checksrc)
  238. $(call if_changed_rule,cc_o_c)
  239. # Single-part modules are special since we need to mark them in $(MODVERDIR)
  240. $(single-used-m): $(obj)/%.o: $(src)/%.c FORCE
  241. $(call cmd,force_checksrc)
  242. $(call if_changed_rule,cc_o_c)
  243. @{ echo $(@:.o=.ko); echo $@; } > $(MODVERDIR)/$(@F:.o=.mod)
  244. quiet_cmd_cc_lst_c = MKLST $@
  245. cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
  246. $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \
  247. System.map $(OBJDUMP) > $@
  248. $(obj)/%.lst: $(src)/%.c FORCE
  249. $(call if_changed_dep,cc_lst_c)
  250. # Compile assembler sources (.S)
  251. # ---------------------------------------------------------------------------
  252. modkern_aflags := $(KBUILD_AFLAGS_KERNEL) $(AFLAGS_KERNEL)
  253. $(real-objs-m) : modkern_aflags := $(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE)
  254. $(real-objs-m:.o=.s): modkern_aflags := $(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE)
  255. quiet_cmd_as_s_S = CPP $(quiet_modtag) $@
  256. cmd_as_s_S = $(CPP) $(a_flags) -o $@ $<
  257. $(obj)/%.s: $(src)/%.S FORCE
  258. $(call if_changed_dep,as_s_S)
  259. quiet_cmd_as_o_S = AS $(quiet_modtag) $@
  260. cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $<
  261. $(obj)/%.o: $(src)/%.S FORCE
  262. $(call if_changed_dep,as_o_S)
  263. targets += $(real-objs-y) $(real-objs-m) $(lib-y)
  264. targets += $(extra-y) $(MAKECMDGOALS) $(always)
  265. # Linker scripts preprocessor (.lds.S -> .lds)
  266. # ---------------------------------------------------------------------------
  267. quiet_cmd_cpp_lds_S = LDS $@
  268. cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -C -U$(ARCH) \
  269. -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $<
  270. $(obj)/%.lds: $(src)/%.lds.S FORCE
  271. $(call if_changed_dep,cpp_lds_S)
  272. # Build the compiled-in targets
  273. # ---------------------------------------------------------------------------
  274. # To build objects in subdirs, we need to descend into the directories
  275. $(sort $(subdir-obj-y)): $(subdir-ym) ;
  276. #
  277. # Rule to compile a set of .o files into one .o file
  278. #
  279. ifdef builtin-target
  280. quiet_cmd_link_o_target = LD $@
  281. # If the list of objects to link is empty, just create an empty built-in.o
  282. cmd_link_o_target = $(if $(strip $(obj-y)),\
  283. $(LD) $(ld_flags) -r -o $@ $(filter $(obj-y), $^) \
  284. $(cmd_secanalysis),\
  285. rm -f $@; $(AR) rcs $@)
  286. $(builtin-target): $(obj-y) FORCE
  287. $(call if_changed,link_o_target)
  288. targets += $(builtin-target)
  289. endif # builtin-target
  290. #
  291. # Rule to create modules.order file
  292. #
  293. # Create commands to either record .ko file or cat modules.order from
  294. # a subdirectory
  295. modorder-cmds = \
  296. $(foreach m, $(modorder), \
  297. $(if $(filter %/modules.order, $m), \
  298. cat $m;, echo kernel/$m;))
  299. $(modorder-target): $(subdir-ym) FORCE
  300. $(Q)(cat /dev/null; $(modorder-cmds)) > $@
  301. #
  302. # Rule to compile a set of .o files into one .a file
  303. #
  304. ifdef lib-target
  305. quiet_cmd_link_l_target = AR $@
  306. cmd_link_l_target = rm -f $@; $(AR) rcs $@ $(lib-y)
  307. $(lib-target): $(lib-y) FORCE
  308. $(call if_changed,link_l_target)
  309. targets += $(lib-target)
  310. endif
  311. #
  312. # Rule to link composite objects
  313. #
  314. # Composite objects are specified in kbuild makefile as follows:
  315. # <composite-object>-objs := <list of .o files>
  316. # or
  317. # <composite-object>-y := <list of .o files>
  318. link_multi_deps = \
  319. $(filter $(addprefix $(obj)/, \
  320. $($(subst $(obj)/,,$(@:.o=-objs))) \
  321. $($(subst $(obj)/,,$(@:.o=-y)))), $^)
  322. quiet_cmd_link_multi-y = LD $@
  323. cmd_link_multi-y = $(LD) $(ld_flags) -r -o $@ $(link_multi_deps) $(cmd_secanalysis)
  324. quiet_cmd_link_multi-m = LD [M] $@
  325. cmd_link_multi-m = $(cmd_link_multi-y)
  326. # We would rather have a list of rules like
  327. # foo.o: $(foo-objs)
  328. # but that's not so easy, so we rather make all composite objects depend
  329. # on the set of all their parts
  330. $(multi-used-y) : %.o: $(multi-objs-y) FORCE
  331. $(call if_changed,link_multi-y)
  332. $(multi-used-m) : %.o: $(multi-objs-m) FORCE
  333. $(call if_changed,link_multi-m)
  334. @{ echo $(@:.o=.ko); echo $(link_multi_deps); } > $(MODVERDIR)/$(@F:.o=.mod)
  335. targets += $(multi-used-y) $(multi-used-m)
  336. # Descending
  337. # ---------------------------------------------------------------------------
  338. PHONY += $(subdir-ym)
  339. $(subdir-ym):
  340. $(Q)$(MAKE) $(build)=$@
  341. # Add FORCE to the prequisites of a target to force it to be always rebuilt.
  342. # ---------------------------------------------------------------------------
  343. PHONY += FORCE
  344. FORCE:
  345. # Read all saved command lines and dependencies for the $(targets) we
  346. # may be building above, using $(if_changed{,_dep}). As an
  347. # optimization, we don't need to read them if the target does not
  348. # exist, we will rebuild anyway in that case.
  349. targets := $(wildcard $(sort $(targets)))
  350. cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
  351. ifneq ($(cmd_files),)
  352. include $(cmd_files)
  353. endif
  354. # Declare the contents of the .PHONY variable as phony. We keep that
  355. # information in a variable se we can use it in if_changed and friends.
  356. .PHONY: $(PHONY)