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