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

Contents of /eclass/toolchain-funcs.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.83 - (show annotations) (download)
Thu Jan 8 09:56:00 2009 UTC (5 years, 7 months ago) by gengor
Branch: MAIN
Changes since 1.82: +1 -4 lines
Revert changes to hardened funcs for now

1 # Copyright 1999-2007 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Header: /var/cvsroot/gentoo-x86/eclass/toolchain-funcs.eclass,v 1.82 2009/01/08 06:33:20 gengor Exp $
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 ___ECLASS_RECUR_TOOLCHAIN_FUNCS="yes"
17 [[ -z ${___ECLASS_RECUR_MULTILIB} ]] && inherit multilib
18
19 DESCRIPTION="Based on the ${ECLASS} eclass"
20
21 tc-getPROG() {
22 local var=$1
23 local prog=$2
24
25 if [[ -n ${!var} ]] ; then
26 echo "${!var}"
27 return 0
28 fi
29
30 local search=
31 [[ -n $3 ]] && search=$(type -p "$3-${prog}")
32 [[ -z ${search} && -n ${CHOST} ]] && search=$(type -p "${CHOST}-${prog}")
33 [[ -n ${search} ]] && prog=${search##*/}
34
35 export ${var}=${prog}
36 echo "${!var}"
37 }
38
39 # @FUNCTION: tc-getAR
40 # @USAGE: [toolchain prefix]
41 # @RETURN: name of the archiver
42 tc-getAR() { tc-getPROG AR ar "$@"; }
43 # @FUNCTION: tc-getAS
44 # @USAGE: [toolchain prefix]
45 # @RETURN: name of the assembler
46 tc-getAS() { tc-getPROG AS as "$@"; }
47 # @FUNCTION: tc-getCC
48 # @USAGE: [toolchain prefix]
49 # @RETURN: name of the C compiler
50 tc-getCC() { tc-getPROG CC gcc "$@"; }
51 # @FUNCTION: tc-getCPP
52 # @USAGE: [toolchain prefix]
53 # @RETURN: name of the C preprocessor
54 tc-getCPP() { tc-getPROG CPP cpp "$@"; }
55 # @FUNCTION: tc-getCXX
56 # @USAGE: [toolchain prefix]
57 # @RETURN: name of the C++ compiler
58 tc-getCXX() { tc-getPROG CXX g++ "$@"; }
59 # @FUNCTION: tc-getLD
60 # @USAGE: [toolchain prefix]
61 # @RETURN: name of the linker
62 tc-getLD() { tc-getPROG LD ld "$@"; }
63 # @FUNCTION: tc-getSTRIP
64 # @USAGE: [toolchain prefix]
65 # @RETURN: name of the strip program
66 tc-getSTRIP() { tc-getPROG STRIP strip "$@"; }
67 # @FUNCTION: tc-getNM
68 # @USAGE: [toolchain prefix]
69 # @RETURN: name of the symbol/object thingy
70 tc-getNM() { tc-getPROG NM nm "$@"; }
71 # @FUNCTION: tc-getRANLIB
72 # @USAGE: [toolchain prefix]
73 # @RETURN: name of the archiver indexer
74 tc-getRANLIB() { tc-getPROG RANLIB ranlib "$@"; }
75 # @FUNCTION: tc-getF77
76 # @USAGE: [toolchain prefix]
77 # @RETURN: name of the Fortran 77 compiler
78 tc-getF77() { tc-getPROG F77 f77 "$@"; }
79 # @FUNCTION: tc-getFC
80 # @USAGE: [toolchain prefix]
81 # @RETURN: name of the Fortran 90 compiler
82 tc-getFC() { tc-getPROG FC gfortran "$@"; }
83 # @FUNCTION: tc-getGCJ
84 # @USAGE: [toolchain prefix]
85 # @RETURN: name of the java compiler
86 tc-getGCJ() { tc-getPROG GCJ gcj "$@"; }
87
88 # @FUNCTION: tc-getBUILD_CC
89 # @USAGE: [toolchain prefix]
90 # @RETURN: name of the C compiler for building binaries to run on the build machine
91 tc-getBUILD_CC() {
92 local v
93 for v in CC_FOR_BUILD BUILD_CC HOSTCC ; do
94 if [[ -n ${!v} ]] ; then
95 export BUILD_CC=${!v}
96 echo "${!v}"
97 return 0
98 fi
99 done
100
101 local search=
102 if [[ -n ${CBUILD} ]] ; then
103 search=$(type -p ${CBUILD}-gcc)
104 search=${search##*/}
105 fi
106 search=${search:-gcc}
107
108 export BUILD_CC=${search}
109 echo "${search}"
110 }
111
112 # @FUNCTION: tc-export
113 # @USAGE: <list of toolchain variables>
114 # @DESCRIPTION:
115 # Quick way to export a bunch of compiler vars at once.
116 tc-export() {
117 local var
118 for var in "$@" ; do
119 [[ $(type -t tc-get${var}) != "function" ]] && die "tc-export: invalid export variable '${var}'"
120 eval tc-get${var} > /dev/null
121 done
122 }
123
124 # @FUNCTION: tc-is-cross-compiler
125 # @RETURN: Shell true if we are using a cross-compiler, shell false otherwise
126 tc-is-cross-compiler() {
127 return $([[ ${CBUILD:-${CHOST}} != ${CHOST} ]])
128 }
129
130 # @FUNCTION: tc-is-softfloat
131 # @DESCRIPTION:
132 # See if this toolchain is a softfloat based one.
133 # @CODE
134 # The possible return values:
135 # - only: the target is always softfloat (never had fpu)
136 # - yes: the target should support softfloat
137 # - no: the target should support hardfloat
138 # @CODE
139 # This allows us to react differently where packages accept
140 # softfloat flags in the case where support is optional, but
141 # rejects softfloat flags where the target always lacks an fpu.
142 tc-is-softfloat() {
143 case ${CTARGET} in
144 bfin*|h8300*)
145 echo "only" ;;
146 *)
147 [[ ${CTARGET//_/-} == *-softfloat-* ]] \
148 && echo "yes" \
149 || echo "no"
150 ;;
151 esac
152 }
153
154 # Parse information from CBUILD/CHOST/CTARGET rather than
155 # use external variables from the profile.
156 tc-ninja_magic_to_arch() {
157 ninj() { [[ ${type} == "kern" ]] && echo $1 || echo $2 ; }
158
159 local type=$1
160 local host=$2
161 [[ -z ${host} ]] && host=${CTARGET:-${CHOST}}
162
163 case ${host} in
164 alpha*) echo alpha;;
165 arm*) echo arm;;
166 avr*) ninj avr32 avr;;
167 bfin*) ninj blackfin bfin;;
168 cris*) echo cris;;
169 hppa*) ninj parisc hppa;;
170 i?86*)
171 # Starting with linux-2.6.24, the 'x86_64' and 'i386'
172 # trees have been unified into 'x86'.
173 # FreeBSD still uses i386
174 if [[ ${type} == "kern" ]] && [[ $(KV_to_int ${KV}) -lt $(KV_to_int 2.6.24) || ${host} == *freebsd* ]] ; then
175 echo i386
176 else
177 echo x86
178 fi
179 ;;
180 ia64*) echo ia64;;
181 m68*) echo m68k;;
182 mips*) echo mips;;
183 nios2*) echo nios2;;
184 nios*) echo nios;;
185 powerpc*)
186 # Starting with linux-2.6.15, the 'ppc' and 'ppc64' trees
187 # have been unified into simply 'powerpc', but until 2.6.16,
188 # ppc32 is still using ARCH="ppc" as default
189 if [[ $(KV_to_int ${KV}) -ge $(KV_to_int 2.6.16) ]] && [[ ${type} == "kern" ]] ; then
190 echo powerpc
191 elif [[ $(KV_to_int ${KV}) -eq $(KV_to_int 2.6.15) ]] && [[ ${type} == "kern" ]] ; then
192 if [[ ${host} == powerpc64* ]] || [[ ${PROFILE_ARCH} == "ppc64" ]] ; then
193 echo powerpc
194 else
195 echo ppc
196 fi
197 elif [[ ${host} == powerpc64* ]] ; then
198 echo ppc64
199 elif [[ ${PROFILE_ARCH} == "ppc64" ]] ; then
200 ninj ppc64 ppc
201 else
202 echo ppc
203 fi
204 ;;
205 s390*) echo s390;;
206 sh64*) ninj sh64 sh;;
207 sh*) echo sh;;
208 sparc64*) ninj sparc64 sparc;;
209 sparc*) [[ ${PROFILE_ARCH} == "sparc64" ]] \
210 && ninj sparc64 sparc \
211 || echo sparc
212 ;;
213 vax*) echo vax;;
214 x86_64*)
215 # Starting with linux-2.6.24, the 'x86_64' and 'i386'
216 # trees have been unified into 'x86'.
217 if [[ ${type} == "kern" ]] && [[ $(KV_to_int ${KV}) -ge $(KV_to_int 2.6.24) ]] ; then
218 echo x86
219 else
220 ninj x86_64 amd64
221 fi
222 ;;
223
224 # since our usage of tc-arch is largely concerned with
225 # normalizing inputs for testing ${CTARGET}, let's filter
226 # other cross targets (mingw and such) into the unknown.
227 *) echo unknown;;
228 esac
229 }
230 # @FUNCTION: tc-arch-kernel
231 # @USAGE: [toolchain prefix]
232 # @RETURN: name of the kernel arch according to the compiler target
233 tc-arch-kernel() {
234 tc-ninja_magic_to_arch kern "$@"
235 }
236 # @FUNCTION: tc-arch
237 # @USAGE: [toolchain prefix]
238 # @RETURN: name of the portage arch according to the compiler target
239 tc-arch() {
240 tc-ninja_magic_to_arch portage "$@"
241 }
242
243 tc-endian() {
244 local host=$1
245 [[ -z ${host} ]] && host=${CTARGET:-${CHOST}}
246 host=${host%%-*}
247
248 case ${host} in
249 alpha*) echo big;;
250 arm*b*) echo big;;
251 arm*) echo little;;
252 cris*) echo little;;
253 hppa*) echo big;;
254 i?86*) echo little;;
255 ia64*) echo little;;
256 m68*) echo big;;
257 mips*l*) echo little;;
258 mips*) echo big;;
259 powerpc*) echo big;;
260 s390*) echo big;;
261 sh*b*) echo big;;
262 sh*) echo little;;
263 sparc*) echo big;;
264 x86_64*) echo little;;
265 *) echo wtf;;
266 esac
267 }
268
269 # @FUNCTION: gcc-fullversion
270 # @RETURN: compiler version (major.minor.micro: [3.4.6])
271 gcc-fullversion() {
272 $(tc-getCC "$@") -dumpversion
273 }
274 # @FUNCTION: gcc-version
275 # @RETURN: compiler version (major.minor: [3.4].6)
276 gcc-version() {
277 gcc-fullversion "$@" | cut -f1,2 -d.
278 }
279 # @FUNCTION: gcc-major-version
280 # @RETURN: major compiler version (major: [3].4.6)
281 gcc-major-version() {
282 gcc-version "$@" | cut -f1 -d.
283 }
284 # @FUNCTION: gcc-minor-version
285 # @RETURN: minor compiler version (minor: 3.[4].6)
286 gcc-minor-version() {
287 gcc-version "$@" | cut -f2 -d.
288 }
289 # @FUNCTION: gcc-micro-version
290 # @RETURN: micro compiler version (micro: 3.4.[6])
291 gcc-micro-version() {
292 gcc-fullversion "$@" | cut -f3 -d. | cut -f1 -d-
293 }
294
295 # Returns the installation directory - internal toolchain
296 # function for use by _gcc-specs-exists (for flag-o-matic).
297 _gcc-install-dir() {
298 echo "$(LC_ALL=C $(tc-getCC) -print-search-dirs 2> /dev/null |\
299 awk '$1=="install:" {print $2}')"
300 }
301 # Returns true if the indicated specs file exists - internal toolchain
302 # function for use by flag-o-matic.
303 _gcc-specs-exists() {
304 [[ -f $(_gcc-install-dir)/$1 ]]
305 }
306
307 # Returns requested gcc specs directive unprocessed - for used by
308 # gcc-specs-directive()
309 # Note; later specs normally overwrite earlier ones; however if a later
310 # spec starts with '+' then it appends.
311 # gcc -dumpspecs is parsed first, followed by files listed by "gcc -v"
312 # as "Reading <file>", in order. Strictly speaking, if there's a
313 # $(gcc_install_dir)/specs, the built-in specs aren't read, however by
314 # the same token anything from 'gcc -dumpspecs' is overridden by
315 # the contents of $(gcc_install_dir)/specs so the result is the
316 # same either way.
317 _gcc-specs-directive_raw() {
318 local cc=$(tc-getCC)
319 local specfiles=$(LC_ALL=C ${cc} -v 2>&1 | awk '$1=="Reading" {print $NF}')
320 ${cc} -dumpspecs 2> /dev/null | cat - ${specfiles} | awk -v directive=$1 \
321 'BEGIN { pspec=""; spec=""; outside=1 }
322 $1=="*"directive":" { pspec=spec; spec=""; outside=0; next }
323 outside || NF==0 || ( substr($1,1,1)=="*" && substr($1,length($1),1)==":" ) { outside=1; next }
324 spec=="" && substr($0,1,1)=="+" { spec=pspec " " substr($0,2); next }
325 { spec=spec $0 }
326 END { print spec }'
327 return 0
328 }
329
330 # Return the requested gcc specs directive, with all included
331 # specs expanded.
332 # Note, it does not check for inclusion loops, which cause it
333 # to never finish - but such loops are invalid for gcc and we're
334 # assuming gcc is operational.
335 gcc-specs-directive() {
336 local directive subdname subdirective
337 directive="$(_gcc-specs-directive_raw $1)"
338 while [[ ${directive} == *%\(*\)* ]]; do
339 subdname=${directive/*%\(}
340 subdname=${subdname/\)*}
341 subdirective="$(_gcc-specs-directive_raw ${subdname})"
342 directive="${directive//\%(${subdname})/${subdirective}}"
343 done
344 echo "${directive}"
345 return 0
346 }
347
348 # Returns true if gcc sets relro
349 gcc-specs-relro() {
350 local directive
351 directive=$(gcc-specs-directive link_command)
352 return $([[ ${directive/\{!norelro:} != ${directive} ]])
353 }
354 # Returns true if gcc sets now
355 gcc-specs-now() {
356 local directive
357 directive=$(gcc-specs-directive link_command)
358 return $([[ ${directive/\{!nonow:} != ${directive} ]])
359 }
360 # Returns true if gcc builds PIEs
361 gcc-specs-pie() {
362 local directive
363 directive=$(gcc-specs-directive cc1)
364 return $([[ ${directive/\{!nopie:} != ${directive} ]])
365 }
366 # Returns true if gcc builds with the stack protector
367 gcc-specs-ssp() {
368 local directive
369 directive=$(gcc-specs-directive cc1)
370 return $([[ ${directive/\{!fno-stack-protector:} != ${directive} ]])
371 }
372 # Returns true if gcc upgrades fstack-protector to fstack-protector-all
373 gcc-specs-ssp-to-all() {
374 local directive
375 directive=$(gcc-specs-directive cc1)
376 return $([[ ${directive/\{!fno-stack-protector-all:} != ${directive} ]])
377 }
378 # Returns true if gcc builds with fno-strict-overflow
379 gcc-specs-nostrict() {
380 local directive
381 directive=$(gcc-specs-directive cc1)
382 return $([[ ${directive/\{!fstrict-overflow:} != ${directive} ]])
383 }
384
385
386 # @FUNCTION: gen_usr_ldscript
387 # @USAGE: <list of libs to create linker scripts for>
388 # @DESCRIPTION:
389 # This function generate linker scripts in /usr/lib for dynamic
390 # libs in /lib. This is to fix linking problems when you have
391 # the .so in /lib, and the .a in /usr/lib. What happens is that
392 # in some cases when linking dynamic, the .a in /usr/lib is used
393 # instead of the .so in /lib due to gcc/libtool tweaking ld's
394 # library search path. This causes many builds to fail.
395 # See bug #4411 for more info.
396 #
397 # Note that you should in general use the unversioned name of
398 # the library (libfoo.so), as ldconfig should usually update it
399 # correctly to point to the latest version of the library present.
400 gen_usr_ldscript() {
401 local lib libdir=$(get_libdir) output_format=""
402 # Just make sure it exists
403 dodir /usr/${libdir}
404
405 # OUTPUT_FORMAT gives hints to the linker as to what binary format
406 # is referenced ... makes multilib saner
407 output_format=$($(tc-getCC) ${CFLAGS} ${LDFLAGS} -Wl,--verbose 2>&1 | sed -n 's/^OUTPUT_FORMAT("\([^"]*\)",.*/\1/p')
408 [[ -n ${output_format} ]] && output_format="OUTPUT_FORMAT ( ${output_format} )"
409
410 for lib in "$@" ; do
411 if [[ ${USERLAND} == "Darwin" ]] ; then
412 ewarn "Not creating fake dynamic library for $lib on Darwin;"
413 ewarn "making a symlink instead."
414 dosym "/${libdir}/${lib}" "/usr/${libdir}/${lib}"
415 else
416 cat > "${D}/usr/${libdir}/${lib}" <<-END_LDSCRIPT
417 /* GNU ld script
418 Since Gentoo has critical dynamic libraries
419 in /lib, and the static versions in /usr/lib,
420 we need to have a "fake" dynamic lib in /usr/lib,
421 otherwise we run into linking problems.
422
423 See bug http://bugs.gentoo.org/4411 for more info.
424 */
425 ${output_format}
426 GROUP ( /${libdir}/${lib} )
427 END_LDSCRIPT
428 fi
429 fperms a+x "/usr/${libdir}/${lib}" || die "could not change perms on ${lib}"
430 done
431 }

  ViewVC Help
Powered by ViewVC 1.1.20