Skip to content

A better example cmake project that builds a kernel module and generates compilation database for it.

License

Notifications You must be signed in to change notification settings

xuwd1/yet-another-cmake-kernel-module

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Yet another cmake-kernel-module

What is this

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

Usage

  1. clone the repository
git clone https://github.com/xuwd1/yet-another-cmake-kernel-module
  1. create build directory
mkdir build
cd build
  1. 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 by clangd. Optional but RECOMMENDED and default=ON

After this step:

  • compile_commands.json will have been generated under build directory. Reload your clangd and you should see it functioning properly.
  • A Kbuild file will have been generated under the source directory.
  1. build the module:
make module
  1. optionally do cleaning:
make module-clean
  1. use this repository as a template and modify it to your own need.

About

A better example cmake project that builds a kernel module and generates compilation database for it.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published