An Overview of Makefile, Kconfig, and .config in Linux Kernel Source

Click on the above“Embedded and Linux Matters”, select“Pin/Star the Official Account”

Welfare and valuable content delivered first-hand

The Linux kernel source code files are numerous, and it can be confusing to understand the relationship between Makefile, Kconfig, and .config. Not knowing the kernel compilation system can lead to issues when compiling or modifying the kernel, writing drivers that aren’t integrated into the kernel, or not knowing how to configure the kernel. These problems are all related to Makefile, Kconfig, and .config. Below, we will briefly discuss Makefile, Kconfig, and .config. I hope this will inspire you.

1. The Roles of the Three

In simple terms, it’s like ordering dishes at a restaurant: Kconfig is the menu, Makefile is the recipe, and .config is the dish you ordered.

Makefile: A text file that defines how to compile source files.

Kconfig: A text file that serves as the kernel configuration menu.

.config: The configuration file used for compiling the kernel.

2. The Syntax of the Three

1. Makefile

Reference: linux-3.4.2/drivers/Makefile

Purpose: Used to define which contents are compiled as modules, conditional compilations, etc. The subdirectory Makefile is included by the top-level Makefile.

(1) Direct Compilation

obj-y += xxx.o

This indicates that xxx.c or xxx.s will be compiled to produce xxx.o, which will be directly included in the kernel.

(2) Conditional Compilation

obj -$(CONFIG_HELLO) += xxx.o

This decides whether the file is included in the kernel based on the CONFIG_XXX in the .config file.

(3) Module Compilation

obj-m +=xxx.o

This indicates that xxx is compiled as a module, which will only be compiled when executing make modules.

2. Kconfig

Each config menu item has a type definition: bool (boolean), tristate (built-in, module, removed), string, hex, and integer.

Purpose: Determines the menu items displayed during make menuconfig.

Reference: linux-3.4.2/drivers/leds/Kconfig:

config LEDS_S3C24XX

tristate “LED Support for Samsung S3C24XX GPIO LEDs”

depends on LEDS_CLASS

depends on ARCH_S3C24XX

help

This option enables support for LEDs connected to GPIO lines

on Samsung S3C24XX series CPUs, such as the S3C2410 and S3C2440.

LEDS_S3C24XX: The name of the configuration option, omitting the prefix “CONFIG_”.

Tristate:

Indicates whether the item is compiled into the kernel or as a module. It displays as < >, and if selected to compile as a kernel module, it will generate a CONFIG_HELLO_MODULE=m configuration in .config. Choosing Y compiles it directly into the kernel, generating a CONFIG_HELLO_MODULE=y configuration item in .config. The string following Tristate is the name displayed during make menuconfig.

bool:

This type can only be selected or not selected, and during make menuconfig, it displays as [ ], meaning it cannot be configured as a module.

dependon:

This option depends on another option, and the prompt for the current configuration option will only appear when the dependency is selected, allowing the current option to be set.

select:

Reverse dependency; when this option is selected, it also selects the option defined after select.

help:

Help information.

Directory Hierarchy Iteration:

In Kconfig, there are statements like: source “drivers/usb/Kconfig” to include (or nest) new Kconfig files, allowing each directory to manage its own configuration content without having to write all configurations in a single file, making it easier to modify and manage.

3. .config

Reference: linux-3.4.2/.config

From the analysis of the previous two files, the meaning of .config is clear: it is a reference file for kernel compilation, and checking its content can reveal which drivers are compiled into the kernel.

There are three ways to configure the kernel (choose any one):

(1) make menuconfig

(2) make xxx_defconfig

(3) Directly modify .config

Note: If you directly modify .config, it may not take effect, as some configurations may have dependencies. During make, it checks the rules based on these dependencies, so direct modifications to .config may sometimes be ineffective, so it is not recommended.

The above may be a bit abstract, so here is an example:

Write a simple entry function to output hello world as a driver and compile it into the kernel.

Steps:

(1) Create a new hello folder under the drivers directory, implementing hello.c, Makefile, and Kconfig inside.

hello.c:

#include <linux/module.h>  #include <linux/kernel.h> #include <linux/init.h>  static int first_drv_init(void)  {    printk("------------------hello world !--------------------");     return 0;  } static void first_drv_exit(void) {      printk("------------------exit hello world !--------------------"); }  module_init(first_drv_init); module_exit(first_drv_exit); MODULE_LICENSE("GPL");

Makefile:

obj-$(CONFIG_HELLO) += hello.o

Kconfig:

config HELLO

tristate “Hello World for fengyuwuzu”

help

Hello for fengyuwuzu

config HELLO decides the name: CONFIG_HELLO.

Hello World for fengyuwuzu: Determines the name displayed during make menuconfig.

(2) Modify the Makefile and Kconfig in the upper level (Linux-3.4.2/drivers).

Makefile:

obj-y += hello/

Kconfig:

source “drivers/hello/Kconfig”

(3) make menuconfig

An Overview of Makefile, Kconfig, and .config in Linux Kernel Source

(4) make uImage and then burn it to the development board.

Check the kernel startup log, the great hello world has appeared! This indicates that hello.c has been successfully compiled into the kernel.

An Overview of Makefile, Kconfig, and .config in Linux Kernel Source

You are welcome to refer to this experiment, and I believe you will have a clearer understanding of the kernel’s organizational structure.

Source material from:Baiwen Technology, Q&A Team
Copyright belongs to the original author. For the dissemination and learning discussion of technology only, if there are copyright issues, please contact me for removal.

end

Previous Recommendations

Must-read classic books for Embedded Linux

Recommended learning path for Embedded Systems

A reader’s clear logic in questioning

Success in transitioning from mechanical engineering to embedded systems

A reader’s experience in securing a job in audio and video direction during the autumn recruitment

An Overview of Makefile, Kconfig, and .config in Linux Kernel Source
An Overview of Makefile, Kconfig, and .config in Linux Kernel Source

Scan to add me on WeChat

Join the technical discussion group

An Overview of Makefile, Kconfig, and .config in Linux Kernel Source
An Overview of Makefile, Kconfig, and .config in Linux Kernel Source

Share

An Overview of Makefile, Kconfig, and .config in Linux Kernel Source

Collect

An Overview of Makefile, Kconfig, and .config in Linux Kernel Source

Like

An Overview of Makefile, Kconfig, and .config in Linux Kernel Source

View

Leave a Comment