annotate scripts/build_project.sh @ 519:5406a719910b prerelease

Restored wav files corrupted in 2a8afa6fa118
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 22 Jun 2016 21:38:43 +0100
parents 8e48dd7c8c23
children 42f6af3a5f1d
rev   line source
giuliomoro@425 1 #!/bin/sh
andrewm@58 2 #
giuliomoro@377 3 # This script compiles a Bela project on the BeagleBone Black and
andrewm@58 4 # optionally runs it. Pass a directory path in the first argument.
andrewm@58 5 # The source files in this directory are copied to the board and compiled.
andrewm@58 6
giuliomoro@262 7 # set defaults unless variables are already set
giuliomoro@445 8
giuliomoro@462 9 SCRIPTDIR=$(dirname "$0")
giuliomoro@462 10 [ -z $SCRIPTDIR ] && SCRIPTDIR="./" || SCRIPTDIR=$SCRIPTDIR/
giuliomoro@462 11 . $SCRIPTDIR.bela_common || { echo "You must be in Bela/scripts to run these scripts" | exit 1; }
andrewm@58 12
giuliomoro@512 13 WATCH="0"
giuliomoro@430 14 usage()
andrewm@58 15 {
giuliomoro@512 16 THIS_SCRIPT=`basename "$0"`
giuliomoro@512 17 echo "Usage: $THIS_SCRIPT [-c command-line-args] [-nbfF] <directory-with-source-files>"
giuliomoro@512 18 echo "
giuliomoro@512 19 This script copies a directory of source files to the BeagleBone, compiles
giuliomoro@512 20 and runs it. The Bela core files should have first been copied over
giuliomoro@512 21 using the setup_board.sh script supplied with Bela.
andrewm@58 22
giuliomoro@512 23 The source directory should contain at least one .c, .cpp or .S file.
giuliomoro@512 24 If the argument -n is passed, the output will not be run after compiling.
giuliomoro@512 25 The -c option passes command-line arguments to
giuliomoro@512 26 the Bela program; enclose the argument string in quotes.
giuliomoro@512 27
giuliomoro@512 28 -p arg : sets the name of the project to run (default: $BBB_PROJECT_NAME )
giuliomoro@512 29 -r arg : additional folder which contents will be copied to the destination folder. Use this, e.g.: for audio files or Pd/pyo sources
giuliomoro@512 30
giuliomoro@512 31 By default, the project runs in the foreground of the current terminal,
giuliomoro@512 32 within a screen session that can be detached later. The -f argument runs
giuliomoro@512 33 the project in the foreground of the current terminal, without screen, so
giuliomoro@512 34 the output can be piped to another destination. The -b argument runs it
giuliomoro@512 35 in a screen in the background, so no output is shown. The -m argument allows
giuliomoro@512 36 to pass arguments to the Makefile before the run target. For instance,
giuliomoro@512 37 pass -m \`"projectclean"\` or \`-m "distclean"\` to clean project-specific
giuliomoro@512 38 pre-built objects, or all the pre-built objects, respectively."
andrewm@58 39 }
andrewm@58 40
andrewm@58 41 OPTIND=1
andrewm@58 42
giuliomoro@512 43 while [ "$2" != "" ]; do
giuliomoro@512 44 case $1 in
giuliomoro@512 45 -c)
giuliomoro@512 46 shift
giuliomoro@512 47 COMMAND_ARGS="$1"
giuliomoro@512 48 ;;
giuliomoro@512 49 -b)
giuliomoro@512 50 RUN_IN_FOREGROUND=0
giuliomoro@512 51 ;;
giuliomoro@512 52 -f)
giuliomoro@512 53 RUN_WITHOUT_SCREEN=1
giuliomoro@512 54 ;;
giuliomoro@512 55 -n)
giuliomoro@512 56 RUN_PROJECT=0
giuliomoro@512 57 ;;
giuliomoro@512 58 -p)
giuliomoro@512 59 shift
giuliomoro@512 60 BBB_PROJECT_NAME="$1"
giuliomoro@512 61 ;;
giuliomoro@512 62 -r)
giuliomoro@512 63 shift
giuliomoro@512 64 # TODO: this option can probably be removed, replaced by including multiple paths as regular positional arguments at the end.
giuliomoro@512 65 ADDITIONAL_PATH="$1"
giuliomoro@512 66 ;;
giuliomoro@512 67 -m)
giuliomoro@512 68 shift
giuliomoro@512 69 BBB_MAKEFILE_OPTIONS="$1"
giuliomoro@512 70 ;;
giuliomoro@512 71 --watch)
giuliomoro@512 72 WATCH=1
giuliomoro@512 73 ;;
giuliomoro@512 74 -h|-\?)
giuliomoro@512 75 usage
giuliomoro@512 76 exit 0
giuliomoro@512 77 ;;
giuliomoro@512 78 *)
giuliomoro@512 79 usage
giuliomoro@512 80 exit 1
giuliomoro@512 81 ;;
giuliomoro@512 82 esac
giuliomoro@512 83 shift
andrewm@58 84 done
andrewm@58 85
andrewm@58 86
giuliomoro@439 87 #Only include all the files if the provided path is not empty
giuliomoro@439 88 [ -z "$ADDITIONAL_PATH" ] || ADDITIONAL_PATH="$ADDITIONAL_PATH/*"
giuliomoro@439 89
andrewm@58 90 # Check that we have a directory containing at least one source file
andrewm@58 91 # as an argument
giuliomoro@64 92
andrewm@58 93 if [ -z "$1" ]
andrewm@58 94 then
giuliomoro@512 95 usage
giuliomoro@512 96 exit 2
andrewm@58 97 fi
andrewm@58 98
giuliomoro@64 99 FIND_STRING="find $* -maxdepth 10000 -type f "
giuliomoro@472 100 EXTENSIONS_TO_FIND='\.cpp\|\.c\|\.S\|\.pd'
giuliomoro@472 101 FOUND_FILES=$($FIND_STRING | grep "$EXTENSIONS_TO_FIND")
giuliomoro@472 102 if [ -z "$FOUND_FILES" ]
andrewm@58 103 then
giuliomoro@512 104 printf "ERROR: Please provide a directory containing .c, .cpp, .S or .pd files.\n\n"
giuliomoro@512 105 exit
andrewm@58 106 fi
andrewm@58 107
giuliomoro@275 108 BBB_PROJECT_FOLDER=$BBB_PROJECT_HOME"/"$BBB_PROJECT_NAME #make sure there is no trailing slash here
giuliomoro@266 109 BBB_NETWORK_TARGET_FOLDER=$BBB_ADDRESS:$BBB_PROJECT_FOLDER
giuliomoro@266 110
giuliomoro@369 111 echo "Stopping running process..."
giuliomoro@369 112 # sets the date and stop running process
giuliomoro@443 113 ssh $BBB_ADDRESS "date -s \"`date '+%Y%m%d %T %Z'`\" > /dev/null; mkdir -p $BBB_PROJECT_FOLDER; make QUIET=true --no-print-directory -C $BBB_BELA_HOME stop"
giuliomoro@64 114
giuliomoro@64 115 #concatenate arguments to form path.
giuliomoro@264 116 HOST_SOURCE_PATH= #initially empty, will be filled with input arguments
giuliomoro@64 117 for i in "$@" #parse input arguments
giuliomoro@64 118 do
giuliomoro@512 119 HOST_SOURCE_PATH+=" $1"
giuliomoro@512 120 shift
giuliomoro@512 121 # Copy new souce files to the board
giuliomoro@64 122 done
andrewm@58 123
giuliomoro@512 124 # This file is used to keep track of when the last upload was made,
giuliomoro@512 125 # so to check for modifications if WATCH is active
giuliomoro@512 126 reference_time_file="$SCRIPTDIR/../tmp/"
giuliomoro@512 127 uploadBuildRun(){
giuliomoro@512 128 [ $WATCH -eq 1 ] && touch $reference_time_file
giuliomoro@512 129 # Copy new source files to the board
giuliomoro@512 130 echo "Copying new source files to BeagleBone..."
giuliomoro@512 131 if [ -z "`which rsync`" ];
giuliomoro@512 132 then
giuliomoro@512 133 #if rsync is not available, brutally clean the destination folder
giuliomoro@512 134 #and copy over all the files again and recompile them
giuliomoro@512 135 ssh bbb "make --no-print-directory -C $BBB_BELA_HOME sourceclean PROJECT=$BBB_PROJECT_NAME";
giuliomoro@512 136 scp -r $HOST_SOURCE_PATH $ADDITIONAL_PATH "$BBB_NETWORK_TARGET_FOLDER"
giuliomoro@512 137 else
giuliomoro@512 138 #rsync
giuliomoro@512 139 # --delete makes sure it removes files that are not in the origin folder
giuliomoro@512 140 # -c evaluates changes using md5 checksum instead of file date, so we don't care about time skews
giuliomoro@512 141 # --no-t makes sure file timestamps are not preserved, so that the Makefile will not think that targets are up to date when replacing files on the BBB
giuliomoro@512 142 # with older files from the host. This will solve 99% of the issues with Makefile thinking a target is up to date when it is not.
giuliomoro@512 143 rsync -ac --out-format=" %n" --no-t --delete-after --exclude=$BBB_PROJECT_NAME --exclude=build $HOST_SOURCE_PATH"/" $ADDITIONAL_PATH "$BBB_NETWORK_TARGET_FOLDER/" #trailing slashes used here make sure rsync does not create another folder inside the target folder
giuliomoro@512 144 fi
giuliomoro@64 145
giuliomoro@512 146 if [ $? -ne 0 ]
giuliomoro@512 147 then
giuliomoro@512 148 echo "Error while copying files"
giuliomoro@512 149 exit
giuliomoro@512 150 fi
giuliomoro@512 151
giuliomoro@512 152 # Make new Bela executable and run
giuliomoro@512 153 MAKE_COMMAND="make --no-print-directory QUIET=true -C $BBB_BELA_HOME PROJECT='$BBB_PROJECT_NAME' CL='$COMMAND_ARGS' $BBB_MAKEFILE_OPTIONS"
giuliomoro@512 154 if [ $RUN_PROJECT -eq 0 ]
giuliomoro@512 155 then
giuliomoro@512 156 echo "Building project..."
giuliomoro@512 157 ssh $BBB_ADDRESS "$MAKE_COMMAND"
giuliomoro@512 158 else
giuliomoro@512 159 echo "Building and running project..."
giuliomoro@512 160 if [ $WATCH -eq 1 ]
giuliomoro@512 161 then
giuliomoro@512 162 if [ $RUN_WITHOUT_SCREEN -eq 1 ];
giuliomoro@512 163 then
giuliomoro@512 164 # try to emulate run_without_screen: run with fifo
giuliomoro@512 165 ssh $BBB_ADDRESS "$MAKE_COMMAND runscreenfifo" & BACKGROUND_PROCESS_PID=$!
giuliomoro@512 166 # run this in the background, it will be killed anyhow when the process stops.
giuliomoro@512 167 # Either with the trap below or in another way
giuliomoro@512 168 trap "kill $BACKGROUND_PROCESS_PID; exit 0;" 2 9
giuliomoro@512 169 else
giuliomoro@512 170 ssh $BBB_ADDRESS "$MAKE_COMMAND runscreen"
giuliomoro@512 171 fi
giuliomoro@512 172 else
giuliomoro@512 173 if [ $RUN_WITHOUT_SCREEN -ne 0 ]
giuliomoro@512 174 then
giuliomoro@512 175 ssh -t $BBB_ADDRESS "$MAKE_COMMAND run"
giuliomoro@512 176 elif [ $RUN_IN_FOREGROUND -eq 0 ]
giuliomoro@512 177 then
giuliomoro@512 178 ssh $BBB_ADDRESS "$MAKE_COMMAND runscreen"
giuliomoro@512 179 else
giuliomoro@512 180 ssh -t $BBB_ADDRESS "$MAKE_COMMAND runscreenfg"
giuliomoro@512 181 fi
giuliomoro@512 182 fi
giuliomoro@512 183 fi
giuliomoro@512 184 }
giuliomoro@512 185 # run it once and then (maybe) start waiting for changes
giuliomoro@512 186 uploadBuildRun
giuliomoro@512 187
giuliomoro@512 188 if [ $WATCH -ne 0 ]; then
giuliomoro@512 189 while true
giuliomoro@512 190 do
giuliomoro@512 191 echo "Waiting for changes in $HOST_SOURCE_PATH, or press ctrl-c to terminate"
giuliomoro@512 192 CORE_DIR="$SCRIPTDIR/../core"
giuliomoro@512 193 INCLUDE_DIR="$SCRIPTDIR/../include"
giuliomoro@512 194 wait_for_change $HOST_SOURCE_PATH "$reference_time_file" && {
giuliomoro@512 195 echo "Content of "$HOST_SOURCE_PATH" has changed"
giuliomoro@512 196 }
giuliomoro@512 197 echo "Files changed"
giuliomoro@512 198 uploadBuildRun
giuliomoro@512 199 done
giuliomoro@64 200 fi