commit: 83e052c75fcfff084d1cd98ddb914c63926ece64
Author: Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Tue Dec 13 09:31:12 2016 +0000
Commit: Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Sun Dec 18 13:46:46 2016 +0000
URL: https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=83e052c7
multiprocessing.eclass: Support passing custom inf values for getters
Support passing custom values for 'infinity' in makeopts_jobs()
and makeopts_loadavg(). This can be used e.g. when a build system does
not support --loadavg, and therefore '--jobs 999' would most likely
be a really bad idea. Combined with get_nproc(), this can be used to
provide a sane replacement instead.
eclass/multiprocessing.eclass | 17 ++++++++++-------
eclass/tests/multiprocessing_makeopts_jobs.sh | 5 ++++-
eclass/tests/multiprocessing_makeopts_loadavg.sh | 5 ++++-
3 files changed, 18 insertions(+), 9 deletions(-)
diff --git a/eclass/multiprocessing.eclass b/eclass/multiprocessing.eclass
index 3c5dfff..70ca475 100644
--- a/eclass/multiprocessing.eclass
+++ b/eclass/multiprocessing.eclass
@@ -86,26 +86,27 @@ get_nproc() {
}
# @FUNCTION: makeopts_jobs
-# @USAGE: [${MAKEOPTS}]
+# @USAGE: [${MAKEOPTS}] [${inf:-999}]
# @DESCRIPTION:
# Searches the arguments (defaults to ${MAKEOPTS}) and extracts the jobs number
# specified therein. Useful for running non-make tools in parallel too.
# i.e. if the user has MAKEOPTS=-j9, this will echo "9" -- we can't return the
# number as bash normalizes it to [0, 255]. If the flags haven't specified a
# -j flag, then "1" is shown as that is the default `make` uses. Since there's
-# no way to represent infinity, we return 999 if the user has -j without a
number.
+# no way to represent infinity, we return ${inf} (defaults to 999) if the user
+# has -j without a number.
makeopts_jobs() {
[[ $# -eq 0 ]] && set -- ${MAKEOPTS}
# This assumes the first .* will be more greedy than the second .*
# since POSIX doesn't specify a non-greedy match (i.e. ".*?").
local jobs=$(echo " $* " | sed -r -n \
-e
's:.*[[:space:]](-[a-z]*j|--jobs[=[:space:]])[[:space:]]*([0-9]+).*:\2:p' \
- -e 's:.*[[:space:]](-[a-z]*j|--jobs)[[:space:]].*:999:p')
+ -e "s:.*[[:space:]](-[a-z]*j|--jobs)[[:space:]].*:${2:-999}:p")
echo ${jobs:-1}
}
# @FUNCTION: makeopts_loadavg
-# @USAGE: [${MAKEOPTS}]
+# @USAGE: [${MAKEOPTS}] [${inf:-999}]
# @DESCRIPTION:
# Searches the arguments (defaults to ${MAKEOPTS}) and extracts the value set
# for load-average. For make and ninja based builds this will mean new jobs are
@@ -113,15 +114,17 @@ makeopts_jobs() {
# get excessive due to I/O and not just due to CPU load.
# Be aware that the returned number might be a floating-point number. Test
# whether your software supports that.
+# If no limit is specified or --load-average is used without a number, ${inf}
+# (defaults to 999) is returned.
makeopts_loadavg() {
[[ $# -eq 0 ]] && set -- ${MAKEOPTS}
# This assumes the first .* will be more greedy than the second .*
# since POSIX doesn't specify a non-greedy match (i.e. ".*?").
local lavg=$(echo " $* " | sed -r -n \
-e
's:.*[[:space:]](-[a-z]*l|--(load-average|max-load)[=[:space:]])[[:space:]]*([0-9]+|[0-9]+\.[0-9]+).*:\3:p'
\
- -e
's:.*[[:space:]](-[a-z]*l|--(load-average|max-load))[[:space:]].*:999:p')
- # Default to 999 since the default is to not use a load limit.
- echo ${lavg:-999}
+ -e
"s:.*[[:space:]](-[a-z]*l|--(load-average|max-load))[[:space:]].*:${2:-999}:p")
+ # Default to ${inf} since the default is to not use a load limit.
+ echo ${lavg:-${2:-999}}
}
# @FUNCTION: multijob_init
diff --git a/eclass/tests/multiprocessing_makeopts_jobs.sh
b/eclass/tests/multiprocessing_makeopts_jobs.sh
index a1e43c8..ef47727 100755
--- a/eclass/tests/multiprocessing_makeopts_jobs.sh
+++ b/eclass/tests/multiprocessing_makeopts_jobs.sh
@@ -9,7 +9,7 @@ inherit multiprocessing
test-makeopts_jobs() {
local exp=$1; shift
- tbegin "makeopts_jobs($*) == ${exp}"
+ tbegin "makeopts_jobs($1${2+; inf=${2}}) == ${exp}"
local act=$(makeopts_jobs "$@")
[[ ${act} == "${exp}" ]]
tend $? "Got back: ${act}"
@@ -39,4 +39,7 @@ for (( i = 0; i < ${#tests[@]}; i += 2 )) ; do
test-makeopts_jobs "${tests[i]}" "${tests[i+1]}"
done
+# test custom inf value
+test-makeopts_jobs 645 "-j" 645
+
texit
diff --git a/eclass/tests/multiprocessing_makeopts_loadavg.sh
b/eclass/tests/multiprocessing_makeopts_loadavg.sh
index 276b7e7..6b976be 100755
--- a/eclass/tests/multiprocessing_makeopts_loadavg.sh
+++ b/eclass/tests/multiprocessing_makeopts_loadavg.sh
@@ -9,7 +9,7 @@ inherit multiprocessing
test-makeopts_loadavg() {
local exp=$1; shift
- tbegin "makeopts_loadavg($*) == ${exp}"
+ tbegin "makeopts_loadavg($1${2+; inf=${2}}) == ${exp}"
local act=$(makeopts_loadavg "$@")
[[ ${act} == "${exp}" ]]
tend $? "Got back: ${act}"
@@ -36,4 +36,7 @@ for (( i = 0; i < ${#tests[@]}; i += 2 )) ; do
test-makeopts_loadavg "${tests[i]}" "${tests[i+1]}"
done
+# test custom inf value
+test-makeopts_loadavg 645 "-l" 645
+
texit