OpenBSD -> Open@acronym{BSD}
[autoconf/tsuna.git] / bin / autoupdate.in
blob2d6a9384b78a9795f30c746e30f5daa913438a00
1 #! @PERL@ -w
2 # -*- perl -*-
3 # @configure_input@
5 # autoupdate - modernize an Autoconf file.
6 # Copyright (C) 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
7 # Free Software Foundation, Inc.
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2, or (at your option)
12 # any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 # 02110-1301, USA.
24 # Originally written by David MacKenzie <djm@gnu.ai.mit.edu>.
25 # Rewritten by Akim Demaille <akim@freefriends.org>.
27 eval 'case $# in 0) exec @PERL@ -S "$0";; *) exec @PERL@ -S "$0" "$@";; esac'
28     if 0;
30 BEGIN
32   my $datadir = $ENV{'autom4te_perllibdir'} || '@datadir@';
33   unshift @INC, $datadir;
35   # Override SHELL.  On DJGPP SHELL may not be set to a shell
36   # that can handle redirection and quote arguments correctly,
37   # e.g.: COMMAND.COM.  For DJGPP always use the shell that configure
38   # has detected.
39   $ENV{'SHELL'} = '@SHELL@' if ($^O eq 'dos');
42 use Autom4te::ChannelDefs;
43 use Autom4te::Channels;
44 use Autom4te::Configure_ac;
45 use Autom4te::FileUtils;
46 use Autom4te::General;
47 use Autom4te::XFile;
48 use File::Basename;
49 use strict;
51 # Lib files.
52 my $autom4te = $ENV{'AUTOM4TE'} || '@bindir@/@autom4te-name@';
53 my $autoconf = "$autom4te --language=autoconf";
54 # We need to find m4sugar.
55 my @prepend_include;
56 my @include = ('@datadir@');
57 my $force = 0;
58 # m4.
59 my $m4 = $ENV{"M4"} || '@M4@';
62 # $HELP
63 # -----
64 $help = "Usage: $0 [OPTION] ...  [TEMPLATE-FILE...]
66 Update the TEMPLATE-FILE... if given, or `configure.ac' if present,
67 or else `configure.in', to the syntax of the current version of
68 Autoconf.  The original files are backed up.
70 Operation modes:
71   -h, --help                 print this help, then exit
72   -V, --version              print version number, then exit
73   -v, --verbose              verbosely report processing
74   -d, --debug                don't remove temporary files
75   -f, --force                consider all files obsolete
77 Library directories:
78   -B, --prepend-include=DIR  prepend directory DIR to search path
79   -I, --include=DIR          append directory DIR to search path
81 Report bugs to <bug-autoconf\@gnu.org>.
84 # $VERSION
85 # --------
86 $version = "autoupdate (@PACKAGE_NAME@) @VERSION@
87 Written by David J. MacKenzie and Akim Demaille.
89 Copyright (C) 2006 Free Software Foundation, Inc.
90 This is free software; see the source for copying conditions.  There is NO
91 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
94 ## ---------- ##
95 ## Routines.  ##
96 ## ---------- ##
99 # parse_args ()
100 # -------------
101 # Process any command line arguments.
102 sub parse_args ()
104   my $srcdir;
106   getopt ('I|include=s'         => \@include,
107           'B|prepend-include=s' => \@prepend_include,
108           'f|force'             => \$force);
110   if (! @ARGV)
111     {
112       my $configure_ac = require_configure_ac;
113       push @ARGV, $configure_ac;
114     }
119 # ----------------- #
120 # Autoconf macros.  #
121 # ----------------- #
123 my (%ac_macros, %au_macros, %m4_builtins);
125 # HANDLE_AUTOCONF_MACROS ()
126 # -------------------------
127 # @M4_BUILTINS -- M4 builtins and a useful comment.
128 sub handle_autoconf_macros ()
130   # Get the builtins.
131   xsystem ("echo dumpdef | $m4 2>$tmp/m4.defs >/dev/null");
132   my $m4_defs = new Autom4te::XFile "$tmp/m4.defs";
133   while ($_ = $m4_defs->getline)
134     {
135       $m4_builtins{$1} = 1
136         if /^(\w+):/;
137     }
138   $m4_defs->close;
140   my $macros = new Autom4te::XFile ("$autoconf"
141                                     . " --trace AU_DEFINE:'AU:\$f:\$1'"
142                                     . " --trace define:'AC:\$f:\$1'"
143                                     . " --melt /dev/null |");
144   while ($_ = $macros->getline)
145     {
146       chomp;
147       my ($domain, $file, $macro) = /^(AC|AU):(.*):([^:]*)$/ or next;
148       if ($domain eq "AU")
149         {
150           $au_macros{$macro} = 1;
151         }
152       elsif ($file =~ /(^|\/)m4sugar\/(m4sugar|version)\.m4$/)
153         {
154           # Add the m4sugar macros to m4_builtins.
155           $m4_builtins{$macro} = 1;
156         }
157       else
158         {
159           # Autoconf, aclocal, and m4sh macros.
160           $ac_macros{$macro} = 1;
161         }
162     }
163   $macros->close;
166   # Don't keep AU macros in @AC_MACROS.
167   delete $ac_macros{$_}
168     foreach (keys %au_macros);
169   # Don't keep M4sugar macros which are redefined by Autoconf,
170   # such as `builtin', `changequote' etc.  See autoconf/autoconf.m4.
171   delete $ac_macros{$_}
172     foreach (keys %m4_builtins);
173   error "no current Autoconf macros found"
174     unless keys %ac_macros;
175   error "no obsolete Autoconf macros found"
176     unless keys %au_macros;
178   if ($debug)
179     {
180       print STDERR "Current Autoconf macros:\n";
181       print STDERR join (' ', sort keys %ac_macros) . "\n\n";
182       print STDERR "Obsolete Autoconf macros:\n";
183       print STDERR join (' ', sort keys %au_macros) . "\n\n";
184     }
186   # ac.m4 -- autoquoting definitions of the AC macros (M4sugar excluded).
187   # unac.m4 -- undefine the AC macros.
188   my $ac_m4 = new Autom4te::XFile ">$tmp/ac.m4";
189   print $ac_m4 "# ac.m4 -- autoquoting definitions of the AC macros.\n";
190   my $unac_m4 = new Autom4te::XFile ">$tmp/unac.m4";
191   print $unac_m4 "# unac.m4 -- undefine the AC macros.\n";
192   foreach (sort keys %ac_macros)
193     {
194       print $ac_m4   "_au_m4_define([$_], [m4_if(\$#, 0, [[\$0]], [[\$0(\$\@)]])])\n";
195       print $unac_m4 "_au_m4_undefine([$_])\n";
196     }
198   # m4save.m4 -- save the m4 builtins.
199   # unm4.m4 -- disable the m4 builtins.
200   # m4.m4 -- enable the m4 builtins.
201   my $m4save_m4 = new Autom4te::XFile ">$tmp/m4save.m4";
202   print $m4save_m4 "# m4save.m4 -- save the m4 builtins.\n";
203   my $unm4_m4 = new Autom4te::XFile ">$tmp/unm4.m4";
204   print $unm4_m4 "# unm4.m4 -- disable the m4 builtins.\n";
205   my $m4_m4 = new Autom4te::XFile ">$tmp/m4.m4";
206   print $m4_m4 "# m4.m4 -- enable the m4 builtins.\n";
207   foreach (sort keys %m4_builtins)
208     {
209       print $m4save_m4 "_au__save([$_])\n";
210       print $unm4_m4   "_au__undefine([$_])\n";
211       print $m4_m4     "_au__restore([$_])\n";
212     }
216 ## -------------- ##
217 ## Main program.  ##
218 ## -------------- ##
220 parse_args;
221 $autoconf .= " --debug" if $debug;
222 $autoconf .= " --force" if $force;
223 $autoconf .= " --verbose" if $verbose;
224 $autoconf .= join (' --include=', '', @include);
225 $autoconf .= join (' --prepend-include=', '', @prepend_include);
227 mktmpdir ('au');
228 handle_autoconf_macros;
230 # $au_changequote -- enable the quote `[', `]' right before any AU macro.
231 my $au_changequote =
232   's/\b(' . join ('|', keys %au_macros) . ')\b/_au_m4_changequote([,])$1/g';
234 # au.m4 -- definitions the AU macros.
235 xsystem ("$autoconf --trace AU_DEFINE:'_au_defun(\@<:\@\$1\@:>\@,
236 \@<:\@\$2\@:>\@)' --melt /dev/null "
237         . ">$tmp/au.m4");
241 ## ------------------- ##
242 ## Process the files.  ##
243 ## ------------------- ##
245 foreach my $file (@ARGV)
246   {
247     # We need an actual file.
248     if ($file eq '-')
249       {
250         $file = "$tmp/stdin";
251         system "cat >$file";
252       }
253     elsif (! -r "$file")
254       {
255         die "$me: $file: No such file or directory";
256       }
258     # input.m4 -- m4 program to produce the updated file.
259     # Load the values, the dispatcher, neutralize m4, and the prepared
260     # input file.
261     my $input_m4 = <<\EOF;
262       divert(-1)                                            -*- Autoconf -*-
263       changequote([,])
265       # Define our special macros:
266       define([_au__defn], defn([defn]))
267       define([_au__divert], defn([divert]))
268       define([_au__include], defn([include]))
269       define([_au__undefine], defn([undefine]))
270       define([_au__save], [m4_ifdef([$1], [m4_copy([$1], [_au_$1])])])
271       define([_au__restore],
272         [_au_m4_ifdef([_au_$1],
273           [_au_m4_define([$1], _au__defn([_au_$1]))])])
275       # Set up m4sugar.
276       include(m4sugar/m4sugar.m4)
278       # Redefine __file__ to make warnings nicer; $file is replaced below.
279       m4_define([__file__], [$file])
281       # Redefine m4_location to fix the line number.
282       m4_define([m4_location], [__file__:m4_eval(__line__ - _au__first_line)])
284       # Move all the builtins into the `_au_' pseudo namespace
285       m4_include([m4save.m4])
287       # _au_defun(NAME, BODY)
288       # ---------------------
289       # Define NAME to BODY, plus AU activation/deactivation.
290       _au_m4_define([_au_defun],
291       [_au_m4_define([$1],
292       [_au_enable()dnl
293       $2[]dnl
294       _au_disable()])])
296       # Import the definition of the obsolete macros.
297       _au__include([au.m4])
300       ## ------------------------ ##
301       ## _au_enable/_au_disable.  ##
302       ## ------------------------ ##
304       # They work by pair: each time an AU macro is activated, it runs
305       # _au_enable, and at its end its runs _au_disable (see _au_defun
306       # above).  AU macros might use AU macros, which should
307       # enable/disable only for the outer AU macros.
308       #
309       # `_au_enabled' is used to this end, determining whether we really
310       # enable/disable.
313       # __au_enable
314       # -----------
315       # Reenable the builtins, m4sugar, and the autoquoting AC macros.
316       _au_m4_define([__au_enable],
317       [_au__divert(-1)
318       # Enable special characters.
319       _au_m4_changecom([#])
321       _au__include([m4.m4])
322       _au__include([ac.m4])
324       _au__divert(0)])
326       # _au_enable
327       # ----------
328       # Called at the beginning of all the obsolete macros.  If this is the
329       # outermost level, call __au_enable.
330       _au_m4_define([_au_enable],
331       [_au_m4_ifdef([_au_enabled],
332                  [],
333                  [__au_enable()])_au_dnl
334       _au_m4_pushdef([_au_enabled])])
337       # __au_disable
338       # ------------
339       # Disable the AC autoquoting macros, m4sugar, and m4.
340       _au_m4_define([__au_disable],
341       [_au__divert(-1)
342       _au__include([unac.m4])
343       _au__include([unm4.m4])
345       # Disable special characters.
346       _au_m4_changequote()
347       _au_m4_changecom()
349       _au__divert(0)])
351       # _au_disable
352       # -----------
353       # Called at the end of all the obsolete macros.  If we are at the
354       # outermost level, call __au_disable.
355       _au_m4_define([_au_disable],
356       [_au_m4_popdef([_au_enabled])_au_dnl
357       _au_m4_ifdef([_au_enabled],
358                 [],
359                 [__au_disable()])])
362       ## ------------------------------- ##
363       ## Disable, and process the file.  ##
364       ## ------------------------------- ##
365       # The AC autoquoting macros are not loaded yet, hence invoking
366       # `_au_disable' would be wrong.
367       _au__include([unm4.m4])
369       # Disable special characters, and set the first line number.
370       _au_m4_changequote()
371       _au_m4_changecom()
373       _au_m4_define(_au__first_line, _au___line__)_au__divert(0)_au_dnl
376     $input_m4 =~ s/^      //mg;
377     $input_m4 =~ s/\$file/$file/g;
379     # prepared input -- input, but reenables the quote before each AU macro.
380     open INPUT_M4, ">$tmp/input.m4"
381        or error "cannot open: $!";
382     open FILE, "<$file"
383        or error "cannot open: $!";
384     print INPUT_M4 "$input_m4";
385     while (<FILE>)
386        {
387          eval $au_changequote;
388          print INPUT_M4;
389        }
390     close FILE
391        or error "cannot close $file: $!";
392     close INPUT_M4
393        or error "cannot close $tmp/input.m4: $!";
395     # Now ask m4 to perform the update.
396     xsystem ("$m4 --include=$tmp"
397              . join (' --include=', '', reverse (@prepend_include))
398              . join (' --include=', '', @include)
399              . " $tmp/input.m4 >$tmp/updated");
400     update_file ("$tmp/updated",
401                  "$file" eq "$tmp/stdin" ? '-' : "$file");
402   }
403 exit 0;
406 #                 ## ---------------------------- ##
407 #                 ## How `autoupdate' functions.  ##
408 #                 ## ---------------------------- ##
410 # The task of `autoupdate' is not trivial: the biggest difficulty being
411 # that you must limit the changes to the parts that really need to be
412 # updated.  Finding a satisfying implementation proved to be quite hard,
413 # as this is the fifth implementation of `autoupdate'.
415 # Below, we will use a simple example of obsolete macro:
417 #     AU_DEFUN([OLD], [NEW([$1, $2], m4_eval([$1 + $2]))])
418 #     AC_DEFUN([NEW], [echo "sum($1) = $2"])
420 # the input file contains
422 #     dnl The Unbelievable Truth
423 #     OLD(1, 2)
424 #     NEW([0, 0], [0])
426 # Of course the expected output is
428 #     dnl The Unbelievable Truth
429 #     NEW([1, 2], [3])
430 #     NEW([0, 0], [0])
433 # # First implementation: sed
434 # # =========================
436 # The first implementation was only able to change the name of obsolete
437 # macros.
439 # The file `acoldnames.m4' defined the old names based on the new names.
440 # It was simple then to produce a sed script such as:
442 #     s/OLD/NEW/g
444 # Updating merely consisted in running this script on the file to
445 # update.
447 # This scheme suffers an obvious limitation: that `autoupdate' was
448 # unable to cope with new macros that just swap some of its arguments
449 # compared to the old macro.  Fortunately, that was enough to upgrade
450 # from Autoconf 1 to Autoconf 2.  (But I have no idea whether the
451 # changes in Autoconf 2 were precisely limited by this constraint.)
454 # # Second implementation: hooks
455 # # ============================
457 # The version 2.15 of Autoconf brought a vast number of changes compared
458 # to 2.13, so a solution was needed.  One could think to extend the
459 # `sed' scripts with specialized code for complex macros.  But this
460 # approach is of course full of flaws:
462 # a. the Autoconf maintainers have to write these snippets, which we
463 #    just don't want to,
465 # b. I really don't think you'll ever manage to handle the quoting of
466 #    m4 from sed.
468 # To satisfy a., let's remark that the code which implements the old
469 # features in term of the new feature is exactly the code which should
470 # replace the old code.
472 # To answer point b, as usual in the history of Autoconf, the answer, at
473 # least on the paper, is simple: m4 is the best tool to parse m4, so
474 # let's use m4.
476 # Therefore the specification is:
478 #     I want to be able to tell Autoconf, well, m4, that the macro I
479 #     am currently defining is an obsolete macro (so that the user is
480 #     warned), which code is the code to use when running autoconf,
481 #     but that the very same code has to be used when running
482 #     autoupdate.  To summarize, the interface I want is
483 #     `AU_DEFUN(OLD-NAME, NEW-CODE)'.
486 # Now for the technical details.
488 # When running autoconf, except for the warning, AU_DEFUN is basically
489 # AC_DEFUN.
491 # When running autoupdate, we want *only* OLD-NAMEs to be expanded.
492 # This obviously means that acgeneral.m4 and acspecific.m4 must not be
493 # loaded.  Nonetheless, because we want to use a rich set of m4
494 # features, m4sugar.m4 is needed.  Please note that the fact that
495 # Autoconf's macros are not loaded is positive on two points:
497 # - we do get an updated `configure.ac', not a `configure'!
499 # - the old macros are replaced by *calls* to the new-macros, not the
500 #   body of the new macros, since their body is not defined!!!
501 #   (Whoa, that's really beautiful!).
503 # Additionally we need to disable the quotes when reading the input for
504 # two reasons: first because otherwise `m4' will swallow the quotes of
505 # other macros:
507 #     NEW([1, 2], 3)
508 #     => NEW(1, 2, 3)
510 # and second, because we want to update the macro calls which are
511 # quoted, i.e., we want
513 #     FOO([OLD(1, 2)])
514 #     => FOO([NEW([1, 2], [3])])
516 # If we don't disable the quotes, only the macros called at the top
517 # level would be updated.
519 # So, let's disable the quotes.
521 # Well, not quite: m4sugar.m4 still needs to use quotes for some macros.
522 # Well, in this case, when running in autoupdate code, each macro first
523 # reestablishes the quotes, expands itself, and disables the quotes.
525 # Thinking a bit more, you realize that in fact, people may use `define'
526 # `ifelse' etc. in their files, and you certainly don't want to process
527 # them.  Another example is `dnl': you don't want to remove the
528 # comments.  You then realize you don't want exactly to import m4sugar:
529 # you want to specify when it is enabled (macros active), and disabled.
530 # m4sugar provides m4_disable/m4_enable to this end.
532 # You're getting close to it.  Now remains one task: how to handle
533 # twofold definitions?
535 # Remember that the same AU_DEFUN must be understood in two different
536 # ways, the AC way, and the AU way.
538 # One first solution is to check whether acgeneral.m4 was loaded.  But
539 # that's definitely not cute.  Another is simply to install `hooks',
540 # that is to say, to keep in some place m4 knows, late `define' to be
541 # triggered *only* in AU mode.
543 # You first think to design AU_DEFUN like this:
545 # 1. AC_DEFUN(OLD-NAME,
546 #             [Warn the user OLD-NAME is obsolete.
547 #              NEW-CODE])
549 # 2. Store for late AU binding([define(OLD_NAME,
550 #                               [Reestablish the quotes.
551 #                                NEW-CODE
552 #                                Disable the quotes.])])
554 # but this will not work: NEW-CODE has probably $1, $2 etc. and these
555 # guys will be replaced with the argument of `Store for late AU binding'
556 # when you call it.
558 # I don't think there is a means to avoid this using this technology
559 # (remember that $1 etc. are *always* expanded in m4).  You may also try
560 # to replace them with $[1] to preserve them for a later evaluation, but
561 # if `Store for late AU binding' is properly written, it will remain
562 # quoted till the end...
564 # You have to change technology.  Since the problem is that `$1'
565 # etc. should be `consumed' right away, one solution is to define now a
566 # second macro, `AU_OLD-NAME', and to install a hook than binds OLD-NAME
567 # to AU_OLD-NAME.  Then, autoupdate.m4 just need to run the hooks.  By
568 # the way, the same method was used in autoheader.
571 # # Third implementation: m4 namespaces by m4sugar
572 # # ==============================================
574 # Actually, this implementation was just a clean up of the previous
575 # implementation: instead of defining hooks by hand, m4sugar was equipped
576 # with `namespaces'.  What are they?
578 # Sometimes we want to disable some *set* of macros, and restore them
579 # later.  We provide support for this via namespaces.
581 # There are basically three characters playing this scene: defining a
582 # macro in a namespace, disabling a namespace, and restoring a namespace
583 # (i.e., all the definitions it holds).
585 # Technically, to define a MACRO in NAMESPACE means to define the macro
586 # named `NAMESPACE::MACRO' to the VALUE.  At the same time, we append
587 # `undefine(NAME)' in the macro named `m4_disable(NAMESPACE)', and
588 # similarly a binding of NAME to the value of `NAMESPACE::MACRO' in
589 # `m4_enable(NAMESPACE)'.  These mechanisms allow to bind the macro of
590 # NAMESPACE and to unbind them at will.
592 # Of course this implementation is really inefficient: m4 has to grow
593 # strings which can become quickly huge, which slows it significantly.
595 # In particular one should avoid as much as possible to use `define' for
596 # temporaries.  Now that `define' has quite a complex meaning, it is an
597 # expensive operations that should be limited to macros.  Use
598 # `m4_define' for temporaries.
600 # Private copies of the macros we used in entering / exiting the m4sugar
601 # namespace.  It is much more convenient than fighting with the renamed
602 # version of define etc.
606 # Those two implementations suffered from serious problems:
608 # - namespaces were really expensive, and incurred a major performance
609 #   loss on `autoconf' itself, not only `autoupdate'.  One solution
610 #   would have been the limit the use of namespaces to `autoupdate', but
611 #   that's again some complications on m4sugar, which really doesn't need
612 #   this.  So we wanted to get rid of the namespaces.
614 # - since the quotes were disabled, autoupdate was sometimes making
615 #   wrong guesses, for instance on:
617 #     foo([1, 2])
619 #   m4 saw 2 arguments: `[1'and `2]'.  A simple solution, somewhat
620 #   fragile, is to reestablish the quotes right before all the obsolete
621 #   macros, i.e., to use sed so that the previous text becomes
623 #     changequote([, ])foo([1, 2])
625 #   To this end, one wants to trace the definition of obsolete macros.
627 # It was there that the limitations of the namespace approach became
628 # painful: because it was a complex machinery playing a lot with the
629 # builtins of m4 (hence, quite fragile), tracing was almost impossible.
632 # So this approach was dropped.
635 # # The fourth implementation: two steps
636 # # ====================================
638 # If you drop the uses of namespaces, you no longer can compute the
639 # updated value, and replace the old call with it simultaneously.
641 # Obviously you will use m4 to compute the updated values, but you may
642 # use some other tool to achieve the replacement.  Personally, I trust
643 # nobody but m4 to parse m4, so below, m4 will perform the two tasks.
645 # How can m4 be used to replace *some* macros calls with newer values.
646 # Well, that's dead simple: m4 should learn the definitions of obsolete
647 # macros, forget its builtins, disable the quotes, and then run on the
648 # input file, which amounts to doing this:
650 #     divert(-1)dnl
651 #     changequote([, ])
652 #     define([OLD], [NEW([$1, $2], m4_eval([$1 + $2]))changequote()])
653 #     undefine([dnl])
654 #     undefine([m4_eval])
655 #     # Some more undefines...
656 #     changequote()
657 #     divert(0)dnl
658 #     dnl The Unbelievable Truth
659 #     changequote([, ])OLD(1, 2)
660 #     NEW([0, 0],
661 #         0)
663 # which will result in
665 #     dnl The Unbelievable Truth
666 #     NEW(1, 2, m4_eval(1 + 2))
667 #     NEW([0, 0],
668 #         0)
670 # Grpmh.  Two problems.  A minor problem: it would have been much better
671 # to have the `m4_eval' computed, and a major problem: you lost the
672 # quotation in the result.
674 # Let's address the big problem first.  One solution is to define any
675 # modern macro to rewrite its calls with the proper quotation, thanks to
676 # `$@'.  Again, tracing the `define's makes it possible to know which
677 # are these macros, so you input is:
679 #     divert(-1)dnl
680 #     changequote([, ])
681 #     define([OLD], [NEW([$1, $2], m4_eval([$1 + $2]))changequote()])
682 #     define([NEW], [[NEW($@)]changequote()])
683 #     undefine([dnl])
684 #     undefine([m4_eval])
685 #     # Some more undefines...
686 #     changequote()
687 #     divert(0)dnl
688 #     dnl The Unbelievable Truth
689 #     changequote([, ])OLD(1, 2)
690 #     changequote([, ])NEW([0, 0],
691 #         0)
693 # which results in
695 #     dnl The Unbelievable Truth
696 #     NEW([1, 2],[m4_eval(1 + 2)])
697 #     NEW([0, 0],[0])
699 # Our problem is solved, i.e., the first call to `NEW' is properly
700 # quoted, but introduced another problem: we changed the layout of the
701 # second calls, which can be a drama in the case of huge macro calls
702 # (think of `AC_TRY_RUN' for instance).  This example didn't show it,
703 # but we also introduced parens to macros which did not have some:
705 #     AC_INIT
706 #     => AC_INIT()
708 # No big deal for the semantics (unless the macro depends upon $#, which
709 # is bad), but the users would not be happy.
711 # Additionally, we introduced quotes that were not there before, which is
712 # OK in most cases, but could change the semantics of the file.
714 # Cruel dilemma: we do want the auto-quoting definition of `NEW' when
715 # evaluating `OLD', but we don't when we evaluate the second `NEW'.
716 # Back to namespaces?
718 # No.
721 # # Second step: replacement
722 # # ------------------------
724 # No, as announced above, we will work in two steps: in a first step we
725 # compute the updated values, and in a second step we replace them.  Our
726 # goal is something like this:
728 #     divert(-1)dnl
729 #     changequote([, ])
730 #     define([OLD], [NEW([1, 2], [3])changequote()])
731 #     undefine([dnl])
732 #     undefine([m4_eval])
733 #     # Some more undefines...
734 #     changequote()
735 #     divert(0)dnl
736 #     dnl The Unbelievable Truth
737 #     changequote([, ])OLD(1, 2)
738 #     NEW([0, 0],
739 #         0)
741 # i.e., the new value of `OLD' is precomputed using the auto-quoting
742 # definition of `NEW' and the m4 builtins.  We'll see how afterwards,
743 # let's finish with the replacement.
745 # Of course the solution above is wrong: if there were other calls to
746 # `OLD' with different values, we would smash them to the same value.
747 # But it is quite easy to generalize the scheme above:
749 #     divert(-1)dnl
750 #     changequote([, ])
751 #     define([OLD([1],[2])], [NEW([1, 2], [3])])
752 #     define([OLD], [defn([OLD($@)])changequote()])
753 #     undefine([dnl])
754 #     undefine([m4_eval])
755 #     # Some more undefines...
756 #     changequote()
757 #     divert(0)dnl
758 #     dnl The Unbelievable Truth
759 #     changequote([, ])OLD(1, 2)
760 #     NEW([0, 0],
761 #         0)
763 # i.e., for each call to obsolete macros, we build an array `call =>
764 # value', and use a macro to dispatch these values.  This results in:
766 #     dnl The Unbelievable Truth
767 #     NEW([1, 2], [3])
768 #     NEW([0, 0],
769 #         0)
771 # In French, we say `Youpi !', which you might roughly translate as
772 # `Yippee!'.
775 # # First step: computation
776 # # -----------------------
778 # Let's study the anatomy of the file, and name its sections:
780 # prologue
781 #     divert(-1)dnl
782 #     changequote([, ])
783 # values
784 #     define([OLD([1],[2])], [NEW([1, 2], [3])])
785 # dispatcher
786 #     define([OLD], [defn([OLD($@)])changequote()])
787 # disabler
788 #     undefine([dnl])
789 #     undefine([m4_eval])
790 #     # Some more undefines...
791 #     changequote()
792 #     divert(0)dnl
793 # input
794 #     dnl The Unbelievable Truth
795 #     changequote([, ])OLD(1, 2)
796 #     NEW([0, 0],
797 #         0)
800 # # Computing the `values' section
801 # # ..............................
803 # First we need to get the list of all the AU macro uses.  To this end,
804 # first get the list of all the AU macros names by tracing `AU_DEFUN' in
805 # the initialization of autoconf.  This list is computed in the file
806 # `au.txt' below.
808 # Then use this list to trace all the AU macro uses in the input.  The
809 # goal is obtain in the case of our example:
811 #     [define([OLD([1],[2])],]@<<@OLD([1],[2])@>>@[)]
813 # This is the file `values.in' below.
815 # We want to evaluate this with only the builtins (in fact m4sugar), the
816 # auto-quoting definitions of the new macros (`new.m4'), and the
817 # definition of the old macros (`old.m4').  Computing these last two
818 # files is easy: it's just a matter of using the right `--trace' option.
820 # So the content of `values.in' is:
822 #     include($autoconf_dir/m4sugar.m4)
823 #     m4_include(new.m4)
824 #     m4_include(old.m4)
825 #     divert(0)dnl
826 #     [define([OLD([1],[2])],]@<<@OLD([1],[2])@>>@[)]
828 # We run m4 on it, which yields:
830 #     define([OLD([1],[2])],@<<@NEW([1, 2], [3])@>>@)
832 # Transform `@<<@' and `@>>@' into quotes and we get
834 #     define([OLD([1],[2])],[NEW([1, 2], [3])])
836 # This is `values.m4'.
839 # # Computing the `dispatcher' section
840 # # ..................................
842 # The `prologue', and the `disabler' are simple and need no commenting.
844 # To compute the `dispatcher' (`dispatch.m4'), again, it is a simple
845 # matter of using the right `--trace'.
847 # Finally, the input is not exactly the input file, rather it is the
848 # input file with the added `changequote'.  To this end, we build
849 # `quote.sed'.
852 # # Putting it all together
853 # # .......................
855 # We build the file `input.m4' which contains:
857 #     divert(-1)dnl
858 #     changequote([, ])
859 #     include(values.m4)
860 #     include(dispatch.m4)
861 #     undefine([dnl])
862 #     undefine([eval])
863 #     # Some more undefines...
864 #     changequote()
865 #     divert(0)dnl
866 #     dnl The Unbelievable Truth
867 #     changequote([, ])OLD(1, 2)
868 #     NEW([0, 0],
869 #         0)
871 # And we just run m4 on it.  Et voila`, Monsieur !  Mais oui, mais oui.
873 # Well, there are a few additional technicalities.  For instance, we
874 # rely on `changequote', `ifelse' and `defn', but we don't want to
875 # interpret the changequotes of the user, so we simply use another name:
876 # `_au_changequote' etc.
879 # # Failure of the fourth approach
880 # # ------------------------------
882 # This approach is heavily based on traces, but then there is an obvious
883 # problem: non expanded code will never be seen.  In particular, the body
884 # of a `define' definition is not seen, so on the input
886 #         define([idem], [OLD(0, [$1])])
888 # autoupdate would never see the `OLD', and wouldn't have updated it.
889 # Worse yet, if `idem(0)' was used later, then autoupdate sees that
890 # `OLD' is used, computes the result for `OLD(0, 0)' and sets up a
891 # dispatcher for `OLD'.  Since there was no computed value for `OLD(0,
892 # [$1])', the dispatcher would have replaced with... nothing, leading
893 # to
895 #         define([idem], [])
897 # With some more thinking, you see that the two step approach is wrong,
898 # the namespace approach was much saner.
900 # But you learned a lot, in particular you realized that using traces
901 # can make it possible to simulate namespaces!
905 # # The fifth implementation: m4 namespaces by files
906 # # ================================================
908 # The fourth implementation demonstrated something unsurprising: you
909 # cannot precompute, i.e., the namespace approach was the right one.
910 # Still, we no longer want them, they're too expensive.  Let's have a
911 # look at the way it worked.
913 # When updating
915 #     dnl The Unbelievable Truth
916 #     OLD(1, 2)
917 #     NEW([0, 0], [0])
919 # you evaluate `input.m4':
921 #     divert(-1)
922 #     changequote([, ])
923 #     define([OLD],
924 #     [m4_enable()NEW([$1, $2], m4_eval([$1 + $2]))m4_disable()])
925 #     ...
926 #     m4_disable()
927 #     dnl The Unbelievable Truth
928 #     OLD(1, 2)
929 #     NEW([0, 0], [0])
931 # where `m4_disable' undefines the m4 and m4sugar, and disables the quotes
932 # and comments:
934 #     define([m4_disable],
935 #     [undefine([__file__])
936 #     ...
937 #     changecom(#)
938 #     changequote()])
940 # `m4_enable' does the converse: reestablish quotes and comments
941 # --easy--, reestablish m4sugar --easy: just load `m4sugar.m4' again-- and
942 # reenable the builtins.  This later task requires that you first save
943 # the builtins.  And BTW, the definition above of `m4_disable' cannot
944 # work: you undefined `changequote' before using it!  So you need to use
945 # your privates copies of the builtins.  Let's introduce three files for
946 # this:
948 #  `m4save.m4'
949 #    moves the m4 builtins into the `_au_' pseudo namespace,
950 #  `unm4.m4'
951 #    undefines the builtins,
952 #  `m4.m4'
953 #    restores them.
955 # So `input.m4' is:
957 #     divert(-1)
958 #     changequote([, ])
960 #     include([m4save.m4])
962 #     # Import AU.
963 #     define([OLD],
964 #     [m4_enable()NEW([$1, $2], m4_eval([$1 + $2]))m4_disable()])
966 #     define([_au_enable],
967 #     [_au_changecom([#])
968 #     _au_include([m4.m4])
969 #     _au_include(m4sugar.m4)])
971 #     define([_au_disable],
972 #     [# Disable m4sugar.
973 #     # Disable the m4 builtins.
974 #     _au_include([unm4.m4])
975 #     # 1. Disable special characters.
976 #     _au_changequote()
977 #     _au_changecom()])
979 #     m4_disable()
980 #     dnl The Unbelievable Truth
981 #     OLD(1, 2)
982 #     NEW([0, 0], [0])
984 # Based on what we learned in the fourth implementation we know that we
985 # have to enable the quotes *before* any AU macro, and we know we need
986 # to build autoquoting versions of the AC macros.  But the autoquoting
987 # AC definitions must be disabled in the rest of the file, and enabled
988 # inside AU macros.
990 # Using `autoconf --trace' it is easy to build the files
992 #   `ac.m4'
993 #     define the autoquoting AC fake macros
994 #   `disable.m4'
995 #     undefine the m4sugar and AC autoquoting macros.
996 #   `au.m4'
997 #     definitions of the AU macros (such as `OLD' above).
999 # Now, `input.m4' is:
1001 #     divert(-1)
1002 #     changequote([, ])
1004 #     include([m4save.m4])
1005 #     # Import AU.
1006 #     include([au.m4])
1008 #     define([_au_enable],
1009 #     [_au_changecom([#])
1010 #     _au_include([m4.m4])
1011 #     _au_include(m4sugar.m4)
1012 #     _au_include(ac.m4)])
1014 #     define([_au_disable],
1015 #     [_au_include([disable.m4])
1016 #     _au_include([unm4.m4])
1017 #     # 1. Disable special characters.
1018 #     _au_changequote()
1019 #     _au_changecom()])
1021 #     m4_disable()
1022 #     dnl The Unbelievable Truth
1023 #     _au_changequote([, ])OLD(1, 2)
1024 #     NEW([0, 0], [0])
1026 # Finally, version V is ready.
1028 # Well... almost.
1030 # There is a slight problem that remains: if an AU macro OUTER includes
1031 # an AU macro INNER, then _au_enable will be run when entering OUTER
1032 # and when entering INNER (not good, but not too bad yet).  But when
1033 # getting out of INNER, _au_disable will disable everything while we
1034 # were still in OUTER.  Badaboom.
1036 # Therefore _au_enable and _au_disable have to be written to work by
1037 # pairs: each _au_enable pushdef's _au_enabled, and each _au_disable
1038 # popdef's _au_enabled.  And of course _au_enable and _au_disable are
1039 # effective when _au_enabled is *not* defined.
1041 # Finally, version V' is ready.  And there is much rejoicing.  (And I
1042 # have free time again.  I think.  Yeah, right.)
1044 ### Setup "GNU" style for perl-mode and cperl-mode.
1045 ## Local Variables:
1046 ## perl-indent-level: 2
1047 ## perl-continued-statement-offset: 2
1048 ## perl-continued-brace-offset: 0
1049 ## perl-brace-offset: 0
1050 ## perl-brace-imaginary-offset: 0
1051 ## perl-label-offset: -2
1052 ## cperl-indent-level: 2
1053 ## cperl-brace-offset: 0
1054 ## cperl-continued-brace-offset: 0
1055 ## cperl-label-offset: -2
1056 ## cperl-extra-newline-before-brace: t
1057 ## cperl-merge-trailing-else: nil
1058 ## cperl-continued-statement-offset: 2
1059 ## End: