From 404fef12fda5fae002ba4c654259c0e3a669356e Mon Sep 17 00:00:00 2001
From: "lintao@ihep.ac.cn" <lintao@ihep.ac.cn>
Date: Wed, 18 Sep 2024 03:20:45 +0000
Subject: [PATCH] Allow users to limit number of cores using NJOBS envvar.

---
 build.sh | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/build.sh b/build.sh
index 85934189..fc9f7fdc 100755
--- a/build.sh
+++ b/build.sh
@@ -83,7 +83,23 @@ function run-cmake() {
 }
 
 function run-make() {
-    local njobs=-j$(nproc)
+    # User can limit the number of cores by setting NJOBS envvar.
+    # For example: NJOBS=8 ./build.sh
+    local njobs=${NJOBS}
+    if [ -z "$njobs" ]; then
+        njobs=$(nproc)
+    fi
+
+    # following is IHEP specific hack due to the limitation of nthreads 150.
+    local nthreads=$(ulimit -u)
+    local factor=16
+    nthreads=$((nthreads/factor)) # assume it is safe to divide by scale factor.
+    
+    # min(njobs, nthreads)
+    njobs=$((njobs<nthreads ?  njobs: nthreads))
+
+    njobs=-j${njobs}
+    echo "njobs: $njobs"
     cmake --build . $njobs
 }
 
-- 
GitLab