| 1 | #! /bin/bash -e |
| 2 | |
| 3 | ##################################################################### |
| 4 | # # |
| 5 | # Compile a Faust program to an Android app # |
| 6 | # (c) Romain Michon CCRMA and Grame, 2014 # |
| 7 | # (c) Yann Orlarey Grame, 2015 # |
| 8 | # # |
| 9 | ##################################################################### |
| 10 | |
| 11 | . faustpath |
| 12 | . faustoptflags |
| 13 | . usage.sh |
| 14 | |
| 15 | # change if you want to get the log of what's happening |
| 16 | LOG="/dev/null" |
| 17 | |
| 18 | # exit if a command fails |
| 19 | set -e |
| 20 | |
| 21 | # Global variables for file and options |
| 22 | FILE= |
| 23 | INSTALL=0 |
| 24 | SOURCE=0 |
| 25 | SWIG=0 |
| 26 | FAUST=0 |
| 27 | KEYBOARD=0 |
| 28 | REUSE=0 |
| 29 | SOUNDFILE=0 |
| 30 | OSCCTRL=0 |
| 31 | |
| 32 | echoHelp() |
| 33 | { |
| 34 | usage faust2android "[options] [Faust options] <file.dsp>" |
| 35 | platform Android |
| 36 | require Android SDK |
| 37 | echo "Compile a Faust program to an Android app (see https://github.com/grame-cncm/faust/tree/master-dev/architecture/android)" |
| 38 | option |
| 39 | option -osc |
| 40 | option -source "creates an eclipse project of the app in the current directory." |
| 41 | option -swig "regenerates the C++ and the JAVA interface for the native portion of the app." |
| 42 | option -faust "only carries out the Faust compilation and install the generated C++ file in the JNI folder." |
| 43 | option -reuse "preserves build directory and reuse it to speedup compilation." |
| 44 | option -soundfile |
| 45 | option -install "once compilation is over, installs the generated apk on the Android device connected to the computer" |
| 46 | option -debug "activates verbose output" |
| 47 | option "Faust options" |
| 48 | exit |
| 49 | } |
| 50 | |
| 51 | if [ "$#" -eq 0 ]; then |
| 52 | echo 'Please, provide a Faust file to process !' |
| 53 | echo '' |
| 54 | echoHelp |
| 55 | fi |
| 56 | |
| 57 | # PHASE 2 : dispatch command arguments |
| 58 | for p in $@; do |
| 59 | if [ $p = "-swig" ]; then |
| 60 | SWIG=1 |
| 61 | elif [[ -f "$p" ]] && [ ${p: -4} == ".dsp" ]; then |
| 62 | FILE="$p" |
| 63 | elif [ $p = "-install" ]; then |
| 64 | INSTALL=1 |
| 65 | elif [ $p = "-soundfile" ]; then |
| 66 | SOUNDFILE="1" |
| 67 | elif [ $p = "-osc" ]; then |
| 68 | OSCCTRL="1" |
| 69 | elif [ $p = "-source" ]; then |
| 70 | SOURCE=1 |
| 71 | elif [ $p = "-faust" ]; then |
| 72 | FAUST=1 |
| 73 | elif [ $p = "-reuse" ]; then |
| 74 | REUSE=1 |
| 75 | elif [ $p = "-keyboard" ]; then |
| 76 | KEYBOARD=1 |
| 77 | elif [ $p = "-debug" ]; then |
| 78 | LOG="/dev/stdout" |
| 79 | elif [ "$p" = "-noagc" ]; then |
| 80 | NOAGC="1" |
| 81 | elif [ $p = "-help" ] || [ $p = "-h" ]; then |
| 82 | echoHelp |
| 83 | elif [ ${p:0:1} = "-" ]; then |
| 84 | OPTIONS="$OPTIONS $p" |
| 85 | else |
| 86 | OPTIONS="$OPTIONS $p" |
| 87 | fi |
| 88 | done |
| 89 | |
| 90 | # only carry out the faust compilation |
| 91 | if [ $FAUST -eq 1 ]; then |
| 92 | faust $OPTIONS -i -a api/DspFaust.cpp "$FILE" -o "app/src/main/cpp/DspFaust.cpp" |
| 93 | exit 1 |
| 94 | fi |
| 95 | |
| 96 | # Create the temporary directory where compilation will take place |
| 97 | |
| 98 | APPNAME=$(basename "$FILE") |
| 99 | APPNAME="${APPNAME%.dsp}" |
| 100 | APPNAME=`filename2ident "$APPNAME"` |
| 101 | BUILDDIR="faustandro.$APPNAME" |
| 102 | APPFOLDER="$BUILDDIR/app/src/main" |
| 103 | JNIFOLDER="$APPFOLDER/cpp" |
| 104 | |
| 105 | if [ $REUSE -eq 0 ]; then |
| 106 | if [ -d "$BUILDDIR" ]; then |
| 107 | echo "Delete existing Android project $BUILDDIR" > $LOG |
| 108 | rm -rf "$BUILDDIR" |
| 109 | fi |
| 110 | fi |
| 111 | |
| 112 | if [ ! -d "$BUILDDIR" ]; then |
| 113 | echo "Creating new Android project $BUILDDIR" > $LOG |
| 114 | |
| 115 | mkdir -p "$BUILDDIR" |
| 116 | cp -r $FAUSTARCH/android/* "$BUILDDIR" |
| 117 | install -d $JNIFOLDER |
| 118 | # Copy include files *.h if any (ignore any error here) |
| 119 | (cp *.h $JNIFOLDER 2> $LOG) || true |
| 120 | |
| 121 | # change 'faust' with real *APPNAME |
| 122 | PLATFORM=$(uname) |
| 123 | |
| 124 | if [ $PLATFORM = "Darwin" ]; then |
| 125 | sed -i '' 's,com.faust,com.'$APPNAME',g' $BUILDDIR/app/build.gradle |
| 126 | if [ "$NOAGC" = "1" ]; then |
| 127 | sed -i '' 's,-DDISABLE_AGC,'-DDISABLE_AGC',g' $BUILDDIR/app/build.gradle |
| 128 | else |
| 129 | sed -i '' 's,-DDISABLE_AGC,'',g' $BUILDDIR/app/build.gradle |
| 130 | fi |
| 131 | sed -i '' 's,com.faust,com.'$APPNAME',g' $APPFOLDER/java/com/faust/* |
| 132 | sed -i '' 's,com.faust,com.'$APPNAME',g' $APPFOLDER/java/com/triggertrap/seekarc/* |
| 133 | sed -i '' 's,com.faust,com.'$APPNAME',g' $APPFOLDER/AndroidManifest.xml |
| 134 | sed -i '' 's,com.faust,com.'$APPNAME',g' $APPFOLDER/res/layout/* |
| 135 | sed -i '' 's,1,'$APPNAME',g' $APPFOLDER/res/values/strings.xml |
| 136 | else |
| 137 | sed -i 's,com.faust,com.'$APPNAME',g' $BUILDDIR/app/build.gradle |
| 138 | if [ "$NOAGC" = "1" ]; then |
| 139 | sed -i 's,-DDISABLE_AGC,'-DDISABLE_AGC',g' $BUILDDIR/app/build.gradle |
| 140 | else |
| 141 | sed -i 's,-DDISABLE_AGC,'',g' $BUILDDIR/app/build.gradle |
| 142 | fi |
| 143 | sed -i 's,com.faust,com.'$APPNAME',g' $APPFOLDER/java/com/faust/* |
| 144 | sed -i 's,com.faust,com.'$APPNAME',g' $APPFOLDER/java/com/triggertrap/seekarc/* |
| 145 | sed -i 's,com.faust,com.'$APPNAME',g' $APPFOLDER/AndroidManifest.xml |
| 146 | sed -i 's,com.faust,com.'$APPNAME',g' $APPFOLDER/res/layout/* |
| 147 | sed -i 's,1,'$APPNAME',g' $APPFOLDER/res/values/strings.xml |
| 148 | fi |
| 149 | |
| 150 | mv $APPFOLDER/java/com/faust $APPFOLDER/java/com/$APPNAME |
| 151 | |
| 152 | # TODO wrong: should be checked |
| 153 | if [ $SWIG -eq 1 ]; then |
| 154 | rm -rf $APPFOLDER/java/com/dsp_faust || true |
| 155 | mkdir -p $APPFOLDER/java/com/dsp_faust |
| 156 | swig -java -package com.dsp_faust -includeall -verbose -outdir $APPFOLDER/java/com/dsp_faust -c++ -I$FAUSTINC -I/System/Library/Frameworks/JavaVM.framework/Headers -I$JNIFOLDER -o $JNIFOLDER/java_interface_wrap.cpp $BUILDDIR/dsp_faust_interface.i |
| 157 | fi |
| 158 | else |
| 159 | echo "Reusing existing Android project $BUILDDIR" > $LOG |
| 160 | fi |
| 161 | |
| 162 | # Copying the Faust API files in the project |
| 163 | cp $FAUSTARCH/api/DspFaust.h $JNIFOLDER |
| 164 | cp $FAUSTARCH/api/android/jni/java_interface_wrap.cpp $JNIFOLDER |
| 165 | mkdir $APPFOLDER/java/com/DspFaust |
| 166 | cp $FAUSTARCH/api/android/jni/*.java $APPFOLDER/java/com/DspFaust |
| 167 | |
| 168 | # Compile the Faust code for the NDK |
| 169 | faust $OPTIONS -i -json -a api/DspFaust.cpp "$FILE" -o "$JNIFOLDER/DspFaust.cpp" || exit |
| 170 | |
| 171 | # Add soundfile support if needed |
| 172 | if [ $SOUNDFILE -eq 1 ]; then |
| 173 | cat $FILE.json | awk ' |
| 174 | BEGIN { FS=":"; SOFI=0; } |
| 175 | /"soundfile"/ { SOFI=1; } |
| 176 | /"url"/ { |
| 177 | if (SOFI) { |
| 178 | match($2, /"[^"]*/); |
| 179 | split(substr($2, RSTART+2, RLENGTH-3), res, ";"); |
| 180 | for (x in res) print substr(res[x], 2, length(res[x])-2); |
| 181 | SOFI=0; |
| 182 | } |
| 183 | } |
| 184 | ' > $APPNAME-tmp.txt |
| 185 | for snd in $(cat $APPNAME-tmp.txt); do |
| 186 | if [ -f $snd ]; then |
| 187 | if [ ${snd:0:1} = "/" ]; then |
| 188 | echo "Warning: soundfile with absolute path is not copied !" |
| 189 | else |
| 190 | #create destination path and possibly create directory |
| 191 | sfpath="$APPFOLDER/assets/$(dirname $snd)/" |
| 192 | if ! [ -d $sfpath ]; then |
| 193 | echo "Create $sfpath" |
| 194 | mkdir -p $sfpath |
| 195 | fi |
| 196 | echo "Copy $snd in apk" |
| 197 | cp $snd $sfpath |
| 198 | fi |
| 199 | else |
| 200 | echo "Error: file $snd not found !" |
| 201 | fi |
| 202 | done |
| 203 | if [ $PLATFORM = "Darwin" ]; then |
| 204 | sed -i '' 's,__CURRENT_ANDROID_PACKAGE__,com.'$APPNAME',g' $APPFOLDER/cpp/DspFaust.cpp |
| 205 | else |
| 206 | sed -i 's,__CURRENT_ANDROID_PACKAGE__,com.'$APPNAME',g' $APPFOLDER/cpp/DspFaust.cpp |
| 207 | fi |
| 208 | mv $BUILDDIR/app/CMakeLists_sndfile.txt $BUILDDIR/app/CMakeLists.txt |
| 209 | if [ $OSCCTRL -eq 1 ]; then |
| 210 | if [ $PLATFORM = "Darwin" ]; then |
| 211 | sed -i '' 's,set(OSCCTRL\ false),set(OSCCTRL\ true),g' $BUILDDIR/app/CMakeLists.txt |
| 212 | else |
| 213 | sed -i 's,set(OSCCTRL\ false),set(OSCCTRL\ true),g' $BUILDDIR/app/CMakeLists.txt |
| 214 | fi |
| 215 | fi |
| 216 | rm $APPNAME-tmp.txt |
| 217 | else |
| 218 | rm $BUILDDIR/app/CMakeLists_sndfile.txt |
| 219 | if [ $OSCCTRL -eq 1 ]; then |
| 220 | if [ $PLATFORM = "Darwin" ]; then |
| 221 | sed -i '' 's,set(OSCCTRL\ false),set(OSCCTRL\ true),g' $BUILDDIR/app/CMakeLists.txt |
| 222 | else |
| 223 | sed -i 's,set(OSCCTRL\ false),set(OSCCTRL\ true),g' $BUILDDIR/app/CMakeLists.txt |
| 224 | fi |
| 225 | fi |
| 226 | rm -r $BUILDDIR/app/lib # that's soooo dirty but that will do for now... |
| 227 | fi |
| 228 | rm $FILE.json |
| 229 | |
| 230 | # Run Gradle |
| 231 | cd $BUILDDIR |
| 232 | |
| 233 | # Try to use installed gradle instead of local ./gradlew in order |
| 234 | # to avoid potential problems when compiling in a shared folder |
| 235 | # where execution rights may not be granted (i.e. on a google VM) |
| 236 | # In order to install gradle-4.6: |
| 237 | # wget https://services.gradle.org/distributions/gradle-4.6-bin.zip |
| 238 | # unzip -d /opt/gradle gradle-4.6-bin.zip |
| 239 | |
| 240 | if [ -f /opt/gradle/gradle-4.10.1/bin/gradle ]; then |
| 241 | FAUSTGRADLE=${FAUSTGRADLE:=/opt/gradle/gradle-4.10.1/bin/gradle} |
| 242 | else |
| 243 | FAUSTGRADLE=${FAUSTGRADLE:=./gradlew} |
| 244 | chmod a+x ./gradlew |
| 245 | fi |
| 246 | |
| 247 | echo "USED GRADLE=$FAUSTGRADLE" > $LOG |
| 248 | |
| 249 | $FAUSTGRADLE assembleRelease > $LOG |
| 250 | cd .. |
| 251 | |
| 252 | cp -r $BUILDDIR/app/build/outputs/apk/release/app-release.apk $APPNAME.apk |
| 253 | |
| 254 | # **************** |
| 255 | # TREAT OPTIONS |
| 256 | # **************** |
| 257 | |
| 258 | if [ $INSTALL -eq 1 ]; then |
| 259 | adb install -r $APPNAME.apk |
| 260 | fi |
| 261 | |
| 262 | if [ $SOURCE -eq 1 ]; then |
| 263 | rm -rf faustApp |
| 264 | mv $BUILDDIR faustApp |
| 265 | echo "An Android studio project named faustApp was created." > $LOG |
| 266 | else |
| 267 | if [ $REUSE -eq 0 ]; then |
| 268 | echo "Delete Android project $BUILDDIR" > $LOG |
| 269 | rm -rf $BUILDDIR |
| 270 | fi |
| 271 | fi |
| 272 | |
| 273 | echo "$APPNAME.apk;" |