3 # Author: Pekka Riikonen <priikone@silcnet.org>
5 # Copyright (C) 2005 Pekka Riikonen
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are
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
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.
33 ###############################################################################
45 # default conffile in -i
46 # default distro in -i
48 # NOTE: inherit inherits only distdefs, undefs, hooks, options, includes
49 # and excludes. It must not inherit others.
53 # Scripts have the following variables in their disposal
55 # distribution distribution name
56 # dist_version distribution version
57 # package the package name of the distribution
59 # Allowed structure for distdefs in non-source files
61 #ifdef SILC_DIST_DEFINE
62 #endif SILC_DIST_DEFINE
64 #ifndef SILC_DIST_DEFINE
65 #endif SILC_DIST_DEFINE
67 #ifdef SILC_DIST_DEFINE
68 #else !SILC_DIST_DEFINE
69 #endif SILC_DIST_DEFINE
71 #ifndef SILC_DIST_DEFINE
72 #else SILC_DIST_DEFINE
73 #endif SILC_DIST_DEFINE
75 # Allowed structure for distdefs in source files
77 #ifdef SILC_DIST_DEFINE
78 #endif /* SILC_DIST_DEFINE */
80 #ifndef SILC_DIST_DEFINE
81 #endif /* SILC_DIST_DEFINE */
83 #ifdef SILC_DIST_DEFINE
84 #else /* !SILC_DIST_DEFINE */
85 #endif /* SILC_DIST_DEFINE */
87 #ifndef SILC_DIST_DEFINE
88 #else /* SILC_DIST_DEFINE */
89 #endif /* SILC_DIST_DEFINE */
91 ###############################################################################
94 # Distribution subdirectory
97 # This current distribution
102 # All inherited distributions in this distribution
105 # All distribution defines for this distribution
108 # All distribution undefines for this distribution
111 # All distribution options
120 # All pre, post pre-dist and post-dist hooks
126 # Distribution license and license header
130 # Whether to output ad_debug information
136 ###############################################################################
138 if test -f "$distdir/autodist.conf"; then
139 . $distdir/autodist.conf
145 ###############################################################################
149 # Print out debug information if debugging is enabled. To enable debugging
150 # set the global variable "debug" to true value.
152 # Arguments: ad_debug <ad_debug string>
156 if test x$debug = xtrue; then
162 # Prints out error message and exits the script.
164 # Arguments: ad_fatal <error message>
168 echo autodist: error: $1
173 # Initializes the Autodist environment, creates default distribution
174 # directory, and default distribution.
176 # Arguments: ad_initialize
180 ad_debug "-->$FUNCNAME()"
182 # Create default distdir
183 if test '!' -f $distdir; then
187 # Create default distribution
190 ad_debug "<--$FUNCNAME()"
194 # Creates the distdefs header file including defined distdefs
196 # Arguments: ad_create_distdefs_h
198 ad_create_distdefs_h()
200 ad_debug "-->$FUNCNAME()"
205 echo " Automatically generated by Autodist $ver. Do not edit." >> $fname
207 echo " Generated: `date` by `whoami`" >> $fname
208 echo " Distribution: $distribution" >> $fname
209 echo " License: $license" >> $fname
212 echo "#ifndef _"$DP"_DISTDEFS_H" >> $fname
213 echo "#define _"$DP"_DISTDEFS_H" >> $fname
218 echo "#define $i 1" >>$fname
222 echo "#endif /* _"$DP"_DISTDEFS_H */" >> $fname
224 ad_debug "<--$FUNCNAME()"
228 # Creates the main configure script for the distribution. This runs
229 # the aclocal, autoheader and autoconf tools.
231 # Arguments: ad_make_configure
235 ad_debug "-->$FUNCNAME()"
236 local run_autoconf=false
240 if test "$ACLOCAL"; then
241 ad_debug "Running aclocal"
242 $ACLOCAL 1>/dev/null 2>/dev/null
243 if test $? != 0; then
244 ad_fatal "aclocal failed"
248 if test "$AUTOCONF"; then
249 ad_debug "Running autoconf"
251 if test $? != 0; then
252 ad_fatal "autoconf failed"
257 if test "$AUTOHEADER"; then
258 ad_debug "Running autoheader"
260 if test $? != 0; then
261 ad_fatal "autoheader failed"
265 if test "$LIBTOOLIZE"; then
266 ad_debug "Running libtoolize"
268 if test $? != 0; then
269 ad_fatal "libtoolize failed"
273 if test x$run_autoconf = xtrue; then
274 if test '!' -f configure; then
275 ad_fatal "creating configure script failed"
279 ad_debug "<--$FUNCNAME()"
283 # Creates the configure.ac script from the configure.ad fragments in
284 # the source tree. Takes the source configure file as argument which
285 # is used to create the actual configure.ac.
287 # Arguments: ad_make_configure_ac <configure_ac_source>
289 ad_make_configure_ac()
291 ad_debug "-->$FUNCNAME(): $1"
293 if test '!' -f $1; then
294 ad_fatal "The configure file '$1' does not exist"
297 rm -f configure.ac configure.ad.cfs
299 cfs=`find . -name configure\*\.ad`
302 if test "x$i" = "x$1"; then
305 ad_debug "including $i"
306 cat $i >> configure.ad.cfs
309 # Header for configure.ac
310 fname="configure.tmp.ac"
311 echo "# Automatically generated by Autodist $ver. Do not edit." > $fname
312 echo "# To make changes edit the $i file in the source tree." >> $fname
314 echo "# Source: $i" >> $fname
315 echo "# Generated: `date` by `whoami`" >> $fname
316 echo "# Distribution: $distribution" >> $fname
317 echo "# License: $license" >> $fname
320 ad_debug "creating configure.ac"
321 if test -f configure.ad.cfs; then
322 sed '/AD_INCLUDE_CONFIGURE/ r configure.ad.cfs' $1 > $fname.tmp
323 sed -e "/AD_INCLUDE_CONFIGURE/s///" $fname.tmp >> $fname
324 rm -f configure.ad.cfs $fname.tmp
330 sed -e "/AD_INIT/s//AC_INIT($distribution, $dist_version, [], $package)/" $fname > $fname.tmp
332 # Process for distribution
333 ad_process_file $fname.tmp configure.ac
334 rm -f $fname $fname.tmp
336 ad_debug "<--$FUNCNAME(): $1"
340 # Creates the Makefile.in files by running the automake tool.
342 # Arguments: ad_make_makefile_ins
344 ad_make_makefile_ins()
346 ad_debug "-->$FUNCNAME()"
348 if test "$AUTOMAKE"; then
349 ad_debug "Running automake"
351 if test $? != 0; then
352 ad_fatal "automake failed"
356 ad_debug "<--$FUNCNAME()"
360 # Creates the Makefile.am files from the Makefile.ad files in the
361 # source tree. This runs the distribution specific processing for the
364 # Arguments: ad_make_makefile_ams
366 ad_make_makefile_ams()
368 ad_debug "-->$FUNCNAME()"
370 files=`find . -name Makefile\.ad`
373 fname=`echo $i | sed s/\.ad//`
375 # Header for the Makefile.am
376 echo "# Automatically generated by Autodist $ver from Makefile.ad. Do not edit." > $fname.am
377 echo "# To make changes edit the $i file in the source tree." >> $fname.am
379 echo "# Source: $i" >> $fname.am
380 echo "# Generated: `date` by `whoami`" >> $fname.am
381 echo "# Distribution: $distribution" >> $fname.am
382 echo "# License: $license" >> $fname.am
385 # Run the distribution processing for this Makefile.ad
386 ad_debug "Processing $i to be $fname.am"
387 ad_process_file $i $fname.am
390 ad_debug "<--$FUNCNAME()"
394 # Processes all files with .ad suffix, with exception of configure*.ad
395 # and Makefile.ad files, for distribution from the source tree.
397 # Arguments: ad_process_ads
401 ad_debug "-->$FUNCNAME()"
403 files=`find . -name \*\.ad \! -name configure\*\.ad \! -name Makefile\.ad`
406 fname=`echo $i | sed s/\.ad//`
409 echo "# Automatically generated by Autodist $ver. Do not edit." > $fname
410 echo "# To make changes edit the $i file in the source tree." >> $fname
412 echo "# Source: $i" >> $fname
413 echo "# Generated: `date` by `whoami`" >> $fname
414 echo "# Distribution: $distribution" >> $fname
415 echo "# License: $license" >> $fname
418 # Run the distribution processing for this file
419 ad_debug "Processing $i to be $fname"
420 ad_process_file $i $fname
423 ad_debug "<--$FUNCNAME()"
427 # Processes the entire source tree for distribution. This inspects files
428 # in the source tree, with exception of any file with .ad suffix, and
429 # performs distribution processing for the file. The original file is
430 # replaced with the processed file. This function is used when creating
431 # the distribution for packaging.
433 # Arguments: ad_process_source_tree <directory>
435 ad_process_source_tree()
437 ad_debug "-->$FUNCNAME(): $1"
439 ad_debug "<--$FUNCNAME(): $1"
443 # Creates distribution of the source tree. All files in the distribution
444 # will be processed and the distribution will be packaged.
448 ad_debug "-->$FUNCNAME()"
450 ad_debug "<--$FUNCNAME()"
454 # Parses the distribution. Gets all distribution defines from the
455 # distribution. This also expands all inherited distributions recursively
456 # to get all inherited distribution defines. From inherited distributions
457 # their name and package name is not inherited.
459 # Arguments: parse_distribution <distribution name> <inherit>
461 ad_parse_distribution()
463 ad_debug "-->$FUNCNAME(): $1"
465 if test '!' -f $distdir/$1; then
466 ad_fatal "Distribution '$1' is not declared"
470 local inhs=`cat $distdir/$1 | grep -v "#" \
471 | grep "inherit " | cut -d' ' -f2 | sort | uniq`
474 local defs=`cat $distdir/$1 | grep -v "#" \
475 | grep "define " | cut -d' ' -f2 | sort | uniq`
477 if test "$inhs" = "" && test "$defs" = ""; then
478 ad_fatal "Distribution '$1' does not define anything"
481 # Get undefined distdefs
482 local undefs=`cat $distdir/$1 | grep -v "#" \
483 | grep "undef " | cut -d' ' -f2`
486 local opts=`cat $distdir/$1 | grep -v "#" \
487 | grep "option " | cut -d' ' -f2`
490 local incs=`cat $distdir/$1 | grep -v "#" \
491 | grep "include " | cut -d' ' -f2`
494 local excs=`cat $distdir/$1 | grep -v "#" \
495 | grep "exclude " | cut -d' ' -f2`
497 ad_debug "inherits=$inhs"
498 ad_debug "distdefs=$defs"
499 ad_debug "includes=$incs"
500 ad_debug "excludes=$excs"
501 ad_debug "undistdefs=$undefs"
502 ad_debug "options=$opts"
504 # Expand distdefs from inherited distributions
507 if test x$1 = x$i; then
508 ad_fatal "Infinite recursion detected. Fix the '$distdir/$1' \
509 distribution to not have 'inherit $i' declared."
512 if test '!' -f $distdir/$i; then
513 ad_fatal "Distribution '$i' is not declared (inherited from '$1')"
516 ad_parse_distribution $i true
520 license=`cat $distdir/$1 | grep -v "#" \
521 | grep "license " | cut -d' ' -f2`
522 licenseh=`cat $distdir/$1 | grep -v "#" \
523 | grep "license-header " | cut -d' ' -f2`
525 ad_debug "license=$license"
526 ad_debug "licenseh=$licenseh"
528 if test x$2 = xfalse; then
529 # Get distribution name
530 local dname=`cat $distdir/$1 | grep -v "#" | grep "name "`
532 if test "$dname"; then
536 # Get distribution package name (optional)
537 local dpname=`cat $distdir/$1 | grep -v "#" \
538 | grep "package " | cut -d' ' -f2`
540 if test "$dpname"; then
543 package=$distribution
546 ad_debug "distribution=$distribution"
547 ad_debug "package=$package"
550 # Get hooks (optional)
551 local prh=`cat $distdir/$1 | grep -v "#" \
552 | grep "pre-hook " | cut -d' ' -f2`
553 local poh=`cat $distdir/$1 | grep -v "#" \
554 | grep "post-hook " | cut -d' ' -f2`
555 local prdh=`cat $distdir/$1 | grep -v "#" \
556 | grep "pre-dist-hook " | cut -d' ' -f2`
557 local podh=`cat $distdir/$1 | grep -v "#" \
558 | grep "post-dist-hook " | cut -d' ' -f2`
561 inherits="$inherits $inhs"
562 distdefs="$distdefs $defs"
563 includes="$includes $incs"
564 excludes="$excludes $incs"
565 undistdefs="$undistdefs $undefs"
566 options="$options $opts"
567 pre_hooks="$pre_hooks $prh"
568 post_hooks="$post_hooks $poh"
569 pre_dist_hooks="$pre_dist_hooks $prdh"
570 post_dist_hooks="$post_dist_hooks $podh"
572 ad_debug "<--$FUNCNAME(): $1"
576 # Processes parsed distdefs. Removes duplicates, and undefined distdefs
579 # Arguments: ad_process_distdefs
581 ad_process_distdefs()
583 ad_debug "-->$FUNCNAME()"
585 # Remove all undefined distribution defines
588 ad_debug "undefining $i distdef"
589 distdefs=`echo $distdefs | sed s/$i//`
592 rm -f autodist.tmp.defs autodist.pre.hooks autodist.post.hooks
593 rm -f autodist.pre.dist.hooks autodist.post.dist.hooks
595 # Remove duplicate distdefs
598 echo $i >>autodist.tmp.defs
600 distdefs=`cat autodist.tmp.defs | sort | uniq`
601 distdefs=`echo $distdefs`
602 rm -f autodist.tmp.defs
604 ad_debug "distdefs=$distdefs"
606 # Remove duplicate pre-hooks
609 echo ". $i" >>autodist.tmp.defs
611 if test -f autodist.tmp.defs; then
612 cat autodist.tmp.defs | sort | uniq > autodist.pre.hooks
613 rm -f autodist.tmp.defs
616 # Remove duplicate post-hooks
619 echo ". $i" >>autodist.tmp.defs
621 if test -f autodist.tmp.defs; then
622 cat autodist.tmp.defs | sort | uniq > autodist.post.hooks
623 rm -f autodist.tmp.defs
626 # Remove duplicate pre-dist-hooks
627 for i in $pre_dist_hooks
629 echo ". $i" >>autodist.tmp.defs
631 if test -f autodist.tmp.defs; then
632 cat autodist.tmp.defs | sort | uniq > autodist.pre.dist.hooks
633 rm -f autodist.tmp.defs
636 # Remove duplicate post-dist-hooks
637 for i in $post_dist_hooks
639 echo ". $i" >>autodist.tmp.defs
641 if test -f autodist.tmp.defs; then
642 cat autodist.tmp.defs | sort | uniq > autodist.post.dist.hooks
643 rm -f autodist.tmp.defs
646 ad_debug "<--$FUNCNAME()"
650 # Process a file given as argument for the distribution.
652 # Arguments: process_file <filepath> <dest_filepath>
658 ad_debug "-->$FUNCNAME(): $1 $2"
660 # Process only regular files
661 if test '!' -f $1; then
662 ad_debug "<--$FUNCNAME(): $1 $2"
666 ad_debug "Getting #ifdef's and #ifndef's"
668 # Get defined distribution defines
669 local defs=`awk "/#ifdef "$DP"_DIST_|#else "$DP"_DIST_/ { print; }" \
670 $1 |cut -d'*' -f2 |cut -d' ' -f2 | sort | uniq`
672 # Get explicitly not-defined distribution defines
673 local ndefs=`awk "/#ifndef "$DP"_DIST_|#else !"$DP"_DIST_/ { print; }" \
674 $1 |cut -d'*' -f2 |cut -d' ' -f2 | cut -d'!' -f2 | sort | uniq`
676 ad_debug "defs=$defs"
677 ad_debug "ndefs=$ndefs"
679 # Create the script to include and exclude stuff in the file according
680 # to the distribution defines
681 local f=autodist.tmp.script
685 ad_debug "processing ifdefs"
691 if test x$d = x$i; then
697 # If distribution define was not found exclude those lines from the file.
698 # This also handles the #ifdef's #else (ie. #ifndef) branch.
699 if test x$found = xfalse; then
700 ad_debug "ifdef $d will be excluded (it is NOT defined)"
701 echo "/^#ifdef $d/,/^#else !$d|^#endif $d/ { next; }" >> $f
703 echo "/^#else !$d/,/^#endif $d/ { next; }" >> $f
708 ad_debug "processing ifndefs"
714 if test x$d = x$i; then
720 # If distribution define was found exclude those lines from the file.
721 # This also handles the #ifndef's #else (ie. #ifdef) branch.
722 if test x$found = xtrue; then
723 ad_debug "ifndef $d will be excluded (it IS defined)"
724 echo "/^#ifndef $d/,/^#else $d|^#endif $d/ { next; }" >> $f
726 echo "/^#else $d/,/^#endif $d/ { next; }" >> $f
730 # Now process the file with the script
733 # Those distdef lines that remain in the file are removed to make
734 # the appearance prettier
735 echo "/^#ifdef "$DP"_DIST_|^#endif "$DP"_DIST_|^#else "$DP"_DIST_|^#else !"$DP"_DIST_|^#ifndef "$DP"_DIST_/ { next; }" >> $f
736 echo "{ print; }" >> $f
744 ad_debug "<--$FUNCNAME(): $1 $2"
749 # Process a source file given as argument for the distribution.
751 # Arguments: process_source_file <filepath> <dest_filepath>
753 ad_process_source_file()
757 ad_debug "-->$FUNCNAME(): $1 $2"
759 # Process only regular files
760 if test '!' -f $1; then
761 ad_debug "<--$FUNCNAME(): $1 $2"
765 ad_debug "Getting #ifdef's and #ifndef's"
767 # Get defined distribution defines
768 local defs=`awk '/#ifdef SILC_DIST_|#else \/\* SILC_DIST_/ { print; }' \
769 $1 |cut -d'*' -f2 |cut -d' ' -f2 | sort | uniq`
771 # Get explicitly not-defined distribution defines
772 local ndefs=`awk '/#ifndef SILC_DIST_|#else \/\* \!SILC_DIST_/ { print; }' \
773 $1 |cut -d'*' -f2 |cut -d' ' -f2 | cut -d'!' -f2 | sort | uniq`
775 ad_debug "defs=$defs"
776 ad_debug "ndefs=$ndefs"
778 # Create the script to include and exclude stuff in the file according
779 # to the distribution defines
780 local f=autodist.tmp.script
784 ad_debug "processing ifdefs"
790 if test x$d = x$i; then
796 # If distribution define was not found exclude those lines from the file.
797 # This also handles the #ifdef's #else (ie. #ifndef) branch.
798 if test x$found = xfalse; then
799 ad_debug "ifdef $d will be excluded (it is NOT defined)"
800 echo "/^#ifdef $d/,/^#else \/\* \!$d|^#endif \/\* $d/ { next; }" >> $f
802 echo "/^#else \/\* \!$d/,/^#endif \/\* $d/ { next; }" >> $f
807 ad_debug "processing ifndefs"
813 if test x$d = x$i; then
819 # If distribution define was found exclude those lines from the file.
820 # This also handles the #ifndef's #else (ie. #ifdef) branch.
821 if test x$found = xtrue; then
822 ad_debug "ifndef $d will be excluded (it IS defined)"
823 echo "/^#ifndef $d/,/^#else \/\* $d|^#endif \/\* $d/ { next; }" >> $f
825 echo "/^#else \/\* $d/,/^#endif \/\* $d/ { next; }" >> $f
829 # Now process the file with the script
832 # Those distdef lines that remain in the file are removed to make
833 # the appearance prettier
834 echo "/^#ifdef SILC_DIST_|^#endif \/\* SILC_DIST_|^#else \/\* SILC_DIST_|^#else \/\* \!SILC_DIST_|^#ifndef SILC_DIST_/ { next; }" >> $f
835 echo "{ print; }" >> $f
843 ad_debug "<--$FUNCNAME(): $1 $2"
847 ###############################################################################
850 usage="Usage: autodist [options] [distribution] [version]"
852 Autodist prepares source tree for configuration, compilation and
853 distribution. Prepares the source tree from the \`autodist.ad'
854 configuration file. Generates Automake.am files from Automake.ad
855 files, configure.ac file from configure.ad file(s), generates the
856 configure script by running Autoconf tool, and generates Makefile.in
857 files by running Automake tool.
860 -h, --help print this help, then exit
861 -V, --version print version number, then exit
862 -v, --verbose verbosely report processing
863 -d, --distdir <dir> search distributions from <dir>
864 -s, --distdefs [<dist>] print distribution defines of <dist>, then exit
865 -m, --makedist create and package distribution
866 -i, --init initialize Autodist environment, create default
867 distribution directory and distribution, then exit"
870 # Process command line arguments
872 while test $# -gt 0; do
877 test $# -eq 0 && { echo "${usage}" 1>&2; exit 1; }
886 if test $# -eq 0; then
887 ad_parse_distribution $distribution false
888 echo "Distribution: ${distribution}" 1>&2;
890 ad_parse_distribution $1 false
891 echo "Distribution: ${1}" 1>&2;
894 echo "Distdefs:" 1>&2;
911 echo "${usage}" 1>&2;
918 echo "autodist (SILC Autodist) $ver" 1>&2;
919 echo "Written by Pekka Riikonen" 1>&2;
921 echo "Copyright (C) 2004 - 2005 SILC Project" 1>&2;
923 This is free software; see the source for copying conditions. There is NO
924 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. " 1>&2;
932 echo "${usage}" 1>&2;
942 # Parse the requested distribution
944 if test $# != 0; then
948 ad_parse_distribution $distribution false
951 if test $# != 0; then
954 ad_debug "Preparing source tree for configuration and compilation..."
955 ad_debug "Preparing $distribution distribution version $dist_version"
958 # Create the distribution defines header file
960 if test "$DISTDEFS"; then
963 ad_fatal "DISTDEFS not defined in $distdir/autodist.conf"
969 if test -f autodist.pre.hooks; then
971 rm -f autodist.pre.hooks
975 # Generate the Makefile.am files from Makefile.ad files
980 # Generate the configure.ac from configure.ad file(s)
982 ad_make_configure_ac ./configure.ad
985 # Generate configure script
990 # Generate Makefile.in files
995 # Process all files with .ad suffix for distribution processing
1002 if test -f autodist.post.hooks; then
1003 . autodist.post.hooks
1004 rm -f autodist.post.hooks
1008 # Generate distribution, if requested
1010 if test x$makedist = xtrue; then
1011 ad_debug "Creating distribution"
1016 ad_debug "Done, now run ./configure and make."