I'm confronting the Linux kernel build system (Kbuild, kernel ≥2.6.28) with the directory structure and build system for a larger project. Our project contains an out-of-tree Linux kernel module, and our directory structure looks like this (simplified, obviously):
checkout/src/common/*.c source files (common to Linux and other platforms)
checkout/src/linux-driver/*.c source files (for the Linux kernel driver)
checkout/build/linux/Kbuild Kbuild
tmp/linux-2.6.xx/ where the Linux kernel is unpacked and configured
output/linux-arm-debug/ where object files must end up
The build process must not modify anything under checkout
, and building the module must not modify anything under tmp/linux-2.6.xx
. All output files must end up under output/linux-arm-debug
(or whatever architecture and debug variant was selected at build time).
I've read kbuild/modules.txt
, and started to write my Kbuild
file:
MOD_OUTPUT_DIR = ../../../output/linux-$(ARCH)-$(DEBUG)
obj-m += $(MOD_OUTPUT_DIR)/foo_mod.o
$(MOD_OUTPUT_DIR)/our_module-objs := $(MOD_OUTPUT_DIR)/foo_common.o $(MOD_OUTPUT_DIR)/foo_linux.o
This handles storing the object files in a different directory from where Kbuild
lives. Now how can I specify that foo_common.o
needs to be compiled from …/checkout/src/common/foo_common.c
and foo_linux.o
from …/checkout/src/linux-driver/foo_linux.c
?
Here is a Makefile which does out of source-tree builds for out of kernel-tree modules (adapted from @Mark's comment)...
Note: You still need a Kbuild file...
You can set the environment variable
KBUILD_OUTPUT
. This functions similar to theO=
option; however, since it's an environment variable, it can span multiple makefiles whereO=
can't be passed or an out-of-directory module needs to be built. I was having this same issue with trying to build a set of compat-wireless modules and I needed to useO=
for the actual kernel image build.While you haven't mentioned what you've tried so far (or whether you found a solution already), it looks like you just need to continue further down the modules.txt file a bit -- to Section 4.3:
I had a similar problem. I modified
linux_2_6_34/scripts/Makefile.build
as follows.SRCDIR
is the directory source.To compile the module, run
A bit late, but it looks like O= flag is what you need.
My inelegant but effective solution is to copy the source files into the output tree.