Merge remote-tracking branch 'github/pr/2627'
[gentoo.git] / eclass / flag-o-matic.eclass
1 # Copyright 1999-2014 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: flag-o-matic.eclass
6 # @MAINTAINER:
7 # toolchain@gentoo.org
8 # @BLURB: common functions to manipulate and query toolchain flags
9 # @DESCRIPTION:
10 # This eclass contains a suite of functions to help developers sanely
11 # and safely manage toolchain flags in their builds.
12
13 if [[ -z ${_FLAG_O_MATIC_ECLASS} ]]; then
14 _FLAG_O_MATIC_ECLASS=1
15
16 inherit eutils toolchain-funcs multilib
17
18 # Return all the flag variables that our high level funcs operate on.
19 all-flag-vars() {
20         echo {C,CPP,CXX,CCAS,F,FC,LD}FLAGS
21 }
22
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() {
26         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*'
34                 '-W*' -w
35
36                 # CPPFLAGS and LDFLAGS
37                 '-[DUILR]*' '-Wl,*'
38         )
39
40         # allow a bunch of flags that negate features / control ABI
41         ALLOWED_FLAGS+=(
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*'
45         )
46         ALLOWED_FLAGS+=(
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
58
59                 # gcc 4.5
60                 -mno-fma4 -mno-movbe -mno-xop -mno-lwp
61                 # gcc 4.6
62                 -mno-fsgsbase -mno-rdrnd -mno-f16c -mno-bmi -mno-tbm
63                 # gcc 4.7
64                 -mno-avx2 -mno-bmi2 -mno-fma -mno-lzcnt
65                 # gcc 4.8
66                 -mno-fxsr -mno-hle -mno-rtm -mno-xsave -mno-xsaveopt
67                 # gcc 4.9
68                 -mno-avx512cd -mno-avx512er -mno-avx512f -mno-avx512pf -mno-sha
69         )
70 }
71
72 # inverted filters for hardened compiler.  This is trying to unpick
73 # the hardened compiler defaults.
74 _filter-hardened() {
75         local f
76         for f in "$@" ; do
77                 case "${f}" in
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
87                                                 append-flags -nopie
88                                         else
89                                                 append-flags -no-pie
90                                         fi
91                                 fi
92                                 ;;
93                         -fstack-protector)
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);;
99                         -fno-strict-overflow)
100                                 gcc-specs-nostrict || continue
101                                 is-flagq -fstrict-overflow || append-flags $(test-flags -fstrict-overflow);;
102                 esac
103         done
104 }
105
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.
109 _filter-var() {
110         local f x var=$1 new=()
111         shift
112
113         for f in ${!var} ; do
114                 for x in "$@" ; do
115                         # Note this should work with globs like -O*
116                         [[ ${f} == ${x} ]] && continue 2
117                 done
118                 new+=( "${f}" )
119         done
120         eval export ${var}=\""${new[*]}"\"
121 }
122
123 # @FUNCTION: filter-flags
124 # @USAGE: <flags>
125 # @DESCRIPTION:
126 # Remove particular <flags> from {C,CPP,CXX,CCAS,F,FC,LD}FLAGS.  Accepts shell globs.
127 filter-flags() {
128         _filter-hardened "$@"
129         local v
130         for v in $(all-flag-vars) ; do
131                 _filter-var ${v} "$@"
132         done
133         return 0
134 }
135
136 # @FUNCTION: filter-lfs-flags
137 # @DESCRIPTION:
138 # Remove flags that enable Large File Support.
139 filter-lfs-flags() {
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
146 }
147
148 # @FUNCTION: filter-ldflags
149 # @USAGE: <flags>
150 # @DESCRIPTION:
151 # Remove particular <flags> from LDFLAGS.  Accepts shell globs.
152 filter-ldflags() {
153         _filter-var LDFLAGS "$@"
154         return 0
155 }
156
157 # @FUNCTION: append-cppflags
158 # @USAGE: <flags>
159 # @DESCRIPTION:
160 # Add extra <flags> to the current CPPFLAGS.
161 append-cppflags() {
162         [[ $# -eq 0 ]] && return 0
163         export CPPFLAGS+=" $*"
164         return 0
165 }
166
167 # @FUNCTION: append-cflags
168 # @USAGE: <flags>
169 # @DESCRIPTION:
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:
172 # @CODE
173 # append-cflags $(test-flags-CC -funky-flag)
174 # @CODE
175 append-cflags() {
176         [[ $# -eq 0 ]] && return 0
177         # Do not do automatic flag testing ourselves. #417047
178         export CFLAGS+=" $*"
179         return 0
180 }
181
182 # @FUNCTION: append-cxxflags
183 # @USAGE: <flags>
184 # @DESCRIPTION:
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:
187 # @CODE
188 # append-cxxflags $(test-flags-CXX -funky-flag)
189 # @CODE
190 append-cxxflags() {
191         [[ $# -eq 0 ]] && return 0
192         # Do not do automatic flag testing ourselves. #417047
193         export CXXFLAGS+=" $*"
194         return 0
195 }
196
197 # @FUNCTION: append-fflags
198 # @USAGE: <flags>
199 # @DESCRIPTION:
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:
202 # @CODE
203 # append-fflags $(test-flags-F77 -funky-flag)
204 # @CODE
205 append-fflags() {
206         [[ $# -eq 0 ]] && return 0
207         # Do not do automatic flag testing ourselves. #417047
208         export FFLAGS+=" $*"
209         export FCFLAGS+=" $*"
210         return 0
211 }
212
213 # @FUNCTION: append-lfs-flags
214 # @DESCRIPTION:
215 # Add flags that enable Large File Support.
216 append-lfs-flags() {
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
220 }
221
222 # @FUNCTION: append-ldflags
223 # @USAGE: <flags>
224 # @DESCRIPTION:
225 # Add extra <flags> to the current LDFLAGS.
226 append-ldflags() {
227         [[ $# -eq 0 ]] && return 0
228         local flag
229         for flag in "$@"; do
230                 [[ ${flag} == -l* ]] && \
231                         eqawarn "Appending a library link instruction (${flag}); libraries to link to should not be passed through LDFLAGS"
232         done
233
234         export LDFLAGS="${LDFLAGS} $*"
235         return 0
236 }
237
238 # @FUNCTION: append-flags
239 # @USAGE: <flags>
240 # @DESCRIPTION:
241 # Add extra <flags> to your current {C,CXX,F,FC}FLAGS.
242 append-flags() {
243         [[ $# -eq 0 ]] && return 0
244         case " $* " in
245         *' '-[DIU]*) eqawarn 'please use append-cppflags for preprocessor flags' ;;
246         *' '-L*|\
247         *' '-Wl,*)  eqawarn 'please use append-ldflags for linker flags' ;;
248         esac
249         append-cflags "$@"
250         append-cxxflags "$@"
251         append-fflags "$@"
252         return 0
253 }
254
255 # @FUNCTION: replace-flags
256 # @USAGE: <old> <new>
257 # @DESCRIPTION:
258 # Replace the <old> flag with <new>.  Accepts shell globs for <old>.
259 replace-flags() {
260         [[ $# != 2 ]] && die "Usage: replace-flags <old flag> <new flag>"
261
262         local f var new
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
267                 # list of flags.
268                 new=()
269                 for f in ${!var} ; do
270                         # Note this should work with globs like -O*
271                         [[ ${f} == ${1} ]] && f=${2}
272                         new+=( "${f}" )
273                 done
274                 eval export ${var}=\""${new[*]}"\"
275         done
276
277         return 0
278 }
279
280 # @FUNCTION: replace-cpu-flags
281 # @USAGE: <old> <new>
282 # @DESCRIPTION:
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}"
293                 shift
294         done
295         return 0
296 }
297
298 _is_flagq() {
299         local x var
300         eval var=\""\${$1[*]}"\"
301         for x in ${var} ; do
302                 [[ ${x} == $2 ]] && return 0
303         done
304         return 1
305 }
306
307 # @FUNCTION: is-flagq
308 # @USAGE: <flag>
309 # @DESCRIPTION:
310 # Returns shell true if <flag> is in {C,CXX,F,FC}FLAGS, else returns shell false.  Accepts shell globs.
311 is-flagq() {
312         [[ -n $2 ]] && die "Usage: is-flag <flag>"
313
314         local var
315         for var in $(all-flag-vars) ; do
316                 _is_flagq ${var} "$1" && return 0
317         done
318         return 1
319 }
320
321 # @FUNCTION: is-flag
322 # @USAGE: <flag>
323 # @DESCRIPTION:
324 # Echo's "true" if flag is set in {C,CXX,F,FC}FLAGS.  Accepts shell globs.
325 is-flag() {
326         is-flagq "$@" && echo true
327 }
328
329 # @FUNCTION: is-ldflagq
330 # @USAGE: <flag>
331 # @DESCRIPTION:
332 # Returns shell true if <flag> is in LDFLAGS, else returns shell false.  Accepts shell globs.
333 is-ldflagq() {
334         [[ -n $2 ]] && die "Usage: is-ldflag <flag>"
335         _is_flagq LDFLAGS $1
336 }
337
338 # @FUNCTION: is-ldflag
339 # @USAGE: <flag>
340 # @DESCRIPTION:
341 # Echo's "true" if flag is set in LDFLAGS.  Accepts shell globs.
342 is-ldflag() {
343         is-ldflagq "$@" && echo true
344 }
345
346 # @FUNCTION: filter-mfpmath
347 # @USAGE: <math types>
348 # @DESCRIPTION:
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
351 # -mfpmath=386.
352 filter-mfpmath() {
353         local orig_mfpmath new_math prune_math
354
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
363         prune_math=""
364         for prune_math in "$@" ; do
365                 new_math=${new_math/ ${prune_math} / }
366         done
367         new_math=$(echo ${new_math})
368         new_math=${new_math// /,}
369
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}
374         else
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}
378         fi
379         return 0
380 }
381
382 # @FUNCTION: strip-flags
383 # @DESCRIPTION:
384 # Strip *FLAGS of everything except known good/safe flags.  This runs over all
385 # flags returned by all_flag_vars().
386 strip-flags() {
387         local x y var
388
389         local ALLOWED_FLAGS
390         setup-allowed-flags
391
392         set -f  # disable pathname expansion
393
394         for var in $(all-flag-vars) ; do
395                 local new=()
396
397                 for x in ${!var} ; do
398                         local flag=${x%%=*}
399                         for y in "${ALLOWED_FLAGS[@]}" ; do
400                                 if [[ -z ${flag%%${y}} ]] ; then
401                                         new+=( "${x}" )
402                                         break
403                                 fi
404                         done
405                 done
406
407                 # In case we filtered out all optimization flags fallback to -O2
408                 if _is_flagq ${var} "-O*" && ! _is_flagq new "-O*" ; then
409                         new+=( -O2 )
410                 fi
411
412                 if [[ ${!var} != "${new[*]}" ]] ; then
413                         einfo "strip-flags: ${var}: changed '${!var}' to '${new[*]}'"
414                 fi
415                 eval export ${var}=\""${new[*]}"\"
416         done
417
418         set +f  # re-enable pathname expansion
419
420         return 0
421 }
422
423 test-flag-PROG() {
424         local comp=$1
425         local lang=$2
426         local flag=$3
427
428         [[ -z ${comp} || -z ${flag} ]] && return 1
429
430         local cmdline=(
431                 $(tc-get${comp})
432                 # Clang will warn about unknown gcc flags but exit 0.
433                 # Need -Werror to force it to exit non-zero.
434                 -Werror
435                 # Use -c so we can test the assembler as well.
436                 -c -o /dev/null
437         )
438         if "${cmdline[@]}" -x${lang} - </dev/null >/dev/null 2>&1 ; then
439                 "${cmdline[@]}" "${flag}" -x${lang} - </dev/null >/dev/null 2>&1
440         else
441                 "${cmdline[@]}" "${flag}" -c -o /dev/null /dev/null >/dev/null 2>&1
442         fi
443 }
444
445 # @FUNCTION: test-flag-CC
446 # @USAGE: <flag>
447 # @DESCRIPTION:
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"; }
450
451 # @FUNCTION: test-flag-CXX
452 # @USAGE: <flag>
453 # @DESCRIPTION:
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"; }
456
457 # @FUNCTION: test-flag-F77
458 # @USAGE: <flag>
459 # @DESCRIPTION:
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"; }
462
463 # @FUNCTION: test-flag-FC
464 # @USAGE: <flag>
465 # @DESCRIPTION:
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"; }
468
469 test-flags-PROG() {
470         local comp=$1
471         local flags=()
472         local x
473
474         shift
475
476         [[ -z ${comp} ]] && return 1
477
478         for x ; do
479                 test-flag-${comp} "${x}" && flags+=( "${x}" )
480         done
481
482         echo "${flags[*]}"
483
484         # Just bail if we dont have any flags
485         [[ ${#flags[@]} -gt 0 ]]
486 }
487
488 # @FUNCTION: test-flags-CC
489 # @USAGE: <flags>
490 # @DESCRIPTION:
491 # Returns shell true if <flags> are supported by the C compiler, else returns shell false.
492 test-flags-CC() { test-flags-PROG "CC" "$@"; }
493
494 # @FUNCTION: test-flags-CXX
495 # @USAGE: <flags>
496 # @DESCRIPTION:
497 # Returns shell true if <flags> are supported by the C++ compiler, else returns shell false.
498 test-flags-CXX() { test-flags-PROG "CXX" "$@"; }
499
500 # @FUNCTION: test-flags-F77
501 # @USAGE: <flags>
502 # @DESCRIPTION:
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" "$@"; }
505
506 # @FUNCTION: test-flags-FC
507 # @USAGE: <flags>
508 # @DESCRIPTION:
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" "$@"; }
511
512 # @FUNCTION: test-flags
513 # @USAGE: <flags>
514 # @DESCRIPTION:
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 "$@"; }
518
519 # @FUNCTION: test_version_info
520 # @USAGE: <version>
521 # @DESCRIPTION:
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
526                 return 0
527         else
528                 return 1
529         fi
530 }
531
532 # @FUNCTION: strip-unsupported-flags
533 # @DESCRIPTION:
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})
540 }
541
542 # @FUNCTION: get-flag
543 # @USAGE: <flag>
544 # @DESCRIPTION:
545 # Find and echo the value for a particular flag.  Accepts shell globs.
546 get-flag() {
547         local f var findflag="$1"
548
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}=}"
558                                 return 0
559                         fi
560                 done
561         done
562         return 1
563 }
564
565 # @FUNCTION: has_m64
566 # @DESCRIPTION:
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.
570 has_m64() {
571         eqawarn "${FUNCNAME}: don't use this anymore"
572
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
576         # clean-up!
577
578         local temp="$(emktemp)"
579         echo "int main() { return(0); }" > "${temp}".c
580         MY_CC=$(tc-getCC)
581         ${MY_CC/ .*/} -m64 -o "$(emktemp)" "${temp}".c > /dev/null 2>&1
582         local ret=$?
583         rm -f "${temp}".c
584         [[ ${ret} != 1 ]] && return 0
585         return 1
586 }
587
588 has_m32() {
589         die "${FUNCNAME}: don't use this anymore"
590 }
591
592 # @FUNCTION: replace-sparc64-flags
593 # @DESCRIPTION:
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"
597
598         if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then
599                 for x in ${SPARC64_CPUS}; do
600                         CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
601                 done
602         else
603                 for x in ${SPARC64_CPUS}; do
604                         CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
605                 done
606         fi
607
608         if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then
609                 for x in ${SPARC64_CPUS}; do
610                         CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
611                 done
612         else
613                 for x in ${SPARC64_CPUS}; do
614                         CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
615                 done
616         fi
617
618         export CFLAGS CXXFLAGS
619 }
620
621 # @FUNCTION: append-libs
622 # @USAGE: <libs>
623 # @DESCRIPTION:
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.
627 append-libs() {
628         [[ $# -eq 0 ]] && return 0
629         local flag
630         for flag in "$@"; do
631                 if [[ -z "${flag// }" ]]; then
632                         eqawarn "Appending an empty argument to LIBS is invalid! Skipping."
633                         continue
634                 fi
635                 case $flag in
636                         -[lL]*)
637                                 export LIBS="${LIBS} ${flag}"
638                                 ;;
639                         -*)
640                                 eqawarn "Appending non-library to LIBS (${flag}); Other linker flags should be passed via LDFLAGS"
641                                 export LIBS="${LIBS} ${flag}"
642                                 ;;
643                         *)
644                                 export LIBS="${LIBS} -l${flag}"
645                 esac
646         done
647
648         return 0
649 }
650
651 # @FUNCTION: raw-ldflags
652 # @USAGE: [flags]
653 # @DESCRIPTION:
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,'.
657 #
658 # If no flags are specified, then default to ${LDFLAGS}.
659 raw-ldflags() {
660         local x input="$@"
661         [[ -z ${input} ]] && input=${LDFLAGS}
662         set --
663         for x in ${input} ; do
664                 case ${x} in
665                 -Wl,*)
666                         x=${x#-Wl,}
667                         set -- "$@" ${x//,/ }
668                         ;;
669                 *)      # Assume it's a compiler driver flag, so throw it away #441808
670                         ;;
671                 esac
672         done
673         echo "$@"
674 }
675
676 # @FUNCTION: no-as-needed
677 # @RETURN: Flag to disable asneeded behavior for use with append-ldflags.
678 no-as-needed() {
679         case $($(tc-getLD) -v 2>&1 </dev/null) in
680                 *GNU*) # GNU ld
681                 echo "-Wl,--no-as-needed" ;;
682         esac
683 }
684
685 fi