summaryrefslogtreecommitdiff
blob: fb3b19681a2eba48dbe918b5d0ffbd25b8113aed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/eclass/ghc-package.eclass,v 1.12 2005/07/06 20:20:03 agriffis Exp $
#
# Author: Andres Loeh <kosmikus@gentoo.org>
#
# This eclass helps with the Glasgow Haskell Compiler's package
# configuration utility.

inherit versionator

INHERITED="${INHERITED} ${ECLASS}"

# promote /opt/ghc/bin to a better position in the search path
PATH="/usr/bin:/opt/ghc/bin:${PATH}"

# for later configuration using environment variables/
# returns the name of the ghc executable
ghc-getghc() {
	echo "ghc"
}

# returns the name of the ghc-pkg executable
ghc-getghcpkg() {
	echo "ghc-pkg"
}

# returns the name of the ghc-pkg binary (ghc-pkg
# itself usually is a shell script, and we have to
# bypass the script under certain circumstances);
# for Cabal, we add the global package config file,
# because for some reason that's required
ghc-getghcpkgbin() {
	if ghc-cabal; then
		echo $(ghc-libdir)/"ghc-pkg.bin" "--global-conf=$(ghc-libdir)/package.conf"
	else
		echo $(ghc-libdir)/"ghc-pkg.bin"
	fi
}

# returns the version of ghc
_GHC_VERSION_CACHE=""
ghc-version() {
	if [[ -z "${_GHC_VERSION_CACHE}" ]]; then
		_GHC_VERSION_CACHE="$($(ghc-getghc) --version | sed 's:^.*version ::')"
	fi
	echo "${_GHC_VERSION_CACHE}"
}

# returns true for ghc >= 6.4
ghc-cabal() {
	version_is_at_least "6.4" "$(ghc-version)"
}

# returns the library directory
_GHC_LIBDIR_CACHE=""
ghc-libdir() {
	if [[ -z "${_GHC_LIBDIR_CACHE}" ]]; then
		_GHC_LIBDIR_CACHE="$($(ghc-getghc) --print-libdir)"
	fi
	echo "${_GHC_LIBDIR_CACHE}"
}

# returns the (Gentoo) library configuration directory
ghc-confdir() {
	echo $(ghc-libdir)/gentoo
}

# returns the name of the local (package-specific)
# package configuration file
ghc-localpkgconf() {
	echo "${PF}.conf"
}

# make a ghci foo.o file from a libfoo.a file
ghc-makeghcilib() {
	local outfile
	outfile="$(dirname $1)/$(basename $1 | sed 's:^lib\?\(.*\)\.a$:\1.o:')"
	ld --relocatable --discard-all --output="${outfile}" --whole-archive $1
}

# creates a local (package-specific) package
# configuration file; the arguments should be
# uninstalled package description files, each
# containing a single package description; if
# no arguments are given, the resulting file is
# empty
ghc-setup-pkg() {
	local localpkgconf
	localpkgconf="${S}/$(ghc-localpkgconf)"
	echo '[]' > ${localpkgconf}
	for pkg in $*; do
		$(ghc-getghcpkgbin) -f ${localpkgconf} -u --force \
			< ${pkg} || die "failed to register ${pkg}"
	done
}

# fixes the library and import directories path
# of the package configuration file
ghc-fixlibpath() {
    sed -i "s|$1|$(ghc-libdir)|g" ${S}/$(ghc-localpkgconf)
	if [[ -n "$2" ]]; then
		sed -i "s|$2|$(ghc-libdir)/imports|g" ${S}/$(ghc-localpkgconf)
	fi
}

# moves the local (package-specific) package configuration
# file to its final destination
ghc-install-pkg() {
	mkdir -p ${D}/$(ghc-confdir)
	cat ${S}/$(ghc-localpkgconf) | sed "s|${D}||g" \
		> ${D}/$(ghc-confdir)/$(ghc-localpkgconf)
}

# registers all packages in the local (package-specific)
# package configuration file
ghc-register-pkg() {
	local localpkgconf
	localpkgconf="$(ghc-confdir)/$1"
	if [[ -f ${localpkgconf} ]]; then
		for pkg in $(ghc-listpkg ${localpkgconf}); do
			ebegin "Registering ${pkg} "
			$(ghc-getghcpkgbin) -f ${localpkgconf} -s ${pkg} \
				| $(ghc-getghcpkg) -u --force > /dev/null
			eend $?
		done
	fi
}

# re-adds all available .conf files to the global
# package conf file, to be used on a ghc reinstallation
ghc-reregister() {
	einfo "Re-adding packages ..."
	einfo "(This may cause several warnings, but they should be harmless.)"
	if [ -d "$(ghc-confdir)" ]; then
		pushd $(ghc-confdir) > /dev/null
		for conf in *.conf; do
			einfo "Processing ${conf} ..."
			ghc-register-pkg ${conf}
		done
		popd > /dev/null
	fi
}

# unregisters ...
ghc-unregister-pkg() {
	local localpkgconf
	localpkgconf="$(ghc-confdir)/$1"
	if [[ -f ${localpkgconf} ]]; then
		for pkg in $(ghc-reverse "$(ghc-listpkg ${localpkgconf})"); do
			ebegin "Unregistering ${pkg} "
			$(ghc-getghcpkg) -r ${pkg} --force > /dev/null
			eend $?
		done
	fi
}

# help-function: reverse a list
ghc-reverse() {
	local result
	for i in $1; do
		result="${i} ${result}"
	done
	echo ${result}
}

# show the packages in a package configuration file
ghc-listpkg() {
	local ghcpkgcall
	if ghc-cabal; then
		echo $($(ghc-getghcpkg) list -f $1) \
			| sed \
				-e "s|^.*${f}:\([^:]*\).*$|\1|" \
				-e "s|/.*$||" \
				-e "s|,| |g" -e "s|[()]||g"
	else
		echo $($(ghc-getghcpkgbin) -l -f $1) \
			| cut -f2 -d':' \
			| sed 's:,: :g'
	fi
}

# exported function: registers the package-specific package
# configuration file
ghc-package_pkg_postinst() {
	ghc-register-pkg $(ghc-localpkgconf)
}

# exported function: unregisters the package-specific
# package configuration file, under the condition that
# after removal, no other instances of the package will
# be left (necessary check because ghc packages are not
# versioned)
ghc-package_pkg_prerm() {
	has_version "<${CATEGORY}/${PF}" || has_version ">${CATEGORY}/${PF}" \
		|| ghc-unregister-pkg $(ghc-localpkgconf)
}

EXPORT_FUNCTIONS pkg_postinst pkg_prerm