What exactly does Linux kernel's `make defconfig` do?
Motivation
The .config
file is not simply copied from your defconfig
file. The motivation for storing defconfig
in such a format is next: in defconfig
we can only specify options with non-default values (i.e. options we changed for our board). This way we can keep it small and clear. Every new kernel version brings a bunch of new options, and this way we don't need to update our defconfig
file each time the kernel releases. Also, it should be mentioned that kernel build system keeps very specific order of options in defconfig
file, so it's better to avoid modifying it by hand. Instead you should use make savedefconfig
rule.
Simplified explanation
When .config
file is being generated, kernel build system goes through all Kconfig
files (from all subdirs), checking all options in those Kconfig
files:
- if option is mentioned in
defconfig
, build system puts that option into.config
with value chosen indefconfig
- if option isn't mentioned in
defconfig
, build system puts that option into.config
using its default value, specified in correspondingKconfig
Check scripts/kconfig/Makefile and scripts/kconfig/conf.c files to see how it's actually done.
More precise and detailed explanation
From "Kbuild: the Linux Kernel Build System" by Javier Martinez:
Defining Configuration Symbols:
Kconfig
FilesConfiguration symbols are defined in files known as
Kconfig
files. EachKconfig
file can describe an arbitrary number of symbols and can also include (source) otherKconfig
files. Compilation targets that construct configuration menus of kernel compile options, such asmake menuconfig
, read these files to build the tree-like structure. Every directory in the kernel has oneKconfig
that includes theKconfig
files of its subdirectories. On top of the kernel source code directory, there is aKconfig
file that is the root of the options tree. Themenuconfig
(scripts/kconfig/mconf
),gconfig
(scripts/kconfig/gconf
) and other compile targets invoke programs that start at this rootKconfig
and recursively read theKconfig
files located in each subdirectory to build their menus. Which subdirectory to visit also is defined in eachKconfig
file and also depends on the config symbol values chosen by the user.Storing Symbol Values:
.config
FileAll config symbol values are saved in a special file called
.config
. Every time you want to change a kernel compile configuration, you execute a make target, such asmenuconfig
orxconfig
. These read theKconfig
files to create the menus and update the config symbols' values using the values defined in the.config
file. Additionally, these tools update the.config
file with the new options you chose and also can generate one if it didn't exist before.Because the
.config
file is plain text, you also can change it without needing any specialized tool. It is very convenient for saving and restoring previous kernel compilation configurations as well.
Useful commands
You can use simpler syntax for make defconfig
, like:
$ make ARCH=arm your_board_defconfig
See the full list of available defconfigs with:
$ make ARCH=arm help | grep defconfig
If you need to do reverse action (i.e. create a neat small defconfig
from extensive .config
), you can use savedefconfig
rule:
$ make ARCH=arm savedefconfig
Also, as 0andriy mentioned, you can use diffconfig
script to see changes from one .config
to another one:
$ scripts/diffconfig .config_old .config_new
It also generate include/generated/autoconf.h
.
This header file is included by C source file. On the other hand, .config
is for Makefile system.
Build system generate two files, and keep them consistent.