eutils.eclass: Disable eqawarn in EAPI 7.
[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 \
24                 toolchain-funcs vcs-clean
25         ;;
26 esac
27
28 # @FUNCTION: emktemp
29 # @USAGE: [temp dir]
30 # @DESCRIPTION:
31 # Cheap replacement for when debianutils (and thus mktemp)
32 # does not exist on the users system.
33 emktemp() {
34         local exe="touch"
35         [[ $1 == -d ]] && exe="mkdir" && shift
36         local topdir=$1
37
38         if [[ -z ${topdir} ]] ; then
39                 [[ -z ${T} ]] \
40                         && topdir="/tmp" \
41                         || topdir=${T}
42         fi
43
44         if ! type -P mktemp > /dev/null ; then
45                 # system lacks `mktemp` so we have to fake it
46                 local tmp=/
47                 while [[ -e ${tmp} ]] ; do
48                         tmp=${topdir}/tmp.${RANDOM}.${RANDOM}.${RANDOM}
49                 done
50                 ${exe} "${tmp}" || ${exe} -p "${tmp}"
51                 echo "${tmp}"
52         else
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
57                 else
58                         TMPDIR="${topdir}" mktemp -dt tmp.XXXXXXXXXX
59                 fi
60         fi
61 }
62
63 # @FUNCTION: edos2unix
64 # @USAGE: <file> [more files ...]
65 # @DESCRIPTION:
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.
70 edos2unix() {
71         [[ $# -eq 0 ]] && return 0
72         sed -i 's/\r$//' -- "$@" || die
73 }
74
75 # @FUNCTION: strip-linguas
76 # @USAGE: [<allow LINGUAS>|<-i|-u> <directories of .po files>]
77 # @DESCRIPTION:
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
84 # of the lists.
85 strip-linguas() {
86         local ls newls nols
87         if [[ $1 == "-i" ]] || [[ $1 == "-u" ]] ; then
88                 local op=$1; shift
89                 ls=$(find "$1" -name '*.po' -exec basename {} .po ';'); shift
90                 local d f
91                 for d in "$@" ; do
92                         if [[ ${op} == "-u" ]] ; then
93                                 newls=${ls}
94                         else
95                                 newls=""
96                         fi
97                         for f in $(find "$d" -name '*.po' -exec basename {} .po ';') ; do
98                                 if [[ ${op} == "-i" ]] ; then
99                                         has ${f} ${ls} && newls="${newls} ${f}"
100                                 else
101                                         has ${f} ${ls} || newls="${newls} ${f}"
102                                 fi
103                         done
104                         ls=${newls}
105                 done
106         else
107                 ls="$@"
108         fi
109
110         nols=""
111         newls=""
112         for f in ${LINGUAS} ; do
113                 if has ${f} ${ls} ; then
114                         newls="${newls} ${f}"
115                 else
116                         nols="${nols} ${f}"
117                 fi
118         done
119         [[ -n ${nols} ]] \
120                 && einfo "Sorry, but ${PN} does not support the LINGUAS:" ${nols}
121         export LINGUAS=${newls:1}
122 }
123
124 # @FUNCTION: make_wrapper
125 # @USAGE: <wrapper> <target> [chdir] [libpaths] [installpath]
126 # @DESCRIPTION:
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.
131 make_wrapper() {
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=""
135
136         (
137         echo '#!/bin/sh'
138         if [[ -n ${libdir} ]] ; then
139                 local var
140                 if [[ ${CHOST} == *-darwin* ]] ; then
141                         var=DYLD_LIBRARY_PATH
142                 else
143                         var=LD_LIBRARY_PATH
144                 fi
145                 cat <<-EOF
146                         if [ "\${${var}+set}" = "set" ] ; then
147                                 export ${var}="\${${var}}:${EPREFIX}${libdir}"
148                         else
149                                 export ${var}="${EPREFIX}${libdir}"
150                         fi
151                 EOF
152         fi
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}/}"
157         ) > "${tmpwrapper}"
158         chmod go+rx "${tmpwrapper}"
159
160         if [[ -n ${path} ]] ; then
161                 (
162                 exeopts -m 0755
163                 exeinto "${path}"
164                 newexe "${tmpwrapper}" "${wrapper}"
165                 ) || die
166         else
167                 newbin "${tmpwrapper}" "${wrapper}" || die
168         fi
169 }
170
171 # @FUNCTION: path_exists
172 # @USAGE: [-a|-o] <paths>
173 # @DESCRIPTION:
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".
180 path_exists() {
181         local opt=$1
182         [[ ${opt} == -[ao] ]] && shift || opt="-a"
183
184         # no paths -> return false
185         # same behavior as: [[ -e "" ]]
186         [[ $# -eq 0 ]] && return 1
187
188         local p r=0
189         for p in "$@" ; do
190                 [[ -e ${p} ]]
191                 : $(( r += $? ))
192         done
193
194         case ${opt} in
195                 -a) return $(( r != 0 )) ;;
196                 -o) return $(( r == $# )) ;;
197         esac
198 }
199
200 # @FUNCTION: use_if_iuse
201 # @USAGE: <flag>
202 # @DESCRIPTION:
203 # Return true if the given flag is in USE and IUSE.
204 #
205 # Note that this function should not be used in the global scope.
206 use_if_iuse() {
207         in_iuse $1 || return 1
208         use $1
209 }
210
211 # @FUNCTION: optfeature
212 # @USAGE: <short description> <package atom to match> [other atoms]
213 # @DESCRIPTION:
214 # Print out a message suggesting an optional package (or packages)
215 # not currently installed which provides the described functionality.
216 #
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.
220 # @CODE
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
224 # @CODE
225 optfeature() {
226         debug-print-function ${FUNCNAME} "$@"
227         local i j msg
228         local desc=$1
229         local flag=0
230         shift
231         for i; do
232                 for j in ${i}; do
233                         if has_version "${j}"; then
234                                 flag=1
235                         else
236                                 flag=0
237                                 break
238                         fi
239                 done
240                 if [[ ${flag} -eq 1 ]]; then
241                         break
242                 fi
243         done
244         if [[ ${flag} -eq 0 ]]; then
245                 for i; do
246                         msg=" "
247                         for j in ${i}; do
248                                 msg+=" ${j} and"
249                         done
250                         msg="${msg:0: -4} for ${desc}"
251                         elog "${msg}"
252                 done
253         fi
254 }
255
256 case ${EAPI:-0} in
257 0|1|2)
258
259 # @FUNCTION: epause
260 # @USAGE: [seconds]
261 # @DESCRIPTION:
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.
266 epause() {
267         [[ -z ${EPAUSE_IGNORE} ]] && sleep ${1:-5}
268 }
269
270 # @FUNCTION: ebeep
271 # @USAGE: [number of beeps]
272 # @DESCRIPTION:
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.
277 ebeep() {
278         local n
279         if [[ -z ${EBEEP_IGNORE} ]] ; then
280                 for ((n=1 ; n <= ${1:-5} ; n++)) ; do
281                         echo -ne "\a"
282                         sleep 0.1 &>/dev/null ; sleep 0,1 &>/dev/null
283                         echo -ne "\a"
284                         sleep 1
285                 done
286         fi
287 }
288
289 ;;
290 *)
291
292 ebeep() {
293         ewarn "QA Notice: ebeep is not defined in EAPI=${EAPI}, please file a bug at https://bugs.gentoo.org"
294 }
295
296 epause() {
297         ewarn "QA Notice: epause is not defined in EAPI=${EAPI}, please file a bug at https://bugs.gentoo.org"
298 }
299
300 ;;
301 esac
302
303 case ${EAPI:-0} in
304 0|1|2|3|4)
305
306 # @FUNCTION: usex
307 # @USAGE: <USE flag> [true output] [false output] [true suffix] [false suffix]
308 # @DESCRIPTION:
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
314
315 ;;
316 esac
317
318 case ${EAPI:-0} in
319 0|1|2|3|4|5)
320
321 # @FUNCTION: einstalldocs
322 # @DESCRIPTION:
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.
326 #
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.
331 #
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.
336 #
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).
339 #
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.
344 #
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.
347 einstalldocs() {
348         debug-print-function ${FUNCNAME} "${@}"
349
350         local dodoc_opts=-r
351         has ${EAPI} 0 1 2 3 && dodoc_opts=
352
353         if ! declare -p DOCS &>/dev/null ; then
354                 local d
355                 for d in README* ChangeLog AUTHORS NEWS TODO CHANGES \
356                                 THANKS BUGS FAQ CREDITS CHANGELOG ; do
357                         if [[ -s ${d} ]] ; then
358                                 dodoc "${d}" || die
359                         fi
360                 done
361         elif [[ $(declare -p DOCS) == "declare -a"* ]] ; then
362                 if [[ ${DOCS[@]} ]] ; then
363                         dodoc ${dodoc_opts} "${DOCS[@]}" || die
364                 fi
365         else
366                 if [[ ${DOCS} ]] ; then
367                         dodoc ${dodoc_opts} ${DOCS} || die
368                 fi
369         fi
370
371         if [[ $(declare -p HTML_DOCS 2>/dev/null) == "declare -a"* ]] ; then
372                 if [[ ${HTML_DOCS[@]} ]] ; then
373                         dohtml -r "${HTML_DOCS[@]}" || die
374                 fi
375         else
376                 if [[ ${HTML_DOCS} ]] ; then
377                         dohtml -r ${HTML_DOCS} || die
378                 fi
379         fi
380
381         return 0
382 }
383
384 # @FUNCTION: in_iuse
385 # @USAGE: <flag>
386 # @DESCRIPTION:
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.
390 #
391 # Note that this function must not be used in the global scope.
392 in_iuse() {
393         debug-print-function ${FUNCNAME} "${@}"
394         [[ ${#} -eq 1 ]] || die "Invalid args to ${FUNCNAME}()"
395
396         local flag=${1}
397         local liuse=( ${IUSE} )
398
399         has "${flag}" "${liuse[@]#[+-]}"
400 }
401
402 ;;
403 esac
404
405 case ${EAPI:-0} in
406 0|1|2|3|4|5|6)
407
408 # @FUNCTION: eqawarn
409 # @USAGE: [message]
410 # @DESCRIPTION:
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
413 # profile.
414 if ! declare -F eqawarn >/dev/null ; then
415         eqawarn() {
416                 has qa ${PORTAGE_ELOG_CLASSES} && ewarn "$@"
417                 :
418         }
419 fi
420
421 ;;
422 esac
423
424 fi