drop haskell on sparc, it doesn't build at all
[gentoo.git] / dev-lang / ghc / ghc-7.10.3.ebuild
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 EAPI=5
5
6 # to make make a crosscompiler use crossdev and symlink ghc tree into
7 # cross overlay. result would look like 'cross-sparc-unknown-linux-gnu/ghc'
8 #
9 # 'CTARGET' definition and 'is_crosscompile' are taken from 'toolchain.eclass'
10 export CTARGET=${CTARGET:-${CHOST}}
11 if [[ ${CTARGET} = ${CHOST} ]] ; then
12         if [[ ${CATEGORY/cross-} != ${CATEGORY} ]] ; then
13                 export CTARGET=${CATEGORY/cross-}
14         fi
15 fi
16
17 inherit autotools bash-completion-r1 eutils flag-o-matic ghc-package
18 inherit multilib pax-utils toolchain-funcs versionator prefix
19
20 DESCRIPTION="The Glasgow Haskell Compiler"
21 HOMEPAGE="https://www.haskell.org/ghc/"
22
23 # we don't have any binaries yet
24 arch_binaries=""
25
26 # sorted!
27 arch_binaries="$arch_binaries alpha? ( https://slyfox.uni.cx/~slyfox/distfiles/ghc-bin-${PV}-alpha.tbz2 )"
28 #arch_binaries="$arch_binaries arm? ( https://slyfox.uni.cx/~slyfox/distfiles/ghc-bin-${PV}-arm.tbz2 )"
29 arch_binaries="$arch_binaries amd64? ( https://slyfox.uni.cx/~slyfox/distfiles/ghc-bin-${PVR}-amd64.tbz2 )"
30 arch_binaries="$arch_binaries ia64?  ( https://slyfox.uni.cx/~slyfox/distfiles/ghc-bin-${PV}-ia64.tbz2 )"
31 arch_binaries="$arch_binaries ppc? ( https://slyfox.uni.cx/~slyfox/distfiles/ghc-bin-${PV}-ppc.tbz2 )"
32 arch_binaries="$arch_binaries ppc64? ( https://slyfox.uni.cx/~slyfox/distfiles/ghc-bin-${PV}-ppc64.tbz2 )"
33 arch_binaries="$arch_binaries sparc? ( https://slyfox.uni.cx/~slyfox/distfiles/ghc-bin-${PV}-sparc.tbz2 )"
34 arch_binaries="$arch_binaries x86? ( https://slyfox.uni.cx/~slyfox/distfiles/ghc-bin-${PVR}-x86.tbz2 )"
35
36 # various ports:
37 #arch_binaries="$arch_binaries x86-fbsd? ( https://slyfox.uni.cx/~slyfox/distfiles/ghc-bin-${PV}-x86-fbsd.tbz2 )"
38
39 # 0 - yet
40 yet_binary() {
41         case "${ARCH}" in
42                 alpha) return 0 ;;
43                 #arm)
44                 #       ewarn "ARM binary is built on armv5tel-eabi toolchain. Use with caution."
45                 #       return 0
46                 #;;
47                 amd64) return 0 ;;
48                 ia64) return 0 ;;
49                 ppc) return 0 ;;
50                 ppc64) return 0 ;;
51                 sparc) return 0 ;;
52                 x86) return 0 ;;
53                 *) return 1 ;;
54         esac
55 }
56
57 GHC_PV=${PV}
58 #GHC_PV=7.10.2.20151030 # uncomment only for -rc ebuilds
59 GHC_P=${PN}-${GHC_PV} # using ${P} is almost never correct
60
61 SRC_URI="!binary? ( https://downloads.haskell.org/~ghc/${PV/_rc/-rc}/${GHC_P}-src.tar.bz2 )"
62 S="${WORKDIR}"/${GHC_P}
63
64 [[ -n $arch_binaries ]] && SRC_URI+=" !ghcbootstrap? ( $arch_binaries )"
65
66 BUMP_LIBRARIES=(
67         # "hackage-name          hackage-version"
68         "binary                  0.7.6.1"
69         "hoopl                   3.10.2.0"
70         "transformers            0.4.3.0"
71 )
72
73 LICENSE="BSD"
74 SLOT="0/${PV}"
75 KEYWORDS="amd64 ~ia64 ~ppc ~ppc64 x86 ~amd64-linux ~x86-linux"
76 IUSE="doc ghcbootstrap ghcmakebinary +gmp"
77 IUSE+=" binary"
78 IUSE+=" elibc_glibc" # system stuff
79
80 RDEPEND="
81         >=dev-lang/perl-5.6.1
82         >=dev-libs/gmp-5:=
83         sys-libs/ncurses:0=[unicode]
84         !ghcmakebinary? ( dev-libs/libffi:= )
85 "
86
87 # This set of dependencies is needed to run
88 # prebuilt ghc. We specifically avoid ncurses
89 # dependency with:
90 #    utils/ghc-pkg_HC_OPTS += -DBOOTSTRAPPING
91 PREBUILT_BINARY_DEPENDS="
92         !prefix? ( elibc_glibc? ( >=sys-libs/glibc-2.17 ) )
93 "
94 # This set of dependencies is needed to install
95 # ghc[binary] in system. terminfo package is linked
96 # against ncurses.
97 PREBUILT_BINARY_RDEPENDS="${PREBUILT_BINARY_DEPENDS}
98         sys-libs/ncurses-compat:5
99 "
100
101 RDEPEND+="binary? ( ${PREBUILT_BINARY_RDEPENDS} )"
102
103 DEPEND="${RDEPEND}
104         doc? ( app-text/docbook-xml-dtd:4.2
105                 app-text/docbook-xml-dtd:4.5
106                 app-text/docbook-xsl-stylesheets
107                 >=dev-libs/libxslt-1.1.2 )
108         !ghcbootstrap? ( ${PREBUILT_BINARY_DEPENDS} )"
109
110 PDEPEND="!ghcbootstrap? ( =app-admin/haskell-updater-1.2* )"
111
112 REQUIRED_USE="?? ( ghcbootstrap binary )"
113
114 # haskell libraries built with cabal in configure mode, #515354
115 QA_CONFIGURE_OPTIONS+=" --with-compiler --with-gcc"
116
117 is_crosscompile() {
118         [[ ${CHOST} != ${CTARGET} ]]
119 }
120
121 # returns tool prefix for crosscompiler.
122 # Example:
123 #  CTARGET=armv7a-unknown-linux-gnueabi
124 #  CHOST=x86_64-pc-linux-gnu
125 #    "armv7a-unknown-linux-gnueabi-"
126 #  CTARGET=${CHOST}
127 #    ""
128 # Used in tools and library prefix:
129 #    "${ED}"/usr/bin/$(cross)haddock
130 #    "${ED}/usr/$(get_libdir)/$(cross)${GHC_P}/package.conf.d"
131
132 cross() {
133         if is_crosscompile; then
134                 echo "${CTARGET}-"
135         else
136                 echo ""
137         fi
138 }
139
140 append-ghc-cflags() {
141         local persistent compile assemble link
142         local flag ghcflag
143
144         for flag in $*; do
145                 case ${flag} in
146                         persistent)     persistent="yes";;
147                         compile)        compile="yes";;
148                         assemble)       assemble="yes";;
149                         link)           link="yes";;
150                         *)
151                                 [[ ${compile}  ]] && ghcflag="-optc${flag}"  CFLAGS+=" ${flag}" && GHC_FLAGS+=" ${ghcflag}" &&
152                                         [[ ${persistent} ]] && GHC_PERSISTENT_FLAGS+=" ${ghcflag}"
153                                 [[ ${assemble} ]] && ghcflag="-opta${flag}"  CFLAGS+=" ${flag}" && GHC_FLAGS+=" ${ghcflag}" &&
154                                         [[ ${persistent} ]] && GHC_PERSISTENT_FLAGS+=" ${ghcflag}"
155                                 [[ ${link}     ]] && ghcflag="-optl${flag}" LDFLAGS+=" ${flag}" && GHC_FLAGS+=" ${ghcflag}" &&
156                                         [[ ${persistent} ]] && GHC_PERSISTENT_FLAGS+=" ${ghcflag}"
157                                 ;;
158                 esac
159         done
160 }
161
162 # $1 - lib name (under libraries/)
163 # $2 - lib version
164 # example: bump_lib "transformers" "0.4.2.0"
165 bump_lib() {
166         local pn=$1 pv=$2
167         local p=${pn}-${pv}
168         local f
169
170         einfo "Bumping ${pn} up to ${pv}"
171
172         for f in ghc.mk GNUmakefile; do
173                 mv libraries/"${pn}"/$f "${WORKDIR}"/"${p}"/$f || die
174         done
175         mv libraries/"${pn}" "${WORKDIR}"/"${pn}".old || die
176         mv "${WORKDIR}"/"${p}" libraries/"${pn}" || die
177 }
178
179 update_SRC_URI() {
180         local p pn pv
181         for p in "${BUMP_LIBRARIES[@]}"; do
182                 set -- $p
183                 pn=$1 pv=$2
184
185                 SRC_URI+=" https://hackage.haskell.org/package/${pn}-${pv}/${pn}-${pv}.tar.gz"
186         done
187 }
188
189 update_SRC_URI
190
191 bump_libs() {
192         local p pn pv
193         for p in "${BUMP_LIBRARIES[@]}"; do
194                 set -- $p
195                 pn=$1 pv=$2
196
197                 bump_lib "${pn}" "${pv}"
198         done
199 }
200
201 ghc_setup_cflags() {
202         if is_crosscompile; then
203                 export CFLAGS=${GHC_CFLAGS-"-O2 -pipe"}
204                 export LDFLAGS=${GHC_LDFLAGS-"-Wl,-O1"}
205                 einfo "Crosscompiling mode:"
206                 einfo "   CHOST:   ${CHOST}"
207                 einfo "   CTARGET: ${CTARGET}"
208                 einfo "   CFLAGS:  ${CFLAGS}"
209                 einfo "   LDFLAGS: ${LDFLAGS}"
210                 einfo "   prefix: $(cross)"
211                 return
212         fi
213         # We need to be very careful with the CFLAGS we ask ghc to pass through to
214         # gcc. There are plenty of flags which will make gcc produce output that
215         # breaks ghc in various ways. The main ones we want to pass through are
216         # -mcpu / -march flags. These are important for arches like alpha & sparc.
217         # We also use these CFLAGS for building the C parts of ghc, ie the rts.
218         strip-flags
219         strip-unsupported-flags
220
221         # Cmm can't parse line numbers #482086
222         replace-flags -ggdb[3-9] -ggdb2
223
224         GHC_FLAGS=""
225         GHC_PERSISTENT_FLAGS=""
226         for flag in ${CFLAGS}; do
227                 case ${flag} in
228
229                         # Ignore extra optimisation (ghc passes -O to gcc anyway)
230                         # -O2 and above break on too many systems
231                         -O*) ;;
232
233                         # Arch and ABI flags are what we're really after
234                         -m*) append-ghc-cflags compile assemble ${flag};;
235
236                         # Sometimes it's handy to see backtrace of RTS
237                         # to get an idea what happens there
238                         -g*) append-ghc-cflags compile ${flag};;
239
240                         # Ignore all other flags, including all -f* flags
241                 esac
242         done
243
244         for flag in ${LDFLAGS}; do
245                 append-ghc-cflags link ${flag}
246         done
247
248         # hardened-gcc needs to be disabled, because our prebuilt binaries/libraries
249         # are not built with fPIC, bug #606666
250         gcc-specs-pie && append-ghc-cflags persistent compile link -nopie
251         tc-is-gcc && version_is_at_least 6.3 $(gcc-version) && if ! use ghcbootstrap; then
252                 # gcc-6.3 has support for -no-pie upstream, but spelling differs from
253                 # gentoo-specific '-nopie'. We enable it in non-bootstrap to allow
254                 # hardened users try '-pie' in USE=ghcbootstrap mode.
255                 append-ghc-cflags compile link -no-pie
256         fi
257         gcc-specs-ssp && append-ghc-cflags persistent compile      -fno-stack-protector
258
259         # prevent from failind building unregisterised ghc:
260         # https://www.mail-archive.com/debian-bugs-dist@lists.debian.org/msg171602.html
261         use ppc64 && append-ghc-cflags persistent compile -mminimal-toc
262         # fix the similar issue as ppc64 TOC on ia64. ia64 has limited size of small data
263         # currently ghc fails to build haddock
264         # https://osdir.com/ml/gnu.binutils.bugs/2004-10/msg00050.html
265         use ia64 && append-ghc-cflags persistent compile -G0
266 }
267
268 # substitutes string $1 to $2 in files $3 $4 ...
269 relocate_path() {
270         local from=$1
271         local   to=$2
272         shift 2
273         local file=
274         for file in "$@"
275         do
276                 sed -i -e "s|$from|$to|g" \
277                         "$file" || die "path relocation failed for '$file'"
278         done
279 }
280
281 # changes hardcoded ghc paths and updates package index
282 # $1 - new absolute root path
283 relocate_ghc() {
284         local to=$1
285
286         # libdir for prebuilt binary and for current system may mismatch
287         # It does for prefix installation for example: bug #476998
288         local bin_ghc_prefix=${WORKDIR}/usr
289         local bin_libpath=$(echo "${bin_ghc_prefix}"/lib*)
290         local bin_libdir=${bin_libpath#${bin_ghc_prefix}/}
291
292         # backup original script to use it later after relocation
293         local gp_back="${T}/ghc-pkg-${GHC_PV}-orig"
294         cp "${WORKDIR}/usr/bin/$(cross)ghc-pkg-${GHC_PV}" "$gp_back" || die "unable to backup ghc-pkg wrapper"
295
296         if [[ ${bin_libdir} != $(get_libdir) ]]; then
297                 einfo "Relocating '${bin_libdir}' to '$(get_libdir)' (bug #476998)"
298                 # moving the dir itself is not strictly needed
299                 # but then USE=binary would result in installing
300                 # in '${bin_libdir}'
301                 mv "${bin_ghc_prefix}/${bin_libdir}" "${bin_ghc_prefix}/$(get_libdir)" || die
302
303                 relocate_path "/usr/${bin_libdir}" "/usr/$(get_libdir)" \
304                         "${WORKDIR}/usr/bin/$(cross)ghc-${GHC_PV}" \
305                         "${WORKDIR}/usr/bin/$(cross)ghci-${GHC_PV}" \
306                         "${WORKDIR}/usr/bin/$(cross)ghc-pkg-${GHC_PV}" \
307                         "${WORKDIR}/usr/bin/$(cross)hsc2hs" \
308                         "${WORKDIR}/usr/bin/$(cross)runghc-${GHC_PV}" \
309                         "$gp_back" \
310                         "${WORKDIR}/usr/$(get_libdir)/$(cross)${GHC_P}/package.conf.d/"*
311         fi
312
313         # Relocate from /usr to ${EPREFIX}/usr
314         relocate_path "/usr" "${to}/usr" \
315                 "${WORKDIR}/usr/bin/$(cross)ghc-${GHC_PV}" \
316                 "${WORKDIR}/usr/bin/$(cross)ghci-${GHC_PV}" \
317                 "${WORKDIR}/usr/bin/$(cross)ghc-pkg-${GHC_PV}" \
318                 "${WORKDIR}/usr/bin/$(cross)hsc2hs" \
319                 "${WORKDIR}/usr/bin/$(cross)runghc-${GHC_PV}" \
320                 "${WORKDIR}/usr/$(get_libdir)/$(cross)${GHC_P}/package.conf.d/"*
321
322         # this one we will use to regenerate cache
323         # so it should point to current tree location
324         relocate_path "/usr" "${WORKDIR}/usr" "$gp_back"
325
326         if use prefix; then
327                 hprefixify "${bin_libpath}"/${PN}*/settings
328         fi
329
330         # regenerate the binary package cache
331         "$gp_back" recache || die "failed to update cache after relocation"
332         rm "$gp_back"
333 }
334
335 pkg_setup() {
336         # quiet portage about prebuilt binaries
337         use binary && QA_PREBUILT="*"
338
339         [[ ${MERGE_TYPE} == binary ]] && return
340
341         if use ghcbootstrap; then
342                 ewarn "You requested ghc bootstrapping, this is usually only used"
343                 ewarn "by Gentoo developers to make binary .tbz2 packages."
344
345                 [[ -z $(type -P ghc) ]] && \
346                         die "Could not find a ghc to bootstrap with."
347         else
348                 if ! yet_binary; then
349                         eerror "Please try emerging with USE=ghcbootstrap and report build"
350                         eerror "sucess or failure to the haskell team (haskell@gentoo.org)"
351                         die "No binary available for '${ARCH}' arch yet, USE=ghcbootstrap"
352                 fi
353         fi
354 }
355
356 src_unpack() {
357         # Create the ${S} dir if we're using the binary version
358         use binary && mkdir "${S}"
359
360         # the Solaris and Darwin binaries from ghc (maeder) need to be
361         # unpacked separately, so prevent them from being unpacked
362         local ONLYA=${A}
363         case ${CHOST} in
364                 *-darwin* | *-solaris*)  ONLYA=${GHC_P}-src.tar.bz2  ;;
365         esac
366         unpack ${ONLYA}
367
368         if [[ -d "${S}"/libraries/dph ]]; then
369                 # Sometimes dph libs get accidentally shipped with ghc
370                 # but they are not installed unless user requests it.
371                 # We never install them.
372                 elog "Removing 'libraries/dph'"
373                 rm -rf "${S}"/libraries/dph
374         fi
375 }
376
377 src_prepare() {
378         ghc_setup_cflags
379
380         if ! use ghcbootstrap && [[ ${CHOST} != *-darwin* && ${CHOST} != *-solaris* ]]; then
381                 # Modify the wrapper script from the binary tarball to use GHC_PERSISTENT_FLAGS.
382                 # See bug #313635.
383                 sed -i -e "s|\"\$topdir\"|\"\$topdir\" ${GHC_PERSISTENT_FLAGS}|" \
384                         "${WORKDIR}/usr/bin/$(cross)ghc-${GHC_PV}"
385
386                 # allow hardened users use vanilla binary to bootstrap ghc
387                 # ghci uses mmap with rwx protection at it implements dynamic
388                 # linking on it's own (bug #299709)
389                 pax-mark -m "${WORKDIR}/usr/$(get_libdir)/$(cross)${GHC_P}/bin/ghc"
390         fi
391
392         if use binary; then
393                 if use prefix; then
394                         relocate_ghc "${EPREFIX}"
395                 fi
396
397                 # Move unpacked files to the expected place
398                 mv "${WORKDIR}/usr" "${S}"
399         else
400                 if ! use ghcbootstrap; then
401                         case ${CHOST} in
402                                 *-darwin* | *-solaris*)
403                                 # UPDATE ME for ghc-7
404                                 mkdir "${WORKDIR}"/ghc-bin-installer || die
405                                 pushd "${WORKDIR}"/ghc-bin-installer > /dev/null || die
406                                 use sparc-solaris && unpack ghc-6.10.4-sparc-sun-solaris2.tar.bz2
407                                 use x86-solaris && unpack ghc-7.0.3-i386-unknown-solaris2.tar.bz2
408                                 use x86-macos && unpack ghc-7.4.1-i386-apple-darwin.tar.bz2
409                                 use x64-macos && unpack ghc-7.4.1-x86_64-apple-darwin.tar.bz2
410                                 popd > /dev/null
411
412                                 pushd "${WORKDIR}"/ghc-bin-installer/ghc-[67].?*.? > /dev/null || die
413                                 # fix the binaries so they run, on Solaris we need an
414                                 # LD_LIBRARY_PATH which has our prefix libdirs, on
415                                 # Darwin we need to replace the frameworks with our libs
416                                 # from the prefix fix before installation, because some
417                                 # of the tools are actually used during configure/make
418                                 if [[ ${CHOST} == *-solaris* ]] ; then
419                                         export LD_LIBRARY_PATH="${EPREFIX}/$(get_libdir):${EPREFIX}/usr/$(get_libdir):${LD_LIBRARY_PATH}"
420                                 elif [[ ${CHOST} == *-darwin* ]] ; then
421                                         local readline_framework=GNUreadline.framework/GNUreadline
422                                         local gmp_framework=/opt/local/lib/libgmp.10.dylib
423                                         local ncurses_file=/opt/local/lib/libncurses.5.dylib
424                                         for binary in $(scanmacho -BRE MH_EXECUTE -F '%F' .) ; do
425                                                 install_name_tool -change \
426                                                         ${readline_framework} \
427                                                         "${EPREFIX}"/lib/libreadline.dylib \
428                                                         ${binary} || die
429                                                 install_name_tool -change \
430                                                         ${gmp_framework} \
431                                                         "${EPREFIX}"/usr/lib/libgmp.dylib \
432                                                         ${binary} || die
433                                                 install_name_tool -change \
434                                                         ${ncurses_file} \
435                                                         "${EPREFIX}"/usr/lib/libncurses.dylib \
436                                                         ${binary} || die
437                                         done
438                                         # we don't do frameworks!
439                                         sed -i \
440                                                 -e 's/\(frameworks = \)\["GMP"\]/\1[]/g' \
441                                                 -e 's/\(extraLibraries = \)\["m"\]/\1["m","gmp"]/g' \
442                                                 rts/package.conf.in || die
443                                 fi
444
445                                 # it is autoconf, but we really don't want to give it too
446                                 # much arguments, in fact we do the make in-place anyway
447                                 ./configure --prefix="${WORKDIR}"/usr || die
448                                 make install || die
449                                 popd > /dev/null
450                                 ;;
451                                 *)
452                                 relocate_ghc "${WORKDIR}"
453                                 ;;
454                         esac
455                 fi
456
457                 sed -i -e "s|\"\$topdir\"|\"\$topdir\" ${GHC_PERSISTENT_FLAGS}|" \
458                         "${S}/ghc/ghc.wrapper"
459
460                 cd "${S}" # otherwise epatch will break
461
462                 epatch "${FILESDIR}"/${PN}-7.0.4-CHOST-prefix.patch
463
464                 epatch "${FILESDIR}"/${PN}-7.8.2-cgen-constify.patch
465                 epatch "${FILESDIR}"/${PN}-7.8.3-prim-lm.patch
466                 epatch "${FILESDIR}"/${PN}-7.10.1-rc3-ghc-7.10-bootstrap.patch
467                 epatch "${FILESDIR}"/${PN}-7.10.3-relnotes.patch
468                 epatch "${FILESDIR}"/${PN}-7.10.3-hardfloat.patch
469
470                 # Since ${S}/packages does not include base, etc. add them to gen_contents_index
471                 sed -e 's@\(for REPO in .*\)@\1 base integer-gmp integer-gmp2 integer-simple template-haskell@' \
472                         -i libraries/gen_contents_index || die
473
474                 if use prefix; then
475                         # Make configure find docbook-xsl-stylesheets from Prefix
476                         sed -e '/^FP_DIR_DOCBOOK_XSL/s:\[.*\]:['"${EPREFIX}"'/usr/share/sgml/docbook/xsl-stylesheets/]:' \
477                                 -i utils/haddock/doc/configure.ac || die
478                 fi
479
480                 bump_libs
481
482                 # as we have changed the build system
483                 eautoreconf
484         fi
485 }
486
487 src_configure() {
488         if ! use binary; then
489                 # initialize build.mk
490                 echo '# Gentoo changes' > mk/build.mk
491
492                 # Put docs into the right place, ie /usr/share/doc/ghc-${GHC_PV}
493                 echo "docdir = ${EPREFIX}/usr/share/doc/${P}" >> mk/build.mk
494                 echo "htmldir = ${EPREFIX}/usr/share/doc/${P}" >> mk/build.mk
495
496                 # We also need to use the GHC_FLAGS flags when building ghc itself
497                 echo "SRC_HC_OPTS+=${HCFLAGS} ${GHC_FLAGS}" >> mk/build.mk
498                 echo "SRC_CC_OPTS+=${CFLAGS}" >> mk/build.mk
499                 echo "SRC_LD_OPTS+=${LDFLAGS}" >> mk/build.mk
500
501                 # We can't depend on haddock except when bootstrapping when we
502                 # must build docs and include them into the binary .tbz2 package
503                 # app-text/dblatex is not in portage, can not build PDF or PS
504                 echo "BUILD_DOCBOOK_PDF  = NO"  >> mk/build.mk
505                 echo "BUILD_DOCBOOK_PS   = NO"  >> mk/build.mk
506                 if use doc; then
507                         echo "BUILD_DOCBOOK_HTML = YES" >> mk/build.mk
508                 else
509                         echo "BUILD_DOCBOOK_HTML = NO" >> mk/build.mk
510                 fi
511
512                 # this controls presence on 'xhtml' and 'haddock' in final install
513                 echo "HADDOCK_DOCS       = YES" >> mk/build.mk
514
515                 # allows overriding build flavours for libraries:
516                 # v   - vanilla (static libs)
517                 # p   - profiled
518                 # dyn - shared libraries
519                 # example: GHC_LIBRARY_WAYS="v dyn"
520                 if [[ -n ${GHC_LIBRARY_WAYS} ]]; then
521                         echo "GhcLibWays=${GHC_LIBRARY_WAYS}" >> mk/build.mk
522                 fi
523
524                 # Get ghc from the unpacked binary .tbz2
525                 # except when bootstrapping we just pick ghc up off the path
526                 if ! use ghcbootstrap; then
527                         export PATH="${WORKDIR}/usr/bin:${PATH}"
528                 fi
529
530                 if use gmp; then
531                         echo "INTEGER_LIBRARY=integer-gmp2" >> mk/build.mk
532                 else
533                         echo "INTEGER_LIBRARY=integer-simple" >> mk/build.mk
534                 fi
535
536                 # don't strip anything. Very useful when stage2 SIGSEGVs on you
537                 echo "STRIP_CMD = :" >> mk/build.mk
538
539                 local econf_args=()
540
541                 # GHC embeds 'gcc' it was built by and uses it later.
542                 # Don't allow things like ccache or versioned binary slip.
543                 # We use stable thing across gcc upgrades.
544                 is_crosscompile || econf_args+=(--with-gcc=${CHOST}-gcc)
545
546                 if use ghcmakebinary; then
547                         # When building booting libary we are trying to
548                         # bundle or restrict most of external depends
549                         # with unstable ABI:
550                         #  - embed libffi (default GHC behaviour)
551                         #  - disable ncurses support for ghci (via haskeline)
552                         #    https://bugs.gentoo.org/557478
553                         #  - disable ncurses support for ghc-pkg
554                         echo "libraries/haskeline_CONFIGURE_OPTS += --flag=-terminfo" >> mk/build.mk
555                         echo "utils/ghc-pkg_HC_OPTS += -DBOOTSTRAPPING" >> mk/build.mk
556                 else
557                         econf_args+=(--with-system-libffi)
558                         econf_args+=(--with-ffi-includes=$(pkg-config libffi --cflags-only-I | sed -e 's@^-I@@'))
559                 fi
560
561                 elog "Final mk/build.mk:"
562                 cat mk/build.mk || die
563
564                 econf ${econf_args[@]} --enable-bootstrap-with-devel-snapshot
565
566                 if [[ ${PV} == *9999* ]]; then
567                         GHC_PV="$(grep 'S\[\"PACKAGE_VERSION\"\]' config.status | sed -e 's@^.*=\"\(.*\)\"@\1@')"
568                         GHC_P=${PN}-${GHC_PV}
569                 fi
570         fi # ! use binary
571 }
572
573 src_compile() {
574         if ! use binary; then
575                 # 1. build compiler binary first
576                 emake ghc/stage2/build/tmp/ghc-stage2
577                 # 2. pax-mark (bug #516430)
578                 pax-mark -m ghc/stage2/build/tmp/ghc-stage2
579                 # 3. and then all the rest
580                 emake all
581         fi # ! use binary
582 }
583
584 src_install() {
585         if use binary; then
586                 use prefix && mkdir -p "${ED}"
587                 mv "${S}/usr" "${ED}"
588         else
589
590                 emake -j1 install DESTDIR="${D}"
591                 dodoc "distrib/README" "ANNOUNCE" "LICENSE" "VERSION"
592
593                 # rename ghc-shipped files to avoid collision
594                 # of external packages. Motivating example:
595                 #  user had installed:
596                 #      dev-lang/ghc-7.8.4-r0 (with transformers-0.3.0.0)
597                 #      dev-haskell/transformers-0.4.2.0
598                 #  then user tried to update to
599                 #      dev-lang/ghc-7.8.4-r1 (with transformers-0.4.2.0)
600                 #  this will lead to single .conf file collision.
601                 local shipped_conf renamed_conf
602                 local package_confdir="${ED}/usr/$(get_libdir)/$(cross)${GHC_P}/package.conf.d"
603                 for shipped_conf in "${package_confdir}"/*.conf; do
604                         # rename 'pkg-ver-id.conf' to 'pkg-ver-id-gentoo-${PF}.conf'
605                         renamed_conf=${shipped_conf%.conf}-gentoo-${PF}.conf
606                         mv "${shipped_conf}" "${renamed_conf}" || die
607                 done
608
609                 # remove link, but leave 'haddock-${GHC_P}'
610                 rm -f "${ED}"/usr/bin/$(cross)haddock
611
612                 if [[ ! -f "${S}/VERSION" ]]; then
613                         echo "${GHC_PV}" > "${S}/VERSION" \
614                                 || die "Could not create file ${S}/VERSION"
615                 fi
616                 if ! is_crosscompile; then
617                         newbashcomp "${FILESDIR}"/ghc-bash-completion ghc-pkg
618                         newbashcomp utils/completion/ghc.bash         ghc
619                 fi
620         fi
621
622         # path to the package.cache
623         local package_confdir="${ED}/usr/$(get_libdir)/$(cross)${GHC_P}/package.conf.d"
624         PKGCACHE="${package_confdir}"/package.cache
625         # copy the package.conf.d, including timestamp, save it so we can help
626         # users that have a broken package.conf.d
627         cp -pR "${package_confdir}"{,.initial} || die "failed to backup intial package.conf.d"
628
629         # copy the package.conf, including timestamp, save it so we later can put it
630         # back before uninstalling, or when upgrading.
631         cp -p "${PKGCACHE}"{,.shipped} \
632                 || die "failed to copy package.conf.d/package.cache"
633 }
634
635 pkg_preinst() {
636         # have we got an earlier version of ghc installed?
637         if has_version "<${CATEGORY}/${PF}"; then
638                 haskell_updater_warn="1"
639         fi
640 }
641
642 pkg_postinst() {
643         ghc-reregister
644
645         # path to the package.cache
646         PKGCACHE="${EROOT}/usr/$(get_libdir)/$(cross)${GHC_P}/package.conf.d/package.cache"
647
648         # give the cache a new timestamp, it must be as recent as
649         # the package.conf.d directory.
650         touch "${PKGCACHE}"
651
652         if [[ "${haskell_updater_warn}" == "1" ]]; then
653                 ewarn
654                 ewarn "\e[1;31m************************************************************************\e[0m"
655                 ewarn
656                 ewarn "You have just upgraded from an older version of GHC."
657                 ewarn "You may have to run"
658                 ewarn "      'haskell-updater'"
659                 ewarn "to rebuild all ghc-based Haskell libraries."
660                 ewarn
661                 ewarn "\e[1;31m************************************************************************\e[0m"
662                 ewarn
663         fi
664 }
665
666 pkg_prerm() {
667         PKGCACHE="${EROOT}/usr/$(get_libdir)/$(cross)${GHC_P}/package.conf.d/package.cache"
668         rm -rf "${PKGCACHE}"
669
670         cp -p "${PKGCACHE}"{.shipped,}
671 }
672
673 pkg_postrm() {
674         ghc-package_pkg_postrm
675 }