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