Match distdefs exactly.
[autodist.git] / apps / autodist / autodist.in
index f45ac7ccb337fc19d1e715f7b632f83d6416e5b9..8b30e67f0f5716fc2de52affe9e9f35bfee17f96 100755 (executable)
@@ -129,6 +129,7 @@ licenseh=
 
 # Whether to output ad_debug information
 debug=false
+nolog=false
 
 # Autodist version
 ver=@VERSION@
@@ -210,7 +211,9 @@ EOF
 #
 ad_log()
 {
-  echo "$1" >&5
+  if test x$nolog = xfalse; then
+    echo "$1" >&5
+  fi
 }
 
 #
@@ -225,7 +228,7 @@ ad_initialize()
 
   # Create default distdir
   if test '!' -f $distdir; then
-    mkdir -p $distdir
+    mkdir -p -- $distdir
   fi
 
   # Create Autodist configuration file
@@ -405,8 +408,8 @@ EOF
 
   ad_debug "creating configure.ac"
   if test -f configure.ad.cfs; then
-    sed '/AD_INCLUDE_CONFIGURE/ r configure.ad.cfs' $1 > $fname.tmp
-    sed -e "/AD_INCLUDE_CONFIGURE/s///" $fname.tmp >> $fname
+    sed '/^AD_INCLUDE_CONFIGURE/ r configure.ad.cfs' $1 > $fname.tmp
+    sed -e "/^AD_INCLUDE_CONFIGURE/d" $fname.tmp >> $fname
     rm -f configure.ad.cfs $fname.tmp
   else
     cat $1 >> $fname
@@ -415,15 +418,18 @@ EOF
   # Process AD_INIT
   sed -e "/AD_INIT/s//AC_INIT([$distribution], [$dist_version], [$bug_report], [$package])/" $fname > $fname.tmp
 
+  # Remove AD_DISABLE_DEPENDENCIES
+  sed -e "/^AD_DISABLE_DEPENDENCIES/d" $fname.tmp > $fname
+
   # Process for distribution
-  rm -f $fname
-  ad_process_file $fname.tmp $fname false
+  rm -f $fname.tmp
+  ad_process_file $fname $fname.tmp false
 
   # Remove any trailing backslashes
-  if test -f "$fname"; then
-    sed -e :a -e '/\\$/N; s/[  ]*\\\n//; ta' < $fname > configure.ac
+  if test -f "$fname.tmp"; then
+    sed -e :a -e '/\\$/N; s/[  ]*\\\n//; ta' < $fname.tmp > configure.ac
   else
-    cp -p $fname.tmp configure.ac
+    cp -p $fname configure.ac
   fi
   rm -f $fname $fname.tmp
 
@@ -473,35 +479,72 @@ ad_make_makefile_ams()
   do
     ad_log "  $ff"
     fname=`echo $ff | sed s/\.ad//`
+    ad_make_makefile_am $ff $fname.am
+  done
+  ad_log ""
+
+  ad_debug "Ending creating Makefile.am files"
+}
+
+#
+# Creates Makefile.am file from the Makefile.ad file.
+#
+# Arguments: ad_make_makefile_am <src> <dst>
+#
+ad_make_makefile_am()
+{
+  local am_deps=true
+  local f=$1
+  local fname=$2
+
+  # Disable dependencies if requested
+  dc=`sed 's/^[        ]*//' < configure.ad | grep -v "^#" \
+    | grep "AD_DISABLE_DEPENDENCIES"`
+  if test "$dc" = "AD_DISABLE_DEPENDENCIES"; then
+    am_deps=false
+  fi
 
-    # Header for the Makefile.am
-  cat > $fname.am <<EOF
+  # Header for the Makefile.am
+  cat > $fname <<EOF
 # Automatically generated by Autodist $ver from Makefile.ad.  Do not edit.
-# To make changes edit the $ff file in the source tree.
+# To make changes edit the $f file in the source tree.
 
-# Source: $ff
+# Source: $f
 # Generated: `date` by `whoami`
 # Distribution: $distribution
 # License: $license
 
 EOF
 
-    # Run the distribution processing for this Makefile.ad
-    ad_debug "Processing $ff to be $fname.am"
-    ad_process_file $ff $fname.tmp false
+  # Run the distribution processing for this Makefile.ad
+  ad_debug "Processing $f to be $fname"
+  ad_process_file $f $fname.tmp false
 
-    # Remove any trailing backslashes
-    if test -f "$fname.tmp"; then
-      sed -e :a -e '/\\$/N; s/[        ]*\\\n//; ta' < $fname.tmp >> $fname.am
-    else
-      cat $ff >> $fname.am
-    fi
+  # Remove any trailing backslashes
+  if test -f "$fname.tmp"; then
+    sed -e :a -e '/\\$/N; s/[  ]*\\\n//; ta' < $fname.tmp >> $fname
+  else
+    cat $f >> $fname
+  fi
 
-    rm -f $fname.tmp
-  done
-  ad_log ""
+  # Enable dependencies if requested
+  if test x$am_deps = xtrue; then
+    # Get list of configure.ad's to get them into deps also
+    cfs=`find . -type f -name configure\*\.ad`
+    cfs=`echo $cfs | sed 's/\.\///g'`
 
-  ad_debug "Ending creating Makefile.am files"
+    cat >> $fname <<EOF
+
+# S_AD_ENABLE_DEPENDENCIES
+\$(srcdir)/Makefile.am: Makefile.ad
+       cd \$(top_srcdir) && autodist -p makefile \$(subdir)/Makefile.ad \$(subdir)/Makefile.am && cd \$(subdir)
+\$(srcdir)/configure.ac: $cfs
+       cd \$(top_srcdir) && autodist -p configure \$(top_srcdir)/configure.ad && cd \$(subdir)
+# E_AD_ENABLE_DEPENDENCIES
+EOF
+  fi
+
+  rm -f $fname.tmp
 }
 
 #
@@ -599,7 +642,7 @@ ad_dist_includes()
         d=`echo $src | sed 's,/[^/]*$,,'`
         if test "$d" != "$src" && test "$d" != "." && \
           test '!' -d $am_distdir/$d; then
-          mkdir -p $am_distdir/$d || exit 1
+          mkdir -p -- $am_distdir/$d || exit 1
         fi
       else
         # Add to different location
@@ -611,7 +654,7 @@ ad_dist_includes()
         d=`echo $dst | sed 's,/[^/]*$,,'`
         if test "$d" != "$dst" && test "$d" != "." && \
           test '!' -d $am_distdir/$d; then
-          mkdir -p $am_distdir/$d || exit 1
+          mkdir -p -- $am_distdir/$d || exit 1
         fi
       fi
 
@@ -634,7 +677,7 @@ ad_dist_includes()
          d=""
        fi
        if test '!' -d $am_distdir/$d && test "$ds" != "$d"; then
-          mkdir -p $am_distdir/$d || exit 1
+          mkdir -p -- $am_distdir/$d || exit 1
        fi
 
        if test x$3 = xtrue; then
@@ -654,7 +697,7 @@ ad_dist_includes()
           d=`echo $d | sed 's,/[^/]*$,,'`
         fi
         if test '!' -d $am_distdir/$d && test "$dst" != "$d"; then
-          mkdir -p $am_distdir/$d || exit 1
+          mkdir -p -- $am_distdir/$d || exit 1
         fi
 
        if test x$3 = xtrue; then
@@ -756,7 +799,7 @@ ad_dist_excludes()
   do
     ad_debug "Excluding $i"
     if test x$2 = xtrue; then
-      ad_log "  $1"
+      ad_log "  $i"
     fi
     rm -rf $i
   done
@@ -861,6 +904,23 @@ ad_process_source_tree()
   ad_debug "Ending processing source files: $1"
 }
 
+#
+# Removes Autodist dependencies, as they cannot be delivered to distribution.
+#
+# Arguments: ad_remove_dependencies <distdir>
+#
+ad_remove_dependencies()
+{
+  ad_debug "Removing dependencies"
+
+  ams=`find $1 -type f -name Makefile\.\*`
+  for i in $ams
+  do
+    sed -e "/^# S_AD_ENABLE_DEPENDENCIES/,/^# E_AD_ENABLE_DEPENDENCIES/d" $i > $i.tmp
+    mv $i.tmp $i
+  done
+}
+
 #
 # Makes distribution sane, ala modtimes.  Since we modify the distribution
 # we need to make it sane after that.
@@ -977,6 +1037,9 @@ ad_makedist()
     cp -p $license $am_distdir/COPYING || exit 1
   fi
 
+  # Remove dependencies
+  ad_remove_dependencies $am_distdir
+
   # Process noprocesses, first pass
   ad_process_noprocess true
 
@@ -1393,7 +1456,7 @@ ad_process_noprocess()
 
     f="$cur/autodist__noprocess"
     rm -rf $f
-    mkdir -p $f || exit 1
+    mkdir -p -- $f || exit 1
 
     # First, include them to new location with correct directory structure.
     old_am_distdir="$am_distdir"
@@ -1460,8 +1523,6 @@ ad_process_file()
     ad_process_license_header $f $1
   fi
 
-  ad_debug "Getting #ifdef's and #ifndef's"
-
   # Get defined distribution defines
   defs=`awk "/^#ifdef "$DP"_DIST_|^#else "$DP"_DIST_/ { print; }" \
     $1 |cut -d'*' -f2 |cut -d' ' -f2 | sort | uniq`
@@ -1493,9 +1554,9 @@ ad_process_file()
     # This also handles the #ifdef's #else (ie. #ifndef) branch.
     if test x$found = xfalse; then
       ad_debug "ifdef $d will be excluded (it is NOT defined)"
-      echo "/^#ifdef $d/,/^#else !$d|^#endif $d/ { next; }" >> $f
+      echo "/^#ifdef $d$/,/^#else !$d$|^#endif $d$/ { next; }" >> $f
     else
-      echo "/^#else !$d/,/^#endif $d/ { next; }" >> $f
+      echo "/^#else !$d$/,/^#endif $d$/ { next; }" >> $f
     fi
   done
 
@@ -1516,9 +1577,9 @@ ad_process_file()
     # This also handles the #ifndef's #else (ie. #ifdef) branch.
     if test x$found = xtrue; then
       ad_debug "ifndef $d will be excluded (it IS defined)"
-      echo "/^#ifndef $d/,/^#else $d|^#endif $d/ { next; }" >> $f
+      echo "/^#ifndef $d$/,/^#else $d$|^#endif $d$/ { next; }" >> $f
     else
-      echo "/^#else $d/,/^#endif $d/ { next; }" >> $f
+      echo "/^#else $d$/,/^#endif $d$/ { next; }" >> $f
     fi
   done
 
@@ -1573,8 +1634,6 @@ ad_process_source_file()
     ad_process_license_header $f $1
   fi
 
-  ad_debug "Getting #ifdef's and #ifndef's"
-
   # Get defined distribution defines
   defs=`awk '/^#ifdef SILC_DIST_|^#else \/\* SILC_DIST_/ { print; }' \
     $1 |cut -d'*' -f2 |cut -d' ' -f2 | sort | uniq`
@@ -1606,9 +1665,9 @@ ad_process_source_file()
     # This also handles the #ifdef's #else (ie. #ifndef) branch.
     if test x$found = xfalse; then
       ad_debug "ifdef $d will be excluded (it is NOT defined)"
-      echo "/^#ifdef $d/,/^#else \/\* \!$d|^#endif \/\* $d/ { next; }" >> $f
+      echo "/^#ifdef $d$/,/^#else \/\* \!$d |^#endif \/\* $d / { next; }" >> $f
     else
-      echo "/^#else \/\* \!$d/,/^#endif \/\* $d/ { next; }" >> $f
+      echo "/^#else \/\* \!$d /,/^#endif \/\* $d / { next; }" >> $f
     fi
   done
 
@@ -1629,9 +1688,9 @@ ad_process_source_file()
     # This also handles the #ifndef's #else (ie. #ifdef) branch.
     if test x$found = xtrue; then
       ad_debug "ifndef $d will be excluded (it IS defined)"
-      echo "/^#ifndef $d/,/^#else \/\* $d|^#endif \/\* $d/ { next; }" >> $f
+      echo "/^#ifndef $d$/,/^#else \/\* $d |^#endif \/\* $d / { next; }" >> $f
     else
-      echo "/^#else \/\* $d/,/^#endif \/\* $d/ { next; }" >> $f
+      echo "/^#else \/\* $d /,/^#endif \/\* $d / { next; }" >> $f
     fi
   done
 
@@ -1659,6 +1718,60 @@ ad_process_source_file()
   ad_debug "Ending processing source file: $1 $2"
 }
 
+#
+# Processes a file.  This is the -p, --process option.
+#
+# Arguments: ad_process <type> <src> <dst>
+#
+ad_process()
+{
+  ad_debug "Starting process: $1 $2 $3"
+
+  nolog=true
+
+  if test '!' -f autodist.dist; then
+    ad_fatal "Autodist has not been run yet to prepare source tree"
+  fi
+
+  # Parse distribution
+  distribution=`grep "dist:" < autodist.dist | cut -d: -f2` || exit 1
+  dist_version=`grep "ver:" < autodist.dist | cut -d: -f2` || exit 1
+  ad_parse_distribution $distribution false
+  ad_process_distdefs
+
+  # Process file
+  case "$1" in
+    makefile)
+      if test -z $3; then
+       ad_fatal "File type $1 requires <dst> argument"
+      fi
+      ad_make_makefile_am $2 $3
+      exit 0;;
+
+    configure)
+      ad_make_configure_ac $2
+      exit 0;;
+
+    non-source)
+      if test -z $3; then
+       ad_fatal "File type $1 requires <dst> argument"
+      fi
+      ad_process_file $2 $3 false
+      exit 0;;
+
+    source)
+      if test -z $3; then
+       ad_fatal "File type $1 requires <dst> argument"
+      fi
+      ad_process_source_file $2 $3 false
+      exit 0;;
+
+    *)
+      ad_fatal "Unknown file type: $1";
+      ;;
+  esac
+}
+
 #
 # Run hooks
 #
@@ -1718,11 +1831,10 @@ ad_run_dist_hooks()
 usage="Usage: autodist [options] [distribution] [version]"
 help="\
 Autodist prepares source tree for configuration, compilation and
-distribution.  Prepares the source tree from the \`autodist.ad'
-configuration file.  Generates Automake.am files from Automake.ad
-files, configure.ac file from configure.ad file(s), generates the
-configure script by running Autoconf tool, and generates Makefile.in
-files by running Automake tool.
+distribution.  Generates Automake.am files from Automake.ad files, 
+configure.ac file from configure.ad file(s), generates the configure 
+script by running Autoconf tool, and generates Makefile.in files by 
+running Automake tool.
 
 Operation modes:
   -h, --help                print this help, then exit
@@ -1732,6 +1844,10 @@ Operation modes:
   -s, --distdefs [<dist>]   print distribution defines of <dist>, then exit
   -i, --init                initialize Autodist environment, create default
                             distribution directory and distribution, then exit
+  -p, --process <type> <src> [<dst>]
+                            process file <src> into <dst> for distribution,
+                            <type> is 'makefile', 'configure', 'non-source'
+                            or 'source' and defines the type of <src>
   -m, --makedist            create and package distribution
       --gzip                create package compressed with gzip (default)
       --bzip2               create also package compressed with bzip2
@@ -1774,6 +1890,15 @@ while test $# -gt 0; do
     ad_initialize;
     exit 0;;
 
+  -p | --process)
+    shift;
+    if test $# -ge 2; then
+      ad_process $1 $2 $3
+    else
+      echo "${usage}" 1>&2;
+    fi
+    exit 0;;
+
   -m | --makedist)
     ad_makedist
     exit 0;;