Linux Kernel configuration file. config and makefile

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/lchjustc/article/details/6925545

========================================================== =
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.

========================================================== =
Makefile explanation 2: 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 add 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 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.

========================================================== =
Makefile 3: 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-d1_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.

========================================================== =
Makefile Interpretation 4: comments 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 # subdirectory to be entered for compiling kernel, which is equal to subdir-y
Unexport all_sub_dirs # All subdirectories
Unexport mod_sub_dirs # subdirectory for the encoding Module
Unexport o_target # output object merged by LD
Unexport all_mobjs # All module names

Unexport obj-y # compile the file set into the 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 Kernel
Unexport subdir-M # subdirectory for the encoding Module
Unexport subdir-n
Unexport subdir-

#
# Get things started.
#
First_rule: sub_dirs
$ (Make) all_targets

# Filter out sub-directories that can be used as modules in the subdirectory of the internal core editor.
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 the c file into a compilation file rule. $ @ 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 rule, $ <is the first dependent object;
#
Generate the flags file in the directory of the target file. Strip deletes unnecessary spaces and replace the comma (,) with the colon (:) In SUBST.
.
%. 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 compiling. s files,
# But these Standard Rules are available for any makefile that
# Wants to use them. Our plan is to incrementally convert all
# The makefiles to these standard rules. -- RMK, MEC

Ifdef use_standard_as_rule
# Standard rules for generating pre-processed files from assembler files.
%. S: %. s
$ (CPP) $ (aflags) $ (extra_aflags) $ (aflags _ $ @) $ <>$ @
# Standard rules for generating the target file for an assembly file.
%. O: %. s
$ (CC) $ (aflags) $ (extra_aflags) $ (aflags _ $ @)-c-o $ @ $ <

Endif
# C file generation debugging list file rules, $ * extended as the target main file name.
%. Lst: %. c
$ (CC) $ (cflags) $ (extra_cflags) $ (cflags _ $ @)-g-c-o $ *. o $ <
$ (Topdir)/scripts/makelst $ * $ (topdir) $ (objdump)
#
#
#
All_targets: $ (o_target) $ (l_target)

#
# Rule to compile a set of. O files into one. o file
#
Ifdef o_target
$ (O_target): $ (obj-y)
Rm-F $ @
# $ ^ Expands to all dependent objects. If obj-y is null, a library file with the same name and null is generated.
Ifneq "$ (Strip $ (obj-y ))"""
$ (LD) $ (extra_ldflags)-R-o $ @ $ (filter $ (obj-y), $ ^)
Else
$ (AR) RCS $ @
Endif
# Generate shell statements for the flags file.
@(\
Echo 'ifeq ($ (Strip $ (SUBST $ (comma),:, $ (extra_ldflags)
$ (Obj-y), $ (Strip $ (SUBST $ (comma),:, $ (extra_ldflags) $ (obj-y )))) ';
\
Echo 'files _ flags_up_to_date + =$ @';\
Echo 'endif '\
)> $ (DIR $ @)/. $ (notdir $ @). flags
Endif # o_target

#
# Rule to compile a set of. O files into one. A file
#
# Combine obj-y into the library l_target.
Ifdef l_target
$ (L_target): $ (obj-y)
Rm-F $ @
$ (AR) $ (extra_arflags) RCS $ @ $ (obj-y)
@(\
Echo 'ifeq ($ (Strip $ (SUBST $ (comma),:, $ (extra_arflags)
$ (Obj-y), $ (Strip $ (SUBST $ (comma),:, $ (extra_arflags) $ (obj-y )))) ';
\
Echo 'files _ flags_up_to_date + =$ @';\
Echo 'endif '\
)> $ (DIR $ @)/. $ (notdir $ @). flags
Endif

#
# This make dependencies quickly
#
# Wildcard is the macro used to find the file name in the directory.
Fastdep: dummy
$ (Topdir)/scripts/mkdep $ (wildcard *. [chs] local. H. Master)>. depend
Ifdef all_sub_dirs
#
Add the directory name in all_sub_dirs with the prefix _ sfdep _ as the target running sub-make, and set all_sub_dirs
Pass
# The Variable _ fastdep_all_sub_dirs is passed to the sub-make.
$ (Make) $ (patsubst %, _ sfdep _ %, $ (all_sub_dirs ))
_ Fastdep_all_sub_dirs = "$ (all_sub_dirs )"
Endif

Ifdef _ fastdep_all_sub_dirs
#
In contrast to the previous section, define the subdirectory target, restore the target name to the directory name, and enter the subdirectory make.
$ (Patsubst %, _ sfdep _ %, $ (_ fastdep_all_sub_dirs )):
$ (Make)-C $ (patsubst _ sfdep _ %, %, $ @) fastdep
Endif

#
# A rule to make subdirectories
#
# Complete make in the kernel compilation subdirectory in the following two sections.
Subdir-list = $ (sort $ (patsubst %, _ subdir _ %, $ (sub_dirs )))
Sub_dirs: dummy $ (subdir-list)

Ifdef sub_dirs
$ (Subdir-list): dummy
$ (Make)-C $ (patsubst _ subdir _ %, %, $ @)
Endif

#
# A rule to make modules
#
# Find a valid module File Table.
All_mobjs =$ (filter-out $ (obj-y), $ (obj-m ))
Ifneq "$ (Strip $ (all_mobjs ))"""
# Obtain the path from the main directory topdir to the current directory.
Pdwn = $ (shell $ (config_shell) $ (topdir)/scripts/pathdown. Sh)
Endif

Unexport mod_dirs
Mod_dirs: = $ (mod_sub_dirs) $ (mod_in_sub_dirs)
# Enter the sub-directory of the module when compiling the encoding module.
Ifneq "$ (Strip $ (mod_dirs ))"""
. Phony: $ (patsubst %, _ modsubdir _ %, $ (mod_dirs ))
$ (Patsubst %, _ modsubdir _ %, $ (mod_dirs): dummy
$ (Make)-C $ (patsubst _ modsubdir _ %, %, $ @) Modules
# Enter the sub-directory of the module when installing the module.
. Phony: $ (patsubst %, _ modinst _ %, $ (mod_dirs ))
$ (Patsubst %, _ modinst _ %, $ (mod_dirs): dummy
$ (Make)-C $ (patsubst _ modinst _ %, %, $ @) modules_install
Endif

# Make modules entry.
. Phony: modules
Modules: $ (all_mobjs) dummy \
$ (Patsubst %, _ modsubdir _ %, $ (mod_dirs ))

. Phony: _ modinst __
# Process of copying a module.
_ Modinst __: dummy
Ifneq "$ (Strip $ (all_mobjs ))"""
Mkdir-p $ (modlib)/kernel/$ (pdwn)
CP $ (all_mobjs) $ (modlib)/kernel/$ (pdwn)
Endif

# Make modules_install.
. Phony: modules_install
Modules_install: _ modinst __\
$ (Patsubst %, _ modinst _ %, $ (mod_dirs ))

#
# A rule to do nothing
#
Dummy:

#
# This is useful for testing
#
Script:
$ (SCRIPT)

#
# This sets version suffixes on exported symbols
# Separate the object into "normal" objects and "exporting" Objects
# Exporting objects are: all objects that define symbol tables
#
Ifdef config_modules
# List-Multi lists modules composed of multiple files;
# Filter out the composite module names from the editing and editing file tables and module File tables.
Multi-used: = $ (filter $ (list-Multi), $ (obj-y) $ (obj-m ))
# Obtain the composition table of the composite module.
Multi-objs: = $ (foreach M, $ (Multi-used), $ (basename $ (M)-objs ))
# Obtain the total module table to be compiled.
Active-objs: = $ (sort $ (Multi-objs) $ (obj-y) $ (obj-m ))

Ifdef config_modversions
Ifneq "$ (Strip $ (export-objs ))"""
# If any file requires versioning.
Modincl = $ (topdir)/include/Linux/modules

# The-W option (enable warnings) for genksyms will return here in 2.1
# So Where has it gone?
#
# Added the SMP separator to stop module accidents between uniprocessor
# And SMP intel boxes-ac-from BITs by Michael Chastain
#

Ifdef config_smp
Genksyms_smp_prefix: =-p smp _
Else
Genksyms_smp_prefix: =
Endif
# Rules for calculating version files from source files.
$ (Modincl)/%. Ver: %. c
@ If [! -R $ (modincl)/$ *. Stamp-o $ (modincl)/$ *. Stamp-ot $ <]; then \
Echo '$ (CC) $ (cflags)-E-d1_genksyms _ $ <';\
Echo '| $ (genksyms) $ (genksyms_smp_prefix)-K
$ (Version). $ (patchlevel). $ (sublevel) >$ @. tmp ';\
$ (CC) $ (cflags)-E-d1_genksyms _ $ <\
| $ (Genksyms) $ (genksyms_smp_prefix)-K
$ (Version). $ (patchlevel). $ (sublevel) >$ @. tmp ;\
If [-r $ @] & CMP-S $ @. tmp; Then ECHO $ @ is unchanged; RM-F
$ @. Tmp ;\
Else echo MV $ @. tmp $ @; MV-F $ @. tmp $ @; FI ;\
FI; touch $ (modincl)/$ *. Stamp
#
Change the extension of the source file for version processing to. Ver, and add the complete path name. They depend on Autoconf. h? BR>? BR> $ (addprefix $ (modincl)/, $ (export-objs:. O =. Ver )):
$ (Topdir)/include/Linux/Autoconf. h

# Updates. Ver files but not modversions. h
# Use fastdep to generate version files corresponding to export-objs one by one.
Fastdep: $ (addprefix $ (modincl)/, $ (export-objs:. O =. Ver ))

# Updates. Ver files and modversions. h like before (is this needed ?)
# Make Dep process entry
Dep: fastdep Update-modverfile

Endif # export-objs

# Update modversions. h, but only if it wocould change
# Refresh the version file.
Update-modverfile:
@ (Echo "# ifndef _ linux_modversions_h ";\
Echo "# DEFINE _ linux_modversions_h ";\
Echo "# include <Linux/modsetver. h> ";\
CD $ (topdir)/include/Linux/modules ;\
For f in *. Ver; do \
If [-F $ F]; then Echo "# include <Linux/modules/$ {f}>"; FI ;\
Done ;\
Echo "# endif ";\
)> $ (Topdir)/include/Linux/modversions. H. tmp
@ If [-r $ (topdir)/include/Linux/modversions. H] & CMP-S
$ (Topdir)/include/Linux/modversions. h
$ (Topdir)/include/Linux/modversions. H. tmp; then \
Echo $ (topdir)/include/Linux/modversions. h was not updated ;\
Rm-F $ (topdir)/include/Linux/modversions. H. tmp ;\
Else \
Echo $ (topdir)/include/Linux/modversions. h was updated ;\
MV-F $ (topdir)/include/Linux/modversions. H. tmp
$ (Topdir)/include/Linux/modversions. h ;\
Fi
$ (Active-objs): $ (topdir)/include/Linux/modversions. h

Else
# Modversions. H if no version is configured.
$ (Topdir)/include/Linux/modversions. h:
@ Echo "# include <Linux/modsetver. h>" >$ @

Endif # config_modversions

Ifneq "$ (Strip $ (export-objs ))"""
# Compile Method for versioning target files.
$ (Export-objs): $ (export-objs:. O =. c) $ (topdir)/include/Linux/modversions. h
$ (CC) $ (cflags) $ (extra_cflags) $ (cflags _ $ @)-dexport_symtab-C $ (@:. O =. c)
@(\
Echo 'ifeq ($ (Strip $ (SUBST $ (comma),:, $ (cflags) $ (extra_cflags)
$ (Cflags _ $ @)-dexport_symtab), $ (Strip $ (SUBST $ (comma),:, $ (cflags)
$ (Extra_cflags) $ (cflags _ $ @)-dexport_symtab )))';\
Echo 'files _ flags_up_to_date + =$ @';\
Echo 'endif '\
)> $ (DIR $ @)/. $ (notdir $ @). flags
Endif

Endif # config_modules

#
# Include dependency files if they exist
#
# Dependency between embedded source files.
Ifneq ($ (wildcard. Depend ),)
Include. depend
Endif
# Dependency between embedded header files.
Ifneq ($ (wildcard $ (topdir)/. hdepend ),)
Include $ (topdir)/. hdepend
Endif

#
# Find Files whose flags have changed and force recompilation.
# For safety, this works in the Converse Direction:
# Every file is forced, except T those whose flags are positively
Up-to-date.
#
# List of updated files.
Files_flags_up_to_date: =

# For use in expunging commas from flags, which Mung our checking.
Comma =,
# Embed all flags files in the current directory.
Files_flags_exist: = $ (wildcard. *. Flags)
Ifneq ($ (files_flags_exist ),)
Include $ (files_flags_exist)
Endif
# Delete files that do not need to be updated from the total object.
Files_flags_changed :=$ (strip \
$ (Filter-out $ (files_flags_up_to_date ),\
$ (O_target) $ (l_target) $ (Active-objs )\
))

# A kludge:. s files don't get flag dependencies (yet ),
# Because that will involve changing a lot of makefiles. Also
# Suppress object files explicitly listed in $ (ignore_flags_objs ).
# This allows handling of assembly files that get translated
# Multiple object files (see ARCH/IA64/lib/idiv. S, for example ).
#
# Delete the object file generated by the Assembly file from files_flags_changed.
Files_flags_changed :=$ (strip \
$ (Filter-out $ (patsubst %. S, %. O, $ (wildcard *. s)
$ (Ignore_flags_objs )),\
$ (Files_flags_changed )))
# Set files_flags_changed to the target.
Ifneq ($ (files_flags_changed ),)
$ (Files_flags_changed): dummy
Endif
</PRE>

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.