Use a shell function for _AC_CHECK_HEADER_MONGREL.
[autoconf.git] / bin / autom4te.in
blob50c410cdcf26637ecc751360ca514df5b693b820
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, 2008 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 object 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 # FIXME: Trace status of renamed builtins was fixed in M4 1.4.5, which
115 # we now depend on; do we still need to do this mapping?
117 # So we will merge them, i.e., tracing `BUILTIN' or tracing
118 # `m4_BUILTIN' will be the same: tracing both, but honoring the
119 # *last* trace specification.
121 # FIXME: This is not enough: in the output `$0' will be `BUILTIN'
122 # sometimes and `m4_BUILTIN' at others.  We should return a unique name,
123 # the one specified by the user.
125 # FIXME: To be absolutely rigorous, I would say that given that we
126 # _redefine_ divert (instead of _copying_ it), divert and the like
127 # should not be part of this list.
128 my %m4_builtin_alternate_name;
129 @m4_builtin_alternate_name{"$_", "m4_$_"} = ("m4_$_", "$_")
130   foreach (grep { !/m4wrap|m4exit|dnl|ifelse|__.*__/ } @m4_builtin);
131 @m4_builtin_alternate_name{"ifelse", "m4_if"}   = ("m4_if", "ifelse");
132 @m4_builtin_alternate_name{"m4exit", "m4_exit"} = ("m4_exit", "m4exit");
133 @m4_builtin_alternate_name{"m4wrap", "m4_wrap"} = ("m4_wrap", "m4wrap");
136 # $HELP
137 # -----
138 $help = "Usage: $0 [OPTION]... [FILES]
140 Run GNU M4 on the FILES, avoiding useless runs.  Output the traces if tracing,
141 the frozen file if freezing, otherwise the expansion of the FILES.
143 If some of the FILES are named \`FILE.m4f\' they are considered to be M4
144 frozen files of all the previous files (which are therefore not loaded).
145 If \`FILE.m4f\' is not found, then \`FILE.m4\' will be used, together with
146 all the previous files.
148 Some files may be optional, i.e., will only be processed if found in the
149 include path, but then must end in \`.m4?\';  the question mark is not part of
150 the actual file name.
152 Operation modes:
153   -h, --help               print this help, then exit
154   -V, --version            print version number, then exit
155   -v, --verbose            verbosely report processing
156   -d, --debug              don\'t remove temporary files
157   -o, --output=FILE        save output in FILE (defaults to \`-\', stdout)
158   -f, --force              don\'t rely on cached values
159   -W, --warnings=CATEGORY  report the warnings falling in CATEGORY
160   -l, --language=LANG      specify the set of M4 macros to use
161   -C, --cache=DIRECTORY    preserve results for future runs in DIRECTORY
162       --no-cache           disable the cache
163   -m, --mode=OCTAL         change the non trace output file mode (0666)
164   -M, --melt               don\'t use M4 frozen files
166 Languages include:
167   \`Autoconf\'   create Autoconf configure scripts
168   \`Autotest\'   create Autotest test suites
169   \`M4sh\'       create M4sh shell scripts
170   \`M4sugar\'    create M4sugar output
172 " . Autom4te::ChannelDefs::usage . "
174 The environment variables \`M4\' and \`WARNINGS\' are honored.
176 Library directories:
177   -B, --prepend-include=DIR  prepend directory DIR to search path
178   -I, --include=DIR          append directory DIR to search path
180 Tracing:
181   -t, --trace=MACRO[:FORMAT]  report the MACRO invocations
182   -p, --preselect=MACRO       prepare to trace MACRO in a future run
184 Freezing:
185   -F, --freeze   produce an M4 frozen state file for FILES
187 FORMAT defaults to \`\$f:\$l:\$n:\$%\', and can use the following escapes:
188   \$\$     literal \$
189   \$f     file where macro was called
190   \$l     line where macro was called
191   \$d     nesting depth of macro call
192   \$n     name of the macro
193   \$NUM   argument NUM, unquoted and with newlines
194   \$SEP\@  all arguments, with newlines, quoted, and separated by SEP
195   \$SEP*  all arguments, with newlines, unquoted, and separated by SEP
196   \$SEP%  all arguments, without newlines, unquoted, and separated by SEP
197 SEP can be empty for the default (comma for \@ and *, colon for %),
198 a single character for that character, or {STRING} to use a string.
200 Report bugs to <bug-autoconf\@gnu.org>.
203 # $VERSION
204 # --------
205 $version =  <<"EOF";
206 autom4te (@PACKAGE_NAME@) @VERSION@
207 Copyright (C) @RELEASE_YEAR@ Free Software Foundation, Inc.
208 License GPLv2+: GNU GPL version 2 or later
209 <http://gnu.org/licenses/old-licenses/gpl-2.0.html>
210 This is free software: you are free to change and redistribute it.
211 There is NO WARRANTY, to the extent permitted by law.
213 Written by Akim Demaille.
217 ## ---------- ##
218 ## Routines.  ##
219 ## ---------- ##
222 # $OPTION
223 # files_to_options (@FILE)
224 # ------------------------
225 # Transform Autom4te conventions (e.g., using foo.m4f to designate a frozen
226 # file) into a suitable command line for M4 (e.g., using --reload-state).
227 # parse_args guarantees that we will see at most one frozen file, and that
228 # if a frozen file is present, it is the first argument.
229 sub files_to_options (@)
231   my (@file) = @_;
232   my @res;
233   foreach my $file (@file)
234     {
235       my $arg = shell_quote ($file);
236       if ($file =~ /\.m4f$/)
237         {
238           $arg = "--reload-state=$arg";
239           # If the user downgraded M4 from 1.6 to 1.4.x after freezing
240           # the file, then we ensure the frozen __m4_version__ will
241           # not cause m4_init to make the wrong decision about the
242           # current M4 version.
243           $arg .= " --undefine=__m4_version__"
244             unless grep {/__m4_version__/} @m4_builtin;
245         }
246       push @res, $arg;
247     }
248   return join ' ', @res;
252 # load_configuration ($FILE)
253 # --------------------------
254 # Load the configuration $FILE.
255 sub load_configuration ($)
257   my ($file) = @_;
258   use Text::ParseWords;
260   my $cfg = new Autom4te::XFile ("< " . open_quote ($file));
261   my $lang;
262   while ($_ = $cfg->getline)
263     {
264       chomp;
265       # Comments.
266       next
267         if /^\s*(\#.*)?$/;
269       my @words = shellwords ($_);
270       my $type = shift @words;
271       if ($type eq 'begin-language:')
272         {
273           fatal "$file:$.: end-language missing for: $lang"
274             if defined $lang;
275           $lang = lc $words[0];
276         }
277       elsif ($type eq 'end-language:')
278         {
279           error "$file:$.: end-language mismatch: $lang"
280             if $lang ne lc $words[0];
281           $lang = undef;
282         }
283       elsif ($type eq 'args:')
284         {
285           fatal "$file:$.: no current language"
286             unless defined $lang;
287           push @{$language{$lang}}, @words;
288         }
289       else
290         {
291           error "$file:$.: unknown directive: $type";
292         }
293     }
297 # parse_args ()
298 # -------------
299 # Process any command line arguments.
300 sub parse_args ()
302   # We want to look for the early options, which should not be found
303   # in the configuration file.  Prepend to the user arguments.
304   # Perform this repeatedly so that we can use --language in language
305   # definitions.  Beware that there can be several --language
306   # invocations.
307   my @language;
308   do {
309     @language = ();
310     use Getopt::Long;
311     Getopt::Long::Configure ("pass_through", "permute");
312     GetOptions ("l|language=s" => \@language);
314     foreach (@language)
315       {
316         error "unknown language: $_"
317           unless exists $language{lc $_};
318         unshift @ARGV, @{$language{lc $_}};
319       }
320   } while @language;
322   # --debug is useless: it is parsed below.
323   if (exists $ENV{'AUTOM4TE_DEBUG'})
324     {
325       print STDERR "$me: concrete arguments:\n";
326       foreach my $arg (@ARGV)
327         {
328           print STDERR "| $arg\n";
329         }
330     }
332   # Process the arguments for real this time.
333   my @trace;
334   my @prepend_include;
335   parse_WARNINGS;
336   getopt
337     (
338      # Operation modes:
339      "o|output=s"   => \$output,
340      "W|warnings=s" => \&parse_warnings,
341      "m|mode=s"     => \$mode,
342      "M|melt"       => \$melt,
344      # Library directories:
345      "B|prepend-include=s" => \@prepend_include,
346      "I|include=s"         => \@include,
348      # Tracing:
349      # Using a hash for traces is seducing.  Unfortunately, upon `-t FOO',
350      # instead of mapping `FOO' to undef, Getopt maps it to `1', preventing
351      # us from distinguishing `-t FOO' from `-t FOO=1'.  So let's do it
352      # by hand.
353      "t|trace=s"     => \@trace,
354      "p|preselect=s" => \@preselect,
356      # Freezing.
357      "F|freeze" => \$freeze,
359      # Caching.
360      "C|cache=s" => \$cache,
361      "no-cache"  => sub { $cache = undef; },
362     );
364   fatal "too few arguments
365 Try `$me --help' for more information."
366     unless @ARGV;
368   # Freezing:
369   # We cannot trace at the same time (well, we can, but it sounds insane).
370   # And it implies melting: there is risk not to update properly using
371   # old frozen files, and worse yet: we could load a frozen file and
372   # refreeze it!  A sort of caching :)
373   fatal "cannot freeze and trace"
374     if $freeze && @trace;
375   $melt = 1
376     if $freeze;
378   # Names of the cache directory, cache directory index, trace cache
379   # prefix, and output cache prefix.  If the cache is not to be
380   # preserved, default to a temporary directory (automatically removed
381   # on exit).
382   $cache = $tmp
383     unless $cache;
384   $icache = "$cache/requests";
385   $tcache = "$cache/traces.";
386   $ocache = "$cache/output.";
388   # Normalize the includes: the first occurrence is enough, several is
389   # a pain since it introduces a useless difference in the path which
390   # invalidates the cache.  And strip `.' which is implicit and always
391   # first.
392   @include = grep { !/^\.$/ } uniq (reverse(@prepend_include), @include);
394   # Convert @trace to %trace, and work around the M4 builtins tracing
395   # problem.
396   # The default format is `$f:$l:$n:$%'.
397   foreach (@trace)
398     {
399       /^([^:]+)(?::(.*))?$/ms;
400       $trace{$1} = defined $2 ? $2 : '$f:$l:$n:$%';
401       $trace{$m4_builtin_alternate_name{$1}} = $trace{$1}
402         if exists $m4_builtin_alternate_name{$1};
403     }
405   # Work around the M4 builtins tracing problem for @PRESELECT.
406   # FIXME: Is this still needed, now that we rely on M4 1.4.5?
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 ($_ eq '-')
422         {
423           push @argv, $_;
424         }
425       elsif (/\.m4f$/)
426         {
427           # Frozen files are optional => pass a `?' to `find_file'.
428           my $file = find_file ("$_?", @include);
429           if (!$melt && $file)
430             {
431               @argv = ($file);
432             }
433           else
434             {
435               s/\.m4f$/.m4/;
436               push @argv, find_file ($_, @include);
437             }
438         }
439       else
440         {
441           my $file = find_file ($_, @include);
442           push @argv, $file
443             if $file;
444         }
445     }
446   @ARGV = @argv;
450 # handle_m4 ($REQ, @MACRO)
451 # ------------------------
452 # Run m4 on the input files, and save the traces on the @MACRO.
453 sub handle_m4 ($@)
455   my ($req, @macro) = @_;
457   # GNU m4 appends when using --debugfile/--error-output.
458   unlink ($tcache . $req->id . "t");
460   # Run m4.
461   #
462   # We don't output directly to the cache files, to avoid problems
463   # when we are interrupted (that leaves corrupted files).
464   xsystem ("$m4"
465            . join (' --include=', '', map { shell_quote ($_) } @include)
466            . ' --debug=aflq'
467            . (!exists $ENV{'AUTOM4TE_NO_FATAL'} ? ' --fatal-warning' : '')
468            . " @M4_DEBUGFILE@=" . shell_quote ("$tcache" . $req->id . "t")
469            . join (' --trace=',   '', sort @macro)
470            . " " . files_to_options (@ARGV)
471            . " > " . shell_quote ("$ocache" . $req->id . "t"));
473   # Everything went ok: preserve the outputs.
474   foreach my $file (map { $_ . $req->id } ($tcache, $ocache))
475     {
476       use File::Copy;
477       move ("${file}t", "$file")
478         or fatal "cannot rename ${file}t as $file: $!";
479     }
483 # warn_forbidden ($WHERE, $WORD, %FORBIDDEN)
484 # ------------------------------------------
485 # $WORD is forbidden.  Warn with a dedicated error message if in
486 # %FORBIDDEN, otherwise a simple `error: possibly undefined macro'
487 # will do.
488 my $first_warn_forbidden = 1;
489 sub warn_forbidden ($$%)
491   my ($where, $word, %forbidden) = @_;
492   my $message;
494   for my $re (sort keys %forbidden)
495     {
496       if ($word =~ $re)
497         {
498           $message = $forbidden{$re};
499           last;
500         }
501     }
502   $message ||= "possibly undefined macro: $word";
503   warn "$where: error: $message\n";
504   if ($first_warn_forbidden)
505     {
506       warn <<EOF;
507       If this token and others are legitimate, please use m4_pattern_allow.
508       See the Autoconf documentation.
510       $first_warn_forbidden = 0;
511     }
515 # handle_output ($REQ, $OUTPUT)
516 # -----------------------------
517 # Run m4 on the input files, perform quadrigraphs substitution, check for
518 # forbidden tokens, and save into $OUTPUT.
519 sub handle_output ($$)
521   my ($req, $output) = @_;
523   verb "creating $output";
525   # Load the forbidden/allowed patterns.
526   handle_traces ($req, "$tmp/patterns",
527                  ('m4_pattern_forbid' => 'forbid:$1:$2',
528                   'm4_pattern_allow'  => 'allow:$1'));
529   my @patterns = new Autom4te::XFile ("< " . open_quote ("$tmp/patterns"))->getlines;
530   chomp @patterns;
531   my %forbidden =
532     map { /^forbid:([^:]+):.+$/ => /^forbid:[^:]+:(.+)$/ } @patterns;
533   my $forbidden = join ('|', map { /^forbid:([^:]+)/ } @patterns) || "^\$";
534   my $allowed   = join ('|', map { /^allow:([^:]+)/  } @patterns) || "^\$";
536   verb "forbidden tokens: $forbidden";
537   verb "forbidden token : $_ => $forbidden{$_}"
538     foreach (sort keys %forbidden);
539   verb "allowed   tokens: $allowed";
541   # Read the (cached) raw M4 output, produce the actual result.  We
542   # have to use the 2nd arg to have Autom4te::XFile honor the third, but then
543   # stdout is to be handled by hand :(.  Don't use fdopen as it means
544   # we will close STDOUT, which we already do in END.
545   my $out = new Autom4te::XFile;
546   if ($output eq '-')
547     {
548       $out->open (">$output");
549     }
550   else
551     {
552       $out->open($output, O_CREAT | O_WRONLY | O_TRUNC, oct ($mode));
553     }
554   fatal "cannot create $output: $!"
555     unless $out;
556   my $in = new Autom4te::XFile ("< " . open_quote ($ocache . $req->id));
558   my %prohibited;
559   my $res;
560   while ($_ = $in->getline)
561     {
562       s/\s+$//;
563       s/__oline__/$./g;
564       s/\@<:\@/[/g;
565       s/\@:>\@/]/g;
566       s/\@\{:\@/(/g;
567       s/\@:\}\@/)/g;
568       s/\@S\|\@/\$/g;
569       s/\@%:\@/#/g;
571       $res = $_;
573       # Don't complain in comments.  Well, until we have something
574       # better, don't consider `#include' etc. are comments.
575       s/\#.*//
576         unless /^\#\s*(if|include|endif|ifdef|ifndef|define)\b/;
577       foreach (split (/\W+/))
578         {
579           $prohibited{$_} = $.
580             if !/^$/ && /$forbidden/o && !/$allowed/o && ! exists $prohibited{$_};
581         }
583       # Performed *last*: the empty quadrigraph.
584       $res =~ s/\@&t\@//g;
586       print $out "$res\n";
587     }
589   $out->close();
591   # If no forbidden words, we're done.
592   return
593     if ! %prohibited;
595   # Locate the forbidden words in the last input file.
596   # This is unsatisfying but...
597   $exit_code = 1;
598   if ($ARGV[$#ARGV] ne '-')
599     {
600       my $prohibited = '\b(' . join ('|', keys %prohibited) . ')\b';
601       my $file = new Autom4te::XFile ("< " . open_quote ($ARGV[$#ARGV]));
603       while ($_ = $file->getline)
604         {
605           # Don't complain in comments.  Well, until we have something
606           # better, don't consider `#include' etc. to be comments.
607           s/\#.*//
608             unless /^\#(if|include|endif|ifdef|ifndef|define)\b/;
610           # Complain once per word, but possibly several times per line.
611           while (/$prohibited/)
612             {
613               my $word = $1;
614               warn_forbidden ("$ARGV[$#ARGV]:$.", $word, %forbidden);
615               delete $prohibited{$word};
616               # If we're done, exit.
617               return
618                 if ! %prohibited;
619               $prohibited = '\b(' . join ('|', keys %prohibited) . ')\b';
620             }
621         }
622     }
623   warn_forbidden ("$output:$prohibited{$_}", $_, %forbidden)
624     foreach (sort { $prohibited{$a} <=> $prohibited{$b} } keys %prohibited);
628 ## --------------------- ##
629 ## Handling the traces.  ##
630 ## --------------------- ##
633 # $M4_MACRO
634 # trace_format_to_m4 ($FORMAT)
635 # ----------------------------
636 # Convert a trace $FORMAT into a M4 trace processing macro's body.
637 sub trace_format_to_m4 ($)
639   my ($format) = @_;
640   my $underscore = $_;
641   my %escape = (# File name.
642                 'f' => '$1',
643                 # Line number.
644                 'l' => '$2',
645                 # Depth.
646                 'd' => '$3',
647                 # Name (also available as $0).
648                 'n' => '$4',
649                 # Escaped dollar.
650                 '$' => '$');
652   my $res = '';
653   $_ = $format;
654   while ($_)
655     {
656       # $n -> $(n + 4)
657       if (s/^\$(\d+)//)
658         {
659           $res .= "\$" . ($1 + 4);
660         }
661       # $x, no separator given.
662       elsif (s/^\$([fldn\$])//)
663         {
664           $res .= $escape{$1};
665         }
666       # $.x or ${sep}x.
667       elsif (s/^\$\{([^}]*)\}([@*%])//
668             || s/^\$(.?)([@*%])//)
669         {
670           # $@, list of quoted effective arguments.
671           if ($2 eq '@')
672             {
673               $res .= ']at_at([' . ($1 ? $1 : ',') . '], $@)[';
674             }
675           # $*, list of unquoted effective arguments.
676           elsif ($2 eq '*')
677             {
678               $res .= ']at_star([' . ($1 ? $1 : ',') . '], $@)[';
679             }
680           # $%, list of flattened unquoted effective arguments.
681           elsif ($2 eq '%')
682             {
683               $res .= ']at_percent([' . ($1 ? $1 : ':') . '], $@)[';
684             }
685         }
686       elsif (/^(\$.)/)
687         {
688           error "invalid escape: $1";
689         }
690       else
691         {
692           s/^([^\$]+)//;
693           $res .= $1;
694         }
695     }
697   $_ = $underscore;
698   return '[[' . $res . ']]';
702 # handle_traces($REQ, $OUTPUT, %TRACE)
703 # ------------------------------------
704 # We use M4 itself to process the traces.  But to avoid name clashes when
705 # processing the traces, the builtins are disabled, and moved into `at_'.
706 # Actually, all the low level processing macros are in `at_' (and `_at_').
707 # To avoid clashes between user macros and `at_' macros, the macros which
708 # implement tracing are in `AT_'.
710 # Having $REQ is needed to neutralize the macros which have been traced,
711 # but are not wanted now.
712 sub handle_traces ($$%)
714   my ($req, $output, %trace) = @_;
716   verb "formatting traces for `$output': " . join (', ', sort keys %trace);
718   # Processing the traces.
719   my $trace_m4 = new Autom4te::XFile ("> " . open_quote ("$tmp/traces.m4"));
721   $_ = <<'EOF';
722   divert(-1)
723   changequote([, ])
724   # _at_MODE(SEPARATOR, ELT1, ELT2...)
725   # ----------------------------------
726   # List the elements, separating then with SEPARATOR.
727   # MODE can be:
728   #  `at'       -- the elements are enclosed in brackets.
729   #  `star'     -- the elements are listed as are.
730   #  `percent'  -- the elements are `flattened': spaces are singled out,
731   #                and no new line remains.
732   define([_at_at],
733   [at_ifelse([$#], [1], [],
734              [$#], [2], [[[$2]]],
735              [[[$2]][$1]$0([$1], at_shift(at_shift($@)))])])
737   define([_at_percent],
738   [at_ifelse([$#], [1], [],
739              [$#], [2], [at_flatten([$2])],
740              [at_flatten([$2])[$1]$0([$1], at_shift(at_shift($@)))])])
742   define([_at_star],
743   [at_ifelse([$#], [1], [],
744              [$#], [2], [[$2]],
745              [[$2][$1]$0([$1], at_shift(at_shift($@)))])])
747   # FLATTEN quotes its result.
748   # Note that the second pattern is `newline, tab or space'.  Don't lose
749   # the tab!
750   define([at_flatten],
751   [at_patsubst(at_patsubst(at_patsubst(at_patsubst([[[[$1]]]], [\\\n]),
752                                        [[\n\t ]+], [ ]),
753                            [ *\(.\)$], [\1]),
754                [^ *\(.*\)], [[\1]])])
756   define([at_args],    [at_shift(at_shift(at_shift(at_shift(at_shift($@)))))])
757   define([at_at],      [_$0([$1], at_args($@))])
758   define([at_percent], [_$0([$1], at_args($@))])
759   define([at_star],    [_$0([$1], at_args($@))])
762   s/^  //mg;s/\\t/\t/mg;s/\\n/\n/mg;
763   print $trace_m4 $_;
765   # If you trace `define', then on `define([m4_exit], defn([m4exit])' you
766   # will produce
767   #
768   #    AT_define([m4sugar.m4], [115], [1], [define], [m4_exit], <m4exit>)
769   #
770   # Since `<m4exit>' is not quoted, the outer m4, when processing
771   # `trace.m4' will exit prematurely.  Hence, move all the builtins to
772   # the `at_' name space.
774   print $trace_m4 "# Copy the builtins.\n";
775   map { print $trace_m4 "define([at_$_], defn([$_]))\n" } @m4_builtin;
776   print $trace_m4 "\n";
778   print $trace_m4 "# Disable them.\n";
779   map { print $trace_m4 "at_undefine([$_])\n" } @m4_builtin;
780   print $trace_m4 "\n";
783   # Neutralize traces: we don't want traces of cached requests (%REQUEST).
784   print $trace_m4
785    "## -------------------------------------- ##\n",
786    "## By default neutralize all the traces.  ##\n",
787    "## -------------------------------------- ##\n",
788    "\n";
789   print $trace_m4 "at_define([AT_$_], [at_dnl])\n"
790     foreach (sort keys %{$req->macro});
791   print $trace_m4 "\n";
793   # Implement traces for current requests (%TRACE).
794   print $trace_m4
795     "## ------------------------- ##\n",
796     "## Trace processing macros.  ##\n",
797     "## ------------------------- ##\n",
798     "\n";
799   foreach (sort keys %trace)
800     {
801       # Trace request can be embed \n.
802       (my $comment = "Trace $_:$trace{$_}") =~ s/^/\# /;
803       print $trace_m4 "$comment\n";
804       print $trace_m4 "at_define([AT_$_],\n";
805       print $trace_m4 trace_format_to_m4 ($trace{$_}) . ")\n\n";
806     }
807   print $trace_m4 "\n";
809   # Reenable output.
810   print $trace_m4 "at_divert(0)at_dnl\n";
812   # Transform the traces from m4 into an m4 input file.
813   # Typically, transform:
814   #
815   # | m4trace:configure.ac:3: -1- AC_SUBST([exec_prefix], [NONE])
816   #
817   # into
818   #
819   # | AT_AC_SUBST([configure.ac], [3], [1], [AC_SUBST], [exec_prefix], [NONE])
820   #
821   # Pay attention that the file name might include colons, if under DOS
822   # for instance, so we don't use `[^:]+'.
823   my $traces = new Autom4te::XFile ("< " . open_quote ($tcache . $req->id));
824   while ($_ = $traces->getline)
825     {
826       # Trace with arguments, as the example above.  We don't try
827       # to match the trailing parenthesis as it might be on a
828       # separate line.
829       s{^m4trace:(.+):(\d+): -(\d+)- ([^(]+)\((.*)$}
830        {AT_$4([$1], [$2], [$3], [$4], $5};
831       # Traces without arguments, always on a single line.
832       s{^m4trace:(.+):(\d+): -(\d+)- ([^)]*)\n$}
833        {AT_$4([$1], [$2], [$3], [$4])\n};
834       print $trace_m4 "$_";
835     }
836   $trace_m4->close;
838   my $in = new Autom4te::XFile ("$m4 " . shell_quote ("$tmp/traces.m4") . " |");
839   my $out = new Autom4te::XFile ("> " . open_quote ($output));
841   # This is dubious: should we really transform the quadrigraphs in
842   # traces?  It might break balanced [ ] etc. in the output.  The
843   # consensus seeems to be that traces are more useful this way.
844   while ($_ = $in->getline)
845     {
846       # It makes no sense to try to transform __oline__.
847       s/\@<:\@/[/g;
848       s/\@:>\@/]/g;
849       s/\@\{:\@/(/g;
850       s/\@:\}\@/)/g;
851       s/\@S\|\@/\$/g;
852       s/\@%:\@/#/g;
853       s/\@&t\@//g;
854       print $out $_;
855     }
859 # $BOOL
860 # up_to_date ($REQ)
861 # -----------------
862 # Are the cache files of $REQ up to date?
863 # $REQ is `valid' if it corresponds to the request and exists, which
864 # does not mean it is up to date.  It is up to date if, in addition,
865 # its files are younger than its dependencies.
866 sub up_to_date ($)
868   my ($req) = @_;
870   return 0
871     if ! $req->valid;
873   my $tfile = $tcache . $req->id;
874   my $ofile = $ocache . $req->id;
876   # We can't answer properly if the traces are not computed since we
877   # need to know what other files were included.  Actually, if any of
878   # the cache files is missing, we are not up to date.
879   return 0
880     if ! -f $tfile || ! -f $ofile;
882   # The youngest of the cache files must be older than the oldest of
883   # the dependencies.
884   my $tmtime = mtime ($tfile);
885   my $omtime = mtime ($ofile);
886   my ($file, $mtime) = ($tmtime < $omtime
887                         ? ($ofile, $omtime) : ($tfile, $tmtime));
889   # We depend at least upon the arguments.
890   my @dep = @ARGV;
892   # stdin is always out of date.
893   if (grep { $_ eq '-' } @dep)
894     { return 0 }
896   # Files may include others.  We can use traces since we just checked
897   # if they are available.
898   handle_traces ($req, "$tmp/dependencies",
899                  ('include'    => '$1',
900                   'm4_include' => '$1'));
901   my $deps = new Autom4te::XFile ("< " . open_quote ("$tmp/dependencies"));
902   while ($_ = $deps->getline)
903     {
904       chomp;
905       my $file = find_file ("$_?", @include);
906       # If a file which used to be included is no longer there, then
907       # don't say it's missing (it might no longer be included).  But
908       # of course, that causes the output to be outdated (as if the
909       # time stamp of that missing file was newer).
910       return 0
911         if ! $file;
912       push @dep, $file;
913     }
915   # If $FILE is younger than one of its dependencies, it is outdated.
916   return up_to_date_p ($file, @dep);
920 ## ---------- ##
921 ## Freezing.  ##
922 ## ---------- ##
924 # freeze ($OUTPUT)
925 # ----------------
926 sub freeze ($)
928   my ($output) = @_;
930   # When processing the file with diversion disabled, there must be no
931   # output but comments and empty lines.
932   my $result = xqx ("$m4"
933                     . ' --fatal-warning'
934                     . join (' --include=', '', map { shell_quote ($_) } @include)
935                     . ' --define=divert'
936                     . " " . files_to_options (@ARGV)
937                     . ' </dev/null');
938   $result =~ s/#.*\n//g;
939   $result =~ s/^\n//mg;
941   fatal "freezing produced output:\n$result"
942     if $result;
944   # If freezing produces output, something went wrong: a bad `divert',
945   # or an improper paren etc.
946   xsystem ("$m4"
947            . ' --fatal-warning'
948            . join (' --include=', '', map { shell_quote ($_) } @include)
949            . " --freeze-state=" . shell_quote ($output)
950            . " " . files_to_options (@ARGV)
951            . ' </dev/null');
954 ## -------------- ##
955 ## Main program.  ##
956 ## -------------- ##
958 mktmpdir ('am4t');
959 load_configuration ($ENV{'AUTOM4TE_CFG'} || "$datadir/autom4te.cfg");
960 load_configuration ("$ENV{'HOME'}/.autom4te.cfg")
961   if exists $ENV{'HOME'} && -f "$ENV{'HOME'}/.autom4te.cfg";
962 load_configuration (".autom4te.cfg")
963   if -f ".autom4te.cfg";
964 parse_args;
966 # Freezing does not involve the cache.
967 if ($freeze)
968   {
969     freeze ($output);
970     exit $exit_code;
971   }
973 # We need our cache directory.
974 if (! -d "$cache")
975   {
976     mkdir "$cache", 0755
977       or fatal "cannot create $cache: $!";
978   }
980 # Open the index for update, and lock it.  autom4te handles several
981 # files, but the index is the first and last file to be updated, so
982 # locking it is sufficient.
983 $icache_file = new Autom4te::XFile $icache, O_RDWR|O_CREAT;
984 $icache_file->lock (LOCK_EX);
986 # Read the cache index if available and older than autom4te itself.
987 # If autom4te is younger, then some structures such as C4che might
988 # have changed, which would corrupt its processing.
989 Autom4te::C4che->load ($icache_file)
990   if -f $icache && mtime ($icache) > mtime ($0);
992 # Add the new trace requests.
993 my $req = Autom4te::C4che->request ('input' => \@ARGV,
994                                     'path'  => \@include,
995                                     'macro' => [keys %trace, @preselect]);
997 # If $REQ's cache files are not up to date, or simply if the user
998 # discarded them (-f), declare it invalid.
999 $req->valid (0)
1000   if $force || ! up_to_date ($req);
1002 # We now know whether we can trust the Request object.  Say it.
1003 verb "the trace request object is:\n" . $req->marshall;
1005 # We need to run M4 if (i) the user wants it (--force), (ii) $REQ is
1006 # invalid.
1007 handle_m4 ($req, keys %{$req->macro})
1008   if $force || ! $req->valid;
1010 # Issue the warnings each time autom4te was run.
1011 my $separator = "\n" . ('-' x 25) . " END OF WARNING " . ('-' x 25) . "\n\n";
1012 handle_traces ($req, "$tmp/warnings",
1013                ('_m4_warn' => "\$1::\$f:\$l::\$2::\$3$separator"));
1014 # Swallow excessive newlines.
1015 for (split (/\n*$separator\n*/o, contents ("$tmp/warnings")))
1017   # The message looks like:
1018   # | syntax::input.as:5::ouch
1019   # | ::input.as:4: baz is expanded from...
1020   # | input.as:2: bar is expanded from...
1021   # | input.as:3: foo is expanded from...
1022   # | input.as:5: the top level
1023   my ($cat, $loc, $msg, $stacktrace) = split ('::', $_, 4);
1024   msg $cat, $loc, "warning: $msg";
1025   for (split /\n/, $stacktrace)
1026     {
1027       my ($loc, $trace) = split (': ', $_, 2);
1028       msg $cat, $loc, $trace;
1029     }
1032 # Now output...
1033 if (%trace)
1034   {
1035     # Always produce traces, since even if the output is young enough,
1036     # there is no guarantee that the traces use the same *format*
1037     # (e.g., `-t FOO:foo' and `-t FOO:bar' are both using the same M4
1038     # traces, hence the M4 traces cache is usable, but its formatting
1039     # will yield different results).
1040     handle_traces ($req, $output, %trace);
1041   }
1042 else
1043   {
1044     # Actual M4 expansion, if the user wants it, or if $output is old
1045     # (STDOUT is pretty old).
1046     handle_output ($req, $output)
1047       if $force || mtime ($output) < mtime ($ocache . $req->id);
1048   }
1050 # If we ran up to here, the cache is valid.
1051 $req->valid (1);
1052 Autom4te::C4che->save ($icache_file);
1054 exit $exit_code;
1056 ### Setup "GNU" style for perl-mode and cperl-mode.
1057 ## Local Variables:
1058 ## perl-indent-level: 2
1059 ## perl-continued-statement-offset: 2
1060 ## perl-continued-brace-offset: 0
1061 ## perl-brace-offset: 0
1062 ## perl-brace-imaginary-offset: 0
1063 ## perl-label-offset: -2
1064 ## cperl-indent-level: 2
1065 ## cperl-brace-offset: 0
1066 ## cperl-continued-brace-offset: 0
1067 ## cperl-label-offset: -2
1068 ## cperl-extra-newline-before-brace: t
1069 ## cperl-merge-trailing-else: nil
1070 ## cperl-continued-statement-offset: 2
1071 ## End: