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-2016 Free Software Foundation, Inc.
11 # This program is free software: you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation, either version 3 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program. If not, see <http://www.gnu.org/licenses/>.
27 my $pkgdatadir = $ENV{'autom4te_perllibdir'} || '@pkgdatadir@';
28 unshift @INC, $pkgdatadir;
30 # Override SHELL. On DJGPP SHELL may not be set to a shell
31 # that can handle redirection and quote arguments correctly,
32 # e.g.: COMMAND.COM. For DJGPP always use the shell that configure
34 $ENV{'SHELL'} = '@SHELL@' if ($^O eq 'dos');
38 use Autom4te::ChannelDefs;
39 use Autom4te::Channels;
40 use Autom4te::FileUtils;
41 use Autom4te::General;
47 my $pkgdatadir = $ENV{'AC_MACRODIR'} || '@pkgdatadir@';
49 # $LANGUAGE{LANGUAGE} -- Automatic options for LANGUAGE.
54 # Mode of the output file except for traces.
57 # If melt, don't use frozen files.
60 # Names of the cache directory, cache directory index, trace cache
61 # prefix, and output cache prefix. And the IO object for the index.
68 my $flock_implemented = '@PERL_FLOCK@';
70 # The macros to trace mapped to their format, as specified by the
74 # The macros the user will want to trace in the future.
75 # We need 'include' to get the included file, 'm4_pattern_forbid' and
76 # 'm4_pattern_allow' to check the output.
78 # FIXME: What about 'sinclude'?
79 my @preselect = ('include',
80 'm4_pattern_allow', 'm4_pattern_forbid',
90 my $m4 = $ENV{"M4"} || '@M4@';
91 # Some non-GNU m4's don't reject the --help option, so give them /dev/null.
92 fatal "need GNU m4 1.4 or later: $m4"
93 if system "$m4 --help </dev/null 2>&1 | grep reload-state >/dev/null";
95 # Set some high recursion limit as the default limit, 250, has already
96 # been hit with AC_OUTPUT. Don't override the user's choice.
97 $m4 .= ' --nesting-limit=1024'
98 if " $m4 " !~ / (--nesting-limit(=[0-9]+)?|-L[0-9]*) /;
101 # @M4_BUILTIN -- M4 builtins and a useful comment.
102 my @m4_builtin = `echo dumpdef | $m4 2>&1 >/dev/null`;
103 map { s/:.*//;s/\W// } @m4_builtin;
106 # %M4_BUILTIN_ALTERNATE_NAME
107 # --------------------------
108 # The builtins are renamed, e.g., 'define' is renamed 'm4_define'.
109 # So map 'define' to 'm4_define' and conversely.
110 # Some macros don't follow this scheme: be sure to properly map to their
111 # alternate name too.
113 # FIXME: Trace status of renamed builtins was fixed in M4 1.4.5, which
114 # we now depend on; do we still need to do this mapping?
116 # So we will merge them, i.e., tracing 'BUILTIN' or tracing
117 # 'm4_BUILTIN' will be the same: tracing both, but honoring the
118 # *last* trace specification.
120 # FIXME: This is not enough: in the output '$0' will be 'BUILTIN'
121 # sometimes and 'm4_BUILTIN' at others. We should return a unique name,
122 # the one specified by the user.
124 # FIXME: To be absolutely rigorous, I would say that given that we
125 # _redefine_ divert (instead of _copying_ it), divert and the like
126 # should not be part of this list.
127 my %m4_builtin_alternate_name;
128 @m4_builtin_alternate_name{"$_", "m4_$_"} = ("m4_$_", "$_")
129 foreach (grep { !/m4wrap|m4exit|dnl|ifelse|__.*__/ } @m4_builtin);
130 @m4_builtin_alternate_name{"ifelse", "m4_if"} = ("m4_if", "ifelse");
131 @m4_builtin_alternate_name{"m4exit", "m4_exit"} = ("m4_exit", "m4exit");
132 @m4_builtin_alternate_name{"m4wrap", "m4_wrap"} = ("m4_wrap", "m4wrap");
137 $help = "Usage: $0 [OPTION]... [FILES]
139 Run GNU M4 on the FILES, avoiding useless runs. Output the traces if tracing,
140 the frozen file if freezing, otherwise the expansion of the FILES.
142 If some of the FILES are named 'FILE.m4f' they are considered to be M4
143 frozen files of all the previous files (which are therefore not loaded).
144 If 'FILE.m4f' is not found, then 'FILE.m4' will be used, together with
145 all the previous files.
147 Some files may be optional, i.e., will only be processed if found in the
148 include path, but then must end in '.m4?'; the question mark is not part
149 of the actual file name.
152 -h, --help print this help, then exit
153 -V, --version print version number, then exit
154 -v, --verbose verbosely report processing
155 -d, --debug don't remove temporary files
156 -o, --output=FILE save output in FILE (defaults to '-', stdout)
157 -f, --force don't rely on cached values
158 -W, --warnings=CATEGORY report the warnings falling in CATEGORY
159 -l, --language=LANG specify the set of M4 macros to use
160 -C, --cache=DIRECTORY preserve results for future runs in DIRECTORY
161 --no-cache disable the cache
162 -m, --mode=OCTAL change the non trace output file mode (0666)
163 -M, --melt don't use M4 frozen files
166 'Autoconf' create Autoconf configure scripts
167 'Autotest' create Autotest test suites
168 'M4sh' create M4sh shell scripts
169 'M4sugar' create M4sugar output
171 " . Autom4te::ChannelDefs::usage . "
173 The environment variables 'M4' and 'WARNINGS' are honored.
176 -B, --prepend-include=DIR prepend directory DIR to search path
177 -I, --include=DIR append directory DIR to search path
180 -t, --trace=MACRO[:FORMAT] report the MACRO invocations
181 -p, --preselect=MACRO prepare to trace MACRO in a future run
184 -F, --freeze produce an M4 frozen state file for FILES
186 FORMAT defaults to '\$f:\$l:\$n:\$%', and can use the following escapes:
188 \$f file where macro was called
189 \$l line where macro was called
190 \$d nesting depth of macro call
191 \$n name of the macro
192 \$NUM argument NUM, unquoted and with newlines
193 \$SEP\@ all arguments, with newlines, quoted, and separated by SEP
194 \$SEP* all arguments, with newlines, unquoted, and separated by SEP
195 \$SEP% all arguments, without newlines, unquoted, and separated by SEP
196 SEP can be empty for the default (comma for \@ and *, colon for %),
197 a single character for that character, or {STRING} to use a string.
199 Report bugs to <bug-autoconf\@gnu.org>.
200 GNU Autoconf home page: <http://www.gnu.org/software/autoconf/>.
201 General help using GNU software: <http://www.gnu.org/gethelp/>.
207 autom4te (@PACKAGE_NAME@) @VERSION@
208 Copyright (C) @RELEASE_YEAR@ Free Software Foundation, Inc.
209 License GPLv3+/Autoconf: GNU GPL version 3 or later
210 <http://gnu.org/licenses/gpl.html>, <http://gnu.org/licenses/exceptions.html>
211 This is free software: you are free to change and redistribute it.
212 There is NO WARRANTY, to the extent permitted by law.
214 Written by Akim Demaille.
224 # files_to_options (@FILE)
225 # ------------------------
226 # Transform Autom4te conventions (e.g., using foo.m4f to designate a frozen
227 # file) into a suitable command line for M4 (e.g., using --reload-state).
228 # parse_args guarantees that we will see at most one frozen file, and that
229 # if a frozen file is present, it is the first argument.
230 sub files_to_options (@)
234 foreach my $file (@file)
236 my $arg = shell_quote ($file);
237 if ($file =~ /\.m4f$/)
239 $arg = "--reload-state=$arg";
240 # If the user downgraded M4 from 1.6 to 1.4.x after freezing
241 # the file, then we ensure the frozen __m4_version__ will
242 # not cause m4_init to make the wrong decision about the
243 # current M4 version.
244 $arg .= " --undefine=__m4_version__"
245 unless grep {/__m4_version__/} @m4_builtin;
249 return join ' ', @res;
253 # load_configuration ($FILE)
254 # --------------------------
255 # Load the configuration $FILE.
256 sub load_configuration ($)
259 use Text::ParseWords;
261 my $cfg = new Autom4te::XFile ($file, "<");
263 while ($_ = $cfg->getline)
270 my @words = shellwords ($_);
271 my $type = shift @words;
272 if ($type eq 'begin-language:')
274 fatal "$file:$.: end-language missing for: $lang"
276 $lang = lc $words[0];
278 elsif ($type eq 'end-language:')
280 error "$file:$.: end-language mismatch: $lang"
281 if $lang ne lc $words[0];
284 elsif ($type eq 'args:')
286 fatal "$file:$.: no current language"
287 unless defined $lang;
288 push @{$language{$lang}}, @words;
292 error "$file:$.: unknown directive: $type";
300 # Process any command line arguments.
303 # We want to look for the early options, which should not be found
304 # in the configuration file. Prepend to the user arguments.
305 # Perform this repeatedly so that we can use --language in language
306 # definitions. Beware that there can be several --language
312 Getopt::Long::Configure ("pass_through", "permute");
313 GetOptions ("l|language=s" => \@language);
317 error "unknown language: $_"
318 unless exists $language{lc $_};
319 unshift @ARGV, @{$language{lc $_}};
323 # --debug is useless: it is parsed below.
324 if (exists $ENV{'AUTOM4TE_DEBUG'})
326 print STDERR "$me: concrete arguments:\n";
327 foreach my $arg (@ARGV)
329 print STDERR "| $arg\n";
333 # Process the arguments for real this time.
340 "o|output=s" => \$output,
341 "W|warnings=s" => \&parse_warnings,
342 "m|mode=s" => \$mode,
345 # Library directories:
346 "B|prepend-include=s" => \@prepend_include,
347 "I|include=s" => \@include,
350 # Using a hash for traces is seducing. Unfortunately, upon '-t FOO',
351 # instead of mapping 'FOO' to undef, Getopt maps it to '1', preventing
352 # us from distinguishing '-t FOO' from '-t FOO=1'. So let's do it
354 "t|trace=s" => \@trace,
355 "p|preselect=s" => \@preselect,
358 "F|freeze" => \$freeze,
361 "C|cache=s" => \$cache,
362 "no-cache" => sub { $cache = undef; },
365 fatal "too few arguments
366 Try '$me --help' for more information."
370 # We cannot trace at the same time (well, we can, but it sounds insane).
371 # And it implies melting: there is risk not to update properly using
372 # old frozen files, and worse yet: we could load a frozen file and
373 # refreeze it! A sort of caching :)
374 fatal "cannot freeze and trace"
375 if $freeze && @trace;
379 # Names of the cache directory, cache directory index, trace cache
380 # prefix, and output cache prefix. If the cache is not to be
381 # preserved, default to a temporary directory (automatically removed
385 $icache = "$cache/requests";
386 $tcache = "$cache/traces.";
387 $ocache = "$cache/output.";
389 # Normalize the includes: the first occurrence is enough, several is
390 # a pain since it introduces a useless difference in the path which
391 # invalidates the cache. And strip '.' which is implicit and always
393 @include = grep { !/^\.$/ } uniq (reverse(@prepend_include), @include);
395 # Convert @trace to %trace, and work around the M4 builtins tracing
397 # The default format is '$f:$l:$n:$%'.
400 /^([^:]+)(?::(.*))?$/ms;
401 $trace{$1} = defined $2 ? $2 : '$f:$l:$n:$%';
402 $trace{$m4_builtin_alternate_name{$1}} = $trace{$1}
403 if exists $m4_builtin_alternate_name{$1};
406 # Work around the M4 builtins tracing problem for @PRESELECT.
407 # FIXME: Is this still needed, now that we rely on M4 1.4.5?
409 map { $m4_builtin_alternate_name{$_} }
410 grep { exists $m4_builtin_alternate_name{$_} } @preselect);
412 # If we find frozen files, then all the files before it are
413 # discarded: the frozen file is supposed to include them all.
415 # We don't want to depend upon m4's --include to find the top level
416 # files, so we use 'find_file' here. Try to get a canonical name,
417 # as it's part of the key for caching. And some files are optional
418 # (also handled by 'find_file').
428 # Frozen files are optional => pass a '?' to 'find_file'.
429 my $file = find_file ("$_?", @include);
437 push @argv, find_file ($_, @include);
442 my $file = find_file ($_, @include);
451 # handle_m4 ($REQ, @MACRO)
452 # ------------------------
453 # Run m4 on the input files, and save the traces on the @MACRO.
456 my ($req, @macro) = @_;
458 # GNU m4 appends when using --debugfile/--error-output.
459 unlink ($tcache . $req->id . "t");
463 # We don't output directly to the cache files, to avoid problems
464 # when we are interrupted (that leaves corrupted files).
465 xsystem ("$m4 @M4_GNU@"
466 . join (' --include=', '', map { shell_quote ($_) } @include)
468 . (!exists $ENV{'AUTOM4TE_NO_FATAL'} ? ' --fatal-warning' : '')
469 . " @M4_DEBUGFILE@=" . shell_quote ("$tcache" . $req->id . "t")
470 . join (' --trace=', '', map { shell_quote ($_) } sort @macro)
471 . " " . files_to_options (@ARGV)
472 . " > " . shell_quote ("$ocache" . $req->id . "t"));
474 # Everything went ok: preserve the outputs.
475 foreach my $file (map { $_ . $req->id } ($tcache, $ocache))
478 move ("${file}t", "$file")
479 or fatal "cannot rename ${file}t as $file: $!";
484 # warn_forbidden ($WHERE, $WORD, %FORBIDDEN)
485 # ------------------------------------------
486 # $WORD is forbidden. Warn with a dedicated error message if in
487 # %FORBIDDEN, otherwise a simple 'error: possibly undefined macro'
489 my $first_warn_forbidden = 1;
490 sub warn_forbidden ($$%)
492 my ($where, $word, %forbidden) = @_;
495 for my $re (sort keys %forbidden)
499 $message = $forbidden{$re};
503 $message ||= "possibly undefined macro: $word";
504 warn "$where: error: $message\n";
505 if ($first_warn_forbidden)
508 If this token and others are legitimate, please use m4_pattern_allow.
509 See the Autoconf documentation.
511 $first_warn_forbidden = 0;
516 # handle_output ($REQ, $OUTPUT)
517 # -----------------------------
518 # Run m4 on the input files, perform quadrigraphs substitution, check for
519 # forbidden tokens, and save into $OUTPUT.
520 sub handle_output ($$)
522 my ($req, $output) = @_;
524 verb "creating $output";
526 # Load the forbidden/allowed patterns.
527 handle_traces ($req, "$tmp/patterns",
528 ('m4_pattern_forbid' => 'forbid:$1:$2',
529 'm4_pattern_allow' => 'allow:$1'));
530 my @patterns = new Autom4te::XFile ("$tmp/patterns", "<")->getlines;
533 map { /^forbid:([^:]+):.+$/ => /^forbid:[^:]+:(.+)$/ } @patterns;
534 my $forbidden = join ('|', map { /^forbid:([^:]+)/ } @patterns) || "^\$";
535 my $allowed = join ('|', map { /^allow:([^:]+)/ } @patterns) || "^\$";
537 verb "forbidden tokens: $forbidden";
538 verb "forbidden token : $_ => $forbidden{$_}"
539 foreach (sort keys %forbidden);
540 verb "allowed tokens: $allowed";
542 # Read the (cached) raw M4 output, produce the actual result. We
543 # have to use the 2nd arg to have Autom4te::XFile honor the third, but then
544 # stdout is to be handled by hand :(. Don't use fdopen as it means
545 # we will close STDOUT, which we already do in END.
546 my $out = new Autom4te::XFile;
549 $out->open (">$output");
553 $out->open ($output, O_CREAT | O_WRONLY | O_TRUNC, oct ($mode));
555 fatal "cannot create $output: $!"
557 my $in = new Autom4te::XFile ($ocache . $req->id, "<");
561 while ($_ = $in->getline)
574 # Don't complain in comments. Well, until we have something
575 # better, don't consider '#include' etc. are comments.
577 unless /^\#\s*(if|include|endif|ifdef|ifndef|define)\b/;
578 foreach (split (/\W+/))
581 if !/^$/ && /$forbidden/o && !/$allowed/o && ! exists $prohibited{$_};
584 # Performed *last*: the empty quadrigraph.
592 # If no forbidden words, we're done.
596 # Locate the forbidden words in the last input file.
597 # This is unsatisfying but...
599 if ($ARGV[$#ARGV] ne '-')
601 my $prohibited = '\b(' . join ('|', keys %prohibited) . ')\b';
602 my $file = new Autom4te::XFile ($ARGV[$#ARGV], "<");
604 while ($_ = $file->getline)
606 # Don't complain in comments. Well, until we have something
607 # better, don't consider '#include' etc. to be comments.
609 unless /^\#(if|include|endif|ifdef|ifndef|define)\b/;
611 # Complain once per word, but possibly several times per line.
612 while (/$prohibited/)
615 warn_forbidden ("$ARGV[$#ARGV]:$.", $word, %forbidden);
616 delete $prohibited{$word};
617 # If we're done, exit.
620 $prohibited = '\b(' . join ('|', keys %prohibited) . ')\b';
624 warn_forbidden ("$output:$prohibited{$_}", $_, %forbidden)
625 foreach (sort { $prohibited{$a} <=> $prohibited{$b} } keys %prohibited);
629 ## --------------------- ##
630 ## Handling the traces. ##
631 ## --------------------- ##
635 # trace_format_to_m4 ($FORMAT)
636 # ----------------------------
637 # Convert a trace $FORMAT into a M4 trace processing macro's body.
638 sub trace_format_to_m4 ($)
642 my %escape = (# File name.
648 # Name (also available as $0).
660 $res .= "\$" . ($1 + 4);
662 # $x, no separator given.
663 elsif (s/^\$([fldn\$])//)
668 elsif (s/^\$\{([^}]*)\}([@*%])//
669 || s/^\$(.?)([@*%])//)
671 # $@, list of quoted effective arguments.
674 $res .= ']at_at([' . ($1 ? $1 : ',') . '], $@)[';
676 # $*, list of unquoted effective arguments.
679 $res .= ']at_star([' . ($1 ? $1 : ',') . '], $@)[';
681 # $%, list of flattened unquoted effective arguments.
684 $res .= ']at_percent([' . ($1 ? $1 : ':') . '], $@)[';
689 error "invalid escape: $1";
699 return '[[' . $res . ']]';
703 # handle_traces($REQ, $OUTPUT, %TRACE)
704 # ------------------------------------
705 # We use M4 itself to process the traces. But to avoid name clashes when
706 # processing the traces, the builtins are disabled, and moved into 'at_'.
707 # Actually, all the low level processing macros are in 'at_' (and '_at_').
708 # To avoid clashes between user macros and 'at_' macros, the macros which
709 # implement tracing are in 'AT_'.
711 # Having $REQ is needed to neutralize the macros which have been traced,
712 # but are not wanted now.
713 sub handle_traces ($$%)
715 my ($req, $output, %trace) = @_;
717 verb "formatting traces for '$output': " . join (', ', sort keys %trace);
719 # Processing the traces.
720 my $trace_m4 = new Autom4te::XFile ("$tmp/traces.m4", ">");
725 # _at_MODE(SEPARATOR, ELT1, ELT2...)
726 # ----------------------------------
727 # List the elements, separating then with SEPARATOR.
729 # 'at' -- the elements are enclosed in brackets.
730 # 'star' -- the elements are listed as are.
731 # 'percent' -- the elements are 'flattened': spaces are singled out,
732 # and no new line remains.
734 [at_ifelse([$#], [1], [],
736 [[[$2]][$1]$0([$1], at_shift(at_shift($@)))])])
738 define([_at_percent],
739 [at_ifelse([$#], [1], [],
740 [$#], [2], [at_flatten([$2])],
741 [at_flatten([$2])[$1]$0([$1], at_shift(at_shift($@)))])])
744 [at_ifelse([$#], [1], [],
746 [[$2][$1]$0([$1], at_shift(at_shift($@)))])])
748 # FLATTEN quotes its result.
749 # Note that the second pattern is 'newline, tab or space'. Don't lose
752 [at_patsubst(at_patsubst([[[$1]]], [\\\n]), [[\n\t ]+], [ ])])
754 define([at_args], [at_shift(at_shift(at_shift(at_shift(at_shift($@)))))])
755 define([at_at], [_$0([$1], at_args($@))])
756 define([at_percent], [_$0([$1], at_args($@))])
757 define([at_star], [_$0([$1], at_args($@))])
760 s/^ //mg;s/\\t/\t/mg;s/\\n/\n/mg;
763 # If you trace 'define', then on 'define([m4_exit], defn([m4exit])' you
766 # AT_define([m4sugar.m4], [115], [1], [define], [m4_exit], <m4exit>)
768 # Since '<m4exit>' is not quoted, the outer m4, when processing
769 # 'trace.m4' will exit prematurely. Hence, move all the builtins to
770 # the 'at_' name space.
772 print $trace_m4 "# Copy the builtins.\n";
773 map { print $trace_m4 "define([at_$_], defn([$_]))\n" } @m4_builtin;
774 print $trace_m4 "\n";
776 print $trace_m4 "# Disable them.\n";
777 map { print $trace_m4 "at_undefine([$_])\n" } @m4_builtin;
778 print $trace_m4 "\n";
781 # Neutralize traces: we don't want traces of cached requests (%REQUEST).
783 "## -------------------------------------- ##\n",
784 "## By default neutralize all the traces. ##\n",
785 "## -------------------------------------- ##\n",
787 print $trace_m4 "at_define([AT_$_], [at_dnl])\n"
788 foreach (sort keys %{$req->macro});
789 print $trace_m4 "\n";
791 # Implement traces for current requests (%TRACE).
793 "## ------------------------- ##\n",
794 "## Trace processing macros. ##\n",
795 "## ------------------------- ##\n",
797 foreach (sort keys %trace)
799 # Trace request can be embed \n.
800 (my $comment = "Trace $_:$trace{$_}") =~ s/^/\# /;
801 print $trace_m4 "$comment\n";
802 print $trace_m4 "at_define([AT_$_],\n";
803 print $trace_m4 trace_format_to_m4 ($trace{$_}) . ")\n\n";
805 print $trace_m4 "\n";
808 print $trace_m4 "at_divert(0)at_dnl\n";
810 # Transform the traces from m4 into an m4 input file.
811 # Typically, transform:
813 # | m4trace:configure.ac:3: -1- AC_SUBST([exec_prefix], [NONE])
817 # | AT_AC_SUBST([configure.ac], [3], [1], [AC_SUBST], [exec_prefix], [NONE])
819 # Pay attention that the file name might include colons, if under DOS
820 # for instance, so we don't use '[^:]+'.
821 my $traces = new Autom4te::XFile ($tcache . $req->id, "<");
822 while ($_ = $traces->getline)
824 # Trace with arguments, as the example above. We don't try
825 # to match the trailing parenthesis as it might be on a
827 s{^m4trace:(.+):(\d+): -(\d+)- ([^(]+)\((.*)$}
828 {AT_$4([$1], [$2], [$3], [$4], $5};
829 # Traces without arguments, always on a single line.
830 s{^m4trace:(.+):(\d+): -(\d+)- ([^)]*)\n$}
831 {AT_$4([$1], [$2], [$3], [$4])\n};
832 print $trace_m4 "$_";
836 my $in = new Autom4te::XFile ("$m4 " . shell_quote ("$tmp/traces.m4") . " |");
837 my $out = new Autom4te::XFile;
840 $out->open (">$output");
844 $out->open ($output, ">");
847 # This is dubious: should we really transform the quadrigraphs in
848 # traces? It might break balanced [ ] etc. in the output. The
849 # consensus seems to be that traces are more useful this way.
850 while ($_ = $in->getline)
852 # It makes no sense to try to transform __oline__.
868 # Are the cache files of $REQ up to date?
869 # $REQ is 'valid' if it corresponds to the request and exists, which
870 # does not mean it is up to date. It is up to date if, in addition,
871 # its files are younger than its dependencies.
879 my $tfile = $tcache . $req->id;
880 my $ofile = $ocache . $req->id;
882 # We can't answer properly if the traces are not computed since we
883 # need to know what other files were included. Actually, if any of
884 # the cache files is missing, we are not up to date.
886 if ! -f $tfile || ! -f $ofile;
888 # The youngest of the cache files must be older than the oldest of
890 my $tmtime = mtime ($tfile);
891 my $omtime = mtime ($ofile);
892 my ($file, $mtime) = ($tmtime < $omtime
893 ? ($ofile, $omtime) : ($tfile, $tmtime));
895 # We depend at least upon the arguments.
898 # stdin is always out of date.
899 if (grep { $_ eq '-' } @dep)
902 # Files may include others. We can use traces since we just checked
903 # if they are available.
904 handle_traces ($req, "$tmp/dependencies",
906 'm4_include' => '$1'));
907 my $deps = new Autom4te::XFile ("$tmp/dependencies", "<");
908 while ($_ = $deps->getline)
911 my $file = find_file ("$_?", @include);
912 # If a file which used to be included is no longer there, then
913 # don't say it's missing (it might no longer be included). But
914 # of course, that causes the output to be outdated (as if the
915 # time stamp of that missing file was newer).
921 # If $FILE is younger than one of its dependencies, it is outdated.
922 return up_to_date_p ($file, @dep);
936 # When processing the file with diversion disabled, there must be no
937 # output but comments and empty lines.
938 my $result = xqx ("$m4"
940 . join (' --include=', '', map { shell_quote ($_) } @include)
942 . " " . files_to_options (@ARGV)
944 $result =~ s/#.*\n//g;
945 $result =~ s/^\n//mg;
947 fatal "freezing produced output:\n$result"
950 # If freezing produces output, something went wrong: a bad 'divert',
951 # or an improper paren etc.
954 . join (' --include=', '', map { shell_quote ($_) } @include)
955 . " --freeze-state=" . shell_quote ($output)
956 . " " . files_to_options (@ARGV)
965 load_configuration ($ENV{'AUTOM4TE_CFG'} || "$pkgdatadir/autom4te.cfg");
966 load_configuration ("$ENV{'HOME'}/.autom4te.cfg")
967 if exists $ENV{'HOME'} && -f "$ENV{'HOME'}/.autom4te.cfg";
968 load_configuration (".autom4te.cfg")
969 if -f ".autom4te.cfg";
972 # Freezing does not involve the cache.
979 # We need our cache directory. Don't fail with parallel creation.
984 or fatal "cannot create $cache: $!";
987 # Open the index for update, and lock it. autom4te handles several
988 # files, but the index is the first and last file to be updated, so
989 # locking it is sufficient.
990 $icache_file = new Autom4te::XFile $icache, O_RDWR|O_CREAT;
991 $icache_file->lock (LOCK_EX)
992 if ($flock_implemented eq "yes");
994 # Read the cache index if available and older than autom4te itself.
995 # If autom4te is younger, then some structures such as C4che might
996 # have changed, which would corrupt its processing.
997 Autom4te::C4che->load ($icache_file)
998 if -f $icache && mtime ($icache) > mtime ($0);
1000 # Add the new trace requests.
1001 my $req = Autom4te::C4che->request ('input' => \@ARGV,
1002 'path' => \@include,
1003 'macro' => [keys %trace, @preselect]);
1005 # If $REQ's cache files are not up to date, or simply if the user
1006 # discarded them (-f), declare it invalid.
1008 if $force || ! up_to_date ($req);
1010 # We now know whether we can trust the Request object. Say it.
1011 verb "the trace request object is:\n" . $req->marshall;
1013 # We need to run M4 if (i) the user wants it (--force), (ii) $REQ is
1015 handle_m4 ($req, keys %{$req->macro})
1016 if $force || ! $req->valid;
1018 # Issue the warnings each time autom4te was run.
1019 my $separator = "\n" . ('-' x 25) . " END OF WARNING " . ('-' x 25) . "\n\n";
1020 handle_traces ($req, "$tmp/warnings",
1021 ('_m4_warn' => "\$1::\$f:\$l::\$2::\$3$separator"));
1022 # Swallow excessive newlines.
1023 for (split (/\n*$separator\n*/o, contents ("$tmp/warnings")))
1025 # The message looks like:
1026 # | syntax::input.as:5::ouch
1027 # | ::input.as:4: baz is expanded from...
1028 # | input.as:2: bar is expanded from...
1029 # | input.as:3: foo is expanded from...
1030 # | input.as:5: the top level
1031 # In particular, m4_warn guarantees that either $stackdump is empty, or
1032 # it consists of lines where only the last line ends in "top level".
1033 my ($cat, $loc, $msg, $stacktrace) = split ('::', $_, 4);
1034 msg $cat, $loc, "warning: $msg",
1035 partial => ($stacktrace =~ /top level$/) + 0;
1036 for (split /\n/, $stacktrace)
1038 my ($loc, $trace) = split (': ', $_, 2);
1039 msg $cat, $loc, $trace, partial => ($trace !~ /top level$/) + 0;
1046 # Always produce traces, since even if the output is young enough,
1047 # there is no guarantee that the traces use the same *format*
1048 # (e.g., '-t FOO:foo' and '-t FOO:bar' are both using the same M4
1049 # traces, hence the M4 traces cache is usable, but its formatting
1050 # will yield different results).
1051 handle_traces ($req, $output, %trace);
1055 # Actual M4 expansion, if the user wants it, or if $output is old
1056 # (STDOUT is pretty old).
1057 handle_output ($req, $output)
1058 if $force || mtime ($output) < mtime ($ocache . $req->id);
1061 # If we ran up to here, the cache is valid.
1063 Autom4te::C4che->save ($icache_file);
1067 ### Setup "GNU" style for perl-mode and cperl-mode.
1069 ## perl-indent-level: 2
1070 ## perl-continued-statement-offset: 2
1071 ## perl-continued-brace-offset: 0
1072 ## perl-brace-offset: 0
1073 ## perl-brace-imaginary-offset: 0
1074 ## perl-label-offset: -2
1075 ## cperl-indent-level: 2
1076 ## cperl-brace-offset: 0
1077 ## cperl-continued-brace-offset: 0
1078 ## cperl-label-offset: -2
1079 ## cperl-extra-newline-before-brace: t
1080 ## cperl-merge-trailing-else: nil
1081 ## cperl-continued-statement-offset: 2