1#!/bin/sh
2#
3#    Copyright (C) 2010, Cisco Systems Inc.
4#
5#    This program is free software; you can redistribute it and/or modify
6#    it under the terms of the GNU General Public License as published by
7#    the Free Software Foundation; either version 2 of the License, or
8#    (at your option) any later version.
9#
10#    This program is distributed in the hope that it will be useful,
11#    but WITHOUT ANY WARRANTY; without even the implied warranty of
12#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#    GNU General Public License for more details.
14#
15#    You should have received a copy of the GNU General Public License along
16#    with this program; if not, write to the Free Software Foundation, Inc.,
17#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18#
19# Garrett Cooper, January 2010
20#
21
22# Temporary directory setup.
23#
24# $TMPDIR - base temporary directory; see mktemp(1) for more details.
25setup_env() {
26	testscript_dir=$(readlink -f "${0%/*}")
27	abspath=$(readlink -f "$testscript_dir/../../scripts/abspath.sh")
28	unset vars
29	for i in tmp_builddir tmp_destdir tmp_prefix tmp_srcdir; do
30		eval "if $i=\$(mktemp -d) ; then vars=\"\$vars \$${i}\"; else echo ${0##*/}: failed to create temporary directory for $i; false; fi"
31	done
32	trap cleanup EXIT
33	cat <<EOF
34========================================
35SUMMARY FOR: $1 Scenario
36========================================
37builddir 	-> $tmp_builddir
38destdir		-> $tmp_destdir
39prefix		-> $tmp_prefix
40srcdir		-> $tmp_srcdir
41========================================
42EOF
43
44}
45
46# Clean up the generated directories.
47#
48# $CLEAN_TEMPFILES - !1 -> don't clean.
49#                  - 1 -> clean.
50cleanup() {
51	if [ "x${CLEAN_TEMPFILES:-0}" = x1 -a "x${TEST_PASSED:-0}" = x1 ] ; then
52		cd /
53		trap '' EXIT
54		rm -Rf $vars
55	fi
56}
57
58# Pull from CVS.
59cvs_pull() {
60
61	export CVSROOT=${CVSROOT:-:pserver:anonymous@ltp.cvs.sf.net:/cvsroot/ltp}
62
63	if ( [ -f ~/.cvspass ] || touch ~/.cvspass ) ; then
64		echo "$CVSROOT" | grep -qv '^:pserver:' || cvs -d$CVSROOT login
65		cvs -z3 export -f -r HEAD ltp && srcdir="$PWD/ltp"
66	fi
67
68}
69
70# Pull from git.
71git_pull() {
72	GIT_REPO=${GIT_REPO:-ltp-dev}
73	git archive master \
74	    --remote=git://ltp.git.sourceforge.net/gitroot/ltp/$GIT_REPO . | tar -xf -
75	srcdir="$PWD"
76}
77
78#
79# Pull a fresh copy of the repository for building.
80#
81# 1 - pull method (currently only cvs is supported, but git may be supported
82#     in the future).
83# 2 - source directory.
84#
85# $LTP_PATCH - -p0 based patch to apply after the pull is complete for
86# precommit testing. THE PATH TO THE PATCH MUST BE ABSOLUTE!
87#
88pull_scm() {
89	cd "$2" && eval "${1}_pull"
90	if [ "x$LTP_PATCH" != x ] ; then
91		patch -p0 < "$LTP_PATCH"
92	fi
93	safe_rm="$srcdir/scripts/safe_rm.sh"
94}
95
96# Verify that clean is sane so peoples' rootfs' / host systems don't get
97# cleaned out by accident [sorry Mitani-san :(...].
98#
99# 1 - source directory
100# 2 - build directory (where to deposit the files produced by configure).
101# 3 - the DESTDIR.
102clean_is_sane() {
103	set +e; for i in distclean ac-maintainer-clean; do
104		output=$(make ${2:+-C "$2"} \
105		     ${1:+-f "$1/Makefile" "top_srcdir=$1"} \
106		     ${2:+"top_builddir=$2"} \
107		     ${MAKEFLAGS} \
108		     DESTDIR="$4" \
109		     RM="$safe_rm" $i)
110		if echo "$output" | egrep 'ERROR : not removing .+ to avoid removing root directory'; then
111			return $?
112		fi
113	done
114}
115
116# Configure a source tree for building.
117#
118# 1 - source directory
119# 2 - build directory (where to deposit the files produced by configure).
120# 3 - the argument to pass to --prefix.
121configure() {
122
123	if [ "x$2" != x ] ; then
124		test -d "$2" || mkdir -p "$2"
125	fi
126
127	make ${1:+-C "$1"} \
128	     ${MAKEFLAGS} \
129	     autotools
130
131	if [ -d "${2:-}" ] ; then
132		cd "$2"
133	fi
134
135	"${1:-.}/configure" ${3:+--prefix=$("$abspath" "$3")}
136
137}
138
139# Build a source tree.
140#
141# 1 - source directory
142# 2 - build directory
143#
144# $MAKEFLAGS - flags to pass directly to gmake(1).
145#
146build() {
147	make ${2:+-C "$2"} \
148	     ${1:+-f "$1/Makefile" "top_srcdir=$1"} \
149	     ${2:+"top_builddir=$2"} \
150	     ${MAKEFLAGS} \
151	     all
152}
153
154# Install the binaries and scripts from a build tree.
155#
156# 1 - source directory
157# 2 - build directory
158# 3 - DESTDIR
159#
160# $MAKEFLAGS - flags to pass directly to gmake(1).
161#
162install_ltp() {
163	make ${2:+-C "$2"} \
164	     ${1:+-f "$1/Makefile" "top_srcdir=$1"} \
165	     ${2:+"top_builddir=$2"} \
166	     ${3:+"DESTDIR=$3"} \
167	     ${MAKEFLAGS} \
168	     install
169}
170
171# Run a test on the installed tree.
172#
173# 1 - install directory for tree, e.g. $(DESTDIR)/$(prefix)
174test_ltp() {
175
176	installdir=$(readlink -f "${1:-.}")
177	test_ltp="$installdir/test_ltp.sh"
178
179	# XXX (garrcoop): I haven't tracked down the root cause for the
180	# issue, but some versions of sed combined with some terminal
181	# configurations cause sed to block waiting for EOF on certain
182	# platforms when executing runltp. Thus, we should effectively close
183	# /dev/stdin before executing runltp via execltp.
184	cat <<EOF > "$test_ltp"
185#!/bin/sh
186
187export AR="$AR"
188export ARFLAGS="$ARFLAGS"
189export CC="$CC"
190export CFLAGS="$CFLAGS"
191export CXX="$CXX"
192export CXXFLAGS="$CXXFLAGS"
193export LD="$LD"
194export LDFLAGS="$LDFLAGS"
195export NM="$NM"
196
197# Optional variables required for some of the testcases.
198for i in AR ARFLAGS CC CFLAGS CXX CXXFLAGS LD LDFLAGS NM; do
199	eval "[ \"x\\\$\$i\" = x ] && unset \$i"
200done
201
202export LTPROOT="$LTPROOT"
203export PATH="\$PATH:$installdir/bin:$PATH"
204
205execltp -l "$installdir" -vv < /dev/null
206EOF
207
208	pre_cmd=
209
210	if [ "x$(id -ru)" != "x0" ] ; then
211
212		# Make sure that environment is translated properly with sudo.
213		if type sudo > /dev/null && sudo -- sh -c 'exit 0' ; then
214			pre_cmd='sudo --'
215		elif type su > /dev/null && groups | grep wheel ; then
216			pre_cmd='su -c'
217		fi
218
219		if [ "x$pre_cmd" != x ] ; then
220			echo "chown -Rf $(id -ru) \"$installdir\"" >> "$test_ltp"
221		fi
222
223	fi
224
225	echo "${0##*/}: will execute test_ltp via ${pre_cmd:+$pre_cmd }$test_ltp"
226	chmod +x "$test_ltp"
227	# XXX (garrcoop): uncommenting the following would work around a
228	# craptacular `bug' with libpam where it outputs the Password: prompt
229	# to /dev/stdout instead of /dev/tty, but it also dumps all output from
230	# runltp, etc to the console instead of a log -- therefore if you do
231	# cat all output to a log, just tail -f it and enter in your password
232	# when necessary.
233	#${pre_cmd} "${test_ltp}" > /dev/tty 2>&1 && TEST_PASSED=1
234	${pre_cmd} "${test_ltp}" && TEST_PASSED=1
235
236}
237
238if [ "x$I_HAVE_READ_THE_README_WARNING" != x1 ] ; then
239	echo 'WARNING: Read '${0%/*}'/README before executing this script ('${0##*/}')!' >&2
240	false
241fi
242
243set -x
244