This repository contains a simple hello world kernel module cmake project that:
- Can be built with cmake
- A proper JSON compilation database could be generated by cmake for
clangd
BEFORE any successful build completes
It's different from the existing works that either:
- Fails to generate a proper compilation database that leads to huge amount of errors showing up in your editor
- Could only generate a proper compilation database AFTER a sucessful module build happens
This was made possible simply by generating and building a dummy module at cmake configuring time to get the compile options that Kbuild system emitted and instructing cmake to use similar compile options for generating the compilation database.
A new feature introduced in this template project is to control whether to use a header-only kernel tree for actual building of the module while using a given full kernel tree for compilation database generation. This was designed considering that it could be very convenient if we can check the kernel source implementations while browsing the headers.
This repository was inspired by cmake-kernel-module
- clone the repository
git clone https://github.com/xuwd1/yet-another-cmake-kernel-module
- create build directory
mkdir build
cd build
- configure the cmake project
cmake .. <OPTIONS>
available <OPTIONS>
are:
-HEADER_BUILD=ON|OFF
: Whether to use full kernel tree for compilation database and build with header-only kernel tree. If set ON, both FULL_KERNEL_DIR and HEADER_ONLY_KERNEL_DIR have to be provided, otherwise use HEADER_ONLY_KERNEL_DIR for both compilation database and building. Required, default=ON
Please note that if no compile_commands.json can be found under the full kernel source tree, language server may (definitely) NOT work well when navigating under the kernel source tree. Thus, when using -HEADER_BUILD=ON
, make sure you have generated it, by first build a kernel image from the full kernel source, and run python /path/to/source/scripts/clang-tools/gen_compile_commands.py
under the full kernel source tree
-DFULL_KERNEL_DIR=/path/to/full/kernel/source
: The full kernel source tree location. Optional if -HEADER_BUILD=OFF and otherwise required-DHEADER_ONLY_KERNEL_DIR=/path/to/header/only/kernel/source
: The kernel header tree location. Required-DCLANGD=ON|OFF
: Wether to exclude the compile options that are not understood byclangd
. Optional but RECOMMENDED and default=ON
After this step:
compile_commands.json
will have been generated underbuild
directory. Reload yourclangd
and you should see it functioning properly.- A
Kbuild
file will have been generated under the source directory.
- build the module:
make module
- optionally do cleaning:
make module-clean
- use this repository as a template and modify it to your own need.