1 # Copyright 1999-2018 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: ghc-package.eclass
6 # "Gentoo's Haskell Language team" <haskell@gentoo.org>
8 # Original Author: Andres Loeh <kosmikus@gentoo.org>
9 # @BLURB: This eclass helps with the Glasgow Haskell Compiler's package configuration utility.
11 # Helper eclass to handle ghc installation/upgrade/deinstallation process.
13 inherit multiprocessing
15 # Maintain version-testing compatibility with ebuilds not using EAPI 7.
17 4|5|6) inherit eapi7-ver ;;
21 # @FUNCTION: ghc-getghc
23 # returns the name of the ghc executable
28 # @FUNCTION: ghc-getghcpkg
30 # Internal function determines returns the name of the ghc-pkg executable
32 type -P ${HC_PKG:-ghc-pkg}
35 # @FUNCTION: ghc-getghcpkgbin
37 # returns the name of the ghc-pkg binary (ghc-pkg
38 # itself usually is a shell script, and we have to
39 # bypass the script under certain circumstances);
40 # for Cabal, we add an empty global package config file,
41 # because for some reason the global package file
44 if ver_test "$(ghc-version)" -ge "7.9.20141222"; then
45 # ghc-7.10 stopped supporting single-file database
46 local empty_db="${T}/empty.conf.d" ghc_pkg="$(ghc-libdir)/bin/ghc-pkg"
47 if [[ ! -d ${empty_db} ]]; then
48 "${ghc_pkg}" init "${empty_db}" || die "Failed to initialize empty global db"
50 echo "$(ghc-libdir)/bin/ghc-pkg" "--global-package-db=${empty_db}"
52 elif ver_test "$(ghc-version)" -ge "7.7.20121101"; then
53 # the ghc-pkg executable changed name in ghc 6.10, as it no longer needs
54 # the wrapper script with the static flags
55 # was moved to bin/ subtree by:
56 # http://www.haskell.org/pipermail/cvs-ghc/2012-September/076546.html
57 echo '[]' > "${T}/empty.conf"
58 echo "$(ghc-libdir)/bin/ghc-pkg" "--global-package-db=${T}/empty.conf"
60 elif ver_test "$(ghc-version)" -ge "7.5.20120516"; then
61 echo '[]' > "${T}/empty.conf"
62 echo "$(ghc-libdir)/ghc-pkg" "--global-package-db=${T}/empty.conf"
65 echo '[]' > "${T}/empty.conf"
66 echo "$(ghc-libdir)/ghc-pkg" "--global-conf=${T}/empty.conf"
70 # @FUNCTION: ghc-version
72 # returns upstream version of ghc
73 # as reported by '--numeric-version'
74 # Examples: "7.10.2", "7.9.20141222"
77 if [[ -z "${_GHC_VERSION_CACHE}" ]]; then
78 _GHC_VERSION_CACHE="$($(ghc-getghc) --numeric-version)"
80 echo "${_GHC_VERSION_CACHE}"
83 # @FUNCTION: ghc-pm-version
85 # returns package manager(PM) version of ghc
86 # as reported by '$(best_version)'
87 # Examples: "PM:7.10.2", "PM:7.10.2_rc1", "PM:7.8.4-r4"
88 _GHC_PM_VERSION_CACHE=""
92 if [[ -z "${_GHC_PM_VERSION_CACHE}" ]]; then
93 pm_ghc_p=$(best_version dev-lang/ghc)
94 _GHC_PM_VERSION_CACHE="PM:${pm_ghc_p#dev-lang/ghc-}"
96 echo "${_GHC_PM_VERSION_CACHE}"
99 # @FUNCTION: ghc-cabal-version
101 # return version of the Cabal library bundled with ghc
102 ghc-cabal-version() {
103 if ver_test "$(ghc-version)" -ge "7.9.20141222"; then
104 # outputs in format: 'version: 1.18.1.5'
105 set -- `$(ghc-getghcpkg) --package-db=$(ghc-libdir)/package.conf.d.initial field Cabal version`
108 local cabal_package=`echo "$(ghc-libdir)"/Cabal-*`
109 # /path/to/ghc/Cabal-${VER} -> ${VER}
110 echo "${cabal_package/*Cabal-/}"
114 # @FUNCTION: ghc-is-dynamic
116 # checks if ghc is built against dynamic libraries
117 # binaries linked against GHC library (and using plugin loading)
118 # have to be linked the same way:
119 # https://ghc.haskell.org/trac/ghc/ticket/10301
121 $(ghc-getghc) --info | grep "GHC Dynamic" | grep -q "YES"
124 # @FUNCTION: ghc-supports-shared-libraries
126 # checks if ghc is built with support for building
127 # shared libraries (aka '-dynamic' option)
128 ghc-supports-shared-libraries() {
129 $(ghc-getghc) --info | grep "RTS ways" | grep -q "dyn"
132 # @FUNCTION: ghc-supports-threaded-runtime
134 # checks if ghc is built with support for threaded
135 # runtime (aka '-threaded' option)
136 ghc-supports-threaded-runtime() {
137 $(ghc-getghc) --info | grep "RTS ways" | grep -q "thr"
140 # @FUNCTION: ghc-supports-smp
142 # checks if ghc is built with support for multiple cores runtime
144 $(ghc-getghc) --info | grep "Support SMP" | grep -q "YES"
147 # @FUNCTION: ghc-supports-interpreter
149 # checks if ghc has interpreter mode (aka GHCi)
150 # It usually means that ghc supports for template haskell.
151 ghc-supports-interpreter() {
152 $(ghc-getghc) --info | grep "Have interpreter" | grep -q "YES"
155 # @FUNCTION: ghc-supports-parallel-make
157 # checks if ghc has support for '--make -j' mode
158 # The option was introduced in ghc-7.8-rc1.
159 ghc-supports-parallel-make() {
160 $(ghc-getghc) --info | grep "Support parallel --make" | grep -q "YES"
163 # @FUNCTION: ghc-extractportageversion
165 # extract the version of a portage-installed package
166 ghc-extractportageversion() {
169 pkg="$(best_version $1)"
171 version="${version%-r*}"
172 version="${version%_pre*}"
176 # @FUNCTION: ghc-libdir
178 # returns the library directory
181 if [[ -z "${_GHC_LIBDIR_CACHE}" ]]; then
182 _GHC_LIBDIR_CACHE="$($(ghc-getghc) --print-libdir)"
184 echo "${_GHC_LIBDIR_CACHE}"
187 # @FUNCTION: ghc-make-args
189 # Returns default arguments passed along 'ghc --make'
190 # build mode. Used mainly to enable parallel build mode.
192 local ghc_make_args=()
193 # parallel on all available cores
194 if ghc-supports-smp && ghc-supports-parallel-make; then
195 # It should have been just -j$(makeopts_jobs)
196 # but GHC does not yet have nice defaults:
197 # https://ghc.haskell.org/trac/ghc/ticket/9221#comment:57
198 # SMP is a requirement for parallel GC's gen0
200 echo "-j$(makeopts_jobs) +RTS -A256M -qb0 -RTS"
203 echo "${ghc_make_args[@]}"
206 # @FUNCTION: ghc-confdir
208 # returns the (Gentoo) library configuration directory, we
209 # store here a hint for 'haskell-updater' about packages
210 # installed for old ghc versions and current ones.
212 echo "$(ghc-libdir)/gentoo"
215 # @FUNCTION: ghc-package-db
217 # returns the global package database directory
219 echo "$(ghc-libdir)/package.conf.d"
222 # @FUNCTION: ghc-localpkgconfd
224 # returns the name of the local (package-specific)
225 # package configuration file
226 ghc-localpkgconfd() {
230 # @FUNCTION: ghc-package-exists
232 # tests if a ghc package exists
233 ghc-package-exists() {
234 $(ghc-getghcpkg) describe "$1" > /dev/null 2>&1
237 # @FUNCTION: check-for-collisions
239 # makes sure no packages
240 # have the same version as initial package setup
241 check-for-collisions() {
242 local localpkgconf=$1
244 local initial_pkg_db="$(ghc-libdir)/package.conf.d.initial"
246 for checked_pkg in `$(ghc-getghcpkgbin) -f "${localpkgconf}" list --simple-output`
248 # should return empty output
249 local collided=`$(ghc-getghcpkgbin) -f ${initial_pkg_db} list --simple-output "${checked_pkg}"`
251 if [[ -n ${collided} ]]; then
252 eerror "Cabal package '${checked_pkg}' is shipped with '$(ghc-pm-version)' ('$(ghc-version)')."
253 eerror "Ebuild author forgot an entry in CABAL_CORE_LIB_GHC_PV='${CABAL_CORE_LIB_GHC_PV}'."
254 eerror "Found in ${initial_pkg_db}."
260 # @FUNCTION: ghc-install-pkg
262 # moves the local (package-specific) package configuration
263 # file to its final destination
265 local localpkgconf="${T}/$(ghc-localpkgconfd)"
266 local pkg_path pkg pkg_db="${D}/$(ghc-package-db)" hint_db="${D}/$(ghc-confdir)"
268 $(ghc-getghcpkgbin) init "${localpkgconf}" || die "Failed to initialize empty local db"
269 for pkg_config_file in "$@"; do
270 $(ghc-getghcpkgbin) -f "${localpkgconf}" update - --force \
271 < "${pkg_config_file}" || die "failed to register ${pkg}"
274 check-for-collisions "${localpkgconf}"
276 mkdir -p "${pkg_db}" || die
277 for pkg_path in "${localpkgconf}"/*.conf; do
278 pkg=$(basename "${pkg_path}")
279 cp "${pkg_path}" "${pkg_db}/${pkg}" || die
282 mkdir -p "${hint_db}" || die
283 for pkg_config_file in "$@"; do
284 local pkg_name="gentoo-${CATEGORY}-${PF}-"$(basename "${pkg_config_file}")
285 cp "${pkg_config_file}" "${hint_db}/${pkg_name}" || die
286 chmod 0644 "${hint_db}/${pkg_name}" || die
290 # @FUNCTION: ghc-recache-db
292 # updates 'package.cache' binary cacne for registered '*.conf'
295 einfo "Recaching GHC package DB"
296 $(ghc-getghcpkg) recache
299 # @FUNCTION: ghc-register-pkg
301 # registers all packages in the local (package-specific)
302 # package configuration file
307 # @FUNCTION: ghc-reregister
309 # re-adds all available .conf files to the global
310 # package conf file, to be used on a ghc reinstallation
315 # @FUNCTION: ghc-unregister-pkg
317 # unregisters a package configuration file
318 ghc-unregister-pkg() {
322 # @FUNCTION: ghc-pkgdeps
324 # exported function: loads a package dependency in a form
325 # cabal_package version
327 echo $($(ghc-getghcpkg) describe "${1}") \
329 -e '/depends/,/^.*:/ !d' \
330 -e 's/\(.*\)-\(.*\)-\(.*\)/\1 \2/' \
334 # @FUNCTION: ghc-package_pkg_postinst
336 # updates package.cache after package install
337 ghc-package_pkg_postinst() {
341 # @FUNCTION: ghc-package_pkg_prerm
343 # updates package.cache after package deinstall
344 ghc-package_pkg_prerm() {
345 ewarn "ghc-package.eclass: 'ghc-package_pkg_prerm()' is a noop"
346 ewarn "ghc-package.eclass: consider 'haskell-cabal_pkg_postrm()' instead"
349 # @FUNCTION: ghc-package_pkg_postrm
351 # updates package.cache after package deinstall
352 ghc-package_pkg_postrm() {