Merge remote-tracking branch 'remotes/sbraz/pysrt'
[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 -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*'
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
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                                 is-flagq -nopie || append-flags -nopie;;
84                         -fstack-protector)
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);;
90                         -fno-strict-overflow)
91                                 gcc-specs-nostrict || continue
92                                 is-flagq -fstrict-overflow || append-flags $(test-flags -fstrict-overflow);;
93                 esac
94         done
95 }
96
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.
100 _filter-var() {
101         local f x var=$1 new=()
102         shift
103
104         for f in ${!var} ; do
105                 for x in "$@" ; do
106                         # Note this should work with globs like -O*
107                         [[ ${f} == ${x} ]] && continue 2
108                 done
109                 new+=( "${f}" )
110         done
111         eval export ${var}=\""${new[*]}"\"
112 }
113
114 # @FUNCTION: filter-flags
115 # @USAGE: <flags>
116 # @DESCRIPTION:
117 # Remove particular <flags> from {C,CPP,CXX,CCAS,F,FC,LD}FLAGS.  Accepts shell globs.
118 filter-flags() {
119         _filter-hardened "$@"
120         local v
121         for v in $(all-flag-vars) ; do
122                 _filter-var ${v} "$@"
123         done
124         return 0
125 }
126
127 # @FUNCTION: filter-lfs-flags
128 # @DESCRIPTION:
129 # Remove flags that enable Large File Support.
130 filter-lfs-flags() {
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
137 }
138
139 # @FUNCTION: filter-ldflags
140 # @USAGE: <flags>
141 # @DESCRIPTION:
142 # Remove particular <flags> from LDFLAGS.  Accepts shell globs.
143 filter-ldflags() {
144         _filter-var LDFLAGS "$@"
145         return 0
146 }
147
148 # @FUNCTION: append-cppflags
149 # @USAGE: <flags>
150 # @DESCRIPTION:
151 # Add extra <flags> to the current CPPFLAGS.
152 append-cppflags() {
153         [[ $# -eq 0 ]] && return 0
154         export CPPFLAGS+=" $*"
155         return 0
156 }
157
158 # @FUNCTION: append-cflags
159 # @USAGE: <flags>
160 # @DESCRIPTION:
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:
163 # @CODE
164 # append-cflags $(test-flags-CC -funky-flag)
165 # @CODE
166 append-cflags() {
167         [[ $# -eq 0 ]] && return 0
168         # Do not do automatic flag testing ourselves. #417047
169         export CFLAGS+=" $*"
170         return 0
171 }
172
173 # @FUNCTION: append-cxxflags
174 # @USAGE: <flags>
175 # @DESCRIPTION:
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:
178 # @CODE
179 # append-cxxflags $(test-flags-CXX -funky-flag)
180 # @CODE
181 append-cxxflags() {
182         [[ $# -eq 0 ]] && return 0
183         # Do not do automatic flag testing ourselves. #417047
184         export CXXFLAGS+=" $*"
185         return 0
186 }
187
188 # @FUNCTION: append-fflags
189 # @USAGE: <flags>
190 # @DESCRIPTION:
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:
193 # @CODE
194 # append-fflags $(test-flags-F77 -funky-flag)
195 # @CODE
196 append-fflags() {
197         [[ $# -eq 0 ]] && return 0
198         # Do not do automatic flag testing ourselves. #417047
199         export FFLAGS+=" $*"
200         export FCFLAGS+=" $*"
201         return 0
202 }
203
204 # @FUNCTION: append-lfs-flags
205 # @DESCRIPTION:
206 # Add flags that enable Large File Support.
207 append-lfs-flags() {
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
211 }
212
213 # @FUNCTION: append-ldflags
214 # @USAGE: <flags>
215 # @DESCRIPTION:
216 # Add extra <flags> to the current LDFLAGS.
217 append-ldflags() {
218         [[ $# -eq 0 ]] && return 0
219         local flag
220         for flag in "$@"; do
221                 [[ ${flag} == -l* ]] && \
222                         eqawarn "Appending a library link instruction (${flag}); libraries to link to should not be passed through LDFLAGS"
223         done
224
225         export LDFLAGS="${LDFLAGS} $*"
226         return 0
227 }
228
229 # @FUNCTION: append-flags
230 # @USAGE: <flags>
231 # @DESCRIPTION:
232 # Add extra <flags> to your current {C,CXX,F,FC}FLAGS.
233 append-flags() {
234         [[ $# -eq 0 ]] && return 0
235         case " $* " in
236         *' '-[DIU]*) eqawarn 'please use append-cppflags for preprocessor flags' ;;
237         *' '-L*|\
238         *' '-Wl,*)  eqawarn 'please use append-ldflags for linker flags' ;;
239         esac
240         append-cflags "$@"
241         append-cxxflags "$@"
242         append-fflags "$@"
243         return 0
244 }
245
246 # @FUNCTION: replace-flags
247 # @USAGE: <old> <new>
248 # @DESCRIPTION:
249 # Replace the <old> flag with <new>.  Accepts shell globs for <old>.
250 replace-flags() {
251         [[ $# != 2 ]] && die "Usage: replace-flags <old flag> <new flag>"
252
253         local f var new
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
258                 # list of flags.
259                 new=()
260                 for f in ${!var} ; do
261                         # Note this should work with globs like -O*
262                         [[ ${f} == ${1} ]] && f=${2}
263                         new+=( "${f}" )
264                 done
265                 eval export ${var}=\""${new[*]}"\"
266         done
267
268         return 0
269 }
270
271 # @FUNCTION: replace-cpu-flags
272 # @USAGE: <old> <new>
273 # @DESCRIPTION:
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}"
284                 shift
285         done
286         return 0
287 }
288
289 _is_flagq() {
290         local x var
291         eval var=\""\${$1[*]}"\"
292         for x in ${var} ; do
293                 [[ ${x} == $2 ]] && return 0
294         done
295         return 1
296 }
297
298 # @FUNCTION: is-flagq
299 # @USAGE: <flag>
300 # @DESCRIPTION:
301 # Returns shell true if <flag> is in {C,CXX,F,FC}FLAGS, else returns shell false.  Accepts shell globs.
302 is-flagq() {
303         [[ -n $2 ]] && die "Usage: is-flag <flag>"
304
305         local var
306         for var in $(all-flag-vars) ; do
307                 _is_flagq ${var} "$1" && return 0
308         done
309         return 1
310 }
311
312 # @FUNCTION: is-flag
313 # @USAGE: <flag>
314 # @DESCRIPTION:
315 # Echo's "true" if flag is set in {C,CXX,F,FC}FLAGS.  Accepts shell globs.
316 is-flag() {
317         is-flagq "$@" && echo true
318 }
319
320 # @FUNCTION: is-ldflagq
321 # @USAGE: <flag>
322 # @DESCRIPTION:
323 # Returns shell true if <flag> is in LDFLAGS, else returns shell false.  Accepts shell globs.
324 is-ldflagq() {
325         [[ -n $2 ]] && die "Usage: is-ldflag <flag>"
326         _is_flagq LDFLAGS $1
327 }
328
329 # @FUNCTION: is-ldflag
330 # @USAGE: <flag>
331 # @DESCRIPTION:
332 # Echo's "true" if flag is set in LDFLAGS.  Accepts shell globs.
333 is-ldflag() {
334         is-ldflagq "$@" && echo true
335 }
336
337 # @FUNCTION: filter-mfpmath
338 # @USAGE: <math types>
339 # @DESCRIPTION:
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
342 # -mfpmath=386.
343 filter-mfpmath() {
344         local orig_mfpmath new_math prune_math
345
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
354         prune_math=""
355         for prune_math in "$@" ; do
356                 new_math=${new_math/ ${prune_math} / }
357         done
358         new_math=$(echo ${new_math})
359         new_math=${new_math// /,}
360
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}
365         else
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}
369         fi
370         return 0
371 }
372
373 # @FUNCTION: strip-flags
374 # @DESCRIPTION:
375 # Strip *FLAGS of everything except known good/safe flags.  This runs over all
376 # flags returned by all_flag_vars().
377 strip-flags() {
378         local x y var
379
380         local ALLOWED_FLAGS
381         setup-allowed-flags
382
383         set -f  # disable pathname expansion
384
385         for var in $(all-flag-vars) ; do
386                 local new=()
387
388                 for x in ${!var} ; do
389                         local flag=${x%%=*}
390                         for y in "${ALLOWED_FLAGS[@]}" ; do
391                                 if [[ -z ${flag%%${y}} ]] ; then
392                                         new+=( "${x}" )
393                                         break
394                                 fi
395                         done
396                 done
397
398                 # In case we filtered out all optimization flags fallback to -O2
399                 if _is_flagq ${var} "-O*" && ! _is_flagq new "-O*" ; then
400                         new+=( -O2 )
401                 fi
402
403                 if [[ ${!var} != "${new[*]}" ]] ; then
404                         einfo "strip-flags: ${var}: changed '${!var}' to '${new[*]}'"
405                 fi
406                 eval export ${var}=\""${new[*]}"\"
407         done
408
409         set +f  # re-enable pathname expansion
410
411         return 0
412 }
413
414 test-flag-PROG() {
415         local comp=$1
416         local lang=$2
417         local flag=$3
418
419         [[ -z ${comp} || -z ${flag} ]] && return 1
420
421         local cmdline=(
422                 $(tc-get${comp})
423                 # Clang will warn about unknown gcc flags but exit 0.
424                 # Need -Werror to force it to exit non-zero.
425                 -Werror
426                 # Use -c so we can test the assembler as well.
427                 -c -o /dev/null
428         )
429         if "${cmdline[@]}" -x${lang} - </dev/null >/dev/null 2>&1 ; then
430                 "${cmdline[@]}" "${flag}" -x${lang} - </dev/null >/dev/null 2>&1
431         else
432                 "${cmdline[@]}" "${flag}" -c -o /dev/null /dev/null >/dev/null 2>&1
433         fi
434 }
435
436 # @FUNCTION: test-flag-CC
437 # @USAGE: <flag>
438 # @DESCRIPTION:
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"; }
441
442 # @FUNCTION: test-flag-CXX
443 # @USAGE: <flag>
444 # @DESCRIPTION:
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"; }
447
448 # @FUNCTION: test-flag-F77
449 # @USAGE: <flag>
450 # @DESCRIPTION:
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"; }
453
454 # @FUNCTION: test-flag-FC
455 # @USAGE: <flag>
456 # @DESCRIPTION:
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"; }
459
460 test-flags-PROG() {
461         local comp=$1
462         local flags=()
463         local x
464
465         shift
466
467         [[ -z ${comp} ]] && return 1
468
469         for x ; do
470                 test-flag-${comp} "${x}" && flags+=( "${x}" )
471         done
472
473         echo "${flags[*]}"
474
475         # Just bail if we dont have any flags
476         [[ ${#flags[@]} -gt 0 ]]
477 }
478
479 # @FUNCTION: test-flags-CC
480 # @USAGE: <flags>
481 # @DESCRIPTION:
482 # Returns shell true if <flags> are supported by the C compiler, else returns shell false.
483 test-flags-CC() { test-flags-PROG "CC" "$@"; }
484
485 # @FUNCTION: test-flags-CXX
486 # @USAGE: <flags>
487 # @DESCRIPTION:
488 # Returns shell true if <flags> are supported by the C++ compiler, else returns shell false.
489 test-flags-CXX() { test-flags-PROG "CXX" "$@"; }
490
491 # @FUNCTION: test-flags-F77
492 # @USAGE: <flags>
493 # @DESCRIPTION:
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" "$@"; }
496
497 # @FUNCTION: test-flags-FC
498 # @USAGE: <flags>
499 # @DESCRIPTION:
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" "$@"; }
502
503 # @FUNCTION: test-flags
504 # @USAGE: <flags>
505 # @DESCRIPTION:
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 "$@"; }
509
510 # @FUNCTION: test_version_info
511 # @USAGE: <version>
512 # @DESCRIPTION:
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
517                 return 0
518         else
519                 return 1
520         fi
521 }
522
523 # @FUNCTION: strip-unsupported-flags
524 # @DESCRIPTION:
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})
531 }
532
533 # @FUNCTION: get-flag
534 # @USAGE: <flag>
535 # @DESCRIPTION:
536 # Find and echo the value for a particular flag.  Accepts shell globs.
537 get-flag() {
538         local f var findflag="$1"
539
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}=}"
549                                 return 0
550                         fi
551                 done
552         done
553         return 1
554 }
555
556 # @FUNCTION: has_m64
557 # @DESCRIPTION:
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.
561 has_m64() {
562         eqawarn "${FUNCNAME}: don't use this anymore"
563
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
567         # clean-up!
568
569         local temp="$(emktemp)"
570         echo "int main() { return(0); }" > "${temp}".c
571         MY_CC=$(tc-getCC)
572         ${MY_CC/ .*/} -m64 -o "$(emktemp)" "${temp}".c > /dev/null 2>&1
573         local ret=$?
574         rm -f "${temp}".c
575         [[ ${ret} != 1 ]] && return 0
576         return 1
577 }
578
579 has_m32() {
580         die "${FUNCNAME}: don't use this anymore"
581 }
582
583 # @FUNCTION: replace-sparc64-flags
584 # @DESCRIPTION:
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"
588
589         if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then
590                 for x in ${SPARC64_CPUS}; do
591                         CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
592                 done
593         else
594                 for x in ${SPARC64_CPUS}; do
595                         CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
596                 done
597         fi
598
599         if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then
600                 for x in ${SPARC64_CPUS}; do
601                         CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
602                 done
603         else
604                 for x in ${SPARC64_CPUS}; do
605                         CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
606                 done
607         fi
608
609         export CFLAGS CXXFLAGS
610 }
611
612 # @FUNCTION: append-libs
613 # @USAGE: <libs>
614 # @DESCRIPTION:
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.
618 append-libs() {
619         [[ $# -eq 0 ]] && return 0
620         local flag
621         for flag in "$@"; do
622                 if [[ -z "${flag// }" ]]; then
623                         eqawarn "Appending an empty argument to LIBS is invalid! Skipping."
624                         continue
625                 fi
626                 case $flag in
627                         -[lL]*)
628                                 export LIBS="${LIBS} ${flag}"
629                                 ;;
630                         -*)
631                                 eqawarn "Appending non-library to LIBS (${flag}); Other linker flags should be passed via LDFLAGS"
632                                 export LIBS="${LIBS} ${flag}"
633                                 ;;
634                         *)
635                                 export LIBS="${LIBS} -l${flag}"
636                 esac
637         done
638
639         return 0
640 }
641
642 # @FUNCTION: raw-ldflags
643 # @USAGE: [flags]
644 # @DESCRIPTION:
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,'.
648 #
649 # If no flags are specified, then default to ${LDFLAGS}.
650 raw-ldflags() {
651         local x input="$@"
652         [[ -z ${input} ]] && input=${LDFLAGS}
653         set --
654         for x in ${input} ; do
655                 case ${x} in
656                 -Wl,*)
657                         x=${x#-Wl,}
658                         set -- "$@" ${x//,/ }
659                         ;;
660                 *)      # Assume it's a compiler driver flag, so throw it away #441808
661                         ;;
662                 esac
663         done
664         echo "$@"
665 }
666
667 # @FUNCTION: no-as-needed
668 # @RETURN: Flag to disable asneeded behavior for use with append-ldflags.
669 no-as-needed() {
670         case $($(tc-getLD) -v 2>&1 </dev/null) in
671                 *GNU*) # GNU ld
672                 echo "-Wl,--no-as-needed" ;;
673         esac
674 }
675
676 fi