Skip to content
Snippets Groups Projects
build_release.sh 4.41 KiB
Newer Older
Bergmann89's avatar
Bergmann89 committed
#!/bin/bash

# set -x #enable debug

SCRIPTPATH=$(readlink -f $0)
SCRIPTNAME=$(basename $SCRIPTPATH)
SCRIPTDIR=$(dirname $SCRIPTPATH)
git="git"
HAS_GIT=true
HEADER_DIR="$SCRIPTDIR/header"
DOCU_DIR="$SCRIPTDIR/doc"
INC_MAJOR=false
INC_MINOR=false
INC_BUGFIX=false

function log()
{
  echo "$@" >&2
}

function cleaupAndExit()
{
  if $HAS_GIT && [[ -n "$TAGNAME" ]] && [[ $1 -ne 0 ]]; then
    log "remove git tag $TAGNAME"
    $git tag -d $TAGNAME
  fi
  exit $1
}

function printHelp()
{
  printf "script to build libShaderFile release
Usage:
  $SCRIPTNAME [parameter]
  [] optional parameters
  () required parameters

Parameter:
  --major | -m
    increment major version and reset the minor version before creating git tag

  --minor | -n
    increment major version and reset the bugfix version before creating git tag

  --bufgix | -b
    increment bugfix version before creating git tag

  --help | -h | -?
    print this help
  "
}

function getLastVersion()
{
  POS="HEAD"
  TAG=$($git describe --tags --abbrev=0 $POS 2>/dev/null)
  RET=$?
  while [[ $RET -eq 0 ]]; do 
    if [[ "$TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
      echo $(echo $TAG | egrep -o "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")
      return 0
    fi
    POS=$TAG"^"
    TAG=$($git describe --tags --abbrev=0 $POS 2>/dev/null)
    RET=$?
  done
  return 1
}

function incVersion()
{
  read -r -a PARTS <<< $(echo "$1" | tr "." " ")
  if $INC_MAJOR; then
    ((PARTS[0]++))
    PARTS[1]=0
  fi
  if $INC_MINOR; then
    ((PARTS[1]++))
    PARTS[2]=0
  fi
  if $INC_BUGFIX; then
    ((PARTS[2]++))
  fi
  ((PARTS[3]++))
  local tmp="${PARTS[@]}"
  echo "${tmp// /.}"
}

function addGitTag()
{
  local INC_VERSION=true
  log "create git version tag"
  CURRENT=$(getLastVersion)
  if [ $? -ne 0 ]; then
    CURRENT="0.0.0.0"
    INC_VERSION=false
  fi
  local tmp="v$CURRENT"
  LINES=$($git log --pretty=oneline $tmp..HEAD)
  if [ $? -eq 0 ]; then 
    DIFF=$(echo $LINES | wc -l)
    if [ $DIFF -eq 0 ] || [ -z "$LINES" ]; then
      log "current commit already has a version tag: $tmp"
      TAGNAME=$tmp
      return 0
    fi
  fi
  if $INC_VERSION; then
    NEWVERSION=$(incVersion $CURRENT)
  else
    NEWVERSION="$CURRENT"
  fi
  log "current version: $CURRENT"
  log "new version:     $NEWVERSION"
  local tmp="v$NEWVERSION"
  $git tag $tmp
  if [[ $? -ne 0 ]]; then
    log "unable to create version tag: exit"
    cleaupAndExit 100
  fi
  TAGNAME=$tmp
  return 0
}

function BuildConfig()
{
  CONFIG=$1
  POSTFIX=$2
  FILEEXT=$3

  printf "\n== build project $CONFIG ==\n"
  lazbuild --build-mode=$CONFIG --build-all --verbose $SCRIPTDIR/libTextSuite.lpi
  if [ $? -ne 0 ]; then
    echo "build failed! exit."
    cleaupAndExit 1
  fi

  ZIPDIR="bin/$POSTFIX"
  BINDIR="$ZIPDIR"
  ZIPPATH="$SCRIPTDIR/libTextSuite-$POSTFIX.zip"
  if [ -n "$TAGNAME" ]; then
    mkdir -p "$SCRIPTDIR/$TAGNAME/"
    ZIPPATH="$SCRIPTDIR/$TAGNAME/libTextSuite-$POSTFIX.zip"
  fi
  BINNAME="libTextSuite-$POSTFIX$FILEEXT"
  if [ ! -f $BINNAME ]; then
    echo "file not found: $EXENAME"
    cleaupAndExit 2
  fi

  mkdir -p $BINDIR
  mv "$BINNAME" "$BINDIR/" || { log "unable to copy linked binary"; cleaupAndExit 3; }
  # use this to copy data folder instead of data archive: cp -R --preserve=links data $BINDIR || { log "unable to copy data folder"; cleaupAndExit 4; }
  cp -r "$HEADER_DIR" "$BINDIR" || { log "unable to copy header files"; cleaupAndExit 4; }
  cp -r "$DOCU_DIR" "$BINDIR/docu" || { log "unable to copy documentation files"; cleaupAndExit 5; }
  
  pushd $ZIPDIR 
  rm -rf $ZIPPATH
  zip -r $ZIPPATH ./* || { log "unable to create zip archive"; cleaupAndExit 6; }
  popd
}

while [[ $# -gt 0 ]]; do
  case $1 in
    "--major" | "-m")
      INC_MAJOR=true
      ;;

    "--minor" | "-n")
      INC_MINOR=true
      ;;

    "--bufgix" | "-b")
      INC_BUGFIX=true
      ;;
 
    "--help" | "-h" | "-?")
      printHelp
      cleaupAndExit 0
      ;;

    *)
      echo "invalid parameter: $1"
      echo "use --help to get further information"
      ;;

  esac
  shift
done

if $HAS_GIT; then
  GIT_CHANGED=$($git status --untracked-files=all --verbose --porcelain)
  if [[ -n "$GIT_CHANGED" ]]; then
    log "git has uncommited changes. please commit before building a release"
    cleaupAndExit 101
  fi
  addGitTag
fi

BuildConfig "Win32Release" "i386-win32" ".dll"
BuildConfig "Win64Release" "x86_64-win64" ".dll"
BuildConfig "Linux32Release" "i386-linux" ".so"
BuildConfig "Linux64Release" "x86_64-linux" ".so"

cleaupAndExit 0