5 eval 'case $# in 0) exec @PERL@ -S "$0";; *) exec @PERL@ -S "$0" "$@";; esac'
8 # autom4te - Wrapper around M4 libraries.
9 # Copyright (C) 2001-2003, 2005-2017, 2020-2023 Free Software
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 <https://www.gnu.org/licenses/>.
27 use warnings FATAL => 'all';
31 my $pkgdatadir = $ENV{'autom4te_perllibdir'} || '@pkgdatadir@';
32 unshift @INC, $pkgdatadir;
34 # Override SHELL. On DJGPP SHELL may not be set to a shell
35 # that can handle redirection and quote arguments correctly,
36 # e.g.: COMMAND.COM. For DJGPP always use the shell that configure
38 $ENV{'SHELL'} = '@SHELL@' if ($^O eq 'dos');
44 use Autom4te::ChannelDefs;
45 use Autom4te::Channels;
46 use Autom4te::FileUtils;
47 use Autom4te::General;
51 my $pkgdatadir = $ENV{'AC_MACRODIR'} || '@pkgdatadir@';
53 # $LANGUAGE{LANGUAGE} -- Automatic options for LANGUAGE.
58 # Mode of the output file except for traces.
61 # If melt, don't use frozen files.
64 # Names of the cache directory, cache directory index, trace cache
65 # prefix, and output cache prefix. And the IO object for the index.
72 my $flock_implemented = '@PERL_FLOCK@';
74 # The macros to trace mapped to their format, as specified by the
78 # The macros the user will want to trace in the future.
79 # We need 'include' to get the included file, 'm4_pattern_forbid' and
80 # 'm4_pattern_allow' to check the output.
82 # FIXME: What about 'sinclude'?
83 my @preselect = ('include',
84 'm4_pattern_allow', 'm4_pattern_forbid',
94 my $m4 = $ENV{"M4"} || '@M4@';
95 # Some non-GNU m4's don't reject the --help option, so give them /dev/null.
96 fatal "need GNU m4 1.4 or later: $m4"
97 if system "$m4 --help </dev/null 2>&1 | grep reload-state >/dev/null";
99 # Set some high recursion limit as the default limit, 250, has already
100 # been hit with AC_OUTPUT. Don't override the user's choice.
101 $m4 .= ' --nesting-limit=1024'
102 if " $m4 " !~ / (--nesting-limit(=[0-9]+)?|-L[0-9]*) /;
105 # @M4_BUILTIN -- M4 builtins and a useful comment.
106 my @m4_builtin = `echo dumpdef | $m4 2>&1 >/dev/null`;
107 map { s/:.*//;s/\W// } @m4_builtin;
110 # %M4_BUILTIN_ALTERNATE_NAME
111 # --------------------------
112 # The builtins are renamed, e.g., 'define' is renamed 'm4_define'.
113 # So map 'define' to 'm4_define' and conversely.
114 # Some macros don't follow this scheme: be sure to properly map to their
115 # alternate name too.
117 # FIXME: Trace status of renamed builtins was fixed in M4 1.4.5, which
118 # we now depend on; do we still need to do this mapping?
120 # So we will merge them, i.e., tracing 'BUILTIN' or tracing
121 # 'm4_BUILTIN' will be the same: tracing both, but honoring the
122 # *last* trace specification.
124 # FIXME: This is not enough: in the output '$0' will be 'BUILTIN'
125 # sometimes and 'm4_BUILTIN' at others. We should return a unique name,
126 # the one specified by the user.
128 # FIXME: To be absolutely rigorous, I would say that given that we
129 # _redefine_ divert (instead of _copying_ it), divert and the like
130 # should not be part of this list.
131 my %m4_builtin_alternate_name;
132 @m4_builtin_alternate_name{"$_", "m4_$_"} = ("m4_$_", "$_")
133 foreach (grep { !/m4wrap|m4exit|dnl|ifelse|__.*__/ } @m4_builtin);
134 @m4_builtin_alternate_name{"ifelse", "m4_if"} = ("m4_if", "ifelse");
135 @m4_builtin_alternate_name{"m4exit", "m4_exit"} = ("m4_exit", "m4exit");
136 @m4_builtin_alternate_name{"m4wrap", "m4_wrap"} = ("m4_wrap", "m4wrap");
141 $help = "Usage: $0 [OPTION]... [FILES]
143 Run GNU M4 on the FILES, avoiding useless runs. Output the traces if tracing,
144 the frozen file if freezing, otherwise the expansion of the FILES.
146 If some of the FILES are named 'FILE.m4f' they are considered to be M4
147 frozen files of all the previous files (which are therefore not loaded).
148 If 'FILE.m4f' is not found, then 'FILE.m4' will be used, together with
149 all the previous files.
151 Some files may be optional, i.e., will only be processed if found in the
152 include path, but then must end in '.m4?'; the question mark is not part
153 of the actual file name.
156 -h, --help print this help, then exit
157 -V, --version print version number, then exit
158 -v, --verbose verbosely report processing
159 -d, --debug don't remove temporary files
160 -o, --output=FILE save output in FILE (defaults to '-', stdout)
161 -f, --force don't rely on cached values
162 -W, --warnings=CATEGORY report the warnings falling in CATEGORY
163 -l, --language=LANG specify the set of M4 macros to use
164 -C, --cache=DIRECTORY preserve results for future runs in DIRECTORY
165 --no-cache disable the cache
166 -m, --mode=OCTAL change the non trace output file mode (0666)
167 -M, --melt don't use M4 frozen files
170 'Autoconf' create Autoconf configure scripts
171 'Autotest' create Autotest test suites
172 'M4sh' create M4sh shell scripts
173 'M4sugar' create M4sugar output
175 " . Autom4te::ChannelDefs::usage . "
177 The environment variables 'M4' and 'WARNINGS' are honored.
180 -B, --prepend-include=DIR prepend directory DIR to search path
181 -I, --include=DIR append directory DIR to search path
184 -t, --trace=MACRO[:FORMAT] report the MACRO invocations
185 -p, --preselect=MACRO prepare to trace MACRO in a future run
188 -F, --freeze produce an M4 frozen state file for FILES
190 FORMAT defaults to '\$f:\$l:\$n:\$%', and can use the following escapes:
192 \$f file where macro was called
193 \$l line where macro was called
194 \$d nesting depth of macro call
195 \$n name of the macro
196 \$NUM argument NUM, unquoted and with newlines
197 \$SEP\@ all arguments, with newlines, quoted, and separated by SEP
198 \$SEP* all arguments, with newlines, unquoted, and separated by SEP
199 \$SEP% all arguments, without newlines, unquoted, and separated by SEP
200 SEP can be empty for the default (comma for \@ and *, colon for %),
201 a single character for that character, or {STRING} to use a string.
203 Report bugs to <bug-autoconf\@gnu.org>.
204 GNU Autoconf home page: <https://www.gnu.org/software/autoconf/>.
205 General help using GNU software: <https://www.gnu.org/gethelp/>.
210 $version = "autom4te (@PACKAGE_NAME@) @VERSION@
211 Copyright (C) @RELEASE_YEAR@ Free Software Foundation, Inc.
212 License GPLv3+/Autoconf: GNU GPL version 3 or later
213 <https://gnu.org/licenses/gpl.html>, <https://gnu.org/licenses/exceptions.html>
214 This is free software: you are free to change and redistribute it.
215 There is NO WARRANTY, to the extent permitted by law.
217 Written by Akim Demaille.
227 # files_to_options (@FILE)
228 # ------------------------
229 # Transform Autom4te conventions (e.g., using foo.m4f to designate a frozen
230 # file) into a suitable command line for M4 (e.g., using --reload-state).
231 # parse_args guarantees that we will see at most one frozen file, and that
232 # if a frozen file is present, it is the first argument.
233 sub files_to_options (@)
237 foreach my $file (@file)
239 my $arg = shell_quote ($file);
240 if ($file =~ /\.m4f$/)
242 $arg = "--reload-state=$arg";
243 # If the user downgraded M4 from 1.6 to 1.4.x after freezing
244 # the file, then we ensure the frozen __m4_version__ will
245 # not cause m4_init to make the wrong decision about the
246 # current M4 version.
247 $arg .= " --undefine=__m4_version__"
248 unless grep {/__m4_version__/} @m4_builtin;
252 return join ' ', @res;
256 # load_configuration ($FILE)
257 # --------------------------
258 # Load the configuration $FILE.
259 sub load_configuration ($)
262 use Text::ParseWords;
264 my $cfg = new Autom4te::XFile ($file, "<");
266 while ($_ = $cfg->getline)
273 my @words = shellwords ($_);
274 my $type = shift @words;
275 if ($type eq 'begin-language:')
277 fatal "$file:$.: end-language missing for: $lang"
279 $lang = lc $words[0];
281 elsif ($type eq 'end-language:')
283 error "$file:$.: end-language mismatch: $lang"
284 if $lang ne lc $words[0];
287 elsif ($type eq 'args:')
289 fatal "$file:$.: no current language"
290 unless defined $lang;
291 push @{$language{$lang}}, @words;
295 error "$file:$.: unknown directive: $type";
303 # Process any command line arguments.
306 # We want to look for the early options, which should not be found
307 # in the configuration file. Prepend to the user arguments.
308 # Perform this repeatedly so that we can use --language in language
309 # definitions. Beware that there can be several --language
315 Getopt::Long::Configure ("pass_through", "permute");
316 GetOptions ("l|language=s" => \@language);
320 error "unknown language: $_"
321 unless exists $language{lc $_};
322 unshift @ARGV, @{$language{lc $_}};
326 # --debug is useless: it is parsed below.
327 if (exists $ENV{'AUTOM4TE_DEBUG'})
329 print STDERR "$me: concrete arguments:\n";
330 foreach my $arg (@ARGV)
332 print STDERR "| $arg\n";
336 # Process the arguments for real this time.
344 "o|output=s" => \$output,
345 "W|warnings=s" => \@warnings,
346 "m|mode=s" => \$mode,
349 # Library directories:
350 "B|prepend-include=s" => \@prepend_include,
351 "I|include=s" => \@include,
354 # Using a hash for traces is seducing. Unfortunately, upon '-t FOO',
355 # instead of mapping 'FOO' to undef, Getopt maps it to '1', preventing
356 # us from distinguishing '-t FOO' from '-t FOO=1'. So let's do it
358 "t|trace=s" => \@trace,
359 "p|preselect=s" => \@preselect,
362 "F|freeze" => \$freeze,
365 "C|cache=s" => \$cache,
366 "no-cache" => sub { $cache = undef; },
370 parse_warnings @warnings;
372 fatal "too few arguments
373 Try '$me --help' for more information."
377 # We cannot trace at the same time (well, we can, but it sounds insane).
378 # And it implies melting: there is risk not to update properly using
379 # old frozen files, and worse yet: we could load a frozen file and
380 # refreeze it! A sort of caching :)
381 fatal "cannot freeze and trace"
382 if $freeze && @trace;
386 # Names of the cache directory, cache directory index, trace cache
387 # prefix, and output cache prefix. If the cache is not to be
388 # preserved, default to a temporary directory (automatically removed
392 $icache = "$cache/requests";
393 $tcache = "$cache/traces.";
394 $ocache = "$cache/output.";
396 # Normalize the includes: the first occurrence is enough, several is
397 # a pain since it introduces a useless difference in the path which
398 # invalidates the cache. And strip '.' which is implicit and always
400 @include = grep { !/^\.$/ } uniq (reverse(@prepend_include), @include);
402 # Convert @trace to %trace, and work around the M4 builtins tracing
404 # The default format is '$f:$l:$n:$%'.
407 /^([^:]+)(?::(.*))?$/ms;
408 $trace{$1} = defined $2 ? $2 : '$f:$l:$n:$%';
409 $trace{$m4_builtin_alternate_name{$1}} = $trace{$1}
410 if exists $m4_builtin_alternate_name{$1};
413 # Work around the M4 builtins tracing problem for @PRESELECT.
414 # FIXME: Is this still needed, now that we rely on M4 1.4.5?
416 map { $m4_builtin_alternate_name{$_} }
417 grep { exists $m4_builtin_alternate_name{$_} } @preselect);
419 # If we find frozen files, then all the files before it are
420 # discarded: the frozen file is supposed to include them all.
422 # We don't want to depend upon m4's --include to find the top level
423 # files, so we use 'find_file' here. Try to get a canonical name,
424 # as it's part of the key for caching. And some files are optional
425 # (also handled by 'find_file').
435 # Frozen files are optional => pass a '?' to 'find_file'.
436 my $file = find_file ("$_?", @include);
444 push @argv, find_file ($_, @include);
449 my $file = find_file ($_, @include);
458 # handle_m4 ($REQ, @MACRO)
459 # ------------------------
460 # Run m4 on the input files, and save the traces on the @MACRO.
463 my ($req, @macro) = @_;
465 # GNU m4 appends when using --debugfile/--error-output.
466 unlink ($tcache . $req->id . "t");
470 # We don't output directly to the cache files, to avoid problems
471 # when we are interrupted (that leaves corrupted files).
472 xsystem ("$m4 @M4_GNU@"
473 . join (' --include=', '', map { shell_quote ($_) } @include)
475 . (!exists $ENV{'AUTOM4TE_NO_FATAL'} ? ' --fatal-warning' : '')
476 . " @M4_DEBUGFILE@=" . shell_quote ("$tcache" . $req->id . "t")
477 . join (' --trace=', '', map { shell_quote ($_) } sort @macro)
478 . " " . files_to_options (@ARGV)
479 . " > " . shell_quote ("$ocache" . $req->id . "t"));
481 # Everything went ok: preserve the outputs.
482 foreach my $file (map { $_ . $req->id } ($tcache, $ocache))
485 move ("${file}t", "$file")
486 or fatal "cannot rename ${file}t as $file: $!";
491 # warn_forbidden ($WHERE, $WORD, %FORBIDDEN)
492 # ------------------------------------------
493 # $WORD is forbidden. Warn with a dedicated error message if in
494 # %FORBIDDEN, otherwise a simple 'error: possibly undefined macro'
496 my $first_warn_forbidden = 1;
497 sub warn_forbidden ($$%)
499 my ($where, $word, %forbidden) = @_;
502 for my $re (sort keys %forbidden)
506 $message = $forbidden{$re};
510 $message ||= "possibly undefined macro: $word";
511 warn "$where: error: $message\n";
512 if ($first_warn_forbidden)
515 If this token and others are legitimate, please use m4_pattern_allow.
516 See the Autoconf documentation.
518 $first_warn_forbidden = 0;
523 # handle_output ($REQ, $OUTPUT)
524 # -----------------------------
525 # Run m4 on the input files, perform quadrigraphs substitution, check for
526 # forbidden tokens, and save into $OUTPUT.
527 sub handle_output ($$)
529 my ($req, $output) = @_;
531 verb "creating $output";
533 # Load the forbidden/allowed patterns.
534 handle_traces ($req, "$tmp/patterns",
535 ('m4_pattern_forbid' => 'forbid:$1:$2',
536 'm4_pattern_allow' => 'allow:$1'));
537 my @patterns = new Autom4te::XFile ("$tmp/patterns", "<")->getlines;
540 map { /^forbid:([^:]+):.+$/ => /^forbid:[^:]+:(.+)$/ } @patterns;
541 my $forbidden = join ('|', map { /^forbid:([^:]+)/ } @patterns) || "^\$";
542 my $allowed = join ('|', map { /^allow:([^:]+)/ } @patterns) || "^\$";
544 verb "forbidden tokens: $forbidden";
545 verb "forbidden token : $_ => $forbidden{$_}"
546 foreach (sort keys %forbidden);
547 verb "allowed tokens: $allowed";
549 # Read the (cached) raw M4 output, produce the actual result.
550 # If we are writing to a regular file, replace it atomically.
555 # Don't just make $out be STDOUT, because then we would close STDOUT,
556 # which we already do in END.
557 $out = new Autom4te::XFile ('>&STDOUT');
559 elsif (-e $output && ! -f $output)
561 $out = new Autom4te::XFile ($output, '>');
565 my (undef, $outdir, undef) = fileparse ($output);
567 use File::Temp qw (tempfile);
568 ($out, $scratchfile) = tempfile (UNLINK => 0, DIR => $outdir);
569 fatal "cannot create a file in $outdir: $!"
572 # File::Temp doesn't give us access to 3-arg open(2), unfortunately.
573 chmod (oct ($mode) & ~(umask), $scratchfile)
574 or fatal "setting mode of " . $scratchfile . ": $!";
577 my $in = new Autom4te::XFile ($ocache . $req->id, "<");
581 while ($_ = $in->getline)
594 # Don't complain in comments. Well, until we have something
595 # better, don't consider '#include' etc. are comments.
597 unless /^\#\s*(if|include|endif|ifdef|ifndef|define)\b/;
598 foreach (split (/\W+/))
601 if !/^$/ && /$forbidden/o && !/$allowed/o
602 && ! exists $prohibited{$_};
605 # Performed *last*: the empty quadrigraph.
612 # Always update the file, even if it didn't change;
613 # Automake relies on this.
614 update_file ($scratchfile, $output, 1)
615 if defined $scratchfile;
617 # If no forbidden words, we're done.
621 # Locate the forbidden words in the last input file.
622 # This is unsatisfying but...
624 if ($ARGV[$#ARGV] ne '-')
626 my $prohibited = '\b(' . join ('|', keys %prohibited) . ')\b';
627 my $file = new Autom4te::XFile ($ARGV[$#ARGV], "<");
629 while ($_ = $file->getline)
631 # Don't complain in comments. Well, until we have something
632 # better, don't consider '#include' etc. to be comments.
634 unless /^\#(if|include|endif|ifdef|ifndef|define)\b/;
636 # Complain once per word, but possibly several times per line.
637 while (/$prohibited/)
640 warn_forbidden ("$ARGV[$#ARGV]:$.", $word, %forbidden);
641 delete $prohibited{$word};
642 # If we're done, exit.
645 $prohibited = '\b(' . join ('|', keys %prohibited) . ')\b';
649 warn_forbidden ("$output:$prohibited{$_}", $_, %forbidden)
650 foreach (sort { $prohibited{$a} <=> $prohibited{$b} } keys %prohibited);
654 ## --------------------- ##
655 ## Handling the traces. ##
656 ## --------------------- ##
660 # trace_format_to_m4 ($FORMAT)
661 # ----------------------------
662 # Convert a trace $FORMAT into a M4 trace processing macro's body.
663 sub trace_format_to_m4 ($)
667 my %escape = (# File name.
673 # Name (also available as $0).
685 $res .= "\$" . ($1 + 4);
687 # $x, no separator given.
688 elsif (s/^\$([fldn\$])//)
693 elsif (s/^\$\{([^}]*)\}([@*%])//
694 || s/^\$(.?)([@*%])//)
696 # $@, list of quoted effective arguments.
699 $res .= ']at_at([' . ($1 ? $1 : ',') . '], $@)[';
701 # $*, list of unquoted effective arguments.
704 $res .= ']at_star([' . ($1 ? $1 : ',') . '], $@)[';
706 # $%, list of flattened unquoted effective arguments.
709 $res .= ']at_percent([' . ($1 ? $1 : ':') . '], $@)[';
714 error "invalid escape: $1";
724 return '[[' . $res . ']]';
728 # handle_traces($REQ, $OUTPUT, %TRACE)
729 # ------------------------------------
730 # We use M4 itself to process the traces. But to avoid name clashes when
731 # processing the traces, the builtins are disabled, and moved into 'at_'.
732 # Actually, all the low level processing macros are in 'at_' (and '_at_').
733 # To avoid clashes between user macros and 'at_' macros, the macros which
734 # implement tracing are in 'AT_'.
736 # Having $REQ is needed to neutralize the macros which have been traced,
737 # but are not wanted now.
738 sub handle_traces ($$%)
740 my ($req, $output, %trace) = @_;
742 verb "formatting traces for '$output': " . join (', ', sort keys %trace);
744 # Processing the traces.
745 my $trace_m4 = new Autom4te::XFile ("$tmp/traces.m4", ">");
750 # _at_MODE(SEPARATOR, ELT1, ELT2...)
751 # ----------------------------------
752 # List the elements, separating then with SEPARATOR.
754 # 'at' -- the elements are enclosed in brackets.
755 # 'star' -- the elements are listed as are.
756 # 'percent' -- the elements are 'flattened': spaces are singled out,
757 # and no new line remains.
759 [at_ifelse([$#], [1], [],
761 [[[$2]][$1]$0([$1], at_shift(at_shift($@)))])])
763 define([_at_percent],
764 [at_ifelse([$#], [1], [],
765 [$#], [2], [at_flatten([$2])],
766 [at_flatten([$2])[$1]$0([$1], at_shift(at_shift($@)))])])
769 [at_ifelse([$#], [1], [],
771 [[$2][$1]$0([$1], at_shift(at_shift($@)))])])
773 # FLATTEN quotes its result.
774 # Note that the second pattern is 'newline, tab or space'. Don't lose
777 [at_patsubst(at_patsubst([[[$1]]], [\\\n]), [[\n\t ]+], [ ])])
779 define([at_args], [at_shift(at_shift(at_shift(at_shift(at_shift($@)))))])
780 define([at_at], [_$0([$1], at_args($@))])
781 define([at_percent], [_$0([$1], at_args($@))])
782 define([at_star], [_$0([$1], at_args($@))])
785 s/^ //mg;s/\\t/\t/mg;s/\\n/\n/mg;
788 # If you trace 'define', then on 'define([m4_exit], defn([m4exit])' you
791 # AT_define([m4sugar.m4], [115], [1], [define], [m4_exit], <m4exit>)
793 # Since '<m4exit>' is not quoted, the outer m4, when processing
794 # 'trace.m4' will exit prematurely. Hence, move all the builtins to
795 # the 'at_' name space.
797 print $trace_m4 "# Copy the builtins.\n";
798 map { print $trace_m4 "define([at_$_], defn([$_]))\n" } @m4_builtin;
799 print $trace_m4 "\n";
801 print $trace_m4 "# Disable them.\n";
802 map { print $trace_m4 "at_undefine([$_])\n" } @m4_builtin;
803 print $trace_m4 "\n";
806 # Neutralize traces: we don't want traces of cached requests (%REQUEST).
808 "## -------------------------------------- ##\n",
809 "## By default neutralize all the traces. ##\n",
810 "## -------------------------------------- ##\n",
812 print $trace_m4 "at_define([AT_$_], [at_dnl])\n"
813 foreach (sort keys %{$req->macro});
814 print $trace_m4 "\n";
816 # Implement traces for current requests (%TRACE).
818 "## ------------------------- ##\n",
819 "## Trace processing macros. ##\n",
820 "## ------------------------- ##\n",
822 foreach (sort keys %trace)
824 # Trace request can be embed \n.
825 (my $comment = "Trace $_:$trace{$_}") =~ s/^/\# /;
826 print $trace_m4 "$comment\n";
827 print $trace_m4 "at_define([AT_$_],\n";
828 print $trace_m4 trace_format_to_m4 ($trace{$_}) . ")\n\n";
830 print $trace_m4 "\n";
833 print $trace_m4 "at_divert(0)at_dnl\n";
835 # Transform the traces from m4 into an m4 input file.
836 # Typically, transform:
838 # | m4trace:configure.ac:3: -1- AC_SUBST([exec_prefix], [NONE])
842 # | AT_AC_SUBST([configure.ac], [3], [1], [AC_SUBST], [exec_prefix], [NONE])
844 # Pay attention that the file name might include colons, if under DOS
845 # for instance, so we don't use '[^:]+'.
846 my $traces = new Autom4te::XFile ($tcache . $req->id, "<");
847 while ($_ = $traces->getline)
849 # Trace with arguments, as the example above. We don't try
850 # to match the trailing parenthesis as it might be on a
852 s{^m4trace:(.+):(\d+): -(\d+)- ([^(]+)\((.*)$}
853 {AT_$4([$1], [$2], [$3], [$4], $5};
854 # Traces without arguments, always on a single line.
855 s{^m4trace:(.+):(\d+): -(\d+)- ([^)]*)\n$}
856 {AT_$4([$1], [$2], [$3], [$4])\n};
857 print $trace_m4 "$_";
861 my $in = new Autom4te::XFile ("$m4 " . shell_quote ("$tmp/traces.m4") . " |");
862 my $out = new Autom4te::XFile;
865 $out->open (">$output");
869 $out->open ($output, ">");
872 # This is dubious: should we really transform the quadrigraphs in
873 # traces? It might break balanced [ ] etc. in the output. The
874 # consensus seems to be that traces are more useful this way.
875 while ($_ = $in->getline)
877 # It makes no sense to try to transform __oline__.
893 # Are the cache files of $REQ up to date?
894 # $REQ is 'valid' if it corresponds to the request and exists, which
895 # does not mean it is up to date. It is up to date if, in addition,
896 # its files are younger than its dependencies.
904 my $tfile = $tcache . $req->id;
905 my $ofile = $ocache . $req->id;
907 # We can't answer properly if the traces are not computed since we
908 # need to know what other files were included. Actually, if any of
909 # the cache files is missing, we are not up to date.
911 if ! -f $tfile || ! -f $ofile;
913 # The youngest of the cache files must be older than the oldest of
915 # FIXME: These timestamps have only 1-second resolution.
916 # Time::HiRes fixes this, but assumes Perl 5.8 or later.
917 my $tmtime = mtime ($tfile);
918 my $omtime = mtime ($ofile);
919 my ($file, $mtime) = ($tmtime < $omtime
920 ? ($ofile, $omtime) : ($tfile, $tmtime));
922 # stdin is always out of date.
923 if (grep { $_ eq '-' } @ARGV)
926 # We depend at least upon the arguments.
927 foreach my $dep (@ARGV)
929 if ($mtime < mtime ($dep))
931 verb "up_to_date ($file): outdated: $dep";
936 # Files may include others. We can use traces since we just checked
937 # if they are available.
938 handle_traces ($req, "$tmp/dependencies",
940 'm4_include' => '$1'));
941 my $deps = new Autom4te::XFile ("$tmp/dependencies", "<");
942 while ($_ = $deps->getline)
945 my $dep = find_file ("$_?", @include);
946 # If a file which used to be included is no longer there, then
947 # don't say it's missing (it might no longer be included). But
948 # of course, that causes the output to be outdated (as if the
949 # timestamp of that missing file was newer).
952 if ($mtime < mtime ($dep))
954 verb "up_to_date ($file): outdated: $dep";
959 verb "up_to_date ($file): up to date";
974 # When processing the file with diversion disabled, there must be no
975 # output but comments and empty lines.
976 my $result = xqx ("$m4"
978 . join (' --include=', '', map { shell_quote ($_) } @include)
980 . " " . files_to_options (@ARGV)
982 $result =~ s/#.*\n//g;
983 $result =~ s/^\n//mg;
985 fatal "freezing produced output:\n$result"
988 # If freezing produces output, something went wrong: a bad 'divert',
989 # or an improper paren etc.
992 . join (' --include=', '', map { shell_quote ($_) } @include)
993 . " --freeze-state=" . shell_quote ($output)
994 . " " . files_to_options (@ARGV)
1000 ## -------------- ##
1003 load_configuration ($ENV{'AUTOM4TE_CFG'} || "$pkgdatadir/autom4te.cfg");
1004 load_configuration ("$ENV{'HOME'}/.autom4te.cfg")
1005 if exists $ENV{'HOME'} && -f "$ENV{'HOME'}/.autom4te.cfg";
1006 load_configuration (".autom4te.cfg")
1007 if -f ".autom4te.cfg";
1010 # Freezing does not involve the cache.
1017 # Ensure the cache directory exists.
1018 if (! mkdir ($cache, 0755))
1020 # Snapshot $! immediately, the next few operations may clobber it.
1021 my $eexist = $!{EEXIST};
1024 # If mkdir failed with EEXIST, that means the *name* $cache
1025 # already exists, but it might be the wrong kind of file.
1026 if (! $eexist || ! -d $cache)
1029 my $cwd = Cwd::cwd();
1030 fatal "cannot create $cache in $cwd: $errmsg";
1034 # Open the index for update, and lock it. autom4te handles several
1035 # files, but the index is the first and last file to be updated, so
1036 # locking it is sufficient.
1037 $icache_file = new Autom4te::XFile $icache, O_RDWR|O_CREAT;
1038 $icache_file->lock (LOCK_EX)
1039 if ($flock_implemented eq "yes");
1041 # Read the cache index if available and older than autom4te itself.
1042 # If autom4te is younger, then some structures such as C4che might
1043 # have changed, which would corrupt its processing.
1044 Autom4te::C4che->load ($icache_file)
1045 if (-f $icache && mtime ($icache) > mtime ($0)
1046 && Autom4te::C4che->good_version ($icache_file, '@VERSION@'));
1048 # Add the new trace requests.
1049 my $req = Autom4te::C4che->request ('input' => \@ARGV,
1050 'path' => \@include,
1051 'macro' => [keys %trace, @preselect]);
1053 # If $REQ's cache files are not up to date, or simply if the user
1054 # discarded them (-f), declare it invalid.
1056 if $force || ! up_to_date ($req);
1058 # We now know whether we can trust the Request object. Say it.
1059 verb "the trace request object is:\n" . $req->marshall;
1061 # We need to run M4 if (i) the user wants it (--force), (ii) $REQ is
1063 handle_m4 ($req, keys %{$req->macro})
1064 if $force || ! $req->valid;
1066 # Issue the warnings each time autom4te was run.
1067 my $separator = "\n" . ('-' x 25) . " END OF WARNING " . ('-' x 25) . "\n\n";
1068 handle_traces ($req, "$tmp/warnings",
1069 ('_m4_warn' => "\$1::\$f:\$l::\$2::\$3$separator"));
1070 # Swallow excessive newlines.
1071 for (split (/\n*$separator\n*/o, contents ("$tmp/warnings")))
1073 # The message looks like:
1074 # | syntax::input.as:5::ouch
1075 # | ::input.as:4: baz is expanded from...
1076 # | input.as:2: bar is expanded from...
1077 # | input.as:3: foo is expanded from...
1078 # | input.as:5: the top level
1079 # In particular, m4_warn guarantees that either $stackdump is empty, or
1080 # it consists of lines where only the last line ends in "top level".
1081 my ($cat, $loc, $msg, $stacktrace) = split ('::', $_, 4);
1082 # There might not have been a stacktrace.
1083 $stacktrace = '' unless defined $stacktrace;
1084 msg $cat, $loc, $msg,
1085 partial => ($stacktrace =~ /top level$/) + 0;
1086 for (split /\n/, $stacktrace)
1088 my ($loc, $trace) = split (': ', $_, 2);
1089 msg $cat, $loc, $trace, partial => ($trace !~ /top level$/) + 0;
1096 # Always produce traces, since even if the output is young enough,
1097 # there is no guarantee that the traces use the same *format*
1098 # (e.g., '-t FOO:foo' and '-t FOO:bar' are both using the same M4
1099 # traces, hence the M4 traces cache is usable, but its formatting
1100 # will yield different results).
1101 handle_traces ($req, $output, %trace);
1105 # Actual M4 expansion, if the user wants it, or if $output is old
1106 # (STDOUT is pretty old).
1107 handle_output ($req, $output)
1108 if $force || mtime ($output) < mtime ($ocache . $req->id);
1111 # If we ran up to here, the cache is valid.
1113 Autom4te::C4che->save ($icache_file, '@VERSION@');
1117 ### Setup "GNU" style for perl-mode and cperl-mode.
1119 ## perl-indent-level: 2
1120 ## perl-continued-statement-offset: 2
1121 ## perl-continued-brace-offset: 0
1122 ## perl-brace-offset: 0
1123 ## perl-brace-imaginary-offset: 0
1124 ## perl-label-offset: -2
1125 ## cperl-indent-level: 2
1126 ## cperl-brace-offset: 0
1127 ## cperl-continued-brace-offset: 0
1128 ## cperl-label-offset: -2
1129 ## cperl-extra-newline-before-brace: t
1130 ## cperl-merge-trailing-else: nil
1131 ## cperl-continued-statement-offset: 2