Skip to contentSkip to conversation

External compilation

DISCUSSION John Levon 4 participants 6 messages Jun 10, 2002

lore
Share & tools
lore

John Levon

author
#0 lore raw

Is there any good example code for compiling a kernel module externally, that works for modversions etc. on 2.2, 2.4, and 2.5, and does the right thing (including Rules.make) ?

I'm having an awful time working out the exact incantations.

On a related note, is it at all possible to make a "mini filesystem" that will work on 2.2 upwards, so I can avoid proc,sysctl, and ioctl ?

thanks
john

Signature · 3 lines
"Saying that taste is just personal preference is a good way to prevent
disputes. The trouble is, it's not true."
	- Paul Graham
#1 lore raw
Quoted 6 lines · John Levon

Is there any good example code for compiling a kernel module externally, that works for modversions etc. on 2.2, 2.4, and 2.5, and does the right thing (including Rules.make) ?

I'm having an awful time working out the exact incantations.

Here is a stripped down version of what I use for bttv currently.

Gerd

==============================[ Makefile ]==============================

# where the kernel sources are located
KDIR := /lib/modules/$(shell uname -r)/build
#KDIR := /work/bk/2.5/build
# kernel version
KVER := $(shell ./uts-release $(KDIR))
MDIR := /lib/modules/$(KVER)/kernel/drivers/media/video

export-objs := bttv-if.o video-buf.o
list-multi := bttv.o
bttv-objs := bttv-driver.o bttv-cards.o bttv-risc.o bttv-if.o bttv-vbi.o

obj-m := video-buf.o bttv.o

multi-m := $(filter $(list-multi), $(obj-m))
int-m := $(sort $(foreach m, $(multi-m), $($(basename $(m))-objs)))

EXTRA_CFLAGS=-g -Wmissing-prototypes -Wstrict-prototypes

here:

	DIR=`pwd`; (cd $(KDIR); make SUBDIRS=$$DIR modules)

include $(KDIR)/Rules.make

bttv.o: $(bttv-objs)

	$(LD) -r -o $@ $(bttv-objs)

==============================[ uts-release ]============================== #! /bin/sh
cat <<-EOF | cpp -P -I$1/include | grep '"' | cut -d'"' -f2

	#include <linux/version.h>
	UTS_RELEASE

EOF

#2 lore raw
In reply to John Levon
Quoted 3 lines · John Levon

Is there any good example code for compiling a kernel module externally, that works for modversions etc. on 2.2, 2.4, and 2.5, and does the right thing (including Rules.make) ?

Well, you need the source for the kernel you're building the module for, it needs to be configured and "make dep" must have been run (for module versions).

(You normally can find it by looking into /lib/modules/uname -r/kernel for the currently running kernel)

Put your module source in some directory, and add a Makefile like

	obj-m := my_module.o

	include $(TOPDIR)/Rules.make

cd into the kernel source and run

make SUBDIRS=/path/to/your/module modules

--Kai

author
#3 lore raw
In reply to John Levon
Quoted 3 lines · Kai Germaschewski

Well, you need the source for the kernel you're building the module for, it needs to be configured and "make dep" must have been run (for module versions).

Obviously :)

I already have stuff working fine w/o the Rules.make include, I'm just trying to do the "right thing"

Quoted 7 lines
	obj-m := my_module.o

	include $(TOPDIR)/Rules.make

cd into the kernel source and run

make SUBDIRS=/path/to/your/module modules

Doesn't work for 2.2. Hopefully I will be able to specify M_OBJS in addition.

Also, you don't specify O_TARGET ?

Given :

TOPDIR=/usr/src/linux
THISDIR=/tmp/mod

O_TARGET=lartmod.o
obj-m := lart.o blah.o

include $(TOPDIR)/Rules.make

all:

        (cd $(TOPDIR) && $(MAKE) SUBDIRS=/tmp/mod modules)

a) default target for Rules.make doesn't do anything useful
b) lartmod.o is never made (how could I convince it to ?)
c) is this going to work OK for modversions...
d) the above seems to disallow a lart.c forming only part of a final lart.o target
e) there seems something is going horribly wrong :

moz mod 317 make all
make: execvp: /usr/src/linux/scripts/pathdown.sh: Permission denied ˆ¶@ˆ¶@re/locale/en_US/LC_MESSAGES/make.mo(cd /usr/src/linux && make SUBDIRS=/tmp/mod modules) make[1]: Entering directory `/usr/src/linux-2.4.0'

The above problems is why I asked for working examples :)

regards
john

Signature · 3 lines
"I continue to be amazed at what Andrei can make templates do. Some of it
 still makes my head hurt."
	- Herb Sutter
#4 lore raw
Quoted 2 lines · John Levon

Doesn't work for 2.2. Hopefully I will be able to specify M_OBJS in addition.

I think later 2.2 support obj-[ym], though I maybe wrong. It won't hurt to specify M_OBJS additionally.

Quoted 7 lines

Also, you don't specify O_TARGET ?

Given :

TOPDIR=/usr/src/linux
THISDIR=/tmp/mod

Quoted 7 lines

O_TARGET=lartmod.o
obj-m := lart.o blah.o

include $(TOPDIR)/Rules.make

all:

        (cd $(TOPDIR) && $(MAKE) SUBDIRS=/tmp/mod modules)

My example was for a single source module. If you have multiple sources, you can either

	O_TARGET := lartmod.o
	obj-y	 := lart.o blah.o
	obj-m    := $(O_TARGET)

or

obj-m := lartmod.o

lartmod-objs := lart.o blah.o

where the later in 2.4 additionally needs

list-multi := lartmod.o

and a link rule like

	lartmod.o: $(lartmod-objs)
        	$(LD) -r -o $@ $(lartmod-objs)
Quoted 1 line

a) default target for Rules.make doesn't do anything useful

The default target (at least in < current 2.5) only builds built-in code.

Quoted 1 line

b) lartmod.o is never made (how could I convince it to ?)

See first example above.

Quoted 1 line

c) is this going to work OK for modversions...

Yup.

Quoted 1 line

d) the above seems to disallow a lart.c forming only part of a final lart.o target

If you only have one source, see my first mail.

Quoted 6 lines

e) there seems something is going horribly wrong :

moz mod 317 make all
make: execvp: /usr/src/linux/scripts/pathdown.sh: Permission denied ˆ¶@ˆ¶@re/locale/en_US/LC_MESSAGES/make.mo(cd /usr/src/linux && make SUBDIRS=/tmp/mod modules) make[1]: Entering directory `/usr/src/linux-2.4.0'

That I suppose is due to the fact that during the "make all" $(CONFIG_SHELL) is not set, thus the

	MOD_DESTDIR := $(shell $(CONFIG_SHELL) $(TOPDIR)/scripts/pathdown.sh)

in Rules.make fails. Try doing the "cd $(TOPDIR) && ..." by hand, it should work then.

--Kai

#5 lore raw
In reply to John Levon
Quoted 3 lines · John Levon

On a related note, is it at all possible to make a "mini filesystem" that will work on 2.2 upwards, so I can avoid proc,sysctl, and ioctl ?

Take a look at drivers/usb/inode.c in the 2.2 kernel. Yes it's ugly, but seems to be the only way to do it on 2.2. It also will work on 2.4 and 2.5. For 2.4/2.5 it's _much_ easier to make a "mini filesystem". See libfs on 2.5, or the pci_hotplug code in 2.4 for examples of this.

Hope this helps,

greg k-h