For kernel module makefile: Use another name for the makefile and using command line parameter - makefile

I have Kernel module sources (for arm) and I would like to compile two different drivers from the same source.
The kernel in compiled with 2 source files and with cross compile.
MODULE_MAME = modulename
SRCS = drv/source.c lib/libsrc.c
OBJS = $(SRCS:.c=.o)
obj-m += $(MODULE_MAME).o
$(MODULE_MAME)-y = $(OBJS)
KDIR := /mykermelsources/
PWD := $(shell pwd)
all:
$(MAKE) -C $(KDIR) ARCH=arm M=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) ARCH=arm M=$(PWD) clean
In one command, I would like to compile 2 modules.
Two choices:
Keeping 3 different Makefiles, one main that will call both other makefiles. One problem with this, I cannot make it working. make -f makefilediff or make --makefile=makefilediff give me an error.
Log:
make -C /mykermelsources/ ARCH=arm M=/home/mychardriver/ modules
make[1]: Entering directory '/mykermelsources'
scripts/Makefile.build:44: /home/mychardriver/Makefile: No such file or directory
make[2]: *** No rule to make target '/home/mychardriver//Makefile'. Stop.
make[1]: *** [_module_/home/mychardriver/] Error 2
make[1]: Leaving directory '/mykermelsources'
make: *** [all] Error 2
Transmitting command line parameter but it doesn't work. make SIDE=1
Seems the SIDE parameter/variable is never transmitted.
ifeq ($(SIDE),1)
MODULE_MAME = modulename_11
else
MODULE_MAME = modulename_22
endif
SRCS = drv/source.c lib/libsrc.c
OBJS = $(SRCS:.c=.o)a
obj-m += $(MODULE_MAME).o
$(MODULE_MAME)-y = $(OBJS)
KDIR := /mykermelsources/
PWD := $(shell pwd)
all:
$(MAKE) -C $(KDIR) ARCH=arm M=$(PWD) SIDE=$(SIDE) modules
clean:
$(MAKE) -C $(KDIR) ARCH=arm M=$(PWD) clean
How can I build 2 kernel modules from same multiple source files ?

Just faced this issue, and this is what I did:
For each moduleX you want to build, write a Kbuild_moduleX with the targets. Example:
obj-$(MODULE) += MODULE.o
MODULE-y := source.o
Then, in your Makefile_moduleX, do:
all:
cp Kbuild_moduleX Kbuild
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
rm Kbuild
This works because the kernel scripts will give Kbuild priority over reading Makefile.
To compile, do make -f Makefile_moduleX
Is it pretty? No. Does it work? Yes.

Related

Warning: modules_install: missing 'System.map' file. Skipping depmod

I am trying to insert a kernel module using depmod and modprobe utilities in-order to resolve any dependencies. When I build the module it throws "Warning: modules_install: missing 'System.map' file. Skipping depmod."
And later when I try to execute modprobe it throws an error saying
"modprobe: FATAL: Module i2c_eeprom_client.ko not found in directory /lib/modules/4.19.58-v7+"
Below is the make file I am using:
obj-m += i2c_eeprom_client.o
KDIR = /lib/modules/$(shell uname -r)/build
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
make -C $(KDIR) M=$(PWD) modules_install
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
And below is the output of build:
make -C /lib/modules/4.19.58-v7+/build M=/home/pi/work/eeprom modules
make[1]: Entering directory '/usr/src/linux-headers-4.19.58-v7+'
Building modules, stage 2.
MODPOST 1 modules
make[1]: Leaving directory '/usr/src/linux-headers-4.19.58-v7+'
make -C /lib/modules/4.19.58-v7+/build M=/home/pi/work/eeprom
modules_install
make[1]: Entering directory '/usr/src/linux-headers-4.19.58-v7+'
INSTALL /home/pi/work/eeprom/i2c_eeprom_client.ko
DEPMOD 4.19.58-v7+
Warning: modules_install: missing 'System.map' file. Skipping depmod.
make[1]: Leaving directory '/usr/src/linux-headers-4.19.58-v7+'
How can i fix this problem ? Please help
Platform : Raspberry PI 3b+, Raspbian - linux 4.19.58-v7+
You can run depmod after the modules_install step. Also, it is better practice to separate the installation from the building to avoid having to build with root privileges:
obj-m += i2c_eeprom_client.o
# Default to running kernel's build directory if KDIR not set externally
KDIR ?= "/lib/modules/$(shell uname -r)/build"
all:
$(MAKE) -C "$(KDIR)" M="$(CURDIR)" modules
install:
$(MAKE) -C "$(KDIR)" M="$(CURDIR)" modules_install
depmod -A
clean:
$(MAKE) -C "$(KDIR)" M="$(CURDIR)" clean
Invoke as:
$ make
$ make install

Making kernel object with make ARCH=i386

I have a file named dice_driver.c that I would like to make a .ko file out of it. Here's my Makefile:
obj-m := dice_driver.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) M=$(PWD) modules
However, when I entered make ARCH=i386 into my VM on VirtualBox, as per my instructor's guidance, it says
make: Nothing to be done for 'default'.
Could anyone help me with this issue?

makefile error: doing first time kernel module

obj-m += hello.0
KDIR = /usr/src/linux-headers-3.16.0-34-generic
all :
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
rm -rf *.o
error is: Makefile:9: *** missing separator. Stop.
You can use the below Makefile to build the kernel module. Comments inlined in the Makefile
There is an extra space before $(MAKE) in your makefile
obj-m = hello.o
KDIR = /usr/src/linux-headers-3.16.0-34-generic
# Here the commands to build the target should start in the next line with a tab space
all:
<TAB>make -C $(KDIR) M=$(PWD) modules
clean:
<TAB>make -C $(KDIR) M=$(PWD) clean

Proper makefile setup for external kernel modules

I'm writing a character driver to sit on top of a modified version of ahci in the source tree. I basically have something that looks like this:
topdir
|
|- Makfile
|
|- mod_ahci
| | - Makefile, codefiles
|
|- char_interface
| | - Makefile, codefiles
now, char_interface requires symbols from mod_ahci. I have the appropriate EXPORT_SYMBOL() macro use for the symbols I need to export. However, I'm having trouble getting the makefiles right to pick up the header file in mod_ahci from char_interface. My toplevel Makefile
ifneq ($(KERNELRELEASE),)
obj-y := mod_ahci/ char_interface/
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
endif
The makefile for char_interface (because the other builds just fine)
ifneq ($(KERNELRELEASE),)
ccflags-y += -I../mod_ahci
obj-m := char_interface.o
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
default:
$(MAKE) -C $(KERNELDIR) M=$(shell pwd) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(shell pwd) modules_install
endif
clean:
-sudo rmmod ahcip
-rm -f *.ko* *.mod.* *.o modules.order Modules.symvers
I have referenced various text files in the kernel documentation. For example, I'm referring to .../Documentation/kbuild/makefiles.txt right now as well as .../Documentation/kbuild/modules.txt. Whenever I build, I'm getting /home/captaink/devel/kmodtests/char_interface/char_interface.c:2:22: error: mod_ahci.h: No such file or directory. There is a file named mod_ahci.h in the directory ../mod_ahci. What am I getting wrong with my use of ccflags-y in the makefile for the char driver?
Thanks
After some digging, I found the answer to the problem. I was misunderstanding what was happening with the makefile's that I was viewing from LDD3 and the kernel documentation (which is, apparently, exactly where O'Reilly took their examples). The build system actually changes directories into /usr/src/kernels/$(uname -r)/build (or similar) because this is why my header file wasn't being found by the compiler.
I'm not saying this is an elegant way of doing this, but here's how I fixed it. The makefile in the top directory now looks like:
ifneq ($(KERNELRELEASE),)
obj-y := mod_ahci/ char_interface/
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) TOP_DIR=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
endif
And the makefile in the subdirectory containing the char driver interface looks like:
ifneq ($(KERNELRELEASE),)
ccflags-y += -I$(TOP_DIR)/mod_ahci
obj-m := char_interface.o
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
default:
$(MAKE) -C $(KERNELDIR) M=$(shell pwd) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(shell pwd) modules_install
endif
clean:
-sudo rmmod ahcip
-rm -f *.ko* *.mod.* *.o modules.order Modules.symvers
As you can tell, the makefiles have been copied extensively. The subdirectories probably don't need the "shared" makefile stuff in them because that's taken care of by the higher level makefile. Nevertheless, the modules now build and the character driver I have knows of the exported symbols I made in my modified ahci driver.
I hope this may help someone who's a neophyte, like myself, to the Linux kernel build world and Linux kernel drivers.

How to install or copy the driver i.e .ko file to a particular location via makefile?

This is my makefile:
ifneq ($(KERNELRELEASE),)
obj-m := dmcdus_dd.o
else
KDIR := /usr/src/linux-headers-3.13.0-24-generic/
all:
$(MAKE) INSTALL_MOD_DIR=kernel/drivers/input/touchscreen -C $(KDIR) M=$$PWD modules_install
endif
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions sample Module.symvers
I have specified my required path where i have to move my dmcdus_dd.ko file using INSTALL_MOD_DIR & install it by modules_install. When i type "make" in the console i get the following results in the console:
make INSTALL_MOD_DIR=kernel/drivers/input/touchscreen -C /usr/src/linux-headers-3.5.0-49-generic/ M=$PWD modules_install
make[1]: Entering directory `/usr/src/linux-headers-3.5.0-49-generic'
DEPMOD 3.5.0-49-generic
make[1]: Leaving directory `/usr/src/linux-headers-3.5.0-49-generic'
But when i go to the location "/lib/modules/3.5.0-49-generic/kernel/drivers/input/touchscreen" i dont see dmcdus.ko file in that directory... How can i copy my driver to that location?
Before installing first you need to make module using make -C $(KDIR) M=$(PWD) modules.
If you want to make little change in your makefile then write in following way:
ifneq ($(KERNELRELEASE),)
obj-m := dmcdus_dd.o
else
KDIR := /usr/src/linux-headers-3.13.0-24-generic/
all:
make -C $(KDIR) M=$(PWD) modules #I've changed makefile here
$(MAKE) INSTALL_MOD_DIR=kernel/drivers/input/touchscreen -C $(KDIR) M=$$PWD modules_install
endif
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions sample Module.symvers
And make sure that you have permission to copy file to destination (/input/touchscreen)folder. If not then change it.
The below makefile is sufficient for you to build and install module
obj-m := dmcdus_dd.o
KDIR := /usr/src/linux-headers-3.13.0-24-generic/
all:
make -C $(KDIR) M=$$PWD modules
make -C $(KDIR) M=$$PWD modules_install
clean:
make -C $(KDIR) M=$$PWD clean
If you specify INSTALL_MOD_DIR then the modules is moved to that directory
make INSTALL_MOD_DIR=kernel/drivers/input/touchscreen -C $(KDIR) M=$$PWD modules_install

Resources