aboutsummaryrefslogtreecommitdiff
blob: 2160b5362151bf5948922cd138ca5f5f034ea617 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# -*-eselect-*-  vim: ft=eselect
# Copyright 1999-2010 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

inherit config package-manager

DESCRIPTION="Manage installed versions of sys-devel/binutils"
MAINTAINER="eselect@gentoo.org"
SVN_DATE='$Date$'
VERSION=$(svn_date_to_version "${SVN_DATE}" )

BINUTILS_ENVFILE=${EROOT}/etc/env.d/05binutils

# find_targets [patterns]
# find all possible targets [that match one of ${@}]
find_targets() {
	local args=${@} targets
	[[ -z ${args[@]} ]] && args='-*'
	for item in ${args} ; do
		item=${item%%-[1-9]*}
		[[ ${item:0:1} == '-' ]] || item=-${item}
		for file in "${EROOT}"/etc/env.d/binutils/config${item} ; do
			[[ -f ${file} ]] || continue
			targets=(${targets[@]} "${file##*/config-}")
		done
	done
	echo ${targets[@]}
}

# find_versions
# find all installed version of installed binutils
find_versions() {
	local -a versions
	local last_versions_count
	for target in $(find_targets) ; do
		last_version_count=${#versions[@]}
		for file in "${EROOT}"/etc/env.d/binutils/${target}-* ; do
			[[ -f ${file} ]] || continue
			versions=(${versions[@]} "${file##*/}")
		done
		[[ ${last_version_count} == ${#versions[@]} ]] \
			&& die -q "Please remove stray file: ${EROOT}/etc/env.d/binutils/config-${target}"
	done
	echo ${versions[@]}
}

# is_active $target $version
# returns true if $version is currently used
is_active() {
	[[ ${#@} -eq 2 ]] || die "Need exactly 2 arguments!"
	current=$(load_config "${EROOT}/etc/env.d/binutils/config-${1}" CURRENT)
	[[ ${current} == ${2} ]]
}

# is_valid $profile
is_valid() {
	[[ ${#@} -eq 1 && -e "${EROOT}/etc/env.d/binutils/${profile}" ]] \
		|| die "Need exactly 1 argument!"
}

# swtich_profile $profile
# switches binutils to $profile
switch_profile() {
	# set us up
	[[ ${#@} -eq 1 ]] || die "Need exactly 1 argument!"
	local profile=${1##*/}
	local profile_file=${EROOT}/etc/env.d/binutils/${profile}
	local target targets version binutilspath libpath chost
	version=$(load_config ${profile_file} VER)
	target=$(load_config ${profile_file} TARGET)
	is_active ${target} ${version} \
		&& die -q "Profile \"${profile}\" is already active!"
	chost=$(envvar sys-devel/binutils CHOST)
	libpath=$(load_config ${profile_file} LIBPATH)
	libpath=${libpath:-${EPREFIX}/usr/lib/binutils/${TARGET}/${VER}}
	faketargets=( $(load_config ${profile_file} FAKE_TARGETS) )

	# Generate binary symlinks
	echo "Switching to ${profile}..."
	[[ -d  "${EROOT}/usr/${target}/binutils-bin/${version}" ]] \
		|| die -q "Can't cd ${EROOT}/usr/${target}/binutils-bin/${version}!"
	mkdir -p "${EROOT}/usr/${target}/bin" \
		|| die -q "Can't create ${EROOT}/usr/${target}/bin!"

	binutilspath=${EPREFIX}/usr/${target}/binutils-bin/${version}
	for app in "${ROOT}${binutilspath}"/* ; do
		app=${app##*/}
		ln -sf \
			"${binutilspath}/${app}" \
			"${EROOT}/usr/${target}/bin/${app}" \
			|| die -q "Linking ${ROOT}/${binutilspath}/${app} failed!"
		ln -sf \
			../${target}/bin/${app} \
			"${EROOT}/usr/bin/${target}-${app}" \
			|| die -q "Linking ${EROOT}/usr/bin/${target}-${app} failed!"
		for fake in ${faketargets} ; do
			[[ -f ${EROOT}/etc/env.d/binutils/config-${fake} ]] \
				&& continue
			ln -sf ../${target}/bin/${app} "${EROOT}/usr/bin/${fake}-${app}"
		done
		[[ ${chost} == ${target} ]] || continue
		ln -sf \
			${target}-${app} \
			"${EROOT}/usr/bin/${app}" \
			|| die -q "Linking ${EROOT}/usr/bin/${app} failed!"
	done

	# Generate library and ldscripts symlinks
	mkdir -p "${EROOT}/usr/${target}/lib"
	rm -r "${EROOT}/usr/${target}/lib/ldscripts"
	ln -sf "${libpath}/ldscripts" "${EROOT}/usr/${target}/lib/ldscripts"
	[[ ${target} == ${chost} ]] \
		&& dest="${EROOT}/usr/${chost}/lib" \
		|| dest="${EROOT}/usr/${chost}/${target}/lib"
	mkdir -p "${dest}"
	for lib in "${ROOT}${libpath}"/lib* ; do
		ln -sf "${libpath}/${lib##*/}" "${dest}/${lib##*/}"
	done

	# Generate include symlink
	[[ ${target} == ${chost} ]] \
		&& dest="${EROOT}/usr/include" \
		|| dest="${EROOT}/usr/${target}/include"
	mkdir -p "${dest}"
	for inc in "${ROOT}${libpath}"/include/* ; do
		ln -sf "${inc##${ROOT}}" "${dest}/${inc##*/}"
	done

	# Update the environment
	if [[ ${target} == ${chost} ]] ; then
		datapath=${EPREFIX}/usr/share/binutils-date/${target}/${version}
		[[ -d ${datapath}/man ]] \
			&& store_config ${BINUTILS_ENVFILE} MANPATH "${datapath}/man"
		[[ -d ${datapath}/info ]] \
			&& store_config ${BINUTILS_ENVFILE} INFOPATH "${datapath}/info"
		store_config ${BINUTILS_ENVFILE} LDPATH "${EPREFIX}/usr/${target}/lib"
	fi

	store_config \
		"${EROOT}/etc/env.d/binutils/config-${target}" \
		CURRENT ${version}
	if [[ ${ROOT:-/} == / ]] && [[ ${target} == ${chost} ]] ; then
		do_action env update
		echo "Please remember to run:"
		echo
		echo "  # source /etc/profile"
		echo
		echo "in order to let changes take effect!"
	fi
}

### list action

describe_list() {
	echo "List all installed version of binutils"
}

do_list() {
	local targets i=0 version files last_processed_count
	local -a processed
	targets=( $(find_targets ${@}) )

	[[ -z ${targets[@]} ]] \
		&& die -q "Pattern ${@} does not match any installed version of binutils!"

	for target in $(find_targets) ; do
		[[ ${targets[@]/${target}/} == ${targets[@]} ]] \
			|| write_list_start "Installed binutils for target $(highlight ${target})"
		last_processed_count=${#processed[@]}
		for file in "${EROOT}"/etc/env.d/binutils/${@:-*} ; do
			[[ -f ${file} ]] || continue
			version=${file##*/${target}-}
			[[ ${version} == ${file} ]] && continue
			[[ ${processed[@]/${file}/} == ${processed[@]} ]] || continue
			processed=(${processed[@]} "${file}")
			i=$(( i + 1 ))
			[[ ${targets[@]/${target}/} == ${targets[@]} ]] && continue
			is_active ${target} ${version} \
				&& version=$(highlight_marker "${version}")
			write_numbered_list_entry $i "${version}"
		done
		[[ ${last_processed_count} == ${#processed[@]} ]] \
			&& die -q "Please remove stray file: ${EROOT}/etc/env.d/binutils/config-${target}"
	done
}

### set action

describe_set() {
	echo "Activate one of the installed binutils"
}

describe_set_parameters() {
	echo "<target>"
}

describe_set_options() {
	echo "target : Target name or number (from 'list' action)"
}

do_set() {
		[[ ${#@} == 1 ]] \
			|| die -q "Please specify exactly one version to activate!"
		local versions file fail=0
		versions=( $(find_versions) )

		for item in ${@} ; do
			if is_number ${item} ; then
				if [[ ${item} -ge 1 ]] && [[ ${item} -le $(( ${#versions[@]} + 2)) ]] ; then
					switch_profile ${versions[$(( ${item} -1 ))]}
				else
					fail=1
					echo "Item not in range 1-$((${#versions[@]} + 1)): ${item}"
					continue
				fi
			else
				file=( "${EROOT}"/etc/env.d/binutils/${item} )
				if [[ ${#file[@]} -gt 1 ]] ; then
					fail=1
					echo "Ambigious pattern: ${item}"
					continue
				fi
				if ! [[ -f ${file} ]] ; then
					fail=1
					echo "Pattern doesn't match anything: ${item}"
					continue
				fi
				switch_profile ${file##*/}
			fi
		done

		[[ ${fail} == 1 ]] && die -q "One or more actions have failed!"
}

### show action

describe_show() {
	echo "Print the currently active binutils version"
}

do_show() {
	local chost=${1:-$(envvar sys-devel/binutils CHOST)} version config

	[[ -n ${chost} ]] \
		|| die -q "Portage returned empty CHOST!"

	config=${EROOT}/etc/env.d/binutils/config-${chost}
	[[ ${#config[@]} -eq 1 ]] \
		|| return
	[[ -e ${config} ]] \
		|| return

	version=$(load_config \
		"${EROOT}/etc/env.d/binutils/config-${chost}" CURRENT)
	[[ -e ${EROOT}/etc/env.d/binutils/${chost}-${version} ]] \
		|| die "File \"${EROOT}/etc/env.d/binutils/${chost}-${version}\" is missing!"

	echo "${chost}-${version}"
}