From 765c430748074357b145e7f18b3ffee0abe06672 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@Germany>
Date: Wed, 19 Apr 2017 19:13:23 +0200
Subject: [PATCH] STYLE: only count processors if required

- POSIX specifies '-c' for counting the number of matching lines,
  so we can just use that instead of 'wc' in wmake
---
 wmake/wmake             | 36 +++++++++++++++++-------------------
 wmake/wmakeLnIncludeAll |  5 +++++
 2 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/wmake/wmake b/wmake/wmake
index bcf6c3fa3ab..100e4d9d692 100755
--- a/wmake/wmake
+++ b/wmake/wmake
@@ -100,25 +100,18 @@ USAGE
     exit 1
 }
 
-
 # Default make is the "make" in the path
 make="make"
 
 
-#------------------------------------------------------------------------------
-# Set WM_NCOMPPROCS to number of cores on local machine
 #------------------------------------------------------------------------------
 
-useAllCores()
+# Set nCores to the number of cores on the local machine
+nCores=0
+allCores()
 {
-    if [ -r /proc/cpuinfo ]
-    then
-        WM_NCOMPPROCS=$(egrep "^processor" /proc/cpuinfo | wc -l)
-    else
-        WM_NCOMPPROCS=1
-    fi
-
-    export WM_NCOMPPROCS
+    nCores=$(egrep -c "^processor" /proc/cpuinfo 2>/dev/null) || nCores=1
+    : ${nCores:=1}
 }
 
 
@@ -145,11 +138,17 @@ do
         -q | -queue | queue)
             all=queue
             ;;
-        # Parallel compilation on all cores of local machine
+        # Parallel compilation on all cores (or specified number of cores)
         -j)
-            useAllCores
+            nCores=0
             test $# -ge 2 && expr $2 + 1 > /dev/null 2>&1 \
-                && shift && export WM_NCOMPPROCS=$1
+                && shift && nCores=$1
+
+            if [ "$nCores" = 0 ]
+            then
+                allCores
+            fi
+            export WM_NCOMPPROCS=$nCores
             echo "Compiling enabled on $WM_NCOMPPROCS cores"
             ;;
         # Parallel compilation on specified number of cores
@@ -214,8 +213,7 @@ checkEnv
 # Set WM_NCOMPPROCS automatically when both WM_HOSTS and WM_SCHEDULER are set
 if [ -z "$WM_NCOMPPROCS" -a -n "$WM_HOSTS" -a -n "$WM_SCHEDULER" ]
 then
-    WM_NCOMPPROCS=$(wmakeScheduler -count)
-    [ $? -eq 0 ] || unset WM_NCOMPPROCS
+    WM_NCOMPPROCS=$(wmakeScheduler -count) || unset WM_NCOMPPROCS
 fi
 
 if [ -n "$WM_NCOMPPROCS" ]
@@ -348,8 +346,8 @@ then
         export WM_COLLECT_DIR=$WM_PROJECT_DIR/build/${WM_OPTIONS}/${PWD////_}
         export WM_SCHEDULER=wmakeCollect
         trap '$WM_SCHEDULER -kill' TERM INT
-        $WM_SCHEDULER -clean                                                   \
-     && wmake -all objects                                                     \
+        $WM_SCHEDULER -clean \
+     && wmake -all objects   \
      && $WM_SCHEDULER
     ) && wmake -all
     exit $?
diff --git a/wmake/wmakeLnIncludeAll b/wmake/wmakeLnIncludeAll
index 5748ec20c6d..f110da9581c 100755
--- a/wmake/wmakeLnIncludeAll
+++ b/wmake/wmakeLnIncludeAll
@@ -78,6 +78,11 @@ do
             nCores=${WM_NCOMPPROCS:-0}
             test $# -ge 2 && expr $2 + 1 > /dev/null 2>&1 \
                 && shift && nCores=$1
+
+            if [ "$nCores" = 0 ]
+            then
+                nCores=1
+            fi
             ;;
         # Parallel compilation on specified number of cores
         -j[1-9]*)
-- 
GitLab