Updated build-nmh-cygwin for release of Cygwin nmh 1.5-2. To use:
[mmh] / SPECS / build-nmh-cygwin
1 #!/bin/bash
2 #
3 # ==========================================================================
4 # Change from generic-build-script:  added this comment block.
5 # Based on Cygwin generic package build script, customized for nmh.
6 #
7 # This script is incompatible with directory names that contain spaces, etc.
8 # To fix that, a whole bunch of shell variables need to be wrapped with "".
9 # ==========================================================================
10 #
11 # Generic package build script
12 #
13 # $Id: generic-build-script,v 1.47 2006/02/01 14:01:14 igor Exp $
14 #
15 # Package maintainers: if the original source is not distributed as a
16 # (possibly compressed) tarball, set the value of ${src_orig_pkg_name},
17 # and redefine the unpack() helper function appropriately.
18 # Also, if the Makefile rule to run the test suite is not "check", change
19 # the definition of ${test_rule} below.
20
21 # find out where the build script is located
22 tdir=`echo "$0" | sed 's%[\\/][^\\/][^\\/]*$%%'`
23 test "x$tdir" = "x$0" && tdir=.
24 scriptdir=`cd $tdir; pwd`
25 # find src directory.
26 # If scriptdir ends in SPECS, then topdir is $scriptdir/..
27 # If scriptdir ends in CYGWIN-PATCHES, then topdir is $scriptdir/../..
28 # Otherwise, we assume that topdir = scriptdir
29 topdir1=`echo ${scriptdir} | sed 's%/SPECS$%%'`
30 topdir2=`echo ${scriptdir} | sed 's%/CYGWIN-PATCHES$%%'`
31 if [ "x$topdir1" != "x$scriptdir" ] ; then # SPECS
32   topdir=`cd ${scriptdir}/..; pwd`
33 else
34   if [ "x$topdir2" != "x$scriptdir" ] ; then # CYGWIN-PATCHES
35     topdir=`cd ${scriptdir}/../..; pwd`
36   else
37     topdir=`cd ${scriptdir}; pwd`
38   fi
39 fi
40
41 tscriptname=`basename $0 .sh`
42 export PKG=`echo $tscriptname | sed -e 's/\-[^\-]*\-[^\-]*$//'`
43 export VER=`echo $tscriptname | sed -e "s/${PKG}\-//" -e 's/\-[^\-]*$//'`
44 export REL=`echo $tscriptname | sed -e "s/${PKG}\-${VER}\-//"`
45 # BASEPKG refers to the upstream base package
46 # SHORTPKG refers to the Cygwin package
47 # Normally, these are identical, but if the Cygwin package name is different
48 # from the upstream package name, you will want to redefine BASEPKG.
49 # Example: For Apache 2, BASEPKG=httpd-2.x.xx but SHORTPKG=apache2-2.x.xx
50 export BASEPKG=${PKG}-${VER}-${REL}
51 export SHORTPKG=${PKG}-${VER}
52 export FULLPKG=${SHORTPKG}-${REL}
53
54 # determine correct decompression option and tarball filename
55 # Change from generic-build-script:  set scr_orig_pkg_name here.
56 export src_orig_pkg_name=nmh-1.5-2.tar.gz
57 if [ -e "${src_orig_pkg_name}" ] ; then
58   # Change from generic-build-script:  don't need the following line, use
59   # opt_decomp of z.
60   # export opt_decomp=? # Make sure tar punts if unpack() is not redefined
61   export opt_decomp=z
62 elif [ -e ${BASEPKG}.tar.bz2 ] ; then
63   export opt_decomp=j
64   export src_orig_pkg_name=${BASEPKG}.tar.bz2
65 elif [ -e ${BASEPKG}.tar.gz ] ; then
66   export opt_decomp=z
67   export src_orig_pkg_name=${BASEPKG}.tar.gz
68 elif [ -e ${BASEPKG}.tgz ] ; then
69   export opt_decomp=z
70   export src_orig_pkg_name=${BASEPKG}.tgz
71 elif [ -e ${BASEPKG}.tar ] ; then
72   export opt_decomp=
73   export src_orig_pkg_name=${BASEPKG}.tar
74 else
75   echo "Cannot find PKG:${PKG} VER:${VER} REL:${REL}.  Rename $0 to"
76   echo "something more appropriate, and try again."
77   exit 1
78 fi
79
80 export src_orig_pkg=${topdir}/${src_orig_pkg_name}
81
82 # determine correct names for generated files
83 export src_pkg_name=${FULLPKG}-src.tar.bz2
84 export src_patch_name=${FULLPKG}.patch
85 export bin_pkg_name=${FULLPKG}.tar.bz2
86 export log_pkg_name=${FULLPKG}-BUILDLOGS.tar.bz2
87
88 export configurelogname=${FULLPKG}-CONFIGURE.LOG
89 export makelogname=${FULLPKG}-MAKE.LOG
90 export checklogname=${FULLPKG}-CHECK.LOG
91 export installlogname=${FULLPKG}-INSTALL.LOG
92
93 export src_pkg=${topdir}/${src_pkg_name}
94 export src_patch=${topdir}/${src_patch_name}
95 export bin_pkg=${topdir}/${bin_pkg_name}
96 export srcdir=${topdir}/${BASEPKG}
97 export objdir=${srcdir}/.build
98 export instdir=${srcdir}/.inst
99 export srcinstdir=${srcdir}/.sinst
100 export buildlogdir=${srcdir}/.buildlogs
101 export configurelogfile=${srcinstdir}/${configurelogname}
102 export makelogfile=${srcinstdir}/${makelogname}
103 export checklogfile=${srcinstdir}/${checklogname}
104 export installlogfile=${srcinstdir}/${installlogname}
105
106 prefix=/usr
107 # Change from generic-build-script:  use /etc/nmh instead of /etc.
108 sysconfdir=/etc/nmh
109 localstatedir=/var
110 if [ -z "$MY_CFLAGS" ]; then
111   MY_CFLAGS="-O2"
112 fi
113 if [ -z "$MY_LDFLAGS" ]; then
114   MY_LDFLAGS=
115 fi
116
117 # Change from generic-build-script:  disabled install_docs because
118 # nmh's make install takes care of it.  Though it installs in
119 # /usr/share/doc/nmh/ instead of /usr/share/doc/nmh-<version>/,
120 # that seems to be more common on Cygwin than using the -<version>.
121 export install_docs=
122 #export install_docs="`for i in ${install_docs}; do echo $i; done | sort -u`"
123 export test_rule=check
124 if [ -z "$SIG" ]; then
125   export SIG=0  # set to 1 to turn on signing by default
126 fi
127 # Sort in POSIX order.
128 export LC_ALL=C
129
130 # helper functions
131
132 # Provide help.
133 help() {
134 cat <<EOF
135 This is the cygwin packaging script for ${FULLPKG}.
136 Usage: $0 [<option>...] <action>...
137 Options are:
138     help, --help        Print this message
139     version, --version  Print the version message
140     with_logs, --logs   Create logs of remaining steps
141 Actions are:
142     prep                Unpack and patch into ${srcdir}
143     mkdirs              Make hidden directories needed during build
144     conf, configure     Configure the package (./configure)
145     reconf              Rerun configure
146     build, make         Build the package (make)
147     check, test         Run the testsuite (make ${test_rule})
148     clean               Remove built files (make clean)
149     install             Install package to staging area (make install)
150     list                List package contents
151     depend              List package dependencies
152     strip               Strip package executables
153     pkg, package        Prepare the binary package ${bin_pkg_name}
154     mkpatch             Prepare the patch file ${src_patch_name}
155     acceptpatch         Copy patch file ${src_patch_name} to ${topdir}
156     spkg, src-package   Prepare the source package ${src_pkg_name}
157     finish              Remove source directory ${srcdir}
158     checksig            Validate GPG signatures (requires gpg)
159     first               Full run for spkg (mkdirs, spkg, finish)
160     almostall           Full run for bin pkg, except for finish
161     all                 Full run for bin pkg
162 EOF
163 }
164
165 # Provide version of generic-build-script modified to make this script.
166 version() {
167     vers=`echo '$Revision: 1.47 $' | sed -e 's/Revision: //' -e 's/ *\\$//g'`
168     echo "$0 based on generic-build-script $vers"
169 }
170
171 # Unpacks the original package source archive into ./${BASEPKG}/.
172 # Change this if the original package was not tarred
173 # or if it doesn't unpack to a correct directory.
174 unpack() {
175   tar xv${opt_decomp}f "$1"
176 }
177
178 # Make the hidden directories used by this script.
179 mkdirs() {
180   (cd ${topdir} && \
181   rm -fr ${objdir} ${instdir} ${srcinstdir} && \
182   mkdir -p ${objdir} && \
183   mkdir -p ${instdir} && \
184   mkdir -p ${srcinstdir} )
185 }
186 mkdirs_log() {
187   (cd ${topdir} && \
188   mkdirs "$@" && \
189   rm -fr ${buildlogdir} && \
190   mkdir -p ${buildlogdir} )
191 }
192
193 # Unpack the original tarball, and get everything set up for this script.
194 prep() {
195   (cd ${topdir} && \
196   unpack ${src_orig_pkg} && \
197   cd ${topdir} && \
198   if [ -f ${src_patch} ] ; then \
199     patch -Z -p0 < ${src_patch} ;\
200   fi && \
201   mkdirs )
202 }
203 prep_log() {
204   prep "$@" && \
205   mkdirs_log && \
206   if [ -f ${topdir}/${log_pkg_name} ] ; then \
207     # Change from generic-build-script:  do the following in subshell
208     # so that cd isn't permanent.
209     (cd ${buildlogdir} && \
210     tar xvjf "${topdir}"/${log_pkg_name})
211   fi
212 }
213
214 # Configure the package.
215 # Change from generic-build-script:  added following notes:
216 # Could add --with-cyrus-sasl if libsasl2 and libsas2-devel
217 #   packages are installed.
218 # Could add --with-tls if libgnutls26 and gnutls-devel
219 #   packages are installed.
220 #
221 conf() {
222   (cd ${objdir} && \
223   CFLAGS="${MY_CFLAGS}" LDFLAGS="${MY_LDFLAGS}" \
224   ${srcdir}/configure \
225   --srcdir=${srcdir} --prefix="${prefix}" \
226   --exec-prefix='${prefix}' --sysconfdir="${sysconfdir}" \
227   --libdir='${prefix}/lib' --includedir='${prefix}/include' \
228   --mandir='${prefix}/share/man' --infodir='${prefix}/share/info' \
229   --libexecdir='${sbindir}' --localstatedir="${localstatedir}" \
230   --datadir='${prefix}/share' )
231 }
232 conf_log() {
233   conf "$@" 2>&1 | tee ${configurelogfile}
234   return ${PIPESTATUS[0]}
235 }
236
237 # Rerun configure to pick up changes in the environment.
238 reconf() {
239   (cd ${topdir} && \
240   rm -fr ${objdir} && \
241   mkdir -p ${objdir} && \
242   conf )
243 }
244 reconf_log() {
245   reconf "$@" 2>&1 | tee ${configurelogfile}
246   return ${PIPESTATUS[0]}
247 }
248
249 # Run make.
250 build() {
251   (cd ${objdir} && \
252   make CFLAGS="${MY_CFLAGS}" )
253 }
254 build_log() {
255   build "$@" 2>&1 | tee ${makelogfile}
256   return ${PIPESTATUS[0]}
257 }
258
259 # Run the package testsuite.
260 check() {
261   (cd ${objdir} && \
262   make -k ${test_rule} )
263 }
264 check_log() {
265   check "$@" 2>&1 | tee ${checklogfile}
266   return ${PIPESTATUS[0]}
267 }
268
269 # Remove files created by configure and make.
270 clean() {
271   (cd ${objdir} && \
272   make clean )
273 }
274
275 # Install the package, with DESTDIR set to '.inst'.
276 # Change from generic-build-script:  added ":;" after "find ... | gzip"
277 # because it returns non-zero status.
278 install() {
279   (cd ${objdir} && \
280   rm -fr ${instdir}/* && \
281   make install DESTDIR=${instdir} && \
282   for f in ${prefix}/share/info/dir ${prefix}/info/dir ; do \
283     if [ -f ${instdir}${f} ] ; then \
284       rm -f ${instdir}${f} ; \
285     fi ;\
286   done &&\
287   for d in ${prefix}/share/doc/${SHORTPKG} ${prefix}/share/doc/Cygwin ; do \
288     if [ ! -d ${instdir}${d} ] ; then \
289       mkdir -p ${instdir}${d} ;\
290     fi ;\
291   done &&\
292   if [ -d ${instdir}${prefix}/share/info ] ; then \
293     find ${instdir}${prefix}/share/info -name "*.info" | xargs -r gzip -q ; \
294   fi && \
295   if [ -d ${instdir}${prefix}/share/man ] ; then \
296     find ${instdir}${prefix}/share/man -name "*.1" -o -name "*.3" -o \
297       -name "*.3x" -o -name "*.3pm" -o -name "*.5" -o -name "*.6" -o \
298       -name "*.7" -o -name "*.8" | xargs -r gzip -q ; :; \
299   fi && \
300   templist="" && \
301   for fp in ${install_docs} ; do \
302     case "$fp" in \
303       */) templist="$templist `find ${srcdir}/$fp -type f`" ;;
304       *)  for f in ${srcdir}/$fp ; do \
305             if [ -f $f ] ; then \
306               templist="$templist $f"; \
307             fi ; \
308           done ;; \
309     esac ; \
310   done && \
311   if [ ! "x$templist" = "x" ]; then \
312     /usr/bin/install -m 644 $templist \
313          ${instdir}${prefix}/share/doc/${SHORTPKG} ; \
314   fi && \
315   if [ -f ${srcdir}/CYGWIN-PATCHES/${PKG}.README ]; then \
316     /usr/bin/install -m 644 ${srcdir}/CYGWIN-PATCHES/${PKG}.README \
317       ${instdir}${prefix}/share/doc/Cygwin/${SHORTPKG}.README ; \
318   elif [ -f ${srcdir}/CYGWIN-PATCHES/README ] ; then \
319     /usr/bin/install -m 644 ${srcdir}/CYGWIN-PATCHES/README \
320       ${instdir}${prefix}/share/doc/Cygwin/${SHORTPKG}.README ; \
321   fi && \
322   if [ -f ${srcdir}/CYGWIN-PATCHES/${PKG}.sh ] ; then \
323     if [ ! -d ${instdir}${sysconfdir}/postinstall ]; then \
324       mkdir -p ${instdir}${sysconfdir}/postinstall ; \
325     fi && \
326     /usr/bin/install -m 755 ${srcdir}/CYGWIN-PATCHES/${PKG}.sh \
327       ${instdir}${sysconfdir}/postinstall/${PKG}.sh ; \
328   elif [ -f ${srcdir}/CYGWIN-PATCHES/postinstall.sh ] ; then \
329     if [ ! -d ${instdir}${sysconfdir}/postinstall ]; then \
330       mkdir -p ${instdir}${sysconfdir}/postinstall ; \
331     fi && \
332     /usr/bin/install -m 755 ${srcdir}/CYGWIN-PATCHES/postinstall.sh \
333       ${instdir}${sysconfdir}/postinstall/${PKG}.sh ; \
334   fi && \
335   if [ -f ${srcdir}/CYGWIN-PATCHES/preremove.sh ] ; then \
336     if [ ! -d ${instdir}${sysconfdir}/preremove ]; then \
337       mkdir -p ${instdir}${sysconfdir}/preremove ; \
338     fi && \
339     /usr/bin/install -m 755 ${srcdir}/CYGWIN-PATCHES/preremove.sh \
340       ${instdir}${sysconfdir}/preremove/${PKG}.sh ; \
341   fi &&
342   if [ -f ${srcdir}/CYGWIN-PATCHES/manifest.lst ] ; then
343     if [ ! -d ${instdir}${sysconfdir}/preremove ]; then
344       mkdir -p ${instdir}${sysconfdir}/preremove ;
345     fi &&
346     /usr/bin/install -m 644 ${srcdir}/CYGWIN-PATCHES/manifest.lst \
347       ${instdir}${sysconfdir}/preremove/${PKG}-manifest.lst ;
348   fi )
349 }
350 install_log() {
351   install "$@" 2>&1 | tee ${installlogfile}
352   return ${PIPESTATUS[0]}
353 }
354
355 # Strip all binaries.
356 strip() {
357   (cd ${instdir} && \
358   find . -name "*.dll" -or -name "*.exe" | xargs -r strip 2>&1 ; \
359   true )
360 }
361
362 # List all non-hidden files that belong to the package.
363 list() {
364   (cd ${instdir} && \
365   find . -name "*" ! -type d | sed 's%^\.%  %' | sort ; \
366   true )
367 }
368
369 # List the static .dll dependencies of the package.  This does not pick up
370 # dynamic dependencies (whether or not libtool was used), nor does it pick
371 # up program dependencies, such as system() depending on /bin/sh.
372 depend() {
373   (cd ${instdir} && \
374   find ${instdir} -name "*.exe" -o -name "*.dll" | xargs -r cygcheck | \
375   sed -ne '/^  [^ ]/ s,\\,/,gp' | sort -bu | \
376   xargs -r -n1 cygpath -u | xargs -r cygcheck -f | sed 's%^%  %' | sort -u ; \
377   true )
378 }
379
380 # Build the binary package tarball.
381 pkg() {
382   (cd ${instdir} && \
383   tar cvjf ${bin_pkg} * )
384 }
385
386 # Compare the original tarball against cygwin modifications.
387 mkpatch() {
388   (cd ${srcdir} && \
389   find . -name "autom4te.cache" | xargs -r rm -rf ; \
390   unpack ${src_orig_pkg} && \
391   mv ${BASEPKG} ../${BASEPKG}-orig && \
392   cd ${topdir} && \
393   diff -urN -x '.build' -x '.inst' -x '.sinst' -x '.buildlogs' \
394     ${BASEPKG}-orig ${BASEPKG} > \
395     ${srcinstdir}/${src_patch_name} ; \
396   rm -rf ${BASEPKG}-orig )
397 }
398
399 # Note: maintainer-only functionality
400 acceptpatch() {
401   cp --backup=numbered ${srcinstdir}/${src_patch_name} ${topdir}
402 }
403
404 # Build the source tarball.
405 spkg() {
406   (mkpatch && \
407   if [ "${SIG}" -eq 1 ] ; then \
408     name=${srcinstdir}/${src_patch_name} text="PATCH" sigfile ; \
409   fi && \
410   cp ${src_orig_pkg} ${srcinstdir}/${src_orig_pkg_name} && \
411   if [ -e ${src_orig_pkg}.sig ] ; then \
412     cp ${src_orig_pkg}.sig ${srcinstdir}/ ; \
413   fi && \
414   cp $0 ${srcinstdir}/`basename $0` && \
415   name=$0 text="SCRIPT" sigfile && \
416   if [ "${SIG}" -eq 1 ] ; then \
417     cp $0.sig ${srcinstdir}/ ; \
418   fi && \
419   cd ${srcinstdir} && \
420   tar cvjf ${src_pkg} * )
421 }
422 spkg_log() {
423   spkg "$@" && \
424   (cd ${srcinstdir} && \
425   if [ -e ${configurelogname} -o -e ${makelogname} -o \
426        -e ${checklogname} -o -e ${installlogname} ]; then
427     tar --ignore-failed-read -cvjf ${log_pkg_name} \
428       ${configurelogname} ${makelogname} ${checklogname} ${installlogname} && \
429     rm -f \
430       ${configurelogname} ${makelogname} ${checklogname} ${installlogname} ; \
431   fi && \
432   tar uvjf ${src_pkg} * )
433 }
434
435 # Clean up everything.
436 finish() {
437   rm -rf ${srcdir}
438 }
439
440 # Generate GPG signatures.
441 sigfile() {
442   if [ \( "${SIG}" -eq 1 \) -a \( -e $name \) -a \( \( ! -e $name.sig \) -o \( $name -nt $name.sig \) \) ]; then \
443     if [ -x /usr/bin/gpg ]; then \
444       echo "$text signature need to be updated"; \
445       rm -f $name.sig; \
446       /usr/bin/gpg --detach-sign $name; \
447     else \
448       echo "You need the gnupg package installed in order to make signatures."; \
449     fi; \
450   fi
451 }
452
453 # Validate GPG signatures.
454 checksig() {
455   if [ -x /usr/bin/gpg ]; then \
456     if [ -e ${src_orig_pkg}.sig ]; then \
457       echo "ORIGINAL PACKAGE signature follows:"; \
458       /usr/bin/gpg --verify ${src_orig_pkg}.sig ${src_orig_pkg}; \
459     else \
460       echo "ORIGINAL PACKAGE signature missing."; \
461     fi; \
462     if [ -e $0.sig ]; then \
463       echo "SCRIPT signature follows:"; \
464       /usr/bin/gpg --verify $0.sig $0; \
465     else \
466       echo "SCRIPT signature missing."; \
467     fi; \
468     if [ -e ${src_patch}.sig ]; then \
469       echo "PATCH signature follows:"; \
470       /usr/bin/gpg --verify ${src_patch}.sig ${src_patch}; \
471     else \
472       echo "PATCH signature missing."; \
473     fi; \
474   else
475     echo "You need the gnupg package installed in order to check signatures." ; \
476   fi
477 }
478
479 f_mkdirs=mkdirs
480 f_prep=prep
481 f_conf=conf
482 f_reconf=conf
483 f_build=build
484 f_check=check
485 f_install=install
486 f_spkg=spkg
487
488 enablelogging() {
489   f_mkdirs=mkdirs_log && \
490   f_prep=prep_log && \
491   f_conf=conf_log && \
492   f_reconf=reconf_log && \
493   f_build=build_log && \
494   f_check=check_log && \
495   f_install=install_log && \
496   f_spkg=spkg_log
497 }
498
499 while test -n "$1" ; do
500   case $1 in
501     help|--help)        help ; STATUS=$? ;;
502     version|--version)  version ; STATUS=$? ;;
503     with_logs|--logs)   enablelogging ; STATUS=$? ;;
504     prep)               $f_prep ; STATUS=$? ;;
505     mkdirs)             $f_mkdirs ; STATUS=$? ;;
506     conf)               $f_conf ; STATUS=$? ;;
507     configure)          $f_conf ; STATUS=$? ;;
508     reconf)             $f_reconf ; STATUS=$? ;;
509     build)              $f_build ; STATUS=$? ;;
510     make)               $f_build ; STATUS=$? ;;
511     check)              $f_check ; STATUS=$? ;;
512     test)               $f_check ; STATUS=$? ;;
513     clean)              $f_clean ; STATUS=$? ;;
514     install)            $f_install ; STATUS=$? ;;
515     list)               list ; STATUS=$? ;;
516     depend)             depend ; STATUS=$? ;;
517     strip)              strip ; STATUS=$? ;;
518     package)            pkg ; STATUS=$? ;;
519     pkg)                pkg ; STATUS=$? ;;
520     mkpatch)            mkpatch ; STATUS=$? ;;
521     acceptpatch)        acceptpatch ; STATUS=$? ;;
522     src-package)        $f_spkg ; STATUS=$? ;;
523     spkg)               $f_spkg ; STATUS=$? ;;
524     finish)             finish ; STATUS=$? ;;
525     checksig)           checksig ; STATUS=$? ;;
526     first)              $f_mkdirs && $f_spkg && finish ; STATUS=$? ;;
527     almostall)          checksig && $f_prep && $f_conf && $f_build && \
528                         $f_install && strip && pkg && $f_spkg ; STATUS=$? ;;
529     all)                checksig && $f_prep && $f_conf && $f_build && \
530                         $f_install && strip && pkg && $f_spkg && finish ; \
531                         STATUS=$? ;;
532     *) echo "Error: bad arguments" ; exit 1 ;;
533   esac
534   ( exit ${STATUS} ) || exit ${STATUS}
535   shift
536 done