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 Makefile:

  • Makefile contains -include include/config/auto.conf
  • include/config/auto.conf is built by recursively calling the toplevel Makefile on the silentoldconfig target.

Conditional compilation of files is done mostly by playing with target names: makefiles include rules like

obj-$(CONFIG_CRC16) += crc16.o

The target 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 oldconfig, scripts/kconfig/qconf for xconfig, etc.); the source code for that is scripts/kconfig/confdata.c which does some very simple text processing.

  • +1. The only trickiness really is the stuff in the top-level Makefile designed to ensure that all the config targets are run before any of the build targets. – Stephen Kitt Nov 18 '16 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.