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 toolchain-funcs
30 # Proxy to ewarn for package managers that don't provide eqawarn and use the PM
31 # implementation if available. Reuses PORTAGE_ELOG_CLASSES as set by the dev
33 if ! declare -F eqawarn >/dev/null ; then
35 has qa ${PORTAGE_ELOG_CLASSES} && ewarn "$@"
40 # @FUNCTION: ecvs_clean
41 # @USAGE: [list of dirs]
43 # Remove CVS directories recursiveley. Useful when a source tarball contains
44 # internal CVS directories. Defaults to $PWD.
46 [[ $# -eq 0 ]] && set -- .
47 find "$@" -type d -name 'CVS' -prune -print0 | xargs -0 rm -rf
48 find "$@" -type f -name '.cvs*' -print0 | xargs -0 rm -rf
51 # @FUNCTION: esvn_clean
52 # @USAGE: [list of dirs]
54 # Remove .svn directories recursiveley. Useful when a source tarball contains
55 # internal Subversion directories. Defaults to $PWD.
57 [[ $# -eq 0 ]] && set -- .
58 find "$@" -type d -name '.svn' -prune -print0 | xargs -0 rm -rf
61 # @FUNCTION: egit_clean
62 # @USAGE: [list of dirs]
64 # Remove .git* directories/files recursiveley. Useful when a source tarball
65 # contains internal Git directories. Defaults to $PWD.
67 [[ $# -eq 0 ]] && set -- .
68 find "$@" -type d -name '.git*' -prune -print0 | xargs -0 rm -rf
74 # Cheap replacement for when debianutils (and thus mktemp)
75 # does not exist on the users system.
78 [[ $1 == -d ]] && exe="mkdir" && shift
81 if [[ -z ${topdir} ]] ; then
87 if ! type -P mktemp > /dev/null ; then
88 # system lacks `mktemp` so we have to fake it
90 while [[ -e ${tmp} ]] ; do
91 tmp=${topdir}/tmp.${RANDOM}.${RANDOM}.${RANDOM}
93 ${exe} "${tmp}" || ${exe} -p "${tmp}"
96 # the args here will give slightly wierd names on BSD,
97 # but should produce a usable file on all userlands
98 if [[ ${exe} == "touch" ]] ; then
99 TMPDIR="${topdir}" mktemp -t tmp.XXXXXXXXXX
101 TMPDIR="${topdir}" mktemp -dt tmp.XXXXXXXXXX
106 # @FUNCTION: edos2unix
107 # @USAGE: <file> [more files ...]
109 # A handy replacement for dos2unix, recode, fixdos, etc... This allows you
110 # to remove all of these text utilities from DEPEND variables because this
111 # is a script based solution. Just give it a list of files to convert and
112 # they will all be changed from the DOS CRLF format to the UNIX LF format.
114 [[ $# -eq 0 ]] && return 0
115 sed -i 's/\r$//' -- "$@" || die
118 # @FUNCTION: strip-linguas
119 # @USAGE: [<allow LINGUAS>|<-i|-u> <directories of .po files>]
121 # Make sure that LINGUAS only contains languages that
122 # a package can support. The first form allows you to
123 # specify a list of LINGUAS. The -i builds a list of po
124 # files found in all the directories and uses the
125 # intersection of the lists. The -u builds a list of po
126 # files found in all the directories and uses the union
130 if [[ $1 == "-i" ]] || [[ $1 == "-u" ]] ; then
132 ls=$(find "$1" -name '*.po' -exec basename {} .po ';'); shift
135 if [[ ${op} == "-u" ]] ; then
140 for f in $(find "$d" -name '*.po' -exec basename {} .po ';') ; do
141 if [[ ${op} == "-i" ]] ; then
142 has ${f} ${ls} && newls="${newls} ${f}"
144 has ${f} ${ls} || newls="${newls} ${f}"
155 for f in ${LINGUAS} ; do
156 if has ${f} ${ls} ; then
157 newls="${newls} ${f}"
163 && einfo "Sorry, but ${PN} does not support the LINGUAS:" ${nols}
164 export LINGUAS=${newls:1}
167 # @FUNCTION: built_with_use
168 # @USAGE: [--hidden] [--missing <action>] [-a|-o] <DEPEND ATOM> <List of USE flags>
171 # Deprecated: Use EAPI 2 use deps in DEPEND|RDEPEND and with has_version calls.
173 # A temporary hack until portage properly supports DEPENDing on USE
174 # flags being enabled in packages. This will check to see if the specified
175 # DEPEND atom was built with the specified list of USE flags. The
176 # --missing option controls the behavior if called on a package that does
177 # not actually support the defined USE flags (aka listed in IUSE).
178 # The default is to abort (call die). The -a and -o flags control
179 # the requirements of the USE flags. They correspond to "and" and "or"
180 # logic. So the -a flag means all listed USE flags must be enabled
181 # while the -o flag means at least one of the listed IUSE flags must be
182 # enabled. The --hidden option is really for internal use only as it
183 # means the USE flag we're checking is hidden expanded, so it won't be found
184 # in IUSE like normal USE flags.
186 # Remember that this function isn't terribly intelligent so order of optional
190 if [[ $1 == "--hidden" ]] ; then
195 local missing_action="die"
196 if [[ $1 == "--missing" ]] ; then
199 case ${missing_action} in
201 *) die "unknown action '${missing_action}'";;
206 [[ ${opt:0:1} = "-" ]] && shift || opt="-a"
208 local PKG=$(best_version $1)
209 [[ -z ${PKG} ]] && die "Unable to resolve $1 to an installed package"
212 has "${EAPI:-0}" 0 1 2 && local EROOT=${ROOT}
213 local USEFILE=${EROOT}/var/db/pkg/${PKG}/USE
214 local IUSEFILE=${EROOT}/var/db/pkg/${PKG}/IUSE
216 # if the IUSE file doesn't exist, the read will error out, we need to handle
218 if [[ ! -e ${USEFILE} ]] || [[ ! -e ${IUSEFILE} && ${hidden} == "no" ]] ; then
219 case ${missing_action} in
222 die) die "Unable to determine what USE flags $PKG was built with";;
226 if [[ ${hidden} == "no" ]] ; then
227 local IUSE_BUILT=( $(<"${IUSEFILE}") )
228 # Don't check USE_EXPAND #147237
230 for expand in $(echo ${USE_EXPAND} | tr '[:upper:]' '[:lower:]') ; do
231 if [[ $1 == ${expand}_* ]] ; then
236 if [[ -n ${expand} ]] ; then
237 if ! has $1 ${IUSE_BUILT[@]#[-+]} ; then
238 case ${missing_action} in
241 die) die "$PKG does not actually support the $1 USE flag!";;
247 local USE_BUILT=$(<${USEFILE})
248 while [[ $# -gt 0 ]] ; do
249 if [[ ${opt} = "-o" ]] ; then
250 has $1 ${USE_BUILT} && return 0
252 has $1 ${USE_BUILT} || return 1
259 # @FUNCTION: make_wrapper
260 # @USAGE: <wrapper> <target> [chdir] [libpaths] [installpath]
262 # Create a shell wrapper script named wrapper in installpath
263 # (defaults to the bindir) to execute target (default of wrapper) by
264 # first optionally setting LD_LIBRARY_PATH to the colon-delimited
265 # libpaths followed by optionally changing directory to chdir.
267 local wrapper=$1 bin=$2 chdir=$3 libdir=$4 path=$5
268 local tmpwrapper=$(emktemp)
269 has "${EAPI:-0}" 0 1 2 && local EPREFIX=""
273 [[ -n ${chdir} ]] && printf 'cd "%s"\n' "${EPREFIX}${chdir}"
274 if [[ -n ${libdir} ]] ; then
276 if [[ ${CHOST} == *-darwin* ]] ; then
277 var=DYLD_LIBRARY_PATH
282 if [ "\${${var}+set}" = "set" ] ; then
283 export ${var}="\${${var}}:${EPREFIX}${libdir}"
285 export ${var}="${EPREFIX}${libdir}"
289 # We don't want to quote ${bin} so that people can pass complex
290 # things as ${bin} ... "./someprog --args"
291 printf 'exec %s "$@"\n' "${bin/#\//${EPREFIX}/}"
293 chmod go+rx "${tmpwrapper}"
295 if [[ -n ${path} ]] ; then
298 newexe "${tmpwrapper}" "${wrapper}"
301 newbin "${tmpwrapper}" "${wrapper}" || die
305 # @FUNCTION: path_exists
306 # @USAGE: [-a|-o] <paths>
308 # Check if the specified paths exist. Works for all types of paths
309 # (files/dirs/etc...). The -a and -o flags control the requirements
310 # of the paths. They correspond to "and" and "or" logic. So the -a
311 # flag means all the paths must exist while the -o flag means at least
312 # one of the paths must exist. The default behavior is "and". If no
313 # paths are specified, then the return value is "false".
316 [[ ${opt} == -[ao] ]] && shift || opt="-a"
318 # no paths -> return false
319 # same behavior as: [[ -e "" ]]
320 [[ $# -eq 0 ]] && return 1
329 -a) return $(( r != 0 )) ;;
330 -o) return $(( r == $# )) ;;
334 # @FUNCTION: use_if_iuse
337 # Return true if the given flag is in USE and IUSE.
339 # Note that this function should not be used in the global scope.
341 in_iuse $1 || return 1
345 # @FUNCTION: optfeature
346 # @USAGE: <short description> <package atom to match> [other atoms]
348 # Print out a message suggesting an optional package (or packages)
349 # not currently installed which provides the described functionality.
351 # The following snippet would suggest app-misc/foo for optional foo support,
352 # app-misc/bar or app-misc/baz[bar] for optional bar support
353 # and either both app-misc/a and app-misc/b or app-misc/c for alphabet support.
355 # optfeature "foo support" app-misc/foo
356 # optfeature "bar support" app-misc/bar app-misc/baz[bar]
357 # optfeature "alphabet support" "app-misc/a app-misc/b" app-misc/c
360 debug-print-function ${FUNCNAME} "$@"
367 if has_version "${j}"; then
374 if [[ ${flag} -eq 1 ]]; then
378 if [[ ${flag} -eq 0 ]]; then
384 msg="${msg:0: -4} for ${desc}"
396 # Sleep for the specified number of seconds (default of 5 seconds). Useful when
397 # printing a message the user should probably be reading and often used in
398 # conjunction with the ebeep function. If the EPAUSE_IGNORE env var is set,
399 # don't wait at all. Defined in EAPIs 0 1 and 2.
401 [[ -z ${EPAUSE_IGNORE} ]] && sleep ${1:-5}
405 # @USAGE: [number of beeps]
407 # Issue the specified number of beeps (default of 5 beeps). Useful when
408 # printing a message the user should probably be reading and often used in
409 # conjunction with the epause function. If the EBEEP_IGNORE env var is set,
410 # don't beep at all. Defined in EAPIs 0 1 and 2.
413 if [[ -z ${EBEEP_IGNORE} ]] ; then
414 for ((n=1 ; n <= ${1:-5} ; n++)) ; do
416 sleep 0.1 &>/dev/null ; sleep 0,1 &>/dev/null
427 ewarn "QA Notice: ebeep is not defined in EAPI=${EAPI}, please file a bug at https://bugs.gentoo.org"
431 ewarn "QA Notice: epause is not defined in EAPI=${EAPI}, please file a bug at https://bugs.gentoo.org"
441 # @USAGE: <USE flag> [true output] [false output] [true suffix] [false suffix]
443 # Proxy to declare usex for package managers or EAPIs that do not provide it
444 # and use the package manager implementation when available (i.e. EAPI >= 5).
445 # If USE flag is set, echo [true output][true suffix] (defaults to "yes"),
446 # otherwise echo [false output][false suffix] (defaults to "no").
447 usex() { use "$1" && echo "${2-yes}$4" || echo "${3-no}$5" ; } #382963
455 # @FUNCTION: einstalldocs
457 # Install documentation using DOCS and HTML_DOCS.
459 # If DOCS is declared and non-empty, all files listed in it are
460 # installed. The files must exist, otherwise the function will fail.
461 # In EAPI 4 and subsequent EAPIs DOCS may specify directories as well,
462 # in other EAPIs using directories is unsupported.
464 # If DOCS is not declared, the files matching patterns given
465 # in the default EAPI implementation of src_install will be installed.
466 # If this is undesired, DOCS can be set to empty value to prevent any
467 # documentation from being installed.
469 # If HTML_DOCS is declared and non-empty, all files and/or directories
470 # listed in it are installed as HTML docs (using dohtml).
472 # Both DOCS and HTML_DOCS can either be an array or a whitespace-
473 # separated list. Whenever directories are allowed, '<directory>/.' may
474 # be specified in order to install all files within the directory
475 # without creating a sub-directory in docdir.
477 # Passing additional options to dodoc and dohtml is not supported.
478 # If you needed such a thing, you need to call those helpers explicitly.
480 debug-print-function ${FUNCNAME} "${@}"
483 has ${EAPI} 0 1 2 3 && dodoc_opts=
485 if ! declare -p DOCS &>/dev/null ; then
487 for d in README* ChangeLog AUTHORS NEWS TODO CHANGES \
488 THANKS BUGS FAQ CREDITS CHANGELOG ; do
489 if [[ -s ${d} ]] ; then
493 elif [[ $(declare -p DOCS) == "declare -a"* ]] ; then
494 if [[ ${DOCS[@]} ]] ; then
495 dodoc ${dodoc_opts} "${DOCS[@]}" || die
498 if [[ ${DOCS} ]] ; then
499 dodoc ${dodoc_opts} ${DOCS} || die
503 if [[ $(declare -p HTML_DOCS 2>/dev/null) == "declare -a"* ]] ; then
504 if [[ ${HTML_DOCS[@]} ]] ; then
505 dohtml -r "${HTML_DOCS[@]}" || die
508 if [[ ${HTML_DOCS} ]] ; then
509 dohtml -r ${HTML_DOCS} || die
519 # Determines whether the given flag is in IUSE. Strips IUSE default prefixes
522 # Note that this function should not be used in the global scope.
524 debug-print-function ${FUNCNAME} "${@}"
525 [[ ${#} -eq 1 ]] || die "Invalid args to ${FUNCNAME}()"
528 local liuse=( ${IUSE} )
530 has "${flag}" "${liuse[@]#[+-]}"