[Android Q&A, Help & Troubleshooting] gilius2k15:
I'm having trouble compiling this kernel for the Realme 2 Pro:

https://github.com/realme-kernel-opensource/Realme2Pro-kernel-source

Firstly, there were misc build errors based around paths and syntax used for .C and .H driver source code files, which needed to be made "standalone compilable" ala OnePlus5:

https://github.com/adin-k/oneplus5-kernel/commit/ec4ec687ba23609b9c323145c7d509262b0f025f

For example, this source file header needed changing from:

#include "pmic-voter.h"

To this:

#include <linux/pmic-voter.h>

In other words, the pmic-voter.h file was not located in the same folder as the source file - but inside the "linux" sub-folder instead.

Once the paths were corrected it then builds successfully (albeit one final outstanding error relating to the fingerprint driver).

My first question then is how on earth might paths within the kernel tree become so messed up as to require such a drastic clean up operation!? Is it deliberate or are sub-folders being moved around or perhaps special environment variables being set at compile time?

I then went inside the make menuconfig to change some default kernel settings - specifically to enable Virtualization and KVM:

View attachment 4749722

The kernel then failed to compile again! The first place it falls over is building the cpu_errata.c, which can be fixed by having more paths updated within that file, but then it soon falls over again elsewhere.

I tried another Snapdragon 660 device with a similar kernel (Nokia 7 Plus) and the KVM parts compile fine. I even tried compiling the exact same kernel used by the Realme 2 Pro:

https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.4.78.tar.gz

Of course it compiles fine! Here's the typical steps that were taken:

export CROSS_COMPILE=/home/gilius/Desktop/tool/bin/aarch64-linux-android-

export ARCH=arm64

make O=out clean && make O=out mrproper

make O=out defconfig

make O=out menuconfig (note: here is where we enable Virtualization and KVM)

make O=out -j10

The Realme, Nokia and original kernel sources all share the same tree/directory structure - at least in terms of KVM. I don't know how the KVM modules were originally added to the official Linux Kernel (from version 3.11 onwards) because there are no instructions, but I do know from comparison that the files and directory structures are identical:

/arch/arm64/kvm/hyp

/virt/kvm/arm

/include/kvm

etc.

I then discovered that the original KVM files (perhaps finalised way back in 2014) do not depend on any particular directory structure as per what was required to fix standard OnePlus5/Realme compiles. For example, the kvm_main.c includes the following:

#include <kvm/iodev.h>

#include <linux/kvm_host.h>

#include <asm/processor.h>

But these folders (kvm/linux/asm) are located elsewhere in the kernel tree; the folder containing kvm_main.c has no such sub-folders - yet the compile still works for the Nokia kernel! And to complicate matters further, there happens to be several duplicate directories named "linux" containing different files in need of inclusion by various C and H source code files. In other words: they require files from all different linux folders scattered in all different locations - but without knowing exactly where to find them.

Therefore, the issue with enabling KVM in the Realme 2 Pro must be something to do with environment variables set during at compile time? So I'm guessing a whole of list of them (or symbolic links or some such) are set within a file somewhere to be referenced at compile time? Since all 3 kernels contain identical directory structures, how else might the Realme 2 pro kernel in particular have got messed up so that even basic parts of the kernel, such as kvm, do not compile by default? And how might this be undone and fixed?

Any help would be much appreciated.