title: Building an out-of-tree Linux kernel module in a separate object directory
body: <p>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):</p>
  
  
  
  <pre><code>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
  </code></pre>
  
  <p>The build process must not modify anything under <code>checkout</code>, and building the module must not modify anything under <code>tmp/linux-2.6.xx</code>. All output files must end up under <code>output/linux-arm-debug</code> (or whatever architecture and debug variant was selected at build time).</p>
  
  <p>I've read <a href="http://www.kernel.org/doc/Documentation/kbuild/modules.txt" rel="noreferrer"><code>kbuild/modules.txt</code></a>, and started to write my <code>Kbuild</code> file:</p>
  
  
  
  <pre><code>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
  </code></pre>
  
  <p>This handles storing the object files in a different directory from where <code>Kbuild</code> lives. Now how can I specify that <code>foo_common.o</code> needs to be compiled from <code>…/checkout/src/common/foo_common.c</code> and <code>foo_linux.o</code> from <code>…/checkout/src/linux-driver/foo_linux.c</code>?</p>