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