build.sh: Add getopts and new features + update travis.yml

* Add feature to pass compiler (-c switch). This will be used for
cross-compilation builds, but it can be handy for testing different
compilers than default system one in local development.

* Add feature to to pass sysroot directory / prefix (-p switch).

* Use getopts.

* DRY.

Signed-off-by: Petr Vorel <pvorel@suse.cz>
diff --git a/build.sh b/build.sh
index 6441851..6b03605 100755
--- a/build.sh
+++ b/build.sh
@@ -9,14 +9,13 @@
 
 set -e
 
-PREFIX="$HOME/ltp-install"
-
-CONFIGURE_OPTS_IN_TREE="--with-open-posix-testsuite --with-realtime-testsuite --prefix=$PREFIX"
-
+DEFAULT_PREFIX="$HOME/ltp-install"
+DEFAULT_BUILD="build_native"
+CONFIGURE_OPTS_IN_TREE="--with-open-posix-testsuite --with-realtime-testsuite"
 # TODO: open posix testsuite is currently broken in out-tree-build. Enable it once it's fixed.
 CONFIGURE_OPTS_OUT_TREE="--with-realtime-testsuite"
-
 MAKE_OPTS="-j$(getconf _NPROCESSORS_ONLN)"
+CC=
 
 build_32()
 {
@@ -26,7 +25,7 @@
 
 build_native()
 {
-	echo "===== native in tree build into $PREFIX ====="
+	echo "===== native in-tree build into $PREFIX ====="
 	build_in_tree
 }
 
@@ -43,15 +42,7 @@
 	make autotools
 
 	cd $build
-	echo "=== configure ==="
-	if ! $tree/configure $CONFIGURE_OPTS_OUT_TREE; then
-		echo "== ERROR: configure failed, config.log =="
-		cat config.log
-		exit 1
-	fi
-
-	echo "== include/config.h =="
-	cat include/config.h
+	run_configure $tree/configure $CONFIGURE_OPTS_OUT_TREE CC="$CC"
 
 	make $make_opts
 	make $make_opts DESTDIR="$PREFIX" SKIP_IDCHECK=1 install
@@ -62,15 +53,7 @@
 	echo "=== autotools ==="
 	make autotools
 
-	echo "=== configure ==="
-	if ! ./configure $CONFIGURE_OPTS_IN_TREE $@; then
-		echo "== ERROR: configure failed, config.log =="
-		cat config.log
-		exit 1
-	fi
-
-	echo "== include/config.h =="
-	cat include/config.h
+	run_configure ./configure $CONFIGURE_OPTS_IN_TREE CC="$CC" --prefix=$PREFIX $@
 
 	echo "=== build ==="
 	make $MAKE_OPTS
@@ -79,31 +62,65 @@
 	make $MAKE_OPTS install
 }
 
+run_configure()
+{
+	local configure=$1
+	shift
+
+	echo "=== configure $configure $@ ==="
+	if ! $configure $@; then
+		echo "== ERROR: configure failed, config.log =="
+		cat config.log
+		exit 1
+	fi
+
+	echo "== include/config.h =="
+	cat include/config.h
+}
+
 usage()
 {
 	cat << EOF
 Usage:
-$0 [ BUILD_TYPE ]
-$0 -h|--help|help
+$0 [ -c CC ] [ -p DIR ] [ -t TYPE ]
+$0 -h
 
 Options:
--h|--help|help  Print this help
+-h       Print this help
+-c CC    Define compiler (\$CC variable)
+-p DIR   Change installation directory. For in-tree build is this value passed
+         to --prefix option of configure script. For out-of-tree build is this
+         value passed to DESTDIR variable (i.e. sysroot) of make install
+         target, which means that LTP will be actually installed into
+         DIR/PREFIX (i.e. DIR/opt/ltp).
+         Default for in-tree build: '$DEFAULT_PREFIX'
+         Default for out-of-tree build: '$DEFAULT_PREFIX/opt/ltp'
+-t TYPE  Specify build type, default: $DEFAULT_BUILD
 
 BUILD TYPES:
-32      32-bit in-tree build
-native  native in-tree build
-out     out-of-tree build
-
-Default build is native in-tree build.
+32       32-bit in-tree build
+native   native in-tree build
+out      out-of-tree build
 EOF
 }
 
-case "$1" in
-	-h|--help|help) usage; exit 0;;
-	32) build="build_32";;
-	out) build="build_out_tree";;
-	*) build="build_native";;
-esac
+PREFIX="$DEFAULT_PREFIX"
+build="$DEFAULT_BUILD"
+
+while getopts "c:hp:t:" opt; do
+	case "$opt" in
+	c) CC="$OPTARG";;
+	h) usage; exit 0;;
+	p) PREFIX="$OPTARG";;
+	t) case "$OPTARG" in
+		32) build="build_32";;
+		native) build="build_native";;
+		out) build="build_out_tree";;
+		*) echo "Wrong build type '$OPTARG'" >&2; usage; exit 1;;
+		esac;;
+	?) usage; exit 1;;
+	esac
+done
 
 cd `dirname $0`
 $build