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.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Home, My Backup Data Center
- What's the tweeting protocol?
- One Hand Slapping
- The Secret Password Is...
- Trying to Tame the Tablet
- RSS Feeds
- Reply to comment | Linux Journal
6 hours 8 min ago - Reply to comment | Linux Journal
8 hours 41 min ago - Reply to comment | Linux Journal
9 hours 58 min ago - great post
10 hours 33 min ago - Google Docs
10 hours 55 min ago - Reply to comment | Linux Journal
15 hours 44 min ago - Reply to comment | Linux Journal
16 hours 31 min ago - Web Hosting IQ
18 hours 4 min ago - Thanks for taking the time to
19 hours 41 min ago - Linux is good
21 hours 39 min ago
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
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.



Comments
Interesting Blog
This is really one of the most interesting blog, I've seen so far.
Post new comment Please note
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!
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
i have very useful stuff with this article ..you posted a freat stuff about kernel
Reply to comment | Linux Journal
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
What's up, after reading this remarkable paragraph i am also cheerful to share my experience here with mates.
bathmate
advantage doubtfull, disadvantage certain
"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
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
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
Good article, thanks for posting it !
Find out where the SD card is
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!
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
Very informative! +1 for
root@sauroncomputer science
Excellent article, I recommend it.