Explanation of the Linux kernel configuration file Makefile

Source: Internet
Author: User
Article title: explanation of the Linux kernel configuration file Makefile. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
   1. Makefile
  
There are two Linux kernel configuration files. one is implicit. the config file is embedded in the main Makefile; the other is include/linux/autoconf. h. embedded in various c source files, which are created by the make config, make menuconfig, and make xconfig processes.
  
Almost all source files are stored in linux/config. h and embedded in autoconf. h. If the file dependency (. depend), as long as the autoconf. h, it will cause re-coding of all source code.
  
To optimize the make process and reduce unnecessary re-coding, a dedicated mkdep tool is developed in Linux to replace gcc to generate the. depend file. When processing source files, mkdep ignores header files such as linux/config. h to identify rows with the "CONFIG _" feature in the macro commands of the source files.
  
For example, if there is a line like "# ifdef CONFIG_SMP", it will be in. output $ (wildcard/usr/src/linux/include/config/smp. h ).
  
The file under include/config/is generated from autoconf. h by another tool split-include. it uses the CONFIG _ mark in autoconf. h to generate the file corresponding to mkdep. For example, if the autoconf. h contains the line "# undef CONFIG_SMP", it generates the include/config/smp. h file with the content "# undef CONFIG_SMP ". These file names only appear in the. depend file, and the kernel source files are not embedded with them.
  
Run split-include every time the kernel is configured. Split-include checks the content of the old sub-files and determines whether to update them. In this way, make will not re-compile the kernel, regardless of the date modified by autoconf. h.
  
If the system's encoding option changes, Linux can also perform incremental encoding. To do this, make generates a flags file for each compilation of a source file. For example, when compiling sched. c, an implicit. sched. o. flags file is generated under the same directory. It is a piece of Makefile. when make enters a subdirectory to compile and compile, it searches for the flags files and embeds them into Makefile.
  
These flags codes test whether the current encoding option is the same as the original one. if the options are the same, add the corresponding target file to the FILES_FLAGS_UP_TO_DATE list. then, the system deletes them from the encoding object table, get the FILES_FLAGS_CHANGED list, and set them as the target for update.
  
The next step is to analyze the Makefile code in depth.
  
   II. Makefile explanation: sub-make
  
Makefile is available in subdirectories of kernel source code at all levels in Linux. most makefiles must be embedded in Rule. make and Rule. make in the main directory to identify the variables defined in each Makefile. The variable obj-y indicates the set of target file names to be compiled into the kernel. defining O_TARGET indicates connecting obj-y to a target file named O_TARGET, define Rochelle target to merge obj-y into a library file named Rochelle target. Similarly, obj-m indicates the set of target file names to be compiled into modules.
  
If you still need to make sub-directories, you need to define subdir-y and subdir-m. In Makefile, use "obj-$ (CONFIG_BINFMT_ELF) + = binfmt_elf.o" and "subdir-$ (CONFIG_EXT2_FS) + = ext2 "automatically adds file names for obj-y, obj-m, subdir-y, and subdir-m. Sometimes, the situation is not so simple, but you also need to use conditional statements to treat it individually. Makefile contains other variables, such as mod-subdirs, which defines all sub-directories of modules other than subdir-m.
  
How does Rules. make enter sub-directories?
  
First, let's take a look at how subdir-y is handled in Rules. in make, add the prefix "_ subdir _" to each file name in subdir-y and then sort it to generate the subdir-list set, and then use it as the target set, create a sub-make for each target, and remove the prefix of the target name to obtain the sub-directory name as the starting directory parameter of the sub-make. Subdir-m is similar to subdir-y, but the situation is slightly more complicated.
  
Subdir-y may have a module definition. Therefore, use the mod-subdirs variable to extract the module directory in subdir-y and then combine it with subdir-m to form a large set of MOD_SUB_DIRS. The target prefix of subdir-m is "_ modsubdir _".
  
One note is that both Makefile and Rules. make in the subdirectory are not embedded in the. config file, which is completed by passing the MAKEFILES variable down through the main Makefile. MAKEFILES is a variable recognized by make. before executing a new Makefile, make loads the files specified by MAKEFILES. In the main Makefile, it points to. config.
  
   III. versioning of modules
  
The module version is a method for strict type matching between the kernel and the module interface. After CONFIG_MODVERSIONS is configured in the kernel, the make dep operation will generate a file with the extension. ver for the source files corresponding to the export-objs variables in makefiles of all levels under the include/linux/modules/directory.
  
For example, for kernel/ksyms. c, make uses the following command to generate the corresponding ksyms. ver:
  
Gcc-E-dashes kernel __
-D1_genksyms _ ksyms. c
|/Sbin/genksyms-k 2.4.1
> Ksyms. ver
  
-D1_genksyms _ is used to make the EXPORT_SYMBOL macro in ksyms. c not extended. The genksyms command identifies the function name and prototype in EXPORT_SYMBOL (), and then calculates the version number of the function based on the prototype. For example, there is a row in ksyms. c:
  
EXPORT_SYMBOL (kmalloc );
  
The kmalloc prototype is:
  
Void * kmalloc (size_t, int );
  
The output of the genksyms program is:
  
# Define _ ver_kmalloc 93d4cfe6
# Define kmalloc _ set_ver (kmalloc)
  
In the kernel symbol table and module, kmalloc is changed to kmalloc_R93d4cfe6. After all. ver files are generated, make will re-build the include/linux/modversions. h file, which contains a series of # include command lines embedded into each. ver file.
  
When compiling files in the kernel itself export-objs, make adds a "-DEXPORT_SYMTAB" encoding mark, which embeds the source file into modversions. H file, which extends the version name of the function name string expanded from the EXPORT_SYMBOL macro. It also defines the _ set_ver () macro as an empty operation so that the function name in the code is not affected.
  
When compiling the functions module, make adds the "-include = linux/modversion. h-DMODVERSIONS" encoding mark to extend the function name of the code in the module.
  
Because it is time-consuming to generate a. ver file, make also creates a timestamp file with the suffix. stamp for each. ver file. During make dep, if the. stamp file is earlier than the source file, the. ver file is regenerated; otherwise, the file is only updated with the timestamp file. In addition, when generating. ver and modversions. h files, make will compare the content of the new and old files to keep them modified at the oldest time.
  
   4. notes of Rules. make
  
[Code: 1: 974578564b]
#
# This file contains rules which are shared between multiple Makefiles.
#
  
#
# False targets.
#
#
. PHONY: dummy
  
#
# Special variables which shocould not be exported
#
# Cancel these variables passed to the make sub-process through the environment.
Unexport EXTRA_AFLAGS
# As switch
Unexport EXTRA_CFLAGS
# Cc switch
Unexport EXTRA_LDFLAGS
# Ld switch
Unexport EXTRA_ARFLAGS
# Ar switch
Unexport SUBDIRS
#
Unexport SUB_DIRS
# Compile the subdirectory to which the kernel needs to enter,
Equal to subdir-y
Unexport ALL_SUB_DIRS
# All subdirectories
Unexport MOD_SUB_DIRS
# Subdirectory to be accessed by the editing module
Unexport O_TARGET
# Output object merged by ld
Unexport ALL_MOBJS
# All module names
  
Unexport obj-y
# Compile a file set into a kernel
Unexport obj-m
# Compile into a module file set
Unexport obj-n
#
Unexport obj-
#
Unexport export-objs
# File set for version processing
Unexport subdir-y
# Subdirectory for compiling the kernel
Unexport subdir-m
# Subdirectory to be entered for the editing module
Unexport subdir-n
Unexport subdir-
  
#
# Get things started.
#
First_rule: sub_dirs
$ (MAKE) all_targets
# Filter out the content in the subdirectory of the Kernel editor
It can be used as a sub-directory of a module.
Both-m
: =$ (Filter $ (mod-subdirs ),
$ (Subdir-y ))
SUB_DIRS: = $ (subdir-y)
# Obtain the subdirectory of the total module
MOD_SUB_DIRS: =
$ (Sort $ (subdir-m)
$ (Both-m ))
# Obtain the total subdirectories
ALL_SUB_DIRS: = $ (sort
$ (Subdir-y) $ (subdir-m)
$ (Subdir-n) $ (subdir -))
#
# Common rules
#
# Compile c files into assembly files,
$ @ Is the target object.
%. S: %. c
$ (CC) $ (CFLAGS)
$ (EXTRA_CFLAGS) $ (CFLAGS _ $ @)
-S $ <-o $ @
# Create a pre-processing file rule for file c.
%. I: %. c
$ (CPP) $ (CFLAGS) $ (EXTRA_CFLAGS)
$ (CFLAGS _ $ @) $ <>$ @
# Compile the c file into the target file,
$ <为第一个所依赖的对象;
#
Generate the flags file in the directory of the target file,
Strip deletes unnecessary spaces,
Subst, replace comma with Colon
.
%. O: %. c
$ (CC) $ (CFLAGS)
$ (EXTRA_CFLAGS)
$ (CFLAGS _ $ @)-c-o $ @ $ <
@ (Echo 'ifeq
($ (Strip $ (subst $ (comma ),:,
$ (CFLAGS) $ (EXTRA_CFLAGS)
$ (CFLAGS _ $ @))),
$ (Strip $ (subst
$ (Comma),:, $ (CFLAGS)
$ (EXTRA_CFLAGS)
$ (CFLAGS _ $ @) '; echo'
FILES_FLAGS_UP_TO_DATE + =$ @';
Echo'
Endif'
)> $ (Dir $ @)/. $ (notdir $ @). flags
# Rules for generating the target file for an assembly file.
%. O: %. s
$ (AS) $ (AFLAGS)
$ (EXTRA_CFLAGS)-o $ @ $ <
  
# Old makefiles define
Their own rules for c
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.