Home:ALL Converter>Build Linux Kernel Module using Makefile with different pathname

Build Linux Kernel Module using Makefile with different pathname

Ask Time:2016-06-01T02:33:18         Author:msmith81886

Json Formatter

I am trying to compile a Linux kernel module using the standard example Makefile specified in the Linux Kernel Module Programming Guide. If the Makefile is called Makefile, then everything works. If I rename the Makefile to Makefile.hello or something else, then it fails as it cannot find the path Makefile. I was wondering if there is a command or set of flags I can add to my Makefile to make this function properly. I need to rename the Makefile as I am calling it from CMake. Cmake creates its own Makefiles and will commonly overwrite what I already have.

I replaced my kernel module code with the hello world example and replicated the problem. I know its the makefile.

hello world example hello.c

/*
 * hello−1.c − The simplest kernel module.
 */
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
int init_module(void)
{
    printk(KERN_INFO "Hello world 1.\n");
    /*
     * A non 0 return means init_module failed; module can't be loaded.
     */
    return 0;
}
void cleanup_module(void)
{
    printk(KERN_INFO "Goodbye world 1.\n");
}

Makefile

obj-m += hello.o

ifeq (,$(KDIR))
    KDIR := /lib/modules/$(shell uname -r)/build
endif

PWD := $(shell pwd)

all:
    $(MAKE) -C $(KDIR) M=$(PWD) $(KCONFIG) modules

clean:
    $(MAKE) -C $(KDIR) M=$(PWD) clean

If makefile is called Makefile. (Successfully builds)

$> make -f Makefile
make -C /lib/modules/4.4.0-21-generic/build M=/home/msmith/Desktop/kernel-test  modules
make[1]: Entering directory '/usr/src/linux-headers-4.4.0-21-generic'
  CC [M]  /home/msmith/Desktop/kernel-test/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/msmith/Desktop/kernel-test/hello.mod.o
  LD [M]  /home/msmith/Desktop/kernel-test/hello.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-21-generic'

Make output if makefile is called Makefile.hello (Fails to build)

$> make -f Makefile.hello
make -C /lib/modules/4.4.0-21-generic/build M=/home/msmith/Desktop/kernel-test  modules
make[1]: Entering directory '/usr/src/linux-headers-4.4.0-21-generic'
scripts/Makefile.build:44: /home/msmith/Desktop/kernel-test/Makefile: No such file or directory
make[2]: *** No rule to make target '/home/msmith/Desktop/kernel-test/Makefile'.  Stop.
Makefile:1396: recipe for target '_module_/home/msmith/Desktop/kernel-test' failed
make[1]: *** [_module_/home/msmith/Desktop/kernel-test] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-21-generic'
Makefile.hello:10: recipe for target 'all' failed
make: *** [all] Error 2

I tried adding the -f to the internal MAKE parameters, however that just caused more issues.

Author:msmith81886,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/37553169/build-linux-kernel-module-using-makefile-with-different-pathname
Tsyvarev :

Move all Kbuild-related logic into the file Kbuild. Kernel's build system checks file with this name first, so it won't look into Makefile, created by CMake. This feature is documented in Documentation/kbuild/makefiles.txt.\n\nI use exactly this approach in my CMake projects, related with Linux kernel.",
2016-05-31T19:50:15
Dmitry :

Open script/Makefile.build into kernel tree:\n\n 41 # The filename Kbuild has precedence over Makefile \n 42 kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src)) \n 43 kbuild-file := $(if $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Kbuild,$(kbuild-dir)/Makefile) \n 44 include $(kbuild-file)\n\n\nThis part of code (43-44) include Makefile with name 'Makefile'.",
2016-05-31T19:51:10
yy