Source

sulu-ocaml-core / build-and-install

Full commit
#!/usr/bin/env bash
set -u -o pipefail

. build-common.sh

function usage {
    echo "usage: ${BASH_SOURCE[0]} [-j <JOBS>] [tree]"
    echo "         -h, --help       Display this help"
    echo "         -j N             Run at most N jobs in parallel"
    echo "         -u               Don't install, uninstall"
}

configflags="
 enable-linux
disable-linux
 enable-posix-timers
disable-posix-timers
"

opts=
if getopt -V | grep -q enhanced; then
  opts=$(getopt -n ${BASH_SOURCE[0]} -o "j:uh" \
                -l "jobs:,help,$(echo -n $configflags | tr ' ' ,)" -- "$@")
else
  opts=$(getopt "j:uh" $*)
fi

if [[ $? != 0 ]]; then usage; exit 1; fi

set -e
eval set -- "$opts"

linuxflag=
timerflag=
makeflags=""
configopts=( )
uninstall=false
while true; do
    case "$1" in
        -h|--help) usage; exit 0;;
        -j|--jobs) makeflags="$makeflags -j $2";      shift 2;;
        -u) uninstall=true; shift;;
        --enable-linux)  linuxflag="--enable-linux";  shift  ;;
        --disable-linux) linuxflag="--disable-linux"; shift  ;;
        --enable-posix-timers)  timerflag="--enable-posix-timers";  shift ;;
        --disable-posix-timers) timerflag="--disable-posix-timers"; shift ;;
        --) shift; break ;;
         *) configopts+=( "$1" )
    esac
done

tree=""
case $# in
    0) tree="."  ;;
    1) tree="$1" ;;
    *) echo "too many anonymous arguments: $@"; exit 1 ;;
esac

if $uninstall; then
  rev_build_order=( )
  for ((i=${#build_order[@]}-1; i>=0; i--)); do
      rev_build_order+=( "${build_order[$i]}" )
  done
  build_order=( "${rev_build_order[@]}" )
fi

cd "$tree"

function findlibname {
    local dir="$1"
    (grep FindlibParent: "$dir"/oasis.sh || grep FindlibName: "$dir"/oasis.sh) \
    | awk '{print $2}' | head -1
}

function uninstall {
    local lib=$(findlibname "$1")
    ocamlfind remove "$lib" || true;
}

for dir in "${build_order[@]}"; do
  echo "------ base/$dir ------"
  pushd base/$dir
  optflags=

  if $uninstall; then
    uninstall .
    popd; continue
  fi

  case $dir in
      core|core/extended) optflags="$linuxflag $timerflag" ;;
      async/scheduler)    optflags="$linuxflag" ;;
      async) # horrible hack (avoids rebuilding async_{core,scheduler,extra})
          src=$(pwd)
          dest=$(mktemp -d -t ocaml-core-async.XXXXXX)
          mkdir -p "$dest/base/async"
          cp -rp ../../build-common.sh "$dest/"
          cp -rp lib oasis.sh "$dest/base/async/"
          cd $dest/base/async
          trap "rm -r '$dest'" EXIT
          ;;
  esac
  ./oasis.sh
  ./configure $optflags "${configopts[@]:+${configopts[@]}}"
  make build BUILDFLAGS="$makeflags"
  uninstall .
  make install

  case $dir in
      async) # copy the generated setup.ml and setup.data back
        cp -rp "$dest"/base/async/{configure,Makefile,setup.ml,setup.data,myocamlbuild.ml} "$src"/
      ;;
  esac
  popd

done