kde-apps/ksirk: x86 stable (bug #661810)
[gentoo.git] / eclass / toolchain-binutils.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 #
4 # Maintainer: Toolchain Ninjas <toolchain@gentoo.org>
5 #
6 # We install binutils into CTARGET-VERSION specific directories.  This lets
7 # us easily merge multiple versions for multiple targets (if we wish) and
8 # then switch the versions on the fly (with `binutils-config`).
9 #
10 # binutils-9999           -> live git
11 # binutils-9999_preYYMMDD -> nightly snapshot date YYMMDD
12 # binutils-#              -> normal release
13
14 if [[ -n ${BINUTILS_TYPE} ]] ; then
15         BTYPE=${BINUTILS_TYPE}
16 else
17         case ${PV} in
18         9999_pre*) BTYPE="snap";;
19         *.*.90)    BTYPE="snap";;
20         *.*.*.*.*) BTYPE="hjlu";;
21         *)         BTYPE="rel";;
22         esac
23 fi
24
25 case ${BTYPE} in
26 snap)
27         BVER=${PV/9999_pre}
28         ;;
29 *)
30         BVER=${BINUTILS_VER:-${PV}}
31         ;;
32 esac
33
34 inherit eutils libtool flag-o-matic gnuconfig multilib versionator unpacker
35 case ${EAPI:-0} in
36 0|1)
37         EXPORT_FUNCTIONS src_unpack src_compile src_test src_install pkg_postinst pkg_postrm ;;
38 2|3|4|5)
39         EXPORT_FUNCTIONS src_unpack src_prepare src_configure src_compile src_test src_install pkg_postinst pkg_postrm ;;
40 *) die "unsupported EAPI ${EAPI}" ;;
41 esac
42
43 export CTARGET=${CTARGET:-${CHOST}}
44 if [[ ${CTARGET} == ${CHOST} ]] ; then
45         if [[ ${CATEGORY} == cross-* ]] ; then
46                 export CTARGET=${CATEGORY#cross-}
47         fi
48 fi
49 is_cross() { [[ ${CHOST} != ${CTARGET} ]] ; }
50
51 DESCRIPTION="Tools necessary to build programs"
52 HOMEPAGE="https://sourceware.org/binutils/"
53
54 case ${BTYPE} in
55         snap)
56                 SRC_URI="ftp://gcc.gnu.org/pub/binutils/snapshots/binutils-${BVER}.tar.bz2
57                         ftp://sourceware.org/pub/binutils/snapshots/binutils-${BVER}.tar.bz2" ;;
58         hjlu)
59                 SRC_URI="mirror://kernel/linux/devel/binutils/binutils-${BVER}.tar."
60                 version_is_at_least 2.21.51.0.5 && SRC_URI+="xz" || SRC_URI+="bz2" ;;
61         rel) SRC_URI="mirror://gnu/binutils/binutils-${BVER}.tar.bz2" ;;
62 esac
63 add_src_uri() {
64         [[ -z $2 ]] && return
65         local a=$1
66         if version_is_at_least 2.22.52.0.2 ; then
67                 a+=".xz"
68         else
69                 a+=".bz2"
70         fi
71         set -- mirror://gentoo https://dev.gentoo.org/~vapier/dist https://dev.gentoo.org/~tamiko/distfiles https://dev.gentoo.org/~dilfridge/distfiles
72         SRC_URI="${SRC_URI} ${@/%//${a}}"
73 }
74 PATCH_BINUTILS_VER=${PATCH_BINUTILS_VER:-${BVER}}
75 add_src_uri binutils-${PATCH_BINUTILS_VER}-patches-${PATCHVER}.tar ${PATCHVER}
76 add_src_uri binutils-${PATCH_BINUTILS_VER}-uclibc-patches-${UCLIBC_PATCHVER}.tar ${UCLIBC_PATCHVER}
77 add_src_uri elf2flt-${ELF2FLT_VER}.tar ${ELF2FLT_VER}
78
79 if version_is_at_least 2.18 ; then
80         LICENSE="|| ( GPL-3 LGPL-3 )"
81 else
82         LICENSE="|| ( GPL-2 LGPL-2 )"
83 fi
84 IUSE="cxx multitarget nls static-libs test vanilla"
85 if version_is_at_least 2.19 && ! version_is_at_least 2.26 ; then
86         IUSE+=" zlib"
87 fi
88 SLOT="${BVER}"
89
90 RDEPEND=">=sys-devel/binutils-config-3"
91 if in_iuse zlib ; then
92         RDEPEND+=" zlib? ( sys-libs/zlib )"
93 elif version_is_at_least 2.26 ; then
94         RDEPEND+=" sys-libs/zlib"
95 fi
96 DEPEND="${RDEPEND}
97         test? ( dev-util/dejagnu )
98         nls? ( sys-devel/gettext )
99         sys-devel/flex
100         virtual/yacc"
101 if is_cross ; then
102         # The build assumes the host has libiberty and such when cross-compiling
103         # its build tools.  We should probably make binutils itself build a local
104         # copy to use, but until then, be lazy.
105         DEPEND+=" >=sys-libs/binutils-libs-${PV}"
106 fi
107
108 S=${WORKDIR}/binutils-${BVER}
109
110 LIBPATH=/usr/$(get_libdir)/binutils/${CTARGET}/${BVER}
111 INCPATH=${LIBPATH}/include
112 DATAPATH=/usr/share/binutils-data/${CTARGET}/${BVER}
113 MY_BUILDDIR=${WORKDIR}/build
114 if is_cross ; then
115         BINPATH=/usr/${CHOST}/${CTARGET}/binutils-bin/${BVER}
116 else
117         BINPATH=/usr/${CTARGET}/binutils-bin/${BVER}
118 fi
119
120 tc-binutils_unpack() {
121         unpacker ${A}
122         mkdir -p "${MY_BUILDDIR}"
123         [[ -d ${WORKDIR}/patch ]] && mkdir "${WORKDIR}"/patch/skip
124 }
125
126 # In case the ebuild wants to add a few of their own.
127 PATCHES=()
128
129 tc-binutils_apply_patches() {
130         cd "${S}"
131
132         if ! use vanilla ; then
133                 if [[ -n ${PATCHVER} ]] ; then
134                         EPATCH_SOURCE=${WORKDIR}/patch
135                         if [[ ${CTARGET} == mips* ]] ; then
136                                 # remove gnu-hash for mips (bug #233233)
137                                 EPATCH_EXCLUDE+=" 77_all_generate-gnu-hash.patch"
138                         fi
139                         [[ -n $(ls "${EPATCH_SOURCE}"/*.bz2 2>/dev/null) ]] \
140                                 && EPATCH_SUFFIX="patch.bz2" \
141                                 || EPATCH_SUFFIX="patch"
142                         epatch
143                 fi
144                 if [[ -n ${UCLIBC_PATCHVER} ]] ; then
145                         EPATCH_SOURCE=${WORKDIR}/uclibc-patches
146                         [[ -n $(ls "${EPATCH_SOURCE}"/*.bz2 2>/dev/null) ]] \
147                                 && EPATCH_SUFFIX="patch.bz2" \
148                                 || EPATCH_SUFFIX="patch"
149                         EPATCH_MULTI_MSG="Applying uClibc fixes ..." \
150                         epatch
151                 elif [[ ${CTARGET} == *-uclibc* ]] ; then
152                         # starting with binutils-2.17.50.0.17, we no longer need
153                         # uClibc patchsets :D
154                         if grep -qs 'linux-gnu' "${S}"/ltconfig ; then
155                                 die "sorry, but this binutils doesn't yet support uClibc :("
156                         fi
157                 fi
158                 [[ ${#PATCHES[@]} -gt 0 ]] && epatch "${PATCHES[@]}"
159
160                 # Make sure our explicit libdir paths don't get clobbered. #562460
161                 sed -i \
162                         -e 's:@bfdlibdir@:@libdir@:g' \
163                         -e 's:@bfdincludedir@:@includedir@:g' \
164                         {bfd,opcodes}/Makefile.in || die
165
166                 epatch_user
167         fi
168
169         # fix locale issues if possible #122216
170         if [[ -e ${FILESDIR}/binutils-configure-LANG.patch ]] ; then
171                 einfo "Fixing misc issues in configure files"
172                 for f in $(find "${S}" -name configure -exec grep -l 'autoconf version 2.13' {} +) ; do
173                         ebegin "  Updating ${f/${S}\/}"
174                         patch "${f}" "${FILESDIR}"/binutils-configure-LANG.patch >& "${T}"/configure-patch.log \
175                                 || eerror "Please file a bug about this"
176                         eend $?
177                 done
178         fi
179         # fix conflicts with newer glibc #272594
180         if [[ -e libiberty/testsuite/test-demangle.c ]] ; then
181                 sed -i 's:\<getline\>:get_line:g' libiberty/testsuite/test-demangle.c
182         fi
183
184         # Fix po Makefile generators
185         sed -i \
186                 -e '/^datadir = /s:$(prefix)/@DATADIRNAME@:@datadir@:' \
187                 -e '/^gnulocaledir = /s:$(prefix)/share:$(datadir):' \
188                 */po/Make-in || die "sed po's failed"
189
190         # Run misc portage update scripts
191         gnuconfig_update
192         elibtoolize --portage --no-uclibc
193 }
194
195 toolchain-binutils_src_unpack() {
196         tc-binutils_unpack
197         case ${EAPI:-0} in
198         0|1) toolchain-binutils_src_prepare ;;
199         esac
200 }
201
202 toolchain-binutils_src_prepare() {
203         tc-binutils_apply_patches
204 }
205
206 _eprefix_init() {
207         has "${EAPI:-0}" 0 1 2 && ED=${D} EPREFIX= EROOT=${ROOT}
208 }
209
210 # Intended for ebuilds to override to set their own versioning information.
211 toolchain-binutils_bugurl() {
212         printf "https://bugs.gentoo.org/"
213 }
214 toolchain-binutils_pkgversion() {
215         printf "Gentoo ${BVER}"
216         [[ -n ${PATCHVER} ]] && printf " p${PATCHVER}"
217 }
218
219 toolchain-binutils_src_configure() {
220         _eprefix_init
221
222         # make sure we filter $LINGUAS so that only ones that
223         # actually work make it through #42033
224         strip-linguas -u */po
225
226         # keep things sane
227         strip-flags
228
229         local x
230         echo
231         for x in CATEGORY CBUILD CHOST CTARGET CFLAGS LDFLAGS ; do
232                 einfo "$(printf '%10s' ${x}:) ${!x}"
233         done
234         echo
235
236         cd "${MY_BUILDDIR}"
237         local myconf=()
238
239         # enable gold if available (installed as ld.gold)
240         if use cxx ; then
241                 if grep -q 'enable-gold=default' "${S}"/configure ; then
242                         myconf+=( --enable-gold )
243                 # old ways - remove when 2.21 is stable
244                 elif grep -q 'enable-gold=both/ld' "${S}"/configure ; then
245                         myconf+=( --enable-gold=both/ld )
246                 elif grep -q 'enable-gold=both/bfd' "${S}"/configure ; then
247                         myconf+=( --enable-gold=both/bfd )
248                 fi
249                 if grep -q -e '--enable-plugins' "${S}"/ld/configure ; then
250                         myconf+=( --enable-plugins )
251                 fi
252         fi
253
254         use nls \
255                 && myconf+=( --without-included-gettext ) \
256                 || myconf+=( --disable-nls )
257
258         if in_iuse zlib ; then
259                 # older versions did not have an explicit configure flag
260                 export ac_cv_search_zlibVersion=$(usex zlib -lz no)
261                 myconf+=( $(use_with zlib) )
262         elif version_is_at_least 2.26 ; then
263                 myconf+=( --with-system-zlib )
264         fi
265
266         # For bi-arch systems, enable a 64bit bfd.  This matches
267         # the bi-arch logic in toolchain.eclass. #446946
268         # We used to do it for everyone, but it's slow on 32bit arches. #438522
269         case $(tc-arch) in
270         ppc|sparc|x86) myconf+=( --enable-64-bit-bfd ) ;;
271         esac
272
273         use multitarget && myconf+=( --enable-targets=all --enable-64-bit-bfd )
274         [[ -n ${CBUILD} ]] && myconf+=( --build=${CBUILD} )
275         is_cross && myconf+=(
276                 --with-sysroot="${EPREFIX}"/usr/${CTARGET}
277                 --enable-poison-system-directories
278         )
279
280         # glibc-2.3.6 lacks support for this ... so rather than force glibc-2.5+
281         # on everyone in alpha (for now), we'll just enable it when possible
282         has_version ">=${CATEGORY}/glibc-2.5" && myconf+=( --enable-secureplt )
283         has_version ">=sys-libs/glibc-2.5" && myconf+=( --enable-secureplt )
284
285         myconf+=(
286                 --prefix="${EPREFIX}"/usr
287                 --host=${CHOST}
288                 --target=${CTARGET}
289                 --datadir="${EPREFIX}"${DATAPATH}
290                 --infodir="${EPREFIX}"${DATAPATH}/info
291                 --mandir="${EPREFIX}"${DATAPATH}/man
292                 --bindir="${EPREFIX}"${BINPATH}
293                 --libdir="${EPREFIX}"${LIBPATH}
294                 --libexecdir="${EPREFIX}"${LIBPATH}
295                 --includedir="${EPREFIX}"${INCPATH}
296                 --enable-obsolete
297                 --enable-shared
298                 --enable-threads
299                 # Newer versions (>=2.27) offer a configure flag now.
300                 --enable-relro
301                 # Newer versions (>=2.24) make this an explicit option. #497268
302                 --enable-install-libiberty
303                 --disable-werror
304                 --with-bugurl="$(toolchain-binutils_bugurl)"
305                 --with-pkgversion="$(toolchain-binutils_pkgversion)"
306                 $(use_enable static-libs static)
307                 ${EXTRA_ECONF}
308                 # Disable modules that are in a combined binutils/gdb tree. #490566
309                 --disable-{gdb,libdecnumber,readline,sim}
310                 # Strip out broken static link flags.
311                 # https://gcc.gnu.org/PR56750
312                 --without-stage1-ldflags
313         )
314         echo ./configure "${myconf[@]}"
315         "${S}"/configure "${myconf[@]}" || die
316
317         # Prevent makeinfo from running in releases.  It may not always be
318         # installed, and older binutils may fail with newer texinfo.
319         # Besides, we never patch the doc files anyways, so regenerating
320         # in the first place is useless. #193364
321         # For older versions, it means we don't get any info pages at all.
322         # Oh well, tough luck. #294617
323         if [[ -e ${S}/gas/doc/as.info ]] || ! version_is_at_least 2.24 ; then
324                 sed -i \
325                         -e '/^MAKEINFO/s:=.*:= true:' \
326                         Makefile || die
327         fi
328 }
329
330 toolchain-binutils_src_compile() {
331         _eprefix_init
332         case ${EAPI:-0} in
333         0|1) toolchain-binutils_src_configure ;;
334         esac
335
336         cd "${MY_BUILDDIR}"
337         emake all || die "emake failed"
338
339         # only build info pages if we user wants them, and if
340         # we have makeinfo (may not exist when we bootstrap)
341         if type -p makeinfo > /dev/null ; then
342                 emake info || die "make info failed"
343         fi
344         # we nuke the manpages when we're left with junk
345         # (like when we bootstrap, no perl -> no manpages)
346         find . -name '*.1' -a -size 0 -delete
347
348         # elf2flt only works on some arches / targets
349         if [[ -n ${ELF2FLT_VER} ]] && [[ ${CTARGET} == *linux* || ${CTARGET} == *-elf* ]] ; then
350                 cd "${WORKDIR}"/elf2flt-${ELF2FLT_VER}
351
352                 local x supported_arches=$(sed -n '/defined(TARGET_/{s:^.*TARGET_::;s:)::;p}' elf2flt.c | sort -u)
353                 for x in ${supported_arches} UNSUPPORTED ; do
354                         [[ ${CTARGET} == ${x}* ]] && break
355                 done
356
357                 if [[ ${x} != "UNSUPPORTED" ]] ; then
358                         append-flags -I"${S}"/include
359                         myconf+=(
360                                 --with-bfd-include-dir=${MY_BUILDDIR}/bfd
361                                 --with-libbfd=${MY_BUILDDIR}/bfd/libbfd.a
362                                 --with-libiberty=${MY_BUILDDIR}/libiberty/libiberty.a
363                                 --with-binutils-ldscript-dir="${EPREFIX}"${LIBPATH}/ldscripts
364                         )
365                         echo ./configure "${myconf[@]}"
366                         ./configure "${myconf[@]}" || die
367                         emake || die "make elf2flt failed"
368                 fi
369         fi
370 }
371
372 toolchain-binutils_src_test() {
373         cd "${MY_BUILDDIR}"
374         emake -k check || die "check failed :("
375 }
376
377 toolchain-binutils_src_install() {
378         _eprefix_init
379         local x d
380
381         cd "${MY_BUILDDIR}"
382         emake DESTDIR="${D}" tooldir="${EPREFIX}${LIBPATH}" install || die
383         rm -rf "${ED}"/${LIBPATH}/bin
384         use static-libs || find "${ED}" -name '*.la' -delete
385
386         # Newer versions of binutils get fancy with ${LIBPATH} #171905
387         cd "${ED}"/${LIBPATH}
388         for d in ../* ; do
389                 [[ ${d} == ../${BVER} ]] && continue
390                 mv ${d}/* . || die
391                 rmdir ${d} || die
392         done
393
394         # Now we collect everything intp the proper SLOT-ed dirs
395         # When something is built to cross-compile, it installs into
396         # /usr/$CHOST/ by default ... we have to 'fix' that :)
397         if is_cross ; then
398                 cd "${ED}"/${BINPATH}
399                 for x in * ; do
400                         mv ${x} ${x/${CTARGET}-}
401                 done
402
403                 if [[ -d ${ED}/usr/${CHOST}/${CTARGET} ]] ; then
404                         mv "${ED}"/usr/${CHOST}/${CTARGET}/include "${ED}"/${INCPATH}
405                         mv "${ED}"/usr/${CHOST}/${CTARGET}/lib/* "${ED}"/${LIBPATH}/
406                         rm -r "${ED}"/usr/${CHOST}/{include,lib}
407                 fi
408         fi
409         insinto ${INCPATH}
410         local libiberty_headers=(
411                 # Not all the libiberty headers.  See libiberty/Makefile.in:install_to_libdir.
412                 demangle.h
413                 dyn-string.h
414                 fibheap.h
415                 hashtab.h
416                 libiberty.h
417                 objalloc.h
418                 splay-tree.h
419         )
420         doins "${libiberty_headers[@]/#/${S}/include/}" || die
421         if [[ -d ${ED}/${LIBPATH}/lib ]] ; then
422                 mv "${ED}"/${LIBPATH}/lib/* "${ED}"/${LIBPATH}/
423                 rm -r "${ED}"/${LIBPATH}/lib
424         fi
425
426         # Insert elf2flt where appropriate
427         if [[ -x ${WORKDIR}/elf2flt-${ELF2FLT_VER}/elf2flt ]] ; then
428                 cd "${WORKDIR}"/elf2flt-${ELF2FLT_VER}
429                 insinto ${LIBPATH}/ldscripts
430                 doins elf2flt.ld || die "doins elf2flt.ld failed"
431                 exeinto ${BINPATH}
432                 doexe elf2flt flthdr || die "doexe elf2flt flthdr failed"
433                 mv "${ED}"/${BINPATH}/{ld,ld.real} || die
434                 newexe ld-elf2flt ld || die "doexe ld-elf2flt failed"
435                 newdoc README README.elf2flt
436         fi
437
438         # Generate an env.d entry for this binutils
439         insinto /etc/env.d/binutils
440         cat <<-EOF > "${T}"/env.d
441                 TARGET="${CTARGET}"
442                 VER="${BVER}"
443                 LIBPATH="${EPREFIX}${LIBPATH}"
444         EOF
445         newins "${T}"/env.d ${CTARGET}-${BVER}
446
447         # Handle documentation
448         if ! is_cross ; then
449                 cd "${S}"
450                 dodoc README
451                 docinto bfd
452                 dodoc bfd/ChangeLog* bfd/README bfd/PORTING bfd/TODO
453                 docinto binutils
454                 dodoc binutils/ChangeLog binutils/NEWS binutils/README
455                 docinto gas
456                 dodoc gas/ChangeLog* gas/CONTRIBUTORS gas/NEWS gas/README*
457                 docinto gprof
458                 dodoc gprof/ChangeLog* gprof/TEST gprof/TODO gprof/bbconv.pl
459                 docinto ld
460                 dodoc ld/ChangeLog* ld/README ld/NEWS ld/TODO
461                 docinto libiberty
462                 dodoc libiberty/ChangeLog* libiberty/README
463                 docinto opcodes
464                 dodoc opcodes/ChangeLog*
465         fi
466         # Remove shared info pages
467         rm -f "${ED}"/${DATAPATH}/info/{dir,configure.info,standards.info}
468         # Trim all empty dirs
469         find "${ED}" -depth -type d -exec rmdir {} + 2>/dev/null
470 }
471
472 toolchain-binutils_pkg_postinst() {
473         _eprefix_init
474         # Make sure this ${CTARGET} has a binutils version selected
475         [[ -e ${EROOT}/etc/env.d/binutils/config-${CTARGET} ]] && return 0
476         binutils-config ${CTARGET}-${BVER}
477 }
478
479 toolchain-binutils_pkg_postrm() {
480         _eprefix_init
481         local current_profile=$(binutils-config -c ${CTARGET})
482
483         # If no other versions exist, then uninstall for this
484         # target ... otherwise, switch to the newest version
485         # Note: only do this if this version is unmerged.  We
486         #       rerun binutils-config if this is a remerge, as
487         #       we want the mtimes on the symlinks updated (if
488         #       it is the same as the current selected profile)
489         if [[ ! -e ${EPREFIX}${BINPATH}/ld ]] && [[ ${current_profile} == ${CTARGET}-${BVER} ]] ; then
490                 local choice=$(binutils-config -l | grep ${CTARGET} | awk '{print $2}')
491                 choice=${choice//$'\n'/ }
492                 choice=${choice/* }
493                 if [[ -z ${choice} ]] ; then
494                         binutils-config -u ${CTARGET}
495                 else
496                         binutils-config ${choice}
497                 fi
498         elif [[ $(CHOST=${CTARGET} binutils-config -c) == ${CTARGET}-${BVER} ]] ; then
499                 binutils-config ${CTARGET}-${BVER}
500         fi
501 }