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