licenses: Add Free-Art-1.3
[gentoo.git] / eclass / haskell-cabal.eclass
1 # Copyright 1999-2018 Gentoo Authors
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|7) 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 ver_test "$(ghc-version)" -ge "7.10.0.20150316"; 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                 if [[ -d "${T}/${P}.conf" ]]; then
432                         ghc-install-pkg "${T}/${P}.conf"/*
433                 else
434                         ghc-install-pkg "${T}/${P}.conf"
435                 fi
436         fi
437 }
438
439 # Some cabal libs are bundled along with some versions of ghc
440 # eg filepath-1.0 comes with ghc-6.6.1
441 # by putting CABAL_CORE_LIB_GHC_PV="6.6.1" in an ebuild we are declaring that
442 # when building with this version of ghc, the ebuild is a dummy that is it will
443 # install no files since the package is already included with ghc.
444 # However portage still records the dependency and we can upgrade the package
445 # to a later one that's not included with ghc.
446 # You can also put a space separated list, eg CABAL_CORE_LIB_GHC_PV="6.6 6.6.1".
447 # Those versions are taken as-is from ghc `--numeric-version`.
448 # Package manager versions are also supported:
449 #     CABAL_CORE_LIB_GHC_PV="7.10.* PM:7.8.4-r1".
450 cabal-is-dummy-lib() {
451         local bin_ghc_version=$(ghc-version)
452         local pm_ghc_version=$(ghc-pm-version)
453
454         for version in ${CABAL_CORE_LIB_GHC_PV}; do
455                 [[ "${bin_ghc_version}" == ${version} ]] && return 0
456                 [[ "${pm_ghc_version}"  == ${version} ]] && return 0
457         done
458
459         return 1
460 }
461
462 # exported function: check if cabal is correctly installed for
463 # the currently active ghc (we cannot guarantee this with portage)
464 haskell-cabal_pkg_setup() {
465         if [[ -n ${CABAL_HAS_LIBRARIES} ]]; then
466                 [[ ${RDEPEND} == *dev-lang/ghc* ]] || eqawarn "QA Notice: A library does not have runtime dependency on dev-lang/ghc."
467         fi
468         if [[ -n "${CABAL_UNKNOWN}" ]]; then
469                 eqawarn "QA Notice: Unknown entry in CABAL_FEATURES: ${CABAL_UNKNOWN}"
470         fi
471         if cabal-is-dummy-lib; then
472                 einfo "${P} is included in ghc-${CABAL_CORE_LIB_GHC_PV}, nothing to install."
473         fi
474 }
475
476 haskell-cabal_src_configure() {
477         cabal-is-dummy-lib && return
478
479         pushd "${S}" > /dev/null || die
480
481         cabal-bootstrap
482
483         cabal-configure "$@"
484
485         popd > /dev/null || die
486 }
487
488 # exported function: nice alias
489 cabal_src_configure() {
490         haskell-cabal_src_configure "$@"
491 }
492
493 # exported function: cabal-style bootstrap configure and compile
494 cabal_src_compile() {
495         # it's a common mistake when one bumps ebuild to EAPI="2" (and upper)
496         # and forgets to separate src_compile() to src_configure()/src_compile().
497         # Such error leads to default src_configure and we lose all passed flags.
498         if ! has "${EAPI:-0}" 0 1; then
499                 local passed_flag
500                 for passed_flag in "$@"; do
501                         [[ ${passed_flag} == --flags=* ]] && \
502                                 eqawarn "QA Notice: Cabal option '${passed_flag}' has effect only in src_configure()"
503                 done
504         fi
505
506         cabal-is-dummy-lib && return
507
508         has src_configure ${HASKELL_CABAL_EXPF} || haskell-cabal_src_configure "$@"
509         cabal-build
510
511         if [[ -n "${CABAL_USE_HADDOCK}" ]] && use doc; then
512                 if [[ -n "${CABAL_USE_HSCOLOUR}" ]] && use hscolour; then
513                         if [[ -n "${CABAL_USE_HOOGLE}" ]] && use hoogle; then
514                                 # hoogle, hscolour and haddock
515                                 cabal-hoogle-hscolour-haddock
516                         else
517                                 # haddock and hscolour
518                                 cabal-hscolour-haddock
519                         fi
520                 else
521                         if [[ -n "${CABAL_USE_HOOGLE}" ]] && use hoogle; then
522                                 # hoogle and haddock
523                                 cabal-hoogle-haddock
524                         else
525                                 # just haddock
526                                 cabal-haddock
527                         fi
528                 fi
529         else
530                 if [[ -n "${CABAL_USE_HSCOLOUR}" ]] && use hscolour; then
531                         if [[ -n "${CABAL_USE_HOOGLE}" ]] && use hoogle; then
532                                 # hoogle and hscolour
533                                 cabal-hoogle-hscolour
534                         else
535                                 # just hscolour
536                                 cabal-hscolour
537                         fi
538                 else
539                         if [[ -n "${CABAL_USE_HOOGLE}" ]] && use hoogle; then
540                                 # just hoogle
541                                 cabal-hoogle
542                         fi
543                 fi
544         fi
545 }
546
547 haskell-cabal_src_compile() {
548         pushd "${S}" > /dev/null || die
549
550         cabal_src_compile "$@"
551
552         popd > /dev/null || die
553 }
554
555 haskell-cabal_src_test() {
556         local cabaltest=()
557
558         pushd "${S}" > /dev/null || die
559
560         if cabal-is-dummy-lib; then
561                 einfo ">>> No tests for dummy library: ${CATEGORY}/${PF}"
562         else
563                 einfo ">>> Test phase [cabal test]: ${CATEGORY}/${PF}"
564
565                 # '--show-details=streaming' appeared in Cabal-1.20
566                 if ./setup test --help | grep -q -- "'streaming'"; then
567                         cabaltest+=(--show-details=streaming)
568                 fi
569
570                 set -- test \
571                         "${cabaltest[@]}" \
572                         ${CABAL_TEST_FLAGS} \
573                         ${CABAL_EXTRA_TEST_FLAGS} \
574                         "$@"
575                 echo ./setup "$@"
576                 ./setup "$@" || die "cabal test failed"
577         fi
578
579         popd > /dev/null || die
580 }
581
582 # exported function: cabal-style copy and register
583 cabal_src_install() {
584         has "${EAPI:-0}" 0 1 2 && ! use prefix && EPREFIX=
585
586         if ! cabal-is-dummy-lib; then
587                 cabal-copy
588                 cabal-pkg
589         fi
590
591         # create a dummy local package conf file for haskell-updater
592         # if it does not exist (dummy libraries and binaries w/o libraries)
593         local ghc_confdir_with_prefix="$(ghc-confdir)"
594         # remove EPREFIX
595         dodir ${ghc_confdir_with_prefix#${EPREFIX}}
596         local hint_db="${D}/$(ghc-confdir)"
597         local hint_file="${hint_db}/gentoo-empty-${CATEGORY}-${PF}.conf"
598         mkdir -p "${hint_db}" || die
599         touch "${hint_file}" || die
600 }
601
602 haskell-cabal_src_install() {
603         pushd "${S}" > /dev/null || die
604
605         cabal_src_install
606
607         popd > /dev/null || die
608 }
609
610 haskell-cabal_pkg_postinst() {
611         ghc-package_pkg_postinst
612 }
613
614 haskell-cabal_pkg_postrm() {
615         ghc-package_pkg_postrm
616 }
617
618 # @FUNCTION: cabal_flag
619 # @DESCRIPTION:
620 # ebuild.sh:use_enable() taken as base
621 #
622 # Usage examples:
623 #
624 #     CABAL_CONFIGURE_FLAGS=$(cabal_flag gui)
625 #  leads to "--flags=gui" or "--flags=-gui" (useflag 'gui')
626 #
627 #     CABAL_CONFIGURE_FLAGS=$(cabal_flag gtk gui)
628 #  also leads to "--flags=gui" or " --flags=-gui" (useflag 'gtk')
629 #
630 cabal_flag() {
631         if [[ -z "$1" ]]; then
632                 echo "!!! cabal_flag() called without a parameter." >&2
633                 echo "!!! cabal_flag() <USEFLAG> [<cabal_flagname>]" >&2
634                 return 1
635         fi
636
637         local UWORD=${2:-$1}
638
639         if use "$1"; then
640                 echo "--flags=${UWORD}"
641         else
642                 echo "--flags=-${UWORD}"
643         fi
644
645         return 0
646 }
647
648 # @FUNCTION: cabal_chdeps
649 # @DESCRIPTION:
650 # Allows easier patching of $CABAL_FILE (${S}/${PN}.cabal by default)
651 # depends
652 #
653 # Accepts argument list as pairs of substitutions: <from-string> <to-string>...
654 #
655 # Dies on error.
656 #
657 # Usage examples:
658 #
659 # src_prepare() {
660 #    cabal_chdeps \
661 #        'base >= 4.2 && < 4.6' 'base >= 4.2 && < 4.7' \
662 #        'containers ==0.4.*' 'containers >= 0.4 && < 0.6'
663 #}
664 # or
665 # src_prepare() {
666 #    CABAL_FILE=${S}/${MY_PN}.cabal cabal_chdeps \
667 #        'base >= 4.2 && < 4.6' 'base >= 4.2 && < 4.7'
668 #    CABAL_FILE=${S}/${MY_PN}-tools.cabal cabal_chdeps \
669 #        'base == 3.*' 'base >= 4.2 && < 4.7'
670 #}
671 #
672 cabal_chdeps() {
673         local cabal_fn=${MY_PN:-${PN}}.cabal
674         local cf=${CABAL_FILE:-${S}/${cabal_fn}}
675         local from_ss # ss - substring
676         local to_ss
677         local orig_c # c - contents
678         local new_c
679
680         [[ -f $cf ]] || die "cabal file '$cf' does not exist"
681
682         orig_c=$(< "$cf")
683
684         while :; do
685                 from_pat=$1
686                 to_str=$2
687
688                 [[ -n ${from_pat} ]] || break
689                 [[ -n ${to_str} ]] || die "'${from_str}' does not have 'to' part"
690
691                 einfo "CHDEP: '${from_pat}' -> '${to_str}'"
692
693                 # escape pattern-like symbols
694                 from_pat=${from_pat//\*/\\*}
695                 from_pat=${from_pat//\[/\\[}
696
697                 new_c=${orig_c//${from_pat}/${to_str}}
698
699                 if [[ -n $CABAL_DEBUG_LOOSENING ]]; then
700                         echo "${orig_c}" >"${T}/${cf}".pre
701                         echo "${new_c}" >"${T}/${cf}".post
702                         diff -u "${T}/${cf}".{pre,post}
703                 fi
704
705                 [[ "${orig_c}" == "${new_c}" ]] && die "no trigger for '${from_pat}'"
706                 orig_c=${new_c}
707                 shift
708                 shift
709         done
710
711         echo "${new_c}" > "$cf" ||
712                 die "failed to update"
713 }
714
715 # @FUNCTION: cabal-constraint
716 # @DESCRIPTION:
717 # Allowes to set contraint to the libraries that are
718 # used by specified package
719 cabal-constraint() {
720         while read p v ; do
721                 echo "--constraint \"$p == $v\""
722         done < $(ghc-pkgdeps ${1})
723 }
724
725 # @FUNCTION: replace-hcflags
726 # @USAGE: <old> <new>
727 # @DESCRIPTION:
728 # Replace the <old> flag with <new> in HCFLAGS. Accepts shell globs for <old>.
729 # The implementation is picked from flag-o-matic.eclass:replace-flags()
730 replace-hcflags() {
731         [[ $# != 2 ]] && die "Usage: replace-hcflags <old flag> <new flag>"
732
733         local f new=()
734         for f in ${HCFLAGS} ; do
735                 # Note this should work with globs like -O*
736                 if [[ ${f} == ${1} ]]; then
737                         einfo "HCFLAGS: replacing '${f}' to '${2}'"
738                         f=${2}
739                 fi
740                 new+=( "${f}" )
741         done
742         export HCFLAGS="${new[*]}"
743
744         return 0
745 }