9f09edd7b4e0756105576e5274885693e3bddd8e
[gentoo.git] / sys-devel / binutils / binutils-2.32-r1.ebuild
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 EAPI=6
5
6 inherit eutils libtool flag-o-matic gnuconfig multilib toolchain-funcs versionator
7
8 DESCRIPTION="Tools necessary to build programs"
9 HOMEPAGE="https://sourceware.org/binutils/"
10 LICENSE="GPL-3+"
11 # USE="+cxx" is a transitional flag until llvm migrates to new flags:
12 #    bug #677888
13 IUSE="+cxx default-gold doc +gold multitarget +nls +plugins static-libs test"
14 REQUIRED_USE="cxx? ( gold plugins ) default-gold? ( gold )"
15
16 # Variables that can be set here:
17 # PATCH_VER          - the patchset version
18 #                      Default: empty, no patching
19 # PATCH_BINUTILS_VER - the binutils version in the patchset name
20 #                    - Default: PV
21 # PATCH_DEV          - Use download URI https://dev.gentoo.org/~{PATCH_DEV}/distfiles/...
22 #                      for the patchsets
23 #                      Default: dilfridge :)
24
25 PATCH_VER=2
26 PATCH_DEV=dilfridge
27
28 case ${PV} in
29         9999)
30                 EGIT_REPO_URI="https://sourceware.org/git/binutils-gdb.git"
31                 inherit git-r3
32                 S=${WORKDIR}/binutils
33                 EGIT_CHECKOUT_DIR=${S}
34                 SLOT=${PV}
35                 ;;
36         *.9999)
37                 EGIT_REPO_URI="https://sourceware.org/git/binutils-gdb.git"
38                 inherit git-r3
39                 S=${WORKDIR}/binutils
40                 EGIT_CHECKOUT_DIR=${S}
41                 EGIT_BRANCH=$(get_version_component_range 1-2)
42                 EGIT_BRANCH="binutils-${EGIT_BRANCH/./_}-branch"
43                 SLOT=$(get_version_component_range 1-2)
44                 ;;
45         *)
46                 SRC_URI="mirror://gnu/binutils/binutils-${PV}.tar.xz"
47                 SLOT=$(get_version_component_range 1-2)
48                 KEYWORDS="~alpha amd64 arm arm64 hppa ~ia64 m68k ~mips ppc ppc64 ~riscv s390 sparc x86"
49                 ;;
50 esac
51
52 #
53 # The Gentoo patchset
54 #
55 PATCH_BINUTILS_VER=${PATCH_BINUTILS_VER:-${PV}}
56 PATCH_DEV=${PATCH_DEV:-slyfox}
57
58 [[ -z ${PATCH_VER} ]] || SRC_URI="${SRC_URI}
59         https://dev.gentoo.org/~${PATCH_DEV}/distfiles/binutils-${PATCH_BINUTILS_VER}-patches-${PATCH_VER}.tar.xz"
60
61 # Disable gold testsuite since it always fails.
62 PATCHES=( "${FILESDIR}/${PN}-2.29.1-nogoldtest.patch" )
63
64 #
65 # The cross-compile logic
66 #
67 export CTARGET=${CTARGET:-${CHOST}}
68 if [[ ${CTARGET} == ${CHOST} ]] ; then
69         if [[ ${CATEGORY} == cross-* ]] ; then
70                 export CTARGET=${CATEGORY#cross-}
71         fi
72 fi
73 is_cross() { [[ ${CHOST} != ${CTARGET} ]] ; }
74
75 #
76 # The dependencies
77 #
78 RDEPEND="
79         >=sys-devel/binutils-config-3
80         sys-libs/zlib
81 "
82 DEPEND="${RDEPEND}
83         doc? ( sys-apps/texinfo )
84         test? ( dev-util/dejagnu )
85         nls? ( sys-devel/gettext )
86         sys-devel/flex
87         virtual/yacc
88 "
89
90 RESTRICT="!test? ( test )"
91
92 MY_BUILDDIR=${WORKDIR}/build
93
94 src_unpack() {
95         case ${PV} in
96                 *9999)
97                         git-r3_src_unpack
98                         ;;
99                 *)
100                         ;;
101         esac
102         default
103         mkdir -p "${MY_BUILDDIR}"
104 }
105
106 src_prepare() {
107         if [[ ! -z ${PATCH_VER} ]] ; then
108                 # Use upstream patch to enable development mode
109                 rm -v "${WORKDIR}/patch"/0000-Gentoo-Git-is-development.patch || die
110
111                 einfo "Applying binutils-${PATCH_BINUTILS_VER} patchset ${PATCH_VER}"
112                 eapply "${WORKDIR}/patch"/*.patch
113         fi
114
115         # This check should probably go somewhere else, like pkg_pretend.
116         if [[ ${CTARGET} == *-uclibc* ]] ; then
117                 if grep -qs 'linux-gnu' "${S}"/ltconfig ; then
118                         die "sorry, but this binutils doesn't yet support uClibc :("
119                 fi
120         fi
121
122         # Make sure our explicit libdir paths don't get clobbered. #562460
123         sed -i \
124                 -e 's:@bfdlibdir@:@libdir@:g' \
125                 -e 's:@bfdincludedir@:@includedir@:g' \
126                 {bfd,opcodes}/Makefile.in || die
127
128         # Fix locale issues if possible #122216
129         if [[ -e ${FILESDIR}/binutils-configure-LANG.patch ]] ; then
130                 einfo "Fixing misc issues in configure files"
131                 for f in $(find "${S}" -name configure -exec grep -l 'autoconf version 2.13' {} +) ; do
132                         ebegin "  Updating ${f/${S}\/}"
133                         patch "${f}" "${FILESDIR}"/binutils-configure-LANG.patch >& "${T}"/configure-patch.log \
134                                 || eerror "Please file a bug about this"
135                         eend $?
136                 done
137         fi
138
139         # Fix conflicts with newer glibc #272594
140         if [[ -e libiberty/testsuite/test-demangle.c ]] ; then
141                 sed -i 's:\<getline\>:get_line:g' libiberty/testsuite/test-demangle.c
142         fi
143
144         # Apply things from PATCHES and user dirs
145         default
146
147         # Run misc portage update scripts
148         gnuconfig_update
149         elibtoolize --portage --no-uclibc
150 }
151
152 toolchain-binutils_bugurl() {
153         printf "https://bugs.gentoo.org/"
154 }
155 toolchain-binutils_pkgversion() {
156         printf "Gentoo ${PV}"
157         [[ -n ${PATCH_VER} ]] && printf " p${PATCH_VER}"
158 }
159
160 src_configure() {
161         # Setup some paths
162         LIBPATH=/usr/$(get_libdir)/binutils/${CTARGET}/${PV}
163         INCPATH=${LIBPATH}/include
164         DATAPATH=/usr/share/binutils-data/${CTARGET}/${PV}
165         if is_cross ; then
166                 TOOLPATH=/usr/${CHOST}/${CTARGET}
167         else
168                 TOOLPATH=/usr/${CTARGET}
169         fi
170         BINPATH=${TOOLPATH}/binutils-bin/${PV}
171
172         # Make sure we filter $LINGUAS so that only ones that
173         # actually work make it through #42033
174         strip-linguas -u */po
175
176         # Keep things sane
177         strip-flags
178
179         local x
180         echo
181         for x in CATEGORY CBUILD CHOST CTARGET CFLAGS LDFLAGS ; do
182                 einfo "$(printf '%10s' ${x}:) ${!x}"
183         done
184         echo
185
186         cd "${MY_BUILDDIR}"
187         local myconf=()
188
189         if use plugins ; then
190                 myconf+=( --enable-plugins )
191         fi
192         # enable gold (installed as ld.gold) and ld's plugin architecture
193         if use gold ; then
194                 myconf+=( --enable-gold )
195                 if use default-gold; then
196                         myconf+=( --enable-gold=default )
197                 fi
198         fi
199
200         if use nls ; then
201                 myconf+=( --without-included-gettext )
202         else
203                 myconf+=( --disable-nls )
204         fi
205
206         myconf+=( --with-system-zlib )
207
208         # For bi-arch systems, enable a 64bit bfd.  This matches
209         # the bi-arch logic in toolchain.eclass. #446946
210         # We used to do it for everyone, but it's slow on 32bit arches. #438522
211         case $(tc-arch) in
212                 ppc|sparc|x86) myconf+=( --enable-64-bit-bfd ) ;;
213         esac
214
215         use multitarget && myconf+=( --enable-targets=all --enable-64-bit-bfd )
216
217         [[ -n ${CBUILD} ]] && myconf+=( --build=${CBUILD} )
218
219         is_cross && myconf+=(
220                 --with-sysroot="${EPREFIX}"/usr/${CTARGET}
221                 --enable-poison-system-directories
222         )
223
224         # glibc-2.3.6 lacks support for this ... so rather than force glibc-2.5+
225         # on everyone in alpha (for now), we'll just enable it when possible
226         has_version ">=${CATEGORY}/glibc-2.5" && myconf+=( --enable-secureplt )
227         has_version ">=sys-libs/glibc-2.5" && myconf+=( --enable-secureplt )
228
229         # mips can't do hash-style=gnu ...
230         if [[ $(tc-arch) != mips ]] ; then
231                 myconf+=( --enable-default-hash-style=gnu )
232         fi
233
234         myconf+=(
235                 --prefix="${EPREFIX}"/usr
236                 --host=${CHOST}
237                 --target=${CTARGET}
238                 --datadir="${EPREFIX}"${DATAPATH}
239                 --datarootdir="${EPREFIX}"${DATAPATH}
240                 --infodir="${EPREFIX}"${DATAPATH}/info
241                 --mandir="${EPREFIX}"${DATAPATH}/man
242                 --bindir="${EPREFIX}"${BINPATH}
243                 --libdir="${EPREFIX}"${LIBPATH}
244                 --libexecdir="${EPREFIX}"${LIBPATH}
245                 --includedir="${EPREFIX}"${INCPATH}
246                 --enable-obsolete
247                 --enable-shared
248                 --enable-threads
249                 # Newer versions (>=2.27) offer a configure flag now.
250                 --enable-relro
251                 # Newer versions (>=2.24) make this an explicit option. #497268
252                 --enable-install-libiberty
253                 --disable-werror
254                 --with-bugurl="$(toolchain-binutils_bugurl)"
255                 --with-pkgversion="$(toolchain-binutils_pkgversion)"
256                 $(use_enable static-libs static)
257                 ${EXTRA_ECONF}
258                 # Disable modules that are in a combined binutils/gdb tree. #490566
259                 --disable-{gdb,libdecnumber,readline,sim}
260                 # Strip out broken static link flags.
261                 # https://gcc.gnu.org/PR56750
262                 --without-stage1-ldflags
263                 # Change SONAME to avoid conflict across
264                 # {native,cross}/binutils, binutils-libs. #666100
265                 --with-extra-soversion-suffix=gentoo-${CATEGORY}-${PN}-$(usex multitarget mt st)
266         )
267         echo ./configure "${myconf[@]}"
268         "${S}"/configure "${myconf[@]}" || die
269
270         # Prevent makeinfo from running if doc is unset.
271         if ! use doc ; then
272                 sed -i \
273                         -e '/^MAKEINFO/s:=.*:= true:' \
274                         Makefile || die
275         fi
276 }
277
278 src_compile() {
279         cd "${MY_BUILDDIR}"
280         # see Note [tooldir hack for ldscripts]
281         emake tooldir="${EPREFIX}${TOOLPATH}" all
282
283         # only build info pages if the user wants them
284         if use doc ; then
285                 emake info
286         fi
287
288         # we nuke the manpages when we're left with junk
289         # (like when we bootstrap, no perl -> no manpages)
290         find . -name '*.1' -a -size 0 -delete
291 }
292
293 src_test() {
294         cd "${MY_BUILDDIR}"
295
296         # bug 637066
297         filter-flags -Wall -Wreturn-type
298
299         # enable verbose test run and result logging
300         emake -k check RUNTESTFLAGS='-a -v' VERBOSE=1
301 }
302
303 src_install() {
304         local x d
305
306         cd "${MY_BUILDDIR}"
307         # see Note [tooldir hack for ldscripts]
308         emake DESTDIR="${D}" tooldir="${EPREFIX}${LIBPATH}" install
309         rm -rf "${ED}"/${LIBPATH}/bin
310         use static-libs || find "${ED}" -name '*.la' -delete
311
312         # Newer versions of binutils get fancy with ${LIBPATH} #171905
313         cd "${ED}"/${LIBPATH}
314         for d in ../* ; do
315                 [[ ${d} == ../${PV} ]] && continue
316                 mv ${d}/* . || die
317                 rmdir ${d} || die
318         done
319
320         # Now we collect everything intp the proper SLOT-ed dirs
321         # When something is built to cross-compile, it installs into
322         # /usr/$CHOST/ by default ... we have to 'fix' that :)
323         if is_cross ; then
324                 cd "${ED}"/${BINPATH}
325                 for x in * ; do
326                         mv ${x} ${x/${CTARGET}-}
327                 done
328
329                 if [[ -d ${ED}/usr/${CHOST}/${CTARGET} ]] ; then
330                         mv "${ED}"/usr/${CHOST}/${CTARGET}/include "${ED}"/${INCPATH}
331                         mv "${ED}"/usr/${CHOST}/${CTARGET}/lib/* "${ED}"/${LIBPATH}/
332                         rm -r "${ED}"/usr/${CHOST}/{include,lib}
333                 fi
334         fi
335         insinto ${INCPATH}
336         local libiberty_headers=(
337                 # Not all the libiberty headers.  See libiberty/Makefile.in:install_to_libdir.
338                 demangle.h
339                 dyn-string.h
340                 fibheap.h
341                 hashtab.h
342                 libiberty.h
343                 objalloc.h
344                 splay-tree.h
345         )
346         doins "${libiberty_headers[@]/#/${S}/include/}"
347         if [[ -d ${ED}/${LIBPATH}/lib ]] ; then
348                 mv "${ED}"/${LIBPATH}/lib/* "${ED}"/${LIBPATH}/
349                 rm -r "${ED}"/${LIBPATH}/lib
350         fi
351
352         # Generate an env.d entry for this binutils
353         insinto /etc/env.d/binutils
354         cat <<-EOF > "${T}"/env.d
355                 TARGET="${CTARGET}"
356                 VER="${PV}"
357                 LIBPATH="${EPREFIX}${LIBPATH}"
358         EOF
359         newins "${T}"/env.d ${CTARGET}-${PV}
360
361         # Handle documentation
362         if ! is_cross ; then
363                 cd "${S}"
364                 dodoc README
365                 docinto bfd
366                 dodoc bfd/ChangeLog* bfd/README bfd/PORTING bfd/TODO
367                 docinto binutils
368                 dodoc binutils/ChangeLog binutils/NEWS binutils/README
369                 docinto gas
370                 dodoc gas/ChangeLog* gas/CONTRIBUTORS gas/NEWS gas/README*
371                 docinto gprof
372                 dodoc gprof/ChangeLog* gprof/TEST gprof/TODO gprof/bbconv.pl
373                 docinto ld
374                 dodoc ld/ChangeLog* ld/README ld/NEWS ld/TODO
375                 docinto libiberty
376                 dodoc libiberty/ChangeLog* libiberty/README
377                 docinto opcodes
378                 dodoc opcodes/ChangeLog*
379         fi
380
381         # Remove shared info pages
382         rm -f "${ED}"/${DATAPATH}/info/{dir,configure.info,standards.info}
383
384         # Trim all empty dirs
385         find "${ED}" -depth -type d -exec rmdir {} + 2>/dev/null
386 }
387
388 pkg_postinst() {
389         # Make sure this ${CTARGET} has a binutils version selected
390         [[ -e ${EROOT}/etc/env.d/binutils/config-${CTARGET} ]] && return 0
391         binutils-config ${CTARGET}-${PV}
392 }
393
394 pkg_postrm() {
395         local current_profile=$(binutils-config -c ${CTARGET})
396
397         # If no other versions exist, then uninstall for this
398         # target ... otherwise, switch to the newest version
399         # Note: only do this if this version is unmerged.  We
400         #       rerun binutils-config if this is a remerge, as
401         #       we want the mtimes on the symlinks updated (if
402         #       it is the same as the current selected profile)
403         if [[ ! -e ${EPREFIX}${BINPATH}/ld ]] && [[ ${current_profile} == ${CTARGET}-${PV} ]] ; then
404                 local choice=$(binutils-config -l | grep ${CTARGET} | awk '{print $2}')
405                 choice=${choice//$'\n'/ }
406                 choice=${choice/* }
407                 if [[ -z ${choice} ]] ; then
408                         binutils-config -u ${CTARGET}
409                 else
410                         binutils-config ${choice}
411                 fi
412         elif [[ $(CHOST=${CTARGET} binutils-config -c) == ${CTARGET}-${PV} ]] ; then
413                 binutils-config ${CTARGET}-${PV}
414         fi
415 }
416
417 # Note [slotting support]
418 # -----------------------
419 # Gentoo's layout for binutils files is non-standard as Gentoo
420 # supports slotted installation for binutils. Many tools
421 # still expect binutils to reside in known locations.
422 # binutils-config package restores symlinks into known locations,
423 # like:
424 #    /usr/bin/${CTARGET}-<tool>
425 #    /usr/bin/${CHOST}/${CTARGET}/lib/ldscrips
426 #    /usr/include/
427 #
428 # Note [tooldir hack for ldscripts]
429 # ---------------------------------
430 # Build system does not allow ./configure to tweak every location
431 # we need for slotting binutils hence all the shuffling in
432 # src_install(). This note is about SCRIPTDIR define handling.
433 #
434 # SCRIPTDIR defines 'ldscripts/' directory location. SCRIPTDIR value
435 # is set at build-time in ld/Makefile.am as: 'scriptdir = $(tooldir)/lib'
436 # and hardcoded as -DSCRIPTDIR='"$(scriptdir)"' at compile time.
437 # Thus we can't just move files around after compilation finished.
438 #
439 # Our goal is the following:
440 # - at build-time set scriptdir to point to symlinked location:
441 #   ${TOOLPATH}: /usr/${CHOST} (or /usr/${CHOST}/${CTARGET} for cross-case)
442 # - at install-time set scriptdir to point to slotted location:
443 #   ${LIBPATH}: /usr/$(get_libdir)/binutils/${CTARGET}/${PV}