#1482·ninja

Environment variable for limiting the number of JOBS [Again]

Author: kobalicekCreated Oct 18, 2018Updated May 21, 2026

Is it possible to finally add the support for an environment variable that would limit the maximum number of jobs ninja can spawn?

I would suggest having an optional environment variable NINJA_NUM_JOBS, which would be used as an initial value when provided. This variable will be parsed before command line arguments that could of course override it.

I'm not asking for "additional flags" support [https://github.com/ninja-build/ninja/pull/1399], just a way to override the initial guess of the number of jobs as I understand the concerns behind NINJA_FLAGS and I understand that it might never get accepted.

I'm proposing a small change like this:

cpp
int GuessParallelism() {
  int n = GetProcessorCount();
  switch (n) {
    case 0: 
    case 1: n = 2; break;
    case 2: n = 3; break;
    default: n += 2; break;
  }

  const char* s = getenv("NINJA_NUM_JOBS"); // or any better name.
  if (!s)
    return n;

  char* end;
  long nUserDefined = strtol(s, &end, 10);

  // If the environment value is invalid or is greater than our initial guess, drop it.
  if (*end != 0 || nUserDefined <= 0 || nUserDefined > n)
    return n;

  // Otherwise use the sane value provided by the user.
  return int(nUserDefined);
}