1 # Copyright 1999-2017 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: flag-o-matic.eclass
7 # @BLURB: common functions to manipulate and query toolchain flags
9 # This eclass contains a suite of functions to help developers sanely
10 # and safely manage toolchain flags in their builds.
12 if [[ -z ${_FLAG_O_MATIC_ECLASS} ]]; then
13 _FLAG_O_MATIC_ECLASS=1
15 inherit eutils toolchain-funcs multilib
17 # Return all the flag variables that our high level funcs operate on.
19 echo {C,CPP,CXX,CCAS,F,FC,LD}FLAGS
22 # {C,CPP,CXX,CCAS,F,FC,LD}FLAGS that we allow in strip-flags
23 # Note: shell globs and character lists are allowed
24 setup-allowed-flags() {
26 -pipe -O '-O[12sg]' -mcpu -march -mtune
27 '-fstack-protector*' '-fsanitize*' '-fstack-check*' -fno-stack-check
28 -fbounds-check -fbounds-checking -fno-strict-overflow
29 -fno-PIE -fno-pie -nopie -no-pie -fno-unit-at-a-time
30 -g '-g[0-9]' -ggdb '-ggdb[0-9]' '-gdwarf-*' gstabs -gstabs+
31 -fno-ident -fpermissive -frecord-gcc-switches
32 '-fdiagnostics*' '-fplugin*'
35 # CPPFLAGS and LDFLAGS
39 # allow a bunch of flags that negate features / control ABI
41 '-fno-stack-protector*' '-fabi-version=*'
42 -fno-strict-aliasing -fno-bounds-check -fno-bounds-checking -fstrict-overflow
43 -fno-omit-frame-pointer '-fno-builtin*'
46 -mregparm -mno-app-regs -mapp-regs -mno-mmx -mno-sse
47 -mno-sse2 -mno-sse3 -mno-ssse3 -mno-sse4 -mno-sse4.1 -mno-sse4.2
48 -mno-avx -mno-aes -mno-pclmul -mno-sse4a -mno-3dnow -mno-popcnt
49 -mno-abm -mips1 -mips2 -mips3 -mips4 -mips32 -mips64 -mips16 -mplt
50 -msoft-float -mno-soft-float -mhard-float -mno-hard-float -mfpu
51 -mieee -mieee-with-inexact -mschedule -mfloat-gprs -mspe -mno-spe
52 -mtls-direct-seg-refs -mno-tls-direct-seg-refs -mflat -mno-flat
53 -mno-faster-structs -mfaster-structs -m32 -m64 -mx32 -mabi
54 -mlittle-endian -mbig-endian -EL -EB -fPIC -mlive-g0 -mcmodel
55 -mstack-bias -mno-stack-bias -msecure-plt '-m*-toc' -mfloat-abi
56 -mfix-r10000 -mno-fix-r10000 -mthumb -marm
59 -mno-fma4 -mno-movbe -mno-xop -mno-lwp
61 -mno-fsgsbase -mno-rdrnd -mno-f16c -mno-bmi -mno-tbm
63 -mno-avx2 -mno-bmi2 -mno-fma -mno-lzcnt
65 -mno-fxsr -mno-hle -mno-rtm -mno-xsave -mno-xsaveopt
67 -mno-avx512cd -mno-avx512er -mno-avx512f -mno-avx512pf -mno-sha
71 # inverted filters for hardened compiler. This is trying to unpick
72 # the hardened compiler defaults.
77 # Ideally we should only concern ourselves with PIE flags,
78 # not -fPIC or -fpic, but too many places filter -fPIC without
79 # thinking about -fPIE.
80 -fPIC|-fpic|-fPIE|-fpie|-Wl,pie|-pie)
81 gcc-specs-pie || continue
82 if ! is-flagq -nopie && ! is-flagq -no-pie ; then
83 # Support older Gentoo form first (-nopie) before falling
84 # back to the official gcc-6+ form (-no-pie).
85 if test-flags -nopie >/dev/null ; then
93 gcc-specs-ssp || continue
94 is-flagq -fno-stack-protector || append-flags $(test-flags -fno-stack-protector);;
95 -fstack-protector-all)
96 gcc-specs-ssp-to-all || continue
97 is-flagq -fno-stack-protector-all || append-flags $(test-flags -fno-stack-protector-all);;
99 gcc-specs-nostrict || continue
100 is-flagq -fstrict-overflow || append-flags $(test-flags -fstrict-overflow);;
105 # Remove occurrences of strings from variable given in $1
106 # Strings removed are matched as globs, so for example
107 # '-O*' would remove -O1, -O2 etc.
109 local f x var=$1 new=()
112 for f in ${!var} ; do
114 # Note this should work with globs like -O*
115 [[ ${f} == ${x} ]] && continue 2
119 export ${var}="${new[*]}"
122 # @FUNCTION: filter-flags
125 # Remove particular <flags> from {C,CPP,CXX,CCAS,F,FC,LD}FLAGS. Accepts shell globs.
127 _filter-hardened "$@"
129 for v in $(all-flag-vars) ; do
130 _filter-var ${v} "$@"
135 # @FUNCTION: filter-lfs-flags
137 # Remove flags that enable Large File Support.
139 [[ $# -ne 0 ]] && die "filter-lfs-flags takes no arguments"
140 # http://www.gnu.org/s/libc/manual/html_node/Feature-Test-Macros.html
141 # _LARGEFILE_SOURCE: enable support for new LFS funcs (ftello/etc...)
142 # _LARGEFILE64_SOURCE: enable support for 64bit variants (off64_t/fseeko64/etc...)
143 # _FILE_OFFSET_BITS: default to 64bit variants (off_t is defined as off64_t)
144 filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
147 # @FUNCTION: filter-ldflags
150 # Remove particular <flags> from LDFLAGS. Accepts shell globs.
152 _filter-var LDFLAGS "$@"
156 # @FUNCTION: append-cppflags
159 # Add extra <flags> to the current CPPFLAGS.
161 [[ $# -eq 0 ]] && return 0
162 export CPPFLAGS+=" $*"
166 # @FUNCTION: append-cflags
169 # Add extra <flags> to the current CFLAGS. If a flag might not be supported
170 # with different compilers (or versions), then use test-flags-CC like so:
172 # append-cflags $(test-flags-CC -funky-flag)
175 [[ $# -eq 0 ]] && return 0
176 # Do not do automatic flag testing ourselves. #417047
181 # @FUNCTION: append-cxxflags
184 # Add extra <flags> to the current CXXFLAGS. If a flag might not be supported
185 # with different compilers (or versions), then use test-flags-CXX like so:
187 # append-cxxflags $(test-flags-CXX -funky-flag)
190 [[ $# -eq 0 ]] && return 0
191 # Do not do automatic flag testing ourselves. #417047
192 export CXXFLAGS+=" $*"
196 # @FUNCTION: append-fflags
199 # Add extra <flags> to the current {F,FC}FLAGS. If a flag might not be supported
200 # with different compilers (or versions), then use test-flags-F77 like so:
202 # append-fflags $(test-flags-F77 -funky-flag)
205 [[ $# -eq 0 ]] && return 0
206 # Do not do automatic flag testing ourselves. #417047
208 export FCFLAGS+=" $*"
212 # @FUNCTION: append-lfs-flags
214 # Add flags that enable Large File Support.
216 [[ $# -ne 0 ]] && die "append-lfs-flags takes no arguments"
217 # see comments in filter-lfs-flags func for meaning of these
218 append-cppflags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
221 # @FUNCTION: append-ldflags
224 # Add extra <flags> to the current LDFLAGS.
226 [[ $# -eq 0 ]] && return 0
229 [[ ${flag} == -l* ]] && \
230 eqawarn "Appending a library link instruction (${flag}); libraries to link to should not be passed through LDFLAGS"
233 export LDFLAGS="${LDFLAGS} $*"
237 # @FUNCTION: append-flags
240 # Add extra <flags> to your current {C,CXX,F,FC}FLAGS.
242 [[ $# -eq 0 ]] && return 0
244 *' '-[DIU]*) eqawarn 'please use append-cppflags for preprocessor flags' ;;
246 *' '-Wl,*) eqawarn 'please use append-ldflags for linker flags' ;;
254 # @FUNCTION: replace-flags
255 # @USAGE: <old> <new>
257 # Replace the <old> flag with <new>. Accepts shell globs for <old>.
259 [[ $# != 2 ]] && die "Usage: replace-flags <old flag> <new flag>"
262 for var in $(all-flag-vars) ; do
263 # Looping over the flags instead of using a global
264 # substitution ensures that we're working with flag atoms.
265 # Otherwise globs like -O* have the potential to wipe out the
268 for f in ${!var} ; do
269 # Note this should work with globs like -O*
270 [[ ${f} == ${1} ]] && f=${2}
273 export ${var}="${new[*]}"
279 # @FUNCTION: replace-cpu-flags
280 # @USAGE: <old> <new>
282 # Replace cpu flags (like -march/-mcpu/-mtune) that select the <old> cpu
283 # with flags that select the <new> cpu. Accepts shell globs for <old>.
284 replace-cpu-flags() {
285 local newcpu="$#" ; newcpu="${!newcpu}"
286 while [ $# -gt 1 ] ; do
287 # quote to make sure that no globbing is done (particularly on
288 # ${oldcpu}) prior to calling replace-flags
289 replace-flags "-march=${1}" "-march=${newcpu}"
290 replace-flags "-mcpu=${1}" "-mcpu=${newcpu}"
291 replace-flags "-mtune=${1}" "-mtune=${newcpu}"
299 for x in ${!var} ; do
300 [[ ${x} == $2 ]] && return 0
305 # @FUNCTION: is-flagq
308 # Returns shell true if <flag> is in {C,CXX,F,FC}FLAGS, else returns shell false. Accepts shell globs.
310 [[ -n $2 ]] && die "Usage: is-flag <flag>"
313 for var in $(all-flag-vars) ; do
314 _is_flagq ${var} "$1" && return 0
322 # Echo's "true" if flag is set in {C,CXX,F,FC}FLAGS. Accepts shell globs.
324 is-flagq "$@" && echo true
327 # @FUNCTION: is-ldflagq
330 # Returns shell true if <flag> is in LDFLAGS, else returns shell false. Accepts shell globs.
332 [[ -n $2 ]] && die "Usage: is-ldflag <flag>"
336 # @FUNCTION: is-ldflag
339 # Echo's "true" if flag is set in LDFLAGS. Accepts shell globs.
341 is-ldflagq "$@" && echo true
344 # @FUNCTION: filter-mfpmath
345 # @USAGE: <math types>
347 # Remove specified math types from the fpmath flag. For example, if the user
348 # has -mfpmath=sse,386, running `filter-mfpmath sse` will leave the user with
351 local orig_mfpmath new_math prune_math
353 # save the original -mfpmath flag
354 orig_mfpmath=$(get-flag -mfpmath)
355 # get the value of the current -mfpmath flag
356 new_math=$(get-flag mfpmath)
357 # convert "both" to something we can filter
358 new_math=${new_math/both/387,sse}
359 new_math=" ${new_math//[,+]/ } "
360 # figure out which math values are to be removed
362 for prune_math in "$@" ; do
363 new_math=${new_math/ ${prune_math} / }
365 new_math=$(echo ${new_math})
366 new_math=${new_math// /,}
368 if [[ -z ${new_math} ]] ; then
369 # if we're removing all user specified math values are
370 # slated for removal, then we just filter the flag
371 filter-flags ${orig_mfpmath}
373 # if we only want to filter some of the user specified
374 # math values, then we replace the current flag
375 replace-flags ${orig_mfpmath} -mfpmath=${new_math}
380 # @FUNCTION: strip-flags
382 # Strip *FLAGS of everything except known good/safe flags. This runs over all
383 # flags returned by all_flag_vars().
390 set -f # disable pathname expansion
392 for var in $(all-flag-vars) ; do
395 for x in ${!var} ; do
397 for y in "${ALLOWED_FLAGS[@]}" ; do
398 if [[ -z ${flag%%${y}} ]] ; then
405 # In case we filtered out all optimization flags fallback to -O2
406 if _is_flagq ${var} "-O*" && ! _is_flagq new "-O*" ; then
410 if [[ ${!var} != "${new[*]}" ]] ; then
411 einfo "strip-flags: ${var}: changed '${!var}' to '${new[*]}'"
413 export ${var}="${new[*]}"
416 set +f # re-enable pathname expansion
426 [[ -z ${comp} || -z ${flag} ]] && return 1
430 # Clang will warn about unknown gcc flags but exit 0.
431 # Need -Werror to force it to exit non-zero.
433 # Use -c so we can test the assembler as well.
436 if "${cmdline[@]}" -x${lang} - </dev/null &>/dev/null ; then
437 cmdline+=( "${flag}" -x${lang} - )
439 # XXX: what's the purpose of this? does it even work with
441 cmdline+=( "${flag}" -c -o /dev/null /dev/null )
444 if ! "${cmdline[@]}" </dev/null &>/dev/null; then
445 # -Werror makes clang bail out on unused arguments as well;
446 # try to add -Qunused-arguments to work-around that
447 # other compilers don't support it but then, it's failure like
449 cmdline+=( -Qunused-arguments )
450 "${cmdline[@]}" </dev/null &>/dev/null
454 # @FUNCTION: test-flag-CC
457 # Returns shell true if <flag> is supported by the C compiler, else returns shell false.
458 test-flag-CC() { test-flag-PROG "CC" c "$1"; }
460 # @FUNCTION: test-flag-CXX
463 # Returns shell true if <flag> is supported by the C++ compiler, else returns shell false.
464 test-flag-CXX() { test-flag-PROG "CXX" c++ "$1"; }
466 # @FUNCTION: test-flag-F77
469 # Returns shell true if <flag> is supported by the Fortran 77 compiler, else returns shell false.
470 test-flag-F77() { test-flag-PROG "F77" f77 "$1"; }
472 # @FUNCTION: test-flag-FC
475 # Returns shell true if <flag> is supported by the Fortran 90 compiler, else returns shell false.
476 test-flag-FC() { test-flag-PROG "FC" f95 "$1"; }
485 [[ -z ${comp} ]] && return 1
488 test-flag-${comp} "${x}" && flags+=( "${x}" )
493 # Just bail if we dont have any flags
494 [[ ${#flags[@]} -gt 0 ]]
497 # @FUNCTION: test-flags-CC
500 # Returns shell true if <flags> are supported by the C compiler, else returns shell false.
501 test-flags-CC() { test-flags-PROG "CC" "$@"; }
503 # @FUNCTION: test-flags-CXX
506 # Returns shell true if <flags> are supported by the C++ compiler, else returns shell false.
507 test-flags-CXX() { test-flags-PROG "CXX" "$@"; }
509 # @FUNCTION: test-flags-F77
512 # Returns shell true if <flags> are supported by the Fortran 77 compiler, else returns shell false.
513 test-flags-F77() { test-flags-PROG "F77" "$@"; }
515 # @FUNCTION: test-flags-FC
518 # Returns shell true if <flags> are supported by the Fortran 90 compiler, else returns shell false.
519 test-flags-FC() { test-flags-PROG "FC" "$@"; }
521 # @FUNCTION: test-flags
524 # Short-hand that should hopefully work for both C and C++ compiler, but
525 # its really only present due to the append-flags() abomination.
526 test-flags() { test-flags-CC "$@"; }
528 # @FUNCTION: test_version_info
531 # Returns shell true if the current C compiler version matches <version>, else returns shell false.
532 # Accepts shell globs.
533 test_version_info() {
534 if [[ $($(tc-getCC) --version 2>&1) == *$1* ]]; then
541 # @FUNCTION: strip-unsupported-flags
543 # Strip {C,CXX,F,FC}FLAGS of any flags not supported by the active toolchain.
544 strip-unsupported-flags() {
545 export CFLAGS=$(test-flags-CC ${CFLAGS})
546 export CXXFLAGS=$(test-flags-CXX ${CXXFLAGS})
547 export FFLAGS=$(test-flags-F77 ${FFLAGS})
548 export FCFLAGS=$(test-flags-FC ${FCFLAGS})
549 # note: this does not verify the linker flags but it is enough
550 # to strip invalid C flags which are much more likely, #621274
551 export LDFLAGS=$(test-flags-CC ${LDFLAGS})
554 # @FUNCTION: get-flag
557 # Find and echo the value for a particular flag. Accepts shell globs.
559 local f var findflag="$1"
561 # this code looks a little flaky but seems to work for
562 # everything we want ...
563 # for example, if CFLAGS="-march=i686":
564 # `get-flag -march` == "-march=i686"
565 # `get-flag march` == "i686"
566 for var in $(all-flag-vars) ; do
567 for f in ${!var} ; do
568 if [ "${f/${findflag}}" != "${f}" ] ; then
569 printf "%s\n" "${f/-${findflag}=}"
578 die "${FUNCNAME}: don't use this anymore"
582 die "${FUNCNAME}: don't use this anymore"
585 # @FUNCTION: replace-sparc64-flags
587 # Sets mcpu to v8 and uses the original value as mtune if none specified.
588 replace-sparc64-flags() {
589 local SPARC64_CPUS="ultrasparc3 ultrasparc v9"
591 if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then
592 for x in ${SPARC64_CPUS}; do
593 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
596 for x in ${SPARC64_CPUS}; do
597 CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
601 if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then
602 for x in ${SPARC64_CPUS}; do
603 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
606 for x in ${SPARC64_CPUS}; do
607 CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
611 export CFLAGS CXXFLAGS
614 # @FUNCTION: append-libs
617 # Add extra <libs> to the current LIBS. All arguments should be prefixed with
618 # either -l or -L. For compatibility, if arguments are not prefixed as
619 # options, they are given a -l prefix automatically.
621 [[ $# -eq 0 ]] && return 0
624 if [[ -z "${flag// }" ]]; then
625 eqawarn "Appending an empty argument to LIBS is invalid! Skipping."
630 export LIBS="${LIBS} ${flag}"
633 eqawarn "Appending non-library to LIBS (${flag}); Other linker flags should be passed via LDFLAGS"
634 export LIBS="${LIBS} ${flag}"
637 export LIBS="${LIBS} -l${flag}"
644 # @FUNCTION: raw-ldflags
647 # Turn C style ldflags (-Wl,-foo) into straight ldflags - the results
648 # are suitable for passing directly to 'ld'; note LDFLAGS is usually passed
649 # to gcc where it needs the '-Wl,'.
651 # If no flags are specified, then default to ${LDFLAGS}.
654 [[ -z ${input} ]] && input=${LDFLAGS}
656 for x in ${input} ; do
660 set -- "$@" ${x//,/ }
662 *) # Assume it's a compiler driver flag, so throw it away #441808
669 # @FUNCTION: no-as-needed
670 # @RETURN: Flag to disable asneeded behavior for use with append-ldflags.
672 case $($(tc-getLD) -v 2>&1 </dev/null) in
674 echo "-Wl,--no-as-needed" ;;