]> diplodocus.org Git - nmh/blob - docs/contrib/build_nmh
b69dc08a98c14e9f28a20b7b242a919a69e6fb15
[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 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 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 asserts enabled and optimization disabled]
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='determined by configure'
192 config_tls='determined by configure'
193 config_debug=n
194
195
196 #### Figure out whether or not to use -n with tail.
197 case `printf 'OK\n' | tail -n 1 2>&1` in
198 OK) tail='tail -n ' ;;
199 *) tail='tail -' ;;
200 esac
201
202 if install-mh -check >/dev/null 2>&1; then
203 #### Determine config options from installed nmh.
204 mhbin=`finddir install-mh`
205
206 config_prefix=`cd $mhbin/.. && pwd`
207
208 mtsconf=`mhparam etcdir`/mts.conf
209 if [ -f "$mtsconf" ]; then
210 mts_entry=`grep '^mts:' "$mtsconf"`
211 if [ "$mts_entry" ]; then
212 mts=`echo "$mts_entry" | sed -e 's/^mts: *//'`
213 if [ "$mts" -a "$mts" != smtp ]; then
214 config_mts="$mts"
215 fi
216 fi
217
218 mtsconfservers=`grep '^servers:' "$mtsconf"`
219 if [ "$mtsconfservers" ]; then
220 servers=`echo "$mtsconfservers" | \
221 sed -e 's/^servers: *//' -e 's/ /\\\ /g'`
222 [ "$servers" ] && config_smtpservers="$servers"
223 fi
224 fi
225
226 if test -x "$mhbin/mhparam"; then
227 if mhparam sasl >/dev/null; then
228 case `$mhbin/mhparam sasl` in
229 *sasl*) config_sasl=y ;;
230 esac
231
232 case `$mhbin/mhparam tls` in
233 *tls*) config_tls=y ;;
234 esac
235 else
236 tput smso
237 echo "$0: SASL and TLS detection not supported with current nmh"
238 [ $yes -eq 1 ] && echo "configure will determine whether to enable"
239 tput rmso
240 fi
241 fi
242 fi
243
244 #### Don't confirm debug interactively below; obey the -d option to this script.
245 [ $debug -ge 1 ] && config_debug=y
246
247 if [ $yes -eq 0 ]; then
248 #### Confirm each config setting with user.
249 printf 'Install prefix [%s]: ' $config_prefix >&3
250 read prefix
251 [ "$prefix" ] && config_prefix="$prefix"
252
253 printf 'Locking type (dot|fcntl|flock|lockf) [determined by configure]: ' >&3
254 read locking
255 [ "$locking" ] && config_locking="$locking"
256
257 printf 'MTS (smtp|sendmail/smtp|sendmail/pipe) [%s]: ' $config_mts >&3
258 read mts
259 [ "$mts" ] && config_mts="$mts"
260
261 if [ "$config_mts" = smtp ]; then
262 printf 'SMTP server(s), space separated [%s]: ' $config_smtpservers >&3
263 read response
264 servers=`echo $response | sed -e 's/ /\\\ /g'`
265 [ "$servers" ] && config_smtpservers="$servers"
266 fi
267
268 printf 'Cyrus SASL support [%s]: ' "$config_sasl" >&3
269 read response
270 [ "$response" = y -o "$response" = Y ] && config_sasl=y
271
272 printf 'TLS support [%s]: ' "$config_tls" >&3
273 read response
274 [ "$response" = y -o "$response" = Y ] && config_tls=y
275 fi
276
277 smtpservers=
278 config_opts="--prefix=$config_prefix"
279
280 [ "$config_locking" ] &&
281 config_opts="$config_opts --with-locking=$config_locking"
282 [ "$config_mts" -a "$config_mts" != smtp ] &&
283 config_opts="$config_opts --with-mts=$config_mts"
284 [ "$config_smtpservers" -a "$config_smtpservers" != localhost ] &&
285 smtpservers="--with-smtpservers=$config_smtpservers"
286 [ "$config_sasl" = y ] && config_opts="$config_opts --with-cyrus-sasl"
287 [ "$config_tls" = y ] && config_opts="$config_opts --with-tls"
288 [ $config_debug = y ] && config_opts="$config_opts --enable-assert"
289
290 #### dotlocking, the usual default, requires chgrp and chmod of inc.
291 installpriv=
292 if [ $install -ge 1 -a "$LOGNAME" != root ]; then
293 if [ "$config_locking" = dot ]; then
294 echo "$0: "'install requires chgrp and chmod 2755'
295 echo 'so will sudo to install. Terminate with Ctrl-C if unacceptable.'
296 installpriv=sudo
297 fi
298 fi
299
300 printf '\n%s %s %s %s\n\n' "`uname -m`" "`uname -s`" "`uname -r`" "`uname -v`"
301 [ -f /etc/os-release ] && printf '%s\n\n' "`cat /etc/os-release`"
302
303 ####
304 #### Set up with autoconfig if necessary.
305 ####
306 if [ -f Makefile ]; then
307 [ $verbose -ge 1 ] && echo cleaning . . . >&3
308 if [ $superclean -ge 1 ]; then
309 make superclean >/dev/null
310 else
311 make distclean >/dev/null
312 fi
313 fi
314
315 if [ ! -f configure -o ! -f Makefile.in ]; then
316 [ $verbose -ge 1 ] && echo autoconfiguring . . . >&3
317 ./autogen.sh
318 [ $? -ne 0 ] &&
319 die "autogen failed, see MACHINES file for autoconf,
320 automake, flex, and bison requirements"
321 fi
322
323
324 ####
325 #### Build.
326 ####
327 [ $verbose -ge 1 ] && echo configuring . . . >&3
328 if [ -z "$CFLAGS" ]; then
329 #### Only use these flags with gcc.
330 if cc -dM -E - </dev/null 2>&1 | grep __GNUC__ >/dev/null; then
331 #### configure will supply -g -O2 with gcc, but only if CFLAGS
332 #### isn't defined.
333 CFLAGS='-g -ansi -pedantic'
334 if [ "$config_debug" = n ]; then
335 CFLAGS="$CFLAGS -O2"
336 else
337 CFLAGS="$CFLAGS -O0"
338 fi
339 fi
340 fi
341
342 printf '\n./configure %s\n' "$config_opts${smtpservers:+ $smtpservers}"
343 ./configure CFLAGS="${CFLAGS}" $config_opts${smtpservers:+" $smtpservers"}
344 status=$?
345
346 if [ $status -eq 0 ]; then
347 [ $verbose -ge 1 ] && echo building . . . >&3
348 make
349 status=$?
350
351 if [ $status -eq 0 ]; then
352 if [ "$TESTS_SHELL"x = x ]; then
353 #### Bonus: use heirloom shell to test, if available, and if
354 #### TESTS_SHELL hadn't already been set.
355 heirloom_shell=/usr/lib/heirloom/5bin/sh
356 if [ -x "$heirloom_shell" ]; then
357 TESTS_SHELL="$heirloom_shell"; export TESTS_SHELL
358 fi
359 fi
360
361 if [ "$CFLAGS" ]; then
362 #### Pass DISTCHECK_CONFIGURE_FLAGS through an environment
363 #### variable to avoid automake's quoting.
364 DISTCHECK_CONFIGURE_FLAGS="CFLAGS='${CFLAGS}'"
365 export DISTCHECK_CONFIGURE_FLAGS
366 fi
367
368 [ $verbose -ge 1 ] && echo testing . . . >&3
369 [ "${TERM:-dumb}" = dumb ] && color=no || color=always
370 checkoutput=`make $check AM_COLOR_TESTS=$color`
371 status=$?
372
373 tests_summary=`echo "$checkoutput" | grep tests`
374 #### If multiple tests not run, that line will be caught by the
375 #### "grep tests" above.
376 test_not_run=`echo "$checkoutput" | grep 'test was not run'`
377 fails=`echo "$checkoutput" | grep FAIL`
378 if [ "$tests_summary" ]; then
379 echo '==================='
380 [ "$test_not_run" ] && echo "$test_not_run"
381 [ "$fails" ] && echo "$fails"
382 echo "$tests_summary"
383 echo '==================='
384 [ "$check" = distcheck ] && echo "$checkoutput" | ${tail}4
385 fi
386
387 if [ $status -eq 0 ]; then
388 if [ $install -ge 1 ]; then
389 [ $verbose -ge 1 ] && echo installing . . . >&3
390 ($installpriv make install) >/dev/null
391 status=$?
392 fi
393
394 if [ $status -eq 0 -a $build_rpm -ge 1 ]; then
395 [ $verbose -ge 1 ] && echo building rpm . . . >&3
396 make rpm >/dev/null
397 status=$?
398 fi
399 fi
400 fi
401 else
402 echo "see nmh MACHINES file for build dependences"
403 fi
404
405 ####
406 #### Report results.
407 ####
408
409 #### Disable output redirection (and flush) so that we can grep.
410 exec 1>&3 3>&- 2>&4 4>&-
411
412 if [ "$logfile" != - ]; then
413 rm -f "$logfile"
414 exec 3>&1 >"$logfile" 2>&1
415 fi
416
417 [ -f "$tmpfile" ] && cat "$tmpfile"
418 [ -f "$tmpfile" ] && grep -E 'Error|warn' "$tmpfile"
419
420 #### Put message on stdout, and in log if different.
421 if [ $status -ne 0 -o $verbose -ge 1 -o "$directory" ]; then
422 [ $status -eq 0 ] && indication=succeeded || indication=failed
423 message="build $indication, build log is in ${directory:+$directory/}$logfile"
424 echo "$message"
425 [ "$logfile" = - ] || echo "$message" >&3
426 fi
427
428 exit $status