chris@160
|
1 #!/bin/bash
|
chris@160
|
2
|
chris@160
|
3 # shell script for automatic uploading/compiling of pd patch onto bbb
|
chris@160
|
4 # Christian Heinrichs 2015
|
chris@160
|
5 #
|
chris@160
|
6 # example usage: sh upload-and-compile.sh -f bwg-tests -q -e
|
chris@160
|
7
|
giuliomoro@203
|
8 trap "{ echo "";exit 0; }" SIGINT SIGTERM
|
giuliomoro@203
|
9
|
chris@160
|
10 workingdir=".."
|
chris@160
|
11 verbose="0"
|
chris@160
|
12 render="0"
|
chris@160
|
13 pdpath=""
|
giuliomoro@398
|
14 NO_UPLOAD="0"
|
giuliomoro@203
|
15 WATCH="0"
|
giuliomoro@203
|
16 FORCE="0"
|
giuliomoro@203
|
17 #make sure the paths have the trailing / .
|
giuliomoro@203
|
18 projectpath="../projects/heavy/hvtemp/"
|
giuliomoro@295
|
19 [ -z "$BBB_ADDRESS" ] && BBB_ADDRESS="root@192.168.7.2"
|
giuliomoro@398
|
20 [ -z "$BBB_BELA_HOME" ] && BBB_BELA_HOME="~/BeagleRT/"
|
giuliomoro@295
|
21 [ -z "$BBB_PROJECT_HOME" ] && BBB_PROJECT_HOME="${BBB_BELA_HOME}/projects/"
|
giuliomoro@295
|
22 BBB_DEFAULT_PROJECT_NAME="heavyProject"
|
giuliomoro@295
|
23 [ -z "$BBB_PROJECT_NAME" ] && BBB_PROJECT_NAME=$BBB_DEFAULT_PROJECT_NAME
|
chris@160
|
24 COMMAND_ARGS=
|
chris@160
|
25 RUN_PROJECT=1
|
chris@160
|
26 RUN_IN_FOREGROUND=0
|
chris@160
|
27 RUN_WITHOUT_SCREEN=1
|
giuliomoro@203
|
28 BELA_PYTHON27=
|
giuliomoro@203
|
29
|
giuliomoro@203
|
30 if [ -z "$BELA_PYTHON27" ]; then
|
giuliomoro@203
|
31 for PY in python python2.7 ; do
|
giuliomoro@203
|
32 python --version 2>&1 | grep "2\.7" &> /dev/null
|
giuliomoro@203
|
33 if [ $? -eq 0 ]; then
|
giuliomoro@203
|
34 BELA_PYTHON27=$PY
|
giuliomoro@203
|
35 break;
|
giuliomoro@203
|
36 fi;
|
giuliomoro@203
|
37 done;
|
giuliomoro@203
|
38 fi;
|
giuliomoro@203
|
39
|
giuliomoro@203
|
40 if [ -z "$BELA_PYTHON27" ]; then
|
giuliomoro@203
|
41 echo "It looks like you might not have python2.7 installed. If you do, please specify the path
|
giuliomoro@203
|
42 to your python2.7 executable in the environmental variable $BELA_PYTHON27"
|
giuliomoro@203
|
43 exit 1;
|
giuliomoro@203
|
44 fi;
|
giuliomoro@203
|
45
|
chris@160
|
46
|
chris@160
|
47 function usage
|
chris@160
|
48 {
|
giuliomoro@203
|
49 printf "\nUSAGE: build_pd.sh [[-i input folder containing _main.pd file ]\
|
giuliomoro@203
|
50 [-o output folder for new heavy project .c files (default ../projects/heavy/hvtemp)]\
|
giuliomoro@398
|
51 [-b bbb path to copy to (default ~/BeagleRT)] | [-h] | [-f|--force] | [-w|--watch] | [-n|--noupload] \n"
|
giuliomoro@203
|
52 printf "\nexample: build_pd.sh -i ../projects/heavy/pd/hello-world -o ../projects/heavy/hello-world\n"
|
giuliomoro@203
|
53 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
|
54 the building process and runs the process.
|
giuliomoro@398
|
55 If --screen is selected, the prompt returns to the user after launching BeagleRT in a screen on the target device.
|
giuliomoro@203
|
56 If --screen and --watch are combined, while the process is running in the screen, modifications to the source files will \
|
giuliomoro@203
|
57 still trigger a new build."
|
chris@160
|
58 }
|
chris@160
|
59
|
chris@160
|
60 while [ "$1" != "" ]; do
|
chris@160
|
61 case $1 in
|
chris@160
|
62 -b | --bbb ) shift
|
giuliomoro@295
|
63 BBB_BELA_HOME=$1
|
chris@160
|
64 ;;
|
giuliomoro@203
|
65 -f | --force) FORCE="1"
|
giuliomoro@203
|
66 ;;
|
chris@160
|
67 -i | --input ) shift
|
chris@160
|
68 pdpath=$1
|
chris@160
|
69 ;;
|
chris@160
|
70 -o | --output ) shift
|
chris@160
|
71 projectpath=$1
|
chris@160
|
72 ;;
|
giuliomoro@203
|
73 -v | --verbose ) verbose=1
|
chris@160
|
74 ;;
|
chris@160
|
75 -r | --render ) shift
|
chris@160
|
76 render=1
|
chris@160
|
77 ;;
|
giuliomoro@203
|
78 -s | --screen ) RUN_WITHOUT_SCREEN="0"
|
giuliomoro@203
|
79 ;;
|
giuliomoro@203
|
80 -w | --watch ) WATCH=1
|
giuliomoro@203
|
81 ;;
|
giuliomoro@398
|
82 -n | --noupload ) NO_UPLOAD=1
|
giuliomoro@398
|
83 ;;
|
chris@160
|
84 -h | --help ) usage
|
chris@160
|
85 exit
|
chris@160
|
86 ;;
|
chris@160
|
87 * ) usage
|
chris@160
|
88 exit 1
|
chris@160
|
89 esac
|
chris@160
|
90 shift
|
chris@160
|
91 done
|
chris@160
|
92
|
giuliomoro@203
|
93 function hasNotChanged(){
|
giuliomoro@203
|
94 if [ $WATCH -eq 0 ];
|
giuliomoro@203
|
95 then
|
giuliomoro@214
|
96 echo "Files in the source folder did not change since the last build, use --force to force recompiling";
|
giuliomoro@203
|
97 exit 0;
|
giuliomoro@203
|
98 fi;
|
giuliomoro@203
|
99 }
|
chris@190
|
100
|
giuliomoro@203
|
101 function checkChanged(){
|
giuliomoro@203
|
102 PD_BACKUP_PATH="hvresources/patch_back/"
|
giuliomoro@203
|
103 if [ $FORCE -eq 1 ] && [ $WATCH -eq 0 ];
|
giuliomoro@203
|
104 then
|
giuliomoro@203
|
105 rm -rf $PD_BACKUP_PATH;
|
giuliomoro@203
|
106 return;
|
giuliomoro@203
|
107 fi;
|
giuliomoro@203
|
108 mkdir -p $PD_BACKUP_PATH;
|
giuliomoro@203
|
109 # count files that have changed
|
giuliomoro@203
|
110 HAS_CHANGED=$(rsync -nac --out-format="%f" "$pdpath" $PD_BACKUP_PATH | grep -v "\/\.$"| wc -l);
|
giuliomoro@203
|
111 if [ $HAS_CHANGED -eq 0 ] && [ $FORCE -eq 0 ];
|
giuliomoro@203
|
112 then
|
giuliomoro@203
|
113 hasNotChanged;
|
giuliomoro@203
|
114 return 1;
|
giuliomoro@203
|
115 fi;
|
giuliomoro@203
|
116 #if we are here and $FORCE==1, it means that $WATCH==1
|
giuliomoro@203
|
117 # so let's make sure only the first run get forced:
|
giuliomoro@203
|
118 FORCE=0;
|
giuliomoro@203
|
119 echo "Files have changed"
|
giuliomoro@203
|
120 # otherwise back up the files that have changed
|
giuliomoro@203
|
121 rsync -vac "$pdpath" $PD_BACKUP_PATH
|
giuliomoro@203
|
122
|
giuliomoro@203
|
123 return 0;
|
giuliomoro@203
|
124 }
|
giuliomoro@203
|
125
|
giuliomoro@203
|
126 function checkUploadBuildRun(){
|
giuliomoro@203
|
127 checkChanged || return # exits if source files have not changed
|
giuliomoro@203
|
128
|
giuliomoro@398
|
129 if [ $NO_UPLOAD -eq 0 ]; then
|
giuliomoro@398
|
130 # remove old static files to avoid obsolete errors
|
giuliomoro@398
|
131 # use -rf to prevent warnings in case they do not exist
|
giuliomoro@398
|
132 rm -rf "$projectpath"/Hv* "$projectpath"/Message* "$projectpath"/Control* "$projectpath"/Signal* &>/dev/null
|
giuliomoro@203
|
133
|
giuliomoro@398
|
134 # invoke the online compiler
|
giuliomoro@398
|
135 "$BELA_PYTHON27" hvresources/uploader.py "$pdpath"/ -n bbb -g c -o "$projectpath";
|
giuliomoro@398
|
136 if [ $? -ne 0 ]; then
|
giuliomoro@398
|
137 #echo "ERROR: an error occurred while executing the uploader.py script"
|
giuliomoro@398
|
138 echo "error"
|
giuliomoro@398
|
139 exit 1
|
giuliomoro@398
|
140 fi;
|
chris@160
|
141 fi;
|
chris@160
|
142
|
giuliomoro@203
|
143 echo "";
|
giuliomoro@203
|
144 #echo "*|*|* Successfully uploaded and converted pd patch into super-fast optimized C code. Brought to you by Heavy! *|*|*";
|
giuliomoro@203
|
145 echo "";
|
giuliomoro@203
|
146
|
giuliomoro@398
|
147 BBB_PROJECT_FOLDER=$BBB_PROJECT_HOME"/"$BBB_PROJECT_NAME #make sure there is no trailing slash here
|
giuliomoro@398
|
148 BBB_NETWORK_TARGET_FOLDER=$BBB_ADDRESS:$BBB_PROJECT_FOLDER
|
giuliomoro@398
|
149
|
giuliomoro@203
|
150 # check how to copy/sync render.cpp file...
|
giuliomoro@398
|
151 # don't replace render.cpp file if custom one provided in output folder
|
giuliomoro@398
|
152 if [ -f $projectpath"/render.cpp" ]; then
|
giuliomoro@398
|
153 echo "Found custom render.cpp file in output folder";
|
giuliomoro@398
|
154 ssh -t $BBB_ADDRESS "rm ${BBB_PROJECT_FOLDER}/build/render.*"
|
giuliomoro@398
|
155 else
|
giuliomoro@398
|
156 if [ $render -eq 0 ]; then
|
giuliomoro@398
|
157 cp "hvresources/render.cpp" $projectpath/;
|
giuliomoro@398
|
158 fi;
|
giuliomoro@203
|
159 fi;
|
chris@160
|
160
|
giuliomoro@203
|
161 cp "hvresources/HvUtils.h" $projectpath/;
|
chris@190
|
162
|
giuliomoro@203
|
163 echo "updating files on board..."
|
chris@190
|
164
|
giuliomoro@398
|
165 echo rsync -c -rv --exclude 'HvContext*' "$projectpath"/ "$BBB_NETWORK_TARGET_FOLDER";
|
giuliomoro@398
|
166 rsync -c -rv --exclude 'HvContext*' "$projectpath"/ "$BBB_NETWORK_TARGET_FOLDER";
|
giuliomoro@398
|
167 # rsync -c -rv "$projectpath"/ "$BBB_ADDRESS":"$BBB_BELA_HOME"/source;
|
giuliomoro@398
|
168 if [ $NO_UPLOAD -eq 0 ]; then
|
giuliomoro@398
|
169 # for whatever reason these big files used to hang when transferring with rsync
|
giuliomoro@398
|
170 scp "$projectpath"/HvContext* $BBB_NETWORK_TARGET_FOLDER
|
giuliomoro@398
|
171 fi;
|
chris@160
|
172
|
giuliomoro@203
|
173 if [ $? -ne 0 ]; then
|
chris@160
|
174 echo "";
|
chris@160
|
175 echo ":( :( :( ERROR: while synchronizing files with the BBB. Is the board connected and the correct SD card inserted? :( :( :(";
|
chris@160
|
176 echo "";
|
chris@160
|
177 exit 1;
|
giuliomoro@203
|
178 fi;
|
giuliomoro@203
|
179 # exit
|
giuliomoro@203
|
180 #produce a list of files which content has changed (not just the date)
|
giuliomoro@203
|
181 #TODO: could be made faster (perhaps) by backing up the folder locally instead of bbb
|
giuliomoro@295
|
182 # UPDATED_FILES=`rsync -naic --log-format="%f" "$projectpath" "$BBB_BELA_HOME"/source | grep -v "\.$"`
|
giuliomoro@203
|
183 # echo "UPDATEDFILES : $UPDATED_FILES"
|
giuliomoro@203
|
184 # exit 2
|
giuliomoro@398
|
185 # remove old executable and heavy context .o/.d files
|
giuliomoro@398
|
186 if [ $NO_UPLOAD -eq 0 ]; then
|
giuliomoro@398
|
187 ssh $BBB_ADDRESS "rm -rf "$BBB_PROJECT_FOLDER/$BBB_PROJECT_NAME;
|
giuliomoro@398
|
188 fi;
|
giuliomoro@398
|
189 SCREEN_NAME=BeagleRT
|
giuliomoro@398
|
190 # Make new BeagleRT execut/able and run
|
giuliomoro@203
|
191 # It does not look very nice that we type the same things over and over
|
giuliomoro@203
|
192 # but that is because each line is an ssh session in its own right
|
giuliomoro@329
|
193 MAKE_COMMAND="make stop -C $BBB_BELA_HOME PROJECT='$BBB_PROJECT_NAME' CL='$COMMAND_ARGS'"
|
giuliomoro@203
|
194 if [ $RUN_PROJECT -eq 0 ]
|
giuliomoro@203
|
195 then
|
giuliomoro@203
|
196 echo "Building project..."
|
giuliomoro@329
|
197 ssh $BBB_ADDRESS "$MAKE_COMMAND"
|
giuliomoro@203
|
198 else
|
giuliomoro@203
|
199 echo "Building and running project..."
|
giuliomoro@203
|
200 if [ $RUN_WITHOUT_SCREEN -eq 1 ]
|
giuliomoro@203
|
201 then
|
giuliomoro@329
|
202 ssh -t $BBB_ADDRESS "$MAKE_COMMAND run"
|
giuliomoro@203
|
203 elif [ $RUN_IN_FOREGROUND -eq 1 ]
|
giuliomoro@203
|
204 then
|
giuliomoro@203
|
205 # Run in screen without detaching
|
giuliomoro@329
|
206 ssh -t $BBB_ADDRESS "$MAKE_COMMAND runscreenfg"
|
giuliomoro@203
|
207 else
|
giuliomoro@203
|
208 # Run in screen and detach
|
giuliomoro@329
|
209 ssh -t $BBB_ADDRESS "$MAKE_COMMAND runscreen"
|
giuliomoro@203
|
210 fi
|
giuliomoro@203
|
211 fi
|
giuliomoro@203
|
212 } #checkUploadBuildRun
|
giuliomoro@203
|
213
|
giuliomoro@203
|
214 checkUploadBuildRun
|
giuliomoro@203
|
215
|
giuliomoro@203
|
216 if [ $WATCH -ne 0 ]; then
|
giuliomoro@203
|
217 WAIT_STRING="\rWaiting for changes in $pdpath"
|
giuliomoro@203
|
218 while true; do
|
giuliomoro@203
|
219 printf "$WAIT_STRING "
|
giuliomoro@203
|
220 sleep 0.3
|
giuliomoro@203
|
221 printf "$WAIT_STRING. "
|
giuliomoro@203
|
222 sleep 0.3
|
giuliomoro@203
|
223 printf "$WAIT_STRING.. "
|
giuliomoro@203
|
224 sleep 0.3
|
giuliomoro@203
|
225 printf "$WAIT_STRING..."
|
giuliomoro@203
|
226 sleep 0.3
|
giuliomoro@203
|
227 checkUploadBuildRun
|
giuliomoro@203
|
228 done;
|
chris@160
|
229 fi;
|
chris@160
|
230 #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
|