preserve-libs.eclass: Split off preserve_old_lib from eutils.
[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: _eutils_eprefix_init
168 # @INTERNAL
169 # @DESCRIPTION:
170 # Initialized prefix variables for EAPI<3.
171 _eutils_eprefix_init() {
172         has "${EAPI:-0}" 0 1 2 && : ${ED:=${D}} ${EPREFIX:=} ${EROOT:=${ROOT}}
173 }
174
175 # @FUNCTION: built_with_use
176 # @USAGE: [--hidden] [--missing <action>] [-a|-o] <DEPEND ATOM> <List of USE flags>
177 # @DESCRIPTION:
178 #
179 # Deprecated: Use EAPI 2 use deps in DEPEND|RDEPEND and with has_version calls.
180 #
181 # A temporary hack until portage properly supports DEPENDing on USE
182 # flags being enabled in packages.  This will check to see if the specified
183 # DEPEND atom was built with the specified list of USE flags.  The
184 # --missing option controls the behavior if called on a package that does
185 # not actually support the defined USE flags (aka listed in IUSE).
186 # The default is to abort (call die).  The -a and -o flags control
187 # the requirements of the USE flags.  They correspond to "and" and "or"
188 # logic.  So the -a flag means all listed USE flags must be enabled
189 # while the -o flag means at least one of the listed IUSE flags must be
190 # enabled.  The --hidden option is really for internal use only as it
191 # means the USE flag we're checking is hidden expanded, so it won't be found
192 # in IUSE like normal USE flags.
193 #
194 # Remember that this function isn't terribly intelligent so order of optional
195 # flags matter.
196 built_with_use() {
197         _eutils_eprefix_init
198         local hidden="no"
199         if [[ $1 == "--hidden" ]] ; then
200                 hidden="yes"
201                 shift
202         fi
203
204         local missing_action="die"
205         if [[ $1 == "--missing" ]] ; then
206                 missing_action=$2
207                 shift ; shift
208                 case ${missing_action} in
209                         true|false|die) ;;
210                         *) die "unknown action '${missing_action}'";;
211                 esac
212         fi
213
214         local opt=$1
215         [[ ${opt:0:1} = "-" ]] && shift || opt="-a"
216
217         local PKG=$(best_version $1)
218         [[ -z ${PKG} ]] && die "Unable to resolve $1 to an installed package"
219         shift
220
221         local USEFILE=${EROOT}/var/db/pkg/${PKG}/USE
222         local IUSEFILE=${EROOT}/var/db/pkg/${PKG}/IUSE
223
224         # if the IUSE file doesn't exist, the read will error out, we need to handle
225         # this gracefully
226         if [[ ! -e ${USEFILE} ]] || [[ ! -e ${IUSEFILE} && ${hidden} == "no" ]] ; then
227                 case ${missing_action} in
228                         true)   return 0;;
229                         false)  return 1;;
230                         die)    die "Unable to determine what USE flags $PKG was built with";;
231                 esac
232         fi
233
234         if [[ ${hidden} == "no" ]] ; then
235                 local IUSE_BUILT=( $(<"${IUSEFILE}") )
236                 # Don't check USE_EXPAND #147237
237                 local expand
238                 for expand in $(echo ${USE_EXPAND} | tr '[:upper:]' '[:lower:]') ; do
239                         if [[ $1 == ${expand}_* ]] ; then
240                                 expand=""
241                                 break
242                         fi
243                 done
244                 if [[ -n ${expand} ]] ; then
245                         if ! has $1 ${IUSE_BUILT[@]#[-+]} ; then
246                                 case ${missing_action} in
247                                         true)  return 0;;
248                                         false) return 1;;
249                                         die)   die "$PKG does not actually support the $1 USE flag!";;
250                                 esac
251                         fi
252                 fi
253         fi
254
255         local USE_BUILT=$(<${USEFILE})
256         while [[ $# -gt 0 ]] ; do
257                 if [[ ${opt} = "-o" ]] ; then
258                         has $1 ${USE_BUILT} && return 0
259                 else
260                         has $1 ${USE_BUILT} || return 1
261                 fi
262                 shift
263         done
264         [[ ${opt} = "-a" ]]
265 }
266
267 # @FUNCTION: make_wrapper
268 # @USAGE: <wrapper> <target> [chdir] [libpaths] [installpath]
269 # @DESCRIPTION:
270 # Create a shell wrapper script named wrapper in installpath
271 # (defaults to the bindir) to execute target (default of wrapper) by
272 # first optionally setting LD_LIBRARY_PATH to the colon-delimited
273 # libpaths followed by optionally changing directory to chdir.
274 make_wrapper() {
275         _eutils_eprefix_init
276         local wrapper=$1 bin=$2 chdir=$3 libdir=$4 path=$5
277         local tmpwrapper=$(emktemp)
278
279         (
280         echo '#!/bin/sh'
281         [[ -n ${chdir} ]] && printf 'cd "%s"\n' "${EPREFIX}${chdir}"
282         if [[ -n ${libdir} ]] ; then
283                 local var
284                 if [[ ${CHOST} == *-darwin* ]] ; then
285                         var=DYLD_LIBRARY_PATH
286                 else
287                         var=LD_LIBRARY_PATH
288                 fi
289                 cat <<-EOF
290                         if [ "\${${var}+set}" = "set" ] ; then
291                                 export ${var}="\${${var}}:${EPREFIX}${libdir}"
292                         else
293                                 export ${var}="${EPREFIX}${libdir}"
294                         fi
295                 EOF
296         fi
297         # We don't want to quote ${bin} so that people can pass complex
298         # things as ${bin} ... "./someprog --args"
299         printf 'exec %s "$@"\n' "${bin/#\//${EPREFIX}/}"
300         ) > "${tmpwrapper}"
301         chmod go+rx "${tmpwrapper}"
302
303         if [[ -n ${path} ]] ; then
304                 (
305                 exeinto "${path}"
306                 newexe "${tmpwrapper}" "${wrapper}"
307                 ) || die
308         else
309                 newbin "${tmpwrapper}" "${wrapper}" || die
310         fi
311 }
312
313 # @FUNCTION: path_exists
314 # @USAGE: [-a|-o] <paths>
315 # @DESCRIPTION:
316 # Check if the specified paths exist.  Works for all types of paths
317 # (files/dirs/etc...).  The -a and -o flags control the requirements
318 # of the paths.  They correspond to "and" and "or" logic.  So the -a
319 # flag means all the paths must exist while the -o flag means at least
320 # one of the paths must exist.  The default behavior is "and".  If no
321 # paths are specified, then the return value is "false".
322 path_exists() {
323         local opt=$1
324         [[ ${opt} == -[ao] ]] && shift || opt="-a"
325
326         # no paths -> return false
327         # same behavior as: [[ -e "" ]]
328         [[ $# -eq 0 ]] && return 1
329
330         local p r=0
331         for p in "$@" ; do
332                 [[ -e ${p} ]]
333                 : $(( r += $? ))
334         done
335
336         case ${opt} in
337                 -a) return $(( r != 0 )) ;;
338                 -o) return $(( r == $# )) ;;
339         esac
340 }
341
342 # @FUNCTION: use_if_iuse
343 # @USAGE: <flag>
344 # @DESCRIPTION:
345 # Return true if the given flag is in USE and IUSE.
346 #
347 # Note that this function should not be used in the global scope.
348 use_if_iuse() {
349         in_iuse $1 || return 1
350         use $1
351 }
352
353 # @FUNCTION: optfeature
354 # @USAGE: <short description> <package atom to match> [other atoms]
355 # @DESCRIPTION:
356 # Print out a message suggesting an optional package (or packages)
357 # not currently installed which provides the described functionality.
358 #
359 # The following snippet would suggest app-misc/foo for optional foo support,
360 # app-misc/bar or app-misc/baz[bar] for optional bar support
361 # and either both app-misc/a and app-misc/b or app-misc/c for alphabet support.
362 # @CODE
363 #       optfeature "foo support" app-misc/foo
364 #       optfeature "bar support" app-misc/bar app-misc/baz[bar]
365 #       optfeature "alphabet support" "app-misc/a app-misc/b" app-misc/c
366 # @CODE
367 optfeature() {
368         debug-print-function ${FUNCNAME} "$@"
369         local i j msg
370         local desc=$1
371         local flag=0
372         shift
373         for i; do
374                 for j in ${i}; do
375                         if has_version "${j}"; then
376                                 flag=1
377                         else
378                                 flag=0
379                                 break
380                         fi
381                 done
382                 if [[ ${flag} -eq 1 ]]; then
383                         break
384                 fi
385         done
386         if [[ ${flag} -eq 0 ]]; then
387                 for i; do
388                         msg=" "
389                         for j in ${i}; do
390                                 msg+=" ${j} and"
391                         done
392                         msg="${msg:0: -4} for ${desc}"
393                         elog "${msg}"
394                 done
395         fi
396 }
397
398 case ${EAPI:-0} in
399 0|1|2)
400
401 # @FUNCTION: epause
402 # @USAGE: [seconds]
403 # @DESCRIPTION:
404 # Sleep for the specified number of seconds (default of 5 seconds).  Useful when
405 # printing a message the user should probably be reading and often used in
406 # conjunction with the ebeep function.  If the EPAUSE_IGNORE env var is set,
407 # don't wait at all. Defined in EAPIs 0 1 and 2.
408 epause() {
409         [[ -z ${EPAUSE_IGNORE} ]] && sleep ${1:-5}
410 }
411
412 # @FUNCTION: ebeep
413 # @USAGE: [number of beeps]
414 # @DESCRIPTION:
415 # Issue the specified number of beeps (default of 5 beeps).  Useful when
416 # printing a message the user should probably be reading and often used in
417 # conjunction with the epause function.  If the EBEEP_IGNORE env var is set,
418 # don't beep at all. Defined in EAPIs 0 1 and 2.
419 ebeep() {
420         local n
421         if [[ -z ${EBEEP_IGNORE} ]] ; then
422                 for ((n=1 ; n <= ${1:-5} ; n++)) ; do
423                         echo -ne "\a"
424                         sleep 0.1 &>/dev/null ; sleep 0,1 &>/dev/null
425                         echo -ne "\a"
426                         sleep 1
427                 done
428         fi
429 }
430
431 ;;
432 *)
433
434 ebeep() {
435         ewarn "QA Notice: ebeep is not defined in EAPI=${EAPI}, please file a bug at https://bugs.gentoo.org"
436 }
437
438 epause() {
439         ewarn "QA Notice: epause is not defined in EAPI=${EAPI}, please file a bug at https://bugs.gentoo.org"
440 }
441
442 ;;
443 esac
444
445 case ${EAPI:-0} in
446 0|1|2|3|4)
447
448 # @FUNCTION: usex
449 # @USAGE: <USE flag> [true output] [false output] [true suffix] [false suffix]
450 # @DESCRIPTION:
451 # Proxy to declare usex for package managers or EAPIs that do not provide it
452 # and use the package manager implementation when available (i.e. EAPI >= 5).
453 # If USE flag is set, echo [true output][true suffix] (defaults to "yes"),
454 # otherwise echo [false output][false suffix] (defaults to "no").
455 usex() { use "$1" && echo "${2-yes}$4" || echo "${3-no}$5" ; } #382963
456
457 ;;
458 esac
459
460 case ${EAPI:-0} in
461 0|1|2|3|4|5)
462
463 # @FUNCTION: einstalldocs
464 # @DESCRIPTION:
465 # Install documentation using DOCS and HTML_DOCS.
466 #
467 # If DOCS is declared and non-empty, all files listed in it are
468 # installed. The files must exist, otherwise the function will fail.
469 # In EAPI 4 and subsequent EAPIs DOCS may specify directories as well,
470 # in other EAPIs using directories is unsupported.
471 #
472 # If DOCS is not declared, the files matching patterns given
473 # in the default EAPI implementation of src_install will be installed.
474 # If this is undesired, DOCS can be set to empty value to prevent any
475 # documentation from being installed.
476 #
477 # If HTML_DOCS is declared and non-empty, all files and/or directories
478 # listed in it are installed as HTML docs (using dohtml).
479 #
480 # Both DOCS and HTML_DOCS can either be an array or a whitespace-
481 # separated list. Whenever directories are allowed, '<directory>/.' may
482 # be specified in order to install all files within the directory
483 # without creating a sub-directory in docdir.
484 #
485 # Passing additional options to dodoc and dohtml is not supported.
486 # If you needed such a thing, you need to call those helpers explicitly.
487 einstalldocs() {
488         debug-print-function ${FUNCNAME} "${@}"
489
490         local dodoc_opts=-r
491         has ${EAPI} 0 1 2 3 && dodoc_opts=
492
493         if ! declare -p DOCS &>/dev/null ; then
494                 local d
495                 for d in README* ChangeLog AUTHORS NEWS TODO CHANGES \
496                                 THANKS BUGS FAQ CREDITS CHANGELOG ; do
497                         if [[ -s ${d} ]] ; then
498                                 dodoc "${d}" || die
499                         fi
500                 done
501         elif [[ $(declare -p DOCS) == "declare -a"* ]] ; then
502                 if [[ ${DOCS[@]} ]] ; then
503                         dodoc ${dodoc_opts} "${DOCS[@]}" || die
504                 fi
505         else
506                 if [[ ${DOCS} ]] ; then
507                         dodoc ${dodoc_opts} ${DOCS} || die
508                 fi
509         fi
510
511         if [[ $(declare -p HTML_DOCS 2>/dev/null) == "declare -a"* ]] ; then
512                 if [[ ${HTML_DOCS[@]} ]] ; then
513                         dohtml -r "${HTML_DOCS[@]}" || die
514                 fi
515         else
516                 if [[ ${HTML_DOCS} ]] ; then
517                         dohtml -r ${HTML_DOCS} || die
518                 fi
519         fi
520
521         return 0
522 }
523
524 # @FUNCTION: in_iuse
525 # @USAGE: <flag>
526 # @DESCRIPTION:
527 # Determines whether the given flag is in IUSE. Strips IUSE default prefixes
528 # as necessary.
529 #
530 # Note that this function should not be used in the global scope.
531 in_iuse() {
532         debug-print-function ${FUNCNAME} "${@}"
533         [[ ${#} -eq 1 ]] || die "Invalid args to ${FUNCNAME}()"
534
535         local flag=${1}
536         local liuse=( ${IUSE} )
537
538         has "${flag}" "${liuse[@]#[+-]}"
539 }
540
541 ;;
542 esac
543
544 fi