Linux Kernel Build System
The kernel has many abstraction layers and levels of indirection and because its build system allows for creating highly customized kernel binary images.

Prerequisites

Please read the "Kbuild: the Linux Kernel Build System" carefully, you will understand how this system works Kbuild Linux Kernel Build System

Hello World Module Source Code

Make a "helloworld" directory under drivers
[email protected]:~/linux$ mkdir drivers/helloworld
Create helloworld.c file under our helloworld directory and add the C code below, this is a simple Hello World Kernel Module
[email protected]:~/linux$ nano drivers/helloworld/helloworld.c
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
static int module_init_function(void)
{
printk(KERN_INFO "Module? Hello!\n");
return 0;
}
static void module_exit_function(void)
{
printk(KERN_INFO "Module? Bye!\n");
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("xe1gyq");
MODULE_DESCRIPTION("My First Linux Kernel Module");
module_init(module_init_function);
module_exit(module_exit_function);

Hello World Module Kconfig

Create the Kconfig file under helloworld directory and add the code below, make sure indentation is correct
[email protected]:~/linux$ nano drivers/helloworld/Kconfig
menu "Hello Module Kernel Support"
config HELLO_WORLD
tristate "Hello Module Driver"
depends on X86
help
Select this option to run a Hello World Module!
endmenu

Hello World Module Makefile

Create the Makefile under helloworld directory and add the code below
[email protected]:~/linux$ nano drivers/helloworld/Makefile
obj-$(CONFIG_HELLO_WORLD) += helloworld.o

Device Drivers Kconfig

Seeing Hello World Directory
Modify Kconfig under drivers directory and add the line with helloworld
[email protected]:~/linux$ nano drivers/Kconfig
menu "Device Drivers"
source "drivers/helloworld/Kconfig"
source "drivers/amba/Kconfig"

Device Drivers Makefile

Compiling Hello World Directory
Modify Makefile under drivers directory and add the line with CONFIG_HELLO_WORLD [email protected]:~/linux$ nano drivers/Makefile
#
# Makefile for the Linux kernel device drivers.
#
# 15 Sep 2000, Christoph Hellwig <[email protected]>
# Rewritten to use lists instead of if-statements.
#
obj-$(CONFIG_HELLO_WORLD) += helloworld/
obj-y += irqchip/
obj-y += bus/
...
[email protected]:~/linux$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: drivers/Kconfig
modified: drivers/Makefile
Untracked files:
(use "git add <file>..." to include in what will be committed)
drivers/helloworld/
no changes added to commit (use "git add" and/or "git commit -a")

Hello World Menuconfig

We are ready to view our Hello World Module under menuconfig
[email protected]:~/linux$ make menuconfig
Go to its location under -> Device Drivers -> Hello Module Kernel Support
Understand the menu options seen below including their fast paths (one letter invocation)
<Select> < Exit > < Help > < Save > < Load >
Get help for the Hello Module Kernel Support using Help function, you should see this
CONFIG_HELLO_WORLD:
Select this option to run a Hello World Module!
Symbol: HELLO_WORLD [=n]
Type : tristate
Prompt: Hello Module Driver
Location:
-> Device Drivers
-> Hello Module Kernel Support
Defined at drivers/helloworld/Kconfig:3
Depends on: X86 [=y]
Understand about the following options from Kconfig by googling or looking at other Kconfigs
  • default
  • tristate
  • Depends on
Take a look at the default building state for our Hello World Module and modify Kconfig so you can have it built as default
Symbol: HELLO_WORLD [=n]