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