flag-o-matic.eclass: Strip LDFLAGS unsupported by the C compiler, #621274
[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 ; then
437                 cmdline+=( "${flag}" -x${lang} - )
438         else
439                 # XXX: what's the purpose of this? does it even work with
440                 # any compiler?
441                 cmdline+=( "${flag}" -c -o /dev/null /dev/null )
442         fi
443
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
448                 # any other
449                 cmdline+=( -Qunused-arguments )
450                 "${cmdline[@]}" </dev/null &>/dev/null
451         fi
452 }
453
454 # @FUNCTION: test-flag-CC
455 # @USAGE: <flag>
456 # @DESCRIPTION:
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"; }
459
460 # @FUNCTION: test-flag-CXX
461 # @USAGE: <flag>
462 # @DESCRIPTION:
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"; }
465
466 # @FUNCTION: test-flag-F77
467 # @USAGE: <flag>
468 # @DESCRIPTION:
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"; }
471
472 # @FUNCTION: test-flag-FC
473 # @USAGE: <flag>
474 # @DESCRIPTION:
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"; }
477
478 test-flags-PROG() {
479         local comp=$1
480         local flags=()
481         local x
482
483         shift
484
485         [[ -z ${comp} ]] && return 1
486
487         for x ; do
488                 test-flag-${comp} "${x}" && flags+=( "${x}" )
489         done
490
491         echo "${flags[*]}"
492
493         # Just bail if we dont have any flags
494         [[ ${#flags[@]} -gt 0 ]]
495 }
496
497 # @FUNCTION: test-flags-CC
498 # @USAGE: <flags>
499 # @DESCRIPTION:
500 # Returns shell true if <flags> are supported by the C compiler, else returns shell false.
501 test-flags-CC() { test-flags-PROG "CC" "$@"; }
502
503 # @FUNCTION: test-flags-CXX
504 # @USAGE: <flags>
505 # @DESCRIPTION:
506 # Returns shell true if <flags> are supported by the C++ compiler, else returns shell false.
507 test-flags-CXX() { test-flags-PROG "CXX" "$@"; }
508
509 # @FUNCTION: test-flags-F77
510 # @USAGE: <flags>
511 # @DESCRIPTION:
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" "$@"; }
514
515 # @FUNCTION: test-flags-FC
516 # @USAGE: <flags>
517 # @DESCRIPTION:
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" "$@"; }
520
521 # @FUNCTION: test-flags
522 # @USAGE: <flags>
523 # @DESCRIPTION:
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 "$@"; }
527
528 # @FUNCTION: test_version_info
529 # @USAGE: <version>
530 # @DESCRIPTION:
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
535                 return 0
536         else
537                 return 1
538         fi
539 }
540
541 # @FUNCTION: strip-unsupported-flags
542 # @DESCRIPTION:
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})
552 }
553
554 # @FUNCTION: get-flag
555 # @USAGE: <flag>
556 # @DESCRIPTION:
557 # Find and echo the value for a particular flag.  Accepts shell globs.
558 get-flag() {
559         local f var findflag="$1"
560
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}=}"
570                                 return 0
571                         fi
572                 done
573         done
574         return 1
575 }
576
577 has_m64() {
578         die "${FUNCNAME}: don't use this anymore"
579 }
580
581 has_m32() {
582         die "${FUNCNAME}: don't use this anymore"
583 }
584
585 # @FUNCTION: replace-sparc64-flags
586 # @DESCRIPTION:
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"
590
591         if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then
592                 for x in ${SPARC64_CPUS}; do
593                         CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
594                 done
595         else
596                 for x in ${SPARC64_CPUS}; do
597                         CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
598                 done
599         fi
600
601         if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then
602                 for x in ${SPARC64_CPUS}; do
603                         CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
604                 done
605         else
606                 for x in ${SPARC64_CPUS}; do
607                         CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
608                 done
609         fi
610
611         export CFLAGS CXXFLAGS
612 }
613
614 # @FUNCTION: append-libs
615 # @USAGE: <libs>
616 # @DESCRIPTION:
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.
620 append-libs() {
621         [[ $# -eq 0 ]] && return 0
622         local flag
623         for flag in "$@"; do
624                 if [[ -z "${flag// }" ]]; then
625                         eqawarn "Appending an empty argument to LIBS is invalid! Skipping."
626                         continue
627                 fi
628                 case $flag in
629                         -[lL]*)
630                                 export LIBS="${LIBS} ${flag}"
631                                 ;;
632                         -*)
633                                 eqawarn "Appending non-library to LIBS (${flag}); Other linker flags should be passed via LDFLAGS"
634                                 export LIBS="${LIBS} ${flag}"
635                                 ;;
636                         *)
637                                 export LIBS="${LIBS} -l${flag}"
638                 esac
639         done
640
641         return 0
642 }
643
644 # @FUNCTION: raw-ldflags
645 # @USAGE: [flags]
646 # @DESCRIPTION:
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,'.
650 #
651 # If no flags are specified, then default to ${LDFLAGS}.
652 raw-ldflags() {
653         local x input="$@"
654         [[ -z ${input} ]] && input=${LDFLAGS}
655         set --
656         for x in ${input} ; do
657                 case ${x} in
658                 -Wl,*)
659                         x=${x#-Wl,}
660                         set -- "$@" ${x//,/ }
661                         ;;
662                 *)      # Assume it's a compiler driver flag, so throw it away #441808
663                         ;;
664                 esac
665         done
666         echo "$@"
667 }
668
669 # @FUNCTION: no-as-needed
670 # @RETURN: Flag to disable asneeded behavior for use with append-ldflags.
671 no-as-needed() {
672         case $($(tc-getLD) -v 2>&1 </dev/null) in
673                 *GNU*) # GNU ld
674                 echo "-Wl,--no-as-needed" ;;
675         esac
676 }
677
678 fi