1 |
Support passing custom values for 'infinity' in makeopts_jobs() |
2 |
and makeopts_loadavg(). This can be used e.g. when a build system does |
3 |
not support --loadavg, and therefore '--jobs 999' would most likely |
4 |
be a really bad idea. Combined with get_nproc(), this can be used to |
5 |
provide a sane replacement instead. |
6 |
|
7 |
// v2: tests included |
8 |
--- |
9 |
eclass/multiprocessing.eclass | 17 ++++++++++------- |
10 |
eclass/tests/multiprocessing_makeopts_jobs.sh | 5 ++++- |
11 |
eclass/tests/multiprocessing_makeopts_loadavg.sh | 5 ++++- |
12 |
3 files changed, 18 insertions(+), 9 deletions(-) |
13 |
|
14 |
diff --git a/eclass/multiprocessing.eclass b/eclass/multiprocessing.eclass |
15 |
index 3c5dfff2d11c..70ca475a8c72 100644 |
16 |
--- a/eclass/multiprocessing.eclass |
17 |
+++ b/eclass/multiprocessing.eclass |
18 |
@@ -86,26 +86,27 @@ get_nproc() { |
19 |
} |
20 |
|
21 |
# @FUNCTION: makeopts_jobs |
22 |
-# @USAGE: [${MAKEOPTS}] |
23 |
+# @USAGE: [${MAKEOPTS}] [${inf:-999}] |
24 |
# @DESCRIPTION: |
25 |
# Searches the arguments (defaults to ${MAKEOPTS}) and extracts the jobs number |
26 |
# specified therein. Useful for running non-make tools in parallel too. |
27 |
# i.e. if the user has MAKEOPTS=-j9, this will echo "9" -- we can't return the |
28 |
# number as bash normalizes it to [0, 255]. If the flags haven't specified a |
29 |
# -j flag, then "1" is shown as that is the default `make` uses. Since there's |
30 |
-# no way to represent infinity, we return 999 if the user has -j without a number. |
31 |
+# no way to represent infinity, we return ${inf} (defaults to 999) if the user |
32 |
+# has -j without a number. |
33 |
makeopts_jobs() { |
34 |
[[ $# -eq 0 ]] && set -- ${MAKEOPTS} |
35 |
# This assumes the first .* will be more greedy than the second .* |
36 |
# since POSIX doesn't specify a non-greedy match (i.e. ".*?"). |
37 |
local jobs=$(echo " $* " | sed -r -n \ |
38 |
-e 's:.*[[:space:]](-[a-z]*j|--jobs[=[:space:]])[[:space:]]*([0-9]+).*:\2:p' \ |
39 |
- -e 's:.*[[:space:]](-[a-z]*j|--jobs)[[:space:]].*:999:p') |
40 |
+ -e "s:.*[[:space:]](-[a-z]*j|--jobs)[[:space:]].*:${2:-999}:p") |
41 |
echo ${jobs:-1} |
42 |
} |
43 |
|
44 |
# @FUNCTION: makeopts_loadavg |
45 |
-# @USAGE: [${MAKEOPTS}] |
46 |
+# @USAGE: [${MAKEOPTS}] [${inf:-999}] |
47 |
# @DESCRIPTION: |
48 |
# Searches the arguments (defaults to ${MAKEOPTS}) and extracts the value set |
49 |
# for load-average. For make and ninja based builds this will mean new jobs are |
50 |
@@ -113,15 +114,17 @@ makeopts_jobs() { |
51 |
# get excessive due to I/O and not just due to CPU load. |
52 |
# Be aware that the returned number might be a floating-point number. Test |
53 |
# whether your software supports that. |
54 |
+# If no limit is specified or --load-average is used without a number, ${inf} |
55 |
+# (defaults to 999) is returned. |
56 |
makeopts_loadavg() { |
57 |
[[ $# -eq 0 ]] && set -- ${MAKEOPTS} |
58 |
# This assumes the first .* will be more greedy than the second .* |
59 |
# since POSIX doesn't specify a non-greedy match (i.e. ".*?"). |
60 |
local lavg=$(echo " $* " | sed -r -n \ |
61 |
-e 's:.*[[:space:]](-[a-z]*l|--(load-average|max-load)[=[:space:]])[[:space:]]*([0-9]+|[0-9]+\.[0-9]+).*:\3:p' \ |
62 |
- -e 's:.*[[:space:]](-[a-z]*l|--(load-average|max-load))[[:space:]].*:999:p') |
63 |
- # Default to 999 since the default is to not use a load limit. |
64 |
- echo ${lavg:-999} |
65 |
+ -e "s:.*[[:space:]](-[a-z]*l|--(load-average|max-load))[[:space:]].*:${2:-999}:p") |
66 |
+ # Default to ${inf} since the default is to not use a load limit. |
67 |
+ echo ${lavg:-${2:-999}} |
68 |
} |
69 |
|
70 |
# @FUNCTION: multijob_init |
71 |
diff --git a/eclass/tests/multiprocessing_makeopts_jobs.sh b/eclass/tests/multiprocessing_makeopts_jobs.sh |
72 |
index a1e43c8b91d7..ef477277ab3b 100755 |
73 |
--- a/eclass/tests/multiprocessing_makeopts_jobs.sh |
74 |
+++ b/eclass/tests/multiprocessing_makeopts_jobs.sh |
75 |
@@ -9,7 +9,7 @@ inherit multiprocessing |
76 |
|
77 |
test-makeopts_jobs() { |
78 |
local exp=$1; shift |
79 |
- tbegin "makeopts_jobs($*) == ${exp}" |
80 |
+ tbegin "makeopts_jobs($1${2+; inf=${2}}) == ${exp}" |
81 |
local act=$(makeopts_jobs "$@") |
82 |
[[ ${act} == "${exp}" ]] |
83 |
tend $? "Got back: ${act}" |
84 |
@@ -39,4 +39,7 @@ for (( i = 0; i < ${#tests[@]}; i += 2 )) ; do |
85 |
test-makeopts_jobs "${tests[i]}" "${tests[i+1]}" |
86 |
done |
87 |
|
88 |
+# test custom inf value |
89 |
+test-makeopts_jobs 645 "-j" 645 |
90 |
+ |
91 |
texit |
92 |
diff --git a/eclass/tests/multiprocessing_makeopts_loadavg.sh b/eclass/tests/multiprocessing_makeopts_loadavg.sh |
93 |
index 276b7e70d393..6b976beb1aef 100755 |
94 |
--- a/eclass/tests/multiprocessing_makeopts_loadavg.sh |
95 |
+++ b/eclass/tests/multiprocessing_makeopts_loadavg.sh |
96 |
@@ -9,7 +9,7 @@ inherit multiprocessing |
97 |
|
98 |
test-makeopts_loadavg() { |
99 |
local exp=$1; shift |
100 |
- tbegin "makeopts_loadavg($*) == ${exp}" |
101 |
+ tbegin "makeopts_loadavg($1${2+; inf=${2}}) == ${exp}" |
102 |
local act=$(makeopts_loadavg "$@") |
103 |
[[ ${act} == "${exp}" ]] |
104 |
tend $? "Got back: ${act}" |
105 |
@@ -36,4 +36,7 @@ for (( i = 0; i < ${#tests[@]}; i += 2 )) ; do |
106 |
test-makeopts_loadavg "${tests[i]}" "${tests[i+1]}" |
107 |
done |
108 |
|
109 |
+# test custom inf value |
110 |
+test-makeopts_loadavg 645 "-l" 645 |
111 |
+ |
112 |
texit |
113 |
-- |
114 |
2.11.0 |