#! /bin/bash
function compile() {
  set -e
  rm -f "$2".so
  faust $faustargs -a "$tooldir"/pythonmodule.cpp -o faustwrap.cpp "$1"
  cython3 -3 --cplus "$2".pyx
  opt="$(python3-config --cflags --ldflags | sed s/-Wstrict-prototypes//)"
  opt="$opt -shared -fPIC -lrt"
  opt="$opt -I$instdir/src/gx_head/engine -I$instdir/src/gx_head/engine/tabels -I$instdir/src/headers"
  opt="$opt -Wall -Wno-unused-function"
  #opt="$opt -Wno-unused-but-set-variable"
  opt="$opt -O3 -march=native -pipe -mmmx -msse4.2 -mfpmath=sse -ftree-loop-linear -ffinite-math-only -fno-math-errno -fno-signed-zeros -fstrength-reduce"
  opt="$opt -DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION"
  g++ "$2".cpp -o "$2".so $opt
  rm -f faustwrap.cpp $2.cpp
  echo "created $2.so"
}

[ "$1" = "" ] && {
  echo "usage: build-module [-single] <dsp-file> [<module-name>]"
  exit 1
}
faustargs="-double"
tooldir="$(dirname "$0")"
instdir="$tooldir"/..
[ "$1" = "-single" ] && {
  faustargs=""
  shift
}
if [ "$2" = "" ]; then
  bname="$(basename "$1" .dsp)"
else
  bname="$2"
fi
if [ "$tooldir" != . -o "$bname" != faustmod ]; then
  trap 'rm -f "$bname".pyx' EXIT
  ln -s "$tooldir"/faustmod.pyx "$bname".pyx
fi
compile "$1" "$bname"
