2011-09-22 22:16:29 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2018-09-13 17:08:20 +00:00
|
|
|
GetPlatform() {
|
|
|
|
# Get OS and platform to decide if we need to limit memory usage
|
|
|
|
# during the build
|
|
|
|
PLATFORM=$(uname -a)
|
|
|
|
case "$PLATFORM" in
|
|
|
|
"Linux raspberrypi"*) MAX_MEM_PER_JOB=300000;;
|
|
|
|
"Linux"*) MAX_MEM_PER_JOB=150000;;
|
|
|
|
*) MAX_MEM_PER_JOB=200000 # If platform is not Linux (fallback value)
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
BuildJobsThrottler(){
|
|
|
|
echo "Building on Linux : computing number of allowed parallel jobs."
|
|
|
|
echo "Maximum allowed RAM memory per job is $MAX_MEM_PER_JOB kB."
|
|
|
|
|
|
|
|
# Get number of CPUs on this target
|
|
|
|
# getconf does not exit on Darwin. Use sysctl on Darwin machines.
|
2020-06-12 02:49:28 +00:00
|
|
|
CPU_N=$(getconf _NPROCESSORS_ONLN 2>/dev/null || sysctl -n hw.ncpu)
|
2018-09-13 17:08:20 +00:00
|
|
|
printf "Number of CPUs is %s and " "$CPU_N"
|
|
|
|
|
|
|
|
# Get remaining RAM that could be used for this build
|
|
|
|
FREE_RAM=$(grep MemAvailable /proc/meminfo | sed 's/[^0-9]//g')
|
|
|
|
|
|
|
|
DEFAULT_MAX_MEM_PER_JOB=200000
|
|
|
|
[ -z "${MAX_MEM_PER_JOB}" ] && MAX_MEM_PER_JOB="$DEFAULT_MAX_MEM_PER_JOB" # Defensive, prevent devision by 0
|
2020-06-12 02:49:28 +00:00
|
|
|
|
2018-09-13 17:08:20 +00:00
|
|
|
# Assuming we may have many 300MB compilation jobs running in parallel
|
|
|
|
MEM_ALLOWED_JOBS=$((FREE_RAM / MAX_MEM_PER_JOB))
|
2020-06-12 02:49:28 +00:00
|
|
|
echo "current free RAM allows us to run $MEM_ALLOWED_JOBS jobs in parallel."
|
2018-09-13 17:08:20 +00:00
|
|
|
|
|
|
|
# Set number of build jobs to be run in parallel as the minimum between $MEM_ALLOWED_JOBS and $CPU_N
|
|
|
|
MAKE_JOBS=$((MEM_ALLOWED_JOBS<CPU_N?MEM_ALLOWED_JOBS:CPU_N))
|
2018-09-13 23:42:50 +00:00
|
|
|
if [ ${MAKE_JOBS} -lt 1 ]; then
|
|
|
|
MAKE_JOBS=8
|
|
|
|
fi
|
2018-09-13 17:08:20 +00:00
|
|
|
echo "So, the build will run on $MAKE_JOBS job(s)."
|
|
|
|
}
|
|
|
|
|
2020-06-12 02:49:28 +00:00
|
|
|
OSNAME=$(uname)
|
|
|
|
|
|
|
|
if [ "${OSNAME}" = Linux ]; then
|
2018-10-23 20:22:38 +00:00
|
|
|
# Identify current platform
|
|
|
|
GetPlatform
|
|
|
|
# Define number of parallel jobs depending on ncpus and memory
|
|
|
|
BuildJobsThrottler
|
|
|
|
fi
|
2018-09-13 17:08:20 +00:00
|
|
|
|
2017-03-13 23:38:56 +00:00
|
|
|
if [ -z "${MAKE}" ]; then
|
2018-05-16 14:35:53 +00:00
|
|
|
MAKE=make
|
|
|
|
gmake --help >/dev/null 2>&1
|
|
|
|
[ $? = 0 ] && MAKE=gmake
|
2017-03-13 23:38:56 +00:00
|
|
|
fi
|
2018-09-13 17:08:20 +00:00
|
|
|
|
2015-11-06 16:34:44 +00:00
|
|
|
[ -z "${MAKE_JOBS}" ] && MAKE_JOBS=12
|
2016-01-16 23:35:30 +00:00
|
|
|
[ -z "${CERTID}" ] && CERTID=org.radare.radare2
|
2015-11-06 16:34:44 +00:00
|
|
|
|
|
|
|
# find root
|
2018-05-16 14:35:53 +00:00
|
|
|
A=$(dirname "$PWD/$0")
|
|
|
|
cd "$A" && cd .. || exit 1
|
2015-11-06 16:34:44 +00:00
|
|
|
|
2020-03-20 14:27:20 +00:00
|
|
|
DEFAULT_PREFIX=/usr/local
|
2020-06-12 02:49:28 +00:00
|
|
|
if [ "${OSNAME}" = Darwin ]; then
|
2018-05-16 14:35:53 +00:00
|
|
|
# purge previous installations on other common paths
|
|
|
|
if [ -f /usr/bin/r2 ]; then
|
|
|
|
type sudo || NOSUDO=1
|
|
|
|
[ "$(id -u)" = 0 ] || SUDO=sudo
|
|
|
|
[ -n "${NOSUDO}" ] && SUDO=
|
|
|
|
fi
|
2015-11-06 16:34:44 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
[ -z "${PREFIX}" ] && PREFIX="${DEFAULT_PREFIX}"
|
2013-05-02 21:49:58 +00:00
|
|
|
|
2019-11-20 15:40:02 +00:00
|
|
|
for a in $* ; do
|
|
|
|
case "$a" in
|
|
|
|
-h|--help)
|
2020-03-20 14:27:20 +00:00
|
|
|
echo "Usage: sys/build.sh [/usr/local]"
|
2019-11-20 15:40:02 +00:00
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
'')
|
|
|
|
:
|
|
|
|
;;
|
|
|
|
--**|-)
|
|
|
|
shift
|
|
|
|
CFGARG="${CFGARG} $a"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
PREFIX="$a"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ "${USE_CS5}" = 1 ]; then
|
2019-05-14 15:53:34 +00:00
|
|
|
CFGARG="${CFGARG} --with-capstone5"
|
|
|
|
fi
|
|
|
|
|
2020-06-12 02:49:28 +00:00
|
|
|
if [ "${OSNAME}" != Darwin ] && [ -n "${PREFIX}" ] && [ "${PREFIX}" != /usr ]; then
|
2020-05-29 03:04:15 +00:00
|
|
|
CFGARG="${CFGARG} --with-rpath"
|
|
|
|
fi
|
|
|
|
|
2020-06-12 02:49:28 +00:00
|
|
|
ccache --help > /dev/null
|
2011-09-22 22:16:29 +00:00
|
|
|
if [ $? = 0 ]; then
|
|
|
|
[ -z "${CC}" ] && CC=gcc
|
|
|
|
CC="ccache ${CC}"
|
|
|
|
export CC
|
|
|
|
fi
|
|
|
|
|
2015-08-19 17:32:58 +00:00
|
|
|
# Required to build on FreeBSD
|
2020-06-12 02:49:28 +00:00
|
|
|
if [ ! -x /usr/bin/gcc ] && [ -x /usr/bin/cc ]; then
|
2015-08-19 17:32:58 +00:00
|
|
|
export CC=cc
|
|
|
|
export HOST_CC=cc
|
|
|
|
fi
|
2015-05-14 01:46:09 +00:00
|
|
|
|
2020-03-20 14:27:20 +00:00
|
|
|
# purge first
|
|
|
|
if [ "${PREFIX}" != "/usr" ]; then
|
2020-06-12 02:49:28 +00:00
|
|
|
A=$(readlink /usr/bin/radare2 2>/dev/null)
|
2020-03-20 14:27:20 +00:00
|
|
|
B="${PWD}/binr/radare2/radare2"
|
2020-06-12 02:49:28 +00:00
|
|
|
if [ -n "$A" ] && [ ! -f "$A" ]; then
|
2020-03-20 14:27:20 +00:00
|
|
|
A="$B"
|
|
|
|
fi
|
|
|
|
if [ "$A" = "$B" ]; then
|
|
|
|
echo "Purging r2 installation from /usr..."
|
|
|
|
./configure --prefix=/usr > /dev/null
|
|
|
|
echo ${SUDO} ${MAKE} uninstall
|
|
|
|
SD=""
|
|
|
|
type sudo && SD=sudo
|
|
|
|
${SD} ${MAKE} uninstall
|
|
|
|
fi
|
|
|
|
fi
|
2014-11-23 22:25:14 +00:00
|
|
|
|
2011-09-22 22:16:29 +00:00
|
|
|
# build
|
2012-08-10 09:35:38 +00:00
|
|
|
${MAKE} mrproper > /dev/null 2>&1
|
2020-06-12 02:49:28 +00:00
|
|
|
[ "${OSNAME}" = Linux ] && export LDFLAGS="-Wl,--as-needed ${LDFLAGS}"
|
2020-03-20 14:27:20 +00:00
|
|
|
[ -z "${KEEP_PLUGINS_CFG}" ] && rm -f plugins.cfg
|
2020-06-12 02:49:28 +00:00
|
|
|
unset R2DEPS
|
2018-05-16 14:35:53 +00:00
|
|
|
pwd
|
2019-11-20 15:40:02 +00:00
|
|
|
|
2018-05-16 14:35:53 +00:00
|
|
|
./configure ${CFGARG} --prefix="${PREFIX}" || exit 1
|
2015-09-20 21:16:09 +00:00
|
|
|
${MAKE} -s -j${MAKE_JOBS} MAKE_JOBS=${MAKE_JOBS} || exit 1
|
2020-06-12 02:49:28 +00:00
|
|
|
if [ "${OSNAME}" = Darwin ]; then
|
2018-05-04 18:07:50 +00:00
|
|
|
./sys/macos-cert.sh
|
2017-11-15 15:43:31 +00:00
|
|
|
${MAKE} macos-sign macos-sign-libs CERTID="${CERTID}" || (
|
2015-09-20 21:16:09 +00:00
|
|
|
echo "CERTID not defined. If you want the bins signed to debug without root"
|
2017-11-15 15:43:31 +00:00
|
|
|
echo "follow the instructions described in doc/macos.md and run make macos-sign."
|
2015-09-20 21:16:09 +00:00
|
|
|
)
|
|
|
|
fi
|