2007-06-24  Benoit Sigoure  <tsuna@lrde.epita.fr>

	* bin/autoconf.as, bin/autoheader.in, bin/autoreconf.in,
        bin/autom4te.in: New option `--clean'.
	* NEWS: Mention it.
	* bin/autoreconf.in (&run_make): New.
	(&autoreconf_current_directory): Use it.  Honor the $clean variable.
	* lib/Autom4te/General.pm ($clean): New boolean value.
	(getopt): Handle `--clean'.
	* lib/Autom4te/FileUtils.pm ($SIMPLE_BACKUP_SUFFIX): Export it.
	* tests/torture.at: Test the new feature.
	* doc/autoconf.texi (autoconf Invocation, autoreconf Invocation,
	autoheader Invocation, autom4te Invocation): Document the new option.

Index: NEWS
===================================================================
RCS file: /sources/autoconf/autoconf/NEWS,v
retrieving revision 1.430
diff -u -r1.430 NEWS
--- NEWS	21 May 2007 17:50:57 -0000	1.430
+++ NEWS	24 Jun 2007 21:41:30 -0000
@@ -1,5 +1,8 @@
 * Major changes in Autoconf 2.61b (????-??-??)
 
+** It is now possible to undo the work of autoconf, autoheader and
+   autoreconf with the --clean option.
+
 ** New macro AC_OPENMP.
 
 ** AC_C_BIGENDIAN now supports universal binaries a la Mac OS X.
Index: bin/autoconf.as
===================================================================
RCS file: /sources/autoconf/autoconf/bin/autoconf.as,v
retrieving revision 1.25
diff -u -r1.25 autoconf.as
--- bin/autoconf.as	17 May 2007 02:43:12 -0000	1.25
+++ bin/autoconf.as	24 Jun 2007 21:41:31 -0000
@@ -31,6 +31,7 @@
   -h, --help                print this help, then exit
   -V, --version             print version number, then exit
   -v, --verbose             verbosely report processing
+      --clean               remove files installed by autoconf
   -d, --debug               don't remove temporary files
   -f, --force               consider all files obsolete
   -o, --output=FILE         save output in FILE (stdout is the default)
@@ -82,6 +83,7 @@
 autom4te_options=
 outfile=
 verbose=false
+clean=false
 
 # Parse command line.
 while test $# -gt 0 ; do
@@ -99,6 +101,10 @@
        verbose=:
        autom4te_options="$autom4te_options $1"; shift ;;
 
+    --clean )
+       clean=:
+       autom4te_options="$autom4te_options $1"; shift ;;
+
     # Arguments passed as is to autom4te.
     --debug      | -d   | \
     --force      | -f   | \
@@ -163,7 +169,11 @@
       AS_ECHO(["$as_me: no input file"]) >&2
       exit 1
     fi
-    test -z "$traces" && test -z "$outfile" && outfile=configure;;
+    test -z "$traces" && test -z "$outfile" && outfile=configure
+    if $clean && test x"$outfile" != x; then
+      rm -f "$outfile"
+    fi
+    ;;
   1)
     infile=$1 ;;
   *) exec >&2
Index: bin/autoheader.in
===================================================================
RCS file: /sources/autoconf/autoconf/bin/autoheader.in,v
retrieving revision 1.147
diff -u -r1.147 autoheader.in
--- bin/autoheader.in	4 Jan 2007 16:43:06 -0000	1.147
+++ bin/autoheader.in	24 Jun 2007 21:41:31 -0000
@@ -70,6 +70,7 @@
   -h, --help               print this help, then exit
   -V, --version            print version number, then exit
   -v, --verbose            verbosely report processing
+      --clean              remove files installed by autoheader
   -d, --debug              don\'t remove temporary files
   -f, --force              consider all files obsolete
   -W, --warnings=CATEGORY  report the warnings falling in CATEGORY
@@ -195,6 +196,16 @@
 ($config_h, $config_h_in) = split (':', $config_h, 2);
 $config_h_in ||= "$config_h.in";
 
+if ($clean)
+  {
+    foreach my $f ($config_h, $config_h_in . $SIMPLE_BACKUP_SUFFIX)
+    {
+      unlink $f or error "error: Cannot remove `$f': $!"
+        unless not -f $f;
+    }
+    exit 0;
+  }
+
 # %SYMBOL might contain things like `F77_FUNC(name,NAME)', but we keep
 # only the name of the macro.
 %symbol = map { s/\(.*//; $_ => 1 } keys %symbol;
Index: bin/autom4te.in
===================================================================
RCS file: /sources/autoconf/autoconf/bin/autom4te.in,v
retrieving revision 1.107
diff -u -r1.107 autom4te.in
--- bin/autom4te.in	17 May 2007 08:20:31 -0000	1.107
+++ bin/autom4te.in	24 Jun 2007 21:41:31 -0000
@@ -174,6 +174,7 @@
   -h, --help               print this help, then exit
   -V, --version            print version number, then exit
   -v, --verbose            verbosely report processing
+      --clean              remove the cache
   -d, --debug              don\'t remove temporary files
   -o, --output=FILE        save output in FILE (defaults to \`-\', stdout)
   -f, --force              don\'t rely on cached values
@@ -333,6 +334,7 @@
   # Process the arguments for real this time.
   my @trace;
   my @prepend_include;
+  my $clean = 0;
   parse_WARNINGS;
   getopt
     (
@@ -341,6 +343,7 @@
      "W|warnings=s" => \&parse_warnings,
      "m|mode=s"     => \$mode,
      "M|melt"       => \$melt,
+     "clean"        => \$clean,
 
      # Library directories:
      "B|prepend-include=s" => \@prepend_include,
@@ -364,7 +367,7 @@
 
   fatal "too few arguments
 Try `$me --help' for more information."
-    unless @ARGV;
+    unless @ARGV or $clean;
 
   # Freezing:
   # We cannot trace at the same time (well, we can, but it sounds insane).
@@ -386,6 +389,14 @@
   $tcache = "$cache/traces.";
   $ocache = "$cache/output.";
 
+  if ($clean)
+    {
+      xsystem ("rm -rf '$cache'")
+        if -e $cache
+        and not File::Spec->file_name_is_absolute ($cache);
+      exit 0;
+    }
+
   # Normalize the includes: the first occurrence is enough, several is
   # a pain since it introduces a useless difference in the path which
   # invalidates the cache.  And strip `.' which is implicit and always
Index: bin/autoreconf.in
===================================================================
RCS file: /sources/autoconf/autoconf/bin/autoreconf.in,v
retrieving revision 1.137
diff -u -r1.137 autoreconf.in
--- bin/autoreconf.in	4 Jan 2007 16:43:06 -0000	1.137
+++ bin/autoreconf.in	24 Jun 2007 21:41:31 -0000
@@ -71,6 +71,7 @@
   -h, --help               print this help, then exit
   -V, --version            print version number, then exit
   -v, --verbose            verbosely report processing
+  -c, --clean              remove auxiliary files
   -d, --debug              don't remove temporary files
   -f, --force              consider all files obsolete
   -i, --install            copy missing auxiliary files
@@ -150,10 +152,15 @@
 	  'I|include=s'          => \@include,
 	  'B|prepend-include=s'	 => \@prepend_include,
 	  'i|install'            => \$install,
+	  # --clean is already handled by getopt, accept a short version:
+	  'c'                    => \$clean,
 	  's|symlink'            => \$symlink,
 	  'm|make'               => \$make,
 	  'recursive!'           => \$recursive);
 
+  error 'Cannot install and clean at the same time'
+    if ($install && $clean);
+
   # Split the warnings as a list of elements instead of a list of
   # lists.
   @warning = map { split /,/ } @warning;
@@ -196,6 +203,15 @@
       $automake   .= ' --copy' unless $symlink;
       $libtoolize .= ' --copy' unless $symlink;
     }
+  elsif ($clean)
+    {
+      # Don't tell autoconf and aclocal to --clean: we need them to do their
+      # work before we can actually remove their files.
+      $autoheader .= ' --clean';
+      $automake   .= ' --clean';
+      $autopoint  .= ' --clean';
+      $libtoolize .= ' --clean';
+    }
   # --force;
   if ($force)
     {
@@ -304,12 +320,50 @@
     }
 }
 
+# &run_make ([TARGET])
+# --------------------
+# Run make in the current directory.  config.status is run first
+# in order to recreate the Makefile unless we're in --clean mode.
+# Return true on success, false if something went wrong.
+sub run_make (;$)
+{
+  my ($target) = @_;
+  $target = '' if not defined $target;
+
+  # Regenerate the Makefile first (unless we're in --clean mode).
+  if (!$clean)
+    {
+      if (!-f 'config.status')
+	{
+	  verb 'no config.status: cannot re-make';
+	  return 0;
+	}
+      else
+	{
+	  xsystem ('./config.status --recheck');
+	  xsystem ('./config.status');
+	}
+    }
+  if (!-f 'Makefile')
+    {
+      verb 'no Makefile: cannot re-make' unless $clean;
+      return 0;
+    }
+  else
+    {
+      xsystem ("make $target");
+    }
+  return 1;
+}
+
 # &autoreconf_current_directory
 # -----------------------------
 sub autoreconf_current_directory ()
 {
   my $configure_ac = find_configure_ac;
 
+  run_make ('maintainer-clean') if ($make && $clean);
+
   # ---------------------- #
   # Is it using Autoconf?  #
   # ---------------------- #
@@ -345,7 +399,7 @@
   # will fail: the Gettext macros are missing.
   #
   # Therefore, we can't use the traces to decide if we use Gettext or
-  # not.  I guess that once Gettext move to 2.5x we will be able to,
+  # not.  I guess that once Gettext moves to 2.5x we will be able to,
   # but in the meanwhile forget it.
   #
   # We can only grep for AM_GNU_GETTEXT_VERSION in configure.ac.  You
@@ -359,7 +413,7 @@
     {
       verb "$configure_ac: not using Gettext";
     }
-  elsif (!$install)
+  elsif (!$install && !$clean)
     {
       verb "$configure_ac: not running autopoint: --install not given";
     }
@@ -519,20 +573,21 @@
   # Running libtoolize.  #
   # -------------------- #
 
+  if ($uses_libltdl)
+    {
+      $libtoolize .= " --ltdl";
+    }
+
   if (!$uses_libtool)
     {
       verb "$configure_ac: not using Libtool";
     }
   elsif ($install)
     {
-      if ($uses_libltdl)
-	{
-	  $libtoolize .= " --ltdl";
-	}
       xsystem ($libtoolize);
       $rerun_aclocal = 1;
     }
-  else
+  elsif (!$clean)
     {
       verb "$configure_ac: not running libtoolize: --install not given";
     }
@@ -569,6 +624,7 @@
   # latter runs the former, and (ii) autoconf is stricter than
   # autoheader.  So all in all, autoconf should give better error
   # messages.
+  $autoconf .= ' --clean' if $clean;
   xsystem ($autoconf);
 
 
@@ -588,7 +644,7 @@
     {
       verb "$configure_ac: not using Autoheader";
     }
-  else
+  elsif (!$clean)
     {
       xsystem ($autoheader);
     }
@@ -610,31 +666,24 @@
       xsystem ($automake);
     }
 
+  # ---------------------- #
+  # Finalize the cleanup.  #
+  # ---------------------- #
+
+  if ($clean)
+    {
+      xsystem ($libtoolize) if ($uses_libtool);
+      xsystem ($autoheader) if ($uses_autoheader);
+      run_aclocal ($aclocal, '--clean ' . $aclocal_flags);
+      return;
+    }
+
 
   # -------------- #
   # Running make.  #
   # -------------- #
 
-  if ($make)
-    {
-      if (!-f "config.status")
-	{
-	  verb "no config.status: cannot re-make";
-	}
-      else
-	{
-	  xsystem ("./config.status --recheck");
-	  xsystem ("./config.status");
-	  if (!-f "Makefile")
-	    {
-	      verb "no Makefile: cannot re-make";
-	    }
-	  else
-	    {
-	      xsystem ("make");
-	    }
-	}
-    }
+  run_make () if ($make);
 }
 
 
Index: doc/autoconf.texi
===================================================================
RCS file: /sources/autoconf/autoconf/doc/autoconf.texi,v
retrieving revision 1.1161
diff -u -r1.1161 autoconf.texi
--- doc/autoconf.texi	13 Jun 2007 16:52:24 -0000	1.1161
+++ doc/autoconf.texi	24 Jun 2007 21:41:38 -0000
@@ -1381,6 +1381,9 @@
 @itemx -v
 Report processing steps.
 
+@item --clean
+Remove the output file.
+
 @item --debug
 @itemx -d
 Don't remove the temporary files.
@@ -1578,6 +1581,12 @@
 commands it runs.  If given two or more times, pass @option{--verbose}
 to subordinate tools that support it.
 
+@item --clean
+@itemx -c
+Remove the auxiliary files in the package that would be installed by
+@option{--install}.  This option can be used to undo the work of
+@command{autoreconf --install}.
+
 @item --debug
 @itemx -d
 Don't remove the temporary files.
@@ -3092,6 +3101,9 @@
 @itemx -v
 Report processing steps.
 
+@item --clean
+Remove the output file.
+
 @item --debug
 @itemx -d
 Don't remove the temporary files.
@@ -9527,6 +9539,9 @@
 @itemx -v
 Report processing steps.
 
+@item --clean
+Remove the cache directory unless it is an absolut path.
+
 @item --debug
 @itemx -d
 Don't remove the temporary files and be even more verbose.
Index: lib/Autom4te/FileUtils.pm
===================================================================
RCS file: /sources/autoconf/autoconf/lib/Autom4te/FileUtils.pm,v
retrieving revision 1.12
diff -u -r1.12 FileUtils.pm
--- lib/Autom4te/FileUtils.pm	17 May 2006 02:17:58 -0000	1.12
+++ lib/Autom4te/FileUtils.pm	24 Jun 2007 21:41:38 -0000
@@ -1,4 +1,4 @@
-# Copyright (C) 2003, 2004, 2005, 2006  Free Software Foundation, Inc.
+# Copyright (C) 2003, 2004, 2005, 2006, 2007  Free Software Foundation, Inc.
 
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -49,7 +49,8 @@
 @EXPORT = qw (&contents
 	      &find_file &mtime
 	      &update_file &up_to_date_p
-	      &xsystem &xqx &dir_has_case_matching_file &reset_dir_cache);
+	      &xsystem &xqx &dir_has_case_matching_file &reset_dir_cache
+	      $SIMPLE_BACKUP_SUFFIX);
 
 
 =item C<find_file ($file_name, @include)>
@@ -119,6 +120,17 @@
 }
 
 
+=item C<$SIMPLE_BACKUP_SUFFIX>
+
+Suffix to use for files backed up.  Defaults to `~'.  Can be overridden
+with the environment variable SIMPLE_BACKUP_SUFFIX.
+
+=cut
+
+use vars qw ($SIMPLE_BACKUP_SUFFIX);
+$SIMPLE_BACKUP_SUFFIX = $ENV{'SIMPLE_BACKUP_SUFFIX'} || '~';
+
+
 =item C<update_file ($from, $to, [$force])>
 
 Rename C<$from> as C<$to>, preserving C<$to> timestamp if it has not
@@ -135,7 +147,6 @@
   my ($from, $to, $force) = @_;
   $force = 0
     unless defined $force;
-  my $SIMPLE_BACKUP_SUFFIX = $ENV{'SIMPLE_BACKUP_SUFFIX'} || '~';
   use File::Compare;
   use File::Copy;
 
Index: lib/Autom4te/General.pm
===================================================================
RCS file: /sources/autoconf/autoconf/lib/Autom4te/General.pm,v
retrieving revision 1.37
diff -u -r1.37 General.pm
--- lib/Autom4te/General.pm	25 Aug 2006 21:21:19 -0000	1.37
+++ lib/Autom4te/General.pm	24 Jun 2007 21:41:38 -0000
@@ -1,5 +1,6 @@
 # autoconf -- create `configure' using m4 macros
-# Copyright (C) 2001, 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
+# Copyright (C) 2001, 2002, 2003, 2004, 2006, 2007  Free Software
+# Foundation, Inc.
 
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -50,7 +51,7 @@
 
 # Variables we define and export.
 my @export_vars =
-  qw ($debug $force $help $me $tmp $verbose $version);
+  qw ($debug $force $clean $help $me $tmp $verbose $version);
 
 # Functions we define and export.
 my @export_subs =
@@ -94,6 +95,16 @@
 use vars qw ($force);
 $force = undef;
 
+=item C<$clean>
+
+Set this variable to 1 to remove all files that would otherwise be
+created.
+
+=cut
+
+use vars qw ($clean);
+$clean = undef;
+
 =item C<$help>
 
 Set to the help message associated to the option C<--help>.
@@ -261,6 +272,7 @@
 	     "v|verbose"  => sub { ++$verbose },
 	     "d|debug"    => sub { ++$debug },
 	     'f|force'    => \$force,
+	     'clean'      => \$clean,
 
 	     # User options last, so that they have precedence.
 	     %option);
Index: tests/torture.at
===================================================================
RCS file: /sources/autoconf/autoconf/tests/torture.at,v
retrieving revision 1.82
diff -u -r1.82 torture.at
--- tests/torture.at	12 Jun 2007 11:36:57 -0000	1.82
+++ tests/torture.at	24 Jun 2007 21:41:39 -0000
@@ -1179,3 +1179,39 @@
 AT_CHECK([test -f HeeHee.in])
 
 AT_CLEANUP
+
+## --------------------- ##
+## Unbootstrap (--clean) ##
+## --------------------- ##
+
+AT_SETUP([Unbootstrap with --clean])
+AT_KEYWORDS([autoreconf])
+
+# We use aclocal (via autoreconf).
+AT_CHECK([aclocal --version || exit 77], [], [ignore], [ignore])
+# We need a version of aclocal that is recent enough to support --clean
+AT_CHECK([aclocal --help | grep ' --clean' || exit 77], [], [ignore], [ignore])
+
+AT_DATA([configure.ac],
+[[AC_INIT
+AC_CONFIG_HEADERS(config.h:config.hin)
+AC_OUTPUT
+]])
+
+
+AT_DATA([config.hin], [])
+
+# Save the result of `find' to see whether we properly cleaned everything.
+AT_DATA([list.after], [])
+AT_DATA([list.before], [])
+
+# Save the list of files in the current directory.
+find . | sort >list.before
+
+AT_CHECK([autoreconf], [0], [ignore], [ignore])
+
+AT_CHECK([autoreconf --clean], [0], [ignore], [ignore])
+find . | sort >list.after
+AT_CMP([list.before], [list.after])
+
+AT_CLEANUP
