How does linux kernel Makefile understands .config? Does it have a parser of defconfig file? It has to produce a lot of #defines for each enabled option from defconfig and also maintain a lot of minor Makefiles which are compiled or not, based on directives in .config file.
The syntax of the
.config file is compatible with make; for example a line like
CONFIG_CRC16=m sets the make variable
CONFIG_CRC16 to the value
m. It's parsed by make and included indirectly in the toplevel
include/config/auto.confis built by recursively calling the toplevel
Conditional compilation of files is done mostly by playing with target names: makefiles include rules like
obj-$(CONFIG_CRC16) += crc16.o
obj-y thus builds all objects that are enabled as built-ins by a configuration option, and
obj-m builds all objects that are enabled as modules. There are also conditional directives in the makefiles for more complex cases.
For conditional compilation in the C language, C source files include
include/generated/autoconf.h which contains lines like
#define CONFIG_CRC16_MODULE 1. This file is generated from
include/config/auto.conf by the programs invoked by the
xxxconfig targets (
scripts/kconfig/conf for batch targets like
xconfig, etc.); the source code for that is
scripts/kconfig/confdata.c which does some very simple text processing.