]> diplodocus.org Git - nmh/blob - docs/contrib/build_nmh
More enhancements to build_nmh.
[nmh] / docs / contrib / build_nmh
1 #! /bin/sh
2 #
3 # Configures and builds nmh.
4 # * If this script is not invoked from an nmh source directory, it
5 # will attempt to download the nmh sources.
6 # * This script retrieves configuration from the first existing nmh
7 # installation on your $PATH, if any.
8 # * Unless the -y option is provided, this script then interactively
9 # walks you through confirmation of common configuration settings.
10 #
11 # This file can be downloaded and immediately run using, e.g.,
12 # wget http://git.savannah.gnu.org/cgit/nmh.git/plain/docs/contrib/build_nmh
13 # sh build_nmh
14 #
15 # Typical usage:
16 # The first time you invoke this script, use the -i option to install
17 # nmh in the specified location. The script will walk you through the
18 # common nmh configuration settings. The -v option will cause display
19 # of brief progress indicators. Be sure to add the bin directory of
20 # the install location to your $PATH, if not already there.
21 # Subsequently, invoke this script with the -y option, to use the
22 # relevant configuration settings from the installed nmh without
23 # confirmation.
24 #
25 # Option summary:
26 # First time use:
27 # -b <branch> to specify branch to check out, only if downloading sources
28 # -i to install nmh
29 # -v to display progress
30 # Subsequent uses, assuming installed nmh bin directory is on $PATH:
31 # -y to accept all configuration options without confirmation
32 # Output control:
33 # -l <logfile name>, default 'build_nmh.log', - for stdout/stderr
34 # Advanced/developer use:
35 # -c to run 'make distcheck' instead of 'make check'
36 # -d to build nmh with debug enabled
37 # -s to use 'make superclean': requires recent autoconf and automake,
38 # see docs/README.developers
39 # -r to build rpm
40 #
41 # To disable colorization of the test summary, either unset the TERM
42 # environment variable or set it to dumb, e.g., TERM=dumb build_nmh.
43 #
44 # See the nmh MACHINES file for build prerequisites. In addition, the rpmbuild
45 # is required to be available if the -r option is used.
46
47 usage="usage: $0
48 [-b <branch>, only if downloading]
49 [-c to run 'make distcheck' instead of 'make check']
50 [-d to build nmh with debug enabled]
51 [-i to install nmh]
52 [-l <logfile name>, default '$logfile']
53 [-r to build rpm]
54 [-s to use 'make superclean': requires recent autoconf and automake]
55 [-v to display progress]
56 [-y to accept all configuration options without confirmation]"
57
58 #### Exit with error message.
59 die() {
60 status=1
61 exec 1>&3 3>&- 2>&4 4>&-
62 cat "$tmpfile" 1>&2
63 echo "$0: $*" 1>&2
64 exit 1
65 }
66
67 #### Find location of a program. Bourne shell just puts the name in
68 #### $0 if it's found from the PATH, so search that if necessary.
69 finddir() {
70 case $1 in
71 */*) dirname "$1" ;;
72 * ) IFS=:
73 for d in $PATH; do
74 [ -f "${d:=.}/$1" -a -x "$d/$1" ] && printf %s "$d" && break
75 done ;;
76 esac
77 }
78
79 #### Make sure user sees error output even on early termination. Assumes
80 #### that called programs exit with non-zero status when terminated.
81 cleanup() {
82 if [ $status -eq 0 ]; then
83 rm -f "$tmpfile"
84 else
85 [ $logfile = - ] && cat "$tmpfile" 1>&3 || mv "$tmpfile" "$logfile"
86 fi
87 }
88 trap cleanup EXIT
89
90 directory=
91 gitrepo=git.savannah.nongnu.org
92 invocation="$0 $*"
93 tmpfile=/tmp/build_nmh-$$.log
94
95
96 ####
97 #### Interpret command arguments.
98 ####
99 branch=master
100 check=check
101 debug=0
102 install=0
103 logfile=build_nmh.log
104 build_rpm=0
105 superclean=0
106 verbose=0
107 yes=0
108
109 while getopts 'cb:dil:rsvy?' arg; do
110 case $arg in
111 b ) branch="$OPTARG" ;;
112 c ) check=distcheck ;;
113 d ) debug=1 ;;
114 i ) install=1 ;;
115 l ) logfile=$OPTARG ;;
116 r ) build_rpm=1 ;;
117 s ) superclean=1 ;;
118 v ) verbose=1 ;;
119 y ) yes=1 ;;
120 '?') echo "$usage"; exit 0 ;;
121 esac
122 done
123 shift `expr $OPTIND - 1`
124
125 #### Redirect all output to tmp file. Then at end of script, copy
126 #### it to either logfile or stdout. Also, grep it for errors and
127 #### warnings.
128 exec 3>&1 4>&2 >"$tmpfile" 2>&1
129
130 echo "$invocation"
131
132 #### No non-option command line arguments are supported.
133 [ $# -gt 0 ] && die "$usage"
134
135 #### Check to see that we're in a nmh source directory.
136 if grep 'the authors of nmh' COPYRIGHT >/dev/null 2>&1; then
137 :
138 else
139 #### Download sources from repo.
140 [ $verbose -ge 1 ] && echo downloading . . . >&3
141 gitdir=`finddir git`
142 if [ "$gitdir" ]; then
143 #### Use git repo.
144 [ "$verbose" -eq 0 ] && git_opts=--quiet
145 [ "$branch" == master ] ||
146 git_opts="${git_opts:+$git_opts }--branch $branch"
147 if "$gitdir"/git clone --depth 1 $git_opts "git://$gitrepo/nmh.git" >&3; then
148 directory=nmh
149 cd "$directory" || die "failed to clone $directory"
150 printf "commit %s\n" `git log --max-count=1 --pretty=format:%H`
151 else
152 die 'failed to clone git repo'
153 fi
154 else
155 [ -e nmh-"$branch" ] && die "nmh-$branch exists, will not overrwrite"
156
157 #### Use snapshot.
158 tarball="nmh-$branch.tar.gz"
159 repo="http://$gitrepo/cgit/nmh.git/snapshot"
160 snapshot="$repo/$tarball"
161 if [ "`finddir wget`" ]; then
162 [ "$verbose" -eq 0 ] && wget_opts='--quiet'
163 wget --output-document - $wget_opts "$snapshot" 2>&3 | gzip -d | tar xf -
164 elif [ "`finddir curl`" ]; then
165 [ "$verbose" -eq 0 ] && curl_opts='--silent --show-error'
166 curl --location $curl_opts "$snapshot" 2>&3 | gzip -d | tar xf -
167 else
168 die 'unable to find program to download nmh sources'
169 fi
170
171 if [ -d nmh-"$branch" ]; then
172 directory=nmh-"$branch"
173 cd "$directory" || die "failed to download and extract $directory"
174 else
175 die "failed to download nmh-$branch sources"
176 fi
177 fi
178 fi
179
180 ####
181 #### Set up configure options. Handle options that can have embedded
182 #### spaces (currently just smtpservers) specially.
183 ####
184
185 #### Here are the config options that we will try to detect, then
186 #### confirm, and finally set.
187 config_prefix=/usr/local/nmh
188 config_locking=
189 config_mts=smtp
190 config_smtpservers=localhost
191 config_sasl=n
192 config_tls=n
193 config_oauth=n
194 config_debug=n
195
196
197 #### Figure out whether or not to use -n with tail.
198 case `printf 'OK\n' | tail -n 1 2>&1` in
199 OK) tail='tail -n ' ;;
200 *) tail='tail -' ;;
201 esac
202
203 if install-mh -check >/dev/null 2>&1; then
204 #### Determine config options from installed nmh.
205 mhbin=`finddir install-mh`
206
207 config_prefix=`cd $mhbin/.. && pwd`
208
209 mtsconf=`mhparam etcdir`/mts.conf
210 if [ -f "$mtsconf" ]; then
211 mts_entry=`grep '^mts:' "$mtsconf"`
212 if [ "$mts_entry" ]; then
213 mts=`echo "$mts_entry" | sed -e 's/^mts: *//'`
214 if [ "$mts" -a "$mts" != smtp ]; then
215 config_mts="$mts"
216 fi
217 fi
218
219 mtsconfservers=`grep '^servers:' "$mtsconf"`
220 if [ "$mtsconfservers" ]; then
221 servers=`echo "$mtsconfservers" | \
222 sed -e 's/^servers: *//' -e 's/ /\\\ /g'`
223 [ "$servers" ] && config_smtpservers="$servers"
224 fi
225 fi
226
227 if test -x "$mhbin/mhparam"; then
228 if mhparam sasl >/dev/null; then
229 case `$mhbin/mhparam sasl` in
230 *sasl*) config_sasl=y ;;
231 esac
232
233 case `$mhbin/mhparam tls` in
234 *tls*) config_tls=y ;;
235 esac
236
237 case `$mhbin/mhparam oauth` in
238 *oauth*) config_oauth=y ;;
239 esac
240 else
241 tput smso
242 echo "$0: SASL, TLS, and OAuth2 detection not supported with current nmh"
243 [ $yes -eq 1 ] && echo "will not configure them in"
244 tput rmso
245 fi
246 fi
247 fi
248
249 [ $debug -ge 1 ] && config_debug=y
250
251 if [ $yes -eq 0 ]; then
252 #### Confirm each config setting with user.
253 printf 'Install prefix [%s]: ' $config_prefix >&3
254 read prefix
255 [ "$prefix" ] && config_prefix="$prefix"
256
257 printf 'Locking type (dot|fcntl|flock|lockf) [determined by configure]: ' >&3
258 read locking
259 [ "$locking" ] && config_locking="$locking"
260
261 printf 'MTS (smtp|sendmail/smtp|sendmail/pipe) [%s]: ' $config_mts >&3
262 read mts
263 [ "$mts" ] && config_mts="$mts"
264
265 if [ "$config_mts" = smtp ]; then
266 printf 'SMTP server(s), space separated [%s]: ' $config_smtpservers >&3
267 read response
268 servers=`echo $response | sed -e 's/ /\\\ /g'`
269 [ "$servers" ] && config_smtpservers="$servers"
270 fi
271
272 printf 'Cyrus SASL support [%s]: ' $config_sasl >&3
273 read response
274 [ "$response" = y -o "$response" = Y ] && config_sasl=y
275
276 printf 'TLS support [%s]: ' $config_tls >&3
277 read response
278 [ "$response" = y -o "$response" = Y ] && config_tls=y
279
280 printf 'OAuth2 support [%s]: ' $config_oauth >&3
281 read response
282 [ "$response" = y -o "$response" = Y ] && config_oauth=y
283
284 #### Don't confirm debug here: obey the -d option to this script.
285 fi
286
287 smtpservers=
288 config_opts="--prefix=$config_prefix"
289
290 [ "$config_locking" ] &&
291 config_opts="$config_opts --with-locking=$config_locking"
292 [ "$config_mts" -a "$config_mts" != smtp ] &&
293 config_opts="$config_opts --with-mts=$config_mts"
294 [ "$config_smtpservers" -a "$config_smtpservers" != localhost ] &&
295 smtpservers="--with-smtpservers=$config_smtpservers"
296 [ "$config_sasl" = y ] && config_opts="$config_opts --with-cyrus-sasl"
297 [ "$config_tls" = y ] && config_opts="$config_opts --with-tls"
298 [ "$config_oauth" = y ] && config_opts="$config_opts --with-oauth"
299 [ $config_debug = y ] && config_opts="$config_opts --enable-assert"
300
301 #### dotlocking, the usual default, requires chgrp and chmod of inc.
302 installpriv=
303 if [ $install -ge 1 -a "$LOGNAME" != root ]; then
304 if [ "$config_locking" = dot ]; then
305 echo "$0: "'install requires chgrp and chmod 2755'
306 echo 'so will sudo to install. Terminate with Ctrl-C if unacceptable.'
307 installpriv=sudo
308 fi
309 fi
310
311 printf '\n%s %s %s %s\n\n' "`uname -m`" "`uname -s`" "`uname -r`" "`uname -v`"
312 [ -f /etc/os-release ] && printf '%s\n\n' "`cat /etc/os-release`"
313
314 ####
315 #### Set up with autoconfig if necessary.
316 ####
317 if [ -f Makefile ]; then
318 [ $verbose -ge 1 ] && echo cleaning . . . >&3
319 if [ $superclean -ge 1 ]; then
320 make superclean >/dev/null
321 else
322 make distclean >/dev/null
323 fi
324 fi
325
326 if [ ! -f configure -o ! -f Makefile.in ]; then
327 [ $verbose -ge 1 ] && echo autoconfiguring . . . >&3
328 ./autogen.sh
329 [ $? -ne 0 ] &&
330 die "autogen failed, see MACHINES file for autoconf,
331 automake, flex, and bison requirements"
332 fi
333
334
335 ####
336 #### Build.
337 ####
338 [ $verbose -ge 1 ] && echo configuring . . . >&3
339 if [ -z "$CFLAGS" ]; then
340 #### Only use these flags with gcc.
341 if cc -dM -E - </dev/null 2>&1 | grep __GNUC__ >/dev/null; then
342 #### configure will supply -g -O2 with gcc, but only if CFLAGS
343 #### isn't defined.
344 CFLAGS='-g -O2 -ansi -pedantic'
345 fi
346 fi
347
348 printf '\n./configure %s\n' "$config_opts${smtpservers:+ $smtpservers}"
349 ./configure CFLAGS="${CFLAGS}" $config_opts${smtpservers:+" $smtpservers"}
350 status=$?
351
352 if [ $status -eq 0 ]; then
353 [ $verbose -ge 1 ] && echo building . . . >&3
354 make
355 status=$?
356
357 if [ $status -eq 0 ]; then
358 if [ "$TESTS_SHELL"x = x ]; then
359 #### Bonus: use heirloom shell to test, if available, and if
360 #### TESTS_SHELL hadn't already been set.
361 heirloom_shell=/usr/lib/heirloom/5bin/sh
362 if [ -x "$heirloom_shell" ]; then
363 TESTS_SHELL="$heirloom_shell"; export TESTS_SHELL
364 fi
365 fi
366
367 if [ "$CFLAGS" ]; then
368 #### Pass DISTCHECK_CONFIGURE_FLAGS through an environment
369 #### variable to avoid automake's quoting.
370 DISTCHECK_CONFIGURE_FLAGS="CFLAGS='${CFLAGS}'"
371 export DISTCHECK_CONFIGURE_FLAGS
372 fi
373
374 [ $verbose -ge 1 ] && echo testing . . . >&3
375 [ "${TERM:-dumb}" = dumb ] && color=no || color=always
376 checkoutput=`make $check AM_COLOR_TESTS=$color`
377 status=$?
378
379 tests_summary=`echo "$checkoutput" | grep tests`
380 #### If multiple tests not run, that line will be caught by the
381 #### "grep tests" above.
382 test_not_run=`echo "$checkoutput" | grep 'test was not run'`
383 fails=`echo "$checkoutput" | grep FAIL`
384 if [ "$tests_summary" ]; then
385 echo '==================='
386 [ "$test_not_run" ] && echo "$test_not_run"
387 [ "$fails" ] && echo "$fails"
388 echo "$tests_summary"
389 echo '==================='
390 [ "$check" = distcheck ] && echo "$checkoutput" | ${tail}4
391 fi
392
393 if [ $status -eq 0 ]; then
394 if [ $install -ge 1 ]; then
395 [ $verbose -ge 1 ] && echo installing . . . >&3
396 ($installpriv make install) >/dev/null
397 status=$?
398 fi
399
400 if [ $status -eq 0 -a $build_rpm -ge 1 ]; then
401 [ $verbose -ge 1 ] && echo building rpm . . . >&3
402 make rpm >/dev/null
403 status=$?
404 fi
405 fi
406 fi
407 else
408 echo "see nmh MACHINES file for build dependences"
409 fi
410
411 ####
412 #### Report results.
413 ####
414
415 #### Disable output redirection (and flush) so that we can grep.
416 exec 1>&3 3>&- 2>&4 4>&-
417
418 if [ "$logfile" != - ]; then
419 rm -f "$logfile"
420 exec 3>&1 >"$logfile" 2>&1
421 fi
422
423 [ -f "$tmpfile" ] && cat "$tmpfile"
424 [ -f "$tmpfile" ] && grep -E 'Error|warn' "$tmpfile"
425
426 #### Put message on stdout, and in log if different.
427 if [ $status -ne 0 -o $verbose -ge 1 -o "$directory" ]; then
428 [ $status -eq 0 ] && indication=succeeded || indication=failed
429 message="build $indication, build log is in ${directory:+$directory/}$logfile"
430 echo "$message"
431 [ "$logfile" = - ] || echo "$message" >&3
432 fi
433
434 exit $status