#!/bin/sh
#**************************************************************************
#*                                                                        *
#*                                 OCaml                                  *
#*                                                                        *
#*          Damien Doligez, projet Gallium, INRIA Rocquencourt            *
#*                                                                        *
#*   Copyright 2014 Institut National de Recherche en Informatique et     *
#*     en Automatique.                                                    *
#*                                                                        *
#*   All rights reserved.  This file is distributed under the terms of    *
#*   the GNU Lesser General Public License version 2.1, with the          *
#*   special exception on linking described in the file LICENSE.          *
#*                                                                        *
#**************************************************************************

# This script is run on our continuous-integration servers to recompile
# from scratch and run the test suite.

# arguments:
# 1. architecture: bsd, macos, linux, cygwin, mingw, mingw64, msvc, msvc64
# 2. directory in which to build (trunk, 4.02, etc)
#    If this is "." then build in the Jenkins-checked out directory
#    Otherwise, go to $HOME/jenkins-workspace/<dir> and check out a
#    fresh copy of the branch, then build there.
# 3. options:
#    -conf configure-option  add configure-option to configure cmd line
#    -patch1 file-name       apply patch with -p1
#    -no-native              do not build "opt" and "opt.opt"

error () {
  echo "$1" >&2
  exit 3
}

quote1 () {
  printf "'%s'" "`printf %s "$1" | sed -e "s/'/'\\\\\\\\''/g"`";
}

#########################################################################
# be verbose
set -x

#########################################################################
# Save the current directory (on cygwin, /etc/profile changes it)
jenkinsdir="$(pwd)"

#########################################################################
# "Parse" mandatory command-line arguments.

arch="$1"
branch="$2"
shift 2

#########################################################################
# If we are called from a Windows batch script, we must set up the
# Unix environment variables (e.g. PATH).

case "$arch" in
  bsd|macos|linux) ;;
  cygwin|mingw|mingw64)
    . /etc/profile
    . "$HOME/.profile"
  ;;
  msvc)
    . /etc/profile
    . "$HOME/.profile"
    . "$HOME/.msenv32"
  ;;
  msvc64)
    . /etc/profile
    . "$HOME/.profile"
    . "$HOME/.msenv64"
  ;;
  *) error "unknown architecture: $arch";;
esac

#########################################################################

# be verbose and stop on error
set -ex

#########################################################################
# set up variables

# default values
make=make
instdir="$HOME/ocaml-tmp-install"
configure=unix

case "$branch" in
  .) workdir="$jenkinsdir"
     docheckout=false
     ;;
  *) workdir="$HOME/jenkins-workspace/$branch"
     docheckout=true
     ;;
esac

case "$arch" in
  bsd) make=gmake ;;
  macos) ;;
  linux) ;;
  cygwin) ;;
  mingw)
    instdir=/cygdrive/c/ocamlmgw
    configure=nt
  ;;
  mingw64)
    instdir=/cygdrive/c/ocamlmgw64
    configure=nt
  ;;
  msvc)
    instdir=/cygdrive/c/ocamlms
    configure=nt
  ;;
  msvc64)
    instdir=/cygdrive/c/ocamlms64
    configure=nt
  ;;
  *) error "unknown architecture: $arch";;
esac

#########################################################################
# Go to the right directory

pwd
cd "$workdir"

#########################################################################
# parse optional command-line arguments (has to be done after the "cd")

confoptions=""
make_native=true
while [ $# -gt 0 ]; do
  case $1 in
    -conf) confoptions="$confoptions `quote1 "$2"`"; shift;;
    -patch1) patch -f -p1 <"$2"; shift;;
    -no-native) make_native=false;;
    *) error "unknown option $1";;
  esac
  shift
done

#########################################################################
# Do the work

# Tell gcc to use only ASCII in its diagnostic outputs.
export LC_ALL=C

$make distclean || :

if $docheckout; then
  git fetch origin
  git reset --hard FETCH_HEAD
fi

case $configure in
  unix) eval "./configure -prefix '$instdir' $confoptions";;
  nt)
    cp config/m-nt.h config/m.h
    cp config/s-nt.h config/s.h
    cp config/Makefile.$arch config/Makefile
  ;;
  *) error "internal error";;
esac

$make coldstart
$make core
$make coreboot
$make world
if $make_native; then
  $make opt
  $make opt.opt
fi
$make install

rm -rf "$instdir"
cd testsuite
$make all
