Search code examples
linux-kernelkernel-module

How can I prepare a Linux source tree so an external module can be compiled against it?


I am keeping a WIFI driver alive by patching compilation errors for new Kernel versions. I can build it against a source tree, so I do not have to boot the kernel for which I want to fix it.

Unfortunately for this I have to fully compile the entire kernel. I know how to build a small version by using make localmodconfig, but that still takes very long.

Recently, I learned about the prepare target. This allows me to "compile" the module, so I learn about compilation problems. However, it fails in the linking phase, which prevents using make prepare in a Git bisect run. I also had the impression that it requires to clean the source tree from time to time due to spurious problems.

The question is: What is the fastest way to prepare a source tree so I can compile a Wifi module against it?


Solution

  • The target you are looking for is modules_prepare. From the doc:

    An alternative is to use the "make" target "modules_prepare." This will make sure the kernel contains the information required. The target exists solely as a simple way to prepare a kernel source tree for building external modules.

    NOTE: "modules_prepare" will not build Module.symvers even if CONFIG_MODVERSIONS is set; therefore, a full kernel build needs to be executed to make module versioning work.

    If you run make -j modules_prepare (-j is important to execute everything in parallel) it should run pretty fast.

    So what you need is basically something like this:

    # Prepare kernel source
    cd '/path/to/kernel/source'
    make localmodconfig
    make -j modules_prepare
    
    # Build your module against it
    cd '/path/to/your/module/source'
    make -j -C '/path/to/kernel/source' M="$(pwd)" modules
    
    # Clean things up 
    make -j -C '/path/to/kernel/source' M="$(pwd)" clean
    cd '/path/to/kernel/source'
    make distclean
    

    The last cleaning up step is needed if you are in a bisect run before proceeding to the next bisection step, otherwise you may leave behind unwanted object files that might make other builds fail.