]> git.rm.cloudns.org Git - xonotic/netradiant.git/commitdiff
easy-builder: improvements
authorThomas Debesse <dev@illwieckz.net>
Tue, 31 Mar 2020 00:40:34 +0000 (02:40 +0200)
committerThomas Debesse <dev@illwieckz.net>
Tue, 31 Mar 2020 00:42:58 +0000 (02:42 +0200)
- handle arguments with spaces properly
- provide fallback for nproc
- replace cmake undocumented -H with -S
- do not require to change directory

easy-builder

index 7966bd266bdb97730307b6a04f1479617423d769..983485ae87ab8df636fa3c6ee5df1d84979801f3 100755 (executable)
@@ -8,93 +8,99 @@ set -o pipefail # Will return the exit status of make if it fails
 
 project_source_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
 
-job_count='4'
-if command -v nproc >/dev/null
-then
-       job_count="$(nproc)"
-fi
-
 build_dir="${project_source_dir}/build${SUBDIR:+/${SUBDIR}}"
 install_dir="${project_source_dir}/install${SUBDIR:+/${SUBDIR}}"
 
 build_type='Release'
 
-cmake_user_opts=''
-while [ ! -z ${1} ]
-do
-    case "${1}" in
-    '-j'*)
-        job_count="${1:2}"
-        shift
-        ;;
-    '--debug')
-        build_type='Debug'
-        shift
-        ;;
-    *)
-       cmake_user_opts+=" ${1}"
-       shift
-        ;;
-    esac
-done
-
-cmake_opts=''
+declare -a cmake_opts
 case "$(uname -s)" in
        'Linux')
-               # no tweak required
+               nproc='egrep "^processor" /proc/cpuinfo | wc -l'
                ;;
        'FreeBSD')
+               nproc='sysctl -n hw.ncpu'
+
                if [ -f "$(ls '/usr/local/bin/g++'* | sort | tail -n1)" ]
                then
                        gcc_version="$(ls '/usr/local/bin/g++'* | sort | tail -n1 | sed -e 's/.*[^0-9]\([0-9][0-9]*\)$/\1/')"
-                       cmake_opts+=" -DCMAKE_C_COMPILER=/usr/local/bin/gcc${gcc_version}"
-                       cmake_opts+=" -DCMAKE_CXX_COMPILER=/usr/local/bin/g++${gcc_version}"
+                       cmake_opts[${#cmake_opts[@]}]="-DCMAKE_C_COMPILER=/usr/local/bin/gcc${gcc_version}"
+                       cmake_opts[${#cmake_opts[@]}]="-DCMAKE_CXX_COMPILER=/usr/local/bin/g++${gcc_version}"
                else
                        printf "WARNING: GCC is recommended: if build fails, install GCC and retry\n" >&2
                fi
                ;;
        'Darwin')
+               nproc='sysctl -n hw.ncpu'
+
                if [ -f "$(ls '/usr/local/bin/g++-'* | sort | tail -n1)" ]
                then
                        gcc_version="$(ls '/usr/local/bin/g++-'* | sort | tail -n1 | sed -e 's/.*[^0-9]\([0-9][0-9]*\)$/\1/')"
-                       cmake_opts+=" -DCMAKE_C_COMPILER=/usr/local/bin/gcc-${gcc_version}"
-                       cmake_opts+=" -DCMAKE_CXX_COMPILER=/usr/local/bin/g++-${gcc_version}"
+                       cmake_opts[${#cmake_opts[@]}]="-DCMAKE_C_COMPILER=/usr/local/bin/gcc-${gcc_version}"
+                       cmake_opts[${#cmake_opts[@]}]="-DCMAKE_CXX_COMPILER=/usr/local/bin/g++-${gcc_version}"
                else
                        printf "WARNING: GCC is recommended: if build fails, install GCC and retry\n" >&2
                fi
                ;;
        'MSYS_NT-'*)
-               # no tweak required
+               nproc='echo "${NUMBER_OF_PROCESSORS}"'
                ;;
        'CYGWIN_NT-'*|'MINGW'*'_NT-'*)
+               nproc='echo "${NUMBER_OF_PROCESSORS}"'
                printf "WARNING: system is not tested: if build fails, use MSYS2 instead\n" >&2
                ;;
        *)
+               nproc='true'
                printf "WARNING: system is not tested\n" >&2
                ;;
 esac
 
-fetch_submodules_cmd=''
+if command -v 'nproc' >/dev/null
+then
+       job_count="$(nproc)"
+else
+       job_count="$(sh -c "${nproc}")"
+fi
+
+job_count="${job_count:-4}"
+
+declare -a cmake_user_opts
+while [ ! -z "${1}" ]
+do
+    case "${1}" in
+    '-j'*)
+        job_count="${1:2}"
+        shift
+        ;;
+    '--debug')
+        build_type='Debug'
+        shift
+        ;;
+    *)
+       cmake_user_opts[${#cmake_user_opts[@]}]="${1}"
+       shift
+        ;;
+    esac
+done
+
+declare -a fetch_submodules_cmd
 if ! [ -f "${project_source_dir}/libs/crunch/inc/crn_decomp.h" ]
 then
-       fetch_submodules_cmd='git submodule update --init --recursive'
+       fetch_submodules_cmd=(git -C "${project_source_dir}" submodule update --init --recursive)
 fi
 
 set -x
 
-cd "${project_source_dir}"
-
-${fetch_submodules_cmd}
+"${fetch_submodules_cmd[@]}"
 
 cmake \
        -G'Unix Makefiles' \
-       -H'.' \
+       -S"${project_source_dir}" \
        -B"${build_dir}" \
        -D'CMAKE_INSTALL_PREFIX'="${install_dir}" \
        -D'CMAKE_BUILD_TYPE'="${build_type}" \
-       ${cmake_opts} \
-       ${cmake_user_opts} \
-       ${@}
+       "${cmake_opts[@]}" \
+       "${cmake_user_opts[@]}"
 
 cmake \
        --build "${build_dir}" \