From 3e04dfe3e54661756cbaf25f564cede61b52350c Mon Sep 17 00:00:00 2001 From: Mel Bourgeois Date: Mon, 9 Jan 2023 21:31:27 -0600 Subject: [PATCH] Make script shebangs more robust with /usr/bin/env The script was failing to run in some environments, notably a pure nix shell. This is the more modern way to resolve bash in shebangs and should be supported on all platforms --- faust2appls/faust2wasm | 363 +++++++++++++++++++------------------- faust2appls/faustoptflags | 8 +- faust2appls/faustpath | 42 ++--- package.json | 2 +- 4 files changed, 208 insertions(+), 207 deletions(-) diff --git a/faust2appls/faust2wasm b/faust2appls/faust2wasm index 9aa8277..b332716 100755 --- a/faust2appls/faust2wasm +++ b/faust2appls/faust2wasm @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash . faustpath . faustoptflags @@ -35,8 +35,7 @@ EXPORTED_POLY="['_"$name"_poly_constructor','_"$name"_poly_destructor','_"$name" # existing *.dsp files -> FILES # -while [ $1 ] -do +while [ $1 ]; do p=$1 if [ $p = "-help" ] || [ $p = "-h" ]; then @@ -55,7 +54,7 @@ do option -npm "add a package.json file for npm package distribution" exit fi - + if [ $p = "-comb" ]; then COMB="true" elif [ $p = "-poly" ]; then @@ -80,14 +79,14 @@ do elif [ $p = "-npm" ]; then NPM="true" elif [ ${p:0:1} = "-" ]; then - OPTIONS="$OPTIONS $p" - elif [[ -f "$p" ]]; then - FILES="$FILES $p" - else - OPTIONS="$OPTIONS $p" - fi + OPTIONS="$OPTIONS $p" + elif [[ -f "$p" ]]; then + FILES="$FILES $p" + else + OPTIONS="$OPTIONS $p" + fi -shift + shift done @@ -140,18 +139,18 @@ BINARIES="" if [ $COMB = "false" ]; then -for f in $FILES; do - name=$(basename "$f" .dsp) - - # compile the Faust DSP to C++ or wasm code - if [ $EMCC = "true" ] ; then - faust -a $FAUSTARCH/webaudio/$CODE_WRAPPER1 -i -uim -cn $name $OPTIONS $f -o $name.cpp || exit - else - faust -lang $WASM -cn $name $OPTIONS $f -o $name.wasm || exit + for f in $FILES; do + name=$(basename "$f" .dsp) - # possibly compile effect - if [ "$EFFECT" = "auto" ]; then - cat > $name"_effect".dsp << EndOfCode + # compile the Faust DSP to C++ or wasm code + if [ $EMCC = "true" ]; then + faust -a $FAUSTARCH/webaudio/$CODE_WRAPPER1 -i -uim -cn $name $OPTIONS $f -o $name.cpp || exit + else + faust -lang $WASM -cn $name $OPTIONS $f -o $name.wasm || exit + + # possibly compile effect + if [ "$EFFECT" = "auto" ]; then + cat >$name"_effect".dsp < wasm optimizations - if [ $OPT = "true" ]; then - echo "Optimize wasm module" - wasm-opt $name.wasm -O3 -o $name.wasm + # wasm ==> wasm optimizations + if [ $OPT = "true" ]; then + echo "Optimize wasm module" + wasm-opt $name.wasm -O3 -o $name.wasm + fi fi - fi - if [ $EMCC = "true" ]; then - - # prepare emcc compilation files - if [ $POLY = false ]; then - EXPORTED=EXPORTED_MONO + if [ $EMCC = "true" ]; then + + # prepare emcc compilation files + if [ $POLY = false ]; then + EXPORTED=EXPORTED_MONO + else + EXPORTED=EXPORTED_POLY + fi + + # compile the C++ code to wasm + emcc -O2 --memory-init-file 0 $name.cpp -s TOTAL_MEMORY=100663296 --post-js $FAUSTARCH/webaudio/$JS_WRAPPER -o $name-temp.js \ + -s EXPORTED_FUNCTIONS=$EXPORTED || exit + + # compose the wasm code + sed -e "s/DSP/"$name"/g" $name-temp.js >$name.js + + rm $name-temp.js + rm $name.cpp else - EXPORTED=EXPORTED_POLY - fi - - # compile the C++ code to wasm - emcc -O2 --memory-init-file 0 $name.cpp -s TOTAL_MEMORY=100663296 --post-js $FAUSTARCH/webaudio/$JS_WRAPPER -o $name-temp.js \ - -s EXPORTED_FUNCTIONS=$EXPORTED || exit - - # compose the wasm code - sed -e "s/DSP/"$name"/g" $name-temp.js > $name.js - - rm $name-temp.js - rm $name.cpp - else - if [ "$EFFECT" != "" ]; then - cat $name"_effect".js >> $name.js - rm $name"_effect".js - fi + if [ "$EFFECT" != "" ]; then + cat $name"_effect".js >>$name.js + rm $name"_effect".js + fi - if [ $WORKLET = "true" ]; then - cp $name.js $name-processor.js - sed -e "s/mydsp/"$name"/g" $FAUSTARCH/webaudio/$CODE_WRAPPER1 >> $name-processor.js - if [ $WAP = "2" ]; then - sed -e "s/class\ mydsp\ /"export\ default\ class\ $name\ "/g" $FAUSTARCH/webaudio/$CODE_WRAPPER2 > $name_tmp1.js - sed -e "s/mydsp/"$name"/g" $name_tmp1.js >> $name.js + if [ $WORKLET = "true" ]; then + cp $name.js $name-processor.js + sed -e "s/mydsp/"$name"/g" $FAUSTARCH/webaudio/$CODE_WRAPPER1 >>$name-processor.js + if [ $WAP = "2" ]; then + sed -e "s/class\ mydsp\ /"export\ default\ class\ $name\ "/g" $FAUSTARCH/webaudio/$CODE_WRAPPER2 >$name_tmp1.js + sed -e "s/mydsp/"$name"/g" $name_tmp1.js >>$name.js + else + sed -e "s/mydsp/"$name"/g" $FAUSTARCH/webaudio/$CODE_WRAPPER2 >>$name.js + fi else - sed -e "s/mydsp/"$name"/g" $FAUSTARCH/webaudio/$CODE_WRAPPER2 >> $name.js + if [ $WAP = "2" ]; then + sed -e "s/class\ mydsp\ /"export\ default\ class\ $name\ "/g" $FAUSTARCH/webaudio/$CODE_WRAPPER1 >$name_tmp1.js + sed -e "s/mydsp/"$name"/g" $name_tmp1.js >>$name.js + else + sed -e "s/mydsp/"$name"/g" $FAUSTARCH/webaudio/$CODE_WRAPPER1 >>$name.js + fi fi - else - if [ $WAP = "2" ]; then - sed -e "s/class\ mydsp\ /"export\ default\ class\ $name\ "/g" $FAUSTARCH/webaudio/$CODE_WRAPPER1 > $name_tmp1.js - sed -e "s/mydsp/"$name"/g" $name_tmp1.js >> $name.js + fi + + # create additional files for WAP + if [ $WAP = "1" ] || [ $WAP = "2" ]; then + + # create the main.json file + echo "{" >main.json + echo "\"name\": \"$name\"," >>main.json + echo "\"vendor\": \"Faust\"," >>main.json + echo "\"version\": \"1.01\"," >>main.json + echo "\"homepage\": \"https://faust.grame.fr\"," >>main.json + echo "\"thumbnail\": \"default.png\"" >>main.json + echo "}" >>main.json + + # compose the HTML test page + if [ $POLY = true ]; then + if [ $WAP = "2" ]; then + cp $FAUSTARCH/webaudio/testWAP2Poly.html test$name-tmp1.html + else + cp $FAUSTARCH/webaudio/testWAPPoly.html test$name-tmp1.html + fi else - sed -e "s/mydsp/"$name"/g" $FAUSTARCH/webaudio/$CODE_WRAPPER1 >> $name.js + if [ $WAP = "2" ]; then + cp $FAUSTARCH/webaudio/testWAP2.html test$name-tmp1.html + else + cp $FAUSTARCH/webaudio/testWAP.html test$name-tmp1.html + fi fi + sed -e "s/VENDOR/"Faust"/g" test$name-tmp1.html >>test$name-tmp2.html + sed -e "s/DSP/"$name"/g" test$name-tmp2.html >test$name.html fi - fi - # create additional files for WAP - if [ $WAP = "1" ] || [ $WAP = "2" ]; then - - # create the main.json file - echo "{" > main.json - echo "\"name\": \"$name\"," >> main.json - echo "\"vendor\": \"Faust\"," >> main.json - echo "\"version\": \"1.01\"," >> main.json - echo "\"homepage\": \"https://faust.grame.fr\"," >> main.json - echo "\"thumbnail\": \"default.png\"" >> main.json - echo "}" >> main.json - - # compose the HTML test page - if [ $POLY = true ]; then - if [ $WAP = "2" ]; then - cp $FAUSTARCH/webaudio/testWAP2Poly.html test$name-tmp1.html - else - cp $FAUSTARCH/webaudio/testWAPPoly.html test$name-tmp1.html - fi + # collect binary file name + if [ $WORKLET = "true" ]; then + rm -f $name"_effect".wasm test$name-tmp1.html test$name-tmp2.html + if [ $WAP = "1" ] || [ $WAP = "2" ]; then + mv $name.js main.js + mv test$name.html index.html + BINARIES="$BINARIES main.js, $name.wasm, main.json, index.html" + else + BINARIES="$BINARIES$name.js, $name-processor.js, $name.wasm" + fi else - if [ $WAP = "2" ]; then - cp $FAUSTARCH/webaudio/testWAP2.html test$name-tmp1.html - else - cp $FAUSTARCH/webaudio/testWAP.html test$name-tmp1.html - fi + BINARIES="$BINARIES$name.js, $name.wasm" fi - sed -e "s/VENDOR/"Faust"/g" test$name-tmp1.html >> test$name-tmp2.html - sed -e "s/DSP/"$name"/g" test$name-tmp2.html > test$name.html - fi - - # collect binary file name - if [ $WORKLET = "true" ]; then - rm -f $name"_effect".wasm test$name-tmp1.html test$name-tmp2.html - if [ $WAP = "1" ] || [ $WAP = "2" ]; then - mv $name.js main.js - mv test$name.html index.html - BINARIES="$BINARIES main.js, $name.wasm, main.json, index.html" - else - BINARIES="$BINARIES$name.js, $name-processor.js, $name.wasm" + + if [ "$NPM" = "true" ]; then + sed -e "s/mydsp/"$name"/g" $FAUSTARCH/webaudio/package.json >package.json + BINARIES="$BINARIES, package.json" fi - else - BINARIES="$BINARIES$name.js, $name.wasm" - fi - if [ "$NPM" = "true" ]; then - sed -e "s/mydsp/"$name"/g" $FAUSTARCH/webaudio/package.json > package.json - BINARIES="$BINARIES, package.json" - fi - - BINARIES="$BINARIES;" + BINARIES="$BINARIES;" -done + done else -# TODO : worklet support -echo "Compiled with 'comb' mode" + # TODO : worklet support + echo "Compiled with 'comb' mode" -for f in $FILES; do - name=$(basename "$f" .dsp) - - # compile the Faust DSP to C++ or wasm code - if [ $EMCC = "true" ] ; then - faust -a $FAUSTARCH/webaudio/$CODE_WRAPPER1 -i -uim -cn $name $OPTIONS $f -o $name.cpp || exit - else - faust -lang $WASM -cn $name $OPTIONS $f -o $name.wasm || exit + for f in $FILES; do + name=$(basename "$f" .dsp) - # wasm ==> wasm optimizations - if [ $OPT = "true" ]; then - echo "Optimize $name wasm module" - wasm-opt $name.wasm -O3 -o $name.wasm - fi + # compile the Faust DSP to C++ or wasm code + if [ $EMCC = "true" ]; then + faust -a $FAUSTARCH/webaudio/$CODE_WRAPPER1 -i -uim -cn $name $OPTIONS $f -o $name.cpp || exit + else + faust -lang $WASM -cn $name $OPTIONS $f -o $name.wasm || exit + + # wasm ==> wasm optimizations + if [ $OPT = "true" ]; then + echo "Optimize $name wasm module" + wasm-opt $name.wasm -O3 -o $name.wasm + fi - # possibly compile effect - if [ "$EFFECT" = "auto" ]; then - cat > $name"_effect".dsp << EndOfCode + # possibly compile effect + if [ "$EFFECT" = "auto" ]; then + cat >$name"_effect".dsp <$name-wrapper.js + + COMB_SRC+=$name.cpp + COMB_SRC+=" " + + COMB_EXPORTED+=$COMB_SEP$EXPORTED + COMB_SEP="," + + COMB_WRAPPED_FILES+=$name-wrapper.js + COMB_WRAPPED_FILES+=" " + + COMB_WRAPPED+=" --post-js " + COMB_WRAPPED+=$name-wrapper.js - # prepare emcc compilation files - if [ $POLY = false ]; then - EXPORTED=EXPORTED_MONO else - EXPORTED=EXPORTED_POLY - fi + echo $name.js - # compose the wasm code - sed -e "s/DSP/"$name"/g" $FAUSTARCH/webaudio/$JS_WRAPPER > $name-wrapper.js - - COMB_SRC+=$name.cpp - COMB_SRC+=" " - - COMB_EXPORTED+=$COMB_SEP$EXPORTED - COMB_SEP="," - - COMB_WRAPPED_FILES+=$name-wrapper.js - COMB_WRAPPED_FILES+=" " - - COMB_WRAPPED+=" --post-js " - COMB_WRAPPED+=$name-wrapper.js - - else - echo $name.js + if [ "$EFFECT" != "" ]; then + cat $name"_effect".js >>$name.js + rm $name"_effect".js + fi - if [ "$EFFECT" != "" ]; then - cat $name"_effect".js >> $name.js - rm $name"_effect".js + sed -e "s/mydsp/"$name"/g" $FAUSTARCH/webaudio/$CODE_WRAPPER1 >>$name.js + cat $name.js >>comb.js + rm $name.js fi - sed -e "s/mydsp/"$name"/g" $FAUSTARCH/webaudio/$CODE_WRAPPER1 >> $name.js - cat $name.js >> comb.js - rm $name.js - fi - -done + done -if [ $EMCC = "true" ]; then - # compile final file - emcc -O2 --memory-init-file 0 $COMB_SRC -s TOTAL_MEMORY=100663296 $COMB_WRAPPED -o comb.js \ - -s EXPORTED_FUNCTIONS="["$COMB_EXPORTED"]" || exit + if [ $EMCC = "true" ]; then + # compile final file + emcc -O2 --memory-init-file 0 $COMB_SRC -s TOTAL_MEMORY=100663296 $COMB_WRAPPED -o comb.js \ + -s EXPORTED_FUNCTIONS="["$COMB_EXPORTED"]" || exit - rm $COMB_SRC $COMB_WRAPPED_FILES -fi + rm $COMB_SRC $COMB_WRAPPED_FILES + fi -# collect binary file name -BINARIES="comb.js" + # collect binary file name + BINARIES="comb.js" -if [ "$NPM" = "true" ]; then - sed -e "s/mydsp/"$name"/g" $FAUSTARCH/webaudio/package.json > package.json - BINARIES="$BINARIES, package.json" -fi + if [ "$NPM" = "true" ]; then + sed -e "s/mydsp/"$name"/g" $FAUSTARCH/webaudio/package.json >package.json + BINARIES="$BINARIES, package.json" + fi -BINARIES="$BINARIES;" + BINARIES="$BINARIES;" fi diff --git a/faust2appls/faustoptflags b/faust2appls/faustoptflags index 0c3cf67..63d713b 100755 --- a/faust2appls/faustoptflags +++ b/faust2appls/faustoptflags @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash ##################################################################### # # @@ -13,9 +13,9 @@ #------------------------------------------------------------------- # Default compilation flags for gcc and icc : # -if [[ $(uname -m) == armv6l ]]; then # for Raspberry PI +if [[ $(uname -m) == armv6l ]]; then # for Raspberry PI MYGCCFLAGS="-std=c++11 -O3 -march=armv6zk -mcpu=arm1176jzf-s -mtune=arm1176jzf-s -mfpu=vfp -mfloat-abi=hard -ffast-math -ftree-vectorize" -elif [[ $(uname -s) == Darwin ]]; then # for macOS +elif [[ $(uname -s) == Darwin ]]; then # for macOS if [[ $(uname -m) == arm64 ]]; then # Silicon MX MYGCCFLAGS="-std=c++11 -Ofast" @@ -24,7 +24,7 @@ elif [[ $(uname -s) == Darwin ]]; then # for macOS MYGCCFLAGS="-std=c++11 -Ofast -march=native" fi MYGCCFLAGSGENERIC="-std=c++11 -Ofast" -else # for Linux (Intel) +else # for Linux (Intel) MYGCCFLAGS="-std=c++11 -Ofast -march=native" MYGCCFLAGSGENERIC="-std=c++11 -Ofast" fi diff --git a/faust2appls/faustpath b/faust2appls/faustpath index f025f79..0737f3c 100755 --- a/faust2appls/faustpath +++ b/faust2appls/faustpath @@ -1,4 +1,6 @@ -#! /bin/bash -e +#! /usr/bin/env bash + +set -e ##################################################################### # # @@ -11,45 +13,45 @@ #------------------------------------------------------------------------------- # Search where Faust is installed. Store '.../share/faust/' in $FAUSTLIB -# and '.../include/(faust)' in $FAUSTINC +# and '.../include/(faust)' in $FAUSTINC # # USAGE : # add line << . faustpath >> to your script # and use -L$FAUSTLIB and -I$FAUSTINC where needed -FAUSTLDDIR=$(faust -libdir); -FAUSTLIB=$(faust -dspdir); -FAUSTINC=$(faust -includedir); -[ -n "$FAUSTARCH" ] || FAUSTARCH=$(faust -archdir); +FAUSTLDDIR=$(faust -libdir) +FAUSTLIB=$(faust -dspdir) +FAUSTINC=$(faust -includedir) +[ -n "$FAUSTARCH" ] || FAUSTARCH=$(faust -archdir) [ -n "$MAXSDK" ] || MAXSDK=/usr/local/include/c74support/ CSOUND_MACSDK=/Library/Frameworks/CsoundLib64.framework # try and guess a good place for Pd header files. Taking the first entry in /Applications/Pd- and taking the folder containing m_pd.h -[ -n "$PUREDATA_MACSDK" ] || PUREDATA_MACSDK=`ls -d /Applications/Pd-* 2> /dev/null | head -n1 | xargs -I REPL find REPL -name m_pd.h -exec dirname {} \; | head -n1` -[ -n "$PUREDATA_LINUXSDK" ] || PUREDATA_LINUXSDK=`find /usr/include/pd /usr/include/libpd/ /usr/include/pdextended /usr/local/include/pd /usr/local/include/libpd -name m_pd.h -exec dirname {} \; 2>/dev/null| head -n1` +[ -n "$PUREDATA_MACSDK" ] || PUREDATA_MACSDK=$(ls -d /Applications/Pd-* 2>/dev/null | head -n1 | xargs -I REPL find REPL -name m_pd.h -exec dirname {} \; | head -n1) +[ -n "$PUREDATA_LINUXSDK" ] || PUREDATA_LINUXSDK=$(find /usr/include/pd /usr/include/libpd/ /usr/include/pdextended /usr/local/include/pd /usr/local/include/libpd -name m_pd.h -exec dirname {} \; 2>/dev/null | head -n1) VSTSDK="" [ -n "$SUPERCOLLIDER_HEADERS" ] || SUPERCOLLIDER_HEADERS=/usr/local/include/SuperCollider/ -FPATH="$FAUST_INSTALL /usr/local /usr /opt /opt/local"; # <- where to search -if [ -z "$FAUSTLIB" -o -z "$FAUSTINC" ]; then -for f in $FPATH; do - if [ -e $f/share/faust ]; then FAUSTLIB=$f/share/faust; fi - if [ -e $f/include/faust ]; then FAUSTINC=$f/include/; fi -done +FPATH="$FAUST_INSTALL /usr/local /usr /opt /opt/local" # <- where to search +if [ -z "$FAUSTLIB" -o -z "$FAUSTINC" ]; then + for f in $FPATH; do + if [ -e $f/share/faust ]; then FAUSTLIB=$f/share/faust; fi + if [ -e $f/include/faust ]; then FAUSTINC=$f/include/; fi + done fi for f in $FPATH; do - if [ -e $f/lib/libmicrohttpd.a ]; then HTTPLIB=$f/share/faust; fi + if [ -e $f/lib/libmicrohttpd.a ]; then HTTPLIB=$f/share/faust; fi done if [ -e /etc/faust/faustpath ]; then - . /etc/faust/faustpath + . /etc/faust/faustpath fi if [ -e ${HOME}/.faust/faustpath ]; then - . ${HOME}/.faust/faustpath + . ${HOME}/.faust/faustpath fi -if [ -z "$FAUSTLIB" -o -z "$FAUSTINC" ]; then - echo "ERROR : $0 cannot find Faust directories (normally /usr/local/include/faust and /usr/local/share/faust)"; - exit 1; +if [ -z "$FAUSTLIB" -o -z "$FAUSTINC" ]; then + echo "ERROR : $0 cannot find Faust directories (normally /usr/local/include/faust and /usr/local/share/faust)" + exit 1 fi diff --git a/package.json b/package.json index 4ba6fc2..26e6c02 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "faust-loader", - "version": "1.2.2", + "version": "1.2.3", "main": "dist/faustLoader.js", "types": "dist/faustLoader.d.ts", "scripts": {