2013-12-15 19:06:50 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -e
|
2016-09-18 21:10:28 +02:00
|
|
|
set -u
|
2013-12-15 19:06:50 +01:00
|
|
|
|
2016-09-18 21:10:28 +02:00
|
|
|
# Print number of enabled CPUs. Use this as a simple, platform-independent
|
|
|
|
# replacement for nproc or ncpus.
|
|
|
|
#
|
|
|
|
# The shell script wraps a simple C++ tool which will be compiled on demand.
|
2013-12-16 21:53:21 +01:00
|
|
|
|
2013-12-15 19:06:50 +01:00
|
|
|
|
2016-09-18 21:10:28 +02:00
|
|
|
# This script's location. The proper way to do this in bash is using
|
|
|
|
# ${BASH_SOURCE[0]}; ignore the possibility of softlinks.
|
|
|
|
script_dir=$(cd $(dirname ${BASH_SOURCE[0]}) && pwd)
|
2013-12-15 19:06:50 +01:00
|
|
|
|
2016-09-18 21:10:28 +02:00
|
|
|
prog="$script_dir/cpucount"
|
2013-12-15 19:06:50 +01:00
|
|
|
case "$(uname -s)" in
|
2015-07-19 23:19:31 +02:00
|
|
|
*NT* | CYGWIN*)
|
2013-12-15 19:06:50 +01:00
|
|
|
prog="${prog}.exe" ;;
|
|
|
|
esac
|
|
|
|
|
2016-09-18 21:10:28 +02:00
|
|
|
if [ ! -f $prog ]
|
|
|
|
then
|
|
|
|
# Don't have cpucount. Build it.
|
2013-12-15 19:06:50 +01:00
|
|
|
|
2016-09-18 21:10:28 +02:00
|
|
|
if ! which c++ >/dev/null
|
|
|
|
then
|
|
|
|
# Can't compile cpucount. Just give the safe answer.
|
|
|
|
echo 1
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Attempt to compile cpucount.cpp.
|
|
|
|
if ! c++ $prog.cpp -o $prog &>/dev/null
|
|
|
|
then
|
|
|
|
# Okay, that didn't work... Try it with gcc/clang's option to force
|
|
|
|
# C++11. Versions of gcc older than 6.x still default to C++98.
|
|
|
|
c++ $prog.cpp -std=c++11 -o $prog >/dev/null
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Now, at last: run cpucount.
|
|
|
|
$prog
|