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