Makefile and kconfig in the kernel

Source: Internet
Author: User

I. Overview
In kernel compilation, it is very important to organize the files in each directory tree and compile the unique kernel according to the user configuration. To solve this problem, the kernel uses two types of files: makefie and kconfig. The kconfig distributed to each directory constitutes a distributed Kernel configuration database. Each kconfig describes the Kernel configuration menu related to the source document of the Directory, that is, we use the command make menuconfig (or xconfig) configuration menu, which consists of multiple layers. Each layer is generated by kconfig in each directory. You can select how to compile the kernel as needed, save the configuration results to. config, and then compile the kernel according to. config results when executing makefile.
This process is completed by the kbuild system. The Linux compiling system scans the Linux makefile twice: first, the compiling system reads the makefile at the top layer of the Linux kernel, then, compile the Linux Kernel Based on the read content and read the makefile of kbuild for the second time. The kernel compilation system or kbuild is a mechanism that allows you to select Kernel configuration options when compiling the kernel. 2.6 this mechanism has been updated in the kernel tree. The new version of kbuild is not only fast, but also provides more comprehensive documentation. The kbuild mechanism relies entirely on the source code hierarchy.
Ii. makefile
In the face of the tree-structured kernel source code directory, the kernel compilation uses each subdirectory to have its own directory-related makefile (called sub-makefile or kbuild makefile ), the kernel compilation depends on the sub-makefile files in each subdirectory. These Sub-makefiles define rules for building the target file based on the source files in the subdirectory, and only modify the files in this directory. The top-level makefile uses Recursion to call makefile files located in directories such as init/, drivers/, sound/, net/, lib/, and usr. Before recursive calling, kbuild must first determine whether certain conditions have been met, including updating include/Linux/version when necessary. h file, and set the symbolic link include/ASM to point to the file related to the target architecture. For example, if you compile code for PPC, include/ASM points to include/ASM-PPC. Kbuild also compiles include/Linux/Autoconf. h and include/Linux/config files. Then start recursion from the root directory.
Each sub-MAKEFILE file is relatively simple and points out how to compile the target file, such as The makefile fragment in the/mm directory:
16 obj-$ (config_proc_page_monitor) + = pagewalk. o
17 obj-$ (config_bounce) + = bounce. o
18 obj-$ (config_swap) + = page_io.o swap_state.o swapfile. O thrash. o
19 obj-$ (config_has_dma) + = dmapool. o
20 obj-$ (config_hugetlbfs) + = hugetlb. o
Iii. kconfig File
Kconfig defines some variables in kconfig to allow you to configure the kernel. You can choose how to personalize your system kernel by setting the value of the variable. The Defined variables are
Each menu has a keyword identifier, and the most common one is config.
Syntax:
Config
Symbol is a new marked menu item. options is the attribute and option under this new menu item.
The options section includes:
1. Type Definition:
Each config menu item must have a type definition, Boolean bool type, tristate three states: Built-in, module, Remove string, Hex hexadecimal, integer
For example, config hello_module
Bool "Hello test module"
The bool type can only be selected or not selected. The menu items of the tristate type have more options for compiling to the kernel module. If the option is compiled to the kernel module, it will be 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.
2. Dependency-defined depends on or requires
Indicates whether or not the menu appears depends on another definition.
Config hello_module
Bool "Hello test module"
Depends on arch_pxa
This example shows that the menu item hello_module is only valid for the XScale processor.
3. Definition of help
Just add the help keyword help or-help-
Iv.. config file
The Kernel configuration tool is used to automatically generate a Kernel configuration file named. config. This is the first step to compile the kernel .. The config file is located in the source code root directory and describes all Kernel configuration options. You can use the Kernel configuration tool to select these options. Each Kernel configuration option has related names and variable values. The name is like config _ <Name>, where <Name> is the identifier of related options, which is defined in the kconfig file. The variable can have three values: Y, m, or N. Y indicates "yes", indicating that the option will be compiled into the kernel source code or compiled into the system. M stands for "module", indicating that this option will be compiled into the kernel as a module. If this option is not selected (the variable value of this option is set to N, which indicates "no. the following comment is displayed in the config file: "Config _ <Name> is not set ".. The options in the config file are sorted by their locations in the Kernel configuration tool. The comments section shows the menu under which the options are located. Let's take a look at the excerpt from a. config file:
1 #
2 # automatically generated make config: Don't edit
3 #
4 config_x86 = y
5 config_mmu = y
6 config_uid16 = y
7 config_generic_isa_dma = y
8
9 #
10 # code maturity level options
11 #
12 config_experimental = y
13 config_clean_compile =
14 config_standalone = y
15 config_broken_on_smp = y
16
17 #
18 # General setup
19 #
20 config_swap = y
21 config_sysvipc = y
22 # config_posix_mqueue is not set
23 config_bsd_process_acct = y
Above. the config file indicates that the options from lines 4th to 7th are located in the top menu, the options from lines 12th to 15th are located in the Code maturity Options menu, and the options from lines 20th to 23rd are located in the general settings Options menu.
All configuration tools generate the above menu, and you can see that the first few options, code maturity options, and general settings options are on the top. The following two options are extended to submenus that contain multiple options. These menus are provided by the qconf Configuration tool when the xconfig command is called. The menus displayed by the configuration tool are used for the X86 architecture by default.
5. DIY: add your own program to the kernel
A. add your own program steps in the Linux kernel (note that this is only a program file ):
1. Copy the written source code to the corresponding directory of the Linux kernel source code.
2. Add the compilation configuration options of the project corresponding to the new source code to the kconfig file in the directory.
3. Add the compilation entries for the new source code to the MAKEFILE file in the directory.
B. Add directories and subdirectories to the drivers/directory of the Linux kernel:
1. The directory to be added is mydriver. The file is as follows:
Mydriver $ tree
|-Kconfig
|-Makefile
|-Key
|-Kconfig
|-Makefile
| '-Key. c
|-Led
|-Kconfig
|-Makefile
| '-Led. c
'-Test. c
# Note that the makefile and kconfig files in each directory are empty at this time.
2. Add the kconfig and makefile files in the newly added directory.
3. Modify the kconfig and makefile files of the parent directory of the newly added directory so that the newly added kconfig and
Makefile can be referenced. Add:
OBJ-y + = mydriver/
Contains the subdirectory mydriver directory during compilation. Then modify the kconfig file and add:
Source "drivers/mydriver/kconfig"
Reference the configuration file kconfig In the subdirectory mydriver during configuration.
4. After the preceding step, you can find the added directory mydriver in the parent directory, edit the makefile and kconfig files in each directory, and add makefile in the mydriver directory you added:
OBJ-$ (config_test) + = test. O # because the test. c file needs to be compiled in the mydriver directory
# Therefore, the compilation options are determined based on config_test.
OBJ-y + = led/# compile the LED subdirectory in the mydriver directory
OBJ-y + = key/# compile the sub-directory key in the mydriver directory
Then the kconfig file is:
Menu "test mydriver" # menu entry to be displayed during make menuconfig
Comment "test mydriver" # menu title
Config Test
Tristate "mydriver test"
Source "drivers/mydriver/led/kconfig" # Add kconfig in the led directory
Source "drivers/mydriver/key/kconfig"
Endmenu
Let's look at makefile and kconfig in the led directory:
Makefile is a file:
OBJ-$ (config_led) + = led. o
Kconfig file:
Config led
Tristate "led support"
The makefile in the Key Directory is similar to kconfig.
5. Now you can make menuconfig to configure and add the mydriver kernel in your directory!

 

 

 

From: http://blog.chinaunix.net/u2/74234/showart_2163347.html

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.