I know the purpose of this make file; it is a driver’s Makefile, which would invoke the kernel’s build system from the kernel source. But Unable to understand what exactly is going on.

# Makefile – makefile of our first driver

# if KERNELRELEASE is not defined, we've been called directly from the command line.
# Invoke the kernel build system.
ifeq (${KERNELRELEASE},)
    KERNEL_SOURCE := /usr/src/linux
    PWD := $(shell pwd)
default:
    ${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} modules

clean:
    ${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} clean

# Otherwise KERNELRELEASE is defined; we've been invoked from the
# kernel build system and can use its language.
else
    obj-m := ofd.o
endif

For example what is happening here:

 `${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} modules

and here:

obj-m := ofd.o` 

Can someone help me understand this by putting some more comments?

I have taken this from this link.

This make file has an associated .c file (driver);

/* ofd.c – Our First Driver code */

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

static int __init ofd_init(void) /* Constructor */
{
    printk(KERN_INFO "Namaskar: ofd registered");

    return 0;
}

static void __exit ofd_exit(void) /* Destructor */
{
    printk(KERN_INFO "Alvida: ofd unregistered");
}

module_init(ofd_init);
module_exit(ofd_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Anil Kumar Pugalia <email@sarika-pugs.com>");
MODULE_DESCRIPTION("Our First Driver");
  • 1
    LDD3 Chapter 2, page 22. Kernel makefiles use an extended syntax. – goldilocks Mar 29 '14 at 11:40

As explained in the Makefile comments, there is two parts to this Makefile. That's because it will be read twice. First when you call make on the command line, then by kbuild.

# if KERNELRELEASE is not defined, we've been called directly from the     command line.
# Invoke the kernel build system.
ifeq (${KERNELRELEASE},)
    KERNEL_SOURCE := /usr/src/linux
    PWD := $(shell pwd)
default:
    ${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} modules

clean:
    ${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} clean

If KERNELRELEASE is not defined, it's because the file is read by make. You have a Makefile that calls make with the -C option to change directory to where your kernel source is.

Make then reads the Makefile there (in the kernel source dir). SUBDIRS is where your module source code is. (I think SUBDIRS is deprecated and M is used instead now).

The kernel build system will look for the Makefile in your module's directory to know what to build. KERNELRELEASE will be set, so that part will be used :

# Otherwise KERNELRELEASE is defined; we've been invoked from the
# kernel build system and can use its language.
else
    obj-m := ofd.o
endif

You'll find more information in the kernel documentation.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.