flag-o-matic.eclass: fix probe when CC points to absolute path
[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 {ADA,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+ -gz
31                 -fno-ident -fpermissive -frecord-gcc-switches
32                 '-fdiagnostics*' '-fplugin*'
33                 '-W*' -w
34
35                 # CPPFLAGS and LDFLAGS
36                 '-[DUILR]*' '-Wl,*'
37
38                 # Linker choice flag
39                 '-fuse-ld'
40         )
41
42         # allow a bunch of flags that negate features / control ABI
43         ALLOWED_FLAGS+=(
44                 '-fno-stack-protector*' '-fabi-version=*'
45                 -fno-strict-aliasing -fno-bounds-check -fno-bounds-checking -fstrict-overflow
46                 -fno-omit-frame-pointer '-fno-builtin*'
47         )
48         ALLOWED_FLAGS+=(
49                 -mregparm -mno-app-regs -mapp-regs -mno-mmx -mno-sse
50                 -mno-sse2 -mno-sse3 -mno-ssse3 -mno-sse4 -mno-sse4.1 -mno-sse4.2
51                 -mno-avx -mno-aes -mno-pclmul -mno-sse4a -mno-3dnow -mno-popcnt
52                 -mno-abm -mips1 -mips2 -mips3 -mips4 -mips32 -mips64 -mips16 -mplt
53                 -msoft-float -mno-soft-float -mhard-float -mno-hard-float -mfpu
54                 -mieee -mieee-with-inexact -mschedule -mfloat-gprs -mspe -mno-spe
55                 -mtls-direct-seg-refs -mno-tls-direct-seg-refs -mflat -mno-flat
56                 -mno-faster-structs -mfaster-structs -m32 -m64 -mx32 -mabi
57                 -mlittle-endian -mbig-endian -EL -EB -fPIC -mlive-g0 -mcmodel
58                 -mstack-bias -mno-stack-bias -msecure-plt '-m*-toc' -mfloat-abi
59                 -mfix-r10000 -mno-fix-r10000 -mthumb -marm
60
61                 # gcc 4.5
62                 -mno-fma4 -mno-movbe -mno-xop -mno-lwp
63                 # gcc 4.6
64                 -mno-fsgsbase -mno-rdrnd -mno-f16c -mno-bmi -mno-tbm
65                 # gcc 4.7
66                 -mno-avx2 -mno-bmi2 -mno-fma -mno-lzcnt
67                 # gcc 4.8
68                 -mno-fxsr -mno-hle -mno-rtm -mno-xsave -mno-xsaveopt
69                 # gcc 4.9
70                 -mno-avx512cd -mno-avx512er -mno-avx512f -mno-avx512pf -mno-sha
71         )
72
73         # Allow some safe individual flags. Should come along with the bug reference.
74         ALLOWED_FLAGS+=(
75                 # Allow explicit stack realignment to run non-conformant
76                 # binaries: bug #677852
77                 -mstackrealign
78         )
79 }
80
81 # inverted filters for hardened compiler.  This is trying to unpick
82 # the hardened compiler defaults.
83 _filter-hardened() {
84         local f
85         for f in "$@" ; do
86                 case "${f}" in
87                         # Ideally we should only concern ourselves with PIE flags,
88                         # not -fPIC or -fpic, but too many places filter -fPIC without
89                         # thinking about -fPIE.
90                         -fPIC|-fpic|-fPIE|-fpie|-Wl,pie|-pie)
91                                 gcc-specs-pie || continue
92                                 if ! is-flagq -nopie && ! is-flagq -no-pie ; then
93                                         # Support older Gentoo form first (-nopie) before falling
94                                         # back to the official gcc-6+ form (-no-pie).
95                                         if test-flags -nopie >/dev/null ; then
96                                                 append-flags -nopie
97                                         else
98                                                 append-flags -no-pie
99                                         fi
100                                 fi
101                                 ;;
102                         -fstack-protector)
103                                 gcc-specs-ssp || continue
104                                 is-flagq -fno-stack-protector || append-flags $(test-flags -fno-stack-protector);;
105                         -fstack-protector-all)
106                                 gcc-specs-ssp-to-all || continue
107                                 is-flagq -fno-stack-protector-all || append-flags $(test-flags -fno-stack-protector-all);;
108                         -fno-strict-overflow)
109                                 gcc-specs-nostrict || continue
110                                 is-flagq -fstrict-overflow || append-flags $(test-flags -fstrict-overflow);;
111                 esac
112         done
113 }
114
115 # Remove occurrences of strings from variable given in $1
116 # Strings removed are matched as globs, so for example
117 # '-O*' would remove -O1, -O2 etc.
118 _filter-var() {
119         local f x var=$1 new=()
120         shift
121
122         for f in ${!var} ; do
123                 for x in "$@" ; do
124                         # Note this should work with globs like -O*
125                         [[ ${f} == ${x} ]] && continue 2
126                 done
127                 new+=( "${f}" )
128         done
129         export ${var}="${new[*]}"
130 }
131
132 # @FUNCTION: filter-flags
133 # @USAGE: <flags>
134 # @DESCRIPTION:
135 # Remove particular <flags> from {C,CPP,CXX,CCAS,F,FC,LD}FLAGS.  Accepts shell globs.
136 filter-flags() {
137         _filter-hardened "$@"
138         local v
139         for v in $(all-flag-vars) ; do
140                 _filter-var ${v} "$@"
141         done
142         return 0
143 }
144
145 # @FUNCTION: filter-lfs-flags
146 # @DESCRIPTION:
147 # Remove flags that enable Large File Support.
148 filter-lfs-flags() {
149         [[ $# -ne 0 ]] && die "filter-lfs-flags takes no arguments"
150         # http://www.gnu.org/s/libc/manual/html_node/Feature-Test-Macros.html
151         # _LARGEFILE_SOURCE: enable support for new LFS funcs (ftello/etc...)
152         # _LARGEFILE64_SOURCE: enable support for 64bit variants (off64_t/fseeko64/etc...)
153         # _FILE_OFFSET_BITS: default to 64bit variants (off_t is defined as off64_t)
154         filter-flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
155 }
156
157 # @FUNCTION: filter-ldflags
158 # @USAGE: <flags>
159 # @DESCRIPTION:
160 # Remove particular <flags> from LDFLAGS.  Accepts shell globs.
161 filter-ldflags() {
162         _filter-var LDFLAGS "$@"
163         return 0
164 }
165
166 # @FUNCTION: append-cppflags
167 # @USAGE: <flags>
168 # @DESCRIPTION:
169 # Add extra <flags> to the current CPPFLAGS.
170 append-cppflags() {
171         [[ $# -eq 0 ]] && return 0
172         export CPPFLAGS+=" $*"
173         return 0
174 }
175
176 # @FUNCTION: append-cflags
177 # @USAGE: <flags>
178 # @DESCRIPTION:
179 # Add extra <flags> to the current CFLAGS.  If a flag might not be supported
180 # with different compilers (or versions), then use test-flags-CC like so:
181 # @CODE
182 # append-cflags $(test-flags-CC -funky-flag)
183 # @CODE
184 append-cflags() {
185         [[ $# -eq 0 ]] && return 0
186         # Do not do automatic flag testing ourselves. #417047
187         export CFLAGS+=" $*"
188         return 0
189 }
190
191 # @FUNCTION: append-cxxflags
192 # @USAGE: <flags>
193 # @DESCRIPTION:
194 # Add extra <flags> to the current CXXFLAGS.  If a flag might not be supported
195 # with different compilers (or versions), then use test-flags-CXX like so:
196 # @CODE
197 # append-cxxflags $(test-flags-CXX -funky-flag)
198 # @CODE
199 append-cxxflags() {
200         [[ $# -eq 0 ]] && return 0
201         # Do not do automatic flag testing ourselves. #417047
202         export CXXFLAGS+=" $*"
203         return 0
204 }
205
206 # @FUNCTION: append-fflags
207 # @USAGE: <flags>
208 # @DESCRIPTION:
209 # Add extra <flags> to the current {F,FC}FLAGS.  If a flag might not be supported
210 # with different compilers (or versions), then use test-flags-F77 like so:
211 # @CODE
212 # append-fflags $(test-flags-F77 -funky-flag)
213 # @CODE
214 append-fflags() {
215         [[ $# -eq 0 ]] && return 0
216         # Do not do automatic flag testing ourselves. #417047
217         export FFLAGS+=" $*"
218         export FCFLAGS+=" $*"
219         return 0
220 }
221
222 # @FUNCTION: append-lfs-flags
223 # @DESCRIPTION:
224 # Add flags that enable Large File Support.
225 append-lfs-flags() {
226         [[ $# -ne 0 ]] && die "append-lfs-flags takes no arguments"
227         # see comments in filter-lfs-flags func for meaning of these
228         append-cppflags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
229 }
230
231 # @FUNCTION: append-ldflags
232 # @USAGE: <flags>
233 # @DESCRIPTION:
234 # Add extra <flags> to the current LDFLAGS.
235 append-ldflags() {
236         [[ $# -eq 0 ]] && return 0
237         local flag
238         for flag in "$@"; do
239                 [[ ${flag} == -l* ]] && \
240                         eqawarn "Appending a library link instruction (${flag}); libraries to link to should not be passed through LDFLAGS"
241         done
242
243         export LDFLAGS="${LDFLAGS} $*"
244         return 0
245 }
246
247 # @FUNCTION: append-flags
248 # @USAGE: <flags>
249 # @DESCRIPTION:
250 # Add extra <flags> to your current {C,CXX,F,FC}FLAGS.
251 append-flags() {
252         [[ $# -eq 0 ]] && return 0
253         case " $* " in
254         *' '-[DIU]*) eqawarn 'please use append-cppflags for preprocessor flags' ;;
255         *' '-L*|\
256         *' '-Wl,*)  eqawarn 'please use append-ldflags for linker flags' ;;
257         esac
258         append-cflags "$@"
259         append-cxxflags "$@"
260         append-fflags "$@"
261         return 0
262 }
263
264 # @FUNCTION: replace-flags
265 # @USAGE: <old> <new>
266 # @DESCRIPTION:
267 # Replace the <old> flag with <new>.  Accepts shell globs for <old>.
268 replace-flags() {
269         [[ $# != 2 ]] && die "Usage: replace-flags <old flag> <new flag>"
270
271         local f var new
272         for var in $(all-flag-vars) ; do
273                 # Looping over the flags instead of using a global
274                 # substitution ensures that we're working with flag atoms.
275                 # Otherwise globs like -O* have the potential to wipe out the
276                 # list of flags.
277                 new=()
278                 for f in ${!var} ; do
279                         # Note this should work with globs like -O*
280                         [[ ${f} == ${1} ]] && f=${2}
281                         new+=( "${f}" )
282                 done
283                 export ${var}="${new[*]}"
284         done
285
286         return 0
287 }
288
289 # @FUNCTION: replace-cpu-flags
290 # @USAGE: <old> <new>
291 # @DESCRIPTION:
292 # Replace cpu flags (like -march/-mcpu/-mtune) that select the <old> cpu
293 # with flags that select the <new> cpu.  Accepts shell globs for <old>.
294 replace-cpu-flags() {
295         local newcpu="$#" ; newcpu="${!newcpu}"
296         while [ $# -gt 1 ] ; do
297                 # quote to make sure that no globbing is done (particularly on
298                 # ${oldcpu}) prior to calling replace-flags
299                 replace-flags "-march=${1}" "-march=${newcpu}"
300                 replace-flags "-mcpu=${1}" "-mcpu=${newcpu}"
301                 replace-flags "-mtune=${1}" "-mtune=${newcpu}"
302                 shift
303         done
304         return 0
305 }
306
307 _is_flagq() {
308         local x var="$1[*]"
309         for x in ${!var} ; do
310                 [[ ${x} == $2 ]] && return 0
311         done
312         return 1
313 }
314
315 # @FUNCTION: is-flagq
316 # @USAGE: <flag>
317 # @DESCRIPTION:
318 # Returns shell true if <flag> is in {C,CXX,F,FC}FLAGS, else returns shell false.  Accepts shell globs.
319 is-flagq() {
320         [[ -n $2 ]] && die "Usage: is-flag <flag>"
321
322         local var
323         for var in $(all-flag-vars) ; do
324                 _is_flagq ${var} "$1" && return 0
325         done
326         return 1
327 }
328
329 # @FUNCTION: is-flag
330 # @USAGE: <flag>
331 # @DESCRIPTION:
332 # Echo's "true" if flag is set in {C,CXX,F,FC}FLAGS.  Accepts shell globs.
333 is-flag() {
334         is-flagq "$@" && echo true
335 }
336
337 # @FUNCTION: is-ldflagq
338 # @USAGE: <flag>
339 # @DESCRIPTION:
340 # Returns shell true if <flag> is in LDFLAGS, else returns shell false.  Accepts shell globs.
341 is-ldflagq() {
342         [[ -n $2 ]] && die "Usage: is-ldflag <flag>"
343         _is_flagq LDFLAGS $1
344 }
345
346 # @FUNCTION: is-ldflag
347 # @USAGE: <flag>
348 # @DESCRIPTION:
349 # Echo's "true" if flag is set in LDFLAGS.  Accepts shell globs.
350 is-ldflag() {
351         is-ldflagq "$@" && echo true
352 }
353
354 # @FUNCTION: filter-mfpmath
355 # @USAGE: <math types>
356 # @DESCRIPTION:
357 # Remove specified math types from the fpmath flag.  For example, if the user
358 # has -mfpmath=sse,386, running `filter-mfpmath sse` will leave the user with
359 # -mfpmath=386.
360 filter-mfpmath() {
361         local orig_mfpmath new_math prune_math
362
363         # save the original -mfpmath flag
364         orig_mfpmath=$(get-flag -mfpmath)
365         # get the value of the current -mfpmath flag
366         new_math=$(get-flag mfpmath)
367         # convert "both" to something we can filter
368         new_math=${new_math/both/387,sse}
369         new_math=" ${new_math//[,+]/ } "
370         # figure out which math values are to be removed
371         prune_math=""
372         for prune_math in "$@" ; do
373                 new_math=${new_math/ ${prune_math} / }
374         done
375         new_math=$(echo ${new_math})
376         new_math=${new_math// /,}
377
378         if [[ -z ${new_math} ]] ; then
379                 # if we're removing all user specified math values are
380                 # slated for removal, then we just filter the flag
381                 filter-flags ${orig_mfpmath}
382         else
383                 # if we only want to filter some of the user specified
384                 # math values, then we replace the current flag
385                 replace-flags ${orig_mfpmath} -mfpmath=${new_math}
386         fi
387         return 0
388 }
389
390 # @FUNCTION: strip-flags
391 # @DESCRIPTION:
392 # Strip *FLAGS of everything except known good/safe flags.  This runs over all
393 # flags returned by all_flag_vars().
394 strip-flags() {
395         local x y var
396
397         local ALLOWED_FLAGS
398         setup-allowed-flags
399
400         set -f  # disable pathname expansion
401
402         for var in $(all-flag-vars) ; do
403                 local new=()
404
405                 for x in ${!var} ; do
406                         local flag=${x%%=*}
407                         for y in "${ALLOWED_FLAGS[@]}" ; do
408                                 if [[ -z ${flag%%${y}} ]] ; then
409                                         new+=( "${x}" )
410                                         break
411                                 fi
412                         done
413                 done
414
415                 # In case we filtered out all optimization flags fallback to -O2
416                 if _is_flagq ${var} "-O*" && ! _is_flagq new "-O*" ; then
417                         new+=( -O2 )
418                 fi
419
420                 if [[ ${!var} != "${new[*]}" ]] ; then
421                         einfo "strip-flags: ${var}: changed '${!var}' to '${new[*]}'"
422                 fi
423                 export ${var}="${new[*]}"
424         done
425
426         set +f  # re-enable pathname expansion
427
428         return 0
429 }
430
431 test-flag-PROG() {
432         local comp=$1
433         local lang=$2
434         shift 2
435
436         [[ -z ${comp} || -z $1 ]] && return 1
437
438         # verify selected compiler exists before using it
439         comp=($(tc-get${comp}))
440         # 'comp' can already contain compiler options.
441         # 'type' needs a binary name
442         type -p ${comp[0]} >/dev/null || return 1
443
444         # Set up test file.
445         local in_src in_ext cmdline_extra=()
446         case "${lang}" in
447                 # compiler/assembler only
448                 c)
449                         in_ext='.c'
450                         in_src='int main(void) { return 0; }'
451                         cmdline_extra+=(-xc -c)
452                         ;;
453                 c++)
454                         in_ext='.cc'
455                         in_src='int main(void) { return 0; }'
456                         cmdline_extra+=(-xc++ -c)
457                         ;;
458                 f77)
459                         in_ext='.f'
460                         # fixed source form
461                         in_src='      end'
462                         cmdline_extra+=(-xf77 -c)
463                         ;;
464                 f95)
465                         in_ext='.f90'
466                         in_src='end'
467                         cmdline_extra+=(-xf95 -c)
468                         ;;
469
470                 # C compiler/assembler/linker
471                 c+ld)
472                         in_ext='.c'
473                         in_src='int main(void) { return 0; }'
474                         cmdline_extra+=(-xc)
475                         ;;
476         esac
477         local test_in=${T}/test-flag.${lang}
478         local test_out=${T}/test-flag.exe
479
480         printf "%s\n" "${in_src}" > "${test_in}" || return 1
481
482         local cmdline=(
483                 "${comp[@]}"
484                 # Clang will warn about unknown gcc flags but exit 0.
485                 # Need -Werror to force it to exit non-zero.
486                 -Werror
487                 "$@"
488                 # -x<lang> options need to go before first source file
489                 "${cmdline_extra[@]}"
490
491                 "${test_in}" -o "${test_out}"
492         )
493
494         if ! "${cmdline[@]}" &>/dev/null; then
495                 # -Werror makes clang bail out on unused arguments as well;
496                 # try to add -Qunused-arguments to work-around that
497                 # other compilers don't support it but then, it's failure like
498                 # any other
499                 cmdline+=( -Qunused-arguments )
500                 "${cmdline[@]}" &>/dev/null
501         fi
502 }
503
504 # @FUNCTION: test-flag-CC
505 # @USAGE: <flag>
506 # @DESCRIPTION:
507 # Returns shell true if <flag> is supported by the C compiler, else returns shell false.
508 test-flag-CC() { test-flag-PROG "CC" c "$@"; }
509
510 # @FUNCTION: test-flag-CXX
511 # @USAGE: <flag>
512 # @DESCRIPTION:
513 # Returns shell true if <flag> is supported by the C++ compiler, else returns shell false.
514 test-flag-CXX() { test-flag-PROG "CXX" c++ "$@"; }
515
516 # @FUNCTION: test-flag-F77
517 # @USAGE: <flag>
518 # @DESCRIPTION:
519 # Returns shell true if <flag> is supported by the Fortran 77 compiler, else returns shell false.
520 test-flag-F77() { test-flag-PROG "F77" f77 "$@"; }
521
522 # @FUNCTION: test-flag-FC
523 # @USAGE: <flag>
524 # @DESCRIPTION:
525 # Returns shell true if <flag> is supported by the Fortran 90 compiler, else returns shell false.
526 test-flag-FC() { test-flag-PROG "FC" f95 "$@"; }
527
528 # @FUNCTION: test-flag-CCLD
529 # @USAGE: <flag>
530 # @DESCRIPTION:
531 # Returns shell true if <flag> is supported by the C compiler and linker, else returns shell false.
532 test-flag-CCLD() { test-flag-PROG "CC" c+ld "$@"; }
533
534 test-flags-PROG() {
535         local comp=$1
536         local flags=()
537         local x
538
539         shift
540
541         [[ -z ${comp} ]] && return 1
542
543         while (( $# )); do
544                 case "$1" in
545                         # '-B /foo': bug # 687198
546                         --param|-B)
547                                 if test-flag-${comp} "$1" "$2"; then
548                                         flags+=( "$1" "$2" )
549                                 fi
550                                 shift 2
551                                 ;;
552                         *)
553                                 if test-flag-${comp} "$1"; then
554                                         flags+=( "$1" )
555                                 fi
556                                 shift 1
557                                 ;;
558                 esac
559         done
560
561         echo "${flags[*]}"
562
563         # Just bail if we dont have any flags
564         [[ ${#flags[@]} -gt 0 ]]
565 }
566
567 # @FUNCTION: test-flags-CC
568 # @USAGE: <flags>
569 # @DESCRIPTION:
570 # Returns shell true if <flags> are supported by the C compiler, else returns shell false.
571 test-flags-CC() { test-flags-PROG "CC" "$@"; }
572
573 # @FUNCTION: test-flags-CXX
574 # @USAGE: <flags>
575 # @DESCRIPTION:
576 # Returns shell true if <flags> are supported by the C++ compiler, else returns shell false.
577 test-flags-CXX() { test-flags-PROG "CXX" "$@"; }
578
579 # @FUNCTION: test-flags-F77
580 # @USAGE: <flags>
581 # @DESCRIPTION:
582 # Returns shell true if <flags> are supported by the Fortran 77 compiler, else returns shell false.
583 test-flags-F77() { test-flags-PROG "F77" "$@"; }
584
585 # @FUNCTION: test-flags-FC
586 # @USAGE: <flags>
587 # @DESCRIPTION:
588 # Returns shell true if <flags> are supported by the Fortran 90 compiler, else returns shell false.
589 test-flags-FC() { test-flags-PROG "FC" "$@"; }
590
591 # @FUNCTION: test-flags-CCLD
592 # @USAGE: <flags>
593 # @DESCRIPTION:
594 # Returns shell true if <flags> are supported by the C compiler and default linker, else returns shell false.
595 test-flags-CCLD() { test-flags-PROG "CCLD" "$@"; }
596
597 # @FUNCTION: test-flags
598 # @USAGE: <flags>
599 # @DESCRIPTION:
600 # Short-hand that should hopefully work for both C and C++ compiler, but
601 # its really only present due to the append-flags() abomination.
602 test-flags() { test-flags-CC "$@"; }
603
604 # @FUNCTION: test_version_info
605 # @USAGE: <version>
606 # @DESCRIPTION:
607 # Returns shell true if the current C compiler version matches <version>, else returns shell false.
608 # Accepts shell globs.
609 test_version_info() {
610         if [[ $($(tc-getCC) --version 2>&1) == *$1* ]]; then
611                 return 0
612         else
613                 return 1
614         fi
615 }
616
617 # @FUNCTION: strip-unsupported-flags
618 # @DESCRIPTION:
619 # Strip {C,CXX,F,FC}FLAGS of any flags not supported by the active toolchain.
620 strip-unsupported-flags() {
621         export CFLAGS=$(test-flags-CC ${CFLAGS})
622         export CXXFLAGS=$(test-flags-CXX ${CXXFLAGS})
623         export FFLAGS=$(test-flags-F77 ${FFLAGS})
624         export FCFLAGS=$(test-flags-FC ${FCFLAGS})
625         export LDFLAGS=$(test-flags-CCLD ${LDFLAGS})
626 }
627
628 # @FUNCTION: get-flag
629 # @USAGE: <flag>
630 # @DESCRIPTION:
631 # Find and echo the value for a particular flag.  Accepts shell globs.
632 get-flag() {
633         local f var findflag="$1"
634
635         # this code looks a little flaky but seems to work for
636         # everything we want ...
637         # for example, if CFLAGS="-march=i686":
638         # `get-flag -march` == "-march=i686"
639         # `get-flag march` == "i686"
640         for var in $(all-flag-vars) ; do
641                 for f in ${!var} ; do
642                         if [ "${f/${findflag}}" != "${f}" ] ; then
643                                 printf "%s\n" "${f/-${findflag}=}"
644                                 return 0
645                         fi
646                 done
647         done
648         return 1
649 }
650
651 has_m64() {
652         die "${FUNCNAME}: don't use this anymore"
653 }
654
655 has_m32() {
656         die "${FUNCNAME}: don't use this anymore"
657 }
658
659 # @FUNCTION: replace-sparc64-flags
660 # @DESCRIPTION:
661 # Sets mcpu to v8 and uses the original value as mtune if none specified.
662 replace-sparc64-flags() {
663         local SPARC64_CPUS="ultrasparc3 ultrasparc v9"
664
665         if [ "${CFLAGS/mtune}" != "${CFLAGS}" ]; then
666                 for x in ${SPARC64_CPUS}; do
667                         CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8}"
668                 done
669         else
670                 for x in ${SPARC64_CPUS}; do
671                         CFLAGS="${CFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
672                 done
673         fi
674
675         if [ "${CXXFLAGS/mtune}" != "${CXXFLAGS}" ]; then
676                 for x in ${SPARC64_CPUS}; do
677                         CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8}"
678                 done
679         else
680                 for x in ${SPARC64_CPUS}; do
681                         CXXFLAGS="${CXXFLAGS/-mcpu=${x}/-mcpu=v8 -mtune=${x}}"
682                 done
683         fi
684
685         export CFLAGS CXXFLAGS
686 }
687
688 # @FUNCTION: append-libs
689 # @USAGE: <libs>
690 # @DESCRIPTION:
691 # Add extra <libs> to the current LIBS. All arguments should be prefixed with
692 # either -l or -L.  For compatibility, if arguments are not prefixed as
693 # options, they are given a -l prefix automatically.
694 append-libs() {
695         [[ $# -eq 0 ]] && return 0
696         local flag
697         for flag in "$@"; do
698                 if [[ -z "${flag// }" ]]; then
699                         eqawarn "Appending an empty argument to LIBS is invalid! Skipping."
700                         continue
701                 fi
702                 case $flag in
703                         -[lL]*)
704                                 export LIBS="${LIBS} ${flag}"
705                                 ;;
706                         -*)
707                                 eqawarn "Appending non-library to LIBS (${flag}); Other linker flags should be passed via LDFLAGS"
708                                 export LIBS="${LIBS} ${flag}"
709                                 ;;
710                         *)
711                                 export LIBS="${LIBS} -l${flag}"
712                 esac
713         done
714
715         return 0
716 }
717
718 # @FUNCTION: raw-ldflags
719 # @USAGE: [flags]
720 # @DESCRIPTION:
721 # Turn C style ldflags (-Wl,-foo) into straight ldflags - the results
722 # are suitable for passing directly to 'ld'; note LDFLAGS is usually passed
723 # to gcc where it needs the '-Wl,'.
724 #
725 # If no flags are specified, then default to ${LDFLAGS}.
726 raw-ldflags() {
727         local x input="$@"
728         [[ -z ${input} ]] && input=${LDFLAGS}
729         set --
730         for x in ${input} ; do
731                 case ${x} in
732                 -Wl,*)
733                         x=${x#-Wl,}
734                         set -- "$@" ${x//,/ }
735                         ;;
736                 *)      # Assume it's a compiler driver flag, so throw it away #441808
737                         ;;
738                 esac
739         done
740         echo "$@"
741 }
742
743 # @FUNCTION: no-as-needed
744 # @RETURN: Flag to disable asneeded behavior for use with append-ldflags.
745 no-as-needed() {
746         case $($(tc-getLD) -v 2>&1 </dev/null) in
747                 *GNU*) # GNU ld
748                 echo "-Wl,--no-as-needed" ;;
749         esac
750 }
751
752 fi