Issue
I'm building the Linux Kernel with the gcc options -save-temps=obj -o
in order to see exactly how all the C files get preprocessed (and, as a bonus, how the files are turned into assembly).
Here's the version details:
- Running WSL 2 on Windows 10 version 1909 (build 18363.1256)
- The distro is Ubuntu 20.04 LTS
- The installed kernel version is 4.19.128-microsoft-standard
- The kernel tree I'm using is from https://github.com/microhobby/linus-tree, a fork of the WSL kernel used for this tutorial on building the kernel.
- When presented with
[Y/n/?]
flags on my first build, I (tried) to answer with the capitalized option.
I'm not interested in using or installing the built kernel for anything, just seeing how the .i
and .s
files look.
I added a few options to the Makefile:
KBUILD_HOSTCFLAGS := -Wall -Wno-error=deprecated-declarations -Wmissing-prototypes -Wstrict-prototypes -O2 \
-fomit-frame-pointer -save-temps=obj -o -std=gnu89 $(HOST_LFS_CFLAGS) \
$(HOSTCFLAGS)
Specifically, I added the -save-temps=obj -o -Wno-error=deprecated-declarations
flags. Running this works, but only generates a small handful of .i
files totalling only about 9.9 MB, which pales in comparison to the 494 MB of .c
files in the kernel.
I know that only part of the kernel is built based on the architecture you're building it for, but this is quite a discrepancy. Are so few .c
files really compiled by a "standard" build of the kernel? If not, how can I get all the .i
and .s
files generated by gcc?
Solution
First of all, you don't want the -o
there. It sets the name of the output file, so as written, every compilation is going to write its output to a file with the unusual name of -std=gnu89
. (Unless the correct -o
option is added later to override it.)
After fixing that...
KBUILD_HOSTCFLAGS
is the CFLAGS used for building the host programs, which are a tiny fraction of the kernel source code.
Terminology: the host is the machine on which you are building the kernel, and the target is the machine on which the built kernel will eventually be booted. They could be different machines (imagine a software distributor building a kernel to be used by all their customers), or even different architectures in the case of cross-compilation.
As part of the build process, it's necessary to compile and run some relatively small C programs on the host. These do things like parse the kernel config file to create header files, or convert binary firmware code into C source to be compiled into the kernel. The KBUILD_HOSTCFLAGS
option only sets the compiler flags to be used when building these small programs. It does not affect the flags used when compiling the kernel itself. For that, according to https://www.kernel.org/doc/Documentation/kbuild/makefiles.txt, you want to add to the cflags-y
variable. If you put -save-temps
there you ought to see everything you expect. I hope you have an awful lot of disk space.
Answered By - Nate Eldredge Answer Checked By - Timothy Miller (WPSolving Admin)