How to force add compiler options for building the Linux kernel?

Debian_SuperUser

Active Member
Joined
Mar 18, 2024
Messages
161
Reaction score
41
Credits
1,949
This is just an experiment. I want to force add compiler options such as "-Ofast -march=native -mtune=native -m64 -flto -s" to each file that compiles in the build process and also remove certain ones such as -g along the replacement of the ones I mentioned. Just settings your CFLAGS or Kbuild flags or setting in the Makefile don't do anything. The build system of each modules will have their own compiler options.

I made a script to intercept all calls to gcc and add/remove compiler options. That worked whenever I tried to compile anything but not for the kernel. The build fails and using V=1 I can see my options aren't being applied.

Why doesn't the build system even make better use of the compiler options to prioritize optimization?

Does anyone have any better idea?
 


Don't use Kbuild for custom builds, it overrides compiler flags. Just run ./configure and make from the command line.

Even if you jhard-code changes in the makefile Kbuild can over-ride them. Do not use Kbuild for custom Kernel compiles.

If you run from the command line, you can pass options like this
Code:
make KCFLAGS="-Ofast -march=native -mtune=native -m64 -flto -s -g0"

As a rule, the Linux kernel doesn't want to be hardware optimized. (There are ways around this).
It wants to be portable and run on as many kinds of hardware as possible. I will tell you from experience, even
if you do get it fully optimized, it will save you something like .0001 seconds in execution.

Kbuild is great if you're just doing a standard compile.
 
Just run ./configure
I can't find any file called configure.

Even if you jhard-code changes in the makefile Kbuild can over-ride them. Do not use Kbuild for custom Kernel compiles.
I actually replaced all instances of -O2 with better optimization flags in all sub directories. The kernel actually compiled and I saw those flags being applied in the build process. I am going to run some benchmarks. But I still don't want to do it like this. I didn't have good control over the compiler options and left some on like -g.

As a rule, the Linux kernel doesn't want to be hardware optimized. (There are ways around this).
It wants to be portable and run on as many kinds of hardware as possible. I will tell you from experience, even
if you do get it fully optimized, it will save you something like .0001 seconds in execution.
Of course march=native won't be distributable. But I don't see any -march option for better optimization. Or at least there should be a build profile to enable them easily. That's how I would write a build system.
 
I can't find any file called configure.

I can't remember now, I think it's config or xconfig.
We had a thread about this a few months go.

 


Follow Linux.org

Members online


Top