Kernel compile flags, How to set, Which ones, more choices


How can I set gcc compiler flags for optimizations when I compile a kernel ?

I followed succesfully the instructions on but I cannot seem to figure out how to set the flags.

I have a i9 9900kf cpu which means skylake for -mtune= (as coffee lake does not exist separately). My env and /usr/share/defaults/etc/profile both show the same situation for CFLAGS and CXXFLAGS, in which there is -mtune=skylake, but when I compile the kernel it always shows -march=westmere and -mtune=haswell in both CFLAGS and CXXFLAGS.

In the kernel Makefile there is a:
section, but settings KCFLAGS and KCPPFLAGS changes nothing.

grep-ing for westmere or haswell gives no result in the clearlinux/packages/linux directory, so these are not written anywhere. How does the kernel make process even decide what flags to use ?

Also, -march used to include -mtune but by other people’s testing this is no longer the case. So, should I use both, should I use only one. Is it better to use -march=native and/or -mtune=native (I gues it would choose skylake) … I have heard that that can give a different final binary, as well even if the correct -mtune and -march are chosen right.

So, which ones are important among: CFLAGS, CXXFLAGS, CPPFLAGS, AFLAGS and LDFLAGS ?
And how can I set them ?
And how about -mtune and/or -march + skylake or native ?

Also, if this is the wrong section of the forum to post this in - please move it to another … I couldn’t choose between general discussion and dev, and since I am not a dev … I figured it should be here.

I am going to be running something heavy on my cpu, so I want to sqweeze it to the limit (plus I have overclocked it pretty decent) and I am also simply always interested in optimization.

You should not have to do this - all software is configured to be compiled with flags most efficiently for your CPU architecture - this is why we compile things several times. The right compiler flags are set by default in your shell.

In theory, indeed using native for both -march and -mtune is near ideal (there’s more flags that we add, of course, by default - don’t forget those)

Note, for the kernel specifically, I would not expect to see much difference in performance except for maybe a micro benchmark. Userspace code likely will have no benefit from tuning the kernel from haswell to skylake.

OK, thanx for the answer, but still … I was browsing the internet for quite a while and I cannot find the current way of setting compiler flags for the kernel. My environment variables are set right, they were set by default, but the kernel does not seem to care. In its Makefile there is no way of finding how to do it, the mechanism is not known to me at the moment. Once upon a time I just set everything in the environment and the make process was reading the variables and … it that was that.

Can someone tell me if I am to compile a kernel now, how to set those variables. And also how to make the make process more verbous. Before I could type make V=1 build, now that does not work, either.

I grep -r CFLAGS *-ed and grep -r CXXFLAGS *-ed at the dir before the very linux-x.x.x source dir and still found nothing of reading these variables … any line of how to use them and are they even useful anymore for kernel compile.

So, how do I do it.