Skip to content
Advertisement

How do I specify which of two kernel modules to compile with a Makefile?

I have a Makefile that compiles two Linux kernel modules (mod1.c and mod2.c).

obj-m = mod1.o mod2.o
KDIR=/lib/modules/$(shell uname -r)/build/
PWD=$(shell pwd)

# build the modules
default:
    $(MAKE) -C $(KDIR) M=$(PWD) modules

#cleanup
clean:
    rm -rf *.ko *.o *.mod* .*.cmd modules.order Module.symvers .tmp_versions

This works fine to build both kernel modules when I run make, but I would like to be able to specify which module to build. For example, make mod1 to compile mod1.c and make mod2 to compile mod2.c.

The thing that I am unsure of is how to handle obj-m. Otherwise, specifying which program to compile is well described online.

Advertisement

Answer

The kernel uses variables to set kernel modules on/off:

CROSS_COMPILE ?= /path/to/compiler/arm-linux-gnueabihf-
T1      ?= n
T2      ?= n

obj-$(T1) += test1.o
obj-$(T2) += test2.o
LINUX_SOURCE_DIR=/path/to/linux


all:
        $(MAKE) -C $(LINUX_SOURCE_DIR) M=$(PWD) modules ARCH=arm

test1:
        $(MAKE) T1=m

test2:
        $(MAKE) T2=m

clean:
        rm *o modules.order Module.symvers *mod.c

You can use this as make T1=m or make test2.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement