Makefile syntax analysis of linux2.6 Kernel

Source: Internet
Author: User
1.1 Overview
The makefile write Method and Application of 2.6 have some limitations compared with 2.4.
Changes may be caused by many people who cannot find the relevant documents. They all imitate the existing files in the kernel to write their makefiles. In fact, in the kernel documentation
/
In the kbuild directory, the kernel makefile syntax is described in detail. Here we will provide a translation and induction for the most common simple application of makefile in the 2.6 kernel.
Shao.

The 2.6 kernel makefile consists of five components:

L top-level makefile
L kernel. config configuration file
L makefile related to the architecture under the arch/$ (ARCH) Directory
L The makefile. * file in the scripts/directory is a general rule for some makefiles.
L about 500 kbuild makefile files under directories at different levels

The makefile in the

top
layer reads the content of the. config file, and is generally responsible for building the kernel and module. Arch
makefile provides information about the supplemental architecture. The makefile in the scripts directory contains all the definitions and rules required to build the kernel based on the kbuild makefile
.
1.2 kbuild makefile
different components of makefiles have different syntax rules. The targets are also different. For most developers and users of kernel modules or device drivers, kbuild makefile files based on the kbuild architecture under the directories of different layers are most commonly exposed.
the syntax structure of kbuild makefile is very simple. The core content mainly includes
1.2.1 target definition
the target definition is used to define the content to be compiled as a module and the compilation link to the kernel.
for example,
obj-y + = Foo. O
indicates to be created by foo. C or Foo. the S file is compiled to get Foo. O and link it to the kernel, while obj-M indicates that the file is to be compiled as a module. Objects in the obj-x format except y and M will not be compiled.
the more common practice is based on. the Config _ variable of the config file determines the file compilation method, for example:
obj-$ (config_isdn) + = ISDN. O
obj-$ (config_isdn_ppp_bsdcomp) + = isdn_bsdcomp.o

In addition to objects in the obj-format, there are also targets such as Lib-y library and hostprogs-y host Program , however, they are basically applied to specific directories and scenarios.
1.2.2 multi-file module definition
the simplest kbuild makefile is enough in the form of a sentence in the previous section. If a module is composed of multiple files, it is a little more complicated, define the component file of the module by adding the-objs suffix or-y suffix. Example:
obj-$ (config_ext2_fs) + = ext2.o
ext2-y: = balloc. O bitmap. O
ext2-$ (config_ext2_fs_xattr) + = xattr. O
module
the block name is ext2, which is determined by balloc. O and bitmap. O generate ext2.o from the final link of the two target files
until ext2.ko file, whether xattr is included. O depends on the configuration of the Kernel configuration file. If the value of config_ext2_fs is Y, the
ext2.o generated during this process will be linked to the built-in.o and eventually linked to the kernel. Note that the directory where the kbuild
makefile is located should no longer contain source files with the same name as the module, such as ext2.c/ext2.s.

Or, it can be written in the form of-objs:
OBJ-$ (config_isdn) + = ISDN. o
ISDN-objs: = isdn_net_lib.o isdn_v110.o isdn_common.o

1.2.3 directory level Iteration
For example:
OBJ-$ (config_ext2_fs) + = ext2/
If the config_ext2_fs value is Y or M, kbuild will include the ext2 directory in the target of the next iteration, but its role is limited to this, the files under the ext2 directory are determined by whether to compile the files as modules, link them to the kernel, or the contents of the makefile under the ext2 directory.

1.2.4 module Compilation
When compiling a module, you can place the module inCodeIn the tree, use make modules to compile your module. You can also place the module-related file directory outside the code tree and use the following command to compile the module:
Make-C path/to/kernel/src M = $ PWD modules
-C specifies the location of the code tree. M = $ PWD or M = 'pwd' tells kbuild to return to the current directory to perform the build operation.

1.2.5 module Installation
When you need to install the module to a non-default location, you can use install_mod_path to specify a prefix, such:
Make install_mod_path =/Foo modules_install
The module will be installed in the/Foo/lib/modules directory.

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.