Fixed distdef prefix handling and processing of non-source files
[autodist.git] / apps / autodist / autodist.in
index c18051dd3a6c7eb39aa038cea7e55dc368a3dd57..b4c1d864296bf6c3e564fb5df326995783ff2cb4 100755 (executable)
@@ -2,7 +2,7 @@
 #
 # Author: Pekka Riikonen <priikone@silcnet.org>
 #
-# Copyright (C) 2005 Pekka Riikonen
+# Copyright (C) 2005 - 2007 Pekka Riikonen
 # All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
@@ -228,7 +228,7 @@ ad_initialize()
 
   # Create default distdir
   if test '!' -f $distdir; then
-    mkdir -p $distdir
+    mkdir -p -- $distdir
   fi
 
   # Create Autodist configuration file
@@ -418,8 +418,8 @@ EOF
   # Process AD_INIT
   sed -e "/AD_INIT/s//AC_INIT([$distribution], [$dist_version], [$bug_report], [$package])/" $fname > $fname.tmp
 
-  # Remove AD_ENABLE_DEPENDENCIES
-  sed -e "/^AD_ENABLE_DEPENDENCIES/d" $fname.tmp > $fname
+  # Remove AD_DISABLE_DEPENDENCIES
+  sed -e "/^AD_DISABLE_DEPENDENCIES/d" $fname.tmp > $fname
 
   # Process for distribution
   rm -f $fname.tmp
@@ -493,15 +493,15 @@ ad_make_makefile_ams()
 #
 ad_make_makefile_am()
 {
-  local am_deps=false
+  local am_deps=true
   local f=$1
   local fname=$2
 
-  # Enable dependencies if requested
+  # Disable dependencies if requested
   dc=`sed 's/^[        ]*//' < configure.ad | grep -v "^#" \
-    | grep "AD_ENABLE_DEPENDENCIES"`
-  if test "$dc" = "AD_ENABLE_DEPENDENCIES"; then
-    am_deps=true
+    | grep "AD_DISABLE_DEPENDENCIES"`
+  if test "$dc" = "AD_DISABLE_DEPENDENCIES"; then
+    am_deps=false
   fi
 
   # Header for the Makefile.am
@@ -529,11 +529,17 @@ EOF
 
   # 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'`
+
     cat >> $fname <<EOF
 
 # S_AD_ENABLE_DEPENDENCIES
 \$(srcdir)/Makefile.am: Makefile.ad
-       cd \$(top_srcdir) && autodist -p \$(subdir)/Makefile.ad \$(subdir)/Makefile.am && cd \$(subdir)
+       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
@@ -559,16 +565,19 @@ ad_process_ads()
   for i in $files
   do
     fname=`echo $i | sed s/\.ad//`
+    orig=$i
 
     ad_debug "Processing $i to be $fname"
     ad_log "  $i into $fname"
 
+    rm -f $fname
+
     # Run the distribution processing for this file
-    ad_process_file $i $fname false
-    if test -f $fname; then
-      cp -p $i $fname || exit 1
-    fi
+    ad_process_file $orig $fname false
 
+    if test '!' -f "$fname"; then
+      cp -p $orig $fname || exit 1
+    fi
   done
   ad_log ""
 
@@ -636,7 +645,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
@@ -648,7 +657,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
 
@@ -671,7 +680,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
@@ -691,7 +700,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
@@ -793,7 +802,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
@@ -827,6 +836,9 @@ ad_process_tree()
        \! -name \*\.[cC][pP][pP] -a \
        \! -name \*\.[cC]++ -a \
        \! -name \*\.m -a \
+       \! -name \*\.mm -a \
+       \! -name \*\.M -a \
+       \! -name \*\.S -a \
        \! -name \*\.[hH] -a \
        \! -name \*\.hh -a \
        \! -name \*\.[cC]\.in -a \
@@ -866,8 +878,9 @@ ad_process_source_tree()
 {
   ad_debug "Starting processing source files: $1"
 
-  # We take only C/C++ files since they use the C compiler friendly
-  # version of distdefs.  Other files are not assumed to use them.
+  # We take only C/C++ (and other files that are run through traditional
+  # preprocessor) files since they use the C compiler friendly version
+  # of distdefs.  Other files are not assumed to use them.
   files=`find $am_distdir -type f \! -name \*\.ad \( \
        -name \*\.[cC] -o \
        -name \*\.[cC][cCpP] -o \
@@ -875,6 +888,9 @@ ad_process_source_tree()
        -name \*\.[cC][pP][pP] -o \
        -name \*\.[cC]++ -o \
        -name \*\.m -o \
+       -name \*\.mm -o \
+       -name \*\.M -o \
+       -name \*\.S -o \
        -name \*\.[hH] -o \
        -name \*\.hh -o \
        -name \*\.[cC]\.in -o \
@@ -990,6 +1006,7 @@ ad_makedist()
   distribution=`grep "dist:" < autodist.dist | cut -d: -f2` || exit 1
   dist_version=`grep "ver:" < autodist.dist | cut -d: -f2` || exit 1
   am_distdir=`grep "distdir:" < autodist.dist | cut -d: -f2` || exit 1
+  params=`grep "params:" < autodist.dist | cut -d: -f2` || exit 1
   ad_parse_distribution $distribution false
   ad_log "  Distribution: $distribution $dist_version"
   ad_log "  Destination directory: $am_distdir"
@@ -1001,7 +1018,7 @@ ad_makedist()
   fi
 
   # Run pre-dist-hooks
-  ad_run_dist_hooks "$pre_dist_hooks"
+  ad_run_dist_hooks "$pre_dist_hooks" "$params"
 
   # Create distribution directory
   ad_debug "Creating distribution directory $am_distdir"
@@ -1013,7 +1030,7 @@ ad_makedist()
   fi
 
   # Run pre-process-dist-hooks
-  ad_run_dist_hooks "$pre_p_dist_hooks"
+  ad_run_dist_hooks "$pre_p_dist_hooks" "$params"
 
   # Run excludes
   ad_dist_excludes "$excludes" true
@@ -1055,7 +1072,7 @@ ad_makedist()
   ad_process_noprocess false
 
   # Run post-process_dist-hooks
-  ad_run_dist_hooks "$post_p_dist_hooks"
+  ad_run_dist_hooks "$post_p_dist_hooks" "$params"
 
   # Make distribution sane
   ad_makedist_makesane
@@ -1086,7 +1103,7 @@ ad_makedist()
   rm -f $am_distdir.tar
 
   # Run post-dist-hooks
-  ad_run_dist_hooks "$post_dist_hooks"
+  ad_run_dist_hooks "$post_dist_hooks" "$params"
 
   # Cleanup
   rm -rf $am_distdir
@@ -1175,6 +1192,15 @@ ad_parse_distribution()
     ad_fatal "Distribution '$1' is not declared"
   fi
 
+  # Get and enforce prereq version
+  prereq=`sed 's/^[    ]*//' < $distdir/$1 | grep -v "^#" \
+    | grep "prereq " | cut -d' ' -f2- | sort | uniq`
+  if test '!' -z $prereq; then
+    if test "$ver" \< "$prereq"; then
+      ad_fatal "Autodist $prereq or newer is required for distribution $1"
+    fi
+  fi
+
   # Get inherited
   inhs=`sed 's/^[      ]*//' < $distdir/$1 | grep -v "^#" \
     | grep "inherit " | cut -d' ' -f2 | sort | uniq`
@@ -1184,7 +1210,9 @@ ad_parse_distribution()
    | grep "define " | cut -d' ' -f2 | sort | uniq`
 
   if test "$inhs" = "" && test "$defs" = ""; then
-    ad_fatal "Distribution '$1' does not define anything"
+    if test "$1" != "default"; then
+      ad_fatal "Distribution '$1' does not define anything"
+    fi
   fi
 
   # Get undefined distdefs
@@ -1355,7 +1383,9 @@ ad_process_distdefs()
   do
     echo $i >>autodist.tmp.defs
   done
-  distdefs=`sort < autodist.tmp.defs | uniq`
+  if test -f autodist.tmp.defs; then
+    distdefs=`sort < autodist.tmp.defs | uniq`
+  fi
   distdefs=`echo $distdefs`
   rm -f autodist.tmp.defs
 
@@ -1450,7 +1480,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"
@@ -1518,11 +1548,11 @@ ad_process_file()
   fi
 
   # Get defined distribution defines
-  defs=`awk "/^#ifdef "$DP"_DIST_|^#else "$DP"_DIST_/ { print; }" \
+  defs=`awk "/^#ifdef ${DP}_DIST_|^#else ${DP}_DIST_/ { print; }" \
     $1 |cut -d'*' -f2 |cut -d' ' -f2 | sort | uniq`
 
   # Get explicitly not-defined distribution defines
-  ndefs=`awk "/^#ifndef "$DP"_DIST_|^#else !"$DP"_DIST_/ { print; }" \
+  ndefs=`awk "/^#ifndef ${DP}_DIST_|^#else !${DP}_DIST_/ { print; }" \
     $1 |cut -d'*' -f2 |cut -d' ' -f2 | cut -d'!' -f2 | sort | uniq`
 
   ad_debug "defs in $1: $defs"
@@ -1548,9 +1578,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
 
@@ -1571,9 +1601,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
 
@@ -1629,11 +1659,11 @@ ad_process_source_file()
   fi
 
   # Get defined distribution defines
-  defs=`awk '/^#ifdef SILC_DIST_|^#else \/\* SILC_DIST_/ { print; }' \
+  defs=`awk "/^#ifdef ${DP}_DIST_|^#else \/\* ${DP}_DIST_/ { print; }" \
     $1 |cut -d'*' -f2 |cut -d' ' -f2 | sort | uniq`
 
   # Get explicitly not-defined distribution defines
-  ndefs=`awk '/^#ifndef SILC_DIST_|^#else \/\* \!SILC_DIST_/ { print; }' \
+  ndefs=`awk "/^#ifndef ${DP}_DIST_|^#else \/\* \!${DP}_DIST_/ { print; }" \
     $1 |cut -d'*' -f2 |cut -d' ' -f2 | cut -d'!' -f2 | sort | uniq`
 
   ad_debug "defs in $1: $defs"
@@ -1659,9 +1689,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
 
@@ -1682,9 +1712,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
 
@@ -1693,7 +1723,7 @@ ad_process_source_file()
 
     # Those distdef lines that remain in the file are removed to make
     # the appearance prettier
-    echo "/^#ifdef SILC_DIST_|^#endif \/\* SILC_DIST_|^#else \/\* SILC_DIST_|^#else \/\* \!SILC_DIST_|^#ifndef SILC_DIST_/ { next; }" >> $f
+    echo "/^#ifdef "$DP"_DIST_|^#endif \/\* "$DP"_DIST_|^#else \/\* "$DP"_DIST_|^#else \/\* \!"$DP"_DIST_|^#ifndef "$DP"_DIST_/ { next; }" >> $f
     echo "{ print; }" >> $f
 
     # Execute the script
@@ -1713,13 +1743,13 @@ ad_process_source_file()
 }
 
 #
-# Processes a non-source file.  This is the -p, --process option.
+# Processes a file.  This is the -p, --process option.
 #
-# Arguments: ad_process <src> <dst>
+# Arguments: ad_process <type> <src> <dst>
 #
 ad_process()
 {
-  ad_debug "Starting process: $1 $2"
+  ad_debug "Starting process: $1 $2 $3"
 
   nolog=true
 
@@ -1729,19 +1759,47 @@ ad_process()
 
   # 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
-  ad_make_makefile_am $1 $2
+  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_debug "Ending process: $1 $2"
+    *)
+      ad_fatal "Unknown file type: $1";
+      ;;
+  esac
 }
 
 #
 # Run hooks
 #
-# Arguments: ad_run_hooks <hooks>
+# Arguments: ad_run_hooks <hooks> <params>
 #
 ad_run_hooks()
 {
@@ -1756,8 +1814,8 @@ ad_run_hooks()
     if test '!' -f $i; then
       ad_fatal "Hook script $i does not exist"
     fi
-    ad_log "  sh $i \"$distribution\" \"$dist_version\" \"$package\""
-    sh $i "$distribution" "$dist_version" "$package" || exit 1
+    ad_log "  sh $i \"$distribution\" \"$dist_version\" \"$package\" \"$2\""
+    sh $i "$distribution" "$dist_version" "$package" "$2" || exit 1
   done
   ad_log ""
 
@@ -1767,7 +1825,7 @@ ad_run_hooks()
 #
 # Run dist hooks
 #
-# Arguments: ad_run_dist_hooks <hooks>
+# Arguments: ad_run_dist_hooks <hooks> <params>
 #
 ad_run_dist_hooks()
 {
@@ -1783,8 +1841,8 @@ ad_run_dist_hooks()
       ad_fatal "Dist hook script $i does not exist"
     fi
 
-    ad_log "  sh $i \"$distribution\" \"$dist_version\" \"$package\" \"$am_distdir\""
-    sh $i "$distribution" "$dist_version" "$package" "$am_distdir" || exit 1
+    ad_log "  sh $i \"$distribution\" \"$dist_version\" \"$package\" \"$am_distdir\" \"$2\""
+    sh $i "$distribution" "$dist_version" "$package" "$am_distdir" "$2" || exit 1
   done
   ad_log ""
 
@@ -1794,14 +1852,13 @@ ad_run_dist_hooks()
 ###############################################################################
 # Autodist code
 
-usage="Usage: autodist [options] [distribution] [version]"
+usage="Usage: autodist [OPTIONS] [DISTRIBUTION] [VERSION] [PARAMS]"
 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
@@ -1811,8 +1868,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 <src> <dst> process file <src> into <dst> for distribution,
-                            only non-source files can be processed with -p
+  -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
@@ -1857,8 +1916,8 @@ while test $# -gt 0; do
 
   -p | --process)
     shift;
-    if test $# -eq 2; then
-      ad_process $1 $2
+    if test $# -ge 2; then
+      ad_process $1 $2 $3
     else
       echo "${usage}" 1>&2;
     fi
@@ -1899,7 +1958,7 @@ while test $# -gt 0; do
     echo "@PACKAGE@ (@PACKAGE_NAME@) $ver" 1>&2;
     echo "Written by Pekka Riikonen" 1>&2;
     echo 1>&2;
-    echo "Copyright (C) 2004 - 2005 SILC Project" 1>&2;
+    echo "Copyright (C) 2004 - 2007 SILC Project" 1>&2;
     echo "\
 This is free software; see the source for copying conditions.  There is NO
 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. " 1>&2;
@@ -1954,14 +2013,17 @@ ad_debug "Preparing $distribution distribution version $dist_version"
 #
 if test "$DISTDEFS"; then
   ad_create_distdefs_h
-else
-  ad_fatal "DISTDEFS not defined in $distdir/autodist.conf"
+fi
+
+# Get extra parameters from command line
+if test $# != 0; then
+  shift
 fi
 
 #
 # Run pre-hooks
 #
-ad_run_hooks "$pre_hooks"
+ad_run_hooks "$pre_hooks" "$@"
 
 #
 # Generate the Makefile.am files from Makefile.ad files
@@ -1995,11 +2057,12 @@ ad_debug "Creating autodist.dist"
 echo "dist:$distfile" > autodist.dist
 echo "ver:$dist_version" >> autodist.dist
 echo "distdir:$package-$dist_version" >> autodist.dist
+echo "params:$@" >> autodist.dist
 
 #
 # Run post-hooks
 #
-ad_run_hooks "$post_hooks"
+ad_run_hooks "$post_hooks" "$@"
 
 ad_log "Source tree processed successfully."
 ad_debug "Done, now run ./configure and make."