giuliomoro@425: #!/bin/sh giuliomoro@523: # This script uploads Pd patches to Enzienaudio's server and compiles them on Bela chris@160: chris@160: pdpath="" giuliomoro@541: release=1 giuliomoro@398: NO_UPLOAD="0" giuliomoro@203: WATCH="0" giuliomoro@203: FORCE="0" giuliomoro@203: #make sure the paths have the trailing / . giuliomoro@295: BBB_DEFAULT_PROJECT_NAME="heavyProject" giuliomoro@203: BELA_PYTHON27= giuliomoro@203: giuliomoro@462: SCRIPTDIR=$(dirname "$0") giuliomoro@462: [ -z $SCRIPTDIR ] && SCRIPTDIR="./" || SCRIPTDIR=$SCRIPTDIR/ giuliomoro@481: [ -z $HVRESOURCES_DIR ] && HVRESOURCES_DIR=$SCRIPTDIR/hvresources/ giuliomoro@462: . $SCRIPTDIR.bela_common || { echo "You must be in Bela/scripts to run these scripts" | exit 1; } giuliomoro@447: giuliomoro@511: projectpath="$SCRIPTDIR/../tmp/heavy/hvtemp/" giuliomoro@511: giuliomoro@203: if [ -z "$BELA_PYTHON27" ]; then giuliomoro@203: for PY in python python2.7 ; do giuliomoro@428: python --version 2>&1 | grep "2\.7" >/dev/null 2>&1 giuliomoro@203: if [ $? -eq 0 ]; then giuliomoro@203: BELA_PYTHON27=$PY giuliomoro@203: break; giuliomoro@203: fi; giuliomoro@203: done; giuliomoro@203: fi; giuliomoro@203: giuliomoro@203: if [ -z "$BELA_PYTHON27" ]; then giuliomoro@203: echo "It looks like you might not have python2.7 installed. If you do, please specify the path giuliomoro@403: to your python2.7 executable in the environmental variable \$BELA_PYTHON27" giuliomoro@203: exit 1; giuliomoro@203: fi; giuliomoro@203: chris@160: giuliomoro@430: usage () chris@160: { giuliomoro@525: build_script_usage_brief ' [-o] [--noupload] [-r|--release release] ' giuliomoro@523: echo " giuliomoro@525: example: build_pd.sh -o ../projects/heavy/hello-world ../projects/heavy/pd/hello-world giuliomoro@525: giuliomoro@541: -r allows to build against a specific Heavy release. Default is: $release (stable) giuliomoro@541: ( see revision list here https://enzienaudio.com/a/releases ) giuliomoro@403: " giuliomoro@523: build_script_usage chris@160: } chris@160: giuliomoro@523: giuliomoro@523: COMMAND_ARGS= giuliomoro@523: RUN_PROJECT=1 giuliomoro@523: RUN_MODE=foreground giuliomoro@523: giuliomoro@523: [ $# -lt 2 ] && { giuliomoro@523: [ -d $1 ] || { usage; exit; } giuliomoro@523: } giuliomoro@523: while [ "$2" != "" ]; do chris@160: case $1 in giuliomoro@523: -c) giuliomoro@523: shift giuliomoro@523: COMMAND_ARGS="$1" giuliomoro@523: ;; giuliomoro@523: -b) giuliomoro@523: RUN_MODE=screen giuliomoro@523: ;; giuliomoro@523: -f) giuliomoro@523: RUN_MODE=foreground giuliomoro@523: ;; giuliomoro@523: -s) giuliomoro@523: RUN_MODE=screenfg giuliomoro@523: ;; giuliomoro@523: -n) giuliomoro@523: RUN_PROJECT=0 giuliomoro@523: ;; giuliomoro@523: -p) giuliomoro@523: shift giuliomoro@523: BBB_PROJECT_NAME="$1" giuliomoro@523: ;; giuliomoro@523: --clean) giuliomoro@525: BBB_MAKEFILE_OPTIONS="$BBB_MAKEFILE_OPTIONS projectclean" giuliomoro@523: ;; giuliomoro@523: -m) giuliomoro@523: shift giuliomoro@525: BBB_MAKEFILE_OPTIONS="$BBB_MAKEFILE_OPTIONS $1" giuliomoro@523: ;; giuliomoro@523: --watch) giuliomoro@523: WATCH=1 giuliomoro@523: ;; giuliomoro@523: -o | --output ) giuliomoro@523: shift giuliomoro@523: projectpath=$1 giuliomoro@523: ;; giuliomoro@523: -r | --release ) giuliomoro@523: shift giuliomoro@523: release=$1 giuliomoro@523: ;; giuliomoro@523: -n | --noupload ) giuliomoro@523: NO_UPLOAD=1 giuliomoro@523: ;; giuliomoro@523: -h | --help | -\?) giuliomoro@523: usage giuliomoro@523: exit giuliomoro@523: ;; giuliomoro@523: *) giuliomoro@523: usage giuliomoro@523: exit 1 chris@160: esac chris@160: shift chris@160: done giuliomoro@523: pdpath=$1 chris@160: giuliomoro@523: [ "$NO_UPLOAD" -eq 0 ] && [ -z "$pdpath" ] && { echo "Error: a path to the source folder should be provided"; exit 1; } giuliomoro@492: [ -z "$ENZIENAUDIO_COM_PATCH_NAME" ] && ENZIENAUDIO_COM_PATCH_NAME=bela giuliomoro@442: giuliomoro@439: if [ -z "$release" ] giuliomoro@403: then giuliomoro@403: RELEASE_STRING= giuliomoro@403: else giuliomoro@403: RELEASE_STRING="-r $release" giuliomoro@403: fi chris@190: giuliomoro@523: #truncated the destination folder if it does not exist" giuliomoro@431: mkdir -p "$projectpath" giuliomoro@431: giuliomoro@442: # These files will be cleared from $projectpath before calling uploader.py giuliomoro@442: #TODO: get a reliable, exhaustive, up-to-date list. giuliomoro@488: HEAVY_FILES='Heavy* Hv*' giuliomoro@445: giuliomoro@549: check_board_alive giuliomoro@445: set_date giuliomoro@445: reference_time_file="$projectpath"/ giuliomoro@490: giuliomoro@430: uploadBuildRun(){ giuliomoro@398: if [ $NO_UPLOAD -eq 0 ]; then giuliomoro@398: # remove old static files to avoid obsolete errors giuliomoro@549: # make sure the path is not empty, so avoiding to rm -rf / by mistake giuliomoro@549: [ -z $projectpath ] && { echo 'ERROR: $projectpath is empty.'; exit 0; } giuliomoro@398: # use -rf to prevent warnings in case they do not exist giuliomoro@442: for file in $HEAVY_FILES giuliomoro@549: do giuliomoro@549: rm -rf "$projectpath"/$file giuliomoro@549: done giuliomoro@549: giuliomoro@549: echo "Invoking the online compiler..." giuliomoro@398: # invoke the online compiler giuliomoro@481: "$BELA_PYTHON27" $HVRESOURCES_DIR/uploader.py "$pdpath"/ -n $ENZIENAUDIO_COM_PATCH_NAME -g c -o "$projectpath" $RELEASE_STRING ||\ giuliomoro@549: { echo "ERROR: an error occurred while executing the uploader.py script"; exit $?; } chris@160: fi; chris@160: giuliomoro@203: echo ""; giuliomoro@442: giuliomoro@442: # Test that files have been retrieved from the online compiler. giuliomoro@549: # TODO: skip this now that uplodaer.py returns meaningful exit codes giuliomoro@442: for file in $HEAVY_FILES; giuliomoro@442: do giuliomoro@523: ls "$projectpath"/$file >/dev/null 2>&1 || { giuliomoro@523: [ $NO_UPLOAD -eq 0 ] && printf "The online compiler did not return all the files or failed without notice, please try again and/or change HEAVY_FILES to be less strict.\n\n" ||\ giuliomoro@523: printf "Folder $projectpath does not contain a valid Heavy project\n"; giuliomoro@523: exit 1; } giuliomoro@442: done giuliomoro@481: giuliomoro@442: # Apply any Bela-specific patches here giuliomoro@481: cp "$HVRESOURCES_DIR/HvUtils.h" $projectpath/ || exit 1; giuliomoro@203: giuliomoro@398: BBB_PROJECT_FOLDER=$BBB_PROJECT_HOME"/"$BBB_PROJECT_NAME #make sure there is no trailing slash here giuliomoro@398: BBB_NETWORK_TARGET_FOLDER=$BBB_ADDRESS:$BBB_PROJECT_FOLDER giuliomoro@398: giuliomoro@203: # check how to copy/sync render.cpp file... giuliomoro@473: # check if custom heavy/render.cpp file is provided in the input folder giuliomoro@442: # TODO: extend this to all non-Pd files giuliomoro@473: CUSTOM_RENDER_SOURCE_PATH="$pdpath/heavy/render.cpp" giuliomoro@442: if [ -f "$CUSTOM_RENDER_SOURCE_PATH" ]; then giuliomoro@473: echo "Found custom heavy/render.cpp file in input folder, using that one instead of the default one."; giuliomoro@479: cp "$CUSTOM_RENDER_SOURCE_PATH" "$projectpath/render.cpp" || exit 1 giuliomoro@398: else giuliomoro@510: echo "Using Heavy default render.cpp" giuliomoro@510: cp "$HVRESOURCES_DIR/render.cpp" "$projectpath/render.cpp" || exit 1 giuliomoro@442: fi giuliomoro@442: giuliomoro@442: echo "Updating files on board..." giuliomoro@442: # HvContext* files tend to hang when transferring with rsync because they are very large and -c checksum takes a lot, I guess giuliomoro@445: giuliomoro@445: touch $reference_time_file giuliomoro@445: # Transfer the files giuliomoro@492: rsync -ac --out-format=" %n" --no-t --delete-during --exclude='HvContext_'$ENZIENAUDIO_COM_PATCH_NAME'.*' --exclude=build --exclude=$BBB_PROJECT_NAME "$projectpath"/ "$BBB_NETWORK_TARGET_FOLDER" &&\ giuliomoro@445: { [ $NO_UPLOAD -eq 1 ] || scp "$projectpath"/HvContext* $BBB_NETWORK_TARGET_FOLDER; } ||\ giuliomoro@442: { echo "ERROR: while synchronizing files with the BBB. Is the board connected?"; exit 1; } chris@160: giuliomoro@445: # TODO: rsync should upload a list of modified files, so that the corresponding objects can be deleted giuliomoro@492: # TODO: this should be run only when Heavy_bela.h changes. Otherwise render is recompiled every time for no good reason giuliomoro@445: #ssh $BBB_ADDRESS "rm -rf ${BBB_PROJECT_FOLDER}/build/render.*" chris@190: giuliomoro@203: #produce a list of files which content has changed (not just the date) giuliomoro@492: # remove old executable to force re-linking giuliomoro@445: #if [ $NO_UPLOAD -eq 0 ]; then giuliomoro@445: # ssh $BBB_ADDRESS "rm -rf "$BBB_PROJECT_FOLDER/$BBB_PROJECT_NAME; giuliomoro@445: #fi; giuliomoro@442: # Make new Bela executable and run giuliomoro@203: # It does not look very nice that we type the same things over and over giuliomoro@203: # but that is because each line is an ssh session in its own right giuliomoro@525: MAKE_COMMAND="make --no-print-directory QUIET=true -C $BBB_BELA_HOME PROJECT='$BBB_PROJECT_NAME' CL='$COMMAND_ARGS' $BBB_MAKEFILE_OPTIONS" giuliomoro@203: if [ $RUN_PROJECT -eq 0 ] giuliomoro@203: then giuliomoro@203: echo "Building project..." giuliomoro@329: ssh $BBB_ADDRESS "$MAKE_COMMAND" giuliomoro@203: else giuliomoro@523: case $RUN_MODE in giuliomoro@523: # Sorry for repeating the options, but "ssh / ssh -t" makes things complicated giuliomoro@523: foreground) giuliomoro@523: ssh -t $BBB_ADDRESS "$MAKE_COMMAND run" giuliomoro@523: ;; giuliomoro@523: screen) giuliomoro@523: ssh $BBB_ADDRESS "$MAKE_COMMAND runscreen" giuliomoro@523: ;; giuliomoro@523: screenfg) giuliomoro@523: ssh -t $BBB_ADDRESS "$MAKE_COMMAND runscreenfg" giuliomoro@523: ;; giuliomoro@523: *) giuliomoro@523: echo $RUN_MODE giuliomoro@523: error giuliomoro@523: ;; giuliomoro@523: esac giuliomoro@203: fi giuliomoro@403: } #uploadBuildRun giuliomoro@203: giuliomoro@403: uploadBuildRun giuliomoro@203: giuliomoro@203: if [ $WATCH -ne 0 ]; then giuliomoro@445: BACK_NO_UPLOAD=$NO_UPLOAD giuliomoro@445: while true giuliomoro@445: do giuliomoro@481: # actually we are watching multiple paths : $pdpath and $HVRESOURCES_DIR giuliomoro@481: # so that it is easier to edit hvresources code without risk of being giuliomoro@481: # overwritten, but we avoid mentioning it to the end user, otherwise they giuliomoro@481: # get confused. giuliomoro@445: echo "Waiting for changes in $pdpath, or press ctrl-c to terminate" giuliomoro@481: while sleep 1 giuliomoro@481: do giuliomoro@481: folder_has_changed "$pdpath" "$reference_time_file" && { giuliomoro@481: echo "Content of $pdpath has changed" giuliomoro@481: break giuliomoro@481: } giuliomoro@481: folder_has_changed "$HVRESOURCES_DIR" "$reference_time_file" && { giuliomoro@511: echo "Content of "$HVRESOURCES_DIR" has changed" giuliomoro@481: break giuliomoro@481: } giuliomoro@481: done giuliomoro@445: echo "Files changed" giuliomoro@445: # if .pd files did not change, no point in re-uploading giuliomoro@445: folder_has_changed "$pdpath" "$reference_time_file" "\.pd" &&\ giuliomoro@445: NO_UPLOAD=$BACK_NO_UPLOAD || NO_UPLOAD=1 giuliomoro@445: uploadBuildRun giuliomoro@445: done chris@160: fi;