Build fails for custom hello world kernel module

I am new to developing on openwrt and I am just trying to get a hello world kernel module built into the firmware but the build keeps failing. I am not sure what I am doing wrong. I am building for openwrt version 21.02 on ubuntu 18.04.

I have the following structure for the package:

<build_root>
          | -- package
                     | -- testing
                              | -- hello-world
                                          | -- Makefile
                                          | -- src
                                                 | -- Makefile
                                                 | -- helloworld.c

My package Makefile:

include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk

PKG_NAME:=helloworld
PKG_VERSION:=1.0
PKG_RELEASE:=0

PKG_BUILD_DIR:=$(KERNEL_BUILD_DIR)/$(PKG_NAME)
PKG_CHECK_FORMAT_SECURITY:=0

include $(INCLUDE_DIR)/package.mk

define KernelPackage/$(PKG_NAME)
	SUBMENU:=Other modules
	TITLE:=helloworld
	FILES:= $(PKG_BUILD_DIR)/helloworld.ko
endef

define KernelPackage/$(PKG_NAME)/description
	A sample kernel module.
endef

define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)
	$(CP) ./src/* $(PKG_BUILD_DIR)/
endef


MAKE_OPTS:= \
	ARCH="$(LINUX_KARCH)" \
	CROSS_COMPILE="$(TARGET_CROSS)" \
	SUBDIRS="$(PKG_BUILD_DIR)"

define Build/Compile
	$(MAKE) -C "$(LINUX_DIR)" \
		$(MAKE_OPTS) \
		modules
endef

$(eval $(call KernelPackage,$(PKG_NAME)))

My source Makefile:

obj-m := helloworld.o

My source file (taken from here:

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

int init_module(void)
{
    printk(KERN_INFO "Hello world 1.\n");
    return 0;
}

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

I see the package show up when i run make menuconfig and select it:

Kernel modules --->
     Other modules --->
            <*> kmod-helloworld....

I then run make clean && make -j4 which results in ERROR: package/testing/hello-world failed to build. Looking at the verbose logs this is the error I am getting:

make[4]: Leaving directory '<build_root>/build_dir/target-mipsel_24kc_musl/linux-ramips_mt76x8/linux-5.4.143'
grep: <build_root>/build_dir/target-mipsel_24kc_musl/linux-ramips_mt76x8/helloworld/./Module.symvers: No such file or directory
ERROR: module '<build_root>/build_dir/target-mipsel_24kc_musl/linux-ramips_mt76x8/helloworld/helloworld.ko' is missing.
Makefile:57: recipe for target '<build_root>/build_dir/target-mipsel_24kc_musl/linux-ramips_mt76x8/helloworld/.pkgdir/kmod-helloworld.installed' failed
make[3]: *** [<build_root>/build_dir/target-mipsel_24kc_musl/linux-ramips_mt76x8/helloworld/.pkgdir/kmod-helloworld.installed] Error 1
time: package/testing/hello-world/compile#51.05#6.21#99.12

Change MAKE_OPTS to something like

MAKE_OPTS := \
        $(KERNEL_MAKE_FLAGS) \
        M="$(PKG_BUILD_DIR)"

BTW in package/kernel there exist many simple examples of out-of-tree kernel modules.

1 Like