Setting up the kernel development environment - Basic
This article describes how to do the basic setup for your kernel development environment.
Kernel development requires a number of Linux packages to be installed. This series documents the setup process for an arch-based linux system. The general requirements are defined in minimal requirements to compile the kernel.
To install the base packages execute the following commands:
The next step is to get the current kernel source to the development machine. There are several ways to do this, but the best way is to use git clone. Using git clone we can fetch the next tree or one of the other development trees. In this example the next tree is cloned. Cloning the next tree is described here.
Add the remote so we can fetch the changes and the new tags.
Query which branches are available to be checked out.
To work on some changes it is best to checkout a new branch. The following commands achieve this. The two commands are only examples on how to checkout a branch. Both commands will check out a branch.
The first step in compiling a kernel is to create a kernel configuration. There are various ways to create a kernel configuration. In this guide we use the machine configuration as the starting point for our config:
Both of the above commands create a kernel config file. It is possible that one of the commands does not work. It depends which features are configured in the current kernel. The kernel config file is called .config.
Afterwards invoke menuconfig to see that we have a valid configuration. In addition it is possible that new configuration options have been added and you will get query prompts for them. If unsure answer the question with ’n’ (no).
The last step is to compile the kernel. The kernel can be compiled with the following command. The command invokes the make command with the -j option. The -j option specifies the parallelism (how many workers work in parallel on the compilation).
or if you want to compile with clang, you can use:
To make development easier tools like cscope and tags are used to query where source code symbols are used or defined. cscope and tags are the older tools. They require a symbol database. The symbol database for these commands can be created with the make command.
The cscope command has the advantage that it also has a terminal program to run queries against the database. Otherwise the queries can be run from an editor like vim or emacs. cscope is described here and ctags is described here.
A more modern way to query source code symbols is by using the compiler databases. Instead of parsing the source code, they use the AST of the compiler. Most editors have the ability to integrate and query that information. The disadvantage to cscope is that only the symbols that are currently compiled in can be queried. Kernel features that are not compiled in will not report source code symbols. This can be a problem when kernel code changes are made. However compiler databases have the advantage that they are more accurate.