ghc-package.eclass 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. # Copyright 1999-2015 Gentoo Foundation
  2. # Distributed under the terms of the GNU General Public License v2
  3. # @ECLASS: ghc-package.eclass
  4. # @MAINTAINER:
  5. # "Gentoo's Haskell Language team" <haskell@gentoo.org>
  6. # @AUTHOR:
  7. # Original Author: Andres Loeh <kosmikus@gentoo.org>
  8. # @BLURB: This eclass helps with the Glasgow Haskell Compiler's package configuration utility.
  9. # @DESCRIPTION:
  10. # Helper eclass to handle ghc installation/upgrade/deinstallation process.
  11. inherit multiprocessing versionator
  12. # @FUNCTION: ghc-getghc
  13. # @DESCRIPTION:
  14. # returns the name of the ghc executable
  15. ghc-getghc() {
  16. type -P ghc
  17. }
  18. # @FUNCTION: ghc-getghcpkg
  19. # @DESCRIPTION:
  20. # Internal function determines returns the name of the ghc-pkg executable
  21. ghc-getghcpkg() {
  22. type -P ghc-pkg
  23. }
  24. # @FUNCTION: ghc-getghcpkgbin
  25. # @DESCRIPTION:
  26. # returns the name of the ghc-pkg binary (ghc-pkg
  27. # itself usually is a shell script, and we have to
  28. # bypass the script under certain circumstances);
  29. # for Cabal, we add an empty global package config file,
  30. # because for some reason the global package file
  31. # must be specified
  32. ghc-getghcpkgbin() {
  33. if version_is_at_least "7.9.20141222" "$(ghc-version)"; then
  34. # ghc-7.10 stopped supporting single-file database
  35. local empty_db="${T}/empty.conf.d" ghc_pkg="$(ghc-libdir)/bin/ghc-pkg"
  36. if [[ ! -d ${empty_db} ]]; then
  37. "${ghc_pkg}" init "${empty_db}" || die "Failed to initialize empty global db"
  38. fi
  39. echo "$(ghc-libdir)/bin/ghc-pkg" "--global-package-db=${empty_db}"
  40. elif version_is_at_least "7.7.20121101" "$(ghc-version)"; then
  41. # the ghc-pkg executable changed name in ghc 6.10, as it no longer needs
  42. # the wrapper script with the static flags
  43. # was moved to bin/ subtree by:
  44. # http://www.haskell.org/pipermail/cvs-ghc/2012-September/076546.html
  45. echo '[]' > "${T}/empty.conf"
  46. echo "$(ghc-libdir)/bin/ghc-pkg" "--global-package-db=${T}/empty.conf"
  47. elif version_is_at_least "7.5.20120516" "$(ghc-version)"; then
  48. echo '[]' > "${T}/empty.conf"
  49. echo "$(ghc-libdir)/ghc-pkg" "--global-package-db=${T}/empty.conf"
  50. else
  51. echo '[]' > "${T}/empty.conf"
  52. echo "$(ghc-libdir)/ghc-pkg" "--global-conf=${T}/empty.conf"
  53. fi
  54. }
  55. # @FUNCTION: ghc-version
  56. # @DESCRIPTION:
  57. # returns upstream version of ghc
  58. # as reported by '--numeric-version'
  59. # Examples: "7.10.2", "7.9.20141222"
  60. _GHC_VERSION_CACHE=""
  61. ghc-version() {
  62. if [[ -z "${_GHC_VERSION_CACHE}" ]]; then
  63. _GHC_VERSION_CACHE="$($(ghc-getghc) --numeric-version)"
  64. fi
  65. echo "${_GHC_VERSION_CACHE}"
  66. }
  67. # @FUNCTION: ghc-pm-version
  68. # @DESCRIPTION:
  69. # returns package manager(PM) version of ghc
  70. # as reported by '$(best_version)'
  71. # Examples: "PM:7.10.2", "PM:7.10.2_rc1", "PM:7.8.4-r4"
  72. _GHC_PM_VERSION_CACHE=""
  73. ghc-pm-version() {
  74. local pm_ghc_p
  75. if [[ -z "${_GHC_PM_VERSION_CACHE}" ]]; then
  76. pm_ghc_p=$(best_version dev-lang/ghc)
  77. _GHC_PM_VERSION_CACHE="PM:${pm_ghc_p#dev-lang/ghc-}"
  78. fi
  79. echo "${_GHC_PM_VERSION_CACHE}"
  80. }
  81. # @FUNCTION: ghc-cabal-version
  82. # @DESCRIPTION:
  83. # return version of the Cabal library bundled with ghc
  84. ghc-cabal-version() {
  85. if version_is_at_least "7.9.20141222" "$(ghc-version)"; then
  86. # outputs in format: 'version: 1.18.1.5'
  87. set -- `$(ghc-getghcpkg) --package-db=$(ghc-libdir)/package.conf.d.initial field Cabal version`
  88. echo "$2"
  89. else
  90. local cabal_package=`echo "$(ghc-libdir)"/Cabal-*`
  91. # /path/to/ghc/Cabal-${VER} -> ${VER}
  92. echo "${cabal_package/*Cabal-/}"
  93. fi
  94. }
  95. # @FUNCTION: ghc-is-dynamic
  96. # @DESCRIPTION:
  97. # checks if ghc is built against dynamic libraries
  98. # binaries linked against GHC library (and using plugin loading)
  99. # have to be linked the same way:
  100. # https://ghc.haskell.org/trac/ghc/ticket/10301
  101. ghc-is-dynamic() {
  102. $(ghc-getghc) --info | grep "GHC Dynamic" | grep -q "YES"
  103. }
  104. # @FUNCTION: ghc-supports-shared-libraries
  105. # @DESCRIPTION:
  106. # checks if ghc is built with support for building
  107. # shared libraries (aka '-dynamic' option)
  108. ghc-supports-shared-libraries() {
  109. $(ghc-getghc) --info | grep "RTS ways" | grep -q "dyn"
  110. }
  111. # @FUNCTION: ghc-supports-threaded-runtime
  112. # @DESCRIPTION:
  113. # checks if ghc is built with support for threaded
  114. # runtime (aka '-threaded' option)
  115. ghc-supports-threaded-runtime() {
  116. $(ghc-getghc) --info | grep "RTS ways" | grep -q "thr"
  117. }
  118. # @FUNCTION: ghc-supports-smp
  119. # @DESCRIPTION:
  120. # checks if ghc is built with support for multiple cores runtime
  121. ghc-supports-smp() {
  122. $(ghc-getghc) --info | grep "Support SMP" | grep -q "YES"
  123. }
  124. # @FUNCTION: ghc-supports-interpreter
  125. # @DESCRIPTION:
  126. # checks if ghc has interpreter mode (aka GHCi)
  127. # It usually means that ghc supports for template haskell.
  128. ghc-supports-interpreter() {
  129. $(ghc-getghc) --info | grep "Have interpreter" | grep -q "YES"
  130. }
  131. # @FUNCTION: ghc-supports-parallel-make
  132. # @DESCRIPTION:
  133. # checks if ghc has support for '--make -j' mode
  134. # The option was introduced in ghc-7.8-rc1.
  135. ghc-supports-parallel-make() {
  136. $(ghc-getghc) --info | grep "Support parallel --make" | grep -q "YES"
  137. }
  138. # @FUNCTION: ghc-extractportageversion
  139. # @DESCRIPTION:
  140. # extract the version of a portage-installed package
  141. ghc-extractportageversion() {
  142. local pkg
  143. local version
  144. pkg="$(best_version $1)"
  145. version="${pkg#$1-}"
  146. version="${version%-r*}"
  147. version="${version%_pre*}"
  148. echo "${version}"
  149. }
  150. # @FUNCTION: ghc-libdir
  151. # @DESCRIPTION:
  152. # returns the library directory
  153. _GHC_LIBDIR_CACHE=""
  154. ghc-libdir() {
  155. if [[ -z "${_GHC_LIBDIR_CACHE}" ]]; then
  156. _GHC_LIBDIR_CACHE="$($(ghc-getghc) --print-libdir)"
  157. fi
  158. echo "${_GHC_LIBDIR_CACHE}"
  159. }
  160. # @FUNCTION: ghc-make-args
  161. # @DESCRIPTION:
  162. # Returns default arguments passed along 'ghc --make'
  163. # build mode. Used mainly to enable parallel build mode.
  164. ghc-make-args() {
  165. local ghc_make_args=()
  166. # parallel on all available cores
  167. if ghc-supports-smp && ghc-supports-parallel-make; then
  168. # It should have been just -j$(makeopts_jobs)
  169. # but GHC does not yet have nice defaults:
  170. # https://ghc.haskell.org/trac/ghc/ticket/9221#comment:57
  171. # SMP is a requirement for parallel GC's gen0
  172. # 'qb' balancing.
  173. echo "-j$(makeopts_jobs) +RTS -A256M -qb0 -RTS"
  174. ghc_make_args=()
  175. fi
  176. echo "${ghc_make_args[@]}"
  177. }
  178. # @FUNCTION: ghc-confdir
  179. # @DESCRIPTION:
  180. # returns the (Gentoo) library configuration directory, we
  181. # store here a hint for 'haskell-updater' about packages
  182. # installed for old ghc versions and current ones.
  183. ghc-confdir() {
  184. echo "$(ghc-libdir)/gentoo"
  185. }
  186. # @FUNCTION: ghc-package-db
  187. # @DESCRIPTION:
  188. # returns the global package database directory
  189. ghc-package-db() {
  190. echo "$(ghc-libdir)/package.conf.d"
  191. }
  192. # @FUNCTION: ghc-localpkgconfd
  193. # @DESCRIPTION:
  194. # returns the name of the local (package-specific)
  195. # package configuration file
  196. ghc-localpkgconfd() {
  197. echo "${PF}.conf.d"
  198. }
  199. # @FUNCTION: ghc-package-exists
  200. # @DESCRIPTION:
  201. # tests if a ghc package exists
  202. ghc-package-exists() {
  203. $(ghc-getghcpkg) describe "$1" > /dev/null 2>&1
  204. }
  205. # @FUNCTION: check-for-collisions
  206. # @DESCRIPTION:
  207. # makes sure no packages
  208. # have the same version as initial package setup
  209. check-for-collisions() {
  210. local localpkgconf=$1
  211. local checked_pkg
  212. local initial_pkg_db="$(ghc-libdir)/package.conf.d.initial"
  213. for checked_pkg in `$(ghc-getghcpkgbin) -f "${localpkgconf}" list --simple-output`
  214. do
  215. # should return empty output
  216. local collided=`$(ghc-getghcpkgbin) -f ${initial_pkg_db} list --simple-output "${checked_pkg}"`
  217. if [[ -n ${collided} ]]; then
  218. eerror "Cabal package '${checked_pkg}' is shipped with '$(ghc-pm-version)' ('$(ghc-version)')."
  219. eerror "Ebuild author forgot an entry in CABAL_CORE_LIB_GHC_PV='${CABAL_CORE_LIB_GHC_PV}'."
  220. eerror "Found in ${initial_pkg_db}."
  221. die
  222. fi
  223. done
  224. }
  225. # @FUNCTION: ghc-install-pkg
  226. # @DESCRIPTION:
  227. # moves the local (package-specific) package configuration
  228. # file to its final destination
  229. ghc-install-pkg() {
  230. local pkg_config_file=$1
  231. local localpkgconf="${T}/$(ghc-localpkgconfd)"
  232. local pkg_path pkg pkg_db="${D}/$(ghc-package-db)" hint_db="${D}/$(ghc-confdir)"
  233. $(ghc-getghcpkgbin) init "${localpkgconf}" || die "Failed to initialize empty local db"
  234. $(ghc-getghcpkgbin) -f "${localpkgconf}" update - --force \
  235. < "${pkg_config_file}" || die "failed to register ${pkg}"
  236. check-for-collisions "${localpkgconf}"
  237. mkdir -p "${pkg_db}" || die
  238. for pkg_path in "${localpkgconf}"/*.conf; do
  239. pkg=$(basename "${pkg_path}")
  240. cp "${pkg_path}" "${pkg_db}/${pkg}" || die
  241. done
  242. mkdir -p "${hint_db}" || die
  243. cp "${pkg_config_file}" "${hint_db}/${PF}.conf" || die
  244. chmod 0644 "${hint_db}/${PF}.conf" || die
  245. }
  246. # @FUNCTION: ghc-recache-db
  247. # @DESCRIPTION:
  248. # updates 'package.cache' binary cacne for registered '*.conf'
  249. # packages
  250. ghc-recache-db() {
  251. einfo "Recaching GHC package DB"
  252. $(ghc-getghcpkg) recache
  253. }
  254. # @FUNCTION: ghc-register-pkg
  255. # @DESCRIPTION:
  256. # registers all packages in the local (package-specific)
  257. # package configuration file
  258. ghc-register-pkg() {
  259. ghc-recache-db
  260. }
  261. # @FUNCTION: ghc-reregister
  262. # @DESCRIPTION:
  263. # re-adds all available .conf files to the global
  264. # package conf file, to be used on a ghc reinstallation
  265. ghc-reregister() {
  266. ghc-recache-db
  267. }
  268. # @FUNCTION: ghc-unregister-pkg
  269. # @DESCRIPTION:
  270. # unregisters a package configuration file
  271. ghc-unregister-pkg() {
  272. ghc-recache-db
  273. }
  274. # @FUNCTION: ghc-pkgdeps
  275. # @DESCRIPTION:
  276. # exported function: loads a package dependency in a form
  277. # cabal_package version
  278. ghc-pkgdeps() {
  279. echo $($(ghc-getghcpkg) describe "${1}") \
  280. | sed \
  281. -e '/depends/,/^.*:/ !d' \
  282. -e 's/\(.*\)-\(.*\)-\(.*\)/\1 \2/' \
  283. -e 's/^.*://g'
  284. }
  285. # @FUNCTION: ghc-package_pkg_postinst
  286. # @DESCRIPTION:
  287. # updates package.cache after package install
  288. ghc-package_pkg_postinst() {
  289. ghc-recache-db
  290. }
  291. # @FUNCTION: ghc-package_pkg_prerm
  292. # @DESCRIPTION:
  293. # updates package.cache after package deinstall
  294. ghc-package_pkg_prerm() {
  295. ewarn "ghc-package.eclass: 'ghc-package_pkg_prerm()' is a noop"
  296. ewarn "ghc-package.eclass: consider 'haskell-cabal_pkg_postrm()' instead"
  297. }
  298. # @FUNCTION: ghc-package_pkg_postrm
  299. # @DESCRIPTION:
  300. # updates package.cache after package deinstall
  301. ghc-package_pkg_postrm() {
  302. ghc-recache-db
  303. }