Add new config options to b-test

Change b-test to allow setting the log directory and the install
directory on the command line. The aim is to allow relocating
output files when executed in the CI.

Signed-off-by: Gyorgy Szing <Gyorgy.Szing@arm.com>
Change-Id: Idd1d82b4abbc6d1ed914c04573a3191183698d0b
diff --git a/tools/b-test/Makefile b/tools/b-test/Makefile
index 9a060b8..a6865a0 100644
--- a/tools/b-test/Makefile
+++ b/tools/b-test/Makefile
@@ -15,23 +15,35 @@
 FILTER_PATTERN ?=
 
 ifneq (,${FILTER_PATTERN})
-FILTER_ARG=-p "${FILTER_PATTERN}"
+RUN_SH_ARGS += -p "${FILTER_PATTERN}"
+endif
+
+ifneq (,${INSTALL_PREFIX})
+RUN_SH_ARGS += -i ${INSTALL_PREFIX}
+endif
+
+ifneq (,${BUILD_PREFIX})
+RUN_SH_ARGS += -b ${BUILD_PREFIX}
+endif
+
+ifneq (,${LOGDIR_ROOT})
+RUN_SH_ARGS += -l ${LOGDIR_ROOT}
 endif
 
 all:
 	${MAKE} config
-	bash run.sh ${FILTER_ARG}
+	bash run.sh ${RUN_SH_ARGS}
 
 config: ${OUTPUTS}
 
 list:
 	${MAKE} config
-	bash run.sh ${FILTER_ARG} help
+	bash run.sh ${RUN_SH_ARGS} help
 
 # run a command of the generated script
 r-%:
 	${MAKE} config
-	bash run.sh ${FILTER_ARG} "$*"
+	bash run.sh ${RUN_SH_ARGS} "$*"
 
 define help_msg
 *************************************************************
@@ -40,9 +52,13 @@
   or be set in the environment (e.g. export <var>=<value>;
   make ...).
 
+  BUILD_PREFIX   - Directory for build directories
   FILTER_PATTERN - grep regexp to limit test scope to tests
                    whose name matches. To see the selected tests
                    run: make FILTER_PATTERN=<pattern> list
+  INSTALL_PREFIX - Directory to install projects
+  LOGDIR_ROOT    - Directory to save build logs
+
 Available targets:
   all          - generate and run test script
   config       - run script generation only
diff --git a/tools/b-test/run.sh.j2 b/tools/b-test/run.sh.j2
index 86fd383..0651f39 100644
--- a/tools/b-test/run.sh.j2
+++ b/tools/b-test/run.sh.j2
@@ -30,6 +30,12 @@
 # By default run as much as we can
 FAIL_FAST=${FAIL_FAST:-0}
 
+# Directory to install projects to.
+INSTALL_PREFIX=${INSTALL_PREFIX:-./install}
+
+# Directory to place cmake build directories to
+BUILD_PREFIX=${BUILD_PREFIX:-.}
+
 # Global exit code.
 exit_code=0
 
@@ -42,7 +48,18 @@
 
 # Convert test name to build directory
 function name-to-bdir() {
-	printf "./build-%s" "$1"
+	printf "$BUILD_PREFIX/build-%s" "$1"
+}
+
+unset btest_logdir_root
+function name-to-logfile {
+	local config_name="$1"
+	if [ -z "$btest_logdir_root" ]
+	then
+		printf "%s/build.log" $(name-to-bdir "$1")
+	else
+		printf "$btest_logdir_root/build-%s.log" "$1"
+	fi
 }
 
 # Wrap cmake to allow verbose vs non-verbose mode
@@ -74,7 +91,7 @@
 	fi
 	{% endif %}
 	b_dir=$(name-to-bdir "{{config.name}}")
-	log_file=$b_dir/build.log
+	log_file=$(name-to-logfile "{{config.name}}")
 	rm -rf "$b_dir"
 	mkdir -p "$b_dir"
 
@@ -90,7 +107,7 @@
 	then
 		if _cmake "$log_file" --build "$b_dir" -j "${NUMBER_OF_PROCESSORS}" --verbose
 		then
-			if _cmake "$log_file" --install "$b_dir" --prefix ./install
+			if _cmake "$log_file" --install "$b_dir" --prefix "$INSTALL_PREFIX"
 			then
 				echo "########################## $COLOR_GREEN {{config.name}} passed $COLOR_RESET"
 			else
@@ -148,14 +165,25 @@
         NUMBER_OF_PROCESSORS=$NUMBER_OF_PROCESSORS
     - CMAKE_EXTRA_FLAGS: additional environment specific CMake flags
         CMAKE_EXTRA_FLAGS=-DNEWLIB_LIBC_PATH=/path/to/newlib
-	- FAIL_FAST: <0/1> see -f
-	    FAIL_FAST=$FAIL_FAST
+    - FAIL_FAST: <0/1> see -f
+        FAIL_FAST=$FAIL_FAST
+    - INSTALL_PREFIX: <path> see -i
+        INSTALL_PREFIX=$INSTALL_PREFIX
+    - BUILD_PREFIX: <path> see -b
+        BUILD_PREFIX=$BUILD_PREFIX
 
 Options:
+    -b|--build-prefix: <path>
+            - directory for cmake build directories
     --color|--no-color
-	        - force or disable output coloring
-	-f|--fail-fast
-	        - exit after the first test failure
+            - force or disable output coloring
+    -f|--fail-fast
+            - exit after the first test failure
+    -i|--install-prefix <path>
+            - directory to install build results to.
+    -l|--log-dir <path>
+            - directory to save log files to. If not set, files are saved
+              to the build directory.
     -p <regexp>
             - filter out all test cases whose name is not matching regexp.
               regexp is a regular expression understood by grep
@@ -268,7 +296,8 @@
 while true
 do
 	# parse options
-	case $1 in
+	arg=$1
+	case $arg in
 		-p)
 			shift
 			pattern=$1
@@ -294,9 +323,52 @@
 		--color)
 			set_color_mode "on"
 			;;
-		--f|--fail-fast)
+		-f|--fail-fast)
 			FAIL_FAST=1
 			;;
+		-l|--log-dir)
+			shift
+			btest_logdir_root=$1
+			[ -z "$btest_logdir_root" ] && {
+				echo "${COLOR_RED}Error: missing argument <path>. $COLOR_RESET"
+				exit_code=1
+				break
+			}
+
+			[ -d "$btest_logdir_root" ] || {
+				mkdir -p "$btest_logdir_root"
+			}
+			;;
+		-i|--install-prefix)
+			shift
+			INSTALL_PREFIX="$1"
+			[ -z "$INSTALL_PREFIX" ] && {
+				echo "${COLOR_RED}Error: missing argument <path> after $arg. $COLOR_RESET"
+				exit_code=1
+				break
+			}
+
+			[ -d "$INSTALL_PREFIX" ] || {
+				echo "${COLOR_RED}Error: install directory $INSTALL_PREFIX can not be found. $COLOR_RESET"
+				exit_code=1
+				break
+			}
+			;;
+		-b|--build-prefix)
+			shift
+			BUILD_PREFIX="$1"
+			[ -z "$BUILD_PREFIX" ] && {
+				echo "${COLOR_RED}Error: missing argument <path> after $arg. $COLOR_RESET"
+				exit_code=1
+				break
+			}
+
+			[ -d "$BUILD_PREFIX" ] || {
+				echo "${COLOR_RED}Error: build directory $BUILD_PREFIX can not be found. $COLOR_RESET"
+				exit_code=1
+				break
+			}
+			;;
 		*)
 			# parse commands
 			process_commands "$@" || exit_code=$?