1 # Copyright 1999-2015 Gentoo Foundation
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 versionator
15 # @FUNCTION: ghc-getghc
17 # returns the name of the ghc executable
22 # @FUNCTION: ghc-getghcpkg
24 # Internal function determines returns the name of the ghc-pkg executable
26 type -P ${HC_PKG:-ghc-pkg}
29 # @FUNCTION: ghc-getghcpkgbin
31 # returns the name of the ghc-pkg binary (ghc-pkg
32 # itself usually is a shell script, and we have to
33 # bypass the script under certain circumstances);
34 # for Cabal, we add an empty global package config file,
35 # because for some reason the global package file
38 if version_is_at_least "7.9.20141222" "$(ghc-version)"; then
39 # ghc-7.10 stopped supporting single-file database
40 local empty_db="${T}/empty.conf.d" ghc_pkg="$(ghc-libdir)/bin/ghc-pkg"
41 if [[ ! -d ${empty_db} ]]; then
42 "${ghc_pkg}" init "${empty_db}" || die "Failed to initialize empty global db"
44 echo "$(ghc-libdir)/bin/ghc-pkg" "--global-package-db=${empty_db}"
46 elif version_is_at_least "7.7.20121101" "$(ghc-version)"; then
47 # the ghc-pkg executable changed name in ghc 6.10, as it no longer needs
48 # the wrapper script with the static flags
49 # was moved to bin/ subtree by:
50 # http://www.haskell.org/pipermail/cvs-ghc/2012-September/076546.html
51 echo '[]' > "${T}/empty.conf"
52 echo "$(ghc-libdir)/bin/ghc-pkg" "--global-package-db=${T}/empty.conf"
54 elif version_is_at_least "7.5.20120516" "$(ghc-version)"; then
55 echo '[]' > "${T}/empty.conf"
56 echo "$(ghc-libdir)/ghc-pkg" "--global-package-db=${T}/empty.conf"
59 echo '[]' > "${T}/empty.conf"
60 echo "$(ghc-libdir)/ghc-pkg" "--global-conf=${T}/empty.conf"
64 # @FUNCTION: ghc-version
66 # returns upstream version of ghc
67 # as reported by '--numeric-version'
68 # Examples: "7.10.2", "7.9.20141222"
71 if [[ -z "${_GHC_VERSION_CACHE}" ]]; then
72 _GHC_VERSION_CACHE="$($(ghc-getghc) --numeric-version)"
74 echo "${_GHC_VERSION_CACHE}"
77 # @FUNCTION: ghc-pm-version
79 # returns package manager(PM) version of ghc
80 # as reported by '$(best_version)'
81 # Examples: "PM:7.10.2", "PM:7.10.2_rc1", "PM:7.8.4-r4"
82 _GHC_PM_VERSION_CACHE=""
86 if [[ -z "${_GHC_PM_VERSION_CACHE}" ]]; then
87 pm_ghc_p=$(best_version dev-lang/ghc)
88 _GHC_PM_VERSION_CACHE="PM:${pm_ghc_p#dev-lang/ghc-}"
90 echo "${_GHC_PM_VERSION_CACHE}"
93 # @FUNCTION: ghc-cabal-version
95 # return version of the Cabal library bundled with ghc
97 if version_is_at_least "7.9.20141222" "$(ghc-version)"; then
98 # outputs in format: 'version: 1.18.1.5'
99 set -- `$(ghc-getghcpkg) --package-db=$(ghc-libdir)/package.conf.d.initial field Cabal version`
102 local cabal_package=`echo "$(ghc-libdir)"/Cabal-*`
103 # /path/to/ghc/Cabal-${VER} -> ${VER}
104 echo "${cabal_package/*Cabal-/}"
108 # @FUNCTION: ghc-is-dynamic
110 # checks if ghc is built against dynamic libraries
111 # binaries linked against GHC library (and using plugin loading)
112 # have to be linked the same way:
113 # https://ghc.haskell.org/trac/ghc/ticket/10301
115 $(ghc-getghc) --info | grep "GHC Dynamic" | grep -q "YES"
118 # @FUNCTION: ghc-supports-shared-libraries
120 # checks if ghc is built with support for building
121 # shared libraries (aka '-dynamic' option)
122 ghc-supports-shared-libraries() {
123 $(ghc-getghc) --info | grep "RTS ways" | grep -q "dyn"
126 # @FUNCTION: ghc-supports-threaded-runtime
128 # checks if ghc is built with support for threaded
129 # runtime (aka '-threaded' option)
130 ghc-supports-threaded-runtime() {
131 $(ghc-getghc) --info | grep "RTS ways" | grep -q "thr"
134 # @FUNCTION: ghc-supports-smp
136 # checks if ghc is built with support for multiple cores runtime
138 $(ghc-getghc) --info | grep "Support SMP" | grep -q "YES"
141 # @FUNCTION: ghc-supports-interpreter
143 # checks if ghc has interpreter mode (aka GHCi)
144 # It usually means that ghc supports for template haskell.
145 ghc-supports-interpreter() {
146 $(ghc-getghc) --info | grep "Have interpreter" | grep -q "YES"
149 # @FUNCTION: ghc-supports-parallel-make
151 # checks if ghc has support for '--make -j' mode
152 # The option was introduced in ghc-7.8-rc1.
153 ghc-supports-parallel-make() {
154 $(ghc-getghc) --info | grep "Support parallel --make" | grep -q "YES"
157 # @FUNCTION: ghc-extractportageversion
159 # extract the version of a portage-installed package
160 ghc-extractportageversion() {
163 pkg="$(best_version $1)"
165 version="${version%-r*}"
166 version="${version%_pre*}"
170 # @FUNCTION: ghc-libdir
172 # returns the library directory
175 if [[ -z "${_GHC_LIBDIR_CACHE}" ]]; then
176 _GHC_LIBDIR_CACHE="$($(ghc-getghc) --print-libdir)"
178 echo "${_GHC_LIBDIR_CACHE}"
181 # @FUNCTION: ghc-make-args
183 # Returns default arguments passed along 'ghc --make'
184 # build mode. Used mainly to enable parallel build mode.
186 local ghc_make_args=()
187 # parallel on all available cores
188 if ghc-supports-smp && ghc-supports-parallel-make; then
189 # It should have been just -j$(makeopts_jobs)
190 # but GHC does not yet have nice defaults:
191 # https://ghc.haskell.org/trac/ghc/ticket/9221#comment:57
192 # SMP is a requirement for parallel GC's gen0
194 echo "-j$(makeopts_jobs) +RTS -A256M -qb0 -RTS"
197 echo "${ghc_make_args[@]}"
200 # @FUNCTION: ghc-confdir
202 # returns the (Gentoo) library configuration directory, we
203 # store here a hint for 'haskell-updater' about packages
204 # installed for old ghc versions and current ones.
206 echo "$(ghc-libdir)/gentoo"
209 # @FUNCTION: ghc-package-db
211 # returns the global package database directory
213 echo "$(ghc-libdir)/package.conf.d"
216 # @FUNCTION: ghc-localpkgconfd
218 # returns the name of the local (package-specific)
219 # package configuration file
220 ghc-localpkgconfd() {
224 # @FUNCTION: ghc-package-exists
226 # tests if a ghc package exists
227 ghc-package-exists() {
228 $(ghc-getghcpkg) describe "$1" > /dev/null 2>&1
231 # @FUNCTION: check-for-collisions
233 # makes sure no packages
234 # have the same version as initial package setup
235 check-for-collisions() {
236 local localpkgconf=$1
238 local initial_pkg_db="$(ghc-libdir)/package.conf.d.initial"
240 for checked_pkg in `$(ghc-getghcpkgbin) -f "${localpkgconf}" list --simple-output`
242 # should return empty output
243 local collided=`$(ghc-getghcpkgbin) -f ${initial_pkg_db} list --simple-output "${checked_pkg}"`
245 if [[ -n ${collided} ]]; then
246 eerror "Cabal package '${checked_pkg}' is shipped with '$(ghc-pm-version)' ('$(ghc-version)')."
247 eerror "Ebuild author forgot an entry in CABAL_CORE_LIB_GHC_PV='${CABAL_CORE_LIB_GHC_PV}'."
248 eerror "Found in ${initial_pkg_db}."
254 # @FUNCTION: ghc-install-pkg
256 # moves the local (package-specific) package configuration
257 # file to its final destination
259 local pkg_config_file=$1
260 local localpkgconf="${T}/$(ghc-localpkgconfd)"
261 local pkg_path pkg pkg_db="${D}/$(ghc-package-db)" hint_db="${D}/$(ghc-confdir)"
263 $(ghc-getghcpkgbin) init "${localpkgconf}" || die "Failed to initialize empty local db"
264 $(ghc-getghcpkgbin) -f "${localpkgconf}" update - --force \
265 < "${pkg_config_file}" || die "failed to register ${pkg}"
267 check-for-collisions "${localpkgconf}"
269 mkdir -p "${pkg_db}" || die
270 for pkg_path in "${localpkgconf}"/*.conf; do
271 pkg=$(basename "${pkg_path}")
272 cp "${pkg_path}" "${pkg_db}/${pkg}" || die
275 mkdir -p "${hint_db}" || die
276 cp "${pkg_config_file}" "${hint_db}/${PF}.conf" || die
277 chmod 0644 "${hint_db}/${PF}.conf" || die
280 # @FUNCTION: ghc-recache-db
282 # updates 'package.cache' binary cacne for registered '*.conf'
285 einfo "Recaching GHC package DB"
286 $(ghc-getghcpkg) recache
289 # @FUNCTION: ghc-register-pkg
291 # registers all packages in the local (package-specific)
292 # package configuration file
297 # @FUNCTION: ghc-reregister
299 # re-adds all available .conf files to the global
300 # package conf file, to be used on a ghc reinstallation
305 # @FUNCTION: ghc-unregister-pkg
307 # unregisters a package configuration file
308 ghc-unregister-pkg() {
312 # @FUNCTION: ghc-pkgdeps
314 # exported function: loads a package dependency in a form
315 # cabal_package version
317 echo $($(ghc-getghcpkg) describe "${1}") \
319 -e '/depends/,/^.*:/ !d' \
320 -e 's/\(.*\)-\(.*\)-\(.*\)/\1 \2/' \
324 # @FUNCTION: ghc-package_pkg_postinst
326 # updates package.cache after package install
327 ghc-package_pkg_postinst() {
331 # @FUNCTION: ghc-package_pkg_prerm
333 # updates package.cache after package deinstall
334 ghc-package_pkg_prerm() {
335 ewarn "ghc-package.eclass: 'ghc-package_pkg_prerm()' is a noop"
336 ewarn "ghc-package.eclass: consider 'haskell-cabal_pkg_postrm()' instead"
339 # @FUNCTION: ghc-package_pkg_postrm
341 # updates package.cache after package deinstall
342 ghc-package_pkg_postrm() {