Add LINK_JOBS logic to all three builder scripts

The stress, helper and bisect build scripts needed the LLVM_LINK_JOBS option
to make sure it doesn't run out of memory while linking, especially in SoCs
that have a lot more cores than RAM (ex. HiKey).

This patch calculates the jobs by seeing how much RAM the target has and adding
+1, as link jobs rarely take more than 1GB, but the final ones do use several
hundred. Also making sure we don't exceed the number of cores.

A few other small changes:
 * Replace "else if" by "elif"
 * Build only minimal targets on ARM (even o full build)
 * Fixed a -j$PARALLEL bug introduced by a prvious commit
 * Converted a few backticks into $()

Change-Id: If4f2189623338d2924357ecd3d4ee18feb522c32
diff --git a/bisect/run.sh b/bisect/run.sh
index 0ff1d0c..97d1a68 100755
--- a/bisect/run.sh
+++ b/bisect/run.sh
@@ -9,19 +9,27 @@
 CORE=`grep "CPU part" /proc/cpuinfo | awk '{print $4}'`
 if [[ $CORE = '0xc08' ]]; then
   CORE="-mcpu=cortex-a8"
-else if [[ $CORE = '0xc09' ]]; then
+elif [[ $CORE = '0xc09' ]]; then
   CORE="-mcpu=cortex-a9"
-else if [[ $CORE = '0xc0f' ]]; then
+elif [[ $CORE = '0xc0f' ]]; then
   CORE="-mcpu=cortex-a15"
-else if [[ $CORE = '0xd03' ]]; then
+elif [[ $CORE = '0xd03' ]]; then
   CORE="-mcpu=cortex-a53"
-else if [[ $CORE = '0xd07' ]]; then
+elif [[ $CORE = '0xd07' ]]; then
   CORE="-mcpu=cortex-a57"
 else
   CORE=''
-fi fi fi fi fi
+fi
 
-PARALLEL=-j`grep -c proc /proc/cpuinfo`
+CPUS=$(grep -c proc /proc/cpuinfo)
+PARALLEL=-j$CPUS
+LINK=$(free -g | awk '/Mem/ {print $2}')
+if [ "$LINK" -gt "$CPUS" ]; then
+  LINK=$CPUS
+else
+  LINK=$((LINK+1))
+fi
+
 
 selfhost=false
 check=false
@@ -82,7 +90,8 @@
                            -DLLVM_TARGETS_TO_BUILD="ARM;AArch64" \
                            -DLLVM_BUILD_TESTS=True \
                            -DLLVM_ENABLE_ASSERTIONS=True \
-                           -DLLVM_LIT_ARGS="-sv $PARALLEL"
+                           -DLLVM_LIT_ARGS="-sv $PARALLEL" \
+                           -DLLVM_PARALLEL_LINK_JOBS=$LINK
   safe_run ninja $PARALLEL
   if $check; then
     safe_run ninja check-all