/[gentoo-x86]/eclass/toolchain-funcs.eclass
Gentoo

Diff of /eclass/toolchain-funcs.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.1.43  
changed lines
  Added in v.1.108

  ViewVC Help
Powered by ViewVC 1.1.20