#!/bin/sh
#
# Copyright © 2011 Ian D. Romanick
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

function check_warnings
{
    bits=$1
    target=$2
    sha=$3

    grep warning: /tmp/build-master-${bits}-${target}.txt | sort -u \
	> /tmp/warn-master-${bits}-${target}.txt
    grep warning: /tmp/build-${sha}-${bits}-${target}.txt | sort -u \
	> /tmp/warn-${sha}-${bits}-${target}.txt

    echo "Warning changes in ${sha} (${bits}-bit, ${target}):"
    diff -ud /tmp/warn-master-${bits}-${target}.txt /tmp/warn-${sha}-${bits}-${target}.txt
}

src_dir=$1
bld_dir=$2
cwd=$(pwd)
shift 2

ARCH=$(uname -m)
if [ -d /opt/xorg-master-$ARCH ]; then
    prefix=/opt/xorg-master-$ARCH
elif [ -d /opt/xorg/xorg-master-$ARCH ]; then
    prefix=/opt/xorg/xorg-master-$ARCH
else
    echo "Installation directory does not exist."
    exit 1
fi

commits=/tmp/commit-list.$$.txt

cd $src_dir
echo origin/master > $commits
git log --oneline --reverse $* >> $commits
if [ $? -ne 0 ]; then
    echo FAIL
    exit 1
fi

cflags_warn="-Wall -Wextra -Wunsafe-loop-optimizations"
cflags_dbg="-ggdb3 -D_FORTIFY_SOURCE"
cflags_arch="-march=core2 -mcx16 -msahf -mfpmath=sse --param l1-cache-line-size=64 -mtune=native"

echo Build checking the following commits:
git log --oneline --reverse $*
echo

#BITS="32 64"
BITS="64"

while read l; do
    sha=$(echo $l | cut -d' ' -f1)
    log=$(echo $l | cut -d' ' -f2-)

    cd $src_dir
    echo "Checking out ${sha}:"
    git checkout $sha > /dev/null 2> /dev/null
    if [ $? -ne 0 ]; then
	echo FAIL
	exit 1
    fi
    echo

    for bits in $BITS; do
	if [ $bits -eq 32 ]; then
	    LIB=lib
	else
	    LIB=lib64
	fi

	for target in debug release; do
	    echo "Processing \"$log\" (${bits}-bit, $target)..."

	    if [ $target = debug ]; then
		cflags_opt="-O1"
		debug='--enable-debug'
	    else
		cflags_opt='-O3 -momit-leaf-frame-pointer'
		debug=''
	    fi

	    if [ "x$sha" = "xorigin/master" ]; then
		name=master-${bits}-${target}
	    else
		name=${sha}-${bits}-${target}
	    fi

	    cd $bld_dir
	    mkdir ${name}

	    cd ${name}
	    lndir -silent -withrevinfo $src_dir

	    export CFLAGS="-pipe $cflags_warn $cflags_opt $cflags_dbg $cflags_arch"
	    export CXXFLAGS="$CFLAGS"
	    export PKG_CONFIG_PATH="$prefix/$LIB/pkgconfig"
	    export ACLOCAL="aclocal -I $prefix/share/aclocal/"

	    echo Configuring...
	    ./autogen.sh --prefix="$prefix" \
		--libdir="$prefix/$LIB" \
		--disable-glw --disable-glut --disable-glu \
		--enable-dri \
		--enable-${bits}-bit \
		--with-dri-drivers=yes \
		--with-gallium-drivers="i915,r300,r600,nouveau,swrast" \
 		--enable-gallium-llvm \
		$debug \
		--enable-glx-tls \
		--enable-gles1 \
		--enable-gles2 > /tmp/configure-${name}.txt 2>&1
	    if [ $? -ne 0 ]; then
		echo FAIL
		exit 1
	    fi

	    echo Building...
	    make 2> /tmp/build-${name}.txt > /dev/null
	    if [ $? -ne 0 ]; then
		echo FAIL
		exit 1
	    else
		cd ..
		rm -rf ${name} &
	    fi
	    
	    echo Done.
	    echo
	done
    done
done < $commits

while read l; do
    sha=$(echo $l | cut -d' ' -f1)
    if [ "x$sha" != "xorigin/master" ] ; then
	for bits in $BITS; do
	    for target in debug release; do
		check_warnings $bits $target $sha
	    done
	done
    fi
done < $commits 2>&1 | tee /tmp/warning-changes.txt

wait
cd $cwd
#rm -f $commits
