[关闭]
@cyysu 2017-10-17T06:41:22.000000Z 字数 13236 阅读 1199

Makefile-项目篇(五)

  • 时间:22017年10月17日
  • 作者:Kali
  • 邮箱:cyysu.github.io@gmail.com
  • 版本:3.0
  • 描述:Makefile-项目五,继Makefile项目篇四的Makefile

Makefile系列教程


实战项目

大多数知识点我已经在基础篇五里面讲解过了,在分析内核的Makefile时最好有一个源码分析工具,还有遇到问题先不要纠结,继续向下面看,然后在回来思考那个问题,问题可能就会换了一个角度。

  1. # To avoid any implicit rule to kick in, define an empty command
  2. $(KCONFIG_CONFIG) include/config/auto.conf.cmd: ;
  3. # If .config is newer than include/config/auto.conf, someone tinkered
  4. # with it and forgot to run make oldconfig.
  5. # if auto.conf.cmd is missing then we are probably in a cleaned tree so
  6. # we execute the config step to be sure to catch updated Kconfig files
  7. include/config/%.conf: $(KCONFIG_CONFIG) include/config/auto.conf.cmd
  8. $(Q)$(MAKE) -f $(srctree)/Makefile silentoldconfig
  9. else
  10. # external modules needs include/generated/autoconf.h and include/config/auto.conf
  11. # but do not care if they are up-to-date. Use auto.conf to trigger the test
  12. PHONY += include/config/auto.conf
  13. include/config/auto.conf:
  14. $(Q)test -e include/generated/autoconf.h -a -e $@ || ( \
  15. echo >&2; \
  16. echo >&2 " ERROR: Kernel configuration is invalid."; \
  17. echo >&2 " include/generated/autoconf.h or $@ are missing.";\
  18. echo >&2 " Run 'make oldconfig && make prepare' on kernel src to fix it."; \
  19. echo >&2 ; \
  20. /bin/false)
  21. endif # KBUILD_EXTMOD
  22. else
  23. # Dummy target needed, because used as prerequisite
  24. include/config/auto.conf: ;
  25. endif # $(dot-config)
  26. # The all: target is the default when no target is given on the
  27. # command line.
  28. # This allow a user to issue only 'make' to build a kernel including modules
  29. # Defaults to vmlinux, but the arch makefile usually adds further targets
  30. all: vmlinux
  31. # The arch Makefile can set ARCH_{CPP,A,C}FLAGS to override the default
  32. # values of the respective KBUILD_* variables
  33. ARCH_CPPFLAGS :=
  34. ARCH_AFLAGS :=
  35. ARCH_CFLAGS :=
  36. # 这个地方涉及到了 编译过程中的make -f ./scripts/Makefile.build obj=arch/x86/tools relocs
  37. include arch/$(SRCARCH)/Makefile
  38. # 这里的call函数,就是可以用后面的参数更新前面的变量
  39. KBUILD_CFLAGS += $(call cc-option,-fno-delete-null-pointer-checks,)
  40. KBUILD_CFLAGS += $(call cc-disable-warning,maybe-uninitialized,)
  41. KBUILD_CFLAGS += $(call cc-disable-warning,frame-address,)
  42. KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation)
  43. KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow)
  44. KBUILD_CFLAGS += $(call cc-disable-warning, int-in-bool-context)
  45. ifdef CONFIG_CC_OPTIMIZE_FOR_SIZE
  46. KBUILD_CFLAGS += -Os
  47. else
  48. ifdef CONFIG_PROFILE_ALL_BRANCHES
  49. KBUILD_CFLAGS += -O2
  50. else
  51. KBUILD_CFLAGS += -O2
  52. endif
  53. endif
  54. # Tell gcc to never replace conditional load with a non-conditional one
  55. KBUILD_CFLAGS += $(call cc-option,--param=allow-store-data-races=0)
  56. # check for 'asm goto'
  57. ifeq ($(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-goto.sh $(CC) $(KBUILD_CFLAGS)), y)
  58. KBUILD_CFLAGS += -DCC_HAVE_ASM_GOTO
  59. KBUILD_AFLAGS += -DCC_HAVE_ASM_GOTO
  60. endif
  61. ifdef CONFIG_READABLE_ASM
  62. # Disable optimizations that make assembler listings hard to read.
  63. # reorder blocks reorders the control in the function
  64. # ipa clone creates specialized cloned functions
  65. # partial inlining inlines only parts of functions
  66. KBUILD_CFLAGS += $(call cc-option,-fno-reorder-blocks,) \
  67. $(call cc-option,-fno-ipa-cp-clone,) \
  68. $(call cc-option,-fno-partial-inlining)
  69. endif
  70. ifneq ($(CONFIG_FRAME_WARN),0)
  71. KBUILD_CFLAGS += $(call cc-option,-Wframe-larger-than=${CONFIG_FRAME_WARN})
  72. endif
  73. # Handle stack protector mode.
  74. #
  75. # Since kbuild can potentially perform two passes (first with the old
  76. # .config values and then with updated .config values), we cannot error out
  77. # if a desired compiler option is unsupported. If we were to error, kbuild
  78. # could never get to the second pass and actually notice that we changed
  79. # the option to something that was supported.
  80. #
  81. # Additionally, we don't want to fallback and/or silently change which compiler
  82. # flags will be used, since that leads to producing kernels with different
  83. # security feature characteristics depending on the compiler used. ("But I
  84. # selected CC_STACKPROTECTOR_STRONG! Why did it build with _REGULAR?!")
  85. #
  86. # The middle ground is to warn here so that the failed option is obvious, but
  87. # to let the build fail with bad compiler flags so that we can't produce a
  88. # kernel when there is a CONFIG and compiler mismatch.
  89. #
  90. ifdef CONFIG_CC_STACKPROTECTOR_REGULAR
  91. stackp-flag := -fstack-protector
  92. ifeq ($(call cc-option, $(stackp-flag)),)
  93. $(warning Cannot use CONFIG_CC_STACKPROTECTOR_REGULAR: \
  94. -fstack-protector not supported by compiler)
  95. endif
  96. else
  97. ifdef CONFIG_CC_STACKPROTECTOR_STRONG
  98. stackp-flag := -fstack-protector-strong
  99. ifeq ($(call cc-option, $(stackp-flag)),)
  100. $(warning Cannot use CONFIG_CC_STACKPROTECTOR_STRONG: \
  101. -fstack-protector-strong not supported by compiler)
  102. endif
  103. else
  104. # Force off for distro compilers that enable stack protector by default.
  105. stackp-flag := $(call cc-option, -fno-stack-protector)
  106. endif
  107. endif
  108. KBUILD_CFLAGS += $(stackp-flag)
  109. ifeq ($(cc-name),clang)
  110. KBUILD_CPPFLAGS += $(call cc-option,-Qunused-arguments,)
  111. KBUILD_CPPFLAGS += $(call cc-option,-Wno-unknown-warning-option,)
  112. KBUILD_CFLAGS += $(call cc-disable-warning, unused-variable)
  113. KBUILD_CFLAGS += $(call cc-disable-warning, format-invalid-specifier)
  114. KBUILD_CFLAGS += $(call cc-disable-warning, gnu)
  115. # Quiet clang warning: comparison of unsigned expression < 0 is always false
  116. KBUILD_CFLAGS += $(call cc-disable-warning, tautological-compare)
  117. # CLANG uses a _MergedGlobals as optimization, but this breaks modpost, as the
  118. # source of a reference will be _MergedGlobals and not on of the whitelisted names.
  119. # See modpost pattern 2
  120. KBUILD_CFLAGS += $(call cc-option, -mno-global-merge,)
  121. KBUILD_CFLAGS += $(call cc-option, -fcatch-undefined-behavior)
  122. else
  123. # These warnings generated too much noise in a regular build.
  124. # Use make W=1 to enable them (see scripts/Makefile.build)
  125. KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
  126. KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
  127. endif
  128. ifdef CONFIG_FRAME_POINTER
  129. KBUILD_CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls
  130. else
  131. # Some targets (ARM with Thumb2, for example), can't be built with frame
  132. # pointers. For those, we don't have FUNCTION_TRACER automatically
  133. # select FRAME_POINTER. However, FUNCTION_TRACER adds -pg, and this is
  134. # incompatible with -fomit-frame-pointer with current GCC, so we don't use
  135. # -fomit-frame-pointer with FUNCTION_TRACER.
  136. ifndef CONFIG_FUNCTION_TRACER
  137. KBUILD_CFLAGS += -fomit-frame-pointer
  138. endif
  139. endif
  140. KBUILD_CFLAGS += $(call cc-option, -fno-var-tracking-assignments)
  141. ifdef CONFIG_DEBUG_INFO
  142. ifdef CONFIG_DEBUG_INFO_SPLIT
  143. KBUILD_CFLAGS += $(call cc-option, -gsplit-dwarf, -g)
  144. else
  145. KBUILD_CFLAGS += -g
  146. endif
  147. KBUILD_AFLAGS += -Wa,-gdwarf-2
  148. endif
  149. ifdef CONFIG_DEBUG_INFO_DWARF4
  150. KBUILD_CFLAGS += $(call cc-option, -gdwarf-4,)
  151. endif
  152. ifdef CONFIG_DEBUG_INFO_REDUCED
  153. KBUILD_CFLAGS += $(call cc-option, -femit-struct-debug-baseonly) \
  154. $(call cc-option,-fno-var-tracking)
  155. endif
  156. ifdef CONFIG_FUNCTION_TRACER
  157. ifndef CC_FLAGS_FTRACE
  158. CC_FLAGS_FTRACE := -pg
  159. endif
  160. export CC_FLAGS_FTRACE
  161. ifdef CONFIG_HAVE_FENTRY
  162. CC_USING_FENTRY := $(call cc-option, -mfentry -DCC_USING_FENTRY)
  163. endif
  164. KBUILD_CFLAGS += $(CC_FLAGS_FTRACE) $(CC_USING_FENTRY)
  165. KBUILD_AFLAGS += $(CC_USING_FENTRY)
  166. ifdef CONFIG_DYNAMIC_FTRACE
  167. ifdef CONFIG_HAVE_C_RECORDMCOUNT
  168. BUILD_C_RECORDMCOUNT := y
  169. export BUILD_C_RECORDMCOUNT
  170. endif
  171. endif
  172. endif
  173. # We trigger additional mismatches with less inlining
  174. ifdef CONFIG_DEBUG_SECTION_MISMATCH
  175. KBUILD_CFLAGS += $(call cc-option, -fno-inline-functions-called-once)
  176. endif
  177. # arch Makefile may override CC so keep this after arch Makefile is included
  178. NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)
  179. CHECKFLAGS += $(NOSTDINC_FLAGS)
  180. # warn about C99 declaration after statement
  181. KBUILD_CFLAGS += $(call cc-option,-Wdeclaration-after-statement,)
  182. # disable pointer signed / unsigned warnings in gcc 4.0
  183. KBUILD_CFLAGS += $(call cc-disable-warning, pointer-sign)
  184. # disable invalid "can't wrap" optimizations for signed / pointers
  185. KBUILD_CFLAGS += $(call cc-option,-fno-strict-overflow)
  186. # conserve stack if available
  187. KBUILD_CFLAGS += $(call cc-option,-fconserve-stack)
  188. # disallow errors like 'EXPORT_GPL(foo);' with missing header
  189. KBUILD_CFLAGS += $(call cc-option,-Werror=implicit-int)
  190. # require functions to have arguments in prototypes, not empty 'int foo()'
  191. KBUILD_CFLAGS += $(call cc-option,-Werror=strict-prototypes)
  192. # Prohibit date/time macros, which would make the build non-deterministic
  193. KBUILD_CFLAGS += $(call cc-option,-Werror=date-time)
  194. # use the deterministic mode of AR if available
  195. KBUILD_ARFLAGS := $(call ar-option,D)
  196. include scripts/Makefile.kasan
  197. include scripts/Makefile.extrawarn
  198. # Add any arch overrides and user supplied CPPFLAGS, AFLAGS and CFLAGS as the
  199. # last assignments
  200. KBUILD_CPPFLAGS += $(ARCH_CPPFLAGS) $(KCPPFLAGS)
  201. KBUILD_AFLAGS += $(ARCH_AFLAGS) $(KAFLAGS)
  202. KBUILD_CFLAGS += $(ARCH_CFLAGS) $(KCFLAGS)
  203. # Use --build-id when available.
  204. LDFLAGS_BUILD_ID = $(patsubst -Wl$(comma)%,%,\
  205. $(call cc-ldoption, -Wl$(comma)--build-id,))
  206. KBUILD_LDFLAGS_MODULE += $(LDFLAGS_BUILD_ID)
  207. LDFLAGS_vmlinux += $(LDFLAGS_BUILD_ID)
  208. ifeq ($(CONFIG_STRIP_ASM_SYMS),y)
  209. LDFLAGS_vmlinux += $(call ld-option, -X,)
  210. endif
  211. # Default kernel image to build when no specific target is given.
  212. # KBUILD_IMAGE may be overruled on the command line or
  213. # set in the environment
  214. # Also any assignments in arch/$(ARCH)/Makefile take precedence over
  215. # this default value
  216. export KBUILD_IMAGE ?= vmlinux
  217. #
  218. # INSTALL_PATH specifies where to place the updated kernel and system map
  219. # images. Default is /boot, but you can set it to other values
  220. export INSTALL_PATH ?= /boot
  221. #
  222. # INSTALL_DTBS_PATH specifies a prefix for relocations required by build roots.
  223. # Like INSTALL_MOD_PATH, it isn't defined in the Makefile, but can be passed as
  224. # an argument if needed. Otherwise it defaults to the kernel install path
  225. #
  226. export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
  227. #
  228. # INSTALL_MOD_PATH specifies a prefix to MODLIB for module directory
  229. # relocations required by build roots. This is not defined in the
  230. # makefile but the argument can be passed to make if needed.
  231. #
  232. MODLIB = $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
  233. export MODLIB
  234. #
  235. # INSTALL_MOD_STRIP, if defined, will cause modules to be
  236. # stripped after they are installed. If INSTALL_MOD_STRIP is '1', then
  237. # the default option --strip-debug will be used. Otherwise,
  238. # INSTALL_MOD_STRIP value will be used as the options to the strip command.
  239. ifdef INSTALL_MOD_STRIP
  240. ifeq ($(INSTALL_MOD_STRIP),1)
  241. mod_strip_cmd = $(STRIP) --strip-debug
  242. else
  243. mod_strip_cmd = $(STRIP) $(INSTALL_MOD_STRIP)
  244. endif # INSTALL_MOD_STRIP=1
  245. else
  246. mod_strip_cmd = true
  247. endif # INSTALL_MOD_STRIP
  248. export mod_strip_cmd
  249. # CONFIG_MODULE_COMPRESS, if defined, will cause module to be compressed
  250. # after they are installed in agreement with CONFIG_MODULE_COMPRESS_GZIP
  251. # or CONFIG_MODULE_COMPRESS_XZ.
  252. mod_compress_cmd = true
  253. ifdef CONFIG_MODULE_COMPRESS
  254. ifdef CONFIG_MODULE_COMPRESS_GZIP
  255. mod_compress_cmd = gzip -n -f
  256. endif # CONFIG_MODULE_COMPRESS_GZIP
  257. ifdef CONFIG_MODULE_COMPRESS_XZ
  258. mod_compress_cmd = xz -f
  259. endif # CONFIG_MODULE_COMPRESS_XZ
  260. endif # CONFIG_MODULE_COMPRESS
  261. export mod_compress_cmd
  262. # Select initial ramdisk compression format, default is gzip(1).
  263. # This shall be used by the dracut(8) tool while creating an initramfs image.
  264. #
  265. INITRD_COMPRESS-y := gzip
  266. INITRD_COMPRESS-$(CONFIG_RD_BZIP2) := bzip2
  267. INITRD_COMPRESS-$(CONFIG_RD_LZMA) := lzma
  268. INITRD_COMPRESS-$(CONFIG_RD_XZ) := xz
  269. INITRD_COMPRESS-$(CONFIG_RD_LZO) := lzo
  270. INITRD_COMPRESS-$(CONFIG_RD_LZ4) := lz4
  271. # do not export INITRD_COMPRESS, since we didn't actually
  272. # choose a sane default compression above.
  273. # export INITRD_COMPRESS := $(INITRD_COMPRESS-y)
  274. ifdef CONFIG_MODULE_SIG_ALL
  275. $(eval $(call config_filename,MODULE_SIG_KEY))
  276. mod_sign_cmd = scripts/sign-file $(CONFIG_MODULE_SIG_HASH) $(MODULE_SIG_KEY_SRCPREFIX)$(CONFIG_MODULE_SIG_KEY) certs/signing_key.x509
  277. else
  278. mod_sign_cmd = true
  279. endif
  280. export mod_sign_cmd
  281. ifeq ($(KBUILD_EXTMOD),)
  282. core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/ block/
  283. # patsubst 函数表示字符串匹配替换
  284. vmlinux-dirs := $(patsubst %/,%,$(filter %/, $(init-y) $(init-m) \
  285. $(core-y) $(core-m) $(drivers-y) $(drivers-m) \
  286. $(net-y) $(net-m) $(libs-y) $(libs-m) $(virt-y)))
  287. vmlinux-alldirs := $(sort $(vmlinux-dirs) $(patsubst %/,%,$(filter %/, \
  288. $(init-) $(core-) $(drivers-) $(net-) $(libs-) $(virt-))))
  289. init-y := $(patsubst %/, %/built-in.o, $(init-y))
  290. core-y := $(patsubst %/, %/built-in.o, $(core-y))
  291. drivers-y := $(patsubst %/, %/built-in.o, $(drivers-y))
  292. net-y := $(patsubst %/, %/built-in.o, $(net-y))
  293. libs-y1 := $(patsubst %/, %/lib.a, $(libs-y))
  294. libs-y2 := $(patsubst %/, %/built-in.o, $(libs-y))
  295. libs-y := $(libs-y1) $(libs-y2)
  296. virt-y := $(patsubst %/, %/built-in.o, $(virt-y))
  297. # Externally visible symbols (used by link-vmlinux.sh)
  298. export KBUILD_VMLINUX_INIT := $(head-y) $(init-y)
  299. export KBUILD_VMLINUX_MAIN := $(core-y) $(libs-y) $(drivers-y) $(net-y) $(virt-y)
  300. export KBUILD_LDS := arch/$(SRCARCH)/kernel/vmlinux.lds
  301. export LDFLAGS_vmlinux
  302. # used by scripts/pacmage/Makefile
  303. export KBUILD_ALLDIRS := $(sort $(filter-out arch/%,$(vmlinux-alldirs)) arch Documentation include samples scripts tools)
  304. # 它是由内核代码下的每个顶级目录的built-in.o组成的。
  305. vmlinux-deps := $(KBUILD_LDS) $(KBUILD_VMLINUX_INIT) $(KBUILD_VMLINUX_MAIN)
  306. # Final link of vmlinux
  307. cmd_link-vmlinux = $(CONFIG_SHELL) $< $(LD) $(LDFLAGS) $(LDFLAGS_vmlinux)
  308. quiet_cmd_link-vmlinux = LINK $@
  309. # Include targets which we want to
  310. # execute if the rest of the kernel build went well.
  311. vmlinux: scripts/link-vmlinux.sh $(vmlinux-deps) FORCE
  312. ifdef CONFIG_HEADERS_CHECK
  313. $(Q)$(MAKE) -f $(srctree)/Makefile headers_check
  314. endif
  315. ifdef CONFIG_SAMPLES
  316. $(Q)$(MAKE) $(build)=samples
  317. endif
  318. ifdef CONFIG_BUILD_DOCSRC
  319. $(Q)$(MAKE) $(build)=Documentation
  320. endif
  321. ifdef CONFIG_GDB_SCRIPTS
  322. $(Q)ln -fsn `cd $(srctree) && /bin/pwd`/scripts/gdb/vmlinux-gdb.py
  323. endif
  324. +$(call if_changed,link-vmlinux)
  325. # The actual objects are generated when descending,
  326. # make sure no implicit rule kicks in
  327. # 可以看到 $(vmlinux-deps)这个目标依赖于$(vmlinux-dirs)这个目标
  328. $(sort $(vmlinux-deps)): $(vmlinux-dirs) ;
  329. # Handle descending into subdirectories listed in $(vmlinux-dirs)
  330. # Preset locale variables to speed up the build process. Limit locale
  331. # tweaks to this spot to avoid wrong language settings when running
  332. # make menuconfig etc.
  333. # Error messages still appears in the original language
  334. # $(vmlinux-dirs)这个目标依赖于 prepare scripts这两个目标
  335. PHONY += $(vmlinux-dirs)
  336. $(vmlinux-dirs): prepare scripts
  337. $(Q)$(MAKE) $(build)=$@
  338. define filechk_kernel.release
  339. echo "$(KERNELVERSION)$$($(CONFIG_SHELL) $(srctree)/scripts/setlocalversion $(srctree))"
  340. endef
  341. # Store (new) KERNELRELEASE string in include/config/kernel.release
  342. include/config/kernel.release: include/config/auto.conf FORCE
  343. $(call filechk,kernel.release)
  344. # Things we need to do before we recursively start building the kernel
  345. # or the modules are listed in "prepare".
  346. # A multi level approach is used. prepareN is processed before prepareN-1.
  347. # archprepare is used in arch Makefiles and when processed asm symlink,
  348. # version.h and scripts_basic is processed / created.

打赏

                    支付宝                                                         微信

微信与支付宝支付

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注