chris@160: #!/bin/bash chris@160: chris@160: # shell script for automatic uploading/compiling of pd patch onto bbb chris@160: # Christian Heinrichs 2015 chris@160: # chris@160: # example usage: sh upload-and-compile.sh -f bwg-tests -q -e chris@160: giuliomoro@203: trap "{ echo "";exit 0; }" SIGINT SIGTERM giuliomoro@203: chris@160: workingdir=".." chris@160: verbose="0" chris@160: render="0" chris@160: pdpath="" giuliomoro@203: WATCH="0" giuliomoro@203: FORCE="0" giuliomoro@203: #make sure the paths have the trailing / . giuliomoro@203: projectpath="../projects/heavy/hvtemp/" giuliomoro@203: BBB_PATH="~/BeagleRT/" chris@160: BBB_ADDRESS="root@192.168.7.2" chris@160: COMMAND_ARGS= chris@160: RUN_PROJECT=1 chris@160: RUN_IN_FOREGROUND=0 chris@160: RUN_WITHOUT_SCREEN=1 giuliomoro@203: BELA_PYTHON27= giuliomoro@203: giuliomoro@203: if [ -z "$BELA_PYTHON27" ]; then giuliomoro@203: for PY in python python2.7 ; do giuliomoro@203: python --version 2>&1 | grep "2\.7" &> /dev/null 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@203: to your python2.7 executable in the environmental variable $BELA_PYTHON27" giuliomoro@203: exit 1; giuliomoro@203: fi; giuliomoro@203: chris@160: chris@160: function usage chris@160: { giuliomoro@203: printf "\nUSAGE: build_pd.sh [[-i input folder containing _main.pd file ]\ giuliomoro@203: [-o output folder for new heavy project .c files (default ../projects/heavy/hvtemp)]\ giuliomoro@203: [-b bbb path to copy to (default ~/BeagleRT)] | [-h] | [-f|--force] | [-w|--watch]\n" giuliomoro@203: printf "\nexample: build_pd.sh -i ../projects/heavy/pd/hello-world -o ../projects/heavy/hello-world\n" giuliomoro@203: echo "If --watch is selected, the script will check every 1s for any file that is modified in the source folder, which triggers\ giuliomoro@203: the building process and runs the process. giuliomoro@203: If --screen is selected, the prompt returns to the user after launching BeagleRT in a screen on the target device. giuliomoro@203: If --screen and --watch are combined, while the process is running in the screen, modifications to the source files will \ giuliomoro@203: still trigger a new build." chris@160: } chris@160: chris@160: while [ "$1" != "" ]; do chris@160: case $1 in chris@160: -b | --bbb ) shift chris@160: BBB_PATH=$1 chris@160: ;; giuliomoro@203: -f | --force) FORCE="1" giuliomoro@203: ;; chris@160: -i | --input ) shift chris@160: pdpath=$1 chris@160: ;; chris@160: -o | --output ) shift chris@160: projectpath=$1 chris@160: ;; giuliomoro@203: -v | --verbose ) verbose=1 chris@160: ;; chris@160: -r | --render ) shift chris@160: render=1 chris@160: ;; giuliomoro@203: -s | --screen ) RUN_WITHOUT_SCREEN="0" giuliomoro@203: ;; giuliomoro@203: -w | --watch ) WATCH=1 giuliomoro@203: ;; chris@160: -h | --help ) usage chris@160: exit chris@160: ;; chris@160: * ) usage chris@160: exit 1 chris@160: esac chris@160: shift chris@160: done chris@160: giuliomoro@203: function hasNotChanged(){ giuliomoro@203: if [ $WATCH -eq 0 ]; giuliomoro@203: then giuliomoro@214: echo "Files in the source folder did not change since the last build, use --force to force recompiling"; giuliomoro@203: exit 0; giuliomoro@203: fi; giuliomoro@203: } chris@190: giuliomoro@203: function checkChanged(){ giuliomoro@203: PD_BACKUP_PATH="hvresources/patch_back/" giuliomoro@203: if [ $FORCE -eq 1 ] && [ $WATCH -eq 0 ]; giuliomoro@203: then giuliomoro@203: rm -rf $PD_BACKUP_PATH; giuliomoro@203: return; giuliomoro@203: fi; giuliomoro@203: mkdir -p $PD_BACKUP_PATH; giuliomoro@203: # count files that have changed giuliomoro@203: HAS_CHANGED=$(rsync -nac --out-format="%f" "$pdpath" $PD_BACKUP_PATH | grep -v "\/\.$"| wc -l); giuliomoro@203: if [ $HAS_CHANGED -eq 0 ] && [ $FORCE -eq 0 ]; giuliomoro@203: then giuliomoro@203: hasNotChanged; giuliomoro@203: return 1; giuliomoro@203: fi; giuliomoro@203: #if we are here and $FORCE==1, it means that $WATCH==1 giuliomoro@203: # so let's make sure only the first run get forced: giuliomoro@203: FORCE=0; giuliomoro@203: echo "Files have changed" giuliomoro@203: # otherwise back up the files that have changed giuliomoro@203: rsync -vac "$pdpath" $PD_BACKUP_PATH giuliomoro@203: giuliomoro@203: return 0; giuliomoro@203: } giuliomoro@203: giuliomoro@203: function checkUploadBuildRun(){ giuliomoro@203: checkChanged || return # exits if source files have not changed giuliomoro@203: giuliomoro@203: # remove old static files to avoid obsolete errors giuliomoro@203: # use -rf to prevent warnings in case they do not exist giuliomoro@203: rm -rf "$projectpath"/Hv* "$projectpath"/Message* "$projectpath"/Control* "$projectpath"/Signal* &>/dev/null giuliomoro@203: giuliomoro@203: # invoke the online compiler giuliomoro@203: "$BELA_PYTHON27" hvresources/uploader.py "$pdpath"/ -n bbb -g c -o "$projectpath"; chris@160: if [ $? -ne 0 ]; then giuliomoro@203: #echo "ERROR: an error occurred while executing the uploader.py script" giuliomoro@203: echo "error" giuliomoro@203: exit 1 chris@160: fi; chris@160: giuliomoro@203: echo ""; giuliomoro@203: #echo "*|*|* Successfully uploaded and converted pd patch into super-fast optimized C code. Brought to you by Heavy! *|*|*"; giuliomoro@203: echo ""; giuliomoro@203: giuliomoro@203: # check how to copy/sync render.cpp file... giuliomoro@203: if [ $render -eq 0 ]; then chris@190: cp "hvresources/render.cpp" $projectpath/; giuliomoro@203: fi; chris@160: giuliomoro@203: cp "hvresources/HvUtils.h" $projectpath/; chris@190: giuliomoro@203: echo "updating files on board..." chris@190: giuliomoro@203: rsync -c -rv --exclude 'HvContext*' "$projectpath"/ "$BBB_ADDRESS":"$BBB_PATH"/source; giuliomoro@203: # rsync -c -rv "$projectpath"/ "$BBB_ADDRESS":"$BBB_PATH"/source; chris@190: giuliomoro@203: # for whatever reason these big files used to hang when transferring with rsync giuliomoro@203: scp "$projectpath"/HvContext* "$BBB_ADDRESS":"$BBB_PATH"/source; chris@160: giuliomoro@203: if [ $? -ne 0 ]; then chris@160: echo ""; chris@160: echo ":( :( :( ERROR: while synchronizing files with the BBB. Is the board connected and the correct SD card inserted? :( :( :("; chris@160: echo ""; chris@160: exit 1; giuliomoro@203: fi; giuliomoro@203: # exit giuliomoro@203: #produce a list of files which content has changed (not just the date) giuliomoro@203: #TODO: could be made faster (perhaps) by backing up the folder locally instead of bbb giuliomoro@203: # UPDATED_FILES=`rsync -naic --log-format="%f" "$projectpath" "$BBB_PATH"/source | grep -v "\.$"` giuliomoro@203: # echo "UPDATEDFILES : $UPDATED_FILES" giuliomoro@203: # exit 2 giuliomoro@203: # remove old executable and heavy context .o/.d files giuliomoro@203: ssh $BBB_ADDRESS "rm -rf $BBB_PATH/BeagleRT $BBB_PATH/build/source/HvContext_bbb.d $BBB_PATH/build/source/HvContext_bbb.o $BBB_PATH/build/source/render.o $BBB_PATH/build/source/render.d"; giuliomoro@203: SCREEN_NAME=BeagleRT giuliomoro@203: KILL_RUNNING_PROCESS="bash -c 'kill -s 2 \`pidof BeagleRT\` 2>/dev/null; screen -r $SCREEN_NAME -X quit 2>/dev/null; sleep 0.5; exit 0'" #always returns true giuliomoro@203: # Make new BeagleRT 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@203: if [ $RUN_PROJECT -eq 0 ] giuliomoro@203: then giuliomoro@203: echo "Building project..." giuliomoro@203: ssh $BBB_ADDRESS "make all -C $BBB_PATH" giuliomoro@203: else giuliomoro@203: echo "Building and running project..." giuliomoro@203: if [ $RUN_WITHOUT_SCREEN -eq 1 ] giuliomoro@203: then giuliomoro@203: ssh -t $BBB_ADDRESS "make all -C $BBB_PATH && $KILL_RUNNING_PROCESS && $BBB_PATH/BeagleRT $COMMAND_ARGS" giuliomoro@203: elif [ $RUN_IN_FOREGROUND -eq 1 ] giuliomoro@203: then giuliomoro@203: # Run in screen without detaching giuliomoro@203: ssh -t $BBB_ADDRESS "make all -C $BBB_PATH && $KILL_RUNNING_PROCESS && screen -S $SCREEN_NAME $BBB_PATH/BeagleRT $COMMAND_ARGS" giuliomoro@203: else giuliomoro@203: # Run in screen and detach giuliomoro@203: ssh $BBB_ADDRESS "make all -C $BBB_PATH && $KILL_RUNNING_PROCESS && screen -dmS $SCREEN_NAME $BBB_PATH/BeagleRT $COMMAND_ARGS" giuliomoro@203: fi giuliomoro@203: fi giuliomoro@203: } #checkUploadBuildRun giuliomoro@203: giuliomoro@203: checkUploadBuildRun giuliomoro@203: giuliomoro@203: if [ $WATCH -ne 0 ]; then giuliomoro@203: WAIT_STRING="\rWaiting for changes in $pdpath" giuliomoro@203: while true; do giuliomoro@203: printf "$WAIT_STRING " giuliomoro@203: sleep 0.3 giuliomoro@203: printf "$WAIT_STRING. " giuliomoro@203: sleep 0.3 giuliomoro@203: printf "$WAIT_STRING.. " giuliomoro@203: sleep 0.3 giuliomoro@203: printf "$WAIT_STRING..." giuliomoro@203: sleep 0.3 giuliomoro@203: checkUploadBuildRun giuliomoro@203: done; chris@160: fi; chris@160: #ssh -t root@192.168.7.2 "kill -s 2 \`pidof heavy_template\` 2>/dev/null; sleep 0.5; rm -f ~/$filename_bbb/Release/source/heavy/HvContext_bbb.? ~/$filename_bbb/Release/heavy_template && make all -C ~/$filename_bbb/Release" &>/dev/null