commit:     b8584ea0ae3b1604e486b31dc569f80c14a2fdc0
Author:     Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Sat Nov  8 19:36:00 2025 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Thu Nov 13 06:16:27 2025 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=b8584ea0

ninja-utils.eclass: Do not pass -j if jobserver can be used

>=dev-build/ninja-1.13 features GNU make jobserver support, but uses
it only if -j is not specified.  Detect if we can use it, and skip -j
from the default NINJAOPTS if that is the case.

Bug: https://bugs.gentoo.org/692576
Signed-off-by: Michał Górny <mgorny <AT> gentoo.org>
Part-of: https://github.com/gentoo/gentoo/pull/44539
Signed-off-by: Sam James <sam <AT> gentoo.org>

 eclass/ninja-utils.eclass | 40 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)

diff --git a/eclass/ninja-utils.eclass b/eclass/ninja-utils.eclass
index 08743bcea1a4..0ac23b7d4799 100644
--- a/eclass/ninja-utils.eclass
+++ b/eclass/ninja-utils.eclass
@@ -72,12 +72,50 @@ case ${NINJA} in
                ;;
 esac
 
+# @FUNCTION: _ninja_uses_jobserver
+# @DESCRIPTION:
+# Return true if current ${NINJA} has jobserver support and we have one
+# running (via MAKEFLAGS).
+_ninja_uses_jobserver() {
+       # ninja supports jobserver via FIFO only
+       [[ ${MAKEFLAGS} == *--jobserver-auth=fifo:* ]] || return 1
+
+       case ${NINJA} in
+               # if using "ninja", make sure its a symlink to real ninja
+               # samu: https://github.com/michaelforney/samurai/issues/71
+               ninja)
+                       if ! has_version -b 
"app-alternatives/ninja[reference]"; then
+                               einfo "ninja != ninja-reference, no jobserver 
support"
+                               return 1
+                       fi
+                       ;&
+               # plus, it must be at least 1.13.0
+               ninja-reference)
+                       if ! has_version -b ">=dev-build/ninja-1.13"; then
+                               einfo "ninja >= 1.13 required for jobserver 
support"
+                               return 1
+                       fi
+                       ;;
+               *)
+                       einfo "NINJA=${NINJA}, no jobserver support"
+                       return 1
+                       ;;
+       esac
+
+       einfo "ninja will use the jobserver"
+       return 0
+}
+
 # @FUNCTION: get_NINJAOPTS
 # @DESCRIPTION:
 # Get the value of NINJAOPTS, inferring them from MAKEOPTS if unset.
 get_NINJAOPTS() {
        if [[ -z ${NINJAOPTS+set} ]]; then
-               NINJAOPTS="-j$(get_makeopts_jobs 999) -l$(get_makeopts_loadavg 
0)"
+               NINJAOPTS="-l$(get_makeopts_loadavg 0)"
+               if ! _ninja_uses_jobserver; then
+                       # ninja only uses jobserver if -j is not passed
+                       NINJAOPTS+=" -j$(get_makeopts_jobs 999)"
+               fi
        fi
        echo "${NINJAOPTS}"
 }

Reply via email to