← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands/number_of_cpus_as_option into lp:widelands

 

GunChleoc has proposed merging lp:~widelands-dev/widelands/number_of_cpus_as_option into lp:widelands.

Commit message:
Add option to compile script to let users pick how many processor cores to compile/link with.

Requested reviews:
  Widelands Developers (widelands-dev)

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands/number_of_cpus_as_option/+merge/358492

Needs testing with Mac - the Darwin detaction option was producing an error message on Linux, so I changed the syntax.
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/number_of_cpus_as_option into lp:widelands.
=== modified file 'compile.sh'
--- compile.sh	2018-11-08 08:47:48 +0000
+++ compile.sh	2018-11-08 10:18:48 +0000
@@ -44,6 +44,10 @@
     echo " "
     echo "Compiler options:"
     echo " "
+    echo "-j <number> or --cores <number>"
+    echo "                      Set the number of processor cores to use for"
+    echo "                      compiling and linking."
+    echo " "
     echo "-r or --release       Create a release build. If this is not set,"
     echo "                      a debug build will be created."
     echo " "
@@ -77,33 +81,77 @@
 BUILD_TYPE="Debug"
 USE_ASAN="ON"
 COMPILER="default"
-while [ "$1" != "" ]; do
-  if [ "$1" = "--no-website" -o "$1" = "-w" ]; then
-    BUILD_WEBSITE="OFF"
-  elif [ "$1" = "--release" -o "$1" = "-r" ]; then
-    BUILD_TYPE="Release"
-    USE_ASAN="OFF"
-  elif [ "$1" = "--no-translations" -o "$1" = "-t" ]; then
-    BUILD_TRANSLATIONS="OFF"
-  elif [ "$1" = "--no-asan" -o "$1" = "-a" ]; then
-    USE_ASAN="OFF"
-  elif [ "$1" = "--gcc" ]; then
-    if [ -f /usr/bin/gcc -a /usr/bin/g++ ]; then
-      export CC=/usr/bin/gcc
-      export CXX=/usr/bin/g++
-    fi
-  elif [ "$1" = "--clang" ]; then
-    if [ -f /usr/bin/clang -a /usr/bin/clang++ ]; then
-      export CC=/usr/bin/clang
-      export CXX=/usr/bin/clang++
-    fi
-  elif [ "$1" = "--help" -o "$1" = "-h" ]; then
-    print_help
-    exit 0
-  fi
-  shift
+
+if [ "$(uname)" = "Darwin" ]; then
+  CORES="$(expr $(sysctl -n hw.ncpu) - 1)"
+else
+  CORES="$(nproc --ignore=1)"
+fi
+
+for opt in "$@"
+do
+  case $opt in
+    -a|--no-asan)
+      USE_ASAN="OFF"
+    shift
+    ;;
+    -h|--help)
+      print_help
+      exit 0
+    shift
+    ;;
+    -j|--cores)
+      MAXCORES=$((CORES + 1))
+      if [ "$2" ]; then
+        if [ "$MAXCORES" -ge "$2" ]; then
+          CORES="$2"
+        else
+          echo "Maximum number of supported cores is $MAXCORES."
+          CORES="$MAXCORES"
+        fi
+      else
+        echo "Call -j/--cores with a number, e.g. '-j $MAXCORES'"
+        exit 1
+      fi
+      shift # past argument
+      shift # past value
+    ;;
+    -r|--release)
+      BUILD_TYPE="Release"
+      USE_ASAN="OFF"
+    shift
+    ;;
+    -t|--no-translations)
+      BUILD_TRANSLATIONS="OFF"
+    shift
+    ;;
+    -w|--no-website)
+      BUILD_WEBSITE="OFF"
+    shift
+    ;;
+    --gcc)
+      if [ -f /usr/bin/gcc -a /usr/bin/g++ ]; then
+        export CC=/usr/bin/gcc
+        export CXX=/usr/bin/g++
+      fi
+    shift
+    ;;
+    --clang)
+      if [ -f /usr/bin/clang -a /usr/bin/clang++ ]; then
+        export CC=/usr/bin/clang
+        export CXX=/usr/bin/clang++
+      fi
+    shift
+    ;;
+    *)
+          # unknown option
+    ;;
+  esac
 done
 
+echo "Using ${CORES} core(s)."
+echo ""
+
 if [ $BUILD_WEBSITE = "ON" ]; then
   echo "A complete build will be created."
   echo "You can use -w or --no-website to omit building and"
@@ -199,11 +247,7 @@
       cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DOPTION_BUILD_WEBSITE_TOOLS=$BUILD_WEBSITE -DOPTION_BUILD_TRANSLATIONS=$BUILD_TRANSLATIONS -DOPTION_ASAN=$USE_ASAN
     fi
 
-    if [ $(uname) == "Darwin" ]; then
-      $buildtool -j "$(expr $(sysctl -n hw.ncpu) - 1)"
-    else
-      $buildtool -j "$(nproc --ignore=1)"
-    fi
+    $buildtool -j $CORES
 
     return 0
   }


Follow ups