2
0

autoupdate.in 33 KB

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