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