How to run Sparse on Linux kernel

Sparse is static analysis tool. We can run Sparse on Linux kernel to find code issues.

Here is now I managed to run Sparse on Linux kernel.

Clone, build and install sparse

Clone sparse

git clone git://git.kernel.org/pub/scm/devel/sparse/sparse.git
cd sparse

Build sparse

make

Install sparse

make install

This will install sparse in user’s home directory ~/bin/. If you want to install to some other directory, you can provide PREFIX parameter

make PREFIX=<install-directory> install

Run sparse on Linux kernel

Add sparse binary to PATH

export PATH=~/bin/:$PATH

Here you may need to use path provided as PREFIX instead of ~/bin if PREFIX option is used while installing.

There are different options to run sparse:

# Run Sparse on files being compiled
make C=1 

# Run Sparse on files all files
make C=2 

# Run Sparse on driver/firmware directory (all files)
make C=2 drivers/firmware/ 

# Provide ARCH and cross-compile options
make ARCH=<> CROSS_COMPILE=<> C=2 

References

https://www.kernel.org/doc/html/v4.11/dev-tools/sparse.html