Re: Kbuilds and Makefiles in building the kernel

Nagy Ábrahám Zsombor <[email protected]> Sun, 30 Nov 2025 16:40:00 +0100
Newsgroups gmane.linux.newbie
Message-ID <[email protected]>
On Sun, 2025-11-30 at 06:25 +0800, Nick Huang wrote:
> Nagy =C3=81brah=C3=A1m Zsombor <[email protected]> =E6=96=BC 2025=E5=B9=
=B411=E6=9C=8830=E6=97=A5=E9=80=B1=E6=97=A5 =E4=B8=8A=E5=8D=881:14=E5=AF=AB=
=E9=81=93=EF=BC=9A
> >=20
> > On Sat, 2025-11-29 at 21:59 +0800, Nick Huang wrote:
> > > Nagy =C3=81brah=C3=A1m Zsombor <[email protected]> =E6=96=BC 2025=
=E5=B9=B411=E6=9C=8829=E6=97=A5=E9=80=B1=E5=85=AD
> > > =E4=B8=8B=E5=8D=889:22=E5=AF=AB=E9=81=93=EF=BC=9A
> > > >=20
> > > > On Sat, 2025-11-29 at 12:09 +0800, Nick Huang wrote:
> > > > > Nagy =C3=81brah=C3=A1m Zsombor <[email protected]> =E6=96=BC 20=
25=E5=B9=B411=E6=9C=8828=E6=97=A5=E9=80=B1=E4=BA=94
> > > > > =E4=B8=8B=E5=8D=884:18=E5=AF=AB=E9=81=93=EF=BC=9A
> > > > > >=20
> > > > > > On Fri, 2025-11-28 at 08:12 +0800, Nick Huang wrote:
> > > > > > > Nagy =C3=81brah=C3=A1m Zsombor <[email protected]> =E6=96=
=BC 2025=E5=B9=B411=E6=9C=8828=E6=97=A5
> > > > > > > =E9=80=B1=E4=BA=94
> > > > > > > =E4=B8=8A=E5=8D=881:37=E5=AF=AB=E9=81=93=EF=BC=9A
> > > > > > > >=20
> > > > > > > > On Thu, 2025-11-27 at 19:26 +0800, Nick Huang wrote:
> > > > > > > > > Abraham Zsombor Nagy <[email protected]> =E6=96=BC
> > > > > > > > > 2025=E5=B9=B411=E6=9C=8827=E6=97=A5=E9=80=B1=E5=9B=9B
> > > > > > > > > =E4=B8=8B=E5=8D=886:43=E5=AF=AB=E9=81=93=EF=BC=9A
> > > > > > > > > >=20
> > > > > > > > > > Hello,
> > > > > > > > > >=20
> > > > > > > > > > https://docs.kernel.org/kbuild/makefiles.html#the-kbuil=
d-files
> > > > > > > > > > stat
> > > > > > > > > > es
> > > > > > > > > > that when a directory contains a Kbuild file, the
> > > > > > > > > > Makefile
> > > > > > > > > > is
> > > > > > > > > > skipped in
> > > > > > > > > > that directory.
> > > > > > > > > >=20
> > > > > > > > > > It feels to me that both files are in use e.g.
> > > > > > > > > > under
> > > > > > > > > > arch/x86/
> > > > > > > > > > directory.
> > > > > > > > > >=20
> > > > > > > > > > I'm trying to see what files are the core of the
> > > > > > > > > > kernel
> > > > > > > > > > and
> > > > > > > > > > what
> > > > > > > > > > files
> > > > > > > > > > can be configured for x86 by partially parsing the
> > > > > > > > > > Makefiles
> > > > > > > > > > and
> > > > > > > > > > the
> > > > > > > > > > Kbuild files. Would it be sufficient to parse them
> > > > > > > > > > as:
> > > > > > > > > >=20
> > > > > > > > > > if file =3D=3D "Makefile" or file =3D=3D "Kbuild":
> > > > > > > > > >=20
> > > > > > > > > > =C2=A0> parse(file)
> > > > > > > > > >=20
> > > > > > > > > > or it should prefer the Kbuild file when present,
> > > > > > > > > > or it
> > > > > > > > > > is
> > > > > > > > > > using
> > > > > > > > > > some
> > > > > > > > > > other logic?
> > > > > > > > > >=20
> > > > > > > > > > Sincerely,
> > > > > > > > > >=20
> > > > > > > > > > Abraham Nagy
> > > > > > > > > >=20
> > > > > > > > > >=20
> > > > > > > > >=20
> > > > > > > > > Hi=C2=A0 Abraham Zsombor Nagy
> > > > > > > > > What functionality are you trying to add that would
> > > > > > > > > require
> > > > > > > > > modifying
> > > > > > > > > the Makefile or Kbuild?
> > > > > > > > >=20
> > > > > > > > > Sincerely,
> > > > > > > > > Nick Huang
> > > > > > > >=20
> > > > > > > > Hi Nick,
> > > > > > > >=20
> > > > > > > > I'm not modifying the build files, only writing a
> > > > > > > > script
> > > > > > > > that
> > > > > > > > walks
> > > > > > > > through the source tree and parses Makefiles and Kbuild
> > > > > > > > files
> > > > > > > > to
> > > > > > > > list
> > > > > > > > what source files are linked to the kernel by default
> > > > > > > > and
> > > > > > > > by
> > > > > > > > CONFIG_*
> > > > > > > > options. This is to see what to read through to get a
> > > > > > > > basic
> > > > > > > > understanding of the kernel at a given config.
> > > > > > > >=20
> > > > > > > > As I understand the documentation linked above, I
> > > > > > > > should
> > > > > > > > skip
> > > > > > > > Makefiles
> > > > > > > > when Kbuild files are present in a directory, but
> > > > > > > > Makefiles
> > > > > > > > feels
> > > > > > > > to
> > > > > > > > have important lines too.
> > > > > > > >=20
> > > > > > > > The approach is something like to extract object files
> > > > > > > > and
> > > > > > > > subdirectories from lines that start with python regex
> > > > > > > > "^\S+-y"
> > > > > > > > or
> > > > > > > > "^\S+-\$\(CONFIG.*\)".
> > > > > > > >=20
> > > > > > > > Sincerely,
> > > > > > > > Abraham
> > > > > > > >=20
> > > > > > > >=20
> > > > > > >=20
> > > > > > > Hi Abraham
> > > > > > > You can use git grep -n "CONFIG_**" to find the files you
> > > > > > > need.
> > > > > > >=20
> > > > > > > Sincerely,
> > > > > > > Nick
> > > > > >=20
> > > > > > Hello Nick,
> > > > > >=20
> > > > > > Thank you, I've just tried git grep, it seems convenient,
> > > > > > however I
> > > > > > still need to further script and fine-tune the output, in
> > > > > > which
> > > > > > case I
> > > > > > think having a customizable walk algorithm is the way to
> > > > > > start
> > > > > > with.
> > > > > >=20
> > > > > > find/git & grep walks through the directory tree that could
> > > > > > include
> > > > > > build files and sources that are from an incompatible set
> > > > > > of
> > > > > > config
> > > > > > options. Also, there are some Makefiles that end the line
> > > > > > with
> > > > > > a
> > > > > > backslash '\' saying to process the next line too, but
> > > > > > which
> > > > > > won't
> > > > > > have
> > > > > > regex match.
> > > > > >=20
> > > > > > A custom algorithm can start from a Makefile (and Kbuild
> > > > > > file?)_and
> > > > > > walk directories only that are visible from there. This way
> > > > > > I
> > > > > > can
> > > > > > enforce a horizon of config options that are meaningfully
> > > > > > able
> > > > > > to
> > > > > > extend the kernel. E.g. starting with minimal config,
> > > > > > seeing
> > > > > > the
> > > > > > available additional configs, then in subsequent calls
> > > > > > adding
> > > > > > configs
> > > > > > one by one until I have a whole kernel (but without extra
> > > > > > parts).
> > > > > >=20
> > > > > > Make menuconfig combined with git grep and a text editor
> > > > > > could
> > > > > > help,
> > > > > > but it would require more manual handling to achieve the
> > > > > > same.
> > > > > >=20
> > > > > > Sincerely,
> > > > > > Abraham
> > > > >=20
> > > > > Hi Abraham
> > > > >=20
> > > > > Are you trying to find out which configs can be enabled? If
> > > > > so, I
> > > > > suggest you check the description of each Kconfig to see if
> > > > > it=E2=80=99s
> > > > > what
> > > > > you need, or if you have a clear idea of the functionality
> > > > > you
> > > > > want
> > > > > to
> > > > > implement.
> > > > >=20
> > > > > Is your main goal to build a Linux kernel?
> > > > >=20
> > > > > Sincerely,
> > > > > Nick
> > > >=20
> > > > Hi Nick,
> > > >=20
> > > > Yes, my main goal is to build a Linux kernel that is configured
> > > > specifically for my laptop and usage. Slowly, I would like to
> > > > learn
> > > > the
> > > > build process too and to get familiar with the source roughly.
> > > >=20
> > > > I see that Kconfig has the config dependencies and feature
> > > > description,
> > > > but yet I would need a script to find the corresponding sources
> > > > that
> > > > parses Kconfig files. If I understand well, it is for composing
> > > > a
> > > > meaningful .config file that then passed to the kernel build
> > > > system.
> > > >=20
> > > > My approach would start with the build files, list the included
> > > > object
> > > > files and visible config options that include additional object
> > > > files.
> > > > I would continue this listing with build files in mentioned
> > > > subdirectories, then grep the corresponding source files for
> > > > the
> > > > rest
> > > > of the configs. Then, I would browse the source based on this
> > > > overview.
> > > >=20
> > > > To see how Makefiles and Kbuild files are used, I would need to
> > > > check
> > > > the build system, but I'm hoping I can get some guidance.
> > > >=20
> > > > Sincerely,
> > > > Abraham
> > > Hi Abraham
> > >=20
> > > Do I only need to build the kernel without modifying any code?
> > >=20
> > > Sincerely,
> > > Nick
> >=20
> > Hi Nick,
> >=20
> > I will only modify it a little, e.g default boot params, in case I
> > want
> > to use EFI_STUB (until I find in my UEFI how to pass them without a
> > bootloader).
> >=20
> > Sincerely,
> > Abraham
>=20
> Hi Abraham
> Are you planning to develop a Linux kernel driver, or do you only
> need
> to build the Linux kernel source code?
>=20
> Sincerely,
> Nick


Hi Nick,

I'm planning mainly to build the kernel, but I would try out minor
modifications.

At the moment, my script walks via the Makefiles, then the same happens
with the Kbuild files. I'm fixing things as I encounter them. Example:

$ ./walk-x86.py
...
obj-y
...
arch/x86/kernel/setup.c
arch/x86/kernel/x86_init.c
arch/x86/kernel/i8259.c
arch/x86/kernel/irqinit.c
arch/x86/kernel/probe_roms.c
arch/x86/kernel/bootflag.c
...
obj-$(CONFIG_PCI_BIOS)
arch/x86/pci/pcbios.c
...

Sincerely,
Abraham