summaryrefslogtreecommitdiff
blob: f3fd6619f22fb30f48e58e54067ce8ad8e959f7b (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
# -*-eselect-*-  vim: ft=eselect
# Copyright 2019-2023 Gentoo Authors
# Distributed under the terms of the GNU GPL version 2 or later

inherit config tests multilib skel

MODULE="LAPACK"
IFACE="lapack"
DESCRIPTION="Manage installed alternatives of $MODULE shared objects"
MAINTAINER="lumin@debian.org"
VERSION="0.3"

### helper utility to find all available LAPACK implementations
find_targets_in() {
	[[ $# -eq 1 ]] || die -q "in which libdir?"

	local cand
	for cand in "${EROOT}/etc/env.d/lapack/$1"/*; do
		[[ "config" = $(basename ${cand}) ]] && continue
		[[ -r ${cand} ]] && basename ${cand}
	done
}

### helper utility to validate a LAPACK library directory
validate_lapacklibdir() {
	[[ -d $1 ]] || return 2  # dir doesn't exist
	[[ -r "${1}/liblapack.so.3" ]] && [[ -r "${1}/liblapack.so" ]] \
	|| return 1
}

### show action
do_show() {
	# do_show -- show both lib64 and lib32 selection
	# do_show libxx -- show libxx selection
	local libdir cur libdirs=( $(list_libdirs) )

	# if manually specified libdirs
	if [[ ! $# -eq 0 ]]; then
		for libdir in $@; do
			has $libdir ${libdirs[@]} || die -q "invalid libdir '$libdir'!"
		done
		libdirs=( $@ )
	fi

	for libdir in ${libdirs[@]}; do
		[[ ! -d ${EROOT}/usr/$libdir ]] && continue
		[[ ! -r ${EROOT}/etc/env.d/lapack/${libdir}/config ]] && continue
		cur=$(load_config "${EROOT}"/etc/env.d/lapack/${libdir}/config CURRENT)
		echo ${libdir}: $cur
	done
}

### list action
do_list() {
	local i cur targets
	local libdir libdirs=( $(list_libdirs) )

	# if manually specified libdirs
	if [[ ! $# -eq 0 ]]; then
		for libdir in $@; do
			has $libdir ${libdirs[@]} || die -q "invalid libdir '$libdir'!"
		done
		libdirs=( $@ )
	fi

	for libdir in ${libdirs[@]}; do
		[[ ! -d ${EROOT}/usr/$libdir ]] || [[ -L ${EROOT}/usr/$libdir ]] && continue
		targets=( $(find_targets_in $libdir) )
		cur=$(load_config "${EROOT}"/etc/env.d/lapack/${libdir}/config CURRENT)

		write_list_start "Available LAPACK ($libdir) candidates:"
		for (( i = 0; i < ${#targets[@]}; i++ )); do
			[[ ${targets[i]} = $cur ]] \
			&& targets[i]=$(highlight_marker "${targets[i]}")
		done
		write_numbered_list -m "(none found)" "${targets[@]}"
	done
}

### validate action
describe_validate() {
	echo "validate candidates and remove invalid ones"
}

do_validate() {
	local i ldpath targets target candidate
	local libdir libdirs=( $(list_libdirs) )
	for libdir in ${libdirs[@]}; do
		[[ ! -d ${EROOT}/usr/$libdir ]] || [[ -L ${EROOT}/usr/$libdir ]] && continue
		targets=( $(find_targets_in $libdir) )
		for target in ${targets[@]}; do
			candidate=${EROOT}/etc/env.d/lapack/${libdir}/${target}
			ldpath=$(load_config "$candidate" LDPATH)
			if ! validate_lapacklibdir "${ROOT}/$ldpath"; then
				echo "Removing invalid LAPACK candidate ($candidate) ..."
				rm -v "$candidate"
			fi
		done
	done
}

### set action

describe_set() {
        echo "Activate one of the installed $MODULE implementations"
}

describe_set_parameters() {
        echo "[<libdir>] <implementation>"
}

describe_set_options() {
	echo "libdir: lib64 or lib32. Will use the native one when omitted."
        echo "implementation : implementation name or number (from 'list' action)"
}

do_set() {
	local libdirs=( $(list_libdirs) )
	local targets libdir ldconf libpath impl

	# check argument existence
	[[ $# -eq 1 ]] || [[ $# -eq 2 ]] || \
		die -q "Please specify [<libdir>] <impl> to activate!"
	# read argumenets
	if [[ $# -eq 2 ]]; then libdir="$1"; else libdir="$(get_libdir)"; fi
	if [[ $# -eq 2 ]]; then impl="$2"; else impl="$1"; fi
	# validate arguments
	has "$libdir" "${libdirs[@]}" || die -q "Invalid libdir ($libdir) !"
	targets=( $(find_targets_in $libdir) )
	if ! has "$impl" "${targets[@]}"; then
		if [[ ${impl} -gt 0 ]] && [[ ${impl} -le ${#targets[@]} ]]; then
			impl=${targets[(${impl} - 1)]}
		else
			die -q "Invalid impl ($impl) !"
		fi
	fi
	# store the configuration to env.d
	store_config "${EROOT}"/etc/env.d/lapack/${libdir}/config CURRENT "${impl}"
	# generate ld.so.conf file
	ldconf="${EROOT}/etc/ld.so.conf.d/82-lapack-${libdir}.conf"
	libpath=$(load_config ${EROOT}/etc/env.d/lapack/${libdir}/${impl} LDPATH)
	truncate -s0 "$ldconf"
	echo "# Auto-generated by eselect::lapack. DON'T EDIT." >> "$ldconf"
	echo "$libpath" >> "$ldconf"

	# refresh cache. env-update will call ldconfig (see
	# portage::env_update.py) so we don't need to call it explicitly.
	env-update
}

### add action
describe_add_parameters() {
	echo "<libdir> <dir> <impl>"
}

describe_add_options() {
	echo "libdir : library directory where $MODULE implementation is installed (lib, lib64, etc.)"
	echo "dir : directory containing liblapack.so{.3}"
	echo "impl : name of the $MODULE implementation"
}

do_add() {
	[[ $# -eq 3 ]] \
		|| die -q "Please specify <libdir> <dir> <impl> to activate!"
	validate_lapacklibdir "${ROOT}/$2" || die -q "invalid lapack libdir!"
	store_config "${EROOT}/etc/env.d/lapack/$1/$3" LDPATH "$2"
}