The kernel build system allows you to do many more things than just build the full kernel and modules. Chapter 10 includes the full list of options that the kernel build system provides. In this section, we will discuss some of these advanced build options. To see a full description of how to use other advanced build options, refer to the in-kernel documentation on the build system, which can be found in the Documentation/kbuild/ directory of the sources.
The kernel build system works very well as a task that can be split up into little pieces and given to different processors. By doing this, you can use the full power of a multiprocessor machine and reduce the kernel build time considerably.
To build the kernel in a multithreaded way, use the
-j
option to the make program. It is best to give a number to
the -j
option that corresponds to twice the number of
processors in the system. So, for a machine with 2 processors present,
use:
$ make -j4
and for a machine with four processors, use:
$ make -j8
If you do not pass a numerical value to the -j
option
$ make -j
the build system will create a new thread for every subdirectory in
the kernel tree, which can easily cause your machine to become
unresponsive and take a much longer time to complete the build.
Because of this, it is recommended that you always pass a number to
the -j
option.
When doing kernel development, sometimes you wish to build only a specific subdirectory or a single file within the whole kernel tree. The kernel build system allows you to easily do this. To selectively build a specific directory, specify it on the build command line. For example, to build the files in the drivers/usb/serial directory, enter:
$ make drivers/usb/serial
Using this syntax, however, will not build the final module images in
that directory. To do that, you can use the M=
argument:
$ make M=drivers/usb/serial
which will build all the needed files in that directory and link the final module images.
When you build a single directory in one of the ways shown, the final kernel image is not relinked together. Therefore, any changes that were made to the subdirectories will not affect the final kernel image, which is probably not what you desire. Execute a final:
$ make
to have the build system check all changed object files and do the final kernel image link properly.
To build only a specific file in the kernel tree, just pass it as the argument to make. For example, if you wish to build only the drivers/usb/serial/visor.ko kernel module, enter:
$ make drivers/usb/serial/visor.ko
The build system will build all needed files for the visor.ko kernel module, and do the final link to create the module.
Sometimes it is easier to have the source code for the kernel
tree in a read-only location (such as on a CD-ROM, or in a source code
control system), and place the output of the kernel build elsewhere,
so that you do not disturb the original source tree. The kernel build
system handles this easily, by requiring only the single argument
O=
to tell it where to place the
output of the build. For example, if the kernel source is located on a
CD-ROM mounted on /mnt/cdrom/ and
you wish to place the built files in your local directory, enter:
$cd /mnt/cdrom/linux-2.6.17.11/
$make O=~/linux/linux-2.6.17.11
All of the build files will be created in the ~/linux/linux-2.6.17.11/ directory. Please
note that this O=
option should
also be passed to the configuration options of the build so that the
configuration is correctly placed in the output directory and not in
the directory containing the source code.
It is very useful to build the kernel in a cross-compiled manner
to allow a more powerful machine to build a kernel for a smaller
embedded system, or just to check a build for a different architecture
to ensure that a change to the source code did not break something
unexpected. The kernel build system allows you to specify a different
architecture from the current system with the ARCH=
argument. The build system also allows
you to specify the specific compiler that you wish to use for the
build by using the CC=
argument or
a cross-compile toolchain with the CROSS_COMPILE
argument.
For example, to get the default kernel configuration of the x86_64 architecture, you would enter:
$ make ARCH=x86_64 defconfig
To build the whole kernel with an ARM toolchain located in /usr/local/bin/ you would enter:
$ make ARCH=arm CROSS_COMPILE=/usr/local/bin/arm-linux-
It is useful even for a non-cross-compiled kernel to change what the build system uses for the compiler. Examples of this are using the distcc or ccache programs, both of which help greatly reduce the time it takes to build a kernel. To use the ccache program as part of the build system, enter:
$ make CC="ccache gcc"
To use both distcc and ccache together, enter:
$ make CC="ccache distcc"
Get Linux Kernel in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.