eutils.eclass: Update function documentation.
[gentoo.git] / eclass / eutils.eclass
1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: eutils.eclass
5 # @MAINTAINER:
6 # base-system@gentoo.org
7 # @BLURB: many extra (but common) functions that are used in ebuilds
8 # @DESCRIPTION:
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.
13 #
14 # Due to the nature of this eclass, some functions may have maintainers
15 # different from the overall eclass!
16
17 if [[ -z ${_EUTILS_ECLASS} ]]; then
18 _EUTILS_ECLASS=1
19
20 # implicitly inherited (now split) eclasses
21 case ${EAPI:-0} in
22 0|1|2|3|4|5|6)
23         inherit desktop epatch estack ltprune multilib preserve-libs toolchain-funcs
24         ;;
25 esac
26
27 # @FUNCTION: eqawarn
28 # @USAGE: [message]
29 # @DESCRIPTION:
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
32 # profile.
33 if ! declare -F eqawarn >/dev/null ; then
34         eqawarn() {
35                 has qa ${PORTAGE_ELOG_CLASSES} && ewarn "$@"
36                 :
37         }
38 fi
39
40 # @FUNCTION: ecvs_clean
41 # @USAGE: [list of dirs]
42 # @DESCRIPTION:
43 # Remove CVS directories recursiveley.  Useful when a source tarball contains
44 # internal CVS directories.  Defaults to $PWD.
45 ecvs_clean() {
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
49 }
50
51 # @FUNCTION: esvn_clean
52 # @USAGE: [list of dirs]
53 # @DESCRIPTION:
54 # Remove .svn directories recursiveley.  Useful when a source tarball contains
55 # internal Subversion directories.  Defaults to $PWD.
56 esvn_clean() {
57         [[ $# -eq 0 ]] && set -- .
58         find "$@" -type d -name '.svn' -prune -print0 | xargs -0 rm -rf
59 }
60
61 # @FUNCTION: egit_clean
62 # @USAGE: [list of dirs]
63 # @DESCRIPTION:
64 # Remove .git* directories/files recursiveley.  Useful when a source tarball
65 # contains internal Git directories.  Defaults to $PWD.
66 egit_clean() {
67         [[ $# -eq 0 ]] && set -- .
68         find "$@" -type d -name '.git*' -prune -print0 | xargs -0 rm -rf
69 }
70
71 # @FUNCTION: emktemp
72 # @USAGE: [temp dir]
73 # @DESCRIPTION:
74 # Cheap replacement for when debianutils (and thus mktemp)
75 # does not exist on the users system.
76 emktemp() {
77         local exe="touch"
78         [[ $1 == -d ]] && exe="mkdir" && shift
79         local topdir=$1
80
81         if [[ -z ${topdir} ]] ; then
82                 [[ -z ${T} ]] \
83                         && topdir="/tmp" \
84                         || topdir=${T}
85         fi
86
87         if ! type -P mktemp > /dev/null ; then
88                 # system lacks `mktemp` so we have to fake it
89                 local tmp=/
90                 while [[ -e ${tmp} ]] ; do
91                         tmp=${topdir}/tmp.${RANDOM}.${RANDOM}.${RANDOM}
92                 done
93                 ${exe} "${tmp}" || ${exe} -p "${tmp}"
94                 echo "${tmp}"
95         else
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
100                 else
101                         TMPDIR="${topdir}" mktemp -dt tmp.XXXXXXXXXX
102                 fi
103         fi
104 }
105
106 # @FUNCTION: edos2unix
107 # @USAGE: <file> [more files ...]
108 # @DESCRIPTION:
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.
113 edos2unix() {
114         [[ $# -eq 0 ]] && return 0
115         sed -i 's/\r$//' -- "$@" || die
116 }
117
118 # @FUNCTION: strip-linguas
119 # @USAGE: [<allow LINGUAS>|<-i|-u> <directories of .po files>]
120 # @DESCRIPTION:
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
127 # of the lists.
128 strip-linguas() {
129         local ls newls nols
130         if [[ $1 == "-i" ]] || [[ $1 == "-u" ]] ; then
131                 local op=$1; shift
132                 ls=$(find "$1" -name '*.po' -exec basename {} .po ';'); shift
133                 local d f
134                 for d in "$@" ; do
135                         if [[ ${op} == "-u" ]] ; then
136                                 newls=${ls}
137                         else
138                                 newls=""
139                         fi
140                         for f in $(find "$d" -name '*.po' -exec basename {} .po ';') ; do
141                                 if [[ ${op} == "-i" ]] ; then
142                                         has ${f} ${ls} && newls="${newls} ${f}"
143                                 else
144                                         has ${f} ${ls} || newls="${newls} ${f}"
145                                 fi
146                         done
147                         ls=${newls}
148                 done
149         else
150                 ls="$@"
151         fi
152
153         nols=""
154         newls=""
155         for f in ${LINGUAS} ; do
156                 if has ${f} ${ls} ; then
157                         newls="${newls} ${f}"
158                 else
159                         nols="${nols} ${f}"
160                 fi
161         done
162         [[ -n ${nols} ]] \
163                 && einfo "Sorry, but ${PN} does not support the LINGUAS:" ${nols}
164         export LINGUAS=${newls:1}
165 }
166
167 # @FUNCTION: make_wrapper
168 # @USAGE: <wrapper> <target> [chdir] [libpaths] [installpath]
169 # @DESCRIPTION:
170 # Create a shell wrapper script named wrapper in installpath
171 # (defaults to the bindir) to execute target (default of wrapper) by
172 # first optionally setting LD_LIBRARY_PATH to the colon-delimited
173 # libpaths followed by optionally changing directory to chdir.
174 make_wrapper() {
175         local wrapper=$1 bin=$2 chdir=$3 libdir=$4 path=$5
176         local tmpwrapper=$(emktemp)
177         has "${EAPI:-0}" 0 1 2 && local EPREFIX=""
178
179         (
180         echo '#!/bin/sh'
181         [[ -n ${chdir} ]] && printf 'cd "%s"\n' "${EPREFIX}${chdir}"
182         if [[ -n ${libdir} ]] ; then
183                 local var
184                 if [[ ${CHOST} == *-darwin* ]] ; then
185                         var=DYLD_LIBRARY_PATH
186                 else
187                         var=LD_LIBRARY_PATH
188                 fi
189                 cat <<-EOF
190                         if [ "\${${var}+set}" = "set" ] ; then
191                                 export ${var}="\${${var}}:${EPREFIX}${libdir}"
192                         else
193                                 export ${var}="${EPREFIX}${libdir}"
194                         fi
195                 EOF
196         fi
197         # We don't want to quote ${bin} so that people can pass complex
198         # things as ${bin} ... "./someprog --args"
199         printf 'exec %s "$@"\n' "${bin/#\//${EPREFIX}/}"
200         ) > "${tmpwrapper}"
201         chmod go+rx "${tmpwrapper}"
202
203         if [[ -n ${path} ]] ; then
204                 (
205                 exeinto "${path}"
206                 newexe "${tmpwrapper}" "${wrapper}"
207                 ) || die
208         else
209                 newbin "${tmpwrapper}" "${wrapper}" || die
210         fi
211 }
212
213 # @FUNCTION: path_exists
214 # @USAGE: [-a|-o] <paths>
215 # @DESCRIPTION:
216 # Check if the specified paths exist.  Works for all types of paths
217 # (files/dirs/etc...).  The -a and -o flags control the requirements
218 # of the paths.  They correspond to "and" and "or" logic.  So the -a
219 # flag means all the paths must exist while the -o flag means at least
220 # one of the paths must exist.  The default behavior is "and".  If no
221 # paths are specified, then the return value is "false".
222 path_exists() {
223         local opt=$1
224         [[ ${opt} == -[ao] ]] && shift || opt="-a"
225
226         # no paths -> return false
227         # same behavior as: [[ -e "" ]]
228         [[ $# -eq 0 ]] && return 1
229
230         local p r=0
231         for p in "$@" ; do
232                 [[ -e ${p} ]]
233                 : $(( r += $? ))
234         done
235
236         case ${opt} in
237                 -a) return $(( r != 0 )) ;;
238                 -o) return $(( r == $# )) ;;
239         esac
240 }
241
242 # @FUNCTION: use_if_iuse
243 # @USAGE: <flag>
244 # @DESCRIPTION:
245 # Return true if the given flag is in USE and IUSE.
246 #
247 # Note that this function should not be used in the global scope.
248 use_if_iuse() {
249         in_iuse $1 || return 1
250         use $1
251 }
252
253 # @FUNCTION: optfeature
254 # @USAGE: <short description> <package atom to match> [other atoms]
255 # @DESCRIPTION:
256 # Print out a message suggesting an optional package (or packages)
257 # not currently installed which provides the described functionality.
258 #
259 # The following snippet would suggest app-misc/foo for optional foo support,
260 # app-misc/bar or app-misc/baz[bar] for optional bar support
261 # and either both app-misc/a and app-misc/b or app-misc/c for alphabet support.
262 # @CODE
263 #       optfeature "foo support" app-misc/foo
264 #       optfeature "bar support" app-misc/bar app-misc/baz[bar]
265 #       optfeature "alphabet support" "app-misc/a app-misc/b" app-misc/c
266 # @CODE
267 optfeature() {
268         debug-print-function ${FUNCNAME} "$@"
269         local i j msg
270         local desc=$1
271         local flag=0
272         shift
273         for i; do
274                 for j in ${i}; do
275                         if has_version "${j}"; then
276                                 flag=1
277                         else
278                                 flag=0
279                                 break
280                         fi
281                 done
282                 if [[ ${flag} -eq 1 ]]; then
283                         break
284                 fi
285         done
286         if [[ ${flag} -eq 0 ]]; then
287                 for i; do
288                         msg=" "
289                         for j in ${i}; do
290                                 msg+=" ${j} and"
291                         done
292                         msg="${msg:0: -4} for ${desc}"
293                         elog "${msg}"
294                 done
295         fi
296 }
297
298 case ${EAPI:-0} in
299 0|1|2)
300
301 # @FUNCTION: epause
302 # @USAGE: [seconds]
303 # @DESCRIPTION:
304 # Sleep for the specified number of seconds (default of 5 seconds).  Useful when
305 # printing a message the user should probably be reading and often used in
306 # conjunction with the ebeep function.  If the EPAUSE_IGNORE env var is set,
307 # don't wait at all. Defined in EAPIs 0 1 and 2.
308 epause() {
309         [[ -z ${EPAUSE_IGNORE} ]] && sleep ${1:-5}
310 }
311
312 # @FUNCTION: ebeep
313 # @USAGE: [number of beeps]
314 # @DESCRIPTION:
315 # Issue the specified number of beeps (default of 5 beeps).  Useful when
316 # printing a message the user should probably be reading and often used in
317 # conjunction with the epause function.  If the EBEEP_IGNORE env var is set,
318 # don't beep at all. Defined in EAPIs 0 1 and 2.
319 ebeep() {
320         local n
321         if [[ -z ${EBEEP_IGNORE} ]] ; then
322                 for ((n=1 ; n <= ${1:-5} ; n++)) ; do
323                         echo -ne "\a"
324                         sleep 0.1 &>/dev/null ; sleep 0,1 &>/dev/null
325                         echo -ne "\a"
326                         sleep 1
327                 done
328         fi
329 }
330
331 ;;
332 *)
333
334 ebeep() {
335         ewarn "QA Notice: ebeep is not defined in EAPI=${EAPI}, please file a bug at https://bugs.gentoo.org"
336 }
337
338 epause() {
339         ewarn "QA Notice: epause is not defined in EAPI=${EAPI}, please file a bug at https://bugs.gentoo.org"
340 }
341
342 ;;
343 esac
344
345 case ${EAPI:-0} in
346 0|1|2|3|4)
347
348 # @FUNCTION: usex
349 # @USAGE: <USE flag> [true output] [false output] [true suffix] [false suffix]
350 # @DESCRIPTION:
351 # Proxy to declare usex for package managers or EAPIs that do not provide it
352 # and use the package manager implementation when available (i.e. EAPI >= 5).
353 # If USE flag is set, echo [true output][true suffix] (defaults to "yes"),
354 # otherwise echo [false output][false suffix] (defaults to "no").
355 usex() { use "$1" && echo "${2-yes}$4" || echo "${3-no}$5" ; } #382963
356
357 ;;
358 esac
359
360 case ${EAPI:-0} in
361 0|1|2|3|4|5)
362
363 # @FUNCTION: einstalldocs
364 # @DESCRIPTION:
365 # Install documentation using DOCS and HTML_DOCS, in EAPIs that do not
366 # provide this function.  When available (i.e., in EAPI 6 or later),
367 # the package manager implementation should be used instead.
368 #
369 # If DOCS is declared and non-empty, all files listed in it are
370 # installed.  The files must exist, otherwise the function will fail.
371 # In EAPI 4 and 5, DOCS may specify directories as well; in earlier
372 # EAPIs using directories is unsupported.
373 #
374 # If DOCS is not declared, the files matching patterns given
375 # in the default EAPI implementation of src_install will be installed.
376 # If this is undesired, DOCS can be set to empty value to prevent any
377 # documentation from being installed.
378 #
379 # If HTML_DOCS is declared and non-empty, all files and/or directories
380 # listed in it are installed as HTML docs (using dohtml).
381 #
382 # Both DOCS and HTML_DOCS can either be an array or a whitespace-
383 # separated list. Whenever directories are allowed, '<directory>/.' may
384 # be specified in order to install all files within the directory
385 # without creating a sub-directory in docdir.
386 #
387 # Passing additional options to dodoc and dohtml is not supported.
388 # If you needed such a thing, you need to call those helpers explicitly.
389 einstalldocs() {
390         debug-print-function ${FUNCNAME} "${@}"
391
392         local dodoc_opts=-r
393         has ${EAPI} 0 1 2 3 && dodoc_opts=
394
395         if ! declare -p DOCS &>/dev/null ; then
396                 local d
397                 for d in README* ChangeLog AUTHORS NEWS TODO CHANGES \
398                                 THANKS BUGS FAQ CREDITS CHANGELOG ; do
399                         if [[ -s ${d} ]] ; then
400                                 dodoc "${d}" || die
401                         fi
402                 done
403         elif [[ $(declare -p DOCS) == "declare -a"* ]] ; then
404                 if [[ ${DOCS[@]} ]] ; then
405                         dodoc ${dodoc_opts} "${DOCS[@]}" || die
406                 fi
407         else
408                 if [[ ${DOCS} ]] ; then
409                         dodoc ${dodoc_opts} ${DOCS} || die
410                 fi
411         fi
412
413         if [[ $(declare -p HTML_DOCS 2>/dev/null) == "declare -a"* ]] ; then
414                 if [[ ${HTML_DOCS[@]} ]] ; then
415                         dohtml -r "${HTML_DOCS[@]}" || die
416                 fi
417         else
418                 if [[ ${HTML_DOCS} ]] ; then
419                         dohtml -r ${HTML_DOCS} || die
420                 fi
421         fi
422
423         return 0
424 }
425
426 # @FUNCTION: in_iuse
427 # @USAGE: <flag>
428 # @DESCRIPTION:
429 # Determines whether the given flag is in IUSE.  Strips IUSE default
430 # prefixes as necessary.  In EAPIs where it is available (i.e., EAPI 6
431 # or later), the package manager implementation should be used instead.
432 #
433 # Note that this function must not be used in the global scope.
434 in_iuse() {
435         debug-print-function ${FUNCNAME} "${@}"
436         [[ ${#} -eq 1 ]] || die "Invalid args to ${FUNCNAME}()"
437
438         local flag=${1}
439         local liuse=( ${IUSE} )
440
441         has "${flag}" "${liuse[@]#[+-]}"
442 }
443
444 ;;
445 esac
446
447 fi