Compile multiple modules with a single makefile
Hi all, I have the one .c source file that I want to create multiple kernel modules out of it by setting macros using the makefile. The idea is that if -DMODULE1 is set, I'll build module1.ko and if -DMODULE2 is set, I'll build module 2. Does anyone know how to do this? I created the following makefile: ... module1: override obj-m := module1.o module1: EXTRA_CFLAGS += -DMODULE1 module1: $(MAKE) -C $(KDIR) M=$(PWD) modules module2: override obj-m := module2.o module2: EXTRA_CFLAGS += -DMODULE2 module2: $(MAKE) -C $(KDIR) M=$(PWD) modules ... But it isn't working as I expected. Anyone with kernel build system experience knows the answer for that? Thanks, -- Henrique Rodrigues
On Fri, 26 Apr 2013 11:11:09 -0700, Henrique Rodrigues said:
I have the one .c source file that I want to create multiple kernel modules out of it by setting macros using the makefile.
Generally considered ugly by the mainstream kernel.
The idea is that if -DMODULE1 is set, I'll build module1.ko and if -DMODULE2 is set, I'll build module 2.
The way this would usually be done is splitting your module into 3 parts - one common, one for device 1 and one for device 2. Then something like this (cut-n-paste from drivers/net/wireless/hostap/Makefile: hostap-y := hostap_80211_rx.o hostap_80211_tx.o hostap_ap.o hostap_info.o \ hostap_ioctl.o hostap_main.o hostap_proc.o obj-$(CONFIG_HOSTAP) += hostap.o obj-$(CONFIG_HOSTAP_CS) += hostap_cs.o obj-$(CONFIG_HOSTAP_PLX) += hostap_plx.o obj-$(CONFIG_HOSTAP_PCI) += hostap_pci.o Which will build 3 different modules (_cs, _plx, _pci) using the common C files and 1 device-specific C file for each one.
Hi Valdis, On Fri, Apr 26, 2013 at 12:48 PM, <Valdis.Kletnieks@vt.edu> wrote:
On Fri, 26 Apr 2013 11:11:09 -0700, Henrique Rodrigues said:
I have the one .c source file that I want to create multiple kernel modules out of it by setting macros using the makefile.
Generally considered ugly by the mainstream kernel.
I agree. I'm actually trying to split the code that was given to me into three modules with minimal effort (without having to rewrite new code). The behavior of the code is defined by lots of #if macros in it, and I wanted to take advantage of that by rewriting only the makefile. The problem is that if I set the obj-m variable within a make target (instead of defining it globally), the kernel build system doesn't compile anything.
The idea is that if -DMODULE1 is set, I'll build module1.ko and if -DMODULE2 is set, I'll build module 2.
The way this would usually be done is splitting your module into 3 parts - one common, one for device 1 and one for device 2.
Then something like this (cut-n-paste from drivers/net/wireless/hostap/Makefile:
hostap-y := hostap_80211_rx.o hostap_80211_tx.o hostap_ap.o hostap_info.o \ hostap_ioctl.o hostap_main.o hostap_proc.o obj-$(CONFIG_HOSTAP) += hostap.o
obj-$(CONFIG_HOSTAP_CS) += hostap_cs.o obj-$(CONFIG_HOSTAP_PLX) += hostap_plx.o obj-$(CONFIG_HOSTAP_PCI) += hostap_pci.o
Which will build 3 different modules (_cs, _plx, _pci) using the common C files and 1 device-specific C file for each one.
I checked the hostap code and it looks like each file (_cs, _plx, _pci) defines a different module. I believe that is not exactly what I wanted, but I understand that it works. Thanks, -- Henrique Rodrigues http://www.dcc.ufmg.br/~hsr
I figured it out. The problem I was having is that the statement "module1: override obj-m := module1.o" in my make target is not setting obj-m globally. Then, when the kernel build system comes into action (when the "make -C" command is invoked), the obj-m variable isn't set. This is probably more a make related question, but since it is in kernel build context, I thought I could get some advice from kernel folks. Thanks, -Henrique On Fri, Apr 26, 2013 at 2:18 PM, Henrique Rodrigues < henriquesilvar@gmail.com> wrote:
Hi Valdis,
On Fri, Apr 26, 2013 at 12:48 PM, <Valdis.Kletnieks@vt.edu> wrote:
On Fri, 26 Apr 2013 11:11:09 -0700, Henrique Rodrigues said:
I have the one .c source file that I want to create multiple kernel modules out of it by setting macros using the makefile.
Generally considered ugly by the mainstream kernel.
I agree. I'm actually trying to split the code that was given to me into three modules with minimal effort (without having to rewrite new code). The behavior of the code is defined by lots of #if macros in it, and I wanted to take advantage of that by rewriting only the makefile.
The problem is that if I set the obj-m variable within a make target (instead of defining it globally), the kernel build system doesn't compile anything.
The idea is that if -DMODULE1 is set, I'll build module1.ko and if -DMODULE2 is set, I'll build module 2.
The way this would usually be done is splitting your module into 3 parts - one common, one for device 1 and one for device 2.
Then something like this (cut-n-paste from drivers/net/wireless/hostap/Makefile:
hostap-y := hostap_80211_rx.o hostap_80211_tx.o hostap_ap.o hostap_info.o \ hostap_ioctl.o hostap_main.o hostap_proc.o obj-$(CONFIG_HOSTAP) += hostap.o
obj-$(CONFIG_HOSTAP_CS) += hostap_cs.o obj-$(CONFIG_HOSTAP_PLX) += hostap_plx.o obj-$(CONFIG_HOSTAP_PCI) += hostap_pci.o
Which will build 3 different modules (_cs, _plx, _pci) using the common C files and 1 device-specific C file for each one.
I checked the hostap code and it looks like each file (_cs, _plx, _pci) defines a different module. I believe that is not exactly what I wanted, but I understand that it works.
Thanks, -- Henrique Rodrigues http://www.dcc.ufmg.br/~hsr
-- Henrique Rodrigues http://www.dcc.ufmg.br/~hsr
participants (2)
-
Henrique Rodrigues -
Valdis.Kletnieks@vt.edu