@cyysu
2017-10-16T09:58:11.000000Z
字数 9750
阅读 1121
- 时间:2017年10月16日
- 作者:Kali
- 邮箱:cyysu.github.io@gmail.com
- 版本:3.0
- 描述:Makefile-项目四,继Makefile项目篇三的Makefile
Makefile系列教程
# 从250行之后到609行# Cross compiling and selecting different set of gcc/bin-utils# ---------------------------------------------------------------------------## When performing cross compilation for other architectures ARCH shall be set# to the target architecture. (See arch/* for the possibilities).# ARCH can be set during invocation of make:# make ARCH=ia64# Another way is to have ARCH set in the environment.# The default ARCH is the host where make is executed.# CROSS_COMPILE specify the prefix used for all executables used# during compilation. Only gcc and related bin-utils executables# are prefixed with $(CROSS_COMPILE).# CROSS_COMPILE can be set on the command line# make CROSS_COMPILE=ia64-linux-# Alternatively CROSS_COMPILE can be set in the environment.# A third alternative is to store a setting in .config so that plain# "make" in the configured kernel build directory always uses that.# Default value for CROSS_COMPILE is not to prefix executables# Note: Some architectures assign CROSS_COMPILE in their arch/*/MakefileARCH ?= $(SUBARCH)CROSS_COMPILE ?= $(CONFIG_CROSS_COMPILE:"%"=%)# Architecture as present in compile.hUTS_MACHINE := $(ARCH)SRCARCH := $(ARCH)# Additional ARCH settings for x86ifeq ($(ARCH),i386)SRCARCH := x86endififeq ($(ARCH),x86_64)SRCARCH := x86endif# Additional ARCH settings for sparcifeq ($(ARCH),sparc32)SRCARCH := sparcendififeq ($(ARCH),sparc64)SRCARCH := sparcendif# Additional ARCH settings for shifeq ($(ARCH),sh64)SRCARCH := shendif# Additional ARCH settings for tileifeq ($(ARCH),tilepro)SRCARCH := tileendififeq ($(ARCH),tilegx)SRCARCH := tileendif# Where to locate arch specific headershdr-arch := $(SRCARCH)KCONFIG_CONFIG ?= .configexport KCONFIG_CONFIG# SHELL used by kbuildCONFIG_SHELL := $(shell if [ -x "$$BASH" ]; then echo $$BASH; \else if [ -x /bin/bash ]; then echo /bin/bash; \else echo sh; fi ; fi)HOSTCC = gccHOSTCXX = g++HOSTCFLAGS = -Wall -Wmissing-prototypes -Wstrict-prototypes -O2 -fomit-frame-pointer -std=gnu89HOSTCXXFLAGS = -O2# 查看$(HOSTCC)中是否时clang version 并统计行数,然后增加编译选项,这些选项可以自行查找什么含义ifeq ($(shell $(HOSTCC) -v 2>&1 | grep -c "clang version"), 1)HOSTCFLAGS += -Wno-unused-value -Wno-unused-parameter \-Wno-missing-field-initializers -fno-delete-null-pointer-checksendif# Decide whether to build built-in, modular, or both.# Normally, just do built-in.KBUILD_MODULES :=KBUILD_BUILTIN := 1# If we have only "make modules", don't compile built-in objects.# When we're building modules with modversions, we need to consider# the built-in objects during the descend as well, in order to# make sure the checksums are up to date before we record them.ifeq ($(MAKECMDGOALS),modules)KBUILD_BUILTIN := $(if $(CONFIG_MODVERSIONS),1)endif# If we have "make <whatever> modules", compile modules# in addition to whatever we do anyway.# Just "make" or "make all" shall build modules as wellifneq ($(filter all _all modules,$(MAKECMDGOALS)),)KBUILD_MODULES := 1endififeq ($(MAKECMDGOALS),)KBUILD_MODULES := 1endifexport KBUILD_MODULES KBUILD_BUILTINexport KBUILD_CHECKSRC KBUILD_SRC KBUILD_EXTMOD# We need some generic definitions (do not try to remake the file).scripts/Kbuild.include: ;include scripts/Kbuild.include# Make variables (CC, etc...)AS = $(CROSS_COMPILE)asLD = $(CROSS_COMPILE)ldCC = $(CROSS_COMPILE)gccCPP = $(CC) -EAR = $(CROSS_COMPILE)arNM = $(CROSS_COMPILE)nmSTRIP = $(CROSS_COMPILE)stripOBJCOPY = $(CROSS_COMPILE)objcopyOBJDUMP = $(CROSS_COMPILE)objdumpAWK = awkGENKSYMS = scripts/genksyms/genksymsINSTALLKERNEL := installkernelDEPMOD = /sbin/depmodPERL = perlPYTHON = pythonCHECK = sparseCHECKFLAGS := -D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ \-Wbitwise -Wno-return-void $(CF)CFLAGS_MODULE =AFLAGS_MODULE =LDFLAGS_MODULE =CFLAGS_KERNEL =AFLAGS_KERNEL =CFLAGS_GCOV = -fprofile-arcs -ftest-coverage -fno-tree-loop-im# Use USERINCLUDE when you must reference the UAPI directories only.USERINCLUDE := \-I$(srctree)/arch/$(hdr-arch)/include/uapi \-Iarch/$(hdr-arch)/include/generated/uapi \-I$(srctree)/include/uapi \-Iinclude/generated/uapi \-include $(srctree)/include/linux/kconfig.h# Use LINUXINCLUDE when you must reference the include/ directory.# Needed to be compatible with the O= optionLINUXINCLUDE := \-I$(srctree)/arch/$(hdr-arch)/include \-Iarch/$(hdr-arch)/include/generated/uapi \-Iarch/$(hdr-arch)/include/generated \$(if $(KBUILD_SRC), -I$(srctree)/include) \-Iinclude \$(USERINCLUDE)KBUILD_CPPFLAGS := -D__KERNEL__KBUILD_CFLAGS := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \-fno-strict-aliasing -fno-common \-Werror-implicit-function-declaration \-Wno-format-security \-std=gnu89 $(call cc-option,-fno-PIE)KBUILD_AFLAGS_KERNEL :=KBUILD_CFLAGS_KERNEL :=KBUILD_AFLAGS := -D__ASSEMBLY__ $(call cc-option,-fno-PIE)KBUILD_AFLAGS_MODULE := -DMODULEKBUILD_CFLAGS_MODULE := -DMODULEKBUILD_LDFLAGS_MODULE := -T $(srctree)/scripts/module-common.lds# Read KERNELRELEASE from include/config/kernel.release (if it exists)KERNELRELEASE = $(shell cat include/config/kernel.release 2> /dev/null)KERNELVERSION = $(VERSION)$(if $(PATCHLEVEL),.$(PATCHLEVEL)$(if $(SUBLEVEL),.$(SUBLEVEL)))$(EXTRAVERSION)export VERSION PATCHLEVEL SUBLEVEL KERNELRELEASE KERNELVERSIONexport ARCH SRCARCH CONFIG_SHELL HOSTCC HOSTCFLAGS CROSS_COMPILE AS LD CCexport CPP AR NM STRIP OBJCOPY OBJDUMPexport MAKE AWK GENKSYMS INSTALLKERNEL PERL PYTHON UTS_MACHINEexport HOSTCXX HOSTCXXFLAGS LDFLAGS_MODULE CHECK CHECKFLAGSexport KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS LDFLAGSexport KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE CFLAGS_GCOV CFLAGS_KASANexport KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULEexport KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_LDFLAGS_MODULEexport KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNELexport KBUILD_ARFLAGS# When compiling out-of-tree modules, put MODVERDIR in the module# tree rather than in the kernel tree. The kernel tree might# even be read-only.export MODVERDIR := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/).tmp_versions# Files to ignore in find ... statementsexport RCS_FIND_IGNORE := \( -name SCCS -o -name BitKeeper -o -name .svn -o \-name CVS -o -name .pc -o -name .hg -o -name .git \) \-prune -oexport RCS_TAR_IGNORE := --exclude SCCS --exclude BitKeeper --exclude .svn \--exclude CVS --exclude .pc --exclude .hg --exclude .git# ===========================================================================# Rules shared between *config targets and build targets# Basic helpers built in scripts/# 这里的=不要认为时赋值操作 这个$(build)变量定义来自Kbuild.include,这个文件也是在上面包含进来的PHONY += scripts_basicscripts_basic:$(Q)$(MAKE) $(build)=scripts/basic$(Q)rm -f .tmp_quiet_recordmcount# To avoid any implicit rule to kick in, define an empty command.scripts/basic/%: scripts_basic ;PHONY += outputmakefile# outputmakefile generates a Makefile in the output directory, if using a# separate output directory. This allows convenient use of make in the# output directory.outputmakefile:ifneq ($(KBUILD_SRC),)$(Q)ln -fsn $(srctree) source$(Q)$(CONFIG_SHELL) $(srctree)/scripts/mkmakefile \$(srctree) $(objtree) $(VERSION) $(PATCHLEVEL)endif# Support for using generic headers in asm-genericPHONY += asm-genericasm-generic:$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.asm-generic \src=asm obj=arch/$(SRCARCH)/include/generated/asm$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.asm-generic \src=uapi/asm obj=arch/$(SRCARCH)/include/generated/uapi/asm# To make sure we do not include .config for any of the *config targets# catch them early, and hand them over to scripts/kconfig/Makefile# It is allowed to specify more targets when calling make, including# mixing *config targets and build targets.# For example 'make oldconfig all'.# Detect when mixed targets is specified, and make a second invocation# of make so .config is not included in this case either (for *config).version_h := include/generated/uapi/linux/version.hold_version_h := include/linux/version.hno-dot-config-targets := clean mrproper distclean \cscope gtags TAGS tags help% %docs check% coccicheck \$(version_h) headers_% archheaders archscripts \kernelversion %src-pkgconfig-targets := 0mixed-targets := 0dot-config := 1ifneq ($(filter $(no-dot-config-targets), $(MAKECMDGOALS)),)ifeq ($(filter-out $(no-dot-config-targets), $(MAKECMDGOALS)),)dot-config := 0endifendififeq ($(KBUILD_EXTMOD),)ifneq ($(filter config %config,$(MAKECMDGOALS)),)config-targets := 1ifneq ($(words $(MAKECMDGOALS)),1)mixed-targets := 1endifendifendif# install and module_install need also be processed one by oneifneq ($(filter install,$(MAKECMDGOALS)),)ifneq ($(filter modules_install,$(MAKECMDGOALS)),)mixed-targets := 1endifendififeq ($(mixed-targets),1)# ===========================================================================# We're called with mixed targets (*config and build targets).# Handle them one by one.PHONY += $(MAKECMDGOALS) __build_one_by_one$(filter-out __build_one_by_one, $(MAKECMDGOALS)): __build_one_by_one@:__build_one_by_one:$(Q)set -e; \for i in $(MAKECMDGOALS); do \$(MAKE) -f $(srctree)/Makefile $$i; \doneelseifeq ($(config-targets),1)# ===========================================================================# *config targets only - make sure prerequisites are updated, and descend# in scripts/kconfig to make the *config target# Read arch specific Makefile to set KBUILD_DEFCONFIG as needed.# KBUILD_DEFCONFIG may point out an alternative default configuration# used for 'make defconfig'# 这里是设置make menuconfig的还有make configinclude arch/$(SRCARCH)/Makefileexport KBUILD_DEFCONFIG KBUILD_KCONFIGconfig: scripts_basic outputmakefile FORCE$(Q)$(MAKE) $(build)=scripts/kconfig $@%config: scripts_basic outputmakefile FORCE$(Q)$(MAKE) $(build)=scripts/kconfig $@else# ===========================================================================# Build targets only - this includes vmlinux, arch specific targets, clean# targets and others. In general all targets except *config targets.ifeq ($(KBUILD_EXTMOD),)# Additional helpers built in scripts/# Carefully list dependencies so we do not try to build scripts twice# in parallel# Makefile中的等号,在目标中可以起到连接字符串的操作 $(build)=$(@) 表示的就是-f $(srctree)/scripts/Makefile.build obj=$(@)PHONY += scriptsscripts: scripts_basic include/config/auto.conf include/config/tristate.conf \asm-generic$(Q)$(MAKE) $(build)=$(@)# Objects we will link into vmlinux / subdirs we need to visit# := 表示的时立即展开init-y := init/drivers-y := drivers/ sound/ firmware/net-y := net/libs-y := lib/core-y := usr/virt-y := virt/endif # KBUILD_EXTMODifeq ($(dot-config),1)# Read in config-include include/config/auto.confifeq ($(KBUILD_EXTMOD),)# Read in dependencies to all Kconfig* files, make sure to run# oldconfig if changes are detected.-include include/config/auto.conf.cmd
支付宝 微信