MKaama
MKaama

Reputation: 1939

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.

Upvotes: 1

Views: 1691

Answers (2)

rindeal
rindeal

Reputation: 1063

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.)

Upvotes: 1

MKaama
MKaama

Reputation: 1939

Just documenting the commands I actually used to resolve the issue.

mv /usr/bin/i686-pc-linux-gnu-gcc /usr/bin/i686-pc-linux-gnu-gcc.OLD
cat >/usr/bin/i686-pc-linux-gnu-gcc
/usr/bin/i686-pc-linux-gnu-gcc.OLD -O "$@"

ctrl+D

chmod +x /usr/bin/i686-pc-linux-gnu-gcc
cp /usr/i686-pc-linux-gnu/gcc-bin/4.6.3/i686-pc-linux-gnu-gcc /usr/i686-pc-linux-gnu/gcc-bin/4.6.3/i686-pc-linux-gnuu-gcc
mv /usr/bin/i686-pc-linux-gnu-g++ /usr/bin/i686-pc-linux-gnuu-g++
cat >/usr/bin/i686-pc-linux-gnu-g++
/usr/bin/i686-pc-linux-gnuu-g++ -O "$@"

ctrl+D

chmod +x /usr/bin/i686-pc-linux-gnu-g++
cp /usr/i686-pc-linux-gnu/gcc-bin/4.6.3/i686-pc-linux-gnu-g++ /usr/i686-pc-linux-gnu/gcc-bin/4.6.3/i686-pc-linux-gnuu-g++
cp /etc/env.d/gcc/i686-pc-linux-gnu-4.6.3.O /etc/env.d/gcc/i686-pc-linux-gnuu-4.6.3

The credit all goes to rindeal. Recap:

  1. identify the binaries that get invoked as compilers
  2. rename them
  3. in their place, create shell scripts that prepend "-O"
  4. create a gcc profile to appease gcc-config
  5. emerge all the unwieldy packages!

Upvotes: 0

Related Questions