net-fs/openafs-kernel: remove vulnerable versions
[gentoo.git] / eclass / toolchain-funcs.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: toolchain-funcs.eclass
6 # @MAINTAINER:
7 # Toolchain Ninjas <toolchain@gentoo.org>
8 # @BLURB: functions to query common info about the toolchain
9 # @DESCRIPTION:
10 # The toolchain-funcs aims to provide a complete suite of functions
11 # for gleaning useful information about the toolchain and to simplify
12 # ugly things like cross-compiling and multilib.  All of this is done
13 # in such a way that you can rely on the function always returning
14 # something sane.
15
16 if [[ -z ${_TOOLCHAIN_FUNCS_ECLASS} ]]; then
17 _TOOLCHAIN_FUNCS_ECLASS=1
18
19 inherit multilib
20
21 # tc-getPROG <VAR [search vars]> <default> [tuple]
22 _tc-getPROG() {
23         local tuple=$1
24         local v var vars=$2
25         local prog=$3
26
27         var=${vars%% *}
28         for v in ${vars} ; do
29                 if [[ -n ${!v} ]] ; then
30                         export ${var}="${!v}"
31                         echo "${!v}"
32                         return 0
33                 fi
34         done
35
36         local search=
37         [[ -n $4 ]] && search=$(type -p "$4-${prog}")
38         [[ -z ${search} && -n ${!tuple} ]] && search=$(type -p "${!tuple}-${prog}")
39         [[ -n ${search} ]] && prog=${search##*/}
40
41         export ${var}=${prog}
42         echo "${!var}"
43 }
44 tc-getBUILD_PROG() { _tc-getPROG CBUILD "BUILD_$1 $1_FOR_BUILD HOST$1" "${@:2}"; }
45 tc-getPROG() { _tc-getPROG CHOST "$@"; }
46
47 # @FUNCTION: tc-getAR
48 # @USAGE: [toolchain prefix]
49 # @RETURN: name of the archiver
50 tc-getAR() { tc-getPROG AR ar "$@"; }
51 # @FUNCTION: tc-getAS
52 # @USAGE: [toolchain prefix]
53 # @RETURN: name of the assembler
54 tc-getAS() { tc-getPROG AS as "$@"; }
55 # @FUNCTION: tc-getCC
56 # @USAGE: [toolchain prefix]
57 # @RETURN: name of the C compiler
58 tc-getCC() { tc-getPROG CC gcc "$@"; }
59 # @FUNCTION: tc-getCPP
60 # @USAGE: [toolchain prefix]
61 # @RETURN: name of the C preprocessor
62 tc-getCPP() { tc-getPROG CPP cpp "$@"; }
63 # @FUNCTION: tc-getCXX
64 # @USAGE: [toolchain prefix]
65 # @RETURN: name of the C++ compiler
66 tc-getCXX() { tc-getPROG CXX g++ "$@"; }
67 # @FUNCTION: tc-getLD
68 # @USAGE: [toolchain prefix]
69 # @RETURN: name of the linker
70 tc-getLD() { tc-getPROG LD ld "$@"; }
71 # @FUNCTION: tc-getSTRIP
72 # @USAGE: [toolchain prefix]
73 # @RETURN: name of the strip program
74 tc-getSTRIP() { tc-getPROG STRIP strip "$@"; }
75 # @FUNCTION: tc-getNM
76 # @USAGE: [toolchain prefix]
77 # @RETURN: name of the symbol/object thingy
78 tc-getNM() { tc-getPROG NM nm "$@"; }
79 # @FUNCTION: tc-getRANLIB
80 # @USAGE: [toolchain prefix]
81 # @RETURN: name of the archiver indexer
82 tc-getRANLIB() { tc-getPROG RANLIB ranlib "$@"; }
83 # @FUNCTION: tc-getOBJCOPY
84 # @USAGE: [toolchain prefix]
85 # @RETURN: name of the object copier
86 tc-getOBJCOPY() { tc-getPROG OBJCOPY objcopy "$@"; }
87 # @FUNCTION: tc-getOBJDUMP
88 # @USAGE: [toolchain prefix]
89 # @RETURN: name of the object dumper
90 tc-getOBJDUMP() { tc-getPROG OBJDUMP objdump "$@"; }
91 # @FUNCTION: tc-getF77
92 # @USAGE: [toolchain prefix]
93 # @RETURN: name of the Fortran 77 compiler
94 tc-getF77() { tc-getPROG F77 gfortran "$@"; }
95 # @FUNCTION: tc-getFC
96 # @USAGE: [toolchain prefix]
97 # @RETURN: name of the Fortran 90 compiler
98 tc-getFC() { tc-getPROG FC gfortran "$@"; }
99 # @FUNCTION: tc-getGCJ
100 # @USAGE: [toolchain prefix]
101 # @RETURN: name of the java compiler
102 tc-getGCJ() { tc-getPROG GCJ gcj "$@"; }
103 # @FUNCTION: tc-getGO
104 # @USAGE: [toolchain prefix]
105 # @RETURN: name of the Go compiler
106 tc-getGO() { tc-getPROG GO gccgo "$@"; }
107 # @FUNCTION: tc-getPKG_CONFIG
108 # @USAGE: [toolchain prefix]
109 # @RETURN: name of the pkg-config tool
110 tc-getPKG_CONFIG() { tc-getPROG PKG_CONFIG pkg-config "$@"; }
111 # @FUNCTION: tc-getRC
112 # @USAGE: [toolchain prefix]
113 # @RETURN: name of the Windows resource compiler
114 tc-getRC() { tc-getPROG RC windres "$@"; }
115 # @FUNCTION: tc-getDLLWRAP
116 # @USAGE: [toolchain prefix]
117 # @RETURN: name of the Windows dllwrap utility
118 tc-getDLLWRAP() { tc-getPROG DLLWRAP dllwrap "$@"; }
119
120 # @FUNCTION: tc-getBUILD_AR
121 # @USAGE: [toolchain prefix]
122 # @RETURN: name of the archiver for building binaries to run on the build machine
123 tc-getBUILD_AR() { tc-getBUILD_PROG AR ar "$@"; }
124 # @FUNCTION: tc-getBUILD_AS
125 # @USAGE: [toolchain prefix]
126 # @RETURN: name of the assembler for building binaries to run on the build machine
127 tc-getBUILD_AS() { tc-getBUILD_PROG AS as "$@"; }
128 # @FUNCTION: tc-getBUILD_CC
129 # @USAGE: [toolchain prefix]
130 # @RETURN: name of the C compiler for building binaries to run on the build machine
131 tc-getBUILD_CC() { tc-getBUILD_PROG CC gcc "$@"; }
132 # @FUNCTION: tc-getBUILD_CPP
133 # @USAGE: [toolchain prefix]
134 # @RETURN: name of the C preprocessor for building binaries to run on the build machine
135 tc-getBUILD_CPP() { tc-getBUILD_PROG CPP cpp "$@"; }
136 # @FUNCTION: tc-getBUILD_CXX
137 # @USAGE: [toolchain prefix]
138 # @RETURN: name of the C++ compiler for building binaries to run on the build machine
139 tc-getBUILD_CXX() { tc-getBUILD_PROG CXX g++ "$@"; }
140 # @FUNCTION: tc-getBUILD_LD
141 # @USAGE: [toolchain prefix]
142 # @RETURN: name of the linker for building binaries to run on the build machine
143 tc-getBUILD_LD() { tc-getBUILD_PROG LD ld "$@"; }
144 # @FUNCTION: tc-getBUILD_STRIP
145 # @USAGE: [toolchain prefix]
146 # @RETURN: name of the strip program for building binaries to run on the build machine
147 tc-getBUILD_STRIP() { tc-getBUILD_PROG STRIP strip "$@"; }
148 # @FUNCTION: tc-getBUILD_NM
149 # @USAGE: [toolchain prefix]
150 # @RETURN: name of the symbol/object thingy for building binaries to run on the build machine
151 tc-getBUILD_NM() { tc-getBUILD_PROG NM nm "$@"; }
152 # @FUNCTION: tc-getBUILD_RANLIB
153 # @USAGE: [toolchain prefix]
154 # @RETURN: name of the archiver indexer for building binaries to run on the build machine
155 tc-getBUILD_RANLIB() { tc-getBUILD_PROG RANLIB ranlib "$@"; }
156 # @FUNCTION: tc-getBUILD_OBJCOPY
157 # @USAGE: [toolchain prefix]
158 # @RETURN: name of the object copier for building binaries to run on the build machine
159 tc-getBUILD_OBJCOPY() { tc-getBUILD_PROG OBJCOPY objcopy "$@"; }
160 # @FUNCTION: tc-getBUILD_PKG_CONFIG
161 # @USAGE: [toolchain prefix]
162 # @RETURN: name of the pkg-config tool for building binaries to run on the build machine
163 tc-getBUILD_PKG_CONFIG() { tc-getBUILD_PROG PKG_CONFIG pkg-config "$@"; }
164
165 # @FUNCTION: tc-export
166 # @USAGE: <list of toolchain variables>
167 # @DESCRIPTION:
168 # Quick way to export a bunch of compiler vars at once.
169 tc-export() {
170         local var
171         for var in "$@" ; do
172                 [[ $(type -t tc-get${var}) != "function" ]] && die "tc-export: invalid export variable '${var}'"
173                 eval tc-get${var} > /dev/null
174         done
175 }
176
177 # @FUNCTION: tc-is-cross-compiler
178 # @RETURN: Shell true if we are using a cross-compiler, shell false otherwise
179 tc-is-cross-compiler() {
180         [[ ${CBUILD:-${CHOST}} != ${CHOST} ]]
181 }
182
183 # @FUNCTION: tc-is-softfloat
184 # @DESCRIPTION:
185 # See if this toolchain is a softfloat based one.
186 # @CODE
187 # The possible return values:
188 #  - only:   the target is always softfloat (never had fpu)
189 #  - yes:    the target should support softfloat
190 #  - softfp: (arm specific) the target should use hardfloat insns, but softfloat calling convention
191 #  - no:     the target doesn't support softfloat
192 # @CODE
193 # This allows us to react differently where packages accept
194 # softfloat flags in the case where support is optional, but
195 # rejects softfloat flags where the target always lacks an fpu.
196 tc-is-softfloat() {
197         local CTARGET=${CTARGET:-${CHOST}}
198         case ${CTARGET} in
199                 bfin*|h8300*)
200                         echo "only" ;;
201                 *)
202                         if [[ ${CTARGET//_/-} == *-softfloat-* ]] ; then
203                                 echo "yes"
204                         elif [[ ${CTARGET//_/-} == *-softfp-* ]] ; then
205                                 echo "softfp"
206                         else
207                                 echo "no"
208                         fi
209                         ;;
210         esac
211 }
212
213 # @FUNCTION: tc-is-static-only
214 # @DESCRIPTION:
215 # Return shell true if the target does not support shared libs, shell false
216 # otherwise.
217 tc-is-static-only() {
218         local host=${CTARGET:-${CHOST}}
219
220         # *MiNT doesn't have shared libraries, only platform so far
221         [[ ${host} == *-mint* ]]
222 }
223
224 # @FUNCTION: tc-export_build_env
225 # @USAGE: [compiler variables]
226 # @DESCRIPTION:
227 # Export common build related compiler settings.
228 tc-export_build_env() {
229         tc-export "$@"
230         # Some build envs will initialize vars like:
231         # : ${BUILD_LDFLAGS:-${LDFLAGS}}
232         # So make sure all variables are non-empty. #526734
233         : ${BUILD_CFLAGS:=-O1 -pipe}
234         : ${BUILD_CXXFLAGS:=-O1 -pipe}
235         : ${BUILD_CPPFLAGS:= }
236         : ${BUILD_LDFLAGS:= }
237         export BUILD_{C,CXX,CPP,LD}FLAGS
238
239         # Some packages use XXX_FOR_BUILD.
240         local v
241         for v in BUILD_{C,CXX,CPP,LD}FLAGS ; do
242                 export ${v#BUILD_}_FOR_BUILD="${!v}"
243         done
244 }
245
246 # @FUNCTION: tc-env_build
247 # @USAGE: <command> [command args]
248 # @INTERNAL
249 # @DESCRIPTION:
250 # Setup the compile environment to the build tools and then execute the
251 # specified command.  We use tc-getBUILD_XX here so that we work with
252 # all of the semi-[non-]standard env vars like $BUILD_CC which often
253 # the target build system does not check.
254 tc-env_build() {
255         tc-export_build_env
256         CFLAGS=${BUILD_CFLAGS} \
257         CXXFLAGS=${BUILD_CXXFLAGS} \
258         CPPFLAGS=${BUILD_CPPFLAGS} \
259         LDFLAGS=${BUILD_LDFLAGS} \
260         AR=$(tc-getBUILD_AR) \
261         AS=$(tc-getBUILD_AS) \
262         CC=$(tc-getBUILD_CC) \
263         CPP=$(tc-getBUILD_CPP) \
264         CXX=$(tc-getBUILD_CXX) \
265         LD=$(tc-getBUILD_LD) \
266         NM=$(tc-getBUILD_NM) \
267         PKG_CONFIG=$(tc-getBUILD_PKG_CONFIG) \
268         RANLIB=$(tc-getBUILD_RANLIB) \
269         "$@"
270 }
271
272 # @FUNCTION: econf_build
273 # @USAGE: [econf flags]
274 # @DESCRIPTION:
275 # Sometimes we need to locally build up some tools to run on CBUILD because
276 # the package has helper utils which are compiled+executed when compiling.
277 # This won't work when cross-compiling as the CHOST is set to a target which
278 # we cannot natively execute.
279 #
280 # For example, the python package will build up a local python binary using
281 # a portable build system (configure+make), but then use that binary to run
282 # local python scripts to build up other components of the overall python.
283 # We cannot rely on the python binary in $PATH as that often times will be
284 # a different version, or not even installed in the first place.  Instead,
285 # we compile the code in a different directory to run on CBUILD, and then
286 # use that binary when compiling the main package to run on CHOST.
287 #
288 # For example, with newer EAPIs, you'd do something like:
289 # @CODE
290 # src_configure() {
291 #       ECONF_SOURCE=${S}
292 #       if tc-is-cross-compiler ; then
293 #               mkdir "${WORKDIR}"/${CBUILD}
294 #               pushd "${WORKDIR}"/${CBUILD} >/dev/null
295 #               econf_build --disable-some-unused-stuff
296 #               popd >/dev/null
297 #       fi
298 #       ... normal build paths ...
299 # }
300 # src_compile() {
301 #       if tc-is-cross-compiler ; then
302 #               pushd "${WORKDIR}"/${CBUILD} >/dev/null
303 #               emake one-or-two-build-tools
304 #               ln/mv build-tools to normal build paths in ${S}/
305 #               popd >/dev/null
306 #       fi
307 #       ... normal build paths ...
308 # }
309 # @CODE
310 econf_build() {
311         local CBUILD=${CBUILD:-${CHOST}}
312         tc-env_build econf --build=${CBUILD} --host=${CBUILD} "$@"
313 }
314
315 # @FUNCTION: tc-ld-is-gold
316 # @USAGE: [toolchain prefix]
317 # @DESCRIPTION:
318 # Return true if the current linker is set to gold.
319 tc-ld-is-gold() {
320         local out
321
322         # First check the linker directly.
323         out=$($(tc-getLD "$@") --version 2>&1)
324         if [[ ${out} == *"GNU gold"* ]] ; then
325                 return 0
326         fi
327
328         # Then see if they're selecting gold via compiler flags.
329         # Note: We're assuming they're using LDFLAGS to hold the
330         # options and not CFLAGS/CXXFLAGS.
331         local base="${T}/test-tc-gold"
332         cat <<-EOF > "${base}.c"
333         int main() { return 0; }
334         EOF
335         out=$($(tc-getCC "$@") ${CFLAGS} ${CPPFLAGS} ${LDFLAGS} -Wl,--version "${base}.c" -o "${base}" 2>&1)
336         rm -f "${base}"*
337         if [[ ${out} == *"GNU gold"* ]] ; then
338                 return 0
339         fi
340
341         # No gold here!
342         return 1
343 }
344
345 # @FUNCTION: tc-ld-disable-gold
346 # @USAGE: [toolchain prefix]
347 # @DESCRIPTION:
348 # If the gold linker is currently selected, configure the compilation
349 # settings so that we use the older bfd linker instead.
350 tc-ld-disable-gold() {
351         if ! tc-ld-is-gold "$@" ; then
352                 # They aren't using gold, so nothing to do!
353                 return
354         fi
355
356         ewarn "Forcing usage of the BFD linker instead of GOLD"
357
358         # Set up LD to point directly to bfd if it's available.
359         # We need to extract the first word in case there are flags appended
360         # to its value (like multilib).  #545218
361         local ld=$(tc-getLD "$@")
362         local bfd_ld="${ld%% *}.bfd"
363         local path_ld=$(which "${bfd_ld}" 2>/dev/null)
364         [[ -e ${path_ld} ]] && export LD=${bfd_ld}
365
366         # Set up LDFLAGS to select gold based on the gcc version.
367         local major=$(gcc-major-version "$@")
368         local minor=$(gcc-minor-version "$@")
369         if [[ ${major} -lt 4 ]] || [[ ${major} -eq 4 && ${minor} -lt 8 ]] ; then
370                 # <=gcc-4.7 requires some coercion.  Only works if bfd exists.
371                 if [[ -e ${path_ld} ]] ; then
372                         local d="${T}/bfd-linker"
373                         mkdir -p "${d}"
374                         ln -sf "${path_ld}" "${d}"/ld
375                         export LDFLAGS="${LDFLAGS} -B${d}"
376                 else
377                         die "unable to locate a BFD linker to bypass gold"
378                 fi
379         else
380                 # gcc-4.8+ supports -fuse-ld directly.
381                 export LDFLAGS="${LDFLAGS} -fuse-ld=bfd"
382         fi
383 }
384
385 # @FUNCTION: tc-has-openmp
386 # @USAGE: [toolchain prefix]
387 # @DESCRIPTION:
388 # See if the toolchain supports OpenMP.
389 tc-has-openmp() {
390         local base="${T}/test-tc-openmp"
391         cat <<-EOF > "${base}.c"
392         #include <omp.h>
393         int main() {
394                 int nthreads, tid, ret = 0;
395                 #pragma omp parallel private(nthreads, tid)
396                 {
397                 tid = omp_get_thread_num();
398                 nthreads = omp_get_num_threads(); ret += tid + nthreads;
399                 }
400                 return ret;
401         }
402         EOF
403         $(tc-getCC "$@") -fopenmp "${base}.c" -o "${base}" >&/dev/null
404         local ret=$?
405         rm -f "${base}"*
406         return ${ret}
407 }
408
409 # @FUNCTION: tc-has-tls
410 # @USAGE: [-s|-c|-l] [toolchain prefix]
411 # @DESCRIPTION:
412 # See if the toolchain supports thread local storage (TLS).  Use -s to test the
413 # compiler, -c to also test the assembler, and -l to also test the C library
414 # (the default).
415 tc-has-tls() {
416         local base="${T}/test-tc-tls"
417         cat <<-EOF > "${base}.c"
418         int foo(int *i) {
419                 static __thread int j = 0;
420                 return *i ? j : *i;
421         }
422         EOF
423         local flags
424         case $1 in
425                 -s) flags="-S";;
426                 -c) flags="-c";;
427                 -l) ;;
428                 -*) die "Usage: tc-has-tls [-c|-l] [toolchain prefix]";;
429         esac
430         : ${flags:=-fPIC -shared -Wl,-z,defs}
431         [[ $1 == -* ]] && shift
432         $(tc-getCC "$@") ${flags} "${base}.c" -o "${base}" >&/dev/null
433         local ret=$?
434         rm -f "${base}"*
435         return ${ret}
436 }
437
438
439 # Parse information from CBUILD/CHOST/CTARGET rather than
440 # use external variables from the profile.
441 tc-ninja_magic_to_arch() {
442 ninj() { [[ ${type} == "kern" ]] && echo $1 || echo $2 ; }
443
444         local type=$1
445         local host=$2
446         [[ -z ${host} ]] && host=${CTARGET:-${CHOST}}
447
448         local KV=${KV:-${KV_FULL}}
449         [[ ${type} == "kern" ]] && [[ -z ${KV} ]] && \
450         ewarn "QA: Kernel version could not be determined, please inherit kernel-2 or linux-info"
451
452         case ${host} in
453                 aarch64*)       echo arm64;;
454                 alpha*)         echo alpha;;
455                 arm*)           echo arm;;
456                 avr*)           ninj avr32 avr;;
457                 bfin*)          ninj blackfin bfin;;
458                 c6x*)           echo c6x;;
459                 cris*)          echo cris;;
460                 frv*)           echo frv;;
461                 hexagon*)       echo hexagon;;
462                 hppa*)          ninj parisc hppa;;
463                 i?86*)
464                         # Starting with linux-2.6.24, the 'x86_64' and 'i386'
465                         # trees have been unified into 'x86'.
466                         # FreeBSD still uses i386
467                         if [[ ${type} == "kern" ]] && [[ $(KV_to_int ${KV}) -lt $(KV_to_int 2.6.24) || ${host} == *freebsd* ]] ; then
468                                 echo i386
469                         else
470                                 echo x86
471                         fi
472                         ;;
473                 ia64*)          echo ia64;;
474                 m68*)           echo m68k;;
475                 metag*)         echo metag;;
476                 microblaze*)    echo microblaze;;
477                 mips*)          echo mips;;
478                 nios2*)         echo nios2;;
479                 nios*)          echo nios;;
480                 or32*)          echo openrisc;;
481                 powerpc*)
482                         # Starting with linux-2.6.15, the 'ppc' and 'ppc64' trees
483                         # have been unified into simply 'powerpc', but until 2.6.16,
484                         # ppc32 is still using ARCH="ppc" as default
485                         if [[ ${type} == "kern" ]] && [[ $(KV_to_int ${KV}) -ge $(KV_to_int 2.6.16) ]] ; then
486                                 echo powerpc
487                         elif [[ ${type} == "kern" ]] && [[ $(KV_to_int ${KV}) -eq $(KV_to_int 2.6.15) ]] ; then
488                                 if [[ ${host} == powerpc64* ]] || [[ ${PROFILE_ARCH} == "ppc64" ]] ; then
489                                         echo powerpc
490                                 else
491                                         echo ppc
492                                 fi
493                         elif [[ ${host} == powerpc64* ]] ; then
494                                 echo ppc64
495                         elif [[ ${PROFILE_ARCH} == "ppc64" ]] ; then
496                                 ninj ppc64 ppc
497                         else
498                                 echo ppc
499                         fi
500                         ;;
501                 riscv*)         echo riscv;;
502                 s390*)          echo s390;;
503                 score*)         echo score;;
504                 sh64*)          ninj sh64 sh;;
505                 sh*)            echo sh;;
506                 sparc64*)       ninj sparc64 sparc;;
507                 sparc*)         [[ ${PROFILE_ARCH} == "sparc64" ]] \
508                                                 && ninj sparc64 sparc \
509                                                 || echo sparc
510                                         ;;
511                 tile*)          echo tile;;
512                 vax*)           echo vax;;
513                 x86_64*freebsd*) echo amd64;;
514                 x86_64*)
515                         # Starting with linux-2.6.24, the 'x86_64' and 'i386'
516                         # trees have been unified into 'x86'.
517                         if [[ ${type} == "kern" ]] && [[ $(KV_to_int ${KV}) -ge $(KV_to_int 2.6.24) ]] ; then
518                                 echo x86
519                         else
520                                 ninj x86_64 amd64
521                         fi
522                         ;;
523                 xtensa*)        echo xtensa;;
524
525                 # since our usage of tc-arch is largely concerned with
526                 # normalizing inputs for testing ${CTARGET}, let's filter
527                 # other cross targets (mingw and such) into the unknown.
528                 *)                      echo unknown;;
529         esac
530 }
531 # @FUNCTION: tc-arch-kernel
532 # @USAGE: [toolchain prefix]
533 # @RETURN: name of the kernel arch according to the compiler target
534 tc-arch-kernel() {
535         tc-ninja_magic_to_arch kern "$@"
536 }
537 # @FUNCTION: tc-arch
538 # @USAGE: [toolchain prefix]
539 # @RETURN: name of the portage arch according to the compiler target
540 tc-arch() {
541         tc-ninja_magic_to_arch portage "$@"
542 }
543
544 tc-endian() {
545         local host=$1
546         [[ -z ${host} ]] && host=${CTARGET:-${CHOST}}
547         host=${host%%-*}
548
549         case ${host} in
550                 aarch64*be)     echo big;;
551                 aarch64)        echo little;;
552                 alpha*)         echo big;;
553                 arm*b*)         echo big;;
554                 arm*)           echo little;;
555                 cris*)          echo little;;
556                 hppa*)          echo big;;
557                 i?86*)          echo little;;
558                 ia64*)          echo little;;
559                 m68*)           echo big;;
560                 mips*l*)        echo little;;
561                 mips*)          echo big;;
562                 powerpc*le)     echo little;;
563                 powerpc*)       echo big;;
564                 s390*)          echo big;;
565                 sh*b*)          echo big;;
566                 sh*)            echo little;;
567                 sparc*)         echo big;;
568                 x86_64*)        echo little;;
569                 *)                      echo wtf;;
570         esac
571 }
572
573 # Internal func.  The first argument is the version info to expand.
574 # Query the preprocessor to improve compatibility across different
575 # compilers rather than maintaining a --version flag matrix. #335943
576 _gcc_fullversion() {
577         local ver="$1"; shift
578         set -- `$(tc-getCPP "$@") -E -P - <<<"__GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__"`
579         eval echo "$ver"
580 }
581
582 # @FUNCTION: gcc-fullversion
583 # @RETURN: compiler version (major.minor.micro: [3.4.6])
584 gcc-fullversion() {
585         _gcc_fullversion '$1.$2.$3' "$@"
586 }
587 # @FUNCTION: gcc-version
588 # @RETURN: compiler version (major.minor: [3.4].6)
589 gcc-version() {
590         _gcc_fullversion '$1.$2' "$@"
591 }
592 # @FUNCTION: gcc-major-version
593 # @RETURN: major compiler version (major: [3].4.6)
594 gcc-major-version() {
595         _gcc_fullversion '$1' "$@"
596 }
597 # @FUNCTION: gcc-minor-version
598 # @RETURN: minor compiler version (minor: 3.[4].6)
599 gcc-minor-version() {
600         _gcc_fullversion '$2' "$@"
601 }
602 # @FUNCTION: gcc-micro-version
603 # @RETURN: micro compiler version (micro: 3.4.[6])
604 gcc-micro-version() {
605         _gcc_fullversion '$3' "$@"
606 }
607
608 # Returns the installation directory - internal toolchain
609 # function for use by _gcc-specs-exists (for flag-o-matic).
610 _gcc-install-dir() {
611         echo "$(LC_ALL=C $(tc-getCC) -print-search-dirs 2> /dev/null |\
612                 awk '$1=="install:" {print $2}')"
613 }
614 # Returns true if the indicated specs file exists - internal toolchain
615 # function for use by flag-o-matic.
616 _gcc-specs-exists() {
617         [[ -f $(_gcc-install-dir)/$1 ]]
618 }
619
620 # Returns requested gcc specs directive unprocessed - for used by
621 # gcc-specs-directive()
622 # Note; later specs normally overwrite earlier ones; however if a later
623 # spec starts with '+' then it appends.
624 # gcc -dumpspecs is parsed first, followed by files listed by "gcc -v"
625 # as "Reading <file>", in order.  Strictly speaking, if there's a
626 # $(gcc_install_dir)/specs, the built-in specs aren't read, however by
627 # the same token anything from 'gcc -dumpspecs' is overridden by
628 # the contents of $(gcc_install_dir)/specs so the result is the
629 # same either way.
630 _gcc-specs-directive_raw() {
631         local cc=$(tc-getCC)
632         local specfiles=$(LC_ALL=C ${cc} -v 2>&1 | awk '$1=="Reading" {print $NF}')
633         ${cc} -dumpspecs 2> /dev/null | cat - ${specfiles} | awk -v directive=$1 \
634 'BEGIN  { pspec=""; spec=""; outside=1 }
635 $1=="*"directive":"  { pspec=spec; spec=""; outside=0; next }
636         outside || NF==0 || ( substr($1,1,1)=="*" && substr($1,length($1),1)==":" ) { outside=1; next }
637         spec=="" && substr($0,1,1)=="+" { spec=pspec " " substr($0,2); next }
638         { spec=spec $0 }
639 END     { print spec }'
640         return 0
641 }
642
643 # Return the requested gcc specs directive, with all included
644 # specs expanded.
645 # Note, it does not check for inclusion loops, which cause it
646 # to never finish - but such loops are invalid for gcc and we're
647 # assuming gcc is operational.
648 gcc-specs-directive() {
649         local directive subdname subdirective
650         directive="$(_gcc-specs-directive_raw $1)"
651         while [[ ${directive} == *%\(*\)* ]]; do
652                 subdname=${directive/*%\(}
653                 subdname=${subdname/\)*}
654                 subdirective="$(_gcc-specs-directive_raw ${subdname})"
655                 directive="${directive//\%(${subdname})/${subdirective}}"
656         done
657         echo "${directive}"
658         return 0
659 }
660
661 # Returns true if gcc sets relro
662 gcc-specs-relro() {
663         local directive
664         directive=$(gcc-specs-directive link_command)
665         [[ "${directive/\{!norelro:}" != "${directive}" ]]
666 }
667 # Returns true if gcc sets now
668 gcc-specs-now() {
669         local directive
670         directive=$(gcc-specs-directive link_command)
671         [[ "${directive/\{!nonow:}" != "${directive}" ]]
672 }
673 # Returns true if gcc builds PIEs
674 gcc-specs-pie() {
675         local directive
676         directive=$(gcc-specs-directive cc1)
677         [[ "${directive/\{!nopie:}" != "${directive}" ]]
678 }
679 # Returns true if gcc builds with the stack protector
680 gcc-specs-ssp() {
681         local directive
682         directive=$(gcc-specs-directive cc1)
683         [[ "${directive/\{!fno-stack-protector:}" != "${directive}" ]]
684 }
685 # Returns true if gcc upgrades fstack-protector to fstack-protector-all
686 gcc-specs-ssp-to-all() {
687         local directive
688         directive=$(gcc-specs-directive cc1)
689         [[ "${directive/\{!fno-stack-protector-all:}" != "${directive}" ]]
690 }
691 # Returns true if gcc builds with fno-strict-overflow
692 gcc-specs-nostrict() {
693         local directive
694         directive=$(gcc-specs-directive cc1)
695         [[ "${directive/\{!fstrict-overflow:}" != "${directive}" ]]
696 }
697 # Returns true if gcc builds with fstack-check
698 gcc-specs-stack-check() {
699         local directive
700         directive=$(gcc-specs-directive cc1)
701         [[ "${directive/\{!fno-stack-check:}" != "${directive}" ]]
702 }
703
704
705 # @FUNCTION: gen_usr_ldscript
706 # @USAGE: [-a] <list of libs to create linker scripts for>
707 # @DESCRIPTION:
708 # This function generate linker scripts in /usr/lib for dynamic
709 # libs in /lib.  This is to fix linking problems when you have
710 # the .so in /lib, and the .a in /usr/lib.  What happens is that
711 # in some cases when linking dynamic, the .a in /usr/lib is used
712 # instead of the .so in /lib due to gcc/libtool tweaking ld's
713 # library search path.  This causes many builds to fail.
714 # See bug #4411 for more info.
715 #
716 # Note that you should in general use the unversioned name of
717 # the library (libfoo.so), as ldconfig should usually update it
718 # correctly to point to the latest version of the library present.
719 gen_usr_ldscript() {
720         local lib libdir=$(get_libdir) output_format="" auto=false suffix=$(get_libname)
721         [[ -z ${ED+set} ]] && local ED=${D%/}${EPREFIX}/
722
723         tc-is-static-only && return
724
725         # Eventually we'd like to get rid of this func completely #417451
726         case ${CTARGET:-${CHOST}} in
727         *-darwin*) ;;
728         *-android*) return 0 ;;
729         *linux*|*-freebsd*|*-openbsd*|*-netbsd*)
730                 use prefix && return 0 ;;
731         *) return 0 ;;
732         esac
733
734         # Just make sure it exists
735         dodir /usr/${libdir}
736
737         if [[ $1 == "-a" ]] ; then
738                 auto=true
739                 shift
740                 dodir /${libdir}
741         fi
742
743         # OUTPUT_FORMAT gives hints to the linker as to what binary format
744         # is referenced ... makes multilib saner
745         local flags=( ${CFLAGS} ${LDFLAGS} -Wl,--verbose )
746         if $(tc-getLD) --version | grep -q 'GNU gold' ; then
747                 # If they're using gold, manually invoke the old bfd. #487696
748                 local d="${T}/bfd-linker"
749                 mkdir -p "${d}"
750                 ln -sf $(which ${CHOST}-ld.bfd) "${d}"/ld
751                 flags+=( -B"${d}" )
752         fi
753         output_format=$($(tc-getCC) "${flags[@]}" 2>&1 | sed -n 's/^OUTPUT_FORMAT("\([^"]*\)",.*/\1/p')
754         [[ -n ${output_format} ]] && output_format="OUTPUT_FORMAT ( ${output_format} )"
755
756         for lib in "$@" ; do
757                 local tlib
758                 if ${auto} ; then
759                         lib="lib${lib}${suffix}"
760                 else
761                         # Ensure /lib/${lib} exists to avoid dangling scripts/symlinks.
762                         # This especially is for AIX where $(get_libname) can return ".a",
763                         # so /lib/${lib} might be moved to /usr/lib/${lib} (by accident).
764                         [[ -r ${ED}/${libdir}/${lib} ]] || continue
765                         #TODO: better die here?
766                 fi
767
768                 case ${CTARGET:-${CHOST}} in
769                 *-darwin*)
770                         if ${auto} ; then
771                                 tlib=$(scanmacho -qF'%S#F' "${ED}"/usr/${libdir}/${lib})
772                         else
773                                 tlib=$(scanmacho -qF'%S#F' "${ED}"/${libdir}/${lib})
774                         fi
775                         [[ -z ${tlib} ]] && die "unable to read install_name from ${lib}"
776                         tlib=${tlib##*/}
777
778                         if ${auto} ; then
779                                 mv "${ED}"/usr/${libdir}/${lib%${suffix}}.*${suffix#.} "${ED}"/${libdir}/ || die
780                                 # some install_names are funky: they encode a version
781                                 if [[ ${tlib} != ${lib%${suffix}}.*${suffix#.} ]] ; then
782                                         mv "${ED}"/usr/${libdir}/${tlib%${suffix}}.*${suffix#.} "${ED}"/${libdir}/ || die
783                                 fi
784                                 rm -f "${ED}"/${libdir}/${lib}
785                         fi
786
787                         # Mach-O files have an id, which is like a soname, it tells how
788                         # another object linking against this lib should reference it.
789                         # Since we moved the lib from usr/lib into lib this reference is
790                         # wrong.  Hence, we update it here.  We don't configure with
791                         # libdir=/lib because that messes up libtool files.
792                         # Make sure we don't lose the specific version, so just modify the
793                         # existing install_name
794                         if [[ ! -w "${ED}/${libdir}/${tlib}" ]] ; then
795                                 chmod u+w "${ED}${libdir}/${tlib}" # needed to write to it
796                                 local nowrite=yes
797                         fi
798                         install_name_tool \
799                                 -id "${EPREFIX}"/${libdir}/${tlib} \
800                                 "${ED}"/${libdir}/${tlib} || die "install_name_tool failed"
801                         [[ -n ${nowrite} ]] && chmod u-w "${ED}${libdir}/${tlib}"
802                         # Now as we don't use GNU binutils and our linker doesn't
803                         # understand linker scripts, just create a symlink.
804                         pushd "${ED}/usr/${libdir}" > /dev/null
805                         ln -snf "../../${libdir}/${tlib}" "${lib}"
806                         popd > /dev/null
807                         ;;
808                 *)
809                         if ${auto} ; then
810                                 tlib=$(scanelf -qF'%S#F' "${ED}"/usr/${libdir}/${lib})
811                                 [[ -z ${tlib} ]] && die "unable to read SONAME from ${lib}"
812                                 mv "${ED}"/usr/${libdir}/${lib}* "${ED}"/${libdir}/ || die
813                                 # some SONAMEs are funky: they encode a version before the .so
814                                 if [[ ${tlib} != ${lib}* ]] ; then
815                                         mv "${ED}"/usr/${libdir}/${tlib}* "${ED}"/${libdir}/ || die
816                                 fi
817                                 rm -f "${ED}"/${libdir}/${lib}
818                         else
819                                 tlib=${lib}
820                         fi
821                         cat > "${ED}/usr/${libdir}/${lib}" <<-END_LDSCRIPT
822                         /* GNU ld script
823                            Since Gentoo has critical dynamic libraries in /lib, and the static versions
824                            in /usr/lib, we need to have a "fake" dynamic lib in /usr/lib, otherwise we
825                            run into linking problems.  This "fake" dynamic lib is a linker script that
826                            redirects the linker to the real lib.  And yes, this works in the cross-
827                            compiling scenario as the sysroot-ed linker will prepend the real path.
828
829                            See bug https://bugs.gentoo.org/4411 for more info.
830                          */
831                         ${output_format}
832                         GROUP ( ${EPREFIX}/${libdir}/${tlib} )
833                         END_LDSCRIPT
834                         ;;
835                 esac
836                 fperms a+x "/usr/${libdir}/${lib}" || die "could not change perms on ${lib}"
837         done
838 }
839
840 fi