Linux kernel top-level Makefile preliminary work analysis 2

1. Preliminary work of Linux kernel top-level Makefile

This article continues to analyze the preparation work done by the top-level Makefile of the Linux kernel source code .

This article is a continuation of the previous article at the following address:

Analysis of the preliminary work of the top-level Makefile of the Linux kernel 1_Ling Xiaozhan's blog-CSDN blog

2. Preliminary work of kernel top-level Makefile

7. Module compilation

Linux allows you to compile a module separately, just use the command " make M=dir ", the old syntax " make SUBDIRS=dir "
It is also supported. The code in the top-level Makefile is as follows:
182 ifdef SUBDIRS
183 KBUILD_EXTMOD ?= $(SUBDIRS)
184 endif
185
186 ifeq ("$(origin M)", "command line")
187 KBUILD_EXTMOD := $(M)
188 endif
189
190 # If building an external module we do not care about the all: rule
191 # but instead _all depend on modules
192 PHONY += all
193 ifeq ($(KBUILD_EXTMOD),)
194 _all: all
195 else
196 _all: modules
197 endif
198
199 ifeq ($(KBUILD_SRC),)
200 # building in the source tree
201 srctree := .
202 else
203 ifeq ($(KBUILD_SRC)/,$(dir $(CURDIR)))
204 # building in a subdirectory of the source tree
205 srctree := ..
206 else
207 srctree := $(KBUILD_SRC)
208 endif
209 endif
210 objtree := .
211 src := $(srctree)
212 obj := $(objtree)
213
214 VPATH := $(srctree)$(if $(KBUILD_EXTMOD),:$(KBUILD_EXTMOD))
215
216 export srctree objtree VPATH

The external module compilation process is the same as uboot , and the values ​​of the three variables srctree , objtree and VPATH are finally exported.
Among them, srctree=. , which is the current directory, and objtree is also " . ".

8. Set target architecture and cross-compiler

Like uboot , when compiling Linux , you need to set the target board architecture ARCH and cross-compiler CROSS_COMPILE .
The code in the top-level Makefile is as follows:
252 ARCH ?= $(SUBARCH)
253 CROSS_COMPILE ?= $(CONFIG_CROSS_COMPILE:"%"=%)
For convenience, generally directly modify ARCH and CROSS_COMPILE in the top-level Makefile and set them directly.
for the corresponding architecture and compiler. For example, change this to:
252 ARCH ?= arm
253 CROSS_COMPILE ?= arm-linux-gnueabihf-

9. Call scripts/Kbuild.include file

Like uboot , the Linux top-level Makefile will also call the file scripts/Kbuild.include . The top-level Makefile is also
The code should be as follows:
349 scripts/Kbuild.include: ;
350 include scripts/Kbuild.include

10. Set cross-compilation tool variables

Other cross-compiler-related variables in the top-level Makefile are set as follows:
353 AS = $(CROSS_COMPILE)as
354 LD = $(CROSS_COMPILE)ld
355 CC = $(CROSS_COMPILE)gcc
356 CPP = $(CC) -E
357 AR = $(CROSS_COMPILE)ar
358 NM = $(CROSS_COMPILE)nm
359 STRIP = $(CROSS_COMPILE)strip
360 OBJCOPY = $(CROSS_COMPILE)objcopy
361 OBJDUMP = $(CROSS_COMPILE)objdump
LA , LD , CC , etc. are all tools used by cross-compilers.

11. Header file path variable

The top-level Makefile defines two variables to save the header file path: USERINCLUDE and LINUXINCLUDE , respectively.
The relevant code is as follows:
381 USERINCLUDE := \
382 -I$(srctree)/arch/$(hdr-arch)/include/uapi \
383 -Iarch/$(hdr-arch)/include/generated/uapi \
384 -I$(srctree)/include/uapi \
385 -Iinclude/generated/uapi \
386 -include $(srctree)/include/linux/kconfig.h
387
388 # Use LINUXINCLUDE when you must reference the include/ directory.
389 # Needed to be compatible with the O= option
390 LINUXINCLUDE := \
391 -I$(srctree)/arch/$(hdr-arch)/include \
392 -Iarch/$(hdr-arch)/include/generated/uapi \
393 -Iarch/$(hdr-arch)/include/generated \
394 $(if $(KBUILD_SRC), -I$(srctree)/include) \
395 -Iinclude \
396 $(USERINCLUDE)

Lines 381 ~386 are USERINCLUDE , which is the header file path related to UAPI , and lines 390~396 are LINUXINCLUDE, which is the header file path of the Linux kernel source code.

12. Export variables

The top-level Makefile will export many variables for use by sub- Makefiles . The exported variables are as follows:
417 export VERSION PATCHLEVEL SUBLEVEL KERNELRELEASE KERNELVERSION
418 export ARCH SRCARCH CONFIG_SHELL HOSTCC HOSTCFLAGS CROSS_COMPILE AS 
LD CC
419 export CPP AR NM STRIP OBJCOPY OBJDUMP
420 export MAKE AWK GENKSYMS INSTALLKERNEL PERL PYTHON UTS_MACHINE
421 export HOSTCXX HOSTCXXFLAGS LDFLAGS_MODULE CHECK CHECKFLAGS
422
423 export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS 
LDFLAGS
424 export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE CFLAGS_GCOV 
CFLAGS_KASAN
425 export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE
426 export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE 
KBUILD_LDFLAGS_MODULE
427 export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL
428 export KBUILD_ARFLAGS

The next article continues to understand the preparation of the top-level Makefile of the Linux kernel source code.

Guess you like

Origin blog.csdn.net/wojiaxiaohuang2014/article/details/133105841