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