Add a `--clean' option for autoconf, autoheader, autoreconf and autom4te.
[autoconf/tsuna.git] / bin / autom4te.in
blob01632fc025c76c38d7bb2d3eb79b03235e225d20
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 3 of the License, or
15 # (at your option) 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, see <http://www.gnu.org/licenses/>.
26 BEGIN
28   my $datadir = $ENV{'autom4te_perllibdir'} || '@datadir@';
29   unshift @INC, $datadir;
31   # Override SHELL.  On DJGPP SHELL may not be set to a shell
32   # that can handle redirection and quote arguments correctly,
33   # e.g.: COMMAND.COM.  For DJGPP always use the shell that configure
34   # has detected.
35   $ENV{'SHELL'} = '@SHELL@' if ($^O eq 'dos');
38 use Autom4te::C4che;
39 use Autom4te::ChannelDefs;
40 use Autom4te::Channels;
41 use Autom4te::FileUtils;
42 use Autom4te::General;
43 use Autom4te::XFile;
44 use File::Basename;
45 use strict;
47 # Data directory.
48 my $datadir = $ENV{'AC_MACRODIR'} || '@datadir@';
50 # $LANGUAGE{LANGUAGE} -- Automatic options for LANGUAGE.
51 my %language;
53 my $output = '-';
55 # Mode of the output file except for traces.
56 my $mode = "0666";
58 # If melt, don't use frozen files.
59 my $melt = 0;
61 # Names of the cache directory, cache directory index, trace cache
62 # prefix, and output cache prefix.  And the IO object for the index.
63 my $cache;
64 my $icache;
65 my $tcache;
66 my $ocache;
67 my $icache_file;
69 # The macros to trace mapped to their format, as specified by the
70 # user.
71 my %trace;
73 # The macros the user will want to trace in the future.
74 # We need `include' to get the included file, `m4_pattern_forbid' and
75 # `m4_pattern_allow' to check the output.
77 # FIXME: What about `sinclude'?
78 my @preselect = ('include',
79                  'm4_pattern_allow', 'm4_pattern_forbid',
80                  '_m4_warn');
82 # M4 include path.
83 my @include;
85 # Do we freeze?
86 my $freeze = 0;
88 # $M4.
89 my $m4 = $ENV{"M4"} || '@M4@';
90 # Some non-GNU m4's don't reject the --help option, so give them /dev/null.
91 fatal "need GNU m4 1.4 or later: $m4"
92   if system "$m4 --help </dev/null 2>&1 | grep reload-state >/dev/null";
94 # Set some high recursion limit as the default limit, 250, has already
95 # been hit with AC_OUTPUT.  Don't override the user's choice.
96 $m4 .= ' --nesting-limit=1024'
97   if " $m4 " !~ / (--nesting-limit(=[0-9]+)?|-L[0-9]*) /;
100 # @M4_BUILTIN -- M4 builtins and a useful comment.
101 my @m4_builtin = `echo dumpdef | $m4 2>&1 >/dev/null`;
102 map { s/:.*//;s/\W// } @m4_builtin;
105 # %M4_BUILTIN_ALTERNATE_NAME
106 # --------------------------
107 # The builtins are renamed, e.g., `define' is renamed `m4_define'.
108 # So map `define' to `m4_define' and conversely.
109 # Some macros don't follow this scheme: be sure to properly map to their
110 # alternate name too.
112 # This is because GNU M4 1.4's tracing of builtins is buggy.  When run on
113 # this input:
115 # | divert(-1)
116 # | changequote([, ])
117 # | define([m4_eval], defn([eval]))
118 # | eval(1)
119 # | m4_eval(2)
120 # | undefine([eval])
121 # | m4_eval(3)
123 # it behaves this way:
125 # | % m4 input.m4 -da -t eval
126 # | m4trace: -1- eval(1)
127 # | m4trace: -1- m4_eval(2)
128 # | m4trace: -1- m4_eval(3)
129 # | %
131 # Conversely:
133 # | % m4 input.m4 -da -t m4_eval
134 # | %
136 # So we will merge them, i.e., tracing `BUILTIN' or tracing
137 # `m4_BUILTIN' will be the same: tracing both, but honoring the
138 # *last* trace specification.
140 # FIXME: This is not enough: in the output `$0' will be `BUILTIN'
141 # sometimes and `m4_BUILTIN' at others.  We should return a unique name,
142 # the one specified by the user.
144 # FIXME: To be absolutely rigorous, I would say that given that we
145 # _redefine_ divert (instead of _copying_ it), divert and the like
146 # should not be part of this list.
147 my %m4_builtin_alternate_name;
148 @m4_builtin_alternate_name{"$_", "m4_$_"} = ("m4_$_", "$_")
149   foreach (grep { !/m4wrap|m4exit|dnl|ifelse|__.*__/ } @m4_builtin);
150 @m4_builtin_alternate_name{"ifelse", "m4_if"}   = ("m4_if", "ifelse");
151 @m4_builtin_alternate_name{"m4exit", "m4_exit"} = ("m4_exit", "m4exit");
152 @m4_builtin_alternate_name{"m4wrap", "m4_wrap"} = ("m4_wrap", "m4wrap");
155 # $HELP
156 # -----
157 $help = "Usage: $0 [OPTION] ... [FILES]
159 Run GNU M4 on the FILES, avoiding useless runs.  Output the traces if tracing,
160 the frozen file if freezing, otherwise the expansion of the FILES.
162 If some of the FILES are named \`FILE.m4f\' they are considered to be M4
163 frozen files of all the previous files (which are therefore not loaded).
164 If \`FILE.m4f\' is not found, then \`FILE.m4\' will be used, together with
165 all the previous files.
167 Some files may be optional, i.e., will only be processed if found in the
168 include path, but then must end in \`.m4?\';  the question mark is not part of
169 the actual file name.
171 Operation modes:
172   -h, --help               print this help, then exit
173   -V, --version            print version number, then exit
174   -v, --verbose            verbosely report processing
175       --clean              remove the cache
176   -d, --debug              don\'t remove temporary files
177   -o, --output=FILE        save output in FILE (defaults to \`-\', stdout)
178   -f, --force              don\'t rely on cached values
179   -W, --warnings=CATEGORY  report the warnings falling in CATEGORY
180   -l, --language=LANG      specify the set of M4 macros to use
181   -C, --cache=DIRECTORY    preserve results for future runs in DIRECTORY
182       --no-cache           disable the cache
183   -m, --mode=OCTAL         change the non trace output file mode (0666)
184   -M, --melt               don\'t use M4 frozen files
186 Languages include:
187   \`Autoconf\'   create Autoconf configure scripts
188   \`Autotest\'   create Autotest test suites
189   \`M4sh\'       create M4sh shell scripts
190   \`M4sugar\'    create M4sugar output
192 " . Autom4te::ChannelDefs::usage . "
194 The environment variables \`M4\' and \`WARNINGS\' are honored.
196 Library directories:
197   -B, --prepend-include=DIR  prepend directory DIR to search path
198   -I, --include=DIR          append directory DIR to search path
200 Tracing:
201   -t, --trace=MACRO      report the MACRO invocations
202   -p, --preselect=MACRO  prepare to trace MACRO in a future run
204 Freezing:
205   -F, --freeze   produce an M4 frozen state file for FILES
207 Report bugs to <bug-autoconf\@gnu.org>.
210 # $VERSION
211 # --------
212 $version =  <<"EOF";
213 autom4te (@PACKAGE_NAME@) @VERSION@
214 Copyright (C) @RELEASE_YEAR@ Free Software Foundation, Inc.
215 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
216 This is free software: you are free to change and redistribute it.
217 There is NO WARRANTY, to the extent permitted by law.
219 Written by Akim Demaille.
223 ## ---------- ##
224 ## Routines.  ##
225 ## ---------- ##
228 # $OPTION
229 # files_to_options (@FILE)
230 # ------------------------
231 # Transform Autom4te conventions (e.g., using foo.m4f to designate a frozen
232 # file) into a suitable command line for M4 (e.g., using --reload-state).
233 sub files_to_options (@)
235   my (@file) = @_;
236   my @res;
237   foreach my $file (@file)
238     {
239       if ($file =~ /\.m4f$/)
240         {
241           push @res, "--reload-state=$file";
242         }
243       else
244         {
245           push @res, $file;
246         }
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 ($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   my $clean = 0;
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,
344      "clean"        => \$clean,
346      # Library directories:
347      "B|prepend-include=s" => \@prepend_include,
348      "I|include=s"         => \@include,
350      # Tracing:
351      # Using a hash for traces is seducing.  Unfortunately, upon `-t FOO',
352      # instead of mapping `FOO' to undef, Getopt maps it to `1', preventing
353      # us from distinguishing `-t FOO' from `-t FOO=1'.  So let's do it
354      # by hand.
355      "t|trace=s"     => \@trace,
356      "p|preselect=s" => \@preselect,
358      # Freezing.
359      "F|freeze" => \$freeze,
361      # Caching.
362      "C|cache=s" => \$cache,
363      "no-cache"  => sub { $cache = undef; },
364     );
366   fatal "too few arguments
367 Try `$me --help' for more information."
368     unless @ARGV or $clean;
370   # Freezing:
371   # We cannot trace at the same time (well, we can, but it sounds insane).
372   # And it implies melting: there is risk not to update properly using
373   # old frozen files, and worse yet: we could load a frozen file and
374   # refreeze it!  A sort of caching :)
375   fatal "cannot freeze and trace"
376     if $freeze && @trace;
377   $melt = 1
378     if $freeze;
380   # Names of the cache directory, cache directory index, trace cache
381   # prefix, and output cache prefix.  If the cache is not to be
382   # preserved, default to a temporary directory (automatically removed
383   # on exit).
384   $cache = $tmp
385     unless $cache;
386   $icache = "$cache/requests";
387   $tcache = "$cache/traces.";
388   $ocache = "$cache/output.";
390   if ($clean)
391     {
392       xsystem ("rm -rf '$cache'")
393         if -e $cache
394         and not File::Spec->file_name_is_absolute ($cache);
395       exit 0;
396     }
398   # Normalize the includes: the first occurrence is enough, several is
399   # a pain since it introduces a useless difference in the path which
400   # invalidates the cache.  And strip `.' which is implicit and always
401   # first.
402   @include = grep { !/^\.$/ } uniq (reverse(@prepend_include), @include);
404   # Convert @trace to %trace, and work around the M4 builtins tracing
405   # problem.
406   # The default format is `$f:$l:$n:$%'.
407   foreach (@trace)
408     {
409       /^([^:]+)(?::(.*))?$/ms;
410       $trace{$1} = defined $2 ? $2 : '$f:$l:$n:$%';
411       $trace{$m4_builtin_alternate_name{$1}} = $trace{$1}
412         if exists $m4_builtin_alternate_name{$1};
413     }
415   # Work around the M4 builtins tracing problem for @PRESELECT.
416   push (@preselect,
417         map { $m4_builtin_alternate_name{$_} }
418         grep { exists $m4_builtin_alternate_name{$_} } @preselect);
420   # If we find frozen files, then all the files before it are
421   # discarded: the frozen file is supposed to include them all.
422   #
423   # We don't want to depend upon m4's --include to find the top level
424   # files, so we use `find_file' here.  Try to get a canonical name,
425   # as it's part of the key for caching.  And some files are optional
426   # (also handled by `find_file').
427   my @argv;
428   foreach (@ARGV)
429     {
430       if ($_ eq '-')
431         {
432           push @argv, $_;
433         }
434       elsif (/\.m4f$/)
435         {
436           # Frozen files are optional => pass a `?' to `find_file'.
437           my $file = find_file ("$_?", @include);
438           if (!$melt && $file)
439             {
440               @argv = ($file);
441             }
442           else
443             {
444               s/\.m4f$/.m4/;
445               push @argv, find_file ($_, @include);
446             }
447         }
448       else
449         {
450           my $file = find_file ($_, @include);
451           push @argv, $file
452             if $file;
453         }
454     }
455   @ARGV = @argv;
459 # handle_m4 ($REQ, @MACRO)
460 # ------------------------
461 # Run m4 on the input files, and save the traces on the @MACRO.
462 sub handle_m4 ($@)
464   my ($req, @macro) = @_;
466   # GNU m4 appends when using --debugfile/--error-output.
467   unlink ($tcache . $req->id . "t");
469   # Run m4.
470   #
471   # We don't output directly to the cache files, to avoid problems
472   # when we are interrupted (that leaves corrupted files).
473   xsystem ("$m4"
474            . join (' --include=', '', @include)
475            . ' --debug=aflq'
476            . (!exists $ENV{'AUTOM4TE_NO_FATAL'} ? ' --fatal-warning' : '')
477            . " @M4_DEBUGFILE@=$tcache" . $req->id . "t"
478            . join (' --trace=',   '', sort @macro)
479            . " " . files_to_options (@ARGV)
480            . " >$ocache" . $req->id . "t");
482   # Everything went ok: preserve the outputs.
483   foreach my $file (map { $_ . $req->id } ($tcache, $ocache))
484     {
485       use File::Copy;
486       move ("${file}t", "$file")
487         or fatal "cannot rename ${file}t as $file: $!";
488     }
492 # warn_forbidden ($WHERE, $WORD, %FORBIDDEN)
493 # ------------------------------------------
494 # $WORD is forbidden.  Warn with a dedicated error message if in
495 # %FORBIDDEN, otherwise a simple `error: possibly undefined macro'
496 # will do.
497 my $first_warn_forbidden = 1;
498 sub warn_forbidden ($$%)
500   my ($where, $word, %forbidden) = @_;
501   my $message;
503   for my $re (sort keys %forbidden)
504     {
505       if ($word =~ $re)
506         {
507           $message = $forbidden{$re};
508           last;
509         }
510     }
511   $message ||= "possibly undefined macro: $word";
512   warn "$where: error: $message\n";
513   if ($first_warn_forbidden)
514     {
515       warn <<EOF;
516       If this token and others are legitimate, please use m4_pattern_allow.
517       See the Autoconf documentation.
519       $first_warn_forbidden = 0;
520     }
524 # handle_output ($REQ, $OUTPUT)
525 # -----------------------------
526 # Run m4 on the input files, perform quadrigraphs substitution, check for
527 # forbidden tokens, and save into $OUTPUT.
528 sub handle_output ($$)
530   my ($req, $output) = @_;
532   verb "creating $output";
534   # Load the forbidden/allowed patterns.
535   handle_traces ($req, "$tmp/patterns",
536                  ('m4_pattern_forbid' => 'forbid:$1:$2',
537                   'm4_pattern_allow'  => 'allow:$1'));
538   my @patterns = new Autom4te::XFile ("$tmp/patterns")->getlines;
539   chomp @patterns;
540   my %forbidden =
541     map { /^forbid:([^:]+):.+$/ => /^forbid:[^:]+:(.+)$/ } @patterns;
542   my $forbidden = join ('|', map { /^forbid:([^:]+)/ } @patterns) || "^\$";
543   my $allowed   = join ('|', map { /^allow:([^:]+)/  } @patterns) || "^\$";
545   verb "forbidden tokens: $forbidden";
546   verb "forbidden token : $_ => $forbidden{$_}"
547     foreach (sort keys %forbidden);
548   verb "allowed   tokens: $allowed";
550   # Read the (cached) raw M4 output, produce the actual result.  We
551   # have to use the 2nd arg to have Autom4te::XFile honor the third, but then
552   # stdout is to be handled by hand :(.  Don't use fdopen as it means
553   # we will close STDOUT, which we already do in END.
554   my $out = new Autom4te::XFile;
555   if ($output eq '-')
556     {
557       $out->open (">$output");
558     }
559   else
560     {
561       $out->open($output, O_CREAT | O_WRONLY | O_TRUNC, oct ($mode));
562     }
563   fatal "cannot create $output: $!"
564     unless $out;
565   my $in = new Autom4te::XFile ($ocache . $req->id);
567   my %prohibited;
568   my $res;
569   while ($_ = $in->getline)
570     {
571       s/\s+$//;
572       s/__oline__/$./g;
573       s/\@<:\@/[/g;
574       s/\@:>\@/]/g;
575       s/\@S\|\@/\$/g;
576       s/\@%:\@/#/g;
578       $res = $_;
580       # Don't complain in comments.  Well, until we have something
581       # better, don't consider `#include' etc. are comments.
582       s/\#.*//
583         unless /^\#\s*(if|include|endif|ifdef|ifndef|define)\b/;
584       foreach (split (/\W+/))
585         {
586           $prohibited{$_} = $.
587             if !/^$/ && /$forbidden/o && !/$allowed/o && ! exists $prohibited{$_};
588         }
590       # Performed *last*: the empty quadrigraph.
591       $res =~ s/\@&t\@//g;
593       print $out "$res\n";
594     }
596   # If no forbidden words, we're done.
597   return
598     if ! %prohibited;
600   # Locate the forbidden words in the last input file.
601   # This is unsatisfying but...
602   $exit_code = 1;
603   if ($ARGV[$#ARGV] ne '-')
604     {
605       my $prohibited = '\b(' . join ('|', keys %prohibited) . ')\b';
606       my $file = new Autom4te::XFile ($ARGV[$#ARGV]);
608       while ($_ = $file->getline)
609         {
610           # Don't complain in comments.  Well, until we have something
611           # better, don't consider `#include' etc. to be comments.
612           s/\#.*//
613             unless /^\#(if|include|endif|ifdef|ifndef|define)\b/;
615           # Complain once per word, but possibly several times per line.
616           while (/$prohibited/)
617             {
618               my $word = $1;
619               warn_forbidden ("$ARGV[$#ARGV]:$.", $word, %forbidden);
620               delete $prohibited{$word};
621               # If we're done, exit.
622               return
623                 if ! %prohibited;
624               $prohibited = '\b(' . join ('|', keys %prohibited) . ')\b';
625             }
626         }
627     }
628   warn_forbidden ("$output:$prohibited{$_}", $_, %forbidden)
629     foreach (sort { $prohibited{$a} <=> $prohibited{$b} } keys %prohibited);
633 ## --------------------- ##
634 ## Handling the traces.  ##
635 ## --------------------- ##
638 # $M4_MACRO
639 # trace_format_to_m4 ($FORMAT)
640 # ----------------------------
641 # Convert a trace $FORMAT into a M4 trace processing macro's body.
642 sub trace_format_to_m4 ($)
644   my ($format) = @_;
645   my $underscore = $_;
646   my %escape = (# File name.
647                 'f' => '$1',
648                 # Line number.
649                 'l' => '$2',
650                 # Depth.
651                 'd' => '$3',
652                 # Name (also available as $0).
653                 'n' => '$4',
654                 # Escaped dollar.
655                 '$' => '$');
657   my $res = '';
658   $_ = $format;
659   while ($_)
660     {
661       # $n -> $(n + 4)
662       if (s/^\$(\d+)//)
663         {
664           $res .= "\$" . ($1 + 4);
665         }
666       # $x, no separator given.
667       elsif (s/^\$([fldn\$])//)
668         {
669           $res .= $escape{$1};
670         }
671       # $.x or ${sep}x.
672       elsif (s/^\$\{([^}]*)\}([@*%])//
673             || s/^\$(.?)([@*%])//)
674         {
675           # $@, list of quoted effective arguments.
676           if ($2 eq '@')
677             {
678               $res .= ']at_at([' . ($1 ? $1 : ',') . '], $@)[';
679             }
680           # $*, list of unquoted effective arguments.
681           elsif ($2 eq '*')
682             {
683               $res .= ']at_star([' . ($1 ? $1 : ',') . '], $@)[';
684             }
685           # $%, list of flattened unquoted effective arguments.
686           elsif ($2 eq '%')
687             {
688               $res .= ']at_percent([' . ($1 ? $1 : ':') . '], $@)[';
689             }
690         }
691       elsif (/^(\$.)/)
692         {
693           error "invalid escape: $1";
694         }
695       else
696         {
697           s/^([^\$]+)//;
698           $res .= $1;
699         }
700     }
702   $_ = $underscore;
703   return '[[' . $res . ']]';
707 # handle_traces($REQ, $OUTPUT, %TRACE)
708 # ------------------------------------
709 # We use M4 itself to process the traces.  But to avoid name clashes when
710 # processing the traces, the builtins are disabled, and moved into `at_'.
711 # Actually, all the low level processing macros are in `at_' (and `_at_').
712 # To avoid clashes between user macros and `at_' macros, the macros which
713 # implement tracing are in `AT_'.
715 # Having $REQ is needed to neutralize the macros which have been traced,
716 # but are not wanted now.
717 sub handle_traces ($$%)
719   my ($req, $output, %trace) = @_;
721   verb "formatting traces for `$output': " . join (', ', sort keys %trace);
723   # Processing the traces.
724   my $trace_m4 = new Autom4te::XFile (">$tmp/traces.m4");
726   $_ = <<'EOF';
727   divert(-1)
728   changequote([, ])
729   # _at_MODE(SEPARATOR, ELT1, ELT2...)
730   # ----------------------------------
731   # List the elements, separating then with SEPARATOR.
732   # MODE can be:
733   #  `at'       -- the elements are enclosed in brackets.
734   #  `star'     -- the elements are listed as are.
735   #  `percent'  -- the elements are `flattened': spaces are singled out,
736   #                and no new line remains.
737   define([_at_at],
738   [at_ifelse([$#], [1], [],
739              [$#], [2], [[[$2]]],
740              [[[$2]][$1]$0([$1], at_shift(at_shift($@)))])])
742   define([_at_percent],
743   [at_ifelse([$#], [1], [],
744              [$#], [2], [at_flatten([$2])],
745              [at_flatten([$2])[$1]$0([$1], at_shift(at_shift($@)))])])
747   define([_at_star],
748   [at_ifelse([$#], [1], [],
749              [$#], [2], [[$2]],
750              [[$2][$1]$0([$1], at_shift(at_shift($@)))])])
752   # FLATTEN quotes its result.
753   # Note that the second pattern is `newline, tab or space'.  Don't lose
754   # the tab!
755   define([at_flatten],
756   [at_patsubst(at_patsubst(at_patsubst(at_patsubst([[[[$1]]]], [\\\n]),
757                                        [[\n\t ]+], [ ]),
758                            [ *\(.\)$], [\1]),
759                [^ *\(.*\)], [[\1]])])
761   define([at_args],    [at_shift(at_shift(at_shift(at_shift(at_shift($@)))))])
762   define([at_at],      [_$0([$1], at_args($@))])
763   define([at_percent], [_$0([$1], at_args($@))])
764   define([at_star],    [_$0([$1], at_args($@))])
767   s/^  //mg;s/\\t/\t/mg;s/\\n/\n/mg;
768   print $trace_m4 $_;
770   # If you trace `define', then on `define([m4_exit], defn([m4exit])' you
771   # will produce
772   #
773   #    AT_define([m4sugar.m4], [115], [1], [define], [m4_exit], <m4exit>)
774   #
775   # Since `<m4exit>' is not quoted, the outer m4, when processing
776   # `trace.m4' will exit prematurely.  Hence, move all the builtins to
777   # the `at_' name space.
779   print $trace_m4 "# Copy the builtins.\n";
780   map { print $trace_m4 "define([at_$_], defn([$_]))\n" } @m4_builtin;
781   print $trace_m4 "\n";
783   print $trace_m4 "# Disable them.\n";
784   map { print $trace_m4 "at_undefine([$_])\n" } @m4_builtin;
785   print $trace_m4 "\n";
788   # Neutralize traces: we don't want traces of cached requests (%REQUEST).
789   print $trace_m4
790    "## -------------------------------------- ##\n",
791    "## By default neutralize all the traces.  ##\n",
792    "## -------------------------------------- ##\n",
793    "\n";
794   print $trace_m4 "at_define([AT_$_], [at_dnl])\n"
795     foreach (sort keys %{$req->macro});
796   print $trace_m4 "\n";
798   # Implement traces for current requests (%TRACE).
799   print $trace_m4
800     "## ------------------------- ##\n",
801     "## Trace processing macros.  ##\n",
802     "## ------------------------- ##\n",
803     "\n";
804   foreach (sort keys %trace)
805     {
806       # Trace request can be embed \n.
807       (my $comment = "Trace $_:$trace{$_}") =~ s/^/\# /;
808       print $trace_m4 "$comment\n";
809       print $trace_m4 "at_define([AT_$_],\n";
810       print $trace_m4 trace_format_to_m4 ($trace{$_}) . ")\n\n";
811     }
812   print $trace_m4 "\n";
814   # Reenable output.
815   print $trace_m4 "at_divert(0)at_dnl\n";
817   # Transform the traces from m4 into an m4 input file.
818   # Typically, transform:
819   #
820   # | m4trace:configure.ac:3: -1- AC_SUBST([exec_prefix], [NONE])
821   #
822   # into
823   #
824   # | AT_AC_SUBST([configure.ac], [3], [1], [AC_SUBST], [exec_prefix], [NONE])
825   #
826   # Pay attention that the file name might include colons, if under DOS
827   # for instance, so we don't use `[^:]+'.
828   my $traces = new Autom4te::XFile ($tcache . $req->id);
829   while ($_ = $traces->getline)
830     {
831       # Trace with arguments, as the example above.  We don't try
832       # to match the trailing parenthesis as it might be on a
833       # separate line.
834       s{^m4trace:(.+):(\d+): -(\d+)- ([^(]+)\((.*)$}
835        {AT_$4([$1], [$2], [$3], [$4], $5};
836       # Traces without arguments, always on a single line.
837       s{^m4trace:(.+):(\d+): -(\d+)- ([^)]*)\n$}
838        {AT_$4([$1], [$2], [$3], [$4])\n};
839       print $trace_m4 "$_";
840     }
841   $trace_m4->close;
843   my $in = new Autom4te::XFile ("$m4 $tmp/traces.m4 |");
844   my $out = new Autom4te::XFile (">$output");
846   # This is dubious: should we really transform the quadrigraphs in
847   # traces?  It might break balanced [ ] etc. in the output.  The
848   # consensus seeems to be that traces are more useful this way.
849   while ($_ = $in->getline)
850     {
851       # It makes no sense to try to transform __oline__.
852       s/\@<:\@/[/g;
853       s/\@:>\@/]/g;
854       s/\@S\|\@/\$/g;
855       s/\@%:\@/#/g;
856       s/\@&t\@//g;
857       print $out $_;
858     }
862 # $BOOL
863 # up_to_date ($REQ)
864 # -----------------
865 # Are the cache files of $REQ up to date?
866 # $REQ is `valid' if it corresponds to the request and exists, which
867 # does not mean it is up to date.  It is up to date if, in addition,
868 # its files are younger than its dependencies.
869 sub up_to_date ($)
871   my ($req) = @_;
873   return 0
874     if ! $req->valid;
876   my $tfile = $tcache . $req->id;
877   my $ofile = $ocache . $req->id;
879   # We can't answer properly if the traces are not computed since we
880   # need to know what other files were included.  Actually, if any of
881   # the cache files is missing, we are not up to date.
882   return 0
883     if ! -f $tfile || ! -f $ofile;
885   # The youngest of the cache files must be older than the oldest of
886   # the dependencies.
887   my $tmtime = mtime ($tfile);
888   my $omtime = mtime ($ofile);
889   my ($file, $mtime) = ($tmtime < $omtime
890                         ? ($ofile, $omtime) : ($tfile, $tmtime));
892   # We depend at least upon the arguments.
893   my @dep = @ARGV;
895   # stdin is always out of date.
896   if (grep { $_ eq '-' } @dep)
897     { return 0 }
899   # Files may include others.  We can use traces since we just checked
900   # if they are available.
901   handle_traces ($req, "$tmp/dependencies",
902                  ('include'    => '$1',
903                   'm4_include' => '$1'));
904   my $deps = new Autom4te::XFile ("$tmp/dependencies");
905   while ($_ = $deps->getline)
906     {
907       chomp;
908       my $file = find_file ("$_?", @include);
909       # If a file which used to be included is no longer there, then
910       # don't say it's missing (it might no longer be included).  But
911       # of course, that causes the output to be outdated (as if the
912       # time stamp of that missing file was newer).
913       return 0
914         if ! $file;
915       push @dep, $file;
916     }
918   # If $FILE is younger than one of its dependencies, it is outdated.
919   return up_to_date_p ($file, @dep);
923 ## ---------- ##
924 ## Freezing.  ##
925 ## ---------- ##
927 # freeze ($OUTPUT)
928 # ----------------
929 sub freeze ($)
931   my ($output) = @_;
933   # When processing the file with diversion disabled, there must be no
934   # output but comments and empty lines.
935   my $result = xqx ("$m4"
936                     . ' --fatal-warning'
937                     . join (' --include=', '', @include)
938                     . ' --define=divert'
939                     . " " . files_to_options (@ARGV)
940                     . ' </dev/null');
941   $result =~ s/#.*\n//g;
942   $result =~ s/^\n//mg;
944   fatal "freezing produced output:\n$result"
945     if $result;
947   # If freezing produces output, something went wrong: a bad `divert',
948   # or an improper paren etc.
949   xsystem ("$m4"
950            . ' --fatal-warning'
951            . join (' --include=', '', @include)
952            . " --freeze-state=$output"
953            . " " . files_to_options (@ARGV)
954            . ' </dev/null');
957 ## -------------- ##
958 ## Main program.  ##
959 ## -------------- ##
961 mktmpdir ('am4t');
962 load_configuration ($ENV{'AUTOM4TE_CFG'} || "$datadir/autom4te.cfg");
963 load_configuration ("$ENV{'HOME'}/.autom4te.cfg")
964   if exists $ENV{'HOME'} && -f "$ENV{'HOME'}/.autom4te.cfg";
965 load_configuration (".autom4te.cfg")
966   if -f ".autom4te.cfg";
967 parse_args;
969 # Freezing does not involve the cache.
970 if ($freeze)
971   {
972     freeze ($output);
973     exit $exit_code;
974   }
976 # We need our cache directory.
977 if (! -d "$cache")
978   {
979     mkdir "$cache", 0755
980       or fatal "cannot create $cache: $!";
981   }
983 # Open the index for update, and lock it.  autom4te handles several
984 # files, but the index is the first and last file to be updated, so
985 # locking it is sufficient.
986 $icache_file = new Autom4te::XFile $icache, O_RDWR|O_CREAT;
987 $icache_file->lock (LOCK_EX);
989 # Read the cache index if available and older than autom4te itself.
990 # If autom4te is younger, then some structures such as C4che might
991 # have changed, which would corrupt its processing.
992 Autom4te::C4che->load ($icache_file)
993   if -f $icache && mtime ($icache) > mtime ($0);
995 # Add the new trace requests.
996 my $req = Autom4te::C4che->request ('input' => \@ARGV,
997                                     'path'  => \@include,
998                                     'macro' => [keys %trace, @preselect]);
1000 # If $REQ's cache files are not up to date, or simply if the user
1001 # discarded them (-f), declare it invalid.
1002 $req->valid (0)
1003   if $force || ! up_to_date ($req);
1005 # We now know whether we can trust the Request object.  Say it.
1006 verb "the trace request object is:\n" . $req->marshall;
1008 # We need to run M4 if (i) the user wants it (--force), (ii) $REQ is
1009 # invalid.
1010 handle_m4 ($req, keys %{$req->macro})
1011   if $force || ! $req->valid;
1013 # Issue the warnings each time autom4te was run.
1014 my $separator = "\n" . ('-' x 25) . " END OF WARNING " . ('-' x 25) . "\n\n";
1015 handle_traces ($req, "$tmp/warnings",
1016                ('_m4_warn' => "\$1::\$f:\$l::\$2::\$3$separator"));
1017 # Swallow excessive newlines.
1018 for (split (/\n*$separator\n*/o, contents ("$tmp/warnings")))
1020   # The message looks like:
1021   # | syntax::input.as:5::ouch
1022   # | ::input.as:4: baz is expanded from...
1023   # | input.as:2: bar is expanded from...
1024   # | input.as:3: foo is expanded from...
1025   # | input.as:5: the top level
1026   my ($cat, $loc, $msg, $stacktrace) = split ('::', $_, 4);
1027   msg $cat, $loc, "warning: $msg";
1028   for (split /\n/, $stacktrace)
1029     {
1030       my ($loc, $trace) = split (': ', $_, 2);
1031       msg $cat, $loc, $trace;
1032     }
1035 # Now output...
1036 if (%trace)
1037   {
1038     # Always produce traces, since even if the output is young enough,
1039     # there is no guarantee that the traces use the same *format*
1040     # (e.g., `-t FOO:foo' and `-t FOO:bar' are both using the same M4
1041     # traces, hence the M4 traces cache is usable, but its formatting
1042     # will yield different results).
1043     handle_traces ($req, $output, %trace);
1044   }
1045 else
1046   {
1047     # Actual M4 expansion, if the user wants it, or if $output is old
1048     # (STDOUT is pretty old).
1049     handle_output ($req, $output)
1050       if $force || mtime ($output) < mtime ($ocache . $req->id);
1051   }
1053 # If we ran up to here, the cache is valid.
1054 $req->valid (1);
1055 Autom4te::C4che->save ($icache_file);
1057 exit $exit_code;
1059 ### Setup "GNU" style for perl-mode and cperl-mode.
1060 ## Local Variables:
1061 ## perl-indent-level: 2
1062 ## perl-continued-statement-offset: 2
1063 ## perl-continued-brace-offset: 0
1064 ## perl-brace-offset: 0
1065 ## perl-brace-imaginary-offset: 0
1066 ## perl-label-offset: -2
1067 ## cperl-indent-level: 2
1068 ## cperl-brace-offset: 0
1069 ## cperl-continued-brace-offset: 0
1070 ## cperl-label-offset: -2
1071 ## cperl-extra-newline-before-brace: t
1072 ## cperl-merge-trailing-else: nil
1073 ## cperl-continued-statement-offset: 2
1074 ## End: