Gentoo Wiki




This page is a work in progress. I wrote it because to my knowledge the subject matter is not documented in any official sources. None of the methods shown here are supported or endorsed by Gentoo developers. Do not expect to receive support if you post your issues on Bugzilla.

Tested on: sys-apps/portage-


ADHD version: How to set CFLAGS on per-package basis.

The CFLAGS variable is the primary mechanism to control compiler-level optimization of packages installed by Portage. CFLAGS defined in /etc/make.conf specifies global optimizations. Therefore, it should be set to optimize the "common case". This typically means that the GCC's -O flag should be set to -O2, as most applications perform best at that setting.

However, some applications benefit significantly from GCC settings that make other applications slower. For example, Python benefits from -O3. And, under some circumstances, performance of BLAS ATLAS can be improved by non-standard -mfpmath flags.

This article is about configuring portage to use custom CFLAGS for specific packages. In other words, how to set package-specific exceptions to CFLAGS defined in /etc/make.conf


(thanks devsk)

  1. Create a custom CFLAGS file in /etc/portage/env/ containing CFLAGS=<blah> in one line and CXXFLAGS=<blah> in next.
  2. Create /etc/portage/env/<pkg_cat>/
  3. Link /etc/portage/env/<pkg_cat>/<pkg_name_withoutversion> to the custom CFLAGS file

E.g. to compile Python with -O3:

File: /etc/portage/env/O3-cflags
CFLAGS="-march=<your_arch> -O3 -pipe"

mkdir -p /etc/portage/env/dev-lang

cd /etc/portage/env/dev-lang

ln -s ../O3-cflags python

CFLAGS-sensitive applications

(please contribute to this section!)

Safe Options


* Python (
* SQLite (up to 35%+ speedup;

Unsafe Options

The following are ones that are known to break with specific CFLAGS.

-ffast-math -mfpmath=sse,387

* fontconfig (will break when Firefox is run)


If you are not using the hardened profile, it is possible to build most of your system with this, but don't compile glibc or gcc with it


Based on:

(thanks mudrii)

Retrieved from ""

Last modified: Fri, 05 Sep 2008 08:51:00 +0000 Hits: 6,556