llvm.eclass: Remove remnants of slot :0 support
[gentoo.git] / eclass / multiprocessing.eclass
1 # Copyright 1999-2017 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: multiprocessing.eclass
5 # @MAINTAINER:
6 # base-system@gentoo.org
7 # @AUTHOR:
8 # Brian Harring <ferringb@gentoo.org>
9 # Mike Frysinger <vapier@gentoo.org>
10 # @BLURB: multiprocessing helper functions
11 # @DESCRIPTION:
12 # The multiprocessing eclass contains a suite of utility functions
13 # that could be helpful to controlling parallel multiple job execution.
14 # The most common use is processing MAKEOPTS in order to obtain job
15 # count.
16 #
17 # @EXAMPLE:
18 #
19 # @CODE
20 # src_compile() {
21 #   # custom build system that does not support most of MAKEOPTS
22 #   ./mybs -j$(makeopts_jobs)
23 # }
24 # @CODE
25
26 if [[ -z ${_MULTIPROCESSING_ECLASS} ]]; then
27 _MULTIPROCESSING_ECLASS=1
28
29 # @FUNCTION: get_nproc
30 # @USAGE: [${fallback:-1}]
31 # @DESCRIPTION:
32 # Attempt to figure out the number of processing units available.
33 # If the value can not be determined, prints the provided fallback
34 # instead. If no fallback is provided, defaults to 1.
35 get_nproc() {
36         local nproc
37
38         # GNU
39         if type -P nproc &>/dev/null; then
40                 nproc=$(nproc)
41         fi
42
43         # BSD
44         if [[ -z ${nproc} ]] && type -P sysctl &>/dev/null; then
45                 nproc=$(sysctl -n hw.ncpu 2>/dev/null)
46         fi
47
48         # fallback to python2.6+
49         # note: this may fail (raise NotImplementedError)
50         if [[ -z ${nproc} ]] && type -P python &>/dev/null; then
51                 nproc=$(python -c 'import multiprocessing; print(multiprocessing.cpu_count());' 2>/dev/null)
52         fi
53
54         if [[ -n ${nproc} ]]; then
55                 echo "${nproc}"
56         else
57                 echo "${1:-1}"
58         fi
59 }
60
61 # @FUNCTION: makeopts_jobs
62 # @USAGE: [${MAKEOPTS}] [${inf:-999}]
63 # @DESCRIPTION:
64 # Searches the arguments (defaults to ${MAKEOPTS}) and extracts the jobs number
65 # specified therein.  Useful for running non-make tools in parallel too.
66 # i.e. if the user has MAKEOPTS=-j9, this will echo "9" -- we can't return the
67 # number as bash normalizes it to [0, 255].  If the flags haven't specified a
68 # -j flag, then "1" is shown as that is the default `make` uses.  Since there's
69 # no way to represent infinity, we return ${inf} (defaults to 999) if the user
70 # has -j without a number.
71 makeopts_jobs() {
72         [[ $# -eq 0 ]] && set -- "${MAKEOPTS}"
73         # This assumes the first .* will be more greedy than the second .*
74         # since POSIX doesn't specify a non-greedy match (i.e. ".*?").
75         local jobs=$(echo " $* " | sed -r -n \
76                 -e 's:.*[[:space:]](-[a-z]*j|--jobs[=[:space:]])[[:space:]]*([0-9]+).*:\2:p' \
77                 -e "s:.*[[:space:]](-[a-z]*j|--jobs)[[:space:]].*:${2:-999}:p")
78         echo ${jobs:-1}
79 }
80
81 # @FUNCTION: makeopts_loadavg
82 # @USAGE: [${MAKEOPTS}] [${inf:-999}]
83 # @DESCRIPTION:
84 # Searches the arguments (defaults to ${MAKEOPTS}) and extracts the value set
85 # for load-average. For make and ninja based builds this will mean new jobs are
86 # not only limited by the jobs-value, but also by the current load - which might
87 # get excessive due to I/O and not just due to CPU load.
88 # Be aware that the returned number might be a floating-point number. Test
89 # whether your software supports that.
90 # If no limit is specified or --load-average is used without a number, ${inf}
91 # (defaults to 999) is returned.
92 makeopts_loadavg() {
93         [[ $# -eq 0 ]] && set -- "${MAKEOPTS}"
94         # This assumes the first .* will be more greedy than the second .*
95         # since POSIX doesn't specify a non-greedy match (i.e. ".*?").
96         local lavg=$(echo " $* " | sed -r -n \
97                 -e 's:.*[[:space:]](-[a-z]*l|--(load-average|max-load)[=[:space:]])[[:space:]]*([0-9]+(\.[0-9]+)?)[[:space:]].*:\3:p' \
98                 -e "s:.*[[:space:]](-[a-z]*l|--(load-average|max-load))[[:space:]].*:${2:-999}:p")
99         # Default to ${inf} since the default is to not use a load limit.
100         echo ${lavg:-${2:-999}}
101 }
102
103 fi