Search code examples
gccconfiguregentoo

Can I change gcc default options without using command-line?


I am plagued by the Gentoo bug #580414. In short, the default options mislead configure into not detecting standard include files because some headers contain this code:

#if defined _FORTIFY_SOURCE && _FORTIFY_SOURCE > 0
# if !defined __OPTIMIZE__ || __OPTIMIZE__ <= 0
#  warning _FORTIFY_SOURCE requires compiling with optimization (-O)

, and __OPTIMIZE__ is off by default and _FORTIFY_SOURCE is on by default, and the generated warning is perceived as an error, indicating that "stdint.h", "stdlib.h" and many others are absent. Compilation eventually fails and I cannot install programs or even upgrade the gcc itself.

Can I simply put something in environment vars or in the /etc directory to turn on -O or turn off _FORTIFY_SOURCE for every invocation of gcc without editing gentoo build scripts?

Tried in /etc/portage/make.conf

EPATCH_USER_EXCLUDE='*10_all_default-fortify-source*'
CFLAGS="-O2 -O -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0"
CFLAGS_FOR_BUILD="-O2 -O -U_FORTIFY_SOURCE"

without any improvement.


Solution

  • There is no such environment variable. CFLAGS in make.conf won't work because the build systems usually do something like this:

        $(CC) $(CFLAGS) $(MY_HARDCODED_CFLAGS)
    

    thus overwriting your flags.

    But to mangle any argument passed to gcc, you can use the following workaround.

    1. create a directory, eg. under /usr/local/bin/
    2. create a script which will mangle its arguments as you wish and then passes them to gcc or "/usr/bin/" + basename(argv[0]) (beware of infinite recursion)
    3. make this script executable
    4. create symlinks to the script in that directory with names like gcc, cc, x86_64-pc-linux-gnu-gcc
    5. put a bunch of lines like this into /etc/portage/bashrc:

      the_dir="/usr/local/bin/THE_DIR"
      if [[ "${PATH}" != *"${the_dir}"* ]] ; then
          export PATH="${the_dir}:${PATH}"
      fi
      

    Also to save yourself from possible problems in the future, do not forget to put a note about this change somewhere. (As should be done with any workaround anyway.)