automated: linux: ltp: incorporate building LTP from git

Added variables: TEST_GIT_URL url to the git repository, TEST_DIR
download directory where the build happens, and a BUILD_FROM_TAR flag.

Signed-off-by: Anders Roxell <anders.roxell@linaro.org>
diff --git a/automated/linux/ltp/ltp.sh b/automated/linux/ltp/ltp.sh
index 7536242..e2f1f08 100755
--- a/automated/linux/ltp/ltp.sh
+++ b/automated/linux/ltp/ltp.sh
@@ -22,9 +22,15 @@
 ENVIRONMENT=""
 # LTP version
 LTP_VERSION="20180926"
+TEST_PROGRAM=ltp
+# https://github.com/linux-test-project/ltp.git
+TEST_GIT_URL=""
+TEST_DIR="$(pwd)/${TEST_PROGRAM}"
+BUILD_FROM_TAR="false"
+
 LTP_TMPDIR=/ltp-tmp
 
-LTP_PATH=/opt/ltp
+LTP_INSTALL_PATH=/opt/ltp
 
 usage() {
     echo "Usage: ${0} [-T mm,math,syscalls]
@@ -36,11 +42,15 @@
                       [-s True|False]
                       [-v LTP_VERSION]
                       [-M Timeout_Multiplier]
-                      [-R root_password]" 1>&2
+                      [-R root_password]
+                      [-u git url]
+                      [-p build directory]
+                      [-t build from tarfile ]
+" 1>&2
     exit 0
 }
 
-while getopts "M:T:S:b:d:g:e:s:v:R:" arg; do
+while getopts "M:T:S:b:d:g:e:s:v:R:u:p:t:" arg; do
    case "$arg" in
      T)
         TST_CMDFILES="${OPTARG}"
@@ -89,6 +99,20 @@
      # Slow machines need more timeout Default is 5min and multiply * MINUTES
      M) export LTP_TIMEOUT_MUL="${OPTARG}";;
      R) export PASSWD="${OPTARG}";;
+     u)
+        if [[ "$OPTARG" != '' ]]; then
+          TEST_GIT_URL="$OPTARG"
+          TEST_TARFILE=""
+        fi
+        ;;
+     p)
+        if [[ "$OPTARG" != '' ]]; then
+          TEST_DIR="$OPTARG"
+        fi
+        ;;
+     t)
+        BUILD_FROM_TAR="$OPTARG"
+        ;;
      *)
         usage
         error_msg "No flag ${OPTARG}"
@@ -96,6 +120,8 @@
   esac
 done
 
+TEST_TARFILE=https://github.com/linux-test-project/ltp/releases/download/"${LTP_VERSION}"/ltp-full-"${LTP_VERSION}".tar.xz
+
 if [ -n "${SKIPFILE_YAML}" ]; then
     export SKIPFILE_PATH="${SCRIPTPATH}/generated_skipfile"
     generate_skipfile
@@ -105,20 +131,6 @@
     SKIPFILE="-S ${SKIPFILE_PATH}"
 fi
 
-# Install LTP test suite
-install_ltp() {
-    rm -rf /opt/ltp
-    mkdir -p /opt/ltp
-    # shellcheck disable=SC2164
-    cd /opt/ltp
-    # shellcheck disable=SC2140
-    wget https://github.com/linux-test-project/ltp/releases/download/"${LTP_VERSION}"/ltp-full-"${LTP_VERSION}".tar.xz
-    tar --strip-components=1 -Jxf ltp-full-"${LTP_VERSION}".tar.xz
-    ./configure
-    make -j8 all
-    make SKIP_IDCHECK=1 install
-}
-
 # Parse LTP output
 parse_ltp_output() {
     grep -E "PASS|FAIL|CONF"  "$1" \
@@ -129,7 +141,7 @@
 # Run LTP test suite
 run_ltp() {
     # shellcheck disable=SC2164
-    cd "${LTP_PATH}"
+    cd "${LTP_INSTALL_PATH}"
     # shellcheck disable=SC2174
     mkdir -m 777 -p "${LTP_TMPDIR}"
 
@@ -162,25 +174,39 @@
     fi
 }
 
-# Test run.
-! check_root && error_msg "This script must be run as root"
-create_out_dir "${OUTPUT}"
+get_tarfile() {
+    local test_tarfile="$1"
+    mkdir "${TEST_DIR}"
+    pushd "${TEST_DIR}" || exit 1
 
-info_msg "About to run ltp test..."
-info_msg "Output directory: ${OUTPUT}"
+    wget "${test_tarfile}"
+    tar --strip-components=1 -Jxf "$(basename "${test_tarfile}")"
+    popd || exit 1
+}
 
-if [ "${SKIP_INSTALL}" = "True" ] || [ "${SKIP_INSTALL}" = "true" ]; then
-    info_msg "install_ltp skipped"
-else
+build_install_tests() {
+    rm -rf "${LTP_INSTALL_PATH}"
+    pushd "${TEST_DIR}" || exit 1
+    [[ -n "${TEST_GIT_URL}" ]] && make autotools
+    ./configure
+    make -j"$(proc)" all
+    make SKIP_IDCHECK=1 install
+    popd || exit 1
+}
+
+install() {
+    dist=
     dist_name
     # shellcheck disable=SC2154
     case "${dist}" in
       debian|ubuntu)
-        pkgs="xz-utils flex bison build-essential wget curl net-tools quota genisoimage sudo libaio-dev expect automake acl"
+        [[ -n "${TEST_GIT_URL}" ]] && pkgs="git"
+        pkgs="${pkgs} xz-utils flex bison build-essential wget curl net-tools quota genisoimage sudo libaio-dev libattr1-dev libcap-dev expect automake acl autotools-dev autoconf m4 pkgconf"
         install_deps "${pkgs}" "${SKIP_INSTALL}"
         ;;
       centos|fedora)
-        pkgs="xz flex bison make automake gcc gcc-c++ kernel-devel wget curl net-tools quota genisoimage sudo libaio expect acl"
+        [[ -n "${TEST_GIT_URL}" ]] && pkgs="git-core"
+        pkgs="${pkgs} xz flex bison make automake gcc gcc-c++ kernel-devel wget curl net-tools quota genisoimage sudo libaio-devel libattr-devel libcap-devel m4 au expect acl pkgconf"
         install_deps "${pkgs}" "${SKIP_INSTALL}"
         ;;
       *)
@@ -198,9 +224,30 @@
             fi
         fi
     fi
+}
 
-    info_msg "Run install_ltp"
-    install_ltp
+# Test run.
+! check_root && error_msg "This script must be run as root"
+create_out_dir "${OUTPUT}"
+
+info_msg "About to run ltp test..."
+info_msg "Output directory: ${OUTPUT}"
+
+if [ "${SKIP_INSTALL}" = "true" ] || [ "${SKIP_INSTALL}" = "True" ]; then
+    info_msg "${TEST_PROGRAM} installation skipped altogether"
+else
+    install
+fi
+
+if [ ! -d ${LTP_INSTALL_PATH} ]; then
+    if [ "${BUILD_FROM_TAR}" = "true" ] || [ "${BUILD_FROM_TAR}" = "True" ]; then
+        get_tarfile "${TEST_TARFILE}"
+    elif [ -n "${TEST_GIT_URL}" ]; then
+        get_test_program "${TEST_GIT_URL}" "${TEST_DIR}" "${LTP_VERSION}" "${TEST_PROGRAM}"
+    else
+        error_msg "I'm confused, get me out of here, can't fetch tar or test version."
+    fi
+    build_install_tests
 fi
 info_msg "Running prep_system"
 prep_system