[关闭]
@cyysu 2017-10-16T09:58:11.000000Z 字数 9750 阅读 1063

Makefile-项目篇(四)

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

Makefile系列教程


实战项目

  1. # 从250行之后到609行
  2. # Cross compiling and selecting different set of gcc/bin-utils
  3. # ---------------------------------------------------------------------------
  4. #
  5. # When performing cross compilation for other architectures ARCH shall be set
  6. # to the target architecture. (See arch/* for the possibilities).
  7. # ARCH can be set during invocation of make:
  8. # make ARCH=ia64
  9. # Another way is to have ARCH set in the environment.
  10. # The default ARCH is the host where make is executed.
  11. # CROSS_COMPILE specify the prefix used for all executables used
  12. # during compilation. Only gcc and related bin-utils executables
  13. # are prefixed with $(CROSS_COMPILE).
  14. # CROSS_COMPILE can be set on the command line
  15. # make CROSS_COMPILE=ia64-linux-
  16. # Alternatively CROSS_COMPILE can be set in the environment.
  17. # A third alternative is to store a setting in .config so that plain
  18. # "make" in the configured kernel build directory always uses that.
  19. # Default value for CROSS_COMPILE is not to prefix executables
  20. # Note: Some architectures assign CROSS_COMPILE in their arch/*/Makefile
  21. ARCH ?= $(SUBARCH)
  22. CROSS_COMPILE ?= $(CONFIG_CROSS_COMPILE:"%"=%)
  23. # Architecture as present in compile.h
  24. UTS_MACHINE := $(ARCH)
  25. SRCARCH := $(ARCH)
  26. # Additional ARCH settings for x86
  27. ifeq ($(ARCH),i386)
  28. SRCARCH := x86
  29. endif
  30. ifeq ($(ARCH),x86_64)
  31. SRCARCH := x86
  32. endif
  33. # Additional ARCH settings for sparc
  34. ifeq ($(ARCH),sparc32)
  35. SRCARCH := sparc
  36. endif
  37. ifeq ($(ARCH),sparc64)
  38. SRCARCH := sparc
  39. endif
  40. # Additional ARCH settings for sh
  41. ifeq ($(ARCH),sh64)
  42. SRCARCH := sh
  43. endif
  44. # Additional ARCH settings for tile
  45. ifeq ($(ARCH),tilepro)
  46. SRCARCH := tile
  47. endif
  48. ifeq ($(ARCH),tilegx)
  49. SRCARCH := tile
  50. endif
  51. # Where to locate arch specific headers
  52. hdr-arch := $(SRCARCH)
  53. KCONFIG_CONFIG ?= .config
  54. export KCONFIG_CONFIG
  55. # SHELL used by kbuild
  56. CONFIG_SHELL := $(shell if [ -x "$$BASH" ]; then echo $$BASH; \
  57. else if [ -x /bin/bash ]; then echo /bin/bash; \
  58. else echo sh; fi ; fi)
  59. HOSTCC = gcc
  60. HOSTCXX = g++
  61. HOSTCFLAGS = -Wall -Wmissing-prototypes -Wstrict-prototypes -O2 -fomit-frame-pointer -std=gnu89
  62. HOSTCXXFLAGS = -O2
  63. # 查看$(HOSTCC)中是否时clang version 并统计行数,然后增加编译选项,这些选项可以自行查找什么含义
  64. ifeq ($(shell $(HOSTCC) -v 2>&1 | grep -c "clang version"), 1)
  65. HOSTCFLAGS += -Wno-unused-value -Wno-unused-parameter \
  66. -Wno-missing-field-initializers -fno-delete-null-pointer-checks
  67. endif
  68. # Decide whether to build built-in, modular, or both.
  69. # Normally, just do built-in.
  70. KBUILD_MODULES :=
  71. KBUILD_BUILTIN := 1
  72. # If we have only "make modules", don't compile built-in objects.
  73. # When we're building modules with modversions, we need to consider
  74. # the built-in objects during the descend as well, in order to
  75. # make sure the checksums are up to date before we record them.
  76. ifeq ($(MAKECMDGOALS),modules)
  77. KBUILD_BUILTIN := $(if $(CONFIG_MODVERSIONS),1)
  78. endif
  79. # If we have "make <whatever> modules", compile modules
  80. # in addition to whatever we do anyway.
  81. # Just "make" or "make all" shall build modules as well
  82. ifneq ($(filter all _all modules,$(MAKECMDGOALS)),)
  83. KBUILD_MODULES := 1
  84. endif
  85. ifeq ($(MAKECMDGOALS),)
  86. KBUILD_MODULES := 1
  87. endif
  88. export KBUILD_MODULES KBUILD_BUILTIN
  89. export KBUILD_CHECKSRC KBUILD_SRC KBUILD_EXTMOD
  90. # We need some generic definitions (do not try to remake the file).
  91. scripts/Kbuild.include: ;
  92. include scripts/Kbuild.include
  93. # Make variables (CC, etc...)
  94. AS = $(CROSS_COMPILE)as
  95. LD = $(CROSS_COMPILE)ld
  96. CC = $(CROSS_COMPILE)gcc
  97. CPP = $(CC) -E
  98. AR = $(CROSS_COMPILE)ar
  99. NM = $(CROSS_COMPILE)nm
  100. STRIP = $(CROSS_COMPILE)strip
  101. OBJCOPY = $(CROSS_COMPILE)objcopy
  102. OBJDUMP = $(CROSS_COMPILE)objdump
  103. AWK = awk
  104. GENKSYMS = scripts/genksyms/genksyms
  105. INSTALLKERNEL := installkernel
  106. DEPMOD = /sbin/depmod
  107. PERL = perl
  108. PYTHON = python
  109. CHECK = sparse
  110. CHECKFLAGS := -D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ \
  111. -Wbitwise -Wno-return-void $(CF)
  112. CFLAGS_MODULE =
  113. AFLAGS_MODULE =
  114. LDFLAGS_MODULE =
  115. CFLAGS_KERNEL =
  116. AFLAGS_KERNEL =
  117. CFLAGS_GCOV = -fprofile-arcs -ftest-coverage -fno-tree-loop-im
  118. # Use USERINCLUDE when you must reference the UAPI directories only.
  119. USERINCLUDE := \
  120. -I$(srctree)/arch/$(hdr-arch)/include/uapi \
  121. -Iarch/$(hdr-arch)/include/generated/uapi \
  122. -I$(srctree)/include/uapi \
  123. -Iinclude/generated/uapi \
  124. -include $(srctree)/include/linux/kconfig.h
  125. # Use LINUXINCLUDE when you must reference the include/ directory.
  126. # Needed to be compatible with the O= option
  127. LINUXINCLUDE := \
  128. -I$(srctree)/arch/$(hdr-arch)/include \
  129. -Iarch/$(hdr-arch)/include/generated/uapi \
  130. -Iarch/$(hdr-arch)/include/generated \
  131. $(if $(KBUILD_SRC), -I$(srctree)/include) \
  132. -Iinclude \
  133. $(USERINCLUDE)
  134. KBUILD_CPPFLAGS := -D__KERNEL__
  135. KBUILD_CFLAGS := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \
  136. -fno-strict-aliasing -fno-common \
  137. -Werror-implicit-function-declaration \
  138. -Wno-format-security \
  139. -std=gnu89 $(call cc-option,-fno-PIE)
  140. KBUILD_AFLAGS_KERNEL :=
  141. KBUILD_CFLAGS_KERNEL :=
  142. KBUILD_AFLAGS := -D__ASSEMBLY__ $(call cc-option,-fno-PIE)
  143. KBUILD_AFLAGS_MODULE := -DMODULE
  144. KBUILD_CFLAGS_MODULE := -DMODULE
  145. KBUILD_LDFLAGS_MODULE := -T $(srctree)/scripts/module-common.lds
  146. # Read KERNELRELEASE from include/config/kernel.release (if it exists)
  147. KERNELRELEASE = $(shell cat include/config/kernel.release 2> /dev/null)
  148. KERNELVERSION = $(VERSION)$(if $(PATCHLEVEL),.$(PATCHLEVEL)$(if $(SUBLEVEL),.$(SUBLEVEL)))$(EXTRAVERSION)
  149. export VERSION PATCHLEVEL SUBLEVEL KERNELRELEASE KERNELVERSION
  150. export ARCH SRCARCH CONFIG_SHELL HOSTCC HOSTCFLAGS CROSS_COMPILE AS LD CC
  151. export CPP AR NM STRIP OBJCOPY OBJDUMP
  152. export MAKE AWK GENKSYMS INSTALLKERNEL PERL PYTHON UTS_MACHINE
  153. export HOSTCXX HOSTCXXFLAGS LDFLAGS_MODULE CHECK CHECKFLAGS
  154. export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS LDFLAGS
  155. export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE CFLAGS_GCOV CFLAGS_KASAN
  156. export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE
  157. export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_LDFLAGS_MODULE
  158. export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL
  159. export KBUILD_ARFLAGS
  160. # When compiling out-of-tree modules, put MODVERDIR in the module
  161. # tree rather than in the kernel tree. The kernel tree might
  162. # even be read-only.
  163. export MODVERDIR := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/).tmp_versions
  164. # Files to ignore in find ... statements
  165. export RCS_FIND_IGNORE := \( -name SCCS -o -name BitKeeper -o -name .svn -o \
  166. -name CVS -o -name .pc -o -name .hg -o -name .git \) \
  167. -prune -o
  168. export RCS_TAR_IGNORE := --exclude SCCS --exclude BitKeeper --exclude .svn \
  169. --exclude CVS --exclude .pc --exclude .hg --exclude .git
  170. # ===========================================================================
  171. # Rules shared between *config targets and build targets
  172. # Basic helpers built in scripts/
  173. # 这里的=不要认为时赋值操作 这个$(build)变量定义来自Kbuild.include,这个文件也是在上面包含进来的
  174. PHONY += scripts_basic
  175. scripts_basic:
  176. $(Q)$(MAKE) $(build)=scripts/basic
  177. $(Q)rm -f .tmp_quiet_recordmcount
  178. # To avoid any implicit rule to kick in, define an empty command.
  179. scripts/basic/%: scripts_basic ;
  180. PHONY += outputmakefile
  181. # outputmakefile generates a Makefile in the output directory, if using a
  182. # separate output directory. This allows convenient use of make in the
  183. # output directory.
  184. outputmakefile:
  185. ifneq ($(KBUILD_SRC),)
  186. $(Q)ln -fsn $(srctree) source
  187. $(Q)$(CONFIG_SHELL) $(srctree)/scripts/mkmakefile \
  188. $(srctree) $(objtree) $(VERSION) $(PATCHLEVEL)
  189. endif
  190. # Support for using generic headers in asm-generic
  191. PHONY += asm-generic
  192. asm-generic:
  193. $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.asm-generic \
  194. src=asm obj=arch/$(SRCARCH)/include/generated/asm
  195. $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.asm-generic \
  196. src=uapi/asm obj=arch/$(SRCARCH)/include/generated/uapi/asm
  197. # To make sure we do not include .config for any of the *config targets
  198. # catch them early, and hand them over to scripts/kconfig/Makefile
  199. # It is allowed to specify more targets when calling make, including
  200. # mixing *config targets and build targets.
  201. # For example 'make oldconfig all'.
  202. # Detect when mixed targets is specified, and make a second invocation
  203. # of make so .config is not included in this case either (for *config).
  204. version_h := include/generated/uapi/linux/version.h
  205. old_version_h := include/linux/version.h
  206. no-dot-config-targets := clean mrproper distclean \
  207. cscope gtags TAGS tags help% %docs check% coccicheck \
  208. $(version_h) headers_% archheaders archscripts \
  209. kernelversion %src-pkg
  210. config-targets := 0
  211. mixed-targets := 0
  212. dot-config := 1
  213. ifneq ($(filter $(no-dot-config-targets), $(MAKECMDGOALS)),)
  214. ifeq ($(filter-out $(no-dot-config-targets), $(MAKECMDGOALS)),)
  215. dot-config := 0
  216. endif
  217. endif
  218. ifeq ($(KBUILD_EXTMOD),)
  219. ifneq ($(filter config %config,$(MAKECMDGOALS)),)
  220. config-targets := 1
  221. ifneq ($(words $(MAKECMDGOALS)),1)
  222. mixed-targets := 1
  223. endif
  224. endif
  225. endif
  226. # install and module_install need also be processed one by one
  227. ifneq ($(filter install,$(MAKECMDGOALS)),)
  228. ifneq ($(filter modules_install,$(MAKECMDGOALS)),)
  229. mixed-targets := 1
  230. endif
  231. endif
  232. ifeq ($(mixed-targets),1)
  233. # ===========================================================================
  234. # We're called with mixed targets (*config and build targets).
  235. # Handle them one by one.
  236. PHONY += $(MAKECMDGOALS) __build_one_by_one
  237. $(filter-out __build_one_by_one, $(MAKECMDGOALS)): __build_one_by_one
  238. @:
  239. __build_one_by_one:
  240. $(Q)set -e; \
  241. for i in $(MAKECMDGOALS); do \
  242. $(MAKE) -f $(srctree)/Makefile $$i; \
  243. done
  244. else
  245. ifeq ($(config-targets),1)
  246. # ===========================================================================
  247. # *config targets only - make sure prerequisites are updated, and descend
  248. # in scripts/kconfig to make the *config target
  249. # Read arch specific Makefile to set KBUILD_DEFCONFIG as needed.
  250. # KBUILD_DEFCONFIG may point out an alternative default configuration
  251. # used for 'make defconfig'
  252. # 这里是设置make menuconfig的还有make config
  253. include arch/$(SRCARCH)/Makefile
  254. export KBUILD_DEFCONFIG KBUILD_KCONFIG
  255. config: scripts_basic outputmakefile FORCE
  256. $(Q)$(MAKE) $(build)=scripts/kconfig $@
  257. %config: scripts_basic outputmakefile FORCE
  258. $(Q)$(MAKE) $(build)=scripts/kconfig $@
  259. else
  260. # ===========================================================================
  261. # Build targets only - this includes vmlinux, arch specific targets, clean
  262. # targets and others. In general all targets except *config targets.
  263. ifeq ($(KBUILD_EXTMOD),)
  264. # Additional helpers built in scripts/
  265. # Carefully list dependencies so we do not try to build scripts twice
  266. # in parallel
  267. # Makefile中的等号,在目标中可以起到连接字符串的操作 $(build)=$(@) 表示的就是-f $(srctree)/scripts/Makefile.build obj=$(@)
  268. PHONY += scripts
  269. scripts: scripts_basic include/config/auto.conf include/config/tristate.conf \
  270. asm-generic
  271. $(Q)$(MAKE) $(build)=$(@)
  272. # Objects we will link into vmlinux / subdirs we need to visit
  273. # := 表示的时立即展开
  274. init-y := init/
  275. drivers-y := drivers/ sound/ firmware/
  276. net-y := net/
  277. libs-y := lib/
  278. core-y := usr/
  279. virt-y := virt/
  280. endif # KBUILD_EXTMOD
  281. ifeq ($(dot-config),1)
  282. # Read in config
  283. -include include/config/auto.conf
  284. ifeq ($(KBUILD_EXTMOD),)
  285. # Read in dependencies to all Kconfig* files, make sure to run
  286. # oldconfig if changes are detected.
  287. -include include/config/auto.conf.cmd

打赏

                    支付宝                                                         微信

微信与支付宝支付

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