1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: toolchain-funcs.eclass
7 # Toolchain Ninjas <toolchain@gentoo.org>
8 # @BLURB: functions to query common info about the toolchain
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
16 if [[ -z ${_TOOLCHAIN_FUNCS_ECLASS} ]]; then
17 _TOOLCHAIN_FUNCS_ECLASS=1
21 # tc-getPROG <VAR [search vars]> <default> [tuple]
29 if [[ -n ${!v} ]] ; then
37 [[ -n $4 ]] && search=$(type -p "$4-${prog}")
38 [[ -z ${search} && -n ${!tuple} ]] && search=$(type -p "${!tuple}-${prog}")
39 [[ -n ${search} ]] && prog=${search##*/}
44 tc-getBUILD_PROG() { _tc-getPROG CBUILD "BUILD_$1 $1_FOR_BUILD HOST$1" "${@:2}"; }
45 tc-getPROG() { _tc-getPROG CHOST "$@"; }
48 # @USAGE: [toolchain prefix]
49 # @RETURN: name of the archiver
50 tc-getAR() { tc-getPROG AR ar "$@"; }
52 # @USAGE: [toolchain prefix]
53 # @RETURN: name of the assembler
54 tc-getAS() { tc-getPROG AS as "$@"; }
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++ "$@"; }
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 "$@"; }
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 "$@"; }
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 "$@"; }
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 "$@"; }
165 # @FUNCTION: tc-export
166 # @USAGE: <list of toolchain variables>
168 # Quick way to export a bunch of compiler vars at once.
172 [[ $(type -t tc-get${var}) != "function" ]] && die "tc-export: invalid export variable '${var}'"
173 eval tc-get${var} > /dev/null
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} ]]
183 # @FUNCTION: tc-is-softfloat
185 # See if this toolchain is a softfloat based one.
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
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.
197 local CTARGET=${CTARGET:-${CHOST}}
202 if [[ ${CTARGET//_/-} == *-softfloat-* ]] ; then
204 elif [[ ${CTARGET//_/-} == *-softfp-* ]] ; then
213 # @FUNCTION: tc-is-static-only
215 # Return shell true if the target does not support shared libs, shell false
217 tc-is-static-only() {
218 local host=${CTARGET:-${CHOST}}
220 # *MiNT doesn't have shared libraries, only platform so far
221 [[ ${host} == *-mint* ]]
224 # @FUNCTION: tc-export_build_env
225 # @USAGE: [compiler variables]
227 # Export common build related compiler settings.
228 tc-export_build_env() {
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
239 # Some packages use XXX_FOR_BUILD.
241 for v in BUILD_{C,CXX,CPP,LD}FLAGS ; do
242 export ${v#BUILD_}_FOR_BUILD="${!v}"
246 # @FUNCTION: tc-env_build
247 # @USAGE: <command> [command args]
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.
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) \
272 # @FUNCTION: econf_build
273 # @USAGE: [econf flags]
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.
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.
288 # For example, with newer EAPIs, you'd do something like:
292 # if tc-is-cross-compiler ; then
293 # mkdir "${WORKDIR}"/${CBUILD}
294 # pushd "${WORKDIR}"/${CBUILD} >/dev/null
295 # econf_build --disable-some-unused-stuff
298 # ... normal build paths ...
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}/
307 # ... normal build paths ...
311 local CBUILD=${CBUILD:-${CHOST}}
312 tc-env_build econf --build=${CBUILD} --host=${CBUILD} "$@"
315 # @FUNCTION: tc-ld-is-gold
316 # @USAGE: [toolchain prefix]
318 # Return true if the current linker is set to gold.
322 # First check the linker directly.
323 out=$($(tc-getLD "$@") --version 2>&1)
324 if [[ ${out} == *"GNU gold"* ]] ; then
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; }
335 out=$($(tc-getCC "$@") ${CFLAGS} ${CPPFLAGS} ${LDFLAGS} -Wl,--version "${base}.c" -o "${base}" 2>&1)
337 if [[ ${out} == *"GNU gold"* ]] ; then
345 # @FUNCTION: tc-ld-disable-gold
346 # @USAGE: [toolchain prefix]
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!
356 ewarn "Forcing usage of the BFD linker instead of GOLD"
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}
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"
374 ln -sf "${path_ld}" "${d}"/ld
375 export LDFLAGS="${LDFLAGS} -B${d}"
377 die "unable to locate a BFD linker to bypass gold"
380 # gcc-4.8+ supports -fuse-ld directly.
381 export LDFLAGS="${LDFLAGS} -fuse-ld=bfd"
385 # @FUNCTION: tc-has-openmp
386 # @USAGE: [toolchain prefix]
388 # See if the toolchain supports OpenMP.
390 local base="${T}/test-tc-openmp"
391 cat <<-EOF > "${base}.c"
394 int nthreads, tid, ret = 0;
395 #pragma omp parallel private(nthreads, tid)
397 tid = omp_get_thread_num();
398 nthreads = omp_get_num_threads(); ret += tid + nthreads;
403 $(tc-getCC "$@") -fopenmp "${base}.c" -o "${base}" >&/dev/null
409 # @FUNCTION: tc-has-tls
410 # @USAGE: [-s|-c|-l] [toolchain prefix]
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
416 local base="${T}/test-tc-tls"
417 cat <<-EOF > "${base}.c"
419 static __thread int j = 0;
428 -*) die "Usage: tc-has-tls [-c|-l] [toolchain prefix]";;
430 : ${flags:=-fPIC -shared -Wl,-z,defs}
431 [[ $1 == -* ]] && shift
432 $(tc-getCC "$@") ${flags} "${base}.c" -o "${base}" >&/dev/null
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 ; }
446 [[ -z ${host} ]] && host=${CTARGET:-${CHOST}}
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"
453 aarch64*) echo arm64;;
456 avr*) ninj avr32 avr;;
457 bfin*) ninj blackfin bfin;;
461 hexagon*) echo hexagon;;
462 hppa*) ninj parisc hppa;;
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
476 microblaze*) echo microblaze;;
480 or32*) echo openrisc;;
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
487 elif [[ ${type} == "kern" ]] && [[ $(KV_to_int ${KV}) -eq $(KV_to_int 2.6.15) ]] ; then
488 if [[ ${host} == powerpc64* ]] || [[ ${PROFILE_ARCH} == "ppc64" ]] ; then
493 elif [[ ${host} == powerpc64* ]] ; then
495 elif [[ ${PROFILE_ARCH} == "ppc64" ]] ; then
504 sh64*) ninj sh64 sh;;
506 sparc64*) ninj sparc64 sparc;;
507 sparc*) [[ ${PROFILE_ARCH} == "sparc64" ]] \
508 && ninj sparc64 sparc \
513 x86_64*freebsd*) echo amd64;;
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
523 xtensa*) echo xtensa;;
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.
531 # @FUNCTION: tc-arch-kernel
532 # @USAGE: [toolchain prefix]
533 # @RETURN: name of the kernel arch according to the compiler target
535 tc-ninja_magic_to_arch kern "$@"
538 # @USAGE: [toolchain prefix]
539 # @RETURN: name of the portage arch according to the compiler target
541 tc-ninja_magic_to_arch portage "$@"
546 [[ -z ${host} ]] && host=${CTARGET:-${CHOST}}
550 aarch64*be) echo big;;
551 aarch64) echo little;;
560 mips*l*) echo little;;
562 powerpc*le) echo little;;
568 x86_64*) echo little;;
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
577 local ver="$1"; shift
578 set -- `$(tc-getCPP "$@") -E -P - <<<"__GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__"`
582 # @FUNCTION: gcc-fullversion
583 # @RETURN: compiler version (major.minor.micro: [3.4.6])
585 _gcc_fullversion '$1.$2.$3' "$@"
587 # @FUNCTION: gcc-version
588 # @RETURN: compiler version (major.minor: [3.4].6)
590 _gcc_fullversion '$1.$2' "$@"
592 # @FUNCTION: gcc-major-version
593 # @RETURN: major compiler version (major: [3].4.6)
594 gcc-major-version() {
595 _gcc_fullversion '$1' "$@"
597 # @FUNCTION: gcc-minor-version
598 # @RETURN: minor compiler version (minor: 3.[4].6)
599 gcc-minor-version() {
600 _gcc_fullversion '$2' "$@"
602 # @FUNCTION: gcc-micro-version
603 # @RETURN: micro compiler version (micro: 3.4.[6])
604 gcc-micro-version() {
605 _gcc_fullversion '$3' "$@"
608 # Returns the installation directory - internal toolchain
609 # function for use by _gcc-specs-exists (for flag-o-matic).
611 echo "$(LC_ALL=C $(tc-getCC) -print-search-dirs 2> /dev/null |\
612 awk '$1=="install:" {print $2}')"
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 ]]
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
630 _gcc-specs-directive_raw() {
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 }
643 # Return the requested gcc specs directive, with all included
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}}"
661 # Returns true if gcc sets relro
664 directive=$(gcc-specs-directive link_command)
665 [[ "${directive/\{!norelro:}" != "${directive}" ]]
667 # Returns true if gcc sets now
670 directive=$(gcc-specs-directive link_command)
671 [[ "${directive/\{!nonow:}" != "${directive}" ]]
673 # Returns true if gcc builds PIEs
676 directive=$(gcc-specs-directive cc1)
677 [[ "${directive/\{!nopie:}" != "${directive}" ]]
679 # Returns true if gcc builds with the stack protector
682 directive=$(gcc-specs-directive cc1)
683 [[ "${directive/\{!fno-stack-protector:}" != "${directive}" ]]
685 # Returns true if gcc upgrades fstack-protector to fstack-protector-all
686 gcc-specs-ssp-to-all() {
688 directive=$(gcc-specs-directive cc1)
689 [[ "${directive/\{!fno-stack-protector-all:}" != "${directive}" ]]
691 # Returns true if gcc builds with fno-strict-overflow
692 gcc-specs-nostrict() {
694 directive=$(gcc-specs-directive cc1)
695 [[ "${directive/\{!fstrict-overflow:}" != "${directive}" ]]
697 # Returns true if gcc builds with fstack-check
698 gcc-specs-stack-check() {
700 directive=$(gcc-specs-directive cc1)
701 [[ "${directive/\{!fno-stack-check:}" != "${directive}" ]]
705 # @FUNCTION: gen_usr_ldscript
706 # @USAGE: [-a] <list of libs to create linker scripts for>
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.
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.
720 local lib libdir=$(get_libdir) output_format="" auto=false suffix=$(get_libname)
721 [[ -z ${ED+set} ]] && local ED=${D%/}${EPREFIX}/
723 tc-is-static-only && return
725 # Eventually we'd like to get rid of this func completely #417451
726 case ${CTARGET:-${CHOST}} in
728 *-android*) return 0 ;;
729 *linux*|*-freebsd*|*-openbsd*|*-netbsd*)
730 use prefix && return 0 ;;
734 # Just make sure it exists
737 if [[ $1 == "-a" ]] ; then
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"
750 ln -sf $(which ${CHOST}-ld.bfd) "${d}"/ld
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} )"
759 lib="lib${lib}${suffix}"
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?
768 case ${CTARGET:-${CHOST}} in
771 tlib=$(scanmacho -qF'%S#F' "${ED}"/usr/${libdir}/${lib})
773 tlib=$(scanmacho -qF'%S#F' "${ED}"/${libdir}/${lib})
775 [[ -z ${tlib} ]] && die "unable to read install_name from ${lib}"
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
784 rm -f "${ED}"/${libdir}/${lib}
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
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}"
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
817 rm -f "${ED}"/${libdir}/${lib}
821 cat > "${ED}/usr/${libdir}/${lib}" <<-END_LDSCRIPT
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.
829 See bug https://bugs.gentoo.org/4411 for more info.
832 GROUP ( ${EPREFIX}/${libdir}/${tlib} )
836 fperms a+x "/usr/${libdir}/${lib}" || die "could not change perms on ${lib}"