aboutsummaryrefslogtreecommitdiff
blob: c675e7d8a34f78299eb8264ad38d722634c3f866 (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
# -*-eselect-*-  vim: ft=eselect
# Copyright (c) 2005-2017 Gentoo Foundation
#
# This file is part of the 'eselect' tools framework.
#
# eselect is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 2 of the License, or (at your option) any later
# version.
#
# eselect is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# eselect.  If not, see <http://www.gnu.org/licenses/>.

# package_manager PRIVATE
# Return the package manager we're going to use.
package_manager() {
	local pm
	case ${PACKAGE_MANAGER} in
		portage) pm=portage ;;
		pkgcore) pm=pkgcore ;;
		paludis) pm=paludis ;;
	esac
	echo "${pm:-portage}"
}

# portageq
# Run portageq. Redirect stderr
portageq() {
	command portageq "$@" 2>/dev/null
}

# run_paludis PRIVATE
# Run CAVE (defaults to "cave"). Redirect stderr
run_paludis() {
	${CAVE:-cave} "$@" 2>/dev/null
}

# arch
# Return the architecture we're running on...
arch() {
	local ret=$(envvar sys-devel/gcc ARCH)

	if [[ -n ${EPREFIX} && -n ${ret} && ${ret%-*} = "${ret}" ]]; then
		# prefix/linux profiles lie about their ARCH
		case $(envvar sys-devel/gcc KERNEL) in
			linux) ret+="-linux" ;;
			*)
				write_warning_msg \
					"Failed to determine \${ARCH}." \
					"Please submit a bug report."
				return 1
				;;
		esac
	fi

	# $arch will be null if there's no current make.profile symlink.
	# We cannot get a list of valid profiles without it.
	if [[ -z ${ret} ]]; then
		if [[ -n ${ROOT} && ${ROOT} != "/" ]]; then
			write_warning_msg \
				"Failed to determine \${ARCH}." \
				"Is your make.profile symlink valid?"
			return 1
		fi

		# Try to determine arch from HOSTTYPE and OSTYPE, which are
		# derived from the GNU triplet (at build time of bash)
		case ${HOSTTYPE} in
			aarch64*)  	ret=arm64 	;;
			alpha*) 	ret=alpha	;;
			arm*)   	ret=arm 	;;
			hppa*)  	ret=hppa	;;
			i?86)   	ret=x86 	;;
			ia64*)  	ret=ia64	;;
			m68k*)  	ret=m68k	;;
			mips*)  	ret=mips	;;
			nios2)  	ret=nios2	;;
			or1k|or32*)	ret=openrisc;;
			powerpc64*)	ret=ppc64	;;
			powerpc*)	ret=ppc 	;;
			riscv*) 	ret=riscv	;;
			s390*)  	ret=s390	;;
			sh*)    	ret=sh  	;;
			sparc*) 	ret=sparc	;;
			x86_64) 	ret=amd64	;;
			*)
				write_warning_msg \
					"Unknown architecture \"${HOSTTYPE}\"." \
					"Please submit a bug report."
				return 1
				;;
		esac

		if [[ -z ${EPREFIX} ]]; then
			case ${OSTYPE} in
				linux*) 	;;
				dragonfly*)	ret+="-dfly"	;;
				freebsd*)	ret+="-fbsd"	;;
				netbsd*)	ret+="-nbsd"	;;
				openbsd*)	ret+="-obsd"	;;
				*)
					write_warning_msg \
						"Unknown OS \"${OSTYPE}\"." \
						"Please submit a bug report."
					return 1
					;;
			esac
		else
			# Prefix architectures
			if [[ ${ret} = amd64 && ${OSTYPE} != linux* ]]; then
				# amd64 is sometimes called x64 on Prefix
				ret=x64
			fi
			case ${OSTYPE} in
				aix*)   	ret+="-aix" 	;;
				cygwin*)	ret+="-cygwin"	;;
				darwin*)	ret+="-macos"	;;
				freebsd*)	ret+="-freebsd"	;;
				hpux*)  	ret+="-hpux"	;;
				interix*)	ret+="-interix"	;;
				linux*) 	ret+="-linux"	;;
				mint*)  	ret+="-mint"	;;
				netbsd*)	ret+="-netbsd"	;;
				openbsd*)	ret+="-openbsd"	;;
				solaris*)	ret+="-solaris"	;;
				winnt*) 	ret+="-winnt"	;;
				*)
					write_warning_msg \
						"Unknown OS \"${OSTYPE}\"." \
						"Please submit a bug report."
					return 1
					;;
			esac
		fi
	fi

	echo "${ret}"
}

# envvar
# Return the contents of environment variable $2 as seen by package manager
# for package $1.
envvar() {
	[[ $# -eq 2 ]] || die "envvar expects exactly 2 arguments"
	case $(package_manager) in
		# portage does not support per-package envvar lookup
		portage) portageq envvar "$2" ;;
		pkgcore) pinspect portageq envvar "$2" ;;
		paludis) run_paludis print-id-environment-variable --best \
			--variable-name "$2" --format '%v\n' "$1" ;;
	esac
}

# best_version
# Return true if package $1 is available in ${ROOT}
best_version() {
	[[ $# -eq 1 ]] || die "best_version expects exactly one argument"
	case $(package_manager) in
		portage) portageq best_version "${EROOT:-/}" "$1" ;;
		pkgcore) pinspect portageq best_version "${ROOT:-/}" "$1" ;;
		paludis) run_paludis print-best-version --format name-version "$1" ;;
	esac
}

# has_version
# Return true if package $1 is available in ${ROOT}
has_version() {
	[[ $# -eq 1 ]] || die "has_version expects exactly one argument"
	case $(package_manager) in
		portage) portageq has_version "${EROOT:-/}" "$1" ;;
		pkgcore) pinspect portageq has_version "${ROOT:-/}" "$1" ;;
		paludis) run_paludis has-version "$1" ;;
	esac
}

# get_repositories
# return list of repositories known to the package manager
get_repositories() {
	case $(package_manager) in
		portage) portageq get_repos "${EROOT:-/}" ;;
		pkgcore) pinspect portageq get_repos "${ROOT:-/}" ;;
		paludis) run_paludis print-repositories ;;
	esac
}

# get_repo_news_dir
# return the directory where to find GLEP 42 news items for repository $1
get_repo_news_dir() {
	[[ $# -eq 1 ]] || die "get_repo_news_dir expects exactly one argument"
	local repo=$1
	case $(package_manager) in
		portage) echo "$(portageq get_repo_path \
			"${EROOT:-/}" "${repo}")/metadata/news" ;;
		pkgcore) pinspect portageq get_repo_news_path "${ROOT:-/}" "${repo}" ;;
		paludis) run_paludis print-repository-metadata ${repo} \
			--raw-name newsdir --format '%v\n' ;;
	esac
}

# env_update
# Run env-update command, if available with the package manager
# If $1 is non-zero: also run ldconfig to update /etc/ld.so.cache
env_update() {
	local noldconfig
	[[ $1 -ne 0 ]] || noldconfig=y
	case $(package_manager) in
		portage) "@ENV_UPDATE@" ${noldconfig:+--no-ldconfig} ;;
		pkgcore) pmaint env-update ${noldconfig:+--skip-ldconfig} ;;
		paludis) return 127 ;;
	esac
}