+ if [[ -n "$TESTS_ENVIRONMENT" ]]; then
+ OLD_TESTS_ENVIRONMENT=$TESTS_ENVIRONMENT
+ TESTS_ENVIRONMENT=
+ fi
+}
+
+pop_TESTS_ENVIRONMENT ()
+{
+ TESTS_ENVIRONMENT=
+ if [[ -n "$OLD_TESTS_ENVIRONMENT" ]]; then
+ TESTS_ENVIRONMENT=$OLD_TESTS_ENVIRONMENT
+ OLD_TESTS_ENVIRONMENT=
+ fi
+}
+
+safe_pushd ()
+{
+ pushd $1 &> /dev/null ;
+
+ if $VERBOSE -a test -n "$BUILD_DIR"; then
+ echo "BUILD_DIR=$BUILD_DIR"
+ fi
+}
+
+safe_popd ()
+{
+ local directory_to_delete=`pwd`
+ popd &> /dev/null ;
+ if [ $? -eq 0 ]; then
+ if [[ "$top_srcdir" == "$directory_to_delete" ]]; then
+ die "We almost deleted top_srcdir($top_srcdir), programmer error"
+ fi
+
+ rm -r -f "$directory_to_delete"
+ fi
+}
+
+make_valgrind ()
+{
+ if [[ "$VENDOR_DISTRIBUTION" == 'darwin' ]]; then
+ make_darwin_malloc
+ return
+ fi
+
+ # If the env VALGRIND_COMMAND is set then we assume it is valid
+ local valgrind_was_set=false
+ if [[ -z "$VALGRIND_COMMAND" ]]; then
+ setup_valgrind_command
+ if [[ -n "$VALGRIND_COMMAND" ]]; then
+ valgrind_was_set=true
+ fi
+ else
+ valgrind_was_set=true
+ fi
+
+ # If valgrind_was_set is set to no we bail
+ if ! $valgrind_was_set; then
+ echo 'valgrind was not present'
+ return 1
+ fi
+
+ # If we are required to run configure, do so now
+ run_configure_if_required
+
+ push_TESTS_ENVIRONMENT
+
+ if [[ -f 'libtool' ]]; then
+ TESTS_ENVIRONMENT="$LIBTOOL_COMMAND $VALGRIND_COMMAND"
+ else
+ TESTS_ENVIRONMENT="$VALGRIND_COMMAND"
+ fi
+
+ make_target 'check' || return 1
+
+ pop_TESTS_ENVIRONMENT
+}
+
+make_install_system ()
+{
+ local INSTALL_LOCATION=$(mktemp -d /tmp/XXXXXXXXXX)
+ push_PREFIX_ARG $INSTALL_LOCATION
+
+ if [ ! -d $INSTALL_LOCATION ] ; then
+ die "ASSERT temp directory not found '$INSTALL_LOCATION'"
+ fi
+
+ run_configure #install_buid_dir
+
+ push_TESTS_ENVIRONMENT
+
+ make_target 'install'
+
+ make_target 'installcheck'
+
+ make_target 'uninstall'
+
+ pop_TESTS_ENVIRONMENT
+ pop_PREFIX_ARG
+
+ rm -r -f $INSTALL_LOCATION
+ make 'distclean'
+
+ if [ -f 'Makefile' ]; then
+ die "ASSERT Makefile should not exist"
+ fi
+
+ safe_popd
+}
+
+make_darwin_malloc ()
+{
+ run_configure_if_required
+
+ old_MallocGuardEdges=$MallocGuardEdges
+ MallocGuardEdges=1
+ old_MallocErrorAbort=$MallocErrorAbort
+ MallocErrorAbort=1
+ old_MallocScribble=$MallocScribble
+ MallocScribble=1
+
+ make_check
+
+ MallocGuardEdges=$old_MallocGuardEdges
+ MallocErrorAbort=$old_MallocErrorAbort
+ MallocScribble=$old_MallocScribble
+}
+
+snapshot_check ()
+{
+ if [ -n "$BOOTSTRAP_SNAPSHOT_CHECK" ]; then
+ assert_file "$BOOTSTRAP_SNAPSHOT_CHECK" 'snapshot check failed'
+ fi
+}
+
+# This will reset our environment, and make sure built files are available.
+make_for_snapshot ()
+{
+ # Make sure it is clean
+ make_maintainer_clean
+
+ run_configure
+ make_target 'dist'
+ make_target 'distclean'
+
+ # We should have a configure, but no Makefile at the end of this exercise
+ assert_no_file 'Makefile'
+ assert_exec_file 'configure'
+
+ snapshot_check
+}
+
+# If we are locally testing, we should make sure the environment is setup correctly
+check_for_jenkins ()
+{
+ if ! $jenkins_build_environment; then
+ echo "Not inside of jenkins"
+
+ if [ -f 'configure' ]; then
+ make_maintainer_clean
+ fi
+
+ if $BOOTSTRAP_SNAPSHOT; then
+ make_for_snapshot
+ fi
+ fi
+}
+
+make_for_continuus_integration ()
+{
+ # Setup the environment if we are local
+ check_for_jenkins
+
+ # No matter then evironment, we should not have a Makefile at this point
+ assert_no_file 'Makefile'
+
+ # Platforms which require bootstrap should have some setup done before we hit this stage.
+ # If we are building locally, skip this step, unless we are just testing locally.
+ if $BOOTSTRAP_SNAPSHOT; then
+ snapshot_check
+ else
+ # If we didn't require a snapshot, then we should not have a configure
+ assert_no_file 'configure'
+
+ run_autoreconf
+ fi
+
+ assert_no_file 'Makefile' 'Programmer error, Makefile existed where build state should have been clean'
+
+ case $HOST_OS in
+ *-fedora-*)
+ run_configure
+
+ assert_exec_file 'configure'
+ assert_file 'Makefile'
+
+ # make rpm includes "make distcheck"
+ if [[ -f rpm.am ]]; then
+ make_rpm
+ elif [[ -d rpm ]]; then
+ make_rpm
+ else
+ make_distcheck
+ fi
+
+ assert_exec_file 'configure'
+ assert_file 'Makefile'
+
+ make_install_system
+ ;;
+ *-precise-*)
+ run_configure
+
+ assert_exec_file 'configure'
+ assert_file 'Makefile'
+
+ make_distcheck
+
+ assert_exec_file 'configure'
+ assert_file 'Makefile'
+
+ make_valgrind
+
+ assert_exec_file 'configure'
+ assert_file 'Makefile'
+
+ make_install_system
+ ;;
+ *)
+ run_configure
+ make_all
+ ;;
+ esac
+
+ make_maintainer_clean
+
+ safe_popd
+}
+
+make_gdb ()
+{
+ run_configure_if_required
+
+ if command_exists gdb; then
+
+ push_TESTS_ENVIRONMENT
+
+ # Set ENV GDB_COMMAND
+ if [[ -z "$GDB_COMMAND" ]]; then
+ setup_gdb_command
+ fi
+
+ if [ -f 'libtool' ]; then
+ TESTS_ENVIRONMENT="$LIBTOOL_COMMAND $GDB_COMMAND"
+ else
+ TESTS_ENVIRONMENT="$GDB_COMMAND"
+ fi
+
+ make_target check
+
+ if [ -f 'gdb.txt' ]; then
+ rm 'gdb.txt'
+ fi
+
+ pop_TESTS_ENVIRONMENT
+
+ if [ -f '.gdb_history' ]; then
+ rm '.gdb_history'
+ fi
+ else
+ echo 'gdb was not present'
+ return 1
+ fi
+}
+
+# $1 target to compile
+# $2 to die, or not to die, based on contents
+function make_target ()
+{
+ if [[ -z "$1" ]]; then
+ die "Programmer error, no target provided for make"
+ fi
+
+ if [ ! -f 'Makefile' ]; then
+ die "Programmer error, make was called before configure"
+ run_configure
+ fi
+
+ if test -n "$TESTS_ENVIRONMENT" -a $VERBOSE; then
+ echo "TESTS_ENVIRONMENT=$TESTS_ENVIRONMENT"
+ fi
+
+ if [[ -z "$MAKE" ]]; then
+ die "MAKE was not set"
+ fi
+
+ if [[ -n "$2" ]]; then
+ run $MAKE $1 || return 1