Linux kconfig and makefile Learning

Source: Internet
Author: User
Linux kconfig and makefile Learning)

Reprinted from: http://blog.chinaunix.net/uid-20769502-id-147170.html

The Directory of the kernel source code tree contains two documents: kconfig (config. In version 2.4) and makefile. The kconfig distributed to each directory constitutes a distributed Kernel configuration database. Each kconfig describes the Kernel configuration menus related to the source document of the directory. When you configure make menuconfig in the kernel, read the menu from kconfig and save it to the Kernel configuration document of. config. When the kernel is compiled, the main makefile calls this. config, and the user's choice is known.


The above content describes,Kconfig is the configuration menu corresponding to the kernel.If you want to add a new driver to the kernel source code, you can modify kconfig so that you can select this driver,To compile the driver, modify the makefile.Therefore, there are two types of documents to be modified when adding a new driver (note not only two)
* Kconfig
* Makefile

Bytes ---------------------------------------------------------------------------------------------
Kconfig

1. first, let's take a look at the kconfig Syntax:
A typical Kernel configuration menu is as follows:
menu "network device support"
config netdevices
bool "enable net devices"
depends on net
default Y
help
This is help desciption.
...
endmenu
the content contained in menu/endmenu is a sub-menu of network device support. Each sub-menu item is defined by config. The bool, depends on, default, and help attributes under congfig are config attributes used to define the type, dependency, default value, and help information of the menu item.

2. Add the following types:
Each config menu item must have a bool boolean type, tristate three-state (built-in, module, remove), string, Hex hexadecimal, and integer type.
For example:
Config hello_module
Bool "Hello test module"
The bool type can only be selected or not selected, and is displayed as []; the tristate type menu items include the option to compile to the kernel module, which is displayed as <>. If you choose to compile to the kernel module, in. generate a config_hello_module = m configuration in config. If the built-in configuration is selected, it is directly compiled into the kernel. generate a config_hello_module = y configuration in config. hex hexadecimal type is displayed ().

3. Directory level Iteration
Similar statement in kconfig: Source "Drivers/USB/kconfig"
It is used to include (or nest) New kconfig files, so that each directory can manage its own configuration content, so that you do not have to write those configurations in the same file for ease of modification and management.

Bytes ----------------------------------------------------------------------------------------------

Makefile

The 2.6 kernel makefile consists of five components:
1. The top-level makefile
2. The. config configuration file of the kernel
3. makefile related to the architecture under the arch/$ (ARCH) Directory
4. The makefile. * file under the S directory is a general rule for some makefiles.
5. About 500 kbuild makefile files under directories at different levels

The makefile on the top layer reads the content of the. config file, and is responsible for building the kernel and module. Arch makefile provides supplementary information about the architecture. The makefile in the S directory contains all the definitions and rules required to build the kernel based on the kbuild makefile.


Kbuild makefile
There are some different syntax rules for different components of makefiles. 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 core content of kbuild makefile mainly includes:

1. Target Definition
The target definition is used to define the content to be compiled as a module and the content to be compiled and linked to the kernel. For example:

OBJ-y + = Foo. o

It indicates that Foo. o needs to be compiled by the foo. C or Foo. s file and linked to the kernel, while obj-M indicates that the file should be compiled as a module. Objects in the obj-x format except y and M will not be compiled. The more common practice is to determine the file Compilation Method Based on the config _ variable of the. config file. (for how the variable works, see the other article at the end of this article.Article), Such:
OBJ-$ (config_ext2) + = ext2.o

In addition to objects in the obj-format, there are also Lib-y library and hostprogs-y hostProgramBut they are basically applied to specific directories and scenarios.


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 and usesModule name plus-objs suffix or-y suffix to define the module composition File. For example:
OBJ-$ (config_ext2) + = ext2.o
Ext2-y: = balloc. O bitmap. o

Or, it can be written in the form of-objs:
OBJ-$ (config_ext2) + = ext2.o
Ext2-objs: = balloc. O bitmap. o

The module name is ext2. If the config_ext2 value is m. O and bitmap. O the final link between the two destination files generates the ext2.o until the ext2.ko file, if the config_ext2 value is Y, the generated ext2.o will be linked to the built-in.o and eventually linked to the kernel.


3. Directory level Iteration
For example:
OBJ-$ (config_ext2) + = ext2/

If the config_ext2 value is Y or M, kbuild will include the ext2 directory in the target of the next iteration.

Bytes ----------------------------------------------------------------------------------------------

Module Compilation

When compiling a module,You can place the module inCodeIn the tree, use make modules to compile your module,
The makefile content is very simple, for example, obj-$ (config_ext2) + = ext2.o.

YouYou can also place the module-related file directories outside the code tree.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.
Of course, we can also write a makefile for it. here we will introduce a general makefile (version 2.6 ):

# Makefile2.6
Ifneq ($ (kernelrelease ),)
# Kbuild syntax. Dependency relationshsip of files and target modules are listed here.

OBJ-M: = Hello. o
Hello-objs: = Hello. o
Else
PWD: = $ (shell PWD)
Kdir: =/lib/modules/$ (shell uname-R)/build
ALL:
$ (Make)-C $ (kdir) M = $ (PWD) Modules
Clean:
Rm-RF. *. CMD *. O *. Mod. C *. KO. tmp_versions
Endif

In this example, the driver directory is "hello". In practice, replace "hello" with your own directory name. The code tree path is automatically obtained. Then, run the make command in the directory.

Bytes ----------------------------------------------------------------------------------------------

When adding a new driver, you need to create the kconfig and makefile files, and modify the parent directory kconfig and makefile to include your own driver. For example, see:Add a new driver to the android kernel and add it to the menuconfig menu [Practice Notes]

The linux2.6.x/documentation/kbuild Directory provides a detailed introduction to kconfig and makefile.

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.