perl-functions.eclass: should 'just work' in EAPI=6
[gentoo.git] / eclass / haskell-cabal.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: haskell-cabal.eclass
6 # @MAINTAINER:
7 # Haskell herd <haskell@gentoo.org>
8 # @AUTHOR:
9 # Original author: Andres Loeh <kosmikus@gentoo.org>
10 # Original author: Duncan Coutts <dcoutts@gentoo.org>
11 # @BLURB: for packages that make use of the Haskell Common Architecture for Building Applications and Libraries (cabal)
12 # @DESCRIPTION:
13 # Basic instructions:
14 #
15 # Before inheriting the eclass, set CABAL_FEATURES to
16 # reflect the tools and features that the package makes
17 # use of.
18 #
19 # Currently supported features:
20 #   haddock    --  for documentation generation
21 #   hscolour   --  generation of colourised sources
22 #   hoogle     --  generation of documentation search index
23 #   alex       --  lexer/scanner generator
24 #   happy      --  parser generator
25 #   c2hs       --  C interface generator
26 #   cpphs      --  C preprocessor clone written in Haskell
27 #   profile    --  if package supports to build profiling-enabled libraries
28 #   bootstrap  --  only used for the cabal package itself
29 #   bin        --  the package installs binaries
30 #   lib        --  the package installs libraries
31 #   nocabaldep --  don't add dependency on cabal.
32 #                  only used for packages that _must_ not pull the dependency
33 #                  on cabal, but still use this eclass (e.g. haskell-updater).
34 #   ghcdeps    --  constraint dependency on package to ghc onces
35 #                  only used for packages that use libghc internally and _must_
36 #                  not pull upper versions
37 #   test-suite --  add support for cabal test-suites (introduced in Cabal-1.8)
38
39 inherit eutils ghc-package multilib multiprocessing
40
41 # @ECLASS-VARIABLE: CABAL_EXTRA_CONFIGURE_FLAGS
42 # @DESCRIPTION:
43 # User-specified additional parameters passed to 'setup configure'.
44 # example: /etc/portage/make.conf:
45 #    CABAL_EXTRA_CONFIGURE_FLAGS="--enable-shared --enable-executable-dynamic"
46 : ${CABAL_EXTRA_CONFIGURE_FLAGS:=}
47
48 # @ECLASS-VARIABLE: CABAL_EXTRA_BUILD_FLAGS
49 # @DESCRIPTION:
50 # User-specified additional parameters passed to 'setup build'.
51 # example: /etc/portage/make.conf: CABAL_EXTRA_BUILD_FLAGS=-v
52 : ${CABAL_EXTRA_BUILD_FLAGS:=}
53
54 # @ECLASS-VARIABLE: GHC_BOOTSTRAP_FLAGS
55 # @DESCRIPTION:
56 # User-specified additional parameters for ghc when building
57 # _only_ 'setup' binary bootstrap.
58 # example: /etc/portage/make.conf: GHC_BOOTSTRAP_FLAGS=-dynamic to make
59 # linking 'setup' faster.
60 : ${GHC_BOOTSTRAP_FLAGS:=}
61
62 # @ECLASS-VARIABLE: CABAL_DEBUG_LOOSENING
63 # @DESCRIPTION:
64 # Show debug output for 'cabal_chdeps' function if set.
65 # Needs working 'diff'.
66 : ${CABAL_DEBUG_LOOSENING:=}
67
68 HASKELL_CABAL_EXPF="pkg_setup src_compile src_test src_install pkg_postinst pkg_postrm"
69
70 # 'dev-haskell/cabal' passes those options with ./configure-based
71 # configuration, but most packages don't need/don't accept it:
72 # #515362, #515362
73 QA_CONFIGURE_OPTIONS+=" --with-compiler --with-hc --with-hc-pkg --with-gcc"
74
75 case "${EAPI:-0}" in
76         2|3|4|5) HASKELL_CABAL_EXPF+=" src_configure" ;;
77         *) ;;
78 esac
79
80 EXPORT_FUNCTIONS ${HASKELL_CABAL_EXPF}
81
82 for feature in ${CABAL_FEATURES}; do
83         case ${feature} in
84                 haddock)    CABAL_USE_HADDOCK=yes;;
85                 hscolour)   CABAL_USE_HSCOLOUR=yes;;
86                 hoogle)     CABAL_USE_HOOGLE=yes;;
87                 alex)       CABAL_USE_ALEX=yes;;
88                 happy)      CABAL_USE_HAPPY=yes;;
89                 c2hs)       CABAL_USE_C2HS=yes;;
90                 cpphs)      CABAL_USE_CPPHS=yes;;
91                 profile)    CABAL_USE_PROFILE=yes;;
92                 bootstrap)  CABAL_BOOTSTRAP=yes;;
93                 bin)        CABAL_HAS_BINARIES=yes;;
94                 lib)        CABAL_HAS_LIBRARIES=yes;;
95                 nocabaldep) CABAL_FROM_GHC=yes;;
96                 ghcdeps)    CABAL_GHC_CONSTRAINT=yes;;
97                 test-suite) CABAL_TEST_SUITE=yes;;
98                 *) CABAL_UNKNOWN="${CABAL_UNKNOWN} ${feature}";;
99         esac
100 done
101
102 if [[ -n "${CABAL_USE_HADDOCK}" ]]; then
103         IUSE="${IUSE} doc"
104         # don't require depend on itself to build docs.
105         # ebuild bootstraps docs from just built binary
106         #
107         # starting from ghc-7.10.2 we install haddock bundled with
108         # ghc to keep links to base and ghc library, otherwise
109         # newer haddock versions change index format and can't
110         # read index files for packages coming with ghc.
111         [[ ${CATEGORY}/${PN} = "dev-haskell/haddock" ]] || \
112                 DEPEND="${DEPEND} doc? ( || ( dev-haskell/haddock >=dev-lang/ghc-7.10.2 ) )"
113 fi
114
115 if [[ -n "${CABAL_USE_HSCOLOUR}" ]]; then
116         IUSE="${IUSE} hscolour"
117         DEPEND="${DEPEND} hscolour? ( dev-haskell/hscolour )"
118 fi
119
120 if [[ -n "${CABAL_USE_HOOGLE}" ]]; then
121         # enabled only in ::haskell
122         CABAL_USE_HOOGLE=
123 fi
124
125 if [[ -n "${CABAL_USE_ALEX}" ]]; then
126         DEPEND="${DEPEND} dev-haskell/alex"
127 fi
128
129 if [[ -n "${CABAL_USE_HAPPY}" ]]; then
130         DEPEND="${DEPEND} dev-haskell/happy"
131 fi
132
133 if [[ -n "${CABAL_USE_C2HS}" ]]; then
134         DEPEND="${DEPEND} dev-haskell/c2hs"
135 fi
136
137 if [[ -n "${CABAL_USE_CPPHS}" ]]; then
138         DEPEND="${DEPEND} dev-haskell/cpphs"
139 fi
140
141 if [[ -n "${CABAL_USE_PROFILE}" ]]; then
142         IUSE="${IUSE} profile"
143 fi
144
145 if [[ -n "${CABAL_TEST_SUITE}" ]]; then
146         IUSE="${IUSE} test"
147 fi
148
149 # We always use a standalone version of Cabal, rather than the one that comes
150 # with GHC. But of course we can't depend on cabal when building cabal itself.
151 if [[ -z ${CABAL_MIN_VERSION} ]]; then
152         CABAL_MIN_VERSION=1.1.4
153 fi
154 if [[ -z "${CABAL_BOOTSTRAP}" && -z "${CABAL_FROM_GHC}" ]]; then
155         DEPEND="${DEPEND} >=dev-haskell/cabal-${CABAL_MIN_VERSION}"
156 fi
157
158 # returns the version of cabal currently in use.
159 # Rarely it's handy to pin cabal version from outside.
160 : ${_CABAL_VERSION_CACHE:=""}
161 cabal-version() {
162         if [[ -z "${_CABAL_VERSION_CACHE}" ]]; then
163                 if [[ "${CABAL_BOOTSTRAP}" ]]; then
164                         # We're bootstrapping cabal, so the cabal version is the version
165                         # of this package itself.
166                         _CABAL_VERSION_CACHE="${PV}"
167                 elif [[ "${CABAL_FROM_GHC}" ]]; then
168                         _CABAL_VERSION_CACHE="$(ghc-cabal-version)"
169                 else
170                         # We ask portage, not ghc, so that we only pick up
171                         # portage-installed cabal versions.
172                         _CABAL_VERSION_CACHE="$(ghc-extractportageversion dev-haskell/cabal)"
173                 fi
174         fi
175         echo "${_CABAL_VERSION_CACHE}"
176 }
177
178 cabal-bootstrap() {
179         local setupmodule
180         local cabalpackage
181         local setup_bootstrap_args=()
182
183         if [[ -f "${S}/Setup.lhs" ]]; then
184                 setupmodule="${S}/Setup.lhs"
185         elif [[ -f "${S}/Setup.hs" ]]; then
186                 setupmodule="${S}/Setup.hs"
187         else
188                 die "No Setup.lhs or Setup.hs found"
189         fi
190
191         if [[ -z "${CABAL_BOOTSTRAP}" && -z "${CABAL_FROM_GHC}" ]] && ! ghc-sanecabal "${CABAL_MIN_VERSION}"; then
192                 eerror "The package dev-haskell/cabal is not correctly installed for"
193                 eerror "the currently active version of ghc ($(ghc-version)). Please"
194                 eerror "run haskell-updater or re-build dev-haskell/cabal."
195                 die "cabal is not correctly installed"
196         fi
197
198         # We build the setup program using the latest version of
199         # cabal that we have installed
200         cabalpackage=Cabal-$(cabal-version)
201         einfo "Using cabal-$(cabal-version)."
202
203         if $(ghc-supports-threaded-runtime); then
204                 # Cabal has a bug that deadlocks non-threaded RTS:
205                 #     https://bugs.gentoo.org/537500
206                 #     https://github.com/haskell/cabal/issues/2398
207                 setup_bootstrap_args+=(-threaded)
208         fi
209
210         make_setup() {
211                 set -- -package "${cabalpackage}" --make "${setupmodule}" \
212                         "${setup_bootstrap_args[@]}" \
213                         ${HCFLAGS} \
214                         ${GHC_BOOTSTRAP_FLAGS} \
215                         "$@" \
216                         -o setup
217                 echo $(ghc-getghc) "$@"
218                 $(ghc-getghc) "$@"
219         }
220         if $(ghc-supports-shared-libraries); then
221                 # # some custom build systems might use external libraries,
222                 # # for which we don't have shared libs, so keep static fallback
223                 # bug #411789, http://hackage.haskell.org/trac/ghc/ticket/5743#comment:3
224                 # http://hackage.haskell.org/trac/ghc/ticket/7062
225                 # http://hackage.haskell.org/trac/ghc/ticket/3072
226                 # ghc does not set RPATH for extralibs, thus we do it ourselves by hands
227                 einfo "Prepending $(ghc-libdir) to LD_LIBRARY_PATH"
228                 if [[ ${CHOST} != *-darwin* ]]; then
229                         LD_LIBRARY_PATH="$(ghc-libdir)${LD_LIBRARY_PATH:+:}${LD_LIBRARY_PATH}"
230                         export LD_LIBRARY_PATH
231                 else
232                         DYLD_LIBRARY_PATH="$(ghc-libdir)${DYLD_LIBRARY_PATH:+:}${DYLD_LIBRARY_PATH}"
233                         export DYLD_LIBRARY_PATH
234                 fi
235                 { make_setup -dynamic "$@" && ./setup --help >/dev/null; } ||
236                 make_setup "$@" || die "compiling ${setupmodule} failed"
237         else
238                 make_setup "$@" || die "compiling ${setupmodule} failed"
239         fi
240 }
241
242 cabal-mksetup() {
243         local setupdir=${1:-${S}}
244         local setup_src=${setupdir}/Setup.hs
245
246         rm -vf "${setupdir}"/Setup.{lhs,hs}
247         elog "Creating 'Setup.hs' for 'Simple' build type."
248
249         echo 'import Distribution.Simple; main = defaultMain' \
250                 > "${setup_src}" || die "failed to create default Setup.hs"
251 }
252
253 cabal-hscolour() {
254         set -- hscolour "$@"
255         echo ./setup "$@"
256         ./setup "$@" || die "setup hscolour failed"
257 }
258
259 cabal-haddock() {
260         set -- haddock "$@"
261         echo ./setup "$@"
262         ./setup "$@" || die "setup haddock failed"
263 }
264
265 cabal-hoogle() {
266         ewarn "hoogle USE flag requires doc USE flag, building without hoogle"
267 }
268
269 cabal-hscolour-haddock() {
270         # --hyperlink-source implies calling 'setup hscolour'
271         set -- haddock --hyperlink-source
272         echo ./setup "$@"
273         ./setup "$@" --hyperlink-source || die "setup haddock --hyperlink-source failed"
274 }
275
276 cabal-hoogle-haddock() {
277         set -- haddock --hoogle
278         echo ./setup "$@"
279         ./setup "$@" || die "setup haddock --hoogle failed"
280 }
281
282 cabal-hoogle-hscolour-haddock() {
283         cabal-hscolour-haddock
284         cabal-hoogle-haddock
285 }
286
287 cabal-hoogle-hscolour() {
288         ewarn "hoogle USE flag requires doc USE flag, building without hoogle"
289         cabal-hscolour
290 }
291
292 cabal-die-if-nonempty() {
293         local breakage_type=$1
294         shift
295
296         [[ "${#@}" == 0 ]] && return 0
297         eerror "Detected ${breakage_type} packages: ${@}"
298         die "//==-- Please, run 'haskell-updater' to fix ${breakage_type} packages --==//"
299 }
300
301 cabal-show-brokens() {
302         elog "ghc-pkg check: 'checking for other broken packages:'"
303         # pretty-printer
304         $(ghc-getghcpkg) check 2>&1 \
305                 | egrep -v '^Warning: haddock-(html|interfaces): ' \
306                 | egrep -v '^Warning: include-dirs: ' \
307                 | head -n 20
308
309         cabal-die-if-nonempty 'broken' \
310                 $($(ghc-getghcpkg) check --simple-output)
311 }
312
313 cabal-show-old() {
314         cabal-die-if-nonempty 'outdated' \
315                 $("${EPREFIX}"/usr/sbin/haskell-updater --quiet --upgrade --list-only)
316 }
317
318 cabal-show-brokens-and-die() {
319         cabal-show-brokens
320         cabal-show-old
321
322         die "$@"
323 }
324
325 cabal-configure() {
326         local cabalconf=()
327         has "${EAPI:-0}" 0 1 2 && ! use prefix && EPREFIX=
328
329         if [[ -n "${CABAL_USE_HADDOCK}" ]] && use doc; then
330                 # We use the bundled with GHC version if exists
331                 # Haddock is very picky about index files
332                 # it generates for ghc's base and other packages.
333                 local p=${EPREFIX}/usr/bin/haddock-ghc-$(ghc-version)
334                 if [[ -f $p ]]; then
335                         cabalconf+=(--with-haddock="${p}")
336                 else
337                         cabalconf+=(--with-haddock=${EPREFIX}/usr/bin/haddock)
338                 fi
339         fi
340         if [[ -n "${CABAL_USE_PROFILE}" ]] && use profile; then
341                 cabalconf+=(--enable-library-profiling)
342         fi
343         if [[ -n "${CABAL_USE_ALEX}" ]]; then
344                 cabalconf+=(--with-alex=${EPREFIX}/usr/bin/alex)
345         fi
346
347         if [[ -n "${CABAL_USE_HAPPY}" ]]; then
348                 cabalconf+=(--with-happy=${EPREFIX}/usr/bin/happy)
349         fi
350
351         if [[ -n "${CABAL_USE_C2HS}" ]]; then
352                 cabalconf+=(--with-c2hs=${EPREFIX}/usr/bin/c2hs)
353         fi
354         if [[ -n "${CABAL_USE_CPPHS}" ]]; then
355                 cabalconf+=(--with-cpphs=${EPREFIX}/usr/bin/cpphs)
356         fi
357         if [[ -n "${CABAL_TEST_SUITE}" ]]; then
358                 cabalconf+=($(use_enable test tests))
359         fi
360
361         if [[ -n "${CABAL_GHC_CONSTRAINT}" ]]; then
362                 cabalconf+=($(cabal-constraint "ghc"))
363         fi
364
365         local option
366         for option in ${HCFLAGS}
367         do
368                 cabalconf+=(--ghc-option="$option")
369         done
370
371         # parallel on all available cores
372         if ghc-supports-parallel-make; then
373                 local max_jobs=$(makeopts_jobs)
374
375                 # limit to very small value, as parallelism
376                 # helps slightly, but makes things severely worse
377                 # when amount of threads is Very Large.
378                 [[ ${max_jobs} -gt 4 ]] && max_jobs=4
379
380                 cabalconf+=(--ghc-option=-j"$max_jobs")
381         fi
382
383         # Building GHCi libs on ppc64 causes "TOC overflow".
384         if use ppc64; then
385                 cabalconf+=(--disable-library-for-ghci)
386         fi
387
388         # currently cabal does not respect CFLAGS and LDFLAGS on it's own (bug #333217)
389         # so translate LDFLAGS to ghc parameters (without filtering)
390         local flag
391         for flag in   $CFLAGS; do cabalconf+=(--ghc-option="-optc$flag"); done
392         for flag in  $LDFLAGS; do cabalconf+=(--ghc-option="-optl$flag"); done
393
394         # disable executable stripping for the executables, as portage will
395         # strip by itself, and pre-stripping gives a QA warning.
396         # cabal versions previous to 1.4 does not strip executables, and does
397         # not accept the flag.
398         # this fixes numerous bugs, amongst them;
399         # bug #251881, bug #251882, bug #251884, bug #251886, bug #299494
400         cabalconf+=(--disable-executable-stripping)
401
402         cabalconf+=(--docdir="${EPREFIX}"/usr/share/doc/${PF})
403         # As of Cabal 1.2, configure is quite quiet. For diagnostic purposes
404         # it's better if the configure chatter is in the build logs:
405         cabalconf+=(--verbose)
406
407         # We build shared version of our Cabal where ghc ships it's shared
408         # version of it. We will link ./setup as dynamic binary againt Cabal later.
409         [[ ${CATEGORY}/${PN} == "dev-haskell/cabal" ]] && \
410                 $(ghc-supports-shared-libraries) && \
411                         cabalconf+=(--enable-shared)
412
413         if $(ghc-supports-shared-libraries); then
414                 # Experimental support for dynamically linked binaries.
415                 # We are enabling it since 7.10.1_rc3
416                 if version_is_at_least "7.10.0.20150316" "$(ghc-version)"; then
417                         # we didn't enable it before as it was not stable on all arches
418                         cabalconf+=(--enable-shared)
419                         # Known to break on ghc-7.8/Cabal-1.18
420                         # https://ghc.haskell.org/trac/ghc/ticket/9625
421                         cabalconf+=(--enable-executable-dynamic)
422                 fi
423         fi
424
425         # --sysconfdir appeared in Cabal-1.18+
426         if ./setup configure --help | grep -q -- --sysconfdir; then
427                 cabalconf+=(--sysconfdir="${EPREFIX}"/etc)
428         fi
429
430         # appeared in Cabal-1.18+ (see '--disable-executable-stripping')
431         if ./setup configure --help | grep -q -- --disable-library-stripping; then
432                 cabalconf+=(--disable-library-stripping)
433         fi
434
435         set -- configure \
436                 --ghc --prefix="${EPREFIX}"/usr \
437                 --with-compiler="$(ghc-getghc)" \
438                 --with-hc-pkg="$(ghc-getghcpkg)" \
439                 --prefix="${EPREFIX}"/usr \
440                 --libdir="${EPREFIX}"/usr/$(get_libdir) \
441                 --libsubdir=${P}/ghc-$(ghc-version) \
442                 --datadir="${EPREFIX}"/usr/share/ \
443                 --datasubdir=${P}/ghc-$(ghc-version) \
444                 "${cabalconf[@]}" \
445                 ${CABAL_CONFIGURE_FLAGS} \
446                 ${CABAL_EXTRA_CONFIGURE_FLAGS} \
447                 "$@"
448         echo ./setup "$@"
449         ./setup "$@" || cabal-show-brokens-and-die "setup configure failed"
450 }
451
452 cabal-build() {
453         set --  build ${CABAL_EXTRA_BUILD_FLAGS} "$@"
454         echo ./setup "$@"
455         ./setup "$@" \
456                 || die "setup build failed"
457 }
458
459 cabal-copy() {
460         has "${EAPI:-0}" 0 1 2 && ! use prefix && ED=${D}
461
462         set -- copy --destdir="${D}" "$@"
463         echo ./setup "$@"
464         ./setup "$@" || die "setup copy failed"
465
466         # cabal is a bit eager about creating dirs,
467         # so remove them if they are empty
468         rmdir "${ED}/usr/bin" 2> /dev/null
469 }
470
471 cabal-pkg() {
472         # This does not actually register since we're using true instead
473         # of ghc-pkg. So it just leaves the .conf file and we can
474         # register that ourselves (if it exists).
475
476         if [[ -n ${CABAL_HAS_LIBRARIES} ]]; then
477                 # Newer cabal can generate a package conf for us:
478                 ./setup register --gen-pkg-config="${T}/${P}.conf"
479                 ghc-install-pkg "${T}/${P}.conf"
480         fi
481 }
482
483 # Some cabal libs are bundled along with some versions of ghc
484 # eg filepath-1.0 comes with ghc-6.6.1
485 # by putting CABAL_CORE_LIB_GHC_PV="6.6.1" in an ebuild we are declaring that
486 # when building with this version of ghc, the ebuild is a dummy that is it will
487 # install no files since the package is already included with ghc.
488 # However portage still records the dependency and we can upgrade the package
489 # to a later one that's not included with ghc.
490 # You can also put a space separated list, eg CABAL_CORE_LIB_GHC_PV="6.6 6.6.1".
491 # Those versions are taken as-is from ghc `--numeric-version`.
492 # Package manager versions are also supported:
493 #     CABAL_CORE_LIB_GHC_PV="7.10.* PM:7.8.4-r1".
494 cabal-is-dummy-lib() {
495         local bin_ghc_version=$(ghc-version)
496         local pm_ghc_version=$(ghc-pm-version)
497
498         for version in ${CABAL_CORE_LIB_GHC_PV}; do
499                 [[ "${bin_ghc_version}" == ${version} ]] && return 0
500                 [[ "${pm_ghc_version}"  == ${version} ]] && return 0
501         done
502
503         return 1
504 }
505
506 # exported function: check if cabal is correctly installed for
507 # the currently active ghc (we cannot guarantee this with portage)
508 haskell-cabal_pkg_setup() {
509         if [[ -n ${CABAL_HAS_LIBRARIES} ]]; then
510                 [[ ${RDEPEND} == *dev-lang/ghc* ]] || eqawarn "QA Notice: A library does not have runtime dependency on dev-lang/ghc."
511         fi
512         if [[ -z "${CABAL_HAS_BINARIES}" ]] && [[ -z "${CABAL_HAS_LIBRARIES}" ]]; then
513                 eqawarn "QA Notice: Neither bin nor lib are in CABAL_FEATURES."
514         fi
515         if [[ -n "${CABAL_UNKNOWN}" ]]; then
516                 eqawarn "QA Notice: Unknown entry in CABAL_FEATURES: ${CABAL_UNKNOWN}"
517         fi
518         if cabal-is-dummy-lib; then
519                 einfo "${P} is included in ghc-${CABAL_CORE_LIB_GHC_PV}, nothing to install."
520         fi
521 }
522
523 haskell-cabal_src_configure() {
524         cabal-is-dummy-lib && return
525
526         pushd "${S}" > /dev/null || die
527
528         cabal-bootstrap
529
530         cabal-configure "$@"
531
532         popd > /dev/null || die
533 }
534
535 # exported function: nice alias
536 cabal_src_configure() {
537         haskell-cabal_src_configure "$@"
538 }
539
540 # exported function: cabal-style bootstrap configure and compile
541 cabal_src_compile() {
542         # it's a common mistake when one bumps ebuild to EAPI="2" (and upper)
543         # and forgets to separate src_compile() to src_configure()/src_compile().
544         # Such error leads to default src_configure and we lose all passed flags.
545         if ! has "${EAPI:-0}" 0 1; then
546                 local passed_flag
547                 for passed_flag in "$@"; do
548                         [[ ${passed_flag} == --flags=* ]] && \
549                                 eqawarn "QA Notice: Cabal option '${passed_flag}' has effect only in src_configure()"
550                 done
551         fi
552
553         cabal-is-dummy-lib && return
554
555         has src_configure ${HASKELL_CABAL_EXPF} || haskell-cabal_src_configure "$@"
556         cabal-build
557
558         if [[ -n "${CABAL_USE_HADDOCK}" ]] && use doc; then
559                 if [[ -n "${CABAL_USE_HSCOLOUR}" ]] && use hscolour; then
560                         if [[ -n "${CABAL_USE_HOOGLE}" ]] && use hoogle; then
561                                 # hoogle, hscolour and haddock
562                                 cabal-hoogle-hscolour-haddock
563                         else
564                                 # haddock and hscolour
565                                 cabal-hscolour-haddock
566                         fi
567                 else
568                         if [[ -n "${CABAL_USE_HOOGLE}" ]] && use hoogle; then
569                                 # hoogle and haddock
570                                 cabal-hoogle-haddock
571                         else
572                                 # just haddock
573                                 cabal-haddock
574                         fi
575                 fi
576         else
577                 if [[ -n "${CABAL_USE_HSCOLOUR}" ]] && use hscolour; then
578                         if [[ -n "${CABAL_USE_HOOGLE}" ]] && use hoogle; then
579                                 # hoogle and hscolour
580                                 cabal-hoogle-hscolour
581                         else
582                                 # just hscolour
583                                 cabal-hscolour
584                         fi
585                 else
586                         if [[ -n "${CABAL_USE_HOOGLE}" ]] && use hoogle; then
587                                 # just hoogle
588                                 cabal-hoogle
589                         fi
590                 fi
591         fi
592 }
593
594 haskell-cabal_src_compile() {
595         pushd "${S}" > /dev/null || die
596
597         cabal_src_compile "$@"
598
599         popd > /dev/null || die
600 }
601
602 haskell-cabal_src_test() {
603         pushd "${S}" > /dev/null || die
604
605         if cabal-is-dummy-lib; then
606                 einfo ">>> No tests for dummy library: ${CATEGORY}/${PF}"
607         else
608                 einfo ">>> Test phase [cabal test]: ${CATEGORY}/${PF}"
609                 set -- test "$@"
610                 echo ./setup "$@"
611                 ./setup "$@" || die "cabal test failed"
612         fi
613
614         popd > /dev/null || die
615 }
616
617 # exported function: cabal-style copy and register
618 cabal_src_install() {
619         has "${EAPI:-0}" 0 1 2 && ! use prefix && EPREFIX=
620
621         if ! cabal-is-dummy-lib; then
622                 cabal-copy
623                 cabal-pkg
624         fi
625
626         # create a dummy local package conf file for haskell-updater
627         # if it does not exist (dummy libraries and binaries w/o libraries)
628         local ghc_confdir_with_prefix="$(ghc-confdir)"
629         # remove EPREFIX
630         dodir ${ghc_confdir_with_prefix#${EPREFIX}}
631         local hint_db="${D}/$(ghc-confdir)"
632         local hint_file="${hint_db}/${PF}.conf"
633         mkdir -p "${hint_db}" || die
634         touch "${hint_file}" || die
635 }
636
637 haskell-cabal_src_install() {
638         pushd "${S}" > /dev/null || die
639
640         cabal_src_install
641
642         popd > /dev/null || die
643 }
644
645 haskell-cabal_pkg_postinst() {
646         ghc-package_pkg_postinst
647 }
648
649 haskell-cabal_pkg_postrm() {
650         ghc-package_pkg_postrm
651 }
652
653 # @FUNCTION: cabal_flag
654 # @DESCRIPTION:
655 # ebuild.sh:use_enable() taken as base
656 #
657 # Usage examples:
658 #
659 #     CABAL_CONFIGURE_FLAGS=$(cabal_flag gui)
660 #  leads to "--flags=gui" or "--flags=-gui" (useflag 'gui')
661 #
662 #     CABAL_CONFIGURE_FLAGS=$(cabal_flag gtk gui)
663 #  also leads to "--flags=gui" or " --flags=-gui" (useflag 'gtk')
664 #
665 cabal_flag() {
666         if [[ -z "$1" ]]; then
667                 echo "!!! cabal_flag() called without a parameter." >&2
668                 echo "!!! cabal_flag() <USEFLAG> [<cabal_flagname>]" >&2
669                 return 1
670         fi
671
672         local UWORD=${2:-$1}
673
674         if use "$1"; then
675                 echo "--flags=${UWORD}"
676         else
677                 echo "--flags=-${UWORD}"
678         fi
679
680         return 0
681 }
682
683 # @FUNCTION: cabal_chdeps
684 # @DESCRIPTION:
685 # Allows easier patching of $CABAL_FILE (${S}/${PN}.cabal by default)
686 # depends
687 #
688 # Accepts argument list as pairs of substitutions: <from-string> <to-string>...
689 #
690 # Dies on error.
691 #
692 # Usage examples:
693 #
694 # src_prepare() {
695 #    cabal_chdeps \
696 #        'base >= 4.2 && < 4.6' 'base >= 4.2 && < 4.7' \
697 #        'containers ==0.4.*' 'containers >= 0.4 && < 0.6'
698 #}
699 # or
700 # src_prepare() {
701 #    CABAL_FILE=${S}/${MY_PN}.cabal cabal_chdeps \
702 #        'base >= 4.2 && < 4.6' 'base >= 4.2 && < 4.7'
703 #    CABAL_FILE=${S}/${MY_PN}-tools.cabal cabal_chdeps \
704 #        'base == 3.*' 'base >= 4.2 && < 4.7'
705 #}
706 #
707 cabal_chdeps() {
708         local cabal_fn=${MY_PN:-${PN}}.cabal
709         local cf=${CABAL_FILE:-${S}/${cabal_fn}}
710         local from_ss # ss - substring
711         local to_ss
712         local orig_c # c - contents
713         local new_c
714
715         [[ -f $cf ]] || die "cabal file '$cf' does not exist"
716
717         orig_c=$(< "$cf")
718
719         while :; do
720                 from_pat=$1
721                 to_str=$2
722
723                 [[ -n ${from_pat} ]] || break
724                 [[ -n ${to_str} ]] || die "'${from_str}' does not have 'to' part"
725
726                 einfo "CHDEP: '${from_pat}' -> '${to_str}'"
727
728                 # escape pattern-like symbols
729                 from_pat=${from_pat//\*/\\*}
730                 from_pat=${from_pat//\[/\\[}
731
732                 new_c=${orig_c//${from_pat}/${to_str}}
733
734                 if [[ -n $CABAL_DEBUG_LOOSENING ]]; then
735                         echo "${orig_c}" >"${T}/${cf}".pre
736                         echo "${new_c}" >"${T}/${cf}".post
737                         diff -u "${T}/${cf}".{pre,post}
738                 fi
739
740                 [[ "${orig_c}" == "${new_c}" ]] && die "no trigger for '${from_pat}'"
741                 orig_c=${new_c}
742                 shift
743                 shift
744         done
745
746         echo "${new_c}" > "$cf" ||
747                 die "failed to update"
748 }
749
750 # @FUNCTION: cabal-constraint
751 # @DESCRIPTION:
752 # Allowes to set contraint to the libraries that are
753 # used by specified package
754 cabal-constraint() {
755         while read p v ; do
756                 echo "--constraint \"$p == $v\""
757         done < $(ghc-pkgdeps ${1})
758 }
759
760 # @FUNCTION: replace-hcflags
761 # @USAGE: <old> <new>
762 # @DESCRIPTION:
763 # Replace the <old> flag with <new> in HCFLAGS. Accepts shell globs for <old>.
764 # The implementation is picked from flag-o-matic.eclass:replace-flags()
765 replace-hcflags() {
766         [[ $# != 2 ]] && die "Usage: replace-hcflags <old flag> <new flag>"
767
768         local f new=()
769         for f in ${HCFLAGS} ; do
770                 # Note this should work with globs like -O*
771                 if [[ ${f} == ${1} ]]; then
772                         einfo "HCFLAGS: replacing '${f}' to '${2}'"
773                         f=${2}
774                 fi
775                 new+=( "${f}" )
776         done
777         export HCFLAGS="${new[*]}"
778
779         return 0
780 }