| 1 | #!/bin/bash |
| 2 | |
| 3 | ##################################################################### |
| 4 | # # |
| 5 | # Compiles Faust DSP to machine code # |
| 6 | # for several CPUs using C++ compiler # |
| 7 | # or LLVM with dynamic-faust # |
| 8 | # (c) Grame, 2020-2024 # |
| 9 | # # |
| 10 | ##################################################################### |
| 11 | |
| 12 | . faustpath |
| 13 | . faustoptflags |
| 14 | |
| 15 | FILES="" |
| 16 | # always include |
| 17 | CPU="generic" |
| 18 | OPT="" |
| 19 | SOURCES=false |
| 20 | MULTI=false |
| 21 | MULTIFUN=false |
| 22 | LLVM=false |
| 23 | TEST=false |
| 24 | US="0" |
| 25 | DS="0" |
| 26 | FILTER="0" |
| 27 | LLVM_ADAPTER=$FAUSTINC/faust/dsp/llvm-dsp-adapter.h |
| 28 | CPP_ADAPTER=$FAUSTINC/faust/dsp/cpp-dsp-adapter.h |
| 29 | MULTI_ADAPTER=$FAUSTINC/faust/dsp/dsp-multi.h |
| 30 | MULTIFUN_ADAPTER=$FAUSTINC/faust/dsp/dsp-multifun.h |
| 31 | SOUNDFILE="0" |
| 32 | SOUNDFILEDEFS="" |
| 33 | SOUNDFILEINCS="" |
| 34 | SOUNDFILELIBS="" |
| 35 | dspName="" |
| 36 | OPT_LD="" |
| 37 | |
| 38 | if [ $(uname -s) == Darwin ] && [ $(arch) == arm64 ]; then |
| 39 | #CXX="arch -arch x86_64 "$CXX |
| 40 | OPT_LD="-L /opt/local/lib" |
| 41 | fi |
| 42 | |
| 43 | ALL_CPU_LIST="nocona core2 penryn bonnell atom silvermont slm goldmont goldmont-plus tremont nehalem corei7 westmere sandybridge corei7-avx ivybridge core-avx-i haswell core-avx2 broadwell skylake skylake-avx512 skx cascadelake cooperlake cannonlake icelake-client icelake-server tigerlake knl knm k8 athlon64 athlon-fx opteron k8-sse3 athlon64-sse3 opteron-sse3 amdfam10 barcelona btver1 btver2 bdver1 bdver2 bdver3 bdver4 znver1 znver2 x86-64 apple-m1 apple-m2 apple-m3 generic" |
| 44 | |
| 45 | while [ $1 ] |
| 46 | do |
| 47 | p=$1 |
| 48 | |
| 49 | if [ $p = "-help" ] || [ $p = "-h" ]; then |
| 50 | echo "faust2object [nocona] [core2] [penryn] [bonnell] [atom] [silvermont] [slm] [goldmont] [goldmont-plus] [tremont] [nehalem] [corei7] [westmere] [sandybridge] [corei7-avx] [ivybridge] [core-avx-i] [haswell] [core-avx2] [broadwell] [skylake] [skylake-avx512] [skx] [cascadelake] [cooperlake] [cannonlake] [icelake-client] [icelake-server] [tigerlake] [knl] [knm] [k8] [athlon64] [athlon-fx] [opteron] [k8-sse3] [athlon64-sse3] [opteron-sse3] [amdfam10] [barcelona] [btver1] [btver2] [bdver1] [bdver2] [bdver3] [bdver4] [znver1] [znver2] [apple-m1] [apple-m2] [apple-m3] [x86-64] [generic] [-all] [-soundfile] [-sources] [-multi] [-multifun] [-opt native|generic] [-llvm] [-test] [-us <factor>] [-ds <factor>] [-filter <filter(0..4)>] [additional Faust options (-vec -vs 8...)] <file.dsp>" |
| 51 | echo "Use 'xxx' to compile for 'xxx' CPU" |
| 52 | echo "Use 'generic' to compile for generic CPU" |
| 53 | echo "Use '-all' to compile for all CPUs" |
| 54 | echo "Use '-soundfile' when compiling a DSP using the 'soundfile' primitive, add required resources" |
| 55 | echo "Use '-sources' to only generate source files" |
| 56 | echo "Use '-multi' to compile for several CPUs and aggregate them in a 'multi' class that choose the correct one at runtime" |
| 57 | echo "Use '-multifun' to compile for several CPUs using GCC MultiFun feature and aggregate them in a 'multi' class that choose the correct one at runtime" |
| 58 | echo "Use '-opt native' to activate the best compilation options for the native CPU" |
| 59 | echo "Use '-opt generic' to activate the best compilation options for a generic CPU" |
| 60 | echo "Use '-llvm' to compile using the LLVM backend, otherwise the C++ backend is used" |
| 61 | echo "Use '-test' to compile a test program which will bench the DSP and render it" |
| 62 | echo "Use '-us <factor>' to upsample the DSP by a factor" |
| 63 | echo "Use '-ds <factor>' to downsample the DSP by a factor" |
| 64 | echo "Use '-filter <filter>' for upsampling or downsampling [0..4]" |
| 65 | exit |
| 66 | fi |
| 67 | |
| 68 | if [ $p = "-opt" ]; then |
| 69 | shift |
| 70 | OPT=$1 |
| 71 | elif [ $p = "-cn" ]; then |
| 72 | shift |
| 73 | dspName=$1 |
| 74 | elif [ $p = "-sources" ]; then |
| 75 | SOURCES=true |
| 76 | elif [ $p = "-llvm" ]; then |
| 77 | LLVM=true |
| 78 | elif [ $p = "-multi" ]; then |
| 79 | MULTI=true |
| 80 | elif [ $p = "-multifun" ]; then |
| 81 | MULTIFUN=true |
| 82 | elif [ $p = "-test" ]; then |
| 83 | TEST=true |
| 84 | elif [ $p = "-all" ]; then |
| 85 | CPU=$ALL_CPU_LIST |
| 86 | elif [ $p = "-us" ]; then |
| 87 | shift |
| 88 | US=$1 |
| 89 | elif [ $p = "-ds" ]; then |
| 90 | shift |
| 91 | DS=$1 |
| 92 | elif [ $p = "-filter" ]; then |
| 93 | shift |
| 94 | FILTER=$1 |
| 95 | # '-A path' also added as '-I path' in the C++ command |
| 96 | elif [ $p = "-A" ]; then |
| 97 | shift |
| 98 | CXXFLAGS="$CXXFLAGS -I $1" |
| 99 | OPTIONS="$OPTIONS -A $1" |
| 100 | elif [ $p = "-soundfile" ]; then |
| 101 | SOUNDFILE="1" |
| 102 | SOUNDFILEDEFS="-DSOUNDFILE" |
| 103 | SOUNDFILEINCS=`pkg-config --cflags sndfile` |
| 104 | SOUNDFILELIBS=`pkg-config --static --libs sndfile` |
| 105 | elif [ ${p:0:1} = "-" ]; then |
| 106 | OPTIONS="$OPTIONS $p" |
| 107 | elif [[ -f "$p" ]] && [ ${p: -4} == ".dsp" ]; then |
| 108 | FILES="$FILES $p" |
| 109 | elif echo $ALL_CPU_LIST | grep -E "(\s|^)$p(\s|$)" > /dev/null; then #See https://www.golinuxcloud.com/grep-exact-match/ |
| 110 | # 'generic' is always included |
| 111 | if [ $p != "generic" ]; then |
| 112 | CPU="$CPU $p" |
| 113 | fi |
| 114 | else |
| 115 | OPTIONS="$OPTIONS $p" |
| 116 | fi |
| 117 | |
| 118 | shift |
| 119 | done |
| 120 | |
| 121 | # If no CPU is gven, compile by default for 'generic' |
| 122 | if [ -z "$CPU" ]; then |
| 123 | echo "ERROR : no CPU was given" |
| 124 | exit |
| 125 | fi |
| 126 | |
| 127 | for p in $FILES; do |
| 128 | |
| 129 | f=$(basename "$p") |
| 130 | |
| 131 | if [ -z $dspName ]; then |
| 132 | dspName="${f%.dsp}" |
| 133 | fi |
| 134 | |
| 135 | # discover best compilation options |
| 136 | if [ "$OPT" = "generic" ]; then |
| 137 | echo "Look for best compilation options in 'generic' mode..." |
| 138 | OPTIONS="$OPTIONS $(faustbench-llvm -notrace -generic $OPTIONS $f)" |
| 139 | echo $OPTIONS |
| 140 | elif [ "$OPT" = "native" ]; then |
| 141 | echo "Look for best compilation options in 'native' mode..." |
| 142 | OPTIONS="$OPTIONS $(faustbench-llvm -notrace $OPTIONS $f)" |
| 143 | echo $OPTIONS |
| 144 | fi |
| 145 | |
| 146 | # compile for each CPU |
| 147 | for arch_cpu in $CPU; do |
| 148 | |
| 149 | # replace '-' with '_' |
| 150 | cpu=$(echo $arch_cpu | sed -e 's/-/_/g') |
| 151 | echo $cpu |
| 152 | |
| 153 | # renaming |
| 154 | if [ $arch_cpu = "generic" ]; then |
| 155 | arch_cpu="" |
| 156 | fi |
| 157 | |
| 158 | if [ $LLVM = true ]; then |
| 159 | # generating the LLVM code |
| 160 | echo "Compiled using LLVM" |
| 161 | sed -e "s/mydsp/$dspName$cpu/g" $LLVM_ADAPTER > $dspName$cpu".h" |
| 162 | |
| 163 | # compiling |
| 164 | if [ $SOURCES = false ]; then |
| 165 | dynamic-faust -target x86_64-apple-darwin15.6.0:$arch_cpu -cn $dspName$cpu $OPTIONS $f -o $dspName$cpu".o" |
| 166 | fi |
| 167 | else |
| 168 | # generating the C++ code |
| 169 | echo "Compiled using C++" |
| 170 | sed -e "s/mydsp/$dspName$cpu/g" $CPP_ADAPTER > $dspName$cpu".h" |
| 171 | if [ $MULTIFUN = true ]; then |
| 172 | faust -lang c -a $FAUSTARCH/minimal-effect.c -cn $dspName$cpu $OPTIONS $f -o $dspName$cpu".cpp" |
| 173 | else |
| 174 | faust -a $FAUSTARCH/minimal-effect.cpp -cn $dspName$cpu $OPTIONS $f -o $dspName$cpu".cpp" |
| 175 | fi |
| 176 | |
| 177 | # compiling |
| 178 | if [ $SOURCES = false ]; then |
| 179 | $CXX $CXXFLAGS -Ofast -std=c++17 -march=$arch_cpu -I `faust -includedir` $SOUNDFILEDEFS $SOUNDFILEINCS -c $dspName$cpu".cpp" |
| 180 | fi |
| 181 | fi |
| 182 | done |
| 183 | |
| 184 | # possibly aggregate in a unique multi file |
| 185 | if [ $MULTI = true ]; then |
| 186 | # create file |
| 187 | echo "" > $dspName"multi.h" |
| 188 | # add conditional #define 'cpu' |
| 189 | for arch_cpu in $CPU; do |
| 190 | # replace '-' with '_' |
| 191 | cpu=$(echo $arch_cpu | sed -e 's/-/_/g') |
| 192 | echo "#define "$cpu >> $dspName"multi.h" |
| 193 | done |
| 194 | # class naming |
| 195 | sed -e "s/mydsp/$dspName/g" $MULTI_ADAPTER >> $dspName"multi.h" |
| 196 | fi |
| 197 | |
| 198 | # possibly aggregate in a unique multi file |
| 199 | if [ $MULTIFUN = true ]; then |
| 200 | # class naming |
| 201 | sed -e "s/mydsp/$dspName/g" $MULTIFUN_ADAPTER >> $dspName"multi.h" |
| 202 | fi |
| 203 | |
| 204 | # add info for DS/US + filter adapter |
| 205 | if [ -f $dspName"multi.h" ]; then |
| 206 | echo "#define DOWN_SAMPLING $DS" | cat - $dspName"multi.h" > temp && mv temp $dspName"multi.h" |
| 207 | echo "#define UP_SAMPLING $US" | cat - $dspName"multi.h" > temp && mv temp $dspName"multi.h" |
| 208 | echo "#define FILTER_TYPE $FILTER" | cat - $dspName"multi.h" > temp && mv temp $dspName"multi.h" |
| 209 | fi |
| 210 | |
| 211 | # create a test program |
| 212 | if [ $TEST = true ]; then |
| 213 | |
| 214 | if [ $MULTI = false ] && [ $MULTIFUN = false ]; then |
| 215 | echo "ERROR : -test option can only be used with -multi or -multifun options" |
| 216 | exit |
| 217 | fi |
| 218 | |
| 219 | # create and compile the test (note that -inj $dspName"multi.h" will correctly be used instead of any other use of 'inj -foo.cpp' in $OPTIONS) |
| 220 | faust $OPTIONS -inj $dspName"multi.h" -a $FAUSTARCH/minimal-bench.cpp -cn $dspName"multi" $p -o $dspName"multi.cpp" |
| 221 | |
| 222 | if [ $MULTI = true ]; then |
| 223 | $CXX $CXXFLAGS -Ofast -std=c++17 $dspName"multi.cpp" -dead_strip $SOUNDFILEDEFS $SOUNDFILELIBS -I `llvm-config --link-static --includedir --ldflags --libs all --system-libs` $OPT_LD $dspName*".o" -o $dspName"multi" |
| 224 | elif [ $MULTIFUN = true ]; then |
| 225 | $CXX $CXXFLAGS -Ofast -std=c++17 $dspName"multi.cpp" $dspName"generic.cpp" $SOUNDFILEDEFS $SOUNDFILELIBS -o $dspName"multi" |
| 226 | fi |
| 227 | |
| 228 | echo "Run ./"$dspName"multi"" to test the program" |
| 229 | fi |
| 230 | |
| 231 | done |