From 25a828190b2a08105ad561b5b417bb61a383fc4f Mon Sep 17 00:00:00 2001
From: Francis Rowe <info@gluglug.org.uk>
Date: Thu, 10 Mar 2016 20:18:49 +0000
Subject: build system: allow arbitrary specification of number of cores used

Use NPROC=foo
Replace "foo" with a number. By default, the build system uses $(nproc).
This patch allows the user to specify any number of cores. This is useful
on some systems, or certain chroot environments.
---
 resources/scripts/helpers/build/module/bucts       | 13 +++++++++++-
 resources/scripts/helpers/build/module/flashrom    | 23 ++++++++++++++++------
 resources/scripts/helpers/build/module/grub        | 13 +++++++++++-
 .../scripts/helpers/build/roms/withdepthcharge     | 17 +++++++++++++---
 .../scripts/helpers/build/roms/withgrub_helper     | 13 +++++++++++-
 5 files changed, 67 insertions(+), 12 deletions(-)

diff --git a/resources/scripts/helpers/build/module/bucts b/resources/scripts/helpers/build/module/bucts
index 6e54fc1b..8e2b371d 100755
--- a/resources/scripts/helpers/build/module/bucts
+++ b/resources/scripts/helpers/build/module/bucts
@@ -24,6 +24,17 @@
 
 [ "x${DEBUG+set}" = 'xset' ] && set -v
 set -u -e
+if [ -z ${NPROC+x} ]; then
+    cores="$(nproc)"
+else
+    case ${NPROC} in
+        ''|*[!0-9]*)
+            printf "value '%s' for NPROC is invalid. non-numeric. Exiting.\n" "${NPROC}"
+            exit 1
+            ;;
+    esac
+    cores="${NPROC}"
+fi
 
 # Build BUC.TS utility (needed for flashing ROM images on X60/T60 while Lenovo BIOS is running)
 # --------------------------------------------------------------------
@@ -44,7 +55,7 @@ if [ "${buildtype}" = "static" ]; then
 fi
 
 make clean
-make -j$(nproc)
+make -j${cores}
 
 if [ "${buildtype}" = "static" ]; then
 	patch "Makefile" -R < "../resources/bucts/patch/staticlink.diff"
diff --git a/resources/scripts/helpers/build/module/flashrom b/resources/scripts/helpers/build/module/flashrom
index 3664f565..54c12edb 100755
--- a/resources/scripts/helpers/build/module/flashrom
+++ b/resources/scripts/helpers/build/module/flashrom
@@ -24,6 +24,17 @@
 
 [ "x${DEBUG+set}" = 'xset' ] && set -v
 set -u -e
+if [ -z ${NPROC+x} ]; then
+    cores="$(nproc)"
+else
+    case ${NPROC} in
+        ''|*[!0-9]*)
+            printf "value '%s' for NPROC is invalid. non-numeric. Exiting.\n" "${NPROC}"
+            exit 1
+            ;;
+    esac
+    cores="${NPROC}"
+fi
 
 # Build "flashrom" (utility for flashing/dumping ROMs)
 # --------------------------------------------------------------------
@@ -34,12 +45,12 @@ cd "flashrom/"
 
 make clean
 if (( $# != 1 )); then
-	make -j$(nproc)
+	make -j${cores}
 else
 	if [ "${1}" = "static" ]; then
-		make SHARED=0 CC='gcc -static' -j$(nproc)
+		make SHARED=0 CC='gcc -static' -j${cores}
 	else
-		make -j$(nproc)
+		make -j${cores}
 	fi
 fi
 
@@ -60,12 +71,12 @@ do
 	
 	make clean
 	if (( $# != 1 )); then
-		make -j$(nproc)
+		make -j${cores}
 	else
 		if [ "${1}" = "static" ]; then
-			make SHARED=0 CC='gcc -static' -j$(nproc)
+			make SHARED=0 CC='gcc -static' -j${cores}
 		else
-			make -j$(nproc)
+			make -j${cores}
 		fi
 	fi
 	
diff --git a/resources/scripts/helpers/build/module/grub b/resources/scripts/helpers/build/module/grub
index f899bc25..5b2c7a6d 100755
--- a/resources/scripts/helpers/build/module/grub
+++ b/resources/scripts/helpers/build/module/grub
@@ -24,6 +24,17 @@
 
 [ "x${DEBUG+set}" = 'xset' ] && set -v
 set -u -e
+if [ -z ${NPROC+x} ]; then
+    cores="$(nproc)"
+else
+    case ${NPROC} in
+        ''|*[!0-9]*)
+            printf "value '%s' for NPROC is invalid. non-numeric. Exiting.\n" "${NPROC}"
+            exit 1
+            ;;
+    esac
+    cores="${NPROC}"
+fi
 
 # Build GRUB2 as coreboot payload
 
@@ -39,5 +50,5 @@ cd grub/
 # build
 ./autogen.sh
 ./configure --with-platform=coreboot
-make -j$(nproc)
+make -j${cores}
 )
diff --git a/resources/scripts/helpers/build/roms/withdepthcharge b/resources/scripts/helpers/build/roms/withdepthcharge
index eaab8f04..cf473e20 100755
--- a/resources/scripts/helpers/build/roms/withdepthcharge
+++ b/resources/scripts/helpers/build/roms/withdepthcharge
@@ -24,6 +24,17 @@
 
 [ "x${DEBUG+set}" = 'xset' ] && set -v
 set -u -e
+if [ -z ${NPROC+x} ]; then
+    cores="$(nproc)"
+else
+    case ${NPROC} in
+        ''|*[!0-9]*)
+            printf "value '%s' for NPROC is invalid. non-numeric. Exiting.\n" "${NPROC}"
+            exit 1
+            ;;
+    esac
+    cores="${NPROC}"
+fi
 
 printf "Building ROM images with the depthcharge payload\n"
 
@@ -63,7 +74,7 @@ buildlibpayload() {
 	rm -Rf "install/"
 
 	make KBUILD_DEFCONFIG="configs/config.$family" defconfig
-	make -j$(nproc)
+	make -j${cores}
 	make DESTDIR="install" install
 
     cd "../../"
@@ -94,7 +105,7 @@ builddepthcharge() {
 	rm -f "../coreboot/${cbrevision:?}/${cbrevision:?}/depthcharge.elf"
 
 	make BOARD="${board}" defconfig
-	make BOARD="${board}" LIBPAYLOAD_DIR="$(pwd)/../coreboot/${cbrevision}/${cbrevision}/payloads/libpayload/install/libpayload" VB_SOURCE="$(pwd)/../coreboot/${cbrevision}/${cbrevision}/3rdparty/vboot" -j$(nproc) depthcharge_unified
+	make BOARD="${board}" LIBPAYLOAD_DIR="$(pwd)/../coreboot/${cbrevision}/${cbrevision}/payloads/libpayload/install/libpayload" VB_SOURCE="$(pwd)/../coreboot/${cbrevision}/${cbrevision}/3rdparty/vboot" -j${cores} depthcharge_unified
 
 	cp "build/depthcharge.elf" "../coreboot/${cbrevision}/${cbrevision}/"
 
@@ -129,7 +140,7 @@ buildcoreboot() {
 	printf 'libreboot-%s\n' "${version}" > ".coreboot-version" # needed for reproducible builds in coreboot
 
 	make KBUILD_DEFCONFIG="$(pwd)/../../../resources/libreboot/config/depthcharge/${board}/config" defconfig
-	make -j$(nproc)
+	make -j${cores}
 
 	cbfs_size=$( grep CONFIG_CBFS_SIZE ".config" | sed "s/.*[[:space:]]*=[[:space:]]*//g" )
 	cbfs_size=$( printf "%d\n" "${cbfs_size}" )
diff --git a/resources/scripts/helpers/build/roms/withgrub_helper b/resources/scripts/helpers/build/roms/withgrub_helper
index bd1af190..16e861e1 100755
--- a/resources/scripts/helpers/build/roms/withgrub_helper
+++ b/resources/scripts/helpers/build/roms/withgrub_helper
@@ -24,6 +24,17 @@
 
 [ "x${DEBUG+set}" = 'xset' ] && set -v
 set -u -e
+if [ -z ${NPROC+x} ]; then
+    cores="$(nproc)"
+else
+    case ${NPROC} in
+        ''|*[!0-9]*)
+            printf "value '%s' for NPROC is invalid. non-numeric. Exiting.\n" "${NPROC}"
+            exit 1
+            ;;
+    esac
+    cores="${NPROC}"
+fi
 
 if (( $# != 1 )); then
 	printf "Usage: ./buildrom-withgrub boardname\n"
@@ -98,7 +109,7 @@ do
 	make clean
 	mv "config_${romtype}" ".config"
 	cp "../../grub_${romtype}.elf" "grub.elf"
-	make -j$(nproc)
+	make -j${cores}
 	rm -f "grub.elf"
 	mv "build/coreboot.rom" "${boardtarget}_${romtype}.rom"
 	# .config no longer needed
-- 
cgit v1.2.3-70-g09d2