1 |
# Copyright 1999-2011 Gentoo Foundation |
2 |
# Distributed under the terms of the GNU General Public License v2 |
3 |
# $Header: /var/cvsroot/gentoo-x86/eclass/ghc-package.eclass,v 1.31 2011/08/22 04:46:31 vapier Exp $ |
4 |
|
5 |
# @ECLASS: ghc-package.eclass |
6 |
# @MAINTAINER: |
7 |
# "Gentoo's Haskell Language team" <haskell@gentoo.org> |
8 |
# @AUTHOR: |
9 |
# Original Author: Andres Loeh <kosmikus@gentoo.org> |
10 |
# @BLURB: This eclass helps with the Glasgow Haskell Compiler's package configuration utility. |
11 |
# @DESCRIPTION: |
12 |
# Helper eclass to handle ghc installation/upgrade/deinstallation process. |
13 |
|
14 |
inherit versionator |
15 |
|
16 |
# @FUNCTION: ghc-getghc |
17 |
# @DESCRIPTION: |
18 |
# returns the name of the ghc executable |
19 |
ghc-getghc() { |
20 |
type -P ghc |
21 |
} |
22 |
|
23 |
# @FUNCTION: ghc-getghcpkg |
24 |
# @DESCRIPTION: |
25 |
# Internal function determines returns the name of the ghc-pkg executable |
26 |
ghc-getghcpkg() { |
27 |
type -P ghc-pkg |
28 |
} |
29 |
|
30 |
# @FUNCTION: ghc-getghcpkgbin |
31 |
# @DESCRIPTION: |
32 |
# returns the name of the ghc-pkg binary (ghc-pkg |
33 |
# itself usually is a shell script, and we have to |
34 |
# bypass the script under certain circumstances); |
35 |
# for Cabal, we add an empty global package config file, |
36 |
# because for some reason the global package file |
37 |
# must be specified |
38 |
ghc-getghcpkgbin() { |
39 |
if version_is_at_least "6.10" "$(ghc-version)"; then |
40 |
# the ghc-pkg executable changed name in ghc 6.10, as it no longer needs |
41 |
# the wrapper script with the static flags |
42 |
echo '[]' > "${T}/empty.conf" |
43 |
echo "$(ghc-libdir)/ghc-pkg" "--global-conf=${T}/empty.conf" |
44 |
elif ghc-cabal; then |
45 |
echo '[]' > "${T}/empty.conf" |
46 |
echo "$(ghc-libdir)/ghc-pkg.bin" "--global-conf=${T}/empty.conf" |
47 |
else |
48 |
echo "$(ghc-libdir)/ghc-pkg.bin" |
49 |
fi |
50 |
} |
51 |
|
52 |
# @FUNCTION: ghc-version |
53 |
# @DESCRIPTION: |
54 |
# returns the version of ghc |
55 |
_GHC_VERSION_CACHE="" |
56 |
ghc-version() { |
57 |
if [[ -z "${_GHC_VERSION_CACHE}" ]]; then |
58 |
_GHC_VERSION_CACHE="$($(ghc-getghc) --numeric-version)" |
59 |
fi |
60 |
echo "${_GHC_VERSION_CACHE}" |
61 |
} |
62 |
|
63 |
# @FUNCTION: ghc-cabal |
64 |
# @DESCRIPTION: |
65 |
# this function can be used to determine if ghc itself |
66 |
# uses the Cabal package format; it has nothing to do |
67 |
# with the Cabal libraries ... ghc uses the Cabal package |
68 |
# format since version 6.4 |
69 |
ghc-cabal() { |
70 |
version_is_at_least "6.4" "$(ghc-version)" |
71 |
} |
72 |
|
73 |
# @FUNCTION: ghc-bestcabalversion |
74 |
# @DESCRIPTION: |
75 |
# return the best version of the Cabal library that is available |
76 |
ghc-bestcabalversion() { |
77 |
local cabalversion |
78 |
if ghc-cabal; then |
79 |
# We ask portage, not ghc, so that we only pick up |
80 |
# portage-installed cabal versions. |
81 |
cabalversion="$(ghc-extractportageversion dev-haskell/cabal)" |
82 |
echo "Cabal-${cabalversion}" |
83 |
else |
84 |
# older ghc's don't support package versioning |
85 |
echo Cabal |
86 |
fi |
87 |
} |
88 |
|
89 |
# @FUNCTION: ghc-sanecabal |
90 |
# @DESCRIPTION: |
91 |
# check if a standalone Cabal version is available for the |
92 |
# currently used ghc; takes minimal version of Cabal as |
93 |
# an optional argument |
94 |
ghc-sanecabal() { |
95 |
local f |
96 |
local version |
97 |
if [[ -z "$1" ]]; then version="1.0.1"; else version="$1"; fi |
98 |
for f in $(ghc-confdir)/cabal-*; do |
99 |
[[ -f "${f}" ]] && version_is_at_least "${version}" "${f#*cabal-}" && return |
100 |
done |
101 |
return 1 |
102 |
} |
103 |
|
104 |
# @FUNCTION: ghc-saneghc |
105 |
# @DESCRIPTION: |
106 |
# checks if ghc and ghc-bin are installed in the same version |
107 |
# (if they're both installed); if this is not the case, we |
108 |
# unfortunately cannot trust portage's dependency resolution |
109 |
ghc-saneghc() { |
110 |
local ghcversion |
111 |
local ghcbinversion |
112 |
if [[ "${PN}" == "ghc" || "${PN}" == "ghc-bin" ]]; then |
113 |
return |
114 |
fi |
115 |
if has_version dev-lang/ghc && has_version dev-lang/ghc-bin; then |
116 |
ghcversion="$(ghc-extractportageversion dev-lang/ghc)" |
117 |
ghcbinversion="$(ghc-extractportageversion dev-lang/ghc-bin)" |
118 |
if [[ "${ghcversion}" != "${ghcbinversion}" ]]; then |
119 |
return 1 |
120 |
fi |
121 |
fi |
122 |
return |
123 |
} |
124 |
|
125 |
# @FUNCTION: ghc-supports-shared-libraries |
126 |
# @DESCRIPTION: |
127 |
# checks if ghc is built with support for building |
128 |
# shared libraries (aka '-dynamic' option) |
129 |
ghc-supports-shared-libraries() { |
130 |
$(ghc-getghc) --info | grep "RTS ways" | grep -q "dyn" |
131 |
} |
132 |
|
133 |
# @FUNCTION: ghc-extractportageversion |
134 |
# @DESCRIPTION: |
135 |
# extract the version of a portage-installed package |
136 |
ghc-extractportageversion() { |
137 |
local pkg |
138 |
local version |
139 |
pkg="$(best_version $1)" |
140 |
version="${pkg#$1-}" |
141 |
version="${version%-r*}" |
142 |
version="${version%_pre*}" |
143 |
echo "${version}" |
144 |
} |
145 |
|
146 |
# @FUNCTION: ghc-libdir |
147 |
# @DESCRIPTION: |
148 |
# returns the library directory |
149 |
_GHC_LIBDIR_CACHE="" |
150 |
ghc-libdir() { |
151 |
if [[ -z "${_GHC_LIBDIR_CACHE}" ]]; then |
152 |
_GHC_LIBDIR_CACHE="$($(ghc-getghc) --print-libdir)" |
153 |
fi |
154 |
echo "${_GHC_LIBDIR_CACHE}" |
155 |
} |
156 |
|
157 |
# @FUNCTION: ghc-confdir |
158 |
# @DESCRIPTION: |
159 |
# returns the (Gentoo) library configuration directory |
160 |
ghc-confdir() { |
161 |
echo "$(ghc-libdir)/gentoo" |
162 |
} |
163 |
|
164 |
# @FUNCTION: ghc-localpkgconf |
165 |
# @DESCRIPTION: |
166 |
# returns the name of the local (package-specific) |
167 |
# package configuration file |
168 |
ghc-localpkgconf() { |
169 |
echo "${PF}.conf" |
170 |
} |
171 |
|
172 |
# @FUNCTION: ghc-makeghcilib |
173 |
# @DESCRIPTION: |
174 |
# make a ghci foo.o file from a libfoo.a file |
175 |
ghc-makeghcilib() { |
176 |
local outfile |
177 |
outfile="$(dirname $1)/$(basename $1 | sed 's:^lib\?\(.*\)\.a$:\1.o:')" |
178 |
ld --relocatable --discard-all --output="${outfile}" --whole-archive "$1" |
179 |
} |
180 |
|
181 |
# @FUNCTION: ghc-package-exists |
182 |
# @DESCRIPTION: |
183 |
# tests if a ghc package exists |
184 |
ghc-package-exists() { |
185 |
local describe_flag |
186 |
if version_is_at_least "6.4" "$(ghc-version)"; then |
187 |
describe_flag="describe" |
188 |
else |
189 |
describe_flag="--show-package" |
190 |
fi |
191 |
|
192 |
$(ghc-getghcpkg) "${describe_flag}" "$1" > /dev/null 2>&1 |
193 |
} |
194 |
|
195 |
# @FUNCTION: ghc-setup-pkg |
196 |
# @DESCRIPTION: |
197 |
# creates a local (package-specific) package |
198 |
# configuration file; the arguments should be |
199 |
# uninstalled package description files, each |
200 |
# containing a single package description; if |
201 |
# no arguments are given, the resulting file is |
202 |
# empty |
203 |
ghc-setup-pkg() { |
204 |
local localpkgconf |
205 |
localpkgconf="${S}/$(ghc-localpkgconf)" |
206 |
echo '[]' > "${localpkgconf}" |
207 |
local update_flag |
208 |
if version_is_at_least "6.4" "$(ghc-version)"; then |
209 |
update_flag="update -" |
210 |
else |
211 |
update_flag="--update-package" |
212 |
fi |
213 |
for pkg in $*; do |
214 |
$(ghc-getghcpkgbin) -f "${localpkgconf}" ${update_flag} --force \ |
215 |
< "${pkg}" || die "failed to register ${pkg}" |
216 |
done |
217 |
} |
218 |
|
219 |
# @FUNCTION: ghc-fixlibpath |
220 |
# @DESCRIPTION: |
221 |
# fixes the library and import directories path |
222 |
# of the package configuration file |
223 |
ghc-fixlibpath() { |
224 |
sed -i "s|$1|$(ghc-libdir)|g" "${S}/$(ghc-localpkgconf)" |
225 |
if [[ -n "$2" ]]; then |
226 |
sed -i "s|$2|$(ghc-libdir)/imports|g" "${S}/$(ghc-localpkgconf)" |
227 |
fi |
228 |
} |
229 |
|
230 |
# @FUNCTION: ghc-install-pkg |
231 |
# @DESCRIPTION: |
232 |
# moves the local (package-specific) package configuration |
233 |
# file to its final destination |
234 |
ghc-install-pkg() { |
235 |
mkdir -p "${D}/$(ghc-confdir)" |
236 |
cat "${S}/$(ghc-localpkgconf)" | sed "s|${D}||g" \ |
237 |
> "${D}/$(ghc-confdir)/$(ghc-localpkgconf)" |
238 |
} |
239 |
|
240 |
# @FUNCTION: ghc-register-pkg |
241 |
# @DESCRIPTION: |
242 |
# registers all packages in the local (package-specific) |
243 |
# package configuration file |
244 |
ghc-register-pkg() { |
245 |
local localpkgconf |
246 |
localpkgconf="$(ghc-confdir)/$1" |
247 |
local update_flag |
248 |
local describe_flag |
249 |
if version_is_at_least "6.4" "$(ghc-version)"; then |
250 |
update_flag="update -" |
251 |
describe_flag="describe" |
252 |
else |
253 |
update_flag="--update-package" |
254 |
describe_flag="--show-package" |
255 |
fi |
256 |
if [[ -f "${localpkgconf}" ]]; then |
257 |
for pkg in $(ghc-listpkg "${localpkgconf}"); do |
258 |
ebegin "Registering ${pkg} " |
259 |
$(ghc-getghcpkgbin) -f "${localpkgconf}" "${describe_flag}" "${pkg}" \ |
260 |
| $(ghc-getghcpkg) ${update_flag} --force > /dev/null |
261 |
eend $? |
262 |
done |
263 |
fi |
264 |
} |
265 |
|
266 |
# @FUNCTION: ghc-reregister |
267 |
# @DESCRIPTION: |
268 |
# re-adds all available .conf files to the global |
269 |
# package conf file, to be used on a ghc reinstallation |
270 |
ghc-reregister() { |
271 |
has "${EAPI:-0}" 0 1 2 && ! use prefix && EPREFIX= |
272 |
einfo "Re-adding packages (may cause several harmless warnings) ..." |
273 |
PATH="${EPREFIX}/usr/bin:${PATH}" CONFDIR="$(ghc-confdir)" |
274 |
if [ -d "${CONFDIR}" ]; then |
275 |
pushd "${CONFDIR}" > /dev/null |
276 |
for conf in *.conf; do |
277 |
PATH="${EPREFIX}/usr/bin:${PATH}" ghc-register-pkg "${conf}" |
278 |
done |
279 |
popd > /dev/null |
280 |
fi |
281 |
} |
282 |
|
283 |
# @FUNCTION: ghc-unregister-pkg |
284 |
# @DESCRIPTION: |
285 |
# unregisters a package configuration file |
286 |
# protected are all packages that are still contained in |
287 |
# another package configuration file |
288 |
ghc-unregister-pkg() { |
289 |
local localpkgconf |
290 |
local i |
291 |
local pkg |
292 |
local unregister_flag |
293 |
localpkgconf="$(ghc-confdir)/$1" |
294 |
|
295 |
if version_is_at_least "6.4" "$(ghc-version)"; then |
296 |
unregister_flag="unregister" |
297 |
else |
298 |
unregister_flag="--remove-package" |
299 |
fi |
300 |
|
301 |
if [[ -f "${localpkgconf}" ]]; then |
302 |
for pkg in $(ghc-reverse "$(ghc-listpkg ${localpkgconf})"); do |
303 |
if ! ghc-package-exists "${pkg}"; then |
304 |
einfo "Package ${pkg} is not installed for ghc-$(ghc-version)." |
305 |
else |
306 |
ebegin "Unregistering ${pkg} " |
307 |
$(ghc-getghcpkg) "${unregister_flag}" "${pkg}" --force > /dev/null |
308 |
eend $? |
309 |
fi |
310 |
done |
311 |
fi |
312 |
} |
313 |
|
314 |
# @FUNCTION: ghc-reverse |
315 |
# @DESCRIPTION: |
316 |
# help-function: reverse a list |
317 |
ghc-reverse() { |
318 |
local result |
319 |
local i |
320 |
for i in $1; do |
321 |
result="${i} ${result}" |
322 |
done |
323 |
echo "${result}" |
324 |
} |
325 |
|
326 |
# @FUNCTION: ghc-elem |
327 |
# @DESCRIPTION: |
328 |
# help-function: element-check |
329 |
ghc-elem() { |
330 |
local i |
331 |
for i in $2; do |
332 |
[[ "$1" == "${i}" ]] && return 0 |
333 |
done |
334 |
return 1 |
335 |
} |
336 |
|
337 |
# @FUNCTION: ghc-listpkg |
338 |
# @DESCRIPTION: |
339 |
# show the packages in a package configuration file |
340 |
ghc-listpkg() { |
341 |
local ghcpkgcall |
342 |
local i |
343 |
local extra_flags |
344 |
if version_is_at_least '6.12.3' "$(ghc-version)"; then |
345 |
extra_flags="${extra_flags} -v0" |
346 |
fi |
347 |
for i in $*; do |
348 |
if ghc-cabal; then |
349 |
echo $($(ghc-getghcpkg) list ${extra_flags} -f "${i}") \ |
350 |
| sed \ |
351 |
-e "s|^.*${i}:\([^:]*\).*$|\1|" \ |
352 |
-e "s|/.*$||" \ |
353 |
-e "s|,| |g" -e "s|[(){}]||g" |
354 |
else |
355 |
echo $($(ghc-getghcpkgbin) -l -f "${i}") \ |
356 |
| cut -f2 -d':' \ |
357 |
| sed 's:,: :g' |
358 |
fi |
359 |
done |
360 |
} |
361 |
|
362 |
# @FUNCTION: ghc-package_pkg_setup |
363 |
# @DESCRIPTION: |
364 |
# exported function: check if we have a consistent ghc installation |
365 |
ghc-package_pkg_setup() { |
366 |
if ! ghc-saneghc; then |
367 |
eerror "You have inconsistent versions of dev-lang/ghc and dev-lang/ghc-bin" |
368 |
eerror "installed. Portage currently cannot work correctly with this setup." |
369 |
eerror "There are several possibilities to work around this problem:" |
370 |
eerror "(1) Up/downgrade ghc-bin to the same version as ghc." |
371 |
eerror "(2) Unmerge ghc-bin." |
372 |
eerror "(3) Unmerge ghc." |
373 |
eerror "You probably want option 1 or 2." |
374 |
die "Inconsistent versions of ghc and ghc-bin." |
375 |
fi |
376 |
} |
377 |
|
378 |
# @FUNCTION: ghc-package_pkg_postinst |
379 |
# @DESCRIPTION: |
380 |
# exported function: registers the package-specific package |
381 |
# configuration file |
382 |
ghc-package_pkg_postinst() { |
383 |
ghc-register-pkg "$(ghc-localpkgconf)" |
384 |
} |
385 |
|
386 |
# @FUNCTION: ghc-package_pkg_prerm |
387 |
# @DESCRIPTION: |
388 |
# exported function: unregisters the package-specific package |
389 |
# configuration file; a package contained therein is unregistered |
390 |
# only if it the same package is not also contained in another |
391 |
# package configuration file ... |
392 |
ghc-package_pkg_prerm() { |
393 |
ghc-unregister-pkg "$(ghc-localpkgconf)" |
394 |
} |
395 |
|
396 |
EXPORT_FUNCTIONS pkg_setup pkg_postinst pkg_prerm |