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