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