Fixed distdef prefix handling and processing of non-source files
[autodist.git] / apps / autodist / autodist.in
1 #!/bin/sh
2 #
3 # Author: Pekka Riikonen <priikone@silcnet.org>
4 #
5 # Copyright (C) 2005 - 2007 Pekka Riikonen
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are
10 # met:
11 #
12 #   1. Redistributions of source code must retain the above copyright
13 #      notice, this list of conditions and the following disclaimer.
14 #   2. Redistributions in binary form must reproduce the above copyright
15 #      notice, this list of conditions and the following disclaimer in the
16 #      documentation and/or other materials provided with the distribution.
17 #   3. The name of the author may not be used to endorse or promote
18 #      products derived from this software without specific prior written
19 #      permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
24 # NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26 # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #
32
33 ###############################################################################
34 # Shell compatibility
35
36 # Be Bourne compatible
37 if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
38   # Despite of this, we have managed to segfault some zsh's.
39   emulate sh
40   NULLCMD=:
41   # Zsh 3.x and 4.x performs word splitting on ${1+"$@"}, which
42   # is contrary to our usage.  Disable this feature.
43   alias -g '${1+"$@"}'='"$@"'
44 fi
45 DUALCASE=1; export DUALCASE # for MKS sh
46
47 # Support unset when possible.
48 if ( (MAIL=60; unset MAIL) || exit) >/dev/null 2>&1; then
49   as_unset=unset
50 else
51   as_unset=false
52 fi
53
54 # Work around bugs in pre-3.0 UWIN ksh.
55 $as_unset ENV MAIL MAILPATH
56 PS1='$ '
57 PS2='> '
58 PS4='+ '
59
60 # NLS nuisances.
61 for as_var in \
62   LANG LANGUAGE LC_ADDRESS LC_ALL LC_COLLATE LC_CTYPE LC_IDENTIFICATION \
63   LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER \
64   LC_TELEPHONE LC_TIME
65 do
66   if (set +x; test -z "`(eval $as_var=C; export $as_var) 2>&1`"); then
67     eval $as_var=C; export $as_var
68   else
69     $as_unset $as_var
70   fi
71 done
72
73
74 ###############################################################################
75 # Global variables
76
77 # Packaging and compressing
78 ad_gzip=true
79 ad_bzip2=false
80 ad_compress=false
81 ad_zip=false
82
83 # Distribution subdirectory
84 distdir="distdir"
85 am_distdir=
86
87 # This current distribution
88 distribution=default
89 distfile=$distribution
90 dist_version=0.0
91 package=
92 bug_report=
93
94 # All inherited distributions in this distribution
95 inherits=
96
97 # All distribution defines for this distribution
98 distdefs=
99
100 # All distribution undefines for this distribution
101 undistdefs=
102
103 # All distribution options
104 doptions=
105 opt_template=false
106 opt_no_dist=false
107 opt_no_inherit=false
108
109 # All includes
110 includes=
111
112 # All excludes
113 excludes=
114
115 # All noprocesses
116 noprocess=
117
118 # All hooks
119 pre_hooks=
120 post_hooks=
121 pre_dist_hooks=
122 post_dist_hooks=
123 pre_p_dist_hooks=
124 post_p_dist_hooks=
125
126 # Distribution license and license header
127 license=
128 licenseh=
129
130 # Whether to output ad_debug information
131 debug=false
132 nolog=false
133
134 # Autodist version
135 ver=@VERSION@
136
137 ###############################################################################
138 # Configuration file
139 if test -f "$distdir/autodist.conf"; then
140   . $distdir/autodist.conf
141 fi
142
143 DP=$DISTPREFIX
144
145
146 ###############################################################################
147 # Functions
148
149 #
150 # Print out debug information if debugging is enabled.  To enable debugging
151 # set the global variable "debug" to true value.
152 #
153 # Arguments: ad_debug <ad_debug string>
154 #
155 ad_debug()
156 {
157   if test x$debug = xtrue; then
158     set -f
159     echo autodist: $1
160     set +f
161   fi
162 }
163
164 #
165 # Prints out error message and exits the script.
166 #
167 # Arguments: ad_fatal <error message>
168 #
169 ad_fatal()
170 {
171   set -f
172   ad_log "error: $1"
173   echo autodist: error: $1
174   set +f
175   exit 1
176 }
177
178 #
179 # Prints out warning message
180 #
181 # Arguments: ad_warning <warning message>
182 #
183 ad_warning()
184 {
185   set -f
186   ad_log "warning: $1"
187   echo autodist: warning: $1
188   set +f
189 }
190
191 #
192 # Opens a log file.
193 #
194 # Arguments: ad_log_open <logfile>
195 #
196 ad_log_open()
197 {
198   rm -rf $1
199   exec 5> $1
200
201   cat >&5 << EOF
202 This file contains messages produced by the Autodist $ver.
203
204 EOF
205 }
206
207 #
208 # Prints to an open log file
209 #
210 # Arguments: ad_log
211 #
212 ad_log()
213 {
214   if test x$nolog = xfalse; then
215     echo "$1" >&5
216   fi
217 }
218
219 #
220 # Initializes the Autodist environment, creates default distribution
221 # directory, and default distribution.
222 #
223 # Arguments: ad_initialize
224 #
225 ad_initialize()
226 {
227   ad_debug "Initializing Autodist environment"
228
229   # Create default distdir
230   if test '!' -f $distdir; then
231     mkdir -p -- $distdir
232   fi
233
234   # Create Autodist configuration file
235   if test -f @AUTODISTDIR@/autodist.conf; then
236     cp -p @AUTODISTDIR@/autodist.conf $distdir
237   fi
238
239   # Create default distribution
240   if test -f @AUTODISTDIR@/default; then
241     cp -p @AUTODISTDIR@/default $distdir
242   fi
243
244   ad_debug "Autodist environment initialized"
245 }
246
247 #
248 # Creates the distdefs header file including defined distdefs
249 #
250 # Arguments: ad_create_distdefs_h
251 #
252 ad_create_distdefs_h()
253 {
254   ad_debug "Creating distdef header file"
255
256   fname=$DISTDEFS
257   rm -f $fname
258   cat > $fname <<EOF
259 /*
260   Automatically generated by Autodist $ver.  Do not edit.
261
262   Generated: `date` by `whoami`
263   Distribution: $distribution
264   License: $license
265 */
266
267 #ifndef _`echo $DP`_DISTDEFS_H
268 #define _`echo $DP`_DISTDEFS_H
269
270 EOF
271
272   for i in $distdefs
273   do
274     echo "#define $i 1" >>$fname
275   done
276
277   cat >> $fname <<EOF
278
279 #endif /* _`echo $DP`_DISTDEFS_H */
280 EOF
281
282   ad_debug "Distdef header file created"
283 }
284
285 #
286 # Creates the main configure script for the distribution.  This runs
287 # the aclocal, autoheader and autoconf tools.
288 #
289 # Arguments: ad_make_configure
290 #
291 ad_make_configure()
292 {
293   local run_autoconf=false
294
295   ad_debug "Starting configure creation"
296
297   rm -f configure
298
299   if test "$ACLOCAL"; then
300     ad_debug "Running aclocal"
301     if test x$debug = xtrue; then
302       $ACLOCAL
303     else
304       $ACLOCAL 1>/dev/null 2>/dev/null
305     fi
306     if test $? != 0; then
307       ad_fatal "aclocal failed"
308     fi
309   fi
310
311   if test "$AUTOCONF"; then
312     ad_debug "Running autoconf"
313     $AUTOCONF
314     if test $? != 0; then
315       ad_fatal "autoconf failed"
316     fi
317     run_autoconf=true
318   fi
319
320   if test "$AUTOHEADER"; then
321     ad_debug "Running autoheader"
322     $AUTOHEADER
323     if test $? != 0; then
324       ad_fatal "autoheader failed"
325     fi
326   fi
327
328   if test "$LIBTOOLIZE"; then
329     ad_debug "Running libtoolize"
330     $LIBTOOLIZE
331     if test $? != 0; then
332       ad_fatal "libtoolize failed"
333     fi
334   fi
335
336   if test x$run_autoconf = xtrue; then
337     if test '!' -f configure; then
338       ad_fatal "creating configure script failed"
339     fi
340   fi
341
342   ad_debug "Ending configure creation"
343 }
344
345 #
346 # Creates the configure.ac script from the configure.ad fragments in
347 # the source tree.  Takes the source configure file as argument which
348 # is used to create the actual configure.ac.
349 #
350 # Arguments: ad_make_configure_ac <configure_ac_source>
351 #
352 ad_make_configure_ac()
353 {
354   local check
355
356   ad_debug "Starting creating configure.ac: $1"
357
358   ad_log ""
359   ad_log "Following configure.ad files were processed into configure.ac:"
360   ad_log "--------------------------------------------------------------"
361
362   if test '!' -f $1; then
363     ad_fatal "The configure file '$1' does not exist"
364   fi
365
366   check="`sed 's/^[     ]*//' < $1 | grep -v "^#" | grep -e "AD_INIT"`"
367   if test -z $check; then
368     rm -f configure.ad.cfs
369     rm -f $fname $fname.tmp
370     ad_fatal "The 'AD_INIT' macro has not been set in configure.ac"
371   fi
372
373   rm -f configure.ac configure.ad.cfs
374
375   cfs=`find . -type f -name configure\*\.ad`
376   for i in $cfs
377   do
378     if test "x$i" = "x$1"; then
379       continue
380     fi
381
382     ad_debug "including $i"
383     ad_log "  $i"
384
385     cat $i >> configure.ad.cfs
386   done
387
388   if test -f configure.ad.cfs; then
389     check="`sed 's/^[   ]*//' < $1 | grep -v "^#" | grep -e "AD_INCLUDE_CONFIGURE"`"
390     if test -z $check; then
391       rm -f configure.ad.cfs
392       ad_warning "configure.ad fragments found but 'AD_INCLUDE_CONFIGURE' is not set"
393     fi
394   fi
395
396   # Header for configure.ac
397   fname="configure.tmp.ac"
398   cat > $fname <<EOF
399 # Automatically generated by Autodist $ver.  Do not edit.
400 # To make changes edit the configure.ad file in the source tree.
401
402 # Source: configure.ad
403 # Generated: `date` by `whoami`
404 # Distribution: $distribution
405 # License: $license
406
407 EOF
408
409   ad_debug "creating configure.ac"
410   if test -f configure.ad.cfs; then
411     sed '/^AD_INCLUDE_CONFIGURE/ r configure.ad.cfs' $1 > $fname.tmp
412     sed -e "/^AD_INCLUDE_CONFIGURE/d" $fname.tmp >> $fname
413     rm -f configure.ad.cfs $fname.tmp
414   else
415     cat $1 >> $fname
416   fi
417
418   # Process AD_INIT
419   sed -e "/AD_INIT/s//AC_INIT([$distribution], [$dist_version], [$bug_report], [$package])/" $fname > $fname.tmp
420
421   # Remove AD_DISABLE_DEPENDENCIES
422   sed -e "/^AD_DISABLE_DEPENDENCIES/d" $fname.tmp > $fname
423
424   # Process for distribution
425   rm -f $fname.tmp
426   ad_process_file $fname $fname.tmp false
427
428   # Remove any trailing backslashes
429   if test -f "$fname.tmp"; then
430     sed -e :a -e '/\\$/N; s/[   ]*\\\n//; ta' < $fname.tmp > configure.ac
431   else
432     cp -p $fname configure.ac
433   fi
434   rm -f $fname $fname.tmp
435
436   ad_log "  ./configure.ad"
437   ad_log ""
438
439   ad_debug "Ending creating configure.ac: $1"
440 }
441
442 #
443 # Creates the Makefile.in files by running the automake tool.
444 #
445 # Arguments: ad_make_makefile_ins
446 #
447 ad_make_makefile_ins()
448 {
449   ad_debug "Starting creating Makefile.in files"
450
451   if test "$AUTOMAKE"; then
452     ad_debug "Running automake"
453     $AUTOMAKE
454     if test $? != 0; then
455       ad_fatal "automake failed"
456     fi
457   fi
458
459   ad_debug "Ending creating Makefile.in files"
460 }
461
462 #
463 # Creates the Makefile.am files from the Makefile.ad files in the
464 # source tree.  This runs the distribution specific processing for the
465 # Makefile.ad files.
466 #
467 # Arguments: ad_make_makefile_ams
468 #
469 ad_make_makefile_ams()
470 {
471   ad_debug "Starting creating Makefile.am files"
472
473   ad_log ""
474   ad_log "Following Makefile.ad files were processed into Makefile.am files:"
475   ad_log "------------------------------------------------------------------"
476
477   files=`find . -type f -name Makefile\.ad`
478   for ff in $files
479   do
480     ad_log "  $ff"
481     fname=`echo $ff | sed s/\.ad//`
482     ad_make_makefile_am $ff $fname.am
483   done
484   ad_log ""
485
486   ad_debug "Ending creating Makefile.am files"
487 }
488
489 #
490 # Creates Makefile.am file from the Makefile.ad file.
491 #
492 # Arguments: ad_make_makefile_am <src> <dst>
493 #
494 ad_make_makefile_am()
495 {
496   local am_deps=true
497   local f=$1
498   local fname=$2
499
500   # Disable dependencies if requested
501   dc=`sed 's/^[         ]*//' < configure.ad | grep -v "^#" \
502     | grep "AD_DISABLE_DEPENDENCIES"`
503   if test "$dc" = "AD_DISABLE_DEPENDENCIES"; then
504     am_deps=false
505   fi
506
507   # Header for the Makefile.am
508   cat > $fname <<EOF
509 # Automatically generated by Autodist $ver from Makefile.ad.  Do not edit.
510 # To make changes edit the $f file in the source tree.
511
512 # Source: $f
513 # Generated: `date` by `whoami`
514 # Distribution: $distribution
515 # License: $license
516
517 EOF
518
519   # Run the distribution processing for this Makefile.ad
520   ad_debug "Processing $f to be $fname"
521   ad_process_file $f $fname.tmp false
522
523   # Remove any trailing backslashes
524   if test -f "$fname.tmp"; then
525     sed -e :a -e '/\\$/N; s/[   ]*\\\n//; ta' < $fname.tmp >> $fname
526   else
527     cat $f >> $fname
528   fi
529
530   # Enable dependencies if requested
531   if test x$am_deps = xtrue; then
532     # Get list of configure.ad's to get them into deps also
533     cfs=`find . -type f -name configure\*\.ad`
534     cfs=`echo $cfs | sed 's/\.\///g'`
535
536     cat >> $fname <<EOF
537
538 # S_AD_ENABLE_DEPENDENCIES
539 \$(srcdir)/Makefile.am: Makefile.ad
540         cd \$(top_srcdir) && autodist -p makefile \$(subdir)/Makefile.ad \$(subdir)/Makefile.am && cd \$(subdir)
541 \$(srcdir)/configure.ac: $cfs
542         cd \$(top_srcdir) && autodist -p configure \$(top_srcdir)/configure.ad && cd \$(subdir)
543 # E_AD_ENABLE_DEPENDENCIES
544 EOF
545   fi
546
547   rm -f $fname.tmp
548 }
549
550 #
551 # Processes all files with .ad suffix, with exception of configure*.ad
552 # and Makefile.ad files, for distribution from the source tree.
553 #
554 # Arguments: ad_process_ads false
555 #
556 ad_process_ads()
557 {
558   ad_debug "Starting processing .ad files"
559
560   ad_log ""
561   ad_log "Following .ad files were processed:"
562   ad_log "-----------------------------------"
563
564   files=`find . -type f -name \*\.ad \! -name configure\*\.ad \! -name Makefile\.ad`
565   for i in $files
566   do
567     fname=`echo $i | sed s/\.ad//`
568     orig=$i
569
570     ad_debug "Processing $i to be $fname"
571     ad_log "  $i into $fname"
572
573     rm -f $fname
574
575     # Run the distribution processing for this file
576     ad_process_file $orig $fname false
577
578     if test '!' -f "$fname"; then
579       cp -p $orig $fname || exit 1
580     fi
581   done
582   ad_log ""
583
584   ad_debug "Ending processing .ad files"
585 }
586
587 #
588 # Includes files specified in the distribution for inclusion.  Used when
589 # creating the distribution for packaging.
590 #
591 # include has the following format in distfile:
592 #
593 #   include <path> [<dest path>]
594 #
595 # If only source path, which may be file, directory or regular expression,
596 # is specified the path will be same in distribution.  If the destination
597 # path is specified that will be the new name and/or new location of the
598 # source path.  This, in effect, is a cp utility with ability to create
599 # directories if they do not exist.
600 #
601 # Arguments: ad_dist_includes <includeslist> <recursive> <log>
602 #
603 ad_dist_includes()
604 {
605   local incs
606
607   ad_debug "Starting running includes: $1 $2"
608
609   if test x$3 = xtrue; then
610     ad_log ""
611     ad_log "Following files and directories were included in distribution:"
612     ad_log "--------------------------------------------------------------"
613   fi
614
615   # By default do not expand pathnames
616   set -f
617
618   # Add : separator at the end
619   incs="`echo "$1" | sed 's/$/ : /'`"
620
621   src=
622   dst=
623   for i in $incs
624   do
625     if test "$i" = ":" && test -z "$src"; then
626       continue
627     fi
628     if test -z "$src"; then
629       src=$i
630       continue
631     fi
632     if test -z "$dst" && test "$i" != ":"; then
633       dst=$i
634     else
635       dst=$src
636     fi
637
638     ad_debug "Including $src into $dst"
639
640     if test -f "$src"; then
641       # Add file
642
643       if test "$src" = "$dst"; then
644         # Add to same location
645         d=`echo $src | sed 's,/[^/]*$,,'`
646         if test "$d" != "$src" && test "$d" != "." && \
647            test '!' -d $am_distdir/$d; then
648           mkdir -p -- $am_distdir/$d || exit 1
649         fi
650       else
651         # Add to different location
652         check=`echo "$dst" | sed 's/?//; s/*//; s/\[//; s/\]//'`
653         if test "$check" != "$dst"; then
654           ad_fatal "Invalid destination in 'include $src $dst'"
655         fi
656
657         d=`echo $dst | sed 's,/[^/]*$,,'`
658         if test "$d" != "$dst" && test "$d" != "." && \
659            test '!' -d $am_distdir/$d; then
660           mkdir -p -- $am_distdir/$d || exit 1
661         fi
662       fi
663
664       if test x$3 = xtrue; then
665         ad_log "  $src into $am_distdir/$d"
666       fi
667       cp -p $src $am_distdir/$d || exit 1
668
669     elif test -d "$src"; then
670       # Add directory
671
672       if test "$src" = "$dst"; then
673         # Add to same location
674         d=`echo $src | sed 's,/[^/]*$,,'`
675         ds=`echo $src | sed 's/\/$//'`
676         if test "$ds" = "$d"; then
677           d=`echo $d | sed 's,/[^/]*$,,'`
678         fi
679         if test "$ds" = "$d"; then
680           d=""
681         fi
682         if test '!' -d $am_distdir/$d && test "$ds" != "$d"; then
683           mkdir -p -- $am_distdir/$d || exit 1
684         fi
685
686         if test x$3 = xtrue; then
687           ad_log "  $src into $am_distdir/$d"
688         fi
689         cp -pR $src $am_distdir/$d || exit 1
690       else
691         # Add to different location
692         check=`echo "$dst" | sed 's/?//; s/*//; s/\[//; s/\]//'`
693         if test "$check" != "$dst"; then
694           ad_fatal "Invalid destination in 'include $src $dst'"
695         fi
696
697         d=`echo $dst | sed 's,/[^/]*$,,'`
698         ds=`echo $dst | sed 's/\/$//'`
699         if test "$ds" = "$d"; then
700           d=`echo $d | sed 's,/[^/]*$,,'`
701         fi
702         if test '!' -d $am_distdir/$d && test "$dst" != "$d"; then
703           mkdir -p -- $am_distdir/$d || exit 1
704         fi
705
706         if test x$3 = xtrue; then
707           ad_log "  $src into $am_distdir/$dst"
708         fi
709         cp -pR $src $am_distdir/$dst || exit 1
710       fi
711
712     elif test x$2 != xtrue; then
713       # We assume regular expression in filename
714       check=`echo "$src" | sed 's/?//; s/*//; s/\[//; s/\]//'`
715       if test "$check" == "$src"; then
716         if test '!' -a $src; then
717           ad_fatal "Including $src: No such file or directory"
718         fi
719         src=
720         dst=
721         continue
722       fi
723
724       # Recursively call this function with expanded pathnames.  The
725       # reason why we don't let sh by default expand pathnames is that
726       # the include's destination is optional.  If sh expands by default
727       # we don't know the destination.  For this reason, we handle the
728       # expansion here ourselves.
729
730       # If src and dst are same, then expand the pathname as we'll copy
731       # matches to their own locations.
732       if test "$src" = "$dst"; then
733         # Expand pathnames, and format to our include format
734         set +f
735         srcs=`echo $src | sed -e 's/ / : /g' -e 's/^/ : /'` || exit 1
736         set -f
737       else
738         # Destination is new, and it has to be a directory.
739         check=`echo "$dst" | sed 's/?//; s/*//; s/\[//; s/\]//'`
740         if test "$check" != "$dst"; then
741           ad_fatal "Invalid destination in 'include $src $dst'"
742         fi
743
744         # Make sure dst has / at the end, as this must be a directory
745         dst=`echo $dst | sed 's/\/$//; s/$/\//'`
746
747         # Escape dst for sed
748         dste=`echo $dst | sed 's/\\//\\\\\//g'` || exit 1
749
750         # Expand pathnames, and format to our include format
751         set +f
752         srcs=`echo $src | sed -e "s/ / $dste : /g" \
753           -e 's/^/ : /' -e "s/$/ $dste/"` || exit 1
754         set -f
755       fi
756
757       # Include recursively
758       ad_dist_includes "$srcs" true
759
760     elif test '!' -a $src; then
761       ad_fatal "Including $src: No such file or directory"
762     fi
763
764     src=
765     dst=
766   done
767
768   if test x$3 = xtrue; then
769     ad_log ""
770   fi
771
772   set +f
773
774   ad_debug "Ending running includes: $1 $2"
775 }
776
777 #
778 # Excludes files specified in the distribution for exclusion.  Used when
779 # creating the distribution for packaging.
780 #
781 # exclude has the following format in distfile:
782 #
783 #  exclude <path>
784 #
785 # The path may be file, directory or regular expression.
786 #
787 # Arguments: ad_dist_includes <excludelist> <log>
788 #
789 ad_dist_excludes()
790 {
791   ad_debug "Starting running excludes: $1"
792
793   if test x$2 = xtrue; then
794     ad_log ""
795     ad_log "Following files and directories were excluded from distribution:"
796     ad_log "----------------------------------------------------------------"
797   fi
798
799   cur=`pwd`
800   cd $am_distdir || exit 1
801   for i in $1
802   do
803     ad_debug "Excluding $i"
804     if test x$2 = xtrue; then
805       ad_log "  $i"
806     fi
807     rm -rf $i
808   done
809   cd $cur || exit 1
810
811   if test x$2 = xtrue; then
812     ad_log ""
813   fi
814
815   ad_debug "Ending running excludes: $1"
816 }
817
818 #
819 # Processes the entire tree for distribution.  This inspects files other
820 # than source and header files, with exception of any file with .ad
821 # suffix, and performs distribution processing for the file.  The original
822 # file is replaced with the processed file.  This function is used when
823 # creating the distribution for packaging.
824 #
825 # Arguments: ad_process_tree <directory>
826 #
827 ad_process_tree()
828 {
829   ad_debug "Starting processing non-source files: $1"
830
831   # Take files, except source and .ad files.
832   files=`find $am_distdir -type f \! -name \*\.ad \( \
833         \! -name \*\.[cC] -a \
834         \! -name \*\.[cC][cCpP] -a \
835         \! -name \*\.[cC][xX][xX] -a \
836         \! -name \*\.[cC][pP][pP] -a \
837         \! -name \*\.[cC]++ -a \
838         \! -name \*\.m -a \
839         \! -name \*\.mm -a \
840         \! -name \*\.M -a \
841         \! -name \*\.S -a \
842         \! -name \*\.[hH] -a \
843         \! -name \*\.hh -a \
844         \! -name \*\.[cC]\.in -a \
845         \! -name \*\.[cC][cCpP]\.in -a \
846         \! -name \*\.[cC][xX][xX]\.in -a \
847         \! -name \*\.[cC][pP][pP]\.in -a \
848         \! -name \*\.[cC]++\.in -a \
849         \! -name \*\.m\.in -a \
850         \! -name \*\.[hH]\.in -a \
851         \! -name \*\.hh\.in \)`
852   files=`echo $files | sed 's/$am_distdir//'`
853
854   for ff in $files
855   do
856     ad_process_file $ff $ff.tmp true
857     if test -f $ff.tmp; then
858       rm -f $ff || exit 1
859       mv -f $ff.tmp $ff || exit 1
860     fi
861   done
862
863   ad_debug "Ending processing non-source files: $1"
864 }
865
866 #
867 # Processes the entire source tree for distribution.  This inspects files
868 # in the source tree, with exception of any file with .ad suffix, and
869 # performs distribution processing for the file.  The original file is
870 # replaced with the processed file.  This function is used when creating
871 # the distribution for packaging.
872 #
873 # Call this before ad_process_tree().
874 #
875 # Arguments: ad_process_source_tree <directory>
876 #
877 ad_process_source_tree()
878 {
879   ad_debug "Starting processing source files: $1"
880
881   # We take only C/C++ (and other files that are run through traditional
882   # preprocessor) files since they use the C compiler friendly version
883   # of distdefs.  Other files are not assumed to use them.
884   files=`find $am_distdir -type f \! -name \*\.ad \( \
885         -name \*\.[cC] -o \
886         -name \*\.[cC][cCpP] -o \
887         -name \*\.[cC][xX][xX] -o \
888         -name \*\.[cC][pP][pP] -o \
889         -name \*\.[cC]++ -o \
890         -name \*\.m -o \
891         -name \*\.mm -o \
892         -name \*\.M -o \
893         -name \*\.S -o \
894         -name \*\.[hH] -o \
895         -name \*\.hh -o \
896         -name \*\.[cC]\.in -o \
897         -name \*\.[cC][cCpP]\.in -o \
898         -name \*\.[cC][xX][xX]\.in -o \
899         -name \*\.[cC][pP][pP]\.in -o \
900         -name \*\.[cC]++\.in -o \
901         -name \*\.m\.in -o \
902         -name \*\.[hH]\.in -o \
903         -name \*\.hh\.in \)`
904
905   for ff in $files
906   do
907     ad_process_source_file $ff $ff.tmp true
908     if test -f $ff.tmp; then
909       rm -f $ff || exit 1
910       mv -f $ff.tmp $ff || exit 1
911     fi
912   done
913
914   ad_debug "Ending processing source files: $1"
915 }
916
917 #
918 # Removes Autodist dependencies, as they cannot be delivered to distribution.
919 #
920 # Arguments: ad_remove_dependencies <distdir>
921 #
922 ad_remove_dependencies()
923 {
924   ad_debug "Removing dependencies"
925
926   ams=`find $1 -type f -name Makefile\.\*`
927   for i in $ams
928   do
929     sed -e "/^# S_AD_ENABLE_DEPENDENCIES/,/^# E_AD_ENABLE_DEPENDENCIES/d" $i > $i.tmp
930     mv $i.tmp $i
931   done
932 }
933
934 #
935 # Makes distribution sane, ala modtimes.  Since we modify the distribution
936 # we need to make it sane after that.
937 #
938 # Arguments: ad_makedist_makesane
939 #
940 ad_makedist_makesane()
941 {
942   ad_debug "Making distribution file modtimes sane"
943
944   # DO NOT change these order unless you know what you are doing.
945   if test -f $am_distdir/configure.ac; then
946     touch $am_distdir/configure.ac
947   fi
948
949   if test -f $am_distdir/aclocal.m4; then
950     touch $am_distdir/aclocal.m4
951   fi
952
953   if test '!' -f Makefile; then
954     ad_fatal "Makefile: No such file or directory"
955   fi
956
957   configh=`grep "^CONFIG_HEADER" Makefile | cut -d= -f2 | sed 's/^[     ]*//'`
958   touch $am_distdir/$configh.in 1>/dev/null 2>/dev/null
959
960   files=`find $am_distdir -type f -name Makefile\.in`
961   for i in $files
962   do
963     touch $i
964   done
965
966   if test -f $am_distdir/configure; then
967     touch $am_distdir/configure
968   fi
969
970   if test -f $am_distdir/config.status; then
971     touch $am_distdir/config.status
972   fi
973
974   ad_debug "Distribution made sane"
975 }
976
977 #
978 # Creates distribution of the source tree.  All files in the distribution
979 # will be processed and the distribution will be packaged.
980 #
981 # Arguments: ad_makedist
982 #
983 ad_makedist()
984 {
985   ad_log_open "makedist.log"
986   ad_debug "Starting distribution creation"
987
988   ad_log "Created distribution"
989   ad_log "--------------------"
990   ad_log ""
991
992   if test '!' -f autodist.dist; then
993     ad_fatal "Autodist has not been run yet to prepare source tree"
994   fi
995
996   if test -z $MAKE; then
997     ad_fatal "The MAKE variable is not set in autodist.conf"
998   fi
999
1000   # Get distdir from Makefile
1001   if test '!' -f Makefile; then
1002     ad_fatal "The source tree is not configured, run ./configure first"
1003   fi
1004
1005   # Parse the requested distribution
1006   distribution=`grep "dist:" < autodist.dist | cut -d: -f2` || exit 1
1007   dist_version=`grep "ver:" < autodist.dist | cut -d: -f2` || exit 1
1008   am_distdir=`grep "distdir:" < autodist.dist | cut -d: -f2` || exit 1
1009   params=`grep "params:" < autodist.dist | cut -d: -f2` || exit 1
1010   ad_parse_distribution $distribution false
1011   ad_log "  Distribution: $distribution $dist_version"
1012   ad_log "  Destination directory: $am_distdir"
1013   ad_log ""
1014   ad_process_distdefs
1015
1016   if test x$opt_no_dist = xtrue; then
1017     ad_fatal "The '$distribution' distribution cannot be packaged"
1018   fi
1019
1020   # Run pre-dist-hooks
1021   ad_run_dist_hooks "$pre_dist_hooks" "$params"
1022
1023   # Create distribution directory
1024   ad_debug "Creating distribution directory $am_distdir"
1025   $MAKE distdir || exit 1
1026   chmod -R a+r $am_distdir
1027
1028   if test '!' -d $am_distdir; then
1029     ad_fatal "Distribution directory $am_distdir not created"
1030   fi
1031
1032   # Run pre-process-dist-hooks
1033   ad_run_dist_hooks "$pre_p_dist_hooks" "$params"
1034
1035   # Run excludes
1036   ad_dist_excludes "$excludes" true
1037
1038   # Run includes
1039   ad_dist_includes "$includes" false true
1040
1041   # Include specific license file if specified
1042   if test "$license" != ""; then
1043     ad_log ""
1044     ad_log "License file in distribution:"
1045     ad_log "-----------------------------"
1046     ad_log "  $license into $am_distdir/COPYING"
1047     ad_log ""
1048     cp -p $license $am_distdir/COPYING || exit 1
1049   fi
1050
1051   # Remove dependencies
1052   ad_remove_dependencies $am_distdir
1053
1054   # Process noprocesses, first pass
1055   ad_process_noprocess true
1056
1057   ad_log ""
1058   ad_log "Following files were not re-licensed:"
1059   ad_log "-------------------------------------"
1060
1061   # Process source files
1062   ad_debug "Process distribution source tree"
1063   ad_process_source_tree $am_distdir
1064
1065   # Process non-source files
1066   ad_debug "Process distribution tree"
1067   ad_process_tree $am_distdir
1068
1069   ad_log ""
1070
1071   # Process noprocesses, second pass
1072   ad_process_noprocess false
1073
1074   # Run post-process_dist-hooks
1075   ad_run_dist_hooks "$post_p_dist_hooks" "$params"
1076
1077   # Make distribution sane
1078   ad_makedist_makesane
1079
1080   # Package
1081   ad_debug "Packaging distribution"
1082   tar chof $am_distdir.tar $am_distdir || exit 1
1083
1084   # Compress
1085   ad_debug "Compressing distribution package"
1086   if test x$ad_gzip = xtrue; then
1087     ad_debug "Compressing distribution package $am_distdir.tar.gz"
1088     gzip -9 -c $am_distdir.tar > $am_distdir.tar.gz || exit 1
1089   fi
1090   if test x$ad_bzip2 = xtrue; then
1091     ad_debug "Compressing distribution package $am_distdir.tar.bz2"
1092     bzip2 -9 -c $am_distdir.tar > $am_distdir.tar.bz2 || exit 1
1093   fi
1094   if test x$ad_compress = xtrue; then
1095     ad_debug "Compressing distribution package $am_distdir.tar.Z"
1096     compress -c $am_distdir.tar > $am_distdir.tar.Z || exit 1
1097   fi
1098   if test x$ad_zip = xtrue; then
1099     rm -f $am_distdir.zip
1100     ad_debug "Compressing distribution package $am_distdir.zip"
1101     zip -rq $am_distdir.zip $am_distdir || exit 1
1102   fi
1103   rm -f $am_distdir.tar
1104
1105   # Run post-dist-hooks
1106   ad_run_dist_hooks "$post_dist_hooks" "$params"
1107
1108   # Cleanup
1109   rm -rf $am_distdir
1110
1111   ad_log "Distribution created successfully."
1112
1113   ad_debug "Ending distribution creation"
1114 }
1115
1116 #
1117 # Handles distribution options.
1118 #
1119 # option has the following format in distfile:
1120 #
1121 #   option <option>
1122 #
1123 # Following options are supported:
1124 #
1125 #   template
1126 #   no-dist
1127 #   no-inherit
1128 #
1129 # Arguments: ad_handle_options <options>
1130 #
1131 ad_handle_options()
1132 {
1133   ad_debug "Handling options: $1"
1134
1135   for i in $1
1136   do
1137     if test "$i" = "template"; then
1138       opt_template=true
1139       continue
1140     elif test "$i" = "no-dist"; then
1141       opt_no_dist=true
1142       continue
1143     elif test "$i" = "no-inherit"; then
1144       opt_no_inherit=true
1145       continue
1146     fi
1147   done
1148 }
1149
1150 #
1151 # Clears set options
1152 #
1153 # Arguments: ad_clear_options
1154 #
1155 ad_clear_options()
1156 {
1157   opt_template=false
1158   opt_no_dist=false
1159   opt_no_inherit=false
1160 }
1161
1162 #
1163 # Parses the distribution.  Gets all distribution defines from the
1164 # distribution.  This also expands all inherited distributions recursively
1165 # to get all inherited distribution defines.  From inherited distributions
1166 # their name and package name is not inherited.
1167 #
1168 # Arguments: ad_parse_distribution <distribution name> <inherit>
1169 #
1170 ad_parse_distribution()
1171 {
1172   local inhs
1173   local defs
1174   local undefs
1175   local incs
1176   local excs
1177   local nops
1178   local opts
1179   local dname
1180   local dpname
1181   local bugr
1182   local prh
1183   local poh
1184   local prdh
1185   local podh
1186   local prpdh
1187   local popdh
1188
1189   ad_debug "Starting parsing distribution: $1 $2"
1190
1191   if test '!' -f $distdir/$1; then
1192     ad_fatal "Distribution '$1' is not declared"
1193   fi
1194
1195   # Get and enforce prereq version
1196   prereq=`sed 's/^[     ]*//' < $distdir/$1 | grep -v "^#" \
1197     | grep "prereq " | cut -d' ' -f2- | sort | uniq`
1198   if test '!' -z $prereq; then
1199     if test "$ver" \< "$prereq"; then
1200       ad_fatal "Autodist $prereq or newer is required for distribution $1"
1201     fi
1202   fi
1203
1204   # Get inherited
1205   inhs=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1206     | grep "inherit " | cut -d' ' -f2 | sort | uniq`
1207
1208   # Get distdefs
1209   defs=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1210    | grep "define " | cut -d' ' -f2 | sort | uniq`
1211
1212   if test "$inhs" = "" && test "$defs" = ""; then
1213     if test "$1" != "default"; then
1214       ad_fatal "Distribution '$1' does not define anything"
1215     fi
1216   fi
1217
1218   # Get undefined distdefs
1219   undefs=`sed 's/^[     ]*//' < $distdir/$1 | grep -v "^#" \
1220    | grep "undef " | cut -d' ' -f2 | sort | uniq`
1221
1222   # Get includes
1223   incs=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1224     | grep "include " | sed 's/include / : /'`
1225
1226   # Get excludes
1227   excs=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1228     | grep "exclude " | cut -d' ' -f2- | sort | uniq`
1229
1230   # Get noprocesses
1231   nops=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1232     | grep "noprocess " | cut -d' ' -f2- | sort | uniq`
1233
1234   # Get options
1235   opts=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1236     | grep "option " | cut -d' ' -f2- | sort | uniq`
1237
1238   # Check options
1239   ad_handle_options "$opts"
1240   if test x$2 = xtrue && test x$opt_no_inherit = xtrue; then
1241     ad_fatal "Distribution '$1' cannot be inherited"
1242   fi
1243   if test x$2 = xfalse && test x$opt_template = xtrue; then
1244     ad_fatal "Template distribution '$1' cannot be prepared or packaged"
1245   fi
1246
1247   ad_debug "inherits: $inhs"
1248   ad_debug "distdefs: $defs"
1249   ad_debug "includes: $incs"
1250   ad_debug "excludes: $excs"
1251   ad_debug "noprocess: $nops"
1252   ad_debug "undistdefs: $undefs"
1253   ad_debug "options: $opts"
1254
1255   # Expand distdefs from inherited distributions
1256   for i in $inhs
1257   do
1258     if test x$1 = x$i; then
1259       ad_fatal "Infinite recursion detected.  Fix the '$distdir/$1' \
1260             distribution to not have 'inherit $i' declared."
1261     fi
1262
1263     if test '!' -f $distdir/$i; then
1264       ad_fatal "Distribution '$i' is not declared (inherited from '$1')"
1265     fi
1266
1267     ad_parse_distribution $i true
1268     ad_clear_options
1269   done
1270
1271   # Get license
1272   license=`sed 's/^[    ]*//' < $distdir/$1 | grep -v "^#" \
1273     | grep "license " | cut -d' ' -f2`
1274   licenseh=`sed 's/^[   ]*//' < $distdir/$1 | grep -v "^#" \
1275     | grep "license-header " | sed 's/license-header / : /'`
1276
1277   ad_debug "license: $license"
1278   ad_debug "licenseh: $licenseh"
1279
1280   if test x$2 = xfalse; then
1281     # Take rest of the stuff from top distribution
1282
1283     # We take precedence on defined and undefined distdefs.  Remove
1284     # undefined distdefs if we have defined them.
1285     for d in $defs
1286     do
1287       ad_debug "defining undefined $d distdef"
1288       undistdefs=`echo $undistdefs | sed s/$d//g`
1289     done
1290
1291     # Get distribution name
1292     dname=`sed 's/^[    ]*//' < $distdir/$1 | grep -v "^#" \
1293       | grep "name " | cut -d' ' -f2-`
1294
1295     if test "$dname"; then
1296       distribution=$dname
1297     fi
1298
1299     # Get distribution package name (optional)
1300     dpname=`sed 's/^[   ]*//' < $distdir/$1 | grep -v "^#" \
1301      | grep "package " | cut -d' ' -f2`
1302
1303     if test "$dpname"; then
1304       package=$dpname
1305     else
1306       package=$distribution
1307     fi
1308
1309     # Get Bug-report email address (optional)
1310     bugr=`sed 's/^[     ]*//' < $distdir/$1 | grep -v "^#" \
1311      | grep "bug-report " | cut -d' ' -f2-`
1312
1313     if test "$bugr"; then
1314       bug_report=$bugr
1315     fi
1316
1317     ad_debug "distribution: $distribution"
1318     ad_debug "package: $package"
1319     ad_debug "bug-report: $bug_report"
1320
1321     # Get hooks (optional)
1322     prh=`sed 's/^[      ]*//' < $distdir/$1 | grep -v "^#" \
1323      | grep "pre-hook " | cut -d' ' -f2-`
1324     poh=`sed 's/^[      ]*//' < $distdir/$1 | grep -v "^#" \
1325      | grep "post-hook " | cut -d' ' -f2-`
1326     prdh=`sed 's/^[     ]*//' < $distdir/$1 | grep -v "^#" \
1327      | grep "pre-dist-hook " | cut -d' ' -f2-`
1328     podh=`sed 's/^[     ]*//' < $distdir/$1 | grep -v "^#" \
1329      | grep "post-dist-hook " | cut -d' ' -f2-`
1330     prpdh=`sed 's/^[    ]*//' < $distdir/$1 | grep -v "^#" \
1331      | grep "pre-process-dist-hook " | cut -d' ' -f2-`
1332     popdh=`sed 's/^[    ]*//' < $distdir/$1 | grep -v "^#" \
1333      | grep "post-process-dist-hook " | cut -d' ' -f2-`
1334
1335     pre_hooks="$pre_hooks $prh"
1336     post_hooks="$post_hooks $poh"
1337     pre_dist_hooks="$pre_dist_hooks $prdh"
1338     post_dist_hooks="$post_dist_hooks $podh"
1339     pre_p_dist_hooks="$pre_p_dist_hooks $prpdh"
1340     post_p_dist_hooks="$post_p_dist_hooks $popdh"
1341     doptions="$doptions $opts"
1342
1343     ad_handle_options "$doptions"
1344     ad_debug "options: $doptions"
1345   fi
1346
1347   # Return to caller
1348   inherits="$inherits $inhs"
1349   distdefs="$distdefs $defs"
1350   includes="$includes $incs"
1351   excludes="$excludes $excs"
1352   noprocess="$noprocess $nops"
1353   undistdefs="$undistdefs $undefs"
1354
1355   ad_debug "Ending parsing distribution: $1 $2"
1356 }
1357
1358 #
1359 # Processes parsed distdefs.  Removes duplicates, and undefined distdefs
1360 # from the distdefs.
1361 #
1362 # Arguments: ad_process_distdefs
1363 #
1364 ad_process_distdefs()
1365 {
1366   ad_debug "Starting processing distdefs"
1367
1368   ad_log ""
1369   ad_log "Following distdefs were processed:"
1370   ad_log "----------------------------------"
1371
1372   # Remove all undefined distribution defines
1373   for i in $undistdefs
1374   do
1375     ad_debug "undefining $i distdef"
1376     distdefs=`echo $distdefs | sed s/$i//g`
1377   done
1378
1379   rm -f autodist.tmp.defs
1380
1381   # Remove duplicate distdefs
1382   for i in $distdefs
1383   do
1384     echo $i >>autodist.tmp.defs
1385   done
1386   if test -f autodist.tmp.defs; then
1387     distdefs=`sort < autodist.tmp.defs | uniq`
1388   fi
1389   distdefs=`echo $distdefs`
1390   rm -f autodist.tmp.defs
1391
1392   # Log
1393   for i in $distdefs
1394   do
1395     ad_log "  $i"
1396   done
1397   ad_log ""
1398
1399   ad_debug "distdefs=$distdefs"
1400
1401   ad_debug "Ending processing distdefs"
1402 }
1403
1404 #
1405 # Processes for a license header change.
1406 #
1407 # Arguments: ad_process_license_header <scriptfile> <sourcefile>
1408 #
1409 ad_process_license_header()
1410 {
1411   ad_debug "Starting license header processing"
1412
1413   # Add : separator at the end
1414   lics=`echo "$licenseh" | sed 's/$/ : /'`
1415
1416   src=
1417   dst=
1418   for i in $lics
1419   do
1420     if test "$i" = ":" && test -z "$src"; then
1421       continue
1422     fi
1423     if test -z "$src"; then
1424       src=$i
1425       continue
1426     fi
1427     if test -z "$dst" && test "$i" != ":"; then
1428       dst=$i
1429     else
1430       ad_fatal "Missing argument in 'license-header $src'"
1431     fi
1432
1433     ad_debug "Replacing $src license with $dst license"
1434
1435     if test '!' -f $src; then
1436       ad_fatal "License header $src: No such file or directory"
1437     fi
1438
1439     if test '!' -f $dst; then
1440       ad_fatal "License header $dst: No such file or directory"
1441     fi
1442
1443     # Awk script to replace the license header
1444     fl=`sed q $src | sed 's/\\//\\\\\//g' > autodist.lsrc` || exit 1
1445     ll=`sed -n '$p' $src | sed 's/\\//\\\\\//g' > autodist.ldst` || exit 1
1446     echo "BEGIN { N=0; } /`cat autodist.lsrc`/,/`cat autodist.ldst`/ { FILE1=\"$src\"; FILE2=\"$dst\"; getline F1 < FILE1; getline F2 < FILE2; if (F1) { N=sub(F1, F2); } else { F1=\"\$\"; N=sub(F1, F2); } if (N == 0) print 0 > \"$2.norelicense\"; } END { if (N == 0) print 0 > \"$2.norelicense\"; }" >> $1
1447     rm -f autodist.lsrc autodist.ldst
1448
1449     src=
1450     dst=
1451   done
1452
1453   ad_debug "Ending license header processing"
1454 }
1455
1456 #
1457 # Process specified noprocesses.  This is called during makedist.
1458 # The noprocess first copies the noprocess files and dirs into a temp
1459 # directory, and then removes them from the distdir.  This way they are
1460 # not processed by Autodist.  After processing they are returned to their
1461 # correct locations from the temp dir.  Dirty, yeah, but the way we do
1462 # this until better one comes along.
1463 #
1464 # Arguments: ad_process_noprocess <process>
1465 #
1466 # If <process> is true this excludes and if false this includes.
1467 #
1468 ad_process_noprocess()
1469 {
1470   ad_debug "Starting running noprocesses"
1471
1472   set -f
1473
1474   cur=`pwd`
1475
1476   if test x$1 = xtrue; then
1477     ad_log ""
1478     ad_log "Following files and directories were not processed:"
1479     ad_log "---------------------------------------------------"
1480
1481     f="$cur/autodist__noprocess"
1482     rm -rf $f
1483     mkdir -p -- $f || exit 1
1484
1485     # First, include them to new location with correct directory structure.
1486     old_am_distdir="$am_distdir"
1487     cd $am_distdir || exit 1
1488     am_distdir="$f"
1489     nops=" $noprocess"
1490     nops=`echo $nops | sed -e 's/ / : /g' -e 's/^/ : /'` || exit 1
1491     ad_dist_includes "$nops" false false
1492     am_distdir="$old_am_distdir"
1493
1494     # Then, remove from distdir (they are in temp dir now)
1495     for i in $noprocess
1496     do
1497       ad_log "  $i"
1498       rm -rf $i
1499     done
1500     cd $cur || exit 1
1501
1502     ad_log ""
1503   else
1504     # Copy from the temp dir back to distdir
1505     cd autodist__noprocess || exit 1
1506     old_am_distdir="$am_distdir"
1507     am_distdir="$cur/$old_am_distdir"
1508     nops=" $noprocess"
1509     nops=`echo $nops | sed -e 's/ / : /g' -e 's/^/ : /'` || exit 1
1510
1511     ad_dist_includes "$nops" false false
1512
1513     am_distdir="$old_am_distdir"
1514     cd $cur || exit 1
1515     rm -rf autodist__noprocess
1516   fi
1517
1518   set +f
1519
1520   ad_debug "Ending running noprocesses"
1521 }
1522
1523 #
1524 # Process a file given as argument for the distribution.
1525 #
1526 # Arguments: ad_process_file <filepath> <dest_filepath> <re-license>
1527 #
1528 ad_process_file()
1529 {
1530   local found=false
1531   local f
1532   local defs
1533   local ndefs
1534
1535   # Process only regular files
1536   if test '!' -f $1; then
1537     return
1538   fi
1539
1540   ad_debug "Starting processing file: $1 $2"
1541
1542   f="autodist.tmp.script"
1543   rm -f $f
1544
1545   # If license header is provided, replace the license header in the file.
1546   if test x$3 = xtrue; then
1547     ad_process_license_header $f $1
1548   fi
1549
1550   # Get defined distribution defines
1551   defs=`awk "/^#ifdef ${DP}_DIST_|^#else ${DP}_DIST_/ { print; }" \
1552     $1 |cut -d'*' -f2 |cut -d' ' -f2 | sort | uniq`
1553
1554   # Get explicitly not-defined distribution defines
1555   ndefs=`awk "/^#ifndef ${DP}_DIST_|^#else !${DP}_DIST_/ { print; }" \
1556     $1 |cut -d'*' -f2 |cut -d' ' -f2 | cut -d'!' -f2 | sort | uniq`
1557
1558   ad_debug "defs in $1: $defs"
1559   ad_debug "ndefs in $1: $ndefs"
1560
1561   # Create the script to include and exclude stuff in the file according
1562   # to the distribution defines
1563
1564   # ifdefs
1565   ad_debug "processing ifdefs"
1566   for d in $defs
1567   do
1568     found=false
1569     for i in $distdefs
1570     do
1571       if test x$d = x$i; then
1572         found=true
1573         break
1574       fi
1575     done
1576
1577     # If distribution define was not found exclude those lines from the file.
1578     # This also handles the #ifdef's #else (ie. #ifndef) branch.
1579     if test x$found = xfalse; then
1580       ad_debug "ifdef $d will be excluded (it is NOT defined)"
1581       echo "/^#ifdef $d$/,/^#else !$d$|^#endif $d$/ { next; }" >> $f
1582     else
1583       echo "/^#else !$d$/,/^#endif $d$/ { next; }" >> $f
1584     fi
1585   done
1586
1587   # ifndefs
1588   ad_debug "processing ifndefs"
1589   for d in $ndefs
1590   do
1591     found=false
1592     for i in $distdefs
1593     do
1594       if test x$d = x$i; then
1595         found=true
1596         break
1597       fi
1598     done
1599
1600     # If distribution define was found exclude those lines from the file.
1601     # This also handles the #ifndef's #else (ie. #ifdef) branch.
1602     if test x$found = xtrue; then
1603       ad_debug "ifndef $d will be excluded (it IS defined)"
1604       echo "/^#ifndef $d$/,/^#else $d$|^#endif $d$/ { next; }" >> $f
1605     else
1606       echo "/^#else $d$/,/^#endif $d$/ { next; }" >> $f
1607     fi
1608   done
1609
1610   # Now process the file with the script
1611   if test -f $f; then
1612
1613     # Those distdef lines that remain in the file are removed to make
1614     # the appearance prettier
1615     echo "/^#ifdef "$DP"_DIST_|^#endif "$DP"_DIST_|^#else "$DP"_DIST_|^#else !"$DP"_DIST_|^#ifndef "$DP"_DIST_/ { next; }" >> $f
1616     echo "{ print; }" >> $f
1617
1618     # Execute the script
1619     cp -p $1 $2 || exit 1
1620     awk -f $f $1 > $2 || exit 1
1621   fi
1622
1623   rm -f $f
1624
1625   # Log if file was not relicensed
1626   if test -f "$1.norelicense"; then
1627     ad_log "  $1"
1628     rm -f $1.norelicense
1629   fi
1630
1631   ad_debug "Ending processing file: $1 $2"
1632 }
1633
1634 #
1635 # Process a source file given as argument for the distribution.
1636 #
1637 # Arguments: ad_process_source_file <filepath> <dest_filepath> <re-license>
1638 #
1639 ad_process_source_file()
1640 {
1641   local found=false
1642   local f
1643   local defs
1644   local ndefs
1645
1646   # Process only regular files
1647   if test '!' -f $1; then
1648     return
1649   fi
1650
1651   ad_debug "Starting processing source file: $1 $2"
1652
1653   f="autodist.tmp.script"
1654   rm -f $f
1655
1656   # If license header is provided, replace the license header in the file.
1657   if test x$3 = xtrue; then
1658     ad_process_license_header $f $1
1659   fi
1660
1661   # Get defined distribution defines
1662   defs=`awk "/^#ifdef ${DP}_DIST_|^#else \/\* ${DP}_DIST_/ { print; }" \
1663     $1 |cut -d'*' -f2 |cut -d' ' -f2 | sort | uniq`
1664
1665   # Get explicitly not-defined distribution defines
1666   ndefs=`awk "/^#ifndef ${DP}_DIST_|^#else \/\* \!${DP}_DIST_/ { print; }" \
1667     $1 |cut -d'*' -f2 |cut -d' ' -f2 | cut -d'!' -f2 | sort | uniq`
1668
1669   ad_debug "defs in $1: $defs"
1670   ad_debug "ndefs in $1: $ndefs"
1671
1672   # Create the script to include and exclude stuff in the file according
1673   # to the distribution defines
1674
1675   # ifdefs
1676   ad_debug "processing ifdefs"
1677   for d in $defs
1678   do
1679     found=false
1680     for i in $distdefs
1681     do
1682       if test x$d = x$i; then
1683         found=true
1684         break
1685       fi
1686     done
1687
1688     # If distribution define was not found exclude those lines from the file.
1689     # This also handles the #ifdef's #else (ie. #ifndef) branch.
1690     if test x$found = xfalse; then
1691       ad_debug "ifdef $d will be excluded (it is NOT defined)"
1692       echo "/^#ifdef $d$/,/^#else \/\* \!$d |^#endif \/\* $d / { next; }" >> $f
1693     else
1694       echo "/^#else \/\* \!$d /,/^#endif \/\* $d / { next; }" >> $f
1695     fi
1696   done
1697
1698   # ifndefs
1699   ad_debug "processing ifndefs"
1700   for d in $ndefs
1701   do
1702     found=false
1703     for i in $distdefs
1704     do
1705       if test x$d = x$i; then
1706         found=true
1707         break
1708       fi
1709     done
1710
1711     # If distribution define was found exclude those lines from the file.
1712     # This also handles the #ifndef's #else (ie. #ifdef) branch.
1713     if test x$found = xtrue; then
1714       ad_debug "ifndef $d will be excluded (it IS defined)"
1715       echo "/^#ifndef $d$/,/^#else \/\* $d |^#endif \/\* $d / { next; }" >> $f
1716     else
1717       echo "/^#else \/\* $d /,/^#endif \/\* $d / { next; }" >> $f
1718     fi
1719   done
1720
1721   # Now process the file with the script
1722   if test -f $f; then
1723
1724     # Those distdef lines that remain in the file are removed to make
1725     # the appearance prettier
1726     echo "/^#ifdef "$DP"_DIST_|^#endif \/\* "$DP"_DIST_|^#else \/\* "$DP"_DIST_|^#else \/\* \!"$DP"_DIST_|^#ifndef "$DP"_DIST_/ { next; }" >> $f
1727     echo "{ print; }" >> $f
1728
1729     # Execute the script
1730     cp -p $1 $2 || exit 1
1731     awk -f $f $1 > $2 || exit 1
1732   fi
1733
1734   rm -f $f
1735
1736   # Log if file was not relicensed
1737   if test -f "$1.norelicense"; then
1738     ad_log "  $1"
1739     rm -f $1.norelicense
1740   fi
1741
1742   ad_debug "Ending processing source file: $1 $2"
1743 }
1744
1745 #
1746 # Processes a file.  This is the -p, --process option.
1747 #
1748 # Arguments: ad_process <type> <src> <dst>
1749 #
1750 ad_process()
1751 {
1752   ad_debug "Starting process: $1 $2 $3"
1753
1754   nolog=true
1755
1756   if test '!' -f autodist.dist; then
1757     ad_fatal "Autodist has not been run yet to prepare source tree"
1758   fi
1759
1760   # Parse distribution
1761   distribution=`grep "dist:" < autodist.dist | cut -d: -f2` || exit 1
1762   dist_version=`grep "ver:" < autodist.dist | cut -d: -f2` || exit 1
1763   ad_parse_distribution $distribution false
1764   ad_process_distdefs
1765
1766   # Process file
1767   case "$1" in
1768     makefile)
1769       if test -z $3; then
1770         ad_fatal "File type $1 requires <dst> argument"
1771       fi
1772       ad_make_makefile_am $2 $3
1773       exit 0;;
1774
1775     configure)
1776       ad_make_configure_ac $2
1777       exit 0;;
1778
1779     non-source)
1780       if test -z $3; then
1781         ad_fatal "File type $1 requires <dst> argument"
1782       fi
1783       ad_process_file $2 $3 false
1784       exit 0;;
1785
1786     source)
1787       if test -z $3; then
1788         ad_fatal "File type $1 requires <dst> argument"
1789       fi
1790       ad_process_source_file $2 $3 false
1791       exit 0;;
1792
1793     *)
1794       ad_fatal "Unknown file type: $1";
1795       ;;
1796   esac
1797 }
1798
1799 #
1800 # Run hooks
1801 #
1802 # Arguments: ad_run_hooks <hooks> <params>
1803 #
1804 ad_run_hooks()
1805 {
1806   ad_debug "Running hooks: $1"
1807
1808   ad_log ""
1809   ad_log "Hooks executed:"
1810   ad_log "--------------"
1811
1812   for i in $1
1813   do
1814     if test '!' -f $i; then
1815       ad_fatal "Hook script $i does not exist"
1816     fi
1817     ad_log "  sh $i \"$distribution\" \"$dist_version\" \"$package\" \"$2\""
1818     sh $i "$distribution" "$dist_version" "$package" "$2" || exit 1
1819   done
1820   ad_log ""
1821
1822   ad_debug "Ending running hooks: $1"
1823 }
1824
1825 #
1826 # Run dist hooks
1827 #
1828 # Arguments: ad_run_dist_hooks <hooks> <params>
1829 #
1830 ad_run_dist_hooks()
1831 {
1832   ad_debug "Starting running distributions hooks: $1"
1833
1834   ad_log ""
1835   ad_log "Distribution hooks executed:"
1836   ad_log "----------------------------"
1837
1838   for i in $1
1839   do
1840     if test '!' -f $i; then
1841       ad_fatal "Dist hook script $i does not exist"
1842     fi
1843
1844     ad_log "  sh $i \"$distribution\" \"$dist_version\" \"$package\" \"$am_distdir\" \"$2\""
1845     sh $i "$distribution" "$dist_version" "$package" "$am_distdir" "$2" || exit 1
1846   done
1847   ad_log ""
1848
1849   ad_debug "Ending running distribution hooks"
1850 }
1851
1852 ###############################################################################
1853 # Autodist code
1854
1855 usage="Usage: autodist [OPTIONS] [DISTRIBUTION] [VERSION] [PARAMS]"
1856 help="\
1857 Autodist prepares source tree for configuration, compilation and
1858 distribution.  Generates Automake.am files from Automake.ad files,
1859 configure.ac file from configure.ad file(s), generates the configure
1860 script by running Autoconf tool, and generates Makefile.in files by
1861 running Automake tool.
1862
1863 Operation modes:
1864   -h, --help                print this help, then exit
1865   -V, --version             print version number, then exit
1866   -v, --verbose             verbosely report processing
1867   -d, --distdir <dir>       search distributions from <dir>
1868   -s, --distdefs [<dist>]   print distribution defines of <dist>, then exit
1869   -i, --init                initialize Autodist environment, create default
1870                             distribution directory and distribution, then exit
1871   -p, --process <type> <src> [<dst>]
1872                             process file <src> into <dst> for distribution,
1873                             <type> is 'makefile', 'configure', 'non-source'
1874                             or 'source' and defines the type of <src>
1875   -m, --makedist            create and package distribution
1876       --gzip                create package compressed with gzip (default)
1877       --bzip2               create also package compressed with bzip2
1878       --compress            create also package compressed with compress
1879       --zip                 create also package compressed with zip"
1880
1881 #
1882 # Process command line arguments
1883 #
1884 while test $# -gt 0; do
1885   case "${1}" in
1886
1887   -d |--distdir)
1888     shift;
1889     test $# -eq 0 && { echo "${usage}" 1>&2; exit 1; }
1890     distdir="${1}";
1891     shift;;
1892
1893   --list)
1894     exit 0;;
1895
1896   -s | --distdefs)
1897     shift;
1898     if test $# -eq 0; then
1899       ad_parse_distribution $distribution false
1900       echo "Distribution: ${distribution}" 1>&2;
1901     else
1902       ad_parse_distribution $1 false
1903       echo "Distribution: ${1}" 1>&2;
1904     fi
1905     ad_process_distdefs
1906     echo "Distdefs:" 1>&2;
1907     for i in $distdefs
1908     do
1909       echo "$i";
1910     done
1911     exit 0;;
1912
1913   -i | --init)
1914     ad_initialize;
1915     exit 0;;
1916
1917   -p | --process)
1918     shift;
1919     if test $# -ge 2; then
1920       ad_process $1 $2 $3
1921     else
1922       echo "${usage}" 1>&2;
1923     fi
1924     exit 0;;
1925
1926   -m | --makedist)
1927     ad_makedist
1928     exit 0;;
1929
1930   --gzip)
1931     ad_gzip=true
1932     shift;;
1933
1934   --bzip2)
1935     ad_bzip2=true
1936     shift;;
1937
1938   --compress)
1939     ad_compress=true
1940     shift;;
1941
1942   --zip)
1943     ad_zip=true
1944     shift;;
1945
1946   -v | --verbose)
1947     debug=true
1948     shift;;
1949
1950   -h | --help | --h*)
1951     echo "${usage}" 1>&2;
1952     echo 1>&2;
1953     echo "${help}" 1>&2;
1954     echo 1>&2;
1955     exit 0;;
1956
1957   -V | --version)
1958     echo "@PACKAGE@ (@PACKAGE_NAME@) $ver" 1>&2;
1959     echo "Written by Pekka Riikonen" 1>&2;
1960     echo 1>&2;
1961     echo "Copyright (C) 2004 - 2007 SILC Project" 1>&2;
1962     echo "\
1963 This is free software; see the source for copying conditions.  There is NO
1964 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. " 1>&2;
1965     exit 0;;
1966
1967   --)
1968     shift;
1969     break;;
1970
1971   -*)
1972     echo "${usage}" 1>&2;
1973     exit 1;;
1974
1975   *)
1976     break;;
1977
1978   esac
1979 done
1980
1981 # Open log file
1982 ad_log_open "autodist.log"
1983
1984 ad_log "Processing source tree for compilation and configuration"
1985 ad_log "--------------------------------------------------------"
1986 ad_log ""
1987
1988 #
1989 # Parse the requested distribution
1990 #
1991 if test $# != 0; then
1992   distribution="${1}";
1993   distfile=$distribution
1994   shift
1995 fi
1996
1997 ad_parse_distribution $distribution false
1998
1999 if test $# != 0; then
2000   dist_version="${1}";
2001 fi
2002
2003 ad_log "  Distribution: $distribution $dist_version"
2004 ad_log ""
2005
2006 ad_process_distdefs
2007
2008 ad_debug "Preparing source tree for configuration and compilation..."
2009 ad_debug "Preparing $distribution distribution version $dist_version"
2010
2011 #
2012 # Create the distribution defines header file
2013 #
2014 if test "$DISTDEFS"; then
2015   ad_create_distdefs_h
2016 fi
2017
2018 # Get extra parameters from command line
2019 if test $# != 0; then
2020   shift
2021 fi
2022
2023 #
2024 # Run pre-hooks
2025 #
2026 ad_run_hooks "$pre_hooks" "$@"
2027
2028 #
2029 # Generate the Makefile.am files from Makefile.ad files
2030 #
2031 ad_make_makefile_ams
2032
2033 #
2034 # Generate the configure.ac from configure.ad file(s)
2035 #
2036 ad_make_configure_ac ./configure.ad
2037
2038 #
2039 # Process all files with .ad suffix for distribution processing
2040 #
2041 ad_process_ads
2042
2043 #
2044 # Generate configure script
2045 #
2046 ad_make_configure
2047
2048 #
2049 # Generate Makefile.in files
2050 #
2051 ad_make_makefile_ins
2052
2053 #
2054 # Create autodist.dist
2055 #
2056 ad_debug "Creating autodist.dist"
2057 echo "dist:$distfile" > autodist.dist
2058 echo "ver:$dist_version" >> autodist.dist
2059 echo "distdir:$package-$dist_version" >> autodist.dist
2060 echo "params:$@" >> autodist.dist
2061
2062 #
2063 # Run post-hooks
2064 #
2065 ad_run_hooks "$post_hooks" "$@"
2066
2067 ad_log "Source tree processed successfully."
2068 ad_debug "Done, now run ./configure and make."
2069
2070 exit 0