1. Overview

There might be situations where we may need to build the Linux kernel source. For example, we may need to customize the kernel because of specific hardware requirements.

Later, we may need to clean and rebuild the already-built Linux kernel because of new modifications in the Linux kernel source code. In this case, two rules in the top-level Makefile of the Linux kernel source code, namely the clean and the mrproper rules, help clean the previous build.

In this tutorial, we’ll discuss the difference between the clean and the mrproper rules in the Linux kernel top-level Makefile.

2. Brief Information About Compiling a Linux Kernel

We must first download the Linux kernel source code. The desired version of the Linux kernel source code can be downloaded from https://cdn.kernel.org/pub/linux/kernel.

After downloading and extracting the source code, we must configure the kernel. There are several options for configuring the kernel. For example, we can use the make oldconfig command for the configuration. The oldconfig target uses a pre-existing configuration file and updates it to work with the new kernel.

After the configuration step, we can build the kernel using make.

After the build process ends, we can install the built modules and the kernel. We can use the make modules_install and make install commands for this purpose.

Later, if we want to rebuild the kernel source code, we can use the clean or the mrproper rules to clean the already compiled code. We’ll discuss these two rules in the next section.

3. The clean and the mrproper Rules

Let’s first check the explanation in the top-level Makefile in the Linux kernel source code:

###
# Cleaning is done on three levels.
# make clean     Delete most generated files
#                Leave enough to build external modules
# make mrproper  Delete the current configuration, and all generated files
# make distclean Remove editor backup files, patch leftover files and the like

According to this explanation, the clean rule deletes most of the generated files but not all of them. However, the mrproper rule deletes all the generated files, including the generated configuration files.

Let’s check the dependencies of the clean rule in the top-level Makefile:

clean: archclean vmlinuxclean resolve_btfids_clean

The clean rule depends on three other rules, namely archclean, vmlinuxclean, and resolve_btfids_clean.

The mrproper rule, on the other hand, depends on the clean rule in addition to the directories specified by the $(mrproper-dirs) variable:

mrproper: clean $(mrproper-dirs)

Therefore, running make mrproper also calls the clean rule. So, running a command like make clean && make mrpoper isn’t meaningful. Running only make mrproper is enough.

4. Comparing Behaviors of clean and mrproper With an Example

We’ll check the effects of the clean and the mrproper rules now. We’ll use the already compiled Linux kernel source code in the /home/alice/work/linux directory.

4.1. make clean

As an example, let’s check the existence of built object files in the /home/alice/work/linux/arch/x86/tools directory:

$ pwd
/home/alice/work/linux
$ ls arch/x86/tools/*o
arch/x86/tools/relocs_32.o  arch/x86/tools/relocs_64.o  arch/x86/tools/relocs_common.o

There are three object files in the arch/x86/tools directory.

There is also a hidden configuration file, .config, generated while configuring the Linux kernel code:

$ ls .config
.config

Now, let’s clean the built kernel code using make clean:

$ make clean >& /dev/null

We redirect the output of make clean to /dev/null using >& /dev/null to suppress the output. Let’s check the existence of the object files in the arch/x86/tools directory:

$ ls arch/x86/tools/*.o
ls: cannot access 'arch/x86/tools/*.o': No such file or directory

Running make clean deletes the object files, as expected. Now, let’s check the existence of the configuration file, .config:

$ ls .config
.config

It still exists as the clean rule doesn’t delete the generated configuration files.

4.2. make mrproper

The already compiled Linux kernel source code is in the /home/alice/work/linux directory as before.

Now, let’s clean the built kernel code using make mrproper:

$ make mrproper >& /dev/null

Let’s check the existence of the object files in the arch/x86/tools directory:

$ ls arch/x86/tools/*.o
ls: cannot access 'arch/x86/tools/*.o': No such file or directory

Running make mrproper deletes the object files, as expected. Now, let’s also check the existence of the configuration file, .config:

$ ls .config
ls: cannot access '.config': No such file or directory

It doesn’t exist as the mrproper rule also deletes the generated configuration files.

5. Conclusion

In this article, we discussed the difference between the clean and the mrproper rules in the Linux kernel top-level Makefile.

First, we discussed briefly the steps of building the Linux source code. Then, we examined the clean and the mrproper rules in the top-level Makefile. We saw that running make mrproper calls the clean rule as the mrproper rule depends on the clean rule.

Additionally, we learned that the clean rule deletes the generated files such as object files, but it doesn’t delete the generated configuration files. But the mrproper rule additionally deletes the generated configuration files.

Finally, we experimented with the clean and the mrproper rules to see their effects on the compiled Linux kernel code.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.