1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: eutils.eclass
6 # base-system@gentoo.org
7 # @BLURB: many extra (but common) functions that are used in ebuilds
9 # The eutils eclass contains a suite of functions that complement
10 # the ones that ebuild.sh already contain. The idea is that the functions
11 # are not required in all ebuilds but enough utilize them to have a common
12 # home rather than having multiple ebuilds implementing the same thing.
14 # Due to the nature of this eclass, some functions may have maintainers
15 # different from the overall eclass!
17 if [[ -z ${_EUTILS_ECLASS} ]]; then
20 # implicitly inherited (now split) eclasses
23 inherit desktop epatch estack ltprune multilib preserve-libs \
24 toolchain-funcs vcs-clean
31 # Proxy to ewarn for package managers that don't provide eqawarn and use the PM
32 # implementation if available. Reuses PORTAGE_ELOG_CLASSES as set by the dev
34 if ! declare -F eqawarn >/dev/null ; then
36 has qa ${PORTAGE_ELOG_CLASSES} && ewarn "$@"
44 # Cheap replacement for when debianutils (and thus mktemp)
45 # does not exist on the users system.
48 [[ $1 == -d ]] && exe="mkdir" && shift
51 if [[ -z ${topdir} ]] ; then
57 if ! type -P mktemp > /dev/null ; then
58 # system lacks `mktemp` so we have to fake it
60 while [[ -e ${tmp} ]] ; do
61 tmp=${topdir}/tmp.${RANDOM}.${RANDOM}.${RANDOM}
63 ${exe} "${tmp}" || ${exe} -p "${tmp}"
66 # the args here will give slightly wierd names on BSD,
67 # but should produce a usable file on all userlands
68 if [[ ${exe} == "touch" ]] ; then
69 TMPDIR="${topdir}" mktemp -t tmp.XXXXXXXXXX
71 TMPDIR="${topdir}" mktemp -dt tmp.XXXXXXXXXX
76 # @FUNCTION: edos2unix
77 # @USAGE: <file> [more files ...]
79 # A handy replacement for dos2unix, recode, fixdos, etc... This allows you
80 # to remove all of these text utilities from DEPEND variables because this
81 # is a script based solution. Just give it a list of files to convert and
82 # they will all be changed from the DOS CRLF format to the UNIX LF format.
84 [[ $# -eq 0 ]] && return 0
85 sed -i 's/\r$//' -- "$@" || die
88 # @FUNCTION: strip-linguas
89 # @USAGE: [<allow LINGUAS>|<-i|-u> <directories of .po files>]
91 # Make sure that LINGUAS only contains languages that
92 # a package can support. The first form allows you to
93 # specify a list of LINGUAS. The -i builds a list of po
94 # files found in all the directories and uses the
95 # intersection of the lists. The -u builds a list of po
96 # files found in all the directories and uses the union
100 if [[ $1 == "-i" ]] || [[ $1 == "-u" ]] ; then
102 ls=$(find "$1" -name '*.po' -exec basename {} .po ';'); shift
105 if [[ ${op} == "-u" ]] ; then
110 for f in $(find "$d" -name '*.po' -exec basename {} .po ';') ; do
111 if [[ ${op} == "-i" ]] ; then
112 has ${f} ${ls} && newls="${newls} ${f}"
114 has ${f} ${ls} || newls="${newls} ${f}"
125 for f in ${LINGUAS} ; do
126 if has ${f} ${ls} ; then
127 newls="${newls} ${f}"
133 && einfo "Sorry, but ${PN} does not support the LINGUAS:" ${nols}
134 export LINGUAS=${newls:1}
137 # @FUNCTION: make_wrapper
138 # @USAGE: <wrapper> <target> [chdir] [libpaths] [installpath]
140 # Create a shell wrapper script named wrapper in installpath
141 # (defaults to the bindir) to execute target (default of wrapper) by
142 # first optionally setting LD_LIBRARY_PATH to the colon-delimited
143 # libpaths followed by optionally changing directory to chdir.
145 local wrapper=$1 bin=$2 chdir=$3 libdir=$4 path=$5
146 local tmpwrapper=$(emktemp)
147 has "${EAPI:-0}" 0 1 2 && local EPREFIX=""
151 [[ -n ${chdir} ]] && printf 'cd "%s"\n' "${EPREFIX}${chdir}"
152 if [[ -n ${libdir} ]] ; then
154 if [[ ${CHOST} == *-darwin* ]] ; then
155 var=DYLD_LIBRARY_PATH
160 if [ "\${${var}+set}" = "set" ] ; then
161 export ${var}="\${${var}}:${EPREFIX}${libdir}"
163 export ${var}="${EPREFIX}${libdir}"
167 # We don't want to quote ${bin} so that people can pass complex
168 # things as ${bin} ... "./someprog --args"
169 printf 'exec %s "$@"\n' "${bin/#\//${EPREFIX}/}"
171 chmod go+rx "${tmpwrapper}"
173 if [[ -n ${path} ]] ; then
176 newexe "${tmpwrapper}" "${wrapper}"
179 newbin "${tmpwrapper}" "${wrapper}" || die
183 # @FUNCTION: path_exists
184 # @USAGE: [-a|-o] <paths>
186 # Check if the specified paths exist. Works for all types of paths
187 # (files/dirs/etc...). The -a and -o flags control the requirements
188 # of the paths. They correspond to "and" and "or" logic. So the -a
189 # flag means all the paths must exist while the -o flag means at least
190 # one of the paths must exist. The default behavior is "and". If no
191 # paths are specified, then the return value is "false".
194 [[ ${opt} == -[ao] ]] && shift || opt="-a"
196 # no paths -> return false
197 # same behavior as: [[ -e "" ]]
198 [[ $# -eq 0 ]] && return 1
207 -a) return $(( r != 0 )) ;;
208 -o) return $(( r == $# )) ;;
212 # @FUNCTION: use_if_iuse
215 # Return true if the given flag is in USE and IUSE.
217 # Note that this function should not be used in the global scope.
219 in_iuse $1 || return 1
223 # @FUNCTION: optfeature
224 # @USAGE: <short description> <package atom to match> [other atoms]
226 # Print out a message suggesting an optional package (or packages)
227 # not currently installed which provides the described functionality.
229 # The following snippet would suggest app-misc/foo for optional foo support,
230 # app-misc/bar or app-misc/baz[bar] for optional bar support
231 # and either both app-misc/a and app-misc/b or app-misc/c for alphabet support.
233 # optfeature "foo support" app-misc/foo
234 # optfeature "bar support" app-misc/bar app-misc/baz[bar]
235 # optfeature "alphabet support" "app-misc/a app-misc/b" app-misc/c
238 debug-print-function ${FUNCNAME} "$@"
245 if has_version "${j}"; then
252 if [[ ${flag} -eq 1 ]]; then
256 if [[ ${flag} -eq 0 ]]; then
262 msg="${msg:0: -4} for ${desc}"
274 # Sleep for the specified number of seconds (default of 5 seconds). Useful when
275 # printing a message the user should probably be reading and often used in
276 # conjunction with the ebeep function. If the EPAUSE_IGNORE env var is set,
277 # don't wait at all. Defined in EAPIs 0 1 and 2.
279 [[ -z ${EPAUSE_IGNORE} ]] && sleep ${1:-5}
283 # @USAGE: [number of beeps]
285 # Issue the specified number of beeps (default of 5 beeps). Useful when
286 # printing a message the user should probably be reading and often used in
287 # conjunction with the epause function. If the EBEEP_IGNORE env var is set,
288 # don't beep at all. Defined in EAPIs 0 1 and 2.
291 if [[ -z ${EBEEP_IGNORE} ]] ; then
292 for ((n=1 ; n <= ${1:-5} ; n++)) ; do
294 sleep 0.1 &>/dev/null ; sleep 0,1 &>/dev/null
305 ewarn "QA Notice: ebeep is not defined in EAPI=${EAPI}, please file a bug at https://bugs.gentoo.org"
309 ewarn "QA Notice: epause is not defined in EAPI=${EAPI}, please file a bug at https://bugs.gentoo.org"
319 # @USAGE: <USE flag> [true output] [false output] [true suffix] [false suffix]
321 # Proxy to declare usex for package managers or EAPIs that do not provide it
322 # and use the package manager implementation when available (i.e. EAPI >= 5).
323 # If USE flag is set, echo [true output][true suffix] (defaults to "yes"),
324 # otherwise echo [false output][false suffix] (defaults to "no").
325 usex() { use "$1" && echo "${2-yes}$4" || echo "${3-no}$5" ; } #382963
333 # @FUNCTION: einstalldocs
335 # Install documentation using DOCS and HTML_DOCS, in EAPIs that do not
336 # provide this function. When available (i.e., in EAPI 6 or later),
337 # the package manager implementation should be used instead.
339 # If DOCS is declared and non-empty, all files listed in it are
340 # installed. The files must exist, otherwise the function will fail.
341 # In EAPI 4 and 5, DOCS may specify directories as well; in earlier
342 # EAPIs using directories is unsupported.
344 # If DOCS is not declared, the files matching patterns given
345 # in the default EAPI implementation of src_install will be installed.
346 # If this is undesired, DOCS can be set to empty value to prevent any
347 # documentation from being installed.
349 # If HTML_DOCS is declared and non-empty, all files and/or directories
350 # listed in it are installed as HTML docs (using dohtml).
352 # Both DOCS and HTML_DOCS can either be an array or a whitespace-
353 # separated list. Whenever directories are allowed, '<directory>/.' may
354 # be specified in order to install all files within the directory
355 # without creating a sub-directory in docdir.
357 # Passing additional options to dodoc and dohtml is not supported.
358 # If you needed such a thing, you need to call those helpers explicitly.
360 debug-print-function ${FUNCNAME} "${@}"
363 has ${EAPI} 0 1 2 3 && dodoc_opts=
365 if ! declare -p DOCS &>/dev/null ; then
367 for d in README* ChangeLog AUTHORS NEWS TODO CHANGES \
368 THANKS BUGS FAQ CREDITS CHANGELOG ; do
369 if [[ -s ${d} ]] ; then
373 elif [[ $(declare -p DOCS) == "declare -a"* ]] ; then
374 if [[ ${DOCS[@]} ]] ; then
375 dodoc ${dodoc_opts} "${DOCS[@]}" || die
378 if [[ ${DOCS} ]] ; then
379 dodoc ${dodoc_opts} ${DOCS} || die
383 if [[ $(declare -p HTML_DOCS 2>/dev/null) == "declare -a"* ]] ; then
384 if [[ ${HTML_DOCS[@]} ]] ; then
385 dohtml -r "${HTML_DOCS[@]}" || die
388 if [[ ${HTML_DOCS} ]] ; then
389 dohtml -r ${HTML_DOCS} || die
399 # Determines whether the given flag is in IUSE. Strips IUSE default
400 # prefixes as necessary. In EAPIs where it is available (i.e., EAPI 6
401 # or later), the package manager implementation should be used instead.
403 # Note that this function must not be used in the global scope.
405 debug-print-function ${FUNCNAME} "${@}"
406 [[ ${#} -eq 1 ]] || die "Invalid args to ${FUNCNAME}()"
409 local liuse=( ${IUSE} )
411 has "${flag}" "${liuse[@]#[+-]}"