Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass compiler options during Linux kernel compilation?

For reasons, I need to compile the Linux kernel (currently 4.7.10) passing some simple and innocent additional command line options (e.g. -pipe -Wsomething etc) to the C compiler.

How do I do it?

More specifically, how do I enforce these compiler flags during plain make as well as during make menuconfig and similar, i.e. so that they are always passed to the C compiler whenever the latter is executed.

like image 373
jotik Avatar asked Nov 05 '16 19:11

jotik


People also ask

What is Kbuild in kernel?

Kbuild refers to different kinds of makefiles: Makefile is the top makefile located in source root. . config is the kernel configuration file. arch/$(ARCH)/Makefile is the arch makefile, which is the supplement to the top makefile.

How long does it take to compile Linux kernel?

The Linux kernel takes around 5 minutes (without modules) to build on an Intel Core i5 Jasper Lake mini PC with 16 GB RAM and a fast SSD based on our recent review of Beelink GTi 11 mini PC.


2 Answers

From Linux kernel's makefile:

# Add any arch overrides and user supplied CPPFLAGS, AFLAGS and CFLAGS as the
# last assignments
KBUILD_CPPFLAGS += $(ARCH_CPPFLAGS) $(KCPPFLAGS)
KBUILD_AFLAGS   += $(ARCH_AFLAGS)   $(KAFLAGS)
KBUILD_CFLAGS   += $(ARCH_CFLAGS)   $(KCFLAGS)

So, passing additional options for Kbuild uses usual environment/makefile variables but with K prefix:

make "KCFLAGS=-pipe -Wsomething"
like image 194
Tsyvarev Avatar answered Oct 21 '22 08:10

Tsyvarev


Kbuild — The Linux Kernel documentation provides a list of options,

  • KCPPFLAGS

    Additional options to pass when preprocessing. The preprocessing options will be used in all cases where kbuild does preprocessing including building C files and assembler files.

  • KAFLAGS

    Additional options to the assembler (for built-in and modules).

  • AFLAGS_MODULE

    Additional assembler options for modules.

  • AFLAGS_KERNEL

    Additional assembler options for built-in.

  • KCFLAGS

    Additional options to the C compiler (for built-in and modules).

  • CFLAGS_KERNEL

    Additional options for $(CC) when used to compile code that is compiled as built-in.

  • CFLAGS_MODULE

    Additional module specific options to use for $(CC).

  • LDFLAGS_MODULE

    Additional options used for $(LD) when linking modules.

  • HOSTCFLAGS

    Additional flags to be passed to $(HOSTCC) when building host programs

like image 34
Coiby Avatar answered Oct 21 '22 08:10

Coiby