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