Makefile in Linux Kernel

Source: Internet
Author: User
For the kernel, makefiles are classified into five categories:

Documentation/kbuild/makefiles.txt is described as follows:

50 The makefiles have five parts:

51

52 makefile total makefile, controlling kernel Compilation

53. config Kernel configuration file, generated when the kernel is configured, such as after make menuconfig

54 ARCH/$ (ARCH)/makefile corresponding to the architecture

55 scripts/makefile. * rules shared by makefile, form the configuration interface.

56. makefile under each subdirectory of kbuild makefiles is called by makefile on the upper layer.

 

To put it simply, compiling the kernel will do the following.

1. Make menuconfig

1.1 copy a configuration file of the corresponding architecture to the home directory and change it. in this way, some default configurations are available in the graphic configuration generated by make menuconfig to reduce the labor volume of users.

1.2 The makefile in the top-level directory of the kernel determines the compiled architecture (ARCH). The compilation tool (cross_compile) and the directory to be compiled.

1.3 enter the directory of the corresponding architecture according to the arch variable of the total makefile, read ARCH/$ (ARCH)/makefile, and decide which directories under the corresponding architecture need to be compiled.

1.4 According to arch/$ (ARCH)/makefile, The makefile under the specified directory is recursively accessed one by one, the configuration interface is generated based on the kconfig in the Directory and the user decides whether to compile the file into a module or into the kernel.

1.5 save and exit after configuration. The original. config content will be changed.

2. Make

1.1 remove the comments from the generated. config file and create a new configuration file named include/config/auto. conf.

1.2 generate a subdirectory of the file to be compiled according to the requirements of the configuration file. O or. file a, and then the connection script ARCH/$ (ARCH)/kernel/vmlinux specified by the total makefile. LDS generates vmlinux, compresses it into bzimage, or compiles it into a module under the corresponding subdirectory as required.

 

But how does one generate a configuration file?

Note: The kernel I use has been modified. It may be different from the original kernel in some places. For example, $ (ARCH) in my kernel is written as $ (srcarch ). The number of lines in the file is inconsistent with that in the original kernel, but this does not affect the analysis. You can search for the lines.

1. In the total makefile, enter the directory to be compiled according to the following statements

470 # objects we will link into vmlinux/subdirs we need to visit

471 init-Y: = init/

472 drivers-Y: = Drivers/sound/firmware/

473 net-Y: = net/

474 libs-Y: = lib/

475 core-Y: = usr/

476 endif # kbuild_extmod

639 core-y + = kernel/MM/fs/IPC/security/crypto/block/

# Another system-related arch directory

529 include $ (srctree)/ARCH/$ (srcarch)/makefile

 

In this way, the system structure determines the directory to be compiled.

2. the directory contained in the total makefile is not enough. The kernel also needs to decide which subdirectories need to be compiled into the kernel based on the corresponding CPU architecture, in the total makefile, read the makefile: ARCH/$ (srcarch)/makefile of the corresponding architecture.

Sub-directories in the total makefile and system architecture ARCH/(srcarch)/makefile will be compiled into modules or into the kernel according to the makefile requirements in the directory, of course, you can also skip compilation.

 

For example, under ARCH/ARM/makefile:

187 # If we have a machine-specific directory, then include it in the build.

188 core-y + = ARCH/ARM/kernel/ARCH/ARM/MM/ARCH/ARM/common/

189 core-y + = $ (machdirs) $ (platdirs)

190 core-$ (config_fpe_nwfpe) + = ARCH/ARM/nwfpe/

191 core-$ (config_fpe_fastfpe) + = $ (fastfpe_obj)

192 core-$ (config_vfp) + = ARCH/ARM/VFP/

193

194 drivers-$ (config_oprofile) + = ARCH/ARM/oprofile/

195

196 libs-Y: = ARCH/ARM/lib/$ (libs-y)

 

 

Among them, y indicates compiling into a module, M indicates compiling into the kernel (not above, because arm is all compiled into the kernel by default), but what is $ (config_oprofile?

The generated values are assigned to config_oprofile Based on the settings in make menuconfig. This option is selected and configured by kconfig in each subdirectory. Such as ARCH/ARM/kconfig.

In addition, some configurations read the kconfig sub-directories Based on the arch/$ (ARCH)/kconfig file and generate a configuration interface. Each makefile directory has a corresponding kconfig file, which is used to generate the configuration interface to determine how the kernel is configured.

Summarize the functions of kconfig:

2. 1. You can configure options under make menuconfig;

2. In. config, determine the value of config_xxx.

3. It is not enough to read the above two makefiles. The kernel will also read the makefile and kconfig in the contained subdirectories layer by layer.

 

Suppose I configure the kernel now

1. The simplest method is to directly modify the makefile of the subdirectory.

For example, if I want to cancel the S3C2440 clock (of course, this must be enabled, just for example ).

ARCH/ARM/mach-s3c2440/makefile can be directly modified

12 obj-$ (config_cpu_s3c2440) + = S3C2440. o dsc. o

13 obj-$ (config_cpu_s3c2440) + = IRQ. o

14 obj-$ (config_cpu_s3c2440) + = clock. o

15 obj-$ (config_s3c2440_dma) + = DMA. o

 

Change obj-$ (config_cpu_s3c2440) + = clock. O

OBJ-+ = clock. o

You can also compile it into a module:

OBJ-M + = clock. o

2. Of course, it is more convenient to use the graphic interface, make menuconfig. Next I will implement how to add an option to the graphic configuration interface.

 

2. 1. Enter the kernel directory

CD linux-2.6.29

2. simulate a folder named test1 driver under the driver directory

Mkdir driver/test1

2. 3. Write a c file in the test1 directory.

CD driver/test1

Vim test1.c

1 void Foo ()

2 {

3;

4}

 

. Write a simple makefile in the directory

Vim makefile

OBJ-$ (config_test1) + = test1.o

Config_test1 determines whether test1 is compiled into the kernel or a module. This is selected by the user in make menuconfig through kconfig.

. Therefore, you need to write a kconfig file in the directory.

Vim kconfig

1 menu "test1 driver here"

2 config test1

3 bool "Xiaobai test1 driver"

4 help

5 This is test1

6 endmenu

 

 

To put it bluntly, a configuration option is added under the driver of the graphic configuration. After the user configures the configuration, the config_test1 value is stored in. in config, makefile is read. the Autoconf in the comments version of config reads the value of config_test and then compiles it. However, the preceding steps cannot be achieved, because although the directory driver is already included in the total makefile, The makefile in the driver directory does not contain the test directory. Therefore, you must add the following in Driver/makefile:

2.6.vim driver/makefile

Add the following sentence:

104 obj-$ (config_of) + =/

105 obj-$ (config_ssb) + = SSB/

106 obj-$ (config_virtio) + = virtio/

107 obj-$ (config_staging) + = staging/

108 obj-y + = platform/

109 obj-$ (config_test1) + = test1 // Add this sentence

 

 

Although makefile already exists, this still does not work. When you need to configure the arm, the kconfig in the arm structure does not contain the kconfig of test. In this way, the statement will not appear on the graphic configuration interface. Therefore, add the statement to arch/ARM/kconfig.

2.7.vim ARCH/ARM/kconfig

 

1230 menu "device drivers"

1231

1232 source "Drivers/base/kconfig"

.......................................... .......

1328 source "Drivers/staging/kconfig"

1329

1330 source "Drivers/test1/kconfig"

1331

1332 endmenu

 

 

Success!

In this way, the driver devices written on the make menuconfig interface has an additional "test1 driver here" Directory, which contains a configuration option "Xiaobai test1 driver ".

The syntax of the kconfig file is explained in detail in the documentation/kbuild/kconfig-language.txt file, above I just implemented a simple bit, but are all fur.

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.