1#!/usr/local/bin/bash -e
2
3#############################################################################################
4#                                                                                           #
5#   Compiles Faust programs to Microsoft libraries suitable for the Unity environment       #
6#                                                                                           #
7#   (c) Grame, 2017                                                                         #
8#                                                                                           #
9#############################################################################################
10
11#   This script shouldn't be used on its own, use the "faust2unity" script instead
12#   The libraries are firstly wrapped by the unityplugin.cpp architecture file
13#   They have to be used in the Unity environment
14#   The libraries does not work by themselves, they need the FaustPlugin_<dspname>.cs and FaustUtilities_<dspname>.cs files to be functionnal
15#   These files are automatically generated by the "faust2unity" script
16
17. faustpath
18. faustoptflags
19
20CXXFLAGS+=" $MYGCCFLAGS"  # So that additional CXXFLAGS can be used
21
22NVOICES=-1
23
24#-----------------------------------------------------------------------
25# Dispatch command arguments
26
27while [ $1 ]
28do
29    p=$1
30
31    if [ $p = "-help" ] || [ $p = "-h" ]; then
32        echo "faust2unitywin -32 -64 [additional Faust options (-vec -vs 8...)] <file1.dsp> [<file2.dsp>]"
33        echo "Create Microsoft 32/64bits library for faust unity plugin."
34        echo "Mingw crosscompiler should be installed ('mingw32' package on Ubuntu)"
35        echo "Use 'faust2unity -w32 -w64' to also create the C# and JSON files"
36        echo "Use '-nvoices <num>' to produce a polyphonic self-contained DSP with <num> voices, ready to be used with MIDI"
37        echo "See architecture/unity/README.md for more info"
38        exit
39    fi
40
41    if [ $p = "-32" ]; then
42        BITS="$BITS 32"
43    elif [ $p = "-64" ]; then
44        BITS="$BITS 64"
45    elif [ $p = "-nvoices" ]; then
46        shift
47        NVOICES=$1
48    elif [ ${p:0:1} = "-" ]; then
49        OPTIONS="$OPTIONS $p"
50    elif [[ -f "$p" ]] && [ ${p: -4} == ".dsp" ]; then
51        FILES="$FILES $p"
52    else
53        OPTIONS="$OPTIONS $p"
54    fi
55
56shift
57done
58
59# Check at least one bit setting has been specified
60if [ -z "$BITS"  ]; then
61    echo "bits not specified, pass -32 and/or -64"
62    exit
63fi
64
65#-----------------------------------------------------------------------
66# Compiler settings
67
68CXXFLAGS+=" -Wl,--enable-auto-import"
69LIB="-shared"
70EXT=".dll"
71
72#-----------------------------------------------------------------------
73# compiles the *.dsp files
74
75for p in $FILES; do
76
77    CUR=$(pwd)
78    f=$(basename "$p")
79    NAME=${f%.dsp}
80    FNAME=FaustPlugin_$NAME
81    LIBNAME="lib$FNAME$EXT"
82
83    SRCDIR=$(dirname "$p")
84
85    # create a temporary dir
86    TDR=$(mktemp -d faust.XXXXXX)
87    TMP=$TDR/$NAME
88    mkdir "$TMP"
89
90    # compile faust to c++
91    faust -uim -i -a $FAUSTARCH/unity/unityplugin.cpp $OPTIONS "$SRCDIR/$f" -o "$TMP/$NAME.cpp" || (echo "$f : Faust to C++ compilation failed in faust2unitywin"; exit 1)
92
93    for BIT in $BITS; do
94        #-----------------------------------------------------------------------
95        # mingw crosscompiler should be installed ('mingw32' package on Ubuntu)
96        # the exact prefix should be specified in the environment variable MINGW
97        if [ $BIT = "32" ]; then
98            FIN=$FNAME/Windows/x86
99            MINGWPREFIX="i686-w64-mingw32-"
100        elif [ $BIT = "64" ]; then
101            FIN=$FNAME/Windows/x64
102            MINGWPREFIX="x86_64-w64-mingw32-"
103        fi
104
105        # checks if final dir exists, if not creates it
106        if [ ! -d "$FIN" ]; then
107            mkdir -p "$FIN"
108        fi
109
110        CXX="${MINGWPREFIX}g++"
111        (which "$CXX" >/dev/null) || (echo "MingW compiler $CXX not found. See -help for more info"; exit 1)
112
113        # compile c++ to binary
114       	(
115            cd "$TMP"
116            if [ $NVOICES == -1 ]; then
117            	$CXX $CXXFLAGS $PROCARCH $LIB $OMP -static-libstdc++ -static-libgcc -Dmydsp=$NAME -o $LIBNAME $NAME.cpp
118            else
119            	$CXX $CXXFLAGS $PROCARCH $LIB $OMP -static-libstdc++ -static-libgcc -DPOLY -Dmydsp=$NAME -o $LIBNAME $NAME.cpp
120            fi
121        ) > /dev/null || (echo "$f : C++ to win$BIT library compilation failed in faust2unitywin"; exit 1)
122
123        # moves binary to final dir
124        mv "$TMP/$LIBNAME" "$FIN/$LIBNAME"
125        echo "$f : w$BIT compilation completed"
126    done
127
128    # remove intermediary files
129    rm -rf "$SRCDIR/$LIBNAME"
130    rm -rf "$TDR"
131
132done
133
134