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 -no-pie -fno-unit-at-a-time
31 -g '-g[0-9]' -ggdb '-ggdb[0-9]' '-gdwarf-*' gstabs -gstabs+
32 -fno-ident -fpermissive -frecord-gcc-switches
33 '-fdiagnostics*' '-fplugin*'
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 -mthumb -marm
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 if ! is-flagq -nopie && ! is-flagq -no-pie ; then
84 # Support older Gentoo form first (-nopie) before falling
85 # back to the official gcc-6+ form (-no-pie).
86 if test-flags -nopie >/dev/null ; then
94 gcc-specs-ssp || continue
95 is-flagq -fno-stack-protector || append-flags $(test-flags -fno-stack-protector);;
96 -fstack-protector-all)
97 gcc-specs-ssp-to-all || continue
98 is-flagq -fno-stack-protector-all || append-flags $(test-flags -fno-stack-protector-all);;
100 gcc-specs-nostrict || continue
101 is-flagq -fstrict-overflow || append-flags $(test-flags -fstrict-overflow);;
106 # Remove occurrences of strings from variable given in $1
107 # Strings removed are matched as globs, so for example
108 # '-O*' would remove -O1, -O2 etc.
110 local f x var=$1 new=()
113 for f in ${!var} ; do
115 # Note this should work with globs like -O*
116 [[ ${f} == ${x} ]] && continue 2
120 eval export ${var}=\""${new[*]}"\"
123 # @FUNCTION: filter-flags
126 # Remove particular <flags> from {C,CPP,CXX,CCAS,F,FC,LD}FLAGS. Accepts shell globs.
128 _filter-hardened "$@"
130 for v in $(all-flag-vars) ; do
131 _filter-var ${v} "$@"
136 # @FUNCTION: filter-lfs-flags
138 # Remove flags that enable Large File Support.
140 [[ $# -ne 0 ]] && die "filter-lfs-flags takes no arguments"
141 # http://www.gnu.org/s/libc/manual/html_node/Feature-Test-Macros.html
142 # _LARGEFILE_SOURCE: enable support for new LFS funcs (ftello/etc...)
143 # _LARGEFILE64_SOURCE: enable support for 64bit variants (off64_t/fseeko64/etc...)
144 # _FILE_OFFSET_BITS: default to 64bit variants (off_t is defined as off64_t)
145 filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
148 # @FUNCTION: filter-ldflags
151 # Remove particular <flags> from LDFLAGS. Accepts shell globs.
153 _filter-var LDFLAGS "$@"
157 # @FUNCTION: append-cppflags
160 # Add extra <flags> to the current CPPFLAGS.
162 [[ $# -eq 0 ]] && return 0
163 export CPPFLAGS+=" $*"
167 # @FUNCTION: append-cflags
170 # Add extra <flags> to the current CFLAGS. If a flag might not be supported
171 # with different compilers (or versions), then use test-flags-CC like so:
173 # append-cflags $(test-flags-CC -funky-flag)
176 [[ $# -eq 0 ]] && return 0
177 # Do not do automatic flag testing ourselves. #417047
182 # @FUNCTION: append-cxxflags
185 # Add extra <flags> to the current CXXFLAGS. If a flag might not be supported
186 # with different compilers (or versions), then use test-flags-CXX like so:
188 # append-cxxflags $(test-flags-CXX -funky-flag)
191 [[ $# -eq 0 ]] && return 0
192 # Do not do automatic flag testing ourselves. #417047
193 export CXXFLAGS+=" $*"
197 # @FUNCTION: append-fflags
200 # Add extra <flags> to the current {F,FC}FLAGS. If a flag might not be supported
201 # with different compilers (or versions), then use test-flags-F77 like so:
203 # append-fflags $(test-flags-F77 -funky-flag)
206 [[ $# -eq 0 ]] && return 0
207 # Do not do automatic flag testing ourselves. #417047
209 export FCFLAGS+=" $*"
213 # @FUNCTION: append-lfs-flags
215 # Add flags that enable Large File Support.
217 [[ $# -ne 0 ]] && die "append-lfs-flags takes no arguments"
218 # see comments in filter-lfs-flags func for meaning of these
219 append-cppflags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
222 # @FUNCTION: append-ldflags
225 # Add extra <flags> to the current LDFLAGS.
227 [[ $# -eq 0 ]] && return 0
230 [[ ${flag} == -l* ]] && \
231 eqawarn "Appending a library link instruction (${flag}); libraries to link to should not be passed through LDFLAGS"
234 export LDFLAGS="${LDFLAGS} $*"
238 # @FUNCTION: append-flags
241 # Add extra <flags> to your current {C,CXX,F,FC}FLAGS.
243 [[ $# -eq 0 ]] && return 0
245 *' '-[DIU]*) eqawarn 'please use append-cppflags for preprocessor flags' ;;
247 *' '-Wl,*) eqawarn 'please use append-ldflags for linker flags' ;;
255 # @FUNCTION: replace-flags
256 # @USAGE: <old> <new>
258 # Replace the <old> flag with <new>. Accepts shell globs for <old>.
260 [[ $# != 2 ]] && die "Usage: replace-flags <old flag> <new flag>"
263 for var in $(all-flag-vars) ; do
264 # Looping over the flags instead of using a global
265 # substitution ensures that we're working with flag atoms.
266 # Otherwise globs like -O* have the potential to wipe out the
269 for f in ${!var} ; do
270 # Note this should work with globs like -O*
271 [[ ${f} == ${1} ]] && f=${2}
274 eval export ${var}=\""${new[*]}"\"
280 # @FUNCTION: replace-cpu-flags
281 # @USAGE: <old> <new>
283 # Replace cpu flags (like -march/-mcpu/-mtune) that select the <old> cpu
284 # with flags that select the <new> cpu. Accepts shell globs for <old>.
285 replace-cpu-flags() {
286 local newcpu="$#" ; newcpu="${!newcpu}"
287 while [ $# -gt 1 ] ; do
288 # quote to make sure that no globbing is done (particularly on
289 # ${oldcpu}) prior to calling replace-flags
290 replace-flags "-march=${1}" "-march=${newcpu}"
291 replace-flags "-mcpu=${1}" "-mcpu=${newcpu}"
292 replace-flags "-mtune=${1}" "-mtune=${newcpu}"
300 eval var=\""\${$1[*]}"\"
302 [[ ${x} == $2 ]] && return 0
307 # @FUNCTION: is-flagq
310 # Returns shell true if <flag> is in {C,CXX,F,FC}FLAGS, else returns shell false. Accepts shell globs.
312 [[ -n $2 ]] && die "Usage: is-flag <flag>"
315 for var in $(all-flag-vars) ; do
316 _is_flagq ${var} "$1" && return 0
324 # Echo's "true" if flag is set in {C,CXX,F,FC}FLAGS. Accepts shell globs.
326 is-flagq "$@" && echo true
329 # @FUNCTION: is-ldflagq
332 # Returns shell true if <flag> is in LDFLAGS, else returns shell false. Accepts shell globs.
334 [[ -n $2 ]] && die "Usage: is-ldflag <flag>"
338 # @FUNCTION: is-ldflag
341 # Echo's "true" if flag is set in LDFLAGS. Accepts shell globs.
343 is-ldflagq "$@" && echo true
346 # @FUNCTION: filter-mfpmath
347 # @USAGE: <math types>
349 # Remove specified math types from the fpmath flag. For example, if the user
350 # has -mfpmath=sse,386, running `filter-mfpmath sse` will leave the user with
353 local orig_mfpmath new_math prune_math
355 # save the original -mfpmath flag
356 orig_mfpmath=$(get-flag -mfpmath)
357 # get the value of the current -mfpmath flag
358 new_math=$(get-flag mfpmath)
359 # convert "both" to something we can filter
360 new_math=${new_math/both/387,sse}
361 new_math=" ${new_math//[,+]/ } "
362 # figure out which math values are to be removed
364 for prune_math in "$@" ; do
365 new_math=${new_math/ ${prune_math} / }
367 new_math=$(echo ${new_math})
368 new_math=${new_math// /,}
370 if [[ -z ${new_math} ]] ; then
371 # if we're removing all user specified math values are
372 # slated for removal, then we just filter the flag
373 filter-flags ${orig_mfpmath}
375 # if we only want to filter some of the user specified
376 # math values, then we replace the current flag
377 replace-flags ${orig_mfpmath} -mfpmath=${new_math}
382 # @FUNCTION: strip-flags
384 # Strip *FLAGS of everything except known good/safe flags. This runs over all
385 # flags returned by all_flag_vars().
392 set -f # disable pathname expansion
394 for var in $(all-flag-vars) ; do
397 for x in ${!var} ; do
399 for y in "${ALLOWED_FLAGS[@]}" ; do
400 if [[ -z ${flag%%${y}} ]] ; then
407 # In case we filtered out all optimization flags fallback to -O2
408 if _is_flagq ${var} "-O*" && ! _is_flagq new "-O*" ; then
412 if [[ ${!var} != "${new[*]}" ]] ; then
413 einfo "strip-flags: ${var}: changed '${!var}' to '${new[*]}'"
415 eval export ${var}=\""${new[*]}"\"
418 set +f # re-enable pathname expansion
428 [[ -z ${comp} || -z ${flag} ]] && return 1
432 # Clang will warn about unknown gcc flags but exit 0.
433 # Need -Werror to force it to exit non-zero.
435 # Use -c so we can test the assembler as well.
438 if "${cmdline[@]}" -x${lang} - </dev/null >/dev/null 2>&1 ; then
439 "${cmdline[@]}" "${flag}" -x${lang} - </dev/null >/dev/null 2>&1
441 "${cmdline[@]}" "${flag}" -c -o /dev/null /dev/null >/dev/null 2>&1
445 # @FUNCTION: test-flag-CC
448 # Returns shell true if <flag> is supported by the C compiler, else returns shell false.
449 test-flag-CC() { test-flag-PROG "CC" c "$1"; }
451 # @FUNCTION: test-flag-CXX
454 # Returns shell true if <flag> is supported by the C++ compiler, else returns shell false.
455 test-flag-CXX() { test-flag-PROG "CXX" c++ "$1"; }
457 # @FUNCTION: test-flag-F77
460 # Returns shell true if <flag> is supported by the Fortran 77 compiler, else returns shell false.
461 test-flag-F77() { test-flag-PROG "F77" f77 "$1"; }
463 # @FUNCTION: test-flag-FC
466 # Returns shell true if <flag> is supported by the Fortran 90 compiler, else returns shell false.
467 test-flag-FC() { test-flag-PROG "FC" f95 "$1"; }
476 [[ -z ${comp} ]] && return 1
479 test-flag-${comp} "${x}" && flags+=( "${x}" )
484 # Just bail if we dont have any flags
485 [[ ${#flags[@]} -gt 0 ]]
488 # @FUNCTION: test-flags-CC
491 # Returns shell true if <flags> are supported by the C compiler, else returns shell false.
492 test-flags-CC() { test-flags-PROG "CC" "$@"; }
494 # @FUNCTION: test-flags-CXX
497 # Returns shell true if <flags> are supported by the C++ compiler, else returns shell false.
498 test-flags-CXX() { test-flags-PROG "CXX" "$@"; }
500 # @FUNCTION: test-flags-F77
503 # Returns shell true if <flags> are supported by the Fortran 77 compiler, else returns shell false.
504 test-flags-F77() { test-flags-PROG "F77" "$@"; }
506 # @FUNCTION: test-flags-FC
509 # Returns shell true if <flags> are supported by the Fortran 90 compiler, else returns shell false.
510 test-flags-FC() { test-flags-PROG "FC" "$@"; }
512 # @FUNCTION: test-flags
515 # Short-hand that should hopefully work for both C and C++ compiler, but
516 # its really only present due to the append-flags() abomination.
517 test-flags() { test-flags-CC "$@"; }
519 # @FUNCTION: test_version_info
522 # Returns shell true if the current C compiler version matches <version>, else returns shell false.
523 # Accepts shell globs.
524 test_version_info() {
525 if [[ $($(tc-getCC) --version 2>&1) == *$1* ]]; then
532 # @FUNCTION: strip-unsupported-flags
534 # Strip {C,CXX,F,FC}FLAGS of any flags not supported by the active toolchain.
535 strip-unsupported-flags() {
536 export CFLAGS=$(test-flags-CC ${CFLAGS})
537 export CXXFLAGS=$(test-flags-CXX ${CXXFLAGS})
538 export FFLAGS=$(test-flags-F77 ${FFLAGS})
539 export FCFLAGS=$(test-flags-FC ${FCFLAGS})
542 # @FUNCTION: get-flag
545 # Find and echo the value for a particular flag. Accepts shell globs.
547 local f var findflag="$1"
549 # this code looks a little flaky but seems to work for
550 # everything we want ...
551 # for example, if CFLAGS="-march=i686":
552 # `get-flag -march` == "-march=i686"
553 # `get-flag march` == "i686"
554 for var in $(all-flag-vars) ; do
555 for f in ${!var} ; do
556 if [ "${f/${findflag}}" != "${f}" ] ; then
557 printf "%s\n" "${f/-${findflag}=}"
567 # This doesn't test if the flag is accepted, it tests if the flag actually
568 # WORKS. Non-multilib gcc will take both -m32 and -m64. If the flag works
569 # return code is 0, else the return code is 1.
571 eqawarn "${FUNCNAME}: don't use this anymore"
573 # this doesnt test if the flag is accepted, it tests if the flag
574 # actually -WORKS-. non-multilib gcc will take both -m32 and -m64!
575 # please dont replace this function with test_flag in some future
578 local temp="$(emktemp)"
579 echo "int main() { return(0); }" > "${temp}".c
581 ${MY_CC/ .*/} -m64 -o "$(emktemp)" "${temp}".c > /dev/null 2>&1
584 [[ ${ret} != 1 ]] && return 0
589 die "${FUNCNAME}: don't use this anymore"
592 # @FUNCTION: replace-sparc64-flags
594 # Sets mcpu to v8 and uses the original value as mtune if none specified.
595 replace-sparc64-flags() {
596 local SPARC64_CPUS="ultrasparc3 ultrasparc v9"
598 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then
599 for x in ${SPARC64_CPUS}; do
600 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
603 for x in ${SPARC64_CPUS}; do
604 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
608 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then
609 for x in ${SPARC64_CPUS}; do
610 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
613 for x in ${SPARC64_CPUS}; do
614 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
618 export CFLAGS CXXFLAGS
621 # @FUNCTION: append-libs
624 # Add extra <libs> to the current LIBS. All arguments should be prefixed with
625 # either -l or -L. For compatibility, if arguments are not prefixed as
626 # options, they are given a -l prefix automatically.
628 [[ $# -eq 0 ]] && return 0
631 if [[ -z "${flag// }" ]]; then
632 eqawarn "Appending an empty argument to LIBS is invalid! Skipping."
637 export LIBS="${LIBS} ${flag}"
640 eqawarn "Appending non-library to LIBS (${flag}); Other linker flags should be passed via LDFLAGS"
641 export LIBS="${LIBS} ${flag}"
644 export LIBS="${LIBS} -l${flag}"
651 # @FUNCTION: raw-ldflags
654 # Turn C style ldflags (-Wl,-foo) into straight ldflags - the results
655 # are suitable for passing directly to 'ld'; note LDFLAGS is usually passed
656 # to gcc where it needs the '-Wl,'.
658 # If no flags are specified, then default to ${LDFLAGS}.
661 [[ -z ${input} ]] && input=${LDFLAGS}
663 for x in ${input} ; do
667 set -- "$@" ${x//,/ }
669 *) # Assume it's a compiler driver flag, so throw it away #441808
676 # @FUNCTION: no-as-needed
677 # @RETURN: Flag to disable asneeded behavior for use with append-ldflags.
679 case $($(tc-getLD) -v 2>&1 </dev/null) in
681 echo "-Wl,--no-as-needed" ;;