Kbuild: the Linux Kernel Build System
Listing 2 shows a segment of the drivers/char/Kconfig file with the symbols added for the coin driver.
Listing 2. Kconfig Entries for the Coin Driver
# # Character device configuration # menu "Character devices" config COIN tristate "Coin char device support" help Say Y here if you want to add support for the coin char device. If unsure, say N. To compile this driver as a module, choose M here: the module will be called coin. config COIN_STAT bool "flipping statistics" depends on COIN help Say Y here if you want to enable statistics about the coin char device.
So, how can you use your recently added symbols?
As mentioned previously,
make targets that build a tree menu with all the
compilation options use this config symbol, so you can choose what to
compile in your kernel and its modules. For example, when you execute:
$ make menuconfig
the command-line utility scripts/kconfig/mconf will start and read all
the Kconfig files to build a menu-based interface. You then use these
programs to update the values of your
options. Figure 1 shows how the menu looks when you navigate to Device
Drivers→Character devices; see how the options for the coin driver
can be set.
Figure 1. Menu Example
Once you are done with the compilation option configuration, exit the program, and if you made some changes, you will be asked to save your new configuration. This saves the configuration options to the .config file. For every symbol, a CONFIG_ prefix is appended in the .config file. For example, if the symbol is of type boolean and you chose it, in the .config file, the symbol will be saved like this:
On the other hand, if you didn't choose the symbol, it won't be set in the .config file, and you will see something like this:
# CONFIG_COIN_STAT is not set
Tristate symbols have the same behavior as bool types when chosen or not. But, remember that tristate also has the third option of compiling the feature as a module. For example, you can choose to compile the COIN driver as a module and have something like this in the .config file:
The following is a segment of the .config file that shows the values chosen for the coin driver symbols:
Here you are telling kbuild that you want to compile the coin driver as a module and activate the flipping statistics. If you have chosen to compile the driver built-in and without the flipping statistics, you will have something like this:
CONFIG_COIN=y # CONFIG_COIN_STAT is not set
Once you have your .config file, you are ready to compile your kernel and its modules. When you execute a compile target to compile the kernel or the modules, it first executes a binary that reads all the Kconfig files and .config:
$ scripts/kconfig/conf Kconfig
This binary updates (or creates) a C header file with the values you chose for all the configuration symbols. This file is include/generated/autoconf.h, and every gcc compile instruction includes it, so the symbols can be used in any source file in the kernel.
The file is composed of thousands of #define macros that describe the state for each symbol. Let's look at the conventions for the macros.
Bool symbols with the value true and tristate symbols with the value yes are treated equally. For both of them, three macros are defined.
For example, the bool
CONFIG_COIN_STAT symbol with the value true and the
CONFIG_COIN symbol with the value yes will generate the following:
#define __enabled_CONFIG_COIN_STAT 1 #define __enabled_CONFIG_COIN_STAT_MODULE 0 #define CONFIG_COIN_STAT 1 #define __enabled_CONFIG_COIN 1 #define __enabled_CONFIG_COIN_MODULE 0 #define CONFIG_COIN 1
In the same way, bool symbols with the value false and tristate symbols with
the value no have the same semantics. For both of them, two macros are defined.
For example, the
CONFIG_COIN_STAT with the value
false and the
with the value no will generate the following group of macros:
#define __enabled_CONFIG_COIN_STAT 0 #define __enabled_CONFIG_COIN_STAT_MODULE 0 #define __enabled_CONFIG_COIN 0 #define __enabled_CONFIG_COIN_MODULE 0
For tristate symbols with the value module, three macros are defined. For
CONFIG_COIN with the value module will generate the following:
#define __enabled_CONFIG_COIN 0 #define __enabled_CONFIG_COIN_MODULE 1 #define CONFIG_COIN_MODULE 1
Curious readers probably will ask why are those
macros needed? Wouldn't it be sufficient to have only the
CONFIG_option_MODULE? And, why is
_MODULE declared even for symbols that
are of type bool?
__enabled_ constants are used by three macros:
#define IS_ENABLED(option) \ (__enabled_ ## option || __enabled_ ## option ## _MODULE) #define IS_BUILTIN(option) __enabled_ ## option #define IS_MODULE(option) __enabled_ ## option ## _MODULE
__enabled_option_MODULE always are defined,
even for bool symbols to make sure that this macro will work for any
The third and last step is to update the Makefiles for the subdirectories where you put your source files, so kbuild can compile your driver if you chose it.
But, how do you instruct kbuild to compile your code conditionally?
The kernel build system has two main tasks: creating the kernel binary image and the kernel modules. To do that, it maintains two lists of objects: obj-y and obj-m, respectively. The former is a list of all the objects that will be built in the kernel image, and the latter is the list of the objects that will be compiled as modules.
The configuration symbols from .config and the macros from autoconf.h are used along with some GNU make syntax extensions to fill these lists. Kbuild recursively enters each directory and builds the lists adding the objects defined in each subdirectory's Makefile. For more information about the GNU make extensions and the objects list, read Documentation/kbuild/makefiles.txt.
For the coin driver, the only thing you need to do is add a line in drivers/char/Makefile:
obj-$(CONFIG_COIN) += coin.o
This tells kbuild to create an object from the source file coin.c and
to add it to an object list. Because
CONFIG_COIN's value can be y or m, the
coin.o object will be added to the obj-y or obj-m list depending on the
symbol value. It then will be built in the kernel or as a module. If you
didn't choose the
CONFIG_COIN option, the symbol is undefined, and
coin.o will not be compiled at all.
Now you know how to include source files conditionally. The last part of the puzzle is how to compile source code segments conditionally. This can be done easily by using the macros defined in autoconf.h. Listing 3 shows the complete coin character device driver.
Javier Martinez Canillas is a longtime Linux user, administrator and open-source advocate developer. He has an MS from the Universitat Autònoma de Barcelona and works as a Linux kernel engineer.
- Resurrecting the Armadillo
- Nmap—Not Just for Evil!
- High-Availability Storage with HA-LVM
- Real-Time Rogue Wireless Access Point Detection with the Raspberry Pi
- March 2015 Issue of Linux Journal: System Administration
- DNSMasq, the Pint-Sized Super Dæmon!
- Localhost DNS Cache
- Days Between Dates: the Counting
- The Usability of GNOME
- Linux for Astronomers