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 # Cheap replacement for when debianutils (and thus mktemp)
32 # does not exist on the users system.
35 [[ $1 == -d ]] && exe="mkdir" && shift
38 if [[ -z ${topdir} ]] ; then
44 if ! type -P mktemp > /dev/null ; then
45 # system lacks `mktemp` so we have to fake it
47 while [[ -e ${tmp} ]] ; do
48 tmp=${topdir}/tmp.${RANDOM}.${RANDOM}.${RANDOM}
50 ${exe} "${tmp}" || ${exe} -p "${tmp}"
53 # the args here will give slightly wierd names on BSD,
54 # but should produce a usable file on all userlands
55 if [[ ${exe} == "touch" ]] ; then
56 TMPDIR="${topdir}" mktemp -t tmp.XXXXXXXXXX
58 TMPDIR="${topdir}" mktemp -dt tmp.XXXXXXXXXX
63 # @FUNCTION: edos2unix
64 # @USAGE: <file> [more files ...]
66 # A handy replacement for dos2unix, recode, fixdos, etc... This allows you
67 # to remove all of these text utilities from DEPEND variables because this
68 # is a script based solution. Just give it a list of files to convert and
69 # they will all be changed from the DOS CRLF format to the UNIX LF format.
71 [[ $# -eq 0 ]] && return 0
72 sed -i 's/\r$//' -- "$@" || die
75 # @FUNCTION: strip-linguas
76 # @USAGE: [<allow LINGUAS>|<-i|-u> <directories of .po files>]
78 # Make sure that LINGUAS only contains languages that
79 # a package can support. The first form allows you to
80 # specify a list of LINGUAS. The -i builds a list of po
81 # files found in all the directories and uses the
82 # intersection of the lists. The -u builds a list of po
83 # files found in all the directories and uses the union
87 if [[ $1 == "-i" ]] || [[ $1 == "-u" ]] ; then
89 ls=$(find "$1" -name '*.po' -exec basename {} .po ';'); shift
92 if [[ ${op} == "-u" ]] ; then
97 for f in $(find "$d" -name '*.po' -exec basename {} .po ';') ; do
98 if [[ ${op} == "-i" ]] ; then
99 has ${f} ${ls} && newls="${newls} ${f}"
101 has ${f} ${ls} || newls="${newls} ${f}"
112 for f in ${LINGUAS} ; do
113 if has ${f} ${ls} ; then
114 newls="${newls} ${f}"
120 && einfo "Sorry, but ${PN} does not support the LINGUAS:" ${nols}
121 export LINGUAS=${newls:1}
124 # @FUNCTION: make_wrapper
125 # @USAGE: <wrapper> <target> [chdir] [libpaths] [installpath]
127 # Create a shell wrapper script named wrapper in installpath
128 # (defaults to the bindir) to execute target (default of wrapper) by
129 # first optionally setting LD_LIBRARY_PATH to the colon-delimited
130 # libpaths followed by optionally changing directory to chdir.
132 local wrapper=$1 bin=$2 chdir=$3 libdir=$4 path=$5
133 local tmpwrapper=$(emktemp)
134 has "${EAPI:-0}" 0 1 2 && local EPREFIX=""
138 if [[ -n ${libdir} ]] ; then
140 if [[ ${CHOST} == *-darwin* ]] ; then
141 var=DYLD_LIBRARY_PATH
146 if [ "\${${var}+set}" = "set" ] ; then
147 export ${var}="\${${var}}:${EPREFIX}${libdir}"
149 export ${var}="${EPREFIX}${libdir}"
153 [[ -n ${chdir} ]] && printf 'cd "%s" &&\n' "${EPREFIX}${chdir}"
154 # We don't want to quote ${bin} so that people can pass complex
155 # things as ${bin} ... "./someprog --args"
156 printf 'exec %s "$@"\n' "${bin/#\//${EPREFIX}/}"
158 chmod go+rx "${tmpwrapper}"
160 if [[ -n ${path} ]] ; then
164 newexe "${tmpwrapper}" "${wrapper}"
167 newbin "${tmpwrapper}" "${wrapper}" || die
171 # @FUNCTION: path_exists
172 # @USAGE: [-a|-o] <paths>
174 # Check if the specified paths exist. Works for all types of paths
175 # (files/dirs/etc...). The -a and -o flags control the requirements
176 # of the paths. They correspond to "and" and "or" logic. So the -a
177 # flag means all the paths must exist while the -o flag means at least
178 # one of the paths must exist. The default behavior is "and". If no
179 # paths are specified, then the return value is "false".
182 [[ ${opt} == -[ao] ]] && shift || opt="-a"
184 # no paths -> return false
185 # same behavior as: [[ -e "" ]]
186 [[ $# -eq 0 ]] && return 1
195 -a) return $(( r != 0 )) ;;
196 -o) return $(( r == $# )) ;;
200 # @FUNCTION: use_if_iuse
203 # Return true if the given flag is in USE and IUSE.
205 # Note that this function should not be used in the global scope.
207 in_iuse $1 || return 1
211 # @FUNCTION: optfeature
212 # @USAGE: <short description> <package atom to match> [other atoms]
214 # Print out a message suggesting an optional package (or packages)
215 # not currently installed which provides the described functionality.
217 # The following snippet would suggest app-misc/foo for optional foo support,
218 # app-misc/bar or app-misc/baz[bar] for optional bar support
219 # and either both app-misc/a and app-misc/b or app-misc/c for alphabet support.
221 # optfeature "foo support" app-misc/foo
222 # optfeature "bar support" app-misc/bar app-misc/baz[bar]
223 # optfeature "alphabet support" "app-misc/a app-misc/b" app-misc/c
226 debug-print-function ${FUNCNAME} "$@"
233 if has_version "${j}"; then
240 if [[ ${flag} -eq 1 ]]; then
244 if [[ ${flag} -eq 0 ]]; then
250 msg="${msg:0: -4} for ${desc}"
262 # Sleep for the specified number of seconds (default of 5 seconds). Useful when
263 # printing a message the user should probably be reading and often used in
264 # conjunction with the ebeep function. If the EPAUSE_IGNORE env var is set,
265 # don't wait at all. Defined in EAPIs 0 1 and 2.
267 [[ -z ${EPAUSE_IGNORE} ]] && sleep ${1:-5}
271 # @USAGE: [number of beeps]
273 # Issue the specified number of beeps (default of 5 beeps). Useful when
274 # printing a message the user should probably be reading and often used in
275 # conjunction with the epause function. If the EBEEP_IGNORE env var is set,
276 # don't beep at all. Defined in EAPIs 0 1 and 2.
279 if [[ -z ${EBEEP_IGNORE} ]] ; then
280 for ((n=1 ; n <= ${1:-5} ; n++)) ; do
282 sleep 0.1 &>/dev/null ; sleep 0,1 &>/dev/null
293 ewarn "QA Notice: ebeep is not defined in EAPI=${EAPI}, please file a bug at https://bugs.gentoo.org"
297 ewarn "QA Notice: epause is not defined in EAPI=${EAPI}, please file a bug at https://bugs.gentoo.org"
307 # @USAGE: <USE flag> [true output] [false output] [true suffix] [false suffix]
309 # Proxy to declare usex for package managers or EAPIs that do not provide it
310 # and use the package manager implementation when available (i.e. EAPI >= 5).
311 # If USE flag is set, echo [true output][true suffix] (defaults to "yes"),
312 # otherwise echo [false output][false suffix] (defaults to "no").
313 usex() { use "$1" && echo "${2-yes}$4" || echo "${3-no}$5" ; } #382963
321 # @FUNCTION: einstalldocs
323 # Install documentation using DOCS and HTML_DOCS, in EAPIs that do not
324 # provide this function. When available (i.e., in EAPI 6 or later),
325 # the package manager implementation should be used instead.
327 # If DOCS is declared and non-empty, all files listed in it are
328 # installed. The files must exist, otherwise the function will fail.
329 # In EAPI 4 and 5, DOCS may specify directories as well; in earlier
330 # EAPIs using directories is unsupported.
332 # If DOCS is not declared, the files matching patterns given
333 # in the default EAPI implementation of src_install will be installed.
334 # If this is undesired, DOCS can be set to empty value to prevent any
335 # documentation from being installed.
337 # If HTML_DOCS is declared and non-empty, all files and/or directories
338 # listed in it are installed as HTML docs (using dohtml).
340 # Both DOCS and HTML_DOCS can either be an array or a whitespace-
341 # separated list. Whenever directories are allowed, '<directory>/.' may
342 # be specified in order to install all files within the directory
343 # without creating a sub-directory in docdir.
345 # Passing additional options to dodoc and dohtml is not supported.
346 # If you needed such a thing, you need to call those helpers explicitly.
348 debug-print-function ${FUNCNAME} "${@}"
351 has ${EAPI} 0 1 2 3 && dodoc_opts=
353 if ! declare -p DOCS &>/dev/null ; then
355 for d in README* ChangeLog AUTHORS NEWS TODO CHANGES \
356 THANKS BUGS FAQ CREDITS CHANGELOG ; do
357 if [[ -s ${d} ]] ; then
361 elif [[ $(declare -p DOCS) == "declare -a"* ]] ; then
362 if [[ ${DOCS[@]} ]] ; then
363 dodoc ${dodoc_opts} "${DOCS[@]}" || die
366 if [[ ${DOCS} ]] ; then
367 dodoc ${dodoc_opts} ${DOCS} || die
371 if [[ $(declare -p HTML_DOCS 2>/dev/null) == "declare -a"* ]] ; then
372 if [[ ${HTML_DOCS[@]} ]] ; then
373 dohtml -r "${HTML_DOCS[@]}" || die
376 if [[ ${HTML_DOCS} ]] ; then
377 dohtml -r ${HTML_DOCS} || die
387 # Determines whether the given flag is in IUSE. Strips IUSE default
388 # prefixes as necessary. In EAPIs where it is available (i.e., EAPI 6
389 # or later), the package manager implementation should be used instead.
391 # Note that this function must not be used in the global scope.
393 debug-print-function ${FUNCNAME} "${@}"
394 [[ ${#} -eq 1 ]] || die "Invalid args to ${FUNCNAME}()"
397 local liuse=( ${IUSE} )
399 has "${flag}" "${liuse[@]#[+-]}"
411 # Proxy to ewarn for package managers that don't provide eqawarn and use the PM
412 # implementation if available. Reuses PORTAGE_ELOG_CLASSES as set by the dev
414 if ! declare -F eqawarn >/dev/null ; then
416 has qa ${PORTAGE_ELOG_CLASSES} && ewarn "$@"