eutils.eclass: Ban path_exists
[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 path_exists() {
172         eerror "path_exists has been removed.  Please see the following post"
173         eerror "for a replacement snippet:"
174         eerror "https://blogs.gentoo.org/mgorny/2018/08/09/inlining-path_exists/"
175         die "path_exists is banned"
176 }
177
178 # @FUNCTION: use_if_iuse
179 # @USAGE: <flag>
180 # @DESCRIPTION:
181 # Return true if the given flag is in USE and IUSE.
182 #
183 # Note that this function should not be used in the global scope.
184 use_if_iuse() {
185         in_iuse $1 || return 1
186         use $1
187 }
188
189 # @FUNCTION: optfeature
190 # @USAGE: <short description> <package atom to match> [other atoms]
191 # @DESCRIPTION:
192 # Print out a message suggesting an optional package (or packages)
193 # not currently installed which provides the described functionality.
194 #
195 # The following snippet would suggest app-misc/foo for optional foo support,
196 # app-misc/bar or app-misc/baz[bar] for optional bar support
197 # and either both app-misc/a and app-misc/b or app-misc/c for alphabet support.
198 # @CODE
199 #       optfeature "foo support" app-misc/foo
200 #       optfeature "bar support" app-misc/bar app-misc/baz[bar]
201 #       optfeature "alphabet support" "app-misc/a app-misc/b" app-misc/c
202 # @CODE
203 optfeature() {
204         debug-print-function ${FUNCNAME} "$@"
205         local i j msg
206         local desc=$1
207         local flag=0
208         shift
209         for i; do
210                 for j in ${i}; do
211                         if has_version "${j}"; then
212                                 flag=1
213                         else
214                                 flag=0
215                                 break
216                         fi
217                 done
218                 if [[ ${flag} -eq 1 ]]; then
219                         break
220                 fi
221         done
222         if [[ ${flag} -eq 0 ]]; then
223                 for i; do
224                         msg=" "
225                         for j in ${i}; do
226                                 msg+=" ${j} and"
227                         done
228                         msg="${msg:0: -4} for ${desc}"
229                         elog "${msg}"
230                 done
231         fi
232 }
233
234 case ${EAPI:-0} in
235 0|1|2)
236
237 # @FUNCTION: epause
238 # @USAGE: [seconds]
239 # @DESCRIPTION:
240 # Sleep for the specified number of seconds (default of 5 seconds).  Useful when
241 # printing a message the user should probably be reading and often used in
242 # conjunction with the ebeep function.  If the EPAUSE_IGNORE env var is set,
243 # don't wait at all. Defined in EAPIs 0 1 and 2.
244 epause() {
245         [[ -z ${EPAUSE_IGNORE} ]] && sleep ${1:-5}
246 }
247
248 # @FUNCTION: ebeep
249 # @USAGE: [number of beeps]
250 # @DESCRIPTION:
251 # Issue the specified number of beeps (default of 5 beeps).  Useful when
252 # printing a message the user should probably be reading and often used in
253 # conjunction with the epause function.  If the EBEEP_IGNORE env var is set,
254 # don't beep at all. Defined in EAPIs 0 1 and 2.
255 ebeep() {
256         local n
257         if [[ -z ${EBEEP_IGNORE} ]] ; then
258                 for ((n=1 ; n <= ${1:-5} ; n++)) ; do
259                         echo -ne "\a"
260                         sleep 0.1 &>/dev/null ; sleep 0,1 &>/dev/null
261                         echo -ne "\a"
262                         sleep 1
263                 done
264         fi
265 }
266
267 ;;
268 *)
269
270 ebeep() {
271         ewarn "QA Notice: ebeep is not defined in EAPI=${EAPI}, please file a bug at https://bugs.gentoo.org"
272 }
273
274 epause() {
275         ewarn "QA Notice: epause is not defined in EAPI=${EAPI}, please file a bug at https://bugs.gentoo.org"
276 }
277
278 ;;
279 esac
280
281 case ${EAPI:-0} in
282 0|1|2|3|4)
283
284 # @FUNCTION: usex
285 # @USAGE: <USE flag> [true output] [false output] [true suffix] [false suffix]
286 # @DESCRIPTION:
287 # Proxy to declare usex for package managers or EAPIs that do not provide it
288 # and use the package manager implementation when available (i.e. EAPI >= 5).
289 # If USE flag is set, echo [true output][true suffix] (defaults to "yes"),
290 # otherwise echo [false output][false suffix] (defaults to "no").
291 usex() { use "$1" && echo "${2-yes}$4" || echo "${3-no}$5" ; } #382963
292
293 ;;
294 esac
295
296 case ${EAPI:-0} in
297 0|1|2|3|4|5)
298
299 # @FUNCTION: einstalldocs
300 # @DESCRIPTION:
301 # Install documentation using DOCS and HTML_DOCS, in EAPIs that do not
302 # provide this function.  When available (i.e., in EAPI 6 or later),
303 # the package manager implementation should be used instead.
304 #
305 # If DOCS is declared and non-empty, all files listed in it are
306 # installed.  The files must exist, otherwise the function will fail.
307 # In EAPI 4 and 5, DOCS may specify directories as well; in earlier
308 # EAPIs using directories is unsupported.
309 #
310 # If DOCS is not declared, the files matching patterns given
311 # in the default EAPI implementation of src_install will be installed.
312 # If this is undesired, DOCS can be set to empty value to prevent any
313 # documentation from being installed.
314 #
315 # If HTML_DOCS is declared and non-empty, all files and/or directories
316 # listed in it are installed as HTML docs (using dohtml).
317 #
318 # Both DOCS and HTML_DOCS can either be an array or a whitespace-
319 # separated list. Whenever directories are allowed, '<directory>/.' may
320 # be specified in order to install all files within the directory
321 # without creating a sub-directory in docdir.
322 #
323 # Passing additional options to dodoc and dohtml is not supported.
324 # If you needed such a thing, you need to call those helpers explicitly.
325 einstalldocs() {
326         debug-print-function ${FUNCNAME} "${@}"
327
328         local dodoc_opts=-r
329         has ${EAPI} 0 1 2 3 && dodoc_opts=
330
331         if ! declare -p DOCS &>/dev/null ; then
332                 local d
333                 for d in README* ChangeLog AUTHORS NEWS TODO CHANGES \
334                                 THANKS BUGS FAQ CREDITS CHANGELOG ; do
335                         if [[ -s ${d} ]] ; then
336                                 dodoc "${d}" || die
337                         fi
338                 done
339         elif [[ $(declare -p DOCS) == "declare -a"* ]] ; then
340                 if [[ ${DOCS[@]} ]] ; then
341                         dodoc ${dodoc_opts} "${DOCS[@]}" || die
342                 fi
343         else
344                 if [[ ${DOCS} ]] ; then
345                         dodoc ${dodoc_opts} ${DOCS} || die
346                 fi
347         fi
348
349         if [[ $(declare -p HTML_DOCS 2>/dev/null) == "declare -a"* ]] ; then
350                 if [[ ${HTML_DOCS[@]} ]] ; then
351                         dohtml -r "${HTML_DOCS[@]}" || die
352                 fi
353         else
354                 if [[ ${HTML_DOCS} ]] ; then
355                         dohtml -r ${HTML_DOCS} || die
356                 fi
357         fi
358
359         return 0
360 }
361
362 # @FUNCTION: in_iuse
363 # @USAGE: <flag>
364 # @DESCRIPTION:
365 # Determines whether the given flag is in IUSE.  Strips IUSE default
366 # prefixes as necessary.  In EAPIs where it is available (i.e., EAPI 6
367 # or later), the package manager implementation should be used instead.
368 #
369 # Note that this function must not be used in the global scope.
370 in_iuse() {
371         debug-print-function ${FUNCNAME} "${@}"
372         [[ ${#} -eq 1 ]] || die "Invalid args to ${FUNCNAME}()"
373
374         local flag=${1}
375         local liuse=( ${IUSE} )
376
377         has "${flag}" "${liuse[@]#[+-]}"
378 }
379
380 ;;
381 esac
382
383 case ${EAPI:-0} in
384 0|1|2|3|4|5|6)
385
386 # @FUNCTION: eqawarn
387 # @USAGE: [message]
388 # @DESCRIPTION:
389 # Proxy to ewarn for package managers that don't provide eqawarn and use the PM
390 # implementation if available. Reuses PORTAGE_ELOG_CLASSES as set by the dev
391 # profile.
392 if ! declare -F eqawarn >/dev/null ; then
393         eqawarn() {
394                 has qa ${PORTAGE_ELOG_CLASSES} && ewarn "$@"
395                 :
396         }
397 fi
398
399 ;;
400 esac
401
402 fi