andrewm@58: #!/bin/bash
andrewm@58: #
andrewm@58: # This script compiles a BeagleRT project on the BeagleBone Black and
andrewm@58: # optionally runs it. Pass a directory path in the first argument. 
andrewm@58: # The source files in this directory are copied to the board and compiled.
andrewm@58: 
giuliomoro@262: # set defaults unless variables are already set
giuliomoro@262: [ -z "$BBB_ADDRESS" ] && BBB_ADDRESS="root@192.168.7.2"
giuliomoro@266: [ -z "$BBB_BELA_HOME" ] && BBB_BELA_HOME="~/BeagleRT/"
giuliomoro@266: [ -z "$BBB_SCREEN_NAME" ] && BBB_SCREEN_NAME="BeagleRT"
giuliomoro@262: [ -z "$RUN_PROJECT" ] && RUN_PROJECT=1
giuliomoro@262: [ -z "$COMMAND_ARGS" ] && COMMAND_ARGS=
giuliomoro@262: [ -z "$RUN_IN_FOREGROUND" ] && RUN_IN_FOREGROUND=1
giuliomoro@262: [ -z "$RUN_WITHOUT_SCREEN" ] && RUN_WITHOUT_SCREEN=0
giuliomoro@264: [ -z "$BBB_PROJECT_HOME" ] && BBB_PROJECT_HOME="${BBB_BELA_HOME}/projects/"
giuliomoro@277: [ -z "$BBB_DEFAULT_PROJECT_NAME" ] && BBB_DEFAULT_PROJECT_NAME="scriptUploadedProject"
giuliomoro@277: [ -z "$BBB_PROJECT_NAME" ] && BBB_PROJECT_NAME=$BBB_DEFAULT_PROJECT_NAME
andrewm@58: 
andrewm@58: function usage
andrewm@58: {
andrewm@58:     THIS_SCRIPT=`basename "$0"`
andrewm@90:     echo "Usage: $THIS_SCRIPT [-b path-on-beaglebone] [-c command-line-args] [-nfF] <directory-with-source-files>"
andrewm@58:     echo "
andrewm@58:     This script copies a directory of source files to the BeagleBone, compiles
andrewm@58:     and runs it. The BeagleRT core files should have first been copied over
andrewm@58:     using the setup_board.sh script supplied with BeagleRT.
andrewm@58: 
andrewm@58:     The source directory should contain at least one .c, .cpp or .S file.
andrewm@58:     If the argument -n is passed, the output will not be run after compiling.
andrewm@58:     The argument -b will change the local path on the BeagleBone where the
andrewm@60:     BeagleRT files are found. The -c option passes command-line arguments to
andrewm@90:     the BeagleRT program; enclose the argument string in quotes.
andrewm@90: 	
giuliomoro@263:     By default, the project runs in the foreground of the current terminal,
andrewm@90:     within a screen session that can be detached later. The -F argument runs
giuliomoro@263:     the project in the foreground of the current terminal, without screen, so
giuliomoro@263:     the output can be piped to another destination. The -f argument runs it
giuliomoro@263:     in a screen in the background, so no output is shown."
andrewm@58: }
andrewm@58: 
andrewm@58: OPTIND=1
andrewm@58: 
giuliomoro@277: while getopts "b:c:nfFhp:" opt; do
andrewm@58:     case $opt in
giuliomoro@264:         b)            BBB_BELA_HOME=$OPTARG
andrewm@58:                       ;;
andrewm@60:         c)            COMMAND_ARGS=$OPTARG
andrewm@60:                       ;;
giuliomoro@264:         f)            RUN_IN_FOREGROUND=0
giuliomoro@277:                       ;;
giuliomoro@264:         F)            RUN_WITHOUT_SCREEN=1
giuliomoro@277:                       ;;
andrewm@58:         n)    	      RUN_PROJECT=0
andrewm@58:                       ;;
giuliomoro@277: 	p)            BBB_PROJECT_NAME=$OPTARG
giuliomoro@277: 		      ;;	
andrewm@58:         h|\?)         usage
andrewm@58:                       exit 1
andrewm@58:     esac
andrewm@58: done
andrewm@58: 
andrewm@58: shift $((OPTIND-1))
andrewm@58: 
andrewm@58: # Check that we have a directory containing at least one source file
andrewm@58: # as an argument
giuliomoro@64: 
andrewm@58: if [ -z "$1" ]
andrewm@58: then
andrewm@58:     usage
andrewm@58:     exit
andrewm@58: fi
andrewm@58: 
giuliomoro@64: FIND_STRING="find $* -maxdepth 10000 -type f "
giuliomoro@64: 
giuliomoro@64: C_FILES=$($FIND_STRING -name "*.c")
giuliomoro@64: CPP_FILES=$($FIND_STRING -name "*.cpp")
giuliomoro@64: ASM_FILES=$($FIND_STRING -name "*.S")
andrewm@58: 
andrewm@58: if [[ -z $C_FILES ]] && [[ -z $CPP_FILES ]] && [[ -z $ASM_FILES ]]
andrewm@58: then
andrewm@58:     echo "Please provide a directory containing .c, .cpp or .S files."
andrewm@58: #    echo "Usage: $THIS_SCRIPT [directory-with-source-files]"
andrewm@58:     usage
andrewm@58:     exit
andrewm@58: fi
andrewm@58: 
giuliomoro@275: BBB_PROJECT_FOLDER=$BBB_PROJECT_HOME"/"$BBB_PROJECT_NAME #make sure there is no trailing slash here
giuliomoro@266: BBB_NETWORK_TARGET_FOLDER=$BBB_ADDRESS:$BBB_PROJECT_FOLDER
giuliomoro@266: 
andrewm@58: # Stop BeagleRT and clean out old source files
giuliomoro@261: echo "Stopping running program..."
giuliomoro@261: # sets the date, stops the running process
giuliomoro@266: ssh $BBB_ADDRESS "date -s '`date`' > /dev/null; mkdir -p $BBB_PROJECT_FOLDER; screen -X -S '"$BBB_SCREEN_NAME"' quit &>/dev/null;"
giuliomoro@64: 
giuliomoro@64: #concatenate arguments to form path.
giuliomoro@264: HOST_SOURCE_PATH= #initially empty, will be filled with input arguments
giuliomoro@64: for i in "$@" #parse input arguments
giuliomoro@64: do
giuliomoro@275:   HOST_SOURCE_PATH+=" $1"
giuliomoro@64:   shift
giuliomoro@64:   # Copy new souce files to the board
giuliomoro@64: done
andrewm@58: 
andrewm@58: # Copy new source files to the board
andrewm@58: echo "Copying new source files to BeagleBone..."
giuliomoro@264: if [ -z `which rsync` ];
giuliomoro@264: then
giuliomoro@264:     #if rsync is not available, brutally clean the destination folder
giuliomoro@264:     #and copy over all the files again and recompile them
giuliomoro@266:     ssh bbb "make -C $BBB_BELA_HOME sourceclean PROJECT=$BBB_PROJECT_NAME";
giuliomoro@266:     scp $HOST_SOURCE_PATH "$BBB_NETWORK_TARGET_FOLDER"
giuliomoro@264: else
giuliomoro@264:     #rsync --delete makes sure it removes files that are not in the origin folder
giuliomoro@275:     rsync -av --delete-after --exclude=build $HOST_SOURCE_PATH"/" "$BBB_NETWORK_TARGET_FOLDER/" #trailing slashes used here make sure rsync does not create another folder inside the target folder
giuliomoro@264: fi;
giuliomoro@64: 
giuliomoro@64: if [ $? -ne 0 ]
giuliomoro@64: then
giuliomoro@64: 	echo "Error while copying files"
giuliomoro@64: 	exit
giuliomoro@64: fi
andrewm@58: 
andrewm@58: # Make new BeagleRT executable and run
andrewm@58: if [ $RUN_PROJECT -eq 0 ]
andrewm@58: then
andrewm@58:     echo "Building project..."
giuliomoro@266:     ssh $BBB_ADDRESS "make all -C $BBB_BELA_HOME PROJECT=$BBB_PROJECT_NAME"
andrewm@58: else
andrewm@58:     echo "Building and running project..."
andrewm@90: 	
andrewm@90: 	if [ $RUN_WITHOUT_SCREEN -ne 0 ]
andrewm@90: 	then
giuliomoro@266: 		ssh -t $BBB_ADDRESS "cd $BBB_BELA_HOME && make all && cd source && $BBB_PROJECT_FOLDER/$BBB_PROJECT_NAME $COMMAND_ARGS"	
andrewm@90: 	elif [ $RUN_IN_FOREGROUND -eq 0 ]
andrewm@90: 	then
giuliomoro@266: 	    ssh $BBB_ADDRESS "cd $BBB_BELA_HOME && make all PROJECT=$BBB_PROJECT_NAME && cd source && screen -S $BBB_SCREEN_NAME -d -m $BBB_PROJECT_FOLDER/$BBB_PROJECT_NAME $COMMAND_ARGS"
andrewm@90: 	else
giuliomoro@266: 	    ssh -t $BBB_ADDRESS "cd $BBB_BELA_HOME && make all PROJECT=$BBB_PROJECT_NAME && cd source && screen -S $BBB_SCREEN_NAME $BBB_PROJECT_FOLDER/$BBB_PROJECT_NAME $COMMAND_ARGS"
andrewm@90: 	fi
giuliomoro@249: fi