Initial bulk commit for "Git on MSys"
[msysgit/historical-msysgit.git] / bin / autoupdate
blobc38abf947cc1dba1c5720fefc37dc38029191574
1 #! /bin/perl -w
2 # -*- perl -*-
3 # autoupdate - modernize an Autoconf file.
4 # Copyright (C) 1994, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
9 # any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 # 02111-1307, USA.
21 # Originally written by David MacKenzie <djm@gnu.ai.mit.edu>.
22 # Rewritten by Akim Demaille <akim@freefriends.org>.
24 eval 'case $# in 0) exec /bin/perl -S "$0";; *) exec /bin/perl -S "$0" "$@";; esac'
25 if 0;
27 BEGIN
29 my $perllibdir = $ENV{'autom4te_perllibdir'} || "/usr/share/autoconf";
30 unshift @INC, "$perllibdir";
33 use File::Basename;
34 use Autom4te::General;
35 use Autom4te::XFile;
36 use strict;
38 # Lib files.
39 my $autom4te = $ENV{'AUTOM4TE'} || '/usr/bin/autom4te';
40 my $autoconf = "$autom4te --language=autoconf";
41 # We need to find m4sugar.
42 my @prepend_include;
43 my @include = ('/usr/share/autoconf');
44 my $force = 0;
45 # m4.
46 my $m4 = $ENV{"M4"} || "/bin/m4";
49 # $HELP
50 # -----
51 $help = "Usage: $0 [OPTION] ... [TEMPLATE-FILE...]
53 Update the TEMPLATE-FILE... if given, or `configure.ac' if present,
54 or else `configure.in', to the syntax of the current version of
55 Autoconf. The original files are backed up.
57 Operation modes:
58 -h, --help print this help, then exit
59 -V, --version print version number, then exit
60 -v, --verbose verbosely report processing
61 -d, --debug don't remove temporary files
62 -f, --force consider all files obsolete
64 Library directories:
65 -B, --prepend-include=DIR prepend directory DIR to search path
66 -I, --include=DIR append directory DIR to search path
68 Report bugs to <bug-autoconf\@gnu.org>.
71 # $VERSION
72 # --------
73 $version = "autoupdate (GNU Autoconf) 2.56
74 Written by David J. MacKenzie and Akim Demaille.
76 Copyright 2002 Free Software Foundation, Inc.
77 This is free software; see the source for copying conditions. There is NO
78 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
81 ## ---------- ##
82 ## Routines. ##
83 ## ---------- ##
86 # parse_args ()
87 # -------------
88 # Process any command line arguments.
89 sub parse_args ()
91 my $srcdir;
93 getopt ('I|include=s' => \@include,
94 'B|prepend-include=s' => \@prepend_include,
95 'f|force' => \$force);
97 if (! @ARGV)
99 my $configure_ac = find_configure_ac;
100 error "no input file"
101 unless $configure_ac;
102 push @ARGV, $configure_ac;
107 # ------------- #
108 # M4 builtins. #
109 # ------------- #
111 my @m4_builtins;
113 # &handle_m4_symbols ()
114 # ---------------------
115 # Create the following $tmp files:
116 # m4.m4 -- enable the m4 builtins.
117 # unm4.m4 -- disable the m4 builtins.
118 # savem4.m4 -- save the m4 builtins.
119 sub handle_m4_macros ()
121 # Get the list of builtins.
122 xsystem ("echo dumpdef | $m4 2>$tmp/m4.defs >/dev/null");
123 my $m4_defs = new Autom4te::XFile "$tmp/m4.defs";
124 while ($_ = $m4_defs->getline)
126 push @m4_builtins, $1
127 if /^(\w+):/;
129 $m4_defs->close;
131 # Output the files.
132 my $m4_m4 = new Autom4te::XFile ">$tmp/m4.m4";
133 print $m4_m4 "# m4.m4 -- enable the m4 builtins.\n";
134 my $unm4_m4 = new Autom4te::XFile ">$tmp/unm4.m4";
135 print $unm4_m4 "# unm4.m4 -- disable the m4 builtins.\n";
136 print $unm4_m4 "# Because Autoconf, via M4sugar, redefines some of these\n";
137 print $unm4_m4 "# macros, and therefore since unac.m4 disables them,\n";
138 print $unm4_m4 "# disable only if defined.\n";
139 my $m4save_m4 = new Autom4te::XFile ">$tmp/m4save.m4";
140 print $m4save_m4 "# savem4.m4 -- save the m4 builtins.\n";
141 foreach (@m4_builtins)
143 print $m4save_m4 "define([_au_$_], defn([$_]))\n";
144 print $unm4_m4 "_au_ifdef([$_], [_au_undefine([$_])])\n";
145 print $m4_m4 "_au_define([$_], _au_defn([_au_$_]))\n";
151 # ----------------- #
152 # Autoconf macros. #
153 # ----------------- #
156 # @AU_MACROS & AC_MACROS -- AU and AC macros and yet another useful comment.
157 my (%ac_macros, %au_macros);
160 # HANDLE_AUTOCONF_MACROS ()
161 # -------------------------
162 # @M4_BUILTINS -- M4 builtins and a useful comment.
163 sub handle_autoconf_macros ()
165 my $macros = new Autom4te::XFile ("$autoconf"
166 . " --trace AU_DEFUN:'AU:\$f:\$1'"
167 . " --trace define:'AC:\$f:\$1'"
168 . " --melt /dev/null |");
169 while ($_ = $macros->getline)
171 chomp;
172 my ($domain, $file, $macro) = /^(AC|AU):(.*):([^:]*)$/ or next;
173 # ../lib/m4sugar/m4sugar.m4 -> m4sugar
174 # ../lib/autoconf/general.m4 -> autoconf
175 # aclocal.m4 -> aclocal
176 my $set = basename (dirname ($file));
177 $set = 'aclocal' if $file eq 'aclocal.m4';
178 error "unknown set: $set: $_"
179 unless $set =~ /^(m4sugar|aclocal|autoconf)$/;
180 if ($domain eq "AC")
182 $ac_macros{$macro} = $set;
184 else
186 $au_macros{$macro} = $set;
189 $macros->close;
191 # Don't keep AU macros in @AC_MACROS.
192 delete $ac_macros{$_}
193 foreach (keys %au_macros);
194 # Don't keep M4sugar macros which are redefined by Autoconf,
195 # such as `builtin', `changequote' etc. See autoconf/autoconf.m4.
196 delete $ac_macros{$_}
197 foreach (@m4_builtins);
198 error "no current Autoconf macros found"
199 unless keys %ac_macros;
200 error "no obsolete Autoconf macros found"
201 unless keys %au_macros;
203 if ($debug)
205 print STDERR "Current Autoconf macros:\n";
206 print STDERR join (' ', sort keys %ac_macros) . "\n\n";
207 print STDERR "Obsolete Autoconf macros:\n";
208 print STDERR join (' ', sort keys %au_macros) . "\n\n";
211 # ac.m4 -- autoquoting definitions of the AC macros (M4sugar excluded).
212 # unac.m4 -- undefine the AC macros.
213 my $ac_m4 = new Autom4te::XFile ">$tmp/ac.m4";
214 print $ac_m4 "# ac.m4 -- autoquoting definitions of the AC macros.\n";
215 my $unac_m4 = new Autom4te::XFile ">$tmp/unac.m4";
216 print $unac_m4 "# unac.m4 -- undefine the AC macros.\n";
217 foreach (sort grep { $ac_macros{$_} ne 'm4sugar' } keys %ac_macros)
219 print $ac_m4 "_au_define([$_], [[\$0(\$\@)]])\n";
220 print $unac_m4 "_au_undefine([$_])\n";
225 ## -------------- ##
226 ## Main program. ##
227 ## -------------- ##
229 parse_args;
230 $autoconf .= " --debug" if $debug;
231 $autoconf .= " --force" if $force;
232 $autoconf .= " --verbose" if $verbose;
233 $autoconf .= join (' --include=', '', @include);
234 $autoconf .= join (' --prepend-include=', '', @prepend_include);
236 mktmpdir ('au');
237 handle_m4_macros;
238 handle_autoconf_macros;
240 # $au_changequote -- enable the quote `[', `]' right before any AU macro.
241 my $au_changequote =
242 's/\b(' . join ('|', keys %au_macros) . ')\b/_au_changequote([,])$1/g';
244 # au.m4 -- definitions the AU macros.
245 xsystem ("$autoconf --trace AU_DEFUN:'_au_defun(\@<:\@\$1\@:>\@,
246 \@<:\@\$2\@:>\@)' --melt /dev/null "
247 . ">$tmp/au.m4");
251 ## ------------------- ##
252 ## Process the files. ##
253 ## ------------------- ##
255 foreach my $file (@ARGV)
257 my $filename = $file;
258 # We need an actual file.
259 if ($file eq '-')
261 $file = "$tmp/stdin";
262 system "cat >$file";
264 elsif (! -r "$file")
266 die "$me: $file: No such file or directory";
269 # input.m4 -- m4 program to produce the updated file.
270 # Load the values, the dispatcher, neutralize m4, and the prepared
271 # input file.
272 my $input_m4 = <<\EOF;
273 divert(-1) -*- Autoconf -*-
274 changequote([, ])
276 # Move all the builtins into the `_au_' pseudo namespace
277 include([m4save.m4])
279 # _au_defun(NAME, BODY)
280 # ---------------------
281 # Define NAME to BODY, plus AU activation/deactivation.
282 _au_define([_au_defun],
283 [_au_define([$1],
284 [_au_enable()dnl
285 $2[]dnl
286 _au_disable()])])
288 # Import the definition of the obsolete macros.
289 _au_include([au.m4])
292 ## ------------------------ ##
293 ## _au_enable/_au_disable. ##
294 ## ------------------------ ##
296 # They work by pair: each time an AU macro is activated, it runs
297 # _au_enable, and at its end its runs _au_disable (see _au_defun
298 # above). AU macros might use AU macros, which should
299 # enable/disable only for the outer AU macros.
301 # `_au_enabled' is used to this end, determining whether we really
302 # enable/disable.
305 # __au_enable
306 # -----------
307 # Reenable the builtins, and m4sugar.
308 _au_define([__au_enable],
309 [_au_divert(-1)
310 # Enable special characters.
311 _au_changecom([#])
313 # Enable the m4 builtins, m4sugar and the autoquoting AC macros.
314 _au_include([m4.m4])
315 _au_include([m4sugar/m4sugar.m4])
316 _au_include([ac.m4])
318 _au_divert(0)])
320 # _au_enable
321 # ----------
322 # Called at the beginning of all the obsolete macros. Reenable the
323 # builtins, and m4sugar if needed.
324 _au_define([_au_enable],
325 [_au_ifdef([_au_enabled],
327 [__au_enable()])_au_dnl
328 _au_pushdef([_au_enabled])])
331 # __au_disable
332 # ------------
333 # Disable the builtins, and m4sugar.
334 _au_define([__au_disable],
335 [_au_divert(-1)
336 # Disable m4sugar, the AC autoquoting macros, and m4.
337 _au_include([unac.m4])
338 _au_include([unm4.m4])
340 # Disable special characters.
341 _au_changequote()
342 _au_changecom()
344 _au_divert(0)])
346 # _au_disable
347 # -----------
348 # Called at the end of all the obsolete macros. Disable the
349 # builtins, and m4sugar if needed..
350 _au_define([_au_disable],
351 [_au_popdef([_au_enabled])_au_dnl
352 _au_ifdef([_au_enabled],
354 [__au_disable()])])
357 ## ------------------------------- ##
358 ## Disable, and process the file. ##
359 ## ------------------------------- ##
360 _au_divert(-1)
361 # Disable m4: M4sugar and the AC autoquoting macros are not loaded yet,
362 # hence invoking `_au_disable' is wrong.
363 _au_include([unm4.m4])
365 # Disable special characters.
366 _au_changequote()
367 _au_changecom()
369 _au_divert(0)_au_dnl
372 $input_m4 =~ s/^ //mg;
374 # prepared input -- input, but reenables the quote before each AU macro.
375 open INPUT_M4, ">$tmp/input.m4"
376 or error "cannot open: $!";
377 open FILE, "<$file"
378 or error "cannot open: $!";
379 print INPUT_M4 "$input_m4";
380 while (<FILE>)
382 eval $au_changequote;
383 print INPUT_M4;
385 close FILE
386 or error "cannot close $file: $!";
387 close INPUT_M4
388 or error "cannot close $tmp/input.m4: $!";
390 # Now ask m4 to perform the update.
391 xsystem ("$m4 --include=$tmp"
392 . join (' --include=', '', reverse (@prepend_include))
393 . join (' --include=', '', @include)
394 . " $tmp/input.m4 >$tmp/updated");
395 update_file ("$tmp/updated",
396 "$file" eq "$tmp/stdin" ? '-' : "$file");
398 exit 0;
401 # ## ---------------------------- ##
402 # ## How `autoupdate' functions. ##
403 # ## ---------------------------- ##
405 # The task of `autoupdate' is not trivial: the biggest difficulty being
406 # that you must limit the changes to the parts that really need to be
407 # updated. Finding a satisfying implementation proved to be quite hard,
408 # as this is the fourth implementation of `autoupdate'.
410 # Below, we will use a simple example of obsolete macro:
412 # AU_DEFUN([OLD], [NEW([$1, $2], m4_eval([$1 + $2]))])
413 # AC_DEFUN([NEW], [echo "sum($1) = $2"])
415 # the input file contains
417 # dnl The Unbelievable Truth
418 # OLD(1, 2)
419 # NEW([0, 0], [0])
421 # Of course the expected output is
423 # dnl The Unbelievable Truth
424 # NEW([1, 2], [3])
425 # NEW([0, 0], [0])
428 # # First implementation: sed
429 # # =========================
431 # The first implementation was only able to change the name of obsolete
432 # macros.
434 # The file `acoldnames.m4' defined the old names based on the new names.
435 # It was simple then to produce a sed script such as:
437 # s/OLD/NEW/g
439 # Updating merely consisted in running this script on the file to
440 # update.
442 # This scheme suffers an obvious limitation: that `autoupdate' was
443 # unable to cope with new macros that just swap some of its arguments
444 # compared to the old macro. Fortunately, that was enough to upgrade
445 # from Autoconf 1 to Autoconf 2. (But I have no idea whether the
446 # changes in Autoconf 2 were precisely limited by this constraint.)
449 # # Second implementation: hooks
450 # # ============================
452 # The version 2.15 of Autoconf brought a vast number of changes compared
453 # to 2.13, so a solution was needed. One could think to extend the
454 # `sed' scripts with specialized code for complex macros. But this
455 # approach is of course full of flaws:
457 # a. the Autoconf maintainers have to write these snippets, which we
458 # just don't want to,
460 # b. I really don't think you'll ever manage to handle the quoting of
461 # m4 from sed.
463 # To satisfy a., let's remark that the code which implements the old
464 # features in term of the new feature is exactly the code which should
465 # replace the old code.
467 # To answer point b, as usual in the history of Autoconf, the answer, at
468 # least on the paper, is simple: m4 is the best tool to parse m4, so
469 # let's use m4.
471 # Therefore the specification is:
473 # I want to be able to tell Autoconf, well, m4, that the macro I
474 # am currently defining is an obsolete macro (so that the user is
475 # warned), which code is the code to use when running autoconf,
476 # but that the very same code has to be used when running
477 # autoupdate. To summarize, the interface I want is
478 # `AU_DEFUN(OLD-NAME, NEW-CODE)'.
481 # Now for the technical details.
483 # When running autoconf, except for the warning, AU_DEFUN is basically
484 # AC_DEFUN.
486 # When running autoupdate, we want *only* OLD-NAMEs to be expanded.
487 # This obviously means that acgeneral.m4 and acspecific.m4 must not be
488 # loaded. Nonetheless, because we want to use a rich set of m4
489 # features, m4sugar.m4 is needed. Please note that the fact that
490 # Autoconf's macros are not loaded is positive on two points:
492 # - we do get an updated `configure.ac', not a `configure'!
494 # - the old macros are replaced by *calls* to the new-macros, not the
495 # body of the new macros, since their body is not defined!!!
496 # (Whoa, that's really beautiful!).
498 # Additionally we need to disable the quotes when reading the input for
499 # two reasons: first because otherwise `m4' will swallow the quotes of
500 # other macros:
502 # NEW([1, 2], 3)
503 # => NEW(1, 2, 3)
505 # and second, because we want to update the macro calls which are
506 # quoted, i.e., we want
508 # FOO([OLD(1, 2)])
509 # => FOO([NEW([1, 2], [3])])
511 # If we don't disable the quotes, only the macros called at the top
512 # level would be updated.
514 # So, let's disable the quotes.
516 # Well, not quite: m4sugar.m4 still needs to use quotes for some macros.
517 # Well, in this case, when running in autoupdate code, each macro first
518 # reestablishes the quotes, expands itself, and disables the quotes.
520 # Thinking a bit more, you realize that in fact, people may use `define'
521 # `ifelse' etc. in their files, and you certainly don't want to process
522 # them. Another example is `dnl': you don't want to remove the
523 # comments. You then realize you don't want exactly to import m4sugar:
524 # you want to specify when it is enabled (macros active), and disabled.
525 # m4sugar provides m4_disable/m4_enable to this end.
527 # You're getting close to it. Now remains one task: how to handle
528 # twofold definitions?
530 # Remember that the same AU_DEFUN must be understood in two different
531 # ways, the AC way, and the AU way.
533 # One first solution is to check whether acgeneral.m4 was loaded. But
534 # that's definitely not cute. Another is simply to install `hooks',
535 # that is to say, to keep in some place m4 knows, late `define' to be
536 # triggered *only* in AU mode.
538 # You first think to design AU_DEFUN like this:
540 # 1. AC_DEFUN(OLD-NAME,
541 # [Warn the user OLD-NAME is obsolete.
542 # NEW-CODE])
544 # 2. Store for late AU binding([define(OLD_NAME,
545 # [Reestablish the quotes.
546 # NEW-CODE
547 # Disable the quotes.])])
549 # but this will not work: NEW-CODE has probably $1, $2 etc. and these
550 # guys will be replaced with the argument of `Store for late AU binding'
551 # when you call it.
553 # I don't think there is a means to avoid this using this technology
554 # (remember that $1 etc. are *always* expanded in m4). You may also try
555 # to replace them with $[1] to preserve them for a later evaluation, but
556 # if `Store for late AU binding' is properly written, it will remain
557 # quoted till the end...
559 # You have to change technology. Since the problem is that `$1'
560 # etc. should be `consumed' right away, one solution is to define now a
561 # second macro, `AU_OLD-NAME', and to install a hook than binds OLD-NAME
562 # to AU_OLD-NAME. Then, autoupdate.m4 just need to run the hooks. By
563 # the way, the same method was used in autoheader.
566 # # Third implementation: m4 namespaces by m4sugar
567 # # ==============================================
569 # Actually, this implementation was just a clean up of the previous
570 # implementation: instead of defining hooks by hand, m4sugar was equipped
571 # with `namespaces'. What are they?
573 # Sometimes we want to disable some *set* of macros, and restore them
574 # later. We provide support for this via namespaces.
576 # There are basically three characters playing this scene: defining a
577 # macro in a namespace, disabling a namespace, and restoring a namespace
578 # (i.e., all the definitions it holds).
580 # Technically, to define a MACRO in NAMESPACE means to define the macro
581 # named `NAMESPACE::MACRO' to the VALUE. At the same time, we append
582 # `undefine(NAME)' in the macro named `m4_disable(NAMESPACE)', and
583 # similarly a binding of NAME to the value of `NAMESPACE::MACRO' in
584 # `m4_enable(NAMESPACE)'. These mechanisms allow to bind the macro of
585 # NAMESPACE and to unbind them at will.
587 # Of course this implementation is really inefficient: m4 has to grow
588 # strings which can become quickly huge, which slows it significantly.
590 # In particular one should avoid as much as possible to use `define' for
591 # temporaries. Now that `define' as quite a complex meaning, it is an
592 # expensive operations that should be limited to macros. Use
593 # `m4_define' for temporaries.
595 # Private copies of the macros we used in entering / exiting the m4sugar
596 # namespace. It is much more convenient than fighting with the renamed
597 # version of define etc.
601 # Those two implementations suffered from serious problems:
603 # - namespaces were really expensive, and incurred a major performance
604 # loss on `autoconf' itself, not only `autoupdate'. One solution
605 # would have been the limit the use of namespaces to `autoupdate', but
606 # that's again some complications on m4sugar, which really doesn't need
607 # this. So we wanted to get rid of the namespaces.
609 # - since the quotes were disabled, autoupdate was sometimes making
610 # wrong guesses, for instance on:
612 # foo([1, 2])
614 # m4 saw 2 arguments: `[1'and `2]'. A simple solution, somewhat
615 # fragile, is to reestablish the quotes right before all the obsolete
616 # macros, i.e., to use sed so that the previous text becomes
618 # changequote([, ])foo([1, 2])
620 # To this end, one wants to trace the definition of obsolete macros.
622 # It was there that the limitations of the namespace approach became
623 # painful: because it was a complex machinery playing a lot with the
624 # builtins of m4 (hence, quite fragile), tracing was almost impossible.
627 # So this approach was dropped.
630 # # The fourth implementation: two steps
631 # # ====================================
633 # If you drop the uses of namespaces, you no longer can compute the
634 # updated value, and replace the old call with it simultaneously.
636 # Obviously you will use m4 to compute the updated values, but you may
637 # use some other tool to achieve the replacement. Personally, I trust
638 # nobody but m4 to parse m4, so below, m4 will perform the two tasks.
640 # How can m4 be used to replace *some* macros calls with newer values.
641 # Well, that's dead simple: m4 should learn the definitions of obsolete
642 # macros, forget its builtins, disable the quotes, and then run on the
643 # input file, which amounts to doing this:
645 # divert(-1)dnl
646 # changequote([, ])
647 # define([OLD], [NEW([$1, $2], m4_eval([$1 + $2]))changequote()])
648 # undefine([dnl])
649 # undefine([m4_eval])
650 # # Some more undefines...
651 # changequote()
652 # divert(0)dnl
653 # dnl The Unbelievable Truth
654 # changequote([, ])OLD(1, 2)
655 # NEW([0, 0],
656 # 0)
658 # which will result in
660 # dnl The Unbelievable Truth
661 # NEW(1, 2, m4_eval(1 + 2))
662 # NEW([0, 0],
663 # 0)
665 # Grpmh. Two problems. A minor problem: it would have been much better
666 # to have the `m4_eval' computed, and a major problem: you lost the
667 # quotation in the result.
669 # Let's address the big problem first. One solution is to define any
670 # modern macro to rewrite its calls with the proper quotation, thanks to
671 # `$@'. Again, tracing the `define's makes it possible to know which
672 # are these macros, so you input is:
674 # divert(-1)dnl
675 # changequote([, ])
676 # define([OLD], [NEW([$1, $2], m4_eval([$1 + $2]))changequote()])
677 # define([NEW], [[NEW($@)]changequote()])
678 # undefine([dnl])
679 # undefine([m4_eval])
680 # # Some more undefines...
681 # changequote()
682 # divert(0)dnl
683 # dnl The Unbelievable Truth
684 # changequote([, ])OLD(1, 2)
685 # changequote([, ])NEW([0, 0],
686 # 0)
688 # which results in
690 # dnl The Unbelievable Truth
691 # NEW([1, 2],[m4_eval(1 + 2)])
692 # NEW([0, 0],[0])
694 # Our problem is solved, i.e., the first call to `NEW' is properly
695 # quoted, but introduced another problem: we changed the layout of the
696 # second calls, which can be a drama in the case of huge macro calls
697 # (think of `AC_TRY_RUN' for instance). This example didn't show it,
698 # but we also introduced parens to macros which did not have some:
700 # AC_INIT
701 # => AC_INIT()
703 # No big deal for the semantics (unless the macro depends upon $#, which
704 # is bad), but the users would not be happy.
706 # Additionally, we introduced quotes that we not there before, which is
707 # OK in most cases, but could change the semantics of the file.
709 # Cruel dilemma: we do want the auto-quoting definition of `NEW' when
710 # evaluating `OLD', but we don't when we evaluate the second `NEW'.
711 # Back to namespaces?
713 # No.
716 # # Second step: replacement
717 # # ------------------------
719 # No, as announced above, we will work in two steps: in a first step we
720 # compute the updated values, and in a second step we replace them. Our
721 # goal is something like this:
723 # divert(-1)dnl
724 # changequote([, ])
725 # define([OLD], [NEW([1, 2], [3])changequote()])
726 # undefine([dnl])
727 # undefine([m4_eval])
728 # # Some more undefines...
729 # changequote()
730 # divert(0)dnl
731 # dnl The Unbelievable Truth
732 # changequote([, ])OLD(1, 2)
733 # NEW([0, 0],
734 # 0)
736 # i.e., the new value of `OLD' is precomputed using the auto-quoting
737 # definition of `NEW' and the m4 builtins. We'll see how afterwards,
738 # let's finish with the replacement.
740 # Of course the solution above is wrong: if there were other calls to
741 # `OLD' with different values, we would smash them to the same value.
742 # But it is quite easy to generalize the scheme above:
744 # divert(-1)dnl
745 # changequote([, ])
746 # define([OLD([1],[2])], [NEW([1, 2], [3])])
747 # define([OLD], [defn([OLD($@)])changequote()])
748 # undefine([dnl])
749 # undefine([m4_eval])
750 # # Some more undefines...
751 # changequote()
752 # divert(0)dnl
753 # dnl The Unbelievable Truth
754 # changequote([, ])OLD(1, 2)
755 # NEW([0, 0],
756 # 0)
758 # i.e., for each call to obsolete macros, we build an array `call =>
759 # value', and use a macro to dispatch these values. This results in:
761 # dnl The Unbelievable Truth
762 # NEW([1, 2], [3])
763 # NEW([0, 0],
764 # 0)
766 # In French, we say `Youpi !', which you might roughly translate as
767 # `Yippee!'.
770 # # First step: computation
771 # # -----------------------
773 # Let's study the anatomy of the file, and name its sections:
775 # prologue
776 # divert(-1)dnl
777 # changequote([, ])
778 # values
779 # define([OLD([1],[2])], [NEW([1, 2], [3])])
780 # dispatcher
781 # define([OLD], [defn([OLD($@)])changequote()])
782 # disabler
783 # undefine([dnl])
784 # undefine([m4_eval])
785 # # Some more undefines...
786 # changequote()
787 # divert(0)dnl
788 # input
789 # dnl The Unbelievable Truth
790 # changequote([, ])OLD(1, 2)
791 # NEW([0, 0],
792 # 0)
795 # # Computing the `values' section
796 # # ..............................
798 # First we need to get the list of all the AU macro uses. To this end,
799 # first get the list of all the AU macros names by tracing `AU_DEFUN' in
800 # the initialization of autoconf. This list is computed in the file
801 # `au.txt' below.
803 # Then use this list to trace all the AU macro uses in the input. The
804 # goal is obtain in the case of our example:
806 # [define([OLD([1],[2])],]@<<@OLD([1],[2])@>>@[)]
808 # This is the file `values.in' below.
810 # We want to evaluate this with only the builtins (in fact m4sugar), the
811 # auto-quoting definitions of the new macros (`new.m4'), and the
812 # definition of the old macros (`old.m4'). Computing these last two
813 # files is easy: it's just a matter of using the right `--trace' option.
815 # So the content of `values.in' is:
817 # include($autoconf_dir/m4sugar.m4)
818 # m4_include(new.m4)
819 # m4_include(old.m4)
820 # divert(0)dnl
821 # [define([OLD([1],[2])],]@<<@OLD([1],[2])@>>@[)]
823 # We run m4 on it, which yields:
825 # define([OLD([1],[2])],@<<@NEW([1, 2], [3])@>>@)
827 # Transform `@<<@' and `@>>@' into quotes and we get
829 # define([OLD([1],[2])],[NEW([1, 2], [3])])
831 # This is `values.m4'.
834 # # Computing the `dispatcher' section
835 # # ..................................
837 # The `prologue', and the `disabler' are simple and need no commenting.
839 # To compute the `dispatcher' (`dispatch.m4'), again, it is a simple
840 # matter of using the right `--trace'.
842 # Finally, the input is not exactly the input file, rather it is the
843 # input file with the added `changequote'. To this end, we build
844 # `quote.sed'.
847 # # Putting it all together
848 # # .......................
850 # We build the file `input.m4' which contains:
852 # divert(-1)dnl
853 # changequote([, ])
854 # include(values.m4)
855 # include(dispatch.m4)
856 # undefine([dnl])
857 # undefine([eval])
858 # # Some more undefines...
859 # changequote()
860 # divert(0)dnl
861 # dnl The Unbelievable Truth
862 # changequote([, ])OLD(1, 2)
863 # NEW([0, 0],
864 # 0)
866 # And we just run m4 on it. Et voila`, Monsieur ! Mais oui, mais oui.
868 # Well, there are a few additional technicalities. For instance, we
869 # rely on `changequote', `ifelse' and `defn', but we don't want to
870 # interpret the changequotes of the user, so we simply use another name:
871 # `_au_changequote' etc.
874 # # Failure of the fourth approach
875 # # ------------------------------
877 # This approach is heavily based on traces, but then there is an obvious
878 # problem: non expanded code will never be seen/ In particular, the body
879 # of a `define' definition is not seen, so on the input
881 # define([idem], [OLD(0, [$1])])
883 # autoupdate would never see the `OLD', and wouldn't have updated it.
884 # Worse yet, if `idem(0)' was used later, then autoupdate sees that
885 # `OLD' is used, computes the result for `OLD(0, 0)' and sets up a
886 # dispatcher for `OLD'. Since there was no computed value for `OLD(0,
887 # [$1])', the dispatcher would have replaced with... nothing, leading
888 # to
890 # define([idem], [])
892 # With some more thinking, you see that the two step approach is wrong,
893 # the namespace approach was much saner.
895 # But you learned a lot, in particular you realized that using traces
896 # can make it possible to simulate namespaces!
900 # # The fifth implementation: m4 namespaces by files
901 # # ================================================
903 # The fourth implementation demonstrated something unsurprising: you
904 # cannot precompute, i.e., the namespace approach was the right one.
905 # Still, we no longer want them, they're too expensive. Let's have a
906 # look at the way it worked.
908 # When updating
910 # dnl The Unbelievable Truth
911 # OLD(1, 2)
912 # NEW([0, 0], [0])
914 # you evaluate `input.m4':
916 # divert(-1)
917 # changequote([, ])
918 # define([OLD],
919 # [m4_enable()NEW([$1, $2], m4_eval([$1 + $2]))m4_disable()])
920 # ...
921 # m4_disable()
922 # dnl The Unbelievable Truth
923 # OLD(1, 2)
924 # NEW([0, 0], [0])
926 # where `m4_disable' undefines the m4 and m4sugar, and disables the quotes
927 # and comments:
929 # define([m4_disable],
930 # [undefine([__file__])
931 # ...
932 # changecom(#)
933 # changequote()])
935 # `m4_enable' does the converse: reestablish quotes and comments
936 # --easy--, reestablish m4sugar --easy: just load `m4sugar.m4' again-- and
937 # reenable the builtins. This later task requires that you first save
938 # the builtins. And BTW, the definition above of `m4_disable' cannot
939 # work: you undefined `changequote' before using it! So you need to use
940 # your privates copies of the builtins. Let's introduce three files for
941 # this:
943 # `m4save.m4'
944 # moves the m4 builtins into the `_au_' pseudo namespace
945 # `unm4.m4'
946 # undefines the builtins
947 # `m4.m4'
948 # restores them
950 # So `input.m4' is:
952 # divert(-1)
953 # changequote([, ])
955 # include([m4save.m4])
957 # # Import AU.
958 # define([OLD],
959 # [m4_enable()NEW([$1, $2], m4_eval([$1 + $2]))m4_disable()])
961 # define([_au_enable],
962 # [_au_changecom([#])
963 # _au_include([m4.m4])
964 # _au_include(m4sugar.m4)])
966 # define([_au_disable],
967 # [# Disable m4sugar.
968 # # Disable the m4 builtins.
969 # _au_include([unm4.m4])
970 # # 1. Disable special characters.
971 # _au_changequote()
972 # _au_changecom()])
974 # m4_disable()
975 # dnl The Unbelievable Truth
976 # OLD(1, 2)
977 # NEW([0, 0], [0])
979 # Based on what we learned in the fourth implementation we know that we
980 # have to enable the quotes *before* any AU macro, and we know we need
981 # to build autoquoting versions of the AC macros. But the autoquoting
982 # AC definitions must be disabled in the rest of the file, and enabled
983 # inside AU macros.
985 # Using `autoconf --trace' it is easy to build the files
987 # `ac.m4'
988 # define the autoquoting AC fake macros
989 # `disable.m4'
990 # undefine the m4sugar and AC autoquoting macros.
991 # `au.m4'
992 # definitions of the AU macros (such as `OLD' above).
994 # Now, `input.m4' is:
996 # divert(-1)
997 # changequote([, ])
999 # include([m4save.m4])
1000 # # Import AU.
1001 # include([au.m4])
1003 # define([_au_enable],
1004 # [_au_changecom([#])
1005 # _au_include([m4.m4])
1006 # _au_include(m4sugar.m4)
1007 # _au_include(ac.m4)])
1009 # define([_au_disable],
1010 # [_au_include([disable.m4])
1011 # _au_include([unm4.m4])
1012 # # 1. Disable special characters.
1013 # _au_changequote()
1014 # _au_changecom()])
1016 # m4_disable()
1017 # dnl The Unbelievable Truth
1018 # _au_changequote([, ])OLD(1, 2)
1019 # NEW([0, 0], [0])
1021 # Finally, version V is ready.
1023 # Well... almost.
1025 # There is a slight problem that remains: if an AU macro OUTER includes
1026 # an AU macro INNER, then _au_enable will be run when entering OUTER
1027 # and when entering INNER (not good, but not too bad yet). But when
1028 # getting out of INNER, _au_disable will disable everything while we
1029 # were still in OUTER. Badaboom.
1031 # Therefore _au_enable and _au_disable have to be written to work by
1032 # pairs: each _au_enable pushdef's _au_enabled, and each _au_disable
1033 # popdef's _au_enabled. And of course _au_enable and _au_disable are
1034 # effective when _au_enabled is *not* defined.
1036 # Finally, version V' is ready. And there is much rejoicing. (And I
1037 # have free time again. I think. Yeah, right.)
1039 ### Setup "GNU" style for perl-mode and cperl-mode.
1040 ## Local Variables:
1041 ## perl-indent-level: 2
1042 ## perl-continued-statement-offset: 2
1043 ## perl-continued-brace-offset: 0
1044 ## perl-brace-offset: 0
1045 ## perl-brace-imaginary-offset: 0
1046 ## perl-label-offset: -2
1047 ## cperl-indent-level: 2
1048 ## cperl-brace-offset: 0
1049 ## cperl-continued-brace-offset: 0
1050 ## cperl-label-offset: -2
1051 ## cperl-extra-newline-before-brace: t
1052 ## cperl-merge-trailing-else: nil
1053 ## cperl-continued-statement-offset: 2
1054 ## End: