Made Autodist work without the autodist.conf file.
[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     # Run the distribution processing for this file
574     ad_process_file $i $fname false
575
576     if test '!' -f "$fname"; then
577       cp -p $orig $fname || exit 1
578     fi
579   done
580   ad_log ""
581
582   ad_debug "Ending processing .ad files"
583 }
584
585 #
586 # Includes files specified in the distribution for inclusion.  Used when
587 # creating the distribution for packaging.
588 #
589 # include has the following format in distfile:
590 #
591 #   include <path> [<dest path>]
592 #
593 # If only source path, which may be file, directory or regular expression,
594 # is specified the path will be same in distribution.  If the destination
595 # path is specified that will be the new name and/or new location of the
596 # source path.  This, in effect, is a cp utility with ability to create
597 # directories if they do not exist.
598 #
599 # Arguments: ad_dist_includes <includeslist> <recursive> <log>
600 #
601 ad_dist_includes()
602 {
603   local incs
604
605   ad_debug "Starting running includes: $1 $2"
606
607   if test x$3 = xtrue; then
608     ad_log ""
609     ad_log "Following files and directories were included in distribution:"
610     ad_log "--------------------------------------------------------------"
611   fi
612
613   # By default do not expand pathnames
614   set -f
615
616   # Add : separator at the end
617   incs="`echo "$1" | sed 's/$/ : /'`"
618
619   src=
620   dst=
621   for i in $incs
622   do
623     if test "$i" = ":" && test -z "$src"; then
624       continue
625     fi
626     if test -z "$src"; then
627       src=$i
628       continue
629     fi
630     if test -z "$dst" && test "$i" != ":"; then
631       dst=$i
632     else
633       dst=$src
634     fi
635
636     ad_debug "Including $src into $dst"
637
638     if test -f "$src"; then
639       # Add file
640
641       if test "$src" = "$dst"; then
642         # Add to same location
643         d=`echo $src | sed 's,/[^/]*$,,'`
644         if test "$d" != "$src" && test "$d" != "." && \
645            test '!' -d $am_distdir/$d; then
646           mkdir -p -- $am_distdir/$d || exit 1
647         fi
648       else
649         # Add to different location
650         check=`echo "$dst" | sed 's/?//; s/*//; s/\[//; s/\]//'`
651         if test "$check" != "$dst"; then
652           ad_fatal "Invalid destination in 'include $src $dst'"
653         fi
654
655         d=`echo $dst | sed 's,/[^/]*$,,'`
656         if test "$d" != "$dst" && test "$d" != "." && \
657            test '!' -d $am_distdir/$d; then
658           mkdir -p -- $am_distdir/$d || exit 1
659         fi
660       fi
661
662       if test x$3 = xtrue; then
663         ad_log "  $src into $am_distdir/$d"
664       fi
665       cp -p $src $am_distdir/$d || exit 1
666
667     elif test -d "$src"; then
668       # Add directory
669
670       if test "$src" = "$dst"; then
671         # Add to same location
672         d=`echo $src | sed 's,/[^/]*$,,'`
673         ds=`echo $src | sed 's/\/$//'`
674         if test "$ds" = "$d"; then
675           d=`echo $d | sed 's,/[^/]*$,,'`
676         fi
677         if test "$ds" = "$d"; then
678           d=""
679         fi
680         if test '!' -d $am_distdir/$d && test "$ds" != "$d"; then
681           mkdir -p -- $am_distdir/$d || exit 1
682         fi
683
684         if test x$3 = xtrue; then
685           ad_log "  $src into $am_distdir/$d"
686         fi
687         cp -pR $src $am_distdir/$d || exit 1
688       else
689         # Add to different location
690         check=`echo "$dst" | sed 's/?//; s/*//; s/\[//; s/\]//'`
691         if test "$check" != "$dst"; then
692           ad_fatal "Invalid destination in 'include $src $dst'"
693         fi
694
695         d=`echo $dst | sed 's,/[^/]*$,,'`
696         ds=`echo $dst | sed 's/\/$//'`
697         if test "$ds" = "$d"; then
698           d=`echo $d | sed 's,/[^/]*$,,'`
699         fi
700         if test '!' -d $am_distdir/$d && test "$dst" != "$d"; then
701           mkdir -p -- $am_distdir/$d || exit 1
702         fi
703
704         if test x$3 = xtrue; then
705           ad_log "  $src into $am_distdir/$dst"
706         fi
707         cp -pR $src $am_distdir/$dst || exit 1
708       fi
709
710     elif test x$2 != xtrue; then
711       # We assume regular expression in filename
712       check=`echo "$src" | sed 's/?//; s/*//; s/\[//; s/\]//'`
713       if test "$check" == "$src"; then
714         if test '!' -a $src; then
715           ad_fatal "Including $src: No such file or directory"
716         fi
717         src=
718         dst=
719         continue
720       fi
721
722       # Recursively call this function with expanded pathnames.  The
723       # reason why we don't let sh by default expand pathnames is that
724       # the include's destination is optional.  If sh expands by default
725       # we don't know the destination.  For this reason, we handle the
726       # expansion here ourselves.
727
728       # If src and dst are same, then expand the pathname as we'll copy
729       # matches to their own locations.
730       if test "$src" = "$dst"; then
731         # Expand pathnames, and format to our include format
732         set +f
733         srcs=`echo $src | sed -e 's/ / : /g' -e 's/^/ : /'` || exit 1
734         set -f
735       else
736         # Destination is new, and it has to be a directory.
737         check=`echo "$dst" | sed 's/?//; s/*//; s/\[//; s/\]//'`
738         if test "$check" != "$dst"; then
739           ad_fatal "Invalid destination in 'include $src $dst'"
740         fi
741
742         # Make sure dst has / at the end, as this must be a directory
743         dst=`echo $dst | sed 's/\/$//; s/$/\//'`
744
745         # Escape dst for sed
746         dste=`echo $dst | sed 's/\\//\\\\\//g'` || exit 1
747
748         # Expand pathnames, and format to our include format
749         set +f
750         srcs=`echo $src | sed -e "s/ / $dste : /g" \
751           -e 's/^/ : /' -e "s/$/ $dste/"` || exit 1
752         set -f
753       fi
754
755       # Include recursively
756       ad_dist_includes "$srcs" true
757
758     elif test '!' -a $src; then
759       ad_fatal "Including $src: No such file or directory"
760     fi
761
762     src=
763     dst=
764   done
765
766   if test x$3 = xtrue; then
767     ad_log ""
768   fi
769
770   set +f
771
772   ad_debug "Ending running includes: $1 $2"
773 }
774
775 #
776 # Excludes files specified in the distribution for exclusion.  Used when
777 # creating the distribution for packaging.
778 #
779 # exclude has the following format in distfile:
780 #
781 #  exclude <path>
782 #
783 # The path may be file, directory or regular expression.
784 #
785 # Arguments: ad_dist_includes <excludelist> <log>
786 #
787 ad_dist_excludes()
788 {
789   ad_debug "Starting running excludes: $1"
790
791   if test x$2 = xtrue; then
792     ad_log ""
793     ad_log "Following files and directories were excluded from distribution:"
794     ad_log "----------------------------------------------------------------"
795   fi
796
797   cur=`pwd`
798   cd $am_distdir || exit 1
799   for i in $1
800   do
801     ad_debug "Excluding $i"
802     if test x$2 = xtrue; then
803       ad_log "  $i"
804     fi
805     rm -rf $i
806   done
807   cd $cur || exit 1
808
809   if test x$2 = xtrue; then
810     ad_log ""
811   fi
812
813   ad_debug "Ending running excludes: $1"
814 }
815
816 #
817 # Processes the entire tree for distribution.  This inspects files other
818 # than source and header files, with exception of any file with .ad
819 # suffix, and performs distribution processing for the file.  The original
820 # file is replaced with the processed file.  This function is used when
821 # creating the distribution for packaging.
822 #
823 # Arguments: ad_process_tree <directory>
824 #
825 ad_process_tree()
826 {
827   ad_debug "Starting processing non-source files: $1"
828
829   # Take files, except source and .ad files.
830   files=`find $am_distdir -type f \! -name \*\.ad \( \
831         \! -name \*\.[cC] -a \
832         \! -name \*\.[cC][cCpP] -a \
833         \! -name \*\.[cC][xX][xX] -a \
834         \! -name \*\.[cC][pP][pP] -a \
835         \! -name \*\.[cC]++ -a \
836         \! -name \*\.m -a \
837         \! -name \*\.mm -o \
838         \! -name \*\.M -o \
839         \! -name \*\.S -o \
840         \! -name \*\.[hH] -a \
841         \! -name \*\.hh -a \
842         \! -name \*\.[cC]\.in -a \
843         \! -name \*\.[cC][cCpP]\.in -a \
844         \! -name \*\.[cC][xX][xX]\.in -a \
845         \! -name \*\.[cC][pP][pP]\.in -a \
846         \! -name \*\.[cC]++\.in -a \
847         \! -name \*\.m\.in -a \
848         \! -name \*\.[hH]\.in -a \
849         \! -name \*\.hh\.in \)`
850   files=`echo $files | sed 's/$am_distdir//'`
851
852   for ff in $files
853   do
854     ad_process_file $ff $ff.tmp true
855     if test -f $ff.tmp; then
856       rm -f $ff || exit 1
857       mv -f $ff.tmp $ff || exit 1
858     fi
859   done
860
861   ad_debug "Ending processing non-source files: $1"
862 }
863
864 #
865 # Processes the entire source tree for distribution.  This inspects files
866 # in the source tree, with exception of any file with .ad suffix, and
867 # performs distribution processing for the file.  The original file is
868 # replaced with the processed file.  This function is used when creating
869 # the distribution for packaging.
870 #
871 # Call this before ad_process_tree().
872 #
873 # Arguments: ad_process_source_tree <directory>
874 #
875 ad_process_source_tree()
876 {
877   ad_debug "Starting processing source files: $1"
878
879   # We take only C/C++ (and other files that are run through traditional
880   # preprocessor) files since they use the C compiler friendly version
881   # of distdefs.  Other files are not assumed to use them.
882   files=`find $am_distdir -type f \! -name \*\.ad \( \
883         -name \*\.[cC] -o \
884         -name \*\.[cC][cCpP] -o \
885         -name \*\.[cC][xX][xX] -o \
886         -name \*\.[cC][pP][pP] -o \
887         -name \*\.[cC]++ -o \
888         -name \*\.m -o \
889         -name \*\.mm -o \
890         -name \*\.M -o \
891         -name \*\.S -o \
892         -name \*\.[hH] -o \
893         -name \*\.hh -o \
894         -name \*\.[cC]\.in -o \
895         -name \*\.[cC][cCpP]\.in -o \
896         -name \*\.[cC][xX][xX]\.in -o \
897         -name \*\.[cC][pP][pP]\.in -o \
898         -name \*\.[cC]++\.in -o \
899         -name \*\.m\.in -o \
900         -name \*\.[hH]\.in -o \
901         -name \*\.hh\.in \)`
902
903   for ff in $files
904   do
905     ad_process_source_file $ff $ff.tmp true
906     if test -f $ff.tmp; then
907       rm -f $ff || exit 1
908       mv -f $ff.tmp $ff || exit 1
909     fi
910   done
911
912   ad_debug "Ending processing source files: $1"
913 }
914
915 #
916 # Removes Autodist dependencies, as they cannot be delivered to distribution.
917 #
918 # Arguments: ad_remove_dependencies <distdir>
919 #
920 ad_remove_dependencies()
921 {
922   ad_debug "Removing dependencies"
923
924   ams=`find $1 -type f -name Makefile\.\*`
925   for i in $ams
926   do
927     sed -e "/^# S_AD_ENABLE_DEPENDENCIES/,/^# E_AD_ENABLE_DEPENDENCIES/d" $i > $i.tmp
928     mv $i.tmp $i
929   done
930 }
931
932 #
933 # Makes distribution sane, ala modtimes.  Since we modify the distribution
934 # we need to make it sane after that.
935 #
936 # Arguments: ad_makedist_makesane
937 #
938 ad_makedist_makesane()
939 {
940   ad_debug "Making distribution file modtimes sane"
941
942   # DO NOT change these order unless you know what you are doing.
943   if test -f $am_distdir/configure.ac; then
944     touch $am_distdir/configure.ac
945   fi
946
947   if test -f $am_distdir/aclocal.m4; then
948     touch $am_distdir/aclocal.m4
949   fi
950
951   if test '!' -f Makefile; then
952     ad_fatal "Makefile: No such file or directory"
953   fi
954
955   configh=`grep "^CONFIG_HEADER" Makefile | cut -d= -f2 | sed 's/^[     ]*//'`
956   touch $am_distdir/$configh.in 1>/dev/null 2>/dev/null
957
958   files=`find $am_distdir -type f -name Makefile\.in`
959   for i in $files
960   do
961     touch $i
962   done
963
964   if test -f $am_distdir/configure; then
965     touch $am_distdir/configure
966   fi
967
968   if test -f $am_distdir/config.status; then
969     touch $am_distdir/config.status
970   fi
971
972   ad_debug "Distribution made sane"
973 }
974
975 #
976 # Creates distribution of the source tree.  All files in the distribution
977 # will be processed and the distribution will be packaged.
978 #
979 # Arguments: ad_makedist
980 #
981 ad_makedist()
982 {
983   ad_log_open "makedist.log"
984   ad_debug "Starting distribution creation"
985
986   ad_log "Created distribution"
987   ad_log "--------------------"
988   ad_log ""
989
990   if test '!' -f autodist.dist; then
991     ad_fatal "Autodist has not been run yet to prepare source tree"
992   fi
993
994   if test -z $MAKE; then
995     ad_fatal "The MAKE variable is not set in autodist.conf"
996   fi
997
998   # Get distdir from Makefile
999   if test '!' -f Makefile; then
1000     ad_fatal "The source tree is not configured, run ./configure first"
1001   fi
1002
1003   # Parse the requested distribution
1004   distribution=`grep "dist:" < autodist.dist | cut -d: -f2` || exit 1
1005   dist_version=`grep "ver:" < autodist.dist | cut -d: -f2` || exit 1
1006   am_distdir=`grep "distdir:" < autodist.dist | cut -d: -f2` || exit 1
1007   params=`grep "params:" < autodist.dist | cut -d: -f2` || exit 1
1008   ad_parse_distribution $distribution false
1009   ad_log "  Distribution: $distribution $dist_version"
1010   ad_log "  Destination directory: $am_distdir"
1011   ad_log ""
1012   ad_process_distdefs
1013
1014   if test x$opt_no_dist = xtrue; then
1015     ad_fatal "The '$distribution' distribution cannot be packaged"
1016   fi
1017
1018   # Run pre-dist-hooks
1019   ad_run_dist_hooks "$pre_dist_hooks" "$params"
1020
1021   # Create distribution directory
1022   ad_debug "Creating distribution directory $am_distdir"
1023   $MAKE distdir || exit 1
1024   chmod -R a+r $am_distdir
1025
1026   if test '!' -d $am_distdir; then
1027     ad_fatal "Distribution directory $am_distdir not created"
1028   fi
1029
1030   # Run pre-process-dist-hooks
1031   ad_run_dist_hooks "$pre_p_dist_hooks" "$params"
1032
1033   # Run excludes
1034   ad_dist_excludes "$excludes" true
1035
1036   # Run includes
1037   ad_dist_includes "$includes" false true
1038
1039   # Include specific license file if specified
1040   if test "$license" != ""; then
1041     ad_log ""
1042     ad_log "License file in distribution:"
1043     ad_log "-----------------------------"
1044     ad_log "  $license into $am_distdir/COPYING"
1045     ad_log ""
1046     cp -p $license $am_distdir/COPYING || exit 1
1047   fi
1048
1049   # Remove dependencies
1050   ad_remove_dependencies $am_distdir
1051
1052   # Process noprocesses, first pass
1053   ad_process_noprocess true
1054
1055   ad_log ""
1056   ad_log "Following files were not re-licensed:"
1057   ad_log "-------------------------------------"
1058
1059   # Process source files
1060   ad_debug "Process distribution source tree"
1061   ad_process_source_tree $am_distdir
1062
1063   # Process non-source files
1064   ad_debug "Process distribution tree"
1065   ad_process_tree $am_distdir
1066
1067   ad_log ""
1068
1069   # Process noprocesses, second pass
1070   ad_process_noprocess false
1071
1072   # Run post-process_dist-hooks
1073   ad_run_dist_hooks "$post_p_dist_hooks" "$params"
1074
1075   # Make distribution sane
1076   ad_makedist_makesane
1077
1078   # Package
1079   ad_debug "Packaging distribution"
1080   tar chof $am_distdir.tar $am_distdir || exit 1
1081
1082   # Compress
1083   ad_debug "Compressing distribution package"
1084   if test x$ad_gzip = xtrue; then
1085     ad_debug "Compressing distribution package $am_distdir.tar.gz"
1086     gzip -9 -c $am_distdir.tar > $am_distdir.tar.gz || exit 1
1087   fi
1088   if test x$ad_bzip2 = xtrue; then
1089     ad_debug "Compressing distribution package $am_distdir.tar.bz2"
1090     bzip2 -9 -c $am_distdir.tar > $am_distdir.tar.bz2 || exit 1
1091   fi
1092   if test x$ad_compress = xtrue; then
1093     ad_debug "Compressing distribution package $am_distdir.tar.Z"
1094     compress -c $am_distdir.tar > $am_distdir.tar.Z || exit 1
1095   fi
1096   if test x$ad_zip = xtrue; then
1097     rm -f $am_distdir.zip
1098     ad_debug "Compressing distribution package $am_distdir.zip"
1099     zip -rq $am_distdir.zip $am_distdir || exit 1
1100   fi
1101   rm -f $am_distdir.tar
1102
1103   # Run post-dist-hooks
1104   ad_run_dist_hooks "$post_dist_hooks" "$params"
1105
1106   # Cleanup
1107   rm -rf $am_distdir
1108
1109   ad_log "Distribution created successfully."
1110
1111   ad_debug "Ending distribution creation"
1112 }
1113
1114 #
1115 # Handles distribution options.
1116 #
1117 # option has the following format in distfile:
1118 #
1119 #   option <option>
1120 #
1121 # Following options are supported:
1122 #
1123 #   template
1124 #   no-dist
1125 #   no-inherit
1126 #
1127 # Arguments: ad_handle_options <options>
1128 #
1129 ad_handle_options()
1130 {
1131   ad_debug "Handling options: $1"
1132
1133   for i in $1
1134   do
1135     if test "$i" = "template"; then
1136       opt_template=true
1137       continue
1138     elif test "$i" = "no-dist"; then
1139       opt_no_dist=true
1140       continue
1141     elif test "$i" = "no-inherit"; then
1142       opt_no_inherit=true
1143       continue
1144     fi
1145   done
1146 }
1147
1148 #
1149 # Clears set options
1150 #
1151 # Arguments: ad_clear_options
1152 #
1153 ad_clear_options()
1154 {
1155   opt_template=false
1156   opt_no_dist=false
1157   opt_no_inherit=false
1158 }
1159
1160 #
1161 # Parses the distribution.  Gets all distribution defines from the
1162 # distribution.  This also expands all inherited distributions recursively
1163 # to get all inherited distribution defines.  From inherited distributions
1164 # their name and package name is not inherited.
1165 #
1166 # Arguments: ad_parse_distribution <distribution name> <inherit>
1167 #
1168 ad_parse_distribution()
1169 {
1170   local inhs
1171   local defs
1172   local undefs
1173   local incs
1174   local excs
1175   local nops
1176   local opts
1177   local dname
1178   local dpname
1179   local bugr
1180   local prh
1181   local poh
1182   local prdh
1183   local podh
1184   local prpdh
1185   local popdh
1186
1187   ad_debug "Starting parsing distribution: $1 $2"
1188
1189   if test '!' -f $distdir/$1; then
1190     ad_fatal "Distribution '$1' is not declared"
1191   fi
1192
1193   # Get and enforce prereq version
1194   prereq=`sed 's/^[     ]*//' < $distdir/$1 | grep -v "^#" \
1195     | grep "prereq " | cut -d' ' -f2- | sort | uniq`
1196   if test '!' -z $prereq; then
1197     if test "$ver" \< "$prereq"; then
1198       ad_fatal "Autodist $prereq or newer is required for distribution $1"
1199     fi
1200   fi
1201
1202   # Get inherited
1203   inhs=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1204     | grep "inherit " | cut -d' ' -f2 | sort | uniq`
1205
1206   # Get distdefs
1207   defs=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1208    | grep "define " | cut -d' ' -f2 | sort | uniq`
1209
1210   if test "$inhs" = "" && test "$defs" = ""; then
1211     if test "$1" != "default"; then
1212       ad_fatal "Distribution '$1' does not define anything"
1213     fi
1214   fi
1215
1216   # Get undefined distdefs
1217   undefs=`sed 's/^[     ]*//' < $distdir/$1 | grep -v "^#" \
1218    | grep "undef " | cut -d' ' -f2 | sort | uniq`
1219
1220   # Get includes
1221   incs=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1222     | grep "include " | sed 's/include / : /'`
1223
1224   # Get excludes
1225   excs=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1226     | grep "exclude " | cut -d' ' -f2- | sort | uniq`
1227
1228   # Get noprocesses
1229   nops=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1230     | grep "noprocess " | cut -d' ' -f2- | sort | uniq`
1231
1232   # Get options
1233   opts=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1234     | grep "option " | cut -d' ' -f2- | sort | uniq`
1235
1236   # Check options
1237   ad_handle_options "$opts"
1238   if test x$2 = xtrue && test x$opt_no_inherit = xtrue; then
1239     ad_fatal "Distribution '$1' cannot be inherited"
1240   fi
1241   if test x$2 = xfalse && test x$opt_template = xtrue; then
1242     ad_fatal "Template distribution '$1' cannot be prepared or packaged"
1243   fi
1244
1245   ad_debug "inherits: $inhs"
1246   ad_debug "distdefs: $defs"
1247   ad_debug "includes: $incs"
1248   ad_debug "excludes: $excs"
1249   ad_debug "noprocess: $nops"
1250   ad_debug "undistdefs: $undefs"
1251   ad_debug "options: $opts"
1252
1253   # Expand distdefs from inherited distributions
1254   for i in $inhs
1255   do
1256     if test x$1 = x$i; then
1257       ad_fatal "Infinite recursion detected.  Fix the '$distdir/$1' \
1258             distribution to not have 'inherit $i' declared."
1259     fi
1260
1261     if test '!' -f $distdir/$i; then
1262       ad_fatal "Distribution '$i' is not declared (inherited from '$1')"
1263     fi
1264
1265     ad_parse_distribution $i true
1266     ad_clear_options
1267   done
1268
1269   # Get license
1270   license=`sed 's/^[    ]*//' < $distdir/$1 | grep -v "^#" \
1271     | grep "license " | cut -d' ' -f2`
1272   licenseh=`sed 's/^[   ]*//' < $distdir/$1 | grep -v "^#" \
1273     | grep "license-header " | sed 's/license-header / : /'`
1274
1275   ad_debug "license: $license"
1276   ad_debug "licenseh: $licenseh"
1277
1278   if test x$2 = xfalse; then
1279     # Take rest of the stuff from top distribution
1280
1281     # We take precedence on defined and undefined distdefs.  Remove
1282     # undefined distdefs if we have defined them.
1283     for d in $defs
1284     do
1285       ad_debug "defining undefined $d distdef"
1286       undistdefs=`echo $undistdefs | sed s/$d//g`
1287     done
1288
1289     # Get distribution name
1290     dname=`sed 's/^[    ]*//' < $distdir/$1 | grep -v "^#" \
1291       | grep "name " | cut -d' ' -f2-`
1292
1293     if test "$dname"; then
1294       distribution=$dname
1295     fi
1296
1297     # Get distribution package name (optional)
1298     dpname=`sed 's/^[   ]*//' < $distdir/$1 | grep -v "^#" \
1299      | grep "package " | cut -d' ' -f2`
1300
1301     if test "$dpname"; then
1302       package=$dpname
1303     else
1304       package=$distribution
1305     fi
1306
1307     # Get Bug-report email address (optional)
1308     bugr=`sed 's/^[     ]*//' < $distdir/$1 | grep -v "^#" \
1309      | grep "bug-report " | cut -d' ' -f2-`
1310
1311     if test "$bugr"; then
1312       bug_report=$bugr
1313     fi
1314
1315     ad_debug "distribution: $distribution"
1316     ad_debug "package: $package"
1317     ad_debug "bug-report: $bug_report"
1318
1319     # Get hooks (optional)
1320     prh=`sed 's/^[      ]*//' < $distdir/$1 | grep -v "^#" \
1321      | grep "pre-hook " | cut -d' ' -f2-`
1322     poh=`sed 's/^[      ]*//' < $distdir/$1 | grep -v "^#" \
1323      | grep "post-hook " | cut -d' ' -f2-`
1324     prdh=`sed 's/^[     ]*//' < $distdir/$1 | grep -v "^#" \
1325      | grep "pre-dist-hook " | cut -d' ' -f2-`
1326     podh=`sed 's/^[     ]*//' < $distdir/$1 | grep -v "^#" \
1327      | grep "post-dist-hook " | cut -d' ' -f2-`
1328     prpdh=`sed 's/^[    ]*//' < $distdir/$1 | grep -v "^#" \
1329      | grep "pre-process-dist-hook " | cut -d' ' -f2-`
1330     popdh=`sed 's/^[    ]*//' < $distdir/$1 | grep -v "^#" \
1331      | grep "post-process-dist-hook " | cut -d' ' -f2-`
1332
1333     pre_hooks="$pre_hooks $prh"
1334     post_hooks="$post_hooks $poh"
1335     pre_dist_hooks="$pre_dist_hooks $prdh"
1336     post_dist_hooks="$post_dist_hooks $podh"
1337     pre_p_dist_hooks="$pre_p_dist_hooks $prpdh"
1338     post_p_dist_hooks="$post_p_dist_hooks $popdh"
1339     doptions="$doptions $opts"
1340
1341     ad_handle_options "$doptions"
1342     ad_debug "options: $doptions"
1343   fi
1344
1345   # Return to caller
1346   inherits="$inherits $inhs"
1347   distdefs="$distdefs $defs"
1348   includes="$includes $incs"
1349   excludes="$excludes $excs"
1350   noprocess="$noprocess $nops"
1351   undistdefs="$undistdefs $undefs"
1352
1353   ad_debug "Ending parsing distribution: $1 $2"
1354 }
1355
1356 #
1357 # Processes parsed distdefs.  Removes duplicates, and undefined distdefs
1358 # from the distdefs.
1359 #
1360 # Arguments: ad_process_distdefs
1361 #
1362 ad_process_distdefs()
1363 {
1364   ad_debug "Starting processing distdefs"
1365
1366   ad_log ""
1367   ad_log "Following distdefs were processed:"
1368   ad_log "----------------------------------"
1369
1370   # Remove all undefined distribution defines
1371   for i in $undistdefs
1372   do
1373     ad_debug "undefining $i distdef"
1374     distdefs=`echo $distdefs | sed s/$i//g`
1375   done
1376
1377   rm -f autodist.tmp.defs
1378
1379   # Remove duplicate distdefs
1380   for i in $distdefs
1381   do
1382     echo $i >>autodist.tmp.defs
1383   done
1384   if test -f autodist.tmp.defs; then
1385     distdefs=`sort < autodist.tmp.defs | uniq`
1386   fi
1387   distdefs=`echo $distdefs`
1388   rm -f autodist.tmp.defs
1389
1390   # Log
1391   for i in $distdefs
1392   do
1393     ad_log "  $i"
1394   done
1395   ad_log ""
1396
1397   ad_debug "distdefs=$distdefs"
1398
1399   ad_debug "Ending processing distdefs"
1400 }
1401
1402 #
1403 # Processes for a license header change.
1404 #
1405 # Arguments: ad_process_license_header <scriptfile> <sourcefile>
1406 #
1407 ad_process_license_header()
1408 {
1409   ad_debug "Starting license header processing"
1410
1411   # Add : separator at the end
1412   lics=`echo "$licenseh" | sed 's/$/ : /'`
1413
1414   src=
1415   dst=
1416   for i in $lics
1417   do
1418     if test "$i" = ":" && test -z "$src"; then
1419       continue
1420     fi
1421     if test -z "$src"; then
1422       src=$i
1423       continue
1424     fi
1425     if test -z "$dst" && test "$i" != ":"; then
1426       dst=$i
1427     else
1428       ad_fatal "Missing argument in 'license-header $src'"
1429     fi
1430
1431     ad_debug "Replacing $src license with $dst license"
1432
1433     if test '!' -f $src; then
1434       ad_fatal "License header $src: No such file or directory"
1435     fi
1436
1437     if test '!' -f $dst; then
1438       ad_fatal "License header $dst: No such file or directory"
1439     fi
1440
1441     # Awk script to replace the license header
1442     fl=`sed q $src | sed 's/\\//\\\\\//g' > autodist.lsrc` || exit 1
1443     ll=`sed -n '$p' $src | sed 's/\\//\\\\\//g' > autodist.ldst` || exit 1
1444     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
1445     rm -f autodist.lsrc autodist.ldst
1446
1447     src=
1448     dst=
1449   done
1450
1451   ad_debug "Ending license header processing"
1452 }
1453
1454 #
1455 # Process specified noprocesses.  This is called during makedist.
1456 # The noprocess first copies the noprocess files and dirs into a temp
1457 # directory, and then removes them from the distdir.  This way they are
1458 # not processed by Autodist.  After processing they are returned to their
1459 # correct locations from the temp dir.  Dirty, yeah, but the way we do
1460 # this until better one comes along.
1461 #
1462 # Arguments: ad_process_noprocess <process>
1463 #
1464 # If <process> is true this excludes and if false this includes.
1465 #
1466 ad_process_noprocess()
1467 {
1468   ad_debug "Starting running noprocesses"
1469
1470   set -f
1471
1472   cur=`pwd`
1473
1474   if test x$1 = xtrue; then
1475     ad_log ""
1476     ad_log "Following files and directories were not processed:"
1477     ad_log "---------------------------------------------------"
1478
1479     f="$cur/autodist__noprocess"
1480     rm -rf $f
1481     mkdir -p -- $f || exit 1
1482
1483     # First, include them to new location with correct directory structure.
1484     old_am_distdir="$am_distdir"
1485     cd $am_distdir || exit 1
1486     am_distdir="$f"
1487     nops=" $noprocess"
1488     nops=`echo $nops | sed -e 's/ / : /g' -e 's/^/ : /'` || exit 1
1489     ad_dist_includes "$nops" false false
1490     am_distdir="$old_am_distdir"
1491
1492     # Then, remove from distdir (they are in temp dir now)
1493     for i in $noprocess
1494     do
1495       ad_log "  $i"
1496       rm -rf $i
1497     done
1498     cd $cur || exit 1
1499
1500     ad_log ""
1501   else
1502     # Copy from the temp dir back to distdir
1503     cd autodist__noprocess || exit 1
1504     old_am_distdir="$am_distdir"
1505     am_distdir="$cur/$old_am_distdir"
1506     nops=" $noprocess"
1507     nops=`echo $nops | sed -e 's/ / : /g' -e 's/^/ : /'` || exit 1
1508
1509     ad_dist_includes "$nops" false false
1510
1511     am_distdir="$old_am_distdir"
1512     cd $cur || exit 1
1513     rm -rf autodist__noprocess
1514   fi
1515
1516   set +f
1517
1518   ad_debug "Ending running noprocesses"
1519 }
1520
1521 #
1522 # Process a file given as argument for the distribution.
1523 #
1524 # Arguments: ad_process_file <filepath> <dest_filepath> <re-license>
1525 #
1526 ad_process_file()
1527 {
1528   local found=false
1529   local f
1530   local defs
1531   local ndefs
1532
1533   # Process only regular files
1534   if test '!' -f $1; then
1535     return
1536   fi
1537
1538   ad_debug "Starting processing file: $1 $2"
1539
1540   f="autodist.tmp.script"
1541   rm -f $f
1542
1543   # If license header is provided, replace the license header in the file.
1544   if test x$3 = xtrue; then
1545     ad_process_license_header $f $1
1546   fi
1547
1548   # Get defined distribution defines
1549   defs=`awk "/^#ifdef "$DP"_DIST_|^#else "$DP"_DIST_/ { print; }" \
1550     $1 |cut -d'*' -f2 |cut -d' ' -f2 | sort | uniq`
1551
1552   # Get explicitly not-defined distribution defines
1553   ndefs=`awk "/^#ifndef "$DP"_DIST_|^#else !"$DP"_DIST_/ { print; }" \
1554     $1 |cut -d'*' -f2 |cut -d' ' -f2 | cut -d'!' -f2 | sort | uniq`
1555
1556   ad_debug "defs in $1: $defs"
1557   ad_debug "ndefs in $1: $ndefs"
1558
1559   # Create the script to include and exclude stuff in the file according
1560   # to the distribution defines
1561
1562   # ifdefs
1563   ad_debug "processing ifdefs"
1564   for d in $defs
1565   do
1566     found=false
1567     for i in $distdefs
1568     do
1569       if test x$d = x$i; then
1570         found=true
1571         break
1572       fi
1573     done
1574
1575     # If distribution define was not found exclude those lines from the file.
1576     # This also handles the #ifdef's #else (ie. #ifndef) branch.
1577     if test x$found = xfalse; then
1578       ad_debug "ifdef $d will be excluded (it is NOT defined)"
1579       echo "/^#ifdef $d$/,/^#else !$d$|^#endif $d$/ { next; }" >> $f
1580     else
1581       echo "/^#else !$d$/,/^#endif $d$/ { next; }" >> $f
1582     fi
1583   done
1584
1585   # ifndefs
1586   ad_debug "processing ifndefs"
1587   for d in $ndefs
1588   do
1589     found=false
1590     for i in $distdefs
1591     do
1592       if test x$d = x$i; then
1593         found=true
1594         break
1595       fi
1596     done
1597
1598     # If distribution define was found exclude those lines from the file.
1599     # This also handles the #ifndef's #else (ie. #ifdef) branch.
1600     if test x$found = xtrue; then
1601       ad_debug "ifndef $d will be excluded (it IS defined)"
1602       echo "/^#ifndef $d$/,/^#else $d$|^#endif $d$/ { next; }" >> $f
1603     else
1604       echo "/^#else $d$/,/^#endif $d$/ { next; }" >> $f
1605     fi
1606   done
1607
1608   # Now process the file with the script
1609   if test -f $f; then
1610
1611     # Those distdef lines that remain in the file are removed to make
1612     # the appearance prettier
1613     echo "/^#ifdef "$DP"_DIST_|^#endif "$DP"_DIST_|^#else "$DP"_DIST_|^#else !"$DP"_DIST_|^#ifndef "$DP"_DIST_/ { next; }" >> $f
1614     echo "{ print; }" >> $f
1615
1616     # Execute the script
1617     cp -p $1 $2 || exit 1
1618     awk -f $f $1 > $2 || exit 1
1619   fi
1620
1621   rm -f $f
1622
1623   # Log if file was not relicensed
1624   if test -f "$1.norelicense"; then
1625     ad_log "  $1"
1626     rm -f $1.norelicense
1627   fi
1628
1629   ad_debug "Ending processing file: $1 $2"
1630 }
1631
1632 #
1633 # Process a source file given as argument for the distribution.
1634 #
1635 # Arguments: ad_process_source_file <filepath> <dest_filepath> <re-license>
1636 #
1637 ad_process_source_file()
1638 {
1639   local found=false
1640   local f
1641   local defs
1642   local ndefs
1643
1644   # Process only regular files
1645   if test '!' -f $1; then
1646     return
1647   fi
1648
1649   ad_debug "Starting processing source file: $1 $2"
1650
1651   f="autodist.tmp.script"
1652   rm -f $f
1653
1654   # If license header is provided, replace the license header in the file.
1655   if test x$3 = xtrue; then
1656     ad_process_license_header $f $1
1657   fi
1658
1659   # Get defined distribution defines
1660   defs=`awk '/^#ifdef SILC_DIST_|^#else \/\* SILC_DIST_/ { print; }' \
1661     $1 |cut -d'*' -f2 |cut -d' ' -f2 | sort | uniq`
1662
1663   # Get explicitly not-defined distribution defines
1664   ndefs=`awk '/^#ifndef SILC_DIST_|^#else \/\* \!SILC_DIST_/ { print; }' \
1665     $1 |cut -d'*' -f2 |cut -d' ' -f2 | cut -d'!' -f2 | sort | uniq`
1666
1667   ad_debug "defs in $1: $defs"
1668   ad_debug "ndefs in $1: $ndefs"
1669
1670   # Create the script to include and exclude stuff in the file according
1671   # to the distribution defines
1672
1673   # ifdefs
1674   ad_debug "processing ifdefs"
1675   for d in $defs
1676   do
1677     found=false
1678     for i in $distdefs
1679     do
1680       if test x$d = x$i; then
1681         found=true
1682         break
1683       fi
1684     done
1685
1686     # If distribution define was not found exclude those lines from the file.
1687     # This also handles the #ifdef's #else (ie. #ifndef) branch.
1688     if test x$found = xfalse; then
1689       ad_debug "ifdef $d will be excluded (it is NOT defined)"
1690       echo "/^#ifdef $d$/,/^#else \/\* \!$d |^#endif \/\* $d / { next; }" >> $f
1691     else
1692       echo "/^#else \/\* \!$d /,/^#endif \/\* $d / { next; }" >> $f
1693     fi
1694   done
1695
1696   # ifndefs
1697   ad_debug "processing ifndefs"
1698   for d in $ndefs
1699   do
1700     found=false
1701     for i in $distdefs
1702     do
1703       if test x$d = x$i; then
1704         found=true
1705         break
1706       fi
1707     done
1708
1709     # If distribution define was found exclude those lines from the file.
1710     # This also handles the #ifndef's #else (ie. #ifdef) branch.
1711     if test x$found = xtrue; then
1712       ad_debug "ifndef $d will be excluded (it IS defined)"
1713       echo "/^#ifndef $d$/,/^#else \/\* $d |^#endif \/\* $d / { next; }" >> $f
1714     else
1715       echo "/^#else \/\* $d /,/^#endif \/\* $d / { next; }" >> $f
1716     fi
1717   done
1718
1719   # Now process the file with the script
1720   if test -f $f; then
1721
1722     # Those distdef lines that remain in the file are removed to make
1723     # the appearance prettier
1724     echo "/^#ifdef SILC_DIST_|^#endif \/\* SILC_DIST_|^#else \/\* SILC_DIST_|^#else \/\* \!SILC_DIST_|^#ifndef SILC_DIST_/ { next; }" >> $f
1725     echo "{ print; }" >> $f
1726
1727     # Execute the script
1728     cp -p $1 $2 || exit 1
1729     awk -f $f $1 > $2 || exit 1
1730   fi
1731
1732   rm -f $f
1733
1734   # Log if file was not relicensed
1735   if test -f "$1.norelicense"; then
1736     ad_log "  $1"
1737     rm -f $1.norelicense
1738   fi
1739
1740   ad_debug "Ending processing source file: $1 $2"
1741 }
1742
1743 #
1744 # Processes a file.  This is the -p, --process option.
1745 #
1746 # Arguments: ad_process <type> <src> <dst>
1747 #
1748 ad_process()
1749 {
1750   ad_debug "Starting process: $1 $2 $3"
1751
1752   nolog=true
1753
1754   if test '!' -f autodist.dist; then
1755     ad_fatal "Autodist has not been run yet to prepare source tree"
1756   fi
1757
1758   # Parse distribution
1759   distribution=`grep "dist:" < autodist.dist | cut -d: -f2` || exit 1
1760   dist_version=`grep "ver:" < autodist.dist | cut -d: -f2` || exit 1
1761   ad_parse_distribution $distribution false
1762   ad_process_distdefs
1763
1764   # Process file
1765   case "$1" in
1766     makefile)
1767       if test -z $3; then
1768         ad_fatal "File type $1 requires <dst> argument"
1769       fi
1770       ad_make_makefile_am $2 $3
1771       exit 0;;
1772
1773     configure)
1774       ad_make_configure_ac $2
1775       exit 0;;
1776
1777     non-source)
1778       if test -z $3; then
1779         ad_fatal "File type $1 requires <dst> argument"
1780       fi
1781       ad_process_file $2 $3 false
1782       exit 0;;
1783
1784     source)
1785       if test -z $3; then
1786         ad_fatal "File type $1 requires <dst> argument"
1787       fi
1788       ad_process_source_file $2 $3 false
1789       exit 0;;
1790
1791     *)
1792       ad_fatal "Unknown file type: $1";
1793       ;;
1794   esac
1795 }
1796
1797 #
1798 # Run hooks
1799 #
1800 # Arguments: ad_run_hooks <hooks> <params>
1801 #
1802 ad_run_hooks()
1803 {
1804   ad_debug "Running hooks: $1"
1805
1806   ad_log ""
1807   ad_log "Hooks executed:"
1808   ad_log "--------------"
1809
1810   for i in $1
1811   do
1812     if test '!' -f $i; then
1813       ad_fatal "Hook script $i does not exist"
1814     fi
1815     ad_log "  sh $i \"$distribution\" \"$dist_version\" \"$package\" \"$2\""
1816     sh $i "$distribution" "$dist_version" "$package" "$2" || exit 1
1817   done
1818   ad_log ""
1819
1820   ad_debug "Ending running hooks: $1"
1821 }
1822
1823 #
1824 # Run dist hooks
1825 #
1826 # Arguments: ad_run_dist_hooks <hooks> <params>
1827 #
1828 ad_run_dist_hooks()
1829 {
1830   ad_debug "Starting running distributions hooks: $1"
1831
1832   ad_log ""
1833   ad_log "Distribution hooks executed:"
1834   ad_log "----------------------------"
1835
1836   for i in $1
1837   do
1838     if test '!' -f $i; then
1839       ad_fatal "Dist hook script $i does not exist"
1840     fi
1841
1842     ad_log "  sh $i \"$distribution\" \"$dist_version\" \"$package\" \"$am_distdir\" \"$2\""
1843     sh $i "$distribution" "$dist_version" "$package" "$am_distdir" "$2" || exit 1
1844   done
1845   ad_log ""
1846
1847   ad_debug "Ending running distribution hooks"
1848 }
1849
1850 ###############################################################################
1851 # Autodist code
1852
1853 usage="Usage: autodist [OPTIONS] [DISTRIBUTION] [VERSION] [PARAMS]"
1854 help="\
1855 Autodist prepares source tree for configuration, compilation and
1856 distribution.  Generates Automake.am files from Automake.ad files,
1857 configure.ac file from configure.ad file(s), generates the configure
1858 script by running Autoconf tool, and generates Makefile.in files by
1859 running Automake tool.
1860
1861 Operation modes:
1862   -h, --help                print this help, then exit
1863   -V, --version             print version number, then exit
1864   -v, --verbose             verbosely report processing
1865   -d, --distdir <dir>       search distributions from <dir>
1866   -s, --distdefs [<dist>]   print distribution defines of <dist>, then exit
1867   -i, --init                initialize Autodist environment, create default
1868                             distribution directory and distribution, then exit
1869   -p, --process <type> <src> [<dst>]
1870                             process file <src> into <dst> for distribution,
1871                             <type> is 'makefile', 'configure', 'non-source'
1872                             or 'source' and defines the type of <src>
1873   -m, --makedist            create and package distribution
1874       --gzip                create package compressed with gzip (default)
1875       --bzip2               create also package compressed with bzip2
1876       --compress            create also package compressed with compress
1877       --zip                 create also package compressed with zip"
1878
1879 #
1880 # Process command line arguments
1881 #
1882 while test $# -gt 0; do
1883   case "${1}" in
1884
1885   -d |--distdir)
1886     shift;
1887     test $# -eq 0 && { echo "${usage}" 1>&2; exit 1; }
1888     distdir="${1}";
1889     shift;;
1890
1891   --list)
1892     exit 0;;
1893
1894   -s | --distdefs)
1895     shift;
1896     if test $# -eq 0; then
1897       ad_parse_distribution $distribution false
1898       echo "Distribution: ${distribution}" 1>&2;
1899     else
1900       ad_parse_distribution $1 false
1901       echo "Distribution: ${1}" 1>&2;
1902     fi
1903     ad_process_distdefs
1904     echo "Distdefs:" 1>&2;
1905     for i in $distdefs
1906     do
1907       echo "$i";
1908     done
1909     exit 0;;
1910
1911   -i | --init)
1912     ad_initialize;
1913     exit 0;;
1914
1915   -p | --process)
1916     shift;
1917     if test $# -ge 2; then
1918       ad_process $1 $2 $3
1919     else
1920       echo "${usage}" 1>&2;
1921     fi
1922     exit 0;;
1923
1924   -m | --makedist)
1925     ad_makedist
1926     exit 0;;
1927
1928   --gzip)
1929     ad_gzip=true
1930     shift;;
1931
1932   --bzip2)
1933     ad_bzip2=true
1934     shift;;
1935
1936   --compress)
1937     ad_compress=true
1938     shift;;
1939
1940   --zip)
1941     ad_zip=true
1942     shift;;
1943
1944   -v | --verbose)
1945     debug=true
1946     shift;;
1947
1948   -h | --help | --h*)
1949     echo "${usage}" 1>&2;
1950     echo 1>&2;
1951     echo "${help}" 1>&2;
1952     echo 1>&2;
1953     exit 0;;
1954
1955   -V | --version)
1956     echo "@PACKAGE@ (@PACKAGE_NAME@) $ver" 1>&2;
1957     echo "Written by Pekka Riikonen" 1>&2;
1958     echo 1>&2;
1959     echo "Copyright (C) 2004 - 2007 SILC Project" 1>&2;
1960     echo "\
1961 This is free software; see the source for copying conditions.  There is NO
1962 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. " 1>&2;
1963     exit 0;;
1964
1965   --)
1966     shift;
1967     break;;
1968
1969   -*)
1970     echo "${usage}" 1>&2;
1971     exit 1;;
1972
1973   *)
1974     break;;
1975
1976   esac
1977 done
1978
1979 # Open log file
1980 ad_log_open "autodist.log"
1981
1982 ad_log "Processing source tree for compilation and configuration"
1983 ad_log "--------------------------------------------------------"
1984 ad_log ""
1985
1986 #
1987 # Parse the requested distribution
1988 #
1989 if test $# != 0; then
1990   distribution="${1}";
1991   distfile=$distribution
1992   shift
1993 fi
1994
1995 ad_parse_distribution $distribution false
1996
1997 if test $# != 0; then
1998   dist_version="${1}";
1999 fi
2000
2001 ad_log "  Distribution: $distribution $dist_version"
2002 ad_log ""
2003
2004 ad_process_distdefs
2005
2006 ad_debug "Preparing source tree for configuration and compilation..."
2007 ad_debug "Preparing $distribution distribution version $dist_version"
2008
2009 #
2010 # Create the distribution defines header file
2011 #
2012 if test "$DISTDEFS"; then
2013   ad_create_distdefs_h
2014 fi
2015
2016 # Get extra parameters from command line
2017 if test $# != 0; then
2018   shift
2019 fi
2020
2021 #
2022 # Run pre-hooks
2023 #
2024 ad_run_hooks "$pre_hooks" "$@"
2025
2026 #
2027 # Generate the Makefile.am files from Makefile.ad files
2028 #
2029 ad_make_makefile_ams
2030
2031 #
2032 # Generate the configure.ac from configure.ad file(s)
2033 #
2034 ad_make_configure_ac ./configure.ad
2035
2036 #
2037 # Process all files with .ad suffix for distribution processing
2038 #
2039 ad_process_ads
2040
2041 #
2042 # Generate configure script
2043 #
2044 ad_make_configure
2045
2046 #
2047 # Generate Makefile.in files
2048 #
2049 ad_make_makefile_ins
2050
2051 #
2052 # Create autodist.dist
2053 #
2054 ad_debug "Creating autodist.dist"
2055 echo "dist:$distfile" > autodist.dist
2056 echo "ver:$dist_version" >> autodist.dist
2057 echo "distdir:$package-$dist_version" >> autodist.dist
2058 echo "params:$@" >> autodist.dist
2059
2060 #
2061 # Run post-hooks
2062 #
2063 ad_run_hooks "$post_hooks" "$@"
2064
2065 ad_log "Source tree processed successfully."
2066 ad_debug "Done, now run ./configure and make."
2067
2068 exit 0