From 1a27f8134273d2f1397cbdc844dbe6bcf829d64a Mon Sep 17 00:00:00 2001 From: Rafael Kitover Date: Thu, 31 Aug 2017 16:56:06 -0700 Subject: [PATCH] installdeps: use -j$(nproc) not -j8 in info #146 Replace the hardcoded `make -j8` command in the build instructions with a `-j` parameter that is the number of the host's CPUs minus one. Subtracting 1 is done to reduce chances of overloading the host. If the value is `1`, then don't print the `-j` flag at all. --- installdeps | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/installdeps b/installdeps index d9c8bbe3..ae1af887 100755 --- a/installdeps +++ b/installdeps @@ -28,12 +28,15 @@ main() { case "$(uname -s)" in Linux) + os=linux linux_installdeps ;; Darwin) + os=mac osx_installdeps ;; MINGW*|MSYS*) + os=windows msys2_installdeps ;; *) @@ -154,6 +157,38 @@ linux_installdeps() { fi } +# the -j flag for make parameter, empty if 1 +jobs_flag() { + if [ $(num_cpus) -gt 1 ]; then + echo "-j$(num_cpus)" + fi +} + +# number of CPUs to use for jobs, 1 less than total to not overload resources +num_cpus() { + if [ -n "$_num_cpus" ]; then + if [ $((_num_cpus - 1)) -lt 1 ]; then + echo 1 + else + echo $((_num_cpus - 1)) + fi + return 0 + fi + + # determine number of CPUs and cache it + if command -v nproc >/dev/null; then + _num_cpus=$(nproc) + elif [ $os = linux -o $os = windows ]; then + _num_cpus=$(grep '^processor *:' /proc/cpuinfo | wc -l) + elif [ $os = mac ]; then + _num_cpus=$(sysctl -n hw.ncpu) + fi + + [ -z "$_num_cpus" ] && _num_cpus=1 + + num_cpus +} + check_cross() { target=$(echo "$target" | tr 'A-Z' 'a-z') @@ -519,7 +554,7 @@ EOF grep -Ev '^[ ]*MAKEFLAGS=' "$MAKEPKG_CONF" > "$tmp/makepkg.conf" - export MAKEFLAGS="-j$(($(cat /proc/cpuinfo | grep -E '^processor ' | wc -l)+1))" + export MAKEFLAGS=$(jobs_flag) echo "MAKEFLAGS=\"$MAKEFLAGS\"" >> "$tmp/makepkg.conf" export MAKEPKG_CONF="$tmp/makepkg.conf" @@ -731,7 +766,7 @@ build_instructions() { $pre_build mkdir -p build && cd build $cmake .. $cmake_flags -make -j8 +make $(jobs_flag) $post_build EOF