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