ocaml-core / build-and-install

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

function usage {
    echo "usage: ${BASH_SOURCE[0]} [-s] [-v] [-j <JOBS>]"
    echo "         -h, --help       Display this help"
    echo "         -s, --sudo       Install with sudo"
    echo "         -v, --verbose    Log to the terminal"
    echo "         -j N             Run at most N jobs in parallel"
    echo "         -u               Don't install, uninstall"
}

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

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

eval set -- "$opts"

makeflags=""
uninstall=false
use_sudo=false
verbose=false
while true; do
    case "$1" in
        -h|--help) usage; exit 0;;
        -j|--jobs) makeflags="$makeflags -j $2";      shift 2;;
        -u)        uninstall=true;                    shift  ;;
        -s|--sudo) use_sudo=true;                     shift  ;;
        -v|--verbose) verbose=true    ;               shift  ;;
        --) shift; break ;;
         *) echo "unexpected flag '$1'"; usage; exit 1 ;;
    esac
done

if [[ $# -gt 0 ]]; then echo "unexpected arguments $@"; usage; exit 1; fi

source $(dirname -- "$0")/build-common.sh
root="$HERE";
cd "$root"

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

function maybe_sudo {
    if $use_sudo; then sudo "$@"; else "$@"; fi
}

function maybe_verbose {
    if $verbose; then "$@"; else
        local ret=0
        local output=''
        output=$("$@" 2>&1)
        ret=$?
        if [[ $ret -ne 0 ]]; then
            echo "$output"
            return $ret
        fi
    fi
}

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

function die { echo "*** ERROR: $1"; exit 1; }

for dir in "${build_order[@]}"; do
  echo "=> $dir"

  pushd $dir >/dev/null

  case $dir in
      *-$core_version) ;; # no need to do anything for released packages
      *) maybe_verbose ./oasis.sh || die "Error generating $dir/_oasis";;
  esac

  lib=$(findlibname '_oasis')
  if [[ $? -ne 0 ]]; then
      die "Couldn't figure out the library name from $dir/_oasis"
  fi

  if ocamlfind query "$lib" 2>/dev/null >/dev/null; then
      echo "   * uninstalling"
      maybe_verbose maybe_sudo ocamlfind remove "$lib" \
          || die "Couldn't uninstall $lib"
  fi

  if ! $uninstall; then

      echo "   * configuring"
      maybe_verbose ./configure \
          || die "Couldn't configure $lib"

      echo "   * compiling"
      maybe_verbose make build BUILDFLAGS="$makeflags" \
          || die "Couldn't compile $lib"

      echo "   * installing"
      maybe_verbose maybe_sudo make install \
          || die "Couldn't install $lib"

  fi
  popd >/dev/null
done

echo "All finished!"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.