# [FAQF] KC14: Kernel compiling, CFLAGS and make.conf settings

## rac

Thanks, int1.  

If the Gentoo developers wanted to make the make.conf CFLAGS apply to the kernel as well, they could rather easily (lostlogic patches the kernel fairly heavily as it is  :Smile: ).  So I don't think it's really the distribution indpendence that's the reason the kernel CFLAGS are kept separate.  I think it's simply the fact that the kernel has its own set of rather demanding restrictions and opinions about which flags it should be compiled with, that are independent of the choices made at the application level.  I could certainly be wrong on this point, however.

It's also not necessary to edit Makefiles at all to change kernel CFLAGS, although that is one way to do it.  Another way is to define the CFLAGS_KERNEL envariable when you compile.

----------

## int1

Didn't know that one.  Thanks for the tip!

int1

PS> I can rewrite this with your tip if you'd like.

----------

## rac

 *int1 wrote:*   

> PS> I can rewrite this with your tip if you'd like.

 

Any time you'd like.  If you'd prefer to wait some time for more feedback here, that's OK, too.

----------

## zypher

BTW: My kernel compilation warns about -malign-functions is obsolete and you should use -falign-functions (on 2.3.18-wolk3.8, which really should be in portage one day...).

Does this mean the function is translated to -falign-functions or is it discarded?

I'll change the Makefile and try it again anyway, just curious.

----------

## ebrostig

 *zypher wrote:*   

> BTW: My kernel compilation warns about -malign-functions is obsolete and you should use -falign-functions (on 2.3.18-wolk3.8, which really should be in portage one day...).
> 
> Does this mean the function is translated to -falign-functions or is it discarded?
> 
> I'll change the Makefile and try it again anyway, just curious.

 

2.3.18 wow! That is old! LOL

The reason is that the compiler flags changed with the gcc 3.x release and the old 2.3.x series of the kernel is not updated to be aware of this fact. besides, the 2.3.x series was the development series for 2.4.x and should not be used anymore.

Why such an old kernel?

Erik

----------

## rac

I think it's a typo of 2.4.18. -malign-functions changed names to -falign-functions somewhere between GCC 2.95 and 3.2.

----------

## Zu`

This is interesting, but I wonder if it's worth it ? Especially since the configuration of your kernel (.config) will specify the arch. (-march=athlon in my case, while I compile everthing else with -march=athlon-xp.) 

So what's useable? -O3 ? -fomit-frame-pointer ? 

Are there any real advantages of compiling the kernel with modified CFLAGS? I.e. noticeable speed increase, or other .. ?

For the ones who have tested this, is it worth bothering with ? 

Thanks.

----------

## rac

Oh, yeah.  Thanks, Zu'.  Thanks for reminding me.  There should probably also be something in here about the GCC3 patch in recent gentoo-sources, which adds "gcc 3+" options to the processor types config option.  That allows another way to change -march.  And, just to make sure things are clear, Zu', the point of this FAQ is not to encourage people to mess around with kernel CFLAGS, it's just that there have been several people that got confused by the fact that the kernel is not compiled with the CFLAGS in make.conf.  They assumed that the kernel is just like any other software being compiled by portage, but it's not.  This confusion has made some kernel compilation threads get confusing, when people start asking about and changing CFLAGS when it doesn't have any effect.  That's why this FAQ entry was born.

----------

## Zu`

Thanks for clarifying rac.

I'm using vanilla-sources and I wasn't aware of that patch in the gentoo-sources. I'll have a look at it. I wonder if it just sets the -march or 'forces' even more optimalisation.

About the FAQ, yes I see how it's meant to be used. Still, It'd be interesting to hear from people who have modified their kernel CFLAGS. Nevertheless, this isn't necessarily the right place to discuss this, we better keep FAQ's somewhat clean  :Smile: 

Regards,

zu

----------

## rac

 *Zu` wrote:*   

> I wonder if it just sets the -march or 'forces' even more optimalisation.

 AFAIK, it just sets -march.

 *Quote:*   

> Nevertheless, this isn't necessarily the right place to discuss this, we better keep FAQ's somewhat clean 

 Don't worry about that.  This is a fine place for feedback on this issue.  In the end, int1 (or somebody who takes over as maintainer) will just keep editing the first post in this thread, and eventually that first post will be split into its own thread in the FAQ forum.  The rest of this discussion will stay here or go somewhere else, but it won't clutter up the eventual FAQ answer.

----------

## int1

 *Zu` wrote:*   

> So what's useable? -O3 ? -fomit-frame-pointer ? 

 

The above options I use as an example are my real kernel compile options, so both work plus some others.

PS> I updated the FAQ....

Thanks to all for comments!

int1

----------

## int1

Noticed this still isn't linked in the FAQ.  Any specific reason? 

int1

----------

## rac

 *int1 wrote:*   

> Noticed this still isn't linked in the FAQ.  Any specific reason?

 Sorry about that.  I'll get to it right now.

----------

## cazze

Hi,

i tried to add -march=i586 for my c3 ezra processor in the Makefile, but with the compile, i got -march=i586 (the one i added) AND -march=i486.

i think it's important to say that if you change something like -march in your Makefile, you have to edit your .config file and deselect all the possible processors.

you can see wich flags are added for your processor in arch/i386(in my case)/Makefile

Hope this will help

kammicazze

----------

## bushwacker

is this information safe to use with the 2.6.x kernel line? I'd like to optimize my kernel a bit, but I dont want to do anything crazy until I know if/what settings can be applied. I have an Athlon XP CPU, so I"d like to use (if possible):

```
-03 -march=athlon-xp -pipe -formit-frame-pointer -funroll-loops
```

I have other options enabled in /etc/make.conf for general apps, most notably -mmmx, -m3dnow, and -msse. Could these safely be added as well?

Thanks.

----------

## wdreinhart

-mmmx -msse and -m3dnow are implied by -march=athlon-xp.  You don't really need them anywhere.

----------

## bushwacker

right now I'm recompiling some KDE Arts library stuff (kde startup segfault, various apps not running because of missing libs, etc), and its taking *much* longer than it did originally. Is -march=xxxx known to take a lot longer than -mcpu=xxxx to operate?

----------

## bushwacker

applying KERNEL_CFLAGS could be done by putting the following in /etc/make.conf, right?

KERNEL_CFLAGS="MY_COMPILE_PARAMETERS_HERE"

It seems to be going ok, but the new kernel 2.6.x build script doesn't show you the compilation data like the older kernels always did. Is there  a way to show this info, too, BTW?

Thanks.

----------

## Master_Of_Disaster

 *bushwacker wrote:*   

> It seems to be going ok, but the new kernel 2.6.x build script doesn't show you the compilation data like the older kernels always did. Is there  a way to show this info, too, BTW?

 

make V=1

----------

## phreaqy

Nobody seem to post if the kernel CFLAGS actually have a speed increase, so has anybody that tried this noticed a speed increase?

----------

## bushwacker

The kernel seems to perform a bit better in terms of task switching/vm/cpu time/etc. I'm not sure if this is just because it's the 2.6x kernel as opposed to the 2.4 though. There seems to be  a larger performance boost between stock 2.4 and stock 2.6 than between stock 2.6 and optimized 2.6  kernels.

----------

## phreaqy

Thanks Man  :Surprised: 

----------

## bushwacker

NP. Let me know if you notice any positive results w/ this.

----------

## dj_farid

Any results?   :Cool: 

----------

## DRZIN

i belive all my issue that have been having is becouse my make.conf is not set up right please help!

what is differentes between menuconfig and make.conf

----------

## Marel

I tried 

```
# export CFLAGS_KERNEL="athlon-xp"

# make V=1
```

but in  output is still only athlon (without xp). Is there something else to do?

----------

## Enlight

https://forums.gentoo.org/viewtopic-p-3350460-highlight-.html#3350460

It's in french but should be understanble. You just have to apply the commands.

BTW the march option is defined by menuconfig. Theese few commands should allow you to build your kernel with the CFLAGS from the make.conf

----------

## charlie

This function, that you can paste into your .bashrc file, uses the MAKEOPTS and CFLAGS variables from /etc/portage/make.conf to compile your kernel with.  

```
cdku () {

JOBS=`cat /etc/portage/make.conf | grep ^MAKEOPTS | sed 's@MAKEOPTS="\(.*\)"$@\1@1'`

cd /usr/src/linux &&

mv -v Makefile{,.orig} && 

cat Makefile.orig | sed 's@CFLAGS_KERNEL =@CFLAGS_KERNEL +=@1' > Makefile &&

export CFLAGS_KERNEL=`cat /etc/portage/make.conf | grep ^CFLAGS | sed 's@CFLAGS="\(.*\)"$@\1@1'` &&

sed -i "s@\(export.*INSTALL_PATH\) ?= /boot@\1=/boot@g" Makefile ;

make all ${JOBS} && make modules_install && make firmware && make firmware_install && make install ;

sed -i "s@\(export.*INSTALL_PATH\)=/boot@\1 ?= /boot@g" Makefile

}
```

If using lilo--the sed instruction changing the INSTALL_PATH line in "Makefile" is there just in case you are compiling the kernel from git and don't want to have a modified Makefile interfering with your "git pull".

----------

## Massimo B.

Is the guide about exporting CFLAGS_KERNEL still valid today?

I was warned to change anything on the kernel CFLAGS as the kernel is very restrictive about that and rarely uses floating point arithmetic at all, however there have been drivers reported to use that..

Anyway, usually I would prefer some -march=native, but as distcc isn't compatible with that I have set the explicit CFLAGS in make.conf, that would be applied by -march=native:

```
# gcc -march=native -v -E - < /dev/null 2>&1 | grep cc1 | perl -pe 's/ -mno-\S+//g; s/^.* - //g;'

-march=sandybridge -mmmx -msse -msse2 -msse3 -mssse3 -mcx16 -msahf -maes -mpclmul -mpopcnt -mavx -msse4.2 -msse4.1 -mfxsr -mxsave -mxsaveopt --param l1-cache-size=32 --param l1-cache-line-size=64 --param l2-cache-size=4096 -mtune=sandybridge -fstack-protector-strong
```

Using distcc for the kernel build as well, I guess the CONFIG_MNATIVE wouldn't work there either, so I plan to use some 

```
export CFLAGS_KERNEL="-march=sandybridge -mmmx -msse -msse2 -msse3 -mssse3 -mcx16 -msahf -maes -mpclmul -mpopcnt -mavx -msse4.2 -msse4.1 -mfxsr -mxsave -mxsaveopt --param l1-cache-size=32 --param l1-cache-line-size=64 --param l2-cache-size=4096 -mtune=sandybridge -fstack-protector-strong
```

..though discouraged on the ##kernel channel. Just trying, this is Gentoo.

Anyone with comments or experience about that?

How would I pass that to genkernel, could that work in a command like this or is there some better place to set the build environment?

```
CFLAGS_KERNEL="-march=sandybridge -mmmx -msse -msse2 -msse3 -mssse3 -mcx16 -msahf -maes -mpclmul -mpopcnt -mavx -msse4.2 -msse4.1 -mfxsr -mxsave -mxsaveopt --param l1-cache-size=32 --param l1-cache-line-size=64 --param l2-cache-size=4096 -mtune=sandybridge -fstack-protector-strong"     pump genkernel all --kernel-cc=distcc --makeopts="-j7 -l2" --loglevel=4 && grub-mkconfig -o /boot/grub/grub.cfg
```

----------

## Sadako

There is a kernel patch to let you specify a more precise processor/family, and enable the relevant -march flag, might be worth trying;

https://github.com/graysky2/kernel_gcc_patch

Been meaning to try it myself, just haven't gotten around to it yet.

BTW, for most options the only thing missing from the proper -march setting that march=native adds is the param options for specifying cache sizes.

----------

## Massimo B.

I guess this is done by USE="experimental", but alas ck-sources don't have that. For comparison I emerged gentoo-sources with that experimental, but I can't see any more features in make menuconfig around CONFIG_MSANDYBRIDGE=y.

Anyway I just patched the Makefile for a try:

```
# egrep "HOSTC.*?FLAGS.* = " /usr/src/linux-4.10.8-ck/Makefile

HOSTCFLAGS   = -Wall -Wmissing-prototypes -Wstrict-prototypes -std=gnu89   -O2 -pipe -fomit-frame-pointer     -march=sandybridge -mmmx -msse -msse2 -msse3 -mssse3 -mcx16 -msahf -maes -mpclmul -mpopcnt -mavx -msse4.2 -msse4 -mfxsr -mxsave -mxsaveopt --param l1-cache-size=32 --param l1-cache-line-size=64 --param l2-cache-size=4096 -mtune=sandybridge -fstack-protector-strong

HOSTCXXFLAGS = -O2 -pipe -fomit-frame-pointer     -march=sandybridge -mmmx -msse -msse2 -msse3 -mssse3 -mcx16 -msahf -maes -mpclmul -mpopcnt -mavx -msse4.2 -msse4 -mfxsr -mxsave -mxsaveopt --param l1-cache-size=32 --param l1-cache-line-size=64 --param l2-cache-size=4096 -mtune=sandybridge -fstack-protector-strong  -fvisibility-inlines-hidden
```

A kernel made like this runs fine currently, does my kernel have the CFLAGS applied now?

I wondered that -march=sandybridge sets a lot less than -march=native.

In the past there was also --help=target to make the settings visible, but due to a old bug 39851 --help=target is a liar:

```
vimdiff <(gcc -march=native -Q --help=target) <(gcc -march=sandybridge -Q --help=target)
```

----------

