Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters from makefile to Linux kernel module source code

I have the source code:

#include <linux/module.h>   
#include <linux/kernel.h>   

int init_module(void)
{
    printk(KERN_INFO "Hello world %i\n", BUILD_NUMBER);
    return 0;
}

void cleanup_module(void)
{
    printk(KERN_INFO "Goodbye world 1.\n");
}

and a makefile:

obj-m += hello-1.o

BUILD_NUMBER := 42

# How to pass BUILD_NUMBER to hello-1.c ???

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Now how do I pass the BUILD_NUMBER parameter from the makefile to the source code?

like image 231
Rasmus Friis Kjeldsen Avatar asked Mar 15 '13 11:03

Rasmus Friis Kjeldsen


People also ask

What is Makefile in kernel module?

A kernel module is not an independant executable, but an object file which will be linked into the kernel in runtime. As a result, they should be compiled with the -c flag. Also, all kernel modules have to be compiled with certain symbols defined.

How use Modinfo command in Linux?

modinfo command with help option: It will print the general syntax of the modinfo along with the various options and gives a brief description about each option. modinfo -V: This option gives the version information of modinfo command. modinfo -F: This option only print this field value, one per line.

What is Obj Y in Makefile?

These lines define the files to be built, any special compilation options, and any subdirectories to be entered recursively. The most simple kbuild makefile contains one line: Example: obj-y += foo.o. This tell kbuild that there is one object in that directory named foo.o. foo.o will be build from foo.

What does Objs mean in Makefile?

It is standard practice for every makefile to have a variable named objects , OBJECTS , objs , OBJS , obj , or OBJ which is a list of all object file names. We would define such a variable objects with a line like this in the makefile: objects = main.o kbd.o command.o display.o \ insert.o search.o files.o utils.o.


1 Answers

As the Linux build system uses kernel provided Makefiles that can reasonably not be changed. I'd suggest to place your version number directly into the source code instead of in the Makefile.

There's a possibility thought. You can define a CPPFLAGS environment variable. It should be passed by the kernel Makefile to the C compiler command line. If you define this CPPFLAGS variable as -DVERSION=42, maybe you can use this VERSION macro inside your source file.

all:
    CPPFLAGS="-DVERSION=42" make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

Note that CPPFLAGS stands for "C PreProcessor FLAGS". It is unrelated to C++.

After testing it. This does not work. There's a solution however. The kernel Makefile allows (and uses) the definition of the KCPPFLAGS environment variable that will be added to the kernel Makefile defined own CPPFLAGS.

You have to use:

all:
    KCPPFLAGS="-DVERSION=42" make -C /lib/modules/$(shell uname -r)/build M=$(PWD) 
like image 192
Didier Trosset Avatar answered Oct 06 '22 03:10

Didier Trosset