* doc/autoconf.texi (C Compiler): Warn that AC_C_BIGENDIAN
[autoconf/tsuna.git] / bin / autom4te.in
blob5c5af9363c479e13032c10c0ab04e60c31148214
1 #! @PERL@ -w
2 # -*- perl -*-
3 # @configure_input@
5 eval 'case $# in 0) exec @PERL@ -S "$0";; *) exec @PERL@ -S "$0" "$@";; esac'
6     if 0;
8 # autom4te - Wrapper around M4 libraries.
9 # Copyright (C) 2001, 2002, 2003, 2005, 2006, 2007 Free Software
10 # Foundation, Inc.
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2, or (at your option)
15 # any later version.
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 # 02110-1301, USA.
28 BEGIN
30   my $datadir = $ENV{'autom4te_perllibdir'} || '@datadir@';
31   unshift @INC, $datadir;
33   # Override SHELL.  On DJGPP SHELL may not be set to a shell
34   # that can handle redirection and quote arguments correctly,
35   # e.g.: COMMAND.COM.  For DJGPP always use the shell that configure
36   # has detected.
37   $ENV{'SHELL'} = '@SHELL@' if ($^O eq 'dos');
40 use Autom4te::C4che;
41 use Autom4te::ChannelDefs;
42 use Autom4te::Channels;
43 use Autom4te::FileUtils;
44 use Autom4te::General;
45 use Autom4te::XFile;
46 use File::Basename;
47 use strict;
49 # Data directory.
50 my $datadir = $ENV{'AC_MACRODIR'} || '@datadir@';
52 # $LANGUAGE{LANGUAGE} -- Automatic options for LANGUAGE.
53 my %language;
55 my $output = '-';
57 # Mode of the output file except for traces.
58 my $mode = "0666";
60 # If melt, don't use frozen files.
61 my $melt = 0;
63 # Names of the cache directory, cache directory index, trace cache
64 # prefix, and output cache prefix.  And the IO objet for the index.
65 my $cache;
66 my $icache;
67 my $tcache;
68 my $ocache;
69 my $icache_file;
71 # The macros to trace mapped to their format, as specified by the
72 # user.
73 my %trace;
75 # The macros the user will want to trace in the future.
76 # We need `include' to get the included file, `m4_pattern_forbid' and
77 # `m4_pattern_allow' to check the output.
79 # FIXME: What about `sinclude'?
80 my @preselect = ('include',
81                  'm4_pattern_allow', 'm4_pattern_forbid',
82                  '_m4_warn');
84 # M4 include path.
85 my @include;
87 # Do we freeze?
88 my $freeze = 0;
90 # $M4.
91 my $m4 = $ENV{"M4"} || '@M4@';
92 # Some non-GNU m4's don't reject the --help option, so give them /dev/null.
93 fatal "need GNU m4 1.4 or later: $m4"
94   if system "$m4 --help </dev/null 2>&1 | grep reload-state >/dev/null";
96 # Set some high recursion limit as the default limit, 250, has already
97 # been hit with AC_OUTPUT.  Don't override the user's choice.
98 $m4 .= ' --nesting-limit=1024'
99   if " $m4 " !~ / (--nesting-limit(=[0-9]+)?|-L[0-9]*) /;
102 # @M4_BUILTIN -- M4 builtins and a useful comment.
103 my @m4_builtin = `echo dumpdef | $m4 2>&1 >/dev/null`;
104 map { s/:.*//;s/\W// } @m4_builtin;
107 # %M4_BUILTIN_ALTERNATE_NAME
108 # --------------------------
109 # The builtins are renamed, e.g., `define' is renamed `m4_define'.
110 # So map `define' to `m4_define' and conversely.
111 # Some macros don't follow this scheme: be sure to properly map to their
112 # alternate name too.
114 # This is because GNU M4 1.4's tracing of builtins is buggy.  When run on
115 # this input:
117 # | divert(-1)
118 # | changequote([, ])
119 # | define([m4_eval], defn([eval]))
120 # | eval(1)
121 # | m4_eval(2)
122 # | undefine([eval])
123 # | m4_eval(3)
125 # it behaves this way:
127 # | % m4 input.m4 -da -t eval
128 # | m4trace: -1- eval(1)
129 # | m4trace: -1- m4_eval(2)
130 # | m4trace: -1- m4_eval(3)
131 # | %
133 # Conversely:
135 # | % m4 input.m4 -da -t m4_eval
136 # | %
138 # So we will merge them, i.e.  tracing `BUILTIN' or tracing
139 # `m4_BUILTIN' will be the same: tracing both, but honoring the
140 # *last* trace specification.
142 # FIXME: This is not enough: in the output `$0' will be `BUILTIN'
143 # sometimes and `m4_BUILTIN' at others.  We should return a unique name,
144 # the one specified by the user.
146 # FIXME: To be absolutely rigorous, I would say that given that we
147 # _redefine_ divert (instead of _copying_ it), divert and the like
148 # should not be part of this list.
149 my %m4_builtin_alternate_name;
150 @m4_builtin_alternate_name{"$_", "m4_$_"} = ("m4_$_", "$_")
151   foreach (grep { !/m4wrap|m4exit|dnl|ifelse|__.*__/ } @m4_builtin);
152 @m4_builtin_alternate_name{"ifelse", "m4_if"}   = ("m4_if", "ifelse");
153 @m4_builtin_alternate_name{"m4exit", "m4_exit"} = ("m4_exit", "m4exit");
154 @m4_builtin_alternate_name{"m4wrap", "m4_wrap"} = ("m4_wrap", "m4wrap");
157 # $HELP
158 # -----
159 $help = "Usage: $0 [OPTION] ... [FILES]
161 Run GNU M4 on the FILES, avoiding useless runs.  Output the traces if tracing,
162 the frozen file if freezing, otherwise the expansion of the FILES.
164 If some of the FILES are named \`FILE.m4f\' they are considered to be M4
165 frozen files of all the previous files (which are therefore not loaded).
166 If \`FILE.m4f\' is not found, then \`FILE.m4\' will be used, together with
167 all the previous files.
169 Some files may be optional, i.e., will only be processed if found in the
170 include path, but then must end in \`.m4?\';  the question mark is not part of
171 the actual file name.
173 Operation modes:
174   -h, --help               print this help, then exit
175   -V, --version            print version number, then exit
176   -v, --verbose            verbosely report processing
177   -d, --debug              don\'t remove temporary files
178   -o, --output=FILE        save output in FILE (defaults to \`-\', stdout)
179   -f, --force              don\'t rely on cached values
180   -W, --warnings=CATEGORY  report the warnings falling in CATEGORY
181   -l, --language=LANG      specify the set of M4 macros to use
182   -C, --cache=DIRECTORY    preserve results for future runs in DIRECTORY
183       --no-cache           disable the cache
184   -m, --mode=OCTAL         change the non trace output file mode (0666)
185   -M, --melt               don\'t use M4 frozen files
187 Languages include:
188   \`Autoconf\'   create Autoconf configure scripts
189   \`Autotest\'   create Autotest test suites
190   \`M4sh\'       create M4sh shell scripts
191   \`M4sugar\'    create M4sugar output
193 " . Autom4te::ChannelDefs::usage . "
195 The environment variables \`M4\' and \`WARNINGS\' are honored.
197 Library directories:
198   -B, --prepend-include=DIR  prepend directory DIR to search path
199   -I, --include=DIR          append directory DIR to search path
201 Tracing:
202   -t, --trace=MACRO      report the MACRO invocations
203   -p, --preselect=MACRO  prepare to trace MACRO in a future run
205 Freezing:
206   -F, --freeze   produce an M4 frozen state file for FILES
208 Report bugs to <bug-autoconf\@gnu.org>.
211 # $VERSION
212 # --------
213 $version =  <<"EOF";
214 autom4te (@PACKAGE_NAME@) @VERSION@
215 Copyright (C) @RELEASE_YEAR@ Free Software Foundation, Inc.
216 This is free software.  You may redistribute copies of it under the terms of
217 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
218 There is NO WARRANTY, to the extent permitted by law.
220 Written by Akim Demaille.
224 ## ---------- ##
225 ## Routines.  ##
226 ## ---------- ##
229 # $OPTION
230 # files_to_options (@FILE)
231 # ------------------------
232 # Transform Autom4te conventions (e.g., using foo.m4f to designate a frozen
233 # file) into a suitable command line for M4 (e.g., using --reload-state).
234 sub files_to_options (@)
236   my (@file) = @_;
237   my @res;
238   foreach my $file (@file)
239     {
240       if ($file =~ /\.m4f$/)
241         {
242           push @res, "--reload-state=$file";
243         }
244       else
245         {
246           push @res, $file;
247         }
248     }
249   return join ' ', @res;
253 # load_configuration ($FILE)
254 # --------------------------
255 # Load the configuration $FILE.
256 sub load_configuration ($)
258   my ($file) = @_;
259   use Text::ParseWords;
261   my $cfg = new Autom4te::XFile ($file);
262   my $lang;
263   while ($_ = $cfg->getline)
264     {
265       chomp;
266       # Comments.
267       next
268         if /^\s*(\#.*)?$/;
270       my @words = shellwords ($_);
271       my $type = shift @words;
272       if ($type eq 'begin-language:')
273         {
274           fatal "$file:$.: end-language missing for: $lang"
275             if defined $lang;
276           $lang = lc $words[0];
277         }
278       elsif ($type eq 'end-language:')
279         {
280           error "$file:$.: end-language mismatch: $lang"
281             if $lang ne lc $words[0];
282           $lang = undef;
283         }
284       elsif ($type eq 'args:')
285         {
286           fatal "$file:$.: no current language"
287             unless defined $lang;
288           push @{$language{$lang}}, @words;
289         }
290       else
291         {
292           error "$file:$.: unknown directive: $type";
293         }
294     }
298 # parse_args ()
299 # -------------
300 # Process any command line arguments.
301 sub parse_args ()
303   # We want to look for the early options, which should not be found
304   # in the configuration file.  Prepend to the user arguments.
305   # Perform this repeatedly so that we can use --language in language
306   # definitions.  Beware that there can be several --language
307   # invocations.
308   my @language;
309   do {
310     @language = ();
311     use Getopt::Long;
312     Getopt::Long::Configure ("pass_through", "permute");
313     GetOptions ("l|language=s" => \@language);
315     foreach (@language)
316       {
317         error "unknown language: $_"
318           unless exists $language{lc $_};
319         unshift @ARGV, @{$language{lc $_}};
320       }
321   } while @language;
323   # --debug is useless: it is parsed below.
324   if (exists $ENV{'AUTOM4TE_DEBUG'})
325     {
326       print STDERR "$me: concrete arguments:\n";
327       foreach my $arg (@ARGV)
328         {
329           print STDERR "| $arg\n";
330         }
331     }
333   # Process the arguments for real this time.
334   my @trace;
335   my @prepend_include;
336   parse_WARNINGS;
337   getopt
338     (
339      # Operation modes:
340      "o|output=s"   => \$output,
341      "W|warnings=s" => \&parse_warnings,
342      "m|mode=s"     => \$mode,
343      "M|melt"       => \$melt,
345      # Library directories:
346      "B|prepend-include=s" => \@prepend_include,
347      "I|include=s"         => \@include,
349      # Tracing:
350      # Using a hash for traces is seducing.  Unfortunately, upon `-t FOO',
351      # instead of mapping `FOO' to undef, Getopt maps it to `1', preventing
352      # us from distinguishing `-t FOO' from `-t FOO=1'.  So let's do it
353      # by hand.
354      "t|trace=s"     => \@trace,
355      "p|preselect=s" => \@preselect,
357      # Freezing.
358      "F|freeze" => \$freeze,
360      # Caching.
361      "C|cache=s" => \$cache,
362      "no-cache"  => sub { $cache = undef; },
363     );
365   fatal "too few arguments
366 Try `$me --help' for more information."
367     unless @ARGV;
369   # Freezing:
370   # We cannot trace at the same time (well, we can, but it sounds insane).
371   # And it implies melting: there is risk not to update properly using
372   # old frozen files, and worse yet: we could load a frozen file and
373   # refreeze it!  A sort of caching :)
374   fatal "cannot freeze and trace"
375     if $freeze && @trace;
376   $melt = 1
377     if $freeze;
379   # Names of the cache directory, cache directory index, trace cache
380   # prefix, and output cache prefix.  If the cache is not to be
381   # preserved, default to a temporary directory (automatically removed
382   # on exit).
383   $cache = $tmp
384     unless $cache;
385   $icache = "$cache/requests";
386   $tcache = "$cache/traces.";
387   $ocache = "$cache/output.";
389   # Normalize the includes: the first occurrence is enough, several is
390   # a pain since it introduces a useless difference in the path which
391   # invalidates the cache.  And strip `.' which is implicit and always
392   # first.
393   @include = grep { !/^\.$/ } uniq (reverse(@prepend_include), @include);
395   # Convert @trace to %trace, and work around the M4 builtins tracing
396   # problem.
397   # The default format is `$f:$l:$n:$%'.
398   foreach (@trace)
399     {
400       /^([^:]+)(?::(.*))?$/ms;
401       $trace{$1} = defined $2 ? $2 : '$f:$l:$n:$%';
402       $trace{$m4_builtin_alternate_name{$1}} = $trace{$1}
403         if exists $m4_builtin_alternate_name{$1};
404     }
406   # Work around the M4 builtins tracing problem for @PRESELECT.
407   push (@preselect,
408         map { $m4_builtin_alternate_name{$_} }
409         grep { exists $m4_builtin_alternate_name{$_} } @preselect);
411   # If we find frozen files, then all the files before it are
412   # discarded: the frozen file is supposed to include them all.
413   #
414   # We don't want to depend upon m4's --include to find the top level
415   # files, so we use `find_file' here.  Try to get a canonical name,
416   # as it's part of the key for caching.  And some files are optional
417   # (also handled by `find_file').
418   my @argv;
419   foreach (@ARGV)
420     {
421       if (/\.m4f$/)
422         {
423           # Frozen files are optional => pass a `?' to `find_file'.
424           my $file = find_file ("$_?", @include);
425           if (!$melt && $file)
426             {
427               @argv = ($file);
428             }
429           else
430             {
431               s/\.m4f$/.m4/;
432               push @argv, find_file ($_, @include);
433             }
434         }
435       else
436         {
437           my $file = find_file ($_, @include);
438           push @argv, $file
439             if $file;
440         }
441     }
442   @ARGV = @argv;
446 # handle_m4 ($REQ, @MACRO)
447 # ------------------------
448 # Run m4 on the input files, and save the traces on the @MACRO.
449 sub handle_m4 ($@)
451   my ($req, @macro) = @_;
453   # GNU m4 appends when using --debugfile/--error-output.
454   unlink ($tcache . $req->id . "t");
456   # Run m4.
457   #
458   # We don't output directly to the cache files, to avoid problems
459   # when we are interrupted (that leaves corrupted files).
460   xsystem ("$m4"
461            . join (' --include=', '', @include)
462            . ' --debug=aflq'
463            . (!exists $ENV{'AUTOM4TE_NO_FATAL'} ? ' --fatal-warning' : '')
464            . " @M4_DEBUGFILE@=$tcache" . $req->id . "t"
465            . join (' --trace=',   '', sort @macro)
466            . " " . files_to_options (@ARGV)
467            . " >$ocache" . $req->id . "t");
469   # Everything went ok: preserve the outputs.
470   foreach my $file (map { $_ . $req->id } ($tcache, $ocache))
471     {
472       use File::Copy;
473       move ("${file}t", "$file")
474         or fatal "cannot rename ${file}t as $file: $!";
475     }
479 # warn_forbidden ($WHERE, $WORD, %FORBIDDEN)
480 # ------------------------------------------
481 # $WORD is forbidden.  Warn with a dedicated error message if in
482 # %FORBIDDEN, otherwise, a simple `error: possibly undefined macro'
483 # will do.
484 my $first_warn_forbidden = 1;
485 sub warn_forbidden ($$%)
487   my ($where, $word, %forbidden) = @_;
488   my $message;
490   for my $re (sort keys %forbidden)
491     {
492       if ($word =~ $re)
493         {
494           $message = $forbidden{$re};
495           last;
496         }
497     }
498   $message ||= "possibly undefined macro: $word";
499   warn "$where: error: $message\n";
500   if ($first_warn_forbidden)
501     {
502       warn <<EOF;
503       If this token and others are legitimate, please use m4_pattern_allow.
504       See the Autoconf documentation.
506       $first_warn_forbidden = 0;
507     }
511 # handle_output ($REQ, $OUTPUT)
512 # -----------------------------
513 # Run m4 on the input files, perform quadrigraphs substitution, check for
514 # forbidden tokens, and save into $OUTPUT.
515 sub handle_output ($$)
517   my ($req, $output) = @_;
519   verb "creating $output";
521   # Load the forbidden/allowed patterns.
522   handle_traces ($req, "$tmp/patterns",
523                  ('m4_pattern_forbid' => 'forbid:$1:$2',
524                   'm4_pattern_allow'  => 'allow:$1'));
525   my @patterns = new Autom4te::XFile ("$tmp/patterns")->getlines;
526   chomp @patterns;
527   my %forbidden =
528     map { /^forbid:([^:]+):.+$/ => /^forbid:[^:]+:(.+)$/ } @patterns;
529   my $forbidden = join ('|', map { /^forbid:([^:]+)/ } @patterns) || "^\$";
530   my $allowed   = join ('|', map { /^allow:([^:]+)/  } @patterns) || "^\$";
532   verb "forbidden tokens: $forbidden";
533   verb "forbidden token : $_ => $forbidden{$_}"
534     foreach (sort keys %forbidden);
535   verb "allowed   tokens: $allowed";
537   # Read the (cached) raw M4 output, produce the actual result.  We
538   # have to use the 2nd arg to have Autom4te::XFile honor the third, but then
539   # stdout is to be handled by hand :(.  Don't use fdopen as it means
540   # we will close STDOUT, which we already do in END.
541   my $out = new Autom4te::XFile;
542   if ($output eq '-')
543     {
544       $out->open (">$output");
545     }
546   else
547     {
548       $out->open($output, O_CREAT | O_WRONLY | O_TRUNC, oct ($mode));
549     }
550   fatal "cannot create $output: $!"
551     unless $out;
552   my $in = new Autom4te::XFile ($ocache . $req->id);
554   my %prohibited;
555   my $res;
556   while ($_ = $in->getline)
557     {
558       s/\s+$//;
559       s/__oline__/$./g;
560       s/\@<:\@/[/g;
561       s/\@:>\@/]/g;
562       s/\@S\|\@/\$/g;
563       s/\@%:\@/#/g;
565       $res = $_;
567       # Don't complain in comments.  Well, until we have something
568       # better, don't consider `#include' etc. are comments.
569       s/\#.*//
570         unless /^\#\s*(if|include|endif|ifdef|ifndef|define)\b/;
571       foreach (split (/\W+/))
572         {
573           $prohibited{$_} = $.
574             if !/^$/ && /$forbidden/o && !/$allowed/o && ! exists $prohibited{$_};
575         }
577       # Performed *last*: the empty quadrigraph.
578       $res =~ s/\@&t\@//g;
580       print $out "$res\n";
581     }
583   # If no forbidden words, we're done.
584   return
585     if ! %prohibited;
587   # Locate the forbidden words in the last input file.
588   # This is unsatisfying but...
589   my $prohibited = '\b(' . join ('|', keys %prohibited) . ')\b';
590   my $file = new Autom4te::XFile ($ARGV[$#ARGV]);
591   $exit_code = 1;
593   while ($_ = $file->getline)
594     {
595       # Don't complain in comments.  Well, until we have something
596       # better, don't consider `#include' etc. are comments.
597       s/\#.*//
598         unless /^\#(if|include|endif|ifdef|ifndef|define)\b/;
600       # Complain once per word, but possibly several times per line.
601       while (/$prohibited/)
602         {
603           my $word = $1;
604           warn_forbidden ("$ARGV[$#ARGV]:$.", $word, %forbidden);
605           delete $prohibited{$word};
606           # If we're done, exit.
607           return
608             if ! %prohibited;
609           $prohibited = '\b(' . join ('|', keys %prohibited) . ')\b';
610         }
611     }
612   warn_forbidden ("$output:$prohibited{$_}", $_, %forbidden)
613     foreach (sort { $prohibited{$a} <=> $prohibited{$b} } keys %prohibited);
617 ## --------------------- ##
618 ## Handling the traces.  ##
619 ## --------------------- ##
622 # $M4_MACRO
623 # trace_format_to_m4 ($FORMAT)
624 # ----------------------------
625 # Convert a trace $FORMAT into a M4 trace processing macro's body.
626 sub trace_format_to_m4 ($)
628   my ($format) = @_;
629   my $underscore = $_;
630   my %escape = (# File name.
631                 'f' => '$1',
632                 # Line number.
633                 'l' => '$2',
634                 # Depth.
635                 'd' => '$3',
636                 # Name (also available as $0).
637                 'n' => '$4',
638                 # Escaped dollar.
639                 '$' => '$');
641   my $res = '';
642   $_ = $format;
643   while ($_)
644     {
645       # $n -> $(n + 4)
646       if (s/^\$(\d+)//)
647         {
648           $res .= "\$" . ($1 + 4);
649         }
650       # $x, no separator given.
651       elsif (s/^\$([fldn\$])//)
652         {
653           $res .= $escape{$1};
654         }
655       # $.x or ${sep}x.
656       elsif (s/^\$\{([^}]*)\}([@*%])//
657             || s/^\$(.?)([@*%])//)
658         {
659           # $@, list of quoted effective arguments.
660           if ($2 eq '@')
661             {
662               $res .= ']at_at([' . ($1 ? $1 : ',') . '], $@)[';
663             }
664           # $*, list of unquoted effective arguments.
665           elsif ($2 eq '*')
666             {
667               $res .= ']at_star([' . ($1 ? $1 : ',') . '], $@)[';
668             }
669           # $%, list of flattened unquoted effective arguments.
670           elsif ($2 eq '%')
671             {
672               $res .= ']at_percent([' . ($1 ? $1 : ':') . '], $@)[';
673             }
674         }
675       elsif (/^(\$.)/)
676         {
677           error "invalid escape: $1";
678         }
679       else
680         {
681           s/^([^\$]+)//;
682           $res .= $1;
683         }
684     }
686   $_ = $underscore;
687   return '[[' . $res . ']]';
691 # handle_traces($REQ, $OUTPUT, %TRACE)
692 # ------------------------------------
693 # We use M4 itself to process the traces.  But to avoid name clashes when
694 # processing the traces, the builtins are disabled, and moved into `at_'.
695 # Actually, all the low level processing macros are in `at_' (and `_at_').
696 # To avoid clashes between user macros and `at_' macros, the macros which
697 # implement tracing are in `AT_'.
699 # Having $REQ is needed to neutralize the macros which have been traced,
700 # but are not wanted now.
701 sub handle_traces ($$%)
703   my ($req, $output, %trace) = @_;
705   verb "formatting traces for `$output': " . join (', ', sort keys %trace);
707   # Processing the traces.
708   my $trace_m4 = new Autom4te::XFile (">$tmp/traces.m4");
710   $_ = <<'EOF';
711   divert(-1)
712   changequote([, ])
713   # _at_MODE(SEPARATOR, ELT1, ELT2...)
714   # ----------------------------------
715   # List the elements, separating then with SEPARATOR.
716   # MODE can be:
717   #  `at'       -- the elements are enclosed in brackets.
718   #  `star'     -- the elements are listed as are.
719   #  `percent'  -- the elements are `flattened': spaces are singled out,
720   #                and no new line remains.
721   define([_at_at],
722   [at_ifelse([$#], [1], [],
723              [$#], [2], [[[$2]]],
724              [[[$2]][$1]$0([$1], at_shift(at_shift($@)))])])
726   define([_at_percent],
727   [at_ifelse([$#], [1], [],
728              [$#], [2], [at_flatten([$2])],
729              [at_flatten([$2])[$1]$0([$1], at_shift(at_shift($@)))])])
731   define([_at_star],
732   [at_ifelse([$#], [1], [],
733              [$#], [2], [[$2]],
734              [[$2][$1]$0([$1], at_shift(at_shift($@)))])])
736   # FLATTEN quotes its result.
737   # Note that the second pattern is `newline, tab or space'.  Don't lose
738   # the tab!
739   define([at_flatten],
740   [at_patsubst(at_patsubst(at_patsubst(at_patsubst([[[[$1]]]], [\\\n]),
741                                        [[\n\t ]+], [ ]),
742                            [ *\(.\)$], [\1]),
743                [^ *\(.*\)], [[\1]])])
745   define([at_args],    [at_shift(at_shift(at_shift(at_shift(at_shift($@)))))])
746   define([at_at],      [_$0([$1], at_args($@))])
747   define([at_percent], [_$0([$1], at_args($@))])
748   define([at_star],    [_$0([$1], at_args($@))])
751   s/^  //mg;s/\\t/\t/mg;s/\\n/\n/mg;
752   print $trace_m4 $_;
754   # If you trace `define', then on `define([m4_exit], defn([m4exit])' you
755   # will produce
756   #
757   #    AT_define([m4sugar.m4], [115], [1], [define], [m4_exit], <m4exit>)
758   #
759   # Since `<m4exit>' is not quoted, the outer m4, when processing
760   # `trace.m4' will exit prematurely.  Hence, move all the builtins to
761   # the `at_' name space.
763   print $trace_m4 "# Copy the builtins.\n";
764   map { print $trace_m4 "define([at_$_], defn([$_]))\n" } @m4_builtin;
765   print $trace_m4 "\n";
767   print $trace_m4 "# Disable them.\n";
768   map { print $trace_m4 "at_undefine([$_])\n" } @m4_builtin;
769   print $trace_m4 "\n";
772   # Neutralize traces: we don't want traces of cached requests (%REQUEST).
773   print $trace_m4
774    "## -------------------------------------- ##\n",
775    "## By default neutralize all the traces.  ##\n",
776    "## -------------------------------------- ##\n",
777    "\n";
778   print $trace_m4 "at_define([AT_$_], [at_dnl])\n"
779     foreach (sort keys %{$req->macro});
780   print $trace_m4 "\n";
782   # Implement traces for current requests (%TRACE).
783   print $trace_m4
784     "## ------------------------- ##\n",
785     "## Trace processing macros.  ##\n",
786     "## ------------------------- ##\n",
787     "\n";
788   foreach (sort keys %trace)
789     {
790       # Trace request can be embed \n.
791       (my $comment = "Trace $_:$trace{$_}") =~ s/^/\# /;
792       print $trace_m4 "$comment\n";
793       print $trace_m4 "at_define([AT_$_],\n";
794       print $trace_m4 trace_format_to_m4 ($trace{$_}) . ")\n\n";
795     }
796   print $trace_m4 "\n";
798   # Reenable output.
799   print $trace_m4 "at_divert(0)at_dnl\n";
801   # Transform the traces from m4 into an m4 input file.
802   # Typically, transform:
803   #
804   # | m4trace:configure.ac:3: -1- AC_SUBST([exec_prefix], [NONE])
805   #
806   # into
807   #
808   # | AT_AC_SUBST([configure.ac], [3], [1], [AC_SUBST], [exec_prefix], [NONE])
809   #
810   # Pay attention that the file name might include colons, if under DOS
811   # for instance, so we don't use `[^:]+'.
812   my $traces = new Autom4te::XFile ($tcache . $req->id);
813   while ($_ = $traces->getline)
814     {
815       # Trace with arguments, as the example above.  We don't try
816       # to match the trailing parenthesis as it might be on a
817       # separate line.
818       s{^m4trace:(.+):(\d+): -(\d+)- ([^(]+)\((.*)$}
819        {AT_$4([$1], [$2], [$3], [$4], $5};
820       # Traces without arguments, always on a single line.
821       s{^m4trace:(.+):(\d+): -(\d+)- ([^)]*)\n$}
822        {AT_$4([$1], [$2], [$3], [$4])\n};
823       print $trace_m4 "$_";
824     }
825   $trace_m4->close;
827   my $in = new Autom4te::XFile ("$m4 $tmp/traces.m4 |");
828   my $out = new Autom4te::XFile (">$output");
830   # This is dubious: should we really transform the quadrigraphs in
831   # traces?  It might break balanced [ ] etc. in the output.  The
832   # consensus seeems to be that traces are more useful this way.
833   while ($_ = $in->getline)
834     {
835       # It makes no sense to try to transform __oline__.
836       s/\@<:\@/[/g;
837       s/\@:>\@/]/g;
838       s/\@S\|\@/\$/g;
839       s/\@%:\@/#/g;
840       s/\@&t\@//g;
841       print $out $_;
842     }
846 # $BOOL
847 # up_to_date ($REQ)
848 # -----------------
849 # Are the cache files of $REQ up to date?
850 # $REQ is `valid' if it corresponds to the request and exists, which
851 # does not mean it is up to date.  It is up to date if, in addition,
852 # its files are younger than its dependencies.
853 sub up_to_date ($)
855   my ($req) = @_;
857   return 0
858     if ! $req->valid;
860   my $tfile = $tcache . $req->id;
861   my $ofile = $ocache . $req->id;
863   # We can't answer properly if the traces are not computed since we
864   # need to know what other files were included.  Actually, if any of
865   # the cache files is missing, we are not up to date.
866   return 0
867     if ! -f $tfile || ! -f $ofile;
869   # The youngest of the cache files must be older than the oldest of
870   # the dependencies.
871   my $tmtime = mtime ($tfile);
872   my $omtime = mtime ($ofile);
873   my ($file, $mtime) = ($tmtime < $omtime
874                         ? ($ofile, $omtime) : ($tfile, $tmtime));
876   # We depend at least upon the arguments.
877   my @dep = @ARGV;
879   # Files may include others.  We can use traces since we just checked
880   # if they are available.
881   handle_traces ($req, "$tmp/dependencies",
882                  ('include'    => '$1',
883                   'm4_include' => '$1'));
884   my $deps = new Autom4te::XFile ("$tmp/dependencies");
885   while ($_ = $deps->getline)
886     {
887       chomp;
888       my $file = find_file ("$_?", @include);
889       # If a file which used to be included is no longer there, then
890       # don't say it's missing (it might no longer be included).  But
891       # of course, that cause the output to be outdated (as if the
892       # time stamp of that missing file was newer).
893       return 0
894         if ! $file;
895       push @dep, $file;
896     }
898   # If $FILE is younger than one of its dependencies, it is outdated.
899   return up_to_date_p ($file, @dep);
903 ## ---------- ##
904 ## Freezing.  ##
905 ## ---------- ##
907 # freeze ($OUTPUT)
908 # ----------------
909 sub freeze ($)
911   my ($output) = @_;
913   # When processing the file with diversion disabled, there must be no
914   # output but comments and empty lines.
915   my $result = xqx ("$m4"
916                     . ' --fatal-warning'
917                     . join (' --include=', '', @include)
918                     . ' --define=divert'
919                     . " " . files_to_options (@ARGV)
920                     . ' </dev/null');
921   $result =~ s/#.*\n//g;
922   $result =~ s/^\n//mg;
924   fatal "freezing produced output:\n$result"
925     if $result;
927   # If freezing produces output, something went wrong: a bad `divert',
928   # or an improper paren etc.
929   xsystem ("$m4"
930            . ' --fatal-warning'
931            . join (' --include=', '', @include)
932            . " --freeze-state=$output"
933            . " " . files_to_options (@ARGV)
934            . ' </dev/null');
937 ## -------------- ##
938 ## Main program.  ##
939 ## -------------- ##
941 mktmpdir ('am4t');
942 load_configuration ($ENV{'AUTOM4TE_CFG'} || "$datadir/autom4te.cfg");
943 load_configuration ("$ENV{'HOME'}/.autom4te.cfg")
944   if exists $ENV{'HOME'} && -f "$ENV{'HOME'}/.autom4te.cfg";
945 load_configuration (".autom4te.cfg")
946   if -f ".autom4te.cfg";
947 parse_args;
949 # Freezing does not involve the cache.
950 if ($freeze)
951   {
952     freeze ($output);
953     exit $exit_code;
954   }
956 # We need our cache directory.
957 if (! -d "$cache")
958   {
959     mkdir "$cache", 0755
960       or fatal "cannot create $cache: $!";
961   }
963 # Open the index for update, and lock it.  autom4te handles several
964 # files, but the index is the first and last file to be update, so
965 # locking it is sufficient.
966 $icache_file = new Autom4te::XFile $icache, O_RDWR|O_CREAT;
967 $icache_file->lock (LOCK_EX);
969 # Read the cache index if available and older than autom4te itself.
970 # If autom4te is younger, then some structures such as C4che, might
971 # have changed, which would corrupt its processing.
972 Autom4te::C4che->load ($icache_file)
973   if -f $icache && mtime ($icache) > mtime ($0);
975 # Add the new trace requests.
976 my $req = Autom4te::C4che->request ('input' => \@ARGV,
977                                     'path'  => \@include,
978                                     'macro' => [keys %trace, @preselect]);
980 # If $REQ's cache files are not up to date, or simply if the user
981 # discarded them (-f), declare it invalid.
982 $req->valid (0)
983   if $force || ! up_to_date ($req);
985 # We now know whether we can trust the Request object.  Say it.
986 verb "the trace request object is:\n" . $req->marshall;
988 # We need to run M4 if (i) the user wants it (--force), (ii) $REQ is
989 # invalid.
990 handle_m4 ($req, keys %{$req->macro})
991   if $force || ! $req->valid;
993 # Issue the warnings each time autom4te was run.
994 my $separator = "\n" . ('-' x 25) . " END OF WARNING " . ('-' x 25) . "\n\n";
995 handle_traces ($req, "$tmp/warnings",
996                ('_m4_warn' => "\$1::\$f:\$l::\$2::\$3$separator"));
997 # Swallow excessive newlines.
998 for (split (/\n*$separator\n*/o, contents ("$tmp/warnings")))
1000   # The message looks like:
1001   # | syntax::input.as:5::ouch
1002   # | ::input.as:4: baz is expanded from...
1003   # | input.as:2: bar is expanded from...
1004   # | input.as:3: foo is expanded from...
1005   # | input.as:5: the top level
1006   my ($cat, $loc, $msg, $stacktrace) = split ('::', $_, 4);
1007   msg $cat, $loc, "warning: $msg";
1008   for (split /\n/, $stacktrace)
1009     {
1010       my ($loc, $trace) = split (': ', $_, 2);
1011       msg $cat, $loc, $trace;
1012     }
1015 # Now output...
1016 if (%trace)
1017   {
1018     # Always produce traces, since even if the output is young enough,
1019     # there is no guarantee that the traces use the same *format*
1020     # (e.g., `-t FOO:foo' and `-t FOO:bar' are both using the same M4
1021     # traces, hence the M4 traces cache is usable, but its formatting
1022     # will yield different results).
1023     handle_traces ($req, $output, %trace);
1024   }
1025 else
1026   {
1027     # Actual M4 expansion, if the user wants it, or if $output is old
1028     # (STDOUT is pretty old).
1029     handle_output ($req, $output)
1030       if $force || mtime ($output) < mtime ($ocache . $req->id);
1031   }
1033 # If we ran up to here, the cache is valid.
1034 $req->valid (1);
1035 Autom4te::C4che->save ($icache_file);
1037 exit $exit_code;
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: