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 COIN and COIN_STAT compilation 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:


CONFIG_COIN_STAT=y

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:


CONFIG_COIN=m

The following is a segment of the .config file that shows the values chosen for the coin driver symbols:


CONFIG_COIN=m
CONFIG_COIN_STAT=y

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 tristate 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 CONFIG_COIN 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 example, the 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 __enabled_option macros needed? Wouldn't it be sufficient to have only the CONFIG_option and CONFIG_option_MODULE? And, why is _MODULE declared even for symbols that are of type bool?

Well, the __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

So, the __enabled_option and __enabled_option_MODULE always are defined, even for bool symbols to make sure that this macro will work for any configuration option.

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.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Interesting Blog

Systems engineering services's picture

This is really one of the most interesting blog, I've seen so far.

Post new comment Please note

Anonymous's picture

Post new comment
Please note that comments may not appear immediately, so there is no need to repost your comment.IBM's platform as a service (PaaS), IBM SmartCloud Application Services, is now generally available and ready to help your development team collaborate in the cloud!
http://www.lehighvalleylive.com/bethlehem/index.ssf/2008/11/australian_c...

Great Work!

Linux Schulungen's picture

It´s a excellent post, with great insight, and lots to think about Linux. That´s all I could come up with after reading it.
Thank you very much.

query

Anonymous's picture

i have very useful stuff with this article ..you posted a freat stuff about kernel

Reply to comment | Linux Journal

freecriminalattorney.info's picture

Hi there! I just wanted to ask if you ever have any
issues with hackers? My last blog (wordpress) was
hacked and I ended up losing several weeks of hard work due to no back up.
Do you have any solutions to stop hackers?

What's up, after reading this

nevster's picture

What's up, after reading this remarkable paragraph i am also cheerful to share my experience here with mates.
bathmate

advantage doubtfull, disadvantage certain

Anonymous's picture

"I know I will never need, in this build, the drivers for thousands of NIC and printers and whatsoever, Buetooth functions...and whatever.
I have THIS antenna, THIS hard Disk....and so on."

With hard drive space being like $60/TB you'd never recover enough space to make the endeavor worthwhile.

OTOH there is a major disadvantage to your idea. You "never" need *this* network driver, wifi, etc. right now, but what if your motherboard dies? With Windows you hope you can find all your original media and try to reinstall or hope you made a "good" backup that you can restore all your software and settings to a fresh install (something I've never completely succeeded with on Windows, usually not eve close).

With all the drivers in place as modules you can move the drive to a new system and let the auto hardware detection in modern distributions (Knoppix, Ubuntu etc.) get you up and running without effort.

In fact I use this as a feature by "cloning" my development system and then distributing them as "appliance" computers to run my software. Should I need to do "field service" my developent tools and environment are there waiting for me.

I know this will mark me as a

Anonymous's picture

I know this will mark me as a "total noob", but.....

Is there an EASY way to remove ANY UNNECESSARY COMPONENT from the Kernel to suite exactly what needed for my hardware AND preferred applications?

I mean something like a list of checkbox to mark after a deep research on my hardware, that then produce the right system iso to install

I know I will never need, in this build, the drivers for thousands of NIC and printers and whatsoever, Buetooth functions...and whatever.
I have THIS antenna, THIS hard Disk....and so on.

And most of all...there would be any advantage in doing so?

OR....would it be possible to use Synaptic to remove anything unneeded also in kernel? (I already panic when it tells me gnome metapackages must be remove to get rid of the - for me - unuseful Totem...)

Thanks

I know this will mark me as a

Anonymous's picture

I know this will mark me as a "total noob", but.....

Is there an EASY way to remove ANY UNNECESSARY COMPONENT from the Kernel to suite exactly what needed for my hardware AND preferred applications?

I mean something like a list of checkbox to mark after a deep research on my hardware, that then produce the right system iso to install

I know I will never need, in this build, the drivers for thousands of NIC and printers and whatsoever, Buetooth functions...and whatever.
I have THIS antenna, THIS hard Disk....and so on.

And most of all...there would be any advantage in doing so?

OR....would it be possible to use Synaptic to remove anything unneeded also in kernel? (I already panic when it tells me gnome metapackages must be remove to get rid of the - for me - unuseful Totem...)

Thanks

Good article

Anonymous's picture

Good article, thanks for posting it !

Find out where the SD card is

autelcn's picture

Find out where the SD card is mounted to begin the formatting process. Launch a "Terminal" window if you are not already in a terminal shell account. To launch the "Terminal" in Ubuntu Linux, select "Applications" from the menu bar and drag to "Accessories" and then to "Terminal." Release the Mouse button to launch "Terminal."

_______________________
http://www.autelcn.com/
autel ds708

Great article!

Mdturnerinoz's picture

I've played round with kernel builds some but didn't know all this detail until your article: thanks for sharing you insights!

Very informative! +1 for

Marcelo Elizeche Landó's picture

Very informative! +1 for root@sauron

computer science

ma. elena's picture

Excellent article, I recommend it.

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix