1 # Copyright 1999-2014 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: flag-o-matic.eclass
8 # @BLURB: common functions to manipulate and query toolchain flags
10 # This eclass contains a suite of functions to help developers sanely
11 # and safely manage toolchain flags in their builds.
13 if [[ -z ${_FLAG_O_MATIC_ECLASS} ]]; then
14 _FLAG_O_MATIC_ECLASS=1
16 inherit eutils toolchain-funcs multilib
18 # Return all the flag variables that our high level funcs operate on.
20 echo {C,CPP,CXX,CCAS,F,FC,LD}FLAGS
23 # {C,CPP,CXX,CCAS,F,FC,LD}FLAGS that we allow in strip-flags
24 # Note: shell globs and character lists are allowed
25 setup-allowed-flags() {
27 -pipe -O '-O[12sg]' -mcpu -march -mtune
28 '-fstack-protector*' '-fsanitize*'
29 -fbounds-check -fbounds-checking -fno-strict-overflow
30 -fno-PIE -fno-pie -nopie -fno-unit-at-a-time
31 -g '-g[0-9]' -ggdb '-ggdb[0-9]' '-gdwarf-*' gstabs -gstabs+
32 -fno-ident -fpermissive -frecord-gcc-switches
36 # CPPFLAGS and LDFLAGS
40 # allow a bunch of flags that negate features / control ABI
42 '-fno-stack-protector*' '-fabi-version=*'
43 -fno-strict-aliasing -fno-bounds-check -fno-bounds-checking -fstrict-overflow
44 -fno-omit-frame-pointer '-fno-builtin*'
47 -mregparm -mno-app-regs -mapp-regs -mno-mmx -mno-sse
48 -mno-sse2 -mno-sse3 -mno-ssse3 -mno-sse4 -mno-sse4.1 -mno-sse4.2
49 -mno-avx -mno-aes -mno-pclmul -mno-sse4a -mno-3dnow -mno-popcnt
50 -mno-abm -mips1 -mips2 -mips3 -mips4 -mips32 -mips64 -mips16 -mplt
51 -msoft-float -mno-soft-float -mhard-float -mno-hard-float -mfpu
52 -mieee -mieee-with-inexact -mschedule -mfloat-gprs -mspe -mno-spe
53 -mtls-direct-seg-refs -mno-tls-direct-seg-refs -mflat -mno-flat
54 -mno-faster-structs -mfaster-structs -m32 -m64 -mx32 -mabi
55 -mlittle-endian -mbig-endian -EL -EB -fPIC -mlive-g0 -mcmodel
56 -mstack-bias -mno-stack-bias -msecure-plt '-m*-toc' -mfloat-abi
57 -mfix-r10000 -mno-fix-r10000
60 -mno-fma4 -mno-movbe -mno-xop -mno-lwp
62 -mno-fsgsbase -mno-rdrnd -mno-f16c -mno-bmi -mno-tbm
64 -mno-avx2 -mno-bmi2 -mno-fma -mno-lzcnt
66 -mno-fxsr -mno-hle -mno-rtm -mno-xsave -mno-xsaveopt
68 -mno-avx512cd -mno-avx512er -mno-avx512f -mno-avx512pf -mno-sha
72 # inverted filters for hardened compiler. This is trying to unpick
73 # the hardened compiler defaults.
78 # Ideally we should only concern ourselves with PIE flags,
79 # not -fPIC or -fpic, but too many places filter -fPIC without
80 # thinking about -fPIE.
81 -fPIC|-fpic|-fPIE|-fpie|-Wl,pie|-pie)
82 gcc-specs-pie || continue
83 is-flagq -nopie || append-flags -nopie;;
85 gcc-specs-ssp || continue
86 is-flagq -fno-stack-protector || append-flags $(test-flags -fno-stack-protector);;
87 -fstack-protector-all)
88 gcc-specs-ssp-to-all || continue
89 is-flagq -fno-stack-protector-all || append-flags $(test-flags -fno-stack-protector-all);;
91 gcc-specs-nostrict || continue
92 is-flagq -fstrict-overflow || append-flags $(test-flags -fstrict-overflow);;
97 # Remove occurrences of strings from variable given in $1
98 # Strings removed are matched as globs, so for example
99 # '-O*' would remove -O1, -O2 etc.
101 local f x var=$1 new=()
104 for f in ${!var} ; do
106 # Note this should work with globs like -O*
107 [[ ${f} == ${x} ]] && continue 2
111 eval export ${var}=\""${new[*]}"\"
114 # @FUNCTION: filter-flags
117 # Remove particular <flags> from {C,CPP,CXX,CCAS,F,FC,LD}FLAGS. Accepts shell globs.
119 _filter-hardened "$@"
121 for v in $(all-flag-vars) ; do
122 _filter-var ${v} "$@"
127 # @FUNCTION: filter-lfs-flags
129 # Remove flags that enable Large File Support.
131 [[ $# -ne 0 ]] && die "filter-lfs-flags takes no arguments"
132 # http://www.gnu.org/s/libc/manual/html_node/Feature-Test-Macros.html
133 # _LARGEFILE_SOURCE: enable support for new LFS funcs (ftello/etc...)
134 # _LARGEFILE64_SOURCE: enable support for 64bit variants (off64_t/fseeko64/etc...)
135 # _FILE_OFFSET_BITS: default to 64bit variants (off_t is defined as off64_t)
136 filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
139 # @FUNCTION: filter-ldflags
142 # Remove particular <flags> from LDFLAGS. Accepts shell globs.
144 _filter-var LDFLAGS "$@"
148 # @FUNCTION: append-cppflags
151 # Add extra <flags> to the current CPPFLAGS.
153 [[ $# -eq 0 ]] && return 0
154 export CPPFLAGS+=" $*"
158 # @FUNCTION: append-cflags
161 # Add extra <flags> to the current CFLAGS. If a flag might not be supported
162 # with different compilers (or versions), then use test-flags-CC like so:
164 # append-cflags $(test-flags-CC -funky-flag)
167 [[ $# -eq 0 ]] && return 0
168 # Do not do automatic flag testing ourselves. #417047
173 # @FUNCTION: append-cxxflags
176 # Add extra <flags> to the current CXXFLAGS. If a flag might not be supported
177 # with different compilers (or versions), then use test-flags-CXX like so:
179 # append-cxxflags $(test-flags-CXX -funky-flag)
182 [[ $# -eq 0 ]] && return 0
183 # Do not do automatic flag testing ourselves. #417047
184 export CXXFLAGS+=" $*"
188 # @FUNCTION: append-fflags
191 # Add extra <flags> to the current {F,FC}FLAGS. If a flag might not be supported
192 # with different compilers (or versions), then use test-flags-F77 like so:
194 # append-fflags $(test-flags-F77 -funky-flag)
197 [[ $# -eq 0 ]] && return 0
198 # Do not do automatic flag testing ourselves. #417047
200 export FCFLAGS+=" $*"
204 # @FUNCTION: append-lfs-flags
206 # Add flags that enable Large File Support.
208 [[ $# -ne 0 ]] && die "append-lfs-flags takes no arguments"
209 # see comments in filter-lfs-flags func for meaning of these
210 append-cppflags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
213 # @FUNCTION: append-ldflags
216 # Add extra <flags> to the current LDFLAGS.
218 [[ $# -eq 0 ]] && return 0
221 [[ ${flag} == -l* ]] && \
222 eqawarn "Appending a library link instruction (${flag}); libraries to link to should not be passed through LDFLAGS"
225 export LDFLAGS="${LDFLAGS} $*"
229 # @FUNCTION: append-flags
232 # Add extra <flags> to your current {C,CXX,F,FC}FLAGS.
234 [[ $# -eq 0 ]] && return 0
236 *' '-[DIU]*) eqawarn 'please use append-cppflags for preprocessor flags' ;;
238 *' '-Wl,*) eqawarn 'please use append-ldflags for linker flags' ;;
246 # @FUNCTION: replace-flags
247 # @USAGE: <old> <new>
249 # Replace the <old> flag with <new>. Accepts shell globs for <old>.
251 [[ $# != 2 ]] && die "Usage: replace-flags <old flag> <new flag>"
254 for var in $(all-flag-vars) ; do
255 # Looping over the flags instead of using a global
256 # substitution ensures that we're working with flag atoms.
257 # Otherwise globs like -O* have the potential to wipe out the
260 for f in ${!var} ; do
261 # Note this should work with globs like -O*
262 [[ ${f} == ${1} ]] && f=${2}
265 eval export ${var}=\""${new[*]}"\"
271 # @FUNCTION: replace-cpu-flags
272 # @USAGE: <old> <new>
274 # Replace cpu flags (like -march/-mcpu/-mtune) that select the <old> cpu
275 # with flags that select the <new> cpu. Accepts shell globs for <old>.
276 replace-cpu-flags() {
277 local newcpu="$#" ; newcpu="${!newcpu}"
278 while [ $# -gt 1 ] ; do
279 # quote to make sure that no globbing is done (particularly on
280 # ${oldcpu}) prior to calling replace-flags
281 replace-flags "-march=${1}" "-march=${newcpu}"
282 replace-flags "-mcpu=${1}" "-mcpu=${newcpu}"
283 replace-flags "-mtune=${1}" "-mtune=${newcpu}"
291 eval var=\""\${$1[*]}"\"
293 [[ ${x} == $2 ]] && return 0
298 # @FUNCTION: is-flagq
301 # Returns shell true if <flag> is in {C,CXX,F,FC}FLAGS, else returns shell false. Accepts shell globs.
303 [[ -n $2 ]] && die "Usage: is-flag <flag>"
306 for var in $(all-flag-vars) ; do
307 _is_flagq ${var} "$1" && return 0
315 # Echo's "true" if flag is set in {C,CXX,F,FC}FLAGS. Accepts shell globs.
317 is-flagq "$@" && echo true
320 # @FUNCTION: is-ldflagq
323 # Returns shell true if <flag> is in LDFLAGS, else returns shell false. Accepts shell globs.
325 [[ -n $2 ]] && die "Usage: is-ldflag <flag>"
329 # @FUNCTION: is-ldflag
332 # Echo's "true" if flag is set in LDFLAGS. Accepts shell globs.
334 is-ldflagq "$@" && echo true
337 # @FUNCTION: filter-mfpmath
338 # @USAGE: <math types>
340 # Remove specified math types from the fpmath flag. For example, if the user
341 # has -mfpmath=sse,386, running `filter-mfpmath sse` will leave the user with
344 local orig_mfpmath new_math prune_math
346 # save the original -mfpmath flag
347 orig_mfpmath=$(get-flag -mfpmath)
348 # get the value of the current -mfpmath flag
349 new_math=$(get-flag mfpmath)
350 # convert "both" to something we can filter
351 new_math=${new_math/both/387,sse}
352 new_math=" ${new_math//[,+]/ } "
353 # figure out which math values are to be removed
355 for prune_math in "$@" ; do
356 new_math=${new_math/ ${prune_math} / }
358 new_math=$(echo ${new_math})
359 new_math=${new_math// /,}
361 if [[ -z ${new_math} ]] ; then
362 # if we're removing all user specified math values are
363 # slated for removal, then we just filter the flag
364 filter-flags ${orig_mfpmath}
366 # if we only want to filter some of the user specified
367 # math values, then we replace the current flag
368 replace-flags ${orig_mfpmath} -mfpmath=${new_math}
373 # @FUNCTION: strip-flags
375 # Strip *FLAGS of everything except known good/safe flags. This runs over all
376 # flags returned by all_flag_vars().
383 set -f # disable pathname expansion
385 for var in $(all-flag-vars) ; do
388 for x in ${!var} ; do
390 for y in "${ALLOWED_FLAGS[@]}" ; do
391 if [[ -z ${flag%%${y}} ]] ; then
398 # In case we filtered out all optimization flags fallback to -O2
399 if _is_flagq ${var} "-O*" && ! _is_flagq new "-O*" ; then
403 if [[ ${!var} != "${new[*]}" ]] ; then
404 einfo "strip-flags: ${var}: changed '${!var}' to '${new[*]}'"
406 eval export ${var}=\""${new[*]}"\"
409 set +f # re-enable pathname expansion
419 [[ -z ${comp} || -z ${flag} ]] && return 1
423 # Clang will warn about unknown gcc flags but exit 0.
424 # Need -Werror to force it to exit non-zero.
426 # Use -c so we can test the assembler as well.
429 if "${cmdline[@]}" -x${lang} - </dev/null >/dev/null 2>&1 ; then
430 "${cmdline[@]}" "${flag}" -x${lang} - </dev/null >/dev/null 2>&1
432 "${cmdline[@]}" "${flag}" -c -o /dev/null /dev/null >/dev/null 2>&1
436 # @FUNCTION: test-flag-CC
439 # Returns shell true if <flag> is supported by the C compiler, else returns shell false.
440 test-flag-CC() { test-flag-PROG "CC" c "$1"; }
442 # @FUNCTION: test-flag-CXX
445 # Returns shell true if <flag> is supported by the C++ compiler, else returns shell false.
446 test-flag-CXX() { test-flag-PROG "CXX" c++ "$1"; }
448 # @FUNCTION: test-flag-F77
451 # Returns shell true if <flag> is supported by the Fortran 77 compiler, else returns shell false.
452 test-flag-F77() { test-flag-PROG "F77" f77 "$1"; }
454 # @FUNCTION: test-flag-FC
457 # Returns shell true if <flag> is supported by the Fortran 90 compiler, else returns shell false.
458 test-flag-FC() { test-flag-PROG "FC" f95 "$1"; }
467 [[ -z ${comp} ]] && return 1
470 test-flag-${comp} "${x}" && flags+=( "${x}" )
475 # Just bail if we dont have any flags
476 [[ ${#flags[@]} -gt 0 ]]
479 # @FUNCTION: test-flags-CC
482 # Returns shell true if <flags> are supported by the C compiler, else returns shell false.
483 test-flags-CC() { test-flags-PROG "CC" "$@"; }
485 # @FUNCTION: test-flags-CXX
488 # Returns shell true if <flags> are supported by the C++ compiler, else returns shell false.
489 test-flags-CXX() { test-flags-PROG "CXX" "$@"; }
491 # @FUNCTION: test-flags-F77
494 # Returns shell true if <flags> are supported by the Fortran 77 compiler, else returns shell false.
495 test-flags-F77() { test-flags-PROG "F77" "$@"; }
497 # @FUNCTION: test-flags-FC
500 # Returns shell true if <flags> are supported by the Fortran 90 compiler, else returns shell false.
501 test-flags-FC() { test-flags-PROG "FC" "$@"; }
503 # @FUNCTION: test-flags
506 # Short-hand that should hopefully work for both C and C++ compiler, but
507 # its really only present due to the append-flags() abomination.
508 test-flags() { test-flags-CC "$@"; }
510 # @FUNCTION: test_version_info
513 # Returns shell true if the current C compiler version matches <version>, else returns shell false.
514 # Accepts shell globs.
515 test_version_info() {
516 if [[ $($(tc-getCC) --version 2>&1) == *$1* ]]; then
523 # @FUNCTION: strip-unsupported-flags
525 # Strip {C,CXX,F,FC}FLAGS of any flags not supported by the active toolchain.
526 strip-unsupported-flags() {
527 export CFLAGS=$(test-flags-CC ${CFLAGS})
528 export CXXFLAGS=$(test-flags-CXX ${CXXFLAGS})
529 export FFLAGS=$(test-flags-F77 ${FFLAGS})
530 export FCFLAGS=$(test-flags-FC ${FCFLAGS})
533 # @FUNCTION: get-flag
536 # Find and echo the value for a particular flag. Accepts shell globs.
538 local f var findflag="$1"
540 # this code looks a little flaky but seems to work for
541 # everything we want ...
542 # for example, if CFLAGS="-march=i686":
543 # `get-flag -march` == "-march=i686"
544 # `get-flag march` == "i686"
545 for var in $(all-flag-vars) ; do
546 for f in ${!var} ; do
547 if [ "${f/${findflag}}" != "${f}" ] ; then
548 printf "%s\n" "${f/-${findflag}=}"
558 # This doesn't test if the flag is accepted, it tests if the flag actually
559 # WORKS. Non-multilib gcc will take both -m32 and -m64. If the flag works
560 # return code is 0, else the return code is 1.
562 eqawarn "${FUNCNAME}: don't use this anymore"
564 # this doesnt test if the flag is accepted, it tests if the flag
565 # actually -WORKS-. non-multilib gcc will take both -m32 and -m64!
566 # please dont replace this function with test_flag in some future
569 local temp="$(emktemp)"
570 echo "int main() { return(0); }" > "${temp}".c
572 ${MY_CC/ .*/} -m64 -o "$(emktemp)" "${temp}".c > /dev/null 2>&1
575 [[ ${ret} != 1 ]] && return 0
580 die "${FUNCNAME}: don't use this anymore"
583 # @FUNCTION: replace-sparc64-flags
585 # Sets mcpu to v8 and uses the original value as mtune if none specified.
586 replace-sparc64-flags() {
587 local SPARC64_CPUS="ultrasparc3 ultrasparc v9"
589 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then
590 for x in ${SPARC64_CPUS}; do
591 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
594 for x in ${SPARC64_CPUS}; do
595 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
599 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then
600 for x in ${SPARC64_CPUS}; do
601 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
604 for x in ${SPARC64_CPUS}; do
605 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
609 export CFLAGS CXXFLAGS
612 # @FUNCTION: append-libs
615 # Add extra <libs> to the current LIBS. All arguments should be prefixed with
616 # either -l or -L. For compatibility, if arguments are not prefixed as
617 # options, they are given a -l prefix automatically.
619 [[ $# -eq 0 ]] && return 0
622 if [[ -z "${flag// }" ]]; then
623 eqawarn "Appending an empty argument to LIBS is invalid! Skipping."
628 export LIBS="${LIBS} ${flag}"
631 eqawarn "Appending non-library to LIBS (${flag}); Other linker flags should be passed via LDFLAGS"
632 export LIBS="${LIBS} ${flag}"
635 export LIBS="${LIBS} -l${flag}"
642 # @FUNCTION: raw-ldflags
645 # Turn C style ldflags (-Wl,-foo) into straight ldflags - the results
646 # are suitable for passing directly to 'ld'; note LDFLAGS is usually passed
647 # to gcc where it needs the '-Wl,'.
649 # If no flags are specified, then default to ${LDFLAGS}.
652 [[ -z ${input} ]] && input=${LDFLAGS}
654 for x in ${input} ; do
658 set -- "$@" ${x//,/ }
660 *) # Assume it's a compiler driver flag, so throw it away #441808
667 # @FUNCTION: no-as-needed
668 # @RETURN: Flag to disable asneeded behavior for use with append-ldflags.
670 case $($(tc-getLD) -v 2>&1 </dev/null) in
672 echo "-Wl,--no-as-needed" ;;