91bd80102e485820ec93efa1ee56c1eb9a8811d7
[m6w6/btr] / share / btr / btr.sh
1 #!/bin/sh
2
3 function btr-help {
4 btr-banner
5 echo
6 echo "Usage: $(basename $0) [-hyvqcC] [<options>]"
7 echo
8 echo " -h, --help Display this help"
9 echo " -y, --yes Always assume yes"
10 echo " -v, --verbose Be more verbose"
11 echo " -q, --quiet Be more quiet"
12 echo " -c, --clean Clean build"
13 echo " -C, --vcsclean Clean repo/branch"
14 echo
15 echo " Options:"
16 echo " -f, --config=<file> Read configuration from a file"
17 echo " -s, --source=<rules> Use the specified source ruleset"
18 echo " -b, --build=<rules> Use the specified build ruleset"
19 echo " -r, --report=<rules> Use the specifued report ruleset"
20 echo " -T, --test=<args> Provide test runner arguments"
21 echo " -B, --branch=<branch> Checkout this branch"
22 echo " -D, --directory=<directory> Use this directory as work root"
23 echo " -S, --suffix=<suffix> Append suffix to the build name"
24 echo
25 echo " Rules format:"
26 echo " type=argument e.g: git=git@github.com:m6w6/btr.git"
27 echo " irc=irc://btr@chat.freenode.org/#btr"
28 echo " mail=\"-c copy@to rcpt@to\""
29 echo " notify-send=\"-u low\""
30 echo
31 echo " Note though, that some rules do not use any argument."
32 echo
33 echo " Rulesets:"
34 for ruleset in source build report
35 do
36 printf " %10s: %s\n" $ruleset \
37 "$(find "$BTR_LIBDIR/$ruleset" -name '*.mk' -exec basename {} .mk \; | sort | xargs)"
38 done
39 echo
40 echo " Examples:"
41 echo
42 echo " Clone PHP's git, use PHP-5.5 branch, build with php ruleset and"
43 echo " run the test suite with valgrind (-m) on a debug build and report"
44 echo " the results with a simple OSD notification:"
45 echo " $ btr -s git=git@php.net:php/php-src.git -B PHP-5.5 \\"
46 echo " -b \"php=--enable-debug\" -T-m -r notify-send"
47 echo " See also php.example.conf"
48 echo
49 echo " Clone CURL's git (use master), build with GNU autotools"
50 echo " ruleset which runs 'make check' and mail the report to the"
51 echo " current user. Verbosely show all actions taken:"
52 echo " $ btr -v -s git=https://github.com/bagder/curl.git -b gnu -r mail"
53 echo " See also curl.example.conf"
54 echo
55 exit
56 }
57 export -f btr-help
58
59 function btr-parseopts {
60 local shortoptions="hvqycCf:T:B:D:S:s:b:r:"
61 local longoptions="help,verbose,quiet,yes,clean,vcsclean,config:,test:,branch:,directory:,suffix:,source:,build:,report:"
62 local options=$(getopt \
63 --options "$shortoptions" \
64 --longoptions "$longoptions" \
65 -- "$@" \
66 )
67
68 if test $? -ne 0 ; then
69 help
70 fi
71
72 eval set -- "$options"
73
74 while test $# -gt 0
75 do
76 case "$1" in
77 -h|--help)
78 btr-help
79 ;;
80 -v|--verbose)
81 BTR_QUIET=false
82 BTR_VERBOSE=true
83 ;;
84 -q|--quiet)
85 BTR_QUIET=true
86 BTR_VERBOSE=false
87 ;;
88 -y|--yes)
89 BTR_FORCEYES=true
90 ;;
91 -c|--clean)
92 BTR_BUILD_CLEAN=true
93 ;;
94 -C|--vcsclean)
95 BTR_SOURCE_CLEAN=true
96 ;;
97 ####
98 -f|--config)
99 source "$2"
100 shift
101 ;;
102 ####
103 -B|--branch)
104 BTR_BRANCH="$2"
105 shift
106 ;;
107 -D|--directory)
108 BTR_RUNDIR="$2"
109 shift
110 ;;
111 -S|--suffix)
112 BTR_SUFFIX="$2"
113 shift
114 ;;
115 -T|--test)
116 BTR_TEST_ARGS="$2"
117 shift
118 ;;
119 ####
120 -s|--source)
121 case "$2" in
122 git*)
123 test -z "$BTR_BRANCH" && BTR_BRANCH=master
124 ;;
125 svn*)
126 test -z "$BTR_BRANCH" && BTR_BRANCH=trunk
127 ;;
128 cvs*)
129 test -z "$BTR_BRANCH" && BTR_BRANCH=HEAD
130 ;;
131 esac
132 BTR_SOURCE_RULES="$(cut -d= -f1 <<<$2)"
133 BTR_SOURCE_ARGS="$(cut -s -d= -f2- <<<$2)"
134 shift
135 ;;
136 -b|--build)
137 BTR_BUILD_RULES="$(cut -d= -f1 <<<$2)"
138 BTR_BUILD_ARGS="$(cut -s -d= -f2- <<<$2)"
139 shift
140 ;;
141 -r|--report)
142 BTR_REPORT_RULES="$(cut -d= -f1 <<<$2)"
143 BTR_REPORT_ARGS="$(cut -s -d= -f2- <<<$2)"
144 shift
145 ;;
146 ####
147 --)
148 # legacy
149 if test "$2"
150 then
151 BTR_SOURCE_ARGS="$2"
152 fi
153 shift
154 ;;
155 esac
156 shift
157 done
158 }
159 export -f btr-parseopts
160
161 function btr-setup {
162 if test -z "$BTR_SOURCE_RULES" -o -z "$BTR_BUILD_RULES" -o -z "$BTR_REPORT_RULES"
163 then
164 btr-help
165 fi
166
167 btr-setup-verbosity true
168 btr-setup-rundir
169
170 export BTR_SOURCE_RULES BTR_BUILD_RULES BTR_REPORT_RULES
171 test -z "$BTR_SOURCE_ARGS" || export BTR_SOURCE_ARGS
172 test -z "$BTR_SOURCE_CLEAN" || export BTR_SOURCE_CLEAN
173 test -z "$BTR_BUILD_ARGS" || export BTR_BUILD_ARGS
174 test -z "$BTR_BUILD_CLEAN" || export BTR_BUILD_CLEAN
175 test -z "$BTR_TEST_ARGS" || export BTR_TEST_ARGS
176 test -z "$BTR_REPORT_ARGS" || export BTR_REPORT_ARGS
177 BTR_REPO=$(basename $(sed -re 's~^.*[/:#]~~' <<<"$BTR_SOURCE_ARGS") .git)
178 BTR_SAFE_BRANCH=$(tr ":/" "_" <<<$(basename "$BTR_BRANCH"))
179 export BTR_REPO BTR_BRANCH BTR_SAFE_BRANCH
180
181 if test -z "$BTR_SUFFIX"
182 then
183 export BTR_BUILD="$BTR_REPO@$BTR_SAFE_BRANCH"
184 else
185 export BTR_BUILD="$BTR_REPO@$BTR_SAFE_BRANCH-$BTR_SUFFIX"
186 fi
187
188 export BTR_REPO_DIR="$BTR_REPO"
189 export BTR_BRANCH_DIR="$BTR_BUILD/checkout"
190 export BTR_BUILD_DIR="$BTR_BUILD/build"
191 export BTR_LOG_DIR="$BTR_BUILD/log"
192 export BTR_CONFIG_REPORT="$BTR_LOG_DIR/config@$DATE.log"
193 export BTR_BUILD_REPORT="$BTR_LOG_DIR/build@$DATE.log"
194 export BTR_TEST_REPORT="$BTR_LOG_DIR/test@$DATE.log"
195 export BTR_LAST_REPORT=$(basename $(ls -t "$BTR_RUNDIR/$BTR_LOG_DIR/test@"* 2>/dev/null | head -n1) 2>/dev/null)
196 export BTR_REPORT="$BTR_LOG_DIR/report@$DATE.log"
197 }
198 export -f btr-setup
199
200 function btr-conf-dump {
201 echo "BTR_QUIET='$BTR_QUIET'"
202 echo "BTR_VERBOSE='$BTR_VEROSE'"
203 echo "BTR_FORCEYES='$BTR_FORCEYES'"
204 echo "BTR_BRANCH='$BTR_BRANCH'"
205 echo "BTR_SUFFIX='$BTR_SUFFIX'"
206 echo "BTR_RUNDIR='$BTR_RUNDIR'"
207 echo "BTR_SOURCE_RULES='$BTR_SOURCE_RULES'"
208 test ${BTR_SOURCE_ARGS+defined} && echo "BTR_SOURCE_ARGS='$BTR_SOURCE_ARGS'"
209 test ${BTR_SOURC_CLEAN+defined} && echo "BTR_SOURCE_CLEAN='$BTR_SOURCE_CLEAN'"
210 echo "BTR_BUILD_RULES='$BTR_BUILD_RULES'"
211 test ${BTR_BUILD_ARGS+defined} && echo "BTR_BUILD_ARGS='$BTR_BUILD_ARGS'"
212 test ${BTR_BUILD_CLEAN+defined} && echo "BTR_BUILD_CLEAN='$BTR_BUILD_CLEAN'"
213 test ${BTR_TEST_ARGS+defined} && echo "BTR_TEST_ARGS='$BTR_TEST_ARGS'"
214 echo "BTR_REPORT_RULES='$BTR_REPORT_RULES'"
215 test ${BTR_REPORT_ARGS+defined} && echo "BTR_REPORT_ARGS='$BTR_REPORT_ARGS'"
216 }
217 export -f btr-conf-dump
218
219 function btr-conf-show {
220 echo
221 echo "# Configuration:"
222 echo
223 echo "BTR_RUNDIR = $BTR_RUNDIR"
224 echo "BTR_BINDIR = $BTR_BINDIR"
225 echo "BTR_LIBDIR = $BTR_LIBDIR"
226 echo
227 echo "BTR_SOURCE_RULES = $BTR_SOURCE_RULES"
228 echo "BTR_SOURCE_ARGS = $BTR_SOURCE_ARGS"
229 echo "BTR_SOURCE_CLEAN = $BTR_SOURCE_CLEAN"
230 echo "BTR_BUILD_RULES = $BTR_BUILD_RULES"
231 echo "BTR_BUILD_ARGS = $BTR_BUILD_ARGS"
232 echo "BTR_BUILD_CLEAN = $BTR_BUILD_CLEAN"
233 echo "BTR_TEST_ARGS = $BTR_TEST_ARGS"
234 echo "BTR_REPORT_RULES = $BTR_REPORT_RULES"
235 echo "BTR_REPORT_ARGS = $BTR_REPORT_ARGS"
236 echo "BTR_REPO = $BTR_REPO"
237 echo "BTR_BRANCH = $BTR_BRANCH"
238 echo "BTR_SAFE_BRANCH = $BTR_SAFE_BRANCH"
239 echo "BTR_BUILD = $BTR_BUILD"
240 echo
241 echo "BTR_REPO_DIR = $BTR_REPO_DIR"
242 echo "BTR_BRANCH_DIR = $BTR_BRANCH_DIR"
243 echo "BTR_BUILD_DIR = $BTR_BUILD_DIR"
244 echo "BTR_LOG_DIR = $BTR_LOG_DIR"
245 echo "BTR_CONFIG_REPORT = $BTR_CONFIG_REPORT"
246 echo "BTR_BUILD_REPORT = $BTR_BUILD_REPORT"
247 echo "BTR_TEST_REPORT = $BTR_TEST_REPORT"
248 echo "BTR_LAST_REPORT = $BTR_LAST_REPORT"
249 echo
250 }
251 export -f btr-conf-show
252
253 function btr-run {
254 set -e
255 make -e $BTR_SILENT_FLAG -C $BTR_RUNDIR -f $BTR_LIBDIR/source/$BTR_SOURCE_RULES.mk
256 make -e $BTR_SILENT_FLAG -C $BTR_RUNDIR -f $BTR_LIBDIR/build/$BTR_BUILD_RULES.mk
257 make -e $BTR_SILENT_FLAG -C $BTR_RUNDIR -f $BTR_LIBDIR/report/$BTR_REPORT_RULES.mk
258 set +e
259 }
260 export -f btr-run
261
262 # vim: noet